1 // SPDX-License-Identifier: GPL-2.0+
6 * The following Boot Header format/structures and values are defined in the
8 * * ug1085 ZynqMP TRM doc v1.4 (Chapter 11, Table 11-4)
10 * Expected Header Size = 0x9C0
11 * Forced as 'little' endian, 32-bit words
13 * 0x 0 - Interrupt table (8 words)
14 * ... (Default value = 0xeafffffe)
16 * 0x 20 - Width detection
17 * * DEFAULT_WIDTHDETECTION 0xaa995566
18 * 0x 24 - Image identifier
19 * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58
22 * * 0xa5c3c5a3 - eFuse
23 * * 0xa5c3c5a7 - obfuscated key in eFUSE
24 * * 0x3a5c3c5a - bbRam
25 * * 0xa35c7ca5 - obfuscated key in boot header
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
38 * 0x 70 - User defined
41 * 0x a0 - Secure header initialization vector
44 * 0x ac - Obfuscated key initialization vector
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
54 * 0x9c0 - Data/Image starts here or above
57 #include "imagetool.h"
61 #define HEADER_INTERRUPT_DEFAULT (cpu_to_le32(0xeafffffe))
62 #define HEADER_REGINIT_NULL (cpu_to_le32(0xffffffff))
63 #define HEADER_WIDTHDETECTION (cpu_to_le32(0xaa995566))
64 #define HEADER_IMAGEIDENTIFIER (cpu_to_le32(0x584c4e58))
67 ENCRYPTION_EFUSE = 0xa5c3c5a3,
68 ENCRYPTION_OEFUSE = 0xa5c3c5a7,
69 ENCRYPTION_BBRAM = 0x3a5c3c5a,
70 ENCRYPTION_OBBRAM = 0xa35c7ca5,
71 ENCRYPTION_NONE = 0x0,
74 struct zynqmp_reginit {
79 #define HEADER_INTERRUPT_VECTORS 8
80 #define HEADER_REGINITS 256
82 struct zynqmp_header {
83 uint32_t interrupt_vectors[HEADER_INTERRUPT_VECTORS]; /* 0x0 */
84 uint32_t width_detection; /* 0x20 */
85 uint32_t image_identifier; /* 0x24 */
86 uint32_t encryption; /* 0x28 */
87 uint32_t image_load; /* 0x2c */
88 uint32_t image_offset; /* 0x30 */
89 uint32_t pfw_image_length; /* 0x34 */
90 uint32_t total_pfw_image_length; /* 0x38 */
91 uint32_t image_size; /* 0x3c */
92 uint32_t image_stored_size; /* 0x40 */
93 uint32_t image_attributes; /* 0x44 */
94 uint32_t checksum; /* 0x48 */
95 uint32_t __reserved1[27]; /* 0x4c */
96 struct zynqmp_reginit register_init[HEADER_REGINITS]; /* 0xb8 */
97 uint32_t __reserved4[66]; /* 0x9c0 */
100 static struct zynqmp_header zynqmpimage_header;
101 static void *dynamic_header;
104 static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
106 uint32_t checksum = 0;
111 checksum += le32_to_cpu(ptr->width_detection);
112 checksum += le32_to_cpu(ptr->image_identifier);
113 checksum += le32_to_cpu(ptr->encryption);
114 checksum += le32_to_cpu(ptr->image_load);
115 checksum += le32_to_cpu(ptr->image_offset);
116 checksum += le32_to_cpu(ptr->pfw_image_length);
117 checksum += le32_to_cpu(ptr->total_pfw_image_length);
118 checksum += le32_to_cpu(ptr->image_size);
119 checksum += le32_to_cpu(ptr->image_stored_size);
120 checksum += le32_to_cpu(ptr->image_attributes);
121 checksum = ~checksum;
123 return cpu_to_le32(checksum);
126 static void zynqmpimage_default_header(struct zynqmp_header *ptr)
133 ptr->width_detection = HEADER_WIDTHDETECTION;
134 ptr->image_attributes = 0x800;
135 ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
136 ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
138 /* Setup not-supported/constant/reserved fields */
139 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
140 ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
142 for (i = 0; i < HEADER_REGINITS; i++) {
143 ptr->register_init[i].address = HEADER_REGINIT_NULL;
144 ptr->register_init[i].data = 0;
148 * Certain reserved fields are required to be set to 0, ensure they are
151 ptr->pfw_image_length = 0x0;
152 ptr->total_pfw_image_length = 0x0;
155 /* mkimage glue functions */
156 static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
157 struct image_tool_params *params)
159 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
161 if (image_size < sizeof(struct zynqmp_header))
164 if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
166 if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
169 if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
175 static void zynqmpimage_print_header(const void *ptr)
177 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
180 printf("Image Type : Xilinx ZynqMP Boot Image support\n");
181 printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
182 printf("Image Size : %lu bytes (%lu bytes packed)\n",
183 (unsigned long)le32_to_cpu(zynqhdr->image_size),
184 (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
186 if (zynqhdr->pfw_image_length)
187 printf("PMUFW Size : %lu bytes (%lu bytes packed)\n",
188 (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length),
189 (unsigned long)le32_to_cpu(
190 zynqhdr->total_pfw_image_length));
192 printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
193 printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
195 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
196 if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
199 printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
200 le32_to_cpu(zynqhdr->interrupt_vectors[i]));
203 for (i = 0; i < HEADER_REGINITS; i++) {
204 if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
208 printf("Custom Register Initialization:\n");
210 printf(" @ 0x%08x -> 0x%08x\n",
211 le32_to_cpu(zynqhdr->register_init[i].address),
212 le32_to_cpu(zynqhdr->register_init[i].data));
215 free(dynamic_header);
218 static int zynqmpimage_check_params(struct image_tool_params *params)
223 if (params->addr != 0x0) {
224 fprintf(stderr, "Error: Load Address cannot be specified.\n");
229 * If the entry point is specified ensure it is 64 byte aligned.
231 if (params->eflag && (params->ep % 64 != 0)) {
233 "Error: Entry Point must be aligned to a 64-byte boundary.\n");
237 return !(params->lflag || params->dflag);
240 static int zynqmpimage_check_image_types(uint8_t type)
242 if (type == IH_TYPE_ZYNQMPIMAGE)
247 static uint32_t fsize(FILE *fp)
249 int size, ret, origin;
253 fprintf(stderr, "Incorrect file size\n");
258 ret = fseek(fp, 0L, SEEK_END);
260 fprintf(stderr, "Incorrect file SEEK_END\n");
267 fprintf(stderr, "Incorrect file size\n");
273 ret = fseek(fp, origin, SEEK_SET);
275 fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin);
283 static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
284 const char *filename)
288 /* Setup PMU fw size */
289 zynqhdr->pfw_image_length = fsize(fpmu);
290 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;
295 /* Read the whole PMUFW to the header */
296 size = fread(&zynqhdr->__reserved4[66], 1,
297 zynqhdr->pfw_image_length, fpmu);
298 if (size != zynqhdr->pfw_image_length) {
299 fprintf(stderr, "Cannot read PMUFW file: %s\n", filename);
307 static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
308 const char *filename)
311 struct zynqmp_reginit reginit;
312 unsigned int reg_count = 0;
314 struct stat path_stat;
316 /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
317 fp = fopen(filename, "r");
319 fprintf(stderr, "Cannot open initparams file: %s\n", filename);
323 err = fstat(fileno(fp), &path_stat);
329 if (!S_ISREG(path_stat.st_mode)) {
335 r = fscanf(fp, "%x %x", ®init.address, ®init.data);
337 zynqhdr->register_init[reg_count] = reginit;
340 r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
341 } while ((r != EOF) && (reg_count < HEADER_REGINITS));
345 static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
346 struct image_tool_params *params)
348 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
349 zynqmpimage_default_header(zynqhdr);
351 /* place image directly after header */
352 zynqhdr->image_offset =
353 cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
354 zynqhdr->image_size = cpu_to_le32(params->file_size -
355 sizeof(struct zynqmp_header));
356 zynqhdr->image_stored_size = zynqhdr->image_size;
357 zynqhdr->image_load = 0xfffc0000;
359 zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
363 zynqmpimage_pmufw(zynqhdr, params->imagename);
365 /* User can pass in text file with init list */
366 if (strlen(params->imagename2))
367 zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
369 zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
372 static int zynqmpimage_vrec_header(struct image_tool_params *params,
373 struct image_type_params *tparams)
375 struct stat path_stat;
376 char *filename = params->imagename;
379 /* Handle static case without PMUFW */
380 tparams->header_size = sizeof(struct zynqmp_header);
381 tparams->hdr = (void *)&zynqmpimage_header;
383 /* PMUFW name is passed via params->imagename */
384 if (strlen(filename) == 0)
387 fpmu = fopen(filename, "r");
389 fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
393 err = fstat(fileno(fpmu), &path_stat);
400 if (!S_ISREG(path_stat.st_mode)) {
406 /* Increase header size by PMUFW file size */
407 tparams->header_size += fsize(fpmu);
409 /* Allocate buffer with space for PMUFW */
410 dynamic_header = calloc(1, tparams->header_size);
411 tparams->hdr = dynamic_header;
418 "Xilinx ZynqMP Boot Image support",
419 sizeof(struct zynqmp_header),
420 (void *)&zynqmpimage_header,
421 zynqmpimage_check_params,
422 zynqmpimage_verify_header,
423 zynqmpimage_print_header,
424 zynqmpimage_set_header,
426 zynqmpimage_check_image_types,
428 zynqmpimage_vrec_header