blob: 820d342ae1005972c2adb3888e31efd00c4de2b3 [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
226 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
227
228 if (part == 7)
229 part = 0;
230
231#ifdef CONFIG_BLK
232 err = blk_dselect_hwpart(blk_desc, part);
233#else
234 err = mmc_switch_part(mmc, part);
235#endif
236
237 if (err) {
238 printf("Error - MMC partition switch failed\n");
239 return err;
240 }
241#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200242
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200243 /* SD reserves LBA-0 for MBR and boots from LBA-1,
244 * MMC/eMMC boots from LBA-0
245 */
246 start_lba = IS_SD(mmc) ? 1 : 0;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200247#ifdef CONFIG_BLK
248 blk_count = image_size / mmc->write_bl_len;
249 if (image_size % mmc->write_bl_len)
250 blk_count += 1;
251
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200252 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
253 (void *)get_load_addr());
254#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200255 blk_count = image_size / mmc->block_dev.blksz;
256 if (image_size % mmc->block_dev.blksz)
257 blk_count += 1;
258
259 blk_written = mmc->block_dev.block_write(mmc_dev_num,
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200260 start_lba, blk_count,
261 (void *)get_load_addr());
262#endif /* CONFIG_BLK */
Pali Roháre4e67772023-01-21 22:58:28 +0100263
264#ifdef CONFIG_SUPPORT_EMMC_BOOT
265#ifdef CONFIG_BLK
266 err = blk_dselect_hwpart(blk_desc, orig_part);
267#else
268 err = mmc_switch_part(mmc, orig_part);
269#endif
270 if (err)
271 printf("Error - MMC failed to switch back to original partition\n");
272#endif
273
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200274 if (blk_written != blk_count) {
275 printf("Error - written %#lx blocks\n", blk_written);
276 return -ENOSPC;
277 }
278 printf("Done!\n");
279
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200280 return 0;
281}
282
283static size_t mmc_read_file(const char *file_name)
284{
285 loff_t act_read = 0;
286 int rc;
287 struct mmc *mmc;
288 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
289
290 mmc = find_mmc_device(mmc_dev_num);
291 if (!mmc) {
292 printf("No SD/MMC/eMMC card found\n");
293 return 0;
294 }
295
296 if (mmc_init(mmc)) {
297 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
298 mmc_dev_num);
299 return 0;
300 }
301
302 /* Load from data partition (0) */
303 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
304 printf("Error: MMC 0 not found\n");
305 return 0;
306 }
307
308 /* Perfrom file read */
309 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
310 if (rc)
311 return 0;
312
313 return act_read;
314}
315
316static int is_mmc_active(void)
317{
318 return 1;
319}
320#else /* CONFIG_DM_MMC */
321static int mmc_burn_image(size_t image_size)
322{
323 return -ENODEV;
324}
325
326static size_t mmc_read_file(const char *file_name)
327{
328 return 0;
329}
330
331static int is_mmc_active(void)
332{
333 return 0;
334}
335#endif /* CONFIG_DM_MMC */
336
337/********************************************************************
Pali Rohár2a27fc22023-01-22 01:25:12 +0100338 * SATA services
339 ********************************************************************/
340#if defined(CONFIG_SCSI) && defined(CONFIG_BLK)
341static int sata_burn_image(size_t image_size)
342{
343#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
344 lbaint_t start_lba;
345 lbaint_t blk_count;
346 ulong blk_written;
347 struct blk_desc *blk_desc;
348#ifdef CONFIG_ARMADA_3700
349 struct disk_partition info;
350 int part;
351#endif
352
353 scsi_scan(false);
354
355 blk_desc = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
356 if (!blk_desc)
357 return -ENODEV;
358
359#ifdef CONFIG_ARMADA_3700
360 /*
361 * 64-bit Armada 3700 BootROM loads SATA firmware from
362 * GPT 'Marvell Armada 3700 Boot partition' or from
363 * MBR 'M' (0x4d) partition.
364 */
365 switch (blk_desc->part_type) {
366 case PART_TYPE_DOS:
367 for (part = 1; part <= 4; part++) {
368 info.sys_ind = 0;
369 if (part_get_info(blk_desc, part, &info))
370 continue;
371 if (info.sys_ind == 'M')
372 break;
373 }
374 if (part > 4) {
375 printf("Error - cannot find MBR 'M' (0x4d) partition on SATA disk\n");
376 return -ENODEV;
377 }
378 start_lba = info.start;
379 break;
380 case PART_TYPE_EFI:
381 for (part = 1; part <= 64; part++) {
382 info.type_guid[0] = 0;
383 if (part_get_info(blk_desc, part, &info))
384 continue;
385 /* Check for GPT type GUID of 'Marvell Armada 3700 Boot partition' */
386 if (strcmp(info.type_guid, "6828311A-BA55-42A4-BCDE-A89BB5EDECAE") == 0)
387 break;
388 }
389 if (part > 64) {
390 printf("Error - cannot find GPT 'Marvell Armada 3700 Boot partition' on SATA disk\n");
391 return -ENODEV;
392 }
393 start_lba = info.start;
394 break;
395 default:
396 printf("Error - no partitions on SATA disk\n");
397 return -ENODEV;
398 }
399#else
400 /* 32-bit Armada BootROM loads SATA firmware from the sector 1. */
401 start_lba = 1;
402#endif
403
404 blk_count = image_size / blk_desc->blksz;
405 if (image_size % blk_desc->blksz)
406 blk_count += 1;
407
408 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
409 (void *)get_load_addr());
410
411 if (blk_written != blk_count) {
412 printf("Error - written %#lx blocks\n", blk_written);
413 return -ENOSPC;
414 }
415
416 printf("Done!\n");
417 return 0;
418#else
419 return -ENODEV;
420#endif
421}
422
Pali Rohárc61feb92023-01-21 23:29:36 +0100423static size_t sata_read_file(const char *file_name)
424{
425 loff_t act_read = 0;
426 struct udevice *dev;
427 int rc;
428
429 /* try to recognize storage devices immediately */
430 scsi_scan(false);
431
432 /* Try to recognize storage devices immediately */
433 blk_first_device(UCLASS_SCSI, &dev);
434 if (!dev) {
435 printf("Error: SATA device not found\n");
436 return 0;
437 }
438
439 /* Always load from scsi 0 */
440 if (fs_set_blk_dev("scsi", "0", FS_TYPE_ANY)) {
441 printf("Error: SATA 0 not found\n");
442 return 0;
443 }
444
445 /* Perfrom file read */
446 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
447 if (rc)
448 return 0;
449
450 return act_read;
451}
452
Pali Rohár2a27fc22023-01-22 01:25:12 +0100453static int is_sata_active(void)
454{
455 return 1;
456}
457#else /* CONFIG_SCSI */
458static int sata_burn_image(size_t image_size)
459{
460 return -ENODEV;
461}
462
Pali Rohárc61feb92023-01-21 23:29:36 +0100463static size_t sata_read_file(const char *file_name)
464{
465 return 0;
466}
467
Pali Rohár2a27fc22023-01-22 01:25:12 +0100468static int is_sata_active(void)
469{
470 return 0;
471}
472#endif /* CONFIG_SCSI */
473
474/********************************************************************
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200475 * SPI services
476 ********************************************************************/
477#ifdef CONFIG_SPI_FLASH
478static int spi_burn_image(size_t image_size)
479{
480 int ret;
481 struct spi_flash *flash;
482 u32 erase_bytes;
483
484 /* Probe the SPI bus to get the flash device */
Tom Rini119d2fb2021-12-11 14:55:48 -0500485 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
486 CONFIG_SF_DEFAULT_CS,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200487 CONFIG_SF_DEFAULT_SPEED,
488 CONFIG_SF_DEFAULT_MODE);
489 if (!flash) {
490 printf("Failed to probe SPI Flash\n");
491 return -ENOMEDIUM;
492 }
493
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200494 erase_bytes = image_size +
495 (flash->erase_size - image_size % flash->erase_size);
496 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
497 erase_bytes, erase_bytes / flash->erase_size);
498 ret = spi_flash_erase(flash, 0, erase_bytes);
499 if (ret)
500 printf("Error!\n");
501 else
502 printf("Done!\n");
503
504 printf("Writing %d bytes from 0x%lx to offset 0 ...",
505 (int)image_size, get_load_addr());
506 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
507 if (ret)
508 printf("Error!\n");
509 else
510 printf("Done!\n");
511
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200512 return ret;
513}
514
515static int is_spi_active(void)
516{
517 return 1;
518}
519
520#else /* CONFIG_SPI_FLASH */
521static int spi_burn_image(size_t image_size)
522{
523 return -ENODEV;
524}
525
526static int is_spi_active(void)
527{
528 return 0;
529}
530#endif /* CONFIG_SPI_FLASH */
531
532/********************************************************************
533 * NAND services
534 ********************************************************************/
535#ifdef CONFIG_CMD_NAND
536static int nand_burn_image(size_t image_size)
537{
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300538 int ret;
539 uint32_t block_size;
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500540 struct mtd_info *mtd;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200541
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500542 mtd = get_nand_dev_by_index(nand_curr_device);
543 if (!mtd) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200544 puts("\nno devices available\n");
545 return -ENOMEDIUM;
546 }
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500547 block_size = mtd->erasesize;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200548
549 /* Align U-Boot size to currently used blocksize */
550 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
551
Joel Johnson22d78e42020-04-17 09:38:05 -0600552 /* Erase the U-Boot image space */
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200553 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500554 ret = nand_erase(mtd, 0, image_size);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200555 if (ret) {
556 printf("Error!\n");
557 goto error;
558 }
559 printf("Done!\n");
560
561 /* Write the image to flash */
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300562 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
563 (int)image_size, get_load_addr());
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500564 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200565 if (ret)
566 printf("Error!\n");
567 else
568 printf("Done!\n");
569
570error:
571 return ret;
572}
573
574static int is_nand_active(void)
575{
576 return 1;
577}
578
579#else /* CONFIG_CMD_NAND */
580static int nand_burn_image(size_t image_size)
581{
582 return -ENODEV;
583}
584
585static int is_nand_active(void)
586{
587 return 0;
588}
589#endif /* CONFIG_CMD_NAND */
590
591/********************************************************************
592 * USB services
593 ********************************************************************/
594#if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
595static size_t usb_read_file(const char *file_name)
596{
597 loff_t act_read = 0;
598 struct udevice *dev;
599 int rc;
600
601 usb_stop();
602
603 if (usb_init() < 0) {
604 printf("Error: usb_init failed\n");
605 return 0;
606 }
607
608 /* Try to recognize storage devices immediately */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600609 blk_first_device(UCLASS_USB, &dev);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200610 if (!dev) {
611 printf("Error: USB storage device not found\n");
612 return 0;
613 }
614
615 /* Always load from usb 0 */
616 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
617 printf("Error: USB 0 not found\n");
618 return 0;
619 }
620
621 /* Perfrom file read */
622 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
623 if (rc)
624 return 0;
625
626 return act_read;
627}
628
629static int is_usb_active(void)
630{
631 return 1;
632}
633
634#else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
635static size_t usb_read_file(const char *file_name)
636{
637 return 0;
638}
639
640static int is_usb_active(void)
641{
642 return 0;
643}
644#endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
645
646/********************************************************************
647 * Network services
648 ********************************************************************/
649#ifdef CONFIG_CMD_NET
650static size_t tftp_read_file(const char *file_name)
651{
Pali Rohár2c96d022022-07-26 16:11:57 +0200652 int ret;
653
Simon Glass892265d2019-12-28 10:45:02 -0700654 /*
655 * update global variable image_load_addr before tftp file from network
656 */
657 image_load_addr = get_load_addr();
Pali Rohár2c96d022022-07-26 16:11:57 +0200658 ret = net_loop(TFTPGET);
659 return ret > 0 ? ret : 0;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200660}
661
662static int is_tftp_active(void)
663{
664 return 1;
665}
666
667#else
668static size_t tftp_read_file(const char *file_name)
669{
670 return 0;
671}
672
673static int is_tftp_active(void)
674{
675 return 0;
676}
677#endif /* CONFIG_CMD_NET */
678
679enum bubt_devices {
680 BUBT_DEV_NET = 0,
681 BUBT_DEV_USB,
682 BUBT_DEV_MMC,
Pali Rohár2a27fc22023-01-22 01:25:12 +0100683 BUBT_DEV_SATA,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200684 BUBT_DEV_SPI,
685 BUBT_DEV_NAND,
686
687 BUBT_MAX_DEV
688};
689
Pali Rohár4b255962023-01-10 22:47:17 +0100690static struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200691 {"tftp", tftp_read_file, NULL, is_tftp_active},
692 {"usb", usb_read_file, NULL, is_usb_active},
693 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
Pali Rohárc61feb92023-01-21 23:29:36 +0100694 {"sata", sata_read_file, sata_burn_image, is_sata_active},
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200695 {"spi", NULL, spi_burn_image, is_spi_active},
696 {"nand", NULL, nand_burn_image, is_nand_active},
697};
698
699static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
700{
701 if (!dst->write) {
702 printf("Error: Write not supported on device %s\n", dst->name);
703 return -ENOTSUPP;
704 }
705
706 return dst->write(image_size);
707}
708
709#if defined(CONFIG_ARMADA_8K)
Pali Rohár4b255962023-01-10 22:47:17 +0100710static u32 do_checksum32(u32 *start, int32_t len)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200711{
712 u32 sum = 0;
713 u32 *startp = start;
714
715 do {
716 sum += *startp;
717 startp++;
718 len -= 4;
719 } while (len > 0);
720
721 return sum;
722}
723
724static int check_image_header(void)
725{
726 struct mvebu_image_header *hdr =
727 (struct mvebu_image_header *)get_load_addr();
728 u32 header_len = hdr->prolog_size;
729 u32 checksum;
730 u32 checksum_ref = hdr->prolog_checksum;
731
732 /*
733 * For now compare checksum, and magic. Later we can
734 * verify more stuff on the header like interface type, etc
735 */
736 if (hdr->magic != MAIN_HDR_MAGIC) {
737 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
738 hdr->magic, MAIN_HDR_MAGIC);
739 return -ENOEXEC;
740 }
741
742 /* The checksum value is discarded from checksum calculation */
743 hdr->prolog_checksum = 0;
744
745 checksum = do_checksum32((u32 *)hdr, header_len);
746 if (checksum != checksum_ref) {
747 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
748 checksum, checksum_ref);
749 return -ENOEXEC;
750 }
751
752 /* Restore the checksum before writing */
753 hdr->prolog_checksum = checksum_ref;
754 printf("Image checksum...OK!\n");
755
756 return 0;
757}
758#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
759static int check_image_header(void)
760{
761 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
762 int image_num;
763 u8 hash_160_output[SHA1_SUM_LEN];
764 u8 hash_256_output[SHA256_SUM_LEN];
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200765 u8 hash_512_output[SHA512_SUM_LEN];
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200766 sha1_context hash1_text;
767 sha256_context hash256_text;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200768 sha512_context hash512_text;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200769 u8 *hash_output;
770 u32 hash_algorithm_id;
771 u32 image_size_to_hash;
772 u32 flash_entry_addr;
773 u32 *hash_value;
774 u32 internal_hash[HASH_SUM_LEN];
775 const u8 *buff;
776 u32 num_of_image = hdr->num_images;
777 u32 version = hdr->version;
778 u32 trusted = hdr->trusted;
779
780 /* bubt checksum validation only supports nontrusted images */
781 if (trusted == 1) {
782 printf("bypass image validation, ");
783 printf("only untrusted image is supported now\n");
784 return 0;
785 }
786 /* only supports image version 3.5 and 3.6 */
787 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
788 printf("Error: Unsupported Image version = 0x%08x\n", version);
789 return -ENOEXEC;
790 }
791 /* validate images hash value */
792 for (image_num = 0; image_num < num_of_image; image_num++) {
793 struct mvebu_image_info *info =
794 (struct mvebu_image_info *)(get_load_addr() +
795 sizeof(struct common_tim_data) +
796 image_num * sizeof(struct mvebu_image_info));
797 hash_algorithm_id = info->hash_algorithm_id;
798 image_size_to_hash = info->image_size_to_hash;
799 flash_entry_addr = info->flash_entry_addr;
800 hash_value = info->hash;
801 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
802
803 if (image_num == 0) {
804 /*
805 * The first image includes hash values in its content.
806 * For hash calculation, we need to save the original
807 * hash values to a local variable that will be
808 * copied back for comparsion and set all zeros to
809 * the orignal hash values for calculating new value.
810 * First image original format :
811 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
812 * Replaced first image format :
813 * x...x (datum1) 0...0(hash values) x...x(datum2)
814 */
815 memcpy(internal_hash, hash_value,
816 sizeof(internal_hash));
817 memset(hash_value, 0, sizeof(internal_hash));
818 }
819 if (image_size_to_hash == 0) {
820 printf("Warning: Image_%d hash checksum is disabled, ",
821 image_num);
822 printf("skip the image validation.\n");
823 continue;
824 }
825 switch (hash_algorithm_id) {
826 case SHA1_SUM_LEN:
827 sha1_starts(&hash1_text);
828 sha1_update(&hash1_text, buff, image_size_to_hash);
829 sha1_finish(&hash1_text, hash_160_output);
830 hash_output = hash_160_output;
831 break;
832 case SHA256_SUM_LEN:
833 sha256_starts(&hash256_text);
834 sha256_update(&hash256_text, buff, image_size_to_hash);
835 sha256_finish(&hash256_text, hash_256_output);
836 hash_output = hash_256_output;
837 break;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200838 case SHA512_SUM_LEN:
839 sha512_starts(&hash512_text);
840 sha512_update(&hash512_text, buff, image_size_to_hash);
841 sha512_finish(&hash512_text, hash_512_output);
842 hash_output = hash_512_output;
843 break;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200844 default:
845 printf("Error: Unsupported hash_algorithm_id = %d\n",
846 hash_algorithm_id);
847 return -ENOEXEC;
848 }
849 if (image_num == 0)
850 memcpy(hash_value, internal_hash,
851 sizeof(internal_hash));
852 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
853 printf("Error: Image_%d checksum is not correct\n",
854 image_num);
855 return -ENOEXEC;
856 }
857 }
858 printf("Image checksum...OK!\n");
859
Joel Johnson37b9c122020-04-17 09:38:04 -0600860 return 0;
861}
Pali Rohár1ce57b82022-07-26 16:11:56 +0200862#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600863static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
864{
865 if (h->version == 1)
866 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
867
868 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
869 h->version);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200870 return 0;
871}
872
Joel Johnson37b9c122020-04-17 09:38:04 -0600873static uint8_t image_checksum8(const void *start, size_t len)
874{
875 u8 csum = 0;
876 const u8 *p = start;
877
878 while (len) {
879 csum += *p;
880 ++p;
881 --len;
882 }
883
884 return csum;
885}
886
Pali Rohár05781ff2022-08-23 14:52:23 +0200887static uint32_t image_checksum32(const void *start, size_t len)
888{
889 u32 csum = 0;
890 const u32 *p = start;
891
892 while (len) {
893 csum += *p;
894 ++p;
895 len -= sizeof(u32);
896 }
897
898 return csum;
899}
900
Joel Johnson37b9c122020-04-17 09:38:04 -0600901static int check_image_header(void)
902{
903 u8 checksum;
Pali Rohár05781ff2022-08-23 14:52:23 +0200904 u32 checksum32, exp_checksum32;
905 u32 offset, size;
Joel Johnson37b9c122020-04-17 09:38:04 -0600906 const struct a38x_main_hdr_v1 *hdr =
907 (struct a38x_main_hdr_v1 *)get_load_addr();
Pali Rohárfc2b9142023-01-08 14:31:28 +0100908 const size_t hdr_size = a38x_header_size(hdr);
Joel Johnson37b9c122020-04-17 09:38:04 -0600909
Pali Rohárfc2b9142023-01-08 14:31:28 +0100910 if (!hdr_size)
Joel Johnson37b9c122020-04-17 09:38:04 -0600911 return -ENOEXEC;
912
Pali Rohárfc2b9142023-01-08 14:31:28 +0100913 checksum = image_checksum8(hdr, hdr_size);
Joel Johnson37b9c122020-04-17 09:38:04 -0600914 checksum -= hdr->checksum;
915 if (checksum != hdr->checksum) {
Pali Rohár05781ff2022-08-23 14:52:23 +0200916 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
Joel Johnson37b9c122020-04-17 09:38:04 -0600917 checksum, hdr->checksum);
918 return -ENOEXEC;
919 }
920
Pali Rohár05781ff2022-08-23 14:52:23 +0200921 offset = le32_to_cpu(hdr->srcaddr);
922 size = le32_to_cpu(hdr->blocksize);
923
Pali Rohár42cc7952023-01-21 13:59:20 +0100924 if (hdr->blockid == 0x78) /* SATA id */
Pali Rohár05781ff2022-08-23 14:52:23 +0200925 offset *= 512;
Pali Rohár05781ff2022-08-23 14:52:23 +0200926
Pali Rohár05781ff2022-08-23 14:52:23 +0200927 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
928 printf("Error: Bad A38x image blocksize.\n");
929 return -ENOEXEC;
930 }
931
932 checksum32 = image_checksum32((u8 *)hdr + offset, size - 4);
933 exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4);
934 if (checksum32 != exp_checksum32) {
935 printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n",
936 checksum32, exp_checksum32);
937 return -ENOEXEC;
938 }
939
Joel Johnson37b9c122020-04-17 09:38:04 -0600940 printf("Image checksum...OK!\n");
941 return 0;
942}
Pali Rohárd83e04a2022-08-23 14:52:24 +0200943
944#if defined(CONFIG_ARMADA_38X)
945static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr)
946{
Pali Rohárfc2b9142023-01-08 14:31:28 +0100947 const size_t hdr_size = a38x_header_size(hdr);
Pali Rohárd83e04a2022-08-23 14:52:24 +0200948 struct a38x_opt_hdr_v1 *ohdr;
949 u32 ohdr_size;
950
951 if (hdr->version != 1)
952 return 0;
953
954 if (!hdr->ext)
955 return 0;
956
957 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
958 do {
959 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
960 return 1;
961
962 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
963
964 if (!*((u8 *)ohdr + ohdr_size - 4))
965 break;
966
967 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
Pali Rohárfc2b9142023-01-08 14:31:28 +0100968 if ((u8 *)ohdr >= (u8 *)hdr + hdr_size)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200969 break;
970 } while (1);
971
972 return 0;
973}
974#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200975#else /* Not ARMADA? */
976static int check_image_header(void)
977{
978 printf("bubt cmd does not support this SoC device or family!\n");
979 return -ENOEXEC;
980}
981#endif
982
Pali Rohárd83e04a2022-08-23 14:52:24 +0200983#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
984static u64 fuse_read_u64(u32 bank)
985{
986 u32 val[2];
987 int ret;
988
989 ret = fuse_read(bank, 0, &val[0]);
990 if (ret < 0)
991 return -1;
992
993 ret = fuse_read(bank, 1, &val[1]);
994 if (ret < 0)
995 return -1;
996
997 return ((u64)val[1] << 32) | val[0];
998}
999#endif
1000
1001#if defined(CONFIG_ARMADA_3700)
1002static inline u8 maj3(u8 val)
1003{
1004 /* return majority vote of 3 bits */
1005 return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0;
1006}
1007#endif
1008
Joel Johnsonab5dd302020-04-17 09:38:06 -06001009static int bubt_check_boot_mode(const struct bubt_dev *dst)
1010{
Pali Rohár1ce57b82022-07-26 16:11:56 +02001011#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
Pali Rohárd83e04a2022-08-23 14:52:24 +02001012 int mode, secure_mode;
Pali Rohár1ce57b82022-07-26 16:11:56 +02001013#if defined(CONFIG_ARMADA_3700)
1014 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
1015 const struct common_tim_data *hdr =
1016 (struct common_tim_data *)get_load_addr();
1017 u32 id = hdr->boot_flash_sign;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001018 int is_secure = hdr->trusted != 0;
1019 u64 otp_secure_bits = fuse_read_u64(1);
1020 int otp_secure_boot = ((maj3(otp_secure_bits >> 0) << 0) |
1021 (maj3(otp_secure_bits >> 4) << 1)) == 2;
1022 unsigned int otp_boot_device = (maj3(otp_secure_bits >> 48) << 0) |
1023 (maj3(otp_secure_bits >> 52) << 1) |
1024 (maj3(otp_secure_bits >> 56) << 2) |
1025 (maj3(otp_secure_bits >> 60) << 3);
Pali Rohár1ce57b82022-07-26 16:11:56 +02001026#elif defined(CONFIG_ARMADA_32BIT)
1027 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
1028 const struct a38x_main_hdr_v1 *hdr =
1029 (struct a38x_main_hdr_v1 *)get_load_addr();
1030 u32 id = hdr->blockid;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001031#if defined(CONFIG_ARMADA_38X)
1032 int is_secure = a38x_image_is_secure(hdr);
1033 u64 otp_secure_bits = fuse_read_u64(EFUSE_LINE_SECURE_BOOT);
1034 int otp_secure_boot = otp_secure_bits & 0x1;
1035 unsigned int otp_boot_device = (otp_secure_bits >> 8) & 0x7;
1036#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +02001037#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -06001038
Pali Rohár1ce57b82022-07-26 16:11:56 +02001039 for (mode = 0; boot_modes[mode].name; mode++) {
1040 if (boot_modes[mode].id == id)
1041 break;
1042 }
Joel Johnson27e9d072020-04-17 09:38:08 -06001043
Pali Rohár1ce57b82022-07-26 16:11:56 +02001044 if (!boot_modes[mode].name) {
1045 printf("Error: unknown boot device in image header: 0x%x\n", id);
Joel Johnsonab5dd302020-04-17 09:38:06 -06001046 return -ENOEXEC;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001047 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001048
Pali Rohárd83e04a2022-08-23 14:52:24 +02001049 if (strcmp(boot_modes[mode].name, dst->name) != 0) {
1050 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
1051 boot_modes[mode].name, dst->name);
1052 return -ENOEXEC;
1053 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001054
Pali Rohárd83e04a2022-08-23 14:52:24 +02001055#if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700)
1056 if (otp_secure_bits == (u64)-1) {
1057 printf("Error: cannot read OTP secure bits\n");
1058 return -ENOEXEC;
1059 } else {
1060 if (otp_secure_boot && !is_secure) {
1061 printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n");
1062 return -ENOEXEC;
1063 } else if (!otp_secure_boot && is_secure) {
1064#if defined(CONFIG_ARMADA_3700)
1065 /*
1066 * Armada 3700 BootROM rejects trusted image when secure boot is not enabled.
1067 * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled.
1068 */
1069 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
1070 return -ENOEXEC;
Pali Rohár1ce57b82022-07-26 16:11:56 +02001071#endif
Pali Rohárd83e04a2022-08-23 14:52:24 +02001072 } else if (otp_boot_device && otp_boot_device != id) {
1073 for (secure_mode = 0; boot_modes[secure_mode].name; secure_mode++) {
1074 if (boot_modes[secure_mode].id == otp_boot_device)
1075 break;
1076 }
1077 printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n",
1078 boot_modes[secure_mode].name ?: "unknown", dst->name);
1079 return -ENOEXEC;
1080 }
1081 }
1082#endif
1083#endif
1084 return 0;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001085}
1086
1087static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001088{
1089 int err;
1090
1091 /* Check a correct image header exists */
1092 err = check_image_header();
1093 if (err) {
1094 printf("Error: Image header verification failed\n");
1095 return err;
1096 }
1097
Joel Johnsonab5dd302020-04-17 09:38:06 -06001098 err = bubt_check_boot_mode(dst);
1099 if (err) {
1100 printf("Error: Image boot mode verification failed\n");
1101 return err;
1102 }
1103
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001104 return 0;
1105}
1106
1107static int bubt_read_file(struct bubt_dev *src)
1108{
1109 size_t image_size;
1110
1111 if (!src->read) {
1112 printf("Error: Read not supported on device \"%s\"\n",
1113 src->name);
1114 return 0;
1115 }
1116
1117 image_size = src->read(net_boot_file_name);
1118 if (image_size <= 0) {
1119 printf("Error: Failed to read file %s from %s\n",
1120 net_boot_file_name, src->name);
1121 return 0;
1122 }
1123
1124 return image_size;
1125}
1126
1127static int bubt_is_dev_active(struct bubt_dev *dev)
1128{
1129 if (!dev->active) {
Joel Johnson22d78e42020-04-17 09:38:05 -06001130 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001131 dev->name);
1132 return 0;
1133 }
1134
1135 if (!dev->active()) {
1136 printf("Device \"%s\" is inactive\n", dev->name);
1137 return 0;
1138 }
1139
1140 return 1;
1141}
1142
Pali Rohár4b255962023-01-10 22:47:17 +01001143static struct bubt_dev *find_bubt_dev(char *dev_name)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001144{
1145 int dev;
1146
1147 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
1148 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
1149 return &bubt_devs[dev];
1150 }
1151
1152 return 0;
1153}
1154
1155#define DEFAULT_BUBT_SRC "tftp"
1156
1157#ifndef DEFAULT_BUBT_DST
1158#ifdef CONFIG_MVEBU_SPI_BOOT
1159#define DEFAULT_BUBT_DST "spi"
1160#elif defined(CONFIG_MVEBU_NAND_BOOT)
1161#define DEFAULT_BUBT_DST "nand"
1162#elif defined(CONFIG_MVEBU_MMC_BOOT)
1163#define DEFAULT_BUBT_DST "mmc"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001164#elif defined(CONFIG_MVEBU_SATA_BOOT)
1165#define DEFAULT_BUBT_DST "sata"
Konstantin Porotchkine78a3892021-03-17 18:53:43 +02001166#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001167#define DEFAULT_BUBT_DST "error"
1168#endif
1169#endif /* DEFAULT_BUBT_DST */
1170
Pali Rohár4b255962023-01-10 22:47:17 +01001171static int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001172{
1173 struct bubt_dev *src, *dst;
1174 size_t image_size;
1175 char src_dev_name[8];
1176 char dst_dev_name[8];
1177 char *name;
1178 int err;
1179
1180 if (argc < 2)
1181 copy_filename(net_boot_file_name,
1182 CONFIG_MVEBU_UBOOT_DFLT_NAME,
1183 sizeof(net_boot_file_name));
1184 else
1185 copy_filename(net_boot_file_name, argv[1],
1186 sizeof(net_boot_file_name));
1187
1188 if (argc >= 3) {
1189 strncpy(dst_dev_name, argv[2], 8);
1190 } else {
1191 name = DEFAULT_BUBT_DST;
1192 strncpy(dst_dev_name, name, 8);
1193 }
1194
1195 if (argc >= 4)
1196 strncpy(src_dev_name, argv[3], 8);
1197 else
1198 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1199
1200 /* Figure out the destination device */
1201 dst = find_bubt_dev(dst_dev_name);
1202 if (!dst) {
1203 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
Pali Rohár873fc6a2022-07-26 16:11:59 +02001204 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001205 }
1206
1207 if (!bubt_is_dev_active(dst))
Pali Rohár873fc6a2022-07-26 16:11:59 +02001208 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001209
1210 /* Figure out the source device */
1211 src = find_bubt_dev(src_dev_name);
1212 if (!src) {
1213 printf("Error: Unknown source \"%s\"\n", src_dev_name);
1214 return 1;
1215 }
1216
1217 if (!bubt_is_dev_active(src))
1218 return -ENODEV;
1219
Joel Johnson22d78e42020-04-17 09:38:05 -06001220 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001221 net_boot_file_name, src->name, dst->name);
1222
1223 image_size = bubt_read_file(src);
1224 if (!image_size)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001225 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001226
Joel Johnsonab5dd302020-04-17 09:38:06 -06001227 err = bubt_verify(dst);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001228 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001229 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001230
1231 err = bubt_write_file(dst, image_size);
1232 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001233 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001234
1235 return 0;
1236}
1237
1238U_BOOT_CMD(
1239 bubt, 4, 0, do_bubt_cmd,
1240 "Burn a u-boot image to flash",
1241 "[file-name] [destination [source]]\n"
Pali Rohárb4188202021-01-27 11:56:02 +01001242 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001243 "\t-destination Flash to burn to [spi, nand, mmc, sata]. Default = " DEFAULT_BUBT_DST "\n"
Pali Rohárc61feb92023-01-21 23:29:36 +01001244 "\t-source The source to load image from [tftp, usb, mmc, sata]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001245 "Examples:\n"
1246 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
1247 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
1248 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
1249
1250);