1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
16 #include <spi_flash.h>
25 #include <u-boot/sha1.h>
26 #include <u-boot/sha256.h>
28 #ifndef CONFIG_SYS_MMC_ENV_DEV
29 #define CONFIG_SYS_MMC_ENV_DEV 0
32 #if defined(CONFIG_ARMADA_8K)
33 #define MAIN_HDR_MAGIC 0xB105B002
35 struct mvebu_image_header {
37 u32 prolog_size; /* 4-7 */
38 u32 prolog_checksum; /* 8-11 */
39 u32 boot_image_size; /* 12-15 */
40 u32 boot_image_checksum; /* 16-19 */
41 u32 rsrvd0; /* 20-23 */
42 u32 load_addr; /* 24-27 */
43 u32 exec_addr; /* 28-31 */
46 u8 ext_count; /* 34 */
47 u8 aux_flags; /* 35 */
48 u32 io_arg_0; /* 36-39 */
49 u32 io_arg_1; /* 40-43 */
50 u32 io_arg_2; /* 43-47 */
51 u32 io_arg_3; /* 48-51 */
52 u32 rsrvd1; /* 52-55 */
53 u32 rsrvd2; /* 56-59 */
54 u32 rsrvd3; /* 60-63 */
56 #elif defined(CONFIG_ARMADA_3700) /* A3700 */
57 #define HASH_SUM_LEN 16
58 #define IMAGE_VERSION_3_6_0 0x030600
59 #define IMAGE_VERSION_3_5_0 0x030500
61 struct common_tim_data {
67 u32 reserved[5]; /* Reserve 20 bytes */
74 struct mvebu_image_info {
80 u32 image_size_to_hash;
81 u32 hash_algorithm_id;
82 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
85 u32 encrypt_start_offset;
88 #elif defined(CONFIG_ARMADA_38X) /* A38X */
90 /* Structure of the main header, version 1 (Armada 370/38x/XP) */
91 struct a38x_main_hdr_v1 {
94 u16 reserved2; /* 0x2-0x3 */
95 u32 blocksize; /* 0x4-0x7 */
97 u8 headersz_msb; /* 0x9 */
98 u16 headersz_lsb; /* 0xA-0xB */
99 u32 srcaddr; /* 0xC-0xF */
100 u32 destaddr; /* 0x10-0x13 */
101 u32 execaddr; /* 0x14-0x17 */
102 u8 options; /* 0x18 */
103 u8 nandblocksize; /* 0x19 */
104 u8 nandbadblklocation; /* 0x1A */
105 u8 reserved4; /* 0x1B */
106 u16 reserved5; /* 0x1C-0x1D */
108 u8 checksum; /* 0x1F */
114 size_t (*read)(const char *file_name);
115 int (*write)(size_t image_size);
119 static ulong get_load_addr(void)
121 const char *addr_str;
124 addr_str = env_get("loadaddr");
126 addr = simple_strtoul(addr_str, NULL, 16);
128 addr = CONFIG_SYS_LOAD_ADDR;
133 /********************************************************************
135 ********************************************************************/
136 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
137 static int mmc_burn_image(size_t image_size)
144 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
146 struct blk_desc *blk_desc;
148 mmc = find_mmc_device(mmc_dev_num);
150 printf("No SD/MMC/eMMC card found\n");
156 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
161 #ifdef CONFIG_SYS_MMC_ENV_PART
162 if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) {
163 err = mmc_switch_part(mmc_dev_num, CONFIG_SYS_MMC_ENV_PART);
165 printf("MMC partition switch failed\n");
171 /* SD reserves LBA-0 for MBR and boots from LBA-1,
172 * MMC/eMMC boots from LBA-0
174 start_lba = IS_SD(mmc) ? 1 : 0;
176 blk_count = image_size / mmc->write_bl_len;
177 if (image_size % mmc->write_bl_len)
180 blk_desc = mmc_get_blk_desc(mmc);
182 printf("Error - failed to obtain block descriptor\n");
185 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
186 (void *)get_load_addr());
188 blk_count = image_size / mmc->block_dev.blksz;
189 if (image_size % mmc->block_dev.blksz)
192 blk_written = mmc->block_dev.block_write(mmc_dev_num,
193 start_lba, blk_count,
194 (void *)get_load_addr());
195 #endif /* CONFIG_BLK */
196 if (blk_written != blk_count) {
197 printf("Error - written %#lx blocks\n", blk_written);
202 #ifdef CONFIG_SYS_MMC_ENV_PART
203 if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART)
204 mmc_switch_part(mmc_dev_num, mmc->part_num);
210 static size_t mmc_read_file(const char *file_name)
215 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
217 mmc = find_mmc_device(mmc_dev_num);
219 printf("No SD/MMC/eMMC card found\n");
224 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
229 /* Load from data partition (0) */
230 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
231 printf("Error: MMC 0 not found\n");
235 /* Perfrom file read */
236 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
243 static int is_mmc_active(void)
247 #else /* CONFIG_DM_MMC */
248 static int mmc_burn_image(size_t image_size)
253 static size_t mmc_read_file(const char *file_name)
258 static int is_mmc_active(void)
262 #endif /* CONFIG_DM_MMC */
264 /********************************************************************
266 ********************************************************************/
267 #ifdef CONFIG_SPI_FLASH
268 static int spi_burn_image(size_t image_size)
271 struct spi_flash *flash;
274 /* Probe the SPI bus to get the flash device */
275 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
277 CONFIG_SF_DEFAULT_SPEED,
278 CONFIG_SF_DEFAULT_MODE);
280 printf("Failed to probe SPI Flash\n");
284 #ifdef CONFIG_SPI_FLASH_PROTECTION
285 spi_flash_protect(flash, 0);
287 erase_bytes = image_size +
288 (flash->erase_size - image_size % flash->erase_size);
289 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
290 erase_bytes, erase_bytes / flash->erase_size);
291 ret = spi_flash_erase(flash, 0, erase_bytes);
297 printf("Writing %d bytes from 0x%lx to offset 0 ...",
298 (int)image_size, get_load_addr());
299 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
305 #ifdef CONFIG_SPI_FLASH_PROTECTION
306 spi_flash_protect(flash, 1);
312 static int is_spi_active(void)
317 #else /* CONFIG_SPI_FLASH */
318 static int spi_burn_image(size_t image_size)
323 static int is_spi_active(void)
327 #endif /* CONFIG_SPI_FLASH */
329 /********************************************************************
331 ********************************************************************/
332 #ifdef CONFIG_CMD_NAND
333 static int nand_burn_image(size_t image_size)
337 struct mtd_info *mtd;
339 mtd = get_nand_dev_by_index(nand_curr_device);
341 puts("\nno devices available\n");
344 block_size = mtd->erasesize;
346 /* Align U-Boot size to currently used blocksize */
347 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
349 /* Erase the U-BOOT image space */
350 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
351 ret = nand_erase(mtd, 0, image_size);
358 /* Write the image to flash */
359 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
360 (int)image_size, get_load_addr());
361 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
371 static int is_nand_active(void)
376 #else /* CONFIG_CMD_NAND */
377 static int nand_burn_image(size_t image_size)
382 static int is_nand_active(void)
386 #endif /* CONFIG_CMD_NAND */
388 /********************************************************************
390 ********************************************************************/
391 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
392 static size_t usb_read_file(const char *file_name)
400 if (usb_init() < 0) {
401 printf("Error: usb_init failed\n");
405 /* Try to recognize storage devices immediately */
406 blk_first_device(IF_TYPE_USB, &dev);
408 printf("Error: USB storage device not found\n");
412 /* Always load from usb 0 */
413 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
414 printf("Error: USB 0 not found\n");
418 /* Perfrom file read */
419 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
426 static int is_usb_active(void)
431 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
432 static size_t usb_read_file(const char *file_name)
437 static int is_usb_active(void)
441 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
443 /********************************************************************
445 ********************************************************************/
446 #ifdef CONFIG_CMD_NET
447 static size_t tftp_read_file(const char *file_name)
450 * update global variable image_load_addr before tftp file from network
452 image_load_addr = get_load_addr();
453 return net_loop(TFTPGET);
456 static int is_tftp_active(void)
462 static size_t tftp_read_file(const char *file_name)
467 static int is_tftp_active(void)
471 #endif /* CONFIG_CMD_NET */
483 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
484 {"tftp", tftp_read_file, NULL, is_tftp_active},
485 {"usb", usb_read_file, NULL, is_usb_active},
486 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
487 {"spi", NULL, spi_burn_image, is_spi_active},
488 {"nand", NULL, nand_burn_image, is_nand_active},
491 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
494 printf("Error: Write not supported on device %s\n", dst->name);
498 return dst->write(image_size);
501 #if defined(CONFIG_ARMADA_8K)
502 u32 do_checksum32(u32 *start, int32_t len)
516 static int check_image_header(void)
518 struct mvebu_image_header *hdr =
519 (struct mvebu_image_header *)get_load_addr();
520 u32 header_len = hdr->prolog_size;
522 u32 checksum_ref = hdr->prolog_checksum;
525 * For now compare checksum, and magic. Later we can
526 * verify more stuff on the header like interface type, etc
528 if (hdr->magic != MAIN_HDR_MAGIC) {
529 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
530 hdr->magic, MAIN_HDR_MAGIC);
534 /* The checksum value is discarded from checksum calculation */
535 hdr->prolog_checksum = 0;
537 checksum = do_checksum32((u32 *)hdr, header_len);
538 if (checksum != checksum_ref) {
539 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
540 checksum, checksum_ref);
544 /* Restore the checksum before writing */
545 hdr->prolog_checksum = checksum_ref;
546 printf("Image checksum...OK!\n");
550 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
551 static int check_image_header(void)
553 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
555 u8 hash_160_output[SHA1_SUM_LEN];
556 u8 hash_256_output[SHA256_SUM_LEN];
557 sha1_context hash1_text;
558 sha256_context hash256_text;
560 u32 hash_algorithm_id;
561 u32 image_size_to_hash;
562 u32 flash_entry_addr;
564 u32 internal_hash[HASH_SUM_LEN];
566 u32 num_of_image = hdr->num_images;
567 u32 version = hdr->version;
568 u32 trusted = hdr->trusted;
570 /* bubt checksum validation only supports nontrusted images */
572 printf("bypass image validation, ");
573 printf("only untrusted image is supported now\n");
576 /* only supports image version 3.5 and 3.6 */
577 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
578 printf("Error: Unsupported Image version = 0x%08x\n", version);
581 /* validate images hash value */
582 for (image_num = 0; image_num < num_of_image; image_num++) {
583 struct mvebu_image_info *info =
584 (struct mvebu_image_info *)(get_load_addr() +
585 sizeof(struct common_tim_data) +
586 image_num * sizeof(struct mvebu_image_info));
587 hash_algorithm_id = info->hash_algorithm_id;
588 image_size_to_hash = info->image_size_to_hash;
589 flash_entry_addr = info->flash_entry_addr;
590 hash_value = info->hash;
591 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
593 if (image_num == 0) {
595 * The first image includes hash values in its content.
596 * For hash calculation, we need to save the original
597 * hash values to a local variable that will be
598 * copied back for comparsion and set all zeros to
599 * the orignal hash values for calculating new value.
600 * First image original format :
601 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
602 * Replaced first image format :
603 * x...x (datum1) 0...0(hash values) x...x(datum2)
605 memcpy(internal_hash, hash_value,
606 sizeof(internal_hash));
607 memset(hash_value, 0, sizeof(internal_hash));
609 if (image_size_to_hash == 0) {
610 printf("Warning: Image_%d hash checksum is disabled, ",
612 printf("skip the image validation.\n");
615 switch (hash_algorithm_id) {
617 sha1_starts(&hash1_text);
618 sha1_update(&hash1_text, buff, image_size_to_hash);
619 sha1_finish(&hash1_text, hash_160_output);
620 hash_output = hash_160_output;
623 sha256_starts(&hash256_text);
624 sha256_update(&hash256_text, buff, image_size_to_hash);
625 sha256_finish(&hash256_text, hash_256_output);
626 hash_output = hash_256_output;
629 printf("Error: Unsupported hash_algorithm_id = %d\n",
634 memcpy(hash_value, internal_hash,
635 sizeof(internal_hash));
636 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
637 printf("Error: Image_%d checksum is not correct\n",
642 printf("Image checksum...OK!\n");
646 #elif defined(CONFIG_ARMADA_38X)
647 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
650 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
652 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
657 static uint8_t image_checksum8(const void *start, size_t len)
671 static int check_image_header(void)
674 const struct a38x_main_hdr_v1 *hdr =
675 (struct a38x_main_hdr_v1 *)get_load_addr();
676 const size_t image_size = a38x_header_size(hdr);
681 checksum = image_checksum8(hdr, image_size);
682 checksum -= hdr->checksum;
683 if (checksum != hdr->checksum) {
684 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
685 checksum, hdr->checksum);
689 printf("Image checksum...OK!\n");
692 #else /* Not ARMADA? */
693 static int check_image_header(void)
695 printf("bubt cmd does not support this SoC device or family!\n");
700 static int bubt_verify(size_t image_size)
704 /* Check a correct image header exists */
705 err = check_image_header();
707 printf("Error: Image header verification failed\n");
714 static int bubt_read_file(struct bubt_dev *src)
719 printf("Error: Read not supported on device \"%s\"\n",
724 image_size = src->read(net_boot_file_name);
725 if (image_size <= 0) {
726 printf("Error: Failed to read file %s from %s\n",
727 net_boot_file_name, src->name);
734 static int bubt_is_dev_active(struct bubt_dev *dev)
737 printf("Device \"%s\" not supported by U-BOOT image\n",
742 if (!dev->active()) {
743 printf("Device \"%s\" is inactive\n", dev->name);
750 struct bubt_dev *find_bubt_dev(char *dev_name)
754 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
755 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
756 return &bubt_devs[dev];
762 #define DEFAULT_BUBT_SRC "tftp"
764 #ifndef DEFAULT_BUBT_DST
765 #ifdef CONFIG_MVEBU_SPI_BOOT
766 #define DEFAULT_BUBT_DST "spi"
767 #elif defined(CONFIG_MVEBU_NAND_BOOT)
768 #define DEFAULT_BUBT_DST "nand"
769 #elif defined(CONFIG_MVEBU_MMC_BOOT)
770 #define DEFAULT_BUBT_DST "mmc"
772 #define DEFAULT_BUBT_DST "error"
774 #endif /* DEFAULT_BUBT_DST */
776 int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
778 struct bubt_dev *src, *dst;
780 char src_dev_name[8];
781 char dst_dev_name[8];
786 copy_filename(net_boot_file_name,
787 CONFIG_MVEBU_UBOOT_DFLT_NAME,
788 sizeof(net_boot_file_name));
790 copy_filename(net_boot_file_name, argv[1],
791 sizeof(net_boot_file_name));
794 strncpy(dst_dev_name, argv[2], 8);
796 name = DEFAULT_BUBT_DST;
797 strncpy(dst_dev_name, name, 8);
801 strncpy(src_dev_name, argv[3], 8);
803 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
805 /* Figure out the destination device */
806 dst = find_bubt_dev(dst_dev_name);
808 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
812 if (!bubt_is_dev_active(dst))
815 /* Figure out the source device */
816 src = find_bubt_dev(src_dev_name);
818 printf("Error: Unknown source \"%s\"\n", src_dev_name);
822 if (!bubt_is_dev_active(src))
825 printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
826 net_boot_file_name, src->name, dst->name);
828 image_size = bubt_read_file(src);
832 err = bubt_verify(image_size);
836 err = bubt_write_file(dst, image_size);
844 bubt, 4, 0, do_bubt_cmd,
845 "Burn a u-boot image to flash",
846 "[file-name] [destination [source]]\n"
847 "\t-file-name The image file name to burn. Default = flash-image.bin\n"
848 "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
849 "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
851 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
852 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
853 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"