1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
16 #include <mach/efuse.h>
18 #include <spi_flash.h>
28 #include <u-boot/sha1.h>
29 #include <u-boot/sha256.h>
30 #include <u-boot/sha512.h>
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 tim_boot_flash_sign
{
66 struct tim_boot_flash_sign tim_boot_flash_signs
[] = {
67 { 0x454d4d08, "mmc" },
68 { 0x454d4d0b, "mmc" },
69 { 0x5350490a, "spi" },
70 { 0x5350491a, "nand" },
71 { 0x55415223, "uart" },
72 { 0x53415432, "sata" },
76 struct common_tim_data
{
82 u32 reserved
[5]; /* Reserve 20 bytes */
89 struct mvebu_image_info
{
95 u32 image_size_to_hash
;
96 u32 hash_algorithm_id
;
97 u32 hash
[HASH_SUM_LEN
]; /* Reserve 512 bits for the hash */
100 u32 encrypt_start_offset
;
103 #elif defined(CONFIG_ARMADA_32BIT)
105 /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
106 struct a38x_main_hdr_v1
{
107 u8 blockid
; /* 0x0 */
109 u16 nandpagesize
; /* 0x2-0x3 */
110 u32 blocksize
; /* 0x4-0x7 */
111 u8 version
; /* 0x8 */
112 u8 headersz_msb
; /* 0x9 */
113 u16 headersz_lsb
; /* 0xA-0xB */
114 u32 srcaddr
; /* 0xC-0xF */
115 u32 destaddr
; /* 0x10-0x13 */
116 u32 execaddr
; /* 0x14-0x17 */
117 u8 options
; /* 0x18 */
118 u8 nandblocksize
; /* 0x19 */
119 u8 nandbadblklocation
; /* 0x1A */
120 u8 reserved4
; /* 0x1B */
121 u16 reserved5
; /* 0x1C-0x1D */
123 u8 checksum
; /* 0x1F */
127 * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
129 struct a38x_opt_hdr_v1
{
135 #define A38X_OPT_HDR_V1_SECURE_TYPE 0x1
137 struct a38x_boot_mode
{
142 /* The blockid header field values used to indicate boot device of image */
143 struct a38x_boot_mode a38x_boot_modes
[] = {
158 size_t (*read
)(const char *file_name
);
159 int (*write
)(size_t image_size
);
163 static ulong
get_load_addr(void)
165 const char *addr_str
;
168 addr_str
= env_get("loadaddr");
170 addr
= hextoul(addr_str
, NULL
);
172 addr
= CONFIG_SYS_LOAD_ADDR
;
177 /********************************************************************
179 ********************************************************************/
180 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
181 static int mmc_burn_image(size_t image_size
)
188 const u8 mmc_dev_num
= CONFIG_SYS_MMC_ENV_DEV
;
190 struct blk_desc
*blk_desc
;
192 #ifdef CONFIG_SUPPORT_EMMC_BOOT
197 mmc
= find_mmc_device(mmc_dev_num
);
199 printf("No SD/MMC/eMMC card found\n");
205 printf("%s(%d) init failed\n", IS_SD(mmc
) ? "SD" : "MMC",
211 blk_desc
= mmc_get_blk_desc(mmc
);
213 printf("Error - failed to obtain block descriptor\n");
218 #ifdef CONFIG_SUPPORT_EMMC_BOOT
220 orig_part
= blk_desc
->hwpart
;
222 orig_part
= mmc
->block_dev
.hwpart
;
225 part
= EXT_CSD_EXTRACT_BOOT_PART(mmc
->part_config
);
226 if (part
== EMMC_BOOT_PART_USER
)
227 part
= EMMC_HWPART_DEFAULT
;
230 err
= blk_dselect_hwpart(blk_desc
, part
);
232 err
= mmc_switch_part(mmc
, part
);
236 printf("Error - MMC partition switch failed\n");
241 /* SD reserves LBA-0 for MBR and boots from LBA-1,
242 * MMC/eMMC boots from LBA-0 and LBA-4096
246 #ifdef CONFIG_SUPPORT_EMMC_BOOT
253 blk_count
= image_size
/ mmc
->write_bl_len
;
254 if (image_size
% mmc
->write_bl_len
)
257 blk_written
= blk_dwrite(blk_desc
, start_lba
, blk_count
,
258 (void *)get_load_addr());
260 blk_count
= image_size
/ mmc
->block_dev
.blksz
;
261 if (image_size
% mmc
->block_dev
.blksz
)
264 blk_written
= mmc
->block_dev
.block_write(mmc_dev_num
,
265 start_lba
, blk_count
,
266 (void *)get_load_addr());
267 #endif /* CONFIG_BLK */
269 #ifdef CONFIG_SUPPORT_EMMC_BOOT
271 err
= blk_dselect_hwpart(blk_desc
, orig_part
);
273 err
= mmc_switch_part(mmc
, orig_part
);
276 printf("Error - MMC failed to switch back to original partition\n");
279 if (blk_written
!= blk_count
) {
280 printf("Error - written %#lx blocks\n", blk_written
);
288 static size_t mmc_read_file(const char *file_name
)
293 const u8 mmc_dev_num
= CONFIG_SYS_MMC_ENV_DEV
;
295 mmc
= find_mmc_device(mmc_dev_num
);
297 printf("No SD/MMC/eMMC card found\n");
302 printf("%s(%d) init failed\n", IS_SD(mmc
) ? "SD" : "MMC",
307 /* Load from data partition (0) */
308 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY
)) {
309 printf("Error: MMC 0 not found\n");
313 /* Perfrom file read */
314 rc
= fs_read(file_name
, get_load_addr(), 0, 0, &act_read
);
321 static int is_mmc_active(void)
325 #else /* CONFIG_DM_MMC */
326 static int mmc_burn_image(size_t image_size
)
331 static size_t mmc_read_file(const char *file_name
)
336 static int is_mmc_active(void)
340 #endif /* CONFIG_DM_MMC */
342 /********************************************************************
344 ********************************************************************/
345 #if defined(CONFIG_SCSI) && defined(CONFIG_BLK)
346 static int sata_burn_image(size_t image_size
)
348 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
352 struct blk_desc
*blk_desc
;
353 #ifdef CONFIG_ARMADA_3700
354 struct disk_partition info
;
360 blk_desc
= blk_get_devnum_by_uclass_id(UCLASS_SCSI
, 0);
364 #ifdef CONFIG_ARMADA_3700
366 * 64-bit Armada 3700 BootROM loads SATA firmware from
367 * GPT 'Marvell Armada 3700 Boot partition' or from
368 * MBR 'M' (0x4d) partition.
370 switch (blk_desc
->part_type
) {
372 for (part
= 1; part
<= 4; part
++) {
374 if (part_get_info(blk_desc
, part
, &info
))
376 if (info
.sys_ind
== 'M')
380 printf("Error - cannot find MBR 'M' (0x4d) partition on SATA disk\n");
383 start_lba
= info
.start
;
386 for (part
= 1; part
<= 64; part
++) {
387 info
.type_guid
[0] = 0;
388 if (part_get_info(blk_desc
, part
, &info
))
390 /* Check for GPT type GUID of 'Marvell Armada 3700 Boot partition' */
391 if (strcmp(info
.type_guid
, "6828311A-BA55-42A4-BCDE-A89BB5EDECAE") == 0)
395 printf("Error - cannot find GPT 'Marvell Armada 3700 Boot partition' on SATA disk\n");
398 start_lba
= info
.start
;
401 printf("Error - no partitions on SATA disk\n");
405 /* 32-bit Armada BootROM loads SATA firmware from the sector 1. */
409 blk_count
= image_size
/ blk_desc
->blksz
;
410 if (image_size
% blk_desc
->blksz
)
413 blk_written
= blk_dwrite(blk_desc
, start_lba
, blk_count
,
414 (void *)get_load_addr());
416 if (blk_written
!= blk_count
) {
417 printf("Error - written %#lx blocks\n", blk_written
);
428 static size_t sata_read_file(const char *file_name
)
434 /* try to recognize storage devices immediately */
437 /* Try to recognize storage devices immediately */
438 blk_first_device(UCLASS_SCSI
, &dev
);
440 printf("Error: SATA device not found\n");
444 /* Always load from scsi 0 */
445 if (fs_set_blk_dev("scsi", "0", FS_TYPE_ANY
)) {
446 printf("Error: SATA 0 not found\n");
450 /* Perfrom file read */
451 rc
= fs_read(file_name
, get_load_addr(), 0, 0, &act_read
);
458 static int is_sata_active(void)
462 #else /* CONFIG_SCSI */
463 static int sata_burn_image(size_t image_size
)
468 static size_t sata_read_file(const char *file_name
)
473 static int is_sata_active(void)
477 #endif /* CONFIG_SCSI */
479 /********************************************************************
481 ********************************************************************/
482 #ifdef CONFIG_SPI_FLASH
483 static int spi_burn_image(size_t image_size
)
486 struct spi_flash
*flash
;
489 /* Probe the SPI bus to get the flash device */
490 flash
= spi_flash_probe(CONFIG_SF_DEFAULT_BUS
,
491 CONFIG_SF_DEFAULT_CS
,
492 CONFIG_SF_DEFAULT_SPEED
,
493 CONFIG_SF_DEFAULT_MODE
);
495 printf("Failed to probe SPI Flash\n");
499 erase_bytes
= image_size
+
500 (flash
->erase_size
- image_size
% flash
->erase_size
);
501 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
502 erase_bytes
, erase_bytes
/ flash
->erase_size
);
503 ret
= spi_flash_erase(flash
, 0, erase_bytes
);
509 printf("Writing %d bytes from 0x%lx to offset 0 ...",
510 (int)image_size
, get_load_addr());
511 ret
= spi_flash_write(flash
, 0, image_size
, (void *)get_load_addr());
520 static int is_spi_active(void)
525 #else /* CONFIG_SPI_FLASH */
526 static int spi_burn_image(size_t image_size
)
531 static int is_spi_active(void)
535 #endif /* CONFIG_SPI_FLASH */
537 /********************************************************************
539 ********************************************************************/
540 #ifdef CONFIG_CMD_NAND
541 static int nand_burn_image(size_t image_size
)
545 struct mtd_info
*mtd
;
547 mtd
= get_nand_dev_by_index(nand_curr_device
);
549 puts("\nno devices available\n");
552 block_size
= mtd
->erasesize
;
554 /* Align U-Boot size to currently used blocksize */
555 image_size
= ((image_size
+ (block_size
- 1)) & (~(block_size
- 1)));
557 /* Erase the U-Boot image space */
558 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size
);
559 ret
= nand_erase(mtd
, 0, image_size
);
566 /* Write the image to flash */
567 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
568 (int)image_size
, get_load_addr());
569 ret
= nand_write(mtd
, 0, &image_size
, (void *)get_load_addr());
579 static int is_nand_active(void)
584 #else /* CONFIG_CMD_NAND */
585 static int nand_burn_image(size_t image_size
)
590 static int is_nand_active(void)
594 #endif /* CONFIG_CMD_NAND */
596 /********************************************************************
598 ********************************************************************/
599 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
600 static size_t usb_read_file(const char *file_name
)
608 if (usb_init() < 0) {
609 printf("Error: usb_init failed\n");
613 /* Try to recognize storage devices immediately */
614 blk_first_device(UCLASS_USB
, &dev
);
616 printf("Error: USB storage device not found\n");
620 /* Always load from usb 0 */
621 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY
)) {
622 printf("Error: USB 0 not found\n");
626 /* Perfrom file read */
627 rc
= fs_read(file_name
, get_load_addr(), 0, 0, &act_read
);
634 static int is_usb_active(void)
639 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
640 static size_t usb_read_file(const char *file_name
)
645 static int is_usb_active(void)
649 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
651 /********************************************************************
653 ********************************************************************/
654 #ifdef CONFIG_CMD_NET
655 static size_t tftp_read_file(const char *file_name
)
660 * update global variable image_load_addr before tftp file from network
662 image_load_addr
= get_load_addr();
663 ret
= net_loop(TFTPGET
);
664 return ret
> 0 ? ret
: 0;
667 static int is_tftp_active(void)
673 static size_t tftp_read_file(const char *file_name
)
678 static int is_tftp_active(void)
682 #endif /* CONFIG_CMD_NET */
695 static struct bubt_dev bubt_devs
[BUBT_MAX_DEV
] = {
696 {"tftp", tftp_read_file
, NULL
, is_tftp_active
},
697 {"usb", usb_read_file
, NULL
, is_usb_active
},
698 {"mmc", mmc_read_file
, mmc_burn_image
, is_mmc_active
},
699 {"sata", sata_read_file
, sata_burn_image
, is_sata_active
},
700 {"spi", NULL
, spi_burn_image
, is_spi_active
},
701 {"nand", NULL
, nand_burn_image
, is_nand_active
},
704 static int bubt_write_file(struct bubt_dev
*dst
, size_t image_size
)
707 printf("Error: Write not supported on device %s\n", dst
->name
);
711 return dst
->write(image_size
);
714 #if defined(CONFIG_ARMADA_8K)
715 static u32
do_checksum32(u32
*start
, int32_t len
)
729 static int check_image_header(void)
731 struct mvebu_image_header
*hdr
=
732 (struct mvebu_image_header
*)get_load_addr();
737 * For now compare checksum, and magic. Later we can
738 * verify more stuff on the header like interface type, etc
740 if (hdr
->magic
!= MAIN_HDR_MAGIC
) {
741 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
742 hdr
->magic
, MAIN_HDR_MAGIC
);
746 checksum_ref
= hdr
->prolog_checksum
;
747 checksum
= do_checksum32((u32
*)hdr
, hdr
->prolog_size
);
748 checksum
-= hdr
->prolog_checksum
;
749 if (checksum
!= checksum_ref
) {
750 printf("Error: Bad Prolog checksum. 0x%x != 0x%x\n",
751 checksum
, checksum_ref
);
755 checksum_ref
= hdr
->boot_image_checksum
;
756 checksum
= do_checksum32((u32
*)((u8
*)hdr
+ hdr
->prolog_size
), hdr
->boot_image_size
);
757 if (checksum
!= checksum_ref
) {
758 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
759 checksum
, checksum_ref
);
763 printf("Image checksum...OK!\n");
767 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
768 static int check_image_header(void)
770 struct common_tim_data
*hdr
= (struct common_tim_data
*)get_load_addr();
772 u8 hash_160_output
[SHA1_SUM_LEN
];
773 u8 hash_256_output
[SHA256_SUM_LEN
];
774 u8 hash_512_output
[SHA512_SUM_LEN
];
775 sha1_context hash1_text
;
776 sha256_context hash256_text
;
777 sha512_context hash512_text
;
779 u32 hash_algorithm_id
;
780 u32 image_size_to_hash
;
781 u32 flash_entry_addr
;
783 u32 internal_hash
[HASH_SUM_LEN
];
785 u32 num_of_image
= hdr
->num_images
;
786 u32 version
= hdr
->version
;
787 u32 trusted
= hdr
->trusted
;
789 /* bubt checksum validation only supports nontrusted images */
791 printf("bypass image validation, ");
792 printf("only untrusted image is supported now\n");
795 /* only supports image version 3.5 and 3.6 */
796 if (version
!= IMAGE_VERSION_3_5_0
&& version
!= IMAGE_VERSION_3_6_0
) {
797 printf("Error: Unsupported Image version = 0x%08x\n", version
);
800 /* validate images hash value */
801 for (image_num
= 0; image_num
< num_of_image
; image_num
++) {
802 struct mvebu_image_info
*info
=
803 (struct mvebu_image_info
*)(get_load_addr() +
804 sizeof(struct common_tim_data
) +
805 image_num
* sizeof(struct mvebu_image_info
));
806 hash_algorithm_id
= info
->hash_algorithm_id
;
807 image_size_to_hash
= info
->image_size_to_hash
;
808 flash_entry_addr
= info
->flash_entry_addr
;
809 hash_value
= info
->hash
;
810 buff
= (const u8
*)(get_load_addr() + flash_entry_addr
);
812 if (image_num
== 0) {
814 * The first image includes hash values in its content.
815 * For hash calculation, we need to save the original
816 * hash values to a local variable that will be
817 * copied back for comparsion and set all zeros to
818 * the orignal hash values for calculating new value.
819 * First image original format :
820 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
821 * Replaced first image format :
822 * x...x (datum1) 0...0(hash values) x...x(datum2)
824 memcpy(internal_hash
, hash_value
,
825 sizeof(internal_hash
));
826 memset(hash_value
, 0, sizeof(internal_hash
));
828 if (image_size_to_hash
== 0) {
829 printf("Warning: Image_%d hash checksum is disabled, ",
831 printf("skip the image validation.\n");
834 switch (hash_algorithm_id
) {
836 sha1_starts(&hash1_text
);
837 sha1_update(&hash1_text
, buff
, image_size_to_hash
);
838 sha1_finish(&hash1_text
, hash_160_output
);
839 hash_output
= hash_160_output
;
842 sha256_starts(&hash256_text
);
843 sha256_update(&hash256_text
, buff
, image_size_to_hash
);
844 sha256_finish(&hash256_text
, hash_256_output
);
845 hash_output
= hash_256_output
;
848 sha512_starts(&hash512_text
);
849 sha512_update(&hash512_text
, buff
, image_size_to_hash
);
850 sha512_finish(&hash512_text
, hash_512_output
);
851 hash_output
= hash_512_output
;
854 printf("Error: Unsupported hash_algorithm_id = %d\n",
859 memcpy(hash_value
, internal_hash
,
860 sizeof(internal_hash
));
861 if (memcmp(hash_value
, hash_output
, hash_algorithm_id
) != 0) {
862 printf("Error: Image_%d checksum is not correct\n",
867 printf("Image checksum...OK!\n");
871 #elif defined(CONFIG_ARMADA_32BIT)
872 static size_t a38x_header_size(const struct a38x_main_hdr_v1
*h
)
875 return (h
->headersz_msb
<< 16) | le16_to_cpu(h
->headersz_lsb
);
877 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
882 static uint8_t image_checksum8(const void *start
, size_t len
)
896 static uint32_t image_checksum32(const void *start
, size_t len
)
899 const u32
*p
= start
;
910 static int check_image_header(void)
913 u32 checksum32
, exp_checksum32
;
915 const struct a38x_main_hdr_v1
*hdr
=
916 (struct a38x_main_hdr_v1
*)get_load_addr();
917 const size_t hdr_size
= a38x_header_size(hdr
);
922 checksum
= image_checksum8(hdr
, hdr_size
);
923 checksum
-= hdr
->checksum
;
924 if (checksum
!= hdr
->checksum
) {
925 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
926 checksum
, hdr
->checksum
);
930 offset
= le32_to_cpu(hdr
->srcaddr
);
931 size
= le32_to_cpu(hdr
->blocksize
);
933 if (hdr
->blockid
== 0x78) { /* SATA id */
934 struct blk_desc
*blk_dev
= IS_ENABLED(BLK
) ? blk_get_devnum_by_uclass_id(UCLASS_SCSI
, 0) : NULL
;
935 unsigned long blksz
= blk_dev
? blk_dev
->blksz
: 512;
939 if (offset
% 4 != 0 || size
< 4 || size
% 4 != 0) {
940 printf("Error: Bad A38x image blocksize.\n");
944 checksum32
= image_checksum32((u8
*)hdr
+ offset
, size
- 4);
945 exp_checksum32
= *(u32
*)((u8
*)hdr
+ offset
+ size
- 4);
946 if (checksum32
!= exp_checksum32
) {
947 printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n",
948 checksum32
, exp_checksum32
);
952 printf("Image checksum...OK!\n");
956 #if defined(CONFIG_ARMADA_38X)
957 static int a38x_image_is_secure(const struct a38x_main_hdr_v1
*hdr
)
959 const size_t hdr_size
= a38x_header_size(hdr
);
960 struct a38x_opt_hdr_v1
*ohdr
;
963 if (hdr
->version
!= 1)
969 ohdr
= (struct a38x_opt_hdr_v1
*)(hdr
+ 1);
971 if (ohdr
->headertype
== A38X_OPT_HDR_V1_SECURE_TYPE
)
974 ohdr_size
= (ohdr
->headersz_msb
<< 16) | le16_to_cpu(ohdr
->headersz_lsb
);
976 if (!*((u8
*)ohdr
+ ohdr_size
- 4))
979 ohdr
= (struct a38x_opt_hdr_v1
*)((u8
*)ohdr
+ ohdr_size
);
980 if ((u8
*)ohdr
>= (u8
*)hdr
+ hdr_size
)
987 #else /* Not ARMADA? */
988 static int check_image_header(void)
990 printf("bubt cmd does not support this SoC device or family!\n");
995 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
996 static u64
fuse_read_u64(u32 bank
)
1001 ret
= fuse_read(bank
, 0, &val
[0]);
1005 ret
= fuse_read(bank
, 1, &val
[1]);
1009 return ((u64
)val
[1] << 32) | val
[0];
1013 #if defined(CONFIG_ARMADA_3700)
1014 static inline u8
maj3(u8 val
)
1016 /* return majority vote of 3 bits */
1017 return ((val
& 0x7) == 3 || (val
& 0x7) > 4) ? 1 : 0;
1021 static int bubt_check_boot_mode(const struct bubt_dev
*dst
)
1023 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
1025 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
1028 #if defined(CONFIG_ARMADA_3700)
1029 const struct tim_boot_flash_sign
*boot_modes
= tim_boot_flash_signs
;
1030 const struct common_tim_data
*hdr
=
1031 (struct common_tim_data
*)get_load_addr();
1032 u32 id
= hdr
->boot_flash_sign
;
1033 int is_secure
= hdr
->trusted
!= 0;
1034 u64 otp_secure_bits
= fuse_read_u64(1);
1035 int otp_secure_boot
= ((maj3(otp_secure_bits
>> 0) << 0) |
1036 (maj3(otp_secure_bits
>> 4) << 1)) == 2;
1037 unsigned int otp_boot_device
= (maj3(otp_secure_bits
>> 48) << 0) |
1038 (maj3(otp_secure_bits
>> 52) << 1) |
1039 (maj3(otp_secure_bits
>> 56) << 2) |
1040 (maj3(otp_secure_bits
>> 60) << 3);
1041 #elif defined(CONFIG_ARMADA_32BIT)
1042 const struct a38x_boot_mode
*boot_modes
= a38x_boot_modes
;
1043 const struct a38x_main_hdr_v1
*hdr
=
1044 (struct a38x_main_hdr_v1
*)get_load_addr();
1045 u32 id
= hdr
->blockid
;
1046 #if defined(CONFIG_ARMADA_38X)
1047 int is_secure
= a38x_image_is_secure(hdr
);
1048 u64 otp_secure_bits
= fuse_read_u64(EFUSE_LINE_SECURE_BOOT
);
1049 int otp_secure_boot
= otp_secure_bits
& 0x1;
1050 unsigned int otp_boot_device
= (otp_secure_bits
>> 8) & 0x7;
1054 for (mode
= 0; boot_modes
[mode
].name
; mode
++) {
1055 if (boot_modes
[mode
].id
== id
)
1059 if (!boot_modes
[mode
].name
) {
1060 printf("Error: unknown boot device in image header: 0x%x\n", id
);
1064 if (strcmp(boot_modes
[mode
].name
, dst
->name
) != 0) {
1065 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
1066 boot_modes
[mode
].name
, dst
->name
);
1070 #if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700)
1071 if (otp_secure_bits
== (u64
)-1) {
1072 printf("Error: cannot read OTP secure bits\n");
1075 if (otp_secure_boot
&& !is_secure
) {
1076 printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n");
1078 } else if (!otp_secure_boot
&& is_secure
) {
1079 #if defined(CONFIG_ARMADA_3700)
1081 * Armada 3700 BootROM rejects trusted image when secure boot is not enabled.
1082 * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled.
1084 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
1087 } else if (otp_boot_device
&& otp_boot_device
!= id
) {
1088 for (secure_mode
= 0; boot_modes
[secure_mode
].name
; secure_mode
++) {
1089 if (boot_modes
[secure_mode
].id
== otp_boot_device
)
1092 printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n",
1093 boot_modes
[secure_mode
].name
?: "unknown", dst
->name
);
1102 static int bubt_verify(const struct bubt_dev
*dst
)
1106 /* Check a correct image header exists */
1107 err
= check_image_header();
1109 printf("Error: Image header verification failed\n");
1113 err
= bubt_check_boot_mode(dst
);
1115 printf("Error: Image boot mode verification failed\n");
1122 static int bubt_read_file(struct bubt_dev
*src
)
1127 printf("Error: Read not supported on device \"%s\"\n",
1132 image_size
= src
->read(net_boot_file_name
);
1133 if (image_size
<= 0) {
1134 printf("Error: Failed to read file %s from %s\n",
1135 net_boot_file_name
, src
->name
);
1142 static int bubt_is_dev_active(struct bubt_dev
*dev
)
1145 printf("Device \"%s\" not supported by U-Boot image\n",
1150 if (!dev
->active()) {
1151 printf("Device \"%s\" is inactive\n", dev
->name
);
1158 static struct bubt_dev
*find_bubt_dev(char *dev_name
)
1162 for (dev
= 0; dev
< BUBT_MAX_DEV
; dev
++) {
1163 if (strcmp(bubt_devs
[dev
].name
, dev_name
) == 0)
1164 return &bubt_devs
[dev
];
1170 #define DEFAULT_BUBT_SRC "tftp"
1172 #ifndef DEFAULT_BUBT_DST
1173 #ifdef CONFIG_MVEBU_SPI_BOOT
1174 #define DEFAULT_BUBT_DST "spi"
1175 #elif defined(CONFIG_MVEBU_NAND_BOOT)
1176 #define DEFAULT_BUBT_DST "nand"
1177 #elif defined(CONFIG_MVEBU_MMC_BOOT)
1178 #define DEFAULT_BUBT_DST "mmc"
1179 #elif defined(CONFIG_MVEBU_SATA_BOOT)
1180 #define DEFAULT_BUBT_DST "sata"
1182 #define DEFAULT_BUBT_DST "error"
1184 #endif /* DEFAULT_BUBT_DST */
1186 static int do_bubt_cmd(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
1188 struct bubt_dev
*src
, *dst
;
1190 char src_dev_name
[8];
1191 char dst_dev_name
[8];
1196 copy_filename(net_boot_file_name
,
1197 CONFIG_MVEBU_UBOOT_DFLT_NAME
,
1198 sizeof(net_boot_file_name
));
1200 copy_filename(net_boot_file_name
, argv
[1],
1201 sizeof(net_boot_file_name
));
1204 strncpy(dst_dev_name
, argv
[2], 8);
1206 name
= DEFAULT_BUBT_DST
;
1207 strncpy(dst_dev_name
, name
, 8);
1211 strncpy(src_dev_name
, argv
[3], 8);
1213 strncpy(src_dev_name
, DEFAULT_BUBT_SRC
, 8);
1215 /* Figure out the destination device */
1216 dst
= find_bubt_dev(dst_dev_name
);
1218 printf("Error: Unknown destination \"%s\"\n", dst_dev_name
);
1222 if (!bubt_is_dev_active(dst
))
1225 /* Figure out the source device */
1226 src
= find_bubt_dev(src_dev_name
);
1228 printf("Error: Unknown source \"%s\"\n", src_dev_name
);
1232 if (!bubt_is_dev_active(src
))
1235 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
1236 net_boot_file_name
, src
->name
, dst
->name
);
1238 image_size
= bubt_read_file(src
);
1242 err
= bubt_verify(dst
);
1246 err
= bubt_write_file(dst
, image_size
);
1254 bubt
, 4, 0, do_bubt_cmd
,
1255 "Burn a u-boot image to flash",
1256 "[file-name] [destination [source]]\n"
1257 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME
"\n"
1258 "\t-destination Flash to burn to [spi, nand, mmc, sata]. Default = " DEFAULT_BUBT_DST
"\n"
1259 "\t-source The source to load image from [tftp, usb, mmc, sata]. Default = " DEFAULT_BUBT_SRC
"\n"
1261 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
1262 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
1263 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"