bubt.c 17 KB

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