1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
17 #include <spi_flash.h>
26 #include <u-boot/sha1.h>
27 #include <u-boot/sha256.h>
28 #include <u-boot/sha512.h>
30 #if defined(CONFIG_ARMADA_8K)
31 #define MAIN_HDR_MAGIC 0xB105B002
33 struct mvebu_image_header {
35 u32 prolog_size; /* 4-7 */
36 u32 prolog_checksum; /* 8-11 */
37 u32 boot_image_size; /* 12-15 */
38 u32 boot_image_checksum; /* 16-19 */
39 u32 rsrvd0; /* 20-23 */
40 u32 load_addr; /* 24-27 */
41 u32 exec_addr; /* 28-31 */
44 u8 ext_count; /* 34 */
45 u8 aux_flags; /* 35 */
46 u32 io_arg_0; /* 36-39 */
47 u32 io_arg_1; /* 40-43 */
48 u32 io_arg_2; /* 43-47 */
49 u32 io_arg_3; /* 48-51 */
50 u32 rsrvd1; /* 52-55 */
51 u32 rsrvd2; /* 56-59 */
52 u32 rsrvd3; /* 60-63 */
54 #elif defined(CONFIG_ARMADA_3700) /* A3700 */
55 #define HASH_SUM_LEN 16
56 #define IMAGE_VERSION_3_6_0 0x030600
57 #define IMAGE_VERSION_3_5_0 0x030500
59 struct tim_boot_flash_sign {
64 struct tim_boot_flash_sign tim_boot_flash_signs[] = {
65 { 0x454d4d08, "mmc" },
66 { 0x454d4d0b, "mmc" },
67 { 0x5350490a, "spi" },
68 { 0x5350491a, "nand" },
69 { 0x55415223, "uart" },
70 { 0x53415432, "sata" },
74 struct common_tim_data {
80 u32 reserved[5]; /* Reserve 20 bytes */
87 struct mvebu_image_info {
93 u32 image_size_to_hash;
94 u32 hash_algorithm_id;
95 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
98 u32 encrypt_start_offset;
101 #elif defined(CONFIG_ARMADA_32BIT)
103 /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
104 struct a38x_main_hdr_v1 {
105 u8 blockid; /* 0x0 */
107 u16 nandpagesize; /* 0x2-0x3 */
108 u32 blocksize; /* 0x4-0x7 */
109 u8 version; /* 0x8 */
110 u8 headersz_msb; /* 0x9 */
111 u16 headersz_lsb; /* 0xA-0xB */
112 u32 srcaddr; /* 0xC-0xF */
113 u32 destaddr; /* 0x10-0x13 */
114 u32 execaddr; /* 0x14-0x17 */
115 u8 options; /* 0x18 */
116 u8 nandblocksize; /* 0x19 */
117 u8 nandbadblklocation; /* 0x1A */
118 u8 reserved4; /* 0x1B */
119 u16 reserved5; /* 0x1C-0x1D */
121 u8 checksum; /* 0x1F */
124 struct a38x_boot_mode {
129 /* The blockid header field values used to indicate boot device of image */
130 struct a38x_boot_mode a38x_boot_modes[] = {
145 size_t (*read)(const char *file_name);
146 int (*write)(size_t image_size);
150 static ulong get_load_addr(void)
152 const char *addr_str;
155 addr_str = env_get("loadaddr");
157 addr = hextoul(addr_str, NULL);
159 addr = CONFIG_SYS_LOAD_ADDR;
164 /********************************************************************
166 ********************************************************************/
167 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
168 static int mmc_burn_image(size_t image_size)
175 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
177 struct blk_desc *blk_desc;
179 mmc = find_mmc_device(mmc_dev_num);
181 printf("No SD/MMC/eMMC card found\n");
187 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
192 /* SD reserves LBA-0 for MBR and boots from LBA-1,
193 * MMC/eMMC boots from LBA-0
195 start_lba = IS_SD(mmc) ? 1 : 0;
197 blk_count = image_size / mmc->write_bl_len;
198 if (image_size % mmc->write_bl_len)
201 blk_desc = mmc_get_blk_desc(mmc);
203 printf("Error - failed to obtain block descriptor\n");
206 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
207 (void *)get_load_addr());
209 blk_count = image_size / mmc->block_dev.blksz;
210 if (image_size % mmc->block_dev.blksz)
213 blk_written = mmc->block_dev.block_write(mmc_dev_num,
214 start_lba, blk_count,
215 (void *)get_load_addr());
216 #endif /* CONFIG_BLK */
217 if (blk_written != blk_count) {
218 printf("Error - written %#lx blocks\n", blk_written);
226 static size_t mmc_read_file(const char *file_name)
231 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
233 mmc = find_mmc_device(mmc_dev_num);
235 printf("No SD/MMC/eMMC card found\n");
240 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
245 /* Load from data partition (0) */
246 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
247 printf("Error: MMC 0 not found\n");
251 /* Perfrom file read */
252 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
259 static int is_mmc_active(void)
263 #else /* CONFIG_DM_MMC */
264 static int mmc_burn_image(size_t image_size)
269 static size_t mmc_read_file(const char *file_name)
274 static int is_mmc_active(void)
278 #endif /* CONFIG_DM_MMC */
280 /********************************************************************
282 ********************************************************************/
283 #ifdef CONFIG_SPI_FLASH
284 static int spi_burn_image(size_t image_size)
287 struct spi_flash *flash;
290 /* Probe the SPI bus to get the flash device */
291 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
292 CONFIG_SF_DEFAULT_CS,
293 CONFIG_SF_DEFAULT_SPEED,
294 CONFIG_SF_DEFAULT_MODE);
296 printf("Failed to probe SPI Flash\n");
300 erase_bytes = image_size +
301 (flash->erase_size - image_size % flash->erase_size);
302 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
303 erase_bytes, erase_bytes / flash->erase_size);
304 ret = spi_flash_erase(flash, 0, erase_bytes);
310 printf("Writing %d bytes from 0x%lx to offset 0 ...",
311 (int)image_size, get_load_addr());
312 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
321 static int is_spi_active(void)
326 #else /* CONFIG_SPI_FLASH */
327 static int spi_burn_image(size_t image_size)
332 static int is_spi_active(void)
336 #endif /* CONFIG_SPI_FLASH */
338 /********************************************************************
340 ********************************************************************/
341 #ifdef CONFIG_CMD_NAND
342 static int nand_burn_image(size_t image_size)
346 struct mtd_info *mtd;
348 mtd = get_nand_dev_by_index(nand_curr_device);
350 puts("\nno devices available\n");
353 block_size = mtd->erasesize;
355 /* Align U-Boot size to currently used blocksize */
356 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
358 /* Erase the U-Boot image space */
359 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
360 ret = nand_erase(mtd, 0, image_size);
367 /* Write the image to flash */
368 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
369 (int)image_size, get_load_addr());
370 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
380 static int is_nand_active(void)
385 #else /* CONFIG_CMD_NAND */
386 static int nand_burn_image(size_t image_size)
391 static int is_nand_active(void)
395 #endif /* CONFIG_CMD_NAND */
397 /********************************************************************
399 ********************************************************************/
400 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
401 static size_t usb_read_file(const char *file_name)
409 if (usb_init() < 0) {
410 printf("Error: usb_init failed\n");
414 /* Try to recognize storage devices immediately */
415 blk_first_device(IF_TYPE_USB, &dev);
417 printf("Error: USB storage device not found\n");
421 /* Always load from usb 0 */
422 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
423 printf("Error: USB 0 not found\n");
427 /* Perfrom file read */
428 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
435 static int is_usb_active(void)
440 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
441 static size_t usb_read_file(const char *file_name)
446 static int is_usb_active(void)
450 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
452 /********************************************************************
454 ********************************************************************/
455 #ifdef CONFIG_CMD_NET
456 static size_t tftp_read_file(const char *file_name)
461 * update global variable image_load_addr before tftp file from network
463 image_load_addr = get_load_addr();
464 ret = net_loop(TFTPGET);
465 return ret > 0 ? ret : 0;
468 static int is_tftp_active(void)
474 static size_t tftp_read_file(const char *file_name)
479 static int is_tftp_active(void)
483 #endif /* CONFIG_CMD_NET */
495 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
496 {"tftp", tftp_read_file, NULL, is_tftp_active},
497 {"usb", usb_read_file, NULL, is_usb_active},
498 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
499 {"spi", NULL, spi_burn_image, is_spi_active},
500 {"nand", NULL, nand_burn_image, is_nand_active},
503 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
506 printf("Error: Write not supported on device %s\n", dst->name);
510 return dst->write(image_size);
513 #if defined(CONFIG_ARMADA_8K)
514 u32 do_checksum32(u32 *start, int32_t len)
528 static int check_image_header(void)
530 struct mvebu_image_header *hdr =
531 (struct mvebu_image_header *)get_load_addr();
532 u32 header_len = hdr->prolog_size;
534 u32 checksum_ref = hdr->prolog_checksum;
537 * For now compare checksum, and magic. Later we can
538 * verify more stuff on the header like interface type, etc
540 if (hdr->magic != MAIN_HDR_MAGIC) {
541 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
542 hdr->magic, MAIN_HDR_MAGIC);
546 /* The checksum value is discarded from checksum calculation */
547 hdr->prolog_checksum = 0;
549 checksum = do_checksum32((u32 *)hdr, header_len);
550 if (checksum != checksum_ref) {
551 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
552 checksum, checksum_ref);
556 /* Restore the checksum before writing */
557 hdr->prolog_checksum = checksum_ref;
558 printf("Image checksum...OK!\n");
562 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
563 static int check_image_header(void)
565 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
567 u8 hash_160_output[SHA1_SUM_LEN];
568 u8 hash_256_output[SHA256_SUM_LEN];
569 u8 hash_512_output[SHA512_SUM_LEN];
570 sha1_context hash1_text;
571 sha256_context hash256_text;
572 sha512_context hash512_text;
574 u32 hash_algorithm_id;
575 u32 image_size_to_hash;
576 u32 flash_entry_addr;
578 u32 internal_hash[HASH_SUM_LEN];
580 u32 num_of_image = hdr->num_images;
581 u32 version = hdr->version;
582 u32 trusted = hdr->trusted;
584 /* bubt checksum validation only supports nontrusted images */
586 printf("bypass image validation, ");
587 printf("only untrusted image is supported now\n");
590 /* only supports image version 3.5 and 3.6 */
591 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
592 printf("Error: Unsupported Image version = 0x%08x\n", version);
595 /* validate images hash value */
596 for (image_num = 0; image_num < num_of_image; image_num++) {
597 struct mvebu_image_info *info =
598 (struct mvebu_image_info *)(get_load_addr() +
599 sizeof(struct common_tim_data) +
600 image_num * sizeof(struct mvebu_image_info));
601 hash_algorithm_id = info->hash_algorithm_id;
602 image_size_to_hash = info->image_size_to_hash;
603 flash_entry_addr = info->flash_entry_addr;
604 hash_value = info->hash;
605 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
607 if (image_num == 0) {
609 * The first image includes hash values in its content.
610 * For hash calculation, we need to save the original
611 * hash values to a local variable that will be
612 * copied back for comparsion and set all zeros to
613 * the orignal hash values for calculating new value.
614 * First image original format :
615 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
616 * Replaced first image format :
617 * x...x (datum1) 0...0(hash values) x...x(datum2)
619 memcpy(internal_hash, hash_value,
620 sizeof(internal_hash));
621 memset(hash_value, 0, sizeof(internal_hash));
623 if (image_size_to_hash == 0) {
624 printf("Warning: Image_%d hash checksum is disabled, ",
626 printf("skip the image validation.\n");
629 switch (hash_algorithm_id) {
631 sha1_starts(&hash1_text);
632 sha1_update(&hash1_text, buff, image_size_to_hash);
633 sha1_finish(&hash1_text, hash_160_output);
634 hash_output = hash_160_output;
637 sha256_starts(&hash256_text);
638 sha256_update(&hash256_text, buff, image_size_to_hash);
639 sha256_finish(&hash256_text, hash_256_output);
640 hash_output = hash_256_output;
643 sha512_starts(&hash512_text);
644 sha512_update(&hash512_text, buff, image_size_to_hash);
645 sha512_finish(&hash512_text, hash_512_output);
646 hash_output = hash_512_output;
649 printf("Error: Unsupported hash_algorithm_id = %d\n",
654 memcpy(hash_value, internal_hash,
655 sizeof(internal_hash));
656 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
657 printf("Error: Image_%d checksum is not correct\n",
662 printf("Image checksum...OK!\n");
666 #elif defined(CONFIG_ARMADA_32BIT)
667 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
670 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
672 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
677 static uint8_t image_checksum8(const void *start, size_t len)
691 static int check_image_header(void)
694 const struct a38x_main_hdr_v1 *hdr =
695 (struct a38x_main_hdr_v1 *)get_load_addr();
696 const size_t image_size = a38x_header_size(hdr);
701 checksum = image_checksum8(hdr, image_size);
702 checksum -= hdr->checksum;
703 if (checksum != hdr->checksum) {
704 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
705 checksum, hdr->checksum);
709 printf("Image checksum...OK!\n");
712 #else /* Not ARMADA? */
713 static int check_image_header(void)
715 printf("bubt cmd does not support this SoC device or family!\n");
720 static int bubt_check_boot_mode(const struct bubt_dev *dst)
722 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
724 #if defined(CONFIG_ARMADA_3700)
725 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
726 const struct common_tim_data *hdr =
727 (struct common_tim_data *)get_load_addr();
728 u32 id = hdr->boot_flash_sign;
729 #elif defined(CONFIG_ARMADA_32BIT)
730 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
731 const struct a38x_main_hdr_v1 *hdr =
732 (struct a38x_main_hdr_v1 *)get_load_addr();
733 u32 id = hdr->blockid;
736 for (mode = 0; boot_modes[mode].name; mode++) {
737 if (boot_modes[mode].id == id)
741 if (!boot_modes[mode].name) {
742 printf("Error: unknown boot device in image header: 0x%x\n", id);
746 if (strcmp(boot_modes[mode].name, dst->name) == 0)
749 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
750 boot_modes[mode].name, dst->name);
757 static int bubt_verify(const struct bubt_dev *dst)
761 /* Check a correct image header exists */
762 err = check_image_header();
764 printf("Error: Image header verification failed\n");
768 err = bubt_check_boot_mode(dst);
770 printf("Error: Image boot mode verification failed\n");
777 static int bubt_read_file(struct bubt_dev *src)
782 printf("Error: Read not supported on device \"%s\"\n",
787 image_size = src->read(net_boot_file_name);
788 if (image_size <= 0) {
789 printf("Error: Failed to read file %s from %s\n",
790 net_boot_file_name, src->name);
797 static int bubt_is_dev_active(struct bubt_dev *dev)
800 printf("Device \"%s\" not supported by U-Boot image\n",
805 if (!dev->active()) {
806 printf("Device \"%s\" is inactive\n", dev->name);
813 struct bubt_dev *find_bubt_dev(char *dev_name)
817 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
818 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
819 return &bubt_devs[dev];
825 #define DEFAULT_BUBT_SRC "tftp"
827 #ifndef DEFAULT_BUBT_DST
828 #ifdef CONFIG_MVEBU_SPI_BOOT
829 #define DEFAULT_BUBT_DST "spi"
830 #elif defined(CONFIG_MVEBU_NAND_BOOT)
831 #define DEFAULT_BUBT_DST "nand"
832 #elif defined(CONFIG_MVEBU_MMC_BOOT)
833 #define DEFAULT_BUBT_DST "mmc"
835 #define DEFAULT_BUBT_DST "error"
837 #endif /* DEFAULT_BUBT_DST */
839 int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
841 struct bubt_dev *src, *dst;
843 char src_dev_name[8];
844 char dst_dev_name[8];
849 copy_filename(net_boot_file_name,
850 CONFIG_MVEBU_UBOOT_DFLT_NAME,
851 sizeof(net_boot_file_name));
853 copy_filename(net_boot_file_name, argv[1],
854 sizeof(net_boot_file_name));
857 strncpy(dst_dev_name, argv[2], 8);
859 name = DEFAULT_BUBT_DST;
860 strncpy(dst_dev_name, name, 8);
864 strncpy(src_dev_name, argv[3], 8);
866 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
868 /* Figure out the destination device */
869 dst = find_bubt_dev(dst_dev_name);
871 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
875 if (!bubt_is_dev_active(dst))
878 /* Figure out the source device */
879 src = find_bubt_dev(src_dev_name);
881 printf("Error: Unknown source \"%s\"\n", src_dev_name);
885 if (!bubt_is_dev_active(src))
888 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
889 net_boot_file_name, src->name, dst->name);
891 image_size = bubt_read_file(src);
895 err = bubt_verify(dst);
899 err = bubt_write_file(dst, image_size);
907 bubt, 4, 0, do_bubt_cmd,
908 "Burn a u-boot image to flash",
909 "[file-name] [destination [source]]\n"
910 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
911 "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
912 "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
914 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
915 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
916 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"