mxsboot.c 15 KB

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