2 * Copyright (C) 2016 Marvell International Ltd.
4 * SPDX-License-Identifier: GPL-2.0
5 * https://spdx.org/licenses
15 #include <spi_flash.h>
24 #include <u-boot/sha1.h>
25 #include <u-boot/sha256.h>
27 #ifndef CONFIG_SYS_MMC_ENV_DEV
28 #define CONFIG_SYS_MMC_ENV_DEV 0
31 #if defined(CONFIG_ARMADA_8K)
32 #define MAIN_HDR_MAGIC 0xB105B002
34 struct mvebu_image_header {
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 */
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 */
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
60 struct common_tim_data {
66 u32 reserved[5]; /* Reserve 20 bytes */
73 struct mvebu_image_info {
79 u32 image_size_to_hash;
80 u32 hash_algorithm_id;
81 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
84 u32 encrypt_start_offset;
87 #endif /* CONFIG_ARMADA_XXX */
91 size_t (*read)(const char *file_name);
92 int (*write)(size_t image_size);
96 static ulong get_load_addr(void)
101 addr_str = env_get("loadaddr");
103 addr = simple_strtoul(addr_str, NULL, 16);
105 addr = CONFIG_SYS_LOAD_ADDR;
110 /********************************************************************
112 ********************************************************************/
114 static int mmc_burn_image(size_t image_size)
121 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
123 struct blk_desc *blk_desc;
125 mmc = find_mmc_device(mmc_dev_num);
127 printf("No SD/MMC/eMMC card found\n");
133 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
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);
142 printf("MMC partition switch failed\n");
148 /* SD reserves LBA-0 for MBR and boots from LBA-1,
149 * MMC/eMMC boots from LBA-0
151 start_lba = IS_SD(mmc) ? 1 : 0;
153 blk_count = image_size / mmc->write_bl_len;
154 if (image_size % mmc->write_bl_len)
157 blk_desc = mmc_get_blk_desc(mmc);
159 printf("Error - failed to obtain block descriptor\n");
162 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
163 (void *)get_load_addr());
165 blk_count = image_size / mmc->block_dev.blksz;
166 if (image_size % mmc->block_dev.blksz)
169 blk_written = mmc->block_dev.block_write(mmc_dev_num,
170 start_lba, blk_count,
171 (void *)get_load_addr());
172 #endif /* CONFIG_BLK */
173 if (blk_written != blk_count) {
174 printf("Error - written %#lx blocks\n", blk_written);
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);
187 static size_t mmc_read_file(const char *file_name)
192 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
194 mmc = find_mmc_device(mmc_dev_num);
196 printf("No SD/MMC/eMMC card found\n");
201 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
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");
212 /* Perfrom file read */
213 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
220 static int is_mmc_active(void)
224 #else /* CONFIG_DM_MMC */
225 static int mmc_burn_image(size_t image_size)
230 static size_t mmc_read_file(const char *file_name)
235 static int is_mmc_active(void)
239 #endif /* CONFIG_DM_MMC */
241 /********************************************************************
243 ********************************************************************/
244 #ifdef CONFIG_SPI_FLASH
245 static int spi_burn_image(size_t image_size)
248 struct spi_flash *flash;
251 /* Probe the SPI bus to get the flash device */
252 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
254 CONFIG_SF_DEFAULT_SPEED,
255 CONFIG_SF_DEFAULT_MODE);
257 printf("Failed to probe SPI Flash\n");
261 #ifdef CONFIG_SPI_FLASH_PROTECTION
262 spi_flash_protect(flash, 0);
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);
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());
282 #ifdef CONFIG_SPI_FLASH_PROTECTION
283 spi_flash_protect(flash, 1);
289 static int is_spi_active(void)
294 #else /* CONFIG_SPI_FLASH */
295 static int spi_burn_image(size_t image_size)
300 static int is_spi_active(void)
304 #endif /* CONFIG_SPI_FLASH */
306 /********************************************************************
308 ********************************************************************/
309 #ifdef CONFIG_CMD_NAND
310 static int nand_burn_image(size_t image_size)
314 struct mtd_info *mtd;
316 mtd = get_nand_dev_by_index(nand_curr_device);
318 puts("\nno devices available\n");
321 block_size = mtd->erasesize;
323 /* Align U-Boot size to currently used blocksize */
324 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
326 /* Erase the U-BOOT image space */
327 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
328 ret = nand_erase(mtd, 0, image_size);
335 /* Write the image to flash */
336 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
337 (int)image_size, get_load_addr());
338 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
348 static int is_nand_active(void)
353 #else /* CONFIG_CMD_NAND */
354 static int nand_burn_image(size_t image_size)
359 static int is_nand_active(void)
363 #endif /* CONFIG_CMD_NAND */
365 /********************************************************************
367 ********************************************************************/
368 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
369 static size_t usb_read_file(const char *file_name)
377 if (usb_init() < 0) {
378 printf("Error: usb_init failed\n");
382 /* Try to recognize storage devices immediately */
383 blk_first_device(IF_TYPE_USB, &dev);
385 printf("Error: USB storage device not found\n");
389 /* Always load from usb 0 */
390 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
391 printf("Error: USB 0 not found\n");
395 /* Perfrom file read */
396 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
403 static int is_usb_active(void)
408 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
409 static size_t usb_read_file(const char *file_name)
414 static int is_usb_active(void)
418 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
420 /********************************************************************
422 ********************************************************************/
423 #ifdef CONFIG_CMD_NET
424 static size_t tftp_read_file(const char *file_name)
426 /* update global variable load_addr before tftp file from network */
427 load_addr = get_load_addr();
428 return net_loop(TFTPGET);
431 static int is_tftp_active(void)
437 static size_t tftp_read_file(const char *file_name)
442 static int is_tftp_active(void)
446 #endif /* CONFIG_CMD_NET */
458 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
459 {"tftp", tftp_read_file, NULL, is_tftp_active},
460 {"usb", usb_read_file, NULL, is_usb_active},
461 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
462 {"spi", NULL, spi_burn_image, is_spi_active},
463 {"nand", NULL, nand_burn_image, is_nand_active},
466 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
469 printf("Error: Write not supported on device %s\n", dst->name);
473 return dst->write(image_size);
476 #if defined(CONFIG_ARMADA_8K)
477 u32 do_checksum32(u32 *start, int32_t len)
491 static int check_image_header(void)
493 struct mvebu_image_header *hdr =
494 (struct mvebu_image_header *)get_load_addr();
495 u32 header_len = hdr->prolog_size;
497 u32 checksum_ref = hdr->prolog_checksum;
500 * For now compare checksum, and magic. Later we can
501 * verify more stuff on the header like interface type, etc
503 if (hdr->magic != MAIN_HDR_MAGIC) {
504 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
505 hdr->magic, MAIN_HDR_MAGIC);
509 /* The checksum value is discarded from checksum calculation */
510 hdr->prolog_checksum = 0;
512 checksum = do_checksum32((u32 *)hdr, header_len);
513 if (checksum != checksum_ref) {
514 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
515 checksum, checksum_ref);
519 /* Restore the checksum before writing */
520 hdr->prolog_checksum = checksum_ref;
521 printf("Image checksum...OK!\n");
525 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
526 static int check_image_header(void)
528 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
530 u8 hash_160_output[SHA1_SUM_LEN];
531 u8 hash_256_output[SHA256_SUM_LEN];
532 sha1_context hash1_text;
533 sha256_context hash256_text;
535 u32 hash_algorithm_id;
536 u32 image_size_to_hash;
537 u32 flash_entry_addr;
539 u32 internal_hash[HASH_SUM_LEN];
541 u32 num_of_image = hdr->num_images;
542 u32 version = hdr->version;
543 u32 trusted = hdr->trusted;
545 /* bubt checksum validation only supports nontrusted images */
547 printf("bypass image validation, ");
548 printf("only untrusted image is supported now\n");
551 /* only supports image version 3.5 and 3.6 */
552 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
553 printf("Error: Unsupported Image version = 0x%08x\n", version);
556 /* validate images hash value */
557 for (image_num = 0; image_num < num_of_image; image_num++) {
558 struct mvebu_image_info *info =
559 (struct mvebu_image_info *)(get_load_addr() +
560 sizeof(struct common_tim_data) +
561 image_num * sizeof(struct mvebu_image_info));
562 hash_algorithm_id = info->hash_algorithm_id;
563 image_size_to_hash = info->image_size_to_hash;
564 flash_entry_addr = info->flash_entry_addr;
565 hash_value = info->hash;
566 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
568 if (image_num == 0) {
570 * The first image includes hash values in its content.
571 * For hash calculation, we need to save the original
572 * hash values to a local variable that will be
573 * copied back for comparsion and set all zeros to
574 * the orignal hash values for calculating new value.
575 * First image original format :
576 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
577 * Replaced first image format :
578 * x...x (datum1) 0...0(hash values) x...x(datum2)
580 memcpy(internal_hash, hash_value,
581 sizeof(internal_hash));
582 memset(hash_value, 0, sizeof(internal_hash));
584 if (image_size_to_hash == 0) {
585 printf("Warning: Image_%d hash checksum is disabled, ",
587 printf("skip the image validation.\n");
590 switch (hash_algorithm_id) {
592 sha1_starts(&hash1_text);
593 sha1_update(&hash1_text, buff, image_size_to_hash);
594 sha1_finish(&hash1_text, hash_160_output);
595 hash_output = hash_160_output;
598 sha256_starts(&hash256_text);
599 sha256_update(&hash256_text, buff, image_size_to_hash);
600 sha256_finish(&hash256_text, hash_256_output);
601 hash_output = hash_256_output;
604 printf("Error: Unsupported hash_algorithm_id = %d\n",
609 memcpy(hash_value, internal_hash,
610 sizeof(internal_hash));
611 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
612 printf("Error: Image_%d checksum is not correct\n",
617 printf("Image checksum...OK!\n");
622 #else /* Not ARMADA? */
623 static int check_image_header(void)
625 printf("bubt cmd does not support this SoC device or family!\n");
630 static int bubt_verify(size_t image_size)
634 /* Check a correct image header exists */
635 err = check_image_header();
637 printf("Error: Image header verification failed\n");
644 static int bubt_read_file(struct bubt_dev *src)
649 printf("Error: Read not supported on device \"%s\"\n",
654 image_size = src->read(net_boot_file_name);
655 if (image_size <= 0) {
656 printf("Error: Failed to read file %s from %s\n",
657 net_boot_file_name, src->name);
664 static int bubt_is_dev_active(struct bubt_dev *dev)
667 printf("Device \"%s\" not supported by U-BOOT image\n",
672 if (!dev->active()) {
673 printf("Device \"%s\" is inactive\n", dev->name);
680 struct bubt_dev *find_bubt_dev(char *dev_name)
684 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
685 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
686 return &bubt_devs[dev];
692 #define DEFAULT_BUBT_SRC "tftp"
694 #ifndef DEFAULT_BUBT_DST
695 #ifdef CONFIG_MVEBU_SPI_BOOT
696 #define DEFAULT_BUBT_DST "spi"
697 #elif defined(CONFIG_MVEBU_NAND_BOOT)
698 #define DEFAULT_BUBT_DST "nand"
699 #elif defined(CONFIG_MVEBU_MMC_BOOT)
700 #define DEFAULT_BUBT_DST "mmc"
702 #define DEFAULT_BUBT_DST "error"
704 #endif /* DEFAULT_BUBT_DST */
706 int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
708 struct bubt_dev *src, *dst;
710 char src_dev_name[8];
711 char dst_dev_name[8];
716 copy_filename(net_boot_file_name,
717 CONFIG_MVEBU_UBOOT_DFLT_NAME,
718 sizeof(net_boot_file_name));
720 copy_filename(net_boot_file_name, argv[1],
721 sizeof(net_boot_file_name));
724 strncpy(dst_dev_name, argv[2], 8);
726 name = DEFAULT_BUBT_DST;
727 strncpy(dst_dev_name, name, 8);
731 strncpy(src_dev_name, argv[3], 8);
733 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
735 /* Figure out the destination device */
736 dst = find_bubt_dev(dst_dev_name);
738 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
742 if (!bubt_is_dev_active(dst))
745 /* Figure out the source device */
746 src = find_bubt_dev(src_dev_name);
748 printf("Error: Unknown source \"%s\"\n", src_dev_name);
752 if (!bubt_is_dev_active(src))
755 printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
756 net_boot_file_name, src->name, dst->name);
758 image_size = bubt_read_file(src);
762 err = bubt_verify(image_size);
766 err = bubt_write_file(dst, image_size);
774 bubt, 4, 0, do_bubt_cmd,
775 "Burn a u-boot image to flash",
776 "[file-name] [destination [source]]\n"
777 "\t-file-name The image file name to burn. Default = flash-image.bin\n"
778 "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
779 "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
781 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
782 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
783 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"