blob: a0caa029cc04b7421f511e0dcf0bd05b0a729ebb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc9c74032015-08-30 16:55:24 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 *
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +02006 * (C) 2017 Theobroma Systems Design und Consulting GmbH
7 *
Simon Glassc9c74032015-08-30 16:55:24 -06008 * Helper functions for Rockchip images
9 */
10
11#include "imagetool.h"
12#include <image.h>
Yi Liu32829bc2021-05-18 10:13:40 +080013#include <u-boot/sha256.h>
Simon Glassc9c74032015-08-30 16:55:24 -060014#include <rc4.h>
15#include "mkimage.h"
16#include "rkcommon.h"
17
18enum {
Kever Yang90683112021-12-24 17:58:32 +080019 RK_MAGIC = 0x0ff0aa55,
Yi Liu32829bc2021-05-18 10:13:40 +080020 RK_MAGIC_V2 = 0x534E4B52,
Simon Glassc9c74032015-08-30 16:55:24 -060021};
22
Kever Yangad6dcde2021-12-24 18:00:36 +080023enum {
24 RK_HEADER_V1 = 1,
Yi Liu32829bc2021-05-18 10:13:40 +080025 RK_HEADER_V2 = 2,
Kever Yangad6dcde2021-12-24 18:00:36 +080026};
27
Yi Liu32829bc2021-05-18 10:13:40 +080028enum 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];
74};
75
Simon Glassc9c74032015-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 Yang90683112021-12-24 17:58:32 +080082 * @magic: Magic (must be RK_MAGIC)
Simon Glassc9c74032015-08-30 16:55:24 -060083 * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
Jeffy Chen0e7c90a2015-11-17 14:20:30 +080084 * @init_offset: Offset in blocks of the SPL code from this header
Simon Glassc9c74032015-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 Yang90683112021-12-24 17:58:32 +080089 uint32_t magic;
Simon Glassc9c74032015-08-30 16:55:24 -060090 uint8_t reserved[4];
91 uint32_t disable_rc4;
Jeffy Chen0e7c90a2015-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 Glassc9c74032015-08-30 16:55:24 -060096 uint8_t reserved2[2];
97};
98
Jeffy Chen06cdc342015-11-27 12:07:17 +080099/**
Philipp Tomsich087ce822017-03-15 12:08:43 +0100100 * struct header1_info
101 */
102struct header1_info {
103 uint32_t magic;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100104};
105
106/**
Jeffy Chen06cdc342015-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übner3b404d42017-02-18 19:46:27 +0100112 * @spl_rc4: RC4 encode the SPL binary (same key as header)
Kever Yangad6dcde2021-12-24 18:00:36 +0800113 * @header_ver: header block version
Jeffy Chen06cdc342015-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übner3b404d42017-02-18 19:46:27 +0100119 const bool spl_rc4;
Kever Yangad6dcde2021-12-24 18:00:36 +0800120 const uint32_t header_ver;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800121};
122
123static struct spl_info spl_infos[] = {
Kever Yangad6dcde2021-12-24 18:00:36 +0800124 { "px30", "RK33", 0x2800, false, RK_HEADER_V1 },
125 { "rk3036", "RK30", 0x1000, false, RK_HEADER_V1 },
Johan Jonker4a6d6ad2022-04-16 17:09:46 +0200126 { "rk3066", "RK30", 0x8000 - 0x800, true, RK_HEADER_V1 },
Kever Yangad6dcde2021-12-24 18:00:36 +0800127 { "rk3128", "RK31", 0x1800, false, RK_HEADER_V1 },
128 { "rk3188", "RK31", 0x8000 - 0x800, true, RK_HEADER_V1 },
129 { "rk322x", "RK32", 0x8000 - 0x1000, false, RK_HEADER_V1 },
130 { "rk3288", "RK32", 0x8000, false, RK_HEADER_V1 },
131 { "rk3308", "RK33", 0x40000 - 0x1000, false, RK_HEADER_V1 },
Jonas Karlman1a3c0e42023-02-25 19:01:34 +0000132 { "rk3328", "RK32", 0x8000 - 0x800, false, RK_HEADER_V1 },
Kever Yangad6dcde2021-12-24 18:00:36 +0800133 { "rk3368", "RK33", 0x8000 - 0x1000, false, RK_HEADER_V1 },
134 { "rk3399", "RK33", 0x30000 - 0x2000, false, RK_HEADER_V1 },
135 { "rv1108", "RK11", 0x1800, false, RK_HEADER_V1 },
Jagan Teki9fa68512022-12-14 23:21:07 +0530136 { "rv1126", "110B", 0x10000 - 0x1000, false, RK_HEADER_V1 },
Yifeng Zhaoa43f6722025-04-07 22:46:47 +0000137 { "rk3528", "RK35", 0x10000 - 0x1000, false, RK_HEADER_V2 },
Jonas Karlman16a91f62023-02-25 19:01:34 +0000138 { "rk3568", "RK35", 0x10000 - 0x1000, false, RK_HEADER_V2 },
Xuhui Lin2386a012025-04-15 23:51:15 +0200139 { "rk3576", "RK35", 0x80000 - 0x1000, false, RK_HEADER_V2 },
Jagan Teki4503b992023-01-30 20:27:32 +0530140 { "rk3588", "RK35", 0x100000 - 0x1000, false, RK_HEADER_V2 },
Jeffy Chen06cdc342015-11-27 12:07:17 +0800141};
142
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800143/**
144 * struct spl_params - spl params parsed in check_params()
145 *
146 * @init_file: Init data file path
147 * @init_size: Aligned size of init data in bytes
148 * @boot_file: Boot data file path
149 * @boot_size: Aligned size of boot data in bytes
150 */
151
152struct spl_params {
153 char *init_file;
154 uint32_t init_size;
155 char *boot_file;
156 uint32_t boot_size;
157};
158
159static struct spl_params spl_params = { 0 };
160
John Keepingf33096e2022-11-18 16:13:18 +0000161static const unsigned char rc4_key[16] = {
Simon Glassc9c74032015-08-30 16:55:24 -0600162 124, 78, 3, 4, 85, 5, 9, 7,
163 45, 44, 123, 56, 23, 13, 23, 17
164};
165
Jeffy Chen06cdc342015-11-27 12:07:17 +0800166static struct spl_info *rkcommon_get_spl_info(char *imagename)
167{
168 int i;
169
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200170 if (!imagename)
171 return NULL;
172
Jeffy Chen06cdc342015-11-27 12:07:17 +0800173 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
174 if (!strncmp(imagename, spl_infos[i].imagename, 6))
175 return spl_infos + i;
176
177 return NULL;
178}
179
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800180static int rkcommon_get_aligned_size(struct image_tool_params *params,
181 const char *fname)
182{
183 int size;
184
185 size = imagetool_get_filesize(params, fname);
186 if (size < 0)
187 return -1;
188
189 /*
190 * Pad to a 2KB alignment, as required for init/boot size by the ROM
191 * (see https://lists.denx.de/pipermail/u-boot/2017-May/293268.html)
192 */
193 return ROUND(size, RK_SIZE_ALIGN);
194}
195
Jeffy Chen06cdc342015-11-27 12:07:17 +0800196int rkcommon_check_params(struct image_tool_params *params)
197{
Heinrich Schuchardt17078162020-05-09 21:31:03 +0200198 int i, size;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800199
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200200 /*
201 * If this is a operation (list or extract), the don't require
202 * imagename to be set.
203 */
204 if (params->lflag || params->iflag)
205 return EXIT_SUCCESS;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800206
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800207 if (!rkcommon_get_spl_info(params->imagename))
208 goto err_spl_info;
209
210 spl_params.init_file = params->datafile;
211
212 spl_params.boot_file = strchr(spl_params.init_file, ':');
213 if (spl_params.boot_file) {
214 *spl_params.boot_file = '\0';
215 spl_params.boot_file += 1;
216 }
217
Heinrich Schuchardt17078162020-05-09 21:31:03 +0200218 size = rkcommon_get_aligned_size(params, spl_params.init_file);
219 if (size < 0)
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800220 return EXIT_FAILURE;
Heinrich Schuchardt17078162020-05-09 21:31:03 +0200221 spl_params.init_size = size;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800222
223 /* Boot file is optional, and only for back-to-bootrom functionality. */
224 if (spl_params.boot_file) {
Heinrich Schuchardt17078162020-05-09 21:31:03 +0200225 size = rkcommon_get_aligned_size(params, spl_params.boot_file);
226 if (size < 0)
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800227 return EXIT_FAILURE;
Heinrich Schuchardt17078162020-05-09 21:31:03 +0200228 spl_params.boot_size = size;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800229 }
230
231 if (spl_params.init_size > rkcommon_get_spl_size(params)) {
232 fprintf(stderr,
233 "Error: SPL image is too large (size %#x than %#x)\n",
234 spl_params.init_size, rkcommon_get_spl_size(params));
235 return EXIT_FAILURE;
236 }
237
238 return EXIT_SUCCESS;
239
240err_spl_info:
Jeffy Chen06cdc342015-11-27 12:07:17 +0800241 fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200242 params->imagename ? params->imagename : "NULL");
Jeffy Chen06cdc342015-11-27 12:07:17 +0800243
244 fprintf(stderr, "Available imagename:");
245 for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
246 fprintf(stderr, "\t%s", spl_infos[i].imagename);
247 fprintf(stderr, "\n");
248
Philipp Tomsich88cc7c02017-04-17 17:48:05 +0200249 return EXIT_FAILURE;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800250}
251
252const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
253{
254 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
255
256 /*
257 * info would not be NULL, because of we checked params before.
258 */
259 return info->spl_hdr;
260}
261
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100262int rkcommon_get_spl_size(struct image_tool_params *params)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100263{
264 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
265
266 /*
267 * info would not be NULL, because of we checked params before.
268 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100269 return info->spl_size;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100270}
271
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100272bool rkcommon_need_rc4_spl(struct image_tool_params *params)
Jeffy Chen06cdc342015-11-27 12:07:17 +0800273{
274 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
275
276 /*
277 * info would not be NULL, because of we checked params before.
278 */
Philipp Tomsiche39ccb52017-03-15 12:08:45 +0100279 return info->spl_rc4;
Jeffy Chen06cdc342015-11-27 12:07:17 +0800280}
281
Yi Liu32829bc2021-05-18 10:13:40 +0800282bool rkcommon_is_header_v2(struct image_tool_params *params)
283{
284 struct spl_info *info = rkcommon_get_spl_info(params->imagename);
285
286 return (info->header_ver == RK_HEADER_V2);
287}
288
289static void do_sha256_hash(uint8_t *buf, uint32_t size, uint8_t *out)
290{
291 sha256_context ctx;
292
293 sha256_starts(&ctx);
294 sha256_update(&ctx, buf, size);
295 sha256_finish(&ctx, out);
296}
297
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800298static void rkcommon_set_header0(void *buf, struct image_tool_params *params)
Simon Glassc9c74032015-08-30 16:55:24 -0600299{
Philipp Tomsich087ce822017-03-15 12:08:43 +0100300 struct header0_info *hdr = buf;
Samuel Holland67e862b2020-10-24 11:43:17 -0500301 uint32_t init_boot_size;
Simon Glassc9c74032015-08-30 16:55:24 -0600302
Philipp Tomsich087ce822017-03-15 12:08:43 +0100303 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
Kever Yang90683112021-12-24 17:58:32 +0800304 hdr->magic = cpu_to_le32(RK_MAGIC);
Samuel Holland67e862b2020-10-24 11:43:17 -0500305 hdr->disable_rc4 = cpu_to_le32(!rkcommon_need_rc4_spl(params));
306 hdr->init_offset = cpu_to_le16(RK_INIT_OFFSET);
307 hdr->init_size = cpu_to_le16(spl_params.init_size / RK_BLK_SIZE);
Simon Glassc9c74032015-08-30 16:55:24 -0600308
Philipp Tomsich3efab262017-04-17 17:48:04 +0200309 /*
Philipp Tomsich3c2610a2017-05-30 23:32:10 +0200310 * init_boot_size needs to be set, as it is read by the BootROM
311 * to determine the size of the next-stage bootloader (e.g. U-Boot
312 * proper), when used with the back-to-bootrom functionality.
313 *
314 * see https://lists.denx.de/pipermail/u-boot/2017-May/293267.html
315 * for a more detailed explanation by Andy Yan
Philipp Tomsich3efab262017-04-17 17:48:04 +0200316 */
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800317 if (spl_params.boot_file)
Samuel Holland67e862b2020-10-24 11:43:17 -0500318 init_boot_size = spl_params.init_size + spl_params.boot_size;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800319 else
Samuel Holland67e862b2020-10-24 11:43:17 -0500320 init_boot_size = spl_params.init_size + RK_MAX_BOOT_SIZE;
321 hdr->init_boot_size = cpu_to_le16(init_boot_size / RK_BLK_SIZE);
Simon Glassc9c74032015-08-30 16:55:24 -0600322
323 rc4_encode(buf, RK_BLK_SIZE, rc4_key);
Philipp Tomsich087ce822017-03-15 12:08:43 +0100324}
325
Yi Liu32829bc2021-05-18 10:13:40 +0800326static void rkcommon_set_header0_v2(void *buf, struct image_tool_params *params)
327{
328 struct header0_info_v2 *hdr = buf;
329 uint32_t sector_offset, image_sector_count;
330 uint32_t image_size_array[2];
331 uint8_t *image_ptr = NULL;
332 int i;
333
334 printf("Image Type: Rockchip %s boot image\n",
335 rkcommon_get_spl_hdr(params));
336 memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
337 hdr->magic = cpu_to_le32(RK_MAGIC_V2);
338 hdr->size_and_nimage = cpu_to_le32((2 << 16) + 384);
339 hdr->boot_flag = cpu_to_le32(HASH_SHA256);
340 sector_offset = 4;
341 image_size_array[0] = spl_params.init_size;
342 image_size_array[1] = spl_params.boot_size;
343
344 for (i = 0; i < 2; i++) {
345 image_sector_count = image_size_array[i] / RK_BLK_SIZE;
346 hdr->images[i].size_and_off = cpu_to_le32((image_sector_count
347 << 16) + sector_offset);
348 hdr->images[i].address = 0xFFFFFFFF;
349 hdr->images[i].counter = cpu_to_le32(i + 1);
350 image_ptr = buf + sector_offset * RK_BLK_SIZE;
351 do_sha256_hash(image_ptr, image_size_array[i],
352 hdr->images[i].hash);
353 sector_offset = sector_offset + image_sector_count;
354 }
355
356 do_sha256_hash(buf, (void *)hdr->hash - buf, hdr->hash);
357}
358
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800359void rkcommon_set_header(void *buf, struct stat *sbuf, int ifd,
360 struct image_tool_params *params)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100361{
362 struct header1_info *hdr = buf + RK_SPL_HDR_START;
363
Yi Liu32829bc2021-05-18 10:13:40 +0800364 if (rkcommon_is_header_v2(params)) {
365 rkcommon_set_header0_v2(buf, params);
366 } else {
367 rkcommon_set_header0(buf, params);
Philipp Tomsich087ce822017-03-15 12:08:43 +0100368
Yi Liu32829bc2021-05-18 10:13:40 +0800369 /* Set up the SPL name (i.e. copy spl_hdr over) */
370 if (memcmp(&hdr->magic, "RSAK", 4))
371 memcpy(&hdr->magic, rkcommon_get_spl_hdr(params), RK_SPL_HDR_SIZE);
Philipp Tomsich087ce822017-03-15 12:08:43 +0100372
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800373 if (rkcommon_need_rc4_spl(params))
Yi Liu32829bc2021-05-18 10:13:40 +0800374 rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
375 spl_params.init_size);
376
377 if (spl_params.boot_file) {
378 if (rkcommon_need_rc4_spl(params))
379 rkcommon_rc4_encode_spl(buf + RK_SPL_HDR_START,
380 spl_params.init_size,
381 spl_params.boot_size);
382 }
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800383 }
Simon Glassc9c74032015-08-30 16:55:24 -0600384}
Heiko Stübner3b404d42017-02-18 19:46:27 +0100385
Yi Liu32829bc2021-05-18 10:13:40 +0800386static inline unsigned int rkcommon_offset_to_spi(unsigned int offset)
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200387{
388 /*
389 * While SD/MMC images use a flat addressing, SPI images are padded
390 * to use the first 2K of every 4K sector only.
391 */
392 return ((offset & ~0x7ff) << 1) + (offset & 0x7ff);
393}
394
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200395static int rkcommon_parse_header(const void *buf, struct header0_info *header0,
396 struct spl_info **spl_info)
397{
Yi Liu32829bc2021-05-18 10:13:40 +0800398 unsigned int hdr1_offset;
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200399 struct header1_info *hdr1_sdmmc, *hdr1_spi;
400 int i;
401
402 if (spl_info)
403 *spl_info = NULL;
404
405 /*
406 * The first header (hdr0) is always RC4 encoded, so try to decrypt
407 * with the well-known key.
408 */
409 memcpy((void *)header0, buf, sizeof(struct header0_info));
410 rc4_encode((void *)header0, sizeof(struct header0_info), rc4_key);
411
Kever Yang90683112021-12-24 17:58:32 +0800412 if (le32_to_cpu(header0->magic) != RK_MAGIC)
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200413 return -EPROTO;
414
415 /* We don't support RC4 encoded image payloads here, yet... */
Samuel Holland67e862b2020-10-24 11:43:17 -0500416 if (le32_to_cpu(header0->disable_rc4) == 0)
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200417 return -ENOSYS;
418
Samuel Holland67e862b2020-10-24 11:43:17 -0500419 hdr1_offset = le16_to_cpu(header0->init_offset) * RK_BLK_SIZE;
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200420 hdr1_sdmmc = (struct header1_info *)(buf + hdr1_offset);
421 hdr1_spi = (struct header1_info *)(buf +
422 rkcommon_offset_to_spi(hdr1_offset));
423
424 for (i = 0; i < ARRAY_SIZE(spl_infos); i++) {
Miquel Raynalb9801042020-03-18 17:22:55 +0100425 if (!memcmp(&hdr1_sdmmc->magic, spl_infos[i].spl_hdr,
426 RK_SPL_HDR_SIZE)) {
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200427 if (spl_info)
428 *spl_info = &spl_infos[i];
429 return IH_TYPE_RKSD;
Miquel Raynalb9801042020-03-18 17:22:55 +0100430 } else if (!memcmp(&hdr1_spi->magic, spl_infos[i].spl_hdr,
431 RK_SPL_HDR_SIZE)) {
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200432 if (spl_info)
433 *spl_info = &spl_infos[i];
434 return IH_TYPE_RKSPI;
435 }
436 }
437
438 return -1;
439}
440
Yi Liu32829bc2021-05-18 10:13:40 +0800441static int rkcommon_parse_header_v2(const void *buf, struct header0_info_v2 *header)
442{
443 memcpy((void *)header, buf, sizeof(struct header0_info_v2));
444
445 if (le32_to_cpu(header->magic) != RK_MAGIC_V2)
446 return -EPROTO;
447
448 return 0;
449}
450
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200451int rkcommon_verify_header(unsigned char *buf, int size,
452 struct image_tool_params *params)
453{
454 struct header0_info header0;
455 struct spl_info *img_spl_info, *spl_info;
456 int ret;
457
Yi Liuaf7173e2022-03-30 18:05:59 +0800458 /* spl_hdr is abandon on header_v2 */
459 if ((*(uint32_t *)buf) == RK_MAGIC_V2)
460 return 0;
461
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200462 ret = rkcommon_parse_header(buf, &header0, &img_spl_info);
463
464 /* If this is the (unimplemented) RC4 case, then rewrite the result */
465 if (ret == -ENOSYS)
466 return 0;
467
468 if (ret < 0)
469 return ret;
470
471 /*
472 * If no 'imagename' is specified via the commandline (e.g. if this is
473 * 'dumpimage -l' w/o any further constraints), we accept any spl_info.
474 */
Quentin Schulze0683f92024-06-06 13:44:04 +0200475 if (params->imagename == NULL || !strlen(params->imagename))
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200476 return 0;
477
478 /* Match the 'imagename' against the 'spl_hdr' found */
479 spl_info = rkcommon_get_spl_info(params->imagename);
480 if (spl_info && img_spl_info)
481 return strcmp(spl_info->spl_hdr, img_spl_info->spl_hdr);
482
483 return -ENOENT;
484}
485
Pali Rohár0ed41e22023-03-29 21:25:54 +0200486void rkcommon_print_header(const void *buf, struct image_tool_params *params)
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200487{
488 struct header0_info header0;
Yi Liu32829bc2021-05-18 10:13:40 +0800489 struct header0_info_v2 header0_v2;
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200490 struct spl_info *spl_info;
491 uint8_t image_type;
Samuel Holland67e862b2020-10-24 11:43:17 -0500492 int ret, boot_size, init_size;
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200493
Yi Liu32829bc2021-05-18 10:13:40 +0800494 if ((*(uint32_t *)buf) == RK_MAGIC_V2) {
495 ret = rkcommon_parse_header_v2(buf, &header0_v2);
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200496
Yi Liu32829bc2021-05-18 10:13:40 +0800497 if (ret < 0) {
498 fprintf(stderr, "Error: image verification failed\n");
499 return;
500 }
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200501
Yi Liu32829bc2021-05-18 10:13:40 +0800502 init_size = header0_v2.images[0].size_and_off >> 16;
503 init_size = init_size * RK_BLK_SIZE;
504 boot_size = header0_v2.images[1].size_and_off >> 16;
505 boot_size = boot_size * RK_BLK_SIZE;
506 } else {
507 ret = rkcommon_parse_header(buf, &header0, &spl_info);
508
509 /* If this is the (unimplemented) RC4 case, then fail silently */
510 if (ret == -ENOSYS)
511 return;
512
513 if (ret < 0) {
514 fprintf(stderr, "Error: image verification failed\n");
515 return;
516 }
517
518 image_type = ret;
519 init_size = header0.init_size * RK_BLK_SIZE;
520 boot_size = header0.init_boot_size * RK_BLK_SIZE - init_size;
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200521
Yi Liu32829bc2021-05-18 10:13:40 +0800522 printf("Image Type: Rockchip %s (%s) boot image\n",
523 spl_info->spl_hdr,
524 (image_type == IH_TYPE_RKSD) ? "SD/MMC" : "SPI");
525 }
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200526
Samuel Holland67e862b2020-10-24 11:43:17 -0500527 printf("Init Data Size: %d bytes\n", init_size);
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800528
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800529 if (boot_size != RK_MAX_BOOT_SIZE)
530 printf("Boot Data Size: %d bytes\n", boot_size);
Philipp Tomsichf0d09fb2017-05-30 23:32:08 +0200531}
532
Heiko Stübner3b404d42017-02-18 19:46:27 +0100533void rkcommon_rc4_encode_spl(void *buf, unsigned int offset, unsigned int size)
534{
535 unsigned int remaining = size;
536
537 while (remaining > 0) {
538 int step = (remaining > RK_BLK_SIZE) ? RK_BLK_SIZE : remaining;
539
540 rc4_encode(buf + offset, step, rc4_key);
541 offset += RK_BLK_SIZE;
542 remaining -= step;
543 }
544}
Philipp Tomsich087ce822017-03-15 12:08:43 +0100545
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200546int rkcommon_vrec_header(struct image_tool_params *params,
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800547 struct image_type_params *tparams)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100548{
549 /*
550 * The SPL image looks as follows:
551 *
552 * 0x0 header0 (see rkcommon.c)
553 * 0x800 spl_name ('RK30', ..., 'RK33')
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200554 * (start of the payload for AArch64 payloads: we expect the
555 * first 4 bytes to be available for overwriting with our
556 * spl_name)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100557 * 0x804 first instruction to be executed
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200558 * (start of the image/payload for 32bit payloads)
Philipp Tomsich087ce822017-03-15 12:08:43 +0100559 *
Philipp Tomsichc6917da2017-04-17 17:48:02 +0200560 * For AArch64 (ARMv8) payloads, natural alignment (8-bytes) is
561 * required for its sections (so the image we receive needs to
562 * have the first 4 bytes reserved for the spl_name). Reserving
563 * these 4 bytes is done using the BOOT0_HOOK infrastructure.
Philipp Tomsich087ce822017-03-15 12:08:43 +0100564 *
Philipp Tomsich64109702017-10-10 16:21:18 +0200565 * The header is always at 0x800 (as we now use a payload
566 * prepadded using the boot0 hook for all targets): the first
567 * 4 bytes of these images can safely be overwritten using the
568 * boot magic.
Philipp Tomsich087ce822017-03-15 12:08:43 +0100569 */
Philipp Tomsich64109702017-10-10 16:21:18 +0200570 tparams->header_size = RK_SPL_HDR_START;
Philipp Tomsich087ce822017-03-15 12:08:43 +0100571
572 /* Allocate, clear and install the header */
573 tparams->hdr = malloc(tparams->header_size);
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800574 if (!tparams->hdr) {
575 fprintf(stderr, "%s: Can't alloc header: %s\n",
576 params->cmdname, strerror(errno));
577 exit(EXIT_FAILURE);
578 }
Philipp Tomsich087ce822017-03-15 12:08:43 +0100579 memset(tparams->hdr, 0, tparams->header_size);
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200580
581 /*
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800582 * We need to store the original file-size (i.e. before padding), as
583 * imagetool does not set this during its adjustment of file_size.
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200584 */
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800585 params->orig_file_size = tparams->header_size +
586 spl_params.init_size + spl_params.boot_size;
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200587
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800588 params->file_size = ROUND(params->orig_file_size, RK_SIZE_ALIGN);
589
590 /* Ignoring pad len, since we are using our own copy_image() */
591 return 0;
592}
593
594static int pad_file(struct image_tool_params *params, int ifd, int pad)
595{
596 uint8_t zeros[4096];
597
598 memset(zeros, 0, sizeof(zeros));
599
600 while (pad > 0) {
601 int todo = sizeof(zeros);
602
603 if (todo > pad)
604 todo = pad;
605 if (write(ifd, (char *)&zeros, todo) != todo) {
606 fprintf(stderr, "%s: Write error on %s: %s\n",
607 params->cmdname, params->imagefile,
608 strerror(errno));
609 return -1;
610 }
611 pad -= todo;
612 }
613
614 return 0;
615}
616
617static int copy_file(struct image_tool_params *params, int ifd,
618 const char *file, int padded_size)
619{
620 int dfd;
621 struct stat sbuf;
622 unsigned char *ptr;
623 int size;
624
625 if (params->vflag)
626 fprintf(stderr, "Adding Image %s\n", file);
627
628 dfd = open(file, O_RDONLY | O_BINARY);
629 if (dfd < 0) {
630 fprintf(stderr, "%s: Can't open %s: %s\n",
631 params->cmdname, file, strerror(errno));
632 return -1;
633 }
634
635 if (fstat(dfd, &sbuf) < 0) {
636 fprintf(stderr, "%s: Can't stat %s: %s\n",
637 params->cmdname, file, strerror(errno));
638 goto err_close;
639 }
640
641 if (params->vflag)
642 fprintf(stderr, "Size %u(pad to %u)\n",
643 (int)sbuf.st_size, padded_size);
644
645 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
646 if (ptr == MAP_FAILED) {
647 fprintf(stderr, "%s: Can't read %s: %s\n",
648 params->cmdname, file, strerror(errno));
649 goto err_munmap;
650 }
651
652 size = sbuf.st_size;
653 if (write(ifd, ptr, size) != size) {
654 fprintf(stderr, "%s: Write error on %s: %s\n",
655 params->cmdname, params->imagefile, strerror(errno));
656 goto err_munmap;
657 }
658
659 munmap((void *)ptr, sbuf.st_size);
660 close(dfd);
661 return pad_file(params, ifd, padded_size - size);
662
663err_munmap:
664 munmap((void *)ptr, sbuf.st_size);
665err_close:
666 close(dfd);
667 return -1;
668}
669
670int rockchip_copy_image(int ifd, struct image_tool_params *params)
671{
672 int ret;
673
674 ret = copy_file(params, ifd, spl_params.init_file,
675 spl_params.init_size);
676 if (ret)
677 return ret;
678
679 if (spl_params.boot_file) {
680 ret = copy_file(params, ifd, spl_params.boot_file,
681 spl_params.boot_size);
682 if (ret)
683 return ret;
684 }
Philipp Tomsichd2eaf412017-04-17 17:48:01 +0200685
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800686 return pad_file(params, ifd,
687 params->file_size - params->orig_file_size);
Philipp Tomsich087ce822017-03-15 12:08:43 +0100688}