1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
17 #include <spi_flash.h>
26 #include <u-boot/sha1.h>
27 #include <u-boot/sha256.h>
29 #ifndef CONFIG_SYS_MMC_ENV_DEV
30 #define CONFIG_SYS_MMC_ENV_DEV 0
33 #if defined(CONFIG_ARMADA_8K)
34 #define MAIN_HDR_MAGIC 0xB105B002
36 struct mvebu_image_header
{
38 u32 prolog_size
; /* 4-7 */
39 u32 prolog_checksum
; /* 8-11 */
40 u32 boot_image_size
; /* 12-15 */
41 u32 boot_image_checksum
; /* 16-19 */
42 u32 rsrvd0
; /* 20-23 */
43 u32 load_addr
; /* 24-27 */
44 u32 exec_addr
; /* 28-31 */
47 u8 ext_count
; /* 34 */
48 u8 aux_flags
; /* 35 */
49 u32 io_arg_0
; /* 36-39 */
50 u32 io_arg_1
; /* 40-43 */
51 u32 io_arg_2
; /* 43-47 */
52 u32 io_arg_3
; /* 48-51 */
53 u32 rsrvd1
; /* 52-55 */
54 u32 rsrvd2
; /* 56-59 */
55 u32 rsrvd3
; /* 60-63 */
57 #elif defined(CONFIG_ARMADA_3700) /* A3700 */
58 #define HASH_SUM_LEN 16
59 #define IMAGE_VERSION_3_6_0 0x030600
60 #define IMAGE_VERSION_3_5_0 0x030500
62 struct common_tim_data
{
68 u32 reserved
[5]; /* Reserve 20 bytes */
75 struct mvebu_image_info
{
81 u32 image_size_to_hash
;
82 u32 hash_algorithm_id
;
83 u32 hash
[HASH_SUM_LEN
]; /* Reserve 512 bits for the hash */
86 u32 encrypt_start_offset
;
91 /* Structure of the main header, version 1 (Armada 370/38x/XP) */
92 struct a38x_main_hdr_v1
{
95 u16 reserved2
; /* 0x2-0x3 */
96 u32 blocksize
; /* 0x4-0x7 */
98 u8 headersz_msb
; /* 0x9 */
99 u16 headersz_lsb
; /* 0xA-0xB */
100 u32 srcaddr
; /* 0xC-0xF */
101 u32 destaddr
; /* 0x10-0x13 */
102 u32 execaddr
; /* 0x14-0x17 */
103 u8 options
; /* 0x18 */
104 u8 nandblocksize
; /* 0x19 */
105 u8 nandbadblklocation
; /* 0x1A */
106 u8 reserved4
; /* 0x1B */
107 u16 reserved5
; /* 0x1C-0x1D */
109 u8 checksum
; /* 0x1F */
112 struct a38x_boot_mode
{
117 /* The blockid header field values used to indicate boot device of image */
118 struct a38x_boot_mode a38x_boot_modes
[] = {
131 size_t (*read
)(const char *file_name
);
132 int (*write
)(size_t image_size
);
136 static ulong
get_load_addr(void)
138 const char *addr_str
;
141 addr_str
= env_get("loadaddr");
143 addr
= simple_strtoul(addr_str
, NULL
, 16);
145 addr
= CONFIG_SYS_LOAD_ADDR
;
150 /********************************************************************
152 ********************************************************************/
153 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
154 static int mmc_burn_image(size_t image_size
)
161 const u8 mmc_dev_num
= CONFIG_SYS_MMC_ENV_DEV
;
163 struct blk_desc
*blk_desc
;
165 mmc
= find_mmc_device(mmc_dev_num
);
167 printf("No SD/MMC/eMMC card found\n");
173 printf("%s(%d) init failed\n", IS_SD(mmc
) ? "SD" : "MMC",
178 #ifdef CONFIG_SYS_MMC_ENV_PART
179 if (mmc
->part_num
!= CONFIG_SYS_MMC_ENV_PART
) {
180 err
= mmc_switch_part(mmc_dev_num
, CONFIG_SYS_MMC_ENV_PART
);
182 printf("MMC partition switch failed\n");
188 /* SD reserves LBA-0 for MBR and boots from LBA-1,
189 * MMC/eMMC boots from LBA-0
191 start_lba
= IS_SD(mmc
) ? 1 : 0;
193 blk_count
= image_size
/ mmc
->write_bl_len
;
194 if (image_size
% mmc
->write_bl_len
)
197 blk_desc
= mmc_get_blk_desc(mmc
);
199 printf("Error - failed to obtain block descriptor\n");
202 blk_written
= blk_dwrite(blk_desc
, start_lba
, blk_count
,
203 (void *)get_load_addr());
205 blk_count
= image_size
/ mmc
->block_dev
.blksz
;
206 if (image_size
% mmc
->block_dev
.blksz
)
209 blk_written
= mmc
->block_dev
.block_write(mmc_dev_num
,
210 start_lba
, blk_count
,
211 (void *)get_load_addr());
212 #endif /* CONFIG_BLK */
213 if (blk_written
!= blk_count
) {
214 printf("Error - written %#lx blocks\n", blk_written
);
219 #ifdef CONFIG_SYS_MMC_ENV_PART
220 if (mmc
->part_num
!= CONFIG_SYS_MMC_ENV_PART
)
221 mmc_switch_part(mmc_dev_num
, mmc
->part_num
);
227 static size_t mmc_read_file(const char *file_name
)
232 const u8 mmc_dev_num
= CONFIG_SYS_MMC_ENV_DEV
;
234 mmc
= find_mmc_device(mmc_dev_num
);
236 printf("No SD/MMC/eMMC card found\n");
241 printf("%s(%d) init failed\n", IS_SD(mmc
) ? "SD" : "MMC",
246 /* Load from data partition (0) */
247 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY
)) {
248 printf("Error: MMC 0 not found\n");
252 /* Perfrom file read */
253 rc
= fs_read(file_name
, get_load_addr(), 0, 0, &act_read
);
260 static int is_mmc_active(void)
264 #else /* CONFIG_DM_MMC */
265 static int mmc_burn_image(size_t image_size
)
270 static size_t mmc_read_file(const char *file_name
)
275 static int is_mmc_active(void)
279 #endif /* CONFIG_DM_MMC */
281 /********************************************************************
283 ********************************************************************/
284 #ifdef CONFIG_SPI_FLASH
285 static int spi_burn_image(size_t image_size
)
288 struct spi_flash
*flash
;
291 /* Probe the SPI bus to get the flash device */
292 flash
= spi_flash_probe(CONFIG_ENV_SPI_BUS
,
294 CONFIG_SF_DEFAULT_SPEED
,
295 CONFIG_SF_DEFAULT_MODE
);
297 printf("Failed to probe SPI Flash\n");
301 #ifdef CONFIG_SPI_FLASH_PROTECTION
302 spi_flash_protect(flash
, 0);
304 erase_bytes
= image_size
+
305 (flash
->erase_size
- image_size
% flash
->erase_size
);
306 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
307 erase_bytes
, erase_bytes
/ flash
->erase_size
);
308 ret
= spi_flash_erase(flash
, 0, erase_bytes
);
314 printf("Writing %d bytes from 0x%lx to offset 0 ...",
315 (int)image_size
, get_load_addr());
316 ret
= spi_flash_write(flash
, 0, image_size
, (void *)get_load_addr());
322 #ifdef CONFIG_SPI_FLASH_PROTECTION
323 spi_flash_protect(flash
, 1);
329 static int is_spi_active(void)
334 #else /* CONFIG_SPI_FLASH */
335 static int spi_burn_image(size_t image_size
)
340 static int is_spi_active(void)
344 #endif /* CONFIG_SPI_FLASH */
346 /********************************************************************
348 ********************************************************************/
349 #ifdef CONFIG_CMD_NAND
350 static int nand_burn_image(size_t image_size
)
354 struct mtd_info
*mtd
;
356 mtd
= get_nand_dev_by_index(nand_curr_device
);
358 puts("\nno devices available\n");
361 block_size
= mtd
->erasesize
;
363 /* Align U-Boot size to currently used blocksize */
364 image_size
= ((image_size
+ (block_size
- 1)) & (~(block_size
- 1)));
366 /* Erase the U-Boot image space */
367 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size
);
368 ret
= nand_erase(mtd
, 0, image_size
);
375 /* Write the image to flash */
376 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
377 (int)image_size
, get_load_addr());
378 ret
= nand_write(mtd
, 0, &image_size
, (void *)get_load_addr());
388 static int is_nand_active(void)
393 #else /* CONFIG_CMD_NAND */
394 static int nand_burn_image(size_t image_size
)
399 static int is_nand_active(void)
403 #endif /* CONFIG_CMD_NAND */
405 /********************************************************************
407 ********************************************************************/
408 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
409 static size_t usb_read_file(const char *file_name
)
417 if (usb_init() < 0) {
418 printf("Error: usb_init failed\n");
422 /* Try to recognize storage devices immediately */
423 blk_first_device(IF_TYPE_USB
, &dev
);
425 printf("Error: USB storage device not found\n");
429 /* Always load from usb 0 */
430 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY
)) {
431 printf("Error: USB 0 not found\n");
435 /* Perfrom file read */
436 rc
= fs_read(file_name
, get_load_addr(), 0, 0, &act_read
);
443 static int is_usb_active(void)
448 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
449 static size_t usb_read_file(const char *file_name
)
454 static int is_usb_active(void)
458 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
460 /********************************************************************
462 ********************************************************************/
463 #ifdef CONFIG_CMD_NET
464 static size_t tftp_read_file(const char *file_name
)
467 * update global variable image_load_addr before tftp file from network
469 image_load_addr
= get_load_addr();
470 return net_loop(TFTPGET
);
473 static int is_tftp_active(void)
479 static size_t tftp_read_file(const char *file_name
)
484 static int is_tftp_active(void)
488 #endif /* CONFIG_CMD_NET */
500 struct bubt_dev bubt_devs
[BUBT_MAX_DEV
] = {
501 {"tftp", tftp_read_file
, NULL
, is_tftp_active
},
502 {"usb", usb_read_file
, NULL
, is_usb_active
},
503 {"mmc", mmc_read_file
, mmc_burn_image
, is_mmc_active
},
504 {"spi", NULL
, spi_burn_image
, is_spi_active
},
505 {"nand", NULL
, nand_burn_image
, is_nand_active
},
508 static int bubt_write_file(struct bubt_dev
*dst
, size_t image_size
)
511 printf("Error: Write not supported on device %s\n", dst
->name
);
515 return dst
->write(image_size
);
518 #if defined(CONFIG_ARMADA_8K)
519 u32
do_checksum32(u32
*start
, int32_t len
)
533 static int check_image_header(void)
535 struct mvebu_image_header
*hdr
=
536 (struct mvebu_image_header
*)get_load_addr();
537 u32 header_len
= hdr
->prolog_size
;
539 u32 checksum_ref
= hdr
->prolog_checksum
;
542 * For now compare checksum, and magic. Later we can
543 * verify more stuff on the header like interface type, etc
545 if (hdr
->magic
!= MAIN_HDR_MAGIC
) {
546 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
547 hdr
->magic
, MAIN_HDR_MAGIC
);
551 /* The checksum value is discarded from checksum calculation */
552 hdr
->prolog_checksum
= 0;
554 checksum
= do_checksum32((u32
*)hdr
, header_len
);
555 if (checksum
!= checksum_ref
) {
556 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
557 checksum
, checksum_ref
);
561 /* Restore the checksum before writing */
562 hdr
->prolog_checksum
= checksum_ref
;
563 printf("Image checksum...OK!\n");
567 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
568 static int check_image_header(void)
570 struct common_tim_data
*hdr
= (struct common_tim_data
*)get_load_addr();
572 u8 hash_160_output
[SHA1_SUM_LEN
];
573 u8 hash_256_output
[SHA256_SUM_LEN
];
574 sha1_context hash1_text
;
575 sha256_context hash256_text
;
577 u32 hash_algorithm_id
;
578 u32 image_size_to_hash
;
579 u32 flash_entry_addr
;
581 u32 internal_hash
[HASH_SUM_LEN
];
583 u32 num_of_image
= hdr
->num_images
;
584 u32 version
= hdr
->version
;
585 u32 trusted
= hdr
->trusted
;
587 /* bubt checksum validation only supports nontrusted images */
589 printf("bypass image validation, ");
590 printf("only untrusted image is supported now\n");
593 /* only supports image version 3.5 and 3.6 */
594 if (version
!= IMAGE_VERSION_3_5_0
&& version
!= IMAGE_VERSION_3_6_0
) {
595 printf("Error: Unsupported Image version = 0x%08x\n", version
);
598 /* validate images hash value */
599 for (image_num
= 0; image_num
< num_of_image
; image_num
++) {
600 struct mvebu_image_info
*info
=
601 (struct mvebu_image_info
*)(get_load_addr() +
602 sizeof(struct common_tim_data
) +
603 image_num
* sizeof(struct mvebu_image_info
));
604 hash_algorithm_id
= info
->hash_algorithm_id
;
605 image_size_to_hash
= info
->image_size_to_hash
;
606 flash_entry_addr
= info
->flash_entry_addr
;
607 hash_value
= info
->hash
;
608 buff
= (const u8
*)(get_load_addr() + flash_entry_addr
);
610 if (image_num
== 0) {
612 * The first image includes hash values in its content.
613 * For hash calculation, we need to save the original
614 * hash values to a local variable that will be
615 * copied back for comparsion and set all zeros to
616 * the orignal hash values for calculating new value.
617 * First image original format :
618 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
619 * Replaced first image format :
620 * x...x (datum1) 0...0(hash values) x...x(datum2)
622 memcpy(internal_hash
, hash_value
,
623 sizeof(internal_hash
));
624 memset(hash_value
, 0, sizeof(internal_hash
));
626 if (image_size_to_hash
== 0) {
627 printf("Warning: Image_%d hash checksum is disabled, ",
629 printf("skip the image validation.\n");
632 switch (hash_algorithm_id
) {
634 sha1_starts(&hash1_text
);
635 sha1_update(&hash1_text
, buff
, image_size_to_hash
);
636 sha1_finish(&hash1_text
, hash_160_output
);
637 hash_output
= hash_160_output
;
640 sha256_starts(&hash256_text
);
641 sha256_update(&hash256_text
, buff
, image_size_to_hash
);
642 sha256_finish(&hash256_text
, hash_256_output
);
643 hash_output
= hash_256_output
;
646 printf("Error: Unsupported hash_algorithm_id = %d\n",
651 memcpy(hash_value
, internal_hash
,
652 sizeof(internal_hash
));
653 if (memcmp(hash_value
, hash_output
, hash_algorithm_id
) != 0) {
654 printf("Error: Image_%d checksum is not correct\n",
659 printf("Image checksum...OK!\n");
663 #elif defined(CONFIG_ARMADA_38X)
664 static size_t a38x_header_size(const struct a38x_main_hdr_v1
*h
)
667 return (h
->headersz_msb
<< 16) | le16_to_cpu(h
->headersz_lsb
);
669 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
674 static uint8_t image_checksum8(const void *start
, size_t len
)
688 static int check_image_header(void)
691 const struct a38x_main_hdr_v1
*hdr
=
692 (struct a38x_main_hdr_v1
*)get_load_addr();
693 const size_t image_size
= a38x_header_size(hdr
);
698 checksum
= image_checksum8(hdr
, image_size
);
699 checksum
-= hdr
->checksum
;
700 if (checksum
!= hdr
->checksum
) {
701 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
702 checksum
, hdr
->checksum
);
706 printf("Image checksum...OK!\n");
709 #else /* Not ARMADA? */
710 static int check_image_header(void)
712 printf("bubt cmd does not support this SoC device or family!\n");
717 static int bubt_check_boot_mode(const struct bubt_dev
*dst
)
719 if (IS_ENABLED(CONFIG_ARMADA_38X
)) {
721 const struct a38x_main_hdr_v1
*hdr
=
722 (struct a38x_main_hdr_v1
*)get_load_addr();
724 for (mode
= 0; mode
< ARRAY_SIZE(a38x_boot_modes
); mode
++) {
725 if (strcmp(a38x_boot_modes
[mode
].name
, dst
->name
) == 0)
729 if (a38x_boot_modes
[mode
].id
== hdr
->blockid
)
732 for (int i
= 0; i
< ARRAY_SIZE(a38x_boot_modes
); i
++) {
733 if (a38x_boot_modes
[i
].id
== hdr
->blockid
) {
734 printf("Error: A38x image meant to be booted from "
735 "\"%s\", not \"%s\"!\n",
736 a38x_boot_modes
[i
].name
, dst
->name
);
741 printf("Error: unknown boot device in A38x image header: "
742 "0x%x\n", hdr
->blockid
);
749 static int bubt_verify(const struct bubt_dev
*dst
)
753 /* Check a correct image header exists */
754 err
= check_image_header();
756 printf("Error: Image header verification failed\n");
760 err
= bubt_check_boot_mode(dst
);
762 printf("Error: Image boot mode verification failed\n");
769 static int bubt_read_file(struct bubt_dev
*src
)
774 printf("Error: Read not supported on device \"%s\"\n",
779 image_size
= src
->read(net_boot_file_name
);
780 if (image_size
<= 0) {
781 printf("Error: Failed to read file %s from %s\n",
782 net_boot_file_name
, src
->name
);
789 static int bubt_is_dev_active(struct bubt_dev
*dev
)
792 printf("Device \"%s\" not supported by U-Boot image\n",
797 if (!dev
->active()) {
798 printf("Device \"%s\" is inactive\n", dev
->name
);
805 struct bubt_dev
*find_bubt_dev(char *dev_name
)
809 for (dev
= 0; dev
< BUBT_MAX_DEV
; dev
++) {
810 if (strcmp(bubt_devs
[dev
].name
, dev_name
) == 0)
811 return &bubt_devs
[dev
];
817 #define DEFAULT_BUBT_SRC "tftp"
819 #ifndef DEFAULT_BUBT_DST
820 #ifdef CONFIG_MVEBU_SPI_BOOT
821 #define DEFAULT_BUBT_DST "spi"
822 #elif defined(CONFIG_MVEBU_NAND_BOOT)
823 #define DEFAULT_BUBT_DST "nand"
824 #elif defined(CONFIG_MVEBU_MMC_BOOT)
825 #define DEFAULT_BUBT_DST "mmc"
827 #define DEFAULT_BUBT_DST "error"
829 #endif /* DEFAULT_BUBT_DST */
831 int do_bubt_cmd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
833 struct bubt_dev
*src
, *dst
;
835 char src_dev_name
[8];
836 char dst_dev_name
[8];
841 copy_filename(net_boot_file_name
,
842 CONFIG_MVEBU_UBOOT_DFLT_NAME
,
843 sizeof(net_boot_file_name
));
845 copy_filename(net_boot_file_name
, argv
[1],
846 sizeof(net_boot_file_name
));
849 strncpy(dst_dev_name
, argv
[2], 8);
851 name
= DEFAULT_BUBT_DST
;
852 strncpy(dst_dev_name
, name
, 8);
856 strncpy(src_dev_name
, argv
[3], 8);
858 strncpy(src_dev_name
, DEFAULT_BUBT_SRC
, 8);
860 /* Figure out the destination device */
861 dst
= find_bubt_dev(dst_dev_name
);
863 printf("Error: Unknown destination \"%s\"\n", dst_dev_name
);
867 if (!bubt_is_dev_active(dst
))
870 /* Figure out the source device */
871 src
= find_bubt_dev(src_dev_name
);
873 printf("Error: Unknown source \"%s\"\n", src_dev_name
);
877 if (!bubt_is_dev_active(src
))
880 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
881 net_boot_file_name
, src
->name
, dst
->name
);
883 image_size
= bubt_read_file(src
);
887 err
= bubt_verify(dst
);
891 err
= bubt_write_file(dst
, image_size
);
899 bubt
, 4, 0, do_bubt_cmd
,
900 "Burn a u-boot image to flash",
901 "[file-name] [destination [source]]\n"
902 "\t-file-name The image file name to burn. Default = flash-image.bin\n"
903 "\t-destination Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
904 "\t-source The source to load image from [tftp, usb, mmc]. Default = tftp\n"
906 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
907 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
908 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"