blob: 04e8272fd5b63b870186bd73a31e4256ee38352f [file] [log] [blame]
Simon Glassc9c74032015-08-30 16:55:24 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +02005 * (C) 2017 Theobroma Systems Design und Consulting GmbH
6 *
Simon Glassc9c74032015-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 Tomsich3efab262017-04-17 17:48:04 +020018#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
19
Simon Glassc9c74032015-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 Chen0e7c90a2015-11-17 14:20:30 +080032 * @init_offset: Offset in blocks of the SPL code from this header
Simon Glassc9c74032015-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 Chen0e7c90a2015-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 Glassc9c74032015-08-30 16:55:24 -060044 uint8_t reserved2[2];
45};
46
Jeffy Chen06cdc342015-11-27 12:07:17 +080047/**
Philipp Tomsich087ce822017-03-15 12:08:43 +010048 * struct header1_info
49 */
50struct header1_info {
51 uint32_t magic;
Philipp Tomsich087ce822017-03-15 12:08:43 +010052};
53
54/**
Jeffy Chen06cdc342015-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übner3b404d42017-02-18 19:46:27 +010060 * @spl_rc4: RC4 encode the SPL binary (same key as header)
Philipp Tomsiche39ccb52017-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 Chen06cdc342015-11-27 12:07:17 +080064 */
Philipp Tomsich087ce822017-03-15 12:08:43 +010065
Jeffy Chen06cdc342015-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übner3b404d42017-02-18 19:46:27 +010070 const bool spl_rc4;
Philipp Tomsiche39ccb52017-03-15 12:08:45 +010071 const bool spl_boot0;
Jeffy Chen06cdc342015-11-27 12:07:17 +080072};
73
74static struct spl_info spl_infos[] = {
Philipp Tomsich087ce822017-03-15 12:08:43 +010075 { "rk3036", "RK30", 0x1000, false, false },
76 { "rk3188", "RK31", 0x8000 - 0x800, true, false },
Kever Yang6c7f02b2017-06-23 17:17:48 +080077 { "rk322x", "RK32", 0x8000 - 0x1000, false, false },
Philipp Tomsich087ce822017-03-15 12:08:43 +010078 { "rk3288", "RK32", 0x8000, false, false },
Kever Yang71ef9262017-04-14 14:55:05 +080079 { "rk3328", "RK32", 0x8000 - 0x1000, false, false },
Philipp Tomsiche9ec8682017-07-13 00:59:06 +020080 { "rk3368", "RK33", 0x8000 - 0x1000, false, true },
Kever Yang5df223c2017-06-14 14:54:20 +080081 { "rk3399", "RK33", 0x30000 - 0x2000, false, true },
Andy Yanb8a14562017-06-01 17:58:31 +080082 { "rv1108", "RK11", 0x1800, false, false},
Jeffy Chen06cdc342015-11-27 12:07:17 +080083};
84
Simon Glassc9c74032015-08-30 16:55:24 -060085static unsigned char rc4_key[16] = {
86 124, 78, 3, 4, 85, 5, 9, 7,
87 45, 44, 123, 56, 23, 13, 23, 17
88};
89
Jeffy Chen06cdc342015-11-27 12:07:17 +080090static struct spl_info *rkcommon_get_spl_info(char *imagename)
91{
92 int i;
93
Philipp Tomsich88cc7c02017-04-17 17:48:05 +020094 if (!imagename)
95 return NULL;
96
Jeffy Chen06cdc342015-11-27 12:07:17 +080097 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
98 if (!strncmp(imagename, spl_infos[i].imagename, 6))
99 return spl_infos + i;
100
101 return NULL;
102}
103
104int rkcommon_check_params(struct image_tool_params *params)
105{
106 int i;
107
108 if (rkcommon_get_spl_info(params->imagename) != NULL)
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200109 return EXIT_SUCCESS;
110
111 /*
112 * If this is a operation (list or extract), the don't require
113 * imagename to be set.
114 */
115 if (params->lflag || params->iflag)
116 return EXIT_SUCCESS;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800117
118 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200119 params->imagename ? params->imagename : "NULL");
Jeffy Chen06cdc342015-11-27 12:07:17 +0800120
121 fprintf(stderr, "Available imagename:");
122 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
123 fprintf(stderr, "\t%s", spl_infos[i].imagename);
124 fprintf(stderr, "\n");
125
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200126 return EXIT_FAILURE;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800127}
128
129const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
130{
131 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
132
133 /*
134 * info would not be NULL, because of we checked params before.
135 */
136 return info->spl_hdr;
137}
138
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100139
140int rkcommon_get_spl_size(struct image_tool_params *params)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100141{
142 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
143
144 /*
145 * info would not be NULL, because of we checked params before.
146 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100147 return info->spl_size;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100148}
149
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100150bool rkcommon_need_rc4_spl(struct image_tool_params *params)
Jeffy Chen06cdc342015-11-27 12:07:17 +0800151{
152 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
153
154 /*
155 * info would not be NULL, because of we checked params before.
156 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100157 return info->spl_rc4;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800158}
159
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100160bool rkcommon_spl_is_boot0(struct image_tool_params *params)
Heiko Stübner3b404d42017-02-18 19:46:27 +0100161{
162 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
163
164 /*
165 * info would not be NULL, because of we checked params before.
166 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100167 return info->spl_boot0;
Heiko Stübner3b404d42017-02-18 19:46:27 +0100168}
169
Philipp Tomsich087ce822017-03-15 12:08:43 +0100170static void rkcommon_set_header0(void *buf, uint file_size,
171 struct image_tool_params *params)
Simon Glassc9c74032015-08-30 16:55:24 -0600172{
Philipp Tomsich087ce822017-03-15 12:08:43 +0100173 struct header0_info *hdr = buf;
Simon Glassc9c74032015-08-30 16:55:24 -0600174
Philipp Tomsich087ce822017-03-15 12:08:43 +0100175 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
Simon Glassc9c74032015-08-30 16:55:24 -0600176 hdr->signature = RK_SIGNATURE;
Heiko Stübner3b404d42017-02-18 19:46:27 +0100177 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
Jeffy Chen0e7c90a2015-11-17 14:20:30 +0800178 hdr->init_offset = RK_INIT_OFFSET;
Simon Glassc9c74032015-08-30 16:55:24 -0600179
Philipp Tomsich3efab262017-04-17 17:48:04 +0200180 hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE);
181 /*
182 * The init_size has to be a multiple of 4 blocks (i.e. of 2K)
183 * or the BootROM will not boot the image.
184 *
185 * Note: To verify that this is not a legacy constraint, we
186 * rechecked this against the RK3399 BootROM.
187 */
188 hdr->init_size = ROUND(hdr->init_size, 4);
189 /*
Philipp Tomsich3c2610a2017-05-30 23:32:10 +0200190 * init_boot_size needs to be set, as it is read by the BootROM
191 * to determine the size of the next-stage bootloader (e.g. U-Boot
192 * proper), when used with the back-to-bootrom functionality.
193 *
194 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
195 * for a more detailed explanation by Andy Yan
Philipp Tomsich3efab262017-04-17 17:48:04 +0200196 */
Philipp Tomsich3c2610a2017-05-30 23:32:10 +0200197 hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
Simon Glassc9c74032015-08-30 16:55:24 -0600198
199 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
Philipp Tomsich087ce822017-03-15 12:08:43 +0100200}
201
202int rkcommon_set_header(void *buf, uint file_size,
203 struct image_tool_params *params)
204{
205 struct header1_info *hdr = buf + RK_SPL_HDR_START;
206
207 if (file_size > rkcommon_get_spl_size(params))
208 return -ENOSPC;
209
210 rkcommon_set_header0(buf, file_size, params);
211
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200212 /* Set up the SPL name (i.e. copy spl_hdr over) */
Philipp Tomsich087ce822017-03-15 12:08:43 +0100213 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
214
Philipp Tomsich087ce822017-03-15 12:08:43 +0100215 if (rkcommon_need_rc4_spl(params))
216 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
217 params->file_size - RK_SPL_HDR_START);
Simon Glassc9c74032015-08-30 16:55:24 -0600218
219 return 0;
220}
Heiko Stübner3b404d42017-02-18 19:46:27 +0100221
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200222static inline unsigned rkcommon_offset_to_spi(unsigned offset)
223{
224 /*
225 * While SD/MMC images use a flat addressing, SPI images are padded
226 * to use the first 2K of every 4K sector only.
227 */
228 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
229}
230
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200231static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
232 struct spl_info **spl_info)
233{
234 unsigned hdr1_offset;
235 struct header1_info *hdr1_sdmmc, *hdr1_spi;
236 int i;
237
238 if (spl_info)
239 *spl_info = NULL;
240
241 /*
242 * The first header (hdr0) is always RC4 encoded, so try to decrypt
243 * with the well-known key.
244 */
245 memcpy((void *)header0, buf, sizeof(struct header0_info));
246 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
247
248 if (header0->signature != RK_SIGNATURE)
249 return -EPROTO;
250
251 /* We don't support RC4 encoded image payloads here, yet... */
252 if (header0->disable_rc4 == 0)
253 return -ENOSYS;
254
255 hdr1_offset = header0->init_offset * RK_BLK_SIZE;
256 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
257 hdr1_spi = (struct header1_info *)(buf +
258 rkcommon_offset_to_spi(hdr1_offset));
259
260 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
261 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
262 if (spl_info)
263 *spl_info = &spl_infos[i];
264 return IH_TYPE_RKSD;
265 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
266 if (spl_info)
267 *spl_info = &spl_infos[i];
268 return IH_TYPE_RKSPI;
269 }
270 }
271
272 return -1;
273}
274
275int rkcommon_verify_header(unsigned char *buf, int size,
276 struct image_tool_params *params)
277{
278 struct header0_info header0;
279 struct spl_info *img_spl_info, *spl_info;
280 int ret;
281
282 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
283
284 /* If this is the (unimplemented) RC4 case, then rewrite the result */
285 if (ret == -ENOSYS)
286 return 0;
287
288 if (ret < 0)
289 return ret;
290
291 /*
292 * If no 'imagename' is specified via the commandline (e.g. if this is
293 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
294 */
295 if (params->imagename == NULL)
296 return 0;
297
298 /* Match the 'imagename' against the 'spl_hdr' found */
299 spl_info = rkcommon_get_spl_info(params->imagename);
300 if (spl_info && img_spl_info)
301 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
302
303 return -ENOENT;
304}
305
306void rkcommon_print_header(const void *buf)
307{
308 struct header0_info header0;
309 struct spl_info *spl_info;
310 uint8_t image_type;
311 int ret;
312
313 ret = rkcommon_parse_header(buf, &header0, &spl_info);
314
315 /* If this is the (unimplemented) RC4 case, then fail silently */
316 if (ret == -ENOSYS)
317 return;
318
319 if (ret < 0) {
320 fprintf(stderr, "Error: image verification failed\n");
321 return;
322 }
323
324 image_type = ret;
325
326 printf("Image Type: Rockchip %s (%s) boot image\n",
327 spl_info->spl_hdr,
328 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
329 printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
330}
331
Heiko Stübner3b404d42017-02-18 19:46:27 +0100332void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
333{
334 unsigned int remaining = size;
335
336 while (remaining > 0) {
337 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
338
339 rc4_encode(buf + offset, step, rc4_key);
340 offset += RK_BLK_SIZE;
341 remaining -= step;
342 }
343}
Philipp Tomsich087ce822017-03-15 12:08:43 +0100344
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200345int rkcommon_vrec_header(struct image_tool_params *params,
346 struct image_type_params *tparams,
347 unsigned int alignment)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100348{
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200349 unsigned int unpadded_size;
350 unsigned int padded_size;
351
Philipp Tomsich087ce822017-03-15 12:08:43 +0100352 /*
353 * The SPL image looks as follows:
354 *
355 * 0x0 header0 (see rkcommon.c)
356 * 0x800 spl_name ('RK30', ..., 'RK33')
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200357 * (start of the payload for AArch64 payloads: we expect the
358 * first 4 bytes to be available for overwriting with our
359 * spl_name)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100360 * 0x804 first instruction to be executed
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200361 * (start of the image/payload for 32bit payloads)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100362 *
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200363 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
364 * required for its sections (so the image we receive needs to
365 * have the first 4 bytes reserved for the spl_name). Reserving
366 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
Philipp Tomsich087ce822017-03-15 12:08:43 +0100367 *
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200368 * Depending on this, the header is either 0x800 (if this is a
369 * 'boot0'-style payload, which has reserved 4 bytes at the
370 * beginning for the 'spl_name' and expects us to overwrite
371 * its first 4 bytes) or 0x804 bytes in length.
Philipp Tomsich087ce822017-03-15 12:08:43 +0100372 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100373 if (rkcommon_spl_is_boot0(params))
374 tparams->header_size = RK_SPL_HDR_START;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100375 else
376 tparams->header_size = RK_SPL_HDR_START + 4;
377
378 /* Allocate, clear and install the header */
379 tparams->hdr = malloc(tparams->header_size);
Simon Glassa50856e2017-06-07 10:28:47 -0600380 if (!tparams->hdr)
381 return -ENOMEM;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100382 memset(tparams->hdr, 0, tparams->header_size);
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200383
384 /*
385 * If someone passed in 0 for the alignment, we'd better handle
386 * it correctly...
387 */
388 if (!alignment)
389 alignment = 1;
390
391 unpadded_size = tparams->header_size + params->file_size;
392 padded_size = ROUND(unpadded_size, alignment);
393
394 return padded_size - unpadded_size;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100395}