mxsboot.c 16 KB

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