blob: 4bad9a69527c4c2bc37f9886b5cc291f536d45b4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02002/*
3 * Copyright (C) 2016 Marvell International Ltd.
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02004 * https://spdx.org/licenses
5 */
6
7#include <config.h>
8#include <common.h>
9#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070011#include <image.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020013#include <vsprintf.h>
14#include <errno.h>
15#include <dm.h>
Pali Rohárd83e04a2022-08-23 14:52:24 +020016#include <fuse.h>
17#include <mach/efuse.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020018
19#include <spi_flash.h>
20#include <spi.h>
21#include <nand.h>
22#include <usb.h>
23#include <fs.h>
24#include <mmc.h>
Konstantin Porotchkina89c0552017-01-08 16:52:06 +020025#ifdef CONFIG_BLK
26#include <blk.h>
27#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020028#include <u-boot/sha1.h>
29#include <u-boot/sha256.h>
Pali Rohára4bbb2c2022-07-26 16:11:58 +020030#include <u-boot/sha512.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020031
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020032#if defined(CONFIG_ARMADA_8K)
33#define MAIN_HDR_MAGIC 0xB105B002
34
35struct 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 */
55};
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
60
Pali Rohár1ce57b82022-07-26 16:11:56 +020061struct tim_boot_flash_sign {
62 unsigned int id;
63 const char *name;
64};
65
66struct 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 {},
74};
75
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020076struct 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;
87};
88
89struct 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;
102};
Pali Rohár1ce57b82022-07-26 16:11:56 +0200103#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600104
Pali Rohár0f7df222021-10-22 12:41:10 +0200105/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Joel Johnson37b9c122020-04-17 09:38:04 -0600106struct a38x_main_hdr_v1 {
107 u8 blockid; /* 0x0 */
108 u8 flags; /* 0x1 */
Pali Rohár7fea8842021-10-22 12:37:48 +0200109 u16 nandpagesize; /* 0x2-0x3 */
Joel Johnson37b9c122020-04-17 09:38:04 -0600110 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 */
124};
Joel Johnsonab5dd302020-04-17 09:38:06 -0600125
Pali Rohárd83e04a2022-08-23 14:52:24 +0200126/*
127 * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
128 */
129struct a38x_opt_hdr_v1 {
130 u8 headertype;
131 u8 headersz_msb;
132 u16 headersz_lsb;
133 u8 data[0];
134};
135#define A38X_OPT_HDR_V1_SECURE_TYPE 0x1
136
Joel Johnsonab5dd302020-04-17 09:38:06 -0600137struct a38x_boot_mode {
138 unsigned int id;
139 const char *name;
140};
141
142/* The blockid header field values used to indicate boot device of image */
143struct 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" },
151 {},
152};
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200153
Pali Rohár1ce57b82022-07-26 16:11:56 +0200154#endif
155
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200156struct 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);
161};
162
163static ulong get_load_addr(void)
164{
165 const char *addr_str;
166 unsigned long addr;
167
Simon Glass64b723f2017-08-03 12:22:12 -0600168 addr_str = env_get("loadaddr");
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200169 if (addr_str)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600170 addr = hextoul(addr_str, NULL);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200171 else
172 addr = CONFIG_SYS_LOAD_ADDR;
173
174 return addr;
175}
176
177/********************************************************************
178 * eMMC services
179 ********************************************************************/
Jean-Jacques Hiblotd0531672018-01-04 15:23:32 +0100180#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200181static int mmc_burn_image(size_t image_size)
182{
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;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200189#ifdef CONFIG_BLK
190 struct blk_desc *blk_desc;
191#endif
Pali Roháre4e67772023-01-21 22:58:28 +0100192#ifdef CONFIG_SUPPORT_EMMC_BOOT
193 u8 part;
194 u8 orig_part;
195#endif
196
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200197 mmc = find_mmc_device(mmc_dev_num);
198 if (!mmc) {
199 printf("No SD/MMC/eMMC card found\n");
200 return -ENOMEDIUM;
201 }
202
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;
208 }
Pali Roháre4e67772023-01-21 22:58:28 +0100209
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;
215 }
216#endif
217
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
224
225 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
226
227 if (part == 7)
228 part = 0;
229
230#ifdef CONFIG_BLK
231 err = blk_dselect_hwpart(blk_desc, part);
232#else
233 err = mmc_switch_part(mmc, part);
234#endif
235
236 if (err) {
237 printf("Error - MMC partition switch failed\n");
238 return err;
239 }
240#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200241
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200242 /* SD reserves LBA-0 for MBR and boots from LBA-1,
243 * MMC/eMMC boots from LBA-0
244 */
245 start_lba = IS_SD(mmc) ? 1 : 0;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200246#ifdef CONFIG_BLK
247 blk_count = image_size / mmc->write_bl_len;
248 if (image_size % mmc->write_bl_len)
249 blk_count += 1;
250
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200251 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
252 (void *)get_load_addr());
253#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200254 blk_count = image_size / mmc->block_dev.blksz;
255 if (image_size % mmc->block_dev.blksz)
256 blk_count += 1;
257
258 blk_written = mmc->block_dev.block_write(mmc_dev_num,
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200259 start_lba, blk_count,
260 (void *)get_load_addr());
261#endif /* CONFIG_BLK */
Pali Roháre4e67772023-01-21 22:58:28 +0100262
263#ifdef CONFIG_SUPPORT_EMMC_BOOT
264#ifdef CONFIG_BLK
265 err = blk_dselect_hwpart(blk_desc, orig_part);
266#else
267 err = mmc_switch_part(mmc, orig_part);
268#endif
269 if (err)
270 printf("Error - MMC failed to switch back to original partition\n");
271#endif
272
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200273 if (blk_written != blk_count) {
274 printf("Error - written %#lx blocks\n", blk_written);
275 return -ENOSPC;
276 }
277 printf("Done!\n");
278
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200279 return 0;
280}
281
282static size_t mmc_read_file(const char *file_name)
283{
284 loff_t act_read = 0;
285 int rc;
286 struct mmc *mmc;
287 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
288
289 mmc = find_mmc_device(mmc_dev_num);
290 if (!mmc) {
291 printf("No SD/MMC/eMMC card found\n");
292 return 0;
293 }
294
295 if (mmc_init(mmc)) {
296 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
297 mmc_dev_num);
298 return 0;
299 }
300
301 /* Load from data partition (0) */
302 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
303 printf("Error: MMC 0 not found\n");
304 return 0;
305 }
306
307 /* Perfrom file read */
308 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
309 if (rc)
310 return 0;
311
312 return act_read;
313}
314
315static int is_mmc_active(void)
316{
317 return 1;
318}
319#else /* CONFIG_DM_MMC */
320static int mmc_burn_image(size_t image_size)
321{
322 return -ENODEV;
323}
324
325static size_t mmc_read_file(const char *file_name)
326{
327 return 0;
328}
329
330static int is_mmc_active(void)
331{
332 return 0;
333}
334#endif /* CONFIG_DM_MMC */
335
336/********************************************************************
337 * SPI services
338 ********************************************************************/
339#ifdef CONFIG_SPI_FLASH
340static int spi_burn_image(size_t image_size)
341{
342 int ret;
343 struct spi_flash *flash;
344 u32 erase_bytes;
345
346 /* Probe the SPI bus to get the flash device */
Tom Rini119d2fb2021-12-11 14:55:48 -0500347 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
348 CONFIG_SF_DEFAULT_CS,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200349 CONFIG_SF_DEFAULT_SPEED,
350 CONFIG_SF_DEFAULT_MODE);
351 if (!flash) {
352 printf("Failed to probe SPI Flash\n");
353 return -ENOMEDIUM;
354 }
355
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200356 erase_bytes = image_size +
357 (flash->erase_size - image_size % flash->erase_size);
358 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
359 erase_bytes, erase_bytes / flash->erase_size);
360 ret = spi_flash_erase(flash, 0, erase_bytes);
361 if (ret)
362 printf("Error!\n");
363 else
364 printf("Done!\n");
365
366 printf("Writing %d bytes from 0x%lx to offset 0 ...",
367 (int)image_size, get_load_addr());
368 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
369 if (ret)
370 printf("Error!\n");
371 else
372 printf("Done!\n");
373
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200374 return ret;
375}
376
377static int is_spi_active(void)
378{
379 return 1;
380}
381
382#else /* CONFIG_SPI_FLASH */
383static int spi_burn_image(size_t image_size)
384{
385 return -ENODEV;
386}
387
388static int is_spi_active(void)
389{
390 return 0;
391}
392#endif /* CONFIG_SPI_FLASH */
393
394/********************************************************************
395 * NAND services
396 ********************************************************************/
397#ifdef CONFIG_CMD_NAND
398static int nand_burn_image(size_t image_size)
399{
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300400 int ret;
401 uint32_t block_size;
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500402 struct mtd_info *mtd;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200403
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500404 mtd = get_nand_dev_by_index(nand_curr_device);
405 if (!mtd) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200406 puts("\nno devices available\n");
407 return -ENOMEDIUM;
408 }
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500409 block_size = mtd->erasesize;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200410
411 /* Align U-Boot size to currently used blocksize */
412 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
413
Joel Johnson22d78e42020-04-17 09:38:05 -0600414 /* Erase the U-Boot image space */
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200415 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500416 ret = nand_erase(mtd, 0, image_size);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200417 if (ret) {
418 printf("Error!\n");
419 goto error;
420 }
421 printf("Done!\n");
422
423 /* Write the image to flash */
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300424 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
425 (int)image_size, get_load_addr());
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500426 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200427 if (ret)
428 printf("Error!\n");
429 else
430 printf("Done!\n");
431
432error:
433 return ret;
434}
435
436static int is_nand_active(void)
437{
438 return 1;
439}
440
441#else /* CONFIG_CMD_NAND */
442static int nand_burn_image(size_t image_size)
443{
444 return -ENODEV;
445}
446
447static int is_nand_active(void)
448{
449 return 0;
450}
451#endif /* CONFIG_CMD_NAND */
452
453/********************************************************************
454 * USB services
455 ********************************************************************/
456#if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
457static size_t usb_read_file(const char *file_name)
458{
459 loff_t act_read = 0;
460 struct udevice *dev;
461 int rc;
462
463 usb_stop();
464
465 if (usb_init() < 0) {
466 printf("Error: usb_init failed\n");
467 return 0;
468 }
469
470 /* Try to recognize storage devices immediately */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600471 blk_first_device(UCLASS_USB, &dev);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200472 if (!dev) {
473 printf("Error: USB storage device not found\n");
474 return 0;
475 }
476
477 /* Always load from usb 0 */
478 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
479 printf("Error: USB 0 not found\n");
480 return 0;
481 }
482
483 /* Perfrom file read */
484 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
485 if (rc)
486 return 0;
487
488 return act_read;
489}
490
491static int is_usb_active(void)
492{
493 return 1;
494}
495
496#else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
497static size_t usb_read_file(const char *file_name)
498{
499 return 0;
500}
501
502static int is_usb_active(void)
503{
504 return 0;
505}
506#endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
507
508/********************************************************************
509 * Network services
510 ********************************************************************/
511#ifdef CONFIG_CMD_NET
512static size_t tftp_read_file(const char *file_name)
513{
Pali Rohár2c96d022022-07-26 16:11:57 +0200514 int ret;
515
Simon Glass892265d2019-12-28 10:45:02 -0700516 /*
517 * update global variable image_load_addr before tftp file from network
518 */
519 image_load_addr = get_load_addr();
Pali Rohár2c96d022022-07-26 16:11:57 +0200520 ret = net_loop(TFTPGET);
521 return ret > 0 ? ret : 0;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200522}
523
524static int is_tftp_active(void)
525{
526 return 1;
527}
528
529#else
530static size_t tftp_read_file(const char *file_name)
531{
532 return 0;
533}
534
535static int is_tftp_active(void)
536{
537 return 0;
538}
539#endif /* CONFIG_CMD_NET */
540
541enum bubt_devices {
542 BUBT_DEV_NET = 0,
543 BUBT_DEV_USB,
544 BUBT_DEV_MMC,
545 BUBT_DEV_SPI,
546 BUBT_DEV_NAND,
547
548 BUBT_MAX_DEV
549};
550
551struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
552 {"tftp", tftp_read_file, NULL, is_tftp_active},
553 {"usb", usb_read_file, NULL, is_usb_active},
554 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
555 {"spi", NULL, spi_burn_image, is_spi_active},
556 {"nand", NULL, nand_burn_image, is_nand_active},
557};
558
559static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
560{
561 if (!dst->write) {
562 printf("Error: Write not supported on device %s\n", dst->name);
563 return -ENOTSUPP;
564 }
565
566 return dst->write(image_size);
567}
568
569#if defined(CONFIG_ARMADA_8K)
570u32 do_checksum32(u32 *start, int32_t len)
571{
572 u32 sum = 0;
573 u32 *startp = start;
574
575 do {
576 sum += *startp;
577 startp++;
578 len -= 4;
579 } while (len > 0);
580
581 return sum;
582}
583
584static int check_image_header(void)
585{
586 struct mvebu_image_header *hdr =
587 (struct mvebu_image_header *)get_load_addr();
588 u32 header_len = hdr->prolog_size;
589 u32 checksum;
590 u32 checksum_ref = hdr->prolog_checksum;
591
592 /*
593 * For now compare checksum, and magic. Later we can
594 * verify more stuff on the header like interface type, etc
595 */
596 if (hdr->magic != MAIN_HDR_MAGIC) {
597 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
598 hdr->magic, MAIN_HDR_MAGIC);
599 return -ENOEXEC;
600 }
601
602 /* The checksum value is discarded from checksum calculation */
603 hdr->prolog_checksum = 0;
604
605 checksum = do_checksum32((u32 *)hdr, header_len);
606 if (checksum != checksum_ref) {
607 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
608 checksum, checksum_ref);
609 return -ENOEXEC;
610 }
611
612 /* Restore the checksum before writing */
613 hdr->prolog_checksum = checksum_ref;
614 printf("Image checksum...OK!\n");
615
616 return 0;
617}
618#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
619static int check_image_header(void)
620{
621 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
622 int image_num;
623 u8 hash_160_output[SHA1_SUM_LEN];
624 u8 hash_256_output[SHA256_SUM_LEN];
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200625 u8 hash_512_output[SHA512_SUM_LEN];
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200626 sha1_context hash1_text;
627 sha256_context hash256_text;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200628 sha512_context hash512_text;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200629 u8 *hash_output;
630 u32 hash_algorithm_id;
631 u32 image_size_to_hash;
632 u32 flash_entry_addr;
633 u32 *hash_value;
634 u32 internal_hash[HASH_SUM_LEN];
635 const u8 *buff;
636 u32 num_of_image = hdr->num_images;
637 u32 version = hdr->version;
638 u32 trusted = hdr->trusted;
639
640 /* bubt checksum validation only supports nontrusted images */
641 if (trusted == 1) {
642 printf("bypass image validation, ");
643 printf("only untrusted image is supported now\n");
644 return 0;
645 }
646 /* only supports image version 3.5 and 3.6 */
647 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
648 printf("Error: Unsupported Image version = 0x%08x\n", version);
649 return -ENOEXEC;
650 }
651 /* validate images hash value */
652 for (image_num = 0; image_num < num_of_image; image_num++) {
653 struct mvebu_image_info *info =
654 (struct mvebu_image_info *)(get_load_addr() +
655 sizeof(struct common_tim_data) +
656 image_num * sizeof(struct mvebu_image_info));
657 hash_algorithm_id = info->hash_algorithm_id;
658 image_size_to_hash = info->image_size_to_hash;
659 flash_entry_addr = info->flash_entry_addr;
660 hash_value = info->hash;
661 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
662
663 if (image_num == 0) {
664 /*
665 * The first image includes hash values in its content.
666 * For hash calculation, we need to save the original
667 * hash values to a local variable that will be
668 * copied back for comparsion and set all zeros to
669 * the orignal hash values for calculating new value.
670 * First image original format :
671 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
672 * Replaced first image format :
673 * x...x (datum1) 0...0(hash values) x...x(datum2)
674 */
675 memcpy(internal_hash, hash_value,
676 sizeof(internal_hash));
677 memset(hash_value, 0, sizeof(internal_hash));
678 }
679 if (image_size_to_hash == 0) {
680 printf("Warning: Image_%d hash checksum is disabled, ",
681 image_num);
682 printf("skip the image validation.\n");
683 continue;
684 }
685 switch (hash_algorithm_id) {
686 case SHA1_SUM_LEN:
687 sha1_starts(&hash1_text);
688 sha1_update(&hash1_text, buff, image_size_to_hash);
689 sha1_finish(&hash1_text, hash_160_output);
690 hash_output = hash_160_output;
691 break;
692 case SHA256_SUM_LEN:
693 sha256_starts(&hash256_text);
694 sha256_update(&hash256_text, buff, image_size_to_hash);
695 sha256_finish(&hash256_text, hash_256_output);
696 hash_output = hash_256_output;
697 break;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200698 case SHA512_SUM_LEN:
699 sha512_starts(&hash512_text);
700 sha512_update(&hash512_text, buff, image_size_to_hash);
701 sha512_finish(&hash512_text, hash_512_output);
702 hash_output = hash_512_output;
703 break;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200704 default:
705 printf("Error: Unsupported hash_algorithm_id = %d\n",
706 hash_algorithm_id);
707 return -ENOEXEC;
708 }
709 if (image_num == 0)
710 memcpy(hash_value, internal_hash,
711 sizeof(internal_hash));
712 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
713 printf("Error: Image_%d checksum is not correct\n",
714 image_num);
715 return -ENOEXEC;
716 }
717 }
718 printf("Image checksum...OK!\n");
719
Joel Johnson37b9c122020-04-17 09:38:04 -0600720 return 0;
721}
Pali Rohár1ce57b82022-07-26 16:11:56 +0200722#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600723static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
724{
725 if (h->version == 1)
726 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
727
728 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
729 h->version);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200730 return 0;
731}
732
Joel Johnson37b9c122020-04-17 09:38:04 -0600733static uint8_t image_checksum8(const void *start, size_t len)
734{
735 u8 csum = 0;
736 const u8 *p = start;
737
738 while (len) {
739 csum += *p;
740 ++p;
741 --len;
742 }
743
744 return csum;
745}
746
Pali Rohár05781ff2022-08-23 14:52:23 +0200747static uint32_t image_checksum32(const void *start, size_t len)
748{
749 u32 csum = 0;
750 const u32 *p = start;
751
752 while (len) {
753 csum += *p;
754 ++p;
755 len -= sizeof(u32);
756 }
757
758 return csum;
759}
760
Joel Johnson37b9c122020-04-17 09:38:04 -0600761static int check_image_header(void)
762{
763 u8 checksum;
Pali Rohár05781ff2022-08-23 14:52:23 +0200764 u32 checksum32, exp_checksum32;
765 u32 offset, size;
Joel Johnson37b9c122020-04-17 09:38:04 -0600766 const struct a38x_main_hdr_v1 *hdr =
767 (struct a38x_main_hdr_v1 *)get_load_addr();
768 const size_t image_size = a38x_header_size(hdr);
769
770 if (!image_size)
771 return -ENOEXEC;
772
773 checksum = image_checksum8(hdr, image_size);
774 checksum -= hdr->checksum;
775 if (checksum != hdr->checksum) {
Pali Rohár05781ff2022-08-23 14:52:23 +0200776 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
Joel Johnson37b9c122020-04-17 09:38:04 -0600777 checksum, hdr->checksum);
778 return -ENOEXEC;
779 }
780
Pali Rohár05781ff2022-08-23 14:52:23 +0200781 offset = le32_to_cpu(hdr->srcaddr);
782 size = le32_to_cpu(hdr->blocksize);
783
Pali Rohár42cc7952023-01-21 13:59:20 +0100784 if (hdr->blockid == 0x78) /* SATA id */
Pali Rohár05781ff2022-08-23 14:52:23 +0200785 offset *= 512;
Pali Rohár05781ff2022-08-23 14:52:23 +0200786
Pali Rohár05781ff2022-08-23 14:52:23 +0200787 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
788 printf("Error: Bad A38x image blocksize.\n");
789 return -ENOEXEC;
790 }
791
792 checksum32 = image_checksum32((u8 *)hdr + offset, size - 4);
793 exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4);
794 if (checksum32 != exp_checksum32) {
795 printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n",
796 checksum32, exp_checksum32);
797 return -ENOEXEC;
798 }
799
Joel Johnson37b9c122020-04-17 09:38:04 -0600800 printf("Image checksum...OK!\n");
801 return 0;
802}
Pali Rohárd83e04a2022-08-23 14:52:24 +0200803
804#if defined(CONFIG_ARMADA_38X)
805static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr)
806{
807 u32 image_size = a38x_header_size(hdr);
808 struct a38x_opt_hdr_v1 *ohdr;
809 u32 ohdr_size;
810
811 if (hdr->version != 1)
812 return 0;
813
814 if (!hdr->ext)
815 return 0;
816
817 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
818 do {
819 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
820 return 1;
821
822 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
823
824 if (!*((u8 *)ohdr + ohdr_size - 4))
825 break;
826
827 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
828 if ((u8 *)ohdr >= (u8 *)hdr + image_size)
829 break;
830 } while (1);
831
832 return 0;
833}
834#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200835#else /* Not ARMADA? */
836static int check_image_header(void)
837{
838 printf("bubt cmd does not support this SoC device or family!\n");
839 return -ENOEXEC;
840}
841#endif
842
Pali Rohárd83e04a2022-08-23 14:52:24 +0200843#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
844static u64 fuse_read_u64(u32 bank)
845{
846 u32 val[2];
847 int ret;
848
849 ret = fuse_read(bank, 0, &val[0]);
850 if (ret < 0)
851 return -1;
852
853 ret = fuse_read(bank, 1, &val[1]);
854 if (ret < 0)
855 return -1;
856
857 return ((u64)val[1] << 32) | val[0];
858}
859#endif
860
861#if defined(CONFIG_ARMADA_3700)
862static inline u8 maj3(u8 val)
863{
864 /* return majority vote of 3 bits */
865 return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0;
866}
867#endif
868
Joel Johnsonab5dd302020-04-17 09:38:06 -0600869static int bubt_check_boot_mode(const struct bubt_dev *dst)
870{
Pali Rohár1ce57b82022-07-26 16:11:56 +0200871#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200872 int mode, secure_mode;
Pali Rohár1ce57b82022-07-26 16:11:56 +0200873#if defined(CONFIG_ARMADA_3700)
874 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
875 const struct common_tim_data *hdr =
876 (struct common_tim_data *)get_load_addr();
877 u32 id = hdr->boot_flash_sign;
Pali Rohárd83e04a2022-08-23 14:52:24 +0200878 int is_secure = hdr->trusted != 0;
879 u64 otp_secure_bits = fuse_read_u64(1);
880 int otp_secure_boot = ((maj3(otp_secure_bits >> 0) << 0) |
881 (maj3(otp_secure_bits >> 4) << 1)) == 2;
882 unsigned int otp_boot_device = (maj3(otp_secure_bits >> 48) << 0) |
883 (maj3(otp_secure_bits >> 52) << 1) |
884 (maj3(otp_secure_bits >> 56) << 2) |
885 (maj3(otp_secure_bits >> 60) << 3);
Pali Rohár1ce57b82022-07-26 16:11:56 +0200886#elif defined(CONFIG_ARMADA_32BIT)
887 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
888 const struct a38x_main_hdr_v1 *hdr =
889 (struct a38x_main_hdr_v1 *)get_load_addr();
890 u32 id = hdr->blockid;
Pali Rohárd83e04a2022-08-23 14:52:24 +0200891#if defined(CONFIG_ARMADA_38X)
892 int is_secure = a38x_image_is_secure(hdr);
893 u64 otp_secure_bits = fuse_read_u64(EFUSE_LINE_SECURE_BOOT);
894 int otp_secure_boot = otp_secure_bits & 0x1;
895 unsigned int otp_boot_device = (otp_secure_bits >> 8) & 0x7;
896#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +0200897#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -0600898
Pali Rohár1ce57b82022-07-26 16:11:56 +0200899 for (mode = 0; boot_modes[mode].name; mode++) {
900 if (boot_modes[mode].id == id)
901 break;
902 }
Joel Johnson27e9d072020-04-17 09:38:08 -0600903
Pali Rohár1ce57b82022-07-26 16:11:56 +0200904 if (!boot_modes[mode].name) {
905 printf("Error: unknown boot device in image header: 0x%x\n", id);
Joel Johnsonab5dd302020-04-17 09:38:06 -0600906 return -ENOEXEC;
Joel Johnsonab5dd302020-04-17 09:38:06 -0600907 }
Pali Rohár1ce57b82022-07-26 16:11:56 +0200908
Pali Rohárd83e04a2022-08-23 14:52:24 +0200909 if (strcmp(boot_modes[mode].name, dst->name) != 0) {
910 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
911 boot_modes[mode].name, dst->name);
912 return -ENOEXEC;
913 }
Pali Rohár1ce57b82022-07-26 16:11:56 +0200914
Pali Rohárd83e04a2022-08-23 14:52:24 +0200915#if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700)
916 if (otp_secure_bits == (u64)-1) {
917 printf("Error: cannot read OTP secure bits\n");
918 return -ENOEXEC;
919 } else {
920 if (otp_secure_boot && !is_secure) {
921 printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n");
922 return -ENOEXEC;
923 } else if (!otp_secure_boot && is_secure) {
924#if defined(CONFIG_ARMADA_3700)
925 /*
926 * Armada 3700 BootROM rejects trusted image when secure boot is not enabled.
927 * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled.
928 */
929 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
930 return -ENOEXEC;
Pali Rohár1ce57b82022-07-26 16:11:56 +0200931#endif
Pali Rohárd83e04a2022-08-23 14:52:24 +0200932 } else if (otp_boot_device && otp_boot_device != id) {
933 for (secure_mode = 0; boot_modes[secure_mode].name; secure_mode++) {
934 if (boot_modes[secure_mode].id == otp_boot_device)
935 break;
936 }
937 printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n",
938 boot_modes[secure_mode].name ?: "unknown", dst->name);
939 return -ENOEXEC;
940 }
941 }
942#endif
943#endif
944 return 0;
Joel Johnsonab5dd302020-04-17 09:38:06 -0600945}
946
947static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200948{
949 int err;
950
951 /* Check a correct image header exists */
952 err = check_image_header();
953 if (err) {
954 printf("Error: Image header verification failed\n");
955 return err;
956 }
957
Joel Johnsonab5dd302020-04-17 09:38:06 -0600958 err = bubt_check_boot_mode(dst);
959 if (err) {
960 printf("Error: Image boot mode verification failed\n");
961 return err;
962 }
963
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200964 return 0;
965}
966
967static int bubt_read_file(struct bubt_dev *src)
968{
969 size_t image_size;
970
971 if (!src->read) {
972 printf("Error: Read not supported on device \"%s\"\n",
973 src->name);
974 return 0;
975 }
976
977 image_size = src->read(net_boot_file_name);
978 if (image_size <= 0) {
979 printf("Error: Failed to read file %s from %s\n",
980 net_boot_file_name, src->name);
981 return 0;
982 }
983
984 return image_size;
985}
986
987static int bubt_is_dev_active(struct bubt_dev *dev)
988{
989 if (!dev->active) {
Joel Johnson22d78e42020-04-17 09:38:05 -0600990 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200991 dev->name);
992 return 0;
993 }
994
995 if (!dev->active()) {
996 printf("Device \"%s\" is inactive\n", dev->name);
997 return 0;
998 }
999
1000 return 1;
1001}
1002
1003struct bubt_dev *find_bubt_dev(char *dev_name)
1004{
1005 int dev;
1006
1007 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
1008 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
1009 return &bubt_devs[dev];
1010 }
1011
1012 return 0;
1013}
1014
1015#define DEFAULT_BUBT_SRC "tftp"
1016
1017#ifndef DEFAULT_BUBT_DST
1018#ifdef CONFIG_MVEBU_SPI_BOOT
1019#define DEFAULT_BUBT_DST "spi"
1020#elif defined(CONFIG_MVEBU_NAND_BOOT)
1021#define DEFAULT_BUBT_DST "nand"
1022#elif defined(CONFIG_MVEBU_MMC_BOOT)
1023#define DEFAULT_BUBT_DST "mmc"
Konstantin Porotchkine78a3892021-03-17 18:53:43 +02001024#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001025#define DEFAULT_BUBT_DST "error"
1026#endif
1027#endif /* DEFAULT_BUBT_DST */
1028
Simon Glassed38aef2020-05-10 11:40:03 -06001029int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001030{
1031 struct bubt_dev *src, *dst;
1032 size_t image_size;
1033 char src_dev_name[8];
1034 char dst_dev_name[8];
1035 char *name;
1036 int err;
1037
1038 if (argc < 2)
1039 copy_filename(net_boot_file_name,
1040 CONFIG_MVEBU_UBOOT_DFLT_NAME,
1041 sizeof(net_boot_file_name));
1042 else
1043 copy_filename(net_boot_file_name, argv[1],
1044 sizeof(net_boot_file_name));
1045
1046 if (argc >= 3) {
1047 strncpy(dst_dev_name, argv[2], 8);
1048 } else {
1049 name = DEFAULT_BUBT_DST;
1050 strncpy(dst_dev_name, name, 8);
1051 }
1052
1053 if (argc >= 4)
1054 strncpy(src_dev_name, argv[3], 8);
1055 else
1056 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1057
1058 /* Figure out the destination device */
1059 dst = find_bubt_dev(dst_dev_name);
1060 if (!dst) {
1061 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
Pali Rohár873fc6a2022-07-26 16:11:59 +02001062 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001063 }
1064
1065 if (!bubt_is_dev_active(dst))
Pali Rohár873fc6a2022-07-26 16:11:59 +02001066 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001067
1068 /* Figure out the source device */
1069 src = find_bubt_dev(src_dev_name);
1070 if (!src) {
1071 printf("Error: Unknown source \"%s\"\n", src_dev_name);
1072 return 1;
1073 }
1074
1075 if (!bubt_is_dev_active(src))
1076 return -ENODEV;
1077
Joel Johnson22d78e42020-04-17 09:38:05 -06001078 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001079 net_boot_file_name, src->name, dst->name);
1080
1081 image_size = bubt_read_file(src);
1082 if (!image_size)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001083 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001084
Joel Johnsonab5dd302020-04-17 09:38:06 -06001085 err = bubt_verify(dst);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001086 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001087 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001088
1089 err = bubt_write_file(dst, image_size);
1090 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001091 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001092
1093 return 0;
1094}
1095
1096U_BOOT_CMD(
1097 bubt, 4, 0, do_bubt_cmd,
1098 "Burn a u-boot image to flash",
1099 "[file-name] [destination [source]]\n"
Pali Rohárb4188202021-01-27 11:56:02 +01001100 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
1101 "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
1102 "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001103 "Examples:\n"
1104 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
1105 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
1106 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
1107
1108);