blob: aed2b95fd62b858ce32abe27ad13ab4a023bce30 [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)
Jeffy Chen06cdc342015-11-27 12:07:17 +080061 */
Philipp Tomsich087ce822017-03-15 12:08:43 +010062
Jeffy Chen06cdc342015-11-27 12:07:17 +080063struct spl_info {
64 const char *imagename;
65 const char *spl_hdr;
66 const uint32_t spl_size;
Heiko Stübner3b404d42017-02-18 19:46:27 +010067 const bool spl_rc4;
Jeffy Chen06cdc342015-11-27 12:07:17 +080068};
69
70static struct spl_info spl_infos[] = {
Philipp Tomsich64109702017-10-10 16:21:18 +020071 { "rk3036", "RK30", 0x1000, false },
72 { "rk3128", "RK31", 0x1800, false },
73 { "rk3188", "RK31", 0x8000 - 0x800, true },
74 { "rk322x", "RK32", 0x8000 - 0x1000, false },
75 { "rk3288", "RK32", 0x8000, false },
76 { "rk3328", "RK32", 0x8000 - 0x1000, false },
77 { "rk3368", "RK33", 0x8000 - 0x1000, false },
78 { "rk3399", "RK33", 0x30000 - 0x2000, false },
79 { "rv1108", "RK11", 0x1800, false },
Jeffy Chen06cdc342015-11-27 12:07:17 +080080};
81
Simon Glassc9c74032015-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 Chen06cdc342015-11-27 12:07:17 +080087static struct spl_info *rkcommon_get_spl_info(char *imagename)
88{
89 int i;
90
Philipp Tomsich88cc7c02017-04-17 17:48:05 +020091 if (!imagename)
92 return NULL;
93
Jeffy Chen06cdc342015-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 Tomsich88cc7c02017-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 Chen06cdc342015-11-27 12:07:17 +0800114
115 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200116 params->imagename ? params->imagename : "NULL");
Jeffy Chen06cdc342015-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 Tomsich88cc7c02017-04-17 17:48:05 +0200123 return EXIT_FAILURE;
Jeffy Chen06cdc342015-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 Tomsiche39ccb52017-03-15 12:08:45 +0100136
137int rkcommon_get_spl_size(struct image_tool_params *params)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100138{
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 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100144 return info->spl_size;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100145}
146
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100147bool rkcommon_need_rc4_spl(struct image_tool_params *params)
Jeffy Chen06cdc342015-11-27 12:07:17 +0800148{
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 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100154 return info->spl_rc4;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800155}
156
Philipp Tomsich087ce822017-03-15 12:08:43 +0100157static void rkcommon_set_header0(void *buf, uint file_size,
158 struct image_tool_params *params)
Simon Glassc9c74032015-08-30 16:55:24 -0600159{
Philipp Tomsich087ce822017-03-15 12:08:43 +0100160 struct header0_info *hdr = buf;
Simon Glassc9c74032015-08-30 16:55:24 -0600161
Philipp Tomsich087ce822017-03-15 12:08:43 +0100162 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
Simon Glassc9c74032015-08-30 16:55:24 -0600163 hdr->signature = RK_SIGNATURE;
Heiko Stübner3b404d42017-02-18 19:46:27 +0100164 hdr->disable_rc4 = !rkcommon_need_rc4_spl(params);
Jeffy Chen0e7c90a2015-11-17 14:20:30 +0800165 hdr->init_offset = RK_INIT_OFFSET;
Simon Glassc9c74032015-08-30 16:55:24 -0600166
Philipp Tomsich3efab262017-04-17 17:48:04 +0200167 hdr->init_size = DIV_ROUND_UP(file_size, RK_BLK_SIZE);
168 /*
169 * The init_size has to be a multiple of 4 blocks (i.e. of 2K)
170 * or the BootROM will not boot the image.
171 *
172 * Note: To verify that this is not a legacy constraint, we
173 * rechecked this against the RK3399 BootROM.
174 */
175 hdr->init_size = ROUND(hdr->init_size, 4);
176 /*
Philipp Tomsich3c2610a2017-05-30 23:32:10 +0200177 * init_boot_size needs to be set, as it is read by the BootROM
178 * to determine the size of the next-stage bootloader (e.g. U-Boot
179 * proper), when used with the back-to-bootrom functionality.
180 *
181 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
182 * for a more detailed explanation by Andy Yan
Philipp Tomsich3efab262017-04-17 17:48:04 +0200183 */
Philipp Tomsich3c2610a2017-05-30 23:32:10 +0200184 hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
Simon Glassc9c74032015-08-30 16:55:24 -0600185
186 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
Philipp Tomsich087ce822017-03-15 12:08:43 +0100187}
188
189int rkcommon_set_header(void *buf, uint file_size,
190 struct image_tool_params *params)
191{
192 struct header1_info *hdr = buf + RK_SPL_HDR_START;
193
194 if (file_size > rkcommon_get_spl_size(params))
195 return -ENOSPC;
196
197 rkcommon_set_header0(buf, file_size, params);
198
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200199 /* Set up the SPL name (i.e. copy spl_hdr over) */
Philipp Tomsich087ce822017-03-15 12:08:43 +0100200 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
201
Philipp Tomsich087ce822017-03-15 12:08:43 +0100202 if (rkcommon_need_rc4_spl(params))
203 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
204 params->file_size - RK_SPL_HDR_START);
Simon Glassc9c74032015-08-30 16:55:24 -0600205
206 return 0;
207}
Heiko Stübner3b404d42017-02-18 19:46:27 +0100208
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200209static inline unsigned rkcommon_offset_to_spi(unsigned offset)
210{
211 /*
212 * While SD/MMC images use a flat addressing, SPI images are padded
213 * to use the first 2K of every 4K sector only.
214 */
215 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
216}
217
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200218static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
219 struct spl_info **spl_info)
220{
221 unsigned hdr1_offset;
222 struct header1_info *hdr1_sdmmc, *hdr1_spi;
223 int i;
224
225 if (spl_info)
226 *spl_info = NULL;
227
228 /*
229 * The first header (hdr0) is always RC4 encoded, so try to decrypt
230 * with the well-known key.
231 */
232 memcpy((void *)header0, buf, sizeof(struct header0_info));
233 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
234
235 if (header0->signature != RK_SIGNATURE)
236 return -EPROTO;
237
238 /* We don't support RC4 encoded image payloads here, yet... */
239 if (header0->disable_rc4 == 0)
240 return -ENOSYS;
241
242 hdr1_offset = header0->init_offset * RK_BLK_SIZE;
243 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
244 hdr1_spi = (struct header1_info *)(buf +
245 rkcommon_offset_to_spi(hdr1_offset));
246
247 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
248 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr, 4)) {
249 if (spl_info)
250 *spl_info = &spl_infos[i];
251 return IH_TYPE_RKSD;
252 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr, 4)) {
253 if (spl_info)
254 *spl_info = &spl_infos[i];
255 return IH_TYPE_RKSPI;
256 }
257 }
258
259 return -1;
260}
261
262int rkcommon_verify_header(unsigned char *buf, int size,
263 struct image_tool_params *params)
264{
265 struct header0_info header0;
266 struct spl_info *img_spl_info, *spl_info;
267 int ret;
268
269 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
270
271 /* If this is the (unimplemented) RC4 case, then rewrite the result */
272 if (ret == -ENOSYS)
273 return 0;
274
275 if (ret < 0)
276 return ret;
277
278 /*
279 * If no 'imagename' is specified via the commandline (e.g. if this is
280 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
281 */
282 if (params->imagename == NULL)
283 return 0;
284
285 /* Match the 'imagename' against the 'spl_hdr' found */
286 spl_info = rkcommon_get_spl_info(params->imagename);
287 if (spl_info && img_spl_info)
288 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
289
290 return -ENOENT;
291}
292
293void rkcommon_print_header(const void *buf)
294{
295 struct header0_info header0;
296 struct spl_info *spl_info;
297 uint8_t image_type;
298 int ret;
299
300 ret = rkcommon_parse_header(buf, &header0, &spl_info);
301
302 /* If this is the (unimplemented) RC4 case, then fail silently */
303 if (ret == -ENOSYS)
304 return;
305
306 if (ret < 0) {
307 fprintf(stderr, "Error: image verification failed\n");
308 return;
309 }
310
311 image_type = ret;
312
313 printf("Image Type: Rockchip %s (%s) boot image\n",
314 spl_info->spl_hdr,
315 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
316 printf("Data Size: %d bytes\n", header0.init_size * RK_BLK_SIZE);
317}
318
Heiko Stübner3b404d42017-02-18 19:46:27 +0100319void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
320{
321 unsigned int remaining = size;
322
323 while (remaining > 0) {
324 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
325
326 rc4_encode(buf + offset, step, rc4_key);
327 offset += RK_BLK_SIZE;
328 remaining -= step;
329 }
330}
Philipp Tomsich087ce822017-03-15 12:08:43 +0100331
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200332int rkcommon_vrec_header(struct image_tool_params *params,
333 struct image_type_params *tparams,
334 unsigned int alignment)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100335{
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200336 unsigned int unpadded_size;
337 unsigned int padded_size;
338
Philipp Tomsich087ce822017-03-15 12:08:43 +0100339 /*
340 * The SPL image looks as follows:
341 *
342 * 0x0 header0 (see rkcommon.c)
343 * 0x800 spl_name ('RK30', ..., 'RK33')
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200344 * (start of the payload for AArch64 payloads: we expect the
345 * first 4 bytes to be available for overwriting with our
346 * spl_name)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100347 * 0x804 first instruction to be executed
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200348 * (start of the image/payload for 32bit payloads)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100349 *
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200350 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
351 * required for its sections (so the image we receive needs to
352 * have the first 4 bytes reserved for the spl_name). Reserving
353 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
Philipp Tomsich087ce822017-03-15 12:08:43 +0100354 *
Philipp Tomsich64109702017-10-10 16:21:18 +0200355 * The header is always at 0x800 (as we now use a payload
356 * prepadded using the boot0 hook for all targets): the first
357 * 4 bytes of these images can safely be overwritten using the
358 * boot magic.
Philipp Tomsich087ce822017-03-15 12:08:43 +0100359 */
Philipp Tomsich64109702017-10-10 16:21:18 +0200360 tparams->header_size = RK_SPL_HDR_START;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100361
362 /* Allocate, clear and install the header */
363 tparams->hdr = malloc(tparams->header_size);
Simon Glassa50856e2017-06-07 10:28:47 -0600364 if (!tparams->hdr)
365 return -ENOMEM;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100366 memset(tparams->hdr, 0, tparams->header_size);
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200367
368 /*
369 * If someone passed in 0 for the alignment, we'd better handle
370 * it correctly...
371 */
372 if (!alignment)
373 alignment = 1;
374
375 unpadded_size = tparams->header_size + params->file_size;
376 padded_size = ROUND(unpadded_size, alignment);
377
378 return padded_size - unpadded_size;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100379}