blob: 37ff9c455228805a01c1c8c2ed59a7673f87115a [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>
Pali Rohár2a27fc22023-01-22 01:25:12 +010022#include <scsi.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020023#include <usb.h>
24#include <fs.h>
25#include <mmc.h>
Konstantin Porotchkina89c0552017-01-08 16:52:06 +020026#ifdef CONFIG_BLK
27#include <blk.h>
28#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020029#include <u-boot/sha1.h>
30#include <u-boot/sha256.h>
Pali Rohára4bbb2c2022-07-26 16:11:58 +020031#include <u-boot/sha512.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020032
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020033#if defined(CONFIG_ARMADA_8K)
34#define MAIN_HDR_MAGIC 0xB105B002
35
36struct mvebu_image_header {
37 u32 magic; /* 0-3 */
38 u32 prolog_size; /* 4-7 */
39 u32 prolog_checksum; /* 8-11 */
40 u32 boot_image_size; /* 12-15 */
41 u32 boot_image_checksum; /* 16-19 */
42 u32 rsrvd0; /* 20-23 */
43 u32 load_addr; /* 24-27 */
44 u32 exec_addr; /* 28-31 */
45 u8 uart_cfg; /* 32 */
46 u8 baudrate; /* 33 */
47 u8 ext_count; /* 34 */
48 u8 aux_flags; /* 35 */
49 u32 io_arg_0; /* 36-39 */
50 u32 io_arg_1; /* 40-43 */
51 u32 io_arg_2; /* 43-47 */
52 u32 io_arg_3; /* 48-51 */
53 u32 rsrvd1; /* 52-55 */
54 u32 rsrvd2; /* 56-59 */
55 u32 rsrvd3; /* 60-63 */
56};
57#elif defined(CONFIG_ARMADA_3700) /* A3700 */
58#define HASH_SUM_LEN 16
59#define IMAGE_VERSION_3_6_0 0x030600
60#define IMAGE_VERSION_3_5_0 0x030500
61
Pali Rohár1ce57b82022-07-26 16:11:56 +020062struct tim_boot_flash_sign {
63 unsigned int id;
64 const char *name;
65};
66
67struct tim_boot_flash_sign tim_boot_flash_signs[] = {
68 { 0x454d4d08, "mmc" },
69 { 0x454d4d0b, "mmc" },
70 { 0x5350490a, "spi" },
71 { 0x5350491a, "nand" },
72 { 0x55415223, "uart" },
73 { 0x53415432, "sata" },
74 {},
75};
76
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020077struct common_tim_data {
78 u32 version;
79 u32 identifier;
80 u32 trusted;
81 u32 issue_date;
82 u32 oem_unique_id;
83 u32 reserved[5]; /* Reserve 20 bytes */
84 u32 boot_flash_sign;
85 u32 num_images;
86 u32 num_keys;
87 u32 size_of_reserved;
88};
89
90struct mvebu_image_info {
91 u32 image_id;
92 u32 next_image_id;
93 u32 flash_entry_addr;
94 u32 load_addr;
95 u32 image_size;
96 u32 image_size_to_hash;
97 u32 hash_algorithm_id;
98 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
99 u32 partition_number;
100 u32 enc_algorithm_id;
101 u32 encrypt_start_offset;
102 u32 encrypt_size;
103};
Pali Rohár1ce57b82022-07-26 16:11:56 +0200104#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600105
Pali Rohár0f7df222021-10-22 12:41:10 +0200106/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Joel Johnson37b9c122020-04-17 09:38:04 -0600107struct a38x_main_hdr_v1 {
108 u8 blockid; /* 0x0 */
109 u8 flags; /* 0x1 */
Pali Rohár7fea8842021-10-22 12:37:48 +0200110 u16 nandpagesize; /* 0x2-0x3 */
Joel Johnson37b9c122020-04-17 09:38:04 -0600111 u32 blocksize; /* 0x4-0x7 */
112 u8 version; /* 0x8 */
113 u8 headersz_msb; /* 0x9 */
114 u16 headersz_lsb; /* 0xA-0xB */
115 u32 srcaddr; /* 0xC-0xF */
116 u32 destaddr; /* 0x10-0x13 */
117 u32 execaddr; /* 0x14-0x17 */
118 u8 options; /* 0x18 */
119 u8 nandblocksize; /* 0x19 */
120 u8 nandbadblklocation; /* 0x1A */
121 u8 reserved4; /* 0x1B */
122 u16 reserved5; /* 0x1C-0x1D */
123 u8 ext; /* 0x1E */
124 u8 checksum; /* 0x1F */
125};
Joel Johnsonab5dd302020-04-17 09:38:06 -0600126
Pali Rohárd83e04a2022-08-23 14:52:24 +0200127/*
128 * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x)
129 */
130struct a38x_opt_hdr_v1 {
131 u8 headertype;
132 u8 headersz_msb;
133 u16 headersz_lsb;
134 u8 data[0];
135};
136#define A38X_OPT_HDR_V1_SECURE_TYPE 0x1
137
Joel Johnsonab5dd302020-04-17 09:38:06 -0600138struct a38x_boot_mode {
139 unsigned int id;
140 const char *name;
141};
142
143/* The blockid header field values used to indicate boot device of image */
144struct a38x_boot_mode a38x_boot_modes[] = {
145 { 0x4D, "i2c" },
146 { 0x5A, "spi" },
147 { 0x69, "uart" },
148 { 0x78, "sata" },
149 { 0x8B, "nand" },
150 { 0x9C, "pex" },
151 { 0xAE, "mmc" },
152 {},
153};
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200154
Pali Rohár1ce57b82022-07-26 16:11:56 +0200155#endif
156
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200157struct bubt_dev {
158 char name[8];
159 size_t (*read)(const char *file_name);
160 int (*write)(size_t image_size);
161 int (*active)(void);
162};
163
164static ulong get_load_addr(void)
165{
166 const char *addr_str;
167 unsigned long addr;
168
Simon Glass64b723f2017-08-03 12:22:12 -0600169 addr_str = env_get("loadaddr");
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200170 if (addr_str)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600171 addr = hextoul(addr_str, NULL);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200172 else
173 addr = CONFIG_SYS_LOAD_ADDR;
174
175 return addr;
176}
177
178/********************************************************************
179 * eMMC services
180 ********************************************************************/
Jean-Jacques Hiblotd0531672018-01-04 15:23:32 +0100181#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200182static int mmc_burn_image(size_t image_size)
183{
184 struct mmc *mmc;
185 lbaint_t start_lba;
186 lbaint_t blk_count;
187 ulong blk_written;
188 int err;
189 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200190#ifdef CONFIG_BLK
191 struct blk_desc *blk_desc;
192#endif
Pali Roháre4e67772023-01-21 22:58:28 +0100193#ifdef CONFIG_SUPPORT_EMMC_BOOT
194 u8 part;
195 u8 orig_part;
196#endif
197
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200198 mmc = find_mmc_device(mmc_dev_num);
199 if (!mmc) {
200 printf("No SD/MMC/eMMC card found\n");
201 return -ENOMEDIUM;
202 }
203
204 err = mmc_init(mmc);
205 if (err) {
206 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
207 mmc_dev_num);
208 return err;
209 }
Pali Roháre4e67772023-01-21 22:58:28 +0100210
211#ifdef CONFIG_BLK
212 blk_desc = mmc_get_blk_desc(mmc);
213 if (!blk_desc) {
214 printf("Error - failed to obtain block descriptor\n");
215 return -ENODEV;
216 }
217#endif
218
219#ifdef CONFIG_SUPPORT_EMMC_BOOT
220#ifdef CONFIG_BLK
221 orig_part = blk_desc->hwpart;
222#else
223 orig_part = mmc->block_dev.hwpart;
224#endif
225
Pali Roháreafdbe52023-03-11 11:44:27 +0100226 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
Pali Roháre4e67772023-01-21 22:58:28 +0100227 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/********************************************************************
Pali Rohár2a27fc22023-01-22 01:25:12 +0100337 * SATA services
338 ********************************************************************/
339#if defined(CONFIG_SCSI) && defined(CONFIG_BLK)
340static int sata_burn_image(size_t image_size)
341{
342#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
343 lbaint_t start_lba;
344 lbaint_t blk_count;
345 ulong blk_written;
346 struct blk_desc *blk_desc;
347#ifdef CONFIG_ARMADA_3700
348 struct disk_partition info;
349 int part;
350#endif
351
352 scsi_scan(false);
353
354 blk_desc = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
355 if (!blk_desc)
356 return -ENODEV;
357
358#ifdef CONFIG_ARMADA_3700
359 /*
360 * 64-bit Armada 3700 BootROM loads SATA firmware from
361 * GPT 'Marvell Armada 3700 Boot partition' or from
362 * MBR 'M' (0x4d) partition.
363 */
364 switch (blk_desc->part_type) {
365 case PART_TYPE_DOS:
366 for (part = 1; part <= 4; part++) {
367 info.sys_ind = 0;
368 if (part_get_info(blk_desc, part, &info))
369 continue;
370 if (info.sys_ind == 'M')
371 break;
372 }
373 if (part > 4) {
374 printf("Error - cannot find MBR 'M' (0x4d) partition on SATA disk\n");
375 return -ENODEV;
376 }
377 start_lba = info.start;
378 break;
379 case PART_TYPE_EFI:
380 for (part = 1; part <= 64; part++) {
381 info.type_guid[0] = 0;
382 if (part_get_info(blk_desc, part, &info))
383 continue;
384 /* Check for GPT type GUID of 'Marvell Armada 3700 Boot partition' */
385 if (strcmp(info.type_guid, "6828311A-BA55-42A4-BCDE-A89BB5EDECAE") == 0)
386 break;
387 }
388 if (part > 64) {
389 printf("Error - cannot find GPT 'Marvell Armada 3700 Boot partition' on SATA disk\n");
390 return -ENODEV;
391 }
392 start_lba = info.start;
393 break;
394 default:
395 printf("Error - no partitions on SATA disk\n");
396 return -ENODEV;
397 }
398#else
399 /* 32-bit Armada BootROM loads SATA firmware from the sector 1. */
400 start_lba = 1;
401#endif
402
403 blk_count = image_size / blk_desc->blksz;
404 if (image_size % blk_desc->blksz)
405 blk_count += 1;
406
407 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
408 (void *)get_load_addr());
409
410 if (blk_written != blk_count) {
411 printf("Error - written %#lx blocks\n", blk_written);
412 return -ENOSPC;
413 }
414
415 printf("Done!\n");
416 return 0;
417#else
418 return -ENODEV;
419#endif
420}
421
Pali Rohárc61feb92023-01-21 23:29:36 +0100422static size_t sata_read_file(const char *file_name)
423{
424 loff_t act_read = 0;
425 struct udevice *dev;
426 int rc;
427
428 /* try to recognize storage devices immediately */
429 scsi_scan(false);
430
431 /* Try to recognize storage devices immediately */
432 blk_first_device(UCLASS_SCSI, &dev);
433 if (!dev) {
434 printf("Error: SATA device not found\n");
435 return 0;
436 }
437
438 /* Always load from scsi 0 */
439 if (fs_set_blk_dev("scsi", "0", FS_TYPE_ANY)) {
440 printf("Error: SATA 0 not found\n");
441 return 0;
442 }
443
444 /* Perfrom file read */
445 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
446 if (rc)
447 return 0;
448
449 return act_read;
450}
451
Pali Rohár2a27fc22023-01-22 01:25:12 +0100452static int is_sata_active(void)
453{
454 return 1;
455}
456#else /* CONFIG_SCSI */
457static int sata_burn_image(size_t image_size)
458{
459 return -ENODEV;
460}
461
Pali Rohárc61feb92023-01-21 23:29:36 +0100462static size_t sata_read_file(const char *file_name)
463{
464 return 0;
465}
466
Pali Rohár2a27fc22023-01-22 01:25:12 +0100467static int is_sata_active(void)
468{
469 return 0;
470}
471#endif /* CONFIG_SCSI */
472
473/********************************************************************
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200474 * SPI services
475 ********************************************************************/
476#ifdef CONFIG_SPI_FLASH
477static int spi_burn_image(size_t image_size)
478{
479 int ret;
480 struct spi_flash *flash;
481 u32 erase_bytes;
482
483 /* Probe the SPI bus to get the flash device */
Tom Rini119d2fb2021-12-11 14:55:48 -0500484 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
485 CONFIG_SF_DEFAULT_CS,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200486 CONFIG_SF_DEFAULT_SPEED,
487 CONFIG_SF_DEFAULT_MODE);
488 if (!flash) {
489 printf("Failed to probe SPI Flash\n");
490 return -ENOMEDIUM;
491 }
492
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200493 erase_bytes = image_size +
494 (flash->erase_size - image_size % flash->erase_size);
495 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
496 erase_bytes, erase_bytes / flash->erase_size);
497 ret = spi_flash_erase(flash, 0, erase_bytes);
498 if (ret)
499 printf("Error!\n");
500 else
501 printf("Done!\n");
502
503 printf("Writing %d bytes from 0x%lx to offset 0 ...",
504 (int)image_size, get_load_addr());
505 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
506 if (ret)
507 printf("Error!\n");
508 else
509 printf("Done!\n");
510
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200511 return ret;
512}
513
514static int is_spi_active(void)
515{
516 return 1;
517}
518
519#else /* CONFIG_SPI_FLASH */
520static int spi_burn_image(size_t image_size)
521{
522 return -ENODEV;
523}
524
525static int is_spi_active(void)
526{
527 return 0;
528}
529#endif /* CONFIG_SPI_FLASH */
530
531/********************************************************************
532 * NAND services
533 ********************************************************************/
534#ifdef CONFIG_CMD_NAND
535static int nand_burn_image(size_t image_size)
536{
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300537 int ret;
538 uint32_t block_size;
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500539 struct mtd_info *mtd;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200540
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500541 mtd = get_nand_dev_by_index(nand_curr_device);
542 if (!mtd) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200543 puts("\nno devices available\n");
544 return -ENOMEDIUM;
545 }
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500546 block_size = mtd->erasesize;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200547
548 /* Align U-Boot size to currently used blocksize */
549 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
550
Joel Johnson22d78e42020-04-17 09:38:05 -0600551 /* Erase the U-Boot image space */
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200552 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500553 ret = nand_erase(mtd, 0, image_size);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200554 if (ret) {
555 printf("Error!\n");
556 goto error;
557 }
558 printf("Done!\n");
559
560 /* Write the image to flash */
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300561 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
562 (int)image_size, get_load_addr());
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500563 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200564 if (ret)
565 printf("Error!\n");
566 else
567 printf("Done!\n");
568
569error:
570 return ret;
571}
572
573static int is_nand_active(void)
574{
575 return 1;
576}
577
578#else /* CONFIG_CMD_NAND */
579static int nand_burn_image(size_t image_size)
580{
581 return -ENODEV;
582}
583
584static int is_nand_active(void)
585{
586 return 0;
587}
588#endif /* CONFIG_CMD_NAND */
589
590/********************************************************************
591 * USB services
592 ********************************************************************/
593#if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
594static size_t usb_read_file(const char *file_name)
595{
596 loff_t act_read = 0;
597 struct udevice *dev;
598 int rc;
599
600 usb_stop();
601
602 if (usb_init() < 0) {
603 printf("Error: usb_init failed\n");
604 return 0;
605 }
606
607 /* Try to recognize storage devices immediately */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600608 blk_first_device(UCLASS_USB, &dev);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200609 if (!dev) {
610 printf("Error: USB storage device not found\n");
611 return 0;
612 }
613
614 /* Always load from usb 0 */
615 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
616 printf("Error: USB 0 not found\n");
617 return 0;
618 }
619
620 /* Perfrom file read */
621 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
622 if (rc)
623 return 0;
624
625 return act_read;
626}
627
628static int is_usb_active(void)
629{
630 return 1;
631}
632
633#else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
634static size_t usb_read_file(const char *file_name)
635{
636 return 0;
637}
638
639static int is_usb_active(void)
640{
641 return 0;
642}
643#endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
644
645/********************************************************************
646 * Network services
647 ********************************************************************/
648#ifdef CONFIG_CMD_NET
649static size_t tftp_read_file(const char *file_name)
650{
Pali Rohár2c96d022022-07-26 16:11:57 +0200651 int ret;
652
Simon Glass892265d2019-12-28 10:45:02 -0700653 /*
654 * update global variable image_load_addr before tftp file from network
655 */
656 image_load_addr = get_load_addr();
Pali Rohár2c96d022022-07-26 16:11:57 +0200657 ret = net_loop(TFTPGET);
658 return ret > 0 ? ret : 0;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200659}
660
661static int is_tftp_active(void)
662{
663 return 1;
664}
665
666#else
667static size_t tftp_read_file(const char *file_name)
668{
669 return 0;
670}
671
672static int is_tftp_active(void)
673{
674 return 0;
675}
676#endif /* CONFIG_CMD_NET */
677
678enum bubt_devices {
679 BUBT_DEV_NET = 0,
680 BUBT_DEV_USB,
681 BUBT_DEV_MMC,
Pali Rohár2a27fc22023-01-22 01:25:12 +0100682 BUBT_DEV_SATA,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200683 BUBT_DEV_SPI,
684 BUBT_DEV_NAND,
685
686 BUBT_MAX_DEV
687};
688
Pali Rohár4b255962023-01-10 22:47:17 +0100689static struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200690 {"tftp", tftp_read_file, NULL, is_tftp_active},
691 {"usb", usb_read_file, NULL, is_usb_active},
692 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
Pali Rohárc61feb92023-01-21 23:29:36 +0100693 {"sata", sata_read_file, sata_burn_image, is_sata_active},
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200694 {"spi", NULL, spi_burn_image, is_spi_active},
695 {"nand", NULL, nand_burn_image, is_nand_active},
696};
697
698static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
699{
700 if (!dst->write) {
701 printf("Error: Write not supported on device %s\n", dst->name);
702 return -ENOTSUPP;
703 }
704
705 return dst->write(image_size);
706}
707
708#if defined(CONFIG_ARMADA_8K)
Pali Rohár4b255962023-01-10 22:47:17 +0100709static u32 do_checksum32(u32 *start, int32_t len)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200710{
711 u32 sum = 0;
712 u32 *startp = start;
713
714 do {
715 sum += *startp;
716 startp++;
717 len -= 4;
718 } while (len > 0);
719
720 return sum;
721}
722
723static int check_image_header(void)
724{
725 struct mvebu_image_header *hdr =
726 (struct mvebu_image_header *)get_load_addr();
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200727 u32 checksum;
Pali Rohár0cebe102023-01-29 18:49:04 +0100728 u32 checksum_ref;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200729
730 /*
731 * For now compare checksum, and magic. Later we can
732 * verify more stuff on the header like interface type, etc
733 */
734 if (hdr->magic != MAIN_HDR_MAGIC) {
735 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
736 hdr->magic, MAIN_HDR_MAGIC);
737 return -ENOEXEC;
738 }
739
Pali Rohár0cebe102023-01-29 18:49:04 +0100740 checksum_ref = hdr->prolog_checksum;
741 checksum = do_checksum32((u32 *)hdr, hdr->prolog_size);
Pali Rohárb4a76fd2023-01-29 18:38:11 +0100742 checksum -= hdr->prolog_checksum;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200743 if (checksum != checksum_ref) {
Pali Rohár0cebe102023-01-29 18:49:04 +0100744 printf("Error: Bad Prolog checksum. 0x%x != 0x%x\n",
745 checksum, checksum_ref);
746 return -ENOEXEC;
747 }
748
749 checksum_ref = hdr->boot_image_checksum;
750 checksum = do_checksum32((u32 *)((u8 *)hdr + hdr->prolog_size), hdr->boot_image_size);
751 if (checksum != checksum_ref) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200752 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
753 checksum, checksum_ref);
754 return -ENOEXEC;
755 }
756
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200757 printf("Image checksum...OK!\n");
758
759 return 0;
760}
761#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
762static int check_image_header(void)
763{
764 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
765 int image_num;
766 u8 hash_160_output[SHA1_SUM_LEN];
767 u8 hash_256_output[SHA256_SUM_LEN];
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200768 u8 hash_512_output[SHA512_SUM_LEN];
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200769 sha1_context hash1_text;
770 sha256_context hash256_text;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200771 sha512_context hash512_text;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200772 u8 *hash_output;
773 u32 hash_algorithm_id;
774 u32 image_size_to_hash;
775 u32 flash_entry_addr;
776 u32 *hash_value;
777 u32 internal_hash[HASH_SUM_LEN];
778 const u8 *buff;
779 u32 num_of_image = hdr->num_images;
780 u32 version = hdr->version;
781 u32 trusted = hdr->trusted;
782
783 /* bubt checksum validation only supports nontrusted images */
784 if (trusted == 1) {
785 printf("bypass image validation, ");
786 printf("only untrusted image is supported now\n");
787 return 0;
788 }
789 /* only supports image version 3.5 and 3.6 */
790 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
791 printf("Error: Unsupported Image version = 0x%08x\n", version);
792 return -ENOEXEC;
793 }
794 /* validate images hash value */
795 for (image_num = 0; image_num < num_of_image; image_num++) {
796 struct mvebu_image_info *info =
797 (struct mvebu_image_info *)(get_load_addr() +
798 sizeof(struct common_tim_data) +
799 image_num * sizeof(struct mvebu_image_info));
800 hash_algorithm_id = info->hash_algorithm_id;
801 image_size_to_hash = info->image_size_to_hash;
802 flash_entry_addr = info->flash_entry_addr;
803 hash_value = info->hash;
804 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
805
806 if (image_num == 0) {
807 /*
808 * The first image includes hash values in its content.
809 * For hash calculation, we need to save the original
810 * hash values to a local variable that will be
811 * copied back for comparsion and set all zeros to
812 * the orignal hash values for calculating new value.
813 * First image original format :
814 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
815 * Replaced first image format :
816 * x...x (datum1) 0...0(hash values) x...x(datum2)
817 */
818 memcpy(internal_hash, hash_value,
819 sizeof(internal_hash));
820 memset(hash_value, 0, sizeof(internal_hash));
821 }
822 if (image_size_to_hash == 0) {
823 printf("Warning: Image_%d hash checksum is disabled, ",
824 image_num);
825 printf("skip the image validation.\n");
826 continue;
827 }
828 switch (hash_algorithm_id) {
829 case SHA1_SUM_LEN:
830 sha1_starts(&hash1_text);
831 sha1_update(&hash1_text, buff, image_size_to_hash);
832 sha1_finish(&hash1_text, hash_160_output);
833 hash_output = hash_160_output;
834 break;
835 case SHA256_SUM_LEN:
836 sha256_starts(&hash256_text);
837 sha256_update(&hash256_text, buff, image_size_to_hash);
838 sha256_finish(&hash256_text, hash_256_output);
839 hash_output = hash_256_output;
840 break;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200841 case SHA512_SUM_LEN:
842 sha512_starts(&hash512_text);
843 sha512_update(&hash512_text, buff, image_size_to_hash);
844 sha512_finish(&hash512_text, hash_512_output);
845 hash_output = hash_512_output;
846 break;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200847 default:
848 printf("Error: Unsupported hash_algorithm_id = %d\n",
849 hash_algorithm_id);
850 return -ENOEXEC;
851 }
852 if (image_num == 0)
853 memcpy(hash_value, internal_hash,
854 sizeof(internal_hash));
855 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
856 printf("Error: Image_%d checksum is not correct\n",
857 image_num);
858 return -ENOEXEC;
859 }
860 }
861 printf("Image checksum...OK!\n");
862
Joel Johnson37b9c122020-04-17 09:38:04 -0600863 return 0;
864}
Pali Rohár1ce57b82022-07-26 16:11:56 +0200865#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600866static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
867{
868 if (h->version == 1)
869 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
870
871 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
872 h->version);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200873 return 0;
874}
875
Joel Johnson37b9c122020-04-17 09:38:04 -0600876static uint8_t image_checksum8(const void *start, size_t len)
877{
878 u8 csum = 0;
879 const u8 *p = start;
880
881 while (len) {
882 csum += *p;
883 ++p;
884 --len;
885 }
886
887 return csum;
888}
889
Pali Rohár05781ff2022-08-23 14:52:23 +0200890static uint32_t image_checksum32(const void *start, size_t len)
891{
892 u32 csum = 0;
893 const u32 *p = start;
894
895 while (len) {
896 csum += *p;
897 ++p;
898 len -= sizeof(u32);
899 }
900
901 return csum;
902}
903
Joel Johnson37b9c122020-04-17 09:38:04 -0600904static int check_image_header(void)
905{
906 u8 checksum;
Pali Rohár05781ff2022-08-23 14:52:23 +0200907 u32 checksum32, exp_checksum32;
908 u32 offset, size;
Joel Johnson37b9c122020-04-17 09:38:04 -0600909 const struct a38x_main_hdr_v1 *hdr =
910 (struct a38x_main_hdr_v1 *)get_load_addr();
Pali Rohárfc2b9142023-01-08 14:31:28 +0100911 const size_t hdr_size = a38x_header_size(hdr);
Joel Johnson37b9c122020-04-17 09:38:04 -0600912
Pali Rohárfc2b9142023-01-08 14:31:28 +0100913 if (!hdr_size)
Joel Johnson37b9c122020-04-17 09:38:04 -0600914 return -ENOEXEC;
915
Pali Rohárfc2b9142023-01-08 14:31:28 +0100916 checksum = image_checksum8(hdr, hdr_size);
Joel Johnson37b9c122020-04-17 09:38:04 -0600917 checksum -= hdr->checksum;
918 if (checksum != hdr->checksum) {
Pali Rohár05781ff2022-08-23 14:52:23 +0200919 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
Joel Johnson37b9c122020-04-17 09:38:04 -0600920 checksum, hdr->checksum);
921 return -ENOEXEC;
922 }
923
Pali Rohár05781ff2022-08-23 14:52:23 +0200924 offset = le32_to_cpu(hdr->srcaddr);
925 size = le32_to_cpu(hdr->blocksize);
926
Pali Rohár42cc7952023-01-21 13:59:20 +0100927 if (hdr->blockid == 0x78) /* SATA id */
Pali Rohár05781ff2022-08-23 14:52:23 +0200928 offset *= 512;
Pali Rohár05781ff2022-08-23 14:52:23 +0200929
Pali Rohár05781ff2022-08-23 14:52:23 +0200930 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
931 printf("Error: Bad A38x image blocksize.\n");
932 return -ENOEXEC;
933 }
934
935 checksum32 = image_checksum32((u8 *)hdr + offset, size - 4);
936 exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4);
937 if (checksum32 != exp_checksum32) {
938 printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n",
939 checksum32, exp_checksum32);
940 return -ENOEXEC;
941 }
942
Joel Johnson37b9c122020-04-17 09:38:04 -0600943 printf("Image checksum...OK!\n");
944 return 0;
945}
Pali Rohárd83e04a2022-08-23 14:52:24 +0200946
947#if defined(CONFIG_ARMADA_38X)
948static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr)
949{
Pali Rohárfc2b9142023-01-08 14:31:28 +0100950 const size_t hdr_size = a38x_header_size(hdr);
Pali Rohárd83e04a2022-08-23 14:52:24 +0200951 struct a38x_opt_hdr_v1 *ohdr;
952 u32 ohdr_size;
953
954 if (hdr->version != 1)
955 return 0;
956
957 if (!hdr->ext)
958 return 0;
959
960 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
961 do {
962 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
963 return 1;
964
965 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
966
967 if (!*((u8 *)ohdr + ohdr_size - 4))
968 break;
969
970 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
Pali Rohárfc2b9142023-01-08 14:31:28 +0100971 if ((u8 *)ohdr >= (u8 *)hdr + hdr_size)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200972 break;
973 } while (1);
974
975 return 0;
976}
977#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200978#else /* Not ARMADA? */
979static int check_image_header(void)
980{
981 printf("bubt cmd does not support this SoC device or family!\n");
982 return -ENOEXEC;
983}
984#endif
985
Pali Rohár85f2a7c2023-02-20 22:42:54 +0100986#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200987static u64 fuse_read_u64(u32 bank)
988{
989 u32 val[2];
990 int ret;
991
992 ret = fuse_read(bank, 0, &val[0]);
993 if (ret < 0)
994 return -1;
995
996 ret = fuse_read(bank, 1, &val[1]);
997 if (ret < 0)
998 return -1;
999
1000 return ((u64)val[1] << 32) | val[0];
1001}
1002#endif
1003
1004#if defined(CONFIG_ARMADA_3700)
1005static inline u8 maj3(u8 val)
1006{
1007 /* return majority vote of 3 bits */
1008 return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0;
1009}
1010#endif
1011
Joel Johnsonab5dd302020-04-17 09:38:06 -06001012static int bubt_check_boot_mode(const struct bubt_dev *dst)
1013{
Pali Rohár1ce57b82022-07-26 16:11:56 +02001014#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
Pali Rohár85f2a7c2023-02-20 22:42:54 +01001015 int mode;
1016#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
1017 int secure_mode;
1018#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +02001019#if defined(CONFIG_ARMADA_3700)
1020 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
1021 const struct common_tim_data *hdr =
1022 (struct common_tim_data *)get_load_addr();
1023 u32 id = hdr->boot_flash_sign;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001024 int is_secure = hdr->trusted != 0;
1025 u64 otp_secure_bits = fuse_read_u64(1);
1026 int otp_secure_boot = ((maj3(otp_secure_bits >> 0) << 0) |
1027 (maj3(otp_secure_bits >> 4) << 1)) == 2;
1028 unsigned int otp_boot_device = (maj3(otp_secure_bits >> 48) << 0) |
1029 (maj3(otp_secure_bits >> 52) << 1) |
1030 (maj3(otp_secure_bits >> 56) << 2) |
1031 (maj3(otp_secure_bits >> 60) << 3);
Pali Rohár1ce57b82022-07-26 16:11:56 +02001032#elif defined(CONFIG_ARMADA_32BIT)
1033 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
1034 const struct a38x_main_hdr_v1 *hdr =
1035 (struct a38x_main_hdr_v1 *)get_load_addr();
1036 u32 id = hdr->blockid;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001037#if defined(CONFIG_ARMADA_38X)
1038 int is_secure = a38x_image_is_secure(hdr);
1039 u64 otp_secure_bits = fuse_read_u64(EFUSE_LINE_SECURE_BOOT);
1040 int otp_secure_boot = otp_secure_bits & 0x1;
1041 unsigned int otp_boot_device = (otp_secure_bits >> 8) & 0x7;
1042#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +02001043#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -06001044
Pali Rohár1ce57b82022-07-26 16:11:56 +02001045 for (mode = 0; boot_modes[mode].name; mode++) {
1046 if (boot_modes[mode].id == id)
1047 break;
1048 }
Joel Johnson27e9d072020-04-17 09:38:08 -06001049
Pali Rohár1ce57b82022-07-26 16:11:56 +02001050 if (!boot_modes[mode].name) {
1051 printf("Error: unknown boot device in image header: 0x%x\n", id);
Joel Johnsonab5dd302020-04-17 09:38:06 -06001052 return -ENOEXEC;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001053 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001054
Pali Rohárd83e04a2022-08-23 14:52:24 +02001055 if (strcmp(boot_modes[mode].name, dst->name) != 0) {
1056 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
1057 boot_modes[mode].name, dst->name);
1058 return -ENOEXEC;
1059 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001060
Pali Rohárd83e04a2022-08-23 14:52:24 +02001061#if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700)
1062 if (otp_secure_bits == (u64)-1) {
1063 printf("Error: cannot read OTP secure bits\n");
1064 return -ENOEXEC;
1065 } else {
1066 if (otp_secure_boot && !is_secure) {
1067 printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n");
1068 return -ENOEXEC;
1069 } else if (!otp_secure_boot && is_secure) {
1070#if defined(CONFIG_ARMADA_3700)
1071 /*
1072 * Armada 3700 BootROM rejects trusted image when secure boot is not enabled.
1073 * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled.
1074 */
1075 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
1076 return -ENOEXEC;
Pali Rohár1ce57b82022-07-26 16:11:56 +02001077#endif
Pali Rohárd83e04a2022-08-23 14:52:24 +02001078 } else if (otp_boot_device && otp_boot_device != id) {
1079 for (secure_mode = 0; boot_modes[secure_mode].name; secure_mode++) {
1080 if (boot_modes[secure_mode].id == otp_boot_device)
1081 break;
1082 }
1083 printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n",
1084 boot_modes[secure_mode].name ?: "unknown", dst->name);
1085 return -ENOEXEC;
1086 }
1087 }
1088#endif
1089#endif
1090 return 0;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001091}
1092
1093static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001094{
1095 int err;
1096
1097 /* Check a correct image header exists */
1098 err = check_image_header();
1099 if (err) {
1100 printf("Error: Image header verification failed\n");
1101 return err;
1102 }
1103
Joel Johnsonab5dd302020-04-17 09:38:06 -06001104 err = bubt_check_boot_mode(dst);
1105 if (err) {
1106 printf("Error: Image boot mode verification failed\n");
1107 return err;
1108 }
1109
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001110 return 0;
1111}
1112
1113static int bubt_read_file(struct bubt_dev *src)
1114{
1115 size_t image_size;
1116
1117 if (!src->read) {
1118 printf("Error: Read not supported on device \"%s\"\n",
1119 src->name);
1120 return 0;
1121 }
1122
1123 image_size = src->read(net_boot_file_name);
1124 if (image_size <= 0) {
1125 printf("Error: Failed to read file %s from %s\n",
1126 net_boot_file_name, src->name);
1127 return 0;
1128 }
1129
1130 return image_size;
1131}
1132
1133static int bubt_is_dev_active(struct bubt_dev *dev)
1134{
1135 if (!dev->active) {
Joel Johnson22d78e42020-04-17 09:38:05 -06001136 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001137 dev->name);
1138 return 0;
1139 }
1140
1141 if (!dev->active()) {
1142 printf("Device \"%s\" is inactive\n", dev->name);
1143 return 0;
1144 }
1145
1146 return 1;
1147}
1148
Pali Rohár4b255962023-01-10 22:47:17 +01001149static struct bubt_dev *find_bubt_dev(char *dev_name)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001150{
1151 int dev;
1152
1153 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
1154 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
1155 return &bubt_devs[dev];
1156 }
1157
1158 return 0;
1159}
1160
1161#define DEFAULT_BUBT_SRC "tftp"
1162
1163#ifndef DEFAULT_BUBT_DST
1164#ifdef CONFIG_MVEBU_SPI_BOOT
1165#define DEFAULT_BUBT_DST "spi"
1166#elif defined(CONFIG_MVEBU_NAND_BOOT)
1167#define DEFAULT_BUBT_DST "nand"
1168#elif defined(CONFIG_MVEBU_MMC_BOOT)
1169#define DEFAULT_BUBT_DST "mmc"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001170#elif defined(CONFIG_MVEBU_SATA_BOOT)
1171#define DEFAULT_BUBT_DST "sata"
Konstantin Porotchkine78a3892021-03-17 18:53:43 +02001172#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001173#define DEFAULT_BUBT_DST "error"
1174#endif
1175#endif /* DEFAULT_BUBT_DST */
1176
Pali Rohár4b255962023-01-10 22:47:17 +01001177static int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001178{
1179 struct bubt_dev *src, *dst;
1180 size_t image_size;
1181 char src_dev_name[8];
1182 char dst_dev_name[8];
1183 char *name;
1184 int err;
1185
1186 if (argc < 2)
1187 copy_filename(net_boot_file_name,
1188 CONFIG_MVEBU_UBOOT_DFLT_NAME,
1189 sizeof(net_boot_file_name));
1190 else
1191 copy_filename(net_boot_file_name, argv[1],
1192 sizeof(net_boot_file_name));
1193
1194 if (argc >= 3) {
1195 strncpy(dst_dev_name, argv[2], 8);
1196 } else {
1197 name = DEFAULT_BUBT_DST;
1198 strncpy(dst_dev_name, name, 8);
1199 }
1200
1201 if (argc >= 4)
1202 strncpy(src_dev_name, argv[3], 8);
1203 else
1204 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1205
1206 /* Figure out the destination device */
1207 dst = find_bubt_dev(dst_dev_name);
1208 if (!dst) {
1209 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
Pali Rohár873fc6a2022-07-26 16:11:59 +02001210 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001211 }
1212
1213 if (!bubt_is_dev_active(dst))
Pali Rohár873fc6a2022-07-26 16:11:59 +02001214 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001215
1216 /* Figure out the source device */
1217 src = find_bubt_dev(src_dev_name);
1218 if (!src) {
1219 printf("Error: Unknown source \"%s\"\n", src_dev_name);
1220 return 1;
1221 }
1222
1223 if (!bubt_is_dev_active(src))
1224 return -ENODEV;
1225
Joel Johnson22d78e42020-04-17 09:38:05 -06001226 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001227 net_boot_file_name, src->name, dst->name);
1228
1229 image_size = bubt_read_file(src);
1230 if (!image_size)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001231 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001232
Joel Johnsonab5dd302020-04-17 09:38:06 -06001233 err = bubt_verify(dst);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001234 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001235 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001236
1237 err = bubt_write_file(dst, image_size);
1238 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001239 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001240
1241 return 0;
1242}
1243
1244U_BOOT_CMD(
1245 bubt, 4, 0, do_bubt_cmd,
1246 "Burn a u-boot image to flash",
1247 "[file-name] [destination [source]]\n"
Pali Rohárb4188202021-01-27 11:56:02 +01001248 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001249 "\t-destination Flash to burn to [spi, nand, mmc, sata]. Default = " DEFAULT_BUBT_DST "\n"
Pali Rohárc61feb92023-01-21 23:29:36 +01001250 "\t-source The source to load image from [tftp, usb, mmc, sata]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001251 "Examples:\n"
1252 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
1253 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
1254 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
1255
1256);