mxsboot.c 15 KB

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