bubt.c 18 KB

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