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 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #ifdef USE_HOSTCC |
| 29 | #include "mkimage.h" |
| 30 | #include <image.h> |
| 31 | #include <time.h> |
| 32 | #else |
| 33 | #include <common.h> |
| 34 | #endif /* !USE_HOSTCC*/ |
| 35 | |
| 36 | #include <bootstage.h> |
| 37 | #include <sha1.h> |
| 38 | #include <u-boot/crc.h> |
| 39 | #include <u-boot/md5.h> |
| 40 | |
| 41 | /*****************************************************************************/ |
| 42 | /* New uImage format routines */ |
| 43 | /*****************************************************************************/ |
| 44 | #ifndef USE_HOSTCC |
| 45 | static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, |
| 46 | ulong *addr, const char **name) |
| 47 | { |
| 48 | const char *sep; |
| 49 | |
| 50 | *addr = addr_curr; |
| 51 | *name = NULL; |
| 52 | |
| 53 | sep = strchr(spec, sepc); |
| 54 | if (sep) { |
| 55 | if (sep - spec > 0) |
| 56 | *addr = simple_strtoul(spec, NULL, 16); |
| 57 | |
| 58 | *name = sep + 1; |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * fit_parse_conf - parse FIT configuration spec |
| 67 | * @spec: input string, containing configuration spec |
| 68 | * @add_curr: current image address (to be used as a possible default) |
| 69 | * @addr: pointer to a ulong variable, will hold FIT image address of a given |
| 70 | * configuration |
| 71 | * @conf_name double pointer to a char, will hold pointer to a configuration |
| 72 | * unit name |
| 73 | * |
| 74 | * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>, |
| 75 | * where <addr> is a FIT image address that contains configuration |
| 76 | * with a <conf> unit name. |
| 77 | * |
| 78 | * Address part is optional, and if omitted default add_curr will |
| 79 | * be used instead. |
| 80 | * |
| 81 | * returns: |
| 82 | * 1 if spec is a valid configuration string, |
| 83 | * addr and conf_name are set accordingly |
| 84 | * 0 otherwise |
| 85 | */ |
| 86 | int fit_parse_conf(const char *spec, ulong addr_curr, |
| 87 | ulong *addr, const char **conf_name) |
| 88 | { |
| 89 | return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * fit_parse_subimage - parse FIT subimage spec |
| 94 | * @spec: input string, containing subimage spec |
| 95 | * @add_curr: current image address (to be used as a possible default) |
| 96 | * @addr: pointer to a ulong variable, will hold FIT image address of a given |
| 97 | * subimage |
| 98 | * @image_name: double pointer to a char, will hold pointer to a subimage name |
| 99 | * |
| 100 | * fit_parse_subimage() expects subimage spec in the for of |
| 101 | * [<addr>]:<subimage>, where <addr> is a FIT image address that contains |
| 102 | * subimage with a <subimg> unit name. |
| 103 | * |
| 104 | * Address part is optional, and if omitted default add_curr will |
| 105 | * be used instead. |
| 106 | * |
| 107 | * returns: |
| 108 | * 1 if spec is a valid subimage string, |
| 109 | * addr and image_name are set accordingly |
| 110 | * 0 otherwise |
| 111 | */ |
| 112 | int fit_parse_subimage(const char *spec, ulong addr_curr, |
| 113 | ulong *addr, const char **image_name) |
| 114 | { |
| 115 | return fit_parse_spec(spec, ':', addr_curr, addr, image_name); |
| 116 | } |
| 117 | #endif /* !USE_HOSTCC */ |
| 118 | |
| 119 | static void fit_get_debug(const void *fit, int noffset, |
| 120 | char *prop_name, int err) |
| 121 | { |
| 122 | debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n", |
| 123 | prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL), |
| 124 | fdt_strerror(err)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * fit_print_contents - prints out the contents of the FIT format image |
| 129 | * @fit: pointer to the FIT format image header |
| 130 | * @p: pointer to prefix string |
| 131 | * |
| 132 | * fit_print_contents() formats a multi line FIT image contents description. |
| 133 | * The routine prints out FIT image properties (root node level) follwed by |
| 134 | * the details of each component image. |
| 135 | * |
| 136 | * returns: |
| 137 | * no returned results |
| 138 | */ |
| 139 | void fit_print_contents(const void *fit) |
| 140 | { |
| 141 | char *desc; |
| 142 | char *uname; |
| 143 | int images_noffset; |
| 144 | int confs_noffset; |
| 145 | int noffset; |
| 146 | int ndepth; |
| 147 | int count = 0; |
| 148 | int ret; |
| 149 | const char *p; |
| 150 | time_t timestamp; |
| 151 | |
| 152 | #ifdef USE_HOSTCC |
| 153 | p = ""; |
| 154 | #else |
| 155 | p = " "; |
| 156 | #endif |
| 157 | |
| 158 | /* Root node properties */ |
| 159 | ret = fit_get_desc(fit, 0, &desc); |
| 160 | printf("%sFIT description: ", p); |
| 161 | if (ret) |
| 162 | printf("unavailable\n"); |
| 163 | else |
| 164 | printf("%s\n", desc); |
| 165 | |
| 166 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 167 | ret = fit_get_timestamp(fit, 0, ×tamp); |
| 168 | printf("%sCreated: ", p); |
| 169 | if (ret) |
| 170 | printf("unavailable\n"); |
| 171 | else |
| 172 | genimg_print_time(timestamp); |
| 173 | } |
| 174 | |
| 175 | /* Find images parent node offset */ |
| 176 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 177 | if (images_noffset < 0) { |
| 178 | printf("Can't find images parent node '%s' (%s)\n", |
| 179 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | /* Process its subnodes, print out component images details */ |
| 184 | for (ndepth = 0, count = 0, |
| 185 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 186 | (noffset >= 0) && (ndepth > 0); |
| 187 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 188 | if (ndepth == 1) { |
| 189 | /* |
| 190 | * Direct child node of the images parent node, |
| 191 | * i.e. component image node. |
| 192 | */ |
| 193 | printf("%s Image %u (%s)\n", p, count++, |
| 194 | fit_get_name(fit, noffset, NULL)); |
| 195 | |
| 196 | fit_image_print(fit, noffset, p); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* Find configurations parent node offset */ |
| 201 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 202 | if (confs_noffset < 0) { |
| 203 | debug("Can't get configurations parent node '%s' (%s)\n", |
| 204 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | /* get default configuration unit name from default property */ |
| 209 | uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); |
| 210 | if (uname) |
| 211 | printf("%s Default Configuration: '%s'\n", p, uname); |
| 212 | |
| 213 | /* Process its subnodes, print out configurations details */ |
| 214 | for (ndepth = 0, count = 0, |
| 215 | noffset = fdt_next_node(fit, confs_noffset, &ndepth); |
| 216 | (noffset >= 0) && (ndepth > 0); |
| 217 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 218 | if (ndepth == 1) { |
| 219 | /* |
| 220 | * Direct child node of the configurations parent node, |
| 221 | * i.e. configuration node. |
| 222 | */ |
| 223 | printf("%s Configuration %u (%s)\n", p, count++, |
| 224 | fit_get_name(fit, noffset, NULL)); |
| 225 | |
| 226 | fit_conf_print(fit, noffset, p); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * fit_image_print - prints out the FIT component image details |
| 233 | * @fit: pointer to the FIT format image header |
| 234 | * @image_noffset: offset of the component image node |
| 235 | * @p: pointer to prefix string |
| 236 | * |
| 237 | * fit_image_print() lists all mandatory properies for the processed component |
| 238 | * image. If present, hash nodes are printed out as well. Load |
| 239 | * address for images of type firmware is also printed out. Since the load |
| 240 | * address is not mandatory for firmware images, it will be output as |
| 241 | * "unavailable" when not present. |
| 242 | * |
| 243 | * returns: |
| 244 | * no returned results |
| 245 | */ |
| 246 | void fit_image_print(const void *fit, int image_noffset, const char *p) |
| 247 | { |
| 248 | char *desc; |
| 249 | uint8_t type, arch, os, comp; |
| 250 | size_t size; |
| 251 | ulong load, entry; |
| 252 | const void *data; |
| 253 | int noffset; |
| 254 | int ndepth; |
| 255 | int ret; |
| 256 | |
| 257 | /* Mandatory properties */ |
| 258 | ret = fit_get_desc(fit, image_noffset, &desc); |
| 259 | printf("%s Description: ", p); |
| 260 | if (ret) |
| 261 | printf("unavailable\n"); |
| 262 | else |
| 263 | printf("%s\n", desc); |
| 264 | |
| 265 | fit_image_get_type(fit, image_noffset, &type); |
| 266 | printf("%s Type: %s\n", p, genimg_get_type_name(type)); |
| 267 | |
| 268 | fit_image_get_comp(fit, image_noffset, &comp); |
| 269 | printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); |
| 270 | |
| 271 | ret = fit_image_get_data(fit, image_noffset, &data, &size); |
| 272 | |
| 273 | #ifndef USE_HOSTCC |
| 274 | printf("%s Data Start: ", p); |
| 275 | if (ret) |
| 276 | printf("unavailable\n"); |
| 277 | else |
| 278 | printf("0x%08lx\n", (ulong)data); |
| 279 | #endif |
| 280 | |
| 281 | printf("%s Data Size: ", p); |
| 282 | if (ret) |
| 283 | printf("unavailable\n"); |
| 284 | else |
| 285 | genimg_print_size(size); |
| 286 | |
| 287 | /* Remaining, type dependent properties */ |
| 288 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 289 | (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || |
| 290 | (type == IH_TYPE_FLATDT)) { |
| 291 | fit_image_get_arch(fit, image_noffset, &arch); |
| 292 | printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); |
| 293 | } |
| 294 | |
| 295 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) { |
| 296 | fit_image_get_os(fit, image_noffset, &os); |
| 297 | printf("%s OS: %s\n", p, genimg_get_os_name(os)); |
| 298 | } |
| 299 | |
| 300 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 301 | (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) { |
| 302 | ret = fit_image_get_load(fit, image_noffset, &load); |
| 303 | printf("%s Load Address: ", p); |
| 304 | if (ret) |
| 305 | printf("unavailable\n"); |
| 306 | else |
| 307 | printf("0x%08lx\n", load); |
| 308 | } |
| 309 | |
| 310 | if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || |
| 311 | (type == IH_TYPE_RAMDISK)) { |
| 312 | fit_image_get_entry(fit, image_noffset, &entry); |
| 313 | printf("%s Entry Point: ", p); |
| 314 | if (ret) |
| 315 | printf("unavailable\n"); |
| 316 | else |
| 317 | printf("0x%08lx\n", entry); |
| 318 | } |
| 319 | |
| 320 | /* Process all hash subnodes of the component image node */ |
| 321 | for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); |
| 322 | (noffset >= 0) && (ndepth > 0); |
| 323 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 324 | if (ndepth == 1) { |
| 325 | /* Direct child node of the component image node */ |
| 326 | fit_image_print_hash(fit, noffset, p); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * fit_image_print_hash - prints out the hash node details |
| 333 | * @fit: pointer to the FIT format image header |
| 334 | * @noffset: offset of the hash node |
| 335 | * @p: pointer to prefix string |
| 336 | * |
| 337 | * fit_image_print_hash() lists properies for the processed hash node |
| 338 | * |
| 339 | * returns: |
| 340 | * no returned results |
| 341 | */ |
| 342 | void fit_image_print_hash(const void *fit, int noffset, const char *p) |
| 343 | { |
| 344 | char *algo; |
| 345 | uint8_t *value; |
| 346 | int value_len; |
| 347 | int i, ret; |
| 348 | |
| 349 | /* |
| 350 | * Check subnode name, must be equal to "hash". |
| 351 | * Multiple hash nodes require unique unit node |
| 352 | * names, e.g. hash@1, hash@2, etc. |
| 353 | */ |
| 354 | if (strncmp(fit_get_name(fit, noffset, NULL), |
| 355 | FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)) != 0) |
| 356 | return; |
| 357 | |
| 358 | debug("%s Hash node: '%s'\n", p, |
| 359 | fit_get_name(fit, noffset, NULL)); |
| 360 | |
| 361 | printf("%s Hash algo: ", p); |
| 362 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
| 363 | printf("invalid/unsupported\n"); |
| 364 | return; |
| 365 | } |
| 366 | printf("%s\n", algo); |
| 367 | |
| 368 | ret = fit_image_hash_get_value(fit, noffset, &value, |
| 369 | &value_len); |
| 370 | printf("%s Hash value: ", p); |
| 371 | if (ret) { |
| 372 | printf("unavailable\n"); |
| 373 | } else { |
| 374 | for (i = 0; i < value_len; i++) |
| 375 | printf("%02x", value[i]); |
| 376 | printf("\n"); |
| 377 | } |
| 378 | |
| 379 | debug("%s Hash len: %d\n", p, value_len); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * fit_get_desc - get node description property |
| 384 | * @fit: pointer to the FIT format image header |
| 385 | * @noffset: node offset |
| 386 | * @desc: double pointer to the char, will hold pointer to the descrption |
| 387 | * |
| 388 | * fit_get_desc() reads description property from a given node, if |
| 389 | * description is found pointer to it is returened in third call argument. |
| 390 | * |
| 391 | * returns: |
| 392 | * 0, on success |
| 393 | * -1, on failure |
| 394 | */ |
| 395 | int fit_get_desc(const void *fit, int noffset, char **desc) |
| 396 | { |
| 397 | int len; |
| 398 | |
| 399 | *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); |
| 400 | if (*desc == NULL) { |
| 401 | fit_get_debug(fit, noffset, FIT_DESC_PROP, len); |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * fit_get_timestamp - get node timestamp property |
| 410 | * @fit: pointer to the FIT format image header |
| 411 | * @noffset: node offset |
| 412 | * @timestamp: pointer to the time_t, will hold read timestamp |
| 413 | * |
| 414 | * fit_get_timestamp() reads timestamp poperty from given node, if timestamp |
| 415 | * is found and has a correct size its value is retured in third call |
| 416 | * argument. |
| 417 | * |
| 418 | * returns: |
| 419 | * 0, on success |
| 420 | * -1, on property read failure |
| 421 | * -2, on wrong timestamp size |
| 422 | */ |
| 423 | int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) |
| 424 | { |
| 425 | int len; |
| 426 | const void *data; |
| 427 | |
| 428 | data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); |
| 429 | if (data == NULL) { |
| 430 | fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); |
| 431 | return -1; |
| 432 | } |
| 433 | if (len != sizeof(uint32_t)) { |
| 434 | debug("FIT timestamp with incorrect size of (%u)\n", len); |
| 435 | return -2; |
| 436 | } |
| 437 | |
| 438 | *timestamp = uimage_to_cpu(*((uint32_t *)data)); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * fit_image_get_node - get node offset for component image of a given unit name |
| 444 | * @fit: pointer to the FIT format image header |
| 445 | * @image_uname: component image node unit name |
| 446 | * |
| 447 | * fit_image_get_node() finds a component image (withing the '/images' |
| 448 | * node) of a provided unit name. If image is found its node offset is |
| 449 | * returned to the caller. |
| 450 | * |
| 451 | * returns: |
| 452 | * image node offset when found (>=0) |
| 453 | * negative number on failure (FDT_ERR_* code) |
| 454 | */ |
| 455 | int fit_image_get_node(const void *fit, const char *image_uname) |
| 456 | { |
| 457 | int noffset, images_noffset; |
| 458 | |
| 459 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 460 | if (images_noffset < 0) { |
| 461 | debug("Can't find images parent node '%s' (%s)\n", |
| 462 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 463 | return images_noffset; |
| 464 | } |
| 465 | |
| 466 | noffset = fdt_subnode_offset(fit, images_noffset, image_uname); |
| 467 | if (noffset < 0) { |
| 468 | debug("Can't get node offset for image unit name: '%s' (%s)\n", |
| 469 | image_uname, fdt_strerror(noffset)); |
| 470 | } |
| 471 | |
| 472 | return noffset; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * fit_image_get_os - get os id for a given component image node |
| 477 | * @fit: pointer to the FIT format image header |
| 478 | * @noffset: component image node offset |
| 479 | * @os: pointer to the uint8_t, will hold os numeric id |
| 480 | * |
| 481 | * fit_image_get_os() finds os property in a given component image node. |
| 482 | * If the property is found, its (string) value is translated to the numeric |
| 483 | * id which is returned to the caller. |
| 484 | * |
| 485 | * returns: |
| 486 | * 0, on success |
| 487 | * -1, on failure |
| 488 | */ |
| 489 | int fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
| 490 | { |
| 491 | int len; |
| 492 | const void *data; |
| 493 | |
| 494 | /* Get OS name from property data */ |
| 495 | data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); |
| 496 | if (data == NULL) { |
| 497 | fit_get_debug(fit, noffset, FIT_OS_PROP, len); |
| 498 | *os = -1; |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | /* Translate OS name to id */ |
| 503 | *os = genimg_get_os_id(data); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * fit_image_get_arch - get arch id for a given component image node |
| 509 | * @fit: pointer to the FIT format image header |
| 510 | * @noffset: component image node offset |
| 511 | * @arch: pointer to the uint8_t, will hold arch numeric id |
| 512 | * |
| 513 | * fit_image_get_arch() finds arch property in a given component image node. |
| 514 | * If the property is found, its (string) value is translated to the numeric |
| 515 | * id which is returned to the caller. |
| 516 | * |
| 517 | * returns: |
| 518 | * 0, on success |
| 519 | * -1, on failure |
| 520 | */ |
| 521 | int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) |
| 522 | { |
| 523 | int len; |
| 524 | const void *data; |
| 525 | |
| 526 | /* Get architecture name from property data */ |
| 527 | data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); |
| 528 | if (data == NULL) { |
| 529 | fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); |
| 530 | *arch = -1; |
| 531 | return -1; |
| 532 | } |
| 533 | |
| 534 | /* Translate architecture name to id */ |
| 535 | *arch = genimg_get_arch_id(data); |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * fit_image_get_type - get type id for a given component image node |
| 541 | * @fit: pointer to the FIT format image header |
| 542 | * @noffset: component image node offset |
| 543 | * @type: pointer to the uint8_t, will hold type numeric id |
| 544 | * |
| 545 | * fit_image_get_type() finds type property in a given component image node. |
| 546 | * If the property is found, its (string) value is translated to the numeric |
| 547 | * id which is returned to the caller. |
| 548 | * |
| 549 | * returns: |
| 550 | * 0, on success |
| 551 | * -1, on failure |
| 552 | */ |
| 553 | int fit_image_get_type(const void *fit, int noffset, uint8_t *type) |
| 554 | { |
| 555 | int len; |
| 556 | const void *data; |
| 557 | |
| 558 | /* Get image type name from property data */ |
| 559 | data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); |
| 560 | if (data == NULL) { |
| 561 | fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); |
| 562 | *type = -1; |
| 563 | return -1; |
| 564 | } |
| 565 | |
| 566 | /* Translate image type name to id */ |
| 567 | *type = genimg_get_type_id(data); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * fit_image_get_comp - get comp id for a given component image node |
| 573 | * @fit: pointer to the FIT format image header |
| 574 | * @noffset: component image node offset |
| 575 | * @comp: pointer to the uint8_t, will hold comp numeric id |
| 576 | * |
| 577 | * fit_image_get_comp() finds comp property in a given component image node. |
| 578 | * If the property is found, its (string) value is translated to the numeric |
| 579 | * id which is returned to the caller. |
| 580 | * |
| 581 | * returns: |
| 582 | * 0, on success |
| 583 | * -1, on failure |
| 584 | */ |
| 585 | int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) |
| 586 | { |
| 587 | int len; |
| 588 | const void *data; |
| 589 | |
| 590 | /* Get compression name from property data */ |
| 591 | data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); |
| 592 | if (data == NULL) { |
| 593 | fit_get_debug(fit, noffset, FIT_COMP_PROP, len); |
| 594 | *comp = -1; |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | /* Translate compression name to id */ |
| 599 | *comp = genimg_get_comp_id(data); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * fit_image_get_load() - get load addr property for given component image node |
| 605 | * @fit: pointer to the FIT format image header |
| 606 | * @noffset: component image node offset |
| 607 | * @load: pointer to the uint32_t, will hold load address |
| 608 | * |
| 609 | * fit_image_get_load() finds load address property in a given component |
| 610 | * image node. If the property is found, its value is returned to the caller. |
| 611 | * |
| 612 | * returns: |
| 613 | * 0, on success |
| 614 | * -1, on failure |
| 615 | */ |
| 616 | int fit_image_get_load(const void *fit, int noffset, ulong *load) |
| 617 | { |
| 618 | int len; |
| 619 | const uint32_t *data; |
| 620 | |
| 621 | data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len); |
| 622 | if (data == NULL) { |
| 623 | fit_get_debug(fit, noffset, FIT_LOAD_PROP, len); |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | *load = uimage_to_cpu(*data); |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * fit_image_get_entry() - get entry point address property |
| 633 | * @fit: pointer to the FIT format image header |
| 634 | * @noffset: component image node offset |
| 635 | * @entry: pointer to the uint32_t, will hold entry point address |
| 636 | * |
| 637 | * This gets the entry point address property for a given component image |
| 638 | * node. |
| 639 | * |
| 640 | * fit_image_get_entry() finds entry point address property in a given |
| 641 | * component image node. If the property is found, its value is returned |
| 642 | * to the caller. |
| 643 | * |
| 644 | * returns: |
| 645 | * 0, on success |
| 646 | * -1, on failure |
| 647 | */ |
| 648 | int fit_image_get_entry(const void *fit, int noffset, ulong *entry) |
| 649 | { |
| 650 | int len; |
| 651 | const uint32_t *data; |
| 652 | |
| 653 | data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len); |
| 654 | if (data == NULL) { |
| 655 | fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len); |
| 656 | return -1; |
| 657 | } |
| 658 | |
| 659 | *entry = uimage_to_cpu(*data); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * fit_image_get_data - get data property and its size for a given component image node |
| 665 | * @fit: pointer to the FIT format image header |
| 666 | * @noffset: component image node offset |
| 667 | * @data: double pointer to void, will hold data property's data address |
| 668 | * @size: pointer to size_t, will hold data property's data size |
| 669 | * |
| 670 | * fit_image_get_data() finds data property in a given component image node. |
| 671 | * If the property is found its data start address and size are returned to |
| 672 | * the caller. |
| 673 | * |
| 674 | * returns: |
| 675 | * 0, on success |
| 676 | * -1, on failure |
| 677 | */ |
| 678 | int fit_image_get_data(const void *fit, int noffset, |
| 679 | const void **data, size_t *size) |
| 680 | { |
| 681 | int len; |
| 682 | |
| 683 | *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); |
| 684 | if (*data == NULL) { |
| 685 | fit_get_debug(fit, noffset, FIT_DATA_PROP, len); |
| 686 | *size = 0; |
| 687 | return -1; |
| 688 | } |
| 689 | |
| 690 | *size = len; |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * fit_image_hash_get_algo - get hash algorithm name |
| 696 | * @fit: pointer to the FIT format image header |
| 697 | * @noffset: hash node offset |
| 698 | * @algo: double pointer to char, will hold pointer to the algorithm name |
| 699 | * |
| 700 | * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. |
| 701 | * If the property is found its data start address is returned to the caller. |
| 702 | * |
| 703 | * returns: |
| 704 | * 0, on success |
| 705 | * -1, on failure |
| 706 | */ |
| 707 | int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) |
| 708 | { |
| 709 | int len; |
| 710 | |
| 711 | *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); |
| 712 | if (*algo == NULL) { |
| 713 | fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); |
| 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * fit_image_hash_get_value - get hash value and length |
| 722 | * @fit: pointer to the FIT format image header |
| 723 | * @noffset: hash node offset |
| 724 | * @value: double pointer to uint8_t, will hold address of a hash value data |
| 725 | * @value_len: pointer to an int, will hold hash data length |
| 726 | * |
| 727 | * fit_image_hash_get_value() finds hash value property in a given hash node. |
| 728 | * If the property is found its data start address and size are returned to |
| 729 | * the caller. |
| 730 | * |
| 731 | * returns: |
| 732 | * 0, on success |
| 733 | * -1, on failure |
| 734 | */ |
| 735 | int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, |
| 736 | int *value_len) |
| 737 | { |
| 738 | int len; |
| 739 | |
| 740 | *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); |
| 741 | if (*value == NULL) { |
| 742 | fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); |
| 743 | *value_len = 0; |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | *value_len = len; |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | #ifndef USE_HOSTCC |
| 752 | /** |
| 753 | * fit_image_hash_get_ignore - get hash ignore flag |
| 754 | * @fit: pointer to the FIT format image header |
| 755 | * @noffset: hash node offset |
| 756 | * @ignore: pointer to an int, will hold hash ignore flag |
| 757 | * |
| 758 | * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. |
| 759 | * If the property is found and non-zero, the hash algorithm is not verified by |
| 760 | * u-boot automatically. |
| 761 | * |
| 762 | * returns: |
| 763 | * 0, on ignore not found |
| 764 | * value, on ignore found |
| 765 | */ |
| 766 | int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) |
| 767 | { |
| 768 | int len; |
| 769 | int *value; |
| 770 | |
| 771 | value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); |
| 772 | if (value == NULL || len != sizeof(int)) |
| 773 | *ignore = 0; |
| 774 | else |
| 775 | *ignore = *value; |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | #endif |
| 780 | |
| 781 | /** |
| 782 | * fit_set_timestamp - set node timestamp property |
| 783 | * @fit: pointer to the FIT format image header |
| 784 | * @noffset: node offset |
| 785 | * @timestamp: timestamp value to be set |
| 786 | * |
| 787 | * fit_set_timestamp() attempts to set timestamp property in the requested |
| 788 | * node and returns operation status to the caller. |
| 789 | * |
| 790 | * returns: |
| 791 | * 0, on success |
| 792 | * -1, on property read failure |
| 793 | */ |
| 794 | int fit_set_timestamp(void *fit, int noffset, time_t timestamp) |
| 795 | { |
| 796 | uint32_t t; |
| 797 | int ret; |
| 798 | |
| 799 | t = cpu_to_uimage(timestamp); |
| 800 | ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, |
| 801 | sizeof(uint32_t)); |
| 802 | if (ret) { |
| 803 | printf("Can't set '%s' property for '%s' node (%s)\n", |
| 804 | FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), |
| 805 | fdt_strerror(ret)); |
| 806 | return -1; |
| 807 | } |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * calculate_hash - calculate and return hash for provided input data |
| 814 | * @data: pointer to the input data |
| 815 | * @data_len: data length |
| 816 | * @algo: requested hash algorithm |
| 817 | * @value: pointer to the char, will hold hash value data (caller must |
| 818 | * allocate enough free space) |
| 819 | * value_len: length of the calculated hash |
| 820 | * |
| 821 | * calculate_hash() computes input data hash according to the requested |
| 822 | * algorithm. |
| 823 | * Resulting hash value is placed in caller provided 'value' buffer, length |
| 824 | * of the calculated hash is returned via value_len pointer argument. |
| 825 | * |
| 826 | * returns: |
| 827 | * 0, on success |
| 828 | * -1, when algo is unsupported |
| 829 | */ |
Simon Glass | 604f23d | 2013-05-07 06:11:54 +0000 | [diff] [blame] | 830 | int calculate_hash(const void *data, int data_len, const char *algo, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 831 | uint8_t *value, int *value_len) |
| 832 | { |
| 833 | if (strcmp(algo, "crc32") == 0) { |
| 834 | *((uint32_t *)value) = crc32_wd(0, data, data_len, |
| 835 | CHUNKSZ_CRC32); |
| 836 | *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); |
| 837 | *value_len = 4; |
| 838 | } else if (strcmp(algo, "sha1") == 0) { |
| 839 | sha1_csum_wd((unsigned char *)data, data_len, |
| 840 | (unsigned char *)value, CHUNKSZ_SHA1); |
| 841 | *value_len = 20; |
| 842 | } else if (strcmp(algo, "md5") == 0) { |
| 843 | md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); |
| 844 | *value_len = 16; |
| 845 | } else { |
| 846 | debug("Unsupported hash alogrithm\n"); |
| 847 | return -1; |
| 848 | } |
| 849 | return 0; |
| 850 | } |
| 851 | |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 852 | /** |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 853 | * fit_image_verify - verify data intergity |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 854 | * @fit: pointer to the FIT format image header |
| 855 | * @image_noffset: component image node offset |
| 856 | * |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 857 | * fit_image_verify() goes over component image hash nodes, |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 858 | * re-calculates each data hash and compares with the value stored in hash |
| 859 | * node. |
| 860 | * |
| 861 | * returns: |
| 862 | * 1, if all hashes are valid |
| 863 | * 0, otherwise (or on error) |
| 864 | */ |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 865 | int fit_image_verify(const void *fit, int image_noffset) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 866 | { |
| 867 | const void *data; |
| 868 | size_t size; |
| 869 | char *algo; |
| 870 | uint8_t *fit_value; |
| 871 | int fit_value_len; |
| 872 | #ifndef USE_HOSTCC |
| 873 | int ignore; |
| 874 | #endif |
| 875 | uint8_t value[FIT_MAX_HASH_LEN]; |
| 876 | int value_len; |
| 877 | int noffset; |
| 878 | int ndepth; |
| 879 | char *err_msg = ""; |
| 880 | |
| 881 | /* Get image data and data length */ |
| 882 | if (fit_image_get_data(fit, image_noffset, &data, &size)) { |
| 883 | printf("Can't get image data/size\n"); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* Process all hash subnodes of the component image node */ |
| 888 | for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); |
| 889 | (noffset >= 0) && (ndepth > 0); |
| 890 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 891 | if (ndepth == 1) { |
| 892 | /* Direct child node of the component image node */ |
| 893 | |
| 894 | /* |
| 895 | * Check subnode name, must be equal to "hash". |
| 896 | * Multiple hash nodes require unique unit node |
| 897 | * names, e.g. hash@1, hash@2, etc. |
| 898 | */ |
| 899 | if (strncmp(fit_get_name(fit, noffset, NULL), |
| 900 | FIT_HASH_NODENAME, |
| 901 | strlen(FIT_HASH_NODENAME)) != 0) |
| 902 | continue; |
| 903 | |
| 904 | if (fit_image_hash_get_algo(fit, noffset, &algo)) { |
| 905 | err_msg = " error!\nCan't get hash algo property"; |
| 906 | goto error; |
| 907 | } |
| 908 | printf("%s", algo); |
| 909 | |
| 910 | #ifndef USE_HOSTCC |
| 911 | fit_image_hash_get_ignore(fit, noffset, &ignore); |
| 912 | if (ignore) { |
| 913 | printf("-skipped "); |
| 914 | continue; |
| 915 | } |
| 916 | #endif |
| 917 | |
| 918 | if (fit_image_hash_get_value(fit, noffset, &fit_value, |
| 919 | &fit_value_len)) { |
| 920 | err_msg = " error!\nCan't get hash value " |
| 921 | "property"; |
| 922 | goto error; |
| 923 | } |
| 924 | |
| 925 | if (calculate_hash(data, size, algo, value, |
| 926 | &value_len)) { |
| 927 | err_msg = " error!\n" |
| 928 | "Unsupported hash algorithm"; |
| 929 | goto error; |
| 930 | } |
| 931 | |
| 932 | if (value_len != fit_value_len) { |
| 933 | err_msg = " error !\nBad hash value len"; |
| 934 | goto error; |
| 935 | } else if (memcmp(value, fit_value, value_len) != 0) { |
| 936 | err_msg = " error!\nBad hash value"; |
| 937 | goto error; |
| 938 | } |
| 939 | printf("+ "); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { |
| 944 | err_msg = " error!\nCorrupted or truncated tree"; |
| 945 | goto error; |
| 946 | } |
| 947 | |
| 948 | return 1; |
| 949 | |
| 950 | error: |
| 951 | printf("%s for '%s' hash node in '%s' image node\n", |
| 952 | err_msg, fit_get_name(fit, noffset, NULL), |
| 953 | fit_get_name(fit, image_noffset, NULL)); |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | /** |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 958 | * fit_all_image_verify - verify data intergity for all images |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 959 | * @fit: pointer to the FIT format image header |
| 960 | * |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 961 | * fit_all_image_verify() goes over all images in the FIT and |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 962 | * for every images checks if all it's hashes are valid. |
| 963 | * |
| 964 | * returns: |
| 965 | * 1, if all hashes of all images are valid |
| 966 | * 0, otherwise (or on error) |
| 967 | */ |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 968 | int fit_all_image_verify(const void *fit) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 969 | { |
| 970 | int images_noffset; |
| 971 | int noffset; |
| 972 | int ndepth; |
| 973 | int count; |
| 974 | |
| 975 | /* Find images parent node offset */ |
| 976 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 977 | if (images_noffset < 0) { |
| 978 | printf("Can't find images parent node '%s' (%s)\n", |
| 979 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | /* Process all image subnodes, check hashes for each */ |
| 984 | printf("## Checking hash(es) for FIT Image at %08lx ...\n", |
| 985 | (ulong)fit); |
| 986 | for (ndepth = 0, count = 0, |
| 987 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 988 | (noffset >= 0) && (ndepth > 0); |
| 989 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 990 | if (ndepth == 1) { |
| 991 | /* |
| 992 | * Direct child node of the images parent node, |
| 993 | * i.e. component image node. |
| 994 | */ |
| 995 | printf(" Hash(es) for Image %u (%s): ", count++, |
| 996 | fit_get_name(fit, noffset, NULL)); |
| 997 | |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 998 | if (!fit_image_verify(fit, noffset)) |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 999 | return 0; |
| 1000 | printf("\n"); |
| 1001 | } |
| 1002 | } |
| 1003 | return 1; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * fit_image_check_os - check whether image node is of a given os type |
| 1008 | * @fit: pointer to the FIT format image header |
| 1009 | * @noffset: component image node offset |
| 1010 | * @os: requested image os |
| 1011 | * |
| 1012 | * fit_image_check_os() reads image os property and compares its numeric |
| 1013 | * id with the requested os. Comparison result is returned to the caller. |
| 1014 | * |
| 1015 | * returns: |
| 1016 | * 1 if image is of given os type |
| 1017 | * 0 otherwise (or on error) |
| 1018 | */ |
| 1019 | int fit_image_check_os(const void *fit, int noffset, uint8_t os) |
| 1020 | { |
| 1021 | uint8_t image_os; |
| 1022 | |
| 1023 | if (fit_image_get_os(fit, noffset, &image_os)) |
| 1024 | return 0; |
| 1025 | return (os == image_os); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * fit_image_check_arch - check whether image node is of a given arch |
| 1030 | * @fit: pointer to the FIT format image header |
| 1031 | * @noffset: component image node offset |
| 1032 | * @arch: requested imagearch |
| 1033 | * |
| 1034 | * fit_image_check_arch() reads image arch property and compares its numeric |
| 1035 | * id with the requested arch. Comparison result is returned to the caller. |
| 1036 | * |
| 1037 | * returns: |
| 1038 | * 1 if image is of given arch |
| 1039 | * 0 otherwise (or on error) |
| 1040 | */ |
| 1041 | int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) |
| 1042 | { |
| 1043 | uint8_t image_arch; |
| 1044 | |
| 1045 | if (fit_image_get_arch(fit, noffset, &image_arch)) |
| 1046 | return 0; |
| 1047 | return (arch == image_arch); |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * fit_image_check_type - check whether image node is of a given type |
| 1052 | * @fit: pointer to the FIT format image header |
| 1053 | * @noffset: component image node offset |
| 1054 | * @type: requested image type |
| 1055 | * |
| 1056 | * fit_image_check_type() reads image type property and compares its numeric |
| 1057 | * id with the requested type. Comparison result is returned to the caller. |
| 1058 | * |
| 1059 | * returns: |
| 1060 | * 1 if image is of given type |
| 1061 | * 0 otherwise (or on error) |
| 1062 | */ |
| 1063 | int fit_image_check_type(const void *fit, int noffset, uint8_t type) |
| 1064 | { |
| 1065 | uint8_t image_type; |
| 1066 | |
| 1067 | if (fit_image_get_type(fit, noffset, &image_type)) |
| 1068 | return 0; |
| 1069 | return (type == image_type); |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * fit_image_check_comp - check whether image node uses given compression |
| 1074 | * @fit: pointer to the FIT format image header |
| 1075 | * @noffset: component image node offset |
| 1076 | * @comp: requested image compression type |
| 1077 | * |
| 1078 | * fit_image_check_comp() reads image compression property and compares its |
| 1079 | * numeric id with the requested compression type. Comparison result is |
| 1080 | * returned to the caller. |
| 1081 | * |
| 1082 | * returns: |
| 1083 | * 1 if image uses requested compression |
| 1084 | * 0 otherwise (or on error) |
| 1085 | */ |
| 1086 | int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) |
| 1087 | { |
| 1088 | uint8_t image_comp; |
| 1089 | |
| 1090 | if (fit_image_get_comp(fit, noffset, &image_comp)) |
| 1091 | return 0; |
| 1092 | return (comp == image_comp); |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * fit_check_format - sanity check FIT image format |
| 1097 | * @fit: pointer to the FIT format image header |
| 1098 | * |
| 1099 | * fit_check_format() runs a basic sanity FIT image verification. |
| 1100 | * Routine checks for mandatory properties, nodes, etc. |
| 1101 | * |
| 1102 | * returns: |
| 1103 | * 1, on success |
| 1104 | * 0, on failure |
| 1105 | */ |
| 1106 | int fit_check_format(const void *fit) |
| 1107 | { |
| 1108 | /* mandatory / node 'description' property */ |
| 1109 | if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { |
| 1110 | debug("Wrong FIT format: no description\n"); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | if (IMAGE_ENABLE_TIMESTAMP) { |
| 1115 | /* mandatory / node 'timestamp' property */ |
| 1116 | if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { |
| 1117 | debug("Wrong FIT format: no timestamp\n"); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* mandatory subimages parent '/images' node */ |
| 1123 | if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { |
| 1124 | debug("Wrong FIT format: no images parent node\n"); |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | return 1; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | /** |
| 1133 | * fit_conf_find_compat |
| 1134 | * @fit: pointer to the FIT format image header |
| 1135 | * @fdt: pointer to the device tree to compare against |
| 1136 | * |
| 1137 | * fit_conf_find_compat() attempts to find the configuration whose fdt is the |
| 1138 | * most compatible with the passed in device tree. |
| 1139 | * |
| 1140 | * Example: |
| 1141 | * |
| 1142 | * / o image-tree |
| 1143 | * |-o images |
| 1144 | * | |-o fdt@1 |
| 1145 | * | |-o fdt@2 |
| 1146 | * | |
| 1147 | * |-o configurations |
| 1148 | * |-o config@1 |
| 1149 | * | |-fdt = fdt@1 |
| 1150 | * | |
| 1151 | * |-o config@2 |
| 1152 | * |-fdt = fdt@2 |
| 1153 | * |
| 1154 | * / o U-Boot fdt |
| 1155 | * |-compatible = "foo,bar", "bim,bam" |
| 1156 | * |
| 1157 | * / o kernel fdt1 |
| 1158 | * |-compatible = "foo,bar", |
| 1159 | * |
| 1160 | * / o kernel fdt2 |
| 1161 | * |-compatible = "bim,bam", "baz,biz" |
| 1162 | * |
| 1163 | * Configuration 1 would be picked because the first string in U-Boot's |
| 1164 | * compatible list, "foo,bar", matches a compatible string in the root of fdt1. |
| 1165 | * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. |
| 1166 | * |
| 1167 | * returns: |
| 1168 | * offset to the configuration to use if one was found |
| 1169 | * -1 otherwise |
| 1170 | */ |
| 1171 | int fit_conf_find_compat(const void *fit, const void *fdt) |
| 1172 | { |
| 1173 | int ndepth = 0; |
| 1174 | int noffset, confs_noffset, images_noffset; |
| 1175 | const void *fdt_compat; |
| 1176 | int fdt_compat_len; |
| 1177 | int best_match_offset = 0; |
| 1178 | int best_match_pos = 0; |
| 1179 | |
| 1180 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1181 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1182 | if (confs_noffset < 0 || images_noffset < 0) { |
| 1183 | debug("Can't find configurations or images nodes.\n"); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | |
| 1187 | fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); |
| 1188 | if (!fdt_compat) { |
| 1189 | debug("Fdt for comparison has no \"compatible\" property.\n"); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * Loop over the configurations in the FIT image. |
| 1195 | */ |
| 1196 | for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); |
| 1197 | (noffset >= 0) && (ndepth > 0); |
| 1198 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 1199 | const void *kfdt; |
| 1200 | const char *kfdt_name; |
| 1201 | int kfdt_noffset; |
| 1202 | const char *cur_fdt_compat; |
| 1203 | int len; |
| 1204 | size_t size; |
| 1205 | int i; |
| 1206 | |
| 1207 | if (ndepth > 1) |
| 1208 | continue; |
| 1209 | |
| 1210 | kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); |
| 1211 | if (!kfdt_name) { |
| 1212 | debug("No fdt property found.\n"); |
| 1213 | continue; |
| 1214 | } |
| 1215 | kfdt_noffset = fdt_subnode_offset(fit, images_noffset, |
| 1216 | kfdt_name); |
| 1217 | if (kfdt_noffset < 0) { |
| 1218 | debug("No image node named \"%s\" found.\n", |
| 1219 | kfdt_name); |
| 1220 | continue; |
| 1221 | } |
| 1222 | /* |
| 1223 | * Get a pointer to this configuration's fdt. |
| 1224 | */ |
| 1225 | if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { |
| 1226 | debug("Failed to get fdt \"%s\".\n", kfdt_name); |
| 1227 | continue; |
| 1228 | } |
| 1229 | |
| 1230 | len = fdt_compat_len; |
| 1231 | cur_fdt_compat = fdt_compat; |
| 1232 | /* |
| 1233 | * Look for a match for each U-Boot compatibility string in |
| 1234 | * turn in this configuration's fdt. |
| 1235 | */ |
| 1236 | for (i = 0; len > 0 && |
| 1237 | (!best_match_offset || best_match_pos > i); i++) { |
| 1238 | int cur_len = strlen(cur_fdt_compat) + 1; |
| 1239 | |
| 1240 | if (!fdt_node_check_compatible(kfdt, 0, |
| 1241 | cur_fdt_compat)) { |
| 1242 | best_match_offset = noffset; |
| 1243 | best_match_pos = i; |
| 1244 | break; |
| 1245 | } |
| 1246 | len -= cur_len; |
| 1247 | cur_fdt_compat += cur_len; |
| 1248 | } |
| 1249 | } |
| 1250 | if (!best_match_offset) { |
| 1251 | debug("No match found.\n"); |
| 1252 | return -1; |
| 1253 | } |
| 1254 | |
| 1255 | return best_match_offset; |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * fit_conf_get_node - get node offset for configuration of a given unit name |
| 1260 | * @fit: pointer to the FIT format image header |
| 1261 | * @conf_uname: configuration node unit name |
| 1262 | * |
| 1263 | * fit_conf_get_node() finds a configuration (withing the '/configurations' |
| 1264 | * parant node) of a provided unit name. If configuration is found its node |
| 1265 | * offset is returned to the caller. |
| 1266 | * |
| 1267 | * When NULL is provided in second argument fit_conf_get_node() will search |
| 1268 | * for a default configuration node instead. Default configuration node unit |
| 1269 | * name is retrived from FIT_DEFAULT_PROP property of the '/configurations' |
| 1270 | * node. |
| 1271 | * |
| 1272 | * returns: |
| 1273 | * configuration node offset when found (>=0) |
| 1274 | * negative number on failure (FDT_ERR_* code) |
| 1275 | */ |
| 1276 | int fit_conf_get_node(const void *fit, const char *conf_uname) |
| 1277 | { |
| 1278 | int noffset, confs_noffset; |
| 1279 | int len; |
| 1280 | |
| 1281 | confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); |
| 1282 | if (confs_noffset < 0) { |
| 1283 | debug("Can't find configurations parent node '%s' (%s)\n", |
| 1284 | FIT_CONFS_PATH, fdt_strerror(confs_noffset)); |
| 1285 | return confs_noffset; |
| 1286 | } |
| 1287 | |
| 1288 | if (conf_uname == NULL) { |
| 1289 | /* get configuration unit name from the default property */ |
| 1290 | debug("No configuration specified, trying default...\n"); |
| 1291 | conf_uname = (char *)fdt_getprop(fit, confs_noffset, |
| 1292 | FIT_DEFAULT_PROP, &len); |
| 1293 | if (conf_uname == NULL) { |
| 1294 | fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, |
| 1295 | len); |
| 1296 | return len; |
| 1297 | } |
| 1298 | debug("Found default configuration: '%s'\n", conf_uname); |
| 1299 | } |
| 1300 | |
| 1301 | noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); |
| 1302 | if (noffset < 0) { |
| 1303 | debug("Can't get node offset for configuration unit name: '%s' (%s)\n", |
| 1304 | conf_uname, fdt_strerror(noffset)); |
| 1305 | } |
| 1306 | |
| 1307 | return noffset; |
| 1308 | } |
| 1309 | |
| 1310 | static int __fit_conf_get_prop_node(const void *fit, int noffset, |
| 1311 | const char *prop_name) |
| 1312 | { |
| 1313 | char *uname; |
| 1314 | int len; |
| 1315 | |
| 1316 | /* get kernel image unit name from configuration kernel property */ |
| 1317 | uname = (char *)fdt_getprop(fit, noffset, prop_name, &len); |
| 1318 | if (uname == NULL) |
| 1319 | return len; |
| 1320 | |
| 1321 | return fit_image_get_node(fit, uname); |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * fit_conf_get_kernel_node - get kernel image node offset that corresponds to |
| 1326 | * a given configuration |
| 1327 | * @fit: pointer to the FIT format image header |
| 1328 | * @noffset: configuration node offset |
| 1329 | * |
| 1330 | * fit_conf_get_kernel_node() retrives kernel image node unit name from |
| 1331 | * configuration FIT_KERNEL_PROP property and translates it to the node |
| 1332 | * offset. |
| 1333 | * |
| 1334 | * returns: |
| 1335 | * image node offset when found (>=0) |
| 1336 | * negative number on failure (FDT_ERR_* code) |
| 1337 | */ |
| 1338 | int fit_conf_get_kernel_node(const void *fit, int noffset) |
| 1339 | { |
| 1340 | return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP); |
| 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to |
| 1345 | * a given configuration |
| 1346 | * @fit: pointer to the FIT format image header |
| 1347 | * @noffset: configuration node offset |
| 1348 | * |
| 1349 | * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from |
| 1350 | * configuration FIT_KERNEL_PROP property and translates it to the node |
| 1351 | * offset. |
| 1352 | * |
| 1353 | * returns: |
| 1354 | * image node offset when found (>=0) |
| 1355 | * negative number on failure (FDT_ERR_* code) |
| 1356 | */ |
| 1357 | int fit_conf_get_ramdisk_node(const void *fit, int noffset) |
| 1358 | { |
| 1359 | return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP); |
| 1360 | } |
| 1361 | |
| 1362 | /** |
| 1363 | * fit_conf_get_fdt_node - get fdt image node offset that corresponds to |
| 1364 | * a given configuration |
| 1365 | * @fit: pointer to the FIT format image header |
| 1366 | * @noffset: configuration node offset |
| 1367 | * |
| 1368 | * fit_conf_get_fdt_node() retrives fdt image node unit name from |
| 1369 | * configuration FIT_KERNEL_PROP property and translates it to the node |
| 1370 | * offset. |
| 1371 | * |
| 1372 | * returns: |
| 1373 | * image node offset when found (>=0) |
| 1374 | * negative number on failure (FDT_ERR_* code) |
| 1375 | */ |
| 1376 | int fit_conf_get_fdt_node(const void *fit, int noffset) |
| 1377 | { |
| 1378 | return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP); |
| 1379 | } |
| 1380 | |
| 1381 | /** |
| 1382 | * fit_conf_print - prints out the FIT configuration details |
| 1383 | * @fit: pointer to the FIT format image header |
| 1384 | * @noffset: offset of the configuration node |
| 1385 | * @p: pointer to prefix string |
| 1386 | * |
| 1387 | * fit_conf_print() lists all mandatory properies for the processed |
| 1388 | * configuration node. |
| 1389 | * |
| 1390 | * returns: |
| 1391 | * no returned results |
| 1392 | */ |
| 1393 | void fit_conf_print(const void *fit, int noffset, const char *p) |
| 1394 | { |
| 1395 | char *desc; |
| 1396 | char *uname; |
| 1397 | int ret; |
| 1398 | |
| 1399 | /* Mandatory properties */ |
| 1400 | ret = fit_get_desc(fit, noffset, &desc); |
| 1401 | printf("%s Description: ", p); |
| 1402 | if (ret) |
| 1403 | printf("unavailable\n"); |
| 1404 | else |
| 1405 | printf("%s\n", desc); |
| 1406 | |
| 1407 | uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); |
| 1408 | printf("%s Kernel: ", p); |
| 1409 | if (uname == NULL) |
| 1410 | printf("unavailable\n"); |
| 1411 | else |
| 1412 | printf("%s\n", uname); |
| 1413 | |
| 1414 | /* Optional properties */ |
| 1415 | uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); |
| 1416 | if (uname) |
| 1417 | printf("%s Init Ramdisk: %s\n", p, uname); |
| 1418 | |
| 1419 | uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL); |
| 1420 | if (uname) |
| 1421 | printf("%s FDT: %s\n", p, uname); |
| 1422 | } |
| 1423 | |
| 1424 | /** |
| 1425 | * fit_check_ramdisk - verify FIT format ramdisk subimage |
| 1426 | * @fit_hdr: pointer to the FIT ramdisk header |
| 1427 | * @rd_noffset: ramdisk subimage node offset within FIT image |
| 1428 | * @arch: requested ramdisk image architecture type |
| 1429 | * @verify: data CRC verification flag |
| 1430 | * |
| 1431 | * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from |
| 1432 | * specified FIT image. |
| 1433 | * |
| 1434 | * returns: |
| 1435 | * 1, on success |
| 1436 | * 0, on failure |
| 1437 | */ |
| 1438 | #ifndef USE_HOSTCC |
| 1439 | int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch, |
| 1440 | int verify) |
| 1441 | { |
| 1442 | fit_image_print(fit, rd_noffset, " "); |
| 1443 | |
| 1444 | if (verify) { |
| 1445 | puts(" Verifying Hash Integrity ... "); |
Simon Glass | b8da836 | 2013-05-07 06:11:57 +0000 | [diff] [blame^] | 1446 | if (!fit_image_verify(fit, rd_noffset)) { |
Simon Glass | 53fbb7e | 2013-05-07 06:11:53 +0000 | [diff] [blame] | 1447 | puts("Bad Data Hash\n"); |
| 1448 | bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH); |
| 1449 | return 0; |
| 1450 | } |
| 1451 | puts("OK\n"); |
| 1452 | } |
| 1453 | |
| 1454 | bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL); |
| 1455 | if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) || |
| 1456 | !fit_image_check_arch(fit, rd_noffset, arch) || |
| 1457 | !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) { |
| 1458 | printf("No Linux %s Ramdisk Image\n", |
| 1459 | genimg_get_arch_name(arch)); |
| 1460 | bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK); |
| 1465 | return 1; |
| 1466 | } |
| 1467 | #endif /* USE_HOSTCC */ |