Sam Protsenko | d03e76a | 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> |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 5 | * Eugeniu Rosca <rosca.eugeniu@gmail.com> |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 9 | #include <image-android-dt.h> |
| 10 | #include <common.h> |
| 11 | |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 12 | #define OPT_INDEX "--index" |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 13 | |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 14 | /* |
| 15 | * Current/working DTB/DTBO Android image address. |
| 16 | * Similar to 'working_fdt' variable in 'fdt' command. |
| 17 | */ |
| 18 | static ulong working_img; |
| 19 | |
| 20 | static int do_adtimg_addr(cmd_tbl_t *cmdtp, int flag, int argc, |
Eugeniu Rosca | 4c6edc2 | 2019-12-24 17:51:07 +0100 | [diff] [blame] | 21 | char * const argv[]) |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 22 | { |
| 23 | char *endp; |
| 24 | ulong hdr_addr; |
| 25 | |
| 26 | if (argc != 2) |
| 27 | return CMD_RET_USAGE; |
| 28 | |
| 29 | hdr_addr = simple_strtoul(argv[1], &endp, 16); |
| 30 | if (*endp != '\0') { |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 31 | printf("Error: Wrong image address '%s'\n", argv[1]); |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 32 | return CMD_RET_FAILURE; |
| 33 | } |
| 34 | |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 35 | /* |
| 36 | * Allow users to set an address prior to copying the DTB/DTBO |
| 37 | * image to that same address, i.e. skip header verification. |
| 38 | */ |
| 39 | |
| 40 | working_img = hdr_addr; |
| 41 | return CMD_RET_SUCCESS; |
| 42 | } |
| 43 | |
| 44 | static int adtimg_check_working_img(void) |
| 45 | { |
| 46 | if (!working_img) { |
| 47 | printf("Error: Please, call 'adtimg addr <addr>'. Aborting!\n"); |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 48 | return CMD_RET_FAILURE; |
| 49 | } |
| 50 | |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 51 | if (!android_dt_check_header(working_img)) { |
| 52 | printf("Error: Invalid image header at 0x%lx\n", working_img); |
| 53 | return CMD_RET_FAILURE; |
| 54 | } |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 55 | |
| 56 | return CMD_RET_SUCCESS; |
| 57 | } |
| 58 | |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 59 | static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc, |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 60 | char * const argv[]) |
| 61 | { |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 62 | if (argc != 1) |
| 63 | return CMD_RET_USAGE; |
| 64 | |
| 65 | if (adtimg_check_working_img() != CMD_RET_SUCCESS) |
| 66 | return CMD_RET_FAILURE; |
| 67 | |
| 68 | android_dt_print_contents(working_img); |
| 69 | |
| 70 | return CMD_RET_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | static int adtimg_getopt_u32(char * const opt, char * const name, u32 *optval) |
| 74 | { |
| 75 | char *endp, *str; |
| 76 | u32 val; |
| 77 | |
| 78 | if (!opt || !name || !optval) |
| 79 | return CMD_RET_FAILURE; |
| 80 | |
| 81 | str = strchr(opt, '='); |
| 82 | if (!str) { |
| 83 | printf("Error: Option '%s' not followed by '='\n", name); |
| 84 | return CMD_RET_FAILURE; |
| 85 | } |
| 86 | |
| 87 | if (*++str == '\0') { |
| 88 | printf("Error: Option '%s=' not followed by value\n", name); |
| 89 | return CMD_RET_FAILURE; |
| 90 | } |
| 91 | |
| 92 | val = simple_strtoul(str, &endp, 0); |
| 93 | if (*endp != '\0') { |
| 94 | printf("Error: Wrong integer value '%s=%s'\n", name, str); |
| 95 | return CMD_RET_FAILURE; |
| 96 | } |
| 97 | |
| 98 | *optval = val; |
| 99 | return CMD_RET_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | static int adtimg_getopt_index(int argc, char * const argv[], u32 *index, |
| 103 | char **avar, char **svar) |
| 104 | { |
| 105 | int ret; |
| 106 | |
| 107 | if (!argv || !avar || !svar) |
| 108 | return CMD_RET_FAILURE; |
| 109 | |
| 110 | if (argc > 3) { |
| 111 | printf("Error: Unexpected argument '%s'\n", argv[3]); |
| 112 | return CMD_RET_FAILURE; |
| 113 | } |
| 114 | |
| 115 | ret = adtimg_getopt_u32(argv[0], OPT_INDEX, index); |
| 116 | if (ret != CMD_RET_SUCCESS) |
| 117 | return ret; |
| 118 | |
| 119 | if (argc > 1) |
| 120 | *avar = argv[1]; |
| 121 | if (argc > 2) |
| 122 | *svar = argv[2]; |
| 123 | |
| 124 | return CMD_RET_SUCCESS; |
| 125 | } |
| 126 | |
| 127 | static int adtimg_get_dt_by_index(int argc, char * const argv[]) |
| 128 | { |
| 129 | ulong addr; |
| 130 | u32 index, size; |
| 131 | int ret; |
| 132 | char *avar = NULL, *svar = NULL; |
| 133 | |
| 134 | ret = adtimg_getopt_index(argc, argv, &index, &avar, &svar); |
| 135 | if (ret != CMD_RET_SUCCESS) |
| 136 | return ret; |
| 137 | |
| 138 | if (!android_dt_get_fdt_by_index(working_img, index, &addr, &size)) |
| 139 | return CMD_RET_FAILURE; |
| 140 | |
| 141 | if (avar && svar) { |
| 142 | ret = env_set_hex(avar, addr); |
| 143 | if (ret) { |
| 144 | printf("Error: Can't set '%s' to 0x%lx\n", avar, addr); |
| 145 | return CMD_RET_FAILURE; |
| 146 | } |
| 147 | ret = env_set_hex(svar, size); |
| 148 | if (ret) { |
| 149 | printf("Error: Can't set '%s' to 0x%x\n", svar, size); |
| 150 | return CMD_RET_FAILURE; |
| 151 | } |
| 152 | } else if (avar) { |
| 153 | ret = env_set_hex(avar, addr); |
| 154 | if (ret) { |
| 155 | printf("Error: Can't set '%s' to 0x%lx\n", avar, addr); |
| 156 | return CMD_RET_FAILURE; |
| 157 | } |
| 158 | printf("0x%x (%d)\n", size, size); |
| 159 | } else { |
| 160 | printf("0x%lx, 0x%x (%d)\n", addr, size, size); |
| 161 | } |
| 162 | |
| 163 | return CMD_RET_SUCCESS; |
| 164 | } |
| 165 | |
| 166 | static int adtimg_get_dt(int argc, char * const argv[]) |
| 167 | { |
| 168 | if (argc < 2) { |
| 169 | printf("Error: No options passed to '%s'\n", argv[0]); |
| 170 | return CMD_RET_FAILURE; |
| 171 | } |
| 172 | |
| 173 | /* Strip off leading 'dt' command argument */ |
| 174 | argc--; |
| 175 | argv++; |
| 176 | |
| 177 | if (!strncmp(argv[0], OPT_INDEX, sizeof(OPT_INDEX) - 1)) |
| 178 | return adtimg_get_dt_by_index(argc, argv); |
| 179 | |
| 180 | printf("Error: Option '%s' not supported\n", argv[0]); |
| 181 | return CMD_RET_FAILURE; |
| 182 | } |
| 183 | |
| 184 | static int do_adtimg_get(cmd_tbl_t *cmdtp, int flag, int argc, |
| 185 | char * const argv[]) |
| 186 | { |
| 187 | if (argc < 2) { |
| 188 | printf("Error: No arguments passed to '%s'\n", argv[0]); |
| 189 | return CMD_RET_FAILURE; |
| 190 | } |
| 191 | |
| 192 | if (adtimg_check_working_img() != CMD_RET_SUCCESS) |
| 193 | return CMD_RET_FAILURE; |
| 194 | |
| 195 | /* Strip off leading 'get' command argument */ |
| 196 | argc--; |
| 197 | argv++; |
| 198 | |
| 199 | if (!strcmp(argv[0], "dt")) |
| 200 | return adtimg_get_dt(argc, argv); |
| 201 | |
| 202 | printf("Error: Wrong argument '%s'\n", argv[0]); |
| 203 | return CMD_RET_FAILURE; |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 204 | } |
| 205 | |
Eugeniu Rosca | 4c6edc2 | 2019-12-24 17:51:07 +0100 | [diff] [blame] | 206 | static cmd_tbl_t cmd_adtimg_sub[] = { |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 207 | U_BOOT_CMD_MKENT(addr, CONFIG_SYS_MAXARGS, 1, do_adtimg_addr, "", ""), |
| 208 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_adtimg_dump, "", ""), |
| 209 | U_BOOT_CMD_MKENT(get, CONFIG_SYS_MAXARGS, 1, do_adtimg_get, "", ""), |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 210 | }; |
| 211 | |
Eugeniu Rosca | 4c6edc2 | 2019-12-24 17:51:07 +0100 | [diff] [blame] | 212 | static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 213 | { |
| 214 | cmd_tbl_t *cp; |
| 215 | |
Eugeniu Rosca | 4c6edc2 | 2019-12-24 17:51:07 +0100 | [diff] [blame] | 216 | cp = find_cmd_tbl(argv[1], cmd_adtimg_sub, ARRAY_SIZE(cmd_adtimg_sub)); |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 217 | |
Eugeniu Rosca | 4c6edc2 | 2019-12-24 17:51:07 +0100 | [diff] [blame] | 218 | /* Strip off leading 'adtimg' command argument */ |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 219 | argc--; |
| 220 | argv++; |
| 221 | |
| 222 | if (!cp || argc > cp->maxargs) |
| 223 | return CMD_RET_USAGE; |
Boris Brezillon | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 224 | if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 225 | return CMD_RET_SUCCESS; |
| 226 | |
| 227 | return cp->cmd(cmdtp, flag, argc, argv); |
| 228 | } |
| 229 | |
| 230 | U_BOOT_CMD( |
Eugeniu Rosca | 4c6edc2 | 2019-12-24 17:51:07 +0100 | [diff] [blame] | 231 | adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg, |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 232 | "manipulate dtb/dtbo Android image", |
Eugeniu Rosca | 4f731c7 | 2019-12-24 17:51:08 +0100 | [diff] [blame] | 233 | "addr <addr> - Set image location to <addr>\n" |
| 234 | "adtimg dump - Print out image contents\n" |
| 235 | "adtimg get dt --index=<index> [avar [svar]] - Get DT address/size by index\n" |
| 236 | "\n" |
| 237 | "Legend:\n" |
| 238 | " - <addr>: DTB/DTBO image address (hex) in RAM\n" |
| 239 | " - <index>: index (hex/dec) of desired DT in the image\n" |
| 240 | " - <avar>: variable name to contain DT address (hex)\n" |
| 241 | " - <svar>: variable name to contain DT size (hex)" |
Sam Protsenko | d03e76a | 2018-08-16 23:34:13 +0300 | [diff] [blame] | 242 | ); |