zynqmpimage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Michal Simek <michals@xilinx.com>
  4. * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
  5. *
  6. * The following Boot Header format/structures and values are defined in the
  7. * following documents:
  8. * * ug1085 ZynqMP TRM doc v1.4 (Chapter 11, Table 11-4)
  9. *
  10. * Expected Header Size = 0x9C0
  11. * Forced as 'little' endian, 32-bit words
  12. *
  13. * 0x 0 - Interrupt table (8 words)
  14. * ... (Default value = 0xeafffffe)
  15. * 0x 1f
  16. * 0x 20 - Width detection
  17. * * DEFAULT_WIDTHDETECTION 0xaa995566
  18. * 0x 24 - Image identifier
  19. * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58
  20. * 0x 28 - Encryption
  21. * * 0x00000000 - None
  22. * * 0xa5c3c5a3 - eFuse
  23. * * 0xa5c3c5a7 - obfuscated key in eFUSE
  24. * * 0x3a5c3c5a - bbRam
  25. * * 0xa35c7ca5 - obfuscated key in boot header
  26. * 0x 2C - Image load
  27. * 0x 30 - Image offset
  28. * 0x 34 - PFW image length
  29. * 0x 38 - Total PFW image length
  30. * 0x 3C - Image length
  31. * 0x 40 - Total image length
  32. * 0x 44 - Image attributes
  33. * 0x 48 - Header checksum
  34. * 0x 4c - Obfuscated key
  35. * ...
  36. * 0x 68
  37. * 0x 6c - Reserved
  38. * 0x 70 - User defined
  39. * ...
  40. * 0x 9c
  41. * 0x a0 - Secure header initialization vector
  42. * ...
  43. * 0x a8
  44. * 0x ac - Obfuscated key initialization vector
  45. * ...
  46. * 0x b4
  47. * 0x b8 - Register Initialization, 511 Address and Data word pairs
  48. * * List is terminated with an address of 0xffffffff or
  49. * ... * at the max number of entries
  50. * 0x8b4
  51. * 0x8b8 - Reserved
  52. * ...
  53. * 0x9bf
  54. * 0x9c0 - Data/Image starts here or above
  55. */
  56. #include "imagetool.h"
  57. #include "mkimage.h"
  58. #include <image.h>
  59. #define HEADER_INTERRUPT_DEFAULT (cpu_to_le32(0xeafffffe))
  60. #define HEADER_REGINIT_NULL (cpu_to_le32(0xffffffff))
  61. #define HEADER_WIDTHDETECTION (cpu_to_le32(0xaa995566))
  62. #define HEADER_IMAGEIDENTIFIER (cpu_to_le32(0x584c4e58))
  63. enum {
  64. ENCRYPTION_EFUSE = 0xa5c3c5a3,
  65. ENCRYPTION_OEFUSE = 0xa5c3c5a7,
  66. ENCRYPTION_BBRAM = 0x3a5c3c5a,
  67. ENCRYPTION_OBBRAM = 0xa35c7ca5,
  68. ENCRYPTION_NONE = 0x0,
  69. };
  70. struct zynqmp_reginit {
  71. uint32_t address;
  72. uint32_t data;
  73. };
  74. #define HEADER_INTERRUPT_VECTORS 8
  75. #define HEADER_REGINITS 256
  76. struct zynqmp_header {
  77. uint32_t interrupt_vectors[HEADER_INTERRUPT_VECTORS]; /* 0x0 */
  78. uint32_t width_detection; /* 0x20 */
  79. uint32_t image_identifier; /* 0x24 */
  80. uint32_t encryption; /* 0x28 */
  81. uint32_t image_load; /* 0x2c */
  82. uint32_t image_offset; /* 0x30 */
  83. uint32_t pfw_image_length; /* 0x34 */
  84. uint32_t total_pfw_image_length; /* 0x38 */
  85. uint32_t image_size; /* 0x3c */
  86. uint32_t image_stored_size; /* 0x40 */
  87. uint32_t image_attributes; /* 0x44 */
  88. uint32_t checksum; /* 0x48 */
  89. uint32_t __reserved1[27]; /* 0x4c */
  90. struct zynqmp_reginit register_init[HEADER_REGINITS]; /* 0xb8 */
  91. uint32_t __reserved4[66]; /* 0x9c0 */
  92. };
  93. static struct zynqmp_header zynqmpimage_header;
  94. static void *dynamic_header;
  95. static FILE *fpmu;
  96. static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
  97. {
  98. uint32_t checksum = 0;
  99. if (ptr == NULL)
  100. return 0;
  101. checksum += le32_to_cpu(ptr->width_detection);
  102. checksum += le32_to_cpu(ptr->image_identifier);
  103. checksum += le32_to_cpu(ptr->encryption);
  104. checksum += le32_to_cpu(ptr->image_load);
  105. checksum += le32_to_cpu(ptr->image_offset);
  106. checksum += le32_to_cpu(ptr->pfw_image_length);
  107. checksum += le32_to_cpu(ptr->total_pfw_image_length);
  108. checksum += le32_to_cpu(ptr->image_size);
  109. checksum += le32_to_cpu(ptr->image_stored_size);
  110. checksum += le32_to_cpu(ptr->image_attributes);
  111. checksum = ~checksum;
  112. return cpu_to_le32(checksum);
  113. }
  114. static void zynqmpimage_default_header(struct zynqmp_header *ptr)
  115. {
  116. int i;
  117. if (ptr == NULL)
  118. return;
  119. ptr->width_detection = HEADER_WIDTHDETECTION;
  120. ptr->image_attributes = 0x800;
  121. ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
  122. ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
  123. /* Setup not-supported/constant/reserved fields */
  124. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
  125. ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
  126. for (i = 0; i < HEADER_REGINITS; i++) {
  127. ptr->register_init[i].address = HEADER_REGINIT_NULL;
  128. ptr->register_init[i].data = 0;
  129. }
  130. /*
  131. * Certain reserved fields are required to be set to 0, ensure they are
  132. * set as such.
  133. */
  134. ptr->pfw_image_length = 0x0;
  135. ptr->total_pfw_image_length = 0x0;
  136. }
  137. /* mkimage glue functions */
  138. static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
  139. struct image_tool_params *params)
  140. {
  141. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  142. if (image_size < sizeof(struct zynqmp_header))
  143. return -1;
  144. if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
  145. return -1;
  146. if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
  147. return -1;
  148. if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
  149. return -1;
  150. return 0;
  151. }
  152. static void zynqmpimage_print_header(const void *ptr)
  153. {
  154. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  155. int i;
  156. printf("Image Type : Xilinx ZynqMP Boot Image support\n");
  157. printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
  158. printf("Image Size : %lu bytes (%lu bytes packed)\n",
  159. (unsigned long)le32_to_cpu(zynqhdr->image_size),
  160. (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
  161. if (zynqhdr->pfw_image_length)
  162. printf("PMUFW Size : %lu bytes (%lu bytes packed)\n",
  163. (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length),
  164. (unsigned long)le32_to_cpu(
  165. zynqhdr->total_pfw_image_length));
  166. printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
  167. printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
  168. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
  169. if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
  170. continue;
  171. printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
  172. le32_to_cpu(zynqhdr->interrupt_vectors[i]));
  173. }
  174. for (i = 0; i < HEADER_REGINITS; i++) {
  175. if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
  176. break;
  177. if (i == 0)
  178. printf("Custom Register Initialization:\n");
  179. printf(" @ 0x%08x -> 0x%08x\n",
  180. le32_to_cpu(zynqhdr->register_init[i].address),
  181. le32_to_cpu(zynqhdr->register_init[i].data));
  182. }
  183. free(dynamic_header);
  184. }
  185. static int zynqmpimage_check_params(struct image_tool_params *params)
  186. {
  187. if (!params)
  188. return 0;
  189. if (params->addr != 0x0) {
  190. fprintf(stderr, "Error: Load Address cannot be specified.\n");
  191. return -1;
  192. }
  193. /*
  194. * If the entry point is specified ensure it is 64 byte aligned.
  195. */
  196. if (params->eflag && (params->ep % 64 != 0)) {
  197. fprintf(stderr,
  198. "Error: Entry Point must be aligned to a 64-byte boundary.\n");
  199. return -1;
  200. }
  201. return !(params->lflag || params->dflag);
  202. }
  203. static int zynqmpimage_check_image_types(uint8_t type)
  204. {
  205. if (type == IH_TYPE_ZYNQMPIMAGE)
  206. return EXIT_SUCCESS;
  207. return EXIT_FAILURE;
  208. }
  209. static uint32_t fsize(FILE *fp)
  210. {
  211. int size, ret, origin;
  212. origin = ftell(fp);
  213. if (origin < 0) {
  214. fprintf(stderr, "Incorrect file size\n");
  215. fclose(fp);
  216. exit(2);
  217. }
  218. ret = fseek(fp, 0L, SEEK_END);
  219. if (ret) {
  220. fprintf(stderr, "Incorrect file SEEK_END\n");
  221. fclose(fp);
  222. exit(3);
  223. }
  224. size = ftell(fp);
  225. if (size < 0) {
  226. fprintf(stderr, "Incorrect file size\n");
  227. fclose(fp);
  228. exit(4);
  229. }
  230. /* going back */
  231. ret = fseek(fp, origin, SEEK_SET);
  232. if (ret) {
  233. fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin);
  234. fclose(fp);
  235. exit(3);
  236. }
  237. return size;
  238. }
  239. static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
  240. const char *filename)
  241. {
  242. uint32_t size;
  243. /* Setup PMU fw size */
  244. zynqhdr->pfw_image_length = fsize(fpmu);
  245. zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length;
  246. zynqhdr->image_size -= zynqhdr->pfw_image_length;
  247. zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length;
  248. /* Read the whole PMUFW to the header */
  249. size = fread(&zynqhdr->__reserved4[66], 1,
  250. zynqhdr->pfw_image_length, fpmu);
  251. if (size != zynqhdr->pfw_image_length) {
  252. fprintf(stderr, "Cannot read PMUFW file: %s\n", filename);
  253. fclose(fpmu);
  254. exit(1);
  255. }
  256. fclose(fpmu);
  257. }
  258. static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
  259. const char *filename)
  260. {
  261. FILE *fp;
  262. struct zynqmp_reginit reginit;
  263. unsigned int reg_count = 0;
  264. int r, err;
  265. struct stat path_stat;
  266. /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
  267. fp = fopen(filename, "r");
  268. if (!fp) {
  269. fprintf(stderr, "Cannot open initparams file: %s\n", filename);
  270. exit(1);
  271. }
  272. err = fstat(fileno(fp), &path_stat);
  273. if (err) {
  274. fclose(fp);
  275. return;
  276. }
  277. if (!S_ISREG(path_stat.st_mode)) {
  278. fclose(fp);
  279. return;
  280. }
  281. do {
  282. r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
  283. if (r == 2) {
  284. zynqhdr->register_init[reg_count] = reginit;
  285. ++reg_count;
  286. }
  287. r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
  288. } while ((r != EOF) && (reg_count < HEADER_REGINITS));
  289. fclose(fp);
  290. }
  291. static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  292. struct image_tool_params *params)
  293. {
  294. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  295. zynqmpimage_default_header(zynqhdr);
  296. /* place image directly after header */
  297. zynqhdr->image_offset =
  298. cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
  299. zynqhdr->image_size = cpu_to_le32(params->file_size -
  300. sizeof(struct zynqmp_header));
  301. zynqhdr->image_stored_size = zynqhdr->image_size;
  302. zynqhdr->image_load = 0xfffc0000;
  303. if (params->eflag)
  304. zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
  305. /* PMUFW */
  306. if (fpmu)
  307. zynqmpimage_pmufw(zynqhdr, params->imagename);
  308. /* User can pass in text file with init list */
  309. if (strlen(params->imagename2))
  310. zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
  311. zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
  312. }
  313. static int zynqmpimage_vrec_header(struct image_tool_params *params,
  314. struct image_type_params *tparams)
  315. {
  316. struct stat path_stat;
  317. char *filename = params->imagename;
  318. int err;
  319. /* Handle static case without PMUFW */
  320. tparams->header_size = sizeof(struct zynqmp_header);
  321. tparams->hdr = (void *)&zynqmpimage_header;
  322. /* PMUFW name is passed via params->imagename */
  323. if (strlen(filename) == 0)
  324. return EXIT_SUCCESS;
  325. fpmu = fopen(filename, "r");
  326. if (!fpmu) {
  327. fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
  328. return EXIT_FAILURE;
  329. }
  330. err = fstat(fileno(fpmu), &path_stat);
  331. if (err) {
  332. fclose(fpmu);
  333. fpmu = NULL;
  334. return EXIT_FAILURE;
  335. }
  336. if (!S_ISREG(path_stat.st_mode)) {
  337. fclose(fpmu);
  338. fpmu = NULL;
  339. return EXIT_FAILURE;
  340. }
  341. /* Increase header size by PMUFW file size */
  342. tparams->header_size += fsize(fpmu);
  343. /* Allocate buffer with space for PMUFW */
  344. dynamic_header = calloc(1, tparams->header_size);
  345. tparams->hdr = dynamic_header;
  346. return EXIT_SUCCESS;
  347. }
  348. U_BOOT_IMAGE_TYPE(
  349. zynqmpimage,
  350. "Xilinx ZynqMP Boot Image support",
  351. sizeof(struct zynqmp_header),
  352. (void *)&zynqmpimage_header,
  353. zynqmpimage_check_params,
  354. zynqmpimage_verify_header,
  355. zynqmpimage_print_header,
  356. zynqmpimage_set_header,
  357. NULL,
  358. zynqmpimage_check_image_types,
  359. NULL,
  360. zynqmpimage_vrec_header
  361. );