blob: e3f21dd0d81521970c4ebbbfc1d80568b7722a15 [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>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02008#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06009#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070010#include <image.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <net.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020012#include <vsprintf.h>
13#include <errno.h>
14#include <dm.h>
Pali Rohárd83e04a2022-08-23 14:52:24 +020015#include <fuse.h>
16#include <mach/efuse.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020017
18#include <spi_flash.h>
19#include <spi.h>
20#include <nand.h>
Pali Rohár2a27fc22023-01-22 01:25:12 +010021#include <scsi.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020022#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
Pali Roháreafdbe52023-03-11 11:44:27 +0100225 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Pali Roháre4e67772023-01-21 22:58:28 +0100226 if (part == 7)
227 part = 0;
228
229#ifdef CONFIG_BLK
230 err = blk_dselect_hwpart(blk_desc, part);
231#else
232 err = mmc_switch_part(mmc, part);
233#endif
234
235 if (err) {
236 printf("Error - MMC partition switch failed\n");
237 return err;
238 }
239#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200240
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200241 /* SD reserves LBA-0 for MBR and boots from LBA-1,
Josua Mayerfaf0ac52023-10-25 10:22:54 +0200242 * MMC/eMMC boots from LBA-0 and LBA-4096
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200243 */
Josua Mayerfaf0ac52023-10-25 10:22:54 +0200244 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;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200252#ifdef CONFIG_BLK
253 blk_count = image_size / mmc->write_bl_len;
254 if (image_size % mmc->write_bl_len)
255 blk_count += 1;
256
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200257 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
258 (void *)get_load_addr());
259#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200260 blk_count = image_size / mmc->block_dev.blksz;
261 if (image_size % mmc->block_dev.blksz)
262 blk_count += 1;
263
264 blk_written = mmc->block_dev.block_write(mmc_dev_num,
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200265 start_lba, blk_count,
266 (void *)get_load_addr());
267#endif /* CONFIG_BLK */
Pali Roháre4e67772023-01-21 22:58:28 +0100268
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
278
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200279 if (blk_written != blk_count) {
280 printf("Error - written %#lx blocks\n", blk_written);
281 return -ENOSPC;
282 }
283 printf("Done!\n");
284
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200285 return 0;
286}
287
288static size_t mmc_read_file(const char *file_name)
289{
290 loff_t act_read = 0;
291 int rc;
292 struct mmc *mmc;
293 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
294
295 mmc = find_mmc_device(mmc_dev_num);
296 if (!mmc) {
297 printf("No SD/MMC/eMMC card found\n");
298 return 0;
299 }
300
301 if (mmc_init(mmc)) {
302 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
303 mmc_dev_num);
304 return 0;
305 }
306
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;
311 }
312
313 /* Perfrom file read */
314 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
315 if (rc)
316 return 0;
317
318 return act_read;
319}
320
321static int is_mmc_active(void)
322{
323 return 1;
324}
325#else /* CONFIG_DM_MMC */
326static int mmc_burn_image(size_t image_size)
327{
328 return -ENODEV;
329}
330
331static size_t mmc_read_file(const char *file_name)
332{
333 return 0;
334}
335
336static int is_mmc_active(void)
337{
338 return 0;
339}
340#endif /* CONFIG_DM_MMC */
341
342/********************************************************************
Pali Rohár2a27fc22023-01-22 01:25:12 +0100343 * SATA services
344 ********************************************************************/
345#if defined(CONFIG_SCSI) && defined(CONFIG_BLK)
346static int sata_burn_image(size_t image_size)
347{
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
357
358 scsi_scan(false);
359
360 blk_desc = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
361 if (!blk_desc)
362 return -ENODEV;
363
364#ifdef CONFIG_ARMADA_3700
365 /*
366 * 64-bit Armada 3700 BootROM loads SATA firmware from
367 * GPT 'Marvell Armada 3700 Boot partition' or from
368 * MBR 'M' (0x4d) partition.
369 */
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;
378 }
379 if (part > 4) {
380 printf("Error - cannot find MBR 'M' (0x4d) partition on SATA disk\n");
381 return -ENODEV;
382 }
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;
393 }
394 if (part > 64) {
395 printf("Error - cannot find GPT 'Marvell Armada 3700 Boot partition' on SATA disk\n");
396 return -ENODEV;
397 }
398 start_lba = info.start;
399 break;
400 default:
401 printf("Error - no partitions on SATA disk\n");
402 return -ENODEV;
403 }
404#else
405 /* 32-bit Armada BootROM loads SATA firmware from the sector 1. */
406 start_lba = 1;
407#endif
408
409 blk_count = image_size / blk_desc->blksz;
410 if (image_size % blk_desc->blksz)
411 blk_count += 1;
412
413 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
414 (void *)get_load_addr());
415
416 if (blk_written != blk_count) {
417 printf("Error - written %#lx blocks\n", blk_written);
418 return -ENOSPC;
419 }
420
421 printf("Done!\n");
422 return 0;
423#else
424 return -ENODEV;
425#endif
426}
427
Pali Rohárc61feb92023-01-21 23:29:36 +0100428static size_t sata_read_file(const char *file_name)
429{
430 loff_t act_read = 0;
431 struct udevice *dev;
432 int rc;
433
434 /* try to recognize storage devices immediately */
435 scsi_scan(false);
436
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;
442 }
443
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;
448 }
449
450 /* Perfrom file read */
451 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
452 if (rc)
453 return 0;
454
455 return act_read;
456}
457
Pali Rohár2a27fc22023-01-22 01:25:12 +0100458static int is_sata_active(void)
459{
460 return 1;
461}
462#else /* CONFIG_SCSI */
463static int sata_burn_image(size_t image_size)
464{
465 return -ENODEV;
466}
467
Pali Rohárc61feb92023-01-21 23:29:36 +0100468static size_t sata_read_file(const char *file_name)
469{
470 return 0;
471}
472
Pali Rohár2a27fc22023-01-22 01:25:12 +0100473static int is_sata_active(void)
474{
475 return 0;
476}
477#endif /* CONFIG_SCSI */
478
479/********************************************************************
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200480 * SPI services
481 ********************************************************************/
482#ifdef CONFIG_SPI_FLASH
483static int spi_burn_image(size_t image_size)
484{
485 int ret;
486 struct spi_flash *flash;
487 u32 erase_bytes;
488
489 /* Probe the SPI bus to get the flash device */
Tom Rini119d2fb2021-12-11 14:55:48 -0500490 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
491 CONFIG_SF_DEFAULT_CS,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200492 CONFIG_SF_DEFAULT_SPEED,
493 CONFIG_SF_DEFAULT_MODE);
494 if (!flash) {
495 printf("Failed to probe SPI Flash\n");
496 return -ENOMEDIUM;
497 }
498
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200499 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");
508
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");
516
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200517 return ret;
518}
519
520static int is_spi_active(void)
521{
522 return 1;
523}
524
525#else /* CONFIG_SPI_FLASH */
526static int spi_burn_image(size_t image_size)
527{
528 return -ENODEV;
529}
530
531static int is_spi_active(void)
532{
533 return 0;
534}
535#endif /* CONFIG_SPI_FLASH */
536
537/********************************************************************
538 * NAND services
539 ********************************************************************/
540#ifdef CONFIG_CMD_NAND
541static int nand_burn_image(size_t image_size)
542{
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300543 int ret;
544 uint32_t block_size;
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500545 struct mtd_info *mtd;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200546
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500547 mtd = get_nand_dev_by_index(nand_curr_device);
548 if (!mtd) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200549 puts("\nno devices available\n");
550 return -ENOMEDIUM;
551 }
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500552 block_size = mtd->erasesize;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200553
554 /* Align U-Boot size to currently used blocksize */
555 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
556
Joel Johnson22d78e42020-04-17 09:38:05 -0600557 /* Erase the U-Boot image space */
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200558 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500559 ret = nand_erase(mtd, 0, image_size);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200560 if (ret) {
561 printf("Error!\n");
562 goto error;
563 }
564 printf("Done!\n");
565
566 /* Write the image to flash */
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300567 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
568 (int)image_size, get_load_addr());
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500569 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200570 if (ret)
571 printf("Error!\n");
572 else
573 printf("Done!\n");
574
575error:
576 return ret;
577}
578
579static int is_nand_active(void)
580{
581 return 1;
582}
583
584#else /* CONFIG_CMD_NAND */
585static int nand_burn_image(size_t image_size)
586{
587 return -ENODEV;
588}
589
590static int is_nand_active(void)
591{
592 return 0;
593}
594#endif /* CONFIG_CMD_NAND */
595
596/********************************************************************
597 * USB services
598 ********************************************************************/
599#if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
600static size_t usb_read_file(const char *file_name)
601{
602 loff_t act_read = 0;
603 struct udevice *dev;
604 int rc;
605
606 usb_stop();
607
608 if (usb_init() < 0) {
609 printf("Error: usb_init failed\n");
610 return 0;
611 }
612
613 /* Try to recognize storage devices immediately */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600614 blk_first_device(UCLASS_USB, &dev);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200615 if (!dev) {
616 printf("Error: USB storage device not found\n");
617 return 0;
618 }
619
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;
624 }
625
626 /* Perfrom file read */
627 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
628 if (rc)
629 return 0;
630
631 return act_read;
632}
633
634static int is_usb_active(void)
635{
636 return 1;
637}
638
639#else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
640static size_t usb_read_file(const char *file_name)
641{
642 return 0;
643}
644
645static int is_usb_active(void)
646{
647 return 0;
648}
649#endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
650
651/********************************************************************
652 * Network services
653 ********************************************************************/
654#ifdef CONFIG_CMD_NET
655static size_t tftp_read_file(const char *file_name)
656{
Pali Rohár2c96d022022-07-26 16:11:57 +0200657 int ret;
658
Simon Glass892265d2019-12-28 10:45:02 -0700659 /*
660 * update global variable image_load_addr before tftp file from network
661 */
662 image_load_addr = get_load_addr();
Pali Rohár2c96d022022-07-26 16:11:57 +0200663 ret = net_loop(TFTPGET);
664 return ret > 0 ? ret : 0;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200665}
666
667static int is_tftp_active(void)
668{
669 return 1;
670}
671
672#else
673static size_t tftp_read_file(const char *file_name)
674{
675 return 0;
676}
677
678static int is_tftp_active(void)
679{
680 return 0;
681}
682#endif /* CONFIG_CMD_NET */
683
684enum bubt_devices {
685 BUBT_DEV_NET = 0,
686 BUBT_DEV_USB,
687 BUBT_DEV_MMC,
Pali Rohár2a27fc22023-01-22 01:25:12 +0100688 BUBT_DEV_SATA,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200689 BUBT_DEV_SPI,
690 BUBT_DEV_NAND,
691
692 BUBT_MAX_DEV
693};
694
Pali Rohár4b255962023-01-10 22:47:17 +0100695static struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200696 {"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},
Pali Rohárc61feb92023-01-21 23:29:36 +0100699 {"sata", sata_read_file, sata_burn_image, is_sata_active},
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200700 {"spi", NULL, spi_burn_image, is_spi_active},
701 {"nand", NULL, nand_burn_image, is_nand_active},
702};
703
704static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
705{
706 if (!dst->write) {
707 printf("Error: Write not supported on device %s\n", dst->name);
708 return -ENOTSUPP;
709 }
710
711 return dst->write(image_size);
712}
713
714#if defined(CONFIG_ARMADA_8K)
Pali Rohár4b255962023-01-10 22:47:17 +0100715static u32 do_checksum32(u32 *start, int32_t len)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200716{
717 u32 sum = 0;
718 u32 *startp = start;
719
720 do {
721 sum += *startp;
722 startp++;
723 len -= 4;
724 } while (len > 0);
725
726 return sum;
727}
728
729static int check_image_header(void)
730{
731 struct mvebu_image_header *hdr =
732 (struct mvebu_image_header *)get_load_addr();
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200733 u32 checksum;
Pali Rohár0cebe102023-01-29 18:49:04 +0100734 u32 checksum_ref;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200735
736 /*
737 * For now compare checksum, and magic. Later we can
738 * verify more stuff on the header like interface type, etc
739 */
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;
744 }
745
Pali Rohár0cebe102023-01-29 18:49:04 +0100746 checksum_ref = hdr->prolog_checksum;
747 checksum = do_checksum32((u32 *)hdr, hdr->prolog_size);
Pali Rohárb4a76fd2023-01-29 18:38:11 +0100748 checksum -= hdr->prolog_checksum;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200749 if (checksum != checksum_ref) {
Pali Rohár0cebe102023-01-29 18:49:04 +0100750 printf("Error: Bad Prolog checksum. 0x%x != 0x%x\n",
751 checksum, checksum_ref);
752 return -ENOEXEC;
753 }
754
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) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200758 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
759 checksum, checksum_ref);
760 return -ENOEXEC;
761 }
762
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200763 printf("Image checksum...OK!\n");
764
765 return 0;
766}
767#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
768static int check_image_header(void)
769{
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];
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200774 u8 hash_512_output[SHA512_SUM_LEN];
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200775 sha1_context hash1_text;
776 sha256_context hash256_text;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200777 sha512_context hash512_text;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200778 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;
788
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;
794 }
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;
799 }
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);
811
812 if (image_num == 0) {
813 /*
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)
823 */
824 memcpy(internal_hash, hash_value,
825 sizeof(internal_hash));
826 memset(hash_value, 0, sizeof(internal_hash));
827 }
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;
833 }
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;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200847 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;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200853 default:
854 printf("Error: Unsupported hash_algorithm_id = %d\n",
855 hash_algorithm_id);
856 return -ENOEXEC;
857 }
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;
865 }
866 }
867 printf("Image checksum...OK!\n");
868
Joel Johnson37b9c122020-04-17 09:38:04 -0600869 return 0;
870}
Pali Rohár1ce57b82022-07-26 16:11:56 +0200871#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600872static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
873{
874 if (h->version == 1)
875 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
876
877 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
878 h->version);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200879 return 0;
880}
881
Joel Johnson37b9c122020-04-17 09:38:04 -0600882static uint8_t image_checksum8(const void *start, size_t len)
883{
884 u8 csum = 0;
885 const u8 *p = start;
886
887 while (len) {
888 csum += *p;
889 ++p;
890 --len;
891 }
892
893 return csum;
894}
895
Pali Rohár05781ff2022-08-23 14:52:23 +0200896static uint32_t image_checksum32(const void *start, size_t len)
897{
898 u32 csum = 0;
899 const u32 *p = start;
900
901 while (len) {
902 csum += *p;
903 ++p;
904 len -= sizeof(u32);
905 }
906
907 return csum;
908}
909
Joel Johnson37b9c122020-04-17 09:38:04 -0600910static int check_image_header(void)
911{
912 u8 checksum;
Pali Rohár05781ff2022-08-23 14:52:23 +0200913 u32 checksum32, exp_checksum32;
914 u32 offset, size;
Joel Johnson37b9c122020-04-17 09:38:04 -0600915 const struct a38x_main_hdr_v1 *hdr =
916 (struct a38x_main_hdr_v1 *)get_load_addr();
Pali Rohárfc2b9142023-01-08 14:31:28 +0100917 const size_t hdr_size = a38x_header_size(hdr);
Joel Johnson37b9c122020-04-17 09:38:04 -0600918
Pali Rohárfc2b9142023-01-08 14:31:28 +0100919 if (!hdr_size)
Joel Johnson37b9c122020-04-17 09:38:04 -0600920 return -ENOEXEC;
921
Pali Rohárfc2b9142023-01-08 14:31:28 +0100922 checksum = image_checksum8(hdr, hdr_size);
Joel Johnson37b9c122020-04-17 09:38:04 -0600923 checksum -= hdr->checksum;
924 if (checksum != hdr->checksum) {
Pali Rohár05781ff2022-08-23 14:52:23 +0200925 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
Joel Johnson37b9c122020-04-17 09:38:04 -0600926 checksum, hdr->checksum);
927 return -ENOEXEC;
928 }
929
Pali Rohár05781ff2022-08-23 14:52:23 +0200930 offset = le32_to_cpu(hdr->srcaddr);
931 size = le32_to_cpu(hdr->blocksize);
932
Pali Roháre3736072023-04-11 20:35:51 +0200933 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;
937 }
Pali Rohár05781ff2022-08-23 14:52:23 +0200938
Pali Rohár05781ff2022-08-23 14:52:23 +0200939 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
940 printf("Error: Bad A38x image blocksize.\n");
941 return -ENOEXEC;
942 }
943
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;
950 }
951
Joel Johnson37b9c122020-04-17 09:38:04 -0600952 printf("Image checksum...OK!\n");
953 return 0;
954}
Pali Rohárd83e04a2022-08-23 14:52:24 +0200955
956#if defined(CONFIG_ARMADA_38X)
957static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr)
958{
Pali Rohárfc2b9142023-01-08 14:31:28 +0100959 const size_t hdr_size = a38x_header_size(hdr);
Pali Rohárd83e04a2022-08-23 14:52:24 +0200960 struct a38x_opt_hdr_v1 *ohdr;
961 u32 ohdr_size;
962
963 if (hdr->version != 1)
964 return 0;
965
966 if (!hdr->ext)
967 return 0;
968
969 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
970 do {
971 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
972 return 1;
973
974 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
975
976 if (!*((u8 *)ohdr + ohdr_size - 4))
977 break;
978
979 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
Pali Rohárfc2b9142023-01-08 14:31:28 +0100980 if ((u8 *)ohdr >= (u8 *)hdr + hdr_size)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200981 break;
982 } while (1);
983
984 return 0;
985}
986#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200987#else /* Not ARMADA? */
988static int check_image_header(void)
989{
990 printf("bubt cmd does not support this SoC device or family!\n");
991 return -ENOEXEC;
992}
993#endif
994
Pali Rohár85f2a7c2023-02-20 22:42:54 +0100995#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200996static u64 fuse_read_u64(u32 bank)
997{
998 u32 val[2];
999 int ret;
1000
1001 ret = fuse_read(bank, 0, &val[0]);
1002 if (ret < 0)
1003 return -1;
1004
1005 ret = fuse_read(bank, 1, &val[1]);
1006 if (ret < 0)
1007 return -1;
1008
1009 return ((u64)val[1] << 32) | val[0];
1010}
1011#endif
1012
1013#if defined(CONFIG_ARMADA_3700)
1014static inline u8 maj3(u8 val)
1015{
1016 /* return majority vote of 3 bits */
1017 return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0;
1018}
1019#endif
1020
Joel Johnsonab5dd302020-04-17 09:38:06 -06001021static int bubt_check_boot_mode(const struct bubt_dev *dst)
1022{
Pali Rohár1ce57b82022-07-26 16:11:56 +02001023#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
Pali Rohár85f2a7c2023-02-20 22:42:54 +01001024 int mode;
1025#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
1026 int secure_mode;
1027#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +02001028#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;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001033 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);
Pali Rohár1ce57b82022-07-26 16:11:56 +02001041#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;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001046#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
Pali Rohár1ce57b82022-07-26 16:11:56 +02001052#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -06001053
Pali Rohár1ce57b82022-07-26 16:11:56 +02001054 for (mode = 0; boot_modes[mode].name; mode++) {
1055 if (boot_modes[mode].id == id)
1056 break;
1057 }
Joel Johnson27e9d072020-04-17 09:38:08 -06001058
Pali Rohár1ce57b82022-07-26 16:11:56 +02001059 if (!boot_modes[mode].name) {
1060 printf("Error: unknown boot device in image header: 0x%x\n", id);
Joel Johnsonab5dd302020-04-17 09:38:06 -06001061 return -ENOEXEC;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001062 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001063
Pali Rohárd83e04a2022-08-23 14:52:24 +02001064 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;
1068 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001069
Pali Rohárd83e04a2022-08-23 14:52:24 +02001070#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)
1080 /*
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.
1083 */
1084 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
1085 return -ENOEXEC;
Pali Rohár1ce57b82022-07-26 16:11:56 +02001086#endif
Pali Rohárd83e04a2022-08-23 14:52:24 +02001087 } 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;
1091 }
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;
1095 }
1096 }
1097#endif
1098#endif
1099 return 0;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001100}
1101
1102static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001103{
1104 int err;
1105
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;
1111 }
1112
Joel Johnsonab5dd302020-04-17 09:38:06 -06001113 err = bubt_check_boot_mode(dst);
1114 if (err) {
1115 printf("Error: Image boot mode verification failed\n");
1116 return err;
1117 }
1118
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001119 return 0;
1120}
1121
1122static int bubt_read_file(struct bubt_dev *src)
1123{
1124 size_t image_size;
1125
1126 if (!src->read) {
1127 printf("Error: Read not supported on device \"%s\"\n",
1128 src->name);
1129 return 0;
1130 }
1131
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;
1137 }
1138
1139 return image_size;
1140}
1141
1142static int bubt_is_dev_active(struct bubt_dev *dev)
1143{
1144 if (!dev->active) {
Joel Johnson22d78e42020-04-17 09:38:05 -06001145 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001146 dev->name);
1147 return 0;
1148 }
1149
1150 if (!dev->active()) {
1151 printf("Device \"%s\" is inactive\n", dev->name);
1152 return 0;
1153 }
1154
1155 return 1;
1156}
1157
Pali Rohár4b255962023-01-10 22:47:17 +01001158static struct bubt_dev *find_bubt_dev(char *dev_name)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001159{
1160 int dev;
1161
1162 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
1163 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
1164 return &bubt_devs[dev];
1165 }
1166
1167 return 0;
1168}
1169
1170#define DEFAULT_BUBT_SRC "tftp"
1171
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"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001179#elif defined(CONFIG_MVEBU_SATA_BOOT)
1180#define DEFAULT_BUBT_DST "sata"
Konstantin Porotchkine78a3892021-03-17 18:53:43 +02001181#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001182#define DEFAULT_BUBT_DST "error"
1183#endif
1184#endif /* DEFAULT_BUBT_DST */
1185
Pali Rohár4b255962023-01-10 22:47:17 +01001186static int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001187{
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;
1194
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));
1202
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);
1208 }
1209
1210 if (argc >= 4)
1211 strncpy(src_dev_name, argv[3], 8);
1212 else
1213 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1214
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);
Pali Rohár873fc6a2022-07-26 16:11:59 +02001219 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001220 }
1221
1222 if (!bubt_is_dev_active(dst))
Pali Rohár873fc6a2022-07-26 16:11:59 +02001223 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001224
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;
1230 }
1231
1232 if (!bubt_is_dev_active(src))
1233 return -ENODEV;
1234
Joel Johnson22d78e42020-04-17 09:38:05 -06001235 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001236 net_boot_file_name, src->name, dst->name);
1237
1238 image_size = bubt_read_file(src);
1239 if (!image_size)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001240 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001241
Joel Johnsonab5dd302020-04-17 09:38:06 -06001242 err = bubt_verify(dst);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001243 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001244 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001245
1246 err = bubt_write_file(dst, image_size);
1247 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001248 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001249
1250 return 0;
1251}
1252
1253U_BOOT_CMD(
1254 bubt, 4, 0, do_bubt_cmd,
1255 "Burn a u-boot image to flash",
1256 "[file-name] [destination [source]]\n"
Pali Rohárb4188202021-01-27 11:56:02 +01001257 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001258 "\t-destination Flash to burn to [spi, nand, mmc, sata]. Default = " DEFAULT_BUBT_DST "\n"
Pali Rohárc61feb92023-01-21 23:29:36 +01001259 "\t-source The source to load image from [tftp, usb, mmc, sata]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001260 "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"
1264
1265);