Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2020 |
| 4 | * Sam Protsenko <joe.skb7@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #include <android_image.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 9 | #include <image.h> |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 10 | #include <mapmem.h> |
| 11 | |
| 12 | #define abootimg_addr() \ |
| 13 | (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr) |
| 14 | |
| 15 | /* Please use abootimg_addr() macro to obtain the boot image address */ |
| 16 | static ulong _abootimg_addr = -1; |
Safae Ouajih | 91da363 | 2023-02-06 00:50:04 +0100 | [diff] [blame] | 17 | static ulong _avendor_bootimg_addr = -1; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 18 | |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 19 | ulong get_abootimg_addr(void) |
| 20 | { |
| 21 | return (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr); |
| 22 | } |
| 23 | |
| 24 | ulong get_avendor_bootimg_addr(void) |
| 25 | { |
| 26 | return _avendor_bootimg_addr; |
| 27 | } |
| 28 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 29 | static int abootimg_get_ver(int argc, char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 30 | { |
Safae Ouajih | 8656e38 | 2023-02-06 00:50:03 +0100 | [diff] [blame] | 31 | const struct andr_boot_img_hdr_v0 *hdr; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 32 | int res = CMD_RET_SUCCESS; |
| 33 | |
| 34 | if (argc > 1) |
| 35 | return CMD_RET_USAGE; |
| 36 | |
| 37 | hdr = map_sysmem(abootimg_addr(), sizeof(*hdr)); |
Safae Ouajih | 88ad0c1 | 2023-02-06 00:50:05 +0100 | [diff] [blame] | 38 | if (!is_android_boot_image_header(hdr)) { |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 39 | printf("Error: Boot Image header is incorrect\n"); |
| 40 | res = CMD_RET_FAILURE; |
| 41 | goto exit; |
| 42 | } |
| 43 | |
| 44 | if (argc == 0) |
| 45 | printf("%u\n", hdr->header_version); |
| 46 | else |
| 47 | env_set_ulong(argv[0], hdr->header_version); |
| 48 | |
| 49 | exit: |
| 50 | unmap_sysmem(hdr); |
| 51 | return res; |
| 52 | } |
| 53 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 54 | static int abootimg_get_recovery_dtbo(int argc, char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 55 | { |
| 56 | ulong addr; |
| 57 | u32 size; |
| 58 | |
| 59 | if (argc > 2) |
| 60 | return CMD_RET_USAGE; |
| 61 | |
| 62 | if (!android_image_get_dtbo(abootimg_addr(), &addr, &size)) |
| 63 | return CMD_RET_FAILURE; |
| 64 | |
| 65 | if (argc == 0) { |
| 66 | printf("%lx\n", addr); |
| 67 | } else { |
| 68 | env_set_hex(argv[0], addr); |
| 69 | if (argc == 2) |
| 70 | env_set_hex(argv[1], size); |
| 71 | } |
| 72 | |
| 73 | return CMD_RET_SUCCESS; |
| 74 | } |
| 75 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 76 | static int abootimg_get_dtb_load_addr(int argc, char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 77 | { |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 78 | if (argc > 1) |
| 79 | return CMD_RET_USAGE; |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 80 | struct andr_image_data img_data = {0}; |
| 81 | const struct andr_boot_img_hdr_v0 *hdr; |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 82 | const struct andr_vnd_boot_img_hdr *vhdr; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 83 | |
| 84 | hdr = map_sysmem(abootimg_addr(), sizeof(*hdr)); |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 85 | if (get_avendor_bootimg_addr() != -1) |
| 86 | vhdr = map_sysmem(get_avendor_bootimg_addr(), sizeof(*vhdr)); |
| 87 | |
| 88 | if (!android_image_get_data(hdr, vhdr, &img_data)) { |
| 89 | if (get_avendor_bootimg_addr() != -1) |
| 90 | unmap_sysmem(vhdr); |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 91 | unmap_sysmem(hdr); |
| 92 | return CMD_RET_FAILURE; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 93 | } |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 94 | |
| 95 | if (get_avendor_bootimg_addr() != -1) |
| 96 | unmap_sysmem(vhdr); |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 97 | unmap_sysmem(hdr); |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 98 | |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 99 | if (img_data.header_version < 2) { |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 100 | printf("Error: header_version must be >= 2 for this\n"); |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 101 | return CMD_RET_FAILURE; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 104 | if (!img_data.dtb_load_addr) { |
| 105 | printf("Error: failed to read dtb_load_addr\n"); |
| 106 | return CMD_RET_FAILURE; |
| 107 | } |
| 108 | |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 109 | if (argc == 0) |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 110 | printf("%lx\n", (ulong)img_data.dtb_load_addr); |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 111 | else |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 112 | env_set_hex(argv[0], (ulong)img_data.dtb_load_addr); |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 113 | |
Safae Ouajih | bced104 | 2023-02-06 00:50:08 +0100 | [diff] [blame] | 114 | return CMD_RET_SUCCESS; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 117 | static int abootimg_get_dtb_by_index(int argc, char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 118 | { |
| 119 | const char *index_str; |
| 120 | u32 num; |
| 121 | char *endp; |
| 122 | ulong addr; |
| 123 | u32 size; |
| 124 | |
| 125 | if (argc < 1 || argc > 3) |
| 126 | return CMD_RET_USAGE; |
| 127 | |
| 128 | index_str = argv[0] + strlen("--index="); |
| 129 | if (index_str[0] == '\0') { |
| 130 | printf("Error: Wrong index num\n"); |
| 131 | return CMD_RET_FAILURE; |
| 132 | } |
| 133 | |
| 134 | num = simple_strtoul(index_str, &endp, 0); |
| 135 | if (*endp != '\0') { |
| 136 | printf("Error: Wrong index num\n"); |
| 137 | return CMD_RET_FAILURE; |
| 138 | } |
| 139 | |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 140 | if (!android_image_get_dtb_by_index(abootimg_addr(), |
| 141 | get_avendor_bootimg_addr(), num, |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 142 | &addr, &size)) { |
| 143 | return CMD_RET_FAILURE; |
| 144 | } |
| 145 | |
| 146 | if (argc == 1) { |
| 147 | printf("%lx\n", addr); |
| 148 | } else { |
| 149 | if (env_set_hex(argv[1], addr)) { |
| 150 | printf("Error: Can't set [addr_var]\n"); |
| 151 | return CMD_RET_FAILURE; |
| 152 | } |
| 153 | |
| 154 | if (argc == 3) { |
| 155 | if (env_set_hex(argv[2], size)) { |
| 156 | printf("Error: Can't set [size_var]\n"); |
| 157 | return CMD_RET_FAILURE; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return CMD_RET_SUCCESS; |
| 163 | } |
| 164 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 165 | static int abootimg_get_dtb(int argc, char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 166 | { |
| 167 | if (argc < 1) |
| 168 | return CMD_RET_USAGE; |
| 169 | |
| 170 | if (strstr(argv[0], "--index=")) |
| 171 | return abootimg_get_dtb_by_index(argc, argv); |
| 172 | |
| 173 | return CMD_RET_USAGE; |
| 174 | } |
| 175 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 176 | static int do_abootimg_addr(struct cmd_tbl *cmdtp, int flag, int argc, |
| 177 | char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 178 | { |
| 179 | char *endp; |
| 180 | ulong img_addr; |
| 181 | |
Safae Ouajih | 91da363 | 2023-02-06 00:50:04 +0100 | [diff] [blame] | 182 | if (argc < 2 || argc > 3) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 183 | return CMD_RET_USAGE; |
| 184 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 185 | img_addr = hextoul(argv[1], &endp); |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 186 | if (*endp != '\0') { |
| 187 | printf("Error: Wrong image address\n"); |
| 188 | return CMD_RET_FAILURE; |
| 189 | } |
| 190 | |
| 191 | _abootimg_addr = img_addr; |
Safae Ouajih | 91da363 | 2023-02-06 00:50:04 +0100 | [diff] [blame] | 192 | |
| 193 | if (argc == 3) { |
| 194 | img_addr = simple_strtoul(argv[2], &endp, 16); |
| 195 | if (*endp != '\0') { |
| 196 | printf("Error: Wrong vendor image address\n"); |
| 197 | return CMD_RET_FAILURE; |
| 198 | } |
| 199 | |
| 200 | _avendor_bootimg_addr = img_addr; |
| 201 | } |
| 202 | |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 203 | return CMD_RET_SUCCESS; |
| 204 | } |
| 205 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 206 | static int do_abootimg_get(struct cmd_tbl *cmdtp, int flag, int argc, |
| 207 | char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 208 | { |
| 209 | const char *param; |
| 210 | |
| 211 | if (argc < 2) |
| 212 | return CMD_RET_USAGE; |
| 213 | |
| 214 | param = argv[1]; |
| 215 | argc -= 2; |
| 216 | argv += 2; |
| 217 | if (!strcmp(param, "ver")) |
| 218 | return abootimg_get_ver(argc, argv); |
| 219 | else if (!strcmp(param, "recovery_dtbo")) |
| 220 | return abootimg_get_recovery_dtbo(argc, argv); |
| 221 | else if (!strcmp(param, "dtb_load_addr")) |
| 222 | return abootimg_get_dtb_load_addr(argc, argv); |
| 223 | else if (!strcmp(param, "dtb")) |
| 224 | return abootimg_get_dtb(argc, argv); |
| 225 | |
| 226 | return CMD_RET_USAGE; |
| 227 | } |
| 228 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 229 | static int do_abootimg_dump(struct cmd_tbl *cmdtp, int flag, int argc, |
| 230 | char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 231 | { |
| 232 | if (argc != 2) |
| 233 | return CMD_RET_USAGE; |
| 234 | |
| 235 | if (!strcmp(argv[1], "dtb")) { |
| 236 | if (android_image_print_dtb_contents(abootimg_addr())) |
| 237 | return CMD_RET_FAILURE; |
| 238 | } else { |
| 239 | return CMD_RET_USAGE; |
| 240 | } |
| 241 | |
| 242 | return CMD_RET_SUCCESS; |
| 243 | } |
| 244 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 245 | static struct cmd_tbl cmd_abootimg_sub[] = { |
Safae Ouajih | 91da363 | 2023-02-06 00:50:04 +0100 | [diff] [blame] | 246 | U_BOOT_CMD_MKENT(addr, 3, 1, do_abootimg_addr, "", ""), |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 247 | U_BOOT_CMD_MKENT(dump, 2, 1, do_abootimg_dump, "", ""), |
| 248 | U_BOOT_CMD_MKENT(get, 5, 1, do_abootimg_get, "", ""), |
| 249 | }; |
| 250 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 251 | static int do_abootimg(struct cmd_tbl *cmdtp, int flag, int argc, |
| 252 | char *const argv[]) |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 253 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 254 | struct cmd_tbl *cp; |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 255 | |
| 256 | cp = find_cmd_tbl(argv[1], cmd_abootimg_sub, |
| 257 | ARRAY_SIZE(cmd_abootimg_sub)); |
| 258 | |
| 259 | /* Strip off leading 'abootimg' command argument */ |
| 260 | argc--; |
| 261 | argv++; |
| 262 | |
| 263 | if (!cp || argc > cp->maxargs) |
| 264 | return CMD_RET_USAGE; |
| 265 | if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) |
| 266 | return CMD_RET_SUCCESS; |
| 267 | |
| 268 | return cp->cmd(cmdtp, flag, argc, argv); |
| 269 | } |
| 270 | |
| 271 | U_BOOT_CMD( |
| 272 | abootimg, CONFIG_SYS_MAXARGS, 0, do_abootimg, |
| 273 | "manipulate Android Boot Image", |
Safae Ouajih | 91da363 | 2023-02-06 00:50:04 +0100 | [diff] [blame] | 274 | "addr <boot_img_addr> [<vendor_boot_img_addr>]>\n" |
Sam Protsenko | 035502e | 2020-01-24 17:53:42 +0200 | [diff] [blame] | 275 | " - set the address in RAM where boot image is located\n" |
| 276 | " ($loadaddr is used by default)\n" |
| 277 | "abootimg dump dtb\n" |
| 278 | " - print info for all DT blobs in DTB area\n" |
| 279 | "abootimg get ver [varname]\n" |
| 280 | " - get header version\n" |
| 281 | "abootimg get recovery_dtbo [addr_var [size_var]]\n" |
| 282 | " - get address and size (hex) of recovery DTBO area in the image\n" |
| 283 | " [addr_var]: variable name to contain DTBO area address\n" |
| 284 | " [size_var]: variable name to contain DTBO area size\n" |
| 285 | "abootimg get dtb_load_addr [varname]\n" |
| 286 | " - get load address (hex) of DTB, from image header\n" |
| 287 | "abootimg get dtb --index=<num> [addr_var [size_var]]\n" |
| 288 | " - get address and size (hex) of DT blob in the image by index\n" |
| 289 | " <num>: index number of desired DT blob in DTB area\n" |
| 290 | " [addr_var]: variable name to contain DT blob address\n" |
| 291 | " [size_var]: variable name to contain DT blob size" |
| 292 | ); |