2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * (C) 2017 Theobroma Systems Design und Consulting GmbH
7 * SPDX-License-Identifier: GPL-2.0+
9 * Helper functions for Rockchip images
12 #include "imagetool.h"
18 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
21 RK_SIGNATURE = 0x0ff0aa55,
25 * struct header0_info - header block for boot ROM
27 * This is stored at SD card block 64 (where each block is 512 bytes, or at
28 * the start of SPI flash. It is encoded with RC4.
30 * @signature: Signature (must be RKSD_SIGNATURE)
31 * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
32 * @init_offset: Offset in blocks of the SPL code from this header
33 * block. E.g. 4 means 2KB after the start of this header.
34 * Other fields are not used by U-Boot
41 uint8_t reserved1[492];
43 uint16_t init_boot_size;
55 * struct spl_info - spl info for each chip
57 * @imagename: Image name(passed by "mkimage -n")
58 * @spl_hdr: Boot ROM requires a 4-bytes spl header
59 * @spl_size: Spl size(include extra 4-bytes spl header)
60 * @spl_rc4: RC4 encode the SPL binary (same key as header)
61 * @spl_boot0: A new-style (ARM_SOC_BOOT0_HOOK) image that should
62 * have the boot magic (e.g. 'RK33') written to its first
67 const char *imagename;
69 const uint32_t spl_size;
74 static struct spl_info spl_infos[] = {
75 { "rk3036", "RK30", 0x1000, false, false },
76 { "rk3188", "RK31", 0x8000 - 0x800, true, false },
77 { "rk322x", "RK32", 0x8000 - 0x1000, false, false },
78 { "rk3288", "RK32", 0x8000, false, false },
79 { "rk3328", "RK32", 0x8000 - 0x1000, false, false },
80 { "rk3399", "RK33", 0x30000 - 0x2000, false, true },
81 { "rv1108", "RK11", 0x1800, false, false},
84 static unsigned char rc4_key[16] = {
85 124, 78, 3, 4, 85, 5, 9, 7,
86 45, 44, 123, 56, 23, 13, 23, 17
89 static struct spl_info *rkcommon_get_spl_info(char *imagename)
96 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
97 if (!strncmp(imagename, spl_infos[i].imagename, 6))
103 int rkcommon_check_params(struct image_tool_params *params)
107 if (rkcommon_get_spl_info(params->imagename) != NULL)
111 * If this is a operation (list or extract), the don't require
112 * imagename to be set.
114 if (params->lflag || params->iflag)
117 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
118 params->imagename ? params->imagename : "NULL");
120 fprintf(stderr, "Available imagename:");
121 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
122 fprintf(stderr, "\t%s", spl_infos[i].imagename);
123 fprintf(stderr, "\n");
128 const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
130 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
133 * info would not be NULL, because of we checked params before.
135 return info->spl_hdr;
139 int rkcommon_get_spl_size(struct image_tool_params *params)
141 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
144 * info would not be NULL, because of we checked params before.
146 return info->spl_size;
149 bool rkcommon_need_rc4_spl(struct image_tool_params *params)
151 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
154 * info would not be NULL, because of we checked params before.
156 return info->spl_rc4;
159 bool rkcommon_spl_is_boot0(struct image_tool_params *params)
161 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
164 * info would not be NULL, because of we checked params before.
166 return info->spl_boot0;
169 static void rkcommon_set_header0(void *buf, uint file_size,
170 struct image_tool_params *params)
172 struct header0_info *hdr = buf;
174 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
175 hdr->signature = RK_SIGNATURE;
176 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
177 hdr->init_offset = RK_INIT_OFFSET;
179 hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE);
181 * The init_size has to be a multiple of 4 blocks (i.e. of 2K)
182 * or the BootROM will not boot the image.
184 * Note: To verify that this is not a legacy constraint, we
185 * rechecked this against the RK3399 BootROM.
187 hdr->init_size = ROUND(hdr->init_size, 4);
189 * init_boot_size needs to be set, as it is read by the BootROM
190 * to determine the size of the next-stage bootloader (e.g. U-Boot
191 * proper), when used with the back-to-bootrom functionality.
193 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
194 * for a more detailed explanation by Andy Yan
196 hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
198 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
201 int rkcommon_set_header(void *buf, uint file_size,
202 struct image_tool_params *params)
204 struct header1_info *hdr = buf + RK_SPL_HDR_START;
206 if (file_size > rkcommon_get_spl_size(params))
209 rkcommon_set_header0(buf, file_size, params);
211 /* Set up the SPL name (i.e. copy spl_hdr over) */
212 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
214 if (rkcommon_need_rc4_spl(params))
215 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
216 params->file_size - RK_SPL_HDR_START);
221 static inline unsigned rkcommon_offset_to_spi(unsigned offset)
224 * While SD/MMC images use a flat addressing, SPI images are padded
225 * to use the first 2K of every 4K sector only.
227 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
230 static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
231 struct spl_info **spl_info)
233 unsigned hdr1_offset;
234 struct header1_info *hdr1_sdmmc, *hdr1_spi;
241 * The first header (hdr0) is always RC4 encoded, so try to decrypt
242 * with the well-known key.
244 memcpy((void *)header0, buf, sizeof(struct header0_info));
245 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
247 if (header0->signature != RK_SIGNATURE)
250 /* We don't support RC4 encoded image payloads here, yet... */
251 if (header0->disable_rc4 == 0)
254 hdr1_offset = header0->init_offset * RK_BLK_SIZE;
255 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
256 hdr1_spi = (struct header1_info *)(buf +
257 rkcommon_offset_to_spi(hdr1_offset));
259 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
260 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
262 *spl_info = &spl_infos[i];
264 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
266 *spl_info = &spl_infos[i];
267 return IH_TYPE_RKSPI;
274 int rkcommon_verify_header(unsigned char *buf, int size,
275 struct image_tool_params *params)
277 struct header0_info header0;
278 struct spl_info *img_spl_info, *spl_info;
281 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
283 /* If this is the (unimplemented) RC4 case, then rewrite the result */
291 * If no 'imagename' is specified via the commandline (e.g. if this is
292 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
294 if (params->imagename == NULL)
297 /* Match the 'imagename' against the 'spl_hdr' found */
298 spl_info = rkcommon_get_spl_info(params->imagename);
299 if (spl_info && img_spl_info)
300 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
305 void rkcommon_print_header(const void *buf)
307 struct header0_info header0;
308 struct spl_info *spl_info;
312 ret = rkcommon_parse_header(buf, &header0, &spl_info);
314 /* If this is the (unimplemented) RC4 case, then fail silently */
319 fprintf(stderr, "Error: image verification failed\n");
325 printf("Image Type: Rockchip %s (%s) boot image\n",
327 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
328 printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
331 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
333 unsigned int remaining = size;
335 while (remaining > 0) {
336 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
338 rc4_encode(buf + offset, step, rc4_key);
339 offset += RK_BLK_SIZE;
344 int rkcommon_vrec_header(struct image_tool_params *params,
345 struct image_type_params *tparams,
346 unsigned int alignment)
348 unsigned int unpadded_size;
349 unsigned int padded_size;
352 * The SPL image looks as follows:
354 * 0x0 header0 (see rkcommon.c)
355 * 0x800 spl_name ('RK30', ..., 'RK33')
356 * (start of the payload for AArch64 payloads: we expect the
357 * first 4 bytes to be available for overwriting with our
359 * 0x804 first instruction to be executed
360 * (start of the image/payload for 32bit payloads)
362 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
363 * required for its sections (so the image we receive needs to
364 * have the first 4 bytes reserved for the spl_name). Reserving
365 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
367 * Depending on this, the header is either 0x800 (if this is a
368 * 'boot0'-style payload, which has reserved 4 bytes at the
369 * beginning for the 'spl_name' and expects us to overwrite
370 * its first 4 bytes) or 0x804 bytes in length.
372 if (rkcommon_spl_is_boot0(params))
373 tparams->header_size = RK_SPL_HDR_START;
375 tparams->header_size = RK_SPL_HDR_START + 4;
377 /* Allocate, clear and install the header */
378 tparams->hdr = malloc(tparams->header_size);
381 memset(tparams->hdr, 0, tparams->header_size);
384 * If someone passed in 0 for the alignment, we'd better handle
390 unpadded_size = tparams->header_size + params->file_size;
391 padded_size = ROUND(unpadded_size, alignment);
393 return padded_size - unpadded_size;