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 #endif /* CONFIG_ARMADA_XXX */
92 size_t (*read)(const char *file_name);
93 int (*write)(size_t image_size);
97 static ulong get_load_addr(void)
102 addr_str = env_get("loadaddr");
104 addr = simple_strtoul(addr_str, NULL, 16);
106 addr = CONFIG_SYS_LOAD_ADDR;
111 /********************************************************************
113 ********************************************************************/
114 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
115 static int mmc_burn_image(size_t image_size)
122 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
124 struct blk_desc *blk_desc;
126 mmc = find_mmc_device(mmc_dev_num);
128 printf("No SD/MMC/eMMC card found\n");
134 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
139 #ifdef CONFIG_SYS_MMC_ENV_PART
140 if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) {
141 err = mmc_switch_part(mmc_dev_num, CONFIG_SYS_MMC_ENV_PART);
143 printf("MMC partition switch failed\n");
149 /* SD reserves LBA-0 for MBR and boots from LBA-1,
150 * MMC/eMMC boots from LBA-0
152 start_lba = IS_SD(mmc) ? 1 : 0;
154 blk_count = image_size / mmc->write_bl_len;
155 if (image_size % mmc->write_bl_len)
158 blk_desc = mmc_get_blk_desc(mmc);
160 printf("Error - failed to obtain block descriptor\n");
163 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
164 (void *)get_load_addr());
166 blk_count = image_size / mmc->block_dev.blksz;
167 if (image_size % mmc->block_dev.blksz)
170 blk_written = mmc->block_dev.block_write(mmc_dev_num,
171 start_lba, blk_count,
172 (void *)get_load_addr());
173 #endif /* CONFIG_BLK */
174 if (blk_written != blk_count) {
175 printf("Error - written %#lx blocks\n", blk_written);
180 #ifdef CONFIG_SYS_MMC_ENV_PART
181 if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART)
182 mmc_switch_part(mmc_dev_num, mmc->part_num);
188 static size_t mmc_read_file(const char *file_name)
193 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
195 mmc = find_mmc_device(mmc_dev_num);
197 printf("No SD/MMC/eMMC card found\n");
202 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
207 /* Load from data partition (0) */
208 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
209 printf("Error: MMC 0 not found\n");
213 /* Perfrom file read */
214 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
221 static int is_mmc_active(void)
225 #else /* CONFIG_DM_MMC */
226 static int mmc_burn_image(size_t image_size)
231 static size_t mmc_read_file(const char *file_name)
236 static int is_mmc_active(void)
240 #endif /* CONFIG_DM_MMC */
242 /********************************************************************
244 ********************************************************************/
245 #ifdef CONFIG_SPI_FLASH
246 static int spi_burn_image(size_t image_size)
249 struct spi_flash *flash;
252 /* Probe the SPI bus to get the flash device */
253 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
255 CONFIG_SF_DEFAULT_SPEED,
256 CONFIG_SF_DEFAULT_MODE);
258 printf("Failed to probe SPI Flash\n");
262 #ifdef CONFIG_SPI_FLASH_PROTECTION
263 spi_flash_protect(flash, 0);
265 erase_bytes = image_size +
266 (flash->erase_size - image_size % flash->erase_size);
267 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
268 erase_bytes, erase_bytes / flash->erase_size);
269 ret = spi_flash_erase(flash, 0, erase_bytes);
275 printf("Writing %d bytes from 0x%lx to offset 0 ...",
276 (int)image_size, get_load_addr());
277 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
283 #ifdef CONFIG_SPI_FLASH_PROTECTION
284 spi_flash_protect(flash, 1);
290 static int is_spi_active(void)
295 #else /* CONFIG_SPI_FLASH */
296 static int spi_burn_image(size_t image_size)
301 static int is_spi_active(void)
305 #endif /* CONFIG_SPI_FLASH */
307 /********************************************************************
309 ********************************************************************/
310 #ifdef CONFIG_CMD_NAND
311 static int nand_burn_image(size_t image_size)
315 struct mtd_info *mtd;
317 mtd = get_nand_dev_by_index(nand_curr_device);
319 puts("\nno devices available\n");
322 block_size = mtd->erasesize;
324 /* Align U-Boot size to currently used blocksize */
325 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
327 /* Erase the U-BOOT image space */
328 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
329 ret = nand_erase(mtd, 0, image_size);
336 /* Write the image to flash */
337 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
338 (int)image_size, get_load_addr());
339 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
349 static int is_nand_active(void)
354 #else /* CONFIG_CMD_NAND */
355 static int nand_burn_image(size_t image_size)
360 static int is_nand_active(void)
364 #endif /* CONFIG_CMD_NAND */
366 /********************************************************************
368 ********************************************************************/
369 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
370 static size_t usb_read_file(const char *file_name)
378 if (usb_init() < 0) {
379 printf("Error: usb_init failed\n");
383 /* Try to recognize storage devices immediately */
384 blk_first_device(IF_TYPE_USB, &dev);
386 printf("Error: USB storage device not found\n");
390 /* Always load from usb 0 */
391 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
392 printf("Error: USB 0 not found\n");
396 /* Perfrom file read */
397 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
404 static int is_usb_active(void)
409 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
410 static size_t usb_read_file(const char *file_name)
415 static int is_usb_active(void)
419 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
421 /********************************************************************
423 ********************************************************************/
424 #ifdef CONFIG_CMD_NET
425 static size_t tftp_read_file(const char *file_name)
428 * update global variable image_load_addr before tftp file from network
430 image_load_addr = get_load_addr();
431 return net_loop(TFTPGET);
434 static int is_tftp_active(void)
440 static size_t tftp_read_file(const char *file_name)
445 static int is_tftp_active(void)
449 #endif /* CONFIG_CMD_NET */
461 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
462 {"tftp", tftp_read_file, NULL, is_tftp_active},
463 {"usb", usb_read_file, NULL, is_usb_active},
464 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
465 {"spi", NULL, spi_burn_image, is_spi_active},
466 {"nand", NULL, nand_burn_image, is_nand_active},
469 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
472 printf("Error: Write not supported on device %s\n", dst->name);
476 return dst->write(image_size);
479 #if defined(CONFIG_ARMADA_8K)
480 u32 do_checksum32(u32 *start, int32_t len)
494 static int check_image_header(void)
496 struct mvebu_image_header *hdr =
497 (struct mvebu_image_header *)get_load_addr();
498 u32 header_len = hdr->prolog_size;
500 u32 checksum_ref = hdr->prolog_checksum;
503 * For now compare checksum, and magic. Later we can
504 * verify more stuff on the header like interface type, etc
506 if (hdr->magic != MAIN_HDR_MAGIC) {
507 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
508 hdr->magic, MAIN_HDR_MAGIC);
512 /* The checksum value is discarded from checksum calculation */
513 hdr->prolog_checksum = 0;
515 checksum = do_checksum32((u32 *)hdr, header_len);
516 if (checksum != checksum_ref) {
517 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
518 checksum, checksum_ref);
522 /* Restore the checksum before writing */
523 hdr->prolog_checksum = checksum_ref;
524 printf("Image checksum...OK!\n");
528 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
529 static int check_image_header(void)
531 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
533 u8 hash_160_output[SHA1_SUM_LEN];
534 u8 hash_256_output[SHA256_SUM_LEN];
535 sha1_context hash1_text;
536 sha256_context hash256_text;
538 u32 hash_algorithm_id;
539 u32 image_size_to_hash;
540 u32 flash_entry_addr;
542 u32 internal_hash[HASH_SUM_LEN];
544 u32 num_of_image = hdr->num_images;
545 u32 version = hdr->version;
546 u32 trusted = hdr->trusted;
548 /* bubt checksum validation only supports nontrusted images */
550 printf("bypass image validation, ");
551 printf("only untrusted image is supported now\n");
554 /* only supports image version 3.5 and 3.6 */
555 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
556 printf("Error: Unsupported Image version = 0x%08x\n", version);
559 /* validate images hash value */
560 for (image_num = 0; image_num < num_of_image; image_num++) {
561 struct mvebu_image_info *info =
562 (struct mvebu_image_info *)(get_load_addr() +
563 sizeof(struct common_tim_data) +
564 image_num * sizeof(struct mvebu_image_info));
565 hash_algorithm_id = info->hash_algorithm_id;
566 image_size_to_hash = info->image_size_to_hash;
567 flash_entry_addr = info->flash_entry_addr;
568 hash_value = info->hash;
569 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
571 if (image_num == 0) {
573 * The first image includes hash values in its content.
574 * For hash calculation, we need to save the original
575 * hash values to a local variable that will be
576 * copied back for comparsion and set all zeros to
577 * the orignal hash values for calculating new value.
578 * First image original format :
579 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
580 * Replaced first image format :
581 * x...x (datum1) 0...0(hash values) x...x(datum2)
583 memcpy(internal_hash, hash_value,
584 sizeof(internal_hash));
585 memset(hash_value, 0, sizeof(internal_hash));
587 if (image_size_to_hash == 0) {
588 printf("Warning: Image_%d hash checksum is disabled, ",
590 printf("skip the image validation.\n");
593 switch (hash_algorithm_id) {
595 sha1_starts(&hash1_text);
596 sha1_update(&hash1_text, buff, image_size_to_hash);
597 sha1_finish(&hash1_text, hash_160_output);
598 hash_output = hash_160_output;
601 sha256_starts(&hash256_text);
602 sha256_update(&hash256_text, buff, image_size_to_hash);
603 sha256_finish(&hash256_text, hash_256_output);
604 hash_output = hash_256_output;
607 printf("Error: Unsupported hash_algorithm_id = %d\n",
612 memcpy(hash_value, internal_hash,
613 sizeof(internal_hash));
614 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
615 printf("Error: Image_%d checksum is not correct\n",
620 printf("Image checksum...OK!\n");
625 #else /* Not ARMADA? */
626 static int check_image_header(void)
628 printf("bubt cmd does not support this SoC device or family!\n");
633 static int bubt_verify(size_t image_size)
637 /* Check a correct image header exists */
638 err = check_image_header();
640 printf("Error: Image header verification failed\n");
647 static int bubt_read_file(struct bubt_dev *src)
652 printf("Error: Read not supported on device \"%s\"\n",
657 image_size = src->read(net_boot_file_name);
658 if (image_size <= 0) {
659 printf("Error: Failed to read file %s from %s\n",
660 net_boot_file_name, src->name);
667 static int bubt_is_dev_active(struct bubt_dev *dev)
670 printf("Device \"%s\" not supported by U-BOOT image\n",
675 if (!dev->active()) {
676 printf("Device \"%s\" is inactive\n", dev->name);
683 struct bubt_dev *find_bubt_dev(char *dev_name)
687 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
688 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
689 return &bubt_devs[dev];
695 #define DEFAULT_BUBT_SRC "tftp"
697 #ifndef DEFAULT_BUBT_DST
698 #ifdef CONFIG_MVEBU_SPI_BOOT
699 #define DEFAULT_BUBT_DST "spi"
700 #elif defined(CONFIG_MVEBU_NAND_BOOT)
701 #define DEFAULT_BUBT_DST "nand"
702 #elif defined(CONFIG_MVEBU_MMC_BOOT)
703 #define DEFAULT_BUBT_DST "mmc"
705 #define DEFAULT_BUBT_DST "error"
707 #endif /* DEFAULT_BUBT_DST */
709 int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
711 struct bubt_dev *src, *dst;
713 char src_dev_name[8];
714 char dst_dev_name[8];
719 copy_filename(net_boot_file_name,
720 CONFIG_MVEBU_UBOOT_DFLT_NAME,
721 sizeof(net_boot_file_name));
723 copy_filename(net_boot_file_name, argv[1],
724 sizeof(net_boot_file_name));
727 strncpy(dst_dev_name, argv[2], 8);
729 name = DEFAULT_BUBT_DST;
730 strncpy(dst_dev_name, name, 8);
734 strncpy(src_dev_name, argv[3], 8);
736 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
738 /* Figure out the destination device */
739 dst = find_bubt_dev(dst_dev_name);
741 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
745 if (!bubt_is_dev_active(dst))
748 /* Figure out the source device */
749 src = find_bubt_dev(src_dev_name);
751 printf("Error: Unknown source \"%s\"\n", src_dev_name);
755 if (!bubt_is_dev_active(src))
758 printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
759 net_boot_file_name, src->name, dst->name);
761 image_size = bubt_read_file(src);
765 err = bubt_verify(image_size);
769 err = bubt_write_file(dst, image_size);
777 bubt, 4, 0, do_bubt_cmd,
778 "Burn a u-boot image to flash",
779 "[file-name] [destination [source]]\n"
780 "\t-file-name The image file name to burn. Default = flash-image.bin\n"
781 "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
782 "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
784 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
785 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
786 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"