blob: 49797b23144b3bcac7ca269f137e84be0ab74a28 [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();
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200728 u32 checksum;
Pali Rohár0cebe102023-01-29 18:49:04 +0100729 u32 checksum_ref;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200730
731 /*
732 * For now compare checksum, and magic. Later we can
733 * verify more stuff on the header like interface type, etc
734 */
735 if (hdr->magic != MAIN_HDR_MAGIC) {
736 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
737 hdr->magic, MAIN_HDR_MAGIC);
738 return -ENOEXEC;
739 }
740
Pali Rohár0cebe102023-01-29 18:49:04 +0100741 checksum_ref = hdr->prolog_checksum;
742 checksum = do_checksum32((u32 *)hdr, hdr->prolog_size);
Pali Rohárb4a76fd2023-01-29 18:38:11 +0100743 checksum -= hdr->prolog_checksum;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200744 if (checksum != checksum_ref) {
Pali Rohár0cebe102023-01-29 18:49:04 +0100745 printf("Error: Bad Prolog checksum. 0x%x != 0x%x\n",
746 checksum, checksum_ref);
747 return -ENOEXEC;
748 }
749
750 checksum_ref = hdr->boot_image_checksum;
751 checksum = do_checksum32((u32 *)((u8 *)hdr + hdr->prolog_size), hdr->boot_image_size);
752 if (checksum != checksum_ref) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200753 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
754 checksum, checksum_ref);
755 return -ENOEXEC;
756 }
757
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200758 printf("Image checksum...OK!\n");
759
760 return 0;
761}
762#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
763static int check_image_header(void)
764{
765 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
766 int image_num;
767 u8 hash_160_output[SHA1_SUM_LEN];
768 u8 hash_256_output[SHA256_SUM_LEN];
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200769 u8 hash_512_output[SHA512_SUM_LEN];
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200770 sha1_context hash1_text;
771 sha256_context hash256_text;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200772 sha512_context hash512_text;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200773 u8 *hash_output;
774 u32 hash_algorithm_id;
775 u32 image_size_to_hash;
776 u32 flash_entry_addr;
777 u32 *hash_value;
778 u32 internal_hash[HASH_SUM_LEN];
779 const u8 *buff;
780 u32 num_of_image = hdr->num_images;
781 u32 version = hdr->version;
782 u32 trusted = hdr->trusted;
783
784 /* bubt checksum validation only supports nontrusted images */
785 if (trusted == 1) {
786 printf("bypass image validation, ");
787 printf("only untrusted image is supported now\n");
788 return 0;
789 }
790 /* only supports image version 3.5 and 3.6 */
791 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
792 printf("Error: Unsupported Image version = 0x%08x\n", version);
793 return -ENOEXEC;
794 }
795 /* validate images hash value */
796 for (image_num = 0; image_num < num_of_image; image_num++) {
797 struct mvebu_image_info *info =
798 (struct mvebu_image_info *)(get_load_addr() +
799 sizeof(struct common_tim_data) +
800 image_num * sizeof(struct mvebu_image_info));
801 hash_algorithm_id = info->hash_algorithm_id;
802 image_size_to_hash = info->image_size_to_hash;
803 flash_entry_addr = info->flash_entry_addr;
804 hash_value = info->hash;
805 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
806
807 if (image_num == 0) {
808 /*
809 * The first image includes hash values in its content.
810 * For hash calculation, we need to save the original
811 * hash values to a local variable that will be
812 * copied back for comparsion and set all zeros to
813 * the orignal hash values for calculating new value.
814 * First image original format :
815 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
816 * Replaced first image format :
817 * x...x (datum1) 0...0(hash values) x...x(datum2)
818 */
819 memcpy(internal_hash, hash_value,
820 sizeof(internal_hash));
821 memset(hash_value, 0, sizeof(internal_hash));
822 }
823 if (image_size_to_hash == 0) {
824 printf("Warning: Image_%d hash checksum is disabled, ",
825 image_num);
826 printf("skip the image validation.\n");
827 continue;
828 }
829 switch (hash_algorithm_id) {
830 case SHA1_SUM_LEN:
831 sha1_starts(&hash1_text);
832 sha1_update(&hash1_text, buff, image_size_to_hash);
833 sha1_finish(&hash1_text, hash_160_output);
834 hash_output = hash_160_output;
835 break;
836 case SHA256_SUM_LEN:
837 sha256_starts(&hash256_text);
838 sha256_update(&hash256_text, buff, image_size_to_hash);
839 sha256_finish(&hash256_text, hash_256_output);
840 hash_output = hash_256_output;
841 break;
Pali Rohára4bbb2c2022-07-26 16:11:58 +0200842 case SHA512_SUM_LEN:
843 sha512_starts(&hash512_text);
844 sha512_update(&hash512_text, buff, image_size_to_hash);
845 sha512_finish(&hash512_text, hash_512_output);
846 hash_output = hash_512_output;
847 break;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200848 default:
849 printf("Error: Unsupported hash_algorithm_id = %d\n",
850 hash_algorithm_id);
851 return -ENOEXEC;
852 }
853 if (image_num == 0)
854 memcpy(hash_value, internal_hash,
855 sizeof(internal_hash));
856 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
857 printf("Error: Image_%d checksum is not correct\n",
858 image_num);
859 return -ENOEXEC;
860 }
861 }
862 printf("Image checksum...OK!\n");
863
Joel Johnson37b9c122020-04-17 09:38:04 -0600864 return 0;
865}
Pali Rohár1ce57b82022-07-26 16:11:56 +0200866#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600867static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
868{
869 if (h->version == 1)
870 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
871
872 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
873 h->version);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200874 return 0;
875}
876
Joel Johnson37b9c122020-04-17 09:38:04 -0600877static uint8_t image_checksum8(const void *start, size_t len)
878{
879 u8 csum = 0;
880 const u8 *p = start;
881
882 while (len) {
883 csum += *p;
884 ++p;
885 --len;
886 }
887
888 return csum;
889}
890
Pali Rohár05781ff2022-08-23 14:52:23 +0200891static uint32_t image_checksum32(const void *start, size_t len)
892{
893 u32 csum = 0;
894 const u32 *p = start;
895
896 while (len) {
897 csum += *p;
898 ++p;
899 len -= sizeof(u32);
900 }
901
902 return csum;
903}
904
Joel Johnson37b9c122020-04-17 09:38:04 -0600905static int check_image_header(void)
906{
907 u8 checksum;
Pali Rohár05781ff2022-08-23 14:52:23 +0200908 u32 checksum32, exp_checksum32;
909 u32 offset, size;
Joel Johnson37b9c122020-04-17 09:38:04 -0600910 const struct a38x_main_hdr_v1 *hdr =
911 (struct a38x_main_hdr_v1 *)get_load_addr();
Pali Rohárfc2b9142023-01-08 14:31:28 +0100912 const size_t hdr_size = a38x_header_size(hdr);
Joel Johnson37b9c122020-04-17 09:38:04 -0600913
Pali Rohárfc2b9142023-01-08 14:31:28 +0100914 if (!hdr_size)
Joel Johnson37b9c122020-04-17 09:38:04 -0600915 return -ENOEXEC;
916
Pali Rohárfc2b9142023-01-08 14:31:28 +0100917 checksum = image_checksum8(hdr, hdr_size);
Joel Johnson37b9c122020-04-17 09:38:04 -0600918 checksum -= hdr->checksum;
919 if (checksum != hdr->checksum) {
Pali Rohár05781ff2022-08-23 14:52:23 +0200920 printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n",
Joel Johnson37b9c122020-04-17 09:38:04 -0600921 checksum, hdr->checksum);
922 return -ENOEXEC;
923 }
924
Pali Rohár05781ff2022-08-23 14:52:23 +0200925 offset = le32_to_cpu(hdr->srcaddr);
926 size = le32_to_cpu(hdr->blocksize);
927
Pali Rohár42cc7952023-01-21 13:59:20 +0100928 if (hdr->blockid == 0x78) /* SATA id */
Pali Rohár05781ff2022-08-23 14:52:23 +0200929 offset *= 512;
Pali Rohár05781ff2022-08-23 14:52:23 +0200930
Pali Rohár05781ff2022-08-23 14:52:23 +0200931 if (offset % 4 != 0 || size < 4 || size % 4 != 0) {
932 printf("Error: Bad A38x image blocksize.\n");
933 return -ENOEXEC;
934 }
935
936 checksum32 = image_checksum32((u8 *)hdr + offset, size - 4);
937 exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4);
938 if (checksum32 != exp_checksum32) {
939 printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n",
940 checksum32, exp_checksum32);
941 return -ENOEXEC;
942 }
943
Joel Johnson37b9c122020-04-17 09:38:04 -0600944 printf("Image checksum...OK!\n");
945 return 0;
946}
Pali Rohárd83e04a2022-08-23 14:52:24 +0200947
948#if defined(CONFIG_ARMADA_38X)
949static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr)
950{
Pali Rohárfc2b9142023-01-08 14:31:28 +0100951 const size_t hdr_size = a38x_header_size(hdr);
Pali Rohárd83e04a2022-08-23 14:52:24 +0200952 struct a38x_opt_hdr_v1 *ohdr;
953 u32 ohdr_size;
954
955 if (hdr->version != 1)
956 return 0;
957
958 if (!hdr->ext)
959 return 0;
960
961 ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1);
962 do {
963 if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE)
964 return 1;
965
966 ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb);
967
968 if (!*((u8 *)ohdr + ohdr_size - 4))
969 break;
970
971 ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size);
Pali Rohárfc2b9142023-01-08 14:31:28 +0100972 if ((u8 *)ohdr >= (u8 *)hdr + hdr_size)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200973 break;
974 } while (1);
975
976 return 0;
977}
978#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200979#else /* Not ARMADA? */
980static int check_image_header(void)
981{
982 printf("bubt cmd does not support this SoC device or family!\n");
983 return -ENOEXEC;
984}
985#endif
986
Pali Rohár85f2a7c2023-02-20 22:42:54 +0100987#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
Pali Rohárd83e04a2022-08-23 14:52:24 +0200988static u64 fuse_read_u64(u32 bank)
989{
990 u32 val[2];
991 int ret;
992
993 ret = fuse_read(bank, 0, &val[0]);
994 if (ret < 0)
995 return -1;
996
997 ret = fuse_read(bank, 1, &val[1]);
998 if (ret < 0)
999 return -1;
1000
1001 return ((u64)val[1] << 32) | val[0];
1002}
1003#endif
1004
1005#if defined(CONFIG_ARMADA_3700)
1006static inline u8 maj3(u8 val)
1007{
1008 /* return majority vote of 3 bits */
1009 return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0;
1010}
1011#endif
1012
Joel Johnsonab5dd302020-04-17 09:38:06 -06001013static int bubt_check_boot_mode(const struct bubt_dev *dst)
1014{
Pali Rohár1ce57b82022-07-26 16:11:56 +02001015#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
Pali Rohár85f2a7c2023-02-20 22:42:54 +01001016 int mode;
1017#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_38X)
1018 int secure_mode;
1019#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +02001020#if defined(CONFIG_ARMADA_3700)
1021 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
1022 const struct common_tim_data *hdr =
1023 (struct common_tim_data *)get_load_addr();
1024 u32 id = hdr->boot_flash_sign;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001025 int is_secure = hdr->trusted != 0;
1026 u64 otp_secure_bits = fuse_read_u64(1);
1027 int otp_secure_boot = ((maj3(otp_secure_bits >> 0) << 0) |
1028 (maj3(otp_secure_bits >> 4) << 1)) == 2;
1029 unsigned int otp_boot_device = (maj3(otp_secure_bits >> 48) << 0) |
1030 (maj3(otp_secure_bits >> 52) << 1) |
1031 (maj3(otp_secure_bits >> 56) << 2) |
1032 (maj3(otp_secure_bits >> 60) << 3);
Pali Rohár1ce57b82022-07-26 16:11:56 +02001033#elif defined(CONFIG_ARMADA_32BIT)
1034 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
1035 const struct a38x_main_hdr_v1 *hdr =
1036 (struct a38x_main_hdr_v1 *)get_load_addr();
1037 u32 id = hdr->blockid;
Pali Rohárd83e04a2022-08-23 14:52:24 +02001038#if defined(CONFIG_ARMADA_38X)
1039 int is_secure = a38x_image_is_secure(hdr);
1040 u64 otp_secure_bits = fuse_read_u64(EFUSE_LINE_SECURE_BOOT);
1041 int otp_secure_boot = otp_secure_bits & 0x1;
1042 unsigned int otp_boot_device = (otp_secure_bits >> 8) & 0x7;
1043#endif
Pali Rohár1ce57b82022-07-26 16:11:56 +02001044#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -06001045
Pali Rohár1ce57b82022-07-26 16:11:56 +02001046 for (mode = 0; boot_modes[mode].name; mode++) {
1047 if (boot_modes[mode].id == id)
1048 break;
1049 }
Joel Johnson27e9d072020-04-17 09:38:08 -06001050
Pali Rohár1ce57b82022-07-26 16:11:56 +02001051 if (!boot_modes[mode].name) {
1052 printf("Error: unknown boot device in image header: 0x%x\n", id);
Joel Johnsonab5dd302020-04-17 09:38:06 -06001053 return -ENOEXEC;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001054 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001055
Pali Rohárd83e04a2022-08-23 14:52:24 +02001056 if (strcmp(boot_modes[mode].name, dst->name) != 0) {
1057 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
1058 boot_modes[mode].name, dst->name);
1059 return -ENOEXEC;
1060 }
Pali Rohár1ce57b82022-07-26 16:11:56 +02001061
Pali Rohárd83e04a2022-08-23 14:52:24 +02001062#if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700)
1063 if (otp_secure_bits == (u64)-1) {
1064 printf("Error: cannot read OTP secure bits\n");
1065 return -ENOEXEC;
1066 } else {
1067 if (otp_secure_boot && !is_secure) {
1068 printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n");
1069 return -ENOEXEC;
1070 } else if (!otp_secure_boot && is_secure) {
1071#if defined(CONFIG_ARMADA_3700)
1072 /*
1073 * Armada 3700 BootROM rejects trusted image when secure boot is not enabled.
1074 * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled.
1075 */
1076 printf("Error: secure boot is disabled in OTP but image has secure boot header!\n");
1077 return -ENOEXEC;
Pali Rohár1ce57b82022-07-26 16:11:56 +02001078#endif
Pali Rohárd83e04a2022-08-23 14:52:24 +02001079 } else if (otp_boot_device && otp_boot_device != id) {
1080 for (secure_mode = 0; boot_modes[secure_mode].name; secure_mode++) {
1081 if (boot_modes[secure_mode].id == otp_boot_device)
1082 break;
1083 }
1084 printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n",
1085 boot_modes[secure_mode].name ?: "unknown", dst->name);
1086 return -ENOEXEC;
1087 }
1088 }
1089#endif
1090#endif
1091 return 0;
Joel Johnsonab5dd302020-04-17 09:38:06 -06001092}
1093
1094static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001095{
1096 int err;
1097
1098 /* Check a correct image header exists */
1099 err = check_image_header();
1100 if (err) {
1101 printf("Error: Image header verification failed\n");
1102 return err;
1103 }
1104
Joel Johnsonab5dd302020-04-17 09:38:06 -06001105 err = bubt_check_boot_mode(dst);
1106 if (err) {
1107 printf("Error: Image boot mode verification failed\n");
1108 return err;
1109 }
1110
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001111 return 0;
1112}
1113
1114static int bubt_read_file(struct bubt_dev *src)
1115{
1116 size_t image_size;
1117
1118 if (!src->read) {
1119 printf("Error: Read not supported on device \"%s\"\n",
1120 src->name);
1121 return 0;
1122 }
1123
1124 image_size = src->read(net_boot_file_name);
1125 if (image_size <= 0) {
1126 printf("Error: Failed to read file %s from %s\n",
1127 net_boot_file_name, src->name);
1128 return 0;
1129 }
1130
1131 return image_size;
1132}
1133
1134static int bubt_is_dev_active(struct bubt_dev *dev)
1135{
1136 if (!dev->active) {
Joel Johnson22d78e42020-04-17 09:38:05 -06001137 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001138 dev->name);
1139 return 0;
1140 }
1141
1142 if (!dev->active()) {
1143 printf("Device \"%s\" is inactive\n", dev->name);
1144 return 0;
1145 }
1146
1147 return 1;
1148}
1149
Pali Rohár4b255962023-01-10 22:47:17 +01001150static struct bubt_dev *find_bubt_dev(char *dev_name)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001151{
1152 int dev;
1153
1154 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
1155 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
1156 return &bubt_devs[dev];
1157 }
1158
1159 return 0;
1160}
1161
1162#define DEFAULT_BUBT_SRC "tftp"
1163
1164#ifndef DEFAULT_BUBT_DST
1165#ifdef CONFIG_MVEBU_SPI_BOOT
1166#define DEFAULT_BUBT_DST "spi"
1167#elif defined(CONFIG_MVEBU_NAND_BOOT)
1168#define DEFAULT_BUBT_DST "nand"
1169#elif defined(CONFIG_MVEBU_MMC_BOOT)
1170#define DEFAULT_BUBT_DST "mmc"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001171#elif defined(CONFIG_MVEBU_SATA_BOOT)
1172#define DEFAULT_BUBT_DST "sata"
Konstantin Porotchkine78a3892021-03-17 18:53:43 +02001173#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001174#define DEFAULT_BUBT_DST "error"
1175#endif
1176#endif /* DEFAULT_BUBT_DST */
1177
Pali Rohár4b255962023-01-10 22:47:17 +01001178static int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001179{
1180 struct bubt_dev *src, *dst;
1181 size_t image_size;
1182 char src_dev_name[8];
1183 char dst_dev_name[8];
1184 char *name;
1185 int err;
1186
1187 if (argc < 2)
1188 copy_filename(net_boot_file_name,
1189 CONFIG_MVEBU_UBOOT_DFLT_NAME,
1190 sizeof(net_boot_file_name));
1191 else
1192 copy_filename(net_boot_file_name, argv[1],
1193 sizeof(net_boot_file_name));
1194
1195 if (argc >= 3) {
1196 strncpy(dst_dev_name, argv[2], 8);
1197 } else {
1198 name = DEFAULT_BUBT_DST;
1199 strncpy(dst_dev_name, name, 8);
1200 }
1201
1202 if (argc >= 4)
1203 strncpy(src_dev_name, argv[3], 8);
1204 else
1205 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
1206
1207 /* Figure out the destination device */
1208 dst = find_bubt_dev(dst_dev_name);
1209 if (!dst) {
1210 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
Pali Rohár873fc6a2022-07-26 16:11:59 +02001211 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001212 }
1213
1214 if (!bubt_is_dev_active(dst))
Pali Rohár873fc6a2022-07-26 16:11:59 +02001215 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001216
1217 /* Figure out the source device */
1218 src = find_bubt_dev(src_dev_name);
1219 if (!src) {
1220 printf("Error: Unknown source \"%s\"\n", src_dev_name);
1221 return 1;
1222 }
1223
1224 if (!bubt_is_dev_active(src))
1225 return -ENODEV;
1226
Joel Johnson22d78e42020-04-17 09:38:05 -06001227 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001228 net_boot_file_name, src->name, dst->name);
1229
1230 image_size = bubt_read_file(src);
1231 if (!image_size)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001232 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001233
Joel Johnsonab5dd302020-04-17 09:38:06 -06001234 err = bubt_verify(dst);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001235 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001236 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001237
1238 err = bubt_write_file(dst, image_size);
1239 if (err)
Pali Rohár873fc6a2022-07-26 16:11:59 +02001240 return 1;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001241
1242 return 0;
1243}
1244
1245U_BOOT_CMD(
1246 bubt, 4, 0, do_bubt_cmd,
1247 "Burn a u-boot image to flash",
1248 "[file-name] [destination [source]]\n"
Pali Rohárb4188202021-01-27 11:56:02 +01001249 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
Pali Rohár2a27fc22023-01-22 01:25:12 +01001250 "\t-destination Flash to burn to [spi, nand, mmc, sata]. Default = " DEFAULT_BUBT_DST "\n"
Pali Rohárc61feb92023-01-21 23:29:36 +01001251 "\t-source The source to load image from [tftp, usb, mmc, sata]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkin97d26782016-12-08 12:22:28 +02001252 "Examples:\n"
1253 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
1254 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
1255 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
1256
1257);