2 * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
4 * SPDX-License-Identifier: GPL-2.0+
6 * The following Boot Header format/structures and values are defined in the
8 * * Xilinx Zynq-7000 Technical Reference Manual (Section 6.3)
9 * * Xilinx Zynq-7000 Software Developers Guide (Appendix A.7 and A.8)
11 * Expected Header Size = 0x8C0
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 * * 0x3a5c3c5a - bbRam
26 * 0x 30 - Image Offset
28 * 0x 38 - Reserved (0x00000000) (according to spec)
29 * * FSBL defines this field for Image Destination Address.
31 * 0x 40 - Image Stored Size
32 * 0x 44 - Reserved (0x00000000) (according to spec)
33 * * FSBL defines this field for QSPI configuration Data.
35 * 0x 4c - Unused (21 words)
38 * 0x a0 - Register Initialization, 256 Address and Data word pairs
39 * * List is terminated with an address of 0xffffffff or
40 * ... * at the max number of entries
42 * 0x8a0 - Unused (8 words)
45 * 0x8c0 - Data/Image starts here or above
48 #include "imagetool.h"
52 #define HEADER_INTERRUPT_DEFAULT (cpu_to_le32(0xeafffffe))
53 #define HEADER_REGINIT_NULL (cpu_to_le32(0xffffffff))
54 #define HEADER_WIDTHDETECTION (cpu_to_le32(0xaa995566))
55 #define HEADER_IMAGEIDENTIFIER (cpu_to_le32(0x584c4e58))
58 ENCRYPTION_EFUSE
= 0xa5c3c5a3,
59 ENCRYPTION_BBRAM
= 0x3a5c3c5a,
60 ENCRYPTION_NONE
= 0x0,
68 #define HEADER_INTERRUPT_VECTORS 8
69 #define HEADER_REGINITS 256
72 uint32_t interrupt_vectors
[HEADER_INTERRUPT_VECTORS
]; /* 0x0 */
73 uint32_t width_detection
; /* 0x20 */
74 uint32_t image_identifier
; /* 0x24 */
75 uint32_t encryption
; /* 0x28 */
76 uint32_t user_field
; /* 0x2c */
77 uint32_t image_offset
; /* 0x30 */
78 uint32_t image_size
; /* 0x34 */
79 uint32_t __reserved1
; /* 0x38 */
80 uint32_t image_load
; /* 0x3c */
81 uint32_t image_stored_size
; /* 0x40 */
82 uint32_t __reserved2
; /* 0x44 */
83 uint32_t checksum
; /* 0x48 */
84 uint32_t __reserved3
[21]; /* 0x4c */
85 struct zynq_reginit register_init
[HEADER_REGINITS
]; /* 0xa0 */
86 uint32_t __reserved4
[8]; /* 0x8a0 */
89 static struct zynq_header zynqimage_header
;
91 static uint32_t zynqimage_checksum(struct zynq_header
*ptr
)
93 uint32_t checksum
= 0;
98 checksum
+= le32_to_cpu(ptr
->width_detection
);
99 checksum
+= le32_to_cpu(ptr
->image_identifier
);
100 checksum
+= le32_to_cpu(ptr
->encryption
);
101 checksum
+= le32_to_cpu(ptr
->user_field
);
102 checksum
+= le32_to_cpu(ptr
->image_offset
);
103 checksum
+= le32_to_cpu(ptr
->image_size
);
104 checksum
+= le32_to_cpu(ptr
->__reserved1
);
105 checksum
+= le32_to_cpu(ptr
->image_load
);
106 checksum
+= le32_to_cpu(ptr
->image_stored_size
);
107 checksum
+= le32_to_cpu(ptr
->__reserved2
);
108 checksum
= ~checksum
;
110 return cpu_to_le32(checksum
);
113 static void zynqimage_default_header(struct zynq_header
*ptr
)
120 ptr
->width_detection
= HEADER_WIDTHDETECTION
;
121 ptr
->image_identifier
= HEADER_IMAGEIDENTIFIER
;
122 ptr
->encryption
= cpu_to_le32(ENCRYPTION_NONE
);
124 /* Setup not-supported/constant/reserved fields */
125 for (i
= 0; i
< HEADER_INTERRUPT_VECTORS
; i
++)
126 ptr
->interrupt_vectors
[i
] = HEADER_INTERRUPT_DEFAULT
;
128 for (i
= 0; i
< HEADER_REGINITS
; i
++) {
129 ptr
->register_init
[i
].address
= HEADER_REGINIT_NULL
;
130 ptr
->register_init
[i
].data
= HEADER_REGINIT_NULL
;
134 * Certain reserved fields are required to be set to 0, ensure they are
137 ptr
->__reserved1
= 0x0;
138 ptr
->__reserved2
= 0x0;
141 /* mkimage glue functions */
142 static int zynqimage_verify_header(unsigned char *ptr
, int image_size
,
143 struct image_tool_params
*params
)
145 struct zynq_header
*zynqhdr
= (struct zynq_header
*)ptr
;
147 if (image_size
< sizeof(struct zynq_header
))
150 if (zynqhdr
->width_detection
!= HEADER_WIDTHDETECTION
)
152 if (zynqhdr
->image_identifier
!= HEADER_IMAGEIDENTIFIER
)
155 if (zynqimage_checksum(zynqhdr
) != zynqhdr
->checksum
)
161 static void zynqimage_print_header(const void *ptr
)
163 struct zynq_header
*zynqhdr
= (struct zynq_header
*)ptr
;
166 printf("Image Type : Xilinx Zynq Boot Image support\n");
167 printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr
->image_offset
));
168 printf("Image Size : %lu bytes (%lu bytes packed)\n",
169 (unsigned long)le32_to_cpu(zynqhdr
->image_size
),
170 (unsigned long)le32_to_cpu(zynqhdr
->image_stored_size
));
171 printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr
->image_load
));
172 printf("User Field : 0x%08x\n", le32_to_cpu(zynqhdr
->user_field
));
173 printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr
->checksum
));
175 for (i
= 0; i
< HEADER_INTERRUPT_VECTORS
; i
++) {
176 if (zynqhdr
->interrupt_vectors
[i
] == HEADER_INTERRUPT_DEFAULT
)
179 printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i
,
180 le32_to_cpu(zynqhdr
->interrupt_vectors
[i
]));
183 for (i
= 0; i
< HEADER_REGINITS
; i
++) {
184 if (zynqhdr
->register_init
[i
].address
== HEADER_REGINIT_NULL
)
188 printf("Custom Register Initialization:\n");
190 printf(" @ 0x%08x -> 0x%08x\n",
191 le32_to_cpu(zynqhdr
->register_init
[i
].address
),
192 le32_to_cpu(zynqhdr
->register_init
[i
].data
));
196 static int zynqimage_check_params(struct image_tool_params
*params
)
201 if (params
->addr
!= 0x0) {
202 fprintf(stderr
, "Error: Load Address cannot be specified.\n");
207 * If the entry point is specified ensure it is 64 byte aligned.
209 if (params
->eflag
&& (params
->ep
% 64 != 0)) {
211 "Error: Entry Point must be aligned to a 64-byte boundary.\n");
215 return !(params
->lflag
|| params
->dflag
);
218 static int zynqimage_check_image_types(uint8_t type
)
220 if (type
== IH_TYPE_ZYNQIMAGE
)
225 static void zynqimage_parse_initparams(struct zynq_header
*zynqhdr
,
226 const char *filename
)
229 struct zynq_reginit reginit
;
230 unsigned int reg_count
= 0;
232 struct stat path_stat
;
234 /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
235 fp
= fopen(filename
, "r");
237 fprintf(stderr
, "Cannot open initparams file: %s\n", filename
);
241 err
= fstat(fileno(fp
), &path_stat
);
247 if (!S_ISREG(path_stat
.st_mode
)) {
253 r
= fscanf(fp
, "%x %x", ®init
.address
, ®init
.data
);
255 zynqhdr
->register_init
[reg_count
] = reginit
;
258 r
= fscanf(fp
, "%*[^\n]\n"); /* Skip to next line */
259 } while ((r
!= EOF
) && (reg_count
< HEADER_REGINITS
));
263 static void zynqimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
264 struct image_tool_params
*params
)
266 struct zynq_header
*zynqhdr
= (struct zynq_header
*)ptr
;
267 zynqimage_default_header(zynqhdr
);
269 /* place image directly after header */
270 zynqhdr
->image_offset
=
271 cpu_to_le32((uint32_t)sizeof(struct zynq_header
));
272 zynqhdr
->image_size
= cpu_to_le32((uint32_t)sbuf
->st_size
);
273 zynqhdr
->image_stored_size
= zynqhdr
->image_size
;
274 zynqhdr
->image_load
= 0x0;
276 zynqhdr
->image_load
= cpu_to_le32((uint32_t)params
->ep
);
278 /* User can pass in text file with init list */
279 if (strlen(params
->imagename2
))
280 zynqimage_parse_initparams(zynqhdr
, params
->imagename2
);
282 zynqhdr
->checksum
= zynqimage_checksum(zynqhdr
);
287 "Xilinx Zynq Boot Image support",
288 sizeof(struct zynq_header
),
289 (void *)&zynqimage_header
,
290 zynqimage_check_params
,
291 zynqimage_verify_header
,
292 zynqimage_print_header
,
293 zynqimage_set_header
,
295 zynqimage_check_image_types
,