mxsboot.c 15 KB

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