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 (Chapter 9, Table 9-3)
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
;
103 static uint32_t zynqmpimage_checksum(struct zynqmp_header
*ptr
)
105 uint32_t checksum
= 0;
110 checksum
+= le32_to_cpu(ptr
->width_detection
);
111 checksum
+= le32_to_cpu(ptr
->image_identifier
);
112 checksum
+= le32_to_cpu(ptr
->encryption
);
113 checksum
+= le32_to_cpu(ptr
->image_load
);
114 checksum
+= le32_to_cpu(ptr
->image_offset
);
115 checksum
+= le32_to_cpu(ptr
->pfw_image_length
);
116 checksum
+= le32_to_cpu(ptr
->total_pfw_image_length
);
117 checksum
+= le32_to_cpu(ptr
->image_size
);
118 checksum
+= le32_to_cpu(ptr
->image_stored_size
);
119 checksum
+= le32_to_cpu(ptr
->image_attributes
);
120 checksum
= ~checksum
;
122 return cpu_to_le32(checksum
);
125 static void zynqmpimage_default_header(struct zynqmp_header
*ptr
)
132 ptr
->width_detection
= HEADER_WIDTHDETECTION
;
133 ptr
->image_attributes
= 0x800;
134 ptr
->image_identifier
= HEADER_IMAGEIDENTIFIER
;
135 ptr
->encryption
= cpu_to_le32(ENCRYPTION_NONE
);
137 /* Setup not-supported/constant/reserved fields */
138 for (i
= 0; i
< HEADER_INTERRUPT_VECTORS
; i
++)
139 ptr
->interrupt_vectors
[i
] = HEADER_INTERRUPT_DEFAULT
;
141 for (i
= 0; i
< HEADER_REGINITS
; i
++) {
142 ptr
->register_init
[i
].address
= HEADER_REGINIT_NULL
;
143 ptr
->register_init
[i
].data
= 0;
147 * Certain reserved fields are required to be set to 0, ensure they are
150 ptr
->pfw_image_length
= 0x0;
151 ptr
->total_pfw_image_length
= 0x0;
154 /* mkimage glue functions */
155 static int zynqmpimage_verify_header(unsigned char *ptr
, int image_size
,
156 struct image_tool_params
*params
)
158 struct zynqmp_header
*zynqhdr
= (struct zynqmp_header
*)ptr
;
160 if (image_size
< sizeof(struct zynqmp_header
))
163 if (zynqhdr
->width_detection
!= HEADER_WIDTHDETECTION
)
165 if (zynqhdr
->image_identifier
!= HEADER_IMAGEIDENTIFIER
)
168 if (zynqmpimage_checksum(zynqhdr
) != zynqhdr
->checksum
)
174 static void zynqmpimage_print_header(const void *ptr
)
176 struct zynqmp_header
*zynqhdr
= (struct zynqmp_header
*)ptr
;
179 printf("Image Type : Xilinx Zynq Boot Image support\n");
180 printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr
->image_offset
));
181 printf("Image Size : %lu bytes (%lu bytes packed)\n",
182 (unsigned long)le32_to_cpu(zynqhdr
->image_size
),
183 (unsigned long)le32_to_cpu(zynqhdr
->image_stored_size
));
184 printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr
->image_load
));
185 printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr
->checksum
));
187 for (i
= 0; i
< HEADER_INTERRUPT_VECTORS
; i
++) {
188 if (zynqhdr
->interrupt_vectors
[i
] == HEADER_INTERRUPT_DEFAULT
)
191 printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i
,
192 le32_to_cpu(zynqhdr
->interrupt_vectors
[i
]));
195 for (i
= 0; i
< HEADER_REGINITS
; i
++) {
196 if (zynqhdr
->register_init
[i
].address
== HEADER_REGINIT_NULL
)
200 printf("Custom Register Initialization:\n");
202 printf(" @ 0x%08x -> 0x%08x\n",
203 le32_to_cpu(zynqhdr
->register_init
[i
].address
),
204 le32_to_cpu(zynqhdr
->register_init
[i
].data
));
208 static int zynqmpimage_check_params(struct image_tool_params
*params
)
213 if (params
->addr
!= 0x0) {
214 fprintf(stderr
, "Error: Load Address cannot be specified.\n");
219 * If the entry point is specified ensure it is 64 byte aligned.
221 if (params
->eflag
&& (params
->ep
% 64 != 0)) {
223 "Error: Entry Point must be aligned to a 64-byte boundary.\n");
227 return !(params
->lflag
|| params
->dflag
);
230 static int zynqmpimage_check_image_types(uint8_t type
)
232 if (type
== IH_TYPE_ZYNQMPIMAGE
)
237 static void zynqmpimage_parse_initparams(struct zynqmp_header
*zynqhdr
,
238 const char *filename
)
241 struct zynqmp_reginit reginit
;
242 unsigned int reg_count
= 0;
244 struct stat path_stat
;
246 /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
247 fp
= fopen(filename
, "r");
249 fprintf(stderr
, "Cannot open initparams file: %s\n", filename
);
253 err
= fstat(fileno(fp
), &path_stat
);
259 if (!S_ISREG(path_stat
.st_mode
)) {
265 r
= fscanf(fp
, "%x %x", ®init
.address
, ®init
.data
);
267 zynqhdr
->register_init
[reg_count
] = reginit
;
270 r
= fscanf(fp
, "%*[^\n]\n"); /* Skip to next line */
271 } while ((r
!= EOF
) && (reg_count
< HEADER_REGINITS
));
275 static void zynqmpimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
276 struct image_tool_params
*params
)
278 struct zynqmp_header
*zynqhdr
= (struct zynqmp_header
*)ptr
;
279 zynqmpimage_default_header(zynqhdr
);
281 /* place image directly after header */
282 zynqhdr
->image_offset
=
283 cpu_to_le32((uint32_t)sizeof(struct zynqmp_header
));
284 zynqhdr
->image_size
= cpu_to_le32(params
->file_size
-
285 sizeof(struct zynqmp_header
));
286 zynqhdr
->image_stored_size
= zynqhdr
->image_size
;
287 zynqhdr
->image_load
= 0xfffc0000;
289 zynqhdr
->image_load
= cpu_to_le32((uint32_t)params
->ep
);
291 /* User can pass in text file with init list */
292 if (strlen(params
->imagename2
))
293 zynqmpimage_parse_initparams(zynqhdr
, params
->imagename2
);
295 zynqhdr
->checksum
= zynqmpimage_checksum(zynqhdr
);
300 "Xilinx ZynqMP Boot Image support",
301 sizeof(struct zynqmp_header
),
302 (void *)&zynqmpimage_header
,
303 zynqmpimage_check_params
,
304 zynqmpimage_verify_header
,
305 zynqmpimage_print_header
,
306 zynqmpimage_set_header
,
308 zynqmpimage_check_image_types
,