Blame view
tools/socfpgaimage.c
10.6 KB
83d290c56 SPDX: Convert all... |
1 |
// SPDX-License-Identifier: GPL-2.0+ |
832472a94 tools: socfpga: A... |
2 3 4 |
/* * Copyright (C) 2014 Charles Manning <cdhmanning@gmail.com> * |
cece78faf tools: socfpga: A... |
5 6 7 8 |
* Reference documents: * Cyclone V SoC: https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/cyclone-v/cv_5400a.pdf * Arria V SoC: https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/arria-v/av_5400a.pdf * Arria 10 SoC: https://www.altera.com/content/dam/altera-www/global/en_US/pdfs/literature/hb/arria-10/a10_5400a.pdf |
832472a94 tools: socfpga: A... |
9 |
* |
cece78faf tools: socfpga: A... |
10 11 |
* Bootable SoCFPGA image requires a structure of the following format * positioned at offset 0x40 of the bootable image. Endian is LSB. |
832472a94 tools: socfpga: A... |
12 |
* |
cece78faf tools: socfpga: A... |
13 14 15 |
* There are two versions of the SoCFPGA header format, v0 and v1. * The version 0 is used by Cyclone V SoC and Arria V SoC, while * the version 1 is used by the Arria 10 SoC. |
832472a94 tools: socfpga: A... |
16 |
* |
cece78faf tools: socfpga: A... |
17 |
* Version 0: |
832472a94 tools: socfpga: A... |
18 19 |
* Offset Length Usage * ----------------------- |
cece78faf tools: socfpga: A... |
20 21 22 23 24 |
* 0x40 4 Validation word (0x31305341) * 0x44 1 Version (0x0) * 0x45 1 Flags (unused, zero is fine) * 0x46 2 Length (in units of u32, including the end checksum). * 0x48 2 Zero (0x0) |
832472a94 tools: socfpga: A... |
25 26 |
* 0x4A 2 Checksum over the header. NB Not CRC32 * |
cece78faf tools: socfpga: A... |
27 28 29 30 31 32 33 34 35 36 37 38 |
* Version 1: * Offset Length Usage * ----------------------- * 0x40 4 Validation word (0x31305341) * 0x44 1 Version (0x1) * 0x45 1 Flags (unused, zero is fine) * 0x46 2 Header length (in units of u8). * 0x48 4 Length (in units of u8). * 0x4C 4 Image entry offset from standard of header * 0x50 2 Zero (0x0) * 0x52 2 Checksum over the header. NB Not CRC32 * |
832472a94 tools: socfpga: A... |
39 40 41 42 43 44 |
* At the end of the code we have a 32-bit CRC checksum over whole binary * excluding the CRC. * * Note that the CRC used here is **not** the zlib/Adler crc32. It is the * CRC-32 used in bzip2, ethernet and elsewhere. * |
cece78faf tools: socfpga: A... |
45 46 47 48 49 50 |
* The Image entry offset in version 1 image is relative the the start of * the header, 0x40, and must not be a negative number. Therefore, it is * only possible to make the SoCFPGA jump forward. The U-Boot bootloader * places a trampoline instruction at offset 0x5c, 0x14 bytes from the * start of the SoCFPGA header, which jumps to the reset vector. * |
832472a94 tools: socfpga: A... |
51 52 53 54 55 56 |
* The image is padded out to 64k, because that is what is * typically used to write the image to the boot medium. */ #include "pbl_crc32.h" #include "imagetool.h" |
266217999 tools: do not pri... |
57 |
#include "mkimage.h" |
3db711085 crc32: Use the cr... |
58 |
#include <u-boot/crc.h> |
266217999 tools: do not pri... |
59 |
|
832472a94 tools: socfpga: A... |
60 61 62 63 |
#include <image.h> #define HEADER_OFFSET 0x40 #define VALIDATION_WORD 0x31305341 |
832472a94 tools: socfpga: A... |
64 |
|
cece78faf tools: socfpga: A... |
65 66 |
static uint8_t buffer_v0[0x10000]; static uint8_t buffer_v1[0x40000]; |
832472a94 tools: socfpga: A... |
67 |
|
cece78faf tools: socfpga: A... |
68 69 70 71 72 73 74 75 |
struct socfpga_header_v0 { uint32_t validation; uint8_t version; uint8_t flags; uint16_t length_u32; uint16_t zero; uint16_t checksum; }; |
832472a94 tools: socfpga: A... |
76 |
|
cece78faf tools: socfpga: A... |
77 78 79 80 81 82 83 84 85 |
struct socfpga_header_v1 { uint32_t validation; uint8_t version; uint8_t flags; uint16_t header_u8; uint32_t length_u8; uint32_t entry_offset; uint16_t zero; uint16_t checksum; |
9f0021a50 tools: socfpga: S... |
86 |
}; |
832472a94 tools: socfpga: A... |
87 |
|
cece78faf tools: socfpga: A... |
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
static unsigned int sfp_hdr_size(uint8_t ver) { if (ver == 0) return sizeof(struct socfpga_header_v0); if (ver == 1) return sizeof(struct socfpga_header_v1); return 0; } static unsigned int sfp_pad_size(uint8_t ver) { if (ver == 0) return sizeof(buffer_v0); if (ver == 1) return sizeof(buffer_v1); return 0; } |
832472a94 tools: socfpga: A... |
105 106 107 108 109 |
/* * The header checksum is just a very simple checksum over * the header area. * There is still a crc32 over the whole lot. */ |
cece78faf tools: socfpga: A... |
110 |
static uint16_t sfp_hdr_checksum(uint8_t *buf, unsigned char ver) |
832472a94 tools: socfpga: A... |
111 |
{ |
832472a94 tools: socfpga: A... |
112 |
uint16_t ret = 0; |
cece78faf tools: socfpga: A... |
113 |
int len = sfp_hdr_size(ver) - sizeof(ret); |
832472a94 tools: socfpga: A... |
114 115 116 117 118 119 |
while (--len) ret += *buf++; return ret; } |
cece78faf tools: socfpga: A... |
120 121 |
static void sfp_build_header(uint8_t *buf, uint8_t ver, uint8_t flags, uint32_t length_bytes) |
832472a94 tools: socfpga: A... |
122 |
{ |
cece78faf tools: socfpga: A... |
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
struct socfpga_header_v0 header_v0 = { .validation = cpu_to_le32(VALIDATION_WORD), .version = 0, .flags = flags, .length_u32 = cpu_to_le16(length_bytes / 4), .zero = 0, }; struct socfpga_header_v1 header_v1 = { .validation = cpu_to_le32(VALIDATION_WORD), .version = 1, .flags = flags, .header_u8 = cpu_to_le16(sizeof(header_v1)), .length_u8 = cpu_to_le32(length_bytes), .entry_offset = cpu_to_le32(0x14), /* Trampoline offset */ .zero = 0, }; uint16_t csum; if (ver == 0) { csum = sfp_hdr_checksum((uint8_t *)&header_v0, 0); header_v0.checksum = cpu_to_le16(csum); memcpy(buf, &header_v0, sizeof(header_v0)); } else { csum = sfp_hdr_checksum((uint8_t *)&header_v1, 1); header_v1.checksum = cpu_to_le16(csum); memcpy(buf, &header_v1, sizeof(header_v1)); } |
832472a94 tools: socfpga: A... |
152 153 154 155 156 157 |
} /* * Perform a rudimentary verification of header and return * size of image. */ |
cece78faf tools: socfpga: A... |
158 |
static int sfp_verify_header(const uint8_t *buf, uint8_t *ver) |
832472a94 tools: socfpga: A... |
159 |
{ |
cece78faf tools: socfpga: A... |
160 161 162 163 |
struct socfpga_header_v0 header_v0; struct socfpga_header_v1 header_v1; uint16_t hdr_csum, sfp_csum; uint32_t img_len; |
9f0021a50 tools: socfpga: S... |
164 |
|
cece78faf tools: socfpga: A... |
165 166 167 168 169 170 171 |
/* * Header v0 is always smaller than Header v1 and the validation * word and version field is at the same place, so use Header v0 * to check for version during verifiction and upgrade to Header * v1 if needed. */ memcpy(&header_v0, buf, sizeof(header_v0)); |
832472a94 tools: socfpga: A... |
172 |
|
cece78faf tools: socfpga: A... |
173 |
if (le32_to_cpu(header_v0.validation) != VALIDATION_WORD) |
832472a94 tools: socfpga: A... |
174 |
return -1; |
cece78faf tools: socfpga: A... |
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
if (header_v0.version == 0) { hdr_csum = le16_to_cpu(header_v0.checksum); sfp_csum = sfp_hdr_checksum((uint8_t *)&header_v0, 0); img_len = le16_to_cpu(header_v0.length_u32) * 4; } else if (header_v0.version == 1) { memcpy(&header_v1, buf, sizeof(header_v1)); hdr_csum = le16_to_cpu(header_v1.checksum); sfp_csum = sfp_hdr_checksum((uint8_t *)&header_v1, 1); img_len = le32_to_cpu(header_v1.length_u8); } else { /* Invalid version */ return -EINVAL; } /* Verify checksum */ if (hdr_csum != sfp_csum) return -EINVAL; |
0ca8fd37a tools: socfpga: f... |
191 |
*ver = header_v0.version; |
cece78faf tools: socfpga: A... |
192 |
return img_len; |
832472a94 tools: socfpga: A... |
193 194 195 |
} /* Sign the buffer and return the signed buffer size */ |
cece78faf tools: socfpga: A... |
196 197 |
static int sfp_sign_buffer(uint8_t *buf, uint8_t ver, uint8_t flags, int len, int pad_64k) |
832472a94 tools: socfpga: A... |
198 199 200 201 |
{ uint32_t calc_crc; /* Align the length up */ |
cece78faf tools: socfpga: A... |
202 |
len = (len + 3) & ~3; |
832472a94 tools: socfpga: A... |
203 204 |
/* Build header, adding 4 bytes to length to hold the CRC32. */ |
cece78faf tools: socfpga: A... |
205 |
sfp_build_header(buf + HEADER_OFFSET, ver, flags, len + 4); |
832472a94 tools: socfpga: A... |
206 207 208 |
/* Calculate and apply the CRC */ calc_crc = ~pbl_crc32(0, (char *)buf, len); |
686ed2c22 tools/socfpgaimag... |
209 |
*((uint32_t *)(buf + len)) = cpu_to_le32(calc_crc); |
832472a94 tools: socfpga: A... |
210 211 212 |
if (!pad_64k) return len + 4; |
cece78faf tools: socfpga: A... |
213 |
return sfp_pad_size(ver); |
832472a94 tools: socfpga: A... |
214 215 216 |
} /* Verify that the buffer looks sane */ |
cece78faf tools: socfpga: A... |
217 |
static int sfp_verify_buffer(const uint8_t *buf) |
832472a94 tools: socfpga: A... |
218 219 220 221 |
{ int len; /* Including 32bit CRC */ uint32_t calc_crc; uint32_t buf_crc; |
cece78faf tools: socfpga: A... |
222 |
uint8_t ver = 0; |
832472a94 tools: socfpga: A... |
223 |
|
cece78faf tools: socfpga: A... |
224 |
len = sfp_verify_header(buf + HEADER_OFFSET, &ver); |
832472a94 tools: socfpga: A... |
225 |
if (len < 0) { |
266217999 tools: do not pri... |
226 227 |
debug("Invalid header "); |
832472a94 tools: socfpga: A... |
228 229 |
return -1; } |
cece78faf tools: socfpga: A... |
230 |
if (len < HEADER_OFFSET || len > sfp_pad_size(ver)) { |
266217999 tools: do not pri... |
231 232 |
debug("Invalid header length (%i) ", len); |
832472a94 tools: socfpga: A... |
233 234 235 236 237 238 239 240 241 242 |
return -1; } /* * Adjust length to the base of the CRC. * Check the CRC. */ len -= 4; calc_crc = ~pbl_crc32(0, (const char *)buf, len); |
686ed2c22 tools/socfpgaimag... |
243 |
buf_crc = le32_to_cpu(*((uint32_t *)(buf + len))); |
832472a94 tools: socfpga: A... |
244 245 246 247 248 249 250 251 252 253 254 255 256 |
if (buf_crc != calc_crc) { fprintf(stderr, "CRC32 does not match (%08x != %08x) ", buf_crc, calc_crc); return -1; } return 0; } /* mkimage glue functions */ static int socfpgaimage_verify_header(unsigned char *ptr, int image_size, |
cece78faf tools: socfpga: A... |
257 |
struct image_tool_params *params) |
832472a94 tools: socfpga: A... |
258 |
{ |
cece78faf tools: socfpga: A... |
259 |
if (image_size < 0x80) |
832472a94 tools: socfpga: A... |
260 |
return -1; |
cece78faf tools: socfpga: A... |
261 |
return sfp_verify_buffer(ptr); |
832472a94 tools: socfpga: A... |
262 263 264 265 |
} static void socfpgaimage_print_header(const void *ptr) { |
cece78faf tools: socfpga: A... |
266 |
if (sfp_verify_buffer(ptr) == 0) |
832472a94 tools: socfpga: A... |
267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
printf("Looks like a sane SOCFPGA preloader "); else printf("Not a sane SOCFPGA preloader "); } static int socfpgaimage_check_params(struct image_tool_params *params) { /* Not sure if we should be accepting fflags */ return (params->dflag && (params->fflag || params->lflag)) || (params->fflag && (params->dflag || params->lflag)) || (params->lflag && (params->dflag || params->fflag)); } |
cece78faf tools: socfpga: A... |
281 |
static int socfpgaimage_check_image_types_v0(uint8_t type) |
832472a94 tools: socfpga: A... |
282 283 284 285 286 |
{ if (type == IH_TYPE_SOCFPGAIMAGE) return EXIT_SUCCESS; return EXIT_FAILURE; } |
cece78faf tools: socfpga: A... |
287 288 289 290 291 292 |
static int socfpgaimage_check_image_types_v1(uint8_t type) { if (type == IH_TYPE_SOCFPGAIMAGE_V1) return EXIT_SUCCESS; return EXIT_FAILURE; } |
832472a94 tools: socfpga: A... |
293 294 295 296 |
/* * To work in with the mkimage framework, we do some ugly stuff... * * First, socfpgaimage_vrec_header() is called. |
cece78faf tools: socfpga: A... |
297 |
* We prepend a fake header big enough to make the file sfp_pad_size(). |
832472a94 tools: socfpga: A... |
298 299 300 301 302 303 304 305 |
* This gives us enough space to do what we want later. * * Next, socfpgaimage_set_header() is called. * We fix up the buffer by moving the image to the start of the buffer. * We now have some room to do what we need (add CRC and padding). */ static int data_size; |
832472a94 tools: socfpga: A... |
306 |
|
cece78faf tools: socfpga: A... |
307 308 309 310 311 312 313 |
static int sfp_fake_header_size(unsigned int size, uint8_t ver) { return sfp_pad_size(ver) - size; } static int sfp_vrec_header(struct image_tool_params *params, struct image_type_params *tparams, uint8_t ver) |
832472a94 tools: socfpga: A... |
314 315 316 317 318 |
{ struct stat sbuf; if (params->datafile && stat(params->datafile, &sbuf) == 0 && |
cece78faf tools: socfpga: A... |
319 |
sbuf.st_size <= (sfp_pad_size(ver) - sizeof(uint32_t))) { |
832472a94 tools: socfpga: A... |
320 |
data_size = sbuf.st_size; |
cece78faf tools: socfpga: A... |
321 |
tparams->header_size = sfp_fake_header_size(data_size, ver); |
832472a94 tools: socfpga: A... |
322 323 |
} return 0; |
cece78faf tools: socfpga: A... |
324 |
|
832472a94 tools: socfpga: A... |
325 |
} |
cece78faf tools: socfpga: A... |
326 327 328 329 330 331 332 333 334 335 336 337 338 |
static int socfpgaimage_vrec_header_v0(struct image_tool_params *params, struct image_type_params *tparams) { return sfp_vrec_header(params, tparams, 0); } static int socfpgaimage_vrec_header_v1(struct image_tool_params *params, struct image_type_params *tparams) { return sfp_vrec_header(params, tparams, 1); } static void sfp_set_header(void *ptr, unsigned char ver) |
832472a94 tools: socfpga: A... |
339 340 341 342 343 |
{ uint8_t *buf = (uint8_t *)ptr; /* * This function is called after vrec_header() has been called. |
cece78faf tools: socfpga: A... |
344 345 |
* At this stage we have the sfp_fake_header_size() dummy bytes * followed by data_size image bytes. Total = sfp_pad_size(). |
832472a94 tools: socfpga: A... |
346 347 348 |
* We need to fix the buffer by moving the image bytes back to * the beginning of the buffer, then actually do the signing stuff... */ |
cece78faf tools: socfpga: A... |
349 350 351 352 353 |
memmove(buf, buf + sfp_fake_header_size(data_size, ver), data_size); memset(buf + data_size, 0, sfp_fake_header_size(data_size, ver)); sfp_sign_buffer(buf, ver, 0, data_size, 0); } |
832472a94 tools: socfpga: A... |
354 |
|
cece78faf tools: socfpga: A... |
355 356 357 358 359 360 361 362 363 364 |
static void socfpgaimage_set_header_v0(void *ptr, struct stat *sbuf, int ifd, struct image_tool_params *params) { sfp_set_header(ptr, 0); } static void socfpgaimage_set_header_v1(void *ptr, struct stat *sbuf, int ifd, struct image_tool_params *params) { sfp_set_header(ptr, 1); |
832472a94 tools: socfpga: A... |
365 |
} |
a93648d19 imagetool: replac... |
366 367 |
U_BOOT_IMAGE_TYPE( socfpgaimage, |
cece78faf tools: socfpga: A... |
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
"Altera SoCFPGA Cyclone V / Arria V image support", 0, /* This will be modified by vrec_header() */ (void *)buffer_v0, socfpgaimage_check_params, socfpgaimage_verify_header, socfpgaimage_print_header, socfpgaimage_set_header_v0, NULL, socfpgaimage_check_image_types_v0, NULL, socfpgaimage_vrec_header_v0 ); U_BOOT_IMAGE_TYPE( socfpgaimage_v1, "Altera SoCFPGA Arria10 image support", |
a93648d19 imagetool: replac... |
384 |
0, /* This will be modified by vrec_header() */ |
cece78faf tools: socfpga: A... |
385 |
(void *)buffer_v1, |
a93648d19 imagetool: replac... |
386 387 388 |
socfpgaimage_check_params, socfpgaimage_verify_header, socfpgaimage_print_header, |
cece78faf tools: socfpga: A... |
389 |
socfpgaimage_set_header_v1, |
a93648d19 imagetool: replac... |
390 |
NULL, |
cece78faf tools: socfpga: A... |
391 |
socfpgaimage_check_image_types_v1, |
a93648d19 imagetool: replac... |
392 |
NULL, |
cece78faf tools: socfpga: A... |
393 |
socfpgaimage_vrec_header_v1 |
a93648d19 imagetool: replac... |
394 |
); |