binman: Handle repeated bytes for Python 3
[oweals/u-boot.git] / cmd / avb.c
index ff00be4cee38e9d82f473776c64b3f9a01fac47b..c5af4a2e461d4ac224ab2410b846aec8ab5f6a4b 100644 (file)
--- a/cmd/avb.c
+++ b/cmd/avb.c
@@ -340,6 +340,76 @@ int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
        return CMD_RET_FAILURE;
 }
 
+int do_avb_read_pvalue(cmd_tbl_t *cmdtp, int flag, int argc,
+                      char * const argv[])
+{
+       const char *name;
+       size_t bytes;
+       size_t bytes_read;
+       void *buffer;
+       char *endp;
+
+       if (!avb_ops) {
+               printf("AVB 2.0 is not initialized, run 'avb init' first\n");
+               return CMD_RET_FAILURE;
+       }
+
+       if (argc != 3)
+               return CMD_RET_USAGE;
+
+       name = argv[1];
+       bytes = simple_strtoul(argv[2], &endp, 10);
+       if (*endp && *endp != '\n')
+               return CMD_RET_USAGE;
+
+       buffer = malloc(bytes);
+       if (!buffer)
+               return CMD_RET_FAILURE;
+
+       if (avb_ops->read_persistent_value(avb_ops, name, bytes, buffer,
+                                          &bytes_read) == AVB_IO_RESULT_OK) {
+               printf("Read %ld bytes, value = %s\n", bytes_read,
+                      (char *)buffer);
+               free(buffer);
+               return CMD_RET_SUCCESS;
+       }
+
+       printf("Failed to read persistent value\n");
+
+       free(buffer);
+
+       return CMD_RET_FAILURE;
+}
+
+int do_avb_write_pvalue(cmd_tbl_t *cmdtp, int flag, int argc,
+                       char * const argv[])
+{
+       const char *name;
+       const char *value;
+
+       if (!avb_ops) {
+               printf("AVB 2.0 is not initialized, run 'avb init' first\n");
+               return CMD_RET_FAILURE;
+       }
+
+       if (argc != 3)
+               return CMD_RET_USAGE;
+
+       name = argv[1];
+       value = argv[2];
+
+       if (avb_ops->write_persistent_value(avb_ops, name, strlen(value) + 1,
+                                           (const uint8_t *)value) ==
+           AVB_IO_RESULT_OK) {
+               printf("Wrote %ld bytes\n", strlen(value) + 1);
+               return CMD_RET_SUCCESS;
+       }
+
+       printf("Failed to write persistent value\n");
+
+       return CMD_RET_FAILURE;
+}
+
 static cmd_tbl_t cmd_avb[] = {
        U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
        U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
@@ -350,6 +420,10 @@ static cmd_tbl_t cmd_avb[] = {
        U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
        U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
        U_BOOT_CMD_MKENT(verify, 1, 0, do_avb_verify_part, "", ""),
+#ifdef CONFIG_OPTEE_TA_AVB
+       U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""),
+       U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""),
+#endif
 };
 
 static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
@@ -384,6 +458,10 @@ U_BOOT_CMD(
        "    partition <partname> and print to stdout\n"
        "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
        "    <partname> by <offset> using data from <addr>\n"
+#ifdef CONFIG_OPTEE_TA_AVB
+       "avb read_pvalue <name> <bytes> - read a persistent value <name>\n"
+       "avb write_pvalue <name> <value> - write a persistent value <name>\n"
+#endif
        "avb verify - run verification process using hash data\n"
        "    from vbmeta structure\n"
        );