mxsboot.c 15 KB

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