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