2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
7 * Helper functions for Rockchip images
10 #include "imagetool.h"
17 RK_SIGNATURE = 0x0ff0aa55,
21 * struct header0_info - header block for boot ROM
23 * This is stored at SD card block 64 (where each block is 512 bytes, or at
24 * the start of SPI flash. It is encoded with RC4.
26 * @signature: Signature (must be RKSD_SIGNATURE)
27 * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
28 * @init_offset: Offset in blocks of the SPL code from this header
29 * block. E.g. 4 means 2KB after the start of this header.
30 * Other fields are not used by U-Boot
37 uint8_t reserved1[492];
39 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)
58 * @spl_aarch64: Pad the header with an AArch64 'nop's to 8-bytes
62 const char *imagename;
64 const uint32_t spl_size;
66 const bool spl_aarch64;
69 static struct spl_info spl_infos[] = {
70 { "rk3036", "RK30", 0x1000, false, false },
71 { "rk3188", "RK31", 0x8000 - 0x800, true, false },
72 { "rk3288", "RK32", 0x8000, false, false },
73 { "rk3399", "RK33", 0x20000, false, true },
76 static unsigned char rc4_key[16] = {
77 124, 78, 3, 4, 85, 5, 9, 7,
78 45, 44, 123, 56, 23, 13, 23, 17
81 static struct spl_info *rkcommon_get_spl_info(char *imagename)
85 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
86 if (!strncmp(imagename, spl_infos[i].imagename, 6))
92 int rkcommon_check_params(struct image_tool_params *params)
96 if (rkcommon_get_spl_info(params->imagename) != NULL)
99 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
100 strlen(params->imagename) > 0 ? params->imagename : "NULL");
102 fprintf(stderr, "Available imagename:");
103 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
104 fprintf(stderr, "\t%s", spl_infos[i].imagename);
105 fprintf(stderr, "\n");
110 const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
112 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
115 * info would not be NULL, because of we checked params before.
117 return info->spl_hdr;
120 const bool rkcommon_get_spl_hdr_padto8(struct image_tool_params *params)
122 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
125 * info would not be NULL, because of we checked params before.
127 return info->spl_aarch64;
130 int rkcommon_get_spl_size(struct image_tool_params *params)
132 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
135 * info would not be NULL, because of we checked params before.
137 return info->spl_size;
140 bool rkcommon_need_rc4_spl(struct image_tool_params *params)
142 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
145 * info would not be NULL, because of we checked params before.
147 return info->spl_rc4;
150 static void rkcommon_set_header0(void *buf, uint file_size,
151 struct image_tool_params *params)
153 struct header0_info *hdr = buf;
155 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
156 hdr->signature = RK_SIGNATURE;
157 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
158 hdr->init_offset = RK_INIT_OFFSET;
160 hdr->init_size = (file_size + RK_BLK_SIZE - 1) / RK_BLK_SIZE;
161 hdr->init_size = (hdr->init_size + 3) & ~3;
162 hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
164 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
167 int rkcommon_set_header(void *buf, uint file_size,
168 struct image_tool_params *params)
170 struct header1_info *hdr = buf + RK_SPL_HDR_START;
172 if (file_size > rkcommon_get_spl_size(params))
175 rkcommon_set_header0(buf, file_size, params);
177 /* Set up the SPL name and add the AArch64 'nop' padding, if needed */
178 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
181 * Pad the 4-byte header to 8-bytes using an AArch64 'nop'.
182 * Note that AArch64 insns are always encoded as little-endian.
184 if (rkcommon_get_spl_hdr_padto8(params))
185 hdr->first_insn = cpu_to_le32(0xd503201f);
187 if (rkcommon_need_rc4_spl(params))
188 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
189 params->file_size - RK_SPL_HDR_START);
194 void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
196 unsigned int remaining = size;
198 while (remaining > 0) {
199 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
201 rc4_encode(buf + offset, step, rc4_key);
202 offset += RK_BLK_SIZE;
207 void rkcommon_vrec_header(struct image_tool_params *params,
208 struct image_type_params *tparams)
211 * The SPL image looks as follows:
213 * 0x0 header0 (see rkcommon.c)
214 * 0x800 spl_name ('RK30', ..., 'RK33')
215 * 0x804 first instruction to be executed
216 * (image start for AArch32, 'nop' for AArch64))
217 * 0x808 second instruction to be executed
218 * (image start for AArch64)
220 * For AArch64 (ARMv8) payloads, we receive an input file that
221 * needs to start on an 8-byte boundary (natural alignment), so
222 * we need to put a NOP at 0x804.
224 * Depending on this, the header is either 0x804 or 0x808 bytes
227 if (rkcommon_get_spl_hdr_padto8(params))
228 tparams->header_size = RK_SPL_HDR_START + 8;
230 tparams->header_size = RK_SPL_HDR_START + 4;
232 /* Allocate, clear and install the header */
233 tparams->hdr = malloc(tparams->header_size);
234 memset(tparams->hdr, 0, tparams->header_size);
235 tparams->header_size = tparams->header_size;