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