1 From ff0e952a3a380ba191375d5f68609cdbe026d535 Mon Sep 17 00:00:00 2001
2 From: Samuel Holland <samuel@sholland.org>
3 Date: Sat, 7 Aug 2021 19:55:20 -0500
4 Subject: [PATCH 33/90] tools: mkimage: Add Allwinner TOC1 support
6 TOC1 is an container format used by Allwinner's boot0 that can hold
7 multiple images. It supports encryption and signatures, but that
8 functionality is not implemented, only the basic "non-secure" subset.
10 A config file is used to provide the list of data files to include. Its
11 path is passed as the argument to "-d". It contains sections of the
18 Specific well-known names, such as "dtb", "opensbi", and "u-boot", are
19 used by the bootloader to distinguish the items inside the image.
22 tools: mkimage: Add Allwinner TOC1 support
24 The SPL port for the Allwinner D1 RISC-V SoC will probably take a while
25 longer than porting U-Boot proper, as none of the relevant drivers are
26 set up for DM in SPL. In the meantime, we are using[1][2] a fork[3] of
27 Allwinner's boot0 loader, which they also call "spl" in their BSP. boot0
28 uses this TOC1 image format.
30 The vendor tools for generating TOC1 images require a binary config file
31 generated by their FEX compiler. Instead of trying to support that, I
32 made up a simple human-readable config file format. I didn't see any
33 existing platform-agnostic parser for multi-image containers in mkimage.
35 I am sending this as RFC because it is only of temporary/limited use.
36 It only works with one specific fork of boot0 which was modified to
37 "behave" (the the original vendor version monkey-patches a custom header
38 inside the U-Boot image during boot). So it will be obsolete once U-Boot
39 SPL is ported. And it is Yet Another Image Format. On the other hand, it
40 does work, and it is currently being used.
42 [1]: https://linux-sunxi.org/Allwinner_Nezha#U-Boot
43 [2]: https://fedoraproject.org/wiki/Architectures/RISC-V/Allwinner
44 [3]: https://github.com/smaeul/sun20i_d1_spl
48 Signed-off-by: Samuel Holland <samuel@sholland.org>
52 include/sunxi_image.h | 26 ++++
54 tools/sunxi_toc1.c | 318 ++++++++++++++++++++++++++++++++++++++++++
55 5 files changed, 347 insertions(+)
56 create mode 100644 tools/sunxi_toc1.c
60 @@ -180,6 +180,7 @@ static const table_entry_t uimage_type[]
61 { IH_TYPE_COPRO, "copro", "Coprocessor Image"},
62 { IH_TYPE_SUNXI_EGON, "sunxi_egon", "Allwinner eGON Boot Image" },
63 { IH_TYPE_SUNXI_TOC0, "sunxi_toc0", "Allwinner TOC0 Boot Image" },
64 + { IH_TYPE_SUNXI_TOC1, "sunxi_toc1", "Allwinner TOC1 Boot Image" },
70 @@ -229,6 +229,7 @@ enum image_type_t {
71 IH_TYPE_COPRO, /* Coprocessor Image for remoteproc*/
72 IH_TYPE_SUNXI_EGON, /* Allwinner eGON Boot Image */
73 IH_TYPE_SUNXI_TOC0, /* Allwinner TOC0 Boot Image */
74 + IH_TYPE_SUNXI_TOC1, /* Allwinner TOC1 Boot Image */
76 IH_TYPE_COUNT, /* Number of image types */
78 --- a/include/sunxi_image.h
79 +++ b/include/sunxi_image.h
80 @@ -116,4 +116,30 @@ struct __packed toc0_item_info {
81 #define TOC0_ITEM_INFO_NAME_KEY 0x00010303
82 #define TOC0_ITEM_INFO_END "IIE;"
84 +struct __packed toc1_main_info {
92 + __le32 major_version;
93 + __le32 minor_version;
98 +struct __packed toc1_item_info {
106 + __le32 reserved[69];
113 @@ -132,6 +132,7 @@ dumpimage-mkimage-objs := aisimage.o \
119 lib/hash-checksum.o \
122 +++ b/tools/sunxi_toc1.c
124 +// SPDX-License-Identifier: GPL-2.0+
126 + * (C) Copyright 2018 Arm Ltd.
127 + * (C) Copyright 2020-2021 Samuel Holland <samuel@sholland.org>
137 +#include <sunxi_image.h>
139 +#include "imagetool.h"
140 +#include "mkimage.h"
142 +#define SECTOR_SIZE 512
147 + unsigned long addr;
151 +static uint32_t toc1_header_length(uint32_t num_items)
153 + return ALIGN(sizeof(struct toc1_main_info) +
154 + sizeof(struct toc1_item_info) * num_items, SECTOR_SIZE);
157 +static int toc1_parse_cfg(const char *file, struct item_desc **desc,
158 + uint32_t *main_length, uint32_t *num_items)
160 + struct item_desc *descs = NULL;
161 + int ret = EXIT_FAILURE;
162 + FILE *cfg, *fp = NULL;
163 + uint32_t ndescs = 0;
171 + cfg = fopen(file, "r");
175 + while (getline(&line, &len, cfg) > 0) {
178 + if (line[0] == '[') {
180 + end = strchr(s, ']');
181 + if (!end || end[1] != '\n')
186 + descs = reallocarray(descs, ndescs, sizeof(*descs));
190 + descs[ndescs - 1].name = strdup(s);
191 + } else if (line[0] != '#' && line[0] != '\n') {
192 + s = strchr(line, '=');
195 + while ((++s)[0] == ' ')
197 + end = strchr(s, '\n');
202 + if (!strncmp(line, "file", strlen("file"))) {
203 + fp = fopen(s, "rb");
206 + if (fseek(fp, 0, SEEK_END) < 0)
208 + descs[ndescs - 1].file = strdup(s);
209 + descs[ndescs - 1].length = ftell(fp);
210 + *main_length += ALIGN(descs[ndescs - 1].length,
214 + } else if (!strncmp(line, "addr", strlen("addr"))) {
215 + descs[ndescs - 1].addr = strtoul(s, NULL, 0);
223 + *main_length += toc1_header_length(ndescs);
224 + *num_items = ndescs;
226 + ret = EXIT_SUCCESS;
239 +static int toc1_create(uint8_t *buf, uint32_t len,
240 + const struct item_desc *desc, uint32_t num_items)
242 + struct toc1_main_info *main = (void *)buf;
243 + struct toc1_item_info *item = (void *)(main + 1);
244 + uint32_t item_offset, item_length;
245 + uint32_t *buf32 = (void *)buf;
246 + int ret = EXIT_FAILURE;
247 + uint32_t checksum = 0;
251 + /* Create the main TOC1 header. */
252 + main->magic = cpu_to_le32(TOC0_MAIN_INFO_MAGIC);
253 + main->checksum = cpu_to_le32(BROM_STAMP_VALUE);
254 + main->num_items = cpu_to_le32(num_items);
255 + memcpy(main->end, TOC0_MAIN_INFO_END, sizeof(main->end));
258 + item_length = toc1_header_length(num_items);
260 + for (i = 0; i < num_items; ++i, ++item, ++desc) {
261 + item_offset = item_offset + item_length;
262 + item_length = desc->length;
264 + /* Create the item header. */
265 + memcpy(item->name, desc->name,
266 + strnlen(desc->name, sizeof(item->name)));
267 + item->offset = cpu_to_le32(item_offset);
268 + item->length = cpu_to_le32(item_length);
269 + item->load_addr = cpu_to_le32(desc->addr);
270 + memcpy(item->end, TOC0_ITEM_INFO_END, sizeof(item->end));
272 + /* Read in the data. */
273 + fp = fopen(desc->file, "rb");
276 + if (!fread(buf + item_offset, item_length, 1, fp))
281 + /* Pad the sectors with 0xff to be flash-friendly. */
282 + item_offset = item_offset + item_length;
283 + item_length = ALIGN(item_offset, SECTOR_SIZE) - item_offset;
284 + memset(buf + item_offset, 0xff, item_length);
287 + /* Fill in the total padded file length. */
288 + item_offset = item_offset + item_length;
289 + main->length = cpu_to_le32(item_offset);
291 + /* Verify enough space was provided when creating the image. */
292 + assert(len >= item_offset);
294 + /* Calculate the checksum. Yes, it's that simple. */
295 + for (i = 0; i < item_offset / 4; ++i)
296 + checksum += le32_to_cpu(buf32[i]);
297 + main->checksum = cpu_to_le32(checksum);
299 + ret = EXIT_SUCCESS;
308 +static int toc1_verify(const uint8_t *buf, uint32_t len)
310 + const struct toc1_main_info *main = (void *)buf;
311 + const struct toc1_item_info *item = (void *)(main + 1);
312 + uint32_t checksum = BROM_STAMP_VALUE;
313 + uint32_t main_length, num_items;
314 + uint32_t *buf32 = (void *)buf;
315 + int ret = EXIT_FAILURE;
318 + num_items = le32_to_cpu(main->num_items);
319 + main_length = le32_to_cpu(main->length);
321 + if (len < main_length || main_length < toc1_header_length(num_items))
324 + /* Verify the main header. */
325 + if (le32_to_cpu(main->magic) != TOC0_MAIN_INFO_MAGIC)
327 + /* Verify the checksum without modifying the buffer. */
328 + for (i = 0; i < main_length / 4; ++i)
329 + checksum += le32_to_cpu(buf32[i]);
330 + if (checksum != 2 * le32_to_cpu(main->checksum))
332 + /* The length must be at least 512 byte aligned. */
333 + if (main_length % SECTOR_SIZE)
335 + if (memcmp(main->end, TOC0_MAIN_INFO_END, sizeof(main->end)))
338 + /* Verify each item header. */
339 + for (i = 0; i < num_items; ++i, ++item)
340 + if (memcmp(item->end, TOC0_ITEM_INFO_END, sizeof(item->end)))
343 + ret = EXIT_SUCCESS;
349 +static int toc1_check_params(struct image_tool_params *params)
351 + if (!params->dflag)
357 +static int toc1_verify_header(unsigned char *buf, int image_size,
358 + struct image_tool_params *params)
360 + return toc1_verify(buf, image_size);
363 +static void toc1_print_header(const void *buf)
365 + const struct toc1_main_info *main = buf;
366 + const struct toc1_item_info *item = (void *)(main + 1);
367 + uint32_t head_length, main_length, num_items;
368 + uint32_t item_offset, item_length, item_addr;
371 + num_items = le32_to_cpu(main->num_items);
372 + head_length = sizeof(*main) + num_items * sizeof(*item);
373 + main_length = le32_to_cpu(main->length);
375 + printf("Allwinner TOC1 Image\n"
377 + "Contents: %d items\n"
378 + " 00000000:%08x Headers\n",
379 + main_length, num_items, head_length);
381 + for (i = 0; i < num_items; ++i, ++item) {
382 + item_offset = le32_to_cpu(item->offset);
383 + item_length = le32_to_cpu(item->length);
384 + item_addr = le32_to_cpu(item->load_addr);
386 + printf(" %08x:%08x => %08x %s\n",
387 + item_offset, item_length, item_addr, item->name);
391 +static void toc1_set_header(void *buf, struct stat *sbuf, int ifd,
392 + struct image_tool_params *params)
394 + /* Image is already written below. */
397 +static int toc1_check_image_type(uint8_t type)
399 + return type == IH_TYPE_SUNXI_TOC1 ? 0 : 1;
402 +static int toc1_vrec_header(struct image_tool_params *params,
403 + struct image_type_params *tparams)
405 + uint32_t main_length, num_items;
406 + struct item_desc *desc;
409 + /* This "header" contains the entire image. */
410 + params->skipcpy = 1;
412 + ret = toc1_parse_cfg(params->datafile, &desc, &main_length, &num_items);
416 + tparams->header_size = main_length;
417 + tparams->hdr = calloc(tparams->header_size, 1);
421 + ret = toc1_create(tparams->hdr, tparams->header_size, desc, num_items);
430 + "Allwinner TOC1 Boot Image support",
434 + toc1_verify_header,
438 + toc1_check_image_type,