bubt.c 20 KB

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