socfpgaimage.c 11 KB

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