zynqmpimage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. * * ug1137 ZynqMP Software Developer Guide v6.0 (Chapter 16)
  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 "zynqmpimage.h"
  60. #include <image.h>
  61. static struct zynqmp_header zynqmpimage_header;
  62. static void *dynamic_header;
  63. static FILE *fpmu;
  64. static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
  65. {
  66. uint32_t checksum = 0;
  67. if (ptr == NULL)
  68. return 0;
  69. checksum += le32_to_cpu(ptr->width_detection);
  70. checksum += le32_to_cpu(ptr->image_identifier);
  71. checksum += le32_to_cpu(ptr->encryption);
  72. checksum += le32_to_cpu(ptr->image_load);
  73. checksum += le32_to_cpu(ptr->image_offset);
  74. checksum += le32_to_cpu(ptr->pfw_image_length);
  75. checksum += le32_to_cpu(ptr->total_pfw_image_length);
  76. checksum += le32_to_cpu(ptr->image_size);
  77. checksum += le32_to_cpu(ptr->image_stored_size);
  78. checksum += le32_to_cpu(ptr->image_attributes);
  79. checksum = ~checksum;
  80. return cpu_to_le32(checksum);
  81. }
  82. void zynqmpimage_default_header(struct zynqmp_header *ptr)
  83. {
  84. int i;
  85. if (ptr == NULL)
  86. return;
  87. ptr->width_detection = HEADER_WIDTHDETECTION;
  88. ptr->image_attributes = HEADER_CPU_SELECT_A53_64BIT;
  89. ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
  90. ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
  91. /* Setup not-supported/constant/reserved fields */
  92. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
  93. ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
  94. for (i = 0; i < HEADER_REGINITS; i++) {
  95. ptr->register_init[i].address = HEADER_REGINIT_NULL;
  96. ptr->register_init[i].data = 0;
  97. }
  98. /*
  99. * Certain reserved fields are required to be set to 0, ensure they are
  100. * set as such.
  101. */
  102. ptr->pfw_image_length = 0x0;
  103. ptr->total_pfw_image_length = 0x0;
  104. }
  105. /* mkimage glue functions */
  106. static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
  107. struct image_tool_params *params)
  108. {
  109. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  110. if (image_size < sizeof(struct zynqmp_header))
  111. return -1;
  112. if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
  113. return -1;
  114. if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
  115. return -1;
  116. if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
  117. return -1;
  118. return 0;
  119. }
  120. static void print_partition(const void *ptr, const struct partition_header *ph)
  121. {
  122. uint32_t attr = le32_to_cpu(ph->attributes);
  123. unsigned long len = le32_to_cpu(ph->len) * 4;
  124. const char *part_owner;
  125. const char *dest_devs[0x8] = {
  126. "none", "PS", "PL", "PMU", "unknown", "unknown", "unknown",
  127. "unknown"
  128. };
  129. switch (attr & PART_ATTR_PART_OWNER_MASK) {
  130. case PART_ATTR_PART_OWNER_FSBL:
  131. part_owner = "FSBL";
  132. break;
  133. case PART_ATTR_PART_OWNER_UBOOT:
  134. part_owner = "U-Boot";
  135. break;
  136. default:
  137. part_owner = "Unknown";
  138. break;
  139. }
  140. printf("%s payload on CPU %s (%s):\n", part_owner,
  141. dest_cpus[(attr & PART_ATTR_DEST_CPU_MASK) >> 8],
  142. dest_devs[(attr & PART_ATTR_DEST_DEVICE_MASK) >> 4]);
  143. printf(" Offset : 0x%08x\n", le32_to_cpu(ph->offset) * 4);
  144. printf(" Size : %lu (0x%lx) bytes\n", len, len);
  145. printf(" Load : 0x%08llx",
  146. (unsigned long long)le64_to_cpu(ph->load_address));
  147. if (ph->load_address != ph->entry_point)
  148. printf(" (entry=0x%08llx)\n",
  149. (unsigned long long)le64_to_cpu(ph->entry_point));
  150. else
  151. printf("\n");
  152. printf(" Attributes : ");
  153. if (attr & PART_ATTR_VEC_LOCATION)
  154. printf("vec ");
  155. if (attr & PART_ATTR_ENCRYPTED)
  156. printf("encrypted ");
  157. switch (attr & PART_ATTR_CHECKSUM_MASK) {
  158. case PART_ATTR_CHECKSUM_MD5:
  159. printf("md5 ");
  160. break;
  161. case PART_ATTR_CHECKSUM_SHA2:
  162. printf("sha2 ");
  163. break;
  164. case PART_ATTR_CHECKSUM_SHA3:
  165. printf("sha3 ");
  166. break;
  167. }
  168. if (attr & PART_ATTR_BIG_ENDIAN)
  169. printf("BigEndian ");
  170. if (attr & PART_ATTR_RSA_SIG)
  171. printf("RSA ");
  172. if (attr & PART_ATTR_A53_EXEC_AARCH32)
  173. printf("AArch32 ");
  174. if (attr & PART_ATTR_TARGET_EL_MASK)
  175. printf("EL%d ", (attr & PART_ATTR_TARGET_EL_MASK) >> 1);
  176. if (attr & PART_ATTR_TZ_SECURE)
  177. printf("secure ");
  178. printf("\n");
  179. printf(" Checksum : 0x%08x\n", le32_to_cpu(ph->checksum));
  180. }
  181. void zynqmpimage_print_header(const void *ptr)
  182. {
  183. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  184. int i;
  185. printf("Image Type : Xilinx ZynqMP Boot Image support\n");
  186. printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
  187. printf("Image Size : %lu bytes (%lu bytes packed)\n",
  188. (unsigned long)le32_to_cpu(zynqhdr->image_size),
  189. (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
  190. if (zynqhdr->pfw_image_length)
  191. printf("PMUFW Size : %lu bytes (%lu bytes packed)\n",
  192. (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length),
  193. (unsigned long)le32_to_cpu(
  194. zynqhdr->total_pfw_image_length));
  195. printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
  196. printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
  197. for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
  198. if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
  199. continue;
  200. printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
  201. le32_to_cpu(zynqhdr->interrupt_vectors[i]));
  202. }
  203. for (i = 0; i < HEADER_REGINITS; i++) {
  204. if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
  205. break;
  206. if (i == 0)
  207. printf("Custom Register Initialization:\n");
  208. printf(" @ 0x%08x -> 0x%08x\n",
  209. le32_to_cpu(zynqhdr->register_init[i].address),
  210. le32_to_cpu(zynqhdr->register_init[i].data));
  211. }
  212. if (zynqhdr->image_header_table_offset) {
  213. struct image_header_table *iht = (void *)ptr +
  214. zynqhdr->image_header_table_offset;
  215. struct partition_header *ph;
  216. uint32_t ph_offset;
  217. uint32_t next;
  218. int i;
  219. ph_offset = le32_to_cpu(iht->partition_header_offset) * 4;
  220. ph = (void *)ptr + ph_offset;
  221. for (i = 0; i < le32_to_cpu(iht->nr_parts); i++) {
  222. next = le32_to_cpu(ph->next_partition_offset) * 4;
  223. /* Partition 0 is the base image itself */
  224. if (i)
  225. print_partition(ptr, ph);
  226. ph = (void *)ptr + next;
  227. }
  228. }
  229. free(dynamic_header);
  230. }
  231. static int zynqmpimage_check_params(struct image_tool_params *params)
  232. {
  233. if (!params)
  234. return 0;
  235. if (params->addr != 0x0) {
  236. fprintf(stderr, "Error: Load Address cannot be specified.\n");
  237. return -1;
  238. }
  239. /*
  240. * If the entry point is specified ensure it is 64 byte aligned.
  241. */
  242. if (params->eflag && (params->ep % 64 != 0)) {
  243. fprintf(stderr,
  244. "Error: Entry Point must be aligned to a 64-byte boundary.\n");
  245. return -1;
  246. }
  247. return !(params->lflag || params->dflag);
  248. }
  249. static int zynqmpimage_check_image_types(uint8_t type)
  250. {
  251. if (type == IH_TYPE_ZYNQMPIMAGE)
  252. return EXIT_SUCCESS;
  253. return EXIT_FAILURE;
  254. }
  255. static uint32_t fsize(FILE *fp)
  256. {
  257. int size, ret, origin;
  258. origin = ftell(fp);
  259. if (origin < 0) {
  260. fprintf(stderr, "Incorrect file size\n");
  261. fclose(fp);
  262. exit(2);
  263. }
  264. ret = fseek(fp, 0L, SEEK_END);
  265. if (ret) {
  266. fprintf(stderr, "Incorrect file SEEK_END\n");
  267. fclose(fp);
  268. exit(3);
  269. }
  270. size = ftell(fp);
  271. if (size < 0) {
  272. fprintf(stderr, "Incorrect file size\n");
  273. fclose(fp);
  274. exit(4);
  275. }
  276. /* going back */
  277. ret = fseek(fp, origin, SEEK_SET);
  278. if (ret) {
  279. fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin);
  280. fclose(fp);
  281. exit(3);
  282. }
  283. return size;
  284. }
  285. static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
  286. const char *filename)
  287. {
  288. uint32_t size;
  289. /* Setup PMU fw size */
  290. zynqhdr->pfw_image_length = fsize(fpmu);
  291. zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length;
  292. zynqhdr->image_size -= zynqhdr->pfw_image_length;
  293. zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length;
  294. /* Read the whole PMUFW to the header */
  295. size = fread(&zynqhdr->__reserved4[66], 1,
  296. zynqhdr->pfw_image_length, fpmu);
  297. if (size != zynqhdr->pfw_image_length) {
  298. fprintf(stderr, "Cannot read PMUFW file: %s\n", filename);
  299. fclose(fpmu);
  300. exit(1);
  301. }
  302. fclose(fpmu);
  303. }
  304. static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
  305. const char *filename)
  306. {
  307. FILE *fp;
  308. struct zynqmp_reginit reginit;
  309. unsigned int reg_count = 0;
  310. int r, err;
  311. struct stat path_stat;
  312. /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
  313. fp = fopen(filename, "r");
  314. if (!fp) {
  315. fprintf(stderr, "Cannot open initparams file: %s\n", filename);
  316. exit(1);
  317. }
  318. err = fstat(fileno(fp), &path_stat);
  319. if (err) {
  320. fclose(fp);
  321. return;
  322. }
  323. if (!S_ISREG(path_stat.st_mode)) {
  324. fclose(fp);
  325. return;
  326. }
  327. do {
  328. r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
  329. if (r == 2) {
  330. zynqhdr->register_init[reg_count] = reginit;
  331. ++reg_count;
  332. }
  333. r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
  334. } while ((r != EOF) && (reg_count < HEADER_REGINITS));
  335. fclose(fp);
  336. }
  337. static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  338. struct image_tool_params *params)
  339. {
  340. struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
  341. zynqmpimage_default_header(zynqhdr);
  342. /* place image directly after header */
  343. zynqhdr->image_offset =
  344. cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
  345. zynqhdr->image_size = cpu_to_le32(params->file_size -
  346. sizeof(struct zynqmp_header));
  347. zynqhdr->image_stored_size = zynqhdr->image_size;
  348. zynqhdr->image_load = 0xfffc0000;
  349. if (params->eflag)
  350. zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
  351. /* PMUFW */
  352. if (fpmu)
  353. zynqmpimage_pmufw(zynqhdr, params->imagename);
  354. /* User can pass in text file with init list */
  355. if (strlen(params->imagename2))
  356. zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
  357. zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
  358. }
  359. static int zynqmpimage_vrec_header(struct image_tool_params *params,
  360. struct image_type_params *tparams)
  361. {
  362. struct stat path_stat;
  363. char *filename = params->imagename;
  364. int err;
  365. /* Handle static case without PMUFW */
  366. tparams->header_size = sizeof(struct zynqmp_header);
  367. tparams->hdr = (void *)&zynqmpimage_header;
  368. /* PMUFW name is passed via params->imagename */
  369. if (strlen(filename) == 0)
  370. return EXIT_SUCCESS;
  371. fpmu = fopen(filename, "r");
  372. if (!fpmu) {
  373. fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
  374. return EXIT_FAILURE;
  375. }
  376. err = fstat(fileno(fpmu), &path_stat);
  377. if (err) {
  378. fclose(fpmu);
  379. fpmu = NULL;
  380. return EXIT_FAILURE;
  381. }
  382. if (!S_ISREG(path_stat.st_mode)) {
  383. fclose(fpmu);
  384. fpmu = NULL;
  385. return EXIT_FAILURE;
  386. }
  387. /* Increase header size by PMUFW file size */
  388. tparams->header_size += fsize(fpmu);
  389. /* Allocate buffer with space for PMUFW */
  390. dynamic_header = calloc(1, tparams->header_size);
  391. tparams->hdr = dynamic_header;
  392. return EXIT_SUCCESS;
  393. }
  394. U_BOOT_IMAGE_TYPE(
  395. zynqmpimage,
  396. "Xilinx ZynqMP Boot Image support",
  397. sizeof(struct zynqmp_header),
  398. (void *)&zynqmpimage_header,
  399. zynqmpimage_check_params,
  400. zynqmpimage_verify_header,
  401. zynqmpimage_print_header,
  402. zynqmpimage_set_header,
  403. NULL,
  404. zynqmpimage_check_image_types,
  405. NULL,
  406. zynqmpimage_vrec_header
  407. );