1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
14 #include <spi_flash.h>
23 #include <u-boot/sha1.h>
24 #include <u-boot/sha256.h>
26 #ifndef CONFIG_SYS_MMC_ENV_DEV
27 #define CONFIG_SYS_MMC_ENV_DEV 0
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;
86 #endif /* CONFIG_ARMADA_XXX */
90 size_t (*read)(const char *file_name);
91 int (*write)(size_t image_size);
95 static ulong get_load_addr(void)
100 addr_str = env_get("loadaddr");
102 addr = simple_strtoul(addr_str, NULL, 16);
104 addr = CONFIG_SYS_LOAD_ADDR;
109 /********************************************************************
111 ********************************************************************/
112 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
113 static int mmc_burn_image(size_t image_size)
120 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
122 struct blk_desc *blk_desc;
124 mmc = find_mmc_device(mmc_dev_num);
126 printf("No SD/MMC/eMMC card found\n");
132 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
137 #ifdef CONFIG_SYS_MMC_ENV_PART
138 if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) {
139 err = mmc_switch_part(mmc_dev_num, CONFIG_SYS_MMC_ENV_PART);
141 printf("MMC partition switch failed\n");
147 /* SD reserves LBA-0 for MBR and boots from LBA-1,
148 * MMC/eMMC boots from LBA-0
150 start_lba = IS_SD(mmc) ? 1 : 0;
152 blk_count = image_size / mmc->write_bl_len;
153 if (image_size % mmc->write_bl_len)
156 blk_desc = mmc_get_blk_desc(mmc);
158 printf("Error - failed to obtain block descriptor\n");
161 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
162 (void *)get_load_addr());
164 blk_count = image_size / mmc->block_dev.blksz;
165 if (image_size % mmc->block_dev.blksz)
168 blk_written = mmc->block_dev.block_write(mmc_dev_num,
169 start_lba, blk_count,
170 (void *)get_load_addr());
171 #endif /* CONFIG_BLK */
172 if (blk_written != blk_count) {
173 printf("Error - written %#lx blocks\n", blk_written);
178 #ifdef CONFIG_SYS_MMC_ENV_PART
179 if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART)
180 mmc_switch_part(mmc_dev_num, mmc->part_num);
186 static size_t mmc_read_file(const char *file_name)
191 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
193 mmc = find_mmc_device(mmc_dev_num);
195 printf("No SD/MMC/eMMC card found\n");
200 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
205 /* Load from data partition (0) */
206 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
207 printf("Error: MMC 0 not found\n");
211 /* Perfrom file read */
212 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
219 static int is_mmc_active(void)
223 #else /* CONFIG_DM_MMC */
224 static int mmc_burn_image(size_t image_size)
229 static size_t mmc_read_file(const char *file_name)
234 static int is_mmc_active(void)
238 #endif /* CONFIG_DM_MMC */
240 /********************************************************************
242 ********************************************************************/
243 #ifdef CONFIG_SPI_FLASH
244 static int spi_burn_image(size_t image_size)
247 struct spi_flash *flash;
250 /* Probe the SPI bus to get the flash device */
251 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
253 CONFIG_SF_DEFAULT_SPEED,
254 CONFIG_SF_DEFAULT_MODE);
256 printf("Failed to probe SPI Flash\n");
260 #ifdef CONFIG_SPI_FLASH_PROTECTION
261 spi_flash_protect(flash, 0);
263 erase_bytes = image_size +
264 (flash->erase_size - image_size % flash->erase_size);
265 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
266 erase_bytes, erase_bytes / flash->erase_size);
267 ret = spi_flash_erase(flash, 0, erase_bytes);
273 printf("Writing %d bytes from 0x%lx to offset 0 ...",
274 (int)image_size, get_load_addr());
275 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
281 #ifdef CONFIG_SPI_FLASH_PROTECTION
282 spi_flash_protect(flash, 1);
288 static int is_spi_active(void)
293 #else /* CONFIG_SPI_FLASH */
294 static int spi_burn_image(size_t image_size)
299 static int is_spi_active(void)
303 #endif /* CONFIG_SPI_FLASH */
305 /********************************************************************
307 ********************************************************************/
308 #ifdef CONFIG_CMD_NAND
309 static int nand_burn_image(size_t image_size)
313 struct mtd_info *mtd;
315 mtd = get_nand_dev_by_index(nand_curr_device);
317 puts("\nno devices available\n");
320 block_size = mtd->erasesize;
322 /* Align U-Boot size to currently used blocksize */
323 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
325 /* Erase the U-BOOT image space */
326 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
327 ret = nand_erase(mtd, 0, image_size);
334 /* Write the image to flash */
335 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
336 (int)image_size, get_load_addr());
337 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
347 static int is_nand_active(void)
352 #else /* CONFIG_CMD_NAND */
353 static int nand_burn_image(size_t image_size)
358 static int is_nand_active(void)
362 #endif /* CONFIG_CMD_NAND */
364 /********************************************************************
366 ********************************************************************/
367 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
368 static size_t usb_read_file(const char *file_name)
376 if (usb_init() < 0) {
377 printf("Error: usb_init failed\n");
381 /* Try to recognize storage devices immediately */
382 blk_first_device(IF_TYPE_USB, &dev);
384 printf("Error: USB storage device not found\n");
388 /* Always load from usb 0 */
389 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
390 printf("Error: USB 0 not found\n");
394 /* Perfrom file read */
395 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
402 static int is_usb_active(void)
407 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
408 static size_t usb_read_file(const char *file_name)
413 static int is_usb_active(void)
417 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
419 /********************************************************************
421 ********************************************************************/
422 #ifdef CONFIG_CMD_NET
423 static size_t tftp_read_file(const char *file_name)
425 /* update global variable load_addr before tftp file from network */
426 load_addr = get_load_addr();
427 return net_loop(TFTPGET);
430 static int is_tftp_active(void)
436 static size_t tftp_read_file(const char *file_name)
441 static int is_tftp_active(void)
445 #endif /* CONFIG_CMD_NET */
457 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
458 {"tftp", tftp_read_file, NULL, is_tftp_active},
459 {"usb", usb_read_file, NULL, is_usb_active},
460 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
461 {"spi", NULL, spi_burn_image, is_spi_active},
462 {"nand", NULL, nand_burn_image, is_nand_active},
465 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
468 printf("Error: Write not supported on device %s\n", dst->name);
472 return dst->write(image_size);
475 #if defined(CONFIG_ARMADA_8K)
476 u32 do_checksum32(u32 *start, int32_t len)
490 static int check_image_header(void)
492 struct mvebu_image_header *hdr =
493 (struct mvebu_image_header *)get_load_addr();
494 u32 header_len = hdr->prolog_size;
496 u32 checksum_ref = hdr->prolog_checksum;
499 * For now compare checksum, and magic. Later we can
500 * verify more stuff on the header like interface type, etc
502 if (hdr->magic != MAIN_HDR_MAGIC) {
503 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
504 hdr->magic, MAIN_HDR_MAGIC);
508 /* The checksum value is discarded from checksum calculation */
509 hdr->prolog_checksum = 0;
511 checksum = do_checksum32((u32 *)hdr, header_len);
512 if (checksum != checksum_ref) {
513 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
514 checksum, checksum_ref);
518 /* Restore the checksum before writing */
519 hdr->prolog_checksum = checksum_ref;
520 printf("Image checksum...OK!\n");
524 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
525 static int check_image_header(void)
527 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
529 u8 hash_160_output[SHA1_SUM_LEN];
530 u8 hash_256_output[SHA256_SUM_LEN];
531 sha1_context hash1_text;
532 sha256_context hash256_text;
534 u32 hash_algorithm_id;
535 u32 image_size_to_hash;
536 u32 flash_entry_addr;
538 u32 internal_hash[HASH_SUM_LEN];
540 u32 num_of_image = hdr->num_images;
541 u32 version = hdr->version;
542 u32 trusted = hdr->trusted;
544 /* bubt checksum validation only supports nontrusted images */
546 printf("bypass image validation, ");
547 printf("only untrusted image is supported now\n");
550 /* only supports image version 3.5 and 3.6 */
551 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
552 printf("Error: Unsupported Image version = 0x%08x\n", version);
555 /* validate images hash value */
556 for (image_num = 0; image_num < num_of_image; image_num++) {
557 struct mvebu_image_info *info =
558 (struct mvebu_image_info *)(get_load_addr() +
559 sizeof(struct common_tim_data) +
560 image_num * sizeof(struct mvebu_image_info));
561 hash_algorithm_id = info->hash_algorithm_id;
562 image_size_to_hash = info->image_size_to_hash;
563 flash_entry_addr = info->flash_entry_addr;
564 hash_value = info->hash;
565 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
567 if (image_num == 0) {
569 * The first image includes hash values in its content.
570 * For hash calculation, we need to save the original
571 * hash values to a local variable that will be
572 * copied back for comparsion and set all zeros to
573 * the orignal hash values for calculating new value.
574 * First image original format :
575 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
576 * Replaced first image format :
577 * x...x (datum1) 0...0(hash values) x...x(datum2)
579 memcpy(internal_hash, hash_value,
580 sizeof(internal_hash));
581 memset(hash_value, 0, sizeof(internal_hash));
583 if (image_size_to_hash == 0) {
584 printf("Warning: Image_%d hash checksum is disabled, ",
586 printf("skip the image validation.\n");
589 switch (hash_algorithm_id) {
591 sha1_starts(&hash1_text);
592 sha1_update(&hash1_text, buff, image_size_to_hash);
593 sha1_finish(&hash1_text, hash_160_output);
594 hash_output = hash_160_output;
597 sha256_starts(&hash256_text);
598 sha256_update(&hash256_text, buff, image_size_to_hash);
599 sha256_finish(&hash256_text, hash_256_output);
600 hash_output = hash_256_output;
603 printf("Error: Unsupported hash_algorithm_id = %d\n",
608 memcpy(hash_value, internal_hash,
609 sizeof(internal_hash));
610 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
611 printf("Error: Image_%d checksum is not correct\n",
616 printf("Image checksum...OK!\n");
621 #else /* Not ARMADA? */
622 static int check_image_header(void)
624 printf("bubt cmd does not support this SoC device or family!\n");
629 static int bubt_verify(size_t image_size)
633 /* Check a correct image header exists */
634 err = check_image_header();
636 printf("Error: Image header verification failed\n");
643 static int bubt_read_file(struct bubt_dev *src)
648 printf("Error: Read not supported on device \"%s\"\n",
653 image_size = src->read(net_boot_file_name);
654 if (image_size <= 0) {
655 printf("Error: Failed to read file %s from %s\n",
656 net_boot_file_name, src->name);
663 static int bubt_is_dev_active(struct bubt_dev *dev)
666 printf("Device \"%s\" not supported by U-BOOT image\n",
671 if (!dev->active()) {
672 printf("Device \"%s\" is inactive\n", dev->name);
679 struct bubt_dev *find_bubt_dev(char *dev_name)
683 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
684 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
685 return &bubt_devs[dev];
691 #define DEFAULT_BUBT_SRC "tftp"
693 #ifndef DEFAULT_BUBT_DST
694 #ifdef CONFIG_MVEBU_SPI_BOOT
695 #define DEFAULT_BUBT_DST "spi"
696 #elif defined(CONFIG_MVEBU_NAND_BOOT)
697 #define DEFAULT_BUBT_DST "nand"
698 #elif defined(CONFIG_MVEBU_MMC_BOOT)
699 #define DEFAULT_BUBT_DST "mmc"
701 #define DEFAULT_BUBT_DST "error"
703 #endif /* DEFAULT_BUBT_DST */
705 int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
707 struct bubt_dev *src, *dst;
709 char src_dev_name[8];
710 char dst_dev_name[8];
715 copy_filename(net_boot_file_name,
716 CONFIG_MVEBU_UBOOT_DFLT_NAME,
717 sizeof(net_boot_file_name));
719 copy_filename(net_boot_file_name, argv[1],
720 sizeof(net_boot_file_name));
723 strncpy(dst_dev_name, argv[2], 8);
725 name = DEFAULT_BUBT_DST;
726 strncpy(dst_dev_name, name, 8);
730 strncpy(src_dev_name, argv[3], 8);
732 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
734 /* Figure out the destination device */
735 dst = find_bubt_dev(dst_dev_name);
737 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
741 if (!bubt_is_dev_active(dst))
744 /* Figure out the source device */
745 src = find_bubt_dev(src_dev_name);
747 printf("Error: Unknown source \"%s\"\n", src_dev_name);
751 if (!bubt_is_dev_active(src))
754 printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
755 net_boot_file_name, src->name, dst->name);
757 image_size = bubt_read_file(src);
761 err = bubt_verify(image_size);
765 err = bubt_write_file(dst, image_size);
773 bubt, 4, 0, do_bubt_cmd,
774 "Burn a u-boot image to flash",
775 "[file-name] [destination [source]]\n"
776 "\t-file-name The image file name to burn. Default = flash-image.bin\n"
777 "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
778 "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
780 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
781 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
782 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"