Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

312 lines
8.3 KiB

  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_parse_initparams(struct zynqmp_header *zynqhdr,
  203. const char *filename)
  204. {
  205. FILE *fp;
  206. struct zynqmp_reginit reginit;
  207. unsigned int reg_count = 0;
  208. int r, err;
  209. struct stat path_stat;
  210. /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
  211. fp = fopen(filename, "r");
  212. if (!fp) {
  213. fprintf(stderr, "Cannot open initparams file: %s\n", filename);
  214. exit(1);
  215. }
  216. err = fstat(fileno(fp), &path_stat);
  217. if (err) {
  218. fclose(fp);
  219. return;
  220. }
  221. if (!S_ISREG(path_stat.st_mode)) {
  222. fclose(fp);
  223. return;
  224. }
  225. do {
  226. r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
  227. if (r == 2) {
  228. zynqhdr->register_init[reg_count] = reginit;
  229. ++reg_count;
  230. }
  231. r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
  232. } while ((r != EOF) && (reg_count < HEADER_REGINITS));
  233. fclose(fp);
  234. }
  235. static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  236. struct image_tool_params *params)
  237. {
  238. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  239. zynqmpimage_default_header(zynqhdr);
  240. /* place image directly after header */
  241. zynqhdr->image_offset =
  242. cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
  243. zynqhdr->image_size = cpu_to_le32(params->file_size -
  244. sizeof(struct zynqmp_header));
  245. zynqhdr->image_stored_size = zynqhdr->image_size;
  246. zynqhdr->image_load = 0xfffc0000;
  247. if (params->eflag)
  248. zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
  249. /* User can pass in text file with init list */
  250. if (strlen(params->imagename2))
  251. zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
  252. zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
  253. }
  254. U_BOOT_IMAGE_TYPE(
  255. zynqmpimage,
  256. "Xilinx ZynqMP Boot Image support",
  257. sizeof(struct zynqmp_header),
  258. (void *)&zynqmpimage_header,
  259. zynqmpimage_check_params,
  260. zynqmpimage_verify_header,
  261. zynqmpimage_print_header,
  262. zynqmpimage_set_header,
  263. NULL,
  264. zynqmpimage_check_image_types,
  265. NULL,
  266. NULL
  267. );