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