]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_tpm.c
tpm: Check that parse_byte_string() has data to parse
[karo-tx-uboot.git] / common / cmd_tpm.c
index c34000a6ebe7d4d0d5fbf9fabb857c7e815a5077..e9c661821ce747e41ec099ba081fc32697035da6 100644 (file)
@@ -1,27 +1,12 @@
 /*
  * Copyright (c) 2013 The Chromium OS Authors.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <command.h>
+#include <dm.h>
 #include <malloc.h>
 #include <tpm.h>
 #include <asm/unaligned.h>
@@ -73,6 +58,8 @@ static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
        size_t count, length;
        int i;
 
+       if (!bytes)
+               return NULL;
        length = strlen(bytes);
        count = length / 2;
 
@@ -95,17 +82,19 @@ static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
 }
 
 /**
- * Convert TPM command return code to U-Boot command error codes.
+ * report_return_code() - Report any error and return failure or success
  *
  * @param return_code  TPM command return code
  * @return value of enum command_ret_t
  */
-static int convert_return_code(uint32_t return_code)
+static int report_return_code(int return_code)
 {
-       if (return_code)
+       if (return_code) {
+               printf("Error: %d\n", return_code);
                return CMD_RET_FAILURE;
-       else
+       } else {
                return CMD_RET_SUCCESS;
+       }
 }
 
 /**
@@ -267,7 +256,7 @@ static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_FAILURE;
        }
 
-       return convert_return_code(tpm_startup(mode));
+       return report_return_code(tpm_startup(mode));
 }
 
 static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
@@ -281,7 +270,7 @@ static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
        perm = simple_strtoul(argv[2], NULL, 0);
        size = simple_strtoul(argv[3], NULL, 0);
 
-       return convert_return_code(tpm_nv_define_space(index, perm, size));
+       return report_return_code(tpm_nv_define_space(index, perm, size));
 }
 
 static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
@@ -302,7 +291,7 @@ static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(data, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
@@ -324,7 +313,7 @@ static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
        rc = tpm_nv_write_value(index, data, count);
        free(data);
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
@@ -347,7 +336,7 @@ static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(out_digest, sizeof(out_digest));
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
@@ -368,7 +357,7 @@ static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(data, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
@@ -380,7 +369,7 @@ static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_USAGE;
        presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
 
-       return convert_return_code(tpm_tsc_physical_presence(presence));
+       return report_return_code(tpm_tsc_physical_presence(presence));
 }
 
 static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
@@ -400,7 +389,7 @@ static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(data, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
@@ -412,7 +401,7 @@ static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_USAGE;
        state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
 
-       return convert_return_code(tpm_physical_set_deactivated(state));
+       return report_return_code(tpm_physical_set_deactivated(state));
 }
 
 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
@@ -435,7 +424,7 @@ static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
                print_byte_string(cap, count);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 #define TPM_COMMAND_NO_ARG(cmd)                                \
@@ -444,7 +433,7 @@ static int do_##cmd(cmd_tbl_t *cmdtp, int flag,             \
 {                                                      \
        if (argc != 1)                                  \
                return CMD_RET_USAGE;                   \
-       return convert_return_code(cmd());              \
+       return report_return_code(cmd());               \
 }
 
 TPM_COMMAND_NO_ARG(tpm_init)
@@ -454,6 +443,21 @@ TPM_COMMAND_NO_ARG(tpm_force_clear)
 TPM_COMMAND_NO_ARG(tpm_physical_enable)
 TPM_COMMAND_NO_ARG(tpm_physical_disable)
 
+#ifdef CONFIG_DM_TPM
+static int get_tpm(struct udevice **devp)
+{
+       int rc;
+
+       rc = uclass_first_device(UCLASS_TPM, devp);
+       if (rc) {
+               printf("Could not find TPM (ret=%d)\n", rc);
+               return CMD_RET_FAILURE;
+       }
+
+       return 0;
+}
+#endif
+
 static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
                int argc, char * const argv[])
 {
@@ -468,14 +472,24 @@ static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
                return CMD_RET_FAILURE;
        }
 
+#ifdef CONFIG_DM_TPM
+       struct udevice *dev;
+
+       rc = get_tpm(&dev);
+       if (rc)
+               return rc;
+
+       rc = tpm_xfer(dev, command, count, response, &response_length);
+#else
        rc = tis_sendrecv(command, count, response, &response_length);
+#endif
        free(command);
        if (!rc) {
                puts("tpm response:\n");
                print_byte_string(response, response_length);
        }
 
-       return convert_return_code(rc);
+       return report_return_code(rc);
 }
 
 static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
@@ -493,7 +507,7 @@ static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
        index = simple_strtoul(argv[2], NULL, 0);
        perm = simple_strtoul(argv[3], NULL, 0);
 
-       return convert_return_code(tpm_nv_define_space(index, perm, size));
+       return report_return_code(tpm_nv_define_space(index, perm, size));
 }
 
 static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
@@ -522,7 +536,7 @@ static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
        }
        free(data);
 
-       return convert_return_code(err);
+       return report_return_code(err);
 }
 
 static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
@@ -550,7 +564,7 @@ static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
        err = tpm_nv_write_value(index, data, count);
        free(data);
 
-       return convert_return_code(err);
+       return report_return_code(err);
 }
 
 #ifdef CONFIG_TPM_AUTH_SESSIONS
@@ -562,7 +576,7 @@ static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
 
        err = tpm_oiap(&auth_handle);
 
-       return convert_return_code(err);
+       return report_return_code(err);
 }
 
 static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
@@ -587,7 +601,7 @@ static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
        if (!err)
                printf("Key handle is 0x%x\n", key_handle);
 
-       return convert_return_code(err);
+       return report_return_code(err);
 }
 
 static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
@@ -612,7 +626,7 @@ static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
                printf("dump of received pub key structure:\n");
                print_byte_string(pub_key_buffer, pub_key_len);
        }
-       return convert_return_code(err);
+       return report_return_code(err);
 }
 
 TPM_COMMAND_NO_ARG(tpm_end_oiap)