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