Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2018 Linaro Ltd. |
| 4 | * Sam Protsenko <semen.protsenko@linaro.org> |
| 5 | */ |
| 6 | |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 7 | #include <env.h> |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 8 | #include <image-android-dt.h> |
| 9 | #include <common.h> |
| 10 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 11 | enum cmd_adtimg_info { |
| 12 | CMD_ADTIMG_START = 0, |
| 13 | CMD_ADTIMG_SIZE, |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 14 | }; |
| 15 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 16 | static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc, |
| 17 | char * const argv[]) |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 18 | { |
| 19 | char *endp; |
| 20 | ulong hdr_addr; |
| 21 | |
| 22 | if (argc != 2) |
| 23 | return CMD_RET_USAGE; |
| 24 | |
| 25 | hdr_addr = simple_strtoul(argv[1], &endp, 16); |
| 26 | if (*endp != '\0') { |
| 27 | printf("Error: Wrong image address\n"); |
| 28 | return CMD_RET_FAILURE; |
| 29 | } |
| 30 | |
| 31 | if (!android_dt_check_header(hdr_addr)) { |
| 32 | printf("Error: DT image header is incorrect\n"); |
| 33 | return CMD_RET_FAILURE; |
| 34 | } |
| 35 | |
| 36 | android_dt_print_contents(hdr_addr); |
| 37 | |
| 38 | return CMD_RET_SUCCESS; |
| 39 | } |
| 40 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 41 | static int adtimg_get_fdt(int argc, char * const argv[], |
| 42 | enum cmd_adtimg_info cmd) |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 43 | { |
| 44 | ulong hdr_addr; |
| 45 | u32 index; |
| 46 | char *endp; |
| 47 | ulong fdt_addr; |
| 48 | u32 fdt_size; |
| 49 | char buf[65]; |
| 50 | |
| 51 | if (argc != 4) |
| 52 | return CMD_RET_USAGE; |
| 53 | |
| 54 | hdr_addr = simple_strtoul(argv[1], &endp, 16); |
| 55 | if (*endp != '\0') { |
| 56 | printf("Error: Wrong image address\n"); |
| 57 | return CMD_RET_FAILURE; |
| 58 | } |
| 59 | |
| 60 | if (!android_dt_check_header(hdr_addr)) { |
| 61 | printf("Error: DT image header is incorrect\n"); |
| 62 | return CMD_RET_FAILURE; |
| 63 | } |
| 64 | |
| 65 | index = simple_strtoul(argv[2], &endp, 0); |
| 66 | if (*endp != '\0') { |
| 67 | printf("Error: Wrong index\n"); |
| 68 | return CMD_RET_FAILURE; |
| 69 | } |
| 70 | |
| 71 | if (!android_dt_get_fdt_by_index(hdr_addr, index, &fdt_addr, &fdt_size)) |
| 72 | return CMD_RET_FAILURE; |
| 73 | |
| 74 | switch (cmd) { |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 75 | case CMD_ADTIMG_START: |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 76 | snprintf(buf, sizeof(buf), "%lx", fdt_addr); |
| 77 | break; |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 78 | case CMD_ADTIMG_SIZE: |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 79 | snprintf(buf, sizeof(buf), "%x", fdt_size); |
| 80 | break; |
| 81 | default: |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 82 | printf("Error: Unknown cmd_adtimg_info value: %d\n", cmd); |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 83 | return CMD_RET_FAILURE; |
| 84 | } |
| 85 | |
| 86 | env_set(argv[3], buf); |
| 87 | |
| 88 | return CMD_RET_SUCCESS; |
| 89 | } |
| 90 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 91 | static int do_adtimg_start(cmd_tbl_t *cmdtp, int flag, int argc, |
| 92 | char * const argv[]) |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 93 | { |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 94 | return adtimg_get_fdt(argc, argv, CMD_ADTIMG_START); |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 97 | static int do_adtimg_size(cmd_tbl_t *cmdtp, int flag, int argc, |
| 98 | char * const argv[]) |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 99 | { |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 100 | return adtimg_get_fdt(argc, argv, CMD_ADTIMG_SIZE); |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 101 | } |
| 102 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 103 | static cmd_tbl_t cmd_adtimg_sub[] = { |
| 104 | U_BOOT_CMD_MKENT(dump, 2, 0, do_adtimg_dump, "", ""), |
| 105 | U_BOOT_CMD_MKENT(start, 4, 0, do_adtimg_start, "", ""), |
| 106 | U_BOOT_CMD_MKENT(size, 4, 0, do_adtimg_size, "", ""), |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 107 | }; |
| 108 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 109 | static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 110 | { |
| 111 | cmd_tbl_t *cp; |
| 112 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 113 | cp = find_cmd_tbl(argv[1], cmd_adtimg_sub, ARRAY_SIZE(cmd_adtimg_sub)); |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 114 | |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 115 | /* Strip off leading 'adtimg' command argument */ |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 116 | argc--; |
| 117 | argv++; |
| 118 | |
| 119 | if (!cp || argc > cp->maxargs) |
| 120 | return CMD_RET_USAGE; |
Boris Brezillon | c71dfd1 | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 121 | if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 122 | return CMD_RET_SUCCESS; |
| 123 | |
| 124 | return cp->cmd(cmdtp, flag, argc, argv); |
| 125 | } |
| 126 | |
| 127 | U_BOOT_CMD( |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 128 | adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg, |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 129 | "manipulate dtb/dtbo Android image", |
| 130 | "dump <addr>\n" |
| 131 | " - parse specified image and print its structure info\n" |
| 132 | " <addr>: image address in RAM, in hex\n" |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 133 | "adtimg start <addr> <index> <varname>\n" |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 134 | " - get address (hex) of FDT in the image, by index\n" |
| 135 | " <addr>: image address in RAM, in hex\n" |
| 136 | " <index>: index of desired FDT in the image\n" |
| 137 | " <varname>: name of variable where to store address of FDT\n" |
Eugeniu Rosca | a91a78e | 2019-12-24 17:51:07 +0100 | [diff] [blame^] | 138 | "adtimg size <addr> <index> <varname>\n" |
Sam Protsenko | 92f95bb | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 139 | " - get size (hex, bytes) of FDT in the image, by index\n" |
| 140 | " <addr>: image address in RAM, in hex\n" |
| 141 | " <index>: index of desired FDT in the image\n" |
| 142 | " <varname>: name of variable where to store size of FDT" |
| 143 | ); |