1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
18 #include <spi_flash.h>
27 #include <u-boot/sha1.h>
28 #include <u-boot/sha256.h>
29 #include <u-boot/sha512.h>
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 tim_boot_flash_sign {
65 struct tim_boot_flash_sign tim_boot_flash_signs[] = {
66 { 0x454d4d08, "mmc" },
67 { 0x454d4d0b, "mmc" },
68 { 0x5350490a, "spi" },
69 { 0x5350491a, "nand" },
70 { 0x55415223, "uart" },
71 { 0x53415432, "sata" },
75 struct common_tim_data {
81 u32 reserved[5]; /* Reserve 20 bytes */
88 struct mvebu_image_info {
94 u32 image_size_to_hash;
95 u32 hash_algorithm_id;
96 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
99 u32 encrypt_start_offset;
102 #elif defined(CONFIG_ARMADA_32BIT)
104 /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
105 struct a38x_main_hdr_v1 {
106 u8 blockid; /* 0x0 */
108 u16 nandpagesize; /* 0x2-0x3 */
109 u32 blocksize; /* 0x4-0x7 */
110 u8 version; /* 0x8 */
111 u8 headersz_msb; /* 0x9 */
112 u16 headersz_lsb; /* 0xA-0xB */
113 u32 srcaddr; /* 0xC-0xF */
114 u32 destaddr; /* 0x10-0x13 */
115 u32 execaddr; /* 0x14-0x17 */
116 u8 options; /* 0x18 */
117 u8 nandblocksize; /* 0x19 */
118 u8 nandbadblklocation; /* 0x1A */
119 u8 reserved4; /* 0x1B */
120 u16 reserved5; /* 0x1C-0x1D */
122 u8 checksum; /* 0x1F */
125 struct a38x_boot_mode {
130 /* The blockid header field values used to indicate boot device of image */
131 struct a38x_boot_mode a38x_boot_modes[] = {
146 size_t (*read)(const char *file_name);
147 int (*write)(size_t image_size);
151 static ulong get_load_addr(void)
153 const char *addr_str;
156 addr_str = env_get("loadaddr");
158 addr = hextoul(addr_str, NULL);
160 addr = CONFIG_SYS_LOAD_ADDR;
165 /********************************************************************
167 ********************************************************************/
168 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
169 static int mmc_burn_image(size_t image_size)
176 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
178 struct blk_desc *blk_desc;
180 mmc = find_mmc_device(mmc_dev_num);
182 printf("No SD/MMC/eMMC card found\n");
188 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
193 /* SD reserves LBA-0 for MBR and boots from LBA-1,
194 * MMC/eMMC boots from LBA-0
196 start_lba = IS_SD(mmc) ? 1 : 0;
198 blk_count = image_size / mmc->write_bl_len;
199 if (image_size % mmc->write_bl_len)
202 blk_desc = mmc_get_blk_desc(mmc);
204 printf("Error - failed to obtain block descriptor\n");
207 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
208 (void *)get_load_addr());
210 blk_count = image_size / mmc->block_dev.blksz;
211 if (image_size % mmc->block_dev.blksz)
214 blk_written = mmc->block_dev.block_write(mmc_dev_num,
215 start_lba, blk_count,
216 (void *)get_load_addr());
217 #endif /* CONFIG_BLK */
218 if (blk_written != blk_count) {
219 printf("Error - written %#lx blocks\n", blk_written);
227 static size_t mmc_read_file(const char *file_name)
232 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
234 mmc = find_mmc_device(mmc_dev_num);
236 printf("No SD/MMC/eMMC card found\n");
241 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
246 /* Load from data partition (0) */
247 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
248 printf("Error: MMC 0 not found\n");
252 /* Perfrom file read */
253 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
260 static int is_mmc_active(void)
264 #else /* CONFIG_DM_MMC */
265 static int mmc_burn_image(size_t image_size)
270 static size_t mmc_read_file(const char *file_name)
275 static int is_mmc_active(void)
279 #endif /* CONFIG_DM_MMC */
281 /********************************************************************
283 ********************************************************************/
284 #ifdef CONFIG_SPI_FLASH
285 static int spi_burn_image(size_t image_size)
288 struct spi_flash *flash;
291 /* Probe the SPI bus to get the flash device */
292 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
293 CONFIG_SF_DEFAULT_CS,
294 CONFIG_SF_DEFAULT_SPEED,
295 CONFIG_SF_DEFAULT_MODE);
297 printf("Failed to probe SPI Flash\n");
301 erase_bytes = image_size +
302 (flash->erase_size - image_size % flash->erase_size);
303 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
304 erase_bytes, erase_bytes / flash->erase_size);
305 ret = spi_flash_erase(flash, 0, erase_bytes);
311 printf("Writing %d bytes from 0x%lx to offset 0 ...",
312 (int)image_size, get_load_addr());
313 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
322 static int is_spi_active(void)
327 #else /* CONFIG_SPI_FLASH */
328 static int spi_burn_image(size_t image_size)
333 static int is_spi_active(void)
337 #endif /* CONFIG_SPI_FLASH */
339 /********************************************************************
341 ********************************************************************/
342 #ifdef CONFIG_CMD_NAND
343 static int nand_burn_image(size_t image_size)
347 struct mtd_info *mtd;
349 mtd = get_nand_dev_by_index(nand_curr_device);
351 puts("\nno devices available\n");
354 block_size = mtd->erasesize;
356 /* Align U-Boot size to currently used blocksize */
357 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
359 /* Erase the U-Boot image space */
360 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
361 ret = nand_erase(mtd, 0, image_size);
368 /* Write the image to flash */
369 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
370 (int)image_size, get_load_addr());
371 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
381 static int is_nand_active(void)
386 #else /* CONFIG_CMD_NAND */
387 static int nand_burn_image(size_t image_size)
392 static int is_nand_active(void)
396 #endif /* CONFIG_CMD_NAND */
398 /********************************************************************
400 ********************************************************************/
401 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
402 static size_t usb_read_file(const char *file_name)
410 if (usb_init() < 0) {
411 printf("Error: usb_init failed\n");
415 /* Try to recognize storage devices immediately */
416 blk_first_device(IF_TYPE_USB, &dev);
418 printf("Error: USB storage device not found\n");
422 /* Always load from usb 0 */
423 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
424 printf("Error: USB 0 not found\n");
428 /* Perfrom file read */
429 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
436 static int is_usb_active(void)
441 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
442 static size_t usb_read_file(const char *file_name)
447 static int is_usb_active(void)
451 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
453 /********************************************************************
455 ********************************************************************/
456 #ifdef CONFIG_CMD_NET
457 static size_t tftp_read_file(const char *file_name)
462 * update global variable image_load_addr before tftp file from network
464 image_load_addr = get_load_addr();
465 ret = net_loop(TFTPGET);
466 return ret > 0 ? ret : 0;
469 static int is_tftp_active(void)
475 static size_t tftp_read_file(const char *file_name)
480 static int is_tftp_active(void)
484 #endif /* CONFIG_CMD_NET */
496 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
497 {"tftp", tftp_read_file, NULL, is_tftp_active},
498 {"usb", usb_read_file, NULL, is_usb_active},
499 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
500 {"spi", NULL, spi_burn_image, is_spi_active},
501 {"nand", NULL, nand_burn_image, is_nand_active},
504 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
507 printf("Error: Write not supported on device %s\n", dst->name);
511 return dst->write(image_size);
514 #if defined(CONFIG_ARMADA_8K)
515 u32 do_checksum32(u32 *start, int32_t len)
529 static int check_image_header(void)
531 struct mvebu_image_header *hdr =
532 (struct mvebu_image_header *)get_load_addr();
533 u32 header_len = hdr->prolog_size;
535 u32 checksum_ref = hdr->prolog_checksum;
538 * For now compare checksum, and magic. Later we can
539 * verify more stuff on the header like interface type, etc
541 if (hdr->magic != MAIN_HDR_MAGIC) {
542 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
543 hdr->magic, MAIN_HDR_MAGIC);
547 /* The checksum value is discarded from checksum calculation */
548 hdr->prolog_checksum = 0;
550 checksum = do_checksum32((u32 *)hdr, header_len);
551 if (checksum != checksum_ref) {
552 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
553 checksum, checksum_ref);
557 /* Restore the checksum before writing */
558 hdr->prolog_checksum = checksum_ref;
559 printf("Image checksum...OK!\n");
563 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
564 static int check_image_header(void)
566 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
568 u8 hash_160_output[SHA1_SUM_LEN];
569 u8 hash_256_output[SHA256_SUM_LEN];
570 u8 hash_512_output[SHA512_SUM_LEN];
571 sha1_context hash1_text;
572 sha256_context hash256_text;
573 sha512_context hash512_text;
575 u32 hash_algorithm_id;
576 u32 image_size_to_hash;
577 u32 flash_entry_addr;
579 u32 internal_hash[HASH_SUM_LEN];
581 u32 num_of_image = hdr->num_images;
582 u32 version = hdr->version;
583 u32 trusted = hdr->trusted;
585 /* bubt checksum validation only supports nontrusted images */
587 printf("bypass image validation, ");
588 printf("only untrusted image is supported now\n");
591 /* only supports image version 3.5 and 3.6 */
592 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
593 printf("Error: Unsupported Image version = 0x%08x\n", version);
596 /* validate images hash value */
597 for (image_num = 0; image_num < num_of_image; image_num++) {
598 struct mvebu_image_info *info =
599 (struct mvebu_image_info *)(get_load_addr() +
600 sizeof(struct common_tim_data) +
601 image_num * sizeof(struct mvebu_image_info));
602 hash_algorithm_id = info->hash_algorithm_id;
603 image_size_to_hash = info->image_size_to_hash;
604 flash_entry_addr = info->flash_entry_addr;
605 hash_value = info->hash;
606 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
608 if (image_num == 0) {
610 * The first image includes hash values in its content.
611 * For hash calculation, we need to save the original
612 * hash values to a local variable that will be
613 * copied back for comparsion and set all zeros to
614 * the orignal hash values for calculating new value.
615 * First image original format :
616 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
617 * Replaced first image format :
618 * x...x (datum1) 0...0(hash values) x...x(datum2)
620 memcpy(internal_hash, hash_value,
621 sizeof(internal_hash));
622 memset(hash_value, 0, sizeof(internal_hash));
624 if (image_size_to_hash == 0) {
625 printf("Warning: Image_%d hash checksum is disabled, ",
627 printf("skip the image validation.\n");
630 switch (hash_algorithm_id) {
632 sha1_starts(&hash1_text);
633 sha1_update(&hash1_text, buff, image_size_to_hash);
634 sha1_finish(&hash1_text, hash_160_output);
635 hash_output = hash_160_output;
638 sha256_starts(&hash256_text);
639 sha256_update(&hash256_text, buff, image_size_to_hash);
640 sha256_finish(&hash256_text, hash_256_output);
641 hash_output = hash_256_output;
644 sha512_starts(&hash512_text);
645 sha512_update(&hash512_text, buff, image_size_to_hash);
646 sha512_finish(&hash512_text, hash_512_output);
647 hash_output = hash_512_output;
650 printf("Error: Unsupported hash_algorithm_id = %d\n",
655 memcpy(hash_value, internal_hash,
656 sizeof(internal_hash));
657 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
658 printf("Error: Image_%d checksum is not correct\n",
663 printf("Image checksum...OK!\n");
667 #elif defined(CONFIG_ARMADA_32BIT)
668 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
671 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
673 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
678 static uint8_t image_checksum8(const void *start, size_t len)
692 static int check_image_header(void)
695 const struct a38x_main_hdr_v1 *hdr =
696 (struct a38x_main_hdr_v1 *)get_load_addr();
697 const size_t image_size = a38x_header_size(hdr);
702 checksum = image_checksum8(hdr, image_size);
703 checksum -= hdr->checksum;
704 if (checksum != hdr->checksum) {
705 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
706 checksum, hdr->checksum);
710 printf("Image checksum...OK!\n");
713 #else /* Not ARMADA? */
714 static int check_image_header(void)
716 printf("bubt cmd does not support this SoC device or family!\n");
721 static int bubt_check_boot_mode(const struct bubt_dev *dst)
723 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
725 #if defined(CONFIG_ARMADA_3700)
726 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
727 const struct common_tim_data *hdr =
728 (struct common_tim_data *)get_load_addr();
729 u32 id = hdr->boot_flash_sign;
730 #elif defined(CONFIG_ARMADA_32BIT)
731 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
732 const struct a38x_main_hdr_v1 *hdr =
733 (struct a38x_main_hdr_v1 *)get_load_addr();
734 u32 id = hdr->blockid;
737 for (mode = 0; boot_modes[mode].name; mode++) {
738 if (boot_modes[mode].id == id)
742 if (!boot_modes[mode].name) {
743 printf("Error: unknown boot device in image header: 0x%x\n", id);
747 if (strcmp(boot_modes[mode].name, dst->name) == 0)
750 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
751 boot_modes[mode].name, dst->name);
758 static int bubt_verify(const struct bubt_dev *dst)
762 /* Check a correct image header exists */
763 err = check_image_header();
765 printf("Error: Image header verification failed\n");
769 err = bubt_check_boot_mode(dst);
771 printf("Error: Image boot mode verification failed\n");
778 static int bubt_read_file(struct bubt_dev *src)
783 printf("Error: Read not supported on device \"%s\"\n",
788 image_size = src->read(net_boot_file_name);
789 if (image_size <= 0) {
790 printf("Error: Failed to read file %s from %s\n",
791 net_boot_file_name, src->name);
798 static int bubt_is_dev_active(struct bubt_dev *dev)
801 printf("Device \"%s\" not supported by U-Boot image\n",
806 if (!dev->active()) {
807 printf("Device \"%s\" is inactive\n", dev->name);
814 struct bubt_dev *find_bubt_dev(char *dev_name)
818 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
819 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
820 return &bubt_devs[dev];
826 #define DEFAULT_BUBT_SRC "tftp"
828 #ifndef DEFAULT_BUBT_DST
829 #ifdef CONFIG_MVEBU_SPI_BOOT
830 #define DEFAULT_BUBT_DST "spi"
831 #elif defined(CONFIG_MVEBU_NAND_BOOT)
832 #define DEFAULT_BUBT_DST "nand"
833 #elif defined(CONFIG_MVEBU_MMC_BOOT)
834 #define DEFAULT_BUBT_DST "mmc"
836 #define DEFAULT_BUBT_DST "error"
838 #endif /* DEFAULT_BUBT_DST */
840 int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
842 struct bubt_dev *src, *dst;
844 char src_dev_name[8];
845 char dst_dev_name[8];
850 copy_filename(net_boot_file_name,
851 CONFIG_MVEBU_UBOOT_DFLT_NAME,
852 sizeof(net_boot_file_name));
854 copy_filename(net_boot_file_name, argv[1],
855 sizeof(net_boot_file_name));
858 strncpy(dst_dev_name, argv[2], 8);
860 name = DEFAULT_BUBT_DST;
861 strncpy(dst_dev_name, name, 8);
865 strncpy(src_dev_name, argv[3], 8);
867 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
869 /* Figure out the destination device */
870 dst = find_bubt_dev(dst_dev_name);
872 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
876 if (!bubt_is_dev_active(dst))
879 /* Figure out the source device */
880 src = find_bubt_dev(src_dev_name);
882 printf("Error: Unknown source \"%s\"\n", src_dev_name);
886 if (!bubt_is_dev_active(src))
889 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
890 net_boot_file_name, src->name, dst->name);
892 image_size = bubt_read_file(src);
896 err = bubt_verify(dst);
900 err = bubt_write_file(dst, image_size);
908 bubt, 4, 0, do_bubt_cmd,
909 "Burn a u-boot image to flash",
910 "[file-name] [destination [source]]\n"
911 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
912 "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
913 "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
915 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
916 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
917 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"