rkcommon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. *
  6. * (C) 2017 Theobroma Systems Design und Consulting GmbH
  7. *
  8. * Helper functions for Rockchip images
  9. */
  10. #include "imagetool.h"
  11. #include <image.h>
  12. #include <u-boot/sha256.h>
  13. #include <rc4.h>
  14. #include "mkimage.h"
  15. #include "rkcommon.h"
  16. enum {
  17. RK_MAGIC = 0x0ff0aa55,
  18. RK_MAGIC_V2 = 0x534E4B52,
  19. };
  20. enum {
  21. RK_HEADER_V1 = 1,
  22. RK_HEADER_V2 = 2,
  23. };
  24. enum hash_type {
  25. HASH_NONE = 0,
  26. HASH_SHA256 = 1,
  27. HASH_SHA512 = 2,
  28. };
  29. /**
  30. * struct image_entry
  31. *
  32. * @size_and_off: [31:16]image size;[15:0]image offset
  33. * @address: default as 0xFFFFFFFF
  34. * @flag: no use
  35. * @counter: no use
  36. * @hash: hash of image
  37. *
  38. */
  39. struct image_entry {
  40. uint32_t size_and_off;
  41. uint32_t address;
  42. uint32_t flag;
  43. uint32_t counter;
  44. uint8_t reserved[8];
  45. uint8_t hash[64];
  46. };
  47. /**
  48. * struct header0_info_v2 - v2 header block for rockchip BootRom
  49. *
  50. * This is stored at SD card block 64 (where each block is 512 bytes)
  51. *
  52. * @magic: Magic (must be RK_MAGIC_V2)
  53. * @size_and_nimage: [31:16]number of images;[15:0]
  54. * offset to hash field of header(unit as 4Byte)
  55. * @boot_flag: [3:0]hash type(0:none,1:sha256,2:sha512)
  56. * @signature: hash or signature for header info
  57. *
  58. */
  59. struct header0_info_v2 {
  60. uint32_t magic;
  61. uint8_t reserved[4];
  62. uint32_t size_and_nimage;
  63. uint32_t boot_flag;
  64. uint8_t reserved1[104];
  65. struct image_entry images[4];
  66. uint8_t reserved2[1064];
  67. uint8_t hash[512];
  68. };
  69. /**
  70. * struct header0_info - header block for boot ROM
  71. *
  72. * This is stored at SD card block 64 (where each block is 512 bytes, or at
  73. * the start of SPI flash. It is encoded with RC4.
  74. *
  75. * @magic: Magic (must be RK_MAGIC)
  76. * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
  77. * @init_offset: Offset in blocks of the SPL code from this header
  78. * block. E.g. 4 means 2KB after the start of this header.
  79. * Other fields are not used by U-Boot
  80. */
  81. struct header0_info {
  82. uint32_t magic;
  83. uint8_t reserved[4];
  84. uint32_t disable_rc4;
  85. uint16_t init_offset;
  86. uint8_t reserved1[492];
  87. uint16_t init_size;
  88. uint16_t init_boot_size;
  89. uint8_t reserved2[2];
  90. };
  91. /**
  92. * struct header1_info
  93. */
  94. struct header1_info {
  95. uint32_t magic;
  96. };
  97. /**
  98. * struct spl_info - spl info for each chip
  99. *
  100. * @imagename: Image name(passed by "mkimage -n")
  101. * @spl_hdr: Boot ROM requires a 4-bytes spl header
  102. * @spl_size: Spl size(include extra 4-bytes spl header)
  103. * @spl_rc4: RC4 encode the SPL binary (same key as header)
  104. * @header_ver: header block version
  105. */
  106. struct spl_info {
  107. const char *imagename;
  108. const char *spl_hdr;
  109. const uint32_t spl_size;
  110. const bool spl_rc4;
  111. const uint32_t header_ver;
  112. };
  113. static struct spl_info spl_infos[] = {
  114. { "px30", "RK33", 0x2800, false, RK_HEADER_V1 },
  115. { "rk3036", "RK30", 0x1000, false, RK_HEADER_V1 },
  116. { "rk3066", "RK30", 0x8000 - 0x800, true, RK_HEADER_V1 },
  117. { "rk3128", "RK31", 0x1800, false, RK_HEADER_V1 },
  118. { "rk3188", "RK31", 0x8000 - 0x800, true, RK_HEADER_V1 },
  119. { "rk322x", "RK32", 0x8000 - 0x1000, false, RK_HEADER_V1 },
  120. { "rk3288", "RK32", 0x8000, false, RK_HEADER_V1 },
  121. { "rk3308", "RK33", 0x40000 - 0x1000, false, RK_HEADER_V1 },
  122. { "rk3328", "RK32", 0x8000 - 0x800, false, RK_HEADER_V1 },
  123. { "rk3368", "RK33", 0x8000 - 0x1000, false, RK_HEADER_V1 },
  124. { "rk3399", "RK33", 0x30000 - 0x2000, false, RK_HEADER_V1 },
  125. { "rv1108", "RK11", 0x1800, false, RK_HEADER_V1 },
  126. { "rv1126", "110B", 0x10000 - 0x1000, false, RK_HEADER_V1 },
  127. { "rk3568", "RK35", 0x10000 - 0x1000, false, RK_HEADER_V2 },
  128. { "rk3588", "RK35", 0x100000 - 0x1000, false, RK_HEADER_V2 },
  129. };
  130. /**
  131. * struct spl_params - spl params parsed in check_params()
  132. *
  133. * @init_file: Init data file path
  134. * @init_size: Aligned size of init data in bytes
  135. * @boot_file: Boot data file path
  136. * @boot_size: Aligned size of boot data in bytes
  137. */
  138. struct spl_params {
  139. char *init_file;
  140. uint32_t init_size;
  141. char *boot_file;
  142. uint32_t boot_size;
  143. };
  144. static struct spl_params spl_params = { 0 };
  145. static const unsigned char rc4_key[16] = {
  146. 124, 78, 3, 4, 85, 5, 9, 7,
  147. 45, 44, 123, 56, 23, 13, 23, 17
  148. };
  149. static struct spl_info *rkcommon_get_spl_info(char *imagename)
  150. {
  151. int i;
  152. if (!imagename)
  153. return NULL;
  154. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  155. if (!strncmp(imagename, spl_infos[i].imagename, 6))
  156. return spl_infos + i;
  157. return NULL;
  158. }
  159. static int rkcommon_get_aligned_size(struct image_tool_params *params,
  160. const char *fname)
  161. {
  162. int size;
  163. size = imagetool_get_filesize(params, fname);
  164. if (size < 0)
  165. return -1;
  166. /*
  167. * Pad to a 2KB alignment, as required for init/boot size by the ROM
  168. * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html)
  169. */
  170. return ROUND(size, RK_SIZE_ALIGN);
  171. }
  172. int rkcommon_check_params(struct image_tool_params *params)
  173. {
  174. int i, size;
  175. /*
  176. * If this is a operation (list or extract), the don't require
  177. * imagename to be set.
  178. */
  179. if (params->lflag || params->iflag)
  180. return EXIT_SUCCESS;
  181. if (!rkcommon_get_spl_info(params->imagename))
  182. goto err_spl_info;
  183. spl_params.init_file = params->datafile;
  184. spl_params.boot_file = strchr(spl_params.init_file, ':');
  185. if (spl_params.boot_file) {
  186. *spl_params.boot_file = '\0';
  187. spl_params.boot_file += 1;
  188. }
  189. size = rkcommon_get_aligned_size(params, spl_params.init_file);
  190. if (size < 0)
  191. return EXIT_FAILURE;
  192. spl_params.init_size = size;
  193. /* Boot file is optional, and only for back-to-bootrom functionality. */
  194. if (spl_params.boot_file) {
  195. size = rkcommon_get_aligned_size(params, spl_params.boot_file);
  196. if (size < 0)
  197. return EXIT_FAILURE;
  198. spl_params.boot_size = size;
  199. }
  200. if (spl_params.init_size > rkcommon_get_spl_size(params)) {
  201. fprintf(stderr,
  202. "Error: SPL image is too large (size %#x than %#x)\n",
  203. spl_params.init_size, rkcommon_get_spl_size(params));
  204. return EXIT_FAILURE;
  205. }
  206. return EXIT_SUCCESS;
  207. err_spl_info:
  208. fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
  209. params->imagename ? params->imagename : "NULL");
  210. fprintf(stderr, "Available imagename:");
  211. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  212. fprintf(stderr, "\t%s", spl_infos[i].imagename);
  213. fprintf(stderr, "\n");
  214. return EXIT_FAILURE;
  215. }
  216. const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
  217. {
  218. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  219. /*
  220. * info would not be NULL, because of we checked params before.
  221. */
  222. return info->spl_hdr;
  223. }
  224. int rkcommon_get_spl_size(struct image_tool_params *params)
  225. {
  226. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  227. /*
  228. * info would not be NULL, because of we checked params before.
  229. */
  230. return info->spl_size;
  231. }
  232. bool rkcommon_need_rc4_spl(struct image_tool_params *params)
  233. {
  234. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  235. /*
  236. * info would not be NULL, because of we checked params before.
  237. */
  238. return info->spl_rc4;
  239. }
  240. bool rkcommon_is_header_v2(struct image_tool_params *params)
  241. {
  242. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  243. return (info->header_ver == RK_HEADER_V2);
  244. }
  245. static void do_sha256_hash(uint8_t *buf, uint32_t size, uint8_t *out)
  246. {
  247. sha256_context ctx;
  248. sha256_starts(&ctx);
  249. sha256_update(&ctx, buf, size);
  250. sha256_finish(&ctx, out);
  251. }
  252. static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
  253. {
  254. struct header0_info *hdr = buf;
  255. uint32_t init_boot_size;
  256. memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
  257. hdr->magic = cpu_to_le32(RK_MAGIC);
  258. hdr->disable_rc4 = cpu_to_le32(!rkcommon_need_rc4_spl(params));
  259. hdr->init_offset = cpu_to_le16(RK_INIT_OFFSET);
  260. hdr->init_size = cpu_to_le16(spl_params.init_size / RK_BLK_SIZE);
  261. /*
  262. * init_boot_size needs to be set, as it is read by the BootROM
  263. * to determine the size of the next-stage bootloader (e.g. U-Boot
  264. * proper), when used with the back-to-bootrom functionality.
  265. *
  266. * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
  267. * for a more detailed explanation by Andy Yan
  268. */
  269. if (spl_params.boot_file)
  270. init_boot_size = spl_params.init_size + spl_params.boot_size;
  271. else
  272. init_boot_size = spl_params.init_size + RK_MAX_BOOT_SIZE;
  273. hdr->init_boot_size = cpu_to_le16(init_boot_size / RK_BLK_SIZE);
  274. rc4_encode(buf, RK_BLK_SIZE, rc4_key);
  275. }
  276. static void rkcommon_set_header0_v2(void *buf, struct image_tool_params *params)
  277. {
  278. struct header0_info_v2 *hdr = buf;
  279. uint32_t sector_offset, image_sector_count;
  280. uint32_t image_size_array[2];
  281. uint8_t *image_ptr = NULL;
  282. int i;
  283. printf("Image Type: Rockchip %s boot image\n",
  284. rkcommon_get_spl_hdr(params));
  285. memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
  286. hdr->magic = cpu_to_le32(RK_MAGIC_V2);
  287. hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
  288. hdr->boot_flag = cpu_to_le32(HASH_SHA256);
  289. sector_offset = 4;
  290. image_size_array[0] = spl_params.init_size;
  291. image_size_array[1] = spl_params.boot_size;
  292. for (i = 0; i < 2; i++) {
  293. image_sector_count = image_size_array[i] / RK_BLK_SIZE;
  294. hdr->images[i].size_and_off = cpu_to_le32((image_sector_count
  295. << 16) + sector_offset);
  296. hdr->images[i].address = 0xFFFFFFFF;
  297. hdr->images[i].counter = cpu_to_le32(i + 1);
  298. image_ptr = buf + sector_offset * RK_BLK_SIZE;
  299. do_sha256_hash(image_ptr, image_size_array[i],
  300. hdr->images[i].hash);
  301. sector_offset = sector_offset + image_sector_count;
  302. }
  303. do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
  304. }
  305. void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
  306. struct image_tool_params *params)
  307. {
  308. struct header1_info *hdr = buf + RK_SPL_HDR_START;
  309. if (rkcommon_is_header_v2(params)) {
  310. rkcommon_set_header0_v2(buf, params);
  311. } else {
  312. rkcommon_set_header0(buf, params);
  313. /* Set up the SPL name (i.e. copy spl_hdr over) */
  314. if (memcmp(&hdr->magic, "RSAK", 4))
  315. memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
  316. if (rkcommon_need_rc4_spl(params))
  317. rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
  318. spl_params.init_size);
  319. if (spl_params.boot_file) {
  320. if (rkcommon_need_rc4_spl(params))
  321. rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
  322. spl_params.init_size,
  323. spl_params.boot_size);
  324. }
  325. }
  326. }
  327. static inline unsigned int rkcommon_offset_to_spi(unsigned int offset)
  328. {
  329. /*
  330. * While SD/MMC images use a flat addressing, SPI images are padded
  331. * to use the first 2K of every 4K sector only.
  332. */
  333. return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
  334. }
  335. static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
  336. struct spl_info **spl_info)
  337. {
  338. unsigned int hdr1_offset;
  339. struct header1_info *hdr1_sdmmc, *hdr1_spi;
  340. int i;
  341. if (spl_info)
  342. *spl_info = NULL;
  343. /*
  344. * The first header (hdr0) is always RC4 encoded, so try to decrypt
  345. * with the well-known key.
  346. */
  347. memcpy((void *)header0, buf, sizeof(struct header0_info));
  348. rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
  349. if (le32_to_cpu(header0->magic) != RK_MAGIC)
  350. return -EPROTO;
  351. /* We don't support RC4 encoded image payloads here, yet... */
  352. if (le32_to_cpu(header0->disable_rc4) == 0)
  353. return -ENOSYS;
  354. hdr1_offset = le16_to_cpu(header0->init_offset) * RK_BLK_SIZE;
  355. hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
  356. hdr1_spi = (struct header1_info *)(buf +
  357. rkcommon_offset_to_spi(hdr1_offset));
  358. for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
  359. if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr,
  360. RK_SPL_HDR_SIZE)) {
  361. if (spl_info)
  362. *spl_info = &spl_infos[i];
  363. return IH_TYPE_RKSD;
  364. } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr,
  365. RK_SPL_HDR_SIZE)) {
  366. if (spl_info)
  367. *spl_info = &spl_infos[i];
  368. return IH_TYPE_RKSPI;
  369. }
  370. }
  371. return -1;
  372. }
  373. static int rkcommon_parse_header_v2(const void *buf, struct header0_info_v2 *header)
  374. {
  375. memcpy((void *)header, buf, sizeof(struct header0_info_v2));
  376. if (le32_to_cpu(header->magic) != RK_MAGIC_V2)
  377. return -EPROTO;
  378. return 0;
  379. }
  380. int rkcommon_verify_header(unsigned char *buf, int size,
  381. struct image_tool_params *params)
  382. {
  383. struct header0_info header0;
  384. struct spl_info *img_spl_info, *spl_info;
  385. int ret;
  386. /* spl_hdr is abandon on header_v2 */
  387. if ((*(uint32_t *)buf) == RK_MAGIC_V2)
  388. return 0;
  389. ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
  390. /* If this is the (unimplemented) RC4 case, then rewrite the result */
  391. if (ret == -ENOSYS)
  392. return 0;
  393. if (ret < 0)
  394. return ret;
  395. /*
  396. * If no 'imagename' is specified via the commandline (e.g. if this is
  397. * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
  398. */
  399. if (params->imagename == NULL)
  400. return 0;
  401. /* Match the 'imagename' against the 'spl_hdr' found */
  402. spl_info = rkcommon_get_spl_info(params->imagename);
  403. if (spl_info && img_spl_info)
  404. return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
  405. return -ENOENT;
  406. }
  407. void rkcommon_print_header(const void *buf, struct image_tool_params *params)
  408. {
  409. struct header0_info header0;
  410. struct header0_info_v2 header0_v2;
  411. struct spl_info *spl_info;
  412. uint8_t image_type;
  413. int ret, boot_size, init_size;
  414. if ((*(uint32_t *)buf) == RK_MAGIC_V2) {
  415. ret = rkcommon_parse_header_v2(buf, &header0_v2);
  416. if (ret < 0) {
  417. fprintf(stderr, "Error: image verification failed\n");
  418. return;
  419. }
  420. init_size = header0_v2.images[0].size_and_off >> 16;
  421. init_size = init_size * RK_BLK_SIZE;
  422. boot_size = header0_v2.images[1].size_and_off >> 16;
  423. boot_size = boot_size * RK_BLK_SIZE;
  424. } else {
  425. ret = rkcommon_parse_header(buf, &header0, &spl_info);
  426. /* If this is the (unimplemented) RC4 case, then fail silently */
  427. if (ret == -ENOSYS)
  428. return;
  429. if (ret < 0) {
  430. fprintf(stderr, "Error: image verification failed\n");
  431. return;
  432. }
  433. image_type = ret;
  434. init_size = header0.init_size * RK_BLK_SIZE;
  435. boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
  436. printf("Image Type: Rockchip %s (%s) boot image\n",
  437. spl_info->spl_hdr,
  438. (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
  439. }
  440. printf("Init Data Size: %d bytes\n", init_size);
  441. if (boot_size != RK_MAX_BOOT_SIZE)
  442. printf("Boot Data Size: %d bytes\n", boot_size);
  443. }
  444. void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
  445. {
  446. unsigned int remaining = size;
  447. while (remaining > 0) {
  448. int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
  449. rc4_encode(buf + offset, step, rc4_key);
  450. offset += RK_BLK_SIZE;
  451. remaining -= step;
  452. }
  453. }
  454. int rkcommon_vrec_header(struct image_tool_params *params,
  455. struct image_type_params *tparams)
  456. {
  457. /*
  458. * The SPL image looks as follows:
  459. *
  460. * 0x0 header0 (see rkcommon.c)
  461. * 0x800 spl_name ('RK30', ..., 'RK33')
  462. * (start of the payload for AArch64 payloads: we expect the
  463. * first 4 bytes to be available for overwriting with our
  464. * spl_name)
  465. * 0x804 first instruction to be executed
  466. * (start of the image/payload for 32bit payloads)
  467. *
  468. * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
  469. * required for its sections (so the image we receive needs to
  470. * have the first 4 bytes reserved for the spl_name). Reserving
  471. * these 4 bytes is done using the BOOT0_HOOK infrastructure.
  472. *
  473. * The header is always at 0x800 (as we now use a payload
  474. * prepadded using the boot0 hook for all targets): the first
  475. * 4 bytes of these images can safely be overwritten using the
  476. * boot magic.
  477. */
  478. tparams->header_size = RK_SPL_HDR_START;
  479. /* Allocate, clear and install the header */
  480. tparams->hdr = malloc(tparams->header_size);
  481. if (!tparams->hdr) {
  482. fprintf(stderr, "%s: Can't alloc header: %s\n",
  483. params->cmdname, strerror(errno));
  484. exit(EXIT_FAILURE);
  485. }
  486. memset(tparams->hdr, 0, tparams->header_size);
  487. /*
  488. * We need to store the original file-size (i.e. before padding), as
  489. * imagetool does not set this during its adjustment of file_size.
  490. */
  491. params->orig_file_size = tparams->header_size +
  492. spl_params.init_size + spl_params.boot_size;
  493. params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
  494. /* Ignoring pad len, since we are using our own copy_image() */
  495. return 0;
  496. }
  497. static int pad_file(struct image_tool_params *params, int ifd, int pad)
  498. {
  499. uint8_t zeros[4096];
  500. memset(zeros, 0, sizeof(zeros));
  501. while (pad > 0) {
  502. int todo = sizeof(zeros);
  503. if (todo > pad)
  504. todo = pad;
  505. if (write(ifd, (char *)&zeros, todo) != todo) {
  506. fprintf(stderr, "%s: Write error on %s: %s\n",
  507. params->cmdname, params->imagefile,
  508. strerror(errno));
  509. return -1;
  510. }
  511. pad -= todo;
  512. }
  513. return 0;
  514. }
  515. static int copy_file(struct image_tool_params *params, int ifd,
  516. const char *file, int padded_size)
  517. {
  518. int dfd;
  519. struct stat sbuf;
  520. unsigned char *ptr;
  521. int size;
  522. if (params->vflag)
  523. fprintf(stderr, "Adding Image %s\n", file);
  524. dfd = open(file, O_RDONLY | O_BINARY);
  525. if (dfd < 0) {
  526. fprintf(stderr, "%s: Can't open %s: %s\n",
  527. params->cmdname, file, strerror(errno));
  528. return -1;
  529. }
  530. if (fstat(dfd, &sbuf) < 0) {
  531. fprintf(stderr, "%s: Can't stat %s: %s\n",
  532. params->cmdname, file, strerror(errno));
  533. goto err_close;
  534. }
  535. if (params->vflag)
  536. fprintf(stderr, "Size %u(pad to %u)\n",
  537. (int)sbuf.st_size, padded_size);
  538. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  539. if (ptr == MAP_FAILED) {
  540. fprintf(stderr, "%s: Can't read %s: %s\n",
  541. params->cmdname, file, strerror(errno));
  542. goto err_munmap;
  543. }
  544. size = sbuf.st_size;
  545. if (write(ifd, ptr, size) != size) {
  546. fprintf(stderr, "%s: Write error on %s: %s\n",
  547. params->cmdname, params->imagefile, strerror(errno));
  548. goto err_munmap;
  549. }
  550. munmap((void *)ptr, sbuf.st_size);
  551. close(dfd);
  552. return pad_file(params, ifd, padded_size - size);
  553. err_munmap:
  554. munmap((void *)ptr, sbuf.st_size);
  555. err_close:
  556. close(dfd);
  557. return -1;
  558. }
  559. int rockchip_copy_image(int ifd, struct image_tool_params *params)
  560. {
  561. int ret;
  562. ret = copy_file(params, ifd, spl_params.init_file,
  563. spl_params.init_size);
  564. if (ret)
  565. return ret;
  566. if (spl_params.boot_file) {
  567. ret = copy_file(params, ifd, spl_params.boot_file,
  568. spl_params.boot_size);
  569. if (ret)
  570. return ret;
  571. }
  572. return pad_file(params, ifd,
  573. params->file_size - params->orig_file_size);
  574. }