socfpgaimage.c 6.5 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 "mkimage.h"
  36. #include <image.h>
  37. #define HEADER_OFFSET 0x40
  38. #define VALIDATION_WORD 0x31305341
  39. #define PADDED_SIZE 0x10000
  40. /* To allow for adding CRC, the max input size is a bit smaller. */
  41. #define MAX_INPUT_SIZE (PADDED_SIZE - sizeof(uint32_t))
  42. static uint8_t buffer[PADDED_SIZE];
  43. static struct socfpga_header {
  44. uint32_t validation;
  45. uint8_t version;
  46. uint8_t flags;
  47. uint16_t length_u32;
  48. uint16_t zero;
  49. uint16_t checksum;
  50. } header;
  51. /*
  52. * The header checksum is just a very simple checksum over
  53. * the header area.
  54. * There is still a crc32 over the whole lot.
  55. */
  56. static uint16_t hdr_checksum(struct socfpga_header *header)
  57. {
  58. int len = sizeof(*header) - sizeof(header->checksum);
  59. uint8_t *buf = (uint8_t *)header;
  60. uint16_t ret = 0;
  61. while (--len)
  62. ret += *buf++;
  63. return ret;
  64. }
  65. static void build_header(uint8_t *buf, uint8_t version, uint8_t flags,
  66. uint16_t length_bytes)
  67. {
  68. header.validation = cpu_to_le32(VALIDATION_WORD);
  69. header.version = version;
  70. header.flags = flags;
  71. header.length_u32 = cpu_to_le16(length_bytes/4);
  72. header.zero = 0;
  73. header.checksum = cpu_to_le16(hdr_checksum(&header));
  74. memcpy(buf, &header, sizeof(header));
  75. }
  76. /*
  77. * Perform a rudimentary verification of header and return
  78. * size of image.
  79. */
  80. static int verify_header(const uint8_t *buf)
  81. {
  82. memcpy(&header, buf, sizeof(header));
  83. if (le32_to_cpu(header.validation) != VALIDATION_WORD)
  84. return -1;
  85. if (le16_to_cpu(header.checksum) != hdr_checksum(&header))
  86. return -1;
  87. return le16_to_cpu(header.length_u32) * 4;
  88. }
  89. /* Sign the buffer and return the signed buffer size */
  90. static int sign_buffer(uint8_t *buf,
  91. uint8_t version, uint8_t flags,
  92. int len, int pad_64k)
  93. {
  94. uint32_t calc_crc;
  95. /* Align the length up */
  96. len = (len + 3) & (~3);
  97. /* Build header, adding 4 bytes to length to hold the CRC32. */
  98. build_header(buf + HEADER_OFFSET, version, flags, len + 4);
  99. /* Calculate and apply the CRC */
  100. calc_crc = ~pbl_crc32(0, (char *)buf, len);
  101. *((uint32_t *)(buf + len)) = cpu_to_le32(calc_crc);
  102. if (!pad_64k)
  103. return len + 4;
  104. return PADDED_SIZE;
  105. }
  106. /* Verify that the buffer looks sane */
  107. static int verify_buffer(const uint8_t *buf)
  108. {
  109. int len; /* Including 32bit CRC */
  110. uint32_t calc_crc;
  111. uint32_t buf_crc;
  112. len = verify_header(buf + HEADER_OFFSET);
  113. if (len < 0) {
  114. debug("Invalid header\n");
  115. return -1;
  116. }
  117. if (len < HEADER_OFFSET || len > PADDED_SIZE) {
  118. debug("Invalid header length (%i)\n", len);
  119. return -1;
  120. }
  121. /*
  122. * Adjust length to the base of the CRC.
  123. * Check the CRC.
  124. */
  125. len -= 4;
  126. calc_crc = ~pbl_crc32(0, (const char *)buf, len);
  127. buf_crc = le32_to_cpu(*((uint32_t *)(buf + len)));
  128. if (buf_crc != calc_crc) {
  129. fprintf(stderr, "CRC32 does not match (%08x != %08x)\n",
  130. buf_crc, calc_crc);
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. /* mkimage glue functions */
  136. static int socfpgaimage_verify_header(unsigned char *ptr, int image_size,
  137. struct image_tool_params *params)
  138. {
  139. if (image_size != PADDED_SIZE)
  140. return -1;
  141. return verify_buffer(ptr);
  142. }
  143. static void socfpgaimage_print_header(const void *ptr)
  144. {
  145. if (verify_buffer(ptr) == 0)
  146. printf("Looks like a sane SOCFPGA preloader\n");
  147. else
  148. printf("Not a sane SOCFPGA preloader\n");
  149. }
  150. static int socfpgaimage_check_params(struct image_tool_params *params)
  151. {
  152. /* Not sure if we should be accepting fflags */
  153. return (params->dflag && (params->fflag || params->lflag)) ||
  154. (params->fflag && (params->dflag || params->lflag)) ||
  155. (params->lflag && (params->dflag || params->fflag));
  156. }
  157. static int socfpgaimage_check_image_types(uint8_t type)
  158. {
  159. if (type == IH_TYPE_SOCFPGAIMAGE)
  160. return EXIT_SUCCESS;
  161. return EXIT_FAILURE;
  162. }
  163. /*
  164. * To work in with the mkimage framework, we do some ugly stuff...
  165. *
  166. * First, socfpgaimage_vrec_header() is called.
  167. * We prepend a fake header big enough to make the file PADDED_SIZE.
  168. * This gives us enough space to do what we want later.
  169. *
  170. * Next, socfpgaimage_set_header() is called.
  171. * We fix up the buffer by moving the image to the start of the buffer.
  172. * We now have some room to do what we need (add CRC and padding).
  173. */
  174. static int data_size;
  175. #define FAKE_HEADER_SIZE (PADDED_SIZE - data_size)
  176. static int socfpgaimage_vrec_header(struct image_tool_params *params,
  177. struct image_type_params *tparams)
  178. {
  179. struct stat sbuf;
  180. if (params->datafile &&
  181. stat(params->datafile, &sbuf) == 0 &&
  182. sbuf.st_size <= MAX_INPUT_SIZE) {
  183. data_size = sbuf.st_size;
  184. tparams->header_size = FAKE_HEADER_SIZE;
  185. }
  186. return 0;
  187. }
  188. static void socfpgaimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  189. struct image_tool_params *params)
  190. {
  191. uint8_t *buf = (uint8_t *)ptr;
  192. /*
  193. * This function is called after vrec_header() has been called.
  194. * At this stage we have the FAKE_HEADER_SIZE dummy bytes followed by
  195. * data_size image bytes. Total = PADDED_SIZE.
  196. * We need to fix the buffer by moving the image bytes back to
  197. * the beginning of the buffer, then actually do the signing stuff...
  198. */
  199. memmove(buf, buf + FAKE_HEADER_SIZE, data_size);
  200. memset(buf + data_size, 0, FAKE_HEADER_SIZE);
  201. sign_buffer(buf, 0, 0, data_size, 0);
  202. }
  203. U_BOOT_IMAGE_TYPE(
  204. socfpgaimage,
  205. "Altera SOCFPGA preloader support",
  206. 0, /* This will be modified by vrec_header() */
  207. (void *)buffer,
  208. socfpgaimage_check_params,
  209. socfpgaimage_verify_header,
  210. socfpgaimage_print_header,
  211. socfpgaimage_set_header,
  212. NULL,
  213. socfpgaimage_check_image_types,
  214. NULL,
  215. socfpgaimage_vrec_header
  216. );