1 // SPDX-License-Identifier: BSD-2-Clause
3 * Generate Renesas RZ/N1 BootROM header (SPKG)
4 * (C) Copyright 2022 Schneider Electric
6 * Based on spkg_utility.c
7 * (C) Copyright 2016 Renesas Electronics Europe Ltd
10 #include "imagetool.h"
15 #include <u-boot/crc.h>
16 #include "renesas_spkgimage.h"
18 /* Note: the ordering of the bitfields does not matter */
20 unsigned int version:1;
21 unsigned int ecc_block_size:2;
22 unsigned int ecc_enable:1;
23 unsigned int ecc_scheme:3;
24 unsigned int ecc_bytes:8;
29 static struct config_file conf;
31 static int check_range(const char *name, int val, int min, int max)
34 fprintf(stderr, "Warning: param '%s' adjusted to min %d\n",
40 fprintf(stderr, "Warning: param '%s' adjusted to max %d\n",
48 static int spkgimage_parse_config_line(char *line, size_t line_num)
52 char *name = strtok_r(line, delim, &saveptr);
53 char *val_str = strtok_r(NULL, delim, &saveptr);
54 int value = atoi(val_str);
56 if (!strcmp("VERSION", name)) {
57 conf.version = check_range(name, value, 1, 15);
58 } else if (!strcmp("NAND_ECC_ENABLE", name)) {
59 conf.ecc_enable = check_range(name, value, 0, 1);
60 } else if (!strcmp("NAND_ECC_BLOCK_SIZE", name)) {
61 conf.ecc_block_size = check_range(name, value, 0, 2);
62 } else if (!strcmp("NAND_ECC_SCHEME", name)) {
63 conf.ecc_scheme = check_range(name, value, 0, 7);
64 } else if (!strcmp("NAND_BYTES_PER_ECC_BLOCK", name)) {
65 conf.ecc_bytes = check_range(name, value, 0, 255);
66 } else if (!strcmp("ADD_DUMMY_BLP", name)) {
67 conf.blp_len = value ? SPKG_BLP_SIZE : 0;
68 } else if (!strcmp("PADDING", name)) {
69 if (strrchr(val_str, 'K'))
71 else if (strrchr(val_str, 'M'))
72 value = value * 1024 * 1024;
73 conf.padding = check_range(name, value, 1, INT_MAX);
76 "config error: unknown keyword on line %zu\n",
84 static int spkgimage_parse_config_file(char *filename)
90 fcfg = fopen(filename, "r");
94 while (fgets(line, sizeof(line), fcfg)) {
97 /* Skip blank lines and comments */
98 if (line[0] == '\n' || line[0] == '#')
101 /* Strip any trailing newline */
102 line[strcspn(line, "\n")] = 0;
105 if (spkgimage_parse_config_line(line, line_num))
111 /* Avoid divide-by-zero later on */
118 static int spkgimage_check_params(struct image_tool_params *params)
121 fprintf(stderr, "Error: Load Address must be set.\n");
125 if (!params->imagename || !params->imagename[0]) {
126 fprintf(stderr, "Error: Image name must be set.\n");
130 if (!params->datafile) {
131 fprintf(stderr, "Error: Data filename must be set.\n");
138 static int spkgimage_verify_header(unsigned char *ptr, int size,
139 struct image_tool_params *param)
141 struct spkg_file *file = (struct spkg_file *)ptr;
142 struct spkg_hdr *header = (struct spkg_hdr *)ptr;
143 char marker[4] = SPKG_HEADER_MARKER;
144 uint32_t payload_length;
148 /* Check the marker bytes */
149 if (memcmp(header->marker, marker, 4)) {
150 fprintf(stderr, "Error: invalid marker bytes\n");
155 crc = crc32(0, ptr, SPKG_HEADER_SIZE - SPKG_CRC_SIZE);
156 if (crc != header->crc) {
157 fprintf(stderr, "Error: invalid header CRC=\n");
161 /* Check all copies of header are the same */
162 for (int i = 1; i < SPKG_HEADER_COUNT; i++) {
163 if (memcmp(&header[0], &header[i], SPKG_HEADER_SIZE)) {
164 fprintf(stderr, "Error: header %d mismatch\n", i);
169 /* Check the payload CRC */
170 payload_length = le32_to_cpu(header->payload_length) >> 8;
171 crc_buf = file->payload + payload_length - SPKG_CRC_SIZE;
172 crc = crc32(0, file->payload, payload_length - SPKG_CRC_SIZE);
173 if (crc_buf[0] != (crc & 0xff) ||
174 crc_buf[1] != (crc >> 8 & 0xff) ||
175 crc_buf[2] != (crc >> 16 & 0xff) ||
176 crc_buf[3] != (crc >> 24 & 0xff)) {
177 fprintf(stderr, "Error: invalid payload CRC\n");
184 static void spkgimage_print_header(const void *ptr,
185 struct image_tool_params *image)
187 const struct spkg_hdr *h = ptr;
188 uint32_t offset = le32_to_cpu(h->execution_offset);
190 printf("Image type\t: Renesas SPKG Image\n");
191 printf("Marker\t\t: %c%c%c%c\n",
192 h->marker[0], h->marker[1], h->marker[2], h->marker[3]);
193 printf("Version\t\t: %d\n", h->version);
196 printf("Scheme %d, Block size %d, Strength %d\n",
197 h->ecc_scheme, (h->ecc >> 1) & 3, h->ecc_bytes);
199 printf("Not enabled\n");
200 printf("Payload length\t: %d\n", le32_to_cpu(h->payload_length) >> 8);
201 printf("Load address\t: 0x%08x\n", le32_to_cpu(h->load_address));
202 printf("Execution offset: 0x%08x (%s mode)\n", offset & ~1,
203 offset & 1 ? "THUMB" : "ARM");
204 printf("Header checksum\t: 0x%08x\n", le32_to_cpu(h->crc));
208 * This is the same as the macro version in include/kernel.h.
209 * However we cannot include that header, because for host tools,
210 * it ends up pulling in the host /usr/include/linux/kernel.h,
211 * which lacks the definition of roundup().
213 static inline uint32_t roundup(uint32_t x, uint32_t y)
215 return ((x + y - 1) / y) * y;
218 static int spkgimage_vrec_header(struct image_tool_params *params,
219 struct image_type_params *tparams)
222 struct spkg_file *out_buf;
224 /* Parse the config file */
225 if (spkgimage_parse_config_file(params->imagename)) {
226 fprintf(stderr, "Error parsing config file\n");
230 /* Get size of input data file */
231 if (stat(params->datafile, &s)) {
232 fprintf(stderr, "Could not stat data file: %s: %s\n",
233 params->datafile, strerror(errno));
236 params->orig_file_size = s.st_size;
238 /* Determine size of resulting SPKG file */
239 uint32_t header_len = SPKG_HEADER_SIZE * SPKG_HEADER_COUNT;
240 uint32_t payload_len = conf.blp_len + s.st_size + SPKG_CRC_SIZE;
241 uint32_t total_len = header_len + payload_len;
243 /* Round up to next multiple of padding size */
244 uint32_t padded_len = roundup(total_len, conf.padding);
246 /* Number of padding bytes to add */
247 conf.padding = padded_len - total_len;
249 /* Fixup payload_len to include padding bytes */
250 payload_len += conf.padding;
252 /* Prepare the header */
253 struct spkg_hdr header = {
254 .marker = SPKG_HEADER_MARKER,
255 .version = conf.version,
256 .ecc = (conf.ecc_enable << 5) | (conf.ecc_block_size << 1),
257 .ecc_scheme = conf.ecc_scheme,
258 .ecc_bytes = conf.ecc_bytes,
259 .payload_length = cpu_to_le32(payload_len << 8),
260 .load_address = cpu_to_le32(params->addr),
261 .execution_offset = cpu_to_le32(params->ep - params->addr),
263 header.crc = crc32(0, (uint8_t *)&header,
264 sizeof(header) - SPKG_CRC_SIZE);
266 /* The SPKG contains 8 copies of the header */
267 out_buf = malloc(sizeof(struct spkg_file));
269 fprintf(stderr, "Error: Data filename must be set.\n");
272 tparams->hdr = out_buf;
273 tparams->header_size = sizeof(struct spkg_file);
275 /* Fill the SPKG with the headers */
276 for (int i = 0; i < SPKG_HEADER_COUNT; i++)
277 memcpy(&out_buf->header[i], &header, sizeof(header));
279 /* Extra bytes to allocate in the output file */
280 return conf.blp_len + conf.padding + 4;
283 static void spkgimage_set_header(void *ptr, struct stat *sbuf, int ifd,
284 struct image_tool_params *params)
286 uint8_t *payload = ptr + SPKG_HEADER_SIZE * SPKG_HEADER_COUNT;
287 uint8_t *file_end = payload + conf.blp_len + params->orig_file_size;
288 uint8_t *crc_buf = file_end + conf.padding;
291 /* Make room for the Dummy BLp header */
292 memmove(payload + conf.blp_len, payload, params->orig_file_size);
294 /* Fill the SPKG with the Dummy BLp */
295 memset(payload, 0x88, conf.blp_len);
298 * mkimage copy_file() pads the input file with zeros.
299 * Replace those zeros with flash friendly one bits.
300 * The original version skipped the first 4 bytes,
301 * probably an oversight, but for consistency we
302 * keep the same behaviour.
304 if (conf.padding >= 4)
305 memset(file_end + 4, 0xff, conf.padding - 4);
307 /* Add Payload CRC */
308 crc = crc32(0, payload, crc_buf - payload);
310 crc_buf[1] = crc >> 8;
311 crc_buf[2] = crc >> 16;
312 crc_buf[3] = crc >> 24;
315 static int spkgimage_check_image_types(uint8_t type)
317 return type == IH_TYPE_RENESAS_SPKG ? 0 : -EINVAL;
321 * spkgimage type parameter definition
325 "Renesas SPKG Image",
328 spkgimage_check_params,
329 spkgimage_verify_header,
330 spkgimage_print_header,
331 spkgimage_set_header,
333 spkgimage_check_image_types,
335 spkgimage_vrec_header