moveconfig: Correct pylint errors
[platform/kernel/u-boot.git] / cmd / tpm-common.c
index 89f2aa0..47adaff 100644 (file)
@@ -6,11 +6,15 @@
 #include <common.h>
 #include <command.h>
 #include <dm.h>
+#include <env.h>
+#include <malloc.h>
 #include <asm/unaligned.h>
 #include <linux/string.h>
 #include <tpm-common.h>
 #include "tpm-user-utils.h"
 
+static struct udevice *tpm_dev;
+
 /**
  * Print a byte string in hexdecimal format, 16-bytes per line.
  *
@@ -42,7 +46,7 @@ void print_byte_string(u8 *data, size_t count)
  *                     NULL is passed, a large enough buffer will be allocated,
  *                     and the caller must free it.
  * @param count_ptr    output variable for the length of byte string
- * @return pointer to output buffer
+ * Return: pointer to output buffer
  */
 void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
 {
@@ -64,7 +68,7 @@ void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
        for (i = 0; i < length; i += 2) {
                byte[0] = bytes[i];
                byte[1] = bytes[i + 1];
-               data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
+               data[i / 2] = (u8)hextoul(byte, NULL);
        }
 
        if (count_ptr)
@@ -77,7 +81,7 @@ void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
  * 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
+ * Return: value of enum command_ret_t
  */
 int report_return_code(int return_code)
 {
@@ -93,7 +97,7 @@ int report_return_code(int return_code)
  * Return number of values defined by a type string.
  *
  * @param type_str     type string
- * @return number of values of type string
+ * Return: number of values of type string
  */
 int type_string_get_num_values(const char *type_str)
 {
@@ -104,7 +108,7 @@ int type_string_get_num_values(const char *type_str)
  * Return total size of values defined by a type string.
  *
  * @param type_str     type string
- * @return total size of values of type string, or 0 if type string
+ * Return: total size of values of type string, or 0 if type string
  *  contains illegal type character.
  */
 size_t type_string_get_space_size(const char *type_str)
@@ -136,7 +140,7 @@ size_t type_string_get_space_size(const char *type_str)
  *
  * @param type_str     type string
  * @param count                pointer for storing size of buffer
- * @return pointer to buffer or NULL on error
+ * Return: pointer to buffer or NULL on error
  */
 void *type_string_alloc(const char *type_str, u32 *count)
 {
@@ -160,7 +164,7 @@ void *type_string_alloc(const char *type_str, u32 *count)
  * @param type_str     type string
  * @param values       text strings of values to be packed
  * @param data         output buffer of values
- * @return 0 on success, non-0 on error
+ * Return: 0 on success, non-0 on error
  */
 int type_string_pack(const char *type_str, char * const values[],
                     u8 *data)
@@ -198,7 +202,7 @@ int type_string_pack(const char *type_str, char * const values[],
  * @param type_str     type string
  * @param data         input buffer of values
  * @param vars         names of environment variables
- * @return 0 on success, non-0 on error
+ * Return: 0 on success, non-0 on error
  */
 int type_string_write_vars(const char *type_str, u8 *data,
                           char * const vars[])
@@ -230,20 +234,87 @@ int type_string_write_vars(const char *type_str, u8 *data,
        return 0;
 }
 
+static int tpm_show_device(void)
+{
+       struct udevice *dev;
+       char buf[80];
+       int n = 0, rc;
+
+       for_each_tpm_device(dev) {
+               rc = tpm_get_desc(dev, buf, sizeof(buf));
+               if (rc < 0)
+                       printf("device %d: can't get info\n", n);
+               else
+                       printf("device %d: %s\n", n, buf);
+
+               n++;
+       };
+
+       return 0;
+}
+
+static int tpm_set_device(unsigned long num)
+{
+       struct udevice *dev;
+       unsigned long n = 0;
+       int rc = CMD_RET_FAILURE;
+
+       for_each_tpm_device(dev) {
+               if (n == num) {
+                       rc = 0;
+                       break;
+               }
+
+               n++;
+       }
+
+       if (!rc)
+               tpm_dev = dev;
+
+       return rc;
+}
+
 int get_tpm(struct udevice **devp)
 {
        int rc;
 
-       rc = uclass_first_device_err(UCLASS_TPM, devp);
-       if (rc) {
-               printf("Could not find TPM (ret=%d)\n", rc);
-               return CMD_RET_FAILURE;
+       /*
+        * To keep a backward compatibility with previous code,
+        * if a tpm device is not explicitly set, we set the first one.
+        */
+       if (!tpm_dev) {
+               rc = tpm_set_device(0);
+               if (rc) {
+                       printf("Couldn't set TPM 0 (rc = %d)\n", rc);
+                       return CMD_RET_FAILURE;
+               }
        }
 
+       if (devp)
+               *devp = tpm_dev;
+
        return 0;
 }
 
-int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+int do_tpm_device(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+       unsigned long num;
+       int rc;
+
+       if (argc == 2) {
+               num = dectoul(argv[1], NULL);
+
+               rc = tpm_set_device(num);
+               if (rc)
+                       printf("Couldn't set TPM %lu (rc = %d)\n", num, rc);
+       } else {
+               rc = tpm_show_device();
+       }
+
+       return rc;
+}
+
+int do_tpm_info(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
        struct udevice *dev;
        char buf[80];
@@ -262,7 +333,7 @@ int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
        return 0;
 }
 
-int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int do_tpm_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
        struct udevice *dev;
        int rc;
@@ -276,9 +347,9 @@ int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        return report_return_code(tpm_init(dev));
 }
 
-int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int do_tpm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-       cmd_tbl_t *tpm_commands, *cmd;
+       struct cmd_tbl *tpm_commands, *cmd;
        struct tpm_chip_priv *priv;
        struct udevice *dev;
        unsigned int size;