2 * Copyright (C) 2014 Charles Manning <cdhmanning@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
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.
10 * "Header" is a structure of the following format.
11 * this is positioned at 0x40.
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).
22 * 0x4A 2 Checksum over the header. NB Not CRC32
24 * At the end of the code we have a 32-bit CRC checksum over whole binary
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.
30 * The image is padded out to 64k, because that is what is
31 * typically used to write the image to the boot medium.
34 #include "pbl_crc32.h"
35 #include "imagetool.h"
38 #define HEADER_OFFSET 0x40
39 #define VALIDATION_WORD 0x31305341
40 #define PADDED_SIZE 0x10000
42 /* To allow for adding CRC, the max input size is a bit smaller. */
43 #define MAX_INPUT_SIZE (PADDED_SIZE - sizeof(uint32_t))
45 static uint8_t buffer
[PADDED_SIZE
];
47 static struct socfpga_header
{
57 * The header checksum is just a very simple checksum over
59 * There is still a crc32 over the whole lot.
61 static uint16_t hdr_checksum(struct socfpga_header
*header
)
63 int len
= sizeof(*header
) - sizeof(header
->checksum
);
64 uint8_t *buf
= (uint8_t *)header
;
74 static void build_header(uint8_t *buf
, uint8_t version
, uint8_t flags
,
75 uint16_t length_bytes
)
77 header
.validation
= cpu_to_le32(VALIDATION_WORD
);
78 header
.version
= version
;
80 header
.length_u32
= cpu_to_le16(length_bytes
/4);
82 header
.checksum
= cpu_to_le16(hdr_checksum(&header
));
84 memcpy(buf
, &header
, sizeof(header
));
88 * Perform a rudimentary verification of header and return
91 static int verify_header(const uint8_t *buf
)
93 memcpy(&header
, buf
, sizeof(header
));
95 if (le32_to_cpu(header
.validation
) != VALIDATION_WORD
)
97 if (le16_to_cpu(header
.checksum
) != hdr_checksum(&header
))
100 return le16_to_cpu(header
.length_u32
) * 4;
103 /* Sign the buffer and return the signed buffer size */
104 static int sign_buffer(uint8_t *buf
,
105 uint8_t version
, uint8_t flags
,
106 int len
, int pad_64k
)
110 /* Align the length up */
111 len
= (len
+ 3) & (~3);
113 /* Build header, adding 4 bytes to length to hold the CRC32. */
114 build_header(buf
+ HEADER_OFFSET
, version
, flags
, len
+ 4);
116 /* Calculate and apply the CRC */
117 calc_crc
= ~pbl_crc32(0, (char *)buf
, len
);
119 *((uint32_t *)(buf
+ len
)) = cpu_to_le32(calc_crc
);
127 /* Verify that the buffer looks sane */
128 static int verify_buffer(const uint8_t *buf
)
130 int len
; /* Including 32bit CRC */
134 len
= verify_header(buf
+ HEADER_OFFSET
);
136 fprintf(stderr
, "Invalid header\n");
140 if (len
< HEADER_OFFSET
|| len
> PADDED_SIZE
) {
141 fprintf(stderr
, "Invalid header length (%i)\n", len
);
146 * Adjust length to the base of the CRC.
151 calc_crc
= ~pbl_crc32(0, (const char *)buf
, len
);
153 buf_crc
= le32_to_cpu(*((uint32_t *)(buf
+ len
)));
155 if (buf_crc
!= calc_crc
) {
156 fprintf(stderr
, "CRC32 does not match (%08x != %08x)\n",
164 /* mkimage glue functions */
165 static int socfpgaimage_verify_header(unsigned char *ptr
, int image_size
,
166 struct image_tool_params
*params
)
168 if (image_size
!= PADDED_SIZE
)
171 return verify_buffer(ptr
);
174 static void socfpgaimage_print_header(const void *ptr
)
176 if (verify_buffer(ptr
) == 0)
177 printf("Looks like a sane SOCFPGA preloader\n");
179 printf("Not a sane SOCFPGA preloader\n");
182 static int socfpgaimage_check_params(struct image_tool_params
*params
)
184 /* Not sure if we should be accepting fflags */
185 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
186 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
187 (params
->lflag
&& (params
->dflag
|| params
->fflag
));
190 static int socfpgaimage_check_image_types(uint8_t type
)
192 if (type
== IH_TYPE_SOCFPGAIMAGE
)
198 * To work in with the mkimage framework, we do some ugly stuff...
200 * First, socfpgaimage_vrec_header() is called.
201 * We prepend a fake header big enough to make the file PADDED_SIZE.
202 * This gives us enough space to do what we want later.
204 * Next, socfpgaimage_set_header() is called.
205 * We fix up the buffer by moving the image to the start of the buffer.
206 * We now have some room to do what we need (add CRC and padding).
209 static int data_size
;
210 #define FAKE_HEADER_SIZE (PADDED_SIZE - data_size)
212 static int socfpgaimage_vrec_header(struct image_tool_params
*params
,
213 struct image_type_params
*tparams
)
217 if (params
->datafile
&&
218 stat(params
->datafile
, &sbuf
) == 0 &&
219 sbuf
.st_size
<= MAX_INPUT_SIZE
) {
220 data_size
= sbuf
.st_size
;
221 tparams
->header_size
= FAKE_HEADER_SIZE
;
226 static void socfpgaimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
227 struct image_tool_params
*params
)
229 uint8_t *buf
= (uint8_t *)ptr
;
232 * This function is called after vrec_header() has been called.
233 * At this stage we have the FAKE_HEADER_SIZE dummy bytes followed by
234 * data_size image bytes. Total = PADDED_SIZE.
235 * We need to fix the buffer by moving the image bytes back to
236 * the beginning of the buffer, then actually do the signing stuff...
238 memmove(buf
, buf
+ FAKE_HEADER_SIZE
, data_size
);
239 memset(buf
+ data_size
, 0, FAKE_HEADER_SIZE
);
241 sign_buffer(buf
, 0, 0, data_size
, 0);
244 static struct image_type_params socfpgaimage_params
= {
245 .name
= "Altera SOCFPGA preloader support",
246 .vrec_header
= socfpgaimage_vrec_header
,
247 .header_size
= 0, /* This will be modified by vrec_header() */
248 .hdr
= (void *)buffer
,
249 .check_image_type
= socfpgaimage_check_image_types
,
250 .verify_header
= socfpgaimage_verify_header
,
251 .print_header
= socfpgaimage_print_header
,
252 .set_header
= socfpgaimage_set_header
,
253 .check_params
= socfpgaimage_check_params
,
256 void init_socfpga_image_type(void)
258 register_image_type(&socfpgaimage_params
);