blob: 673c53258d77b060fb53ac8aaf4b9594ce1da42e [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 *
Charles Manning832472a2014-03-06 15:40:50 +13005 * Reference doc http://www.altera.com.cn/literature/hb/cyclone-v/cv_5400A.pdf
6 * Note this doc is not entirely accurate. Of particular interest to us is the
7 * "header" length field being in U32s and not bytes.
8 *
9 * "Header" is a structure of the following format.
10 * this is positioned at 0x40.
11 *
12 * Endian is LSB.
13 *
14 * Offset Length Usage
15 * -----------------------
16 * 0x40 4 Validation word 0x31305341
17 * 0x44 1 Version (whatever, zero is fine)
18 * 0x45 1 Flags (unused, zero is fine)
19 * 0x46 2 Length (in units of u32, including the end checksum).
20 * 0x48 2 Zero
21 * 0x4A 2 Checksum over the header. NB Not CRC32
22 *
23 * At the end of the code we have a 32-bit CRC checksum over whole binary
24 * excluding the CRC.
25 *
26 * Note that the CRC used here is **not** the zlib/Adler crc32. It is the
27 * CRC-32 used in bzip2, ethernet and elsewhere.
28 *
29 * The image is padded out to 64k, because that is what is
30 * typically used to write the image to the boot medium.
31 */
32
33#include "pbl_crc32.h"
34#include "imagetool.h"
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -020035#include "mkimage.h"
36
Charles Manning832472a2014-03-06 15:40:50 +130037#include <image.h>
38
39#define HEADER_OFFSET 0x40
40#define VALIDATION_WORD 0x31305341
41#define PADDED_SIZE 0x10000
42
43/* To allow for adding CRC, the max input size is a bit smaller. */
44#define MAX_INPUT_SIZE (PADDED_SIZE - sizeof(uint32_t))
45
46static uint8_t buffer[PADDED_SIZE];
47
Marek Vasut9f0021a2018-04-15 13:38:49 +020048struct socfpga_header {
Charles Manning832472a2014-03-06 15:40:50 +130049 uint32_t validation;
50 uint8_t version;
51 uint8_t flags;
52 uint16_t length_u32;
53 uint16_t zero;
54 uint16_t checksum;
Marek Vasut9f0021a2018-04-15 13:38:49 +020055};
Charles Manning832472a2014-03-06 15:40:50 +130056
57/*
58 * The header checksum is just a very simple checksum over
59 * the header area.
60 * There is still a crc32 over the whole lot.
61 */
62static uint16_t hdr_checksum(struct socfpga_header *header)
63{
64 int len = sizeof(*header) - sizeof(header->checksum);
65 uint8_t *buf = (uint8_t *)header;
66 uint16_t ret = 0;
67
68 while (--len)
69 ret += *buf++;
70
71 return ret;
72}
73
74
75static void build_header(uint8_t *buf, uint8_t version, uint8_t flags,
76 uint16_t length_bytes)
77{
Marek Vasut9f0021a2018-04-15 13:38:49 +020078 struct socfpga_header header;
79
Andreas Bießmann686ed2c22014-10-24 23:39:10 +020080 header.validation = cpu_to_le32(VALIDATION_WORD);
Charles Manning832472a2014-03-06 15:40:50 +130081 header.version = version;
82 header.flags = flags;
Andreas Bießmann686ed2c22014-10-24 23:39:10 +020083 header.length_u32 = cpu_to_le16(length_bytes/4);
Charles Manning832472a2014-03-06 15:40:50 +130084 header.zero = 0;
Andreas Bießmann686ed2c22014-10-24 23:39:10 +020085 header.checksum = cpu_to_le16(hdr_checksum(&header));
Charles Manning832472a2014-03-06 15:40:50 +130086
87 memcpy(buf, &header, sizeof(header));
88}
89
90/*
91 * Perform a rudimentary verification of header and return
92 * size of image.
93 */
94static int verify_header(const uint8_t *buf)
95{
Marek Vasut9f0021a2018-04-15 13:38:49 +020096 struct socfpga_header header;
97
Charles Manning832472a2014-03-06 15:40:50 +130098 memcpy(&header, buf, sizeof(header));
99
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200100 if (le32_to_cpu(header.validation) != VALIDATION_WORD)
Charles Manning832472a2014-03-06 15:40:50 +1300101 return -1;
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200102 if (le16_to_cpu(header.checksum) != hdr_checksum(&header))
Charles Manning832472a2014-03-06 15:40:50 +1300103 return -1;
104
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200105 return le16_to_cpu(header.length_u32) * 4;
Charles Manning832472a2014-03-06 15:40:50 +1300106}
107
108/* Sign the buffer and return the signed buffer size */
109static int sign_buffer(uint8_t *buf,
110 uint8_t version, uint8_t flags,
111 int len, int pad_64k)
112{
113 uint32_t calc_crc;
114
115 /* Align the length up */
116 len = (len + 3) & (~3);
117
118 /* Build header, adding 4 bytes to length to hold the CRC32. */
119 build_header(buf + HEADER_OFFSET, version, flags, len + 4);
120
121 /* Calculate and apply the CRC */
122 calc_crc = ~pbl_crc32(0, (char *)buf, len);
123
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200124 *((uint32_t *)(buf + len)) = cpu_to_le32(calc_crc);
Charles Manning832472a2014-03-06 15:40:50 +1300125
126 if (!pad_64k)
127 return len + 4;
128
129 return PADDED_SIZE;
130}
131
132/* Verify that the buffer looks sane */
133static int verify_buffer(const uint8_t *buf)
134{
135 int len; /* Including 32bit CRC */
136 uint32_t calc_crc;
137 uint32_t buf_crc;
138
139 len = verify_header(buf + HEADER_OFFSET);
140 if (len < 0) {
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -0200141 debug("Invalid header\n");
Charles Manning832472a2014-03-06 15:40:50 +1300142 return -1;
143 }
144
145 if (len < HEADER_OFFSET || len > PADDED_SIZE) {
Guilherme Maciel Ferreira26621792015-01-15 02:54:43 -0200146 debug("Invalid header length (%i)\n", len);
Charles Manning832472a2014-03-06 15:40:50 +1300147 return -1;
148 }
149
150 /*
151 * Adjust length to the base of the CRC.
152 * Check the CRC.
153 */
154 len -= 4;
155
156 calc_crc = ~pbl_crc32(0, (const char *)buf, len);
157
Andreas Bießmann686ed2c22014-10-24 23:39:10 +0200158 buf_crc = le32_to_cpu(*((uint32_t *)(buf + len)));
Charles Manning832472a2014-03-06 15:40:50 +1300159
160 if (buf_crc != calc_crc) {
161 fprintf(stderr, "CRC32 does not match (%08x != %08x)\n",
162 buf_crc, calc_crc);
163 return -1;
164 }
165
166 return 0;
167}
168
169/* mkimage glue functions */
170static int socfpgaimage_verify_header(unsigned char *ptr, int image_size,
171 struct image_tool_params *params)
172{
173 if (image_size != PADDED_SIZE)
174 return -1;
175
176 return verify_buffer(ptr);
177}
178
179static void socfpgaimage_print_header(const void *ptr)
180{
181 if (verify_buffer(ptr) == 0)
182 printf("Looks like a sane SOCFPGA preloader\n");
183 else
184 printf("Not a sane SOCFPGA preloader\n");
185}
186
187static int socfpgaimage_check_params(struct image_tool_params *params)
188{
189 /* Not sure if we should be accepting fflags */
190 return (params->dflag && (params->fflag || params->lflag)) ||
191 (params->fflag && (params->dflag || params->lflag)) ||
192 (params->lflag && (params->dflag || params->fflag));
193}
194
195static int socfpgaimage_check_image_types(uint8_t type)
196{
197 if (type == IH_TYPE_SOCFPGAIMAGE)
198 return EXIT_SUCCESS;
199 return EXIT_FAILURE;
200}
201
202/*
203 * To work in with the mkimage framework, we do some ugly stuff...
204 *
205 * First, socfpgaimage_vrec_header() is called.
206 * We prepend a fake header big enough to make the file PADDED_SIZE.
207 * This gives us enough space to do what we want later.
208 *
209 * Next, socfpgaimage_set_header() is called.
210 * We fix up the buffer by moving the image to the start of the buffer.
211 * We now have some room to do what we need (add CRC and padding).
212 */
213
214static int data_size;
215#define FAKE_HEADER_SIZE (PADDED_SIZE - data_size)
216
217static int socfpgaimage_vrec_header(struct image_tool_params *params,
218 struct image_type_params *tparams)
219{
220 struct stat sbuf;
221
222 if (params->datafile &&
223 stat(params->datafile, &sbuf) == 0 &&
224 sbuf.st_size <= MAX_INPUT_SIZE) {
225 data_size = sbuf.st_size;
226 tparams->header_size = FAKE_HEADER_SIZE;
227 }
228 return 0;
229}
230
231static void socfpgaimage_set_header(void *ptr, struct stat *sbuf, int ifd,
232 struct image_tool_params *params)
233{
234 uint8_t *buf = (uint8_t *)ptr;
235
236 /*
237 * This function is called after vrec_header() has been called.
238 * At this stage we have the FAKE_HEADER_SIZE dummy bytes followed by
239 * data_size image bytes. Total = PADDED_SIZE.
240 * We need to fix the buffer by moving the image bytes back to
241 * the beginning of the buffer, then actually do the signing stuff...
242 */
243 memmove(buf, buf + FAKE_HEADER_SIZE, data_size);
244 memset(buf + data_size, 0, FAKE_HEADER_SIZE);
245
246 sign_buffer(buf, 0, 0, data_size, 0);
247}
248
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200249U_BOOT_IMAGE_TYPE(
250 socfpgaimage,
251 "Altera SOCFPGA preloader support",
252 0, /* This will be modified by vrec_header() */
253 (void *)buffer,
254 socfpgaimage_check_params,
255 socfpgaimage_verify_header,
256 socfpgaimage_print_header,
257 socfpgaimage_set_header,
258 NULL,
259 socfpgaimage_check_image_types,
260 NULL,
261 socfpgaimage_vrec_header
262);