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