socfpgaimage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Charles Manning <cdhmanning@gmail.com>
  4. *
  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
  9. *
  10. * Bootable SoCFPGA image requires a structure of the following format
  11. * positioned at offset 0x40 of the bootable image. Endian is LSB.
  12. *
  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.
  16. *
  17. * Version 0:
  18. * Offset Length Usage
  19. * -----------------------
  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)
  25. * 0x4A 2 Checksum over the header. NB Not CRC32
  26. *
  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. *
  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. *
  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. *
  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. #include "pbl_crc32.h"
  55. #include "imagetool.h"
  56. #include "mkimage.h"
  57. #include <image.h>
  58. #define HEADER_OFFSET 0x40
  59. #define VALIDATION_WORD 0x31305341
  60. static uint8_t buffer_v0[0x10000];
  61. static uint8_t buffer_v1[0x40000];
  62. struct socfpga_header_v0 {
  63. uint32_t validation;
  64. uint8_t version;
  65. uint8_t flags;
  66. uint16_t length_u32;
  67. uint16_t zero;
  68. uint16_t checksum;
  69. };
  70. struct socfpga_header_v1 {
  71. uint32_t validation;
  72. uint8_t version;
  73. uint8_t flags;
  74. uint16_t header_u8;
  75. uint32_t length_u8;
  76. uint32_t entry_offset;
  77. uint16_t zero;
  78. uint16_t checksum;
  79. };
  80. static unsigned int sfp_hdr_size(uint8_t ver)
  81. {
  82. if (ver == 0)
  83. return sizeof(struct socfpga_header_v0);
  84. if (ver == 1)
  85. return sizeof(struct socfpga_header_v1);
  86. return 0;
  87. }
  88. static unsigned int sfp_pad_size(uint8_t ver)
  89. {
  90. if (ver == 0)
  91. return sizeof(buffer_v0);
  92. if (ver == 1)
  93. return sizeof(buffer_v1);
  94. return 0;
  95. }
  96. /*
  97. * The header checksum is just a very simple checksum over
  98. * the header area.
  99. * There is still a crc32 over the whole lot.
  100. */
  101. static uint16_t sfp_hdr_checksum(uint8_t *buf, unsigned char ver)
  102. {
  103. uint16_t ret = 0;
  104. int len = sfp_hdr_size(ver) - sizeof(ret);
  105. while (--len)
  106. ret += *buf++;
  107. return ret;
  108. }
  109. static void sfp_build_header(uint8_t *buf, uint8_t ver, uint8_t flags,
  110. uint32_t length_bytes)
  111. {
  112. struct socfpga_header_v0 header_v0 = {
  113. .validation = cpu_to_le32(VALIDATION_WORD),
  114. .version = 0,
  115. .flags = flags,
  116. .length_u32 = cpu_to_le16(length_bytes / 4),
  117. .zero = 0,
  118. };
  119. struct socfpga_header_v1 header_v1 = {
  120. .validation = cpu_to_le32(VALIDATION_WORD),
  121. .version = 1,
  122. .flags = flags,
  123. .header_u8 = cpu_to_le16(sizeof(header_v1)),
  124. .length_u8 = cpu_to_le32(length_bytes),
  125. .entry_offset = cpu_to_le32(0x14), /* Trampoline offset */
  126. .zero = 0,
  127. };
  128. uint16_t csum;
  129. if (ver == 0) {
  130. csum = sfp_hdr_checksum((uint8_t *)&header_v0, 0);
  131. header_v0.checksum = cpu_to_le16(csum);
  132. memcpy(buf, &header_v0, sizeof(header_v0));
  133. } else {
  134. csum = sfp_hdr_checksum((uint8_t *)&header_v1, 1);
  135. header_v1.checksum = cpu_to_le16(csum);
  136. memcpy(buf, &header_v1, sizeof(header_v1));
  137. }
  138. }
  139. /*
  140. * Perform a rudimentary verification of header and return
  141. * size of image.
  142. */
  143. static int sfp_verify_header(const uint8_t *buf, uint8_t *ver)
  144. {
  145. struct socfpga_header_v0 header_v0;
  146. struct socfpga_header_v1 header_v1;
  147. uint16_t hdr_csum, sfp_csum;
  148. uint32_t img_len;
  149. /*
  150. * Header v0 is always smaller than Header v1 and the validation
  151. * word and version field is at the same place, so use Header v0
  152. * to check for version during verifiction and upgrade to Header
  153. * v1 if needed.
  154. */
  155. memcpy(&header_v0, buf, sizeof(header_v0));
  156. if (le32_to_cpu(header_v0.validation) != VALIDATION_WORD)
  157. return -1;
  158. if (header_v0.version == 0) {
  159. hdr_csum = le16_to_cpu(header_v0.checksum);
  160. sfp_csum = sfp_hdr_checksum((uint8_t *)&header_v0, 0);
  161. img_len = le16_to_cpu(header_v0.length_u32) * 4;
  162. } else if (header_v0.version == 1) {
  163. memcpy(&header_v1, buf, sizeof(header_v1));
  164. hdr_csum = le16_to_cpu(header_v1.checksum);
  165. sfp_csum = sfp_hdr_checksum((uint8_t *)&header_v1, 1);
  166. img_len = le32_to_cpu(header_v1.length_u8);
  167. } else { /* Invalid version */
  168. return -EINVAL;
  169. }
  170. /* Verify checksum */
  171. if (hdr_csum != sfp_csum)
  172. return -EINVAL;
  173. return img_len;
  174. }
  175. /* Sign the buffer and return the signed buffer size */
  176. static int sfp_sign_buffer(uint8_t *buf, uint8_t ver, uint8_t flags,
  177. int len, int pad_64k)
  178. {
  179. uint32_t calc_crc;
  180. /* Align the length up */
  181. len = (len + 3) & ~3;
  182. /* Build header, adding 4 bytes to length to hold the CRC32. */
  183. sfp_build_header(buf + HEADER_OFFSET, ver, flags, len + 4);
  184. /* Calculate and apply the CRC */
  185. calc_crc = ~pbl_crc32(0, (char *)buf, len);
  186. *((uint32_t *)(buf + len)) = cpu_to_le32(calc_crc);
  187. if (!pad_64k)
  188. return len + 4;
  189. return sfp_pad_size(ver);
  190. }
  191. /* Verify that the buffer looks sane */
  192. static int sfp_verify_buffer(const uint8_t *buf)
  193. {
  194. int len; /* Including 32bit CRC */
  195. uint32_t calc_crc;
  196. uint32_t buf_crc;
  197. uint8_t ver = 0;
  198. len = sfp_verify_header(buf + HEADER_OFFSET, &ver);
  199. if (len < 0) {
  200. debug("Invalid header\n");
  201. return -1;
  202. }
  203. if (len < HEADER_OFFSET || len > sfp_pad_size(ver)) {
  204. debug("Invalid header length (%i)\n", len);
  205. return -1;
  206. }
  207. /*
  208. * Adjust length to the base of the CRC.
  209. * Check the CRC.
  210. */
  211. len -= 4;
  212. calc_crc = ~pbl_crc32(0, (const char *)buf, len);
  213. buf_crc = le32_to_cpu(*((uint32_t *)(buf + len)));
  214. if (buf_crc != calc_crc) {
  215. fprintf(stderr, "CRC32 does not match (%08x != %08x)\n",
  216. buf_crc, calc_crc);
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. /* mkimage glue functions */
  222. static int socfpgaimage_verify_header(unsigned char *ptr, int image_size,
  223. struct image_tool_params *params)
  224. {
  225. if (image_size < 0x80)
  226. return -1;
  227. return sfp_verify_buffer(ptr);
  228. }
  229. static void socfpgaimage_print_header(const void *ptr)
  230. {
  231. if (sfp_verify_buffer(ptr) == 0)
  232. printf("Looks like a sane SOCFPGA preloader\n");
  233. else
  234. printf("Not a sane SOCFPGA preloader\n");
  235. }
  236. static int socfpgaimage_check_params(struct image_tool_params *params)
  237. {
  238. /* Not sure if we should be accepting fflags */
  239. return (params->dflag && (params->fflag || params->lflag)) ||
  240. (params->fflag && (params->dflag || params->lflag)) ||
  241. (params->lflag && (params->dflag || params->fflag));
  242. }
  243. static int socfpgaimage_check_image_types_v0(uint8_t type)
  244. {
  245. if (type == IH_TYPE_SOCFPGAIMAGE)
  246. return EXIT_SUCCESS;
  247. return EXIT_FAILURE;
  248. }
  249. static int socfpgaimage_check_image_types_v1(uint8_t type)
  250. {
  251. if (type == IH_TYPE_SOCFPGAIMAGE_V1)
  252. return EXIT_SUCCESS;
  253. return EXIT_FAILURE;
  254. }
  255. /*
  256. * To work in with the mkimage framework, we do some ugly stuff...
  257. *
  258. * First, socfpgaimage_vrec_header() is called.
  259. * We prepend a fake header big enough to make the file sfp_pad_size().
  260. * This gives us enough space to do what we want later.
  261. *
  262. * Next, socfpgaimage_set_header() is called.
  263. * We fix up the buffer by moving the image to the start of the buffer.
  264. * We now have some room to do what we need (add CRC and padding).
  265. */
  266. static int data_size;
  267. static int sfp_fake_header_size(unsigned int size, uint8_t ver)
  268. {
  269. return sfp_pad_size(ver) - size;
  270. }
  271. static int sfp_vrec_header(struct image_tool_params *params,
  272. struct image_type_params *tparams, uint8_t ver)
  273. {
  274. struct stat sbuf;
  275. if (params->datafile &&
  276. stat(params->datafile, &sbuf) == 0 &&
  277. sbuf.st_size <= (sfp_pad_size(ver) - sizeof(uint32_t))) {
  278. data_size = sbuf.st_size;
  279. tparams->header_size = sfp_fake_header_size(data_size, ver);
  280. }
  281. return 0;
  282. }
  283. static int socfpgaimage_vrec_header_v0(struct image_tool_params *params,
  284. struct image_type_params *tparams)
  285. {
  286. return sfp_vrec_header(params, tparams, 0);
  287. }
  288. static int socfpgaimage_vrec_header_v1(struct image_tool_params *params,
  289. struct image_type_params *tparams)
  290. {
  291. return sfp_vrec_header(params, tparams, 1);
  292. }
  293. static void sfp_set_header(void *ptr, unsigned char ver)
  294. {
  295. uint8_t *buf = (uint8_t *)ptr;
  296. /*
  297. * This function is called after vrec_header() has been called.
  298. * At this stage we have the sfp_fake_header_size() dummy bytes
  299. * followed by data_size image bytes. Total = sfp_pad_size().
  300. * We need to fix the buffer by moving the image bytes back to
  301. * the beginning of the buffer, then actually do the signing stuff...
  302. */
  303. memmove(buf, buf + sfp_fake_header_size(data_size, ver), data_size);
  304. memset(buf + data_size, 0, sfp_fake_header_size(data_size, ver));
  305. sfp_sign_buffer(buf, ver, 0, data_size, 0);
  306. }
  307. static void socfpgaimage_set_header_v0(void *ptr, struct stat *sbuf, int ifd,
  308. struct image_tool_params *params)
  309. {
  310. sfp_set_header(ptr, 0);
  311. }
  312. static void socfpgaimage_set_header_v1(void *ptr, struct stat *sbuf, int ifd,
  313. struct image_tool_params *params)
  314. {
  315. sfp_set_header(ptr, 1);
  316. }
  317. U_BOOT_IMAGE_TYPE(
  318. socfpgaimage,
  319. "Altera SoCFPGA Cyclone V / Arria V image support",
  320. 0, /* This will be modified by vrec_header() */
  321. (void *)buffer_v0,
  322. socfpgaimage_check_params,
  323. socfpgaimage_verify_header,
  324. socfpgaimage_print_header,
  325. socfpgaimage_set_header_v0,
  326. NULL,
  327. socfpgaimage_check_image_types_v0,
  328. NULL,
  329. socfpgaimage_vrec_header_v0
  330. );
  331. U_BOOT_IMAGE_TYPE(
  332. socfpgaimage_v1,
  333. "Altera SoCFPGA Arria10 image support",
  334. 0, /* This will be modified by vrec_header() */
  335. (void *)buffer_v1,
  336. socfpgaimage_check_params,
  337. socfpgaimage_verify_header,
  338. socfpgaimage_print_header,
  339. socfpgaimage_set_header_v1,
  340. NULL,
  341. socfpgaimage_check_image_types_v1,
  342. NULL,
  343. socfpgaimage_vrec_header_v1
  344. );