mxsboot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Freescale i.MX28 image generator
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <fcntl.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. #include "compiler.h"
  14. /*
  15. * Default BCB layout.
  16. *
  17. * TWEAK this if you have blown any OCOTP fuses.
  18. */
  19. #define STRIDE_PAGES 64
  20. #define STRIDE_COUNT 4
  21. /*
  22. * Layout for 256Mb big NAND with 2048b page size, 64b OOB size and
  23. * 128kb erase size.
  24. *
  25. * TWEAK this if you have different kind of NAND chip.
  26. */
  27. static uint32_t nand_writesize = 2048;
  28. static uint32_t nand_oobsize = 64;
  29. static uint32_t nand_erasesize = 128 * 1024;
  30. /*
  31. * Sector on which the SigmaTel boot partition (0x53) starts.
  32. */
  33. static uint32_t sd_sector = 2048;
  34. /*
  35. * Each of the U-Boot bootstreams is at maximum 1MB big.
  36. *
  37. * TWEAK this if, for some wild reason, you need to boot bigger image.
  38. */
  39. #define MAX_BOOTSTREAM_SIZE (1 * 1024 * 1024)
  40. /* i.MX28 NAND controller-specific constants. DO NOT TWEAK! */
  41. #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
  42. #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
  43. #define MXS_NAND_METADATA_SIZE 10
  44. #define MXS_NAND_COMMAND_BUFFER_SIZE 32
  45. struct mx28_nand_fcb {
  46. uint32_t checksum;
  47. uint32_t fingerprint;
  48. uint32_t version;
  49. struct {
  50. uint8_t data_setup;
  51. uint8_t data_hold;
  52. uint8_t address_setup;
  53. uint8_t dsample_time;
  54. uint8_t nand_timing_state;
  55. uint8_t rea;
  56. uint8_t rloh;
  57. uint8_t rhoh;
  58. } timing;
  59. uint32_t page_data_size;
  60. uint32_t total_page_size;
  61. uint32_t sectors_per_block;
  62. uint32_t number_of_nands; /* Ignored */
  63. uint32_t total_internal_die; /* Ignored */
  64. uint32_t cell_type; /* Ignored */
  65. uint32_t ecc_block_n_ecc_type;
  66. uint32_t ecc_block_0_size;
  67. uint32_t ecc_block_n_size;
  68. uint32_t ecc_block_0_ecc_type;
  69. uint32_t metadata_bytes;
  70. uint32_t num_ecc_blocks_per_page;
  71. uint32_t ecc_block_n_ecc_level_sdk; /* Ignored */
  72. uint32_t ecc_block_0_size_sdk; /* Ignored */
  73. uint32_t ecc_block_n_size_sdk; /* Ignored */
  74. uint32_t ecc_block_0_ecc_level_sdk; /* Ignored */
  75. uint32_t num_ecc_blocks_per_page_sdk; /* Ignored */
  76. uint32_t metadata_bytes_sdk; /* Ignored */
  77. uint32_t erase_threshold;
  78. uint32_t boot_patch;
  79. uint32_t patch_sectors;
  80. uint32_t firmware1_starting_sector;
  81. uint32_t firmware2_starting_sector;
  82. uint32_t sectors_in_firmware1;
  83. uint32_t sectors_in_firmware2;
  84. uint32_t dbbt_search_area_start_address;
  85. uint32_t badblock_marker_byte;
  86. uint32_t badblock_marker_start_bit;
  87. uint32_t bb_marker_physical_offset;
  88. };
  89. struct mx28_nand_dbbt {
  90. uint32_t checksum;
  91. uint32_t fingerprint;
  92. uint32_t version;
  93. uint32_t number_bb;
  94. uint32_t number_2k_pages_bb;
  95. };
  96. struct mx28_nand_bbt {
  97. uint32_t nand;
  98. uint32_t number_bb;
  99. uint32_t badblock[510];
  100. };
  101. struct mx28_sd_drive_info {
  102. uint32_t chip_num;
  103. uint32_t drive_type;
  104. uint32_t tag;
  105. uint32_t first_sector_number;
  106. uint32_t sector_count;
  107. };
  108. struct mx28_sd_config_block {
  109. uint32_t signature;
  110. uint32_t primary_boot_tag;
  111. uint32_t secondary_boot_tag;
  112. uint32_t num_copies;
  113. struct mx28_sd_drive_info drv_info[1];
  114. };
  115. static inline uint32_t mx28_nand_ecc_size_in_bits(uint32_t ecc_strength)
  116. {
  117. return ecc_strength * 13;
  118. }
  119. static inline uint32_t mx28_nand_get_ecc_strength(uint32_t page_data_size,
  120. uint32_t page_oob_size)
  121. {
  122. if (page_data_size == 2048)
  123. return 8;
  124. if (page_data_size == 4096) {
  125. if (page_oob_size == 128)
  126. return 8;
  127. if (page_oob_size == 218)
  128. return 16;
  129. }
  130. return 0;
  131. }
  132. static inline uint32_t mx28_nand_get_mark_offset(uint32_t page_data_size,
  133. uint32_t ecc_strength)
  134. {
  135. uint32_t chunk_data_size_in_bits;
  136. uint32_t chunk_ecc_size_in_bits;
  137. uint32_t chunk_total_size_in_bits;
  138. uint32_t block_mark_chunk_number;
  139. uint32_t block_mark_chunk_bit_offset;
  140. uint32_t block_mark_bit_offset;
  141. chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
  142. chunk_ecc_size_in_bits = mx28_nand_ecc_size_in_bits(ecc_strength);
  143. chunk_total_size_in_bits =
  144. chunk_data_size_in_bits + chunk_ecc_size_in_bits;
  145. /* Compute the bit offset of the block mark within the physical page. */
  146. block_mark_bit_offset = page_data_size * 8;
  147. /* Subtract the metadata bits. */
  148. block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
  149. /*
  150. * Compute the chunk number (starting at zero) in which the block mark
  151. * appears.
  152. */
  153. block_mark_chunk_number =
  154. block_mark_bit_offset / chunk_total_size_in_bits;
  155. /*
  156. * Compute the bit offset of the block mark within its chunk, and
  157. * validate it.
  158. */
  159. block_mark_chunk_bit_offset = block_mark_bit_offset -
  160. (block_mark_chunk_number * chunk_total_size_in_bits);
  161. if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
  162. return 1;
  163. /*
  164. * Now that we know the chunk number in which the block mark appears,
  165. * we can subtract all the ECC bits that appear before it.
  166. */
  167. block_mark_bit_offset -=
  168. block_mark_chunk_number * chunk_ecc_size_in_bits;
  169. return block_mark_bit_offset;
  170. }
  171. static inline uint32_t mx28_nand_mark_byte_offset(void)
  172. {
  173. uint32_t ecc_strength;
  174. ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
  175. return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) >> 3;
  176. }
  177. static inline uint32_t mx28_nand_mark_bit_offset(void)
  178. {
  179. uint32_t ecc_strength;
  180. ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
  181. return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) & 0x7;
  182. }
  183. static uint32_t mx28_nand_block_csum(uint8_t *block, uint32_t size)
  184. {
  185. uint32_t csum = 0;
  186. int i;
  187. for (i = 0; i < size; i++)
  188. csum += block[i];
  189. return csum ^ 0xffffffff;
  190. }
  191. static struct mx28_nand_fcb *mx28_nand_get_fcb(uint32_t size)
  192. {
  193. struct mx28_nand_fcb *fcb;
  194. uint32_t bcb_size_bytes;
  195. uint32_t stride_size_bytes;
  196. uint32_t bootstream_size_pages;
  197. uint32_t fw1_start_page;
  198. uint32_t fw2_start_page;
  199. fcb = malloc(nand_writesize);
  200. if (!fcb) {
  201. printf("MX28 NAND: Unable to allocate FCB\n");
  202. return NULL;
  203. }
  204. memset(fcb, 0, nand_writesize);
  205. fcb->fingerprint = 0x20424346;
  206. fcb->version = 0x01000000;
  207. /*
  208. * FIXME: These here are default values as found in kobs-ng. We should
  209. * probably retrieve the data from NAND or something.
  210. */
  211. fcb->timing.data_setup = 80;
  212. fcb->timing.data_hold = 60;
  213. fcb->timing.address_setup = 25;
  214. fcb->timing.dsample_time = 6;
  215. fcb->page_data_size = nand_writesize;
  216. fcb->total_page_size = nand_writesize + nand_oobsize;
  217. fcb->sectors_per_block = nand_erasesize / nand_writesize;
  218. fcb->num_ecc_blocks_per_page = (nand_writesize / 512) - 1;
  219. fcb->ecc_block_0_size = 512;
  220. fcb->ecc_block_n_size = 512;
  221. fcb->metadata_bytes = 10;
  222. if (nand_writesize == 2048) {
  223. fcb->ecc_block_n_ecc_type = 4;
  224. fcb->ecc_block_0_ecc_type = 4;
  225. } else if (nand_writesize == 4096) {
  226. if (nand_oobsize == 128) {
  227. fcb->ecc_block_n_ecc_type = 4;
  228. fcb->ecc_block_0_ecc_type = 4;
  229. } else if (nand_oobsize == 218) {
  230. fcb->ecc_block_n_ecc_type = 8;
  231. fcb->ecc_block_0_ecc_type = 8;
  232. }
  233. }
  234. if (fcb->ecc_block_n_ecc_type == 0) {
  235. printf("MX28 NAND: Unsupported NAND geometry\n");
  236. goto err;
  237. }
  238. fcb->boot_patch = 0;
  239. fcb->patch_sectors = 0;
  240. fcb->badblock_marker_byte = mx28_nand_mark_byte_offset();
  241. fcb->badblock_marker_start_bit = mx28_nand_mark_bit_offset();
  242. fcb->bb_marker_physical_offset = nand_writesize;
  243. stride_size_bytes = STRIDE_PAGES * nand_writesize;
  244. bcb_size_bytes = stride_size_bytes * STRIDE_COUNT;
  245. bootstream_size_pages = (size + (nand_writesize - 1)) /
  246. nand_writesize;
  247. fw1_start_page = 2 * bcb_size_bytes / nand_writesize;
  248. fw2_start_page = (2 * bcb_size_bytes + MAX_BOOTSTREAM_SIZE) /
  249. nand_writesize;
  250. fcb->firmware1_starting_sector = fw1_start_page;
  251. fcb->firmware2_starting_sector = fw2_start_page;
  252. fcb->sectors_in_firmware1 = bootstream_size_pages;
  253. fcb->sectors_in_firmware2 = bootstream_size_pages;
  254. fcb->dbbt_search_area_start_address = STRIDE_PAGES * STRIDE_COUNT;
  255. return fcb;
  256. err:
  257. free(fcb);
  258. return NULL;
  259. }
  260. static struct mx28_nand_dbbt *mx28_nand_get_dbbt(void)
  261. {
  262. struct mx28_nand_dbbt *dbbt;
  263. dbbt = malloc(nand_writesize);
  264. if (!dbbt) {
  265. printf("MX28 NAND: Unable to allocate DBBT\n");
  266. return NULL;
  267. }
  268. memset(dbbt, 0, nand_writesize);
  269. dbbt->fingerprint = 0x54424244;
  270. dbbt->version = 0x1;
  271. return dbbt;
  272. }
  273. static inline uint8_t mx28_nand_parity_13_8(const uint8_t b)
  274. {
  275. uint32_t parity = 0, tmp;
  276. tmp = ((b >> 6) ^ (b >> 5) ^ (b >> 3) ^ (b >> 2)) & 1;
  277. parity |= tmp << 0;
  278. tmp = ((b >> 7) ^ (b >> 5) ^ (b >> 4) ^ (b >> 2) ^ (b >> 1)) & 1;
  279. parity |= tmp << 1;
  280. tmp = ((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 1) ^ (b >> 0)) & 1;
  281. parity |= tmp << 2;
  282. tmp = ((b >> 7) ^ (b >> 4) ^ (b >> 3) ^ (b >> 0)) & 1;
  283. parity |= tmp << 3;
  284. tmp = ((b >> 6) ^ (b >> 4) ^ (b >> 3) ^
  285. (b >> 2) ^ (b >> 1) ^ (b >> 0)) & 1;
  286. parity |= tmp << 4;
  287. return parity;
  288. }
  289. static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb *fcb)
  290. {
  291. uint8_t *block;
  292. uint8_t *ecc;
  293. int i;
  294. block = malloc(nand_writesize + nand_oobsize);
  295. if (!block) {
  296. printf("MX28 NAND: Unable to allocate FCB block\n");
  297. return NULL;
  298. }
  299. memset(block, 0, nand_writesize + nand_oobsize);
  300. /* Update the FCB checksum */
  301. fcb->checksum = mx28_nand_block_csum(((uint8_t *)fcb) + 4, 508);
  302. /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */
  303. memcpy(block + 12, fcb, sizeof(struct mx28_nand_fcb));
  304. /* ECC is at offset 12 + 512 */
  305. ecc = block + 12 + 512;
  306. /* Compute the ECC parity */
  307. for (i = 0; i < sizeof(struct mx28_nand_fcb); i++)
  308. ecc[i] = mx28_nand_parity_13_8(block[i + 12]);
  309. return block;
  310. }
  311. static int mx28_nand_write_fcb(struct mx28_nand_fcb *fcb, char *buf)
  312. {
  313. uint32_t offset;
  314. uint8_t *fcbblock;
  315. int ret = 0;
  316. int i;
  317. fcbblock = mx28_nand_fcb_block(fcb);
  318. if (!fcbblock)
  319. return -1;
  320. for (i = 0; i < STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
  321. offset = i * nand_writesize;
  322. memcpy(buf + offset, fcbblock, nand_writesize + nand_oobsize);
  323. }
  324. free(fcbblock);
  325. return ret;
  326. }
  327. static int mx28_nand_write_dbbt(struct mx28_nand_dbbt *dbbt, char *buf)
  328. {
  329. uint32_t offset;
  330. int i = STRIDE_PAGES * STRIDE_COUNT;
  331. for (; i < 2 * STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
  332. offset = i * nand_writesize;
  333. memcpy(buf + offset, dbbt, sizeof(struct mx28_nand_dbbt));
  334. }
  335. return 0;
  336. }
  337. static int mx28_nand_write_firmware(struct mx28_nand_fcb *fcb, int infd,
  338. char *buf)
  339. {
  340. int ret;
  341. off_t size;
  342. uint32_t offset1, offset2;
  343. size = lseek(infd, 0, SEEK_END);
  344. lseek(infd, 0, SEEK_SET);
  345. offset1 = fcb->firmware1_starting_sector * nand_writesize;
  346. offset2 = fcb->firmware2_starting_sector * nand_writesize;
  347. ret = read(infd, buf + offset1, size);
  348. if (ret != size)
  349. return -1;
  350. memcpy(buf + offset2, buf + offset1, size);
  351. return 0;
  352. }
  353. static void usage(void)
  354. {
  355. printf(
  356. "Usage: mxsboot [ops] <type> <infile> <outfile>\n"
  357. "Augment BootStream file with a proper header for i.MX28 boot\n"
  358. "\n"
  359. " <type> type of image:\n"
  360. " \"nand\" for NAND image\n"
  361. " \"sd\" for SD image\n"
  362. " <infile> input file, the u-boot.sb bootstream\n"
  363. " <outfile> output file, the bootable image\n"
  364. "\n");
  365. printf(
  366. "For NAND boot, these options are accepted:\n"
  367. " -w <size> NAND page size\n"
  368. " -o <size> NAND OOB size\n"
  369. " -e <size> NAND erase size\n"
  370. "\n"
  371. "For SD boot, these options are accepted:\n"
  372. " -p <sector> Sector where the SGTL partition starts\n"
  373. );
  374. }
  375. static int mx28_create_nand_image(int infd, int outfd)
  376. {
  377. struct mx28_nand_fcb *fcb;
  378. struct mx28_nand_dbbt *dbbt;
  379. int ret = -1;
  380. char *buf;
  381. int size;
  382. ssize_t wr_size;
  383. size = nand_writesize * 512 + 2 * MAX_BOOTSTREAM_SIZE;
  384. buf = malloc(size);
  385. if (!buf) {
  386. printf("Can not allocate output buffer of %d bytes\n", size);
  387. goto err0;
  388. }
  389. memset(buf, 0, size);
  390. fcb = mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE);
  391. if (!fcb) {
  392. printf("Unable to compile FCB\n");
  393. goto err1;
  394. }
  395. dbbt = mx28_nand_get_dbbt();
  396. if (!dbbt) {
  397. printf("Unable to compile DBBT\n");
  398. goto err2;
  399. }
  400. ret = mx28_nand_write_fcb(fcb, buf);
  401. if (ret) {
  402. printf("Unable to write FCB to buffer\n");
  403. goto err3;
  404. }
  405. ret = mx28_nand_write_dbbt(dbbt, buf);
  406. if (ret) {
  407. printf("Unable to write DBBT to buffer\n");
  408. goto err3;
  409. }
  410. ret = mx28_nand_write_firmware(fcb, infd, buf);
  411. if (ret) {
  412. printf("Unable to write firmware to buffer\n");
  413. goto err3;
  414. }
  415. wr_size = write(outfd, buf, size);
  416. if (wr_size != size) {
  417. ret = -1;
  418. goto err3;
  419. }
  420. ret = 0;
  421. err3:
  422. free(dbbt);
  423. err2:
  424. free(fcb);
  425. err1:
  426. free(buf);
  427. err0:
  428. return ret;
  429. }
  430. static int mx28_create_sd_image(int infd, int outfd)
  431. {
  432. int ret = -1;
  433. uint32_t *buf;
  434. int size;
  435. off_t fsize;
  436. ssize_t wr_size;
  437. struct mx28_sd_config_block *cb;
  438. fsize = lseek(infd, 0, SEEK_END);
  439. lseek(infd, 0, SEEK_SET);
  440. size = fsize + 4 * 512;
  441. buf = malloc(size);
  442. if (!buf) {
  443. printf("Can not allocate output buffer of %d bytes\n", size);
  444. goto err0;
  445. }
  446. ret = read(infd, (uint8_t *)buf + 4 * 512, fsize);
  447. if (ret != fsize) {
  448. ret = -1;
  449. goto err1;
  450. }
  451. cb = (struct mx28_sd_config_block *)buf;
  452. cb->signature = 0x00112233;
  453. cb->primary_boot_tag = 0x1;
  454. cb->secondary_boot_tag = 0x1;
  455. cb->num_copies = 1;
  456. cb->drv_info[0].chip_num = 0x0;
  457. cb->drv_info[0].drive_type = 0x0;
  458. cb->drv_info[0].tag = 0x1;
  459. cb->drv_info[0].first_sector_number = sd_sector + 4;
  460. cb->drv_info[0].sector_count = (size - 4) / 512;
  461. wr_size = write(outfd, buf, size);
  462. if (wr_size != size) {
  463. ret = -1;
  464. goto err1;
  465. }
  466. ret = 0;
  467. err1:
  468. free(buf);
  469. err0:
  470. return ret;
  471. }
  472. static int parse_ops(int argc, char **argv)
  473. {
  474. int i;
  475. int tmp;
  476. char *end;
  477. enum param {
  478. PARAM_WRITE,
  479. PARAM_OOB,
  480. PARAM_ERASE,
  481. PARAM_PART,
  482. PARAM_SD,
  483. PARAM_NAND
  484. };
  485. int type;
  486. if (argc < 4)
  487. return -1;
  488. for (i = 1; i < argc; i++) {
  489. if (!strncmp(argv[i], "-w", 2))
  490. type = PARAM_WRITE;
  491. else if (!strncmp(argv[i], "-o", 2))
  492. type = PARAM_OOB;
  493. else if (!strncmp(argv[i], "-e", 2))
  494. type = PARAM_ERASE;
  495. else if (!strncmp(argv[i], "-p", 2))
  496. type = PARAM_PART;
  497. else /* SD/MMC */
  498. break;
  499. tmp = strtol(argv[++i], &end, 10);
  500. if (tmp % 2)
  501. return -1;
  502. if (tmp <= 0)
  503. return -1;
  504. if (type == PARAM_WRITE)
  505. nand_writesize = tmp;
  506. if (type == PARAM_OOB)
  507. nand_oobsize = tmp;
  508. if (type == PARAM_ERASE)
  509. nand_erasesize = tmp;
  510. if (type == PARAM_PART)
  511. sd_sector = tmp;
  512. }
  513. if (strcmp(argv[i], "sd") && strcmp(argv[i], "nand"))
  514. return -1;
  515. if (i + 3 != argc)
  516. return -1;
  517. return i;
  518. }
  519. int main(int argc, char **argv)
  520. {
  521. int infd, outfd;
  522. int ret = 0;
  523. int offset;
  524. offset = parse_ops(argc, argv);
  525. if (offset < 0) {
  526. usage();
  527. ret = 1;
  528. goto err1;
  529. }
  530. infd = open(argv[offset + 1], O_RDONLY);
  531. if (infd < 0) {
  532. printf("Input BootStream file can not be opened\n");
  533. ret = 2;
  534. goto err1;
  535. }
  536. outfd = open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY,
  537. S_IRUSR | S_IWUSR);
  538. if (outfd < 0) {
  539. printf("Output file can not be created\n");
  540. ret = 3;
  541. goto err2;
  542. }
  543. if (!strcmp(argv[offset], "sd"))
  544. ret = mx28_create_sd_image(infd, outfd);
  545. else if (!strcmp(argv[offset], "nand"))
  546. ret = mx28_create_nand_image(infd, outfd);
  547. close(outfd);
  548. err2:
  549. close(infd);
  550. err1:
  551. return ret;
  552. }