xref: /openbmc/u-boot/cmd/mvebu/bubt.c (revision be059e88)
1 /*
2  * Copyright (C) 2016 Marvell International Ltd.
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  * https://spdx.org/licenses
6  */
7 
8 #include <config.h>
9 #include <common.h>
10 #include <command.h>
11 #include <vsprintf.h>
12 #include <errno.h>
13 #include <dm.h>
14 
15 #include <spi_flash.h>
16 #include <spi.h>
17 #include <nand.h>
18 #include <usb.h>
19 #include <fs.h>
20 #include <mmc.h>
21 #ifdef CONFIG_BLK
22 #include <blk.h>
23 #endif
24 #include <u-boot/sha1.h>
25 #include <u-boot/sha256.h>
26 
27 #ifndef CONFIG_SYS_MMC_ENV_DEV
28 #define CONFIG_SYS_MMC_ENV_DEV	0
29 #endif
30 
31 #if defined(CONFIG_ARMADA_8K)
32 #define MAIN_HDR_MAGIC		0xB105B002
33 
34 struct mvebu_image_header {
35 	u32	magic;			/*  0-3  */
36 	u32	prolog_size;		/*  4-7  */
37 	u32	prolog_checksum;	/*  8-11 */
38 	u32	boot_image_size;	/* 12-15 */
39 	u32	boot_image_checksum;	/* 16-19 */
40 	u32	rsrvd0;			/* 20-23 */
41 	u32	load_addr;		/* 24-27 */
42 	u32	exec_addr;		/* 28-31 */
43 	u8	uart_cfg;		/*  32   */
44 	u8	baudrate;		/*  33   */
45 	u8	ext_count;		/*  34   */
46 	u8	aux_flags;		/*  35   */
47 	u32	io_arg_0;		/* 36-39 */
48 	u32	io_arg_1;		/* 40-43 */
49 	u32	io_arg_2;		/* 43-47 */
50 	u32	io_arg_3;		/* 48-51 */
51 	u32	rsrvd1;			/* 52-55 */
52 	u32	rsrvd2;			/* 56-59 */
53 	u32	rsrvd3;			/* 60-63 */
54 };
55 #elif defined(CONFIG_ARMADA_3700)	/* A3700 */
56 #define HASH_SUM_LEN		16
57 #define IMAGE_VERSION_3_6_0	0x030600
58 #define IMAGE_VERSION_3_5_0	0x030500
59 
60 struct common_tim_data {
61 	u32	version;
62 	u32	identifier;
63 	u32	trusted;
64 	u32	issue_date;
65 	u32	oem_unique_id;
66 	u32	reserved[5];		/* Reserve 20 bytes */
67 	u32	boot_flash_sign;
68 	u32	num_images;
69 	u32	num_keys;
70 	u32	size_of_reserved;
71 };
72 
73 struct mvebu_image_info {
74 	u32	image_id;
75 	u32	next_image_id;
76 	u32	flash_entry_addr;
77 	u32	load_addr;
78 	u32	image_size;
79 	u32	image_size_to_hash;
80 	u32	hash_algorithm_id;
81 	u32	hash[HASH_SUM_LEN];	/* Reserve 512 bits for the hash */
82 	u32	partition_number;
83 	u32	enc_algorithm_id;
84 	u32	encrypt_start_offset;
85 	u32	encrypt_size;
86 };
87 #endif /* CONFIG_ARMADA_XXX */
88 
89 struct bubt_dev {
90 	char name[8];
91 	size_t (*read)(const char *file_name);
92 	int (*write)(size_t image_size);
93 	int (*active)(void);
94 };
95 
96 static ulong get_load_addr(void)
97 {
98 	const char *addr_str;
99 	unsigned long addr;
100 
101 	addr_str = getenv("loadaddr");
102 	if (addr_str)
103 		addr = simple_strtoul(addr_str, NULL, 16);
104 	else
105 		addr = CONFIG_SYS_LOAD_ADDR;
106 
107 	return addr;
108 }
109 
110 /********************************************************************
111  *     eMMC services
112  ********************************************************************/
113 #ifdef CONFIG_DM_MMC
114 static int mmc_burn_image(size_t image_size)
115 {
116 	struct mmc	*mmc;
117 	lbaint_t	start_lba;
118 	lbaint_t	blk_count;
119 	ulong		blk_written;
120 	int		err;
121 	const u8	mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
122 #ifdef CONFIG_BLK
123 	struct blk_desc *blk_desc;
124 #endif
125 	mmc = find_mmc_device(mmc_dev_num);
126 	if (!mmc) {
127 		printf("No SD/MMC/eMMC card found\n");
128 		return -ENOMEDIUM;
129 	}
130 
131 	err = mmc_init(mmc);
132 	if (err) {
133 		printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
134 		       mmc_dev_num);
135 		return err;
136 	}
137 
138 #ifdef CONFIG_SYS_MMC_ENV_PART
139 	if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART) {
140 		err = mmc_switch_part(mmc_dev_num, CONFIG_SYS_MMC_ENV_PART);
141 		if (err) {
142 			printf("MMC partition switch failed\n");
143 			return err;
144 		}
145 	}
146 #endif
147 
148 	/* SD reserves LBA-0 for MBR and boots from LBA-1,
149 	 * MMC/eMMC boots from LBA-0
150 	 */
151 	start_lba = IS_SD(mmc) ? 1 : 0;
152 #ifdef CONFIG_BLK
153 	blk_count = image_size / mmc->write_bl_len;
154 	if (image_size % mmc->write_bl_len)
155 		blk_count += 1;
156 
157 	blk_desc = mmc_get_blk_desc(mmc);
158 	if (!blk_desc) {
159 		printf("Error - failed to obtain block descriptor\n");
160 		return -ENODEV;
161 	}
162 	blk_written = blk_dwrite(blk_desc, start_lba, blk_count,
163 				 (void *)get_load_addr());
164 #else
165 	blk_count = image_size / mmc->block_dev.blksz;
166 	if (image_size % mmc->block_dev.blksz)
167 		blk_count += 1;
168 
169 	blk_written = mmc->block_dev.block_write(mmc_dev_num,
170 						 start_lba, blk_count,
171 						 (void *)get_load_addr());
172 #endif /* CONFIG_BLK */
173 	if (blk_written != blk_count) {
174 		printf("Error - written %#lx blocks\n", blk_written);
175 		return -ENOSPC;
176 	}
177 	printf("Done!\n");
178 
179 #ifdef CONFIG_SYS_MMC_ENV_PART
180 	if (mmc->part_num != CONFIG_SYS_MMC_ENV_PART)
181 		mmc_switch_part(mmc_dev_num, mmc->part_num);
182 #endif
183 
184 	return 0;
185 }
186 
187 static size_t mmc_read_file(const char *file_name)
188 {
189 	loff_t		act_read = 0;
190 	int		rc;
191 	struct mmc	*mmc;
192 	const u8	mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;
193 
194 	mmc = find_mmc_device(mmc_dev_num);
195 	if (!mmc) {
196 		printf("No SD/MMC/eMMC card found\n");
197 		return 0;
198 	}
199 
200 	if (mmc_init(mmc)) {
201 		printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
202 		       mmc_dev_num);
203 		return 0;
204 	}
205 
206 	/* Load from data partition (0) */
207 	if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) {
208 		printf("Error: MMC 0 not found\n");
209 		return 0;
210 	}
211 
212 	/* Perfrom file read */
213 	rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
214 	if (rc)
215 		return 0;
216 
217 	return act_read;
218 }
219 
220 static int is_mmc_active(void)
221 {
222 	return 1;
223 }
224 #else /* CONFIG_DM_MMC */
225 static int mmc_burn_image(size_t image_size)
226 {
227 	return -ENODEV;
228 }
229 
230 static size_t mmc_read_file(const char *file_name)
231 {
232 	return 0;
233 }
234 
235 static int is_mmc_active(void)
236 {
237 	return 0;
238 }
239 #endif /* CONFIG_DM_MMC */
240 
241 /********************************************************************
242  *     SPI services
243  ********************************************************************/
244 #ifdef CONFIG_SPI_FLASH
245 static int spi_burn_image(size_t image_size)
246 {
247 	int ret;
248 	struct spi_flash *flash;
249 	u32 erase_bytes;
250 
251 	/* Probe the SPI bus to get the flash device */
252 	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
253 				CONFIG_ENV_SPI_CS,
254 				CONFIG_SF_DEFAULT_SPEED,
255 				CONFIG_SF_DEFAULT_MODE);
256 	if (!flash) {
257 		printf("Failed to probe SPI Flash\n");
258 		return -ENOMEDIUM;
259 	}
260 
261 #ifdef CONFIG_SPI_FLASH_PROTECTION
262 	spi_flash_protect(flash, 0);
263 #endif
264 	erase_bytes = image_size +
265 		(flash->erase_size - image_size % flash->erase_size);
266 	printf("Erasing %d bytes (%d blocks) at offset 0 ...",
267 	       erase_bytes, erase_bytes / flash->erase_size);
268 	ret = spi_flash_erase(flash, 0, erase_bytes);
269 	if (ret)
270 		printf("Error!\n");
271 	else
272 		printf("Done!\n");
273 
274 	printf("Writing %d bytes from 0x%lx to offset 0 ...",
275 	       (int)image_size, get_load_addr());
276 	ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr());
277 	if (ret)
278 		printf("Error!\n");
279 	else
280 		printf("Done!\n");
281 
282 #ifdef CONFIG_SPI_FLASH_PROTECTION
283 	spi_flash_protect(flash, 1);
284 #endif
285 
286 	return ret;
287 }
288 
289 static int is_spi_active(void)
290 {
291 	return 1;
292 }
293 
294 #else /* CONFIG_SPI_FLASH */
295 static int spi_burn_image(size_t image_size)
296 {
297 	return -ENODEV;
298 }
299 
300 static int is_spi_active(void)
301 {
302 	return 0;
303 }
304 #endif /* CONFIG_SPI_FLASH */
305 
306 /********************************************************************
307  *     NAND services
308  ********************************************************************/
309 #ifdef CONFIG_CMD_NAND
310 static int nand_burn_image(size_t image_size)
311 {
312 	int ret, block_size;
313 	nand_info_t *nand;
314 	int dev = nand_curr_device;
315 
316 	if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
317 	    (!nand_info[dev].name)) {
318 		puts("\nno devices available\n");
319 		return -ENOMEDIUM;
320 	}
321 	nand = &nand_info[dev];
322 	block_size = nand->erasesize;
323 
324 	/* Align U-Boot size to currently used blocksize */
325 	image_size = ((image_size + (block_size - 1)) & (~(block_size - 1)));
326 
327 	/* Erase the U-BOOT image space */
328 	printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size);
329 	ret = nand_erase(nand, 0, image_size);
330 	if (ret) {
331 		printf("Error!\n");
332 		goto error;
333 	}
334 	printf("Done!\n");
335 
336 	/* Write the image to flash */
337 	printf("Writing image:...");
338 	printf("&image_size = 0x%p\n", (void *)&image_size);
339 	ret = nand_write(nand, 0, &image_size, (void *)get_load_addr());
340 	if (ret)
341 		printf("Error!\n");
342 	else
343 		printf("Done!\n");
344 
345 error:
346 	return ret;
347 }
348 
349 static int is_nand_active(void)
350 {
351 	return 1;
352 }
353 
354 #else /* CONFIG_CMD_NAND */
355 static int nand_burn_image(size_t image_size)
356 {
357 	return -ENODEV;
358 }
359 
360 static int is_nand_active(void)
361 {
362 	return 0;
363 }
364 #endif /* CONFIG_CMD_NAND */
365 
366 /********************************************************************
367  *     USB services
368  ********************************************************************/
369 #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK)
370 static size_t usb_read_file(const char *file_name)
371 {
372 	loff_t act_read = 0;
373 	struct udevice *dev;
374 	int rc;
375 
376 	usb_stop();
377 
378 	if (usb_init() < 0) {
379 		printf("Error: usb_init failed\n");
380 		return 0;
381 	}
382 
383 	/* Try to recognize storage devices immediately */
384 	blk_first_device(IF_TYPE_USB, &dev);
385 	if (!dev) {
386 		printf("Error: USB storage device not found\n");
387 		return 0;
388 	}
389 
390 	/* Always load from usb 0 */
391 	if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) {
392 		printf("Error: USB 0 not found\n");
393 		return 0;
394 	}
395 
396 	/* Perfrom file read */
397 	rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read);
398 	if (rc)
399 		return 0;
400 
401 	return act_read;
402 }
403 
404 static int is_usb_active(void)
405 {
406 	return 1;
407 }
408 
409 #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
410 static size_t usb_read_file(const char *file_name)
411 {
412 	return 0;
413 }
414 
415 static int is_usb_active(void)
416 {
417 	return 0;
418 }
419 #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */
420 
421 /********************************************************************
422  *     Network services
423  ********************************************************************/
424 #ifdef CONFIG_CMD_NET
425 static size_t tftp_read_file(const char *file_name)
426 {
427 	/* update global variable load_addr before tftp file from network */
428 	load_addr = get_load_addr();
429 	return net_loop(TFTPGET);
430 }
431 
432 static int is_tftp_active(void)
433 {
434 	return 1;
435 }
436 
437 #else
438 static size_t tftp_read_file(const char *file_name)
439 {
440 	return 0;
441 }
442 
443 static int is_tftp_active(void)
444 {
445 	return 0;
446 }
447 #endif /* CONFIG_CMD_NET */
448 
449 enum bubt_devices {
450 	BUBT_DEV_NET = 0,
451 	BUBT_DEV_USB,
452 	BUBT_DEV_MMC,
453 	BUBT_DEV_SPI,
454 	BUBT_DEV_NAND,
455 
456 	BUBT_MAX_DEV
457 };
458 
459 struct bubt_dev bubt_devs[BUBT_MAX_DEV] = {
460 	{"tftp", tftp_read_file, NULL, is_tftp_active},
461 	{"usb",  usb_read_file,  NULL, is_usb_active},
462 	{"mmc",  mmc_read_file,  mmc_burn_image, is_mmc_active},
463 	{"spi",  NULL, spi_burn_image,  is_spi_active},
464 	{"nand", NULL, nand_burn_image, is_nand_active},
465 };
466 
467 static int bubt_write_file(struct bubt_dev *dst, size_t image_size)
468 {
469 	if (!dst->write) {
470 		printf("Error: Write not supported on device %s\n", dst->name);
471 		return -ENOTSUPP;
472 	}
473 
474 	return dst->write(image_size);
475 }
476 
477 #if defined(CONFIG_ARMADA_8K)
478 u32 do_checksum32(u32 *start, int32_t len)
479 {
480 	u32 sum = 0;
481 	u32 *startp = start;
482 
483 	do {
484 		sum += *startp;
485 		startp++;
486 		len -= 4;
487 	} while (len > 0);
488 
489 	return sum;
490 }
491 
492 static int check_image_header(void)
493 {
494 	struct mvebu_image_header *hdr =
495 			(struct mvebu_image_header *)get_load_addr();
496 	u32 header_len = hdr->prolog_size;
497 	u32 checksum;
498 	u32 checksum_ref = hdr->prolog_checksum;
499 
500 	/*
501 	 * For now compare checksum, and magic. Later we can
502 	 * verify more stuff on the header like interface type, etc
503 	 */
504 	if (hdr->magic != MAIN_HDR_MAGIC) {
505 		printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n",
506 		       hdr->magic, MAIN_HDR_MAGIC);
507 		return -ENOEXEC;
508 	}
509 
510 	/* The checksum value is discarded from checksum calculation */
511 	hdr->prolog_checksum = 0;
512 
513 	checksum = do_checksum32((u32 *)hdr, header_len);
514 	if (checksum != checksum_ref) {
515 		printf("Error: Bad Image checksum. 0x%x != 0x%x\n",
516 		       checksum, checksum_ref);
517 		return -ENOEXEC;
518 	}
519 
520 	/* Restore the checksum before writing */
521 	hdr->prolog_checksum = checksum_ref;
522 	printf("Image checksum...OK!\n");
523 
524 	return 0;
525 }
526 #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */
527 static int check_image_header(void)
528 {
529 	struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr();
530 	int image_num;
531 	u8 hash_160_output[SHA1_SUM_LEN];
532 	u8 hash_256_output[SHA256_SUM_LEN];
533 	sha1_context hash1_text;
534 	sha256_context hash256_text;
535 	u8 *hash_output;
536 	u32 hash_algorithm_id;
537 	u32 image_size_to_hash;
538 	u32 flash_entry_addr;
539 	u32 *hash_value;
540 	u32 internal_hash[HASH_SUM_LEN];
541 	const u8 *buff;
542 	u32 num_of_image = hdr->num_images;
543 	u32 version = hdr->version;
544 	u32 trusted = hdr->trusted;
545 
546 	/* bubt checksum validation only supports nontrusted images */
547 	if (trusted == 1) {
548 		printf("bypass image validation, ");
549 		printf("only untrusted image is supported now\n");
550 		return 0;
551 	}
552 	/* only supports image version 3.5 and 3.6 */
553 	if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) {
554 		printf("Error: Unsupported Image version = 0x%08x\n", version);
555 		return -ENOEXEC;
556 	}
557 	/* validate images hash value */
558 	for (image_num = 0; image_num < num_of_image; image_num++) {
559 		struct mvebu_image_info *info =
560 				(struct mvebu_image_info *)(get_load_addr() +
561 				sizeof(struct common_tim_data) +
562 				image_num * sizeof(struct mvebu_image_info));
563 		hash_algorithm_id = info->hash_algorithm_id;
564 		image_size_to_hash = info->image_size_to_hash;
565 		flash_entry_addr = info->flash_entry_addr;
566 		hash_value = info->hash;
567 		buff = (const u8 *)(get_load_addr() + flash_entry_addr);
568 
569 		if (image_num == 0) {
570 			/*
571 			 * The first image includes hash values in its content.
572 			 * For hash calculation, we need to save the original
573 			 * hash values to a local variable that will be
574 			 * copied back for comparsion and set all zeros to
575 			 * the orignal hash values for calculating new value.
576 			 * First image original format :
577 			 * x...x (datum1) x...x(orig. hash values) x...x(datum2)
578 			 * Replaced first image format :
579 			 * x...x (datum1) 0...0(hash values) x...x(datum2)
580 			 */
581 			memcpy(internal_hash, hash_value,
582 			       sizeof(internal_hash));
583 			memset(hash_value, 0, sizeof(internal_hash));
584 		}
585 		if (image_size_to_hash == 0) {
586 			printf("Warning: Image_%d hash checksum is disabled, ",
587 			       image_num);
588 			printf("skip the image validation.\n");
589 			continue;
590 		}
591 		switch (hash_algorithm_id) {
592 		case SHA1_SUM_LEN:
593 			sha1_starts(&hash1_text);
594 			sha1_update(&hash1_text, buff, image_size_to_hash);
595 			sha1_finish(&hash1_text, hash_160_output);
596 			hash_output = hash_160_output;
597 			break;
598 		case SHA256_SUM_LEN:
599 			sha256_starts(&hash256_text);
600 			sha256_update(&hash256_text, buff, image_size_to_hash);
601 			sha256_finish(&hash256_text, hash_256_output);
602 			hash_output = hash_256_output;
603 			break;
604 		default:
605 			printf("Error: Unsupported hash_algorithm_id = %d\n",
606 			       hash_algorithm_id);
607 			return -ENOEXEC;
608 		}
609 		if (image_num == 0)
610 			memcpy(hash_value, internal_hash,
611 			       sizeof(internal_hash));
612 		if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) {
613 			printf("Error: Image_%d checksum is not correct\n",
614 			       image_num);
615 			return -ENOEXEC;
616 		}
617 	}
618 	printf("Image checksum...OK!\n");
619 
620 	return 0;
621 }
622 
623 #else /* Not ARMADA? */
624 static int check_image_header(void)
625 {
626 	printf("bubt cmd does not support this SoC device or family!\n");
627 	return -ENOEXEC;
628 }
629 #endif
630 
631 static int bubt_verify(size_t image_size)
632 {
633 	int err;
634 
635 	/* Check a correct image header exists */
636 	err = check_image_header();
637 	if (err) {
638 		printf("Error: Image header verification failed\n");
639 		return err;
640 	}
641 
642 	return 0;
643 }
644 
645 static int bubt_read_file(struct bubt_dev *src)
646 {
647 	size_t image_size;
648 
649 	if (!src->read) {
650 		printf("Error: Read not supported on device \"%s\"\n",
651 		       src->name);
652 		return 0;
653 	}
654 
655 	image_size = src->read(net_boot_file_name);
656 	if (image_size <= 0) {
657 		printf("Error: Failed to read file %s from %s\n",
658 		       net_boot_file_name, src->name);
659 		return 0;
660 	}
661 
662 	return image_size;
663 }
664 
665 static int bubt_is_dev_active(struct bubt_dev *dev)
666 {
667 	if (!dev->active) {
668 		printf("Device \"%s\" not supported by U-BOOT image\n",
669 		       dev->name);
670 		return 0;
671 	}
672 
673 	if (!dev->active()) {
674 		printf("Device \"%s\" is inactive\n", dev->name);
675 		return 0;
676 	}
677 
678 	return 1;
679 }
680 
681 struct bubt_dev *find_bubt_dev(char *dev_name)
682 {
683 	int dev;
684 
685 	for (dev = 0; dev < BUBT_MAX_DEV; dev++) {
686 		if (strcmp(bubt_devs[dev].name, dev_name) == 0)
687 			return &bubt_devs[dev];
688 	}
689 
690 	return 0;
691 }
692 
693 #define DEFAULT_BUBT_SRC "tftp"
694 
695 #ifndef DEFAULT_BUBT_DST
696 #ifdef CONFIG_MVEBU_SPI_BOOT
697 #define DEFAULT_BUBT_DST "spi"
698 #elif defined(CONFIG_MVEBU_NAND_BOOT)
699 #define DEFAULT_BUBT_DST "nand"
700 #elif defined(CONFIG_MVEBU_MMC_BOOT)
701 #define DEFAULT_BUBT_DST "mmc"
702 else
703 #define DEFAULT_BUBT_DST "error"
704 #endif
705 #endif /* DEFAULT_BUBT_DST */
706 
707 int do_bubt_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
708 {
709 	struct bubt_dev *src, *dst;
710 	size_t image_size;
711 	char src_dev_name[8];
712 	char dst_dev_name[8];
713 	char *name;
714 	int  err;
715 
716 	if (argc < 2)
717 		copy_filename(net_boot_file_name,
718 			      CONFIG_MVEBU_UBOOT_DFLT_NAME,
719 			      sizeof(net_boot_file_name));
720 	else
721 		copy_filename(net_boot_file_name, argv[1],
722 			      sizeof(net_boot_file_name));
723 
724 	if (argc >= 3) {
725 		strncpy(dst_dev_name, argv[2], 8);
726 	} else {
727 		name = DEFAULT_BUBT_DST;
728 		strncpy(dst_dev_name, name, 8);
729 	}
730 
731 	if (argc >= 4)
732 		strncpy(src_dev_name, argv[3], 8);
733 	else
734 		strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8);
735 
736 	/* Figure out the destination device */
737 	dst = find_bubt_dev(dst_dev_name);
738 	if (!dst) {
739 		printf("Error: Unknown destination \"%s\"\n", dst_dev_name);
740 		return -EINVAL;
741 	}
742 
743 	if (!bubt_is_dev_active(dst))
744 		return -ENODEV;
745 
746 	/* Figure out the source device */
747 	src = find_bubt_dev(src_dev_name);
748 	if (!src) {
749 		printf("Error: Unknown source \"%s\"\n", src_dev_name);
750 		return 1;
751 	}
752 
753 	if (!bubt_is_dev_active(src))
754 		return -ENODEV;
755 
756 	printf("Burning U-BOOT image \"%s\" from \"%s\" to \"%s\"\n",
757 	       net_boot_file_name, src->name, dst->name);
758 
759 	image_size = bubt_read_file(src);
760 	if (!image_size)
761 		return -EIO;
762 
763 	err = bubt_verify(image_size);
764 	if (err)
765 		return err;
766 
767 	err = bubt_write_file(dst, image_size);
768 	if (err)
769 		return err;
770 
771 	return 0;
772 }
773 
774 U_BOOT_CMD(
775 	bubt, 4, 0, do_bubt_cmd,
776 	"Burn a u-boot image to flash",
777 	"[file-name] [destination [source]]\n"
778 	"\t-file-name     The image file name to burn. Default = flash-image.bin\n"
779 	"\t-destination   Flash to burn to [spi, nand, mmc]. Default = active boot device\n"
780 	"\t-source        The source to load image from [tftp, usb, mmc]. Default = tftp\n"
781 	"Examples:\n"
782 	"\tbubt - Burn flash-image.bin from tftp to active boot device\n"
783 	"\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n"
784 	"\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n"
785 
786 );
787