bubt.c 21 KB

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