arm64: configs: Remove SYS_BOOTM_LEN for TI devices
[u-boot.git] / cmd / mvebu / bubt.c
blob5e4ffc40d72983aa9a231cdc66c0ecc035cb6463
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2016 Marvell International Ltd.
4 * https://spdx.org/licenses
5 */
7 #include <config.h>
8 #include <command.h>
9 #include <env.h>
10 #include <image.h>
11 #include <net.h>
12 #include <vsprintf.h>
13 #include <errno.h>
14 #include <dm.h>
15 #include <fuse.h>
16 #include <mach/efuse.h>
18 #include <spi_flash.h>
19 #include <spi.h>
20 #include <nand.h>
21 #include <scsi.h>
22 #include <usb.h>
23 #include <fs.h>
24 #include <mmc.h>
25 #ifdef CONFIG_BLK
26 #include <blk.h>
27 #endif
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 {
36 u32 magic; /* 0-3 */
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 */
44 u8 uart_cfg; /* 32 */
45 u8 baudrate; /* 33 */
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 {
62 unsigned int id;
63 const char *name;
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" },
73 {},
76 struct common_tim_data {
77 u32 version;
78 u32 identifier;
79 u32 trusted;
80 u32 issue_date;
81 u32 oem_unique_id;
82 u32 reserved[5]; /* Reserve 20 bytes */
83 u32 boot_flash_sign;
84 u32 num_images;
85 u32 num_keys;
86 u32 size_of_reserved;
89 struct mvebu_image_info {
90 u32 image_id;
91 u32 next_image_id;
92 u32 flash_entry_addr;
93 u32 load_addr;
94 u32 image_size;
95 u32 image_size_to_hash;
96 u32 hash_algorithm_id;
97 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
98 u32 partition_number;
99 u32 enc_algorithm_id;
100 u32 encrypt_start_offset;
101 u32 encrypt_size;
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 */
108 u8 flags; /* 0x1 */
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 */
122 u8 ext; /* 0x1E */
123 u8 checksum; /* 0x1F */
127 * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
129 struct a38x_opt_hdr_v1 {
130 u8 headertype;
131 u8 headersz_msb;
132 u16 headersz_lsb;
133 u8 data[0];
135 #define A38X_OPT_HDR_V1_SECURE_TYPE 0x1
137 struct a38x_boot_mode {
138 unsigned int id;
139 const char *name;
142 /* The blockid header field values used to indicate boot device of image */
143 struct a38x_boot_mode a38x_boot_modes[] = {
144 { 0x4D, "i2c" },
145 { 0x5A, "spi" },
146 { 0x69, "uart" },
147 { 0x78, "sata" },
148 { 0x8B, "nand" },
149 { 0x9C, "pex" },
150 { 0xAE, "mmc" },
154 #endif
156 struct bubt_dev {
157 char name[8];
158 size_t (*read)(const char *file_name);
159 int (*write)(size_t image_size);
160 int (*active)(void);
163 static ulong get_load_addr(void)
165 const char *addr_str;
166 unsigned long addr;
168 addr_str = env_get("loadaddr");
169 if (addr_str)
170 addr = hextoul(addr_str, NULL);
171 else
172 addr = CONFIG_SYS_LOAD_ADDR;
174 return addr;
177 /********************************************************************
178 * eMMC services
179 ********************************************************************/
180 #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
181 static int mmc_burn_image(size_t image_size)
183 struct mmc *mmc;
184 lbaint_t start_lba;
185 lbaint_t blk_count;
186 ulong blk_written;
187 int err;
188 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
189 #ifdef CONFIG_BLK
190 struct blk_desc *blk_desc;
191 #endif
192 #ifdef CONFIG_SUPPORT_EMMC_BOOT
193 u8 part;
194 u8 orig_part;
195 #endif
197 mmc = find_mmc_device(mmc_dev_num);
198 if (!mmc) {
199 printf("No SD/MMC/eMMC card found\n");
200 return -ENOMEDIUM;
203 err = mmc_init(mmc);
204 if (err) {
205 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
206 mmc_dev_num);
207 return err;
210 #ifdef CONFIG_BLK
211 blk_desc = mmc_get_blk_desc(mmc);
212 if (!blk_desc) {
213 printf("Error - failed to obtain block descriptor\n");
214 return -ENODEV;
216 #endif
218 #ifdef CONFIG_SUPPORT_EMMC_BOOT
219 #ifdef CONFIG_BLK
220 orig_part = blk_desc->hwpart;
221 #else
222 orig_part = mmc->block_dev.hwpart;
223 #endif
225 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
226 if (part == EMMC_BOOT_PART_USER)
227 part = EMMC_HWPART_DEFAULT;
229 #ifdef CONFIG_BLK
230 err = blk_dselect_hwpart(blk_desc, part);
231 #else
232 err = mmc_switch_part(mmc, part);
233 #endif
235 if (err) {
236 printf("Error - MMC partition switch failed\n");
237 return err;
239 #endif
241 /* SD reserves LBA-0 for MBR and boots from LBA-1,
242 * MMC/eMMC boots from LBA-0 and LBA-4096
244 if (IS_SD(mmc))
245 start_lba = 1;
246 #ifdef CONFIG_SUPPORT_EMMC_BOOT
247 else if (part)
248 start_lba = 0;
249 #endif
250 else
251 start_lba = 4096;
252 #ifdef CONFIG_BLK
253 blk_count = image_size / mmc->write_bl_len;
254 if (image_size % mmc->write_bl_len)
255 blk_count += 1;
257 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
258 (void *)get_load_addr());
259 #else
260 blk_count = image_size / mmc->block_dev.blksz;
261 if (image_size % mmc->block_dev.blksz)
262 blk_count += 1;
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
270 #ifdef CONFIG_BLK
271 err = blk_dselect_hwpart(blk_desc, orig_part);
272 #else
273 err = mmc_switch_part(mmc, orig_part);
274 #endif
275 if (err)
276 printf("Error - MMC failed to switch back to original partition\n");
277 #endif
279 if (blk_written != blk_count) {
280 printf("Error - written %#lx blocks\n", blk_written);
281 return -ENOSPC;
283 printf("Done!\n");
285 return 0;
288 static size_t mmc_read_file(const char *file_name)
290 loff_t act_read = 0;
291 int rc;
292 struct mmc *mmc;
293 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
295 mmc = find_mmc_device(mmc_dev_num);
296 if (!mmc) {
297 printf("No SD/MMC/eMMC card found\n");
298 return 0;
301 if (mmc_init(mmc)) {
302 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
303 mmc_dev_num);
304 return 0;
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");
310 return 0;
313 /* Perfrom file read */
314 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
315 if (rc)
316 return 0;
318 return act_read;
321 static int is_mmc_active(void)
323 return 1;
325 #else /* CONFIG_DM_MMC */
326 static int mmc_burn_image(size_t image_size)
328 return -ENODEV;
331 static size_t mmc_read_file(const char *file_name)
333 return 0;
336 static int is_mmc_active(void)
338 return 0;
340 #endif /* CONFIG_DM_MMC */
342 /********************************************************************
343 * SATA services
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)
349 lbaint_t start_lba;
350 lbaint_t blk_count;
351 ulong blk_written;
352 struct blk_desc *blk_desc;
353 #ifdef CONFIG_ARMADA_3700
354 struct disk_partition info;
355 int part;
356 #endif
358 scsi_scan(false);
360 blk_desc = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
361 if (!blk_desc)
362 return -ENODEV;
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) {
371 case PART_TYPE_DOS:
372 for (part = 1; part <= 4; part++) {
373 info.sys_ind = 0;
374 if (part_get_info(blk_desc, part, &info))
375 continue;
376 if (info.sys_ind == 'M')
377 break;
379 if (part > 4) {
380 printf("Error - cannot find MBR 'M' (0x4d) partition on SATA disk\n");
381 return -ENODEV;
383 start_lba = info.start;
384 break;
385 case PART_TYPE_EFI:
386 for (part = 1; part <= 64; part++) {
387 info.type_guid[0] = 0;
388 if (part_get_info(blk_desc, part, &info))
389 continue;
390 /* Check for GPT type GUID of 'Marvell Armada 3700 Boot partition' */
391 if (strcmp(info.type_guid, "6828311A-BA55-42A4-BCDE-A89BB5EDECAE") == 0)
392 break;
394 if (part > 64) {
395 printf("Error - cannot find GPT 'Marvell Armada 3700 Boot partition' on SATA disk\n");
396 return -ENODEV;
398 start_lba = info.start;
399 break;
400 default:
401 printf("Error - no partitions on SATA disk\n");
402 return -ENODEV;
404 #else
405 /* 32-bit Armada BootROM loads SATA firmware from the sector 1. */
406 start_lba = 1;
407 #endif
409 blk_count = image_size / blk_desc->blksz;
410 if (image_size % blk_desc->blksz)
411 blk_count += 1;
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);
418 return -ENOSPC;
421 printf("Done!\n");
422 return 0;
423 #else
424 return -ENODEV;
425 #endif
428 static size_t sata_read_file(const char *file_name)
430 loff_t act_read = 0;
431 struct udevice *dev;
432 int rc;
434 /* try to recognize storage devices immediately */
435 scsi_scan(false);
437 /* Try to recognize storage devices immediately */
438 blk_first_device(UCLASS_SCSI, &dev);
439 if (!dev) {
440 printf("Error: SATA device not found\n");
441 return 0;
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");
447 return 0;
450 /* Perfrom file read */
451 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
452 if (rc)
453 return 0;
455 return act_read;
458 static int is_sata_active(void)
460 return 1;
462 #else /* CONFIG_SCSI */
463 static int sata_burn_image(size_t image_size)
465 return -ENODEV;
468 static size_t sata_read_file(const char *file_name)
470 return 0;
473 static int is_sata_active(void)
475 return 0;
477 #endif /* CONFIG_SCSI */
479 /********************************************************************
480 * SPI services
481 ********************************************************************/
482 #ifdef CONFIG_SPI_FLASH
483 static int spi_burn_image(size_t image_size)
485 int ret;
486 struct spi_flash *flash;
487 u32 erase_bytes;
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);
494 if (!flash) {
495 printf("Failed to probe SPI Flash\n");
496 return -ENOMEDIUM;
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);
504 if (ret)
505 printf("Error!\n");
506 else
507 printf("Done!\n");
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());
512 if (ret)
513 printf("Error!\n");
514 else
515 printf("Done!\n");
517 return ret;
520 static int is_spi_active(void)
522 return 1;
525 #else /* CONFIG_SPI_FLASH */
526 static int spi_burn_image(size_t image_size)
528 return -ENODEV;
531 static int is_spi_active(void)
533 return 0;
535 #endif /* CONFIG_SPI_FLASH */
537 /********************************************************************
538 * NAND services
539 ********************************************************************/
540 #ifdef CONFIG_CMD_NAND
541 static int nand_burn_image(size_t image_size)
543 int ret;
544 uint32_t block_size;
545 struct mtd_info *mtd;
547 mtd = get_nand_dev_by_index(nand_curr_device);
548 if (!mtd) {
549 puts("\nno devices available\n");
550 return -ENOMEDIUM;
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);
560 if (ret) {
561 printf("Error!\n");
562 goto error;
564 printf("Done!\n");
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());
570 if (ret)
571 printf("Error!\n");
572 else
573 printf("Done!\n");
575 error:
576 return ret;
579 static int is_nand_active(void)
581 return 1;
584 #else /* CONFIG_CMD_NAND */
585 static int nand_burn_image(size_t image_size)
587 return -ENODEV;
590 static int is_nand_active(void)
592 return 0;
594 #endif /* CONFIG_CMD_NAND */
596 /********************************************************************
597 * USB services
598 ********************************************************************/
599 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
600 static size_t usb_read_file(const char *file_name)
602 loff_t act_read = 0;
603 struct udevice *dev;
604 int rc;
606 usb_stop();
608 if (usb_init() < 0) {
609 printf("Error: usb_init failed\n");
610 return 0;
613 /* Try to recognize storage devices immediately */
614 blk_first_device(UCLASS_USB, &dev);
615 if (!dev) {
616 printf("Error: USB storage device not found\n");
617 return 0;
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");
623 return 0;
626 /* Perfrom file read */
627 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
628 if (rc)
629 return 0;
631 return act_read;
634 static int is_usb_active(void)
636 return 1;
639 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
640 static size_t usb_read_file(const char *file_name)
642 return 0;
645 static int is_usb_active(void)
647 return 0;
649 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
651 /********************************************************************
652 * Network services
653 ********************************************************************/
654 #ifdef CONFIG_CMD_NET
655 static size_t tftp_read_file(const char *file_name)
657 int ret;
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)
669 return 1;
672 #else
673 static size_t tftp_read_file(const char *file_name)
675 return 0;
678 static int is_tftp_active(void)
680 return 0;
682 #endif /* CONFIG_CMD_NET */
684 enum bubt_devices {
685 BUBT_DEV_NET = 0,
686 BUBT_DEV_USB,
687 BUBT_DEV_MMC,
688 BUBT_DEV_SATA,
689 BUBT_DEV_SPI,
690 BUBT_DEV_NAND,
692 BUBT_MAX_DEV
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)
706 if (!dst->write) {
707 printf("Error: Write not supported on device %s\n", dst->name);
708 return -ENOTSUPP;
711 return dst->write(image_size);
714 #if defined(CONFIG_ARMADA_8K)
715 static u32 do_checksum32(u32 *start, int32_t len)
717 u32 sum = 0;
718 u32 *startp = start;
720 do {
721 sum += *startp;
722 startp++;
723 len -= 4;
724 } while (len > 0);
726 return sum;
729 static int check_image_header(void)
731 struct mvebu_image_header *hdr =
732 (struct mvebu_image_header *)get_load_addr();
733 u32 checksum;
734 u32 checksum_ref;
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);
743 return -ENOEXEC;
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);
752 return -ENOEXEC;
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);
760 return -ENOEXEC;
763 printf("Image checksum...OK!\n");
765 return 0;
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();
771 int image_num;
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;
778 u8 *hash_output;
779 u32 hash_algorithm_id;
780 u32 image_size_to_hash;
781 u32 flash_entry_addr;
782 u32 *hash_value;
783 u32 internal_hash[HASH_SUM_LEN];
784 const u8 *buff;
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 */
790 if (trusted == 1) {
791 printf("bypass image validation, ");
792 printf("only untrusted image is supported now\n");
793 return 0;
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);
798 return -ENOEXEC;
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, ",
830 image_num);
831 printf("skip the image validation.\n");
832 continue;
834 switch (hash_algorithm_id) {
835 case SHA1_SUM_LEN:
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;
840 break;
841 case SHA256_SUM_LEN:
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;
846 break;
847 case SHA512_SUM_LEN:
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;
852 break;
853 default:
854 printf("Error: Unsupported hash_algorithm_id = %d\n",
855 hash_algorithm_id);
856 return -ENOEXEC;
858 if (image_num == 0)
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",
863 image_num);
864 return -ENOEXEC;
867 printf("Image checksum...OK!\n");
869 return 0;
871 #elif defined(CONFIG_ARMADA_32BIT)
872 static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
874 if (h->version == 1)
875 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
877 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
878 h->version);
879 return 0;
882 static uint8_t image_checksum8(const void *start, size_t len)
884 u8 csum = 0;
885 const u8 *p = start;
887 while (len) {
888 csum += *p;
889 ++p;
890 --len;
893 return csum;
896 static uint32_t image_checksum32(const void *start, size_t len)
898 u32 csum = 0;
899 const u32 *p = start;
901 while (len) {
902 csum += *p;
903 ++p;
904 len -= sizeof(u32);
907 return csum;
910 static int check_image_header(void)
912 u8 checksum;
913 u32 checksum32, exp_checksum32;
914 u32 offset, size;
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);
919 if (!hdr_size)
920 return -ENOEXEC;
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);
927 return -ENOEXEC;
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;
936 offset *= blksz;
939 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
940 printf("Error: Bad A38x image blocksize.\n");
941 return -ENOEXEC;
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);
949 return -ENOEXEC;
952 printf("Image checksum...OK!\n");
953 return 0;
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;
961 u32 ohdr_size;
963 if (hdr->version != 1)
964 return 0;
966 if (!hdr->ext)
967 return 0;
969 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
970 do {
971 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
972 return 1;
974 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
976 if (!*((u8 *)ohdr + ohdr_size - 4))
977 break;
979 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
980 if ((u8 *)ohdr >= (u8 *)hdr + hdr_size)
981 break;
982 } while (1);
984 return 0;
986 #endif
987 #else /* Not ARMADA? */
988 static int check_image_header(void)
990 printf("bubt cmd does not support this SoC device or family!\n");
991 return -ENOEXEC;
993 #endif
995 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
996 static u64 fuse_read_u64(u32 bank)
998 u32 val[2];
999 int ret;
1001 ret = fuse_read(bank, 0, &val[0]);
1002 if (ret < 0)
1003 return -1;
1005 ret = fuse_read(bank, 1, &val[1]);
1006 if (ret < 0)
1007 return -1;
1009 return ((u64)val[1] << 32) | val[0];
1011 #endif
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;
1019 #endif
1021 static int bubt_check_boot_mode(const struct bubt_dev *dst)
1023 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
1024 int mode;
1025 #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
1026 int secure_mode;
1027 #endif
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;
1051 #endif
1052 #endif
1054 for (mode = 0; boot_modes[mode].name; mode++) {
1055 if (boot_modes[mode].id == id)
1056 break;
1059 if (!boot_modes[mode].name) {
1060 printf("Error: unknown boot device in image header: 0x%x\n", id);
1061 return -ENOEXEC;
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);
1067 return -ENOEXEC;
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");
1073 return -ENOEXEC;
1074 } else {
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");
1077 return -ENOEXEC;
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");
1085 return -ENOEXEC;
1086 #endif
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)
1090 break;
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);
1094 return -ENOEXEC;
1097 #endif
1098 #endif
1099 return 0;
1102 static int bubt_verify(const struct bubt_dev *dst)
1104 int err;
1106 /* Check a correct image header exists */
1107 err = check_image_header();
1108 if (err) {
1109 printf("Error: Image header verification failed\n");
1110 return err;
1113 err = bubt_check_boot_mode(dst);
1114 if (err) {
1115 printf("Error: Image boot mode verification failed\n");
1116 return err;
1119 return 0;
1122 static int bubt_read_file(struct bubt_dev *src)
1124 size_t image_size;
1126 if (!src->read) {
1127 printf("Error: Read not supported on device \"%s\"\n",
1128 src->name);
1129 return 0;
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);
1136 return 0;
1139 return image_size;
1142 static int bubt_is_dev_active(struct bubt_dev *dev)
1144 if (!dev->active) {
1145 printf("Device \"%s\" not supported by U-Boot image\n",
1146 dev->name);
1147 return 0;
1150 if (!dev->active()) {
1151 printf("Device \"%s\" is inactive\n", dev->name);
1152 return 0;
1155 return 1;
1158 static struct bubt_dev *find_bubt_dev(char *dev_name)
1160 int dev;
1162 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
1163 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
1164 return &bubt_devs[dev];
1167 return 0;
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"
1181 #else
1182 #define DEFAULT_BUBT_DST "error"
1183 #endif
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;
1189 size_t image_size;
1190 char src_dev_name[8];
1191 char dst_dev_name[8];
1192 char *name;
1193 int err;
1195 if (argc < 2)
1196 copy_filename(net_boot_file_name,
1197 CONFIG_MVEBU_UBOOT_DFLT_NAME,
1198 sizeof(net_boot_file_name));
1199 else
1200 copy_filename(net_boot_file_name, argv[1],
1201 sizeof(net_boot_file_name));
1203 if (argc >= 3) {
1204 strncpy(dst_dev_name, argv[2], 8);
1205 } else {
1206 name = DEFAULT_BUBT_DST;
1207 strncpy(dst_dev_name, name, 8);
1210 if (argc >= 4)
1211 strncpy(src_dev_name, argv[3], 8);
1212 else
1213 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1215 /* Figure out the destination device */
1216 dst = find_bubt_dev(dst_dev_name);
1217 if (!dst) {
1218 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
1219 return 1;
1222 if (!bubt_is_dev_active(dst))
1223 return 1;
1225 /* Figure out the source device */
1226 src = find_bubt_dev(src_dev_name);
1227 if (!src) {
1228 printf("Error: Unknown source \"%s\"\n", src_dev_name);
1229 return 1;
1232 if (!bubt_is_dev_active(src))
1233 return -ENODEV;
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);
1239 if (!image_size)
1240 return 1;
1242 err = bubt_verify(dst);
1243 if (err)
1244 return 1;
1246 err = bubt_write_file(dst, image_size);
1247 if (err)
1248 return 1;
1250 return 0;
1253 U_BOOT_CMD(
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"
1260 "Examples:\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"