Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Google Inc. |
| 3 | * |
| 4 | * (C) Copyright 2008 Semihalf |
| 5 | * |
| 6 | * (C) Copyright 2000-2006 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifdef USE_HOSTCC |
| 13 | #include "mkimage.h" |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 14 | #include <time.h> |
| 15 | #else |
Andreas Dannenberg | eba3fbd | 2016-07-27 12:12:39 -0500 | [diff] [blame] | 16 | #include <linux/compiler.h> |
York Sun | 020198b | 2016-11-23 09:25:09 -0800 | [diff] [blame] | 17 | #include <linux/kconfig.h> |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 18 | #include <common.h> |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 20 | #include <mapmem.h> |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 23 | #endif /* !USE_HOSTCC*/ |
| 24 | |
Andreas Dannenberg | eba3fbd | 2016-07-27 12:12:39 -0500 | [diff] [blame] | 25 | #include <image.h> |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 26 | #include <bootstage.h> |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 27 | #include <u-boot/crc.h> |
| 28 | #include <u-boot/md5.h> |
Jeroen Hofstee | 2b9912e | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 29 | #include <u-boot/sha1.h> |
| 30 | #include <u-boot/sha256.h> |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 31 | |
| 32 | /*****************************************************************************/ |
| 33 | /* New uImage format routines */ |
| 34 | /*****************************************************************************/ |
| 35 | #ifndef USE_HOSTCC |
| 36 | static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, |
| 37 | ulong *addr, const char **name) |
| 38 | { |
| 39 | const char *sep; |
| 40 | |
| 41 | *addr = addr_curr; |
| 42 | *name = NULL; |
| 43 | |
| 44 | sep = strchr(spec, sepc); |
| 45 | if (sep) { |
| 46 | if (sep - spec > 0) |
| 47 | *addr = simple_strtoul(spec, NULL, 16); |
| 48 | |
| 49 | *name = sep + 1; |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * fit_parse_conf - parse FIT configuration spec |
| 58 | * @spec: input string, containing configuration spec |
| 59 | * @add_curr: current image address (to be used as a possible default) |
| 60 | * @addr: pointer to a ulong variable, will hold FIT image address of a given |
| 61 | * configuration |
| 62 | * @conf_name double pointer to a char, will hold pointer to a configuration |
| 63 | * unit name |
| 64 | * |
Masahiro Yamada | 069d594 | 2013-09-18 09:36:38 +0900 | [diff] [blame] | 65 | * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 66 | * where <addr> is a FIT image address that contains configuration |
| 67 | * with a <conf> unit name. |
| 68 | * |
| 69 | * Address part is optional, and if omitted default add_curr will |
| 70 | * be used instead. |
| 71 | * |
| 72 | * returns: |
| 73 | * 1 if spec is a valid configuration string, |
| 74 | * addr and conf_name are set accordingly |
| 75 | * 0 otherwise |
| 76 | */ |
| 77 | int fit_parse_conf(const char *spec, ulong addr_curr, |
| 78 | ulong *addr, const char **conf_name) |
| 79 | { |
| 80 | return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * fit_parse_subimage - parse FIT subimage spec |
| 85 | * @spec: input string, containing subimage spec |
| 86 | * @add_curr: current image address (to be used as a possible default) |
| 87 | * @addr: pointer to a ulong variable, will hold FIT image address of a given |
| 88 | * subimage |
| 89 | * @image_name: double pointer to a char, will hold pointer to a subimage name |
| 90 | * |
Masahiro Yamada | 069d594 | 2013-09-18 09:36:38 +0900 | [diff] [blame] | 91 | * fit_parse_subimage() expects subimage spec in the form of |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 92 | * [<addr>]:<subimage>, where <addr> is a FIT image address that contains |
| 93 | * subimage with a <subimg> unit name. |
| 94 | * |
| 95 | * Address part is optional, and if omitted default add_curr will |
| 96 | * be used instead. |
| 97 | * |
| 98 | * returns: |
| 99 | * 1 if spec is a valid subimage string, |
| 100 | * addr and image_name are set accordingly |
| 101 | * 0 otherwise |
| 102 | */ |
| 103 | int fit_parse_subimage(const char *spec, ulong addr_curr, |
| 104 | ulong *addr, const char **image_name) |
| 105 | { |
| 106 | return fit_parse_spec(spec, ':', addr_curr, addr, image_name); |
| 107 | } |
| 108 | #endif /* !USE_HOSTCC */ |
| 109 | |
| 110 | static void fit_get_debug(const void *fit, int noffset, |
| 111 | char *prop_name, int err) |
| 112 | { |
| 113 | debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n", |
| 114 | prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL), |
| 115 | fdt_strerror(err)); |
| 116 | } |
| 117 | |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 118 | /** |
| 119 | * fit_get_subimage_count - get component (sub-image) count |
| 120 | * @fit: pointer to the FIT format image header |
| 121 | * @images_noffset: offset of images node |
| 122 | * |
| 123 | * returns: |
| 124 | * number of image components |
| 125 | */ |
| 126 | int fit_get_subimage_count(const void *fit, int images_noffset) |
| 127 | { |
| 128 | int noffset; |
| 129 | int ndepth; |
| 130 | int count = 0; |
| 131 | |
| 132 | /* Process its subnodes, print out component images details */ |
| 133 | for (ndepth = 0, count = 0, |
| 134 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 135 | (noffset >= 0) && (ndepth > 0); |
| 136 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 137 | if (ndepth == 1) { |
| 138 | count++; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return count; |
| 143 | } |
| 144 | |
Simon Glass | 87ebee3 | 2013-05-08 08:05:59 +0000 | [diff] [blame] | 145 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 146 | /** |
| 147 | * fit_print_contents - prints out the contents of the FIT format image |
| 148 | * @fit: pointer to the FIT format image header |
| 149 | * @p: pointer to prefix string |
| 150 | * |
| 151 | * fit_print_contents() formats a multi line FIT image contents description. |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 152 | * The routine prints out FIT image properties (root node level) followed by |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 153 | * the details of each component image. |
| 154 | * |
| 155 | * returns: |
| 156 | * no returned results |
| 157 | */ |
| 158 | void fit_print_contents(const void *fit) |
| 159 | { |
| 160 | char *desc; |
| 161 | char *uname; |
| 162 | int images_noffset; |
| 163 | int confs_noffset; |
| 164 | int noffset; |
| 165 | int ndepth; |
| 166 | int count = 0; |
| 167 | int ret; |
| 168 | const char *p; |
| 169 | time_t timestamp; |
| 170 | |
Simon Glass | 1fe7d93 | 2013-05-08 08:05:58 +0000 | [diff] [blame] | 171 | /* Indent string is defined in header image.h */ |
| 172 | p = IMAGE_INDENT_STRING; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 173 | |
| 174 | /* Root node properties */ |
| 175 | ret = fit_get_desc(fit, 0, &desc); |
| 176 | printf("%sFIT description: ", p); |
| 177 | if (ret) |
| 178 | printf("unavailable\n"); |
| 179 | else |
| 180 | printf("%s\n", desc); |
| 181 | |
| 182 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 183 | ret = fit_get_timestamp(fit, 0, ×tamp); |
| 184 | printf("%sCreated: ", p); |
| 185 | if (ret) |
| 186 | printf("unavailable\n"); |
| 187 | else |
| 188 | genimg_print_time(timestamp); |
| 189 | } |
| 190 | |
| 191 | /* Find images parent node offset */ |
| 192 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 193 | if (images_noffset < 0) { |
| 194 | printf("Can't find images parent node '%s' (%s)\n", |
| 195 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | /* Process its subnodes, print out component images details */ |
| 200 | for (ndepth = 0, count = 0, |
| 201 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 202 | (noffset >= 0) && (ndepth > 0); |
| 203 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 204 | if (ndepth == 1) { |
| 205 | /* |
| 206 | * Direct child node of the images parent node, |
| 207 | * i.e. component image node. |
| 208 | */ |
| 209 | printf("%s Image %u (%s)\n", p, count++, |
| 210 | fit_get_name(fit, noffset, NULL)); |
| 211 | |
| 212 | fit_image_print(fit, noffset, p); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* Find configurations parent node offset */ |
| 217 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 218 | if (confs_noffset < 0) { |
| 219 | debug("Can't get configurations parent node '%s' (%s)\n", |
| 220 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | /* get default configuration unit name from default property */ |
| 225 | uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); |
| 226 | if (uname) |
| 227 | printf("%s Default Configuration: '%s'\n", p, uname); |
| 228 | |
| 229 | /* Process its subnodes, print out configurations details */ |
| 230 | for (ndepth = 0, count = 0, |
| 231 | noffset = fdt_next_node(fit, confs_noffset, &ndepth); |
| 232 | (noffset >= 0) && (ndepth > 0); |
| 233 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 234 | if (ndepth == 1) { |
| 235 | /* |
| 236 | * Direct child node of the configurations parent node, |
| 237 | * i.e. configuration node. |
| 238 | */ |
| 239 | printf("%s Configuration %u (%s)\n", p, count++, |
| 240 | fit_get_name(fit, noffset, NULL)); |
| 241 | |
| 242 | fit_conf_print(fit, noffset, p); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 248 | * fit_image_print_data() - prints out the hash node details |
| 249 | * @fit: pointer to the FIT format image header |
| 250 | * @noffset: offset of the hash node |
| 251 | * @p: pointer to prefix string |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 252 | * @type: Type of information to print ("hash" or "sign") |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 253 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 254 | * fit_image_print_data() lists properties for the processed hash node |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 255 | * |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 256 | * This function avoid using puts() since it prints a newline on the host |
| 257 | * but does not in U-Boot. |
| 258 | * |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 259 | * returns: |
| 260 | * no returned results |
| 261 | */ |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 262 | static void fit_image_print_data(const void *fit, int noffset, const char *p, |
| 263 | const char *type) |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 264 | { |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 265 | const char *keyname; |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 266 | uint8_t *value; |
| 267 | int value_len; |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 268 | char *algo; |
| 269 | int required; |
| 270 | int ret, i; |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 271 | |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 272 | debug("%s %s node: '%s'\n", p, type, |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 273 | fit_get_name(fit, noffset, NULL)); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 274 | printf("%s %s algo: ", p, type); |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 275 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
| 276 | printf("invalid/unsupported\n"); |
| 277 | return; |
| 278 | } |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 279 | printf("%s", algo); |
| 280 | keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); |
| 281 | required = fdt_getprop(fit, noffset, "required", NULL) != NULL; |
| 282 | if (keyname) |
| 283 | printf(":%s", keyname); |
| 284 | if (required) |
| 285 | printf(" (required)"); |
| 286 | printf("\n"); |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 287 | |
| 288 | ret = fit_image_hash_get_value(fit, noffset, &value, |
| 289 | &value_len); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 290 | printf("%s %s value: ", p, type); |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 291 | if (ret) { |
| 292 | printf("unavailable\n"); |
| 293 | } else { |
| 294 | for (i = 0; i < value_len; i++) |
| 295 | printf("%02x", value[i]); |
| 296 | printf("\n"); |
| 297 | } |
| 298 | |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 299 | debug("%s %s len: %d\n", p, type, value_len); |
| 300 | |
| 301 | /* Signatures have a time stamp */ |
| 302 | if (IMAGE_ENABLE_TIMESTAMP && keyname) { |
| 303 | time_t timestamp; |
| 304 | |
| 305 | printf("%s Timestamp: ", p); |
| 306 | if (fit_get_timestamp(fit, noffset, ×tamp)) |
| 307 | printf("unavailable\n"); |
| 308 | else |
| 309 | genimg_print_time(timestamp); |
| 310 | } |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * fit_image_print_verification_data() - prints out the hash/signature details |
| 315 | * @fit: pointer to the FIT format image header |
| 316 | * @noffset: offset of the hash or signature node |
| 317 | * @p: pointer to prefix string |
| 318 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 319 | * This lists properties for the processed hash node |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 320 | * |
| 321 | * returns: |
| 322 | * no returned results |
| 323 | */ |
| 324 | static void fit_image_print_verification_data(const void *fit, int noffset, |
| 325 | const char *p) |
| 326 | { |
| 327 | const char *name; |
| 328 | |
| 329 | /* |
| 330 | * Check subnode name, must be equal to "hash" or "signature". |
| 331 | * Multiple hash/signature nodes require unique unit node |
| 332 | * names, e.g. hash@1, hash@2, signature@1, signature@2, etc. |
| 333 | */ |
| 334 | name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 335 | if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) { |
| 336 | fit_image_print_data(fit, noffset, p, "Hash"); |
| 337 | } else if (!strncmp(name, FIT_SIG_NODENAME, |
| 338 | strlen(FIT_SIG_NODENAME))) { |
| 339 | fit_image_print_data(fit, noffset, p, "Sign"); |
| 340 | } |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 344 | * fit_image_print - prints out the FIT component image details |
| 345 | * @fit: pointer to the FIT format image header |
| 346 | * @image_noffset: offset of the component image node |
| 347 | * @p: pointer to prefix string |
| 348 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 349 | * fit_image_print() lists all mandatory properties for the processed component |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 350 | * image. If present, hash nodes are printed out as well. Load |
| 351 | * address for images of type firmware is also printed out. Since the load |
| 352 | * address is not mandatory for firmware images, it will be output as |
| 353 | * "unavailable" when not present. |
| 354 | * |
| 355 | * returns: |
| 356 | * no returned results |
| 357 | */ |
| 358 | void fit_image_print(const void *fit, int image_noffset, const char *p) |
| 359 | { |
| 360 | char *desc; |
| 361 | uint8_t type, arch, os, comp; |
| 362 | size_t size; |
| 363 | ulong load, entry; |
| 364 | const void *data; |
| 365 | int noffset; |
| 366 | int ndepth; |
| 367 | int ret; |
| 368 | |
| 369 | /* Mandatory properties */ |
| 370 | ret = fit_get_desc(fit, image_noffset, &desc); |
| 371 | printf("%s Description: ", p); |
| 372 | if (ret) |
| 373 | printf("unavailable\n"); |
| 374 | else |
| 375 | printf("%s\n", desc); |
| 376 | |
Simon Glass | 1fd1e2f | 2013-07-16 20:10:01 -0700 | [diff] [blame] | 377 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 378 | time_t timestamp; |
| 379 | |
| 380 | ret = fit_get_timestamp(fit, 0, ×tamp); |
| 381 | printf("%s Created: ", p); |
| 382 | if (ret) |
| 383 | printf("unavailable\n"); |
| 384 | else |
| 385 | genimg_print_time(timestamp); |
| 386 | } |
| 387 | |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 388 | fit_image_get_type(fit, image_noffset, &type); |
| 389 | printf("%s Type: %s\n", p, genimg_get_type_name(type)); |
| 390 | |
| 391 | fit_image_get_comp(fit, image_noffset, &comp); |
| 392 | printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); |
| 393 | |
| 394 | ret = fit_image_get_data(fit, image_noffset, &data, &size); |
| 395 | |
| 396 | #ifndef USE_HOSTCC |
| 397 | printf("%s Data Start: ", p); |
Simon Glass | c6ac13b | 2013-05-16 13:53:26 +0000 | [diff] [blame] | 398 | if (ret) { |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 399 | printf("unavailable\n"); |
Simon Glass | c6ac13b | 2013-05-16 13:53:26 +0000 | [diff] [blame] | 400 | } else { |
| 401 | void *vdata = (void *)data; |
| 402 | |
| 403 | printf("0x%08lx\n", (ulong)map_to_sysmem(vdata)); |
| 404 | } |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 405 | #endif |
| 406 | |
| 407 | printf("%s Data Size: ", p); |
| 408 | if (ret) |
| 409 | printf("unavailable\n"); |
| 410 | else |
| 411 | genimg_print_size(size); |
| 412 | |
| 413 | /* Remaining, type dependent properties */ |
| 414 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 415 | (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || |
| 416 | (type == IH_TYPE_FLATDT)) { |
| 417 | fit_image_get_arch(fit, image_noffset, &arch); |
| 418 | printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); |
| 419 | } |
| 420 | |
| 421 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) { |
| 422 | fit_image_get_os(fit, image_noffset, &os); |
| 423 | printf("%s OS: %s\n", p, genimg_get_os_name(os)); |
| 424 | } |
| 425 | |
| 426 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
Michal Simek | 62afc60 | 2016-05-17 14:03:50 +0200 | [diff] [blame] | 427 | (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) || |
| 428 | (type == IH_TYPE_FPGA)) { |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 429 | ret = fit_image_get_load(fit, image_noffset, &load); |
| 430 | printf("%s Load Address: ", p); |
| 431 | if (ret) |
| 432 | printf("unavailable\n"); |
| 433 | else |
| 434 | printf("0x%08lx\n", load); |
| 435 | } |
| 436 | |
| 437 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 438 | (type == IH_TYPE_RAMDISK)) { |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 439 | ret = fit_image_get_entry(fit, image_noffset, &entry); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 440 | printf("%s Entry Point: ", p); |
| 441 | if (ret) |
| 442 | printf("unavailable\n"); |
| 443 | else |
| 444 | printf("0x%08lx\n", entry); |
| 445 | } |
| 446 | |
| 447 | /* Process all hash subnodes of the component image node */ |
| 448 | for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); |
| 449 | (noffset >= 0) && (ndepth > 0); |
| 450 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 451 | if (ndepth == 1) { |
| 452 | /* Direct child node of the component image node */ |
Simon Glass | d8b7536 | 2013-05-07 06:12:02 +0000 | [diff] [blame] | 453 | fit_image_print_verification_data(fit, noffset, p); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | } |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 457 | |
| 458 | #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */ |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 459 | |
| 460 | /** |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 461 | * fit_get_desc - get node description property |
| 462 | * @fit: pointer to the FIT format image header |
| 463 | * @noffset: node offset |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 464 | * @desc: double pointer to the char, will hold pointer to the description |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 465 | * |
| 466 | * fit_get_desc() reads description property from a given node, if |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 467 | * description is found pointer to it is returned in third call argument. |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 468 | * |
| 469 | * returns: |
| 470 | * 0, on success |
| 471 | * -1, on failure |
| 472 | */ |
| 473 | int fit_get_desc(const void *fit, int noffset, char **desc) |
| 474 | { |
| 475 | int len; |
| 476 | |
| 477 | *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); |
| 478 | if (*desc == NULL) { |
| 479 | fit_get_debug(fit, noffset, FIT_DESC_PROP, len); |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * fit_get_timestamp - get node timestamp property |
| 488 | * @fit: pointer to the FIT format image header |
| 489 | * @noffset: node offset |
| 490 | * @timestamp: pointer to the time_t, will hold read timestamp |
| 491 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 492 | * fit_get_timestamp() reads timestamp property from given node, if timestamp |
| 493 | * 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] | 494 | * argument. |
| 495 | * |
| 496 | * returns: |
| 497 | * 0, on success |
| 498 | * -1, on property read failure |
| 499 | * -2, on wrong timestamp size |
| 500 | */ |
| 501 | int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) |
| 502 | { |
| 503 | int len; |
| 504 | const void *data; |
| 505 | |
| 506 | data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); |
| 507 | if (data == NULL) { |
| 508 | fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); |
| 509 | return -1; |
| 510 | } |
| 511 | if (len != sizeof(uint32_t)) { |
| 512 | debug("FIT timestamp with incorrect size of (%u)\n", len); |
| 513 | return -2; |
| 514 | } |
| 515 | |
| 516 | *timestamp = uimage_to_cpu(*((uint32_t *)data)); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * fit_image_get_node - get node offset for component image of a given unit name |
| 522 | * @fit: pointer to the FIT format image header |
| 523 | * @image_uname: component image node unit name |
| 524 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 525 | * fit_image_get_node() finds a component image (within the '/images' |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 526 | * node) of a provided unit name. If image is found its node offset is |
| 527 | * returned to the caller. |
| 528 | * |
| 529 | * returns: |
| 530 | * image node offset when found (>=0) |
| 531 | * negative number on failure (FDT_ERR_* code) |
| 532 | */ |
| 533 | int fit_image_get_node(const void *fit, const char *image_uname) |
| 534 | { |
| 535 | int noffset, images_noffset; |
| 536 | |
| 537 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 538 | if (images_noffset < 0) { |
| 539 | debug("Can't find images parent node '%s' (%s)\n", |
| 540 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 541 | return images_noffset; |
| 542 | } |
| 543 | |
| 544 | noffset = fdt_subnode_offset(fit, images_noffset, image_uname); |
| 545 | if (noffset < 0) { |
| 546 | debug("Can't get node offset for image unit name: '%s' (%s)\n", |
| 547 | image_uname, fdt_strerror(noffset)); |
| 548 | } |
| 549 | |
| 550 | return noffset; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * fit_image_get_os - get os id for a given component image node |
| 555 | * @fit: pointer to the FIT format image header |
| 556 | * @noffset: component image node offset |
| 557 | * @os: pointer to the uint8_t, will hold os numeric id |
| 558 | * |
| 559 | * fit_image_get_os() finds os property in a given component image node. |
| 560 | * If the property is found, its (string) value is translated to the numeric |
| 561 | * id which is returned to the caller. |
| 562 | * |
| 563 | * returns: |
| 564 | * 0, on success |
| 565 | * -1, on failure |
| 566 | */ |
| 567 | int fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
| 568 | { |
| 569 | int len; |
| 570 | const void *data; |
| 571 | |
| 572 | /* Get OS name from property data */ |
| 573 | data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); |
| 574 | if (data == NULL) { |
| 575 | fit_get_debug(fit, noffset, FIT_OS_PROP, len); |
| 576 | *os = -1; |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | /* Translate OS name to id */ |
| 581 | *os = genimg_get_os_id(data); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * fit_image_get_arch - get arch id for a given component image node |
| 587 | * @fit: pointer to the FIT format image header |
| 588 | * @noffset: component image node offset |
| 589 | * @arch: pointer to the uint8_t, will hold arch numeric id |
| 590 | * |
| 591 | * fit_image_get_arch() finds arch property in a given component image node. |
| 592 | * If the property is found, its (string) value is translated to the numeric |
| 593 | * id which is returned to the caller. |
| 594 | * |
| 595 | * returns: |
| 596 | * 0, on success |
| 597 | * -1, on failure |
| 598 | */ |
| 599 | int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) |
| 600 | { |
| 601 | int len; |
| 602 | const void *data; |
| 603 | |
| 604 | /* Get architecture name from property data */ |
| 605 | data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); |
| 606 | if (data == NULL) { |
| 607 | fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); |
| 608 | *arch = -1; |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | /* Translate architecture name to id */ |
| 613 | *arch = genimg_get_arch_id(data); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * fit_image_get_type - get type id for a given component image node |
| 619 | * @fit: pointer to the FIT format image header |
| 620 | * @noffset: component image node offset |
| 621 | * @type: pointer to the uint8_t, will hold type numeric id |
| 622 | * |
| 623 | * fit_image_get_type() finds type property in a given component image node. |
| 624 | * If the property is found, its (string) value is translated to the numeric |
| 625 | * id which is returned to the caller. |
| 626 | * |
| 627 | * returns: |
| 628 | * 0, on success |
| 629 | * -1, on failure |
| 630 | */ |
| 631 | int fit_image_get_type(const void *fit, int noffset, uint8_t *type) |
| 632 | { |
| 633 | int len; |
| 634 | const void *data; |
| 635 | |
| 636 | /* Get image type name from property data */ |
| 637 | data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); |
| 638 | if (data == NULL) { |
| 639 | fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); |
| 640 | *type = -1; |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | /* Translate image type name to id */ |
| 645 | *type = genimg_get_type_id(data); |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * fit_image_get_comp - get comp id for a given component image node |
| 651 | * @fit: pointer to the FIT format image header |
| 652 | * @noffset: component image node offset |
| 653 | * @comp: pointer to the uint8_t, will hold comp numeric id |
| 654 | * |
| 655 | * fit_image_get_comp() finds comp property in a given component image node. |
| 656 | * If the property is found, its (string) value is translated to the numeric |
| 657 | * id which is returned to the caller. |
| 658 | * |
| 659 | * returns: |
| 660 | * 0, on success |
| 661 | * -1, on failure |
| 662 | */ |
| 663 | int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) |
| 664 | { |
| 665 | int len; |
| 666 | const void *data; |
| 667 | |
| 668 | /* Get compression name from property data */ |
| 669 | data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); |
| 670 | if (data == NULL) { |
| 671 | fit_get_debug(fit, noffset, FIT_COMP_PROP, len); |
| 672 | *comp = -1; |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | /* Translate compression name to id */ |
| 677 | *comp = genimg_get_comp_id(data); |
| 678 | return 0; |
| 679 | } |
| 680 | |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 681 | static int fit_image_get_address(const void *fit, int noffset, char *name, |
| 682 | ulong *load) |
| 683 | { |
York Sun | c1913cb | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 684 | int len, cell_len; |
| 685 | const fdt32_t *cell; |
| 686 | uint64_t load64 = 0; |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 687 | |
York Sun | c1913cb | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 688 | cell = fdt_getprop(fit, noffset, name, &len); |
| 689 | if (cell == NULL) { |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 690 | fit_get_debug(fit, noffset, name, len); |
| 691 | return -1; |
| 692 | } |
| 693 | |
York Sun | c1913cb | 2016-02-29 15:48:41 -0800 | [diff] [blame] | 694 | if (len > sizeof(ulong)) { |
| 695 | printf("Unsupported %s address size\n", name); |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | cell_len = len >> 2; |
| 700 | /* Use load64 to avoid compiling warning for 32-bit target */ |
| 701 | while (cell_len--) { |
| 702 | load64 = (load64 << 32) | uimage_to_cpu(*cell); |
| 703 | cell++; |
| 704 | } |
| 705 | *load = (ulong)load64; |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 706 | |
| 707 | return 0; |
| 708 | } |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 709 | /** |
| 710 | * fit_image_get_load() - get load addr property for given component image node |
| 711 | * @fit: pointer to the FIT format image header |
| 712 | * @noffset: component image node offset |
| 713 | * @load: pointer to the uint32_t, will hold load address |
| 714 | * |
| 715 | * fit_image_get_load() finds load address property in a given component |
| 716 | * image node. If the property is found, its value is returned to the caller. |
| 717 | * |
| 718 | * returns: |
| 719 | * 0, on success |
| 720 | * -1, on failure |
| 721 | */ |
| 722 | int fit_image_get_load(const void *fit, int noffset, ulong *load) |
| 723 | { |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 724 | return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /** |
| 728 | * fit_image_get_entry() - get entry point address property |
| 729 | * @fit: pointer to the FIT format image header |
| 730 | * @noffset: component image node offset |
| 731 | * @entry: pointer to the uint32_t, will hold entry point address |
| 732 | * |
| 733 | * This gets the entry point address property for a given component image |
| 734 | * node. |
| 735 | * |
| 736 | * fit_image_get_entry() finds entry point address property in a given |
| 737 | * component image node. If the property is found, its value is returned |
| 738 | * to the caller. |
| 739 | * |
| 740 | * returns: |
| 741 | * 0, on success |
| 742 | * -1, on failure |
| 743 | */ |
| 744 | int fit_image_get_entry(const void *fit, int noffset, ulong *entry) |
| 745 | { |
York Sun | 6004765 | 2016-02-29 15:48:40 -0800 | [diff] [blame] | 746 | return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /** |
| 750 | * fit_image_get_data - get data property and its size for a given component image node |
| 751 | * @fit: pointer to the FIT format image header |
| 752 | * @noffset: component image node offset |
| 753 | * @data: double pointer to void, will hold data property's data address |
| 754 | * @size: pointer to size_t, will hold data property's data size |
| 755 | * |
| 756 | * fit_image_get_data() finds data property in a given component image node. |
| 757 | * If the property is found its data start address and size are returned to |
| 758 | * the caller. |
| 759 | * |
| 760 | * returns: |
| 761 | * 0, on success |
| 762 | * -1, on failure |
| 763 | */ |
| 764 | int fit_image_get_data(const void *fit, int noffset, |
| 765 | const void **data, size_t *size) |
| 766 | { |
| 767 | int len; |
| 768 | |
| 769 | *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); |
| 770 | if (*data == NULL) { |
| 771 | fit_get_debug(fit, noffset, FIT_DATA_PROP, len); |
| 772 | *size = 0; |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | *size = len; |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * fit_image_hash_get_algo - get hash algorithm name |
| 782 | * @fit: pointer to the FIT format image header |
| 783 | * @noffset: hash node offset |
| 784 | * @algo: double pointer to char, will hold pointer to the algorithm name |
| 785 | * |
| 786 | * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. |
| 787 | * If the property is found its data start address is returned to the caller. |
| 788 | * |
| 789 | * returns: |
| 790 | * 0, on success |
| 791 | * -1, on failure |
| 792 | */ |
| 793 | int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) |
| 794 | { |
| 795 | int len; |
| 796 | |
| 797 | *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); |
| 798 | if (*algo == NULL) { |
| 799 | fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); |
| 800 | return -1; |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * fit_image_hash_get_value - get hash value and length |
| 808 | * @fit: pointer to the FIT format image header |
| 809 | * @noffset: hash node offset |
| 810 | * @value: double pointer to uint8_t, will hold address of a hash value data |
| 811 | * @value_len: pointer to an int, will hold hash data length |
| 812 | * |
| 813 | * fit_image_hash_get_value() finds hash value property in a given hash node. |
| 814 | * If the property is found its data start address and size are returned to |
| 815 | * the caller. |
| 816 | * |
| 817 | * returns: |
| 818 | * 0, on success |
| 819 | * -1, on failure |
| 820 | */ |
| 821 | int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, |
| 822 | int *value_len) |
| 823 | { |
| 824 | int len; |
| 825 | |
| 826 | *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); |
| 827 | if (*value == NULL) { |
| 828 | fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); |
| 829 | *value_len = 0; |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | *value_len = len; |
| 834 | return 0; |
| 835 | } |
| 836 | |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 837 | /** |
| 838 | * fit_image_hash_get_ignore - get hash ignore flag |
| 839 | * @fit: pointer to the FIT format image header |
| 840 | * @noffset: hash node offset |
| 841 | * @ignore: pointer to an int, will hold hash ignore flag |
| 842 | * |
| 843 | * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. |
| 844 | * If the property is found and non-zero, the hash algorithm is not verified by |
| 845 | * u-boot automatically. |
| 846 | * |
| 847 | * returns: |
| 848 | * 0, on ignore not found |
| 849 | * value, on ignore found |
| 850 | */ |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 851 | 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] | 852 | { |
| 853 | int len; |
| 854 | int *value; |
| 855 | |
| 856 | value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); |
| 857 | if (value == NULL || len != sizeof(int)) |
| 858 | *ignore = 0; |
| 859 | else |
| 860 | *ignore = *value; |
| 861 | |
| 862 | return 0; |
| 863 | } |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 864 | |
Simon Glass | 7a80de4 | 2016-02-24 09:14:42 -0700 | [diff] [blame] | 865 | ulong fit_get_end(const void *fit) |
| 866 | { |
| 867 | return map_to_sysmem((void *)(fit + fdt_totalsize(fit))); |
| 868 | } |
| 869 | |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 870 | /** |
| 871 | * fit_set_timestamp - set node timestamp property |
| 872 | * @fit: pointer to the FIT format image header |
| 873 | * @noffset: node offset |
| 874 | * @timestamp: timestamp value to be set |
| 875 | * |
| 876 | * fit_set_timestamp() attempts to set timestamp property in the requested |
| 877 | * node and returns operation status to the caller. |
| 878 | * |
| 879 | * returns: |
| 880 | * 0, on success |
Simon Glass | 4f427a4 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 881 | * -ENOSPC if no space in device tree, -1 for other error |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 882 | */ |
| 883 | int fit_set_timestamp(void *fit, int noffset, time_t timestamp) |
| 884 | { |
| 885 | uint32_t t; |
| 886 | int ret; |
| 887 | |
| 888 | t = cpu_to_uimage(timestamp); |
| 889 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, |
| 890 | sizeof(uint32_t)); |
| 891 | if (ret) { |
Simon Glass | 8df81e1 | 2016-05-01 13:55:37 -0600 | [diff] [blame] | 892 | debug("Can't set '%s' property for '%s' node (%s)\n", |
| 893 | FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), |
| 894 | fdt_strerror(ret)); |
Simon Glass | 4f427a4 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 895 | return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * calculate_hash - calculate and return hash for provided input data |
| 903 | * @data: pointer to the input data |
| 904 | * @data_len: data length |
| 905 | * @algo: requested hash algorithm |
| 906 | * @value: pointer to the char, will hold hash value data (caller must |
| 907 | * allocate enough free space) |
| 908 | * value_len: length of the calculated hash |
| 909 | * |
| 910 | * calculate_hash() computes input data hash according to the requested |
| 911 | * algorithm. |
| 912 | * Resulting hash value is placed in caller provided 'value' buffer, length |
| 913 | * of the calculated hash is returned via value_len pointer argument. |
| 914 | * |
| 915 | * returns: |
| 916 | * 0, on success |
| 917 | * -1, when algo is unsupported |
| 918 | */ |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 919 | int calculate_hash(const void *data, int data_len, const char *algo, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 920 | uint8_t *value, int *value_len) |
| 921 | { |
Simon Glass | 87ebee3 | 2013-05-08 08:05:59 +0000 | [diff] [blame] | 922 | if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 923 | *((uint32_t *)value) = crc32_wd(0, data, data_len, |
| 924 | CHUNKSZ_CRC32); |
| 925 | *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); |
| 926 | *value_len = 4; |
Simon Glass | 87ebee3 | 2013-05-08 08:05:59 +0000 | [diff] [blame] | 927 | } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 928 | sha1_csum_wd((unsigned char *)data, data_len, |
| 929 | (unsigned char *)value, CHUNKSZ_SHA1); |
| 930 | *value_len = 20; |
Heiko Schocher | 2842c1c | 2014-03-03 12:19:25 +0100 | [diff] [blame] | 931 | } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { |
| 932 | sha256_csum_wd((unsigned char *)data, data_len, |
| 933 | (unsigned char *)value, CHUNKSZ_SHA256); |
| 934 | *value_len = SHA256_SUM_LEN; |
Simon Glass | 87ebee3 | 2013-05-08 08:05:59 +0000 | [diff] [blame] | 935 | } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 936 | md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); |
| 937 | *value_len = 16; |
| 938 | } else { |
| 939 | debug("Unsupported hash alogrithm\n"); |
| 940 | return -1; |
| 941 | } |
| 942 | return 0; |
| 943 | } |
| 944 | |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 945 | static int fit_image_check_hash(const void *fit, int noffset, const void *data, |
| 946 | size_t size, char **err_msgp) |
| 947 | { |
| 948 | uint8_t value[FIT_MAX_HASH_LEN]; |
| 949 | int value_len; |
| 950 | char *algo; |
| 951 | uint8_t *fit_value; |
| 952 | int fit_value_len; |
| 953 | int ignore; |
| 954 | |
| 955 | *err_msgp = NULL; |
| 956 | |
| 957 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 958 | *err_msgp = "Can't get hash algo property"; |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 959 | return -1; |
| 960 | } |
| 961 | printf("%s", algo); |
| 962 | |
| 963 | if (IMAGE_ENABLE_IGNORE) { |
| 964 | fit_image_hash_get_ignore(fit, noffset, &ignore); |
| 965 | if (ignore) { |
| 966 | printf("-skipped "); |
| 967 | return 0; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 972 | &fit_value_len)) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 973 | *err_msgp = "Can't get hash value property"; |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 974 | return -1; |
| 975 | } |
| 976 | |
| 977 | if (calculate_hash(data, size, algo, value, &value_len)) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 978 | *err_msgp = "Unsupported hash algorithm"; |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 979 | return -1; |
| 980 | } |
| 981 | |
| 982 | if (value_len != fit_value_len) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 983 | *err_msgp = "Bad hash value len"; |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 984 | return -1; |
| 985 | } else if (memcmp(value, fit_value, value_len) != 0) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 986 | *err_msgp = "Bad hash value"; |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 987 | return -1; |
| 988 | } |
| 989 | |
| 990 | return 0; |
| 991 | } |
| 992 | |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 993 | /** |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 994 | * fit_image_verify - verify data integrity |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 995 | * @fit: pointer to the FIT format image header |
| 996 | * @image_noffset: component image node offset |
| 997 | * |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 998 | * fit_image_verify() goes over component image hash nodes, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 999 | * re-calculates each data hash and compares with the value stored in hash |
| 1000 | * node. |
| 1001 | * |
| 1002 | * returns: |
| 1003 | * 1, if all hashes are valid |
| 1004 | * 0, otherwise (or on error) |
| 1005 | */ |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1006 | int fit_image_verify(const void *fit, int image_noffset) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1007 | { |
| 1008 | const void *data; |
| 1009 | size_t size; |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1010 | int noffset = 0; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1011 | char *err_msg = ""; |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1012 | int verify_all = 1; |
| 1013 | int ret; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* Get image data and data length */ |
| 1016 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1017 | err_msg = "Can't get image data/size"; |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1018 | goto error; |
| 1019 | } |
| 1020 | |
| 1021 | /* Verify all required signatures */ |
| 1022 | if (IMAGE_ENABLE_VERIFY && |
| 1023 | fit_image_verify_required_sigs(fit, image_noffset, data, size, |
| 1024 | gd_fdt_blob(), &verify_all)) { |
| 1025 | err_msg = "Unable to verify required signature"; |
| 1026 | goto error; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* Process all hash subnodes of the component image node */ |
Simon Glass | df87e6b | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 1030 | fdt_for_each_subnode(noffset, fit, image_noffset) { |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1031 | const char *name = fit_get_name(fit, noffset, NULL); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1032 | |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1033 | /* |
| 1034 | * Check subnode name, must be equal to "hash". |
| 1035 | * Multiple hash nodes require unique unit node |
| 1036 | * names, e.g. hash@1, hash@2, etc. |
| 1037 | */ |
| 1038 | if (!strncmp(name, FIT_HASH_NODENAME, |
| 1039 | strlen(FIT_HASH_NODENAME))) { |
| 1040 | if (fit_image_check_hash(fit, noffset, data, size, |
| 1041 | &err_msg)) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1042 | goto error; |
Simon Glass | ab9efc6 | 2013-05-07 06:11:58 +0000 | [diff] [blame] | 1043 | puts("+ "); |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1044 | } else if (IMAGE_ENABLE_VERIFY && verify_all && |
| 1045 | !strncmp(name, FIT_SIG_NODENAME, |
| 1046 | strlen(FIT_SIG_NODENAME))) { |
| 1047 | ret = fit_image_check_sig(fit, noffset, data, |
| 1048 | size, -1, &err_msg); |
Simon Glass | 2e33e76 | 2016-02-24 09:14:43 -0700 | [diff] [blame] | 1049 | |
| 1050 | /* |
| 1051 | * Show an indication on failure, but do not return |
| 1052 | * an error. Only keys marked 'required' can cause |
| 1053 | * an image validation failure. See the call to |
| 1054 | * fit_image_verify_required_sigs() above. |
| 1055 | */ |
| 1056 | if (ret) |
Simon Glass | 56518e7 | 2013-06-13 15:10:01 -0700 | [diff] [blame] | 1057 | puts("- "); |
| 1058 | else |
| 1059 | puts("+ "); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1064 | err_msg = "Corrupted or truncated tree"; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1065 | goto error; |
| 1066 | } |
| 1067 | |
| 1068 | return 1; |
| 1069 | |
| 1070 | error: |
Simon Glass | e754da2 | 2013-05-07 06:11:59 +0000 | [diff] [blame] | 1071 | 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] | 1072 | err_msg, fit_get_name(fit, noffset, NULL), |
| 1073 | fit_get_name(fit, image_noffset, NULL)); |
| 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 1078 | * fit_all_image_verify - verify data integrity for all images |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1079 | * @fit: pointer to the FIT format image header |
| 1080 | * |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1081 | * fit_all_image_verify() goes over all images in the FIT and |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1082 | * for every images checks if all it's hashes are valid. |
| 1083 | * |
| 1084 | * returns: |
| 1085 | * 1, if all hashes of all images are valid |
| 1086 | * 0, otherwise (or on error) |
| 1087 | */ |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1088 | int fit_all_image_verify(const void *fit) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1089 | { |
| 1090 | int images_noffset; |
| 1091 | int noffset; |
| 1092 | int ndepth; |
| 1093 | int count; |
| 1094 | |
| 1095 | /* Find images parent node offset */ |
| 1096 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1097 | if (images_noffset < 0) { |
| 1098 | printf("Can't find images parent node '%s' (%s)\n", |
| 1099 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | /* Process all image subnodes, check hashes for each */ |
| 1104 | printf("## Checking hash(es) for FIT Image at %08lx ...\n", |
| 1105 | (ulong)fit); |
| 1106 | for (ndepth = 0, count = 0, |
| 1107 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 1108 | (noffset >= 0) && (ndepth > 0); |
| 1109 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 1110 | if (ndepth == 1) { |
| 1111 | /* |
| 1112 | * Direct child node of the images parent node, |
| 1113 | * i.e. component image node. |
| 1114 | */ |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 1115 | printf(" Hash(es) for Image %u (%s): ", count, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1116 | fit_get_name(fit, noffset, NULL)); |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 1117 | count++; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1118 | |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 1119 | if (!fit_image_verify(fit, noffset)) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1120 | return 0; |
| 1121 | printf("\n"); |
| 1122 | } |
| 1123 | } |
| 1124 | return 1; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * fit_image_check_os - check whether image node is of a given os type |
| 1129 | * @fit: pointer to the FIT format image header |
| 1130 | * @noffset: component image node offset |
| 1131 | * @os: requested image os |
| 1132 | * |
| 1133 | * fit_image_check_os() reads image os property and compares its numeric |
| 1134 | * id with the requested os. Comparison result is returned to the caller. |
| 1135 | * |
| 1136 | * returns: |
| 1137 | * 1 if image is of given os type |
| 1138 | * 0 otherwise (or on error) |
| 1139 | */ |
| 1140 | int fit_image_check_os(const void *fit, int noffset, uint8_t os) |
| 1141 | { |
| 1142 | uint8_t image_os; |
| 1143 | |
| 1144 | if (fit_image_get_os(fit, noffset, &image_os)) |
| 1145 | return 0; |
| 1146 | return (os == image_os); |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * fit_image_check_arch - check whether image node is of a given arch |
| 1151 | * @fit: pointer to the FIT format image header |
| 1152 | * @noffset: component image node offset |
| 1153 | * @arch: requested imagearch |
| 1154 | * |
| 1155 | * fit_image_check_arch() reads image arch property and compares its numeric |
| 1156 | * id with the requested arch. Comparison result is returned to the caller. |
| 1157 | * |
| 1158 | * returns: |
| 1159 | * 1 if image is of given arch |
| 1160 | * 0 otherwise (or on error) |
| 1161 | */ |
| 1162 | int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) |
| 1163 | { |
| 1164 | uint8_t image_arch; |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 1165 | int aarch32_support = 0; |
| 1166 | |
| 1167 | #ifdef CONFIG_ARM64_SUPPORT_AARCH32 |
| 1168 | aarch32_support = 1; |
| 1169 | #endif |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1170 | |
| 1171 | if (fit_image_get_arch(fit, noffset, &image_arch)) |
| 1172 | return 0; |
Simon Glass | 5bda35c | 2014-10-10 08:21:57 -0600 | [diff] [blame] | 1173 | return (arch == image_arch) || |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 1174 | (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) || |
| 1175 | (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM && |
| 1176 | aarch32_support); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /** |
| 1180 | * fit_image_check_type - check whether image node is of a given type |
| 1181 | * @fit: pointer to the FIT format image header |
| 1182 | * @noffset: component image node offset |
| 1183 | * @type: requested image type |
| 1184 | * |
| 1185 | * fit_image_check_type() reads image type property and compares its numeric |
| 1186 | * id with the requested type. Comparison result is returned to the caller. |
| 1187 | * |
| 1188 | * returns: |
| 1189 | * 1 if image is of given type |
| 1190 | * 0 otherwise (or on error) |
| 1191 | */ |
| 1192 | int fit_image_check_type(const void *fit, int noffset, uint8_t type) |
| 1193 | { |
| 1194 | uint8_t image_type; |
| 1195 | |
| 1196 | if (fit_image_get_type(fit, noffset, &image_type)) |
| 1197 | return 0; |
| 1198 | return (type == image_type); |
| 1199 | } |
| 1200 | |
| 1201 | /** |
| 1202 | * fit_image_check_comp - check whether image node uses given compression |
| 1203 | * @fit: pointer to the FIT format image header |
| 1204 | * @noffset: component image node offset |
| 1205 | * @comp: requested image compression type |
| 1206 | * |
| 1207 | * fit_image_check_comp() reads image compression property and compares its |
| 1208 | * numeric id with the requested compression type. Comparison result is |
| 1209 | * returned to the caller. |
| 1210 | * |
| 1211 | * returns: |
| 1212 | * 1 if image uses requested compression |
| 1213 | * 0 otherwise (or on error) |
| 1214 | */ |
| 1215 | int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) |
| 1216 | { |
| 1217 | uint8_t image_comp; |
| 1218 | |
| 1219 | if (fit_image_get_comp(fit, noffset, &image_comp)) |
| 1220 | return 0; |
| 1221 | return (comp == image_comp); |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * fit_check_format - sanity check FIT image format |
| 1226 | * @fit: pointer to the FIT format image header |
| 1227 | * |
| 1228 | * fit_check_format() runs a basic sanity FIT image verification. |
| 1229 | * Routine checks for mandatory properties, nodes, etc. |
| 1230 | * |
| 1231 | * returns: |
| 1232 | * 1, on success |
| 1233 | * 0, on failure |
| 1234 | */ |
| 1235 | int fit_check_format(const void *fit) |
| 1236 | { |
| 1237 | /* mandatory / node 'description' property */ |
| 1238 | if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { |
| 1239 | debug("Wrong FIT format: no description\n"); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 1244 | /* mandatory / node 'timestamp' property */ |
| 1245 | if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { |
| 1246 | debug("Wrong FIT format: no timestamp\n"); |
| 1247 | return 0; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | /* mandatory subimages parent '/images' node */ |
| 1252 | if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { |
| 1253 | debug("Wrong FIT format: no images parent node\n"); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | return 1; |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | /** |
| 1262 | * fit_conf_find_compat |
| 1263 | * @fit: pointer to the FIT format image header |
| 1264 | * @fdt: pointer to the device tree to compare against |
| 1265 | * |
| 1266 | * fit_conf_find_compat() attempts to find the configuration whose fdt is the |
| 1267 | * most compatible with the passed in device tree. |
| 1268 | * |
| 1269 | * Example: |
| 1270 | * |
| 1271 | * / o image-tree |
| 1272 | * |-o images |
| 1273 | * | |-o fdt@1 |
| 1274 | * | |-o fdt@2 |
| 1275 | * | |
| 1276 | * |-o configurations |
| 1277 | * |-o config@1 |
| 1278 | * | |-fdt = fdt@1 |
| 1279 | * | |
| 1280 | * |-o config@2 |
| 1281 | * |-fdt = fdt@2 |
| 1282 | * |
| 1283 | * / o U-Boot fdt |
| 1284 | * |-compatible = "foo,bar", "bim,bam" |
| 1285 | * |
| 1286 | * / o kernel fdt1 |
| 1287 | * |-compatible = "foo,bar", |
| 1288 | * |
| 1289 | * / o kernel fdt2 |
| 1290 | * |-compatible = "bim,bam", "baz,biz" |
| 1291 | * |
| 1292 | * Configuration 1 would be picked because the first string in U-Boot's |
| 1293 | * compatible list, "foo,bar", matches a compatible string in the root of fdt1. |
| 1294 | * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. |
| 1295 | * |
| 1296 | * returns: |
| 1297 | * offset to the configuration to use if one was found |
| 1298 | * -1 otherwise |
| 1299 | */ |
| 1300 | int fit_conf_find_compat(const void *fit, const void *fdt) |
| 1301 | { |
| 1302 | int ndepth = 0; |
| 1303 | int noffset, confs_noffset, images_noffset; |
| 1304 | const void *fdt_compat; |
| 1305 | int fdt_compat_len; |
| 1306 | int best_match_offset = 0; |
| 1307 | int best_match_pos = 0; |
| 1308 | |
| 1309 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1310 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1311 | if (confs_noffset < 0 || images_noffset < 0) { |
| 1312 | debug("Can't find configurations or images nodes.\n"); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | |
| 1316 | fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); |
| 1317 | if (!fdt_compat) { |
| 1318 | debug("Fdt for comparison has no \"compatible\" property.\n"); |
| 1319 | return -1; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * Loop over the configurations in the FIT image. |
| 1324 | */ |
| 1325 | for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); |
| 1326 | (noffset >= 0) && (ndepth > 0); |
| 1327 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 1328 | const void *kfdt; |
| 1329 | const char *kfdt_name; |
| 1330 | int kfdt_noffset; |
| 1331 | const char *cur_fdt_compat; |
| 1332 | int len; |
| 1333 | size_t size; |
| 1334 | int i; |
| 1335 | |
| 1336 | if (ndepth > 1) |
| 1337 | continue; |
| 1338 | |
| 1339 | kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); |
| 1340 | if (!kfdt_name) { |
| 1341 | debug("No fdt property found.\n"); |
| 1342 | continue; |
| 1343 | } |
| 1344 | kfdt_noffset = fdt_subnode_offset(fit, images_noffset, |
| 1345 | kfdt_name); |
| 1346 | if (kfdt_noffset < 0) { |
| 1347 | debug("No image node named \"%s\" found.\n", |
| 1348 | kfdt_name); |
| 1349 | continue; |
| 1350 | } |
| 1351 | /* |
| 1352 | * Get a pointer to this configuration's fdt. |
| 1353 | */ |
| 1354 | if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { |
| 1355 | debug("Failed to get fdt \"%s\".\n", kfdt_name); |
| 1356 | continue; |
| 1357 | } |
| 1358 | |
| 1359 | len = fdt_compat_len; |
| 1360 | cur_fdt_compat = fdt_compat; |
| 1361 | /* |
| 1362 | * Look for a match for each U-Boot compatibility string in |
| 1363 | * turn in this configuration's fdt. |
| 1364 | */ |
| 1365 | for (i = 0; len > 0 && |
| 1366 | (!best_match_offset || best_match_pos > i); i++) { |
| 1367 | int cur_len = strlen(cur_fdt_compat) + 1; |
| 1368 | |
| 1369 | if (!fdt_node_check_compatible(kfdt, 0, |
| 1370 | cur_fdt_compat)) { |
| 1371 | best_match_offset = noffset; |
| 1372 | best_match_pos = i; |
| 1373 | break; |
| 1374 | } |
| 1375 | len -= cur_len; |
| 1376 | cur_fdt_compat += cur_len; |
| 1377 | } |
| 1378 | } |
| 1379 | if (!best_match_offset) { |
| 1380 | debug("No match found.\n"); |
| 1381 | return -1; |
| 1382 | } |
| 1383 | |
| 1384 | return best_match_offset; |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * fit_conf_get_node - get node offset for configuration of a given unit name |
| 1389 | * @fit: pointer to the FIT format image header |
| 1390 | * @conf_uname: configuration node unit name |
| 1391 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 1392 | * fit_conf_get_node() finds a configuration (within the '/configurations' |
| 1393 | * parent node) of a provided unit name. If configuration is found its node |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1394 | * offset is returned to the caller. |
| 1395 | * |
| 1396 | * When NULL is provided in second argument fit_conf_get_node() will search |
| 1397 | * for a default configuration node instead. Default configuration node unit |
Robert P. J. Day | 1f8b546 | 2013-08-21 11:39:19 -0400 | [diff] [blame] | 1398 | * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations' |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1399 | * node. |
| 1400 | * |
| 1401 | * returns: |
| 1402 | * configuration node offset when found (>=0) |
| 1403 | * negative number on failure (FDT_ERR_* code) |
| 1404 | */ |
| 1405 | int fit_conf_get_node(const void *fit, const char *conf_uname) |
| 1406 | { |
| 1407 | int noffset, confs_noffset; |
| 1408 | int len; |
| 1409 | |
| 1410 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1411 | if (confs_noffset < 0) { |
| 1412 | debug("Can't find configurations parent node '%s' (%s)\n", |
| 1413 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
| 1414 | return confs_noffset; |
| 1415 | } |
| 1416 | |
| 1417 | if (conf_uname == NULL) { |
| 1418 | /* get configuration unit name from the default property */ |
| 1419 | debug("No configuration specified, trying default...\n"); |
| 1420 | conf_uname = (char *)fdt_getprop(fit, confs_noffset, |
| 1421 | FIT_DEFAULT_PROP, &len); |
| 1422 | if (conf_uname == NULL) { |
| 1423 | fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, |
| 1424 | len); |
| 1425 | return len; |
| 1426 | } |
| 1427 | debug("Found default configuration: '%s'\n", conf_uname); |
| 1428 | } |
| 1429 | |
| 1430 | noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); |
| 1431 | if (noffset < 0) { |
| 1432 | debug("Can't get node offset for configuration unit name: '%s' (%s)\n", |
| 1433 | conf_uname, fdt_strerror(noffset)); |
| 1434 | } |
| 1435 | |
| 1436 | return noffset; |
| 1437 | } |
| 1438 | |
Simon Glass | 003efd7 | 2013-05-07 06:12:00 +0000 | [diff] [blame] | 1439 | int fit_conf_get_prop_node(const void *fit, int noffset, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1440 | const char *prop_name) |
| 1441 | { |
| 1442 | char *uname; |
| 1443 | int len; |
| 1444 | |
| 1445 | /* get kernel image unit name from configuration kernel property */ |
| 1446 | uname = (char *)fdt_getprop(fit, noffset, prop_name, &len); |
| 1447 | if (uname == NULL) |
| 1448 | return len; |
| 1449 | |
| 1450 | return fit_image_get_node(fit, uname); |
| 1451 | } |
| 1452 | |
| 1453 | /** |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1454 | * fit_conf_print - prints out the FIT configuration details |
| 1455 | * @fit: pointer to the FIT format image header |
| 1456 | * @noffset: offset of the configuration node |
| 1457 | * @p: pointer to prefix string |
| 1458 | * |
Andreas Dannenberg | e17adbb | 2016-06-15 17:00:19 -0500 | [diff] [blame] | 1459 | * fit_conf_print() lists all mandatory properties for the processed |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1460 | * configuration node. |
| 1461 | * |
| 1462 | * returns: |
| 1463 | * no returned results |
| 1464 | */ |
| 1465 | void fit_conf_print(const void *fit, int noffset, const char *p) |
| 1466 | { |
| 1467 | char *desc; |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 1468 | const char *uname; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1469 | int ret; |
Karl Apsite | ecf8cd6 | 2015-05-21 09:52:47 -0400 | [diff] [blame] | 1470 | int loadables_index; |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1471 | |
| 1472 | /* Mandatory properties */ |
| 1473 | ret = fit_get_desc(fit, noffset, &desc); |
| 1474 | printf("%s Description: ", p); |
| 1475 | if (ret) |
| 1476 | printf("unavailable\n"); |
| 1477 | else |
| 1478 | printf("%s\n", desc); |
| 1479 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 1480 | uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1481 | printf("%s Kernel: ", p); |
| 1482 | if (uname == NULL) |
| 1483 | printf("unavailable\n"); |
| 1484 | else |
| 1485 | printf("%s\n", uname); |
| 1486 | |
| 1487 | /* Optional properties */ |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 1488 | uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1489 | if (uname) |
| 1490 | printf("%s Init Ramdisk: %s\n", p, uname); |
| 1491 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 1492 | uname = fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL); |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1493 | if (uname) |
| 1494 | printf("%s FDT: %s\n", p, uname); |
Karl Apsite | ecf8cd6 | 2015-05-21 09:52:47 -0400 | [diff] [blame] | 1495 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 1496 | uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL); |
Michal Simek | ed0cea7 | 2016-05-17 13:58:44 +0200 | [diff] [blame] | 1497 | if (uname) |
| 1498 | printf("%s FPGA: %s\n", p, uname); |
| 1499 | |
Karl Apsite | ecf8cd6 | 2015-05-21 09:52:47 -0400 | [diff] [blame] | 1500 | /* Print out all of the specified loadables */ |
| 1501 | for (loadables_index = 0; |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 1502 | uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP, |
| 1503 | loadables_index, NULL), uname; |
| 1504 | loadables_index++) { |
Karl Apsite | ecf8cd6 | 2015-05-21 09:52:47 -0400 | [diff] [blame] | 1505 | if (loadables_index == 0) { |
| 1506 | printf("%s Loadables: ", p); |
| 1507 | } else { |
| 1508 | printf("%s ", p); |
| 1509 | } |
| 1510 | printf("%s\n", uname); |
| 1511 | } |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Jeroen Hofstee | 718feca | 2014-10-08 22:57:38 +0200 | [diff] [blame] | 1514 | 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] | 1515 | { |
| 1516 | fit_image_print(fit, rd_noffset, " "); |
| 1517 | |
| 1518 | if (verify) { |
| 1519 | puts(" Verifying Hash Integrity ... "); |
| 1520 | if (!fit_image_verify(fit, rd_noffset)) { |
| 1521 | puts("Bad Data Hash\n"); |
| 1522 | return -EACCES; |
| 1523 | } |
| 1524 | puts("OK\n"); |
| 1525 | } |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1530 | int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name, |
| 1531 | ulong addr) |
| 1532 | { |
| 1533 | int cfg_noffset; |
| 1534 | void *fit_hdr; |
| 1535 | int noffset; |
| 1536 | |
| 1537 | debug("* %s: using config '%s' from image at 0x%08lx\n", |
| 1538 | prop_name, images->fit_uname_cfg, addr); |
| 1539 | |
| 1540 | /* Check whether configuration has this property defined */ |
| 1541 | fit_hdr = map_sysmem(addr, 0); |
| 1542 | cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); |
| 1543 | if (cfg_noffset < 0) { |
| 1544 | debug("* %s: no such config\n", prop_name); |
Paul Burton | bd86ef1 | 2016-09-20 18:17:12 +0100 | [diff] [blame] | 1545 | return -EINVAL; |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name); |
| 1549 | if (noffset < 0) { |
| 1550 | debug("* %s: no '%s' in config\n", prop_name, prop_name); |
Jonathan Gray | bac17b7 | 2016-09-03 08:30:14 +1000 | [diff] [blame] | 1551 | return -ENOENT; |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | return noffset; |
| 1555 | } |
| 1556 | |
Simon Glass | 126cc86 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 1557 | /** |
| 1558 | * fit_get_image_type_property() - get property name for IH_TYPE_... |
| 1559 | * |
| 1560 | * @return the properly name where we expect to find the image in the |
| 1561 | * config node |
| 1562 | */ |
| 1563 | static const char *fit_get_image_type_property(int type) |
| 1564 | { |
| 1565 | /* |
| 1566 | * 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] | 1567 | * 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] | 1568 | * anyway. So let's just keep it here. |
| 1569 | */ |
| 1570 | switch (type) { |
| 1571 | case IH_TYPE_FLATDT: |
| 1572 | return FIT_FDT_PROP; |
| 1573 | case IH_TYPE_KERNEL: |
| 1574 | return FIT_KERNEL_PROP; |
| 1575 | case IH_TYPE_RAMDISK: |
| 1576 | return FIT_RAMDISK_PROP; |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 1577 | case IH_TYPE_X86_SETUP: |
| 1578 | return FIT_SETUP_PROP; |
Karl Apsite | 84a07db | 2015-05-21 09:52:48 -0400 | [diff] [blame] | 1579 | case IH_TYPE_LOADABLE: |
| 1580 | return FIT_LOADABLE_PROP; |
Michal Simek | 62afc60 | 2016-05-17 14:03:50 +0200 | [diff] [blame] | 1581 | case IH_TYPE_FPGA: |
| 1582 | return FIT_FPGA_PROP; |
Simon Glass | 126cc86 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | return "unknown"; |
| 1586 | } |
| 1587 | |
| 1588 | int fit_image_load(bootm_headers_t *images, ulong addr, |
Simon Glass | f320a4d | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 1589 | const char **fit_unamep, const char **fit_uname_configp, |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1590 | int arch, int image_type, int bootstage_id, |
| 1591 | enum fit_load_op load_op, ulong *datap, ulong *lenp) |
| 1592 | { |
| 1593 | int cfg_noffset, noffset; |
| 1594 | const char *fit_uname; |
Simon Glass | f320a4d | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 1595 | const char *fit_uname_config; |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1596 | const void *fit; |
| 1597 | const void *buf; |
| 1598 | size_t size; |
| 1599 | int type_ok, os_ok; |
| 1600 | ulong load, data, len; |
Marek Vasut | 3811723 | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 1601 | uint8_t os; |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 1602 | #ifndef USE_HOSTCC |
| 1603 | uint8_t os_arch; |
| 1604 | #endif |
Simon Glass | 126cc86 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 1605 | const char *prop_name; |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1606 | int ret; |
| 1607 | |
| 1608 | fit = map_sysmem(addr, 0); |
| 1609 | fit_uname = fit_unamep ? *fit_unamep : NULL; |
Simon Glass | f320a4d | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 1610 | fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; |
Simon Glass | 126cc86 | 2014-06-12 07:24:47 -0600 | [diff] [blame] | 1611 | prop_name = fit_get_image_type_property(image_type); |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1612 | printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); |
| 1613 | |
| 1614 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); |
| 1615 | if (!fit_check_format(fit)) { |
| 1616 | printf("Bad FIT %s image format!\n", prop_name); |
| 1617 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); |
| 1618 | return -ENOEXEC; |
| 1619 | } |
| 1620 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); |
| 1621 | if (fit_uname) { |
Masahiro Yamada | f150c83 | 2014-02-18 15:39:21 +0900 | [diff] [blame] | 1622 | /* get FIT component image node offset */ |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1623 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); |
| 1624 | noffset = fit_image_get_node(fit, fit_uname); |
| 1625 | } else { |
| 1626 | /* |
| 1627 | * no image node unit name, try to get config |
| 1628 | * node first. If config unit node name is NULL |
| 1629 | * fit_conf_get_node() will try to find default config node |
| 1630 | */ |
| 1631 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); |
| 1632 | if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) { |
| 1633 | cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); |
| 1634 | } else { |
| 1635 | cfg_noffset = fit_conf_get_node(fit, |
| 1636 | fit_uname_config); |
| 1637 | } |
| 1638 | if (cfg_noffset < 0) { |
| 1639 | puts("Could not find configuration node\n"); |
| 1640 | bootstage_error(bootstage_id + |
| 1641 | BOOTSTAGE_SUB_NO_UNIT_NAME); |
| 1642 | return -ENOENT; |
| 1643 | } |
| 1644 | fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL); |
| 1645 | printf(" Using '%s' configuration\n", fit_uname_config); |
| 1646 | if (image_type == IH_TYPE_KERNEL) { |
| 1647 | /* Remember (and possibly verify) this config */ |
| 1648 | images->fit_uname_cfg = fit_uname_config; |
| 1649 | if (IMAGE_ENABLE_VERIFY && images->verify) { |
| 1650 | puts(" Verifying Hash Integrity ... "); |
Simon Glass | 12df2ab | 2014-06-12 07:24:45 -0600 | [diff] [blame] | 1651 | if (fit_config_verify(fit, cfg_noffset)) { |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1652 | puts("Bad Data Hash\n"); |
| 1653 | bootstage_error(bootstage_id + |
| 1654 | BOOTSTAGE_SUB_HASH); |
| 1655 | return -EACCES; |
| 1656 | } |
| 1657 | puts("OK\n"); |
| 1658 | } |
| 1659 | bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); |
| 1660 | } |
| 1661 | |
| 1662 | noffset = fit_conf_get_prop_node(fit, cfg_noffset, |
| 1663 | prop_name); |
| 1664 | fit_uname = fit_get_name(fit, noffset, NULL); |
| 1665 | } |
| 1666 | if (noffset < 0) { |
| 1667 | puts("Could not find subimage node\n"); |
| 1668 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); |
| 1669 | return -ENOENT; |
| 1670 | } |
| 1671 | |
| 1672 | printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); |
| 1673 | |
| 1674 | ret = fit_image_select(fit, noffset, images->verify); |
| 1675 | if (ret) { |
| 1676 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); |
Simon Glass | 5ba63dd | 2014-10-19 21:11:22 -0600 | [diff] [blame] | 1681 | #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX) |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1682 | if (!fit_image_check_target_arch(fit, noffset)) { |
| 1683 | puts("Unsupported Architecture\n"); |
| 1684 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); |
| 1685 | return -ENOEXEC; |
| 1686 | } |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 1687 | #endif |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 1688 | |
| 1689 | #ifndef USE_HOSTCC |
| 1690 | fit_image_get_arch(fit, noffset, &os_arch); |
| 1691 | images->os.arch = os_arch; |
| 1692 | #endif |
| 1693 | |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1694 | if (image_type == IH_TYPE_FLATDT && |
| 1695 | !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) { |
| 1696 | puts("FDT image is compressed"); |
| 1697 | return -EPROTONOSUPPORT; |
| 1698 | } |
| 1699 | |
| 1700 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); |
| 1701 | type_ok = fit_image_check_type(fit, noffset, image_type) || |
mario.six@gdsys.cc | e8fb435 | 2016-07-20 08:32:50 +0200 | [diff] [blame] | 1702 | fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) || |
| 1703 | (image_type == IH_TYPE_KERNEL && |
| 1704 | fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD)); |
Marek Vasut | 3811723 | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 1705 | |
Andreas Bießmann | 950fe26 | 2016-08-14 20:31:24 +0200 | [diff] [blame] | 1706 | os_ok = image_type == IH_TYPE_FLATDT || |
| 1707 | image_type == IH_TYPE_FPGA || |
Marek Vasut | 3811723 | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 1708 | fit_image_check_os(fit, noffset, IH_OS_LINUX) || |
mario.six@gdsys.cc | e8fb435 | 2016-07-20 08:32:50 +0200 | [diff] [blame] | 1709 | fit_image_check_os(fit, noffset, IH_OS_U_BOOT) || |
Marek Vasut | 3811723 | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 1710 | fit_image_check_os(fit, noffset, IH_OS_OPENRTOS); |
Karl Apsite | 84a07db | 2015-05-21 09:52:48 -0400 | [diff] [blame] | 1711 | |
| 1712 | /* |
| 1713 | * If either of the checks fail, we should report an error, but |
| 1714 | * if the image type is coming from the "loadables" field, we |
| 1715 | * don't care what it is |
| 1716 | */ |
| 1717 | if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) { |
Marek Vasut | 3811723 | 2014-12-16 14:07:22 +0100 | [diff] [blame] | 1718 | fit_image_get_os(fit, noffset, &os); |
| 1719 | printf("No %s %s %s Image\n", |
| 1720 | genimg_get_os_name(os), |
| 1721 | genimg_get_arch_name(arch), |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1722 | genimg_get_type_name(image_type)); |
| 1723 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); |
| 1724 | return -EIO; |
| 1725 | } |
| 1726 | |
| 1727 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); |
| 1728 | |
| 1729 | /* get image data address and length */ |
| 1730 | if (fit_image_get_data(fit, noffset, &buf, &size)) { |
| 1731 | printf("Could not find %s subimage data!\n", prop_name); |
| 1732 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); |
Simon Glass | 2f99807 | 2013-06-16 07:46:49 -0700 | [diff] [blame] | 1733 | return -ENOENT; |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1734 | } |
Andrew F. Davis | 44402fe | 2016-11-21 14:37:09 -0600 | [diff] [blame] | 1735 | |
| 1736 | #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS) |
| 1737 | /* perform any post-processing on the image data */ |
| 1738 | board_fit_image_post_process((void **)&buf, &size); |
| 1739 | #endif |
| 1740 | |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1741 | len = (ulong)size; |
| 1742 | |
| 1743 | /* verify that image data is a proper FDT blob */ |
Masahiro Yamada | 2f0877c | 2013-09-19 12:10:18 +0900 | [diff] [blame] | 1744 | if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) { |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1745 | puts("Subimage data is not a FDT"); |
| 1746 | return -ENOEXEC; |
| 1747 | } |
| 1748 | |
| 1749 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); |
| 1750 | |
| 1751 | /* |
| 1752 | * Work-around for eldk-4.2 which gives this warning if we try to |
Simon Glass | e3c83c0 | 2014-06-12 07:24:49 -0600 | [diff] [blame] | 1753 | * cast in the unmap_sysmem() call: |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1754 | * warning: initialization discards qualifiers from pointer target type |
| 1755 | */ |
| 1756 | { |
| 1757 | void *vbuf = (void *)buf; |
| 1758 | |
| 1759 | data = map_to_sysmem(vbuf); |
| 1760 | } |
| 1761 | |
| 1762 | if (load_op == FIT_LOAD_IGNORED) { |
| 1763 | /* Don't load */ |
| 1764 | } else if (fit_image_get_load(fit, noffset, &load)) { |
| 1765 | if (load_op == FIT_LOAD_REQUIRED) { |
| 1766 | printf("Can't get %s subimage load address!\n", |
| 1767 | prop_name); |
| 1768 | bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); |
| 1769 | return -EBADF; |
| 1770 | } |
Simon Glass | fe20a81 | 2014-08-22 14:26:43 -0600 | [diff] [blame] | 1771 | } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) { |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1772 | ulong image_start, image_end; |
| 1773 | ulong load_end; |
| 1774 | void *dst; |
| 1775 | |
| 1776 | /* |
| 1777 | * move image data to the load address, |
| 1778 | * make sure we don't overwrite initial image |
| 1779 | */ |
| 1780 | image_start = addr; |
| 1781 | image_end = addr + fit_get_size(fit); |
| 1782 | |
| 1783 | load_end = load + len; |
| 1784 | if (image_type != IH_TYPE_KERNEL && |
| 1785 | load < image_end && load_end > image_start) { |
| 1786 | printf("Error: %s overwritten\n", prop_name); |
| 1787 | return -EXDEV; |
| 1788 | } |
| 1789 | |
| 1790 | printf(" Loading %s from 0x%08lx to 0x%08lx\n", |
| 1791 | prop_name, data, load); |
| 1792 | |
| 1793 | dst = map_sysmem(load, len); |
| 1794 | memmove(dst, buf, len); |
| 1795 | data = load; |
| 1796 | } |
| 1797 | bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); |
| 1798 | |
| 1799 | *datap = data; |
| 1800 | *lenp = len; |
| 1801 | if (fit_unamep) |
| 1802 | *fit_unamep = (char *)fit_uname; |
Simon Glass | f320a4d | 2013-07-10 23:08:10 -0700 | [diff] [blame] | 1803 | if (fit_uname_configp) |
| 1804 | *fit_uname_configp = (char *)fit_uname_config; |
Simon Glass | 782cfbb | 2013-05-16 13:53:21 +0000 | [diff] [blame] | 1805 | |
| 1806 | return noffset; |
| 1807 | } |
Simon Glass | 90268b8 | 2014-10-19 21:11:24 -0600 | [diff] [blame] | 1808 | |
| 1809 | int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch, |
| 1810 | ulong *setup_start, ulong *setup_len) |
| 1811 | { |
| 1812 | int noffset; |
| 1813 | ulong addr; |
| 1814 | ulong len; |
| 1815 | int ret; |
| 1816 | |
| 1817 | addr = map_to_sysmem(images->fit_hdr_os); |
| 1818 | noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr); |
| 1819 | if (noffset < 0) |
| 1820 | return noffset; |
| 1821 | |
| 1822 | ret = fit_image_load(images, addr, NULL, NULL, arch, |
| 1823 | IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START, |
| 1824 | FIT_LOAD_REQUIRED, setup_start, &len); |
| 1825 | |
| 1826 | return ret; |
| 1827 | } |