blob: 2924b1539f32b8732236e4cb591125e0ec61cbb2 [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 Glass8e201882020-05-10 11:39:54 -060011#include <flash.h>
Simon Glass85f13782019-12-28 10:45:03 -070012#include <image.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <net.h>
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020014#include <vsprintf.h>
15#include <errno.h>
16#include <dm.h>
17
18#include <spi_flash.h>
19#include <spi.h>
20#include <nand.h>
21#include <usb.h>
22#include <fs.h>
23#include <mmc.h>
Konstantin Porotchkina89c0552017-01-08 16:52:06 +020024#ifdef CONFIG_BLK
25#include <blk.h>
26#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020027#include <u-boot/sha1.h>
28#include <u-boot/sha256.h>
29
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020030#if defined(CONFIG_ARMADA_8K)
31#define MAIN_HDR_MAGIC 0xB105B002
32
33struct mvebu_image_header {
34 u32 magic; /* 0-3 */
35 u32 prolog_size; /* 4-7 */
36 u32 prolog_checksum; /* 8-11 */
37 u32 boot_image_size; /* 12-15 */
38 u32 boot_image_checksum; /* 16-19 */
39 u32 rsrvd0; /* 20-23 */
40 u32 load_addr; /* 24-27 */
41 u32 exec_addr; /* 28-31 */
42 u8 uart_cfg; /* 32 */
43 u8 baudrate; /* 33 */
44 u8 ext_count; /* 34 */
45 u8 aux_flags; /* 35 */
46 u32 io_arg_0; /* 36-39 */
47 u32 io_arg_1; /* 40-43 */
48 u32 io_arg_2; /* 43-47 */
49 u32 io_arg_3; /* 48-51 */
50 u32 rsrvd1; /* 52-55 */
51 u32 rsrvd2; /* 56-59 */
52 u32 rsrvd3; /* 60-63 */
53};
54#elif defined(CONFIG_ARMADA_3700) /* A3700 */
55#define HASH_SUM_LEN 16
56#define IMAGE_VERSION_3_6_0 0x030600
57#define IMAGE_VERSION_3_5_0 0x030500
58
Pali Rohár1ce57b82022-07-26 16:11:56 +020059struct tim_boot_flash_sign {
60 unsigned int id;
61 const char *name;
62};
63
64struct tim_boot_flash_sign tim_boot_flash_signs[] = {
65 { 0x454d4d08, "mmc" },
66 { 0x454d4d0b, "mmc" },
67 { 0x5350490a, "spi" },
68 { 0x5350491a, "nand" },
69 { 0x55415223, "uart" },
70 { 0x53415432, "sata" },
71 {},
72};
73
Konstantin Porotchkin97d26782016-12-08 12:22:28 +020074struct common_tim_data {
75 u32 version;
76 u32 identifier;
77 u32 trusted;
78 u32 issue_date;
79 u32 oem_unique_id;
80 u32 reserved[5]; /* Reserve 20 bytes */
81 u32 boot_flash_sign;
82 u32 num_images;
83 u32 num_keys;
84 u32 size_of_reserved;
85};
86
87struct mvebu_image_info {
88 u32 image_id;
89 u32 next_image_id;
90 u32 flash_entry_addr;
91 u32 load_addr;
92 u32 image_size;
93 u32 image_size_to_hash;
94 u32 hash_algorithm_id;
95 u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */
96 u32 partition_number;
97 u32 enc_algorithm_id;
98 u32 encrypt_start_offset;
99 u32 encrypt_size;
100};
Pali Rohár1ce57b82022-07-26 16:11:56 +0200101#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600102
Pali Rohár0f7df222021-10-22 12:41:10 +0200103/* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */
Joel Johnson37b9c122020-04-17 09:38:04 -0600104struct a38x_main_hdr_v1 {
105 u8 blockid; /* 0x0 */
106 u8 flags; /* 0x1 */
Pali Rohár7fea8842021-10-22 12:37:48 +0200107 u16 nandpagesize; /* 0x2-0x3 */
Joel Johnson37b9c122020-04-17 09:38:04 -0600108 u32 blocksize; /* 0x4-0x7 */
109 u8 version; /* 0x8 */
110 u8 headersz_msb; /* 0x9 */
111 u16 headersz_lsb; /* 0xA-0xB */
112 u32 srcaddr; /* 0xC-0xF */
113 u32 destaddr; /* 0x10-0x13 */
114 u32 execaddr; /* 0x14-0x17 */
115 u8 options; /* 0x18 */
116 u8 nandblocksize; /* 0x19 */
117 u8 nandbadblklocation; /* 0x1A */
118 u8 reserved4; /* 0x1B */
119 u16 reserved5; /* 0x1C-0x1D */
120 u8 ext; /* 0x1E */
121 u8 checksum; /* 0x1F */
122};
Joel Johnsonab5dd302020-04-17 09:38:06 -0600123
124struct a38x_boot_mode {
125 unsigned int id;
126 const char *name;
127};
128
129/* The blockid header field values used to indicate boot device of image */
130struct a38x_boot_mode a38x_boot_modes[] = {
131 { 0x4D, "i2c" },
132 { 0x5A, "spi" },
133 { 0x69, "uart" },
134 { 0x78, "sata" },
135 { 0x8B, "nand" },
136 { 0x9C, "pex" },
137 { 0xAE, "mmc" },
138 {},
139};
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200140
Pali Rohár1ce57b82022-07-26 16:11:56 +0200141#endif
142
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200143struct bubt_dev {
144 char name[8];
145 size_t (*read)(const char *file_name);
146 int (*write)(size_t image_size);
147 int (*active)(void);
148};
149
150static ulong get_load_addr(void)
151{
152 const char *addr_str;
153 unsigned long addr;
154
Simon Glass64b723f2017-08-03 12:22:12 -0600155 addr_str = env_get("loadaddr");
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200156 if (addr_str)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600157 addr = hextoul(addr_str, NULL);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200158 else
159 addr = CONFIG_SYS_LOAD_ADDR;
160
161 return addr;
162}
163
164/********************************************************************
165 * eMMC services
166 ********************************************************************/
Jean-Jacques Hiblotd0531672018-01-04 15:23:32 +0100167#if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200168static int mmc_burn_image(size_t image_size)
169{
170 struct mmc *mmc;
171 lbaint_t start_lba;
172 lbaint_t blk_count;
173 ulong blk_written;
174 int err;
175 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200176#ifdef CONFIG_BLK
177 struct blk_desc *blk_desc;
178#endif
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200179 mmc = find_mmc_device(mmc_dev_num);
180 if (!mmc) {
181 printf("No SD/MMC/eMMC card found\n");
182 return -ENOMEDIUM;
183 }
184
185 err = mmc_init(mmc);
186 if (err) {
187 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
188 mmc_dev_num);
189 return err;
190 }
191
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200192 /* SD reserves LBA-0 for MBR and boots from LBA-1,
193 * MMC/eMMC boots from LBA-0
194 */
195 start_lba = IS_SD(mmc) ? 1 : 0;
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200196#ifdef CONFIG_BLK
197 blk_count = image_size / mmc->write_bl_len;
198 if (image_size % mmc->write_bl_len)
199 blk_count += 1;
200
201 blk_desc = mmc_get_blk_desc(mmc);
202 if (!blk_desc) {
203 printf("Error - failed to obtain block descriptor\n");
204 return -ENODEV;
205 }
206 blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
207 (void *)get_load_addr());
208#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200209 blk_count = image_size / mmc->block_dev.blksz;
210 if (image_size % mmc->block_dev.blksz)
211 blk_count += 1;
212
213 blk_written = mmc->block_dev.block_write(mmc_dev_num,
Konstantin Porotchkina89c0552017-01-08 16:52:06 +0200214 start_lba, blk_count,
215 (void *)get_load_addr());
216#endif /* CONFIG_BLK */
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200217 if (blk_written != blk_count) {
218 printf("Error - written %#lx blocks\n", blk_written);
219 return -ENOSPC;
220 }
221 printf("Done!\n");
222
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200223 return 0;
224}
225
226static size_t mmc_read_file(const char *file_name)
227{
228 loff_t act_read = 0;
229 int rc;
230 struct mmc *mmc;
231 const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
232
233 mmc = find_mmc_device(mmc_dev_num);
234 if (!mmc) {
235 printf("No SD/MMC/eMMC card found\n");
236 return 0;
237 }
238
239 if (mmc_init(mmc)) {
240 printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
241 mmc_dev_num);
242 return 0;
243 }
244
245 /* Load from data partition (0) */
246 if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
247 printf("Error: MMC 0 not found\n");
248 return 0;
249 }
250
251 /* Perfrom file read */
252 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
253 if (rc)
254 return 0;
255
256 return act_read;
257}
258
259static int is_mmc_active(void)
260{
261 return 1;
262}
263#else /* CONFIG_DM_MMC */
264static int mmc_burn_image(size_t image_size)
265{
266 return -ENODEV;
267}
268
269static size_t mmc_read_file(const char *file_name)
270{
271 return 0;
272}
273
274static int is_mmc_active(void)
275{
276 return 0;
277}
278#endif /* CONFIG_DM_MMC */
279
280/********************************************************************
281 * SPI services
282 ********************************************************************/
283#ifdef CONFIG_SPI_FLASH
284static int spi_burn_image(size_t image_size)
285{
286 int ret;
287 struct spi_flash *flash;
288 u32 erase_bytes;
289
290 /* Probe the SPI bus to get the flash device */
Tom Rini119d2fb2021-12-11 14:55:48 -0500291 flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
292 CONFIG_SF_DEFAULT_CS,
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200293 CONFIG_SF_DEFAULT_SPEED,
294 CONFIG_SF_DEFAULT_MODE);
295 if (!flash) {
296 printf("Failed to probe SPI Flash\n");
297 return -ENOMEDIUM;
298 }
299
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200300 erase_bytes = image_size +
301 (flash->erase_size - image_size % flash->erase_size);
302 printf("Erasing %d bytes (%d blocks) at offset 0 ...",
303 erase_bytes, erase_bytes / flash->erase_size);
304 ret = spi_flash_erase(flash, 0, erase_bytes);
305 if (ret)
306 printf("Error!\n");
307 else
308 printf("Done!\n");
309
310 printf("Writing %d bytes from 0x%lx to offset 0 ...",
311 (int)image_size, get_load_addr());
312 ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
313 if (ret)
314 printf("Error!\n");
315 else
316 printf("Done!\n");
317
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200318 return ret;
319}
320
321static int is_spi_active(void)
322{
323 return 1;
324}
325
326#else /* CONFIG_SPI_FLASH */
327static int spi_burn_image(size_t image_size)
328{
329 return -ENODEV;
330}
331
332static int is_spi_active(void)
333{
334 return 0;
335}
336#endif /* CONFIG_SPI_FLASH */
337
338/********************************************************************
339 * NAND services
340 ********************************************************************/
341#ifdef CONFIG_CMD_NAND
342static int nand_burn_image(size_t image_size)
343{
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300344 int ret;
345 uint32_t block_size;
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500346 struct mtd_info *mtd;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200347
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500348 mtd = get_nand_dev_by_index(nand_curr_device);
349 if (!mtd) {
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200350 puts("\nno devices available\n");
351 return -ENOMEDIUM;
352 }
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500353 block_size = mtd->erasesize;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200354
355 /* Align U-Boot size to currently used blocksize */
356 image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
357
Joel Johnson22d78e42020-04-17 09:38:05 -0600358 /* Erase the U-Boot image space */
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200359 printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500360 ret = nand_erase(mtd, 0, image_size);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200361 if (ret) {
362 printf("Error!\n");
363 goto error;
364 }
365 printf("Done!\n");
366
367 /* Write the image to flash */
Konstantin Porotchkinc6db27b2017-03-28 18:16:56 +0300368 printf("Writing %d bytes from 0x%lx to offset 0 ... ",
369 (int)image_size, get_load_addr());
Grygorii Strashko4a240bc2017-06-26 19:13:02 -0500370 ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr());
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200371 if (ret)
372 printf("Error!\n");
373 else
374 printf("Done!\n");
375
376error:
377 return ret;
378}
379
380static int is_nand_active(void)
381{
382 return 1;
383}
384
385#else /* CONFIG_CMD_NAND */
386static int nand_burn_image(size_t image_size)
387{
388 return -ENODEV;
389}
390
391static int is_nand_active(void)
392{
393 return 0;
394}
395#endif /* CONFIG_CMD_NAND */
396
397/********************************************************************
398 * USB services
399 ********************************************************************/
400#if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
401static size_t usb_read_file(const char *file_name)
402{
403 loff_t act_read = 0;
404 struct udevice *dev;
405 int rc;
406
407 usb_stop();
408
409 if (usb_init() < 0) {
410 printf("Error: usb_init failed\n");
411 return 0;
412 }
413
414 /* Try to recognize storage devices immediately */
415 blk_first_device(IF_TYPE_USB, &dev);
416 if (!dev) {
417 printf("Error: USB storage device not found\n");
418 return 0;
419 }
420
421 /* Always load from usb 0 */
422 if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
423 printf("Error: USB 0 not found\n");
424 return 0;
425 }
426
427 /* Perfrom file read */
428 rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
429 if (rc)
430 return 0;
431
432 return act_read;
433}
434
435static int is_usb_active(void)
436{
437 return 1;
438}
439
440#else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
441static size_t usb_read_file(const char *file_name)
442{
443 return 0;
444}
445
446static int is_usb_active(void)
447{
448 return 0;
449}
450#endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
451
452/********************************************************************
453 * Network services
454 ********************************************************************/
455#ifdef CONFIG_CMD_NET
456static size_t tftp_read_file(const char *file_name)
457{
Pali Rohár2c96d022022-07-26 16:11:57 +0200458 int ret;
459
Simon Glass892265d2019-12-28 10:45:02 -0700460 /*
461 * update global variable image_load_addr before tftp file from network
462 */
463 image_load_addr = get_load_addr();
Pali Rohár2c96d022022-07-26 16:11:57 +0200464 ret = net_loop(TFTPGET);
465 return ret > 0 ? ret : 0;
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200466}
467
468static int is_tftp_active(void)
469{
470 return 1;
471}
472
473#else
474static size_t tftp_read_file(const char *file_name)
475{
476 return 0;
477}
478
479static int is_tftp_active(void)
480{
481 return 0;
482}
483#endif /* CONFIG_CMD_NET */
484
485enum bubt_devices {
486 BUBT_DEV_NET = 0,
487 BUBT_DEV_USB,
488 BUBT_DEV_MMC,
489 BUBT_DEV_SPI,
490 BUBT_DEV_NAND,
491
492 BUBT_MAX_DEV
493};
494
495struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
496 {"tftp", tftp_read_file, NULL, is_tftp_active},
497 {"usb", usb_read_file, NULL, is_usb_active},
498 {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active},
499 {"spi", NULL, spi_burn_image, is_spi_active},
500 {"nand", NULL, nand_burn_image, is_nand_active},
501};
502
503static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
504{
505 if (!dst->write) {
506 printf("Error: Write not supported on device %s\n", dst->name);
507 return -ENOTSUPP;
508 }
509
510 return dst->write(image_size);
511}
512
513#if defined(CONFIG_ARMADA_8K)
514u32 do_checksum32(u32 *start, int32_t len)
515{
516 u32 sum = 0;
517 u32 *startp = start;
518
519 do {
520 sum += *startp;
521 startp++;
522 len -= 4;
523 } while (len > 0);
524
525 return sum;
526}
527
528static int check_image_header(void)
529{
530 struct mvebu_image_header *hdr =
531 (struct mvebu_image_header *)get_load_addr();
532 u32 header_len = hdr->prolog_size;
533 u32 checksum;
534 u32 checksum_ref = hdr->prolog_checksum;
535
536 /*
537 * For now compare checksum, and magic. Later we can
538 * verify more stuff on the header like interface type, etc
539 */
540 if (hdr->magic != MAIN_HDR_MAGIC) {
541 printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
542 hdr->magic, MAIN_HDR_MAGIC);
543 return -ENOEXEC;
544 }
545
546 /* The checksum value is discarded from checksum calculation */
547 hdr->prolog_checksum = 0;
548
549 checksum = do_checksum32((u32 *)hdr, header_len);
550 if (checksum != checksum_ref) {
551 printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
552 checksum, checksum_ref);
553 return -ENOEXEC;
554 }
555
556 /* Restore the checksum before writing */
557 hdr->prolog_checksum = checksum_ref;
558 printf("Image checksum...OK!\n");
559
560 return 0;
561}
562#elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
563static int check_image_header(void)
564{
565 struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
566 int image_num;
567 u8 hash_160_output[SHA1_SUM_LEN];
568 u8 hash_256_output[SHA256_SUM_LEN];
569 sha1_context hash1_text;
570 sha256_context hash256_text;
571 u8 *hash_output;
572 u32 hash_algorithm_id;
573 u32 image_size_to_hash;
574 u32 flash_entry_addr;
575 u32 *hash_value;
576 u32 internal_hash[HASH_SUM_LEN];
577 const u8 *buff;
578 u32 num_of_image = hdr->num_images;
579 u32 version = hdr->version;
580 u32 trusted = hdr->trusted;
581
582 /* bubt checksum validation only supports nontrusted images */
583 if (trusted == 1) {
584 printf("bypass image validation, ");
585 printf("only untrusted image is supported now\n");
586 return 0;
587 }
588 /* only supports image version 3.5 and 3.6 */
589 if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
590 printf("Error: Unsupported Image version = 0x%08x\n", version);
591 return -ENOEXEC;
592 }
593 /* validate images hash value */
594 for (image_num = 0; image_num < num_of_image; image_num++) {
595 struct mvebu_image_info *info =
596 (struct mvebu_image_info *)(get_load_addr() +
597 sizeof(struct common_tim_data) +
598 image_num * sizeof(struct mvebu_image_info));
599 hash_algorithm_id = info->hash_algorithm_id;
600 image_size_to_hash = info->image_size_to_hash;
601 flash_entry_addr = info->flash_entry_addr;
602 hash_value = info->hash;
603 buff = (const u8 *)(get_load_addr() + flash_entry_addr);
604
605 if (image_num == 0) {
606 /*
607 * The first image includes hash values in its content.
608 * For hash calculation, we need to save the original
609 * hash values to a local variable that will be
610 * copied back for comparsion and set all zeros to
611 * the orignal hash values for calculating new value.
612 * First image original format :
613 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
614 * Replaced first image format :
615 * x...x (datum1) 0...0(hash values) x...x(datum2)
616 */
617 memcpy(internal_hash, hash_value,
618 sizeof(internal_hash));
619 memset(hash_value, 0, sizeof(internal_hash));
620 }
621 if (image_size_to_hash == 0) {
622 printf("Warning: Image_%d hash checksum is disabled, ",
623 image_num);
624 printf("skip the image validation.\n");
625 continue;
626 }
627 switch (hash_algorithm_id) {
628 case SHA1_SUM_LEN:
629 sha1_starts(&hash1_text);
630 sha1_update(&hash1_text, buff, image_size_to_hash);
631 sha1_finish(&hash1_text, hash_160_output);
632 hash_output = hash_160_output;
633 break;
634 case SHA256_SUM_LEN:
635 sha256_starts(&hash256_text);
636 sha256_update(&hash256_text, buff, image_size_to_hash);
637 sha256_finish(&hash256_text, hash_256_output);
638 hash_output = hash_256_output;
639 break;
640 default:
641 printf("Error: Unsupported hash_algorithm_id = %d\n",
642 hash_algorithm_id);
643 return -ENOEXEC;
644 }
645 if (image_num == 0)
646 memcpy(hash_value, internal_hash,
647 sizeof(internal_hash));
648 if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
649 printf("Error: Image_%d checksum is not correct\n",
650 image_num);
651 return -ENOEXEC;
652 }
653 }
654 printf("Image checksum...OK!\n");
655
Joel Johnson37b9c122020-04-17 09:38:04 -0600656 return 0;
657}
Pali Rohár1ce57b82022-07-26 16:11:56 +0200658#elif defined(CONFIG_ARMADA_32BIT)
Joel Johnson37b9c122020-04-17 09:38:04 -0600659static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h)
660{
661 if (h->version == 1)
662 return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb);
663
664 printf("Error: Invalid A38x image (header version 0x%x unknown)!\n",
665 h->version);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200666 return 0;
667}
668
Joel Johnson37b9c122020-04-17 09:38:04 -0600669static uint8_t image_checksum8(const void *start, size_t len)
670{
671 u8 csum = 0;
672 const u8 *p = start;
673
674 while (len) {
675 csum += *p;
676 ++p;
677 --len;
678 }
679
680 return csum;
681}
682
683static int check_image_header(void)
684{
685 u8 checksum;
686 const struct a38x_main_hdr_v1 *hdr =
687 (struct a38x_main_hdr_v1 *)get_load_addr();
688 const size_t image_size = a38x_header_size(hdr);
689
690 if (!image_size)
691 return -ENOEXEC;
692
693 checksum = image_checksum8(hdr, image_size);
694 checksum -= hdr->checksum;
695 if (checksum != hdr->checksum) {
696 printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n",
697 checksum, hdr->checksum);
698 return -ENOEXEC;
699 }
700
701 printf("Image checksum...OK!\n");
702 return 0;
703}
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200704#else /* Not ARMADA? */
705static int check_image_header(void)
706{
707 printf("bubt cmd does not support this SoC device or family!\n");
708 return -ENOEXEC;
709}
710#endif
711
Joel Johnsonab5dd302020-04-17 09:38:06 -0600712static int bubt_check_boot_mode(const struct bubt_dev *dst)
713{
Pali Rohár1ce57b82022-07-26 16:11:56 +0200714#if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT)
715 int mode;
716#if defined(CONFIG_ARMADA_3700)
717 const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs;
718 const struct common_tim_data *hdr =
719 (struct common_tim_data *)get_load_addr();
720 u32 id = hdr->boot_flash_sign;
721#elif defined(CONFIG_ARMADA_32BIT)
722 const struct a38x_boot_mode *boot_modes = a38x_boot_modes;
723 const struct a38x_main_hdr_v1 *hdr =
724 (struct a38x_main_hdr_v1 *)get_load_addr();
725 u32 id = hdr->blockid;
726#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -0600727
Pali Rohár1ce57b82022-07-26 16:11:56 +0200728 for (mode = 0; boot_modes[mode].name; mode++) {
729 if (boot_modes[mode].id == id)
730 break;
731 }
Joel Johnson27e9d072020-04-17 09:38:08 -0600732
Pali Rohár1ce57b82022-07-26 16:11:56 +0200733 if (!boot_modes[mode].name) {
734 printf("Error: unknown boot device in image header: 0x%x\n", id);
Joel Johnsonab5dd302020-04-17 09:38:06 -0600735 return -ENOEXEC;
Joel Johnsonab5dd302020-04-17 09:38:06 -0600736 }
Pali Rohár1ce57b82022-07-26 16:11:56 +0200737
738 if (strcmp(boot_modes[mode].name, dst->name) == 0)
739 return 0;
740
741 printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n",
742 boot_modes[mode].name, dst->name);
743 return -ENOEXEC;
744#else
745 return 0;
746#endif
Joel Johnsonab5dd302020-04-17 09:38:06 -0600747}
748
749static int bubt_verify(const struct bubt_dev *dst)
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200750{
751 int err;
752
753 /* Check a correct image header exists */
754 err = check_image_header();
755 if (err) {
756 printf("Error: Image header verification failed\n");
757 return err;
758 }
759
Joel Johnsonab5dd302020-04-17 09:38:06 -0600760 err = bubt_check_boot_mode(dst);
761 if (err) {
762 printf("Error: Image boot mode verification failed\n");
763 return err;
764 }
765
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200766 return 0;
767}
768
769static int bubt_read_file(struct bubt_dev *src)
770{
771 size_t image_size;
772
773 if (!src->read) {
774 printf("Error: Read not supported on device \"%s\"\n",
775 src->name);
776 return 0;
777 }
778
779 image_size = src->read(net_boot_file_name);
780 if (image_size <= 0) {
781 printf("Error: Failed to read file %s from %s\n",
782 net_boot_file_name, src->name);
783 return 0;
784 }
785
786 return image_size;
787}
788
789static int bubt_is_dev_active(struct bubt_dev *dev)
790{
791 if (!dev->active) {
Joel Johnson22d78e42020-04-17 09:38:05 -0600792 printf("Device \"%s\" not supported by U-Boot image\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200793 dev->name);
794 return 0;
795 }
796
797 if (!dev->active()) {
798 printf("Device \"%s\" is inactive\n", dev->name);
799 return 0;
800 }
801
802 return 1;
803}
804
805struct bubt_dev *find_bubt_dev(char *dev_name)
806{
807 int dev;
808
809 for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
810 if (strcmp(bubt_devs[dev].name, dev_name) == 0)
811 return &bubt_devs[dev];
812 }
813
814 return 0;
815}
816
817#define DEFAULT_BUBT_SRC "tftp"
818
819#ifndef DEFAULT_BUBT_DST
820#ifdef CONFIG_MVEBU_SPI_BOOT
821#define DEFAULT_BUBT_DST "spi"
822#elif defined(CONFIG_MVEBU_NAND_BOOT)
823#define DEFAULT_BUBT_DST "nand"
824#elif defined(CONFIG_MVEBU_MMC_BOOT)
825#define DEFAULT_BUBT_DST "mmc"
Konstantin Porotchkine78a3892021-03-17 18:53:43 +0200826#else
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200827#define DEFAULT_BUBT_DST "error"
828#endif
829#endif /* DEFAULT_BUBT_DST */
830
Simon Glassed38aef2020-05-10 11:40:03 -0600831int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200832{
833 struct bubt_dev *src, *dst;
834 size_t image_size;
835 char src_dev_name[8];
836 char dst_dev_name[8];
837 char *name;
838 int err;
839
840 if (argc < 2)
841 copy_filename(net_boot_file_name,
842 CONFIG_MVEBU_UBOOT_DFLT_NAME,
843 sizeof(net_boot_file_name));
844 else
845 copy_filename(net_boot_file_name, argv[1],
846 sizeof(net_boot_file_name));
847
848 if (argc >= 3) {
849 strncpy(dst_dev_name, argv[2], 8);
850 } else {
851 name = DEFAULT_BUBT_DST;
852 strncpy(dst_dev_name, name, 8);
853 }
854
855 if (argc >= 4)
856 strncpy(src_dev_name, argv[3], 8);
857 else
858 strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
859
860 /* Figure out the destination device */
861 dst = find_bubt_dev(dst_dev_name);
862 if (!dst) {
863 printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
864 return -EINVAL;
865 }
866
867 if (!bubt_is_dev_active(dst))
868 return -ENODEV;
869
870 /* Figure out the source device */
871 src = find_bubt_dev(src_dev_name);
872 if (!src) {
873 printf("Error: Unknown source \"%s\"\n", src_dev_name);
874 return 1;
875 }
876
877 if (!bubt_is_dev_active(src))
878 return -ENODEV;
879
Joel Johnson22d78e42020-04-17 09:38:05 -0600880 printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n",
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200881 net_boot_file_name, src->name, dst->name);
882
883 image_size = bubt_read_file(src);
884 if (!image_size)
885 return -EIO;
886
Joel Johnsonab5dd302020-04-17 09:38:06 -0600887 err = bubt_verify(dst);
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200888 if (err)
889 return err;
890
891 err = bubt_write_file(dst, image_size);
892 if (err)
893 return err;
894
895 return 0;
896}
897
898U_BOOT_CMD(
899 bubt, 4, 0, do_bubt_cmd,
900 "Burn a u-boot image to flash",
901 "[file-name] [destination [source]]\n"
Pali Rohárb4188202021-01-27 11:56:02 +0100902 "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n"
903 "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n"
904 "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n"
Konstantin Porotchkin97d26782016-12-08 12:22:28 +0200905 "Examples:\n"
906 "\tbubt - Burn flash-image.bin from tftp to active boot device\n"
907 "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
908 "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
909
910);