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