Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Marvell International Ltd. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0 |
| 5 | * https://spdx.org/licenses |
| 6 | */ |
| 7 | |
| 8 | #include <config.h> |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <vsprintf.h> |
| 12 | #include <errno.h> |
| 13 | #include <dm.h> |
| 14 | |
| 15 | #include <spi_flash.h> |
| 16 | #include <spi.h> |
| 17 | #include <nand.h> |
| 18 | #include <usb.h> |
| 19 | #include <fs.h> |
| 20 | #include <mmc.h> |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 21 | #ifdef CONFIG_BLK |
| 22 | #include <blk.h> |
| 23 | #endif |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 24 | #include <u-boot/sha1.h> |
| 25 | #include <u-boot/sha256.h> |
| 26 | |
| 27 | #ifndef CONFIG_SYS_MMC_ENV_DEV |
| 28 | #define CONFIG_SYS_MMC_ENV_DEV 0 |
| 29 | #endif |
| 30 | |
| 31 | #if defined(CONFIG_ARMADA_8K) |
| 32 | #define MAIN_HDR_MAGIC 0xB105B002 |
| 33 | |
| 34 | struct mvebu_image_header { |
| 35 | u32 magic; /* 0-3 */ |
| 36 | u32 prolog_size; /* 4-7 */ |
| 37 | u32 prolog_checksum; /* 8-11 */ |
| 38 | u32 boot_image_size; /* 12-15 */ |
| 39 | u32 boot_image_checksum; /* 16-19 */ |
| 40 | u32 rsrvd0; /* 20-23 */ |
| 41 | u32 load_addr; /* 24-27 */ |
| 42 | u32 exec_addr; /* 28-31 */ |
| 43 | u8 uart_cfg; /* 32 */ |
| 44 | u8 baudrate; /* 33 */ |
| 45 | u8 ext_count; /* 34 */ |
| 46 | u8 aux_flags; /* 35 */ |
| 47 | u32 io_arg_0; /* 36-39 */ |
| 48 | u32 io_arg_1; /* 40-43 */ |
| 49 | u32 io_arg_2; /* 43-47 */ |
| 50 | u32 io_arg_3; /* 48-51 */ |
| 51 | u32 rsrvd1; /* 52-55 */ |
| 52 | u32 rsrvd2; /* 56-59 */ |
| 53 | u32 rsrvd3; /* 60-63 */ |
| 54 | }; |
| 55 | #elif defined(CONFIG_ARMADA_3700) /* A3700 */ |
| 56 | #define HASH_SUM_LEN 16 |
| 57 | #define IMAGE_VERSION_3_6_0 0x030600 |
| 58 | #define IMAGE_VERSION_3_5_0 0x030500 |
| 59 | |
| 60 | struct common_tim_data { |
| 61 | u32 version; |
| 62 | u32 identifier; |
| 63 | u32 trusted; |
| 64 | u32 issue_date; |
| 65 | u32 oem_unique_id; |
| 66 | u32 reserved[5]; /* Reserve 20 bytes */ |
| 67 | u32 boot_flash_sign; |
| 68 | u32 num_images; |
| 69 | u32 num_keys; |
| 70 | u32 size_of_reserved; |
| 71 | }; |
| 72 | |
| 73 | struct mvebu_image_info { |
| 74 | u32 image_id; |
| 75 | u32 next_image_id; |
| 76 | u32 flash_entry_addr; |
| 77 | u32 load_addr; |
| 78 | u32 image_size; |
| 79 | u32 image_size_to_hash; |
| 80 | u32 hash_algorithm_id; |
| 81 | u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */ |
| 82 | u32 partition_number; |
| 83 | u32 enc_algorithm_id; |
| 84 | u32 encrypt_start_offset; |
| 85 | u32 encrypt_size; |
| 86 | }; |
| 87 | #endif /* CONFIG_ARMADA_XXX */ |
| 88 | |
| 89 | struct bubt_dev { |
| 90 | char name[8]; |
| 91 | size_t (*read)(const char *file_name); |
| 92 | int (*write)(size_t image_size); |
| 93 | int (*active)(void); |
| 94 | }; |
| 95 | |
| 96 | static ulong get_load_addr(void) |
| 97 | { |
| 98 | const char *addr_str; |
| 99 | unsigned long addr; |
| 100 | |
| 101 | addr_str = getenv("loadaddr"); |
| 102 | if (addr_str) |
| 103 | addr = simple_strtoul(addr_str, NULL, 16); |
| 104 | else |
| 105 | addr = CONFIG_SYS_LOAD_ADDR; |
| 106 | |
| 107 | return addr; |
| 108 | } |
| 109 | |
| 110 | /******************************************************************** |
| 111 | * eMMC services |
| 112 | ********************************************************************/ |
| 113 | #ifdef CONFIG_DM_MMC |
| 114 | static int mmc_burn_image(size_t image_size) |
| 115 | { |
| 116 | struct mmc *mmc; |
| 117 | lbaint_t start_lba; |
| 118 | lbaint_t blk_count; |
| 119 | ulong blk_written; |
| 120 | int err; |
| 121 | const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV; |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 122 | #ifdef CONFIG_BLK |
| 123 | struct blk_desc *blk_desc; |
| 124 | #endif |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 125 | mmc = find_mmc_device(mmc_dev_num); |
| 126 | if (!mmc) { |
| 127 | printf("No SD/MMC/eMMC card found\n"); |
| 128 | return -ENOMEDIUM; |
| 129 | } |
| 130 | |
| 131 | err = mmc_init(mmc); |
| 132 | if (err) { |
| 133 | printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC", |
| 134 | mmc_dev_num); |
| 135 | return err; |
| 136 | } |
| 137 | |
| 138 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 139 | if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) { |
| 140 | err = mmc_switch_part(mmc_dev_num, CONFIG_SYS_MMC_ENV_PART); |
| 141 | if (err) { |
| 142 | printf("MMC partition switch failed\n"); |
| 143 | return err; |
| 144 | } |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | /* SD reserves LBA-0 for MBR and boots from LBA-1, |
| 149 | * MMC/eMMC boots from LBA-0 |
| 150 | */ |
| 151 | start_lba = IS_SD(mmc) ? 1 : 0; |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 152 | #ifdef CONFIG_BLK |
| 153 | blk_count = image_size / mmc->write_bl_len; |
| 154 | if (image_size % mmc->write_bl_len) |
| 155 | blk_count += 1; |
| 156 | |
| 157 | blk_desc = mmc_get_blk_desc(mmc); |
| 158 | if (!blk_desc) { |
| 159 | printf("Error - failed to obtain block descriptor\n"); |
| 160 | return -ENODEV; |
| 161 | } |
| 162 | blk_written = blk_dwrite(blk_desc, start_lba, blk_count, |
| 163 | (void *)get_load_addr()); |
| 164 | #else |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 165 | blk_count = image_size / mmc->block_dev.blksz; |
| 166 | if (image_size % mmc->block_dev.blksz) |
| 167 | blk_count += 1; |
| 168 | |
| 169 | blk_written = mmc->block_dev.block_write(mmc_dev_num, |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 170 | start_lba, blk_count, |
| 171 | (void *)get_load_addr()); |
| 172 | #endif /* CONFIG_BLK */ |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 173 | if (blk_written != blk_count) { |
| 174 | printf("Error - written %#lx blocks\n", blk_written); |
| 175 | return -ENOSPC; |
| 176 | } |
| 177 | printf("Done!\n"); |
| 178 | |
| 179 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 180 | if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) |
| 181 | mmc_switch_part(mmc_dev_num, mmc->part_num); |
| 182 | #endif |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static size_t mmc_read_file(const char *file_name) |
| 188 | { |
| 189 | loff_t act_read = 0; |
| 190 | int rc; |
| 191 | struct mmc *mmc; |
| 192 | const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV; |
| 193 | |
| 194 | mmc = find_mmc_device(mmc_dev_num); |
| 195 | if (!mmc) { |
| 196 | printf("No SD/MMC/eMMC card found\n"); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | if (mmc_init(mmc)) { |
| 201 | printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC", |
| 202 | mmc_dev_num); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* Load from data partition (0) */ |
| 207 | if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) { |
| 208 | printf("Error: MMC 0 not found\n"); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* Perfrom file read */ |
| 213 | rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read); |
| 214 | if (rc) |
| 215 | return 0; |
| 216 | |
| 217 | return act_read; |
| 218 | } |
| 219 | |
| 220 | static int is_mmc_active(void) |
| 221 | { |
| 222 | return 1; |
| 223 | } |
| 224 | #else /* CONFIG_DM_MMC */ |
| 225 | static int mmc_burn_image(size_t image_size) |
| 226 | { |
| 227 | return -ENODEV; |
| 228 | } |
| 229 | |
| 230 | static size_t mmc_read_file(const char *file_name) |
| 231 | { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int is_mmc_active(void) |
| 236 | { |
| 237 | return 0; |
| 238 | } |
| 239 | #endif /* CONFIG_DM_MMC */ |
| 240 | |
| 241 | /******************************************************************** |
| 242 | * SPI services |
| 243 | ********************************************************************/ |
| 244 | #ifdef CONFIG_SPI_FLASH |
| 245 | static int spi_burn_image(size_t image_size) |
| 246 | { |
| 247 | int ret; |
| 248 | struct spi_flash *flash; |
| 249 | u32 erase_bytes; |
| 250 | |
| 251 | /* Probe the SPI bus to get the flash device */ |
| 252 | flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 253 | CONFIG_ENV_SPI_CS, |
| 254 | CONFIG_SF_DEFAULT_SPEED, |
| 255 | CONFIG_SF_DEFAULT_MODE); |
| 256 | if (!flash) { |
| 257 | printf("Failed to probe SPI Flash\n"); |
| 258 | return -ENOMEDIUM; |
| 259 | } |
| 260 | |
| 261 | #ifdef CONFIG_SPI_FLASH_PROTECTION |
| 262 | spi_flash_protect(flash, 0); |
| 263 | #endif |
| 264 | erase_bytes = image_size + |
| 265 | (flash->erase_size - image_size % flash->erase_size); |
| 266 | printf("Erasing %d bytes (%d blocks) at offset 0 ...", |
| 267 | erase_bytes, erase_bytes / flash->erase_size); |
| 268 | ret = spi_flash_erase(flash, 0, erase_bytes); |
| 269 | if (ret) |
| 270 | printf("Error!\n"); |
| 271 | else |
| 272 | printf("Done!\n"); |
| 273 | |
| 274 | printf("Writing %d bytes from 0x%lx to offset 0 ...", |
| 275 | (int)image_size, get_load_addr()); |
| 276 | ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr()); |
| 277 | if (ret) |
| 278 | printf("Error!\n"); |
| 279 | else |
| 280 | printf("Done!\n"); |
| 281 | |
| 282 | #ifdef CONFIG_SPI_FLASH_PROTECTION |
| 283 | spi_flash_protect(flash, 1); |
| 284 | #endif |
| 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static int is_spi_active(void) |
| 290 | { |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | #else /* CONFIG_SPI_FLASH */ |
| 295 | static int spi_burn_image(size_t image_size) |
| 296 | { |
| 297 | return -ENODEV; |
| 298 | } |
| 299 | |
| 300 | static int is_spi_active(void) |
| 301 | { |
| 302 | return 0; |
| 303 | } |
| 304 | #endif /* CONFIG_SPI_FLASH */ |
| 305 | |
| 306 | /******************************************************************** |
| 307 | * NAND services |
| 308 | ********************************************************************/ |
| 309 | #ifdef CONFIG_CMD_NAND |
| 310 | static int nand_burn_image(size_t image_size) |
| 311 | { |
Konstantin Porotchkin | c6db27b | 2017-03-28 18:16:56 +0300 | [diff] [blame] | 312 | int ret; |
| 313 | uint32_t block_size; |
| 314 | struct mtd_info *nand; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 315 | int dev = nand_curr_device; |
| 316 | |
| 317 | if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) || |
Konstantin Porotchkin | c6db27b | 2017-03-28 18:16:56 +0300 | [diff] [blame] | 318 | (!nand_info[dev]->name)) { |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 319 | puts("\nno devices available\n"); |
| 320 | return -ENOMEDIUM; |
| 321 | } |
Konstantin Porotchkin | c6db27b | 2017-03-28 18:16:56 +0300 | [diff] [blame] | 322 | nand = nand_info[dev]; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 323 | block_size = nand->erasesize; |
| 324 | |
| 325 | /* Align U-Boot size to currently used blocksize */ |
| 326 | image_size = ((image_size + (block_size - 1)) & (~(block_size - 1))); |
| 327 | |
| 328 | /* Erase the U-BOOT image space */ |
| 329 | printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size); |
| 330 | ret = nand_erase(nand, 0, image_size); |
| 331 | if (ret) { |
| 332 | printf("Error!\n"); |
| 333 | goto error; |
| 334 | } |
| 335 | printf("Done!\n"); |
| 336 | |
| 337 | /* Write the image to flash */ |
Konstantin Porotchkin | c6db27b | 2017-03-28 18:16:56 +0300 | [diff] [blame] | 338 | printf("Writing %d bytes from 0x%lx to offset 0 ... ", |
| 339 | (int)image_size, get_load_addr()); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 340 | ret = nand_write(nand, 0, &image_size, (void *)get_load_addr()); |
| 341 | if (ret) |
| 342 | printf("Error!\n"); |
| 343 | else |
| 344 | printf("Done!\n"); |
| 345 | |
| 346 | error: |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static int is_nand_active(void) |
| 351 | { |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | #else /* CONFIG_CMD_NAND */ |
| 356 | static int nand_burn_image(size_t image_size) |
| 357 | { |
| 358 | return -ENODEV; |
| 359 | } |
| 360 | |
| 361 | static int is_nand_active(void) |
| 362 | { |
| 363 | return 0; |
| 364 | } |
| 365 | #endif /* CONFIG_CMD_NAND */ |
| 366 | |
| 367 | /******************************************************************** |
| 368 | * USB services |
| 369 | ********************************************************************/ |
| 370 | #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK) |
| 371 | static size_t usb_read_file(const char *file_name) |
| 372 | { |
| 373 | loff_t act_read = 0; |
| 374 | struct udevice *dev; |
| 375 | int rc; |
| 376 | |
| 377 | usb_stop(); |
| 378 | |
| 379 | if (usb_init() < 0) { |
| 380 | printf("Error: usb_init failed\n"); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* Try to recognize storage devices immediately */ |
| 385 | blk_first_device(IF_TYPE_USB, &dev); |
| 386 | if (!dev) { |
| 387 | printf("Error: USB storage device not found\n"); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* Always load from usb 0 */ |
| 392 | if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) { |
| 393 | printf("Error: USB 0 not found\n"); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* Perfrom file read */ |
| 398 | rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read); |
| 399 | if (rc) |
| 400 | return 0; |
| 401 | |
| 402 | return act_read; |
| 403 | } |
| 404 | |
| 405 | static int is_usb_active(void) |
| 406 | { |
| 407 | return 1; |
| 408 | } |
| 409 | |
| 410 | #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */ |
| 411 | static size_t usb_read_file(const char *file_name) |
| 412 | { |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int is_usb_active(void) |
| 417 | { |
| 418 | return 0; |
| 419 | } |
| 420 | #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */ |
| 421 | |
| 422 | /******************************************************************** |
| 423 | * Network services |
| 424 | ********************************************************************/ |
| 425 | #ifdef CONFIG_CMD_NET |
| 426 | static size_t tftp_read_file(const char *file_name) |
| 427 | { |
| 428 | /* update global variable load_addr before tftp file from network */ |
| 429 | load_addr = get_load_addr(); |
| 430 | return net_loop(TFTPGET); |
| 431 | } |
| 432 | |
| 433 | static int is_tftp_active(void) |
| 434 | { |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | #else |
| 439 | static size_t tftp_read_file(const char *file_name) |
| 440 | { |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int is_tftp_active(void) |
| 445 | { |
| 446 | return 0; |
| 447 | } |
| 448 | #endif /* CONFIG_CMD_NET */ |
| 449 | |
| 450 | enum bubt_devices { |
| 451 | BUBT_DEV_NET = 0, |
| 452 | BUBT_DEV_USB, |
| 453 | BUBT_DEV_MMC, |
| 454 | BUBT_DEV_SPI, |
| 455 | BUBT_DEV_NAND, |
| 456 | |
| 457 | BUBT_MAX_DEV |
| 458 | }; |
| 459 | |
| 460 | struct bubt_dev bubt_devs[BUBT_MAX_DEV] = { |
| 461 | {"tftp", tftp_read_file, NULL, is_tftp_active}, |
| 462 | {"usb", usb_read_file, NULL, is_usb_active}, |
| 463 | {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active}, |
| 464 | {"spi", NULL, spi_burn_image, is_spi_active}, |
| 465 | {"nand", NULL, nand_burn_image, is_nand_active}, |
| 466 | }; |
| 467 | |
| 468 | static int bubt_write_file(struct bubt_dev *dst, size_t image_size) |
| 469 | { |
| 470 | if (!dst->write) { |
| 471 | printf("Error: Write not supported on device %s\n", dst->name); |
| 472 | return -ENOTSUPP; |
| 473 | } |
| 474 | |
| 475 | return dst->write(image_size); |
| 476 | } |
| 477 | |
| 478 | #if defined(CONFIG_ARMADA_8K) |
| 479 | u32 do_checksum32(u32 *start, int32_t len) |
| 480 | { |
| 481 | u32 sum = 0; |
| 482 | u32 *startp = start; |
| 483 | |
| 484 | do { |
| 485 | sum += *startp; |
| 486 | startp++; |
| 487 | len -= 4; |
| 488 | } while (len > 0); |
| 489 | |
| 490 | return sum; |
| 491 | } |
| 492 | |
| 493 | static int check_image_header(void) |
| 494 | { |
| 495 | struct mvebu_image_header *hdr = |
| 496 | (struct mvebu_image_header *)get_load_addr(); |
| 497 | u32 header_len = hdr->prolog_size; |
| 498 | u32 checksum; |
| 499 | u32 checksum_ref = hdr->prolog_checksum; |
| 500 | |
| 501 | /* |
| 502 | * For now compare checksum, and magic. Later we can |
| 503 | * verify more stuff on the header like interface type, etc |
| 504 | */ |
| 505 | if (hdr->magic != MAIN_HDR_MAGIC) { |
| 506 | printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n", |
| 507 | hdr->magic, MAIN_HDR_MAGIC); |
| 508 | return -ENOEXEC; |
| 509 | } |
| 510 | |
| 511 | /* The checksum value is discarded from checksum calculation */ |
| 512 | hdr->prolog_checksum = 0; |
| 513 | |
| 514 | checksum = do_checksum32((u32 *)hdr, header_len); |
| 515 | if (checksum != checksum_ref) { |
| 516 | printf("Error: Bad Image checksum. 0x%x != 0x%x\n", |
| 517 | checksum, checksum_ref); |
| 518 | return -ENOEXEC; |
| 519 | } |
| 520 | |
| 521 | /* Restore the checksum before writing */ |
| 522 | hdr->prolog_checksum = checksum_ref; |
| 523 | printf("Image checksum...OK!\n"); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */ |
| 528 | static int check_image_header(void) |
| 529 | { |
| 530 | struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr(); |
| 531 | int image_num; |
| 532 | u8 hash_160_output[SHA1_SUM_LEN]; |
| 533 | u8 hash_256_output[SHA256_SUM_LEN]; |
| 534 | sha1_context hash1_text; |
| 535 | sha256_context hash256_text; |
| 536 | u8 *hash_output; |
| 537 | u32 hash_algorithm_id; |
| 538 | u32 image_size_to_hash; |
| 539 | u32 flash_entry_addr; |
| 540 | u32 *hash_value; |
| 541 | u32 internal_hash[HASH_SUM_LEN]; |
| 542 | const u8 *buff; |
| 543 | u32 num_of_image = hdr->num_images; |
| 544 | u32 version = hdr->version; |
| 545 | u32 trusted = hdr->trusted; |
| 546 | |
| 547 | /* bubt checksum validation only supports nontrusted images */ |
| 548 | if (trusted == 1) { |
| 549 | printf("bypass image validation, "); |
| 550 | printf("only untrusted image is supported now\n"); |
| 551 | return 0; |
| 552 | } |
| 553 | /* only supports image version 3.5 and 3.6 */ |
| 554 | if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) { |
| 555 | printf("Error: Unsupported Image version = 0x%08x\n", version); |
| 556 | return -ENOEXEC; |
| 557 | } |
| 558 | /* validate images hash value */ |
| 559 | for (image_num = 0; image_num < num_of_image; image_num++) { |
| 560 | struct mvebu_image_info *info = |
| 561 | (struct mvebu_image_info *)(get_load_addr() + |
| 562 | sizeof(struct common_tim_data) + |
| 563 | image_num * sizeof(struct mvebu_image_info)); |
| 564 | hash_algorithm_id = info->hash_algorithm_id; |
| 565 | image_size_to_hash = info->image_size_to_hash; |
| 566 | flash_entry_addr = info->flash_entry_addr; |
| 567 | hash_value = info->hash; |
| 568 | buff = (const u8 *)(get_load_addr() + flash_entry_addr); |
| 569 | |
| 570 | if (image_num == 0) { |
| 571 | /* |
| 572 | * The first image includes hash values in its content. |
| 573 | * For hash calculation, we need to save the original |
| 574 | * hash values to a local variable that will be |
| 575 | * copied back for comparsion and set all zeros to |
| 576 | * the orignal hash values for calculating new value. |
| 577 | * First image original format : |
| 578 | * x...x (datum1) x...x(orig. hash values) x...x(datum2) |
| 579 | * Replaced first image format : |
| 580 | * x...x (datum1) 0...0(hash values) x...x(datum2) |
| 581 | */ |
| 582 | memcpy(internal_hash, hash_value, |
| 583 | sizeof(internal_hash)); |
| 584 | memset(hash_value, 0, sizeof(internal_hash)); |
| 585 | } |
| 586 | if (image_size_to_hash == 0) { |
| 587 | printf("Warning: Image_%d hash checksum is disabled, ", |
| 588 | image_num); |
| 589 | printf("skip the image validation.\n"); |
| 590 | continue; |
| 591 | } |
| 592 | switch (hash_algorithm_id) { |
| 593 | case SHA1_SUM_LEN: |
| 594 | sha1_starts(&hash1_text); |
| 595 | sha1_update(&hash1_text, buff, image_size_to_hash); |
| 596 | sha1_finish(&hash1_text, hash_160_output); |
| 597 | hash_output = hash_160_output; |
| 598 | break; |
| 599 | case SHA256_SUM_LEN: |
| 600 | sha256_starts(&hash256_text); |
| 601 | sha256_update(&hash256_text, buff, image_size_to_hash); |
| 602 | sha256_finish(&hash256_text, hash_256_output); |
| 603 | hash_output = hash_256_output; |
| 604 | break; |
| 605 | default: |
| 606 | printf("Error: Unsupported hash_algorithm_id = %d\n", |
| 607 | hash_algorithm_id); |
| 608 | return -ENOEXEC; |
| 609 | } |
| 610 | if (image_num == 0) |
| 611 | memcpy(hash_value, internal_hash, |
| 612 | sizeof(internal_hash)); |
| 613 | if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) { |
| 614 | printf("Error: Image_%d checksum is not correct\n", |
| 615 | image_num); |
| 616 | return -ENOEXEC; |
| 617 | } |
| 618 | } |
| 619 | printf("Image checksum...OK!\n"); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | #else /* Not ARMADA? */ |
| 625 | static int check_image_header(void) |
| 626 | { |
| 627 | printf("bubt cmd does not support this SoC device or family!\n"); |
| 628 | return -ENOEXEC; |
| 629 | } |
| 630 | #endif |
| 631 | |
| 632 | static int bubt_verify(size_t image_size) |
| 633 | { |
| 634 | int err; |
| 635 | |
| 636 | /* Check a correct image header exists */ |
| 637 | err = check_image_header(); |
| 638 | if (err) { |
| 639 | printf("Error: Image header verification failed\n"); |
| 640 | return err; |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static int bubt_read_file(struct bubt_dev *src) |
| 647 | { |
| 648 | size_t image_size; |
| 649 | |
| 650 | if (!src->read) { |
| 651 | printf("Error: Read not supported on device \"%s\"\n", |
| 652 | src->name); |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | image_size = src->read(net_boot_file_name); |
| 657 | if (image_size <= 0) { |
| 658 | printf("Error: Failed to read file %s from %s\n", |
| 659 | net_boot_file_name, src->name); |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | return image_size; |
| 664 | } |
| 665 | |
| 666 | static int bubt_is_dev_active(struct bubt_dev *dev) |
| 667 | { |
| 668 | if (!dev->active) { |
| 669 | printf("Device \"%s\" not supported by U-BOOT image\n", |
| 670 | dev->name); |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | if (!dev->active()) { |
| 675 | printf("Device \"%s\" is inactive\n", dev->name); |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | return 1; |
| 680 | } |
| 681 | |
| 682 | struct bubt_dev *find_bubt_dev(char *dev_name) |
| 683 | { |
| 684 | int dev; |
| 685 | |
| 686 | for (dev = 0; dev < BUBT_MAX_DEV; dev++) { |
| 687 | if (strcmp(bubt_devs[dev].name, dev_name) == 0) |
| 688 | return &bubt_devs[dev]; |
| 689 | } |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | #define DEFAULT_BUBT_SRC "tftp" |
| 695 | |
| 696 | #ifndef DEFAULT_BUBT_DST |
| 697 | #ifdef CONFIG_MVEBU_SPI_BOOT |
| 698 | #define DEFAULT_BUBT_DST "spi" |
| 699 | #elif defined(CONFIG_MVEBU_NAND_BOOT) |
| 700 | #define DEFAULT_BUBT_DST "nand" |
| 701 | #elif defined(CONFIG_MVEBU_MMC_BOOT) |
| 702 | #define DEFAULT_BUBT_DST "mmc" |
| 703 | else |
| 704 | #define DEFAULT_BUBT_DST "error" |
| 705 | #endif |
| 706 | #endif /* DEFAULT_BUBT_DST */ |
| 707 | |
| 708 | int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 709 | { |
| 710 | struct bubt_dev *src, *dst; |
| 711 | size_t image_size; |
| 712 | char src_dev_name[8]; |
| 713 | char dst_dev_name[8]; |
| 714 | char *name; |
| 715 | int err; |
| 716 | |
| 717 | if (argc < 2) |
| 718 | copy_filename(net_boot_file_name, |
| 719 | CONFIG_MVEBU_UBOOT_DFLT_NAME, |
| 720 | sizeof(net_boot_file_name)); |
| 721 | else |
| 722 | copy_filename(net_boot_file_name, argv[1], |
| 723 | sizeof(net_boot_file_name)); |
| 724 | |
| 725 | if (argc >= 3) { |
| 726 | strncpy(dst_dev_name, argv[2], 8); |
| 727 | } else { |
| 728 | name = DEFAULT_BUBT_DST; |
| 729 | strncpy(dst_dev_name, name, 8); |
| 730 | } |
| 731 | |
| 732 | if (argc >= 4) |
| 733 | strncpy(src_dev_name, argv[3], 8); |
| 734 | else |
| 735 | strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8); |
| 736 | |
| 737 | /* Figure out the destination device */ |
| 738 | dst = find_bubt_dev(dst_dev_name); |
| 739 | if (!dst) { |
| 740 | printf("Error: Unknown destination \"%s\"\n", dst_dev_name); |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
| 744 | if (!bubt_is_dev_active(dst)) |
| 745 | return -ENODEV; |
| 746 | |
| 747 | /* Figure out the source device */ |
| 748 | src = find_bubt_dev(src_dev_name); |
| 749 | if (!src) { |
| 750 | printf("Error: Unknown source \"%s\"\n", src_dev_name); |
| 751 | return 1; |
| 752 | } |
| 753 | |
| 754 | if (!bubt_is_dev_active(src)) |
| 755 | return -ENODEV; |
| 756 | |
| 757 | printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n", |
| 758 | net_boot_file_name, src->name, dst->name); |
| 759 | |
| 760 | image_size = bubt_read_file(src); |
| 761 | if (!image_size) |
| 762 | return -EIO; |
| 763 | |
| 764 | err = bubt_verify(image_size); |
| 765 | if (err) |
| 766 | return err; |
| 767 | |
| 768 | err = bubt_write_file(dst, image_size); |
| 769 | if (err) |
| 770 | return err; |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | U_BOOT_CMD( |
| 776 | bubt, 4, 0, do_bubt_cmd, |
| 777 | "Burn a u-boot image to flash", |
| 778 | "[file-name] [destination [source]]\n" |
| 779 | "\t-file-name The image file name to burn. Default = flash-image.bin\n" |
| 780 | "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n" |
| 781 | "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n" |
| 782 | "Examples:\n" |
| 783 | "\tbubt - Burn flash-image.bin from tftp to active boot device\n" |
| 784 | "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n" |
| 785 | "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n" |
| 786 | |
| 787 | ); |