blob: 1b37ff304778ebd0e57001403d7c693198c227fe [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa131c1f2015-08-30 16:55:24 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 *
Philipp Tomsich2fb371f2017-05-30 23:32:08 +02006 * (C) 2017 Theobroma Systems Design und Consulting GmbH
7 *
Simon Glassa131c1f2015-08-30 16:55:24 -06008 * Helper functions for Rockchip images
9 */
10
11#include "imagetool.h"
12#include <image.h>
Yi Liu8935e522021-05-18 10:13:40 +080013#include <u-boot/sha256.h>
Simon Glassa131c1f2015-08-30 16:55:24 -060014#include <rc4.h>
15#include "mkimage.h"
16#include "rkcommon.h"
17
18enum {
Kever Yangd7a44612021-12-24 17:58:32 +080019 RK_MAGIC = 0x0ff0aa55,
Yi Liu8935e522021-05-18 10:13:40 +080020 RK_MAGIC_V2 = 0x534E4B52,
Simon Glassa131c1f2015-08-30 16:55:24 -060021};
22
Kever Yang0faa7da2021-12-24 18:00:36 +080023enum {
24 RK_HEADER_V1 = 1,
Yi Liu8935e522021-05-18 10:13:40 +080025 RK_HEADER_V2 = 2,
26};
27
28enum hash_type {
29 HASH_NONE = 0,
30 HASH_SHA256 = 1,
31 HASH_SHA512 = 2,
32};
33
34/**
35 * struct image_entry
36 *
37 * @size_and_off: [31:16]image size;[15:0]image offset
38 * @address: default as 0xFFFFFFFF
39 * @flag: no use
40 * @counter: no use
41 * @hash: hash of image
42 *
43 */
44struct image_entry {
45 uint32_t size_and_off;
46 uint32_t address;
47 uint32_t flag;
48 uint32_t counter;
49 uint8_t reserved[8];
50 uint8_t hash[64];
51};
52
53/**
54 * struct header0_info_v2 - v2 header block for rockchip BootRom
55 *
56 * This is stored at SD card block 64 (where each block is 512 bytes)
57 *
58 * @magic: Magic (must be RK_MAGIC_V2)
59 * @size_and_nimage: [31:16]number of images;[15:0]
60 * offset to hash field of header(unit as 4Byte)
61 * @boot_flag: [3:0]hash type(0:none,1:sha256,2:sha512)
62 * @signature: hash or signature for header info
63 *
64 */
65struct header0_info_v2 {
66 uint32_t magic;
67 uint8_t reserved[4];
68 uint32_t size_and_nimage;
69 uint32_t boot_flag;
70 uint8_t reserved1[104];
71 struct image_entry images[4];
72 uint8_t reserved2[1064];
73 uint8_t hash[512];
Kever Yang0faa7da2021-12-24 18:00:36 +080074};
75
Simon Glassa131c1f2015-08-30 16:55:24 -060076/**
77 * struct header0_info - header block for boot ROM
78 *
79 * This is stored at SD card block 64 (where each block is 512 bytes, or at
80 * the start of SPI flash. It is encoded with RC4.
81 *
Kever Yangd7a44612021-12-24 17:58:32 +080082 * @magic: Magic (must be RK_MAGIC)
Simon Glassa131c1f2015-08-30 16:55:24 -060083 * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
Jeffy Chen36413392015-11-17 14:20:30 +080084 * @init_offset: Offset in blocks of the SPL code from this header
Simon Glassa131c1f2015-08-30 16:55:24 -060085 * block. E.g. 4 means 2KB after the start of this header.
86 * Other fields are not used by U-Boot
87 */
88struct header0_info {
Kever Yangd7a44612021-12-24 17:58:32 +080089 uint32_t magic;
Simon Glassa131c1f2015-08-30 16:55:24 -060090 uint8_t reserved[4];
91 uint32_t disable_rc4;
Jeffy Chen36413392015-11-17 14:20:30 +080092 uint16_t init_offset;
93 uint8_t reserved1[492];
94 uint16_t init_size;
95 uint16_t init_boot_size;
Simon Glassa131c1f2015-08-30 16:55:24 -060096 uint8_t reserved2[2];
97};
98
Jeffy Chen7bf274b2015-11-27 12:07:17 +080099/**
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100100 * struct header1_info
101 */
102struct header1_info {
103 uint32_t magic;
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100104};
105
106/**
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800107 * struct spl_info - spl info for each chip
108 *
109 * @imagename: Image name(passed by "mkimage -n")
110 * @spl_hdr: Boot ROM requires a 4-bytes spl header
111 * @spl_size: Spl size(include extra 4-bytes spl header)
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100112 * @spl_rc4: RC4 encode the SPL binary (same key as header)
Kever Yang0faa7da2021-12-24 18:00:36 +0800113 * @header_ver: header block version
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800114 */
115struct spl_info {
116 const char *imagename;
117 const char *spl_hdr;
118 const uint32_t spl_size;
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100119 const bool spl_rc4;
Kever Yang0faa7da2021-12-24 18:00:36 +0800120 const uint32_t header_ver;
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800121};
122
123static struct spl_info spl_infos[] = {
Kever Yang0faa7da2021-12-24 18:00:36 +0800124 { "px30", "RK33", 0x2800, false, RK_HEADER_V1 },
125 { "rk3036", "RK30", 0x1000, false, RK_HEADER_V1 },
126 { "rk3128", "RK31", 0x1800, false, RK_HEADER_V1 },
127 { "rk3188", "RK31", 0x8000 - 0x800, true, RK_HEADER_V1 },
128 { "rk322x", "RK32", 0x8000 - 0x1000, false, RK_HEADER_V1 },
129 { "rk3288", "RK32", 0x8000, false, RK_HEADER_V1 },
130 { "rk3308", "RK33", 0x40000 - 0x1000, false, RK_HEADER_V1 },
131 { "rk3328", "RK32", 0x8000 - 0x1000, false, RK_HEADER_V1 },
132 { "rk3368", "RK33", 0x8000 - 0x1000, false, RK_HEADER_V1 },
133 { "rk3399", "RK33", 0x30000 - 0x2000, false, RK_HEADER_V1 },
134 { "rv1108", "RK11", 0x1800, false, RK_HEADER_V1 },
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800135};
136
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800137/**
138 * struct spl_params - spl params parsed in check_params()
139 *
140 * @init_file: Init data file path
141 * @init_size: Aligned size of init data in bytes
142 * @boot_file: Boot data file path
143 * @boot_size: Aligned size of boot data in bytes
144 */
145
146struct spl_params {
147 char *init_file;
148 uint32_t init_size;
149 char *boot_file;
150 uint32_t boot_size;
151};
152
153static struct spl_params spl_params = { 0 };
154
Simon Glassa131c1f2015-08-30 16:55:24 -0600155static unsigned char rc4_key[16] = {
156 124, 78, 3, 4, 85, 5, 9, 7,
157 45, 44, 123, 56, 23, 13, 23, 17
158};
159
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800160static struct spl_info *rkcommon_get_spl_info(char *imagename)
161{
162 int i;
163
Philipp Tomsich24aae932017-04-17 17:48:05 +0200164 if (!imagename)
165 return NULL;
166
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800167 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
168 if (!strncmp(imagename, spl_infos[i].imagename, 6))
169 return spl_infos + i;
170
171 return NULL;
172}
173
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800174static int rkcommon_get_aligned_size(struct image_tool_params *params,
175 const char *fname)
176{
177 int size;
178
179 size = imagetool_get_filesize(params, fname);
180 if (size < 0)
181 return -1;
182
183 /*
184 * Pad to a 2KB alignment, as required for init/boot size by the ROM
185 * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html)
186 */
187 return ROUND(size, RK_SIZE_ALIGN);
188}
189
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800190int rkcommon_check_params(struct image_tool_params *params)
191{
Heinrich Schuchardt69cd0c42020-05-09 21:31:03 +0200192 int i, size;
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800193
Philipp Tomsich24aae932017-04-17 17:48:05 +0200194 /*
195 * If this is a operation (list or extract), the don't require
196 * imagename to be set.
197 */
198 if (params->lflag || params->iflag)
199 return EXIT_SUCCESS;
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800200
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800201 if (!rkcommon_get_spl_info(params->imagename))
202 goto err_spl_info;
203
204 spl_params.init_file = params->datafile;
205
206 spl_params.boot_file = strchr(spl_params.init_file, ':');
207 if (spl_params.boot_file) {
208 *spl_params.boot_file = '\0';
209 spl_params.boot_file += 1;
210 }
211
Heinrich Schuchardt69cd0c42020-05-09 21:31:03 +0200212 size = rkcommon_get_aligned_size(params, spl_params.init_file);
213 if (size < 0)
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800214 return EXIT_FAILURE;
Heinrich Schuchardt69cd0c42020-05-09 21:31:03 +0200215 spl_params.init_size = size;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800216
217 /* Boot file is optional, and only for back-to-bootrom functionality. */
218 if (spl_params.boot_file) {
Heinrich Schuchardt69cd0c42020-05-09 21:31:03 +0200219 size = rkcommon_get_aligned_size(params, spl_params.boot_file);
220 if (size < 0)
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800221 return EXIT_FAILURE;
Heinrich Schuchardt69cd0c42020-05-09 21:31:03 +0200222 spl_params.boot_size = size;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800223 }
224
225 if (spl_params.init_size > rkcommon_get_spl_size(params)) {
226 fprintf(stderr,
227 "Error: SPL image is too large (size %#x than %#x)\n",
228 spl_params.init_size, rkcommon_get_spl_size(params));
229 return EXIT_FAILURE;
230 }
231
232 return EXIT_SUCCESS;
233
234err_spl_info:
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800235 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
Philipp Tomsich24aae932017-04-17 17:48:05 +0200236 params->imagename ? params->imagename : "NULL");
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800237
238 fprintf(stderr, "Available imagename:");
239 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
240 fprintf(stderr, "\t%s", spl_infos[i].imagename);
241 fprintf(stderr, "\n");
242
Philipp Tomsich24aae932017-04-17 17:48:05 +0200243 return EXIT_FAILURE;
Jeffy Chen7bf274b2015-11-27 12:07:17 +0800244}
245
246const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
247{
248 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
249
250 /*
251 * info would not be NULL, because of we checked params before.
252 */
253 return info->spl_hdr;
254}
255
256int rkcommon_get_spl_size(struct image_tool_params *params)
257{
258 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
259
260 /*
261 * info would not be NULL, because of we checked params before.
262 */
263 return info->spl_size;
264}
265
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100266bool rkcommon_need_rc4_spl(struct image_tool_params *params)
267{
268 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
269
270 /*
271 * info would not be NULL, because of we checked params before.
272 */
273 return info->spl_rc4;
274}
275
Yi Liu8935e522021-05-18 10:13:40 +0800276bool rkcommon_is_header_v2(struct image_tool_params *params)
277{
278 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
279
280 return (info->header_ver == RK_HEADER_V2);
281}
282
283static void do_sha256_hash(uint8_t *buf, uint32_t size, uint8_t *out)
284{
285 sha256_context ctx;
286
287 sha256_starts(&ctx);
288 sha256_update(&ctx, buf, size);
289 sha256_finish(&ctx, out);
290}
291
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800292static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
Simon Glassa131c1f2015-08-30 16:55:24 -0600293{
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100294 struct header0_info *hdr = buf;
Samuel Holland29ef48e2020-10-24 11:43:17 -0500295 uint32_t init_boot_size;
Simon Glassa131c1f2015-08-30 16:55:24 -0600296
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100297 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
Kever Yangd7a44612021-12-24 17:58:32 +0800298 hdr->magic = cpu_to_le32(RK_MAGIC);
Samuel Holland29ef48e2020-10-24 11:43:17 -0500299 hdr->disable_rc4 = cpu_to_le32(!rkcommon_need_rc4_spl(params));
300 hdr->init_offset = cpu_to_le16(RK_INIT_OFFSET);
301 hdr->init_size = cpu_to_le16(spl_params.init_size / RK_BLK_SIZE);
Simon Glassa131c1f2015-08-30 16:55:24 -0600302
Philipp Tomsicha1a2dfb2017-04-17 17:48:04 +0200303 /*
Philipp Tomsicha1c29d42017-05-30 23:32:10 +0200304 * init_boot_size needs to be set, as it is read by the BootROM
305 * to determine the size of the next-stage bootloader (e.g. U-Boot
306 * proper), when used with the back-to-bootrom functionality.
307 *
308 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
309 * for a more detailed explanation by Andy Yan
Philipp Tomsicha1a2dfb2017-04-17 17:48:04 +0200310 */
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800311 if (spl_params.boot_file)
Samuel Holland29ef48e2020-10-24 11:43:17 -0500312 init_boot_size = spl_params.init_size + spl_params.boot_size;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800313 else
Samuel Holland29ef48e2020-10-24 11:43:17 -0500314 init_boot_size = spl_params.init_size + RK_MAX_BOOT_SIZE;
315 hdr->init_boot_size = cpu_to_le16(init_boot_size / RK_BLK_SIZE);
Simon Glassa131c1f2015-08-30 16:55:24 -0600316
317 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100318}
319
Yi Liu8935e522021-05-18 10:13:40 +0800320static void rkcommon_set_header0_v2(void *buf, struct image_tool_params *params)
321{
322 struct header0_info_v2 *hdr = buf;
323 uint32_t sector_offset, image_sector_count;
324 uint32_t image_size_array[2];
325 uint8_t *image_ptr = NULL;
326 int i;
327
328 printf("Image Type: Rockchip %s boot image\n",
329 rkcommon_get_spl_hdr(params));
330 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
331 hdr->magic = cpu_to_le32(RK_MAGIC_V2);
332 hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
333 hdr->boot_flag = cpu_to_le32(HASH_SHA256);
334 sector_offset = 4;
335 image_size_array[0] = spl_params.init_size;
336 image_size_array[1] = spl_params.boot_size;
337
338 for (i = 0; i < 2; i++) {
339 image_sector_count = image_size_array[i] / RK_BLK_SIZE;
340 hdr->images[i].size_and_off = cpu_to_le32((image_sector_count
341 << 16) + sector_offset);
342 hdr->images[i].address = 0xFFFFFFFF;
343 hdr->images[i].counter = cpu_to_le32(i + 1);
344 image_ptr = buf + sector_offset * RK_BLK_SIZE;
345 do_sha256_hash(image_ptr, image_size_array[i],
346 hdr->images[i].hash);
347 sector_offset = sector_offset + image_sector_count;
348 }
349
350 do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
351}
352
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800353void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
354 struct image_tool_params *params)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100355{
356 struct header1_info *hdr = buf + RK_SPL_HDR_START;
357
Yi Liu8935e522021-05-18 10:13:40 +0800358 if (rkcommon_is_header_v2(params)) {
359 rkcommon_set_header0_v2(buf, params);
360 } else {
361 rkcommon_set_header0(buf, params);
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100362
Yi Liu8935e522021-05-18 10:13:40 +0800363 /* Set up the SPL name (i.e. copy spl_hdr over) */
364 if (memcmp(&hdr->magic, "RSAK", 4))
365 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100366
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800367 if (rkcommon_need_rc4_spl(params))
Yi Liu8935e522021-05-18 10:13:40 +0800368 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
369 spl_params.init_size);
370
371 if (spl_params.boot_file) {
372 if (rkcommon_need_rc4_spl(params))
373 rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
374 spl_params.init_size,
375 spl_params.boot_size);
376 }
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800377 }
Simon Glassa131c1f2015-08-30 16:55:24 -0600378}
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100379
Yi Liu8935e522021-05-18 10:13:40 +0800380static inline unsigned int rkcommon_offset_to_spi(unsigned int offset)
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200381{
382 /*
383 * While SD/MMC images use a flat addressing, SPI images are padded
384 * to use the first 2K of every 4K sector only.
385 */
386 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
387}
388
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200389static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
390 struct spl_info **spl_info)
391{
Yi Liu8935e522021-05-18 10:13:40 +0800392 unsigned int hdr1_offset;
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200393 struct header1_info *hdr1_sdmmc, *hdr1_spi;
394 int i;
395
396 if (spl_info)
397 *spl_info = NULL;
398
399 /*
400 * The first header (hdr0) is always RC4 encoded, so try to decrypt
401 * with the well-known key.
402 */
403 memcpy((void *)header0, buf, sizeof(struct header0_info));
404 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
405
Kever Yangd7a44612021-12-24 17:58:32 +0800406 if (le32_to_cpu(header0->magic) != RK_MAGIC)
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200407 return -EPROTO;
408
409 /* We don't support RC4 encoded image payloads here, yet... */
Samuel Holland29ef48e2020-10-24 11:43:17 -0500410 if (le32_to_cpu(header0->disable_rc4) == 0)
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200411 return -ENOSYS;
412
Samuel Holland29ef48e2020-10-24 11:43:17 -0500413 hdr1_offset = le16_to_cpu(header0->init_offset) * RK_BLK_SIZE;
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200414 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
415 hdr1_spi = (struct header1_info *)(buf +
416 rkcommon_offset_to_spi(hdr1_offset));
417
418 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
Miquel Raynale5a40552020-03-18 17:22:55 +0100419 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr,
420 RK_SPL_HDR_SIZE)) {
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200421 if (spl_info)
422 *spl_info = &spl_infos[i];
423 return IH_TYPE_RKSD;
Miquel Raynale5a40552020-03-18 17:22:55 +0100424 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr,
425 RK_SPL_HDR_SIZE)) {
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200426 if (spl_info)
427 *spl_info = &spl_infos[i];
428 return IH_TYPE_RKSPI;
429 }
430 }
431
432 return -1;
433}
434
Yi Liu8935e522021-05-18 10:13:40 +0800435static int rkcommon_parse_header_v2(const void *buf, struct header0_info_v2 *header)
436{
437 memcpy((void *)header, buf, sizeof(struct header0_info_v2));
438
439 if (le32_to_cpu(header->magic) != RK_MAGIC_V2)
440 return -EPROTO;
441
442 return 0;
443}
444
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200445int rkcommon_verify_header(unsigned char *buf, int size,
446 struct image_tool_params *params)
447{
448 struct header0_info header0;
449 struct spl_info *img_spl_info, *spl_info;
450 int ret;
451
452 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
453
454 /* If this is the (unimplemented) RC4 case, then rewrite the result */
455 if (ret == -ENOSYS)
456 return 0;
457
458 if (ret < 0)
459 return ret;
460
461 /*
462 * If no 'imagename' is specified via the commandline (e.g. if this is
463 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
464 */
465 if (params->imagename == NULL)
466 return 0;
467
468 /* Match the 'imagename' against the 'spl_hdr' found */
469 spl_info = rkcommon_get_spl_info(params->imagename);
470 if (spl_info && img_spl_info)
471 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
472
473 return -ENOENT;
474}
475
476void rkcommon_print_header(const void *buf)
477{
478 struct header0_info header0;
Yi Liu8935e522021-05-18 10:13:40 +0800479 struct header0_info_v2 header0_v2;
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200480 struct spl_info *spl_info;
481 uint8_t image_type;
Samuel Holland29ef48e2020-10-24 11:43:17 -0500482 int ret, boot_size, init_size;
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200483
Yi Liu8935e522021-05-18 10:13:40 +0800484 if ((*(uint32_t *)buf) == RK_MAGIC_V2) {
485 ret = rkcommon_parse_header_v2(buf, &header0_v2);
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200486
Yi Liu8935e522021-05-18 10:13:40 +0800487 if (ret < 0) {
488 fprintf(stderr, "Error: image verification failed\n");
489 return;
490 }
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200491
Yi Liu8935e522021-05-18 10:13:40 +0800492 init_size = header0_v2.images[0].size_and_off >> 16;
493 init_size = init_size * RK_BLK_SIZE;
494 boot_size = header0_v2.images[1].size_and_off >> 16;
495 boot_size = boot_size * RK_BLK_SIZE;
496 } else {
497 ret = rkcommon_parse_header(buf, &header0, &spl_info);
498
499 /* If this is the (unimplemented) RC4 case, then fail silently */
500 if (ret == -ENOSYS)
501 return;
502
503 if (ret < 0) {
504 fprintf(stderr, "Error: image verification failed\n");
505 return;
506 }
507
508 image_type = ret;
509 init_size = header0.init_size * RK_BLK_SIZE;
510 boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
511
512 printf("Image Type: Rockchip %s (%s) boot image\n",
513 spl_info->spl_hdr,
514 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200515 }
516
Samuel Holland29ef48e2020-10-24 11:43:17 -0500517 printf("Init Data Size: %d bytes\n", init_size);
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800518
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800519 if (boot_size != RK_MAX_BOOT_SIZE)
520 printf("Boot Data Size: %d bytes\n", boot_size);
Philipp Tomsich2fb371f2017-05-30 23:32:08 +0200521}
522
Heiko Stübnercfbcdad2017-02-18 19:46:27 +0100523void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
524{
525 unsigned int remaining = size;
526
527 while (remaining > 0) {
528 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
529
530 rc4_encode(buf + offset, step, rc4_key);
531 offset += RK_BLK_SIZE;
532 remaining -= step;
533 }
534}
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100535
Philipp Tomsich366aad42017-04-17 17:48:01 +0200536int rkcommon_vrec_header(struct image_tool_params *params,
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800537 struct image_type_params *tparams)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100538{
539 /*
540 * The SPL image looks as follows:
541 *
542 * 0x0 header0 (see rkcommon.c)
543 * 0x800 spl_name ('RK30', ..., 'RK33')
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200544 * (start of the payload for AArch64 payloads: we expect the
545 * first 4 bytes to be available for overwriting with our
546 * spl_name)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100547 * 0x804 first instruction to be executed
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200548 * (start of the image/payload for 32bit payloads)
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100549 *
Philipp Tomsichea3729e2017-04-17 17:48:02 +0200550 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
551 * required for its sections (so the image we receive needs to
552 * have the first 4 bytes reserved for the spl_name). Reserving
553 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100554 *
Philipp Tomsich95d363c2017-10-10 16:21:18 +0200555 * The header is always at 0x800 (as we now use a payload
556 * prepadded using the boot0 hook for all targets): the first
557 * 4 bytes of these images can safely be overwritten using the
558 * boot magic.
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100559 */
Philipp Tomsich95d363c2017-10-10 16:21:18 +0200560 tparams->header_size = RK_SPL_HDR_START;
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100561
562 /* Allocate, clear and install the header */
563 tparams->hdr = malloc(tparams->header_size);
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800564 if (!tparams->hdr) {
565 fprintf(stderr, "%s: Can't alloc header: %s\n",
566 params->cmdname, strerror(errno));
567 exit(EXIT_FAILURE);
568 }
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100569 memset(tparams->hdr, 0, tparams->header_size);
Philipp Tomsich366aad42017-04-17 17:48:01 +0200570
571 /*
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800572 * We need to store the original file-size (i.e. before padding), as
573 * imagetool does not set this during its adjustment of file_size.
Philipp Tomsich366aad42017-04-17 17:48:01 +0200574 */
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800575 params->orig_file_size = tparams->header_size +
576 spl_params.init_size + spl_params.boot_size;
Philipp Tomsich366aad42017-04-17 17:48:01 +0200577
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800578 params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
Philipp Tomsich366aad42017-04-17 17:48:01 +0200579
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800580 /* Ignoring pad len, since we are using our own copy_image() */
581 return 0;
582}
583
584static int pad_file(struct image_tool_params *params, int ifd, int pad)
585{
586 uint8_t zeros[4096];
587
588 memset(zeros, 0, sizeof(zeros));
589
590 while (pad > 0) {
591 int todo = sizeof(zeros);
592
593 if (todo > pad)
594 todo = pad;
595 if (write(ifd, (char *)&zeros, todo) != todo) {
596 fprintf(stderr, "%s: Write error on %s: %s\n",
597 params->cmdname, params->imagefile,
598 strerror(errno));
599 return -1;
600 }
601 pad -= todo;
602 }
603
604 return 0;
605}
606
607static int copy_file(struct image_tool_params *params, int ifd,
608 const char *file, int padded_size)
609{
610 int dfd;
611 struct stat sbuf;
612 unsigned char *ptr;
613 int size;
614
615 if (params->vflag)
616 fprintf(stderr, "Adding Image %s\n", file);
617
618 dfd = open(file, O_RDONLY | O_BINARY);
619 if (dfd < 0) {
620 fprintf(stderr, "%s: Can't open %s: %s\n",
621 params->cmdname, file, strerror(errno));
622 return -1;
623 }
624
625 if (fstat(dfd, &sbuf) < 0) {
626 fprintf(stderr, "%s: Can't stat %s: %s\n",
627 params->cmdname, file, strerror(errno));
628 goto err_close;
629 }
630
631 if (params->vflag)
632 fprintf(stderr, "Size %u(pad to %u)\n",
633 (int)sbuf.st_size, padded_size);
634
635 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
636 if (ptr == MAP_FAILED) {
637 fprintf(stderr, "%s: Can't read %s: %s\n",
638 params->cmdname, file, strerror(errno));
639 goto err_munmap;
640 }
641
642 size = sbuf.st_size;
643 if (write(ifd, ptr, size) != size) {
644 fprintf(stderr, "%s: Write error on %s: %s\n",
645 params->cmdname, params->imagefile, strerror(errno));
646 goto err_munmap;
647 }
648
649 munmap((void *)ptr, sbuf.st_size);
650 close(dfd);
651 return pad_file(params, ifd, padded_size - size);
652
653err_munmap:
654 munmap((void *)ptr, sbuf.st_size);
655err_close:
656 close(dfd);
657 return -1;
658}
659
660int rockchip_copy_image(int ifd, struct image_tool_params *params)
661{
662 int ret;
663
664 ret = copy_file(params, ifd, spl_params.init_file,
665 spl_params.init_size);
666 if (ret)
667 return ret;
668
669 if (spl_params.boot_file) {
670 ret = copy_file(params, ifd, spl_params.boot_file,
671 spl_params.boot_size);
672 if (ret)
673 return ret;
674 }
675
676 return pad_file(params, ifd,
677 params->file_size - params->orig_file_size);
Philipp Tomsich111bcc42017-03-15 12:08:43 +0100678}