blob: 42547aa9d18e020fa9605736d5821c62ebacc159 [file] [log] [blame]
Simon Glassa131c1f2015-08-30 16:55:24 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
Philipp Tomsich2fb371f2017-05-30 23:32:08 +02005 * (C) 2017 Theobroma Systems Design und Consulting GmbH
6 *
Simon Glassa131c1f2015-08-30 16:55:24 -06007 * 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
Philipp Tomsicha1a2dfb2017-04-17 17:48:04 +020018#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
19
Simon Glassa131c1f2015-08-30 16:55:24 -060020enum {
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
Jeffy Chen36413392015-11-17 14:20:30 +080032 * @init_offset: Offset in blocks of the SPL code from this header
Simon Glassa131c1f2015-08-30 16:55:24 -060033 * block. E.g. 4 means 2KB after the start of this header.
34 * Other fields are not used by U-Boot
35 */
36struct header0_info {
37 uint32_t signature;
38 uint8_t reserved[4];
39 uint32_t disable_rc4;
Jeffy Chen36413392015-11-17 14:20:30 +080040 uint16_t init_offset;
41 uint8_t reserved1[492];
42 uint16_t init_size;
43 uint16_t init_boot_size;
Simon Glassa131c1f2015-08-30 16:55:24 -060044 uint8_t reserved2[2];
45};
46
Jeffy Chen7bf274b2015-11-27 12:07:17 +080047/**
Philipp Tomsich111bcc42017-03-15 12:08:43 +010048 * struct header1_info
49 */
50struct header1_info {
51 uint32_t magic;
Philipp Tomsich111bcc42017-03-15 12:08:43 +010052};
53
54/**
Jeffy Chen7bf274b2015-11-27 12:07:17 +080055 * 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)
Heiko Stübnercfbcdad2017-02-18 19:46:27 +010060 * @spl_rc4: RC4 encode the SPL binary (same key as header)
Philipp Tomsich30827752017-03-15 12:08:45 +010061 * @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.
Jeffy Chen7bf274b2015-11-27 12:07:17 +080064 */
Philipp Tomsich111bcc42017-03-15 12:08:43 +010065
Jeffy Chen7bf274b2015-11-27 12:07:17 +080066struct spl_info {
67 const char *imagename;
68 const char *spl_hdr;
69 const uint32_t spl_size;
Heiko Stübnercfbcdad2017-02-18 19:46:27 +010070 const bool spl_rc4;
Philipp Tomsich30827752017-03-15 12:08:45 +010071 const bool spl_boot0;
Jeffy Chen7bf274b2015-11-27 12:07:17 +080072};
73
74static struct spl_info spl_infos[] = {
Philipp Tomsich111bcc42017-03-15 12:08:43 +010075 { "rk3036", "RK30", 0x1000, false, false },
76 { "rk3188", "RK31", 0x8000 - 0x800, true, false },
77 { "rk3288", "RK32", 0x8000, false, false },
Kever Yang68d12602017-04-14 14:55:05 +080078 { "rk3328", "RK32", 0x8000 - 0x1000, false, false },
Philipp Tomsich111bcc42017-03-15 12:08:43 +010079 { "rk3399", "RK33", 0x20000, false, true },
Jeffy Chen7bf274b2015-11-27 12:07:17 +080080};
81
Simon Glassa131c1f2015-08-30 16:55:24 -060082static 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
Jeffy Chen7bf274b2015-11-27 12:07:17 +080087static struct spl_info *rkcommon_get_spl_info(char *imagename)
88{
89 int i;
90
Philipp Tomsich24aae932017-04-17 17:48:05 +020091 if (!imagename)
92 return NULL;
93
Jeffy Chen7bf274b2015-11-27 12:07:17 +080094 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
101int rkcommon_check_params(struct image_tool_params *params)
102{
103 int i;
104
105 if (rkcommon_get_spl_info(params->imagename) != NULL)
Philipp Tomsich24aae932017-04-17 17:48:05 +0200106 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;
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800114
115 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
Philipp Tomsich24aae932017-04-17 17:48:05 +0200116 params->imagename ? params->imagename : "NULL");
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800117
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
Philipp Tomsich24aae932017-04-17 17:48:05 +0200123 return EXIT_FAILURE;
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800124}
125
126const 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
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100136
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800137int rkcommon_get_spl_size(struct image_tool_params *params)
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 */
144 return info->spl_size;
145}
146
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100147bool rkcommon_need_rc4_spl(struct image_tool_params *params)
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 */
154 return info->spl_rc4;
155}
156
Philipp Tomsich30827752017-03-15 12:08:45 +0100157bool rkcommon_spl_is_boot0(struct image_tool_params *params)
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 */
164 return info->spl_boot0;
165}
166
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100167static void rkcommon_set_header0(void *buf, uint file_size,
168 struct image_tool_params *params)
Simon Glassa131c1f2015-08-30 16:55:24 -0600169{
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100170 struct header0_info *hdr = buf;
Simon Glassa131c1f2015-08-30 16:55:24 -0600171
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100172 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
Simon Glassa131c1f2015-08-30 16:55:24 -0600173 hdr->signature = RK_SIGNATURE;
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100174 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
Jeffy Chen36413392015-11-17 14:20:30 +0800175 hdr->init_offset = RK_INIT_OFFSET;
Simon Glassa131c1f2015-08-30 16:55:24 -0600176
Philipp Tomsicha1a2dfb2017-04-17 17:48:04 +0200177 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 /*
Philipp Tomsicha1c29d42017-05-30 23:32:10 +0200187 * init_boot_size needs to be set, as it is read by the BootROM
188 * to determine the size of the next-stage bootloader (e.g. U-Boot
189 * proper), when used with the back-to-bootrom functionality.
190 *
191 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
192 * for a more detailed explanation by Andy Yan
Philipp Tomsicha1a2dfb2017-04-17 17:48:04 +0200193 */
Philipp Tomsicha1c29d42017-05-30 23:32:10 +0200194 hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
Simon Glassa131c1f2015-08-30 16:55:24 -0600195
196 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100197}
198
199int rkcommon_set_header(void *buf, uint file_size,
200 struct image_tool_params *params)
201{
202 struct header1_info *hdr = buf + RK_SPL_HDR_START;
203
204 if (file_size > rkcommon_get_spl_size(params))
205 return -ENOSPC;
206
207 rkcommon_set_header0(buf, file_size, params);
208
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200209 /* Set up the SPL name (i.e. copy spl_hdr over) */
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100210 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
211
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100212 if (rkcommon_need_rc4_spl(params))
213 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
214 params->file_size - RK_SPL_HDR_START);
Simon Glassa131c1f2015-08-30 16:55:24 -0600215
216 return 0;
217}
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100218
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200219static inline unsigned rkcommon_offset_to_spi(unsigned offset)
220{
221 /*
222 * While SD/MMC images use a flat addressing, SPI images are padded
223 * to use the first 2K of every 4K sector only.
224 */
225 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
226}
227
228static inline unsigned rkcommon_spi_to_offset(unsigned offset)
229{
230 return ((offset & ~0x7ff) >> 1) + (offset & 0x7ff);
231}
232
233static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
234 struct spl_info **spl_info)
235{
236 unsigned hdr1_offset;
237 struct header1_info *hdr1_sdmmc, *hdr1_spi;
238 int i;
239
240 if (spl_info)
241 *spl_info = NULL;
242
243 /*
244 * The first header (hdr0) is always RC4 encoded, so try to decrypt
245 * with the well-known key.
246 */
247 memcpy((void *)header0, buf, sizeof(struct header0_info));
248 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
249
250 if (header0->signature != RK_SIGNATURE)
251 return -EPROTO;
252
253 /* We don't support RC4 encoded image payloads here, yet... */
254 if (header0->disable_rc4 == 0)
255 return -ENOSYS;
256
257 hdr1_offset = header0->init_offset * RK_BLK_SIZE;
258 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
259 hdr1_spi = (struct header1_info *)(buf +
260 rkcommon_offset_to_spi(hdr1_offset));
261
262 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
263 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
264 if (spl_info)
265 *spl_info = &spl_infos[i];
266 return IH_TYPE_RKSD;
267 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
268 if (spl_info)
269 *spl_info = &spl_infos[i];
270 return IH_TYPE_RKSPI;
271 }
272 }
273
274 return -1;
275}
276
277int rkcommon_verify_header(unsigned char *buf, int size,
278 struct image_tool_params *params)
279{
280 struct header0_info header0;
281 struct spl_info *img_spl_info, *spl_info;
282 int ret;
283
284 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
285
286 /* If this is the (unimplemented) RC4 case, then rewrite the result */
287 if (ret == -ENOSYS)
288 return 0;
289
290 if (ret < 0)
291 return ret;
292
293 /*
294 * If no 'imagename' is specified via the commandline (e.g. if this is
295 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
296 */
297 if (params->imagename == NULL)
298 return 0;
299
300 /* Match the 'imagename' against the 'spl_hdr' found */
301 spl_info = rkcommon_get_spl_info(params->imagename);
302 if (spl_info && img_spl_info)
303 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
304
305 return -ENOENT;
306}
307
308void rkcommon_print_header(const void *buf)
309{
310 struct header0_info header0;
311 struct spl_info *spl_info;
312 uint8_t image_type;
313 int ret;
314
315 ret = rkcommon_parse_header(buf, &header0, &spl_info);
316
317 /* If this is the (unimplemented) RC4 case, then fail silently */
318 if (ret == -ENOSYS)
319 return;
320
321 if (ret < 0) {
322 fprintf(stderr, "Error: image verification failed\n");
323 return;
324 }
325
326 image_type = ret;
327
328 printf("Image Type: Rockchip %s (%s) boot image\n",
329 spl_info->spl_hdr,
330 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
331 printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
332}
333
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100334void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
335{
336 unsigned int remaining = size;
337
338 while (remaining > 0) {
339 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
340
341 rc4_encode(buf + offset, step, rc4_key);
342 offset += RK_BLK_SIZE;
343 remaining -= step;
344 }
345}
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100346
Philipp Tomsich366aad42017-04-17 17:48:01 +0200347int rkcommon_vrec_header(struct image_tool_params *params,
348 struct image_type_params *tparams,
349 unsigned int alignment)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100350{
Philipp Tomsich366aad42017-04-17 17:48:01 +0200351 unsigned int unpadded_size;
352 unsigned int padded_size;
353
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100354 /*
355 * The SPL image looks as follows:
356 *
357 * 0x0 header0 (see rkcommon.c)
358 * 0x800 spl_name ('RK30', ..., 'RK33')
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200359 * (start of the payload for AArch64 payloads: we expect the
360 * first 4 bytes to be available for overwriting with our
361 * spl_name)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100362 * 0x804 first instruction to be executed
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200363 * (start of the image/payload for 32bit payloads)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100364 *
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200365 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
366 * required for its sections (so the image we receive needs to
367 * have the first 4 bytes reserved for the spl_name). Reserving
368 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100369 *
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200370 * Depending on this, the header is either 0x800 (if this is a
371 * 'boot0'-style payload, which has reserved 4 bytes at the
372 * beginning for the 'spl_name' and expects us to overwrite
373 * its first 4 bytes) or 0x804 bytes in length.
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100374 */
Philipp Tomsich30827752017-03-15 12:08:45 +0100375 if (rkcommon_spl_is_boot0(params))
376 tparams->header_size = RK_SPL_HDR_START;
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100377 else
378 tparams->header_size = RK_SPL_HDR_START + 4;
379
380 /* Allocate, clear and install the header */
381 tparams->hdr = malloc(tparams->header_size);
382 memset(tparams->hdr, 0, tparams->header_size);
Philipp Tomsich366aad42017-04-17 17:48:01 +0200383 tparams->header_size = tparams->header_size;
384
385 /*
386 * If someone passed in 0 for the alignment, we'd better handle
387 * it correctly...
388 */
389 if (!alignment)
390 alignment = 1;
391
392 unpadded_size = tparams->header_size + params->file_size;
393 padded_size = ROUND(unpadded_size, alignment);
394
395 return padded_size - unpadded_size;
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100396}