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