socfpgaimage.c 11 KB

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