Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
| 4 | * |
| 5 | * (C) Copyright 2008 Semihalf |
| 6 | * |
| 7 | * (C) Copyright 2000-2006 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 11 | #define LOG_CATEGORY LOGC_BOOT |
| 12 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 13 | #ifdef USE_HOSTCC |
| 14 | #include "mkimage.h" |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 15 | #include <time.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 16 | #include <linux/libfdt.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 17 | #include <u-boot/crc.h> |
Tom Rini | 8fac3d4 | 2023-12-14 07:16:54 -0500 | [diff] [blame] | 18 | #include <linux/kconfig.h> |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 19 | #else |
Andreas Dannenberg | 67aaa6d | 2016-07-27 12:12:39 -0500 | [diff] [blame] | 20 | #include <linux/compiler.h> |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 21 | #include <linux/sizes.h> |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 23 | #include <log.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 24 | #include <mapmem.h> |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 25 | #include <asm/io.h> |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 26 | #include <malloc.h> |
Sean Anderson | 615e31c | 2022-03-24 11:26:11 -0400 | [diff] [blame] | 27 | #include <memalign.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 28 | #include <asm/global_data.h> |
Chia-Wei Wang | 4a733b3 | 2021-07-30 09:08:05 +0800 | [diff] [blame] | 29 | #ifdef CONFIG_DM_HASH |
| 30 | #include <dm.h> |
| 31 | #include <u-boot/hash.h> |
| 32 | #endif |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 34 | #endif /* !USE_HOSTCC*/ |
| 35 | |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 36 | #include <bootm.h> |
Andreas Dannenberg | 67aaa6d | 2016-07-27 12:12:39 -0500 | [diff] [blame] | 37 | #include <image.h> |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 38 | #include <bootstage.h> |
Simon Glass | 9b5bfba | 2024-08-07 16:47:33 -0600 | [diff] [blame] | 39 | #include <upl.h> |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 40 | #include <u-boot/crc.h> |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 41 | |
| 42 | /*****************************************************************************/ |
| 43 | /* New uImage format routines */ |
| 44 | /*****************************************************************************/ |
| 45 | #ifndef USE_HOSTCC |
| 46 | static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, |
| 47 | ulong *addr, const char **name) |
| 48 | { |
| 49 | const char *sep; |
| 50 | |
| 51 | *addr = addr_curr; |
| 52 | *name = NULL; |
| 53 | |
| 54 | sep = strchr(spec, sepc); |
| 55 | if (sep) { |
| 56 | if (sep - spec > 0) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 57 | *addr = hextoul(spec, NULL); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 58 | |
| 59 | *name = sep + 1; |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * fit_parse_conf - parse FIT configuration spec |
| 68 | * @spec: input string, containing configuration spec |
| 69 | * @add_curr: current image address (to be used as a possible default) |
| 70 | * @addr: pointer to a ulong variable, will hold FIT image address of a given |
| 71 | * configuration |
| 72 | * @conf_name double pointer to a char, will hold pointer to a configuration |
| 73 | * unit name |
| 74 | * |
Masahiro Yamada | 4ac739a | 2013-09-18 09:36:38 +0900 | [diff] [blame] | 75 | * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>, |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 76 | * where <addr> is a FIT image address that contains configuration |
| 77 | * with a <conf> unit name. |
| 78 | * |
| 79 | * Address part is optional, and if omitted default add_curr will |
| 80 | * be used instead. |
| 81 | * |
| 82 | * returns: |
| 83 | * 1 if spec is a valid configuration string, |
| 84 | * addr and conf_name are set accordingly |
| 85 | * 0 otherwise |
| 86 | */ |
| 87 | int fit_parse_conf(const char *spec, ulong addr_curr, |
| 88 | ulong *addr, const char **conf_name) |
| 89 | { |
| 90 | return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * fit_parse_subimage - parse FIT subimage spec |
| 95 | * @spec: input string, containing subimage spec |
| 96 | * @add_curr: current image address (to be used as a possible default) |
| 97 | * @addr: pointer to a ulong variable, will hold FIT image address of a given |
| 98 | * subimage |
| 99 | * @image_name: double pointer to a char, will hold pointer to a subimage name |
| 100 | * |
Masahiro Yamada | 4ac739a | 2013-09-18 09:36:38 +0900 | [diff] [blame] | 101 | * fit_parse_subimage() expects subimage spec in the form of |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 102 | * [<addr>]:<subimage>, where <addr> is a FIT image address that contains |
| 103 | * subimage with a <subimg> unit name. |
| 104 | * |
| 105 | * Address part is optional, and if omitted default add_curr will |
| 106 | * be used instead. |
| 107 | * |
| 108 | * returns: |
| 109 | * 1 if spec is a valid subimage string, |
| 110 | * addr and image_name are set accordingly |
| 111 | * 0 otherwise |
| 112 | */ |
| 113 | int fit_parse_subimage(const char *spec, ulong addr_curr, |
| 114 | ulong *addr, const char **image_name) |
| 115 | { |
| 116 | return fit_parse_spec(spec, ':', addr_curr, addr, image_name); |
| 117 | } |
| 118 | #endif /* !USE_HOSTCC */ |
| 119 | |
Joel Stanley | c3baacd | 2020-12-08 14:42:14 +1030 | [diff] [blame] | 120 | #ifdef USE_HOSTCC |
| 121 | /* Host tools use these implementations for Cipher and Signature support */ |
| 122 | static void *host_blob; |
| 123 | |
| 124 | void image_set_host_blob(void *blob) |
| 125 | { |
| 126 | host_blob = blob; |
| 127 | } |
| 128 | |
| 129 | void *image_get_host_blob(void) |
| 130 | { |
| 131 | return host_blob; |
| 132 | } |
| 133 | #endif /* USE_HOSTCC */ |
| 134 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 135 | static void fit_get_debug(const void *fit, int noffset, |
| 136 | char *prop_name, int err) |
| 137 | { |
| 138 | debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n", |
| 139 | prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL), |
| 140 | fdt_strerror(err)); |
| 141 | } |
| 142 | |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 143 | /** |
| 144 | * fit_get_subimage_count - get component (sub-image) count |
| 145 | * @fit: pointer to the FIT format image header |
| 146 | * @images_noffset: offset of images node |
| 147 | * |
| 148 | * returns: |
| 149 | * number of image components |
| 150 | */ |
| 151 | int fit_get_subimage_count(const void *fit, int images_noffset) |
| 152 | { |
| 153 | int noffset; |
| 154 | int ndepth; |
| 155 | int count = 0; |
| 156 | |
| 157 | /* Process its subnodes, print out component images details */ |
| 158 | for (ndepth = 0, count = 0, |
| 159 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 160 | (noffset >= 0) && (ndepth > 0); |
| 161 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 162 | if (ndepth == 1) { |
| 163 | count++; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return count; |
| 168 | } |
| 169 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 170 | /** |
Tom Rini | 7d77b66 | 2018-05-08 14:34:05 -0400 | [diff] [blame] | 171 | * fit_image_print_data() - prints out the hash node details |
| 172 | * @fit: pointer to the FIT format image header |
| 173 | * @noffset: offset of the hash node |
| 174 | * @p: pointer to prefix string |
| 175 | * @type: Type of information to print ("hash" or "sign") |
| 176 | * |
| 177 | * fit_image_print_data() lists properties for the processed hash node |
| 178 | * |
| 179 | * This function avoid using puts() since it prints a newline on the host |
| 180 | * but does not in U-Boot. |
| 181 | * |
| 182 | * returns: |
| 183 | * no returned results |
| 184 | */ |
| 185 | static void fit_image_print_data(const void *fit, int noffset, const char *p, |
| 186 | const char *type) |
| 187 | { |
| 188 | const char *keyname; |
| 189 | uint8_t *value; |
| 190 | int value_len; |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 191 | const char *algo; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 192 | const char *padding; |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 193 | bool required; |
Tom Rini | 7d77b66 | 2018-05-08 14:34:05 -0400 | [diff] [blame] | 194 | int ret, i; |
| 195 | |
| 196 | debug("%s %s node: '%s'\n", p, type, |
| 197 | fit_get_name(fit, noffset, NULL)); |
| 198 | printf("%s %s algo: ", p, type); |
| 199 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
| 200 | printf("invalid/unsupported\n"); |
| 201 | return; |
| 202 | } |
| 203 | printf("%s", algo); |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 204 | keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL); |
| 205 | required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL; |
Tom Rini | 7d77b66 | 2018-05-08 14:34:05 -0400 | [diff] [blame] | 206 | if (keyname) |
| 207 | printf(":%s", keyname); |
| 208 | if (required) |
| 209 | printf(" (required)"); |
| 210 | printf("\n"); |
| 211 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 212 | padding = fdt_getprop(fit, noffset, "padding", NULL); |
| 213 | if (padding) |
| 214 | printf("%s %s padding: %s\n", p, type, padding); |
| 215 | |
Tom Rini | 7d77b66 | 2018-05-08 14:34:05 -0400 | [diff] [blame] | 216 | ret = fit_image_hash_get_value(fit, noffset, &value, |
| 217 | &value_len); |
| 218 | printf("%s %s value: ", p, type); |
| 219 | if (ret) { |
| 220 | printf("unavailable\n"); |
| 221 | } else { |
| 222 | for (i = 0; i < value_len; i++) |
| 223 | printf("%02x", value[i]); |
| 224 | printf("\n"); |
| 225 | } |
| 226 | |
| 227 | debug("%s %s len: %d\n", p, type, value_len); |
| 228 | |
| 229 | /* Signatures have a time stamp */ |
| 230 | if (IMAGE_ENABLE_TIMESTAMP && keyname) { |
| 231 | time_t timestamp; |
| 232 | |
| 233 | printf("%s Timestamp: ", p); |
| 234 | if (fit_get_timestamp(fit, noffset, ×tamp)) |
| 235 | printf("unavailable\n"); |
| 236 | else |
| 237 | genimg_print_time(timestamp); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * fit_image_print_verification_data() - prints out the hash/signature details |
| 243 | * @fit: pointer to the FIT format image header |
| 244 | * @noffset: offset of the hash or signature node |
| 245 | * @p: pointer to prefix string |
| 246 | * |
| 247 | * This lists properties for the processed hash node |
| 248 | * |
| 249 | * returns: |
| 250 | * no returned results |
| 251 | */ |
| 252 | static void fit_image_print_verification_data(const void *fit, int noffset, |
| 253 | const char *p) |
| 254 | { |
| 255 | const char *name; |
| 256 | |
| 257 | /* |
| 258 | * Check subnode name, must be equal to "hash" or "signature". |
| 259 | * Multiple hash/signature nodes require unique unit node |
| 260 | * names, e.g. hash-1, hash-2, signature-1, signature-2, etc. |
| 261 | */ |
| 262 | name = fit_get_name(fit, noffset, NULL); |
| 263 | if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) { |
| 264 | fit_image_print_data(fit, noffset, p, "Hash"); |
| 265 | } else if (!strncmp(name, FIT_SIG_NODENAME, |
| 266 | strlen(FIT_SIG_NODENAME))) { |
| 267 | fit_image_print_data(fit, noffset, p, "Sign"); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * fit_conf_print - prints out the FIT configuration details |
| 273 | * @fit: pointer to the FIT format image header |
| 274 | * @noffset: offset of the configuration node |
| 275 | * @p: pointer to prefix string |
| 276 | * |
| 277 | * fit_conf_print() lists all mandatory properties for the processed |
| 278 | * configuration node. |
| 279 | * |
| 280 | * returns: |
| 281 | * no returned results |
| 282 | */ |
| 283 | static void fit_conf_print(const void *fit, int noffset, const char *p) |
| 284 | { |
| 285 | char *desc; |
| 286 | const char *uname; |
| 287 | int ret; |
| 288 | int fdt_index, loadables_index; |
| 289 | int ndepth; |
| 290 | |
| 291 | /* Mandatory properties */ |
| 292 | ret = fit_get_desc(fit, noffset, &desc); |
| 293 | printf("%s Description: ", p); |
| 294 | if (ret) |
| 295 | printf("unavailable\n"); |
| 296 | else |
| 297 | printf("%s\n", desc); |
| 298 | |
| 299 | uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); |
| 300 | printf("%s Kernel: ", p); |
| 301 | if (!uname) |
| 302 | printf("unavailable\n"); |
| 303 | else |
| 304 | printf("%s\n", uname); |
| 305 | |
| 306 | /* Optional properties */ |
| 307 | uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); |
| 308 | if (uname) |
| 309 | printf("%s Init Ramdisk: %s\n", p, uname); |
| 310 | |
| 311 | uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL); |
| 312 | if (uname) |
| 313 | printf("%s Firmware: %s\n", p, uname); |
| 314 | |
| 315 | for (fdt_index = 0; |
| 316 | uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP, |
| 317 | fdt_index, NULL), uname; |
| 318 | fdt_index++) { |
| 319 | if (fdt_index == 0) |
| 320 | printf("%s FDT: ", p); |
| 321 | else |
| 322 | printf("%s ", p); |
| 323 | printf("%s\n", uname); |
| 324 | } |
| 325 | |
| 326 | uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL); |
| 327 | if (uname) |
| 328 | printf("%s FPGA: %s\n", p, uname); |
| 329 | |
| 330 | /* Print out all of the specified loadables */ |
| 331 | for (loadables_index = 0; |
| 332 | uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP, |
| 333 | loadables_index, NULL), uname; |
| 334 | loadables_index++) { |
| 335 | if (loadables_index == 0) { |
| 336 | printf("%s Loadables: ", p); |
| 337 | } else { |
| 338 | printf("%s ", p); |
| 339 | } |
| 340 | printf("%s\n", uname); |
| 341 | } |
| 342 | |
| 343 | /* Process all hash subnodes of the component configuration node */ |
| 344 | for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth); |
| 345 | (noffset >= 0) && (ndepth > 0); |
| 346 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 347 | if (ndepth == 1) { |
| 348 | /* Direct child node of the component configuration node */ |
| 349 | fit_image_print_verification_data(fit, noffset, p); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 355 | * fit_print_contents - prints out the contents of the FIT format image |
| 356 | * @fit: pointer to the FIT format image header |
| 357 | * @p: pointer to prefix string |
| 358 | * |
| 359 | * fit_print_contents() formats a multi line FIT image contents description. |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 360 | * The routine prints out FIT image properties (root node level) followed by |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 361 | * the details of each component image. |
| 362 | * |
| 363 | * returns: |
| 364 | * no returned results |
| 365 | */ |
| 366 | void fit_print_contents(const void *fit) |
| 367 | { |
| 368 | char *desc; |
| 369 | char *uname; |
| 370 | int images_noffset; |
| 371 | int confs_noffset; |
| 372 | int noffset; |
| 373 | int ndepth; |
| 374 | int count = 0; |
| 375 | int ret; |
| 376 | const char *p; |
| 377 | time_t timestamp; |
| 378 | |
Simon Glass | 800b6fb | 2021-09-25 19:43:34 -0600 | [diff] [blame] | 379 | if (!CONFIG_IS_ENABLED(FIT_PRINT)) |
| 380 | return; |
| 381 | |
Simon Glass | 1030f16 | 2013-05-08 08:05:58 +0000 | [diff] [blame] | 382 | /* Indent string is defined in header image.h */ |
| 383 | p = IMAGE_INDENT_STRING; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 384 | |
| 385 | /* Root node properties */ |
| 386 | ret = fit_get_desc(fit, 0, &desc); |
| 387 | printf("%sFIT description: ", p); |
| 388 | if (ret) |
| 389 | printf("unavailable\n"); |
| 390 | else |
| 391 | printf("%s\n", desc); |
| 392 | |
| 393 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 394 | ret = fit_get_timestamp(fit, 0, ×tamp); |
| 395 | printf("%sCreated: ", p); |
| 396 | if (ret) |
| 397 | printf("unavailable\n"); |
| 398 | else |
| 399 | genimg_print_time(timestamp); |
| 400 | } |
| 401 | |
| 402 | /* Find images parent node offset */ |
| 403 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 404 | if (images_noffset < 0) { |
| 405 | printf("Can't find images parent node '%s' (%s)\n", |
| 406 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* Process its subnodes, print out component images details */ |
| 411 | for (ndepth = 0, count = 0, |
| 412 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 413 | (noffset >= 0) && (ndepth > 0); |
| 414 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 415 | if (ndepth == 1) { |
| 416 | /* |
| 417 | * Direct child node of the images parent node, |
| 418 | * i.e. component image node. |
| 419 | */ |
| 420 | printf("%s Image %u (%s)\n", p, count++, |
| 421 | fit_get_name(fit, noffset, NULL)); |
| 422 | |
| 423 | fit_image_print(fit, noffset, p); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Find configurations parent node offset */ |
| 428 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 429 | if (confs_noffset < 0) { |
| 430 | debug("Can't get configurations parent node '%s' (%s)\n", |
| 431 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | /* get default configuration unit name from default property */ |
| 436 | uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); |
| 437 | if (uname) |
| 438 | printf("%s Default Configuration: '%s'\n", p, uname); |
| 439 | |
| 440 | /* Process its subnodes, print out configurations details */ |
| 441 | for (ndepth = 0, count = 0, |
| 442 | noffset = fdt_next_node(fit, confs_noffset, &ndepth); |
| 443 | (noffset >= 0) && (ndepth > 0); |
| 444 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 445 | if (ndepth == 1) { |
| 446 | /* |
| 447 | * Direct child node of the configurations parent node, |
| 448 | * i.e. configuration node. |
| 449 | */ |
| 450 | printf("%s Configuration %u (%s)\n", p, count++, |
| 451 | fit_get_name(fit, noffset, NULL)); |
| 452 | |
| 453 | fit_conf_print(fit, noffset, p); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * fit_image_print - prints out the FIT component image details |
| 460 | * @fit: pointer to the FIT format image header |
| 461 | * @image_noffset: offset of the component image node |
| 462 | * @p: pointer to prefix string |
| 463 | * |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 464 | * fit_image_print() lists all mandatory properties for the processed component |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 465 | * image. If present, hash nodes are printed out as well. Load |
| 466 | * address for images of type firmware is also printed out. Since the load |
| 467 | * address is not mandatory for firmware images, it will be output as |
| 468 | * "unavailable" when not present. |
| 469 | * |
| 470 | * returns: |
| 471 | * no returned results |
| 472 | */ |
| 473 | void fit_image_print(const void *fit, int image_noffset, const char *p) |
| 474 | { |
| 475 | char *desc; |
Daniel Golle | 2ebfd22 | 2022-08-27 04:17:28 +0100 | [diff] [blame] | 476 | uint8_t type, arch, os, comp = IH_COMP_NONE; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 477 | size_t size; |
| 478 | ulong load, entry; |
| 479 | const void *data; |
| 480 | int noffset; |
| 481 | int ndepth; |
| 482 | int ret; |
| 483 | |
Simon Glass | 800b6fb | 2021-09-25 19:43:34 -0600 | [diff] [blame] | 484 | if (!CONFIG_IS_ENABLED(FIT_PRINT)) |
| 485 | return; |
| 486 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 487 | /* Mandatory properties */ |
| 488 | ret = fit_get_desc(fit, image_noffset, &desc); |
| 489 | printf("%s Description: ", p); |
| 490 | if (ret) |
| 491 | printf("unavailable\n"); |
| 492 | else |
| 493 | printf("%s\n", desc); |
| 494 | |
Simon Glass | 749a6e7 | 2013-07-16 20:10:01 -0700 | [diff] [blame] | 495 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 496 | time_t timestamp; |
| 497 | |
| 498 | ret = fit_get_timestamp(fit, 0, ×tamp); |
| 499 | printf("%s Created: ", p); |
| 500 | if (ret) |
| 501 | printf("unavailable\n"); |
| 502 | else |
| 503 | genimg_print_time(timestamp); |
| 504 | } |
| 505 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 506 | fit_image_get_type(fit, image_noffset, &type); |
| 507 | printf("%s Type: %s\n", p, genimg_get_type_name(type)); |
| 508 | |
| 509 | fit_image_get_comp(fit, image_noffset, &comp); |
| 510 | printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); |
| 511 | |
Kelvin Cheung | 186cc99 | 2018-05-19 18:21:37 +0800 | [diff] [blame] | 512 | ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 513 | |
Simon Glass | db503fb | 2021-09-25 19:43:14 -0600 | [diff] [blame] | 514 | if (!tools_build()) { |
Sebastian Reichel | bfe8ddf | 2021-01-04 20:48:03 +0100 | [diff] [blame] | 515 | printf("%s Data Start: ", p); |
| 516 | if (ret) { |
| 517 | printf("unavailable\n"); |
| 518 | } else { |
| 519 | void *vdata = (void *)data; |
Simon Glass | e8d6a5b | 2013-05-16 13:53:26 +0000 | [diff] [blame] | 520 | |
Sebastian Reichel | bfe8ddf | 2021-01-04 20:48:03 +0100 | [diff] [blame] | 521 | printf("0x%08lx\n", (ulong)map_to_sysmem(vdata)); |
| 522 | } |
Simon Glass | e8d6a5b | 2013-05-16 13:53:26 +0000 | [diff] [blame] | 523 | } |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 524 | |
| 525 | printf("%s Data Size: ", p); |
| 526 | if (ret) |
| 527 | printf("unavailable\n"); |
| 528 | else |
| 529 | genimg_print_size(size); |
| 530 | |
| 531 | /* Remaining, type dependent properties */ |
| 532 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 533 | (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || |
| 534 | (type == IH_TYPE_FLATDT)) { |
| 535 | fit_image_get_arch(fit, image_noffset, &arch); |
| 536 | printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); |
| 537 | } |
| 538 | |
Michal Simek | 55f698f | 2018-03-26 16:31:27 +0200 | [diff] [blame] | 539 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) || |
| 540 | (type == IH_TYPE_FIRMWARE)) { |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 541 | fit_image_get_os(fit, image_noffset, &os); |
| 542 | printf("%s OS: %s\n", p, genimg_get_os_name(os)); |
| 543 | } |
| 544 | |
| 545 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
Michal Simek | ebae78b | 2016-05-17 14:03:50 +0200 | [diff] [blame] | 546 | (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) || |
| 547 | (type == IH_TYPE_FPGA)) { |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 548 | ret = fit_image_get_load(fit, image_noffset, &load); |
| 549 | printf("%s Load Address: ", p); |
| 550 | if (ret) |
| 551 | printf("unavailable\n"); |
| 552 | else |
| 553 | printf("0x%08lx\n", load); |
| 554 | } |
| 555 | |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 556 | /* optional load address for FDT */ |
| 557 | if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load)) |
| 558 | printf("%s Load Address: 0x%08lx\n", p, load); |
| 559 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 560 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 561 | (type == IH_TYPE_RAMDISK)) { |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 562 | ret = fit_image_get_entry(fit, image_noffset, &entry); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 563 | printf("%s Entry Point: ", p); |
| 564 | if (ret) |
| 565 | printf("unavailable\n"); |
| 566 | else |
| 567 | printf("0x%08lx\n", entry); |
| 568 | } |
| 569 | |
| 570 | /* Process all hash subnodes of the component image node */ |
| 571 | for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); |
| 572 | (noffset >= 0) && (ndepth > 0); |
| 573 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 574 | if (ndepth == 1) { |
| 575 | /* Direct child node of the component image node */ |
Simon Glass | e61aa44 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 576 | fit_image_print_verification_data(fit, noffset, p); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
| 582 | * fit_get_desc - get node description property |
| 583 | * @fit: pointer to the FIT format image header |
| 584 | * @noffset: node offset |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 585 | * @desc: double pointer to the char, will hold pointer to the description |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 586 | * |
| 587 | * fit_get_desc() reads description property from a given node, if |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 588 | * description is found pointer to it is returned in third call argument. |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 589 | * |
| 590 | * returns: |
| 591 | * 0, on success |
| 592 | * -1, on failure |
| 593 | */ |
| 594 | int fit_get_desc(const void *fit, int noffset, char **desc) |
| 595 | { |
| 596 | int len; |
| 597 | |
| 598 | *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); |
| 599 | if (*desc == NULL) { |
| 600 | fit_get_debug(fit, noffset, FIT_DESC_PROP, len); |
| 601 | return -1; |
| 602 | } |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * fit_get_timestamp - get node timestamp property |
| 609 | * @fit: pointer to the FIT format image header |
| 610 | * @noffset: node offset |
| 611 | * @timestamp: pointer to the time_t, will hold read timestamp |
| 612 | * |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 613 | * fit_get_timestamp() reads timestamp property from given node, if timestamp |
| 614 | * is found and has a correct size its value is returned in third call |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 615 | * argument. |
| 616 | * |
| 617 | * returns: |
| 618 | * 0, on success |
| 619 | * -1, on property read failure |
| 620 | * -2, on wrong timestamp size |
| 621 | */ |
| 622 | int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) |
| 623 | { |
| 624 | int len; |
| 625 | const void *data; |
| 626 | |
| 627 | data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); |
| 628 | if (data == NULL) { |
| 629 | fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); |
| 630 | return -1; |
| 631 | } |
| 632 | if (len != sizeof(uint32_t)) { |
| 633 | debug("FIT timestamp with incorrect size of (%u)\n", len); |
| 634 | return -2; |
| 635 | } |
| 636 | |
| 637 | *timestamp = uimage_to_cpu(*((uint32_t *)data)); |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * fit_image_get_node - get node offset for component image of a given unit name |
| 643 | * @fit: pointer to the FIT format image header |
| 644 | * @image_uname: component image node unit name |
| 645 | * |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 646 | * fit_image_get_node() finds a component image (within the '/images' |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 647 | * node) of a provided unit name. If image is found its node offset is |
| 648 | * returned to the caller. |
| 649 | * |
| 650 | * returns: |
| 651 | * image node offset when found (>=0) |
| 652 | * negative number on failure (FDT_ERR_* code) |
| 653 | */ |
| 654 | int fit_image_get_node(const void *fit, const char *image_uname) |
| 655 | { |
| 656 | int noffset, images_noffset; |
| 657 | |
| 658 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 659 | if (images_noffset < 0) { |
| 660 | debug("Can't find images parent node '%s' (%s)\n", |
| 661 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 662 | return images_noffset; |
| 663 | } |
| 664 | |
| 665 | noffset = fdt_subnode_offset(fit, images_noffset, image_uname); |
| 666 | if (noffset < 0) { |
| 667 | debug("Can't get node offset for image unit name: '%s' (%s)\n", |
| 668 | image_uname, fdt_strerror(noffset)); |
| 669 | } |
| 670 | |
| 671 | return noffset; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * fit_image_get_os - get os id for a given component image node |
| 676 | * @fit: pointer to the FIT format image header |
| 677 | * @noffset: component image node offset |
| 678 | * @os: pointer to the uint8_t, will hold os numeric id |
| 679 | * |
| 680 | * fit_image_get_os() finds os property in a given component image node. |
| 681 | * If the property is found, its (string) value is translated to the numeric |
| 682 | * id which is returned to the caller. |
| 683 | * |
| 684 | * returns: |
| 685 | * 0, on success |
| 686 | * -1, on failure |
| 687 | */ |
| 688 | int fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
| 689 | { |
| 690 | int len; |
| 691 | const void *data; |
| 692 | |
| 693 | /* Get OS name from property data */ |
| 694 | data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); |
| 695 | if (data == NULL) { |
| 696 | fit_get_debug(fit, noffset, FIT_OS_PROP, len); |
| 697 | *os = -1; |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | /* Translate OS name to id */ |
| 702 | *os = genimg_get_os_id(data); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * fit_image_get_arch - get arch id for a given component image node |
| 708 | * @fit: pointer to the FIT format image header |
| 709 | * @noffset: component image node offset |
| 710 | * @arch: pointer to the uint8_t, will hold arch numeric id |
| 711 | * |
| 712 | * fit_image_get_arch() finds arch property in a given component image node. |
| 713 | * If the property is found, its (string) value is translated to the numeric |
| 714 | * id which is returned to the caller. |
| 715 | * |
| 716 | * returns: |
| 717 | * 0, on success |
| 718 | * -1, on failure |
| 719 | */ |
| 720 | int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) |
| 721 | { |
| 722 | int len; |
| 723 | const void *data; |
| 724 | |
| 725 | /* Get architecture name from property data */ |
| 726 | data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); |
| 727 | if (data == NULL) { |
| 728 | fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); |
| 729 | *arch = -1; |
| 730 | return -1; |
| 731 | } |
| 732 | |
| 733 | /* Translate architecture name to id */ |
| 734 | *arch = genimg_get_arch_id(data); |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * fit_image_get_type - get type id for a given component image node |
| 740 | * @fit: pointer to the FIT format image header |
| 741 | * @noffset: component image node offset |
| 742 | * @type: pointer to the uint8_t, will hold type numeric id |
| 743 | * |
| 744 | * fit_image_get_type() finds type property in a given component image node. |
| 745 | * If the property is found, its (string) value is translated to the numeric |
| 746 | * id which is returned to the caller. |
| 747 | * |
| 748 | * returns: |
| 749 | * 0, on success |
| 750 | * -1, on failure |
| 751 | */ |
| 752 | int fit_image_get_type(const void *fit, int noffset, uint8_t *type) |
| 753 | { |
| 754 | int len; |
| 755 | const void *data; |
| 756 | |
| 757 | /* Get image type name from property data */ |
| 758 | data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); |
| 759 | if (data == NULL) { |
| 760 | fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); |
| 761 | *type = -1; |
| 762 | return -1; |
| 763 | } |
| 764 | |
| 765 | /* Translate image type name to id */ |
| 766 | *type = genimg_get_type_id(data); |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * fit_image_get_comp - get comp id for a given component image node |
| 772 | * @fit: pointer to the FIT format image header |
| 773 | * @noffset: component image node offset |
| 774 | * @comp: pointer to the uint8_t, will hold comp numeric id |
| 775 | * |
| 776 | * fit_image_get_comp() finds comp property in a given component image node. |
| 777 | * If the property is found, its (string) value is translated to the numeric |
| 778 | * id which is returned to the caller. |
| 779 | * |
| 780 | * returns: |
| 781 | * 0, on success |
| 782 | * -1, on failure |
| 783 | */ |
| 784 | int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) |
| 785 | { |
| 786 | int len; |
| 787 | const void *data; |
| 788 | |
| 789 | /* Get compression name from property data */ |
| 790 | data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); |
| 791 | if (data == NULL) { |
| 792 | fit_get_debug(fit, noffset, FIT_COMP_PROP, len); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | /* Translate compression name to id */ |
| 797 | *comp = genimg_get_comp_id(data); |
| 798 | return 0; |
| 799 | } |
| 800 | |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 801 | /** |
| 802 | * fit_image_get_phase() - get the phase for a configuration node |
| 803 | * @fit: pointer to the FIT format image header |
| 804 | * @offset: configuration-node offset |
| 805 | * @phasep: returns the phase |
| 806 | * |
| 807 | * Finds the phase property in a given configuration node. If the property is |
| 808 | * found, its (string) value is translated to the numeric id which is returned |
| 809 | * to the caller. |
| 810 | * |
| 811 | * Returns: 0 on success, -ENOENT if missing, -EINVAL for invalid value |
| 812 | */ |
| 813 | int fit_image_get_phase(const void *fit, int offset, enum image_phase_t *phasep) |
| 814 | { |
| 815 | const void *data; |
| 816 | int len, ret; |
| 817 | |
| 818 | /* Get phase name from property data */ |
| 819 | data = fdt_getprop(fit, offset, FIT_PHASE_PROP, &len); |
| 820 | if (!data) { |
| 821 | fit_get_debug(fit, offset, FIT_PHASE_PROP, len); |
| 822 | *phasep = 0; |
| 823 | return -ENOENT; |
| 824 | } |
| 825 | |
| 826 | /* Translate phase name to id */ |
| 827 | ret = genimg_get_phase_id(data); |
| 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | *phasep = ret; |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 835 | static int fit_image_get_address(const void *fit, int noffset, char *name, |
| 836 | ulong *load) |
| 837 | { |
York Sun | 31e5add | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 838 | int len, cell_len; |
| 839 | const fdt32_t *cell; |
| 840 | uint64_t load64 = 0; |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 841 | |
York Sun | 31e5add | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 842 | cell = fdt_getprop(fit, noffset, name, &len); |
| 843 | if (cell == NULL) { |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 844 | fit_get_debug(fit, noffset, name, len); |
| 845 | return -1; |
| 846 | } |
| 847 | |
York Sun | 31e5add | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 848 | cell_len = len >> 2; |
| 849 | /* Use load64 to avoid compiling warning for 32-bit target */ |
| 850 | while (cell_len--) { |
| 851 | load64 = (load64 << 32) | uimage_to_cpu(*cell); |
| 852 | cell++; |
| 853 | } |
Michal Simek | d31b40f | 2020-09-03 12:44:51 +0200 | [diff] [blame] | 854 | |
| 855 | if (len > sizeof(ulong) && (uint32_t)(load64 >> 32)) { |
| 856 | printf("Unsupported %s address size\n", name); |
| 857 | return -1; |
| 858 | } |
| 859 | |
York Sun | 31e5add | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 860 | *load = (ulong)load64; |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 861 | |
| 862 | return 0; |
| 863 | } |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 864 | /** |
| 865 | * fit_image_get_load() - get load addr property for given component image node |
| 866 | * @fit: pointer to the FIT format image header |
| 867 | * @noffset: component image node offset |
| 868 | * @load: pointer to the uint32_t, will hold load address |
| 869 | * |
| 870 | * fit_image_get_load() finds load address property in a given component |
| 871 | * image node. If the property is found, its value is returned to the caller. |
| 872 | * |
| 873 | * returns: |
| 874 | * 0, on success |
| 875 | * -1, on failure |
| 876 | */ |
| 877 | int fit_image_get_load(const void *fit, int noffset, ulong *load) |
| 878 | { |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 879 | return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /** |
| 883 | * fit_image_get_entry() - get entry point address property |
| 884 | * @fit: pointer to the FIT format image header |
| 885 | * @noffset: component image node offset |
| 886 | * @entry: pointer to the uint32_t, will hold entry point address |
| 887 | * |
| 888 | * This gets the entry point address property for a given component image |
| 889 | * node. |
| 890 | * |
| 891 | * fit_image_get_entry() finds entry point address property in a given |
| 892 | * component image node. If the property is found, its value is returned |
| 893 | * to the caller. |
| 894 | * |
| 895 | * returns: |
| 896 | * 0, on success |
| 897 | * -1, on failure |
| 898 | */ |
| 899 | int fit_image_get_entry(const void *fit, int noffset, ulong *entry) |
| 900 | { |
York Sun | 2b0464e | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 901 | return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /** |
| 905 | * fit_image_get_data - get data property and its size for a given component image node |
| 906 | * @fit: pointer to the FIT format image header |
| 907 | * @noffset: component image node offset |
| 908 | * @data: double pointer to void, will hold data property's data address |
| 909 | * @size: pointer to size_t, will hold data property's data size |
| 910 | * |
| 911 | * fit_image_get_data() finds data property in a given component image node. |
| 912 | * If the property is found its data start address and size are returned to |
| 913 | * the caller. |
| 914 | * |
| 915 | * returns: |
| 916 | * 0, on success |
| 917 | * -1, on failure |
| 918 | */ |
| 919 | int fit_image_get_data(const void *fit, int noffset, |
| 920 | const void **data, size_t *size) |
| 921 | { |
| 922 | int len; |
| 923 | |
| 924 | *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); |
| 925 | if (*data == NULL) { |
| 926 | fit_get_debug(fit, noffset, FIT_DATA_PROP, len); |
| 927 | *size = 0; |
| 928 | return -1; |
| 929 | } |
| 930 | |
| 931 | *size = len; |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | /** |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 936 | * Get 'data-offset' property from a given image node. |
| 937 | * |
| 938 | * @fit: pointer to the FIT image header |
| 939 | * @noffset: component image node offset |
| 940 | * @data_offset: holds the data-offset property |
| 941 | * |
| 942 | * returns: |
| 943 | * 0, on success |
| 944 | * -ENOENT if the property could not be found |
| 945 | */ |
| 946 | int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset) |
| 947 | { |
| 948 | const fdt32_t *val; |
| 949 | |
| 950 | val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL); |
| 951 | if (!val) |
| 952 | return -ENOENT; |
| 953 | |
| 954 | *data_offset = fdt32_to_cpu(*val); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | /** |
Peng Fan | 8876c7e | 2017-12-05 13:20:59 +0800 | [diff] [blame] | 960 | * Get 'data-position' property from a given image node. |
| 961 | * |
| 962 | * @fit: pointer to the FIT image header |
| 963 | * @noffset: component image node offset |
| 964 | * @data_position: holds the data-position property |
| 965 | * |
| 966 | * returns: |
| 967 | * 0, on success |
| 968 | * -ENOENT if the property could not be found |
| 969 | */ |
| 970 | int fit_image_get_data_position(const void *fit, int noffset, |
| 971 | int *data_position) |
| 972 | { |
| 973 | const fdt32_t *val; |
| 974 | |
| 975 | val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL); |
| 976 | if (!val) |
| 977 | return -ENOENT; |
| 978 | |
| 979 | *data_position = fdt32_to_cpu(*val); |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | /** |
tomas.melin@vaisala.com | 45ee790 | 2017-01-13 13:20:14 +0200 | [diff] [blame] | 985 | * Get 'data-size' property from a given image node. |
| 986 | * |
| 987 | * @fit: pointer to the FIT image header |
| 988 | * @noffset: component image node offset |
| 989 | * @data_size: holds the data-size property |
| 990 | * |
| 991 | * returns: |
| 992 | * 0, on success |
| 993 | * -ENOENT if the property could not be found |
| 994 | */ |
| 995 | int fit_image_get_data_size(const void *fit, int noffset, int *data_size) |
| 996 | { |
| 997 | const fdt32_t *val; |
| 998 | |
| 999 | val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL); |
| 1000 | if (!val) |
| 1001 | return -ENOENT; |
| 1002 | |
| 1003 | *data_size = fdt32_to_cpu(*val); |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
Philippe Reynes | 3d96470 | 2019-12-18 18:25:42 +0100 | [diff] [blame] | 1009 | * Get 'data-size-unciphered' property from a given image node. |
| 1010 | * |
| 1011 | * @fit: pointer to the FIT image header |
| 1012 | * @noffset: component image node offset |
| 1013 | * @data_size: holds the data-size property |
| 1014 | * |
| 1015 | * returns: |
| 1016 | * 0, on success |
| 1017 | * -ENOENT if the property could not be found |
| 1018 | */ |
| 1019 | int fit_image_get_data_size_unciphered(const void *fit, int noffset, |
| 1020 | size_t *data_size) |
| 1021 | { |
| 1022 | const fdt32_t *val; |
| 1023 | |
| 1024 | val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL); |
| 1025 | if (!val) |
| 1026 | return -ENOENT; |
| 1027 | |
| 1028 | *data_size = (size_t)fdt32_to_cpu(*val); |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | /** |
Kelvin Cheung | 186cc99 | 2018-05-19 18:21:37 +0800 | [diff] [blame] | 1034 | * fit_image_get_data_and_size - get data and its size including |
| 1035 | * both embedded and external data |
| 1036 | * @fit: pointer to the FIT format image header |
| 1037 | * @noffset: component image node offset |
| 1038 | * @data: double pointer to void, will hold data property's data address |
| 1039 | * @size: pointer to size_t, will hold data property's data size |
| 1040 | * |
| 1041 | * fit_image_get_data_and_size() finds data and its size including |
| 1042 | * both embedded and external data. If the property is found |
| 1043 | * its data start address and size are returned to the caller. |
| 1044 | * |
| 1045 | * returns: |
| 1046 | * 0, on success |
| 1047 | * otherwise, on failure |
| 1048 | */ |
| 1049 | int fit_image_get_data_and_size(const void *fit, int noffset, |
| 1050 | const void **data, size_t *size) |
| 1051 | { |
| 1052 | bool external_data = false; |
| 1053 | int offset; |
| 1054 | int len; |
| 1055 | int ret; |
| 1056 | |
| 1057 | if (!fit_image_get_data_position(fit, noffset, &offset)) { |
| 1058 | external_data = true; |
| 1059 | } else if (!fit_image_get_data_offset(fit, noffset, &offset)) { |
| 1060 | external_data = true; |
| 1061 | /* |
| 1062 | * For FIT with external data, figure out where |
| 1063 | * the external images start. This is the base |
| 1064 | * for the data-offset properties in each image. |
| 1065 | */ |
| 1066 | offset += ((fdt_totalsize(fit) + 3) & ~3); |
| 1067 | } |
| 1068 | |
| 1069 | if (external_data) { |
| 1070 | debug("External Data\n"); |
| 1071 | ret = fit_image_get_data_size(fit, noffset, &len); |
Heinrich Schuchardt | 0be986f | 2020-03-11 21:51:08 +0100 | [diff] [blame] | 1072 | if (!ret) { |
| 1073 | *data = fit + offset; |
| 1074 | *size = len; |
| 1075 | } |
Kelvin Cheung | 186cc99 | 2018-05-19 18:21:37 +0800 | [diff] [blame] | 1076 | } else { |
| 1077 | ret = fit_image_get_data(fit, noffset, data, size); |
| 1078 | } |
| 1079 | |
| 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | /** |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1084 | * fit_image_hash_get_algo - get hash algorithm name |
| 1085 | * @fit: pointer to the FIT format image header |
| 1086 | * @noffset: hash node offset |
| 1087 | * @algo: double pointer to char, will hold pointer to the algorithm name |
| 1088 | * |
| 1089 | * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. |
| 1090 | * If the property is found its data start address is returned to the caller. |
| 1091 | * |
| 1092 | * returns: |
| 1093 | * 0, on success |
| 1094 | * -1, on failure |
| 1095 | */ |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 1096 | int fit_image_hash_get_algo(const void *fit, int noffset, const char **algo) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1097 | { |
| 1098 | int len; |
| 1099 | |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 1100 | *algo = (const char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1101 | if (*algo == NULL) { |
| 1102 | fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); |
| 1103 | return -1; |
| 1104 | } |
| 1105 | |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * fit_image_hash_get_value - get hash value and length |
| 1111 | * @fit: pointer to the FIT format image header |
| 1112 | * @noffset: hash node offset |
| 1113 | * @value: double pointer to uint8_t, will hold address of a hash value data |
| 1114 | * @value_len: pointer to an int, will hold hash data length |
| 1115 | * |
| 1116 | * fit_image_hash_get_value() finds hash value property in a given hash node. |
| 1117 | * If the property is found its data start address and size are returned to |
| 1118 | * the caller. |
| 1119 | * |
| 1120 | * returns: |
| 1121 | * 0, on success |
| 1122 | * -1, on failure |
| 1123 | */ |
| 1124 | int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, |
| 1125 | int *value_len) |
| 1126 | { |
| 1127 | int len; |
| 1128 | |
| 1129 | *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); |
| 1130 | if (*value == NULL) { |
| 1131 | fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); |
| 1132 | *value_len = 0; |
| 1133 | return -1; |
| 1134 | } |
| 1135 | |
| 1136 | *value_len = len; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1140 | /** |
| 1141 | * fit_image_hash_get_ignore - get hash ignore flag |
| 1142 | * @fit: pointer to the FIT format image header |
| 1143 | * @noffset: hash node offset |
| 1144 | * @ignore: pointer to an int, will hold hash ignore flag |
| 1145 | * |
| 1146 | * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. |
| 1147 | * If the property is found and non-zero, the hash algorithm is not verified by |
| 1148 | * u-boot automatically. |
| 1149 | * |
| 1150 | * returns: |
| 1151 | * 0, on ignore not found |
| 1152 | * value, on ignore found |
| 1153 | */ |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1154 | static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1155 | { |
| 1156 | int len; |
| 1157 | int *value; |
| 1158 | |
| 1159 | value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); |
| 1160 | if (value == NULL || len != sizeof(int)) |
| 1161 | *ignore = 0; |
| 1162 | else |
| 1163 | *ignore = *value; |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1167 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 1168 | /** |
| 1169 | * fit_image_cipher_get_algo - get cipher algorithm name |
| 1170 | * @fit: pointer to the FIT format image header |
| 1171 | * @noffset: cipher node offset |
| 1172 | * @algo: double pointer to char, will hold pointer to the algorithm name |
| 1173 | * |
| 1174 | * fit_image_cipher_get_algo() finds cipher algorithm property in a given |
| 1175 | * cipher node. If the property is found its data start address is returned |
| 1176 | * to the caller. |
| 1177 | * |
| 1178 | * returns: |
| 1179 | * 0, on success |
| 1180 | * -1, on failure |
| 1181 | */ |
| 1182 | int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo) |
| 1183 | { |
| 1184 | int len; |
| 1185 | |
| 1186 | *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); |
| 1187 | if (!*algo) { |
| 1188 | fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); |
| 1189 | return -1; |
| 1190 | } |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Simon Glass | 5b539a0 | 2016-02-24 09:14:42 -0700 | [diff] [blame] | 1195 | ulong fit_get_end(const void *fit) |
| 1196 | { |
| 1197 | return map_to_sysmem((void *)(fit + fdt_totalsize(fit))); |
| 1198 | } |
| 1199 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1200 | /** |
| 1201 | * fit_set_timestamp - set node timestamp property |
| 1202 | * @fit: pointer to the FIT format image header |
| 1203 | * @noffset: node offset |
| 1204 | * @timestamp: timestamp value to be set |
| 1205 | * |
| 1206 | * fit_set_timestamp() attempts to set timestamp property in the requested |
| 1207 | * node and returns operation status to the caller. |
| 1208 | * |
| 1209 | * returns: |
| 1210 | * 0, on success |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 1211 | * -ENOSPC if no space in device tree, -1 for other error |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1212 | */ |
| 1213 | int fit_set_timestamp(void *fit, int noffset, time_t timestamp) |
| 1214 | { |
| 1215 | uint32_t t; |
| 1216 | int ret; |
| 1217 | |
| 1218 | t = cpu_to_uimage(timestamp); |
| 1219 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, |
| 1220 | sizeof(uint32_t)); |
| 1221 | if (ret) { |
Simon Glass | e4016fa | 2016-05-01 13:55:37 -0600 | [diff] [blame] | 1222 | debug("Can't set '%s' property for '%s' node (%s)\n", |
| 1223 | FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), |
| 1224 | fdt_strerror(ret)); |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 1225 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * calculate_hash - calculate and return hash for provided input data |
| 1233 | * @data: pointer to the input data |
| 1234 | * @data_len: data length |
Chia-Wei Wang | 311c1f2 | 2021-10-27 14:17:24 +0800 | [diff] [blame] | 1235 | * @name: requested hash algorithm name |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1236 | * @value: pointer to the char, will hold hash value data (caller must |
| 1237 | * allocate enough free space) |
| 1238 | * value_len: length of the calculated hash |
| 1239 | * |
| 1240 | * calculate_hash() computes input data hash according to the requested |
| 1241 | * algorithm. |
| 1242 | * Resulting hash value is placed in caller provided 'value' buffer, length |
| 1243 | * of the calculated hash is returned via value_len pointer argument. |
| 1244 | * |
| 1245 | * returns: |
| 1246 | * 0, on success |
| 1247 | * -1, when algo is unsupported |
| 1248 | */ |
Alexandru Gagniuc | 650b786 | 2021-09-02 19:54:21 -0500 | [diff] [blame] | 1249 | int calculate_hash(const void *data, int data_len, const char *name, |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1250 | uint8_t *value, int *value_len) |
| 1251 | { |
Chia-Wei Wang | 4a733b3 | 2021-07-30 09:08:05 +0800 | [diff] [blame] | 1252 | #if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH) |
| 1253 | int rc; |
| 1254 | enum HASH_ALGO hash_algo; |
| 1255 | struct udevice *dev; |
| 1256 | |
| 1257 | rc = uclass_get_device(UCLASS_HASH, 0, &dev); |
| 1258 | if (rc) { |
| 1259 | debug("failed to get hash device, rc=%d\n", rc); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
Chia-Wei Wang | 311c1f2 | 2021-10-27 14:17:24 +0800 | [diff] [blame] | 1263 | hash_algo = hash_algo_lookup_by_name(name); |
Chia-Wei Wang | 4a733b3 | 2021-07-30 09:08:05 +0800 | [diff] [blame] | 1264 | if (hash_algo == HASH_ALGO_INVALID) { |
| 1265 | debug("Unsupported hash algorithm\n"); |
| 1266 | return -1; |
| 1267 | }; |
| 1268 | |
| 1269 | rc = hash_digest_wd(dev, hash_algo, data, data_len, value, CHUNKSZ); |
| 1270 | if (rc) { |
| 1271 | debug("failed to get hash value, rc=%d\n", rc); |
| 1272 | return -1; |
| 1273 | } |
| 1274 | |
| 1275 | *value_len = hash_algo_digest_size(hash_algo); |
| 1276 | #else |
Alexandru Gagniuc | 650b786 | 2021-09-02 19:54:21 -0500 | [diff] [blame] | 1277 | struct hash_algo *algo; |
| 1278 | int ret; |
| 1279 | |
| 1280 | ret = hash_lookup_algo(name, &algo); |
| 1281 | if (ret < 0) { |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1282 | debug("Unsupported hash alogrithm\n"); |
| 1283 | return -1; |
| 1284 | } |
Alexandru Gagniuc | 650b786 | 2021-09-02 19:54:21 -0500 | [diff] [blame] | 1285 | |
| 1286 | algo->hash_func_ws(data, data_len, value, algo->chunk_size); |
| 1287 | *value_len = algo->digest_size; |
Chia-Wei Wang | 4a733b3 | 2021-07-30 09:08:05 +0800 | [diff] [blame] | 1288 | #endif |
Alexandru Gagniuc | 650b786 | 2021-09-02 19:54:21 -0500 | [diff] [blame] | 1289 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1293 | static int fit_image_check_hash(const void *fit, int noffset, const void *data, |
| 1294 | size_t size, char **err_msgp) |
| 1295 | { |
Joel Stanley | 447cc7a | 2022-06-20 16:31:17 +0930 | [diff] [blame] | 1296 | ALLOC_CACHE_ALIGN_BUFFER(uint8_t, value, FIT_MAX_HASH_LEN); |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1297 | int value_len; |
Jan Kiszka | 27beec2 | 2022-01-14 10:21:17 +0100 | [diff] [blame] | 1298 | const char *algo; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1299 | uint8_t *fit_value; |
| 1300 | int fit_value_len; |
| 1301 | int ignore; |
| 1302 | |
| 1303 | *err_msgp = NULL; |
| 1304 | |
| 1305 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1306 | *err_msgp = "Can't get hash algo property"; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1307 | return -1; |
| 1308 | } |
| 1309 | printf("%s", algo); |
| 1310 | |
Simon Glass | a0cfb84 | 2021-09-25 19:43:28 -0600 | [diff] [blame] | 1311 | if (!tools_build()) { |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1312 | fit_image_hash_get_ignore(fit, noffset, &ignore); |
| 1313 | if (ignore) { |
| 1314 | printf("-skipped "); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 1320 | &fit_value_len)) { |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1321 | *err_msgp = "Can't get hash value property"; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1322 | return -1; |
| 1323 | } |
| 1324 | |
| 1325 | if (calculate_hash(data, size, algo, value, &value_len)) { |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1326 | *err_msgp = "Unsupported hash algorithm"; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1327 | return -1; |
| 1328 | } |
| 1329 | |
| 1330 | if (value_len != fit_value_len) { |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1331 | *err_msgp = "Bad hash value len"; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1332 | return -1; |
| 1333 | } else if (memcmp(value, fit_value, value_len) != 0) { |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1334 | *err_msgp = "Bad hash value"; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1335 | return -1; |
| 1336 | } |
| 1337 | |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1341 | int fit_image_verify_with_data(const void *fit, int image_noffset, |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 1342 | const void *key_blob, const void *data, |
| 1343 | size_t size) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1344 | { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1345 | int noffset = 0; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1346 | char *err_msg = ""; |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1347 | int verify_all = 1; |
| 1348 | int ret; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1349 | |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1350 | /* Verify all required signatures */ |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 1351 | if (FIT_IMAGE_ENABLE_VERIFY && |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1352 | fit_image_verify_required_sigs(fit, image_noffset, data, size, |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 1353 | key_blob, &verify_all)) { |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1354 | err_msg = "Unable to verify required signature"; |
| 1355 | goto error; |
| 1356 | } |
| 1357 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1358 | /* Process all hash subnodes of the component image node */ |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 1359 | fdt_for_each_subnode(noffset, fit, image_noffset) { |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1360 | const char *name = fit_get_name(fit, noffset, NULL); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1361 | |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1362 | /* |
| 1363 | * Check subnode name, must be equal to "hash". |
| 1364 | * Multiple hash nodes require unique unit node |
Andre Przywara | 3234ecd | 2017-12-04 02:05:10 +0000 | [diff] [blame] | 1365 | * names, e.g. hash-1, hash-2, etc. |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1366 | */ |
| 1367 | if (!strncmp(name, FIT_HASH_NODENAME, |
| 1368 | strlen(FIT_HASH_NODENAME))) { |
| 1369 | if (fit_image_check_hash(fit, noffset, data, size, |
| 1370 | &err_msg)) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1371 | goto error; |
Simon Glass | f0fd511 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1372 | puts("+ "); |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 1373 | } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all && |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1374 | !strncmp(name, FIT_SIG_NODENAME, |
| 1375 | strlen(FIT_SIG_NODENAME))) { |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 1376 | ret = fit_image_check_sig(fit, noffset, data, size, |
| 1377 | gd_fdt_blob(), -1, &err_msg); |
Simon Glass | 51026aa | 2016-02-24 09:14:43 -0700 | [diff] [blame] | 1378 | |
| 1379 | /* |
| 1380 | * Show an indication on failure, but do not return |
| 1381 | * an error. Only keys marked 'required' can cause |
| 1382 | * an image validation failure. See the call to |
| 1383 | * fit_image_verify_required_sigs() above. |
| 1384 | */ |
| 1385 | if (ret) |
Simon Glass | fbabc0f | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1386 | puts("- "); |
| 1387 | else |
| 1388 | puts("+ "); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1393 | err_msg = "Corrupted or truncated tree"; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1394 | goto error; |
| 1395 | } |
| 1396 | |
| 1397 | return 1; |
| 1398 | |
| 1399 | error: |
Simon Glass | 8f3aa46 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1400 | printf(" error!\n%s for '%s' hash node in '%s' image node\n", |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1401 | err_msg, fit_get_name(fit, noffset, NULL), |
| 1402 | fit_get_name(fit, image_noffset, NULL)); |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | /** |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1407 | * fit_image_verify - verify data integrity |
| 1408 | * @fit: pointer to the FIT format image header |
| 1409 | * @image_noffset: component image node offset |
| 1410 | * |
| 1411 | * fit_image_verify() goes over component image hash nodes, |
| 1412 | * re-calculates each data hash and compares with the value stored in hash |
| 1413 | * node. |
| 1414 | * |
| 1415 | * returns: |
| 1416 | * 1, if all hashes are valid |
| 1417 | * 0, otherwise (or on error) |
| 1418 | */ |
| 1419 | int fit_image_verify(const void *fit, int image_noffset) |
| 1420 | { |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 1421 | const char *name = fit_get_name(fit, image_noffset, NULL); |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1422 | const void *data; |
| 1423 | size_t size; |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1424 | char *err_msg = ""; |
| 1425 | |
Simon Glass | 3d70218 | 2021-07-05 16:32:56 -0600 | [diff] [blame] | 1426 | if (IS_ENABLED(CONFIG_FIT_SIGNATURE) && strchr(name, '@')) { |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 1427 | /* |
| 1428 | * We don't support this since libfdt considers names with the |
| 1429 | * name root but different @ suffix to be equal |
| 1430 | */ |
| 1431 | err_msg = "Node name contains @"; |
| 1432 | goto err; |
| 1433 | } |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1434 | /* Get image data and data length */ |
Kelvin Cheung | 186cc99 | 2018-05-19 18:21:37 +0800 | [diff] [blame] | 1435 | if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) { |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1436 | err_msg = "Can't get image data/size"; |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 1437 | goto err; |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Simon Glass | e10e2ee | 2021-11-12 12:28:10 -0700 | [diff] [blame] | 1440 | return fit_image_verify_with_data(fit, image_noffset, gd_fdt_blob(), |
| 1441 | data, size); |
Simon Glass | 4433890 | 2021-02-15 17:08:06 -0700 | [diff] [blame] | 1442 | |
| 1443 | err: |
| 1444 | printf("error!\n%s in '%s' image node\n", err_msg, |
| 1445 | fit_get_name(fit, image_noffset, NULL)); |
| 1446 | return 0; |
Jun Nie | adf5d1c | 2018-02-27 16:55:58 +0800 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /** |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 1450 | * fit_all_image_verify - verify data integrity for all images |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1451 | * @fit: pointer to the FIT format image header |
| 1452 | * |
Simon Glass | 7428ad1 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1453 | * fit_all_image_verify() goes over all images in the FIT and |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1454 | * for every images checks if all it's hashes are valid. |
| 1455 | * |
| 1456 | * returns: |
| 1457 | * 1, if all hashes of all images are valid |
| 1458 | * 0, otherwise (or on error) |
| 1459 | */ |
Simon Glass | 7428ad1 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1460 | int fit_all_image_verify(const void *fit) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1461 | { |
| 1462 | int images_noffset; |
| 1463 | int noffset; |
| 1464 | int ndepth; |
| 1465 | int count; |
| 1466 | |
| 1467 | /* Find images parent node offset */ |
| 1468 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1469 | if (images_noffset < 0) { |
| 1470 | printf("Can't find images parent node '%s' (%s)\n", |
| 1471 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | /* Process all image subnodes, check hashes for each */ |
| 1476 | printf("## Checking hash(es) for FIT Image at %08lx ...\n", |
| 1477 | (ulong)fit); |
| 1478 | for (ndepth = 0, count = 0, |
| 1479 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 1480 | (noffset >= 0) && (ndepth > 0); |
| 1481 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 1482 | if (ndepth == 1) { |
| 1483 | /* |
| 1484 | * Direct child node of the images parent node, |
| 1485 | * i.e. component image node. |
| 1486 | */ |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 1487 | printf(" Hash(es) for Image %u (%s): ", count, |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1488 | fit_get_name(fit, noffset, NULL)); |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 1489 | count++; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1490 | |
Simon Glass | 7428ad1 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1491 | if (!fit_image_verify(fit, noffset)) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1492 | return 0; |
| 1493 | printf("\n"); |
| 1494 | } |
| 1495 | } |
| 1496 | return 1; |
| 1497 | } |
| 1498 | |
Philippe Reynes | 3d96470 | 2019-12-18 18:25:42 +0100 | [diff] [blame] | 1499 | static int fit_image_uncipher(const void *fit, int image_noffset, |
| 1500 | void **data, size_t *size) |
| 1501 | { |
| 1502 | int cipher_noffset, ret; |
| 1503 | void *dst; |
| 1504 | size_t size_dst; |
| 1505 | |
| 1506 | cipher_noffset = fdt_subnode_offset(fit, image_noffset, |
| 1507 | FIT_CIPHER_NODENAME); |
| 1508 | if (cipher_noffset < 0) |
| 1509 | return 0; |
| 1510 | |
| 1511 | ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset, |
| 1512 | *data, *size, &dst, &size_dst); |
| 1513 | if (ret) |
| 1514 | goto out; |
| 1515 | |
| 1516 | *data = dst; |
| 1517 | *size = size_dst; |
| 1518 | |
| 1519 | out: |
| 1520 | return ret; |
| 1521 | } |
Philippe Reynes | 3d96470 | 2019-12-18 18:25:42 +0100 | [diff] [blame] | 1522 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1523 | /** |
| 1524 | * fit_image_check_os - check whether image node is of a given os type |
| 1525 | * @fit: pointer to the FIT format image header |
| 1526 | * @noffset: component image node offset |
| 1527 | * @os: requested image os |
| 1528 | * |
| 1529 | * fit_image_check_os() reads image os property and compares its numeric |
| 1530 | * id with the requested os. Comparison result is returned to the caller. |
| 1531 | * |
| 1532 | * returns: |
| 1533 | * 1 if image is of given os type |
| 1534 | * 0 otherwise (or on error) |
| 1535 | */ |
| 1536 | int fit_image_check_os(const void *fit, int noffset, uint8_t os) |
| 1537 | { |
| 1538 | uint8_t image_os; |
| 1539 | |
| 1540 | if (fit_image_get_os(fit, noffset, &image_os)) |
| 1541 | return 0; |
| 1542 | return (os == image_os); |
| 1543 | } |
| 1544 | |
| 1545 | /** |
| 1546 | * fit_image_check_arch - check whether image node is of a given arch |
| 1547 | * @fit: pointer to the FIT format image header |
| 1548 | * @noffset: component image node offset |
| 1549 | * @arch: requested imagearch |
| 1550 | * |
| 1551 | * fit_image_check_arch() reads image arch property and compares its numeric |
| 1552 | * id with the requested arch. Comparison result is returned to the caller. |
| 1553 | * |
| 1554 | * returns: |
| 1555 | * 1 if image is of given arch |
| 1556 | * 0 otherwise (or on error) |
| 1557 | */ |
| 1558 | int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) |
| 1559 | { |
| 1560 | uint8_t image_arch; |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 1561 | int aarch32_support = 0; |
| 1562 | |
Simon Glass | 062e79f | 2021-03-15 18:11:12 +1300 | [diff] [blame] | 1563 | /* Let's assume that sandbox can load any architecture */ |
| 1564 | if (IS_ENABLED(CONFIG_SANDBOX)) |
| 1565 | return true; |
| 1566 | |
Sebastian Reichel | bfe8ddf | 2021-01-04 20:48:03 +0100 | [diff] [blame] | 1567 | if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32)) |
| 1568 | aarch32_support = 1; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1569 | |
| 1570 | if (fit_image_get_arch(fit, noffset, &image_arch)) |
| 1571 | return 0; |
Simon Glass | 9d42830 | 2014-10-10 08:21:57 -0600 | [diff] [blame] | 1572 | return (arch == image_arch) || |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 1573 | (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) || |
| 1574 | (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM && |
| 1575 | aarch32_support); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * fit_image_check_type - check whether image node is of a given type |
| 1580 | * @fit: pointer to the FIT format image header |
| 1581 | * @noffset: component image node offset |
| 1582 | * @type: requested image type |
| 1583 | * |
| 1584 | * fit_image_check_type() reads image type property and compares its numeric |
| 1585 | * id with the requested type. Comparison result is returned to the caller. |
| 1586 | * |
| 1587 | * returns: |
| 1588 | * 1 if image is of given type |
| 1589 | * 0 otherwise (or on error) |
| 1590 | */ |
| 1591 | int fit_image_check_type(const void *fit, int noffset, uint8_t type) |
| 1592 | { |
| 1593 | uint8_t image_type; |
| 1594 | |
| 1595 | if (fit_image_get_type(fit, noffset, &image_type)) |
| 1596 | return 0; |
| 1597 | return (type == image_type); |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * fit_image_check_comp - check whether image node uses given compression |
| 1602 | * @fit: pointer to the FIT format image header |
| 1603 | * @noffset: component image node offset |
| 1604 | * @comp: requested image compression type |
| 1605 | * |
| 1606 | * fit_image_check_comp() reads image compression property and compares its |
| 1607 | * numeric id with the requested compression type. Comparison result is |
| 1608 | * returned to the caller. |
| 1609 | * |
| 1610 | * returns: |
| 1611 | * 1 if image uses requested compression |
| 1612 | * 0 otherwise (or on error) |
| 1613 | */ |
| 1614 | int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) |
| 1615 | { |
| 1616 | uint8_t image_comp; |
| 1617 | |
| 1618 | if (fit_image_get_comp(fit, noffset, &image_comp)) |
| 1619 | return 0; |
| 1620 | return (comp == image_comp); |
| 1621 | } |
| 1622 | |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 1623 | /** |
| 1624 | * fdt_check_no_at() - Check for nodes whose names contain '@' |
| 1625 | * |
| 1626 | * This checks the parent node and all subnodes recursively |
| 1627 | * |
| 1628 | * @fit: FIT to check |
| 1629 | * @parent: Parent node to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1630 | * Return: 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@' |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 1631 | */ |
| 1632 | static int fdt_check_no_at(const void *fit, int parent) |
| 1633 | { |
| 1634 | const char *name; |
| 1635 | int node; |
| 1636 | int ret; |
| 1637 | |
| 1638 | name = fdt_get_name(fit, parent, NULL); |
| 1639 | if (!name || strchr(name, '@')) |
| 1640 | return -EADDRNOTAVAIL; |
| 1641 | |
| 1642 | fdt_for_each_subnode(node, fit, parent) { |
| 1643 | ret = fdt_check_no_at(fit, node); |
| 1644 | if (ret) |
| 1645 | return ret; |
| 1646 | } |
| 1647 | |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1651 | int fit_check_format(const void *fit, ulong size) |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1652 | { |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1653 | int ret; |
| 1654 | |
Heinrich Schuchardt | ef98b21 | 2021-01-13 02:09:12 +0100 | [diff] [blame] | 1655 | /* A FIT image must be a valid FDT */ |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1656 | ret = fdt_check_header(fit); |
| 1657 | if (ret) { |
| 1658 | log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n", |
| 1659 | ret); |
| 1660 | return -ENOEXEC; |
Heinrich Schuchardt | ef98b21 | 2021-01-13 02:09:12 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
Simon Glass | 244705b | 2021-02-15 17:08:10 -0700 | [diff] [blame] | 1663 | if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) { |
| 1664 | /* |
| 1665 | * If we are not given the size, make do wtih calculating it. |
| 1666 | * This is not as secure, so we should consider a flag to |
| 1667 | * control this. |
| 1668 | */ |
| 1669 | if (size == IMAGE_SIZE_INVAL) |
| 1670 | size = fdt_totalsize(fit); |
| 1671 | ret = fdt_check_full(fit, size); |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 1672 | if (ret) |
| 1673 | ret = -EINVAL; |
| 1674 | |
| 1675 | /* |
| 1676 | * U-Boot stopped using unit addressed in 2017. Since libfdt |
| 1677 | * can match nodes ignoring any unit address, signature |
| 1678 | * verification can see the wrong node if one is inserted with |
| 1679 | * the same name as a valid node but with a unit address |
| 1680 | * attached. Protect against this by disallowing unit addresses. |
| 1681 | */ |
| 1682 | if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) { |
| 1683 | ret = fdt_check_no_at(fit, 0); |
Simon Glass | 244705b | 2021-02-15 17:08:10 -0700 | [diff] [blame] | 1684 | |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 1685 | if (ret) { |
| 1686 | log_debug("FIT check error %d\n", ret); |
| 1687 | return ret; |
| 1688 | } |
| 1689 | } |
Simon Glass | 244705b | 2021-02-15 17:08:10 -0700 | [diff] [blame] | 1690 | if (ret) { |
| 1691 | log_debug("FIT check error %d\n", ret); |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 1692 | return ret; |
Simon Glass | 244705b | 2021-02-15 17:08:10 -0700 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1696 | /* mandatory / node 'description' property */ |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1697 | if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) { |
| 1698 | log_debug("Wrong FIT format: no description\n"); |
| 1699 | return -ENOMSG; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 1703 | /* mandatory / node 'timestamp' property */ |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1704 | if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) { |
| 1705 | log_debug("Wrong FIT format: no timestamp\n"); |
Simon Glass | b641de8 | 2021-02-24 08:50:32 -0500 | [diff] [blame] | 1706 | return -EBADMSG; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /* mandatory subimages parent '/images' node */ |
| 1711 | if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1712 | log_debug("Wrong FIT format: no images parent node\n"); |
| 1713 | return -ENOENT; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 1716 | return 0; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1719 | int fit_conf_find_compat(const void *fit, const void *fdt) |
| 1720 | { |
| 1721 | int ndepth = 0; |
| 1722 | int noffset, confs_noffset, images_noffset; |
| 1723 | const void *fdt_compat; |
| 1724 | int fdt_compat_len; |
| 1725 | int best_match_offset = 0; |
| 1726 | int best_match_pos = 0; |
| 1727 | |
| 1728 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1729 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1730 | if (confs_noffset < 0 || images_noffset < 0) { |
| 1731 | debug("Can't find configurations or images nodes.\n"); |
| 1732 | return -1; |
| 1733 | } |
| 1734 | |
| 1735 | fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); |
| 1736 | if (!fdt_compat) { |
| 1737 | debug("Fdt for comparison has no \"compatible\" property.\n"); |
| 1738 | return -1; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * Loop over the configurations in the FIT image. |
| 1743 | */ |
| 1744 | for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); |
| 1745 | (noffset >= 0) && (ndepth > 0); |
| 1746 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1747 | const void *fdt; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1748 | const char *kfdt_name; |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1749 | int kfdt_noffset, compat_noffset; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1750 | const char *cur_fdt_compat; |
| 1751 | int len; |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1752 | size_t sz; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1753 | int i; |
| 1754 | |
| 1755 | if (ndepth > 1) |
| 1756 | continue; |
| 1757 | |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1758 | /* If there's a compat property in the config node, use that. */ |
| 1759 | if (fdt_getprop(fit, noffset, "compatible", NULL)) { |
| 1760 | fdt = fit; /* search in FIT image */ |
| 1761 | compat_noffset = noffset; /* search under config node */ |
| 1762 | } else { /* Otherwise extract it from the kernel FDT. */ |
| 1763 | kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); |
| 1764 | if (!kfdt_name) { |
| 1765 | debug("No fdt property found.\n"); |
| 1766 | continue; |
| 1767 | } |
| 1768 | kfdt_noffset = fdt_subnode_offset(fit, images_noffset, |
| 1769 | kfdt_name); |
| 1770 | if (kfdt_noffset < 0) { |
| 1771 | debug("No image node named \"%s\" found.\n", |
| 1772 | kfdt_name); |
| 1773 | continue; |
| 1774 | } |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 1775 | |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1776 | if (!fit_image_check_comp(fit, kfdt_noffset, |
| 1777 | IH_COMP_NONE)) { |
| 1778 | debug("Can't extract compat from \"%s\" " |
| 1779 | "(compressed)\n", kfdt_name); |
| 1780 | continue; |
| 1781 | } |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 1782 | |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1783 | /* search in this config's kernel FDT */ |
John Keeping | 645a312 | 2021-06-25 17:58:04 +0100 | [diff] [blame] | 1784 | if (fit_image_get_data_and_size(fit, kfdt_noffset, |
| 1785 | &fdt, &sz)) { |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1786 | debug("Failed to get fdt \"%s\".\n", kfdt_name); |
| 1787 | continue; |
| 1788 | } |
| 1789 | |
| 1790 | compat_noffset = 0; /* search kFDT under root node */ |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | len = fdt_compat_len; |
| 1794 | cur_fdt_compat = fdt_compat; |
| 1795 | /* |
| 1796 | * Look for a match for each U-Boot compatibility string in |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1797 | * turn in the compat string property. |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1798 | */ |
| 1799 | for (i = 0; len > 0 && |
| 1800 | (!best_match_offset || best_match_pos > i); i++) { |
| 1801 | int cur_len = strlen(cur_fdt_compat) + 1; |
| 1802 | |
Julius Werner | 4e82352 | 2019-07-24 19:37:56 -0700 | [diff] [blame] | 1803 | if (!fdt_node_check_compatible(fdt, compat_noffset, |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1804 | cur_fdt_compat)) { |
| 1805 | best_match_offset = noffset; |
| 1806 | best_match_pos = i; |
| 1807 | break; |
| 1808 | } |
| 1809 | len -= cur_len; |
| 1810 | cur_fdt_compat += cur_len; |
| 1811 | } |
| 1812 | } |
| 1813 | if (!best_match_offset) { |
| 1814 | debug("No match found.\n"); |
| 1815 | return -1; |
| 1816 | } |
| 1817 | |
| 1818 | return best_match_offset; |
| 1819 | } |
| 1820 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1821 | int fit_conf_get_node(const void *fit, const char *conf_uname) |
| 1822 | { |
| 1823 | int noffset, confs_noffset; |
| 1824 | int len; |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 1825 | const char *s; |
| 1826 | char *conf_uname_copy = NULL; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1827 | |
| 1828 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1829 | if (confs_noffset < 0) { |
| 1830 | debug("Can't find configurations parent node '%s' (%s)\n", |
| 1831 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
| 1832 | return confs_noffset; |
| 1833 | } |
| 1834 | |
| 1835 | if (conf_uname == NULL) { |
| 1836 | /* get configuration unit name from the default property */ |
| 1837 | debug("No configuration specified, trying default...\n"); |
Simon Glass | db503fb | 2021-09-25 19:43:14 -0600 | [diff] [blame] | 1838 | if (!tools_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) { |
Sebastian Reichel | 0890f94 | 2021-01-04 20:48:04 +0100 | [diff] [blame] | 1839 | noffset = fit_find_config_node(fit); |
| 1840 | if (noffset < 0) |
| 1841 | return noffset; |
| 1842 | conf_uname = fdt_get_name(fit, noffset, NULL); |
| 1843 | } else { |
| 1844 | conf_uname = (char *)fdt_getprop(fit, confs_noffset, |
| 1845 | FIT_DEFAULT_PROP, &len); |
| 1846 | if (conf_uname == NULL) { |
| 1847 | fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, |
| 1848 | len); |
| 1849 | return len; |
| 1850 | } |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1851 | } |
| 1852 | debug("Found default configuration: '%s'\n", conf_uname); |
| 1853 | } |
| 1854 | |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 1855 | s = strchr(conf_uname, '#'); |
| 1856 | if (s) { |
| 1857 | len = s - conf_uname; |
| 1858 | conf_uname_copy = malloc(len + 1); |
| 1859 | if (!conf_uname_copy) { |
| 1860 | debug("Can't allocate uname copy: '%s'\n", |
| 1861 | conf_uname); |
| 1862 | return -ENOMEM; |
| 1863 | } |
| 1864 | memcpy(conf_uname_copy, conf_uname, len); |
| 1865 | conf_uname_copy[len] = '\0'; |
| 1866 | conf_uname = conf_uname_copy; |
| 1867 | } |
| 1868 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1869 | noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); |
| 1870 | if (noffset < 0) { |
| 1871 | debug("Can't get node offset for configuration unit name: '%s' (%s)\n", |
| 1872 | conf_uname, fdt_strerror(noffset)); |
| 1873 | } |
| 1874 | |
Heinrich Schuchardt | f568646 | 2022-04-11 20:08:03 +0200 | [diff] [blame] | 1875 | free(conf_uname_copy); |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 1876 | |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1877 | return noffset; |
| 1878 | } |
| 1879 | |
Pantelis Antoniou | a488074 | 2017-09-04 23:12:14 +0300 | [diff] [blame] | 1880 | int fit_conf_get_prop_node_count(const void *fit, int noffset, |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1881 | const char *prop_name) |
| 1882 | { |
Pantelis Antoniou | a488074 | 2017-09-04 23:12:14 +0300 | [diff] [blame] | 1883 | return fdt_stringlist_count(fit, noffset, prop_name); |
| 1884 | } |
| 1885 | |
| 1886 | int fit_conf_get_prop_node_index(const void *fit, int noffset, |
| 1887 | const char *prop_name, int index) |
| 1888 | { |
| 1889 | const char *uname; |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1890 | int len; |
| 1891 | |
| 1892 | /* get kernel image unit name from configuration kernel property */ |
Pantelis Antoniou | a488074 | 2017-09-04 23:12:14 +0300 | [diff] [blame] | 1893 | uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len); |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1894 | if (uname == NULL) |
| 1895 | return len; |
| 1896 | |
| 1897 | return fit_image_get_node(fit, uname); |
| 1898 | } |
| 1899 | |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 1900 | int fit_conf_get_prop_node(const void *fit, int noffset, const char *prop_name, |
| 1901 | enum image_phase_t sel_phase) |
Pantelis Antoniou | a488074 | 2017-09-04 23:12:14 +0300 | [diff] [blame] | 1902 | { |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 1903 | int i, count; |
| 1904 | |
| 1905 | if (sel_phase == IH_PHASE_NONE) |
| 1906 | return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0); |
| 1907 | |
| 1908 | count = fit_conf_get_prop_node_count(fit, noffset, prop_name); |
| 1909 | if (count < 0) |
| 1910 | return count; |
| 1911 | |
| 1912 | /* check each image in the list */ |
| 1913 | for (i = 0; i < count; i++) { |
| 1914 | enum image_phase_t phase; |
| 1915 | int ret, node; |
| 1916 | |
| 1917 | node = fit_conf_get_prop_node_index(fit, noffset, prop_name, i); |
| 1918 | ret = fit_image_get_phase(fit, node, &phase); |
| 1919 | |
| 1920 | /* if the image is for any phase, let's use it */ |
| 1921 | if (ret == -ENOENT) |
| 1922 | return node; |
| 1923 | else if (ret < 0) |
| 1924 | return ret; |
| 1925 | |
| 1926 | if (phase == sel_phase) |
| 1927 | return node; |
| 1928 | } |
| 1929 | |
| 1930 | return -ENOENT; |
Pantelis Antoniou | a488074 | 2017-09-04 23:12:14 +0300 | [diff] [blame] | 1931 | } |
Simon Glass | da0af36 | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1932 | |
Sean Anderson | 080d3a0 | 2022-08-16 11:16:03 -0400 | [diff] [blame] | 1933 | static int fit_get_data_tail(const void *fit, int noffset, |
| 1934 | const void **data, size_t *size) |
| 1935 | { |
| 1936 | char *desc; |
| 1937 | |
| 1938 | if (noffset < 0) |
| 1939 | return noffset; |
| 1940 | |
| 1941 | if (!fit_image_verify(fit, noffset)) |
| 1942 | return -EINVAL; |
| 1943 | |
| 1944 | if (fit_image_get_data_and_size(fit, noffset, data, size)) |
| 1945 | return -ENOENT; |
| 1946 | |
| 1947 | if (!fit_get_desc(fit, noffset, &desc)) |
| 1948 | printf("%s\n", desc); |
| 1949 | |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
| 1953 | int fit_get_data_node(const void *fit, const char *image_uname, |
| 1954 | const void **data, size_t *size) |
| 1955 | { |
| 1956 | int noffset = fit_image_get_node(fit, image_uname); |
| 1957 | |
| 1958 | return fit_get_data_tail(fit, noffset, data, size); |
| 1959 | } |
| 1960 | |
| 1961 | int fit_get_data_conf_prop(const void *fit, const char *prop_name, |
| 1962 | const void **data, size_t *size) |
| 1963 | { |
| 1964 | int noffset = fit_conf_get_node(fit, NULL); |
| 1965 | |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 1966 | noffset = fit_conf_get_prop_node(fit, noffset, prop_name, |
| 1967 | IH_PHASE_NONE); |
Sean Anderson | 080d3a0 | 2022-08-16 11:16:03 -0400 | [diff] [blame] | 1968 | return fit_get_data_tail(fit, noffset, data, size); |
| 1969 | } |
| 1970 | |
Jeroen Hofstee | ffa60da | 2014-10-08 22:57:38 +0200 | [diff] [blame] | 1971 | static int fit_image_select(const void *fit, int rd_noffset, int verify) |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1972 | { |
| 1973 | fit_image_print(fit, rd_noffset, " "); |
| 1974 | |
| 1975 | if (verify) { |
| 1976 | puts(" Verifying Hash Integrity ... "); |
| 1977 | if (!fit_image_verify(fit, rd_noffset)) { |
| 1978 | puts("Bad Data Hash\n"); |
| 1979 | return -EACCES; |
| 1980 | } |
| 1981 | puts("OK\n"); |
| 1982 | } |
| 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 1987 | int fit_get_node_from_config(struct bootm_headers *images, |
| 1988 | const char *prop_name, ulong addr) |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1989 | { |
| 1990 | int cfg_noffset; |
| 1991 | void *fit_hdr; |
| 1992 | int noffset; |
| 1993 | |
| 1994 | debug("* %s: using config '%s' from image at 0x%08lx\n", |
| 1995 | prop_name, images->fit_uname_cfg, addr); |
| 1996 | |
| 1997 | /* Check whether configuration has this property defined */ |
| 1998 | fit_hdr = map_sysmem(addr, 0); |
| 1999 | cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); |
| 2000 | if (cfg_noffset < 0) { |
| 2001 | debug("* %s: no such config\n", prop_name); |
Paul Burton | 14171b1 | 2016-09-20 18:17:12 +0100 | [diff] [blame] | 2002 | return -EINVAL; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 2005 | noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name, |
| 2006 | IH_PHASE_NONE); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2007 | if (noffset < 0) { |
| 2008 | debug("* %s: no '%s' in config\n", prop_name, prop_name); |
Jonathan Gray | ae6a02f | 2016-09-03 08:30:14 +1000 | [diff] [blame] | 2009 | return -ENOENT; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | return noffset; |
| 2013 | } |
| 2014 | |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2015 | /** |
| 2016 | * fit_get_image_type_property() - get property name for IH_TYPE_... |
| 2017 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 2018 | * Return: the properly name where we expect to find the image in the |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2019 | * config node |
| 2020 | */ |
| 2021 | static const char *fit_get_image_type_property(int type) |
| 2022 | { |
| 2023 | /* |
| 2024 | * This is sort-of available in the uimage_type[] table in image.c |
Andreas Dannenberg | 8ca5a90 | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 2025 | * but we don't have access to the short name, and "fdt" is different |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2026 | * anyway. So let's just keep it here. |
| 2027 | */ |
| 2028 | switch (type) { |
| 2029 | case IH_TYPE_FLATDT: |
| 2030 | return FIT_FDT_PROP; |
| 2031 | case IH_TYPE_KERNEL: |
| 2032 | return FIT_KERNEL_PROP; |
Alexandru Gagniuc | d1f7826 | 2021-04-01 13:25:30 -0500 | [diff] [blame] | 2033 | case IH_TYPE_FIRMWARE: |
| 2034 | return FIT_FIRMWARE_PROP; |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2035 | case IH_TYPE_RAMDISK: |
| 2036 | return FIT_RAMDISK_PROP; |
Simon Glass | 0129b52 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 2037 | case IH_TYPE_X86_SETUP: |
| 2038 | return FIT_SETUP_PROP; |
Karl Apsite | 1b21c28 | 2015-05-21 09:52:48 -0400 | [diff] [blame] | 2039 | case IH_TYPE_LOADABLE: |
| 2040 | return FIT_LOADABLE_PROP; |
Michal Simek | ebae78b | 2016-05-17 14:03:50 +0200 | [diff] [blame] | 2041 | case IH_TYPE_FPGA: |
| 2042 | return FIT_FPGA_PROP; |
Marek Vasut | 93e9575 | 2018-05-13 00:22:54 +0200 | [diff] [blame] | 2043 | case IH_TYPE_STANDALONE: |
| 2044 | return FIT_STANDALONE_PROP; |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | return "unknown"; |
| 2048 | } |
| 2049 | |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 2050 | int fit_image_load(struct bootm_headers *images, ulong addr, |
Simon Glass | ad68fc3 | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 2051 | const char **fit_unamep, const char **fit_uname_configp, |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 2052 | int arch, int ph_type, int bootstage_id, |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2053 | enum fit_load_op load_op, ulong *datap, ulong *lenp) |
| 2054 | { |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 2055 | int image_type = image_ph_type(ph_type); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2056 | int cfg_noffset, noffset; |
| 2057 | const char *fit_uname; |
Simon Glass | ad68fc3 | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 2058 | const char *fit_uname_config; |
Pantelis Antoniou | 5e671d6 | 2017-09-04 23:12:15 +0300 | [diff] [blame] | 2059 | const char *fit_base_uname_config; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2060 | const void *fit; |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2061 | void *buf; |
| 2062 | void *loadbuf; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2063 | size_t size; |
| 2064 | int type_ok, os_ok; |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2065 | ulong load, load_end, data, len; |
| 2066 | uint8_t os, comp; |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2067 | const char *prop_name; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2068 | int ret; |
| 2069 | |
| 2070 | fit = map_sysmem(addr, 0); |
| 2071 | fit_uname = fit_unamep ? *fit_unamep : NULL; |
Simon Glass | ad68fc3 | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 2072 | fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; |
Pantelis Antoniou | 5e671d6 | 2017-09-04 23:12:15 +0300 | [diff] [blame] | 2073 | fit_base_uname_config = NULL; |
Simon Glass | a0c0b63 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 2074 | prop_name = fit_get_image_type_property(image_type); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2075 | printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); |
| 2076 | |
| 2077 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 2078 | ret = fit_check_format(fit, IMAGE_SIZE_INVAL); |
| 2079 | if (ret) { |
| 2080 | printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret); |
| 2081 | if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL) |
| 2082 | printf("Signature checking prevents use of unit addresses (@) in nodes\n"); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2083 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); |
Simon Glass | b823daa | 2021-02-15 17:08:12 -0700 | [diff] [blame] | 2084 | return ret; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2085 | } |
| 2086 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); |
| 2087 | if (fit_uname) { |
Masahiro Yamada | aa58eec | 2014-02-18 15:39:21 +0900 | [diff] [blame] | 2088 | /* get FIT component image node offset */ |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2089 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); |
| 2090 | noffset = fit_image_get_node(fit, fit_uname); |
| 2091 | } else { |
| 2092 | /* |
| 2093 | * no image node unit name, try to get config |
| 2094 | * node first. If config unit node name is NULL |
| 2095 | * fit_conf_get_node() will try to find default config node |
| 2096 | */ |
| 2097 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); |
Simon Glass | 0434ea1 | 2021-07-14 17:05:36 -0500 | [diff] [blame] | 2098 | if (IS_ENABLED(CONFIG_FIT_BEST_MATCH) && !fit_uname_config) { |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2099 | cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); |
| 2100 | } else { |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 2101 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2102 | } |
| 2103 | if (cfg_noffset < 0) { |
| 2104 | puts("Could not find configuration node\n"); |
| 2105 | bootstage_error(bootstage_id + |
| 2106 | BOOTSTAGE_SUB_NO_UNIT_NAME); |
| 2107 | return -ENOENT; |
| 2108 | } |
Marek Vasut | 5dee134 | 2018-05-31 17:59:07 +0200 | [diff] [blame] | 2109 | |
Pantelis Antoniou | 5e671d6 | 2017-09-04 23:12:15 +0300 | [diff] [blame] | 2110 | fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL); |
| 2111 | printf(" Using '%s' configuration\n", fit_base_uname_config); |
Marek Vasut | 5dee134 | 2018-05-31 17:59:07 +0200 | [diff] [blame] | 2112 | /* Remember this config */ |
| 2113 | if (image_type == IH_TYPE_KERNEL) |
Pantelis Antoniou | 5e671d6 | 2017-09-04 23:12:15 +0300 | [diff] [blame] | 2114 | images->fit_uname_cfg = fit_base_uname_config; |
Marek Vasut | 5dee134 | 2018-05-31 17:59:07 +0200 | [diff] [blame] | 2115 | |
AKASHI Takahiro | 2223c7d | 2020-02-21 15:12:55 +0900 | [diff] [blame] | 2116 | if (FIT_IMAGE_ENABLE_VERIFY && images->verify) { |
Marek Vasut | 5dee134 | 2018-05-31 17:59:07 +0200 | [diff] [blame] | 2117 | puts(" Verifying Hash Integrity ... "); |
| 2118 | if (fit_config_verify(fit, cfg_noffset)) { |
| 2119 | puts("Bad Data Hash\n"); |
| 2120 | bootstage_error(bootstage_id + |
| 2121 | BOOTSTAGE_SUB_HASH); |
| 2122 | return -EACCES; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2123 | } |
Marek Vasut | 5dee134 | 2018-05-31 17:59:07 +0200 | [diff] [blame] | 2124 | puts("OK\n"); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Marek Vasut | 5dee134 | 2018-05-31 17:59:07 +0200 | [diff] [blame] | 2127 | bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); |
| 2128 | |
Simon Glass | a42f11f | 2022-10-20 18:23:04 -0600 | [diff] [blame] | 2129 | noffset = fit_conf_get_prop_node(fit, cfg_noffset, prop_name, |
| 2130 | image_ph_phase(ph_type)); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2131 | fit_uname = fit_get_name(fit, noffset, NULL); |
| 2132 | } |
| 2133 | if (noffset < 0) { |
Simon Glass | a559bb2 | 2020-03-18 11:43:56 -0600 | [diff] [blame] | 2134 | printf("Could not find subimage node type '%s'\n", prop_name); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2135 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); |
| 2136 | return -ENOENT; |
| 2137 | } |
| 2138 | |
| 2139 | printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); |
| 2140 | |
| 2141 | ret = fit_image_select(fit, noffset, images->verify); |
| 2142 | if (ret) { |
| 2143 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); |
| 2144 | return ret; |
| 2145 | } |
| 2146 | |
| 2147 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); |
Simon Glass | db503fb | 2021-09-25 19:43:14 -0600 | [diff] [blame] | 2148 | if (!tools_build() && IS_ENABLED(CONFIG_SANDBOX)) { |
Sebastian Reichel | bfe8ddf | 2021-01-04 20:48:03 +0100 | [diff] [blame] | 2149 | if (!fit_image_check_target_arch(fit, noffset)) { |
| 2150 | puts("Unsupported Architecture\n"); |
| 2151 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); |
| 2152 | return -ENOEXEC; |
| 2153 | } |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2154 | } |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 2155 | |
| 2156 | #ifndef USE_HOSTCC |
Simon Glass | c0cabbc | 2021-09-25 19:43:39 -0600 | [diff] [blame] | 2157 | { |
| 2158 | uint8_t os_arch; |
| 2159 | |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 2160 | fit_image_get_arch(fit, noffset, &os_arch); |
| 2161 | images->os.arch = os_arch; |
Simon Glass | c0cabbc | 2021-09-25 19:43:39 -0600 | [diff] [blame] | 2162 | } |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 2163 | #endif |
| 2164 | |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2165 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); |
| 2166 | type_ok = fit_image_check_type(fit, noffset, image_type) || |
mario.six@gdsys.cc | a82eeaf | 2016-07-20 08:32:50 +0200 | [diff] [blame] | 2167 | fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) || |
Alexandru Gagniuc | e14c9b4 | 2021-04-01 13:25:31 -0500 | [diff] [blame] | 2168 | fit_image_check_type(fit, noffset, IH_TYPE_TEE) || |
mario.six@gdsys.cc | a82eeaf | 2016-07-20 08:32:50 +0200 | [diff] [blame] | 2169 | (image_type == IH_TYPE_KERNEL && |
| 2170 | fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD)); |
Marek Vasut | caee41f | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 2171 | |
Andreas Bießmann | b4cc60d | 2016-08-14 20:31:24 +0200 | [diff] [blame] | 2172 | os_ok = image_type == IH_TYPE_FLATDT || |
| 2173 | image_type == IH_TYPE_FPGA || |
Marek Vasut | caee41f | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 2174 | fit_image_check_os(fit, noffset, IH_OS_LINUX) || |
mario.six@gdsys.cc | a82eeaf | 2016-07-20 08:32:50 +0200 | [diff] [blame] | 2175 | fit_image_check_os(fit, noffset, IH_OS_U_BOOT) || |
Alexandru Gagniuc | e14c9b4 | 2021-04-01 13:25:31 -0500 | [diff] [blame] | 2176 | fit_image_check_os(fit, noffset, IH_OS_TEE) || |
Cristian Ciocaltea | 217f964 | 2019-12-24 18:05:38 +0200 | [diff] [blame] | 2177 | fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) || |
Lihua Zhao | 03b0d1e | 2020-03-18 07:32:07 -0700 | [diff] [blame] | 2178 | fit_image_check_os(fit, noffset, IH_OS_EFI) || |
Maxim Moskalets | e2b258c | 2024-06-21 14:42:10 +0300 | [diff] [blame] | 2179 | fit_image_check_os(fit, noffset, IH_OS_VXWORKS) || |
| 2180 | fit_image_check_os(fit, noffset, IH_OS_ELF); |
Karl Apsite | 1b21c28 | 2015-05-21 09:52:48 -0400 | [diff] [blame] | 2181 | |
| 2182 | /* |
| 2183 | * If either of the checks fail, we should report an error, but |
| 2184 | * if the image type is coming from the "loadables" field, we |
| 2185 | * don't care what it is |
| 2186 | */ |
| 2187 | if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) { |
Marek Vasut | caee41f | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 2188 | fit_image_get_os(fit, noffset, &os); |
| 2189 | printf("No %s %s %s Image\n", |
| 2190 | genimg_get_os_name(os), |
| 2191 | genimg_get_arch_name(arch), |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2192 | genimg_get_type_name(image_type)); |
| 2193 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); |
| 2194 | return -EIO; |
| 2195 | } |
| 2196 | |
| 2197 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); |
| 2198 | |
| 2199 | /* get image data address and length */ |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2200 | if (fit_image_get_data_and_size(fit, noffset, |
| 2201 | (const void **)&buf, &size)) { |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2202 | printf("Could not find %s subimage data!\n", prop_name); |
| 2203 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); |
Simon Glass | 9d0da5b | 2013-06-16 07:46:49 -0700 | [diff] [blame] | 2204 | return -ENOENT; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2205 | } |
Andrew F. Davis | c6d8cc0 | 2016-11-21 14:37:09 -0600 | [diff] [blame] | 2206 | |
Philippe Reynes | 3d96470 | 2019-12-18 18:25:42 +0100 | [diff] [blame] | 2207 | /* Decrypt data before uncompress/move */ |
Sebastian Reichel | bfe8ddf | 2021-01-04 20:48:03 +0100 | [diff] [blame] | 2208 | if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) { |
Philippe Reynes | 3d96470 | 2019-12-18 18:25:42 +0100 | [diff] [blame] | 2209 | puts(" Decrypting Data ... "); |
| 2210 | if (fit_image_uncipher(fit, noffset, &buf, &size)) { |
| 2211 | puts("Error\n"); |
| 2212 | return -EACCES; |
| 2213 | } |
| 2214 | puts("OK\n"); |
| 2215 | } |
Philippe Reynes | 3d96470 | 2019-12-18 18:25:42 +0100 | [diff] [blame] | 2216 | |
Andrew F. Davis | c6d8cc0 | 2016-11-21 14:37:09 -0600 | [diff] [blame] | 2217 | /* perform any post-processing on the image data */ |
Simon Glass | db503fb | 2021-09-25 19:43:14 -0600 | [diff] [blame] | 2218 | if (!tools_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS)) |
Lokesh Vutla | b36dd3e | 2021-06-11 11:45:05 +0300 | [diff] [blame] | 2219 | board_fit_image_post_process(fit, noffset, &buf, &size); |
Andrew F. Davis | c6d8cc0 | 2016-11-21 14:37:09 -0600 | [diff] [blame] | 2220 | |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2221 | len = (ulong)size; |
| 2222 | |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2223 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); |
| 2224 | |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2225 | data = map_to_sysmem(buf); |
| 2226 | load = data; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2227 | if (load_op == FIT_LOAD_IGNORED) { |
| 2228 | /* Don't load */ |
| 2229 | } else if (fit_image_get_load(fit, noffset, &load)) { |
| 2230 | if (load_op == FIT_LOAD_REQUIRED) { |
| 2231 | printf("Can't get %s subimage load address!\n", |
| 2232 | prop_name); |
| 2233 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); |
| 2234 | return -EBADF; |
| 2235 | } |
Simon Glass | 05a9ad7 | 2014-08-22 14:26:43 -0600 | [diff] [blame] | 2236 | } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) { |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2237 | ulong image_start, image_end; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2238 | |
| 2239 | /* |
| 2240 | * move image data to the load address, |
| 2241 | * make sure we don't overwrite initial image |
| 2242 | */ |
| 2243 | image_start = addr; |
| 2244 | image_end = addr + fit_get_size(fit); |
| 2245 | |
| 2246 | load_end = load + len; |
| 2247 | if (image_type != IH_TYPE_KERNEL && |
| 2248 | load < image_end && load_end > image_start) { |
| 2249 | printf("Error: %s overwritten\n", prop_name); |
| 2250 | return -EXDEV; |
| 2251 | } |
| 2252 | |
| 2253 | printf(" Loading %s from 0x%08lx to 0x%08lx\n", |
| 2254 | prop_name, data, load); |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2255 | } else { |
| 2256 | load = data; /* No load address specified */ |
| 2257 | } |
| 2258 | |
| 2259 | comp = IH_COMP_NONE; |
| 2260 | loadbuf = buf; |
| 2261 | /* Kernel images get decompressed later in bootm_load_os(). */ |
Julius Werner | 8804002 | 2019-08-02 15:52:28 -0700 | [diff] [blame] | 2262 | if (!fit_image_get_comp(fit, noffset, &comp) && |
| 2263 | comp != IH_COMP_NONE && |
| 2264 | !(image_type == IH_TYPE_KERNEL || |
| 2265 | image_type == IH_TYPE_KERNEL_NOLOAD || |
| 2266 | image_type == IH_TYPE_RAMDISK)) { |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2267 | ulong max_decomp_len = len * 20; |
| 2268 | if (load == data) { |
| 2269 | loadbuf = malloc(max_decomp_len); |
| 2270 | load = map_to_sysmem(loadbuf); |
| 2271 | } else { |
| 2272 | loadbuf = map_sysmem(load, max_decomp_len); |
| 2273 | } |
| 2274 | if (image_decomp(comp, load, data, image_type, |
| 2275 | loadbuf, buf, len, max_decomp_len, &load_end)) { |
| 2276 | printf("Error decompressing %s\n", prop_name); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2277 | |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2278 | return -ENOEXEC; |
| 2279 | } |
| 2280 | len = load_end - load; |
| 2281 | } else if (load != data) { |
| 2282 | loadbuf = map_sysmem(load, len); |
| 2283 | memcpy(loadbuf, buf, len); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2284 | } |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2285 | |
Julius Werner | 8804002 | 2019-08-02 15:52:28 -0700 | [diff] [blame] | 2286 | if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE) |
| 2287 | puts("WARNING: 'compression' nodes for ramdisks are deprecated," |
| 2288 | " please fix your .its file!\n"); |
| 2289 | |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2290 | /* verify that image data is a proper FDT blob */ |
| 2291 | if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) { |
| 2292 | puts("Subimage data is not a FDT"); |
| 2293 | return -ENOEXEC; |
| 2294 | } |
| 2295 | |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2296 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); |
| 2297 | |
Simon Glass | 9b5bfba | 2024-08-07 16:47:33 -0600 | [diff] [blame] | 2298 | upl_add_image(fit, noffset, load, len); |
| 2299 | |
Julius Werner | 97b09cd | 2019-07-24 19:37:55 -0700 | [diff] [blame] | 2300 | *datap = load; |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2301 | *lenp = len; |
| 2302 | if (fit_unamep) |
| 2303 | *fit_unamep = (char *)fit_uname; |
Simon Glass | ad68fc3 | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 2304 | if (fit_uname_configp) |
Pantelis Antoniou | 5e671d6 | 2017-09-04 23:12:15 +0300 | [diff] [blame] | 2305 | *fit_uname_configp = (char *)(fit_uname_config ? : |
| 2306 | fit_base_uname_config); |
Simon Glass | 384d86d | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 2307 | |
| 2308 | return noffset; |
| 2309 | } |
Simon Glass | 0129b52 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 2310 | |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 2311 | int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch, |
| 2312 | ulong *setup_start, ulong *setup_len) |
Simon Glass | 0129b52 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 2313 | { |
| 2314 | int noffset; |
| 2315 | ulong addr; |
| 2316 | ulong len; |
| 2317 | int ret; |
| 2318 | |
| 2319 | addr = map_to_sysmem(images->fit_hdr_os); |
| 2320 | noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr); |
| 2321 | if (noffset < 0) |
| 2322 | return noffset; |
| 2323 | |
| 2324 | ret = fit_image_load(images, addr, NULL, NULL, arch, |
| 2325 | IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START, |
| 2326 | FIT_LOAD_REQUIRED, setup_start, &len); |
| 2327 | |
| 2328 | return ret; |
| 2329 | } |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2330 | |
| 2331 | #ifndef USE_HOSTCC |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 2332 | int boot_get_fdt_fit(struct bootm_headers *images, ulong addr, |
| 2333 | const char **fit_unamep, const char **fit_uname_configp, |
| 2334 | int arch, ulong *datap, ulong *lenp) |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2335 | { |
| 2336 | int fdt_noffset, cfg_noffset, count; |
| 2337 | const void *fit; |
| 2338 | const char *fit_uname = NULL; |
| 2339 | const char *fit_uname_config = NULL; |
| 2340 | char *fit_uname_config_copy = NULL; |
| 2341 | char *next_config = NULL; |
| 2342 | ulong load, len; |
| 2343 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 2344 | ulong image_start, image_end; |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2345 | ulong ovload, ovlen, ovcopylen; |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2346 | const char *uconfig; |
| 2347 | const char *uname; |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2348 | void *base, *ov, *ovcopy = NULL; |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2349 | int i, err, noffset, ov_noffset; |
| 2350 | #endif |
| 2351 | |
| 2352 | fit_uname = fit_unamep ? *fit_unamep : NULL; |
| 2353 | |
| 2354 | if (fit_uname_configp && *fit_uname_configp) { |
| 2355 | fit_uname_config_copy = strdup(*fit_uname_configp); |
| 2356 | if (!fit_uname_config_copy) |
| 2357 | return -ENOMEM; |
| 2358 | |
| 2359 | next_config = strchr(fit_uname_config_copy, '#'); |
| 2360 | if (next_config) |
| 2361 | *next_config++ = '\0'; |
| 2362 | if (next_config - 1 > fit_uname_config_copy) |
| 2363 | fit_uname_config = fit_uname_config_copy; |
| 2364 | } |
| 2365 | |
| 2366 | fdt_noffset = fit_image_load(images, |
| 2367 | addr, &fit_uname, &fit_uname_config, |
| 2368 | arch, IH_TYPE_FLATDT, |
| 2369 | BOOTSTAGE_ID_FIT_FDT_START, |
| 2370 | FIT_LOAD_OPTIONAL, &load, &len); |
| 2371 | |
| 2372 | if (fdt_noffset < 0) |
| 2373 | goto out; |
| 2374 | |
| 2375 | debug("fit_uname=%s, fit_uname_config=%s\n", |
| 2376 | fit_uname ? fit_uname : "<NULL>", |
| 2377 | fit_uname_config ? fit_uname_config : "<NULL>"); |
| 2378 | |
| 2379 | fit = map_sysmem(addr, 0); |
| 2380 | |
| 2381 | cfg_noffset = fit_conf_get_node(fit, fit_uname_config); |
| 2382 | |
| 2383 | /* single blob, or error just return as well */ |
| 2384 | count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP); |
| 2385 | if (count <= 1 && !next_config) |
| 2386 | goto out; |
| 2387 | |
| 2388 | /* we need to apply overlays */ |
| 2389 | |
| 2390 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 2391 | image_start = addr; |
| 2392 | image_end = addr + fit_get_size(fit); |
| 2393 | /* verify that relocation took place by load address not being in fit */ |
| 2394 | if (load >= image_start && load < image_end) { |
| 2395 | /* check is simplified; fit load checks for overlaps */ |
| 2396 | printf("Overlayed FDT requires relocation\n"); |
| 2397 | fdt_noffset = -EBADF; |
| 2398 | goto out; |
| 2399 | } |
| 2400 | |
| 2401 | base = map_sysmem(load, len); |
| 2402 | |
| 2403 | /* apply extra configs in FIT first, followed by args */ |
| 2404 | for (i = 1; ; i++) { |
| 2405 | if (i < count) { |
| 2406 | noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, |
| 2407 | FIT_FDT_PROP, i); |
| 2408 | uname = fit_get_name(fit, noffset, NULL); |
| 2409 | uconfig = NULL; |
| 2410 | } else { |
| 2411 | if (!next_config) |
| 2412 | break; |
| 2413 | uconfig = next_config; |
| 2414 | next_config = strchr(next_config, '#'); |
| 2415 | if (next_config) |
| 2416 | *next_config++ = '\0'; |
| 2417 | uname = NULL; |
Peter Ujfalusi | af15dc4 | 2019-03-06 15:52:27 +0200 | [diff] [blame] | 2418 | |
| 2419 | /* |
| 2420 | * fit_image_load() would load the first FDT from the |
| 2421 | * extra config only when uconfig is specified. |
| 2422 | * Check if the extra config contains multiple FDTs and |
| 2423 | * if so, load them. |
| 2424 | */ |
| 2425 | cfg_noffset = fit_conf_get_node(fit, uconfig); |
| 2426 | |
| 2427 | i = 0; |
| 2428 | count = fit_conf_get_prop_node_count(fit, cfg_noffset, |
| 2429 | FIT_FDT_PROP); |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig); |
| 2433 | |
| 2434 | ov_noffset = fit_image_load(images, |
| 2435 | addr, &uname, &uconfig, |
| 2436 | arch, IH_TYPE_FLATDT, |
| 2437 | BOOTSTAGE_ID_FIT_FDT_START, |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2438 | FIT_LOAD_IGNORED, &ovload, &ovlen); |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2439 | if (ov_noffset < 0) { |
| 2440 | printf("load of %s failed\n", uname); |
| 2441 | continue; |
| 2442 | } |
| 2443 | debug("%s loaded at 0x%08lx len=0x%08lx\n", |
| 2444 | uname, ovload, ovlen); |
| 2445 | ov = map_sysmem(ovload, ovlen); |
| 2446 | |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2447 | ovcopylen = ALIGN(fdt_totalsize(ov), SZ_4K); |
| 2448 | ovcopy = malloc(ovcopylen); |
| 2449 | if (!ovcopy) { |
| 2450 | printf("failed to duplicate DTO before application\n"); |
| 2451 | fdt_noffset = -ENOMEM; |
| 2452 | goto out; |
| 2453 | } |
| 2454 | |
| 2455 | err = fdt_open_into(ov, ovcopy, ovcopylen); |
| 2456 | if (err < 0) { |
| 2457 | printf("failed on fdt_open_into for DTO\n"); |
| 2458 | fdt_noffset = err; |
| 2459 | goto out; |
| 2460 | } |
| 2461 | |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2462 | base = map_sysmem(load, len + ovlen); |
| 2463 | err = fdt_open_into(base, base, len + ovlen); |
| 2464 | if (err < 0) { |
| 2465 | printf("failed on fdt_open_into\n"); |
| 2466 | fdt_noffset = err; |
| 2467 | goto out; |
| 2468 | } |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2469 | |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2470 | /* the verbose method prints out messages on error */ |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2471 | err = fdt_overlay_apply_verbose(base, ovcopy); |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2472 | if (err < 0) { |
| 2473 | fdt_noffset = err; |
| 2474 | goto out; |
| 2475 | } |
| 2476 | fdt_pack(base); |
| 2477 | len = fdt_totalsize(base); |
| 2478 | } |
| 2479 | #else |
| 2480 | printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n"); |
| 2481 | fdt_noffset = -EBADF; |
| 2482 | #endif |
| 2483 | |
| 2484 | out: |
| 2485 | if (datap) |
| 2486 | *datap = load; |
| 2487 | if (lenp) |
| 2488 | *lenp = len; |
| 2489 | if (fit_unamep) |
| 2490 | *fit_unamep = fit_uname; |
| 2491 | if (fit_uname_configp) |
| 2492 | *fit_uname_configp = fit_uname_config; |
| 2493 | |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2494 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
Heinrich Schuchardt | f568646 | 2022-04-11 20:08:03 +0200 | [diff] [blame] | 2495 | free(ovcopy); |
Marek Vasut | f7d2461 | 2021-06-11 04:09:56 +0200 | [diff] [blame] | 2496 | #endif |
Heinrich Schuchardt | f568646 | 2022-04-11 20:08:03 +0200 | [diff] [blame] | 2497 | free(fit_uname_config_copy); |
Pantelis Antoniou | 6e51fb2 | 2017-09-04 23:12:16 +0300 | [diff] [blame] | 2498 | return fdt_noffset; |
| 2499 | } |
| 2500 | #endif |