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 { "rk3588", "RK35", 0x100000 - 0x1000, false, RK_HEADER_V2 },
145 };
146
147 /**
148 * struct spl_params - spl params parsed in check_params()
149 *
150 * @init_file: Init data file path
151 * @init_size: Aligned size of init data in bytes
152 * @boot_file: Boot data file path
153 * @boot_size: Aligned size of boot data in bytes
154 */
155
156 struct spl_params {
157 char *init_file;
158 uint32_t init_size;
159 char *boot_file;
160 uint32_t boot_size;
161 };
162
163 static struct spl_params spl_params = { 0 };
164
165 static unsigned char rc4_key[16] = {
166 124, 78, 3, 4, 85, 5, 9, 7,
167 45, 44, 123, 56, 23, 13, 23, 17
168 };
169
rkcommon_get_spl_info(char * imagename)170 static struct spl_info *rkcommon_get_spl_info(char *imagename)
171 {
172 int i;
173
174 if (!imagename)
175 return NULL;
176
177 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
178 if (!strncmp(imagename, spl_infos[i].imagename, 6))
179 return spl_infos + i;
180
181 return NULL;
182 }
183
rkcommon_get_aligned_size(struct image_tool_params * params,const char * fname)184 static int rkcommon_get_aligned_size(struct image_tool_params *params,
185 const char *fname)
186 {
187 int size;
188
189 size = imagetool_get_filesize(params, fname);
190 if (size < 0)
191 return -1;
192
193 /*
194 * Pad to a 2KB alignment, as required for init/boot size by the ROM
195 * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html)
196 */
197 return ROUND(size, RK_SIZE_ALIGN);
198 }
199
rkcommon_check_params(struct image_tool_params * params)200 int rkcommon_check_params(struct image_tool_params *params)
201 {
202 int i;
203
204 /*
205 * If this is a operation (list or extract), the don't require
206 * imagename to be set.
207 */
208 if (params->lflag || params->iflag)
209 return EXIT_SUCCESS;
210
211 if (!rkcommon_get_spl_info(params->imagename))
212 goto err_spl_info;
213
214 spl_params.init_file = params->datafile;
215
216 spl_params.boot_file = strchr(spl_params.init_file, ':');
217 if (spl_params.boot_file) {
218 *spl_params.boot_file = '\0';
219 spl_params.boot_file += 1;
220 }
221
222 spl_params.init_size =
223 rkcommon_get_aligned_size(params, spl_params.init_file);
224 if (spl_params.init_size < 0)
225 return EXIT_FAILURE;
226
227 /* Boot file is optional, and only for back-to-bootrom functionality. */
228 if (spl_params.boot_file) {
229 spl_params.boot_size =
230 rkcommon_get_aligned_size(params, spl_params.boot_file);
231 if (spl_params.boot_size < 0)
232 return EXIT_FAILURE;
233 }
234
235 if (spl_params.init_size > rkcommon_get_spl_size(params)) {
236 fprintf(stderr,
237 "Error: SPL image is too large (size %#x than %#x)\n",
238 spl_params.init_size, rkcommon_get_spl_size(params));
239 return EXIT_FAILURE;
240 }
241
242 return EXIT_SUCCESS;
243
244 err_spl_info:
245 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
246 params->imagename ? params->imagename : "NULL");
247
248 fprintf(stderr, "Available imagename:");
249 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
250 fprintf(stderr, "\t%s", spl_infos[i].imagename);
251 fprintf(stderr, "\n");
252
253 return EXIT_FAILURE;
254 }
255
rkcommon_get_spl_hdr(struct image_tool_params * params)256 const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
257 {
258 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
259
260 /*
261 * info would not be NULL, because of we checked params before.
262 */
263 return info->spl_hdr;
264 }
265
rkcommon_get_spl_size(struct image_tool_params * params)266 int rkcommon_get_spl_size(struct image_tool_params *params)
267 {
268 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
269
270 /*
271 * info would not be NULL, because of we checked params before.
272 */
273 return info->spl_size;
274 }
275
rkcommon_need_rc4_spl(struct image_tool_params * params)276 bool rkcommon_need_rc4_spl(struct image_tool_params *params)
277 {
278 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
279
280 /*
281 * info would not be NULL, because of we checked params before.
282 */
283 return info->spl_rc4;
284 }
285
rkcommon_is_header_v2(struct image_tool_params * params)286 bool rkcommon_is_header_v2(struct image_tool_params *params)
287 {
288 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
289
290 /*
291 * info would not be NULL, because of we checked params before.
292 */
293 return (info->header_ver == RK_HEADER_V2);
294 }
295
do_sha256_hash(uint8_t * buf,uint32_t size,uint8_t * out)296 static void do_sha256_hash(uint8_t *buf, uint32_t size, uint8_t *out)
297 {
298 sha256_context ctx;
299
300 sha256_starts(&ctx);
301 sha256_update(&ctx, buf, size);
302 sha256_finish(&ctx, out);
303 }
304
rkcommon_set_header0(void * buf,struct image_tool_params * params)305 static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
306 {
307 struct header0_info *hdr = buf;
308
309 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
310 hdr->magic = RK_MAGIC;
311 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
312 hdr->init_offset = RK_INIT_OFFSET;
313 hdr->init_size = spl_params.init_size / RK_BLK_SIZE;
314
315 /*
316 * init_boot_size needs to be set, as it is read by the BootROM
317 * to determine the size of the next-stage bootloader (e.g. U-Boot
318 * proper), when used with the back-to-bootrom functionality.
319 *
320 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
321 * for a more detailed explanation by Andy Yan
322 */
323 if (spl_params.boot_file)
324 hdr->init_boot_size =
325 hdr->init_size + spl_params.boot_size / RK_BLK_SIZE;
326 else
327 hdr->init_boot_size =
328 hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
329
330 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
331 }
332
rkcommon_set_header0_v2(void * buf,struct image_tool_params * params)333 static void rkcommon_set_header0_v2(void *buf, struct image_tool_params *params)
334 {
335 struct header0_info_v2 *hdr = buf;
336 uint32_t sector_offset, image_sector_count;
337 uint32_t image_size_array[2];
338 uint8_t *image_ptr = NULL;
339 int i;
340
341 printf("Image Type: Rockchip %s boot image\n", rkcommon_get_spl_hdr(params));
342 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
343 hdr->magic = cpu_to_le32(RK_MAGIC_V2);
344 hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
345 hdr->boot_flag = cpu_to_le32(HASH_SHA256);
346 sector_offset = 4;
347 image_size_array[0] = spl_params.init_size;
348 image_size_array[1] = spl_params.boot_size;
349
350 for (i = 0; i < 2; i++) {
351 image_sector_count = image_size_array[i] / RK_BLK_SIZE;
352 hdr->images[i].size_and_off = cpu_to_le32((image_sector_count << 16) + sector_offset);
353 hdr->images[i].address = 0xFFFFFFFF;
354 hdr->images[i].counter = cpu_to_le32(i + 1);
355 image_ptr = buf + sector_offset * RK_BLK_SIZE;
356 do_sha256_hash(image_ptr, image_size_array[i], hdr->images[i].hash);
357 sector_offset = sector_offset + image_sector_count;
358 }
359
360 do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
361 }
362
rkcommon_set_header(void * buf,struct stat * sbuf,int ifd,struct image_tool_params * params)363 void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
364 struct image_tool_params *params)
365 {
366 struct header1_info *hdr = buf + RK_SPL_HDR_START;
367
368 if (rkcommon_is_header_v2(params)) {
369 rkcommon_set_header0_v2(buf, params);
370 } else {
371 rkcommon_set_header0(buf, params);
372
373 /* Set up the SPL name (i.e. copy spl_hdr over) */
374 if (memcmp(&hdr->magic, "RSAK", 4))
375 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
376
377 if (rkcommon_need_rc4_spl(params))
378 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
379 spl_params.init_size);
380
381 if (spl_params.boot_file) {
382 if (rkcommon_need_rc4_spl(params))
383 rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
384 spl_params.init_size,
385 spl_params.boot_size);
386 }
387 }
388 }
389
rkcommon_offset_to_spi(unsigned int offset)390 static inline unsigned int rkcommon_offset_to_spi(unsigned int offset)
391 {
392 /*
393 * While SD/MMC images use a flat addressing, SPI images are padded
394 * to use the first 2K of every 4K sector only.
395 */
396 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
397 }
398
rkcommon_parse_header(const void * buf,struct header0_info * header0,struct spl_info ** spl_info)399 static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
400 struct spl_info **spl_info)
401 {
402 unsigned int hdr1_offset;
403 struct header1_info *hdr1_sdmmc, *hdr1_spi;
404 int i;
405
406 if (spl_info)
407 *spl_info = NULL;
408
409 /*
410 * The first header (hdr0) is always RC4 encoded, so try to decrypt
411 * with the well-known key.
412 */
413 memcpy((void *)header0, buf, sizeof(struct header0_info));
414 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
415
416 if (header0->magic != RK_MAGIC)
417 return -EPROTO;
418
419 /* We don't support RC4 encoded image payloads here, yet... */
420 if (header0->disable_rc4 == 0)
421 return -ENOSYS;
422
423 hdr1_offset = header0->init_offset * RK_BLK_SIZE;
424 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
425 hdr1_spi = (struct header1_info *)(buf +
426 rkcommon_offset_to_spi(hdr1_offset));
427
428 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
429 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
430 if (spl_info)
431 *spl_info = &spl_infos[i];
432 return IH_TYPE_RKSD;
433 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
434 if (spl_info)
435 *spl_info = &spl_infos[i];
436 return IH_TYPE_RKSPI;
437 }
438 }
439
440 return -1;
441 }
442
rkcommon_parse_header_v2(const void * buf,struct header0_info_v2 * header)443 static int rkcommon_parse_header_v2(const void *buf, struct header0_info_v2 *header)
444 {
445 memcpy((void *)header, buf, sizeof(struct header0_info_v2));
446
447 if (le32_to_cpu(header->magic) != RK_MAGIC_V2)
448 return -EPROTO;
449
450 return 0;
451 }
452
rkcommon_verify_header(unsigned char * buf,int size,struct image_tool_params * params)453 int rkcommon_verify_header(unsigned char *buf, int size,
454 struct image_tool_params *params)
455 {
456 struct header0_info header0;
457 struct spl_info *img_spl_info, *spl_info;
458 int ret;
459
460 /* spl_hdr is abandon on header_v2 */
461 if ((*(uint32_t *)buf) == RK_MAGIC_V2)
462 return 0;
463
464 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
465
466 /* If this is the (unimplemented) RC4 case, then rewrite the result */
467 if (ret == -ENOSYS)
468 return 0;
469
470 if (ret < 0)
471 return ret;
472
473 /*
474 * If no 'imagename' is specified via the commandline (e.g. if this is
475 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
476 */
477 if (params->imagename == NULL)
478 return 0;
479
480 /* Match the 'imagename' against the 'spl_hdr' found */
481 spl_info = rkcommon_get_spl_info(params->imagename);
482 if (spl_info && img_spl_info)
483 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
484
485 return -ENOENT;
486 }
487
rkcommon_print_header(const void * buf)488 void rkcommon_print_header(const void *buf)
489 {
490 struct header0_info header0;
491 struct header0_info_v2 header0_v2;
492 struct spl_info *spl_info;
493 uint8_t image_type;
494 int ret, boot_size, init_size;
495
496 if ((*(uint32_t *)buf) == RK_MAGIC_V2) {
497 ret = rkcommon_parse_header_v2(buf, &header0_v2);
498
499 if (ret < 0) {
500 fprintf(stderr, "Error: image verification failed\n");
501 return;
502 }
503
504 init_size = header0_v2.images[0].size_and_off >> 16;
505 init_size = init_size * RK_BLK_SIZE;
506 boot_size = header0_v2.images[1].size_and_off >> 16;
507 boot_size = boot_size * RK_BLK_SIZE;
508 } else {
509 ret = rkcommon_parse_header(buf, &header0, &spl_info);
510
511 /* If this is the (unimplemented) RC4 case, then fail silently */
512 if (ret == -ENOSYS)
513 return;
514
515 if (ret < 0) {
516 fprintf(stderr, "Error: image verification failed\n");
517 return;
518 }
519
520 image_type = ret;
521 init_size = header0.init_size * RK_BLK_SIZE;
522 boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
523 printf("Image Type: Rockchip %s (%s) boot image\n",
524 spl_info->spl_hdr,
525 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
526 }
527
528 printf("Init Data Size: %d bytes\n", init_size);
529
530 if (boot_size != RK_MAX_BOOT_SIZE)
531 printf("Boot Data Size: %d bytes\n", boot_size);
532 }
533
rkcommon_rc4_encode_spl(void * buf,unsigned int offset,unsigned int size)534 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
535 {
536 unsigned int remaining = size;
537
538 while (remaining > 0) {
539 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
540
541 rc4_encode(buf + offset, step, rc4_key);
542 offset += RK_BLK_SIZE;
543 remaining -= step;
544 }
545 }
546
rkcommon_vrec_header(struct image_tool_params * params,struct image_type_params * tparams)547 int rkcommon_vrec_header(struct image_tool_params *params,
548 struct image_type_params *tparams)
549 {
550 /*
551 * The SPL image looks as follows:
552 *
553 * 0x0 header0 (see rkcommon.c)
554 * 0x800 spl_name ('RK30', ..., 'RK33')
555 * (start of the payload for AArch64 payloads: we expect the
556 * first 4 bytes to be available for overwriting with our
557 * spl_name)
558 * 0x804 first instruction to be executed
559 * (start of the image/payload for 32bit payloads)
560 *
561 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
562 * required for its sections (so the image we receive needs to
563 * have the first 4 bytes reserved for the spl_name). Reserving
564 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
565 *
566 * The header is always at 0x800 (as we now use a payload
567 * prepadded using the boot0 hook for all targets): the first
568 * 4 bytes of these images can safely be overwritten using the
569 * boot magic.
570 */
571 tparams->header_size = RK_SPL_HDR_START;
572
573 /* Allocate, clear and install the header */
574 tparams->hdr = malloc(tparams->header_size);
575 if (!tparams->hdr) {
576 fprintf(stderr, "%s: Can't alloc header: %s\n",
577 params->cmdname, strerror(errno));
578 exit(EXIT_FAILURE);
579 }
580 memset(tparams->hdr, 0, tparams->header_size);
581
582 /*
583 * We need to store the original file-size (i.e. before padding), as
584 * imagetool does not set this during its adjustment of file_size.
585 */
586 params->orig_file_size = tparams->header_size +
587 spl_params.init_size + spl_params.boot_size;
588
589 params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
590
591 /* Ignoring pad len, since we are using our own copy_image() */
592 return 0;
593 }
594
pad_file(struct image_tool_params * params,int ifd,int pad)595 static int pad_file(struct image_tool_params *params, int ifd, int pad)
596 {
597 uint8_t zeros[4096];
598
599 memset(zeros, 0, sizeof(zeros));
600
601 while (pad > 0) {
602 int todo = sizeof(zeros);
603
604 if (todo > pad)
605 todo = pad;
606 if (write(ifd, (char *)&zeros, todo) != todo) {
607 fprintf(stderr, "%s: Write error on %s: %s\n",
608 params->cmdname, params->imagefile,
609 strerror(errno));
610 return -1;
611 }
612 pad -= todo;
613 }
614
615 return 0;
616 }
617
copy_file(struct image_tool_params * params,int ifd,const char * file,int padded_size)618 static int copy_file(struct image_tool_params *params, int ifd,
619 const char *file, int padded_size)
620 {
621 int dfd;
622 struct stat sbuf;
623 unsigned char *ptr;
624 int size;
625
626 if (params->vflag)
627 fprintf(stderr, "Adding Image %s\n", file);
628
629 dfd = open(file, O_RDONLY | O_BINARY);
630 if (dfd < 0) {
631 fprintf(stderr, "%s: Can't open %s: %s\n",
632 params->cmdname, file, strerror(errno));
633 return -1;
634 }
635
636 if (fstat(dfd, &sbuf) < 0) {
637 fprintf(stderr, "%s: Can't stat %s: %s\n",
638 params->cmdname, file, strerror(errno));
639 goto err_close;
640 }
641
642 if (params->vflag)
643 fprintf(stderr, "Size %u(pad to %u)\n",
644 (int)sbuf.st_size, padded_size);
645
646 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
647 if (ptr == MAP_FAILED) {
648 fprintf(stderr, "%s: Can't read %s: %s\n",
649 params->cmdname, file, strerror(errno));
650 goto err_munmap;
651 }
652
653 size = sbuf.st_size;
654 if (write(ifd, ptr, size) != size) {
655 fprintf(stderr, "%s: Write error on %s: %s\n",
656 params->cmdname, params->imagefile, strerror(errno));
657 goto err_munmap;
658 }
659
660 munmap((void *)ptr, sbuf.st_size);
661 close(dfd);
662 return pad_file(params, ifd, padded_size - size);
663
664 err_munmap:
665 munmap((void *)ptr, sbuf.st_size);
666 err_close:
667 close(dfd);
668 return -1;
669 }
670
rockchip_copy_image(int ifd,struct image_tool_params * params)671 int rockchip_copy_image(int ifd, struct image_tool_params *params)
672 {
673 int ret;
674
675 ret = copy_file(params, ifd, spl_params.init_file,
676 spl_params.init_size);
677 if (ret)
678 return ret;
679
680 if (spl_params.boot_file) {
681 ret = copy_file(params, ifd, spl_params.boot_file,
682 spl_params.boot_size);
683 if (ret)
684 return ret;
685 }
686
687 return pad_file(params, ifd,
688 params->file_size - params->orig_file_size);
689 }
690