mxsboot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. fcb->ecc_block_n_ecc_type = mx28_nand_get_ecc_strength(
  235. nand_writesize, nand_oobsize) >> 1;
  236. fcb->ecc_block_0_ecc_type = mx28_nand_get_ecc_strength(
  237. nand_writesize, nand_oobsize) >> 1;
  238. if (fcb->ecc_block_n_ecc_type == 0) {
  239. printf("MX28 NAND: Unsupported NAND geometry\n");
  240. goto err;
  241. }
  242. fcb->boot_patch = 0;
  243. fcb->patch_sectors = 0;
  244. fcb->badblock_marker_byte = mx28_nand_mark_byte_offset();
  245. fcb->badblock_marker_start_bit = mx28_nand_mark_bit_offset();
  246. fcb->bb_marker_physical_offset = nand_writesize;
  247. stride_size_bytes = STRIDE_PAGES * nand_writesize;
  248. bcb_size_bytes = stride_size_bytes * STRIDE_COUNT;
  249. bootstream_size_pages = (size + (nand_writesize - 1)) /
  250. nand_writesize;
  251. fw1_start_page = 2 * bcb_size_bytes / nand_writesize;
  252. fw2_start_page = (2 * bcb_size_bytes + MAX_BOOTSTREAM_SIZE) /
  253. nand_writesize;
  254. fcb->firmware1_starting_sector = fw1_start_page;
  255. fcb->firmware2_starting_sector = fw2_start_page;
  256. fcb->sectors_in_firmware1 = bootstream_size_pages;
  257. fcb->sectors_in_firmware2 = bootstream_size_pages;
  258. fcb->dbbt_search_area_start_address = STRIDE_PAGES * STRIDE_COUNT;
  259. return fcb;
  260. err:
  261. free(fcb);
  262. return NULL;
  263. }
  264. static struct mx28_nand_dbbt *mx28_nand_get_dbbt(void)
  265. {
  266. struct mx28_nand_dbbt *dbbt;
  267. dbbt = malloc(nand_writesize);
  268. if (!dbbt) {
  269. printf("MX28 NAND: Unable to allocate DBBT\n");
  270. return NULL;
  271. }
  272. memset(dbbt, 0, nand_writesize);
  273. dbbt->fingerprint = 0x54424244;
  274. dbbt->version = 0x1;
  275. return dbbt;
  276. }
  277. static inline uint8_t mx28_nand_parity_13_8(const uint8_t b)
  278. {
  279. uint32_t parity = 0, tmp;
  280. tmp = ((b >> 6) ^ (b >> 5) ^ (b >> 3) ^ (b >> 2)) & 1;
  281. parity |= tmp << 0;
  282. tmp = ((b >> 7) ^ (b >> 5) ^ (b >> 4) ^ (b >> 2) ^ (b >> 1)) & 1;
  283. parity |= tmp << 1;
  284. tmp = ((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 1) ^ (b >> 0)) & 1;
  285. parity |= tmp << 2;
  286. tmp = ((b >> 7) ^ (b >> 4) ^ (b >> 3) ^ (b >> 0)) & 1;
  287. parity |= tmp << 3;
  288. tmp = ((b >> 6) ^ (b >> 4) ^ (b >> 3) ^
  289. (b >> 2) ^ (b >> 1) ^ (b >> 0)) & 1;
  290. parity |= tmp << 4;
  291. return parity;
  292. }
  293. static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb *fcb)
  294. {
  295. uint8_t *block;
  296. uint8_t *ecc;
  297. int i;
  298. block = malloc(nand_writesize + nand_oobsize);
  299. if (!block) {
  300. printf("MX28 NAND: Unable to allocate FCB block\n");
  301. return NULL;
  302. }
  303. memset(block, 0, nand_writesize + nand_oobsize);
  304. /* Update the FCB checksum */
  305. fcb->checksum = mx28_nand_block_csum(((uint8_t *)fcb) + 4, 508);
  306. /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */
  307. memcpy(block + 12, fcb, sizeof(struct mx28_nand_fcb));
  308. /* ECC is at offset 12 + 512 */
  309. ecc = block + 12 + 512;
  310. /* Compute the ECC parity */
  311. for (i = 0; i < sizeof(struct mx28_nand_fcb); i++)
  312. ecc[i] = mx28_nand_parity_13_8(block[i + 12]);
  313. return block;
  314. }
  315. static int mx28_nand_write_fcb(struct mx28_nand_fcb *fcb, uint8_t *buf)
  316. {
  317. uint32_t offset;
  318. uint8_t *fcbblock;
  319. int ret = 0;
  320. int i;
  321. fcbblock = mx28_nand_fcb_block(fcb);
  322. if (!fcbblock)
  323. return -1;
  324. for (i = 0; i < STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
  325. offset = i * nand_writesize;
  326. memcpy(buf + offset, fcbblock, nand_writesize + nand_oobsize);
  327. /* Mark the NAND page is OK. */
  328. buf[offset + nand_writesize] = 0xff;
  329. }
  330. free(fcbblock);
  331. return ret;
  332. }
  333. static int mx28_nand_write_dbbt(struct mx28_nand_dbbt *dbbt, uint8_t *buf)
  334. {
  335. uint32_t offset;
  336. int i = STRIDE_PAGES * STRIDE_COUNT;
  337. for (; i < 2 * STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
  338. offset = i * nand_writesize;
  339. memcpy(buf + offset, dbbt, sizeof(struct mx28_nand_dbbt));
  340. }
  341. return 0;
  342. }
  343. static int mx28_nand_write_firmware(struct mx28_nand_fcb *fcb, int infd,
  344. uint8_t *buf)
  345. {
  346. int ret;
  347. off_t size;
  348. uint32_t offset1, offset2;
  349. size = lseek(infd, 0, SEEK_END);
  350. lseek(infd, 0, SEEK_SET);
  351. offset1 = fcb->firmware1_starting_sector * nand_writesize;
  352. offset2 = fcb->firmware2_starting_sector * nand_writesize;
  353. ret = read(infd, buf + offset1, size);
  354. if (ret != size)
  355. return -1;
  356. memcpy(buf + offset2, buf + offset1, size);
  357. return 0;
  358. }
  359. static void usage(void)
  360. {
  361. printf(
  362. "Usage: mxsboot [ops] <type> <infile> <outfile>\n"
  363. "Augment BootStream file with a proper header for i.MX28 boot\n"
  364. "\n"
  365. " <type> type of image:\n"
  366. " \"nand\" for NAND image\n"
  367. " \"sd\" for SD image\n"
  368. " <infile> input file, the u-boot.sb bootstream\n"
  369. " <outfile> output file, the bootable image\n"
  370. "\n");
  371. printf(
  372. "For NAND boot, these options are accepted:\n"
  373. " -w <size> NAND page size\n"
  374. " -o <size> NAND OOB size\n"
  375. " -e <size> NAND erase size\n"
  376. "\n"
  377. "For SD boot, these options are accepted:\n"
  378. " -p <sector> Sector where the SGTL partition starts\n"
  379. );
  380. }
  381. static int mx28_create_nand_image(int infd, int outfd)
  382. {
  383. struct mx28_nand_fcb *fcb;
  384. struct mx28_nand_dbbt *dbbt;
  385. int ret = -1;
  386. uint8_t *buf;
  387. int size;
  388. ssize_t wr_size;
  389. size = nand_writesize * 512 + 2 * MAX_BOOTSTREAM_SIZE;
  390. buf = malloc(size);
  391. if (!buf) {
  392. printf("Can not allocate output buffer of %d bytes\n", size);
  393. goto err0;
  394. }
  395. memset(buf, 0, size);
  396. fcb = mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE);
  397. if (!fcb) {
  398. printf("Unable to compile FCB\n");
  399. goto err1;
  400. }
  401. dbbt = mx28_nand_get_dbbt();
  402. if (!dbbt) {
  403. printf("Unable to compile DBBT\n");
  404. goto err2;
  405. }
  406. ret = mx28_nand_write_fcb(fcb, buf);
  407. if (ret) {
  408. printf("Unable to write FCB to buffer\n");
  409. goto err3;
  410. }
  411. ret = mx28_nand_write_dbbt(dbbt, buf);
  412. if (ret) {
  413. printf("Unable to write DBBT to buffer\n");
  414. goto err3;
  415. }
  416. ret = mx28_nand_write_firmware(fcb, infd, buf);
  417. if (ret) {
  418. printf("Unable to write firmware to buffer\n");
  419. goto err3;
  420. }
  421. wr_size = write(outfd, buf, size);
  422. if (wr_size != size) {
  423. ret = -1;
  424. goto err3;
  425. }
  426. ret = 0;
  427. err3:
  428. free(dbbt);
  429. err2:
  430. free(fcb);
  431. err1:
  432. free(buf);
  433. err0:
  434. return ret;
  435. }
  436. static int mx28_create_sd_image(int infd, int outfd)
  437. {
  438. int ret = -1;
  439. uint32_t *buf;
  440. int size;
  441. off_t fsize;
  442. ssize_t wr_size;
  443. struct mx28_sd_config_block *cb;
  444. fsize = lseek(infd, 0, SEEK_END);
  445. lseek(infd, 0, SEEK_SET);
  446. size = fsize + 4 * 512;
  447. buf = malloc(size);
  448. if (!buf) {
  449. printf("Can not allocate output buffer of %d bytes\n", size);
  450. goto err0;
  451. }
  452. ret = read(infd, (uint8_t *)buf + 4 * 512, fsize);
  453. if (ret != fsize) {
  454. ret = -1;
  455. goto err1;
  456. }
  457. cb = (struct mx28_sd_config_block *)buf;
  458. cb->signature = cpu_to_le32(0x00112233);
  459. cb->primary_boot_tag = cpu_to_le32(0x1);
  460. cb->secondary_boot_tag = cpu_to_le32(0x1);
  461. cb->num_copies = cpu_to_le32(1);
  462. cb->drv_info[0].chip_num = cpu_to_le32(0x0);
  463. cb->drv_info[0].drive_type = cpu_to_le32(0x0);
  464. cb->drv_info[0].tag = cpu_to_le32(0x1);
  465. cb->drv_info[0].first_sector_number = cpu_to_le32(sd_sector + 4);
  466. cb->drv_info[0].sector_count = cpu_to_le32((size - 4) / 512);
  467. wr_size = write(outfd, buf, size);
  468. if (wr_size != size) {
  469. ret = -1;
  470. goto err1;
  471. }
  472. ret = 0;
  473. err1:
  474. free(buf);
  475. err0:
  476. return ret;
  477. }
  478. static int parse_ops(int argc, char **argv)
  479. {
  480. int i;
  481. int tmp;
  482. char *end;
  483. enum param {
  484. PARAM_WRITE,
  485. PARAM_OOB,
  486. PARAM_ERASE,
  487. PARAM_PART,
  488. PARAM_SD,
  489. PARAM_NAND
  490. };
  491. int type;
  492. if (argc < 4)
  493. return -1;
  494. for (i = 1; i < argc; i++) {
  495. if (!strncmp(argv[i], "-w", 2))
  496. type = PARAM_WRITE;
  497. else if (!strncmp(argv[i], "-o", 2))
  498. type = PARAM_OOB;
  499. else if (!strncmp(argv[i], "-e", 2))
  500. type = PARAM_ERASE;
  501. else if (!strncmp(argv[i], "-p", 2))
  502. type = PARAM_PART;
  503. else /* SD/MMC */
  504. break;
  505. tmp = strtol(argv[++i], &end, 10);
  506. if (tmp % 2)
  507. return -1;
  508. if (tmp <= 0)
  509. return -1;
  510. if (type == PARAM_WRITE)
  511. nand_writesize = tmp;
  512. if (type == PARAM_OOB)
  513. nand_oobsize = tmp;
  514. if (type == PARAM_ERASE)
  515. nand_erasesize = tmp;
  516. if (type == PARAM_PART)
  517. sd_sector = tmp;
  518. }
  519. if (strcmp(argv[i], "sd") && strcmp(argv[i], "nand"))
  520. return -1;
  521. if (i + 3 != argc)
  522. return -1;
  523. return i;
  524. }
  525. int main(int argc, char **argv)
  526. {
  527. int infd, outfd;
  528. int ret = 0;
  529. int offset;
  530. offset = parse_ops(argc, argv);
  531. if (offset < 0) {
  532. usage();
  533. ret = 1;
  534. goto err1;
  535. }
  536. infd = open(argv[offset + 1], O_RDONLY);
  537. if (infd < 0) {
  538. printf("Input BootStream file can not be opened\n");
  539. ret = 2;
  540. goto err1;
  541. }
  542. outfd = open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY,
  543. S_IRUSR | S_IWUSR);
  544. if (outfd < 0) {
  545. printf("Output file can not be created\n");
  546. ret = 3;
  547. goto err2;
  548. }
  549. if (!strcmp(argv[offset], "sd"))
  550. ret = mx28_create_sd_image(infd, outfd);
  551. else if (!strcmp(argv[offset], "nand"))
  552. ret = mx28_create_nand_image(infd, outfd);
  553. close(outfd);
  554. err2:
  555. close(infd);
  556. err1:
  557. return ret;
  558. }