]>
Commit | Line | Data |
---|---|---|
a131c1f4 SG |
1 | /* |
2 | * (C) Copyright 2015 Google, Inc | |
3 | * Written by Simon Glass <sjg@chromium.org> | |
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 }, |
111bcc4f | 79 | { "rk3399", "RK33", 0x20000, false, true }, |
7bf274b9 JC |
80 | }; |
81 | ||
a131c1f4 SG |
82 | static unsigned char rc4_key[16] = { |
83 | 124, 78, 3, 4, 85, 5, 9, 7, | |
84 | 45, 44, 123, 56, 23, 13, 23, 17 | |
85 | }; | |
86 | ||
7bf274b9 JC |
87 | static struct spl_info *rkcommon_get_spl_info(char *imagename) |
88 | { | |
89 | int i; | |
90 | ||
24aae93f PT |
91 | if (!imagename) |
92 | return NULL; | |
93 | ||
7bf274b9 JC |
94 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) |
95 | if (!strncmp(imagename, spl_infos[i].imagename, 6)) | |
96 | return spl_infos + i; | |
97 | ||
98 | return NULL; | |
99 | } | |
100 | ||
101 | int rkcommon_check_params(struct image_tool_params *params) | |
102 | { | |
103 | int i; | |
104 | ||
105 | if (rkcommon_get_spl_info(params->imagename) != NULL) | |
24aae93f PT |
106 | return EXIT_SUCCESS; |
107 | ||
108 | /* | |
109 | * If this is a operation (list or extract), the don't require | |
110 | * imagename to be set. | |
111 | */ | |
112 | if (params->lflag || params->iflag) | |
113 | return EXIT_SUCCESS; | |
7bf274b9 JC |
114 | |
115 | fprintf(stderr, "ERROR: imagename (%s) is not supported!\n", | |
24aae93f | 116 | params->imagename ? params->imagename : "NULL"); |
7bf274b9 JC |
117 | |
118 | fprintf(stderr, "Available imagename:"); | |
119 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) | |
120 | fprintf(stderr, "\t%s", spl_infos[i].imagename); | |
121 | fprintf(stderr, "\n"); | |
122 | ||
24aae93f | 123 | return EXIT_FAILURE; |
7bf274b9 JC |
124 | } |
125 | ||
126 | const char *rkcommon_get_spl_hdr(struct image_tool_params *params) | |
127 | { | |
128 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
129 | ||
130 | /* | |
131 | * info would not be NULL, because of we checked params before. | |
132 | */ | |
133 | return info->spl_hdr; | |
134 | } | |
135 | ||
30827756 PT |
136 | |
137 | int rkcommon_get_spl_size(struct image_tool_params *params) | |
111bcc4f PT |
138 | { |
139 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
140 | ||
141 | /* | |
142 | * info would not be NULL, because of we checked params before. | |
143 | */ | |
30827756 | 144 | return info->spl_size; |
111bcc4f PT |
145 | } |
146 | ||
30827756 | 147 | bool rkcommon_need_rc4_spl(struct image_tool_params *params) |
7bf274b9 JC |
148 | { |
149 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
150 | ||
151 | /* | |
152 | * info would not be NULL, because of we checked params before. | |
153 | */ | |
30827756 | 154 | return info->spl_rc4; |
7bf274b9 JC |
155 | } |
156 | ||
30827756 | 157 | bool rkcommon_spl_is_boot0(struct image_tool_params *params) |
cfbcdade HS |
158 | { |
159 | struct spl_info *info = rkcommon_get_spl_info(params->imagename); | |
160 | ||
161 | /* | |
162 | * info would not be NULL, because of we checked params before. | |
163 | */ | |
30827756 | 164 | return info->spl_boot0; |
cfbcdade HS |
165 | } |
166 | ||
111bcc4f PT |
167 | static void rkcommon_set_header0(void *buf, uint file_size, |
168 | struct image_tool_params *params) | |
a131c1f4 | 169 | { |
111bcc4f | 170 | struct header0_info *hdr = buf; |
a131c1f4 | 171 | |
111bcc4f | 172 | memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE); |
a131c1f4 | 173 | hdr->signature = RK_SIGNATURE; |
cfbcdade | 174 | hdr->disable_rc4 = !rkcommon_need_rc4_spl(params); |
3641339e | 175 | hdr->init_offset = RK_INIT_OFFSET; |
a131c1f4 | 176 | |
a1a2dfb8 PT |
177 | hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE); |
178 | /* | |
179 | * The init_size has to be a multiple of 4 blocks (i.e. of 2K) | |
180 | * or the BootROM will not boot the image. | |
181 | * | |
182 | * Note: To verify that this is not a legacy constraint, we | |
183 | * rechecked this against the RK3399 BootROM. | |
184 | */ | |
185 | hdr->init_size = ROUND(hdr->init_size, 4); | |
186 | /* | |
187 | * The images we create do not contain the stage following the SPL as | |
188 | * part of the SPL image, so the init_boot_size (which might have been | |
189 | * read by Rockchip's miniloder) should be the same as the init_size. | |
190 | */ | |
191 | hdr->init_boot_size = hdr->init_size; | |
a131c1f4 SG |
192 | |
193 | rc4_encode(buf, RK_BLK_SIZE, rc4_key); | |
111bcc4f PT |
194 | } |
195 | ||
196 | int rkcommon_set_header(void *buf, uint file_size, | |
197 | struct image_tool_params *params) | |
198 | { | |
199 | struct header1_info *hdr = buf + RK_SPL_HDR_START; | |
200 | ||
201 | if (file_size > rkcommon_get_spl_size(params)) | |
202 | return -ENOSPC; | |
203 | ||
204 | rkcommon_set_header0(buf, file_size, params); | |
205 | ||
2fb371ff | 206 | /* Set up the SPL name (i.e. copy spl_hdr over) */ |
111bcc4f PT |
207 | memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE); |
208 | ||
111bcc4f PT |
209 | if (rkcommon_need_rc4_spl(params)) |
210 | rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START, | |
211 | params->file_size - RK_SPL_HDR_START); | |
a131c1f4 SG |
212 | |
213 | return 0; | |
214 | } | |
cfbcdade | 215 | |
2fb371ff PT |
216 | static inline unsigned rkcommon_offset_to_spi(unsigned offset) |
217 | { | |
218 | /* | |
219 | * While SD/MMC images use a flat addressing, SPI images are padded | |
220 | * to use the first 2K of every 4K sector only. | |
221 | */ | |
222 | return ((offset & ~0x7ff) << 1) + (offset & 0x7ff); | |
223 | } | |
224 | ||
225 | static inline unsigned rkcommon_spi_to_offset(unsigned offset) | |
226 | { | |
227 | return ((offset & ~0x7ff) >> 1) + (offset & 0x7ff); | |
228 | } | |
229 | ||
230 | static int rkcommon_parse_header(const void *buf, struct header0_info *header0, | |
231 | struct spl_info **spl_info) | |
232 | { | |
233 | unsigned hdr1_offset; | |
234 | struct header1_info *hdr1_sdmmc, *hdr1_spi; | |
235 | int i; | |
236 | ||
237 | if (spl_info) | |
238 | *spl_info = NULL; | |
239 | ||
240 | /* | |
241 | * The first header (hdr0) is always RC4 encoded, so try to decrypt | |
242 | * with the well-known key. | |
243 | */ | |
244 | memcpy((void *)header0, buf, sizeof(struct header0_info)); | |
245 | rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key); | |
246 | ||
247 | if (header0->signature != RK_SIGNATURE) | |
248 | return -EPROTO; | |
249 | ||
250 | /* We don't support RC4 encoded image payloads here, yet... */ | |
251 | if (header0->disable_rc4 == 0) | |
252 | return -ENOSYS; | |
253 | ||
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)); | |
258 | ||
259 | for (i = 0; i < ARRAY_SIZE(spl_infos); i++) { | |
260 | if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) { | |
261 | if (spl_info) | |
262 | *spl_info = &spl_infos[i]; | |
263 | return IH_TYPE_RKSD; | |
264 | } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) { | |
265 | if (spl_info) | |
266 | *spl_info = &spl_infos[i]; | |
267 | return IH_TYPE_RKSPI; | |
268 | } | |
269 | } | |
270 | ||
271 | return -1; | |
272 | } | |
273 | ||
274 | int rkcommon_verify_header(unsigned char *buf, int size, | |
275 | struct image_tool_params *params) | |
276 | { | |
277 | struct header0_info header0; | |
278 | struct spl_info *img_spl_info, *spl_info; | |
279 | int ret; | |
280 | ||
281 | ret = rkcommon_parse_header(buf, &header0, &img_spl_info); | |
282 | ||
283 | /* If this is the (unimplemented) RC4 case, then rewrite the result */ | |
284 | if (ret == -ENOSYS) | |
285 | return 0; | |
286 | ||
287 | if (ret < 0) | |
288 | return ret; | |
289 | ||
290 | /* | |
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. | |
293 | */ | |
294 | if (params->imagename == NULL) | |
295 | return 0; | |
296 | ||
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); | |
301 | ||
302 | return -ENOENT; | |
303 | } | |
304 | ||
305 | void rkcommon_print_header(const void *buf) | |
306 | { | |
307 | struct header0_info header0; | |
308 | struct spl_info *spl_info; | |
309 | uint8_t image_type; | |
310 | int ret; | |
311 | ||
312 | ret = rkcommon_parse_header(buf, &header0, &spl_info); | |
313 | ||
314 | /* If this is the (unimplemented) RC4 case, then fail silently */ | |
315 | if (ret == -ENOSYS) | |
316 | return; | |
317 | ||
318 | if (ret < 0) { | |
319 | fprintf(stderr, "Error: image verification failed\n"); | |
320 | return; | |
321 | } | |
322 | ||
323 | image_type = ret; | |
324 | ||
325 | printf("Image Type: Rockchip %s (%s) boot image\n", | |
326 | spl_info->spl_hdr, | |
327 | (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI"); | |
328 | printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE); | |
329 | } | |
330 | ||
cfbcdade HS |
331 | void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size) |
332 | { | |
333 | unsigned int remaining = size; | |
334 | ||
335 | while (remaining > 0) { | |
336 | int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining; | |
337 | ||
338 | rc4_encode(buf + offset, step, rc4_key); | |
339 | offset += RK_BLK_SIZE; | |
340 | remaining -= step; | |
341 | } | |
342 | } | |
111bcc4f | 343 | |
366aad4d PT |
344 | int rkcommon_vrec_header(struct image_tool_params *params, |
345 | struct image_type_params *tparams, | |
346 | unsigned int alignment) | |
111bcc4f | 347 | { |
366aad4d PT |
348 | unsigned int unpadded_size; |
349 | unsigned int padded_size; | |
350 | ||
111bcc4f PT |
351 | /* |
352 | * The SPL image looks as follows: | |
353 | * | |
354 | * 0x0 header0 (see rkcommon.c) | |
355 | * 0x800 spl_name ('RK30', ..., 'RK33') | |
ea3729e2 PT |
356 | * (start of the payload for AArch64 payloads: we expect the |
357 | * first 4 bytes to be available for overwriting with our | |
358 | * spl_name) | |
111bcc4f | 359 | * 0x804 first instruction to be executed |
ea3729e2 | 360 | * (start of the image/payload for 32bit payloads) |
111bcc4f | 361 | * |
ea3729e2 PT |
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. | |
111bcc4f | 366 | * |
ea3729e2 PT |
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. | |
111bcc4f | 371 | */ |
30827756 PT |
372 | if (rkcommon_spl_is_boot0(params)) |
373 | tparams->header_size = RK_SPL_HDR_START; | |
111bcc4f PT |
374 | else |
375 | tparams->header_size = RK_SPL_HDR_START + 4; | |
376 | ||
377 | /* Allocate, clear and install the header */ | |
378 | tparams->hdr = malloc(tparams->header_size); | |
379 | memset(tparams->hdr, 0, tparams->header_size); | |
366aad4d PT |
380 | tparams->header_size = tparams->header_size; |
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 | } |