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>
30 #ifndef CONFIG_SYS_MMC_ENV_DEV
31 #define CONFIG_SYS_MMC_ENV_DEV 0
34 #if defined(CONFIG_ARMADA_8K)
35 #define MAIN_HDR_MAGIC 0xB105B002
37 struct mvebu_image_header {
39 u32 prolog_size; /* 4-7 */
40 u32 prolog_checksum; /* 8-11 */
41 u32 boot_image_size; /* 12-15 */
42 u32 boot_image_checksum; /* 16-19 */
43 u32 rsrvd0; /* 20-23 */
44 u32 load_addr; /* 24-27 */
45 u32 exec_addr; /* 28-31 */
48 u8 ext_count; /* 34 */
49 u8 aux_flags; /* 35 */
50 u32 io_arg_0; /* 36-39 */
51 u32 io_arg_1; /* 40-43 */
52 u32 io_arg_2; /* 43-47 */
53 u32 io_arg_3; /* 48-51 */
54 u32 rsrvd1; /* 52-55 */
55 u32 rsrvd2; /* 56-59 */
56 u32 rsrvd3; /* 60-63 */
58 #elif defined(CONFIG_ARMADA_3700) /* A3700 */
59 #define HASH_SUM_LEN 16
60 #define IMAGE_VERSION_3_6_0 0x030600
61 #define IMAGE_VERSION_3_5_0 0x030500
63 struct common_tim_data {
69 u32 reserved[5]; /* Reserve 20 bytes */
76 struct mvebu_image_info {
82 u32 image_size_to_hash;
83 u32 hash_algorithm_id;
84 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
87 u32 encrypt_start_offset;
92 /* Structure of the main header, version 1 (Armada 370/38x/XP) */
93 struct a38x_main_hdr_v1 {
96 u16 reserved2; /* 0x2-0x3 */
97 u32 blocksize; /* 0x4-0x7 */
99 u8 headersz_msb; /* 0x9 */
100 u16 headersz_lsb; /* 0xA-0xB */
101 u32 srcaddr; /* 0xC-0xF */
102 u32 destaddr; /* 0x10-0x13 */
103 u32 execaddr; /* 0x14-0x17 */
104 u8 options; /* 0x18 */
105 u8 nandblocksize; /* 0x19 */
106 u8 nandbadblklocation; /* 0x1A */
107 u8 reserved4; /* 0x1B */
108 u16 reserved5; /* 0x1C-0x1D */
110 u8 checksum; /* 0x1F */
113 struct a38x_boot_mode {
118 /* The blockid header field values used to indicate boot device of image */
119 struct a38x_boot_mode a38x_boot_modes[] = {
132 size_t (*read)(const char *file_name);
133 int (*write)(size_t image_size);
137 static ulong get_load_addr(void)
139 const char *addr_str;
142 addr_str = env_get("loadaddr");
144 addr = simple_strtoul(addr_str, NULL, 16);
146 addr = CONFIG_SYS_LOAD_ADDR;
151 /********************************************************************
153 ********************************************************************/
154 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
155 static int mmc_burn_image(size_t image_size)
162 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
164 struct blk_desc *blk_desc;
166 mmc = find_mmc_device(mmc_dev_num);
168 printf("No SD/MMC/eMMC card found\n");
174 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
179 /* SD reserves LBA-0 for MBR and boots from LBA-1,
180 * MMC/eMMC boots from LBA-0
182 start_lba = IS_SD(mmc) ? 1 : 0;
184 blk_count = image_size / mmc->write_bl_len;
185 if (image_size % mmc->write_bl_len)
188 blk_desc = mmc_get_blk_desc(mmc);
190 printf("Error - failed to obtain block descriptor\n");
193 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
194 (void *)get_load_addr());
196 blk_count = image_size / mmc->block_dev.blksz;
197 if (image_size % mmc->block_dev.blksz)
200 blk_written = mmc->block_dev.block_write(mmc_dev_num,
201 start_lba, blk_count,
202 (void *)get_load_addr());
203 #endif /* CONFIG_BLK */
204 if (blk_written != blk_count) {
205 printf("Error - written %#lx blocks\n", blk_written);
213 static size_t mmc_read_file(const char *file_name)
218 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
220 mmc = find_mmc_device(mmc_dev_num);
222 printf("No SD/MMC/eMMC card found\n");
227 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
232 /* Load from data partition (0) */
233 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
234 printf("Error: MMC 0 not found\n");
238 /* Perfrom file read */
239 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
246 static int is_mmc_active(void)
250 #else /* CONFIG_DM_MMC */
251 static int mmc_burn_image(size_t image_size)
256 static size_t mmc_read_file(const char *file_name)
261 static int is_mmc_active(void)
265 #endif /* CONFIG_DM_MMC */
267 /********************************************************************
269 ********************************************************************/
270 #ifdef CONFIG_SPI_FLASH
271 static int spi_burn_image(size_t image_size)
274 struct spi_flash *flash;
277 /* Probe the SPI bus to get the flash device */
278 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
280 CONFIG_SF_DEFAULT_SPEED,
281 CONFIG_SF_DEFAULT_MODE);
283 printf("Failed to probe SPI Flash\n");
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());
308 static int is_spi_active(void)
313 #else /* CONFIG_SPI_FLASH */
314 static int spi_burn_image(size_t image_size)
319 static int is_spi_active(void)
323 #endif /* CONFIG_SPI_FLASH */
325 /********************************************************************
327 ********************************************************************/
328 #ifdef CONFIG_CMD_NAND
329 static int nand_burn_image(size_t image_size)
333 struct mtd_info *mtd;
335 mtd = get_nand_dev_by_index(nand_curr_device);
337 puts("\nno devices available\n");
340 block_size = mtd->erasesize;
342 /* Align U-Boot size to currently used blocksize */
343 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
345 /* Erase the U-Boot image space */
346 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
347 ret = nand_erase(mtd, 0, image_size);
354 /* Write the image to flash */
355 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
356 (int)image_size, get_load_addr());
357 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
367 static int is_nand_active(void)
372 #else /* CONFIG_CMD_NAND */
373 static int nand_burn_image(size_t image_size)
378 static int is_nand_active(void)
382 #endif /* CONFIG_CMD_NAND */
384 /********************************************************************
386 ********************************************************************/
387 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
388 static size_t usb_read_file(const char *file_name)
396 if (usb_init() < 0) {
397 printf("Error: usb_init failed\n");
401 /* Try to recognize storage devices immediately */
402 blk_first_device(IF_TYPE_USB, &dev);
404 printf("Error: USB storage device not found\n");
408 /* Always load from usb 0 */
409 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
410 printf("Error: USB 0 not found\n");
414 /* Perfrom file read */
415 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
422 static int is_usb_active(void)
427 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
428 static size_t usb_read_file(const char *file_name)
433 static int is_usb_active(void)
437 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
439 /********************************************************************
441 ********************************************************************/
442 #ifdef CONFIG_CMD_NET
443 static size_t tftp_read_file(const char *file_name)
446 * update global variable image_load_addr before tftp file from network
448 image_load_addr = get_load_addr();
449 return net_loop(TFTPGET);
452 static int is_tftp_active(void)
458 static size_t tftp_read_file(const char *file_name)
463 static int is_tftp_active(void)
467 #endif /* CONFIG_CMD_NET */
479 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
480 {"tftp", tftp_read_file, NULL, is_tftp_active},
481 {"usb", usb_read_file, NULL, is_usb_active},
482 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
483 {"spi", NULL, spi_burn_image, is_spi_active},
484 {"nand", NULL, nand_burn_image, is_nand_active},
487 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
490 printf("Error: Write not supported on device %s\n", dst->name);
494 return dst->write(image_size);
497 #if defined(CONFIG_ARMADA_8K)
498 u32 do_checksum32(u32 *start, int32_t len)
512 static int check_image_header(void)
514 struct mvebu_image_header *hdr =
515 (struct mvebu_image_header *)get_load_addr();
516 u32 header_len = hdr->prolog_size;
518 u32 checksum_ref = hdr->prolog_checksum;
521 * For now compare checksum, and magic. Later we can
522 * verify more stuff on the header like interface type, etc
524 if (hdr->magic != MAIN_HDR_MAGIC) {
525 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
526 hdr->magic, MAIN_HDR_MAGIC);
530 /* The checksum value is discarded from checksum calculation */
531 hdr->prolog_checksum = 0;
533 checksum = do_checksum32((u32 *)hdr, header_len);
534 if (checksum != checksum_ref) {
535 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
536 checksum, checksum_ref);
540 /* Restore the checksum before writing */
541 hdr->prolog_checksum = checksum_ref;
542 printf("Image checksum...OK!\n");
546 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
547 static int check_image_header(void)
549 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
551 u8 hash_160_output[SHA1_SUM_LEN];
552 u8 hash_256_output[SHA256_SUM_LEN];
553 sha1_context hash1_text;
554 sha256_context hash256_text;
556 u32 hash_algorithm_id;
557 u32 image_size_to_hash;
558 u32 flash_entry_addr;
560 u32 internal_hash[HASH_SUM_LEN];
562 u32 num_of_image = hdr->num_images;
563 u32 version = hdr->version;
564 u32 trusted = hdr->trusted;
566 /* bubt checksum validation only supports nontrusted images */
568 printf("bypass image validation, ");
569 printf("only untrusted image is supported now\n");
572 /* only supports image version 3.5 and 3.6 */
573 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
574 printf("Error: Unsupported Image version = 0x%08x\n", version);
577 /* validate images hash value */
578 for (image_num = 0; image_num < num_of_image; image_num++) {
579 struct mvebu_image_info *info =
580 (struct mvebu_image_info *)(get_load_addr() +
581 sizeof(struct common_tim_data) +
582 image_num * sizeof(struct mvebu_image_info));
583 hash_algorithm_id = info->hash_algorithm_id;
584 image_size_to_hash = info->image_size_to_hash;
585 flash_entry_addr = info->flash_entry_addr;
586 hash_value = info->hash;
587 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
589 if (image_num == 0) {
591 * The first image includes hash values in its content.
592 * For hash calculation, we need to save the original
593 * hash values to a local variable that will be
594 * copied back for comparsion and set all zeros to
595 * the orignal hash values for calculating new value.
596 * First image original format :
597 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
598 * Replaced first image format :
599 * x...x (datum1) 0...0(hash values) x...x(datum2)
601 memcpy(internal_hash, hash_value,
602 sizeof(internal_hash));
603 memset(hash_value, 0, sizeof(internal_hash));
605 if (image_size_to_hash == 0) {
606 printf("Warning: Image_%d hash checksum is disabled, ",
608 printf("skip the image validation.\n");
611 switch (hash_algorithm_id) {
613 sha1_starts(&hash1_text);
614 sha1_update(&hash1_text, buff, image_size_to_hash);
615 sha1_finish(&hash1_text, hash_160_output);
616 hash_output = hash_160_output;
619 sha256_starts(&hash256_text);
620 sha256_update(&hash256_text, buff, image_size_to_hash);
621 sha256_finish(&hash256_text, hash_256_output);
622 hash_output = hash_256_output;
625 printf("Error: Unsupported hash_algorithm_id = %d\n",
630 memcpy(hash_value, internal_hash,
631 sizeof(internal_hash));
632 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
633 printf("Error: Image_%d checksum is not correct\n",
638 printf("Image checksum...OK!\n");
642 #elif defined(CONFIG_ARMADA_38X)
643 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
646 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
648 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
653 static uint8_t image_checksum8(const void *start, size_t len)
667 static int check_image_header(void)
670 const struct a38x_main_hdr_v1 *hdr =
671 (struct a38x_main_hdr_v1 *)get_load_addr();
672 const size_t image_size = a38x_header_size(hdr);
677 checksum = image_checksum8(hdr, image_size);
678 checksum -= hdr->checksum;
679 if (checksum != hdr->checksum) {
680 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
681 checksum, hdr->checksum);
685 printf("Image checksum...OK!\n");
688 #else /* Not ARMADA? */
689 static int check_image_header(void)
691 printf("bubt cmd does not support this SoC device or family!\n");
696 static int bubt_check_boot_mode(const struct bubt_dev *dst)
698 if (IS_ENABLED(CONFIG_ARMADA_38X)) {
700 const struct a38x_main_hdr_v1 *hdr =
701 (struct a38x_main_hdr_v1 *)get_load_addr();
703 for (mode = 0; mode < ARRAY_SIZE(a38x_boot_modes); mode++) {
704 if (strcmp(a38x_boot_modes[mode].name, dst->name) == 0)
708 if (a38x_boot_modes[mode].id == hdr->blockid)
711 for (int i = 0; i < ARRAY_SIZE(a38x_boot_modes); i++) {
712 if (a38x_boot_modes[i].id == hdr->blockid) {
713 printf("Error: A38x image meant to be booted from "
714 "\"%s\", not \"%s\"!\n",
715 a38x_boot_modes[i].name, dst->name);
720 printf("Error: unknown boot device in A38x image header: "
721 "0x%x\n", hdr->blockid);
728 static int bubt_verify(const struct bubt_dev *dst)
732 /* Check a correct image header exists */
733 err = check_image_header();
735 printf("Error: Image header verification failed\n");
739 err = bubt_check_boot_mode(dst);
741 printf("Error: Image boot mode verification failed\n");
748 static int bubt_read_file(struct bubt_dev *src)
753 printf("Error: Read not supported on device \"%s\"\n",
758 image_size = src->read(net_boot_file_name);
759 if (image_size <= 0) {
760 printf("Error: Failed to read file %s from %s\n",
761 net_boot_file_name, src->name);
768 static int bubt_is_dev_active(struct bubt_dev *dev)
771 printf("Device \"%s\" not supported by U-Boot image\n",
776 if (!dev->active()) {
777 printf("Device \"%s\" is inactive\n", dev->name);
784 struct bubt_dev *find_bubt_dev(char *dev_name)
788 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
789 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
790 return &bubt_devs[dev];
796 #define DEFAULT_BUBT_SRC "tftp"
798 #ifndef DEFAULT_BUBT_DST
799 #ifdef CONFIG_MVEBU_SPI_BOOT
800 #define DEFAULT_BUBT_DST "spi"
801 #elif defined(CONFIG_MVEBU_NAND_BOOT)
802 #define DEFAULT_BUBT_DST "nand"
803 #elif defined(CONFIG_MVEBU_MMC_BOOT)
804 #define DEFAULT_BUBT_DST "mmc"
806 #define DEFAULT_BUBT_DST "error"
808 #endif /* DEFAULT_BUBT_DST */
810 int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
812 struct bubt_dev *src, *dst;
814 char src_dev_name[8];
815 char dst_dev_name[8];
820 copy_filename(net_boot_file_name,
821 CONFIG_MVEBU_UBOOT_DFLT_NAME,
822 sizeof(net_boot_file_name));
824 copy_filename(net_boot_file_name, argv[1],
825 sizeof(net_boot_file_name));
828 strncpy(dst_dev_name, argv[2], 8);
830 name = DEFAULT_BUBT_DST;
831 strncpy(dst_dev_name, name, 8);
835 strncpy(src_dev_name, argv[3], 8);
837 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
839 /* Figure out the destination device */
840 dst = find_bubt_dev(dst_dev_name);
842 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
846 if (!bubt_is_dev_active(dst))
849 /* Figure out the source device */
850 src = find_bubt_dev(src_dev_name);
852 printf("Error: Unknown source \"%s\"\n", src_dev_name);
856 if (!bubt_is_dev_active(src))
859 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
860 net_boot_file_name, src->name, dst->name);
862 image_size = bubt_read_file(src);
866 err = bubt_verify(dst);
870 err = bubt_write_file(dst, image_size);
878 bubt, 4, 0, do_bubt_cmd,
879 "Burn a u-boot image to flash",
880 "[file-name] [destination [source]]\n"
881 "\t-file-name The image file name to burn. Default = flash-image.bin\n"
882 "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
883 "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
885 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
886 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
887 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"