socfpgaimage.c 13 KB

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