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