1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
6 * (C) 2017 Theobroma Systems Design und Consulting GmbH
8 * Helper functions for Rockchip images
11 #include "imagetool.h"
18 RK_SIGNATURE = 0x0ff0aa55,
22 * struct header0_info - header block for boot ROM
24 * This is stored at SD card block 64 (where each block is 512 bytes, or at
25 * the start of SPI flash. It is encoded with RC4.
27 * @signature: Signature (must be RKSD_SIGNATURE)
28 * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
29 * @init_offset: Offset in blocks of the SPL code from this header
30 * block. E.g. 4 means 2KB after the start of this header.
31 * Other fields are not used by U-Boot
38 uint8_t reserved1[492];
40 uint16_t init_boot_size;
52 * struct spl_info - spl info for each chip
54 * @imagename: Image name(passed by "mkimage -n")
55 * @spl_hdr: Boot ROM requires a 4-bytes spl header
56 * @spl_size: Spl size(include extra 4-bytes spl header)
57 * @spl_rc4: RC4 encode the SPL binary (same key as header)
61 const char *imagename;
63 const uint32_t spl_size;
67 static struct spl_info spl_infos[] = {
68 { "px30", "RK33", 0x2800, false },
69 { "rk3036", "RK30", 0x1000, false },
70 { "rk3128", "RK31", 0x1800, false },
71 { "rk3188", "RK31", 0x8000 - 0x800, true },
72 { "rk322x", "RK32", 0x8000 - 0x1000, false },
73 { "rk3288", "RK32", 0x8000, false },
74 { "rk3308", "RK33", 0x40000 - 0x1000, false},
75 { "rk3328", "RK32", 0x8000 - 0x1000, false },
76 { "rk3368", "RK33", 0x8000 - 0x1000, false },
77 { "rk3399", "RK33", 0x30000 - 0x2000, false },
78 { "rv1108", "RK11", 0x1800, false },
82 * struct spl_params - spl params parsed in check_params()
84 * @init_file: Init data file path
85 * @init_size: Aligned size of init data in bytes
86 * @boot_file: Boot data file path
87 * @boot_size: Aligned size of boot data in bytes
97 static struct spl_params spl_params = { 0 };
99 static unsigned char rc4_key[16] = {
100 124, 78, 3, 4, 85, 5, 9, 7,
101 45, 44, 123, 56, 23, 13, 23, 17
104 static struct spl_info *rkcommon_get_spl_info(char *imagename)
111 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
112 if (!strncmp(imagename, spl_infos[i].imagename, 6))
113 return spl_infos + i;
118 static int rkcommon_get_aligned_size(struct image_tool_params *params,
123 size = imagetool_get_filesize(params, fname);
128 * Pad to a 2KB alignment, as required for init/boot size by the ROM
129 * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html)
131 return ROUND(size, RK_SIZE_ALIGN);
134 int rkcommon_check_params(struct image_tool_params *params)
139 * If this is a operation (list or extract), the don't require
140 * imagename to be set.
142 if (params->lflag || params->iflag)
145 if (!rkcommon_get_spl_info(params->imagename))
148 spl_params.init_file = params->datafile;
150 spl_params.boot_file = strchr(spl_params.init_file, ':');
151 if (spl_params.boot_file) {
152 *spl_params.boot_file = '\0';
153 spl_params.boot_file += 1;
156 spl_params.init_size =
157 rkcommon_get_aligned_size(params, spl_params.init_file);
158 if (spl_params.init_size < 0)
161 /* Boot file is optional, and only for back-to-bootrom functionality. */
162 if (spl_params.boot_file) {
163 spl_params.boot_size =
164 rkcommon_get_aligned_size(params, spl_params.boot_file);
165 if (spl_params.boot_size < 0)
169 if (spl_params.init_size > rkcommon_get_spl_size(params)) {
171 "Error: SPL image is too large (size %#x than %#x)\n",
172 spl_params.init_size, rkcommon_get_spl_size(params));
179 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
180 params->imagename ? params->imagename : "NULL");
182 fprintf(stderr, "Available imagename:");
183 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
184 fprintf(stderr, "\t%s", spl_infos[i].imagename);
185 fprintf(stderr, "\n");
190 const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
192 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
195 * info would not be NULL, because of we checked params before.
197 return info->spl_hdr;
201 int rkcommon_get_spl_size(struct image_tool_params *params)
203 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
206 * info would not be NULL, because of we checked params before.
208 return info->spl_size;
211 bool rkcommon_need_rc4_spl(struct image_tool_params *params)
213 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
216 * info would not be NULL, because of we checked params before.
218 return info->spl_rc4;
221 static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
223 struct header0_info *hdr = buf;
225 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
226 hdr->signature = RK_SIGNATURE;
227 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
228 hdr->init_offset = RK_INIT_OFFSET;
229 hdr->init_size = spl_params.init_size / RK_BLK_SIZE;
232 * init_boot_size needs to be set, as it is read by the BootROM
233 * to determine the size of the next-stage bootloader (e.g. U-Boot
234 * proper), when used with the back-to-bootrom functionality.
236 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
237 * for a more detailed explanation by Andy Yan
239 if (spl_params.boot_file)
240 hdr->init_boot_size =
241 hdr->init_size + spl_params.boot_size / RK_BLK_SIZE;
243 hdr->init_boot_size =
244 hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
246 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
249 void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
250 struct image_tool_params *params)
252 struct header1_info *hdr = buf + RK_SPL_HDR_START;
254 rkcommon_set_header0(buf, params);
256 /* Set up the SPL name (i.e. copy spl_hdr over) */
257 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
259 if (rkcommon_need_rc4_spl(params))
260 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
261 spl_params.init_size);
263 if (spl_params.boot_file) {
264 if (rkcommon_need_rc4_spl(params))
265 rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
266 spl_params.init_size,
267 spl_params.boot_size);
271 static inline unsigned rkcommon_offset_to_spi(unsigned offset)
274 * While SD/MMC images use a flat addressing, SPI images are padded
275 * to use the first 2K of every 4K sector only.
277 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
280 static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
281 struct spl_info **spl_info)
283 unsigned hdr1_offset;
284 struct header1_info *hdr1_sdmmc, *hdr1_spi;
291 * The first header (hdr0) is always RC4 encoded, so try to decrypt
292 * with the well-known key.
294 memcpy((void *)header0, buf, sizeof(struct header0_info));
295 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
297 if (header0->signature != RK_SIGNATURE)
300 /* We don't support RC4 encoded image payloads here, yet... */
301 if (header0->disable_rc4 == 0)
304 hdr1_offset = header0->init_offset * RK_BLK_SIZE;
305 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
306 hdr1_spi = (struct header1_info *)(buf +
307 rkcommon_offset_to_spi(hdr1_offset));
309 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
310 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr,
313 *spl_info = &spl_infos[i];
315 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr,
318 *spl_info = &spl_infos[i];
319 return IH_TYPE_RKSPI;
326 int rkcommon_verify_header(unsigned char *buf, int size,
327 struct image_tool_params *params)
329 struct header0_info header0;
330 struct spl_info *img_spl_info, *spl_info;
333 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
335 /* If this is the (unimplemented) RC4 case, then rewrite the result */
343 * If no 'imagename' is specified via the commandline (e.g. if this is
344 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
346 if (params->imagename == NULL)
349 /* Match the 'imagename' against the 'spl_hdr' found */
350 spl_info = rkcommon_get_spl_info(params->imagename);
351 if (spl_info && img_spl_info)
352 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
357 void rkcommon_print_header(const void *buf)
359 struct header0_info header0;
360 struct spl_info *spl_info;
364 ret = rkcommon_parse_header(buf, &header0, &spl_info);
366 /* If this is the (unimplemented) RC4 case, then fail silently */
371 fprintf(stderr, "Error: image verification failed\n");
377 printf("Image Type: Rockchip %s (%s) boot image\n",
379 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
380 printf("Init Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
382 boot_size = (header0.init_boot_size - header0.init_size) * RK_BLK_SIZE;
383 if (boot_size != RK_MAX_BOOT_SIZE)
384 printf("Boot Data Size: %d bytes\n", boot_size);
387 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
389 unsigned int remaining = size;
391 while (remaining > 0) {
392 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
394 rc4_encode(buf + offset, step, rc4_key);
395 offset += RK_BLK_SIZE;
400 int rkcommon_vrec_header(struct image_tool_params *params,
401 struct image_type_params *tparams)
404 * The SPL image looks as follows:
406 * 0x0 header0 (see rkcommon.c)
407 * 0x800 spl_name ('RK30', ..., 'RK33')
408 * (start of the payload for AArch64 payloads: we expect the
409 * first 4 bytes to be available for overwriting with our
411 * 0x804 first instruction to be executed
412 * (start of the image/payload for 32bit payloads)
414 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
415 * required for its sections (so the image we receive needs to
416 * have the first 4 bytes reserved for the spl_name). Reserving
417 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
419 * The header is always at 0x800 (as we now use a payload
420 * prepadded using the boot0 hook for all targets): the first
421 * 4 bytes of these images can safely be overwritten using the
424 tparams->header_size = RK_SPL_HDR_START;
426 /* Allocate, clear and install the header */
427 tparams->hdr = malloc(tparams->header_size);
429 fprintf(stderr, "%s: Can't alloc header: %s\n",
430 params->cmdname, strerror(errno));
433 memset(tparams->hdr, 0, tparams->header_size);
436 * We need to store the original file-size (i.e. before padding), as
437 * imagetool does not set this during its adjustment of file_size.
439 params->orig_file_size = tparams->header_size +
440 spl_params.init_size + spl_params.boot_size;
442 params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
444 /* Ignoring pad len, since we are using our own copy_image() */
448 static int pad_file(struct image_tool_params *params, int ifd, int pad)
452 memset(zeros, 0, sizeof(zeros));
455 int todo = sizeof(zeros);
459 if (write(ifd, (char *)&zeros, todo) != todo) {
460 fprintf(stderr, "%s: Write error on %s: %s\n",
461 params->cmdname, params->imagefile,
471 static int copy_file(struct image_tool_params *params, int ifd,
472 const char *file, int padded_size)
480 fprintf(stderr, "Adding Image %s\n", file);
482 dfd = open(file, O_RDONLY | O_BINARY);
484 fprintf(stderr, "%s: Can't open %s: %s\n",
485 params->cmdname, file, strerror(errno));
489 if (fstat(dfd, &sbuf) < 0) {
490 fprintf(stderr, "%s: Can't stat %s: %s\n",
491 params->cmdname, file, strerror(errno));
496 fprintf(stderr, "Size %u(pad to %u)\n",
497 (int)sbuf.st_size, padded_size);
499 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
500 if (ptr == MAP_FAILED) {
501 fprintf(stderr, "%s: Can't read %s: %s\n",
502 params->cmdname, file, strerror(errno));
507 if (write(ifd, ptr, size) != size) {
508 fprintf(stderr, "%s: Write error on %s: %s\n",
509 params->cmdname, params->imagefile, strerror(errno));
513 munmap((void *)ptr, sbuf.st_size);
515 return pad_file(params, ifd, padded_size - size);
518 munmap((void *)ptr, sbuf.st_size);
524 int rockchip_copy_image(int ifd, struct image_tool_params *params)
528 ret = copy_file(params, ifd, spl_params.init_file,
529 spl_params.init_size);
533 if (spl_params.boot_file) {
534 ret = copy_file(params, ifd, spl_params.boot_file,
535 spl_params.boot_size);
540 return pad_file(params, ifd,
541 params->file_size - params->orig_file_size);