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