socfpgaimage.c 6.7 KB

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