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 #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 common_tim_data {
65 u32 reserved[5]; /* Reserve 20 bytes */
72 struct mvebu_image_info {
78 u32 image_size_to_hash;
79 u32 hash_algorithm_id;
80 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
83 u32 encrypt_start_offset;
88 /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
89 struct a38x_main_hdr_v1 {
92 u16 nandpagesize; /* 0x2-0x3 */
93 u32 blocksize; /* 0x4-0x7 */
95 u8 headersz_msb; /* 0x9 */
96 u16 headersz_lsb; /* 0xA-0xB */
97 u32 srcaddr; /* 0xC-0xF */
98 u32 destaddr; /* 0x10-0x13 */
99 u32 execaddr; /* 0x14-0x17 */
100 u8 options; /* 0x18 */
101 u8 nandblocksize; /* 0x19 */
102 u8 nandbadblklocation; /* 0x1A */
103 u8 reserved4; /* 0x1B */
104 u16 reserved5; /* 0x1C-0x1D */
106 u8 checksum; /* 0x1F */
109 struct a38x_boot_mode {
114 /* The blockid header field values used to indicate boot device of image */
115 struct a38x_boot_mode a38x_boot_modes[] = {
128 size_t (*read)(const char *file_name);
129 int (*write)(size_t image_size);
133 static ulong get_load_addr(void)
135 const char *addr_str;
138 addr_str = env_get("loadaddr");
140 addr = hextoul(addr_str, NULL);
142 addr = CONFIG_SYS_LOAD_ADDR;
147 /********************************************************************
149 ********************************************************************/
150 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
151 static int mmc_burn_image(size_t image_size)
158 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
160 struct blk_desc *blk_desc;
162 mmc = find_mmc_device(mmc_dev_num);
164 printf("No SD/MMC/eMMC card found\n");
170 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
175 /* SD reserves LBA-0 for MBR and boots from LBA-1,
176 * MMC/eMMC boots from LBA-0
178 start_lba = IS_SD(mmc) ? 1 : 0;
180 blk_count = image_size / mmc->write_bl_len;
181 if (image_size % mmc->write_bl_len)
184 blk_desc = mmc_get_blk_desc(mmc);
186 printf("Error - failed to obtain block descriptor\n");
189 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
190 (void *)get_load_addr());
192 blk_count = image_size / mmc->block_dev.blksz;
193 if (image_size % mmc->block_dev.blksz)
196 blk_written = mmc->block_dev.block_write(mmc_dev_num,
197 start_lba, blk_count,
198 (void *)get_load_addr());
199 #endif /* CONFIG_BLK */
200 if (blk_written != blk_count) {
201 printf("Error - written %#lx blocks\n", blk_written);
209 static size_t mmc_read_file(const char *file_name)
214 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
216 mmc = find_mmc_device(mmc_dev_num);
218 printf("No SD/MMC/eMMC card found\n");
223 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
228 /* Load from data partition (0) */
229 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
230 printf("Error: MMC 0 not found\n");
234 /* Perfrom file read */
235 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
242 static int is_mmc_active(void)
246 #else /* CONFIG_DM_MMC */
247 static int mmc_burn_image(size_t image_size)
252 static size_t mmc_read_file(const char *file_name)
257 static int is_mmc_active(void)
261 #endif /* CONFIG_DM_MMC */
263 /********************************************************************
265 ********************************************************************/
266 #ifdef CONFIG_SPI_FLASH
267 static int spi_burn_image(size_t image_size)
270 struct spi_flash *flash;
273 /* Probe the SPI bus to get the flash device */
274 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
275 CONFIG_SF_DEFAULT_CS,
276 CONFIG_SF_DEFAULT_SPEED,
277 CONFIG_SF_DEFAULT_MODE);
279 printf("Failed to probe SPI Flash\n");
283 erase_bytes = image_size +
284 (flash->erase_size - image_size % flash->erase_size);
285 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
286 erase_bytes, erase_bytes / flash->erase_size);
287 ret = spi_flash_erase(flash, 0, erase_bytes);
293 printf("Writing %d bytes from 0x%lx to offset 0 ...",
294 (int)image_size, get_load_addr());
295 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
304 static int is_spi_active(void)
309 #else /* CONFIG_SPI_FLASH */
310 static int spi_burn_image(size_t image_size)
315 static int is_spi_active(void)
319 #endif /* CONFIG_SPI_FLASH */
321 /********************************************************************
323 ********************************************************************/
324 #ifdef CONFIG_CMD_NAND
325 static int nand_burn_image(size_t image_size)
329 struct mtd_info *mtd;
331 mtd = get_nand_dev_by_index(nand_curr_device);
333 puts("\nno devices available\n");
336 block_size = mtd->erasesize;
338 /* Align U-Boot size to currently used blocksize */
339 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
341 /* Erase the U-Boot image space */
342 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
343 ret = nand_erase(mtd, 0, image_size);
350 /* Write the image to flash */
351 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
352 (int)image_size, get_load_addr());
353 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
363 static int is_nand_active(void)
368 #else /* CONFIG_CMD_NAND */
369 static int nand_burn_image(size_t image_size)
374 static int is_nand_active(void)
378 #endif /* CONFIG_CMD_NAND */
380 /********************************************************************
382 ********************************************************************/
383 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
384 static size_t usb_read_file(const char *file_name)
392 if (usb_init() < 0) {
393 printf("Error: usb_init failed\n");
397 /* Try to recognize storage devices immediately */
398 blk_first_device(IF_TYPE_USB, &dev);
400 printf("Error: USB storage device not found\n");
404 /* Always load from usb 0 */
405 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
406 printf("Error: USB 0 not found\n");
410 /* Perfrom file read */
411 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
418 static int is_usb_active(void)
423 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
424 static size_t usb_read_file(const char *file_name)
429 static int is_usb_active(void)
433 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
435 /********************************************************************
437 ********************************************************************/
438 #ifdef CONFIG_CMD_NET
439 static size_t tftp_read_file(const char *file_name)
442 * update global variable image_load_addr before tftp file from network
444 image_load_addr = get_load_addr();
445 return net_loop(TFTPGET);
448 static int is_tftp_active(void)
454 static size_t tftp_read_file(const char *file_name)
459 static int is_tftp_active(void)
463 #endif /* CONFIG_CMD_NET */
475 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
476 {"tftp", tftp_read_file, NULL, is_tftp_active},
477 {"usb", usb_read_file, NULL, is_usb_active},
478 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
479 {"spi", NULL, spi_burn_image, is_spi_active},
480 {"nand", NULL, nand_burn_image, is_nand_active},
483 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
486 printf("Error: Write not supported on device %s\n", dst->name);
490 return dst->write(image_size);
493 #if defined(CONFIG_ARMADA_8K)
494 u32 do_checksum32(u32 *start, int32_t len)
508 static int check_image_header(void)
510 struct mvebu_image_header *hdr =
511 (struct mvebu_image_header *)get_load_addr();
512 u32 header_len = hdr->prolog_size;
514 u32 checksum_ref = hdr->prolog_checksum;
517 * For now compare checksum, and magic. Later we can
518 * verify more stuff on the header like interface type, etc
520 if (hdr->magic != MAIN_HDR_MAGIC) {
521 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
522 hdr->magic, MAIN_HDR_MAGIC);
526 /* The checksum value is discarded from checksum calculation */
527 hdr->prolog_checksum = 0;
529 checksum = do_checksum32((u32 *)hdr, header_len);
530 if (checksum != checksum_ref) {
531 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
532 checksum, checksum_ref);
536 /* Restore the checksum before writing */
537 hdr->prolog_checksum = checksum_ref;
538 printf("Image checksum...OK!\n");
542 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
543 static int check_image_header(void)
545 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
547 u8 hash_160_output[SHA1_SUM_LEN];
548 u8 hash_256_output[SHA256_SUM_LEN];
549 sha1_context hash1_text;
550 sha256_context hash256_text;
552 u32 hash_algorithm_id;
553 u32 image_size_to_hash;
554 u32 flash_entry_addr;
556 u32 internal_hash[HASH_SUM_LEN];
558 u32 num_of_image = hdr->num_images;
559 u32 version = hdr->version;
560 u32 trusted = hdr->trusted;
562 /* bubt checksum validation only supports nontrusted images */
564 printf("bypass image validation, ");
565 printf("only untrusted image is supported now\n");
568 /* only supports image version 3.5 and 3.6 */
569 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
570 printf("Error: Unsupported Image version = 0x%08x\n", version);
573 /* validate images hash value */
574 for (image_num = 0; image_num < num_of_image; image_num++) {
575 struct mvebu_image_info *info =
576 (struct mvebu_image_info *)(get_load_addr() +
577 sizeof(struct common_tim_data) +
578 image_num * sizeof(struct mvebu_image_info));
579 hash_algorithm_id = info->hash_algorithm_id;
580 image_size_to_hash = info->image_size_to_hash;
581 flash_entry_addr = info->flash_entry_addr;
582 hash_value = info->hash;
583 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
585 if (image_num == 0) {
587 * The first image includes hash values in its content.
588 * For hash calculation, we need to save the original
589 * hash values to a local variable that will be
590 * copied back for comparsion and set all zeros to
591 * the orignal hash values for calculating new value.
592 * First image original format :
593 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
594 * Replaced first image format :
595 * x...x (datum1) 0...0(hash values) x...x(datum2)
597 memcpy(internal_hash, hash_value,
598 sizeof(internal_hash));
599 memset(hash_value, 0, sizeof(internal_hash));
601 if (image_size_to_hash == 0) {
602 printf("Warning: Image_%d hash checksum is disabled, ",
604 printf("skip the image validation.\n");
607 switch (hash_algorithm_id) {
609 sha1_starts(&hash1_text);
610 sha1_update(&hash1_text, buff, image_size_to_hash);
611 sha1_finish(&hash1_text, hash_160_output);
612 hash_output = hash_160_output;
615 sha256_starts(&hash256_text);
616 sha256_update(&hash256_text, buff, image_size_to_hash);
617 sha256_finish(&hash256_text, hash_256_output);
618 hash_output = hash_256_output;
621 printf("Error: Unsupported hash_algorithm_id = %d\n",
626 memcpy(hash_value, internal_hash,
627 sizeof(internal_hash));
628 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
629 printf("Error: Image_%d checksum is not correct\n",
634 printf("Image checksum...OK!\n");
638 #elif defined(CONFIG_ARMADA_38X)
639 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
642 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
644 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
649 static uint8_t image_checksum8(const void *start, size_t len)
663 static int check_image_header(void)
666 const struct a38x_main_hdr_v1 *hdr =
667 (struct a38x_main_hdr_v1 *)get_load_addr();
668 const size_t image_size = a38x_header_size(hdr);
673 checksum = image_checksum8(hdr, image_size);
674 checksum -= hdr->checksum;
675 if (checksum != hdr->checksum) {
676 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
677 checksum, hdr->checksum);
681 printf("Image checksum...OK!\n");
684 #else /* Not ARMADA? */
685 static int check_image_header(void)
687 printf("bubt cmd does not support this SoC device or family!\n");
692 static int bubt_check_boot_mode(const struct bubt_dev *dst)
694 if (IS_ENABLED(CONFIG_ARMADA_38X)) {
696 const struct a38x_main_hdr_v1 *hdr =
697 (struct a38x_main_hdr_v1 *)get_load_addr();
699 for (mode = 0; mode < ARRAY_SIZE(a38x_boot_modes); mode++) {
700 if (strcmp(a38x_boot_modes[mode].name, dst->name) == 0)
704 if (a38x_boot_modes[mode].id == hdr->blockid)
707 for (int i = 0; i < ARRAY_SIZE(a38x_boot_modes); i++) {
708 if (a38x_boot_modes[i].id == hdr->blockid) {
709 printf("Error: A38x image meant to be booted from "
710 "\"%s\", not \"%s\"!\n",
711 a38x_boot_modes[i].name, dst->name);
716 printf("Error: unknown boot device in A38x image header: "
717 "0x%x\n", hdr->blockid);
724 static int bubt_verify(const struct bubt_dev *dst)
728 /* Check a correct image header exists */
729 err = check_image_header();
731 printf("Error: Image header verification failed\n");
735 err = bubt_check_boot_mode(dst);
737 printf("Error: Image boot mode verification failed\n");
744 static int bubt_read_file(struct bubt_dev *src)
749 printf("Error: Read not supported on device \"%s\"\n",
754 image_size = src->read(net_boot_file_name);
755 if (image_size <= 0) {
756 printf("Error: Failed to read file %s from %s\n",
757 net_boot_file_name, src->name);
764 static int bubt_is_dev_active(struct bubt_dev *dev)
767 printf("Device \"%s\" not supported by U-Boot image\n",
772 if (!dev->active()) {
773 printf("Device \"%s\" is inactive\n", dev->name);
780 struct bubt_dev *find_bubt_dev(char *dev_name)
784 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
785 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
786 return &bubt_devs[dev];
792 #define DEFAULT_BUBT_SRC "tftp"
794 #ifndef DEFAULT_BUBT_DST
795 #ifdef CONFIG_MVEBU_SPI_BOOT
796 #define DEFAULT_BUBT_DST "spi"
797 #elif defined(CONFIG_MVEBU_NAND_BOOT)
798 #define DEFAULT_BUBT_DST "nand"
799 #elif defined(CONFIG_MVEBU_MMC_BOOT)
800 #define DEFAULT_BUBT_DST "mmc"
802 #define DEFAULT_BUBT_DST "error"
804 #endif /* DEFAULT_BUBT_DST */
806 int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
808 struct bubt_dev *src, *dst;
810 char src_dev_name[8];
811 char dst_dev_name[8];
816 copy_filename(net_boot_file_name,
817 CONFIG_MVEBU_UBOOT_DFLT_NAME,
818 sizeof(net_boot_file_name));
820 copy_filename(net_boot_file_name, argv[1],
821 sizeof(net_boot_file_name));
824 strncpy(dst_dev_name, argv[2], 8);
826 name = DEFAULT_BUBT_DST;
827 strncpy(dst_dev_name, name, 8);
831 strncpy(src_dev_name, argv[3], 8);
833 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
835 /* Figure out the destination device */
836 dst = find_bubt_dev(dst_dev_name);
838 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
842 if (!bubt_is_dev_active(dst))
845 /* Figure out the source device */
846 src = find_bubt_dev(src_dev_name);
848 printf("Error: Unknown source \"%s\"\n", src_dev_name);
852 if (!bubt_is_dev_active(src))
855 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
856 net_boot_file_name, src->name, dst->name);
858 image_size = bubt_read_file(src);
862 err = bubt_verify(dst);
866 err = bubt_write_file(dst, image_size);
874 bubt, 4, 0, do_bubt_cmd,
875 "Burn a u-boot image to flash",
876 "[file-name] [destination [source]]\n"
877 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
878 "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
879 "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
881 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
882 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
883 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"