zynqmpimage.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2016 Michal Simek <michals@xilinx.com>
  3. * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * The following Boot Header format/structures and values are defined in the
  8. * following documents:
  9. * * ug1085 ZynqMP TRM (Chapter 9, Table 9-3)
  10. *
  11. * Expected Header Size = 0x9C0
  12. * Forced as 'little' endian, 32-bit words
  13. *
  14. * 0x 0 - Interrupt table (8 words)
  15. * ... (Default value = 0xeafffffe)
  16. * 0x 1f
  17. * 0x 20 - Width detection
  18. * * DEFAULT_WIDTHDETECTION 0xaa995566
  19. * 0x 24 - Image identifier
  20. * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58
  21. * 0x 28 - Encryption
  22. * * 0x00000000 - None
  23. * * 0xa5c3c5a3 - eFuse
  24. * * 0xa5c3c5a7 - obfuscated key in eFUSE
  25. * * 0x3a5c3c5a - bbRam
  26. * * 0xa35c7ca5 - obfuscated key in boot header
  27. * 0x 2C - Image load
  28. * 0x 30 - Image offset
  29. * 0x 34 - PFW image length
  30. * 0x 38 - Total PFW image length
  31. * 0x 3C - Image length
  32. * 0x 40 - Total image length
  33. * 0x 44 - Image attributes
  34. * 0x 48 - Header checksum
  35. * 0x 4c - Obfuscated key
  36. * ...
  37. * 0x 68
  38. * 0x 6c - Reserved
  39. * 0x 70 - User defined
  40. * ...
  41. * 0x 9c
  42. * 0x a0 - Secure header initialization vector
  43. * ...
  44. * 0x a8
  45. * 0x ac - Obfuscated key initialization vector
  46. * ...
  47. * 0x b4
  48. * 0x b8 - Register Initialization, 511 Address and Data word pairs
  49. * * List is terminated with an address of 0xffffffff or
  50. * ... * at the max number of entries
  51. * 0x8b4
  52. * 0x8b8 - Reserved
  53. * ...
  54. * 0x9bf
  55. * 0x9c0 - Data/Image starts here or above
  56. */
  57. #include "imagetool.h"
  58. #include "mkimage.h"
  59. #include <image.h>
  60. #define HEADER_INTERRUPT_DEFAULT (cpu_to_le32(0xeafffffe))
  61. #define HEADER_REGINIT_NULL (cpu_to_le32(0xffffffff))
  62. #define HEADER_WIDTHDETECTION (cpu_to_le32(0xaa995566))
  63. #define HEADER_IMAGEIDENTIFIER (cpu_to_le32(0x584c4e58))
  64. enum {
  65. ENCRYPTION_EFUSE = 0xa5c3c5a3,
  66. ENCRYPTION_OEFUSE = 0xa5c3c5a7,
  67. ENCRYPTION_BBRAM = 0x3a5c3c5a,
  68. ENCRYPTION_OBBRAM = 0xa35c7ca5,
  69. ENCRYPTION_NONE = 0x0,
  70. };
  71. struct zynqmp_reginit {
  72. uint32_t address;
  73. uint32_t data;
  74. };
  75. #define HEADER_INTERRUPT_VECTORS 8
  76. #define HEADER_REGINITS 256
  77. struct zynqmp_header {
  78. uint32_t interrupt_vectors[HEADER_INTERRUPT_VECTORS]; /* 0x0 */
  79. uint32_t width_detection; /* 0x20 */
  80. uint32_t image_identifier; /* 0x24 */
  81. uint32_t encryption; /* 0x28 */
  82. uint32_t image_load; /* 0x2c */
  83. uint32_t image_offset; /* 0x30 */
  84. uint32_t pfw_image_length; /* 0x34 */
  85. uint32_t total_pfw_image_length; /* 0x38 */
  86. uint32_t image_size; /* 0x3c */
  87. uint32_t image_stored_size; /* 0x40 */
  88. uint32_t image_attributes; /* 0x44 */
  89. uint32_t checksum; /* 0x48 */
  90. uint32_t __reserved1[27]; /* 0x4c */
  91. struct zynqmp_reginit register_init[HEADER_REGINITS]; /* 0xb8 */
  92. uint32_t __reserved4[66]; /* 0x9c0 */
  93. };
  94. static struct zynqmp_header zynqmpimage_header;
  95. static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
  96. {
  97. uint32_t checksum = 0;
  98. if (ptr == NULL)
  99. return 0;
  100. checksum += le32_to_cpu(ptr->width_detection);
  101. checksum += le32_to_cpu(ptr->image_identifier);
  102. checksum += le32_to_cpu(ptr->encryption);
  103. checksum += le32_to_cpu(ptr->image_load);
  104. checksum += le32_to_cpu(ptr->image_offset);
  105. checksum += le32_to_cpu(ptr->pfw_image_length);
  106. checksum += le32_to_cpu(ptr->total_pfw_image_length);
  107. checksum += le32_to_cpu(ptr->image_size);
  108. checksum += le32_to_cpu(ptr->image_stored_size);
  109. checksum += le32_to_cpu(ptr->image_attributes);
  110. checksum = ~checksum;
  111. return cpu_to_le32(checksum);
  112. }
  113. static void zynqmpimage_default_header(struct zynqmp_header *ptr)
  114. {
  115. int i;
  116. if (ptr == NULL)
  117. return;
  118. ptr->width_detection = HEADER_WIDTHDETECTION;
  119. ptr->image_attributes = 0x800;
  120. ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
  121. ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
  122. /* Setup not-supported/constant/reserved fields */
  123. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
  124. ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
  125. for (i = 0; i < HEADER_REGINITS; i++) {
  126. ptr->register_init[i].address = HEADER_REGINIT_NULL;
  127. ptr->register_init[i].data = 0;
  128. }
  129. /*
  130. * Certain reserved fields are required to be set to 0, ensure they are
  131. * set as such.
  132. */
  133. ptr->pfw_image_length = 0x0;
  134. ptr->total_pfw_image_length = 0x0;
  135. }
  136. /* mkimage glue functions */
  137. static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
  138. struct image_tool_params *params)
  139. {
  140. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  141. if (image_size < sizeof(struct zynqmp_header))
  142. return -1;
  143. if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
  144. return -1;
  145. if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
  146. return -1;
  147. if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
  148. return -1;
  149. return 0;
  150. }
  151. static void zynqmpimage_print_header(const void *ptr)
  152. {
  153. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  154. int i;
  155. printf("Image Type : Xilinx Zynq Boot Image support\n");
  156. printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
  157. printf("Image Size : %lu bytes (%lu bytes packed)\n",
  158. (unsigned long)le32_to_cpu(zynqhdr->image_size),
  159. (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
  160. printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
  161. printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
  162. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
  163. if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
  164. continue;
  165. printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
  166. le32_to_cpu(zynqhdr->interrupt_vectors[i]));
  167. }
  168. for (i = 0; i < HEADER_REGINITS; i++) {
  169. if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
  170. break;
  171. if (i == 0)
  172. printf("Custom Register Initialization:\n");
  173. printf(" @ 0x%08x -> 0x%08x\n",
  174. le32_to_cpu(zynqhdr->register_init[i].address),
  175. le32_to_cpu(zynqhdr->register_init[i].data));
  176. }
  177. }
  178. static int zynqmpimage_check_params(struct image_tool_params *params)
  179. {
  180. if (!params)
  181. return 0;
  182. if (params->addr != 0x0) {
  183. fprintf(stderr, "Error: Load Address cannot be specified.\n");
  184. return -1;
  185. }
  186. /*
  187. * If the entry point is specified ensure it is 64 byte aligned.
  188. */
  189. if (params->eflag && (params->ep % 64 != 0)) {
  190. fprintf(stderr,
  191. "Error: Entry Point must be aligned to a 64-byte boundary.\n");
  192. return -1;
  193. }
  194. return !(params->lflag || params->dflag);
  195. }
  196. static int zynqmpimage_check_image_types(uint8_t type)
  197. {
  198. if (type == IH_TYPE_ZYNQMPIMAGE)
  199. return EXIT_SUCCESS;
  200. return EXIT_FAILURE;
  201. }
  202. static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  203. struct image_tool_params *params)
  204. {
  205. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  206. zynqmpimage_default_header(zynqhdr);
  207. /* place image directly after header */
  208. zynqhdr->image_offset =
  209. cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
  210. zynqhdr->image_size = cpu_to_le32(params->file_size -
  211. sizeof(struct zynqmp_header));
  212. zynqhdr->image_stored_size = zynqhdr->image_size;
  213. zynqhdr->image_load = 0xfffc0000;
  214. if (params->eflag)
  215. zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
  216. zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
  217. }
  218. U_BOOT_IMAGE_TYPE(
  219. zynqmpimage,
  220. "Xilinx ZynqMP Boot Image support",
  221. sizeof(struct zynqmp_header),
  222. (void *)&zynqmpimage_header,
  223. zynqmpimage_check_params,
  224. zynqmpimage_verify_header,
  225. zynqmpimage_print_header,
  226. zynqmpimage_set_header,
  227. NULL,
  228. zynqmpimage_check_image_types,
  229. NULL,
  230. NULL
  231. );