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