]>
Commit | Line | Data |
---|---|---|
a131c1f4 SG |
1 | /* |
2 | * (C) Copyright 2015 Google, Inc | |
3 | * Written by Simon Glass <[email protected]> | |
4 | * | |
2fb371ff PT |
5 | * (C) 2017 Theobroma Systems Design und Consulting GmbH |
6 | * | |
a131c1f4 SG |
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 <rc4.h> | |
15 | #include "mkimage.h" | |
16 | #include "rkcommon.h" | |
17 | ||
a1a2dfb8 PT |
18 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
19 | ||
a131c1f4 SG |
20 | enum { |
21 | RK_SIGNATURE = 0x0ff0aa55, | |
22 | }; | |
23 | ||
24 | /** | |
25 | * struct header0_info - header block for boot ROM | |
26 | * | |
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. | |
29 | * | |
30 | * @signature: Signature (must be RKSD_SIGNATURE) | |
31 | * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary | |
3641339e | 32 | * @init_offset: Offset in blocks of the SPL code from this header |
a131c1f4 SG |
33 | * block. E.g. 4 means 2KB after the start of this header. |
34 | * Other fields are not used by U-Boot | |
35 | */ | |
36 | struct header0_info { | |
37 | uint32_t signature; | |
38 | uint8_t reserved[4]; | |
39 | uint32_t disable_rc4; | |
3641339e JC |
40 | uint16_t init_offset; |
41 | uint8_t reserved1[492]; | |
42 | uint16_t init_size; | |
43 | uint16_t init_boot_size; | |
a131c1f4 SG |
44 | uint8_t reserved2[2]; |
45 | }; | |
46 | ||
111bcc4f PT |
47 | /** |
48 | * struct header1_info | |
49 | */ | |
50 | struct header1_info { | |
51 | uint32_t magic; | |
111bcc4f PT |
52 | }; |
53 | ||
7bf274b9 JC |
54 | /** |
55 | * struct spl_info - spl info for each chip | |
56 | * | |
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) | |
cfbcdade | 60 | * @spl_rc4: RC4 encode the SPL binary (same key as header) |
30827756 PT |
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 | |
63 | * word. | |
7bf274b9 | 64 | */ |
111bcc4f | 65 | |
7bf274b9 JC |
66 | struct spl_info { |
67 | const char *imagename; | |
68 | const char *spl_hdr; | |
69 | const uint32_t spl_size; | |
cfbcdade | 70 | const bool spl_rc4; |
30827756 | 71 | const bool spl_boot0; |
7bf274b9 JC |
72 | }; |
73 | ||
74 | static struct spl_info spl_infos[] = { | |
111bcc4f PT |
75 | { "rk3036", "RK30", 0x1000, false, false }, |
76 | { "rk3188", "RK31", 0x8000 - 0x800, true, false }, | |
77 | { "rk3288", "RK32", 0x8000, false, false }, | |
68d12600 | 78 | { "rk3328", "RK32", 0x8000 - 0x1000, false, false }, |
915e0981 | 79 | { "rk3399", "RK33", 0x30000 - 0x2000, false, true }, |
79fa1573 | 80 | { "rv1108", "RK11", 0x1800, false, false}, |
7bf274b9 JC |
81 | }; |
82 | ||
a131c1f4 SG |
83 | static unsigned char rc4_key[16] = { |
84 | 124, 78, 3, 4, 85, 5, 9, 7, | |
85 | 45, 44, 123, 56, 23, 13, 23, 17 | |
86 | }; | |
87 | ||
7bf274b9 JC |
88 | static struct spl_info *rkcommon_get_spl_info(char *imagename) |
89 | { | |
90 | int i; | |
91 | ||
24aae93f PT |
92 | if (!imagename) |
93 | return NULL; | |
94 | ||
7bf274b9 JC |
95 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) |
96 | if (!strncmp(imagename, spl_infos[i].imagename, 6)) | |
97 | return spl_infos + i; | |
98 | ||
99 | return NULL; | |
100 | } | |
101 | ||
102 | int rkcommon_check_params(struct image_tool_params *params) | |
103 | { | |
104 | int i; | |
105 | ||
106 | if (rkcommon_get_spl_info(params->imagename) != NULL) | |
24aae93f PT |
107 | return EXIT_SUCCESS; |
108 | ||
109 | /* | |
110 | * If this is a operation (list or extract), the don't require | |
111 | * imagename to be set. | |
112 | */ | |
113 | if (params->lflag || params->iflag) | |
114 | return EXIT_SUCCESS; | |
7bf274b9 JC |
115 | |
116 | fprintf(stderr, "ERROR: imagename (%s) is not supported!\n", | |
24aae93f | 117 | params->imagename ? params->imagename : "NULL"); |
7bf274b9 JC |
118 | |
119 | fprintf(stderr, "Available imagename:"); | |
120 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) | |
121 | fprintf(stderr, "\t%s", spl_infos[i].imagename); | |
122 | fprintf(stderr, "\n"); | |
123 | ||
24aae93f | 124 | return EXIT_FAILURE; |
7bf274b9 JC |
125 | } |
126 | ||
127 | const char *rkcommon_get_spl_hdr(struct image_tool_params *params) | |
128 | { | |
129 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
130 | ||
131 | /* | |
132 | * info would not be NULL, because of we checked params before. | |
133 | */ | |
134 | return info->spl_hdr; | |
135 | } | |
136 | ||
30827756 PT |
137 | |
138 | int rkcommon_get_spl_size(struct image_tool_params *params) | |
111bcc4f PT |
139 | { |
140 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
141 | ||
142 | /* | |
143 | * info would not be NULL, because of we checked params before. | |
144 | */ | |
30827756 | 145 | return info->spl_size; |
111bcc4f PT |
146 | } |
147 | ||
30827756 | 148 | bool rkcommon_need_rc4_spl(struct image_tool_params *params) |
7bf274b9 JC |
149 | { |
150 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
151 | ||
152 | /* | |
153 | * info would not be NULL, because of we checked params before. | |
154 | */ | |
30827756 | 155 | return info->spl_rc4; |
7bf274b9 JC |
156 | } |
157 | ||
30827756 | 158 | bool rkcommon_spl_is_boot0(struct image_tool_params *params) |
cfbcdade HS |
159 | { |
160 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
161 | ||
162 | /* | |
163 | * info would not be NULL, because of we checked params before. | |
164 | */ | |
30827756 | 165 | return info->spl_boot0; |
cfbcdade HS |
166 | } |
167 | ||
111bcc4f PT |
168 | static void rkcommon_set_header0(void *buf, uint file_size, |
169 | struct image_tool_params *params) | |
a131c1f4 | 170 | { |
111bcc4f | 171 | struct header0_info *hdr = buf; |
a131c1f4 | 172 | |
111bcc4f | 173 | memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE); |
a131c1f4 | 174 | hdr->signature = RK_SIGNATURE; |
cfbcdade | 175 | hdr->disable_rc4 = !rkcommon_need_rc4_spl(params); |
3641339e | 176 | hdr->init_offset = RK_INIT_OFFSET; |
a131c1f4 | 177 | |
a1a2dfb8 PT |
178 | hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE); |
179 | /* | |
180 | * The init_size has to be a multiple of 4 blocks (i.e. of 2K) | |
181 | * or the BootROM will not boot the image. | |
182 | * | |
183 | * Note: To verify that this is not a legacy constraint, we | |
184 | * rechecked this against the RK3399 BootROM. | |
185 | */ | |
186 | hdr->init_size = ROUND(hdr->init_size, 4); | |
187 | /* | |
a1c29d4b PT |
188 | * init_boot_size needs to be set, as it is read by the BootROM |
189 | * to determine the size of the next-stage bootloader (e.g. U-Boot | |
190 | * proper), when used with the back-to-bootrom functionality. | |
191 | * | |
192 | * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html | |
193 | * for a more detailed explanation by Andy Yan | |
a1a2dfb8 | 194 | */ |
a1c29d4b | 195 | hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE; |
a131c1f4 SG |
196 | |
197 | rc4_encode(buf, RK_BLK_SIZE, rc4_key); | |
111bcc4f PT |
198 | } |
199 | ||
200 | int rkcommon_set_header(void *buf, uint file_size, | |
201 | struct image_tool_params *params) | |
202 | { | |
203 | struct header1_info *hdr = buf + RK_SPL_HDR_START; | |
204 | ||
205 | if (file_size > rkcommon_get_spl_size(params)) | |
206 | return -ENOSPC; | |
207 | ||
208 | rkcommon_set_header0(buf, file_size, params); | |
209 | ||
2fb371ff | 210 | /* Set up the SPL name (i.e. copy spl_hdr over) */ |
111bcc4f PT |
211 | memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE); |
212 | ||
111bcc4f PT |
213 | if (rkcommon_need_rc4_spl(params)) |
214 | rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START, | |
215 | params->file_size - RK_SPL_HDR_START); | |
a131c1f4 SG |
216 | |
217 | return 0; | |
218 | } | |
cfbcdade | 219 | |
2fb371ff PT |
220 | static inline unsigned rkcommon_offset_to_spi(unsigned offset) |
221 | { | |
222 | /* | |
223 | * While SD/MMC images use a flat addressing, SPI images are padded | |
224 | * to use the first 2K of every 4K sector only. | |
225 | */ | |
226 | return ((offset & ~0x7ff) << 1) + (offset & 0x7ff); | |
227 | } | |
228 | ||
2fb371ff PT |
229 | static int rkcommon_parse_header(const void *buf, struct header0_info *header0, |
230 | struct spl_info **spl_info) | |
231 | { | |
232 | unsigned hdr1_offset; | |
233 | struct header1_info *hdr1_sdmmc, *hdr1_spi; | |
234 | int i; | |
235 | ||
236 | if (spl_info) | |
237 | *spl_info = NULL; | |
238 | ||
239 | /* | |
240 | * The first header (hdr0) is always RC4 encoded, so try to decrypt | |
241 | * with the well-known key. | |
242 | */ | |
243 | memcpy((void *)header0, buf, sizeof(struct header0_info)); | |
244 | rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key); | |
245 | ||
246 | if (header0->signature != RK_SIGNATURE) | |
247 | return -EPROTO; | |
248 | ||
249 | /* We don't support RC4 encoded image payloads here, yet... */ | |
250 | if (header0->disable_rc4 == 0) | |
251 | return -ENOSYS; | |
252 | ||
253 | hdr1_offset = header0->init_offset * RK_BLK_SIZE; | |
254 | hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset); | |
255 | hdr1_spi = (struct header1_info *)(buf + | |
256 | rkcommon_offset_to_spi(hdr1_offset)); | |
257 | ||
258 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) { | |
259 | if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) { | |
260 | if (spl_info) | |
261 | *spl_info = &spl_infos[i]; | |
262 | return IH_TYPE_RKSD; | |
263 | } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) { | |
264 | if (spl_info) | |
265 | *spl_info = &spl_infos[i]; | |
266 | return IH_TYPE_RKSPI; | |
267 | } | |
268 | } | |
269 | ||
270 | return -1; | |
271 | } | |
272 | ||
273 | int rkcommon_verify_header(unsigned char *buf, int size, | |
274 | struct image_tool_params *params) | |
275 | { | |
276 | struct header0_info header0; | |
277 | struct spl_info *img_spl_info, *spl_info; | |
278 | int ret; | |
279 | ||
280 | ret = rkcommon_parse_header(buf, &header0, &img_spl_info); | |
281 | ||
282 | /* If this is the (unimplemented) RC4 case, then rewrite the result */ | |
283 | if (ret == -ENOSYS) | |
284 | return 0; | |
285 | ||
286 | if (ret < 0) | |
287 | return ret; | |
288 | ||
289 | /* | |
290 | * If no 'imagename' is specified via the commandline (e.g. if this is | |
291 | * 'dumpimage -l' w/o any further constraints), we accept any spl_info. | |
292 | */ | |
293 | if (params->imagename == NULL) | |
294 | return 0; | |
295 | ||
296 | /* Match the 'imagename' against the 'spl_hdr' found */ | |
297 | spl_info = rkcommon_get_spl_info(params->imagename); | |
298 | if (spl_info && img_spl_info) | |
299 | return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr); | |
300 | ||
301 | return -ENOENT; | |
302 | } | |
303 | ||
304 | void rkcommon_print_header(const void *buf) | |
305 | { | |
306 | struct header0_info header0; | |
307 | struct spl_info *spl_info; | |
308 | uint8_t image_type; | |
309 | int ret; | |
310 | ||
311 | ret = rkcommon_parse_header(buf, &header0, &spl_info); | |
312 | ||
313 | /* If this is the (unimplemented) RC4 case, then fail silently */ | |
314 | if (ret == -ENOSYS) | |
315 | return; | |
316 | ||
317 | if (ret < 0) { | |
318 | fprintf(stderr, "Error: image verification failed\n"); | |
319 | return; | |
320 | } | |
321 | ||
322 | image_type = ret; | |
323 | ||
324 | printf("Image Type: Rockchip %s (%s) boot image\n", | |
325 | spl_info->spl_hdr, | |
326 | (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI"); | |
327 | printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE); | |
328 | } | |
329 | ||
cfbcdade HS |
330 | void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size) |
331 | { | |
332 | unsigned int remaining = size; | |
333 | ||
334 | while (remaining > 0) { | |
335 | int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining; | |
336 | ||
337 | rc4_encode(buf + offset, step, rc4_key); | |
338 | offset += RK_BLK_SIZE; | |
339 | remaining -= step; | |
340 | } | |
341 | } | |
111bcc4f | 342 | |
366aad4d PT |
343 | int rkcommon_vrec_header(struct image_tool_params *params, |
344 | struct image_type_params *tparams, | |
345 | unsigned int alignment) | |
111bcc4f | 346 | { |
366aad4d PT |
347 | unsigned int unpadded_size; |
348 | unsigned int padded_size; | |
349 | ||
111bcc4f PT |
350 | /* |
351 | * The SPL image looks as follows: | |
352 | * | |
353 | * 0x0 header0 (see rkcommon.c) | |
354 | * 0x800 spl_name ('RK30', ..., 'RK33') | |
ea3729e2 PT |
355 | * (start of the payload for AArch64 payloads: we expect the |
356 | * first 4 bytes to be available for overwriting with our | |
357 | * spl_name) | |
111bcc4f | 358 | * 0x804 first instruction to be executed |
ea3729e2 | 359 | * (start of the image/payload for 32bit payloads) |
111bcc4f | 360 | * |
ea3729e2 PT |
361 | * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is |
362 | * required for its sections (so the image we receive needs to | |
363 | * have the first 4 bytes reserved for the spl_name). Reserving | |
364 | * these 4 bytes is done using the BOOT0_HOOK infrastructure. | |
111bcc4f | 365 | * |
ea3729e2 PT |
366 | * Depending on this, the header is either 0x800 (if this is a |
367 | * 'boot0'-style payload, which has reserved 4 bytes at the | |
368 | * beginning for the 'spl_name' and expects us to overwrite | |
369 | * its first 4 bytes) or 0x804 bytes in length. | |
111bcc4f | 370 | */ |
30827756 PT |
371 | if (rkcommon_spl_is_boot0(params)) |
372 | tparams->header_size = RK_SPL_HDR_START; | |
111bcc4f PT |
373 | else |
374 | tparams->header_size = RK_SPL_HDR_START + 4; | |
375 | ||
376 | /* Allocate, clear and install the header */ | |
377 | tparams->hdr = malloc(tparams->header_size); | |
22929bec SG |
378 | if (!tparams->hdr) |
379 | return -ENOMEM; | |
111bcc4f | 380 | memset(tparams->hdr, 0, tparams->header_size); |
366aad4d PT |
381 | |
382 | /* | |
383 | * If someone passed in 0 for the alignment, we'd better handle | |
384 | * it correctly... | |
385 | */ | |
386 | if (!alignment) | |
387 | alignment = 1; | |
388 | ||
389 | unpadded_size = tparams->header_size + params->file_size; | |
390 | padded_size = ROUND(unpadded_size, alignment); | |
391 | ||
392 | return padded_size - unpadded_size; | |
111bcc4f | 393 | } |