1
2
3
4
5
6
7
8
9
10
11#include "imagetool.h"
12#include <image.h>
13#include <u-boot/sha256.h>
14#include <rc4.h>
15#include "mkimage.h"
16#include "rkcommon.h"
17
18enum {
19 RK_MAGIC = 0x0ff0aa55,
20 RK_MAGIC_V2 = 0x534E4B52,
21};
22
23enum {
24 RK_HEADER_V1 = 1,
25 RK_HEADER_V2 = 2,
26};
27
28enum hash_type {
29 HASH_NONE = 0,
30 HASH_SHA256 = 1,
31 HASH_SHA512 = 2,
32};
33
34
35
36
37
38
39
40
41
42
43
44struct image_entry {
45 uint32_t size_and_off;
46 uint32_t address;
47 uint32_t flag;
48 uint32_t counter;
49 uint8_t reserved[8];
50 uint8_t hash[64];
51};
52
53
54
55
56
57
58
59
60
61
62
63
64
65struct header0_info_v2 {
66 uint32_t magic;
67 uint8_t reserved[4];
68 uint32_t size_and_nimage;
69 uint32_t boot_flag;
70 uint8_t reserved1[104];
71 struct image_entry images[4];
72 uint8_t reserved2[1064];
73 uint8_t hash[512];
74};
75
76
77
78
79
80
81
82
83
84
85
86
87
88struct header0_info {
89 uint32_t magic;
90 uint8_t reserved[4];
91 uint32_t disable_rc4;
92 uint16_t init_offset;
93 uint8_t reserved1[492];
94 uint16_t init_size;
95 uint16_t init_boot_size;
96 uint8_t reserved2[2];
97};
98
99
100
101
102struct header1_info {
103 uint32_t magic;
104};
105
106
107
108
109
110
111
112
113
114
115struct spl_info {
116 const char *imagename;
117 const char *spl_hdr;
118 const uint32_t spl_size;
119 const bool spl_rc4;
120 const uint32_t header_ver;
121};
122
123static struct spl_info spl_infos[] = {
124 { "px30", "RK33", 0x2800, false, RK_HEADER_V1 },
125 { "rk3036", "RK30", 0x1000, false, RK_HEADER_V1 },
126 { "rk3066", "RK30", 0x8000 - 0x800, true, RK_HEADER_V1 },
127 { "rk3128", "RK31", 0x1800, false, RK_HEADER_V1 },
128 { "rk3188", "RK31", 0x8000 - 0x800, true, RK_HEADER_V1 },
129 { "rk322x", "RK32", 0x8000 - 0x1000, false, RK_HEADER_V1 },
130 { "rk3288", "RK32", 0x8000, false, RK_HEADER_V1 },
131 { "rk3308", "RK33", 0x40000 - 0x1000, false, RK_HEADER_V1 },
132 { "rk3328", "RK32", 0x8000 - 0x1000, false, RK_HEADER_V1 },
133 { "rk3368", "RK33", 0x8000 - 0x1000, false, RK_HEADER_V1 },
134 { "rk3399", "RK33", 0x30000 - 0x2000, false, RK_HEADER_V1 },
135 { "rv1108", "RK11", 0x1800, false, RK_HEADER_V1 },
136 { "rk3568", "RK35", 0x14000 - 0x1000, false, RK_HEADER_V2 },
137};
138
139
140
141
142
143
144
145
146
147
148struct spl_params {
149 char *init_file;
150 uint32_t init_size;
151 char *boot_file;
152 uint32_t boot_size;
153};
154
155static struct spl_params spl_params = { 0 };
156
157static unsigned char rc4_key[16] = {
158 124, 78, 3, 4, 85, 5, 9, 7,
159 45, 44, 123, 56, 23, 13, 23, 17
160};
161
162static struct spl_info *rkcommon_get_spl_info(char *imagename)
163{
164 int i;
165
166 if (!imagename)
167 return NULL;
168
169 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
170 if (!strncmp(imagename, spl_infos[i].imagename, 6))
171 return spl_infos + i;
172
173 return NULL;
174}
175
176static int rkcommon_get_aligned_size(struct image_tool_params *params,
177 const char *fname)
178{
179 int size;
180
181 size = imagetool_get_filesize(params, fname);
182 if (size < 0)
183 return -1;
184
185
186
187
188
189 return ROUND(size, RK_SIZE_ALIGN);
190}
191
192int rkcommon_check_params(struct image_tool_params *params)
193{
194 int i, size;
195
196
197
198
199
200 if (params->lflag || params->iflag)
201 return EXIT_SUCCESS;
202
203 if (!rkcommon_get_spl_info(params->imagename))
204 goto err_spl_info;
205
206 spl_params.init_file = params->datafile;
207
208 spl_params.boot_file = strchr(spl_params.init_file, ':');
209 if (spl_params.boot_file) {
210 *spl_params.boot_file = '\0';
211 spl_params.boot_file += 1;
212 }
213
214 size = rkcommon_get_aligned_size(params, spl_params.init_file);
215 if (size < 0)
216 return EXIT_FAILURE;
217 spl_params.init_size = size;
218
219
220 if (spl_params.boot_file) {
221 size = rkcommon_get_aligned_size(params, spl_params.boot_file);
222 if (size < 0)
223 return EXIT_FAILURE;
224 spl_params.boot_size = size;
225 }
226
227 if (spl_params.init_size > rkcommon_get_spl_size(params)) {
228 fprintf(stderr,
229 "Error: SPL image is too large (size %#x than %#x)\n",
230 spl_params.init_size, rkcommon_get_spl_size(params));
231 return EXIT_FAILURE;
232 }
233
234 return EXIT_SUCCESS;
235
236err_spl_info:
237 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
238 params->imagename ? params->imagename : "NULL");
239
240 fprintf(stderr, "Available imagename:");
241 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
242 fprintf(stderr, "\t%s", spl_infos[i].imagename);
243 fprintf(stderr, "\n");
244
245 return EXIT_FAILURE;
246}
247
248const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
249{
250 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
251
252
253
254
255 return info->spl_hdr;
256}
257
258int rkcommon_get_spl_size(struct image_tool_params *params)
259{
260 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
261
262
263
264
265 return info->spl_size;
266}
267
268bool rkcommon_need_rc4_spl(struct image_tool_params *params)
269{
270 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
271
272
273
274
275 return info->spl_rc4;
276}
277
278bool rkcommon_is_header_v2(struct image_tool_params *params)
279{
280 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
281
282 return (info->header_ver == RK_HEADER_V2);
283}
284
285static void do_sha256_hash(uint8_t *buf, uint32_t size, uint8_t *out)
286{
287 sha256_context ctx;
288
289 sha256_starts(&ctx);
290 sha256_update(&ctx, buf, size);
291 sha256_finish(&ctx, out);
292}
293
294static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
295{
296 struct header0_info *hdr = buf;
297 uint32_t init_boot_size;
298
299 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
300 hdr->magic = cpu_to_le32(RK_MAGIC);
301 hdr->disable_rc4 = cpu_to_le32(!rkcommon_need_rc4_spl(params));
302 hdr->init_offset = cpu_to_le16(RK_INIT_OFFSET);
303 hdr->init_size = cpu_to_le16(spl_params.init_size / RK_BLK_SIZE);
304
305
306
307
308
309
310
311
312
313 if (spl_params.boot_file)
314 init_boot_size = spl_params.init_size + spl_params.boot_size;
315 else
316 init_boot_size = spl_params.init_size + RK_MAX_BOOT_SIZE;
317 hdr->init_boot_size = cpu_to_le16(init_boot_size / RK_BLK_SIZE);
318
319 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
320}
321
322static void rkcommon_set_header0_v2(void *buf, struct image_tool_params *params)
323{
324 struct header0_info_v2 *hdr = buf;
325 uint32_t sector_offset, image_sector_count;
326 uint32_t image_size_array[2];
327 uint8_t *image_ptr = NULL;
328 int i;
329
330 printf("Image Type: Rockchip %s boot image\n",
331 rkcommon_get_spl_hdr(params));
332 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
333 hdr->magic = cpu_to_le32(RK_MAGIC_V2);
334 hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
335 hdr->boot_flag = cpu_to_le32(HASH_SHA256);
336 sector_offset = 4;
337 image_size_array[0] = spl_params.init_size;
338 image_size_array[1] = spl_params.boot_size;
339
340 for (i = 0; i < 2; i++) {
341 image_sector_count = image_size_array[i] / RK_BLK_SIZE;
342 hdr->images[i].size_and_off = cpu_to_le32((image_sector_count
343 << 16) + sector_offset);
344 hdr->images[i].address = 0xFFFFFFFF;
345 hdr->images[i].counter = cpu_to_le32(i + 1);
346 image_ptr = buf + sector_offset * RK_BLK_SIZE;
347 do_sha256_hash(image_ptr, image_size_array[i],
348 hdr->images[i].hash);
349 sector_offset = sector_offset + image_sector_count;
350 }
351
352 do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
353}
354
355void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
356 struct image_tool_params *params)
357{
358 struct header1_info *hdr = buf + RK_SPL_HDR_START;
359
360 if (rkcommon_is_header_v2(params)) {
361 rkcommon_set_header0_v2(buf, params);
362 } else {
363 rkcommon_set_header0(buf, params);
364
365
366 if (memcmp(&hdr->magic, "RSAK", 4))
367 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
368
369 if (rkcommon_need_rc4_spl(params))
370 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
371 spl_params.init_size);
372
373 if (spl_params.boot_file) {
374 if (rkcommon_need_rc4_spl(params))
375 rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
376 spl_params.init_size,
377 spl_params.boot_size);
378 }
379 }
380}
381
382static inline unsigned int rkcommon_offset_to_spi(unsigned int offset)
383{
384
385
386
387
388 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
389}
390
391static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
392 struct spl_info **spl_info)
393{
394 unsigned int hdr1_offset;
395 struct header1_info *hdr1_sdmmc, *hdr1_spi;
396 int i;
397
398 if (spl_info)
399 *spl_info = NULL;
400
401
402
403
404
405 memcpy((void *)header0, buf, sizeof(struct header0_info));
406 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
407
408 if (le32_to_cpu(header0->magic) != RK_MAGIC)
409 return -EPROTO;
410
411
412 if (le32_to_cpu(header0->disable_rc4) == 0)
413 return -ENOSYS;
414
415 hdr1_offset = le16_to_cpu(header0->init_offset) * RK_BLK_SIZE;
416 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
417 hdr1_spi = (struct header1_info *)(buf +
418 rkcommon_offset_to_spi(hdr1_offset));
419
420 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
421 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr,
422 RK_SPL_HDR_SIZE)) {
423 if (spl_info)
424 *spl_info = &spl_infos[i];
425 return IH_TYPE_RKSD;
426 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr,
427 RK_SPL_HDR_SIZE)) {
428 if (spl_info)
429 *spl_info = &spl_infos[i];
430 return IH_TYPE_RKSPI;
431 }
432 }
433
434 return -1;
435}
436
437static int rkcommon_parse_header_v2(const void *buf, struct header0_info_v2 *header)
438{
439 memcpy((void *)header, buf, sizeof(struct header0_info_v2));
440
441 if (le32_to_cpu(header->magic) != RK_MAGIC_V2)
442 return -EPROTO;
443
444 return 0;
445}
446
447int rkcommon_verify_header(unsigned char *buf, int size,
448 struct image_tool_params *params)
449{
450 struct header0_info header0;
451 struct spl_info *img_spl_info, *spl_info;
452 int ret;
453
454
455 if ((*(uint32_t *)buf) == RK_MAGIC_V2)
456 return 0;
457
458 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
459
460
461 if (ret == -ENOSYS)
462 return 0;
463
464 if (ret < 0)
465 return ret;
466
467
468
469
470
471 if (params->imagename == NULL)
472 return 0;
473
474
475 spl_info = rkcommon_get_spl_info(params->imagename);
476 if (spl_info && img_spl_info)
477 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
478
479 return -ENOENT;
480}
481
482void rkcommon_print_header(const void *buf)
483{
484 struct header0_info header0;
485 struct header0_info_v2 header0_v2;
486 struct spl_info *spl_info;
487 uint8_t image_type;
488 int ret, boot_size, init_size;
489
490 if ((*(uint32_t *)buf) == RK_MAGIC_V2) {
491 ret = rkcommon_parse_header_v2(buf, &header0_v2);
492
493 if (ret < 0) {
494 fprintf(stderr, "Error: image verification failed\n");
495 return;
496 }
497
498 init_size = header0_v2.images[0].size_and_off >> 16;
499 init_size = init_size * RK_BLK_SIZE;
500 boot_size = header0_v2.images[1].size_and_off >> 16;
501 boot_size = boot_size * RK_BLK_SIZE;
502 } else {
503 ret = rkcommon_parse_header(buf, &header0, &spl_info);
504
505
506 if (ret == -ENOSYS)
507 return;
508
509 if (ret < 0) {
510 fprintf(stderr, "Error: image verification failed\n");
511 return;
512 }
513
514 image_type = ret;
515 init_size = header0.init_size * RK_BLK_SIZE;
516 boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
517
518 printf("Image Type: Rockchip %s (%s) boot image\n",
519 spl_info->spl_hdr,
520 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
521 }
522
523 printf("Init Data Size: %d bytes\n", init_size);
524
525 if (boot_size != RK_MAX_BOOT_SIZE)
526 printf("Boot Data Size: %d bytes\n", boot_size);
527}
528
529void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
530{
531 unsigned int remaining = size;
532
533 while (remaining > 0) {
534 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
535
536 rc4_encode(buf + offset, step, rc4_key);
537 offset += RK_BLK_SIZE;
538 remaining -= step;
539 }
540}
541
542int rkcommon_vrec_header(struct image_tool_params *params,
543 struct image_type_params *tparams)
544{
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566 tparams->header_size = RK_SPL_HDR_START;
567
568
569 tparams->hdr = malloc(tparams->header_size);
570 if (!tparams->hdr) {
571 fprintf(stderr, "%s: Can't alloc header: %s\n",
572 params->cmdname, strerror(errno));
573 exit(EXIT_FAILURE);
574 }
575 memset(tparams->hdr, 0, tparams->header_size);
576
577
578
579
580
581 params->orig_file_size = tparams->header_size +
582 spl_params.init_size + spl_params.boot_size;
583
584 params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
585
586
587 return 0;
588}
589
590static int pad_file(struct image_tool_params *params, int ifd, int pad)
591{
592 uint8_t zeros[4096];
593
594 memset(zeros, 0, sizeof(zeros));
595
596 while (pad > 0) {
597 int todo = sizeof(zeros);
598
599 if (todo > pad)
600 todo = pad;
601 if (write(ifd, (char *)&zeros, todo) != todo) {
602 fprintf(stderr, "%s: Write error on %s: %s\n",
603 params->cmdname, params->imagefile,
604 strerror(errno));
605 return -1;
606 }
607 pad -= todo;
608 }
609
610 return 0;
611}
612
613static int copy_file(struct image_tool_params *params, int ifd,
614 const char *file, int padded_size)
615{
616 int dfd;
617 struct stat sbuf;
618 unsigned char *ptr;
619 int size;
620
621 if (params->vflag)
622 fprintf(stderr, "Adding Image %s\n", file);
623
624 dfd = open(file, O_RDONLY | O_BINARY);
625 if (dfd < 0) {
626 fprintf(stderr, "%s: Can't open %s: %s\n",
627 params->cmdname, file, strerror(errno));
628 return -1;
629 }
630
631 if (fstat(dfd, &sbuf) < 0) {
632 fprintf(stderr, "%s: Can't stat %s: %s\n",
633 params->cmdname, file, strerror(errno));
634 goto err_close;
635 }
636
637 if (params->vflag)
638 fprintf(stderr, "Size %u(pad to %u)\n",
639 (int)sbuf.st_size, padded_size);
640
641 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
642 if (ptr == MAP_FAILED) {
643 fprintf(stderr, "%s: Can't read %s: %s\n",
644 params->cmdname, file, strerror(errno));
645 goto err_munmap;
646 }
647
648 size = sbuf.st_size;
649 if (write(ifd, ptr, size) != size) {
650 fprintf(stderr, "%s: Write error on %s: %s\n",
651 params->cmdname, params->imagefile, strerror(errno));
652 goto err_munmap;
653 }
654
655 munmap((void *)ptr, sbuf.st_size);
656 close(dfd);
657 return pad_file(params, ifd, padded_size - size);
658
659err_munmap:
660 munmap((void *)ptr, sbuf.st_size);
661err_close:
662 close(dfd);
663 return -1;
664}
665
666int rockchip_copy_image(int ifd, struct image_tool_params *params)
667{
668 int ret;
669
670 ret = copy_file(params, ifd, spl_params.init_file,
671 spl_params.init_size);
672 if (ret)
673 return ret;
674
675 if (spl_params.boot_file) {
676 ret = copy_file(params, ifd, spl_params.boot_file,
677 spl_params.boot_size);
678 if (ret)
679 return ret;
680 }
681
682 return pad_file(params, ifd,
683 params->file_size - params->orig_file_size);
684}
685