2 * Copyright (C) 2016 Michal Simek <michals@xilinx.com>
3 * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
5 * SPDX-License-Identifier: GPL-2.0+
7 * The following Boot Header format/structures and values are defined in the
9 * * ug1085 ZynqMP TRM doc v1.4 (Chapter 11, Table 11-4)
11 * Expected Header Size = 0x9C0
12 * Forced as 'little' endian, 32-bit words
14 * 0x 0 - Interrupt table (8 words)
15 * ... (Default value = 0xeafffffe)
17 * 0x 20 - Width detection
18 * * DEFAULT_WIDTHDETECTION 0xaa995566
19 * 0x 24 - Image identifier
20 * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58
23 * * 0xa5c3c5a3 - eFuse
24 * * 0xa5c3c5a7 - obfuscated key in eFUSE
25 * * 0x3a5c3c5a - bbRam
26 * * 0xa35c7ca5 - obfuscated key in boot header
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
39 * 0x 70 - User defined
42 * 0x a0 - Secure header initialization vector
45 * 0x ac - Obfuscated key initialization vector
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
55 * 0x9c0 - Data/Image starts here or above
58 #include "imagetool.h"
62 #define HEADER_INTERRUPT_DEFAULT (cpu_to_le32(0xeafffffe))
63 #define HEADER_REGINIT_NULL (cpu_to_le32(0xffffffff))
64 #define HEADER_WIDTHDETECTION (cpu_to_le32(0xaa995566))
65 #define HEADER_IMAGEIDENTIFIER (cpu_to_le32(0x584c4e58))
68 ENCRYPTION_EFUSE = 0xa5c3c5a3,
69 ENCRYPTION_OEFUSE = 0xa5c3c5a7,
70 ENCRYPTION_BBRAM = 0x3a5c3c5a,
71 ENCRYPTION_OBBRAM = 0xa35c7ca5,
72 ENCRYPTION_NONE = 0x0,
75 struct zynqmp_reginit {
80 #define HEADER_INTERRUPT_VECTORS 8
81 #define HEADER_REGINITS 256
83 struct zynqmp_header {
84 uint32_t interrupt_vectors[HEADER_INTERRUPT_VECTORS]; /* 0x0 */
85 uint32_t width_detection; /* 0x20 */
86 uint32_t image_identifier; /* 0x24 */
87 uint32_t encryption; /* 0x28 */
88 uint32_t image_load; /* 0x2c */
89 uint32_t image_offset; /* 0x30 */
90 uint32_t pfw_image_length; /* 0x34 */
91 uint32_t total_pfw_image_length; /* 0x38 */
92 uint32_t image_size; /* 0x3c */
93 uint32_t image_stored_size; /* 0x40 */
94 uint32_t image_attributes; /* 0x44 */
95 uint32_t checksum; /* 0x48 */
96 uint32_t __reserved1[27]; /* 0x4c */
97 struct zynqmp_reginit register_init[HEADER_REGINITS]; /* 0xb8 */
98 uint32_t __reserved4[66]; /* 0x9c0 */
101 static struct zynqmp_header zynqmpimage_header;
102 static void *dynamic_header;
105 static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
107 uint32_t checksum = 0;
112 checksum += le32_to_cpu(ptr->width_detection);
113 checksum += le32_to_cpu(ptr->image_identifier);
114 checksum += le32_to_cpu(ptr->encryption);
115 checksum += le32_to_cpu(ptr->image_load);
116 checksum += le32_to_cpu(ptr->image_offset);
117 checksum += le32_to_cpu(ptr->pfw_image_length);
118 checksum += le32_to_cpu(ptr->total_pfw_image_length);
119 checksum += le32_to_cpu(ptr->image_size);
120 checksum += le32_to_cpu(ptr->image_stored_size);
121 checksum += le32_to_cpu(ptr->image_attributes);
122 checksum = ~checksum;
124 return cpu_to_le32(checksum);
127 static void zynqmpimage_default_header(struct zynqmp_header *ptr)
134 ptr->width_detection = HEADER_WIDTHDETECTION;
135 ptr->image_attributes = 0x800;
136 ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
137 ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
139 /* Setup not-supported/constant/reserved fields */
140 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
141 ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
143 for (i = 0; i < HEADER_REGINITS; i++) {
144 ptr->register_init[i].address = HEADER_REGINIT_NULL;
145 ptr->register_init[i].data = 0;
149 * Certain reserved fields are required to be set to 0, ensure they are
152 ptr->pfw_image_length = 0x0;
153 ptr->total_pfw_image_length = 0x0;
156 /* mkimage glue functions */
157 static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
158 struct image_tool_params *params)
160 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
162 if (image_size < sizeof(struct zynqmp_header))
165 if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
167 if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
170 if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
176 static void zynqmpimage_print_header(const void *ptr)
178 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
181 printf("Image Type : Xilinx Zynq Boot Image support\n");
182 printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
183 printf("Image Size : %lu bytes (%lu bytes packed)\n",
184 (unsigned long)le32_to_cpu(zynqhdr->image_size),
185 (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
187 if (zynqhdr->pfw_image_length)
188 printf("PMUFW Size : %lu bytes (%lu bytes packed)\n",
189 (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length),
190 (unsigned long)le32_to_cpu(
191 zynqhdr->total_pfw_image_length));
193 printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
194 printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
196 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
197 if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
200 printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
201 le32_to_cpu(zynqhdr->interrupt_vectors[i]));
204 for (i = 0; i < HEADER_REGINITS; i++) {
205 if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
209 printf("Custom Register Initialization:\n");
211 printf(" @ 0x%08x -> 0x%08x\n",
212 le32_to_cpu(zynqhdr->register_init[i].address),
213 le32_to_cpu(zynqhdr->register_init[i].data));
216 free(dynamic_header);
219 static int zynqmpimage_check_params(struct image_tool_params *params)
224 if (params->addr != 0x0) {
225 fprintf(stderr, "Error: Load Address cannot be specified.\n");
230 * If the entry point is specified ensure it is 64 byte aligned.
232 if (params->eflag && (params->ep % 64 != 0)) {
234 "Error: Entry Point must be aligned to a 64-byte boundary.\n");
238 return !(params->lflag || params->dflag);
241 static int zynqmpimage_check_image_types(uint8_t type)
243 if (type == IH_TYPE_ZYNQMPIMAGE)
248 static int fsize(FILE *fp)
251 int origin = ftell(fp);
253 fseek(fp, 0L, SEEK_END);
257 fseek(fp, origin, SEEK_SET);
262 static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
263 const char *filename)
267 /* Setup PMU fw size */
268 zynqhdr->pfw_image_length = fsize(fpmu);
269 zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length;
271 zynqhdr->image_size -= zynqhdr->pfw_image_length;
272 zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length;
274 /* Read the whole PMUFW to the header */
275 size = fread(&zynqhdr->__reserved4[66], 1,
276 zynqhdr->pfw_image_length, fpmu);
277 if (size != zynqhdr->pfw_image_length) {
278 fprintf(stderr, "Cannot read PMUFW file: %s\n", filename);
286 static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
287 const char *filename)
290 struct zynqmp_reginit reginit;
291 unsigned int reg_count = 0;
293 struct stat path_stat;
295 /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
296 fp = fopen(filename, "r");
298 fprintf(stderr, "Cannot open initparams file: %s\n", filename);
302 err = fstat(fileno(fp), &path_stat);
308 if (!S_ISREG(path_stat.st_mode)) {
314 r = fscanf(fp, "%x %x", ®init.address, ®init.data);
316 zynqhdr->register_init[reg_count] = reginit;
319 r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
320 } while ((r != EOF) && (reg_count < HEADER_REGINITS));
324 static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
325 struct image_tool_params *params)
327 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
328 zynqmpimage_default_header(zynqhdr);
330 /* place image directly after header */
331 zynqhdr->image_offset =
332 cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
333 zynqhdr->image_size = cpu_to_le32(params->file_size -
334 sizeof(struct zynqmp_header));
335 zynqhdr->image_stored_size = zynqhdr->image_size;
336 zynqhdr->image_load = 0xfffc0000;
338 zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
342 zynqmpimage_pmufw(zynqhdr, params->imagename);
344 /* User can pass in text file with init list */
345 if (strlen(params->imagename2))
346 zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
348 zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
351 static int zynqmpimage_vrec_header(struct image_tool_params *params,
352 struct image_type_params *tparams)
354 struct stat path_stat;
355 char *filename = params->imagename;
358 /* Handle static case without PMUFW */
359 tparams->header_size = sizeof(struct zynqmp_header);
360 tparams->hdr = (void *)&zynqmpimage_header;
362 /* PMUFW name is passed via params->imagename */
363 if (strlen(filename) == 0)
366 fpmu = fopen(filename, "r");
368 fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
372 err = fstat(fileno(fpmu), &path_stat);
379 if (!S_ISREG(path_stat.st_mode)) {
385 /* Increase header size by PMUFW file size */
386 tparams->header_size += fsize(fpmu);
388 /* Allocate buffer with space for PMUFW */
389 dynamic_header = calloc(1, tparams->header_size);
390 tparams->hdr = dynamic_header;
397 "Xilinx ZynqMP Boot Image support",
398 sizeof(struct zynqmp_header),
399 (void *)&zynqmpimage_header,
400 zynqmpimage_check_params,
401 zynqmpimage_verify_header,
402 zynqmpimage_print_header,
403 zynqmpimage_set_header,
405 zynqmpimage_check_image_types,
407 zynqmpimage_vrec_header