zynqmpimage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 doc v1.4 (Chapter 11, Table 11-4)
  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 void *dynamic_header;
  96. static FILE *fpmu;
  97. static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
  98. {
  99. uint32_t checksum = 0;
  100. if (ptr == NULL)
  101. return 0;
  102. checksum += le32_to_cpu(ptr->width_detection);
  103. checksum += le32_to_cpu(ptr->image_identifier);
  104. checksum += le32_to_cpu(ptr->encryption);
  105. checksum += le32_to_cpu(ptr->image_load);
  106. checksum += le32_to_cpu(ptr->image_offset);
  107. checksum += le32_to_cpu(ptr->pfw_image_length);
  108. checksum += le32_to_cpu(ptr->total_pfw_image_length);
  109. checksum += le32_to_cpu(ptr->image_size);
  110. checksum += le32_to_cpu(ptr->image_stored_size);
  111. checksum += le32_to_cpu(ptr->image_attributes);
  112. checksum = ~checksum;
  113. return cpu_to_le32(checksum);
  114. }
  115. static void zynqmpimage_default_header(struct zynqmp_header *ptr)
  116. {
  117. int i;
  118. if (ptr == NULL)
  119. return;
  120. ptr->width_detection = HEADER_WIDTHDETECTION;
  121. ptr->image_attributes = 0x800;
  122. ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
  123. ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
  124. /* Setup not-supported/constant/reserved fields */
  125. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
  126. ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
  127. for (i = 0; i < HEADER_REGINITS; i++) {
  128. ptr->register_init[i].address = HEADER_REGINIT_NULL;
  129. ptr->register_init[i].data = 0;
  130. }
  131. /*
  132. * Certain reserved fields are required to be set to 0, ensure they are
  133. * set as such.
  134. */
  135. ptr->pfw_image_length = 0x0;
  136. ptr->total_pfw_image_length = 0x0;
  137. }
  138. /* mkimage glue functions */
  139. static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
  140. struct image_tool_params *params)
  141. {
  142. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  143. if (image_size < sizeof(struct zynqmp_header))
  144. return -1;
  145. if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
  146. return -1;
  147. if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
  148. return -1;
  149. if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
  150. return -1;
  151. return 0;
  152. }
  153. static void zynqmpimage_print_header(const void *ptr)
  154. {
  155. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  156. int i;
  157. printf("Image Type : Xilinx Zynq Boot Image support\n");
  158. printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
  159. printf("Image Size : %lu bytes (%lu bytes packed)\n",
  160. (unsigned long)le32_to_cpu(zynqhdr->image_size),
  161. (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
  162. if (zynqhdr->pfw_image_length)
  163. printf("PMUFW Size : %lu bytes (%lu bytes packed)\n",
  164. (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length),
  165. (unsigned long)le32_to_cpu(
  166. zynqhdr->total_pfw_image_length));
  167. printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
  168. printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
  169. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
  170. if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
  171. continue;
  172. printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
  173. le32_to_cpu(zynqhdr->interrupt_vectors[i]));
  174. }
  175. for (i = 0; i < HEADER_REGINITS; i++) {
  176. if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
  177. break;
  178. if (i == 0)
  179. printf("Custom Register Initialization:\n");
  180. printf(" @ 0x%08x -> 0x%08x\n",
  181. le32_to_cpu(zynqhdr->register_init[i].address),
  182. le32_to_cpu(zynqhdr->register_init[i].data));
  183. }
  184. free(dynamic_header);
  185. }
  186. static int zynqmpimage_check_params(struct image_tool_params *params)
  187. {
  188. if (!params)
  189. return 0;
  190. if (params->addr != 0x0) {
  191. fprintf(stderr, "Error: Load Address cannot be specified.\n");
  192. return -1;
  193. }
  194. /*
  195. * If the entry point is specified ensure it is 64 byte aligned.
  196. */
  197. if (params->eflag && (params->ep % 64 != 0)) {
  198. fprintf(stderr,
  199. "Error: Entry Point must be aligned to a 64-byte boundary.\n");
  200. return -1;
  201. }
  202. return !(params->lflag || params->dflag);
  203. }
  204. static int zynqmpimage_check_image_types(uint8_t type)
  205. {
  206. if (type == IH_TYPE_ZYNQMPIMAGE)
  207. return EXIT_SUCCESS;
  208. return EXIT_FAILURE;
  209. }
  210. static uint32_t fsize(FILE *fp)
  211. {
  212. int size, ret, origin;
  213. origin = ftell(fp);
  214. if (origin < 0) {
  215. fprintf(stderr, "Incorrect file size\n");
  216. fclose(fp);
  217. exit(2);
  218. }
  219. ret = fseek(fp, 0L, SEEK_END);
  220. if (ret) {
  221. fprintf(stderr, "Incorrect file SEEK_END\n");
  222. fclose(fp);
  223. exit(3);
  224. }
  225. size = ftell(fp);
  226. if (size < 0) {
  227. fprintf(stderr, "Incorrect file size\n");
  228. fclose(fp);
  229. exit(4);
  230. }
  231. /* going back */
  232. ret = fseek(fp, origin, SEEK_SET);
  233. if (ret) {
  234. fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin);
  235. fclose(fp);
  236. exit(3);
  237. }
  238. return size;
  239. }
  240. static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
  241. const char *filename)
  242. {
  243. uint32_t size;
  244. /* Setup PMU fw size */
  245. zynqhdr->pfw_image_length = fsize(fpmu);
  246. zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length;
  247. zynqhdr->image_size -= zynqhdr->pfw_image_length;
  248. zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length;
  249. /* Read the whole PMUFW to the header */
  250. size = fread(&zynqhdr->__reserved4[66], 1,
  251. zynqhdr->pfw_image_length, fpmu);
  252. if (size != zynqhdr->pfw_image_length) {
  253. fprintf(stderr, "Cannot read PMUFW file: %s\n", filename);
  254. fclose(fpmu);
  255. exit(1);
  256. }
  257. fclose(fpmu);
  258. }
  259. static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
  260. const char *filename)
  261. {
  262. FILE *fp;
  263. struct zynqmp_reginit reginit;
  264. unsigned int reg_count = 0;
  265. int r, err;
  266. struct stat path_stat;
  267. /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
  268. fp = fopen(filename, "r");
  269. if (!fp) {
  270. fprintf(stderr, "Cannot open initparams file: %s\n", filename);
  271. exit(1);
  272. }
  273. err = fstat(fileno(fp), &path_stat);
  274. if (err) {
  275. fclose(fp);
  276. return;
  277. }
  278. if (!S_ISREG(path_stat.st_mode)) {
  279. fclose(fp);
  280. return;
  281. }
  282. do {
  283. r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
  284. if (r == 2) {
  285. zynqhdr->register_init[reg_count] = reginit;
  286. ++reg_count;
  287. }
  288. r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
  289. } while ((r != EOF) && (reg_count < HEADER_REGINITS));
  290. fclose(fp);
  291. }
  292. static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  293. struct image_tool_params *params)
  294. {
  295. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  296. zynqmpimage_default_header(zynqhdr);
  297. /* place image directly after header */
  298. zynqhdr->image_offset =
  299. cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
  300. zynqhdr->image_size = cpu_to_le32(params->file_size -
  301. sizeof(struct zynqmp_header));
  302. zynqhdr->image_stored_size = zynqhdr->image_size;
  303. zynqhdr->image_load = 0xfffc0000;
  304. if (params->eflag)
  305. zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
  306. /* PMUFW */
  307. if (fpmu)
  308. zynqmpimage_pmufw(zynqhdr, params->imagename);
  309. /* User can pass in text file with init list */
  310. if (strlen(params->imagename2))
  311. zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
  312. zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
  313. }
  314. static int zynqmpimage_vrec_header(struct image_tool_params *params,
  315. struct image_type_params *tparams)
  316. {
  317. struct stat path_stat;
  318. char *filename = params->imagename;
  319. int err;
  320. /* Handle static case without PMUFW */
  321. tparams->header_size = sizeof(struct zynqmp_header);
  322. tparams->hdr = (void *)&zynqmpimage_header;
  323. /* PMUFW name is passed via params->imagename */
  324. if (strlen(filename) == 0)
  325. return EXIT_SUCCESS;
  326. fpmu = fopen(filename, "r");
  327. if (!fpmu) {
  328. fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
  329. return EXIT_FAILURE;
  330. }
  331. err = fstat(fileno(fpmu), &path_stat);
  332. if (err) {
  333. fclose(fpmu);
  334. fpmu = NULL;
  335. return EXIT_FAILURE;
  336. }
  337. if (!S_ISREG(path_stat.st_mode)) {
  338. fclose(fpmu);
  339. fpmu = NULL;
  340. return EXIT_FAILURE;
  341. }
  342. /* Increase header size by PMUFW file size */
  343. tparams->header_size += fsize(fpmu);
  344. /* Allocate buffer with space for PMUFW */
  345. dynamic_header = calloc(1, tparams->header_size);
  346. tparams->hdr = dynamic_header;
  347. return EXIT_SUCCESS;
  348. }
  349. U_BOOT_IMAGE_TYPE(
  350. zynqmpimage,
  351. "Xilinx ZynqMP Boot Image support",
  352. sizeof(struct zynqmp_header),
  353. (void *)&zynqmpimage_header,
  354. zynqmpimage_check_params,
  355. zynqmpimage_verify_header,
  356. zynqmpimage_print_header,
  357. zynqmpimage_set_header,
  358. NULL,
  359. zynqmpimage_check_image_types,
  360. NULL,
  361. zynqmpimage_vrec_header
  362. );