blob: 3ba3c93af1bdf5832f74bdd85b92accbaced2c82 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Charles Manning832472a2014-03-06 15:40:50 +13002/*
3 * Copyright (C) 2014 Charles Manning <cdhmanning@gmail.com>
4 *
Marek Vasutcece78f2018-04-15 13:15:33 +02005 * Reference documents:
6 * Cyclone V SoC: https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/cyclone-v/cv_5400a.pdf
7 * Arria V SoC: https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/arria-v/av_5400a.pdf
8 * Arria 10 SoC: https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/arria-10/a10_5400a.pdf
Charles Manning832472a2014-03-06 15:40:50 +13009 *
Marek Vasutcece78f2018-04-15 13:15:33 +020010 * Bootable SoCFPGA image requires a structure of the following format
11 * positioned at offset 0x40 of the bootable image. Endian is LSB.
Charles Manning832472a2014-03-06 15:40:50 +130012 *
Marek Vasutcece78f2018-04-15 13:15:33 +020013 * There are two versions of the SoCFPGA header format, v0 and v1.
14 * The version 0 is used by Cyclone V SoC and Arria V SoC, while
15 * the version 1 is used by the Arria 10 SoC.
Charles Manning832472a2014-03-06 15:40:50 +130016 *
Marek Vasutcece78f2018-04-15 13:15:33 +020017 * Version 0:
Charles Manning832472a2014-03-06 15:40:50 +130018 * Offset Length Usage
19 * -----------------------
Marek Vasutcece78f2018-04-15 13:15:33 +020020 * 0x40 4 Validation word (0x31305341)
21 * 0x44 1 Version (0x0)
22 * 0x45 1 Flags (unused, zero is fine)
23 * 0x46 2 Length (in units of u32, including the end checksum).
24 * 0x48 2 Zero (0x0)
Charles Manning832472a2014-03-06 15:40:50 +130025 * 0x4A 2 Checksum over the header. NB Not CRC32
26 *
Marek Vasutcece78f2018-04-15 13:15:33 +020027 * Version 1:
28 * Offset Length Usage
29 * -----------------------
30 * 0x40 4 Validation word (0x31305341)
31 * 0x44 1 Version (0x1)
32 * 0x45 1 Flags (unused, zero is fine)
33 * 0x46 2 Header length (in units of u8).
34 * 0x48 4 Length (in units of u8).
35 * 0x4C 4 Image entry offset from standard of header
36 * 0x50 2 Zero (0x0)
37 * 0x52 2 Checksum over the header. NB Not CRC32
38 *
Charles Manning832472a2014-03-06 15:40:50 +130039 * At the end of the code we have a 32-bit CRC checksum over whole binary
40 * excluding the CRC.
41 *
42 * Note that the CRC used here is **not** the zlib/Adler crc32. It is the
43 * CRC-32 used in bzip2, ethernet and elsewhere.
44 *
Marek Vasutcece78f2018-04-15 13:15:33 +020045 * The Image entry offset in version 1 image is relative the the start of
46 * the header, 0x40, and must not be a negative number. Therefore, it is
47 * only possible to make the SoCFPGA jump forward. The U-Boot bootloader
48 * places a trampoline instruction at offset 0x5c, 0x14 bytes from the
49 * start of the SoCFPGA header, which jumps to the reset vector.
50 *
Charles Manning832472a2014-03-06 15:40:50 +130051 * The image is padded out to 64k, because that is what is
52 * typically used to write the image to the boot medium.
53 */
54
55#include "pbl_crc32.h"
56#include "imagetool.h"
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -020057#include "mkimage.h"
Simon Glass3db71102019-11-14 12:57:16 -070058#include <u-boot/crc.h>
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -020059
Charles Manning832472a2014-03-06 15:40:50 +130060#include <image.h>
61
62#define HEADER_OFFSET 0x40
63#define VALIDATION_WORD 0x31305341
Charles Manning832472a2014-03-06 15:40:50 +130064
Ley Foon Tan963e17a2020-09-09 11:34:30 +080065/* Minimum and default entry point offset */
66#define ENTRY_POINT_OFFSET 0x14
67
Marek Vasutcece78f2018-04-15 13:15:33 +020068static uint8_t buffer_v0[0x10000];
69static uint8_t buffer_v1[0x40000];
Charles Manning832472a2014-03-06 15:40:50 +130070
Marek Vasutcece78f2018-04-15 13:15:33 +020071struct socfpga_header_v0 {
72 uint32_t validation;
73 uint8_t version;
74 uint8_t flags;
75 uint16_t length_u32;
76 uint16_t zero;
77 uint16_t checksum;
Marek Vasut9f0021a2018-04-15 13:38:49 +020078};
Charles Manning832472a2014-03-06 15:40:50 +130079
Marek Vasutcece78f2018-04-15 13:15:33 +020080struct socfpga_header_v1 {
81 uint32_t validation;
82 uint8_t version;
83 uint8_t flags;
84 uint16_t header_u8;
85 uint32_t length_u8;
86 uint32_t entry_offset;
87 uint16_t zero;
88 uint16_t checksum;
89};
90
91static unsigned int sfp_hdr_size(uint8_t ver)
92{
93 if (ver == 0)
94 return sizeof(struct socfpga_header_v0);
95 if (ver == 1)
96 return sizeof(struct socfpga_header_v1);
97 return 0;
98}
99
100static unsigned int sfp_pad_size(uint8_t ver)
101{
102 if (ver == 0)
103 return sizeof(buffer_v0);
104 if (ver == 1)
105 return sizeof(buffer_v1);
106 return 0;
107}
108
Charles Manning832472a2014-03-06 15:40:50 +1300109/*
110 * The header checksum is just a very simple checksum over
111 * the header area.
112 * There is still a crc32 over the whole lot.
113 */
Marek Vasutcece78f2018-04-15 13:15:33 +0200114static uint16_t sfp_hdr_checksum(uint8_t *buf, unsigned char ver)
Charles Manning832472a2014-03-06 15:40:50 +1300115{
Charles Manning832472a2014-03-06 15:40:50 +1300116 uint16_t ret = 0;
Marek Vasutcece78f2018-04-15 13:15:33 +0200117 int len = sfp_hdr_size(ver) - sizeof(ret);
Charles Manning832472a2014-03-06 15:40:50 +1300118
119 while (--len)
120 ret += *buf++;
121
122 return ret;
123}
124
Marek Vasutcece78f2018-04-15 13:15:33 +0200125static void sfp_build_header(uint8_t *buf, uint8_t ver, uint8_t flags,
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800126 uint32_t length_bytes,
127 struct image_tool_params *params)
Charles Manning832472a2014-03-06 15:40:50 +1300128{
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800129 uint32_t entry_offset = params->eflag ? params->ep : ENTRY_POINT_OFFSET;
Marek Vasutcece78f2018-04-15 13:15:33 +0200130 struct socfpga_header_v0 header_v0 = {
131 .validation = cpu_to_le32(VALIDATION_WORD),
132 .version = 0,
133 .flags = flags,
134 .length_u32 = cpu_to_le16(length_bytes / 4),
135 .zero = 0,
136 };
Marek Vasut9f0021a2018-04-15 13:38:49 +0200137
Marek Vasutcece78f2018-04-15 13:15:33 +0200138 struct socfpga_header_v1 header_v1 = {
139 .validation = cpu_to_le32(VALIDATION_WORD),
140 .version = 1,
141 .flags = flags,
142 .header_u8 = cpu_to_le16(sizeof(header_v1)),
143 .length_u8 = cpu_to_le32(length_bytes),
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800144 /* Trampoline offset */
145 .entry_offset = cpu_to_le32(entry_offset),
Marek Vasutcece78f2018-04-15 13:15:33 +0200146 .zero = 0,
147 };
Charles Manning832472a2014-03-06 15:40:50 +1300148
Marek Vasutcece78f2018-04-15 13:15:33 +0200149 uint16_t csum;
150
151 if (ver == 0) {
152 csum = sfp_hdr_checksum((uint8_t *)&header_v0, 0);
153 header_v0.checksum = cpu_to_le16(csum);
154 memcpy(buf, &header_v0, sizeof(header_v0));
155 } else {
156 csum = sfp_hdr_checksum((uint8_t *)&header_v1, 1);
157 header_v1.checksum = cpu_to_le16(csum);
158 memcpy(buf, &header_v1, sizeof(header_v1));
159 }
Charles Manning832472a2014-03-06 15:40:50 +1300160}
161
162/*
163 * Perform a rudimentary verification of header and return
164 * size of image.
165 */
Marek Vasutcece78f2018-04-15 13:15:33 +0200166static int sfp_verify_header(const uint8_t *buf, uint8_t *ver)
Charles Manning832472a2014-03-06 15:40:50 +1300167{
Marek Vasutcece78f2018-04-15 13:15:33 +0200168 struct socfpga_header_v0 header_v0;
169 struct socfpga_header_v1 header_v1;
170 uint16_t hdr_csum, sfp_csum;
171 uint32_t img_len;
Marek Vasut9f0021a2018-04-15 13:38:49 +0200172
Marek Vasutcece78f2018-04-15 13:15:33 +0200173 /*
174 * Header v0 is always smaller than Header v1 and the validation
175 * word and version field is at the same place, so use Header v0
176 * to check for version during verifiction and upgrade to Header
177 * v1 if needed.
178 */
179 memcpy(&header_v0, buf, sizeof(header_v0));
Charles Manning832472a2014-03-06 15:40:50 +1300180
Marek Vasutcece78f2018-04-15 13:15:33 +0200181 if (le32_to_cpu(header_v0.validation) != VALIDATION_WORD)
Charles Manning832472a2014-03-06 15:40:50 +1300182 return -1;
183
Marek Vasutcece78f2018-04-15 13:15:33 +0200184 if (header_v0.version == 0) {
185 hdr_csum = le16_to_cpu(header_v0.checksum);
186 sfp_csum = sfp_hdr_checksum((uint8_t *)&header_v0, 0);
187 img_len = le16_to_cpu(header_v0.length_u32) * 4;
188 } else if (header_v0.version == 1) {
189 memcpy(&header_v1, buf, sizeof(header_v1));
190 hdr_csum = le16_to_cpu(header_v1.checksum);
191 sfp_csum = sfp_hdr_checksum((uint8_t *)&header_v1, 1);
192 img_len = le32_to_cpu(header_v1.length_u8);
193 } else { /* Invalid version */
194 return -EINVAL;
195 }
196
197 /* Verify checksum */
198 if (hdr_csum != sfp_csum)
199 return -EINVAL;
200
Atsushi Nemoto0ca8fd32018-09-21 09:19:13 +0900201 *ver = header_v0.version;
Marek Vasutcece78f2018-04-15 13:15:33 +0200202 return img_len;
Charles Manning832472a2014-03-06 15:40:50 +1300203}
204
205/* Sign the buffer and return the signed buffer size */
Marek Vasutcece78f2018-04-15 13:15:33 +0200206static int sfp_sign_buffer(uint8_t *buf, uint8_t ver, uint8_t flags,
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800207 int len, int pad_64k,
208 struct image_tool_params *params)
Charles Manning832472a2014-03-06 15:40:50 +1300209{
210 uint32_t calc_crc;
211
212 /* Align the length up */
Kever Yang02560b12020-03-30 11:56:22 +0800213 len = ALIGN(len, 4);
Charles Manning832472a2014-03-06 15:40:50 +1300214
215 /* Build header, adding 4 bytes to length to hold the CRC32. */
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800216 sfp_build_header(buf + HEADER_OFFSET, ver, flags, len + 4, params);
Charles Manning832472a2014-03-06 15:40:50 +1300217
218 /* Calculate and apply the CRC */
219 calc_crc = ~pbl_crc32(0, (char *)buf, len);
220
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200221 *((uint32_t *)(buf + len)) = cpu_to_le32(calc_crc);
Charles Manning832472a2014-03-06 15:40:50 +1300222
223 if (!pad_64k)
224 return len + 4;
225
Marek Vasutcece78f2018-04-15 13:15:33 +0200226 return sfp_pad_size(ver);
Charles Manning832472a2014-03-06 15:40:50 +1300227}
228
229/* Verify that the buffer looks sane */
Marek Vasutcece78f2018-04-15 13:15:33 +0200230static int sfp_verify_buffer(const uint8_t *buf)
Charles Manning832472a2014-03-06 15:40:50 +1300231{
232 int len; /* Including 32bit CRC */
233 uint32_t calc_crc;
234 uint32_t buf_crc;
Marek Vasutcece78f2018-04-15 13:15:33 +0200235 uint8_t ver = 0;
Charles Manning832472a2014-03-06 15:40:50 +1300236
Marek Vasutcece78f2018-04-15 13:15:33 +0200237 len = sfp_verify_header(buf + HEADER_OFFSET, &ver);
Charles Manning832472a2014-03-06 15:40:50 +1300238 if (len < 0) {
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -0200239 debug("Invalid header\n");
Charles Manning832472a2014-03-06 15:40:50 +1300240 return -1;
241 }
242
Marek Vasutcece78f2018-04-15 13:15:33 +0200243 if (len < HEADER_OFFSET || len > sfp_pad_size(ver)) {
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -0200244 debug("Invalid header length (%i)\n", len);
Charles Manning832472a2014-03-06 15:40:50 +1300245 return -1;
246 }
247
248 /*
249 * Adjust length to the base of the CRC.
250 * Check the CRC.
251 */
252 len -= 4;
253
254 calc_crc = ~pbl_crc32(0, (const char *)buf, len);
255
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200256 buf_crc = le32_to_cpu(*((uint32_t *)(buf + len)));
Charles Manning832472a2014-03-06 15:40:50 +1300257
258 if (buf_crc != calc_crc) {
259 fprintf(stderr, "CRC32 does not match (%08x != %08x)\n",
260 buf_crc, calc_crc);
261 return -1;
262 }
263
264 return 0;
265}
266
267/* mkimage glue functions */
268static int socfpgaimage_verify_header(unsigned char *ptr, int image_size,
Marek Vasutcece78f2018-04-15 13:15:33 +0200269 struct image_tool_params *params)
Charles Manning832472a2014-03-06 15:40:50 +1300270{
Marek Vasutcece78f2018-04-15 13:15:33 +0200271 if (image_size < 0x80)
Charles Manning832472a2014-03-06 15:40:50 +1300272 return -1;
273
Marek Vasutcece78f2018-04-15 13:15:33 +0200274 return sfp_verify_buffer(ptr);
Charles Manning832472a2014-03-06 15:40:50 +1300275}
276
277static void socfpgaimage_print_header(const void *ptr)
278{
Marek Vasutcece78f2018-04-15 13:15:33 +0200279 if (sfp_verify_buffer(ptr) == 0)
Charles Manning832472a2014-03-06 15:40:50 +1300280 printf("Looks like a sane SOCFPGA preloader\n");
281 else
282 printf("Not a sane SOCFPGA preloader\n");
283}
284
Ley Foon Tan963e17a2020-09-09 11:34:30 +0800285static int socfpgaimage_check_params_v0(struct image_tool_params *params)
Charles Manning832472a2014-03-06 15:40:50 +1300286{
287 /* Not sure if we should be accepting fflags */
288 return (params->dflag && (params->fflag || params->lflag)) ||
289 (params->fflag && (params->dflag || params->lflag)) ||
290 (params->lflag && (params->dflag || params->fflag));
291}
292
Ley Foon Tan963e17a2020-09-09 11:34:30 +0800293static int socfpgaimage_check_params_v1(struct image_tool_params *params)
294{
295 /*
296 * If the entry point is specified, ensure it is >= ENTRY_POINT_OFFSET
297 * and it is 4 bytes aligned.
298 */
299 if (params->eflag && (params->ep < ENTRY_POINT_OFFSET ||
300 params->ep % 4 != 0)) {
301 fprintf(stderr,
302 "Error: Entry point must be greater than 0x%x.\n",
303 ENTRY_POINT_OFFSET);
304 return -1;
305 }
306
307 /* Not sure if we should be accepting fflags */
308 return (params->dflag && (params->fflag || params->lflag)) ||
309 (params->fflag && (params->dflag || params->lflag)) ||
310 (params->lflag && (params->dflag || params->fflag));
311}
312
Marek Vasutcece78f2018-04-15 13:15:33 +0200313static int socfpgaimage_check_image_types_v0(uint8_t type)
Charles Manning832472a2014-03-06 15:40:50 +1300314{
315 if (type == IH_TYPE_SOCFPGAIMAGE)
316 return EXIT_SUCCESS;
317 return EXIT_FAILURE;
318}
319
Marek Vasutcece78f2018-04-15 13:15:33 +0200320static int socfpgaimage_check_image_types_v1(uint8_t type)
321{
322 if (type == IH_TYPE_SOCFPGAIMAGE_V1)
323 return EXIT_SUCCESS;
324 return EXIT_FAILURE;
325}
326
Charles Manning832472a2014-03-06 15:40:50 +1300327/*
328 * To work in with the mkimage framework, we do some ugly stuff...
329 *
330 * First, socfpgaimage_vrec_header() is called.
Marek Vasutcece78f2018-04-15 13:15:33 +0200331 * We prepend a fake header big enough to make the file sfp_pad_size().
Charles Manning832472a2014-03-06 15:40:50 +1300332 * This gives us enough space to do what we want later.
333 *
334 * Next, socfpgaimage_set_header() is called.
335 * We fix up the buffer by moving the image to the start of the buffer.
336 * We now have some room to do what we need (add CRC and padding).
337 */
338
339static int data_size;
Charles Manning832472a2014-03-06 15:40:50 +1300340
Marek Vasutcece78f2018-04-15 13:15:33 +0200341static int sfp_fake_header_size(unsigned int size, uint8_t ver)
342{
343 return sfp_pad_size(ver) - size;
344}
345
346static int sfp_vrec_header(struct image_tool_params *params,
347 struct image_type_params *tparams, uint8_t ver)
Charles Manning832472a2014-03-06 15:40:50 +1300348{
349 struct stat sbuf;
350
351 if (params->datafile &&
352 stat(params->datafile, &sbuf) == 0 &&
Marek Vasutcece78f2018-04-15 13:15:33 +0200353 sbuf.st_size <= (sfp_pad_size(ver) - sizeof(uint32_t))) {
Charles Manning832472a2014-03-06 15:40:50 +1300354 data_size = sbuf.st_size;
Marek Vasutcece78f2018-04-15 13:15:33 +0200355 tparams->header_size = sfp_fake_header_size(data_size, ver);
Charles Manning832472a2014-03-06 15:40:50 +1300356 }
357 return 0;
Marek Vasutcece78f2018-04-15 13:15:33 +0200358
Charles Manning832472a2014-03-06 15:40:50 +1300359}
360
Marek Vasutcece78f2018-04-15 13:15:33 +0200361static int socfpgaimage_vrec_header_v0(struct image_tool_params *params,
362 struct image_type_params *tparams)
363{
364 return sfp_vrec_header(params, tparams, 0);
365}
366
367static int socfpgaimage_vrec_header_v1(struct image_tool_params *params,
368 struct image_type_params *tparams)
369{
370 return sfp_vrec_header(params, tparams, 1);
371}
372
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800373static void sfp_set_header(void *ptr, unsigned char ver,
374 struct image_tool_params *params)
Charles Manning832472a2014-03-06 15:40:50 +1300375{
376 uint8_t *buf = (uint8_t *)ptr;
377
378 /*
379 * This function is called after vrec_header() has been called.
Marek Vasutcece78f2018-04-15 13:15:33 +0200380 * At this stage we have the sfp_fake_header_size() dummy bytes
381 * followed by data_size image bytes. Total = sfp_pad_size().
Charles Manning832472a2014-03-06 15:40:50 +1300382 * We need to fix the buffer by moving the image bytes back to
383 * the beginning of the buffer, then actually do the signing stuff...
384 */
Marek Vasutcece78f2018-04-15 13:15:33 +0200385 memmove(buf, buf + sfp_fake_header_size(data_size, ver), data_size);
386 memset(buf + data_size, 0, sfp_fake_header_size(data_size, ver));
Charles Manning832472a2014-03-06 15:40:50 +1300387
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800388 sfp_sign_buffer(buf, ver, 0, data_size, 0, params);
Marek Vasutcece78f2018-04-15 13:15:33 +0200389}
390
391static void socfpgaimage_set_header_v0(void *ptr, struct stat *sbuf, int ifd,
392 struct image_tool_params *params)
393{
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800394 sfp_set_header(ptr, 0, params);
Marek Vasutcece78f2018-04-15 13:15:33 +0200395}
396
397static void socfpgaimage_set_header_v1(void *ptr, struct stat *sbuf, int ifd,
398 struct image_tool_params *params)
399{
Ley Foon Tan1d0dc5b2020-09-22 10:19:44 +0800400 sfp_set_header(ptr, 1, params);
Charles Manning832472a2014-03-06 15:40:50 +1300401}
402
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200403U_BOOT_IMAGE_TYPE(
404 socfpgaimage,
Marek Vasutcece78f2018-04-15 13:15:33 +0200405 "Altera SoCFPGA Cyclone V / Arria V image support",
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200406 0, /* This will be modified by vrec_header() */
Marek Vasutcece78f2018-04-15 13:15:33 +0200407 (void *)buffer_v0,
Ley Foon Tan963e17a2020-09-09 11:34:30 +0800408 socfpgaimage_check_params_v0,
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200409 socfpgaimage_verify_header,
410 socfpgaimage_print_header,
Marek Vasutcece78f2018-04-15 13:15:33 +0200411 socfpgaimage_set_header_v0,
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200412 NULL,
Marek Vasutcece78f2018-04-15 13:15:33 +0200413 socfpgaimage_check_image_types_v0,
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200414 NULL,
Marek Vasutcece78f2018-04-15 13:15:33 +0200415 socfpgaimage_vrec_header_v0
416);
417
418U_BOOT_IMAGE_TYPE(
419 socfpgaimage_v1,
420 "Altera SoCFPGA Arria10 image support",
421 0, /* This will be modified by vrec_header() */
422 (void *)buffer_v1,
Ley Foon Tan963e17a2020-09-09 11:34:30 +0800423 socfpgaimage_check_params_v1,
Marek Vasutcece78f2018-04-15 13:15:33 +0200424 socfpgaimage_verify_header,
425 socfpgaimage_print_header,
426 socfpgaimage_set_header_v1,
427 NULL,
428 socfpgaimage_check_image_types_v1,
429 NULL,
430 socfpgaimage_vrec_header_v1
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200431);