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