]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/zynqmpimage.c
SPDX: Convert all of our single license tags to Linux Kernel style
[thirdparty/u-boot.git] / tools / zynqmpimage.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
d9b58b30
MS
2/*
3 * Copyright (C) 2016 Michal Simek <michals@xilinx.com>
4 * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
5 *
d9b58b30
MS
6 * The following Boot Header format/structures and values are defined in the
7 * following documents:
df4950e3 8 * * ug1085 ZynqMP TRM doc v1.4 (Chapter 11, Table 11-4)
d9b58b30
MS
9 *
10 * Expected Header Size = 0x9C0
11 * Forced as 'little' endian, 32-bit words
12 *
13 * 0x 0 - Interrupt table (8 words)
14 * ... (Default value = 0xeafffffe)
15 * 0x 1f
16 * 0x 20 - Width detection
17 * * DEFAULT_WIDTHDETECTION 0xaa995566
18 * 0x 24 - Image identifier
19 * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58
20 * 0x 28 - Encryption
21 * * 0x00000000 - None
22 * * 0xa5c3c5a3 - eFuse
23 * * 0xa5c3c5a7 - obfuscated key in eFUSE
24 * * 0x3a5c3c5a - bbRam
25 * * 0xa35c7ca5 - obfuscated key in boot header
26 * 0x 2C - Image load
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
35 * ...
36 * 0x 68
37 * 0x 6c - Reserved
38 * 0x 70 - User defined
39 * ...
40 * 0x 9c
41 * 0x a0 - Secure header initialization vector
42 * ...
43 * 0x a8
44 * 0x ac - Obfuscated key initialization vector
45 * ...
46 * 0x b4
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
50 * 0x8b4
51 * 0x8b8 - Reserved
52 * ...
53 * 0x9bf
54 * 0x9c0 - Data/Image starts here or above
55 */
56
57#include "imagetool.h"
58#include "mkimage.h"
59#include <image.h>
60
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))
65
66enum {
67 ENCRYPTION_EFUSE = 0xa5c3c5a3,
68 ENCRYPTION_OEFUSE = 0xa5c3c5a7,
69 ENCRYPTION_BBRAM = 0x3a5c3c5a,
70 ENCRYPTION_OBBRAM = 0xa35c7ca5,
71 ENCRYPTION_NONE = 0x0,
72};
73
74struct zynqmp_reginit {
75 uint32_t address;
76 uint32_t data;
77};
78
79#define HEADER_INTERRUPT_VECTORS 8
80#define HEADER_REGINITS 256
81
82struct 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 */
98};
99
100static struct zynqmp_header zynqmpimage_header;
c85a6b79
MS
101static void *dynamic_header;
102static FILE *fpmu;
d9b58b30
MS
103
104static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
105{
106 uint32_t checksum = 0;
107
108 if (ptr == NULL)
109 return 0;
110
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;
122
123 return cpu_to_le32(checksum);
124}
125
126static void zynqmpimage_default_header(struct zynqmp_header *ptr)
127{
128 int i;
129
130 if (ptr == NULL)
131 return;
132
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);
137
138 /* Setup not-supported/constant/reserved fields */
139 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
140 ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
141
142 for (i = 0; i < HEADER_REGINITS; i++) {
143 ptr->register_init[i].address = HEADER_REGINIT_NULL;
144 ptr->register_init[i].data = 0;
145 }
146
147 /*
148 * Certain reserved fields are required to be set to 0, ensure they are
149 * set as such.
150 */
151 ptr->pfw_image_length = 0x0;
152 ptr->total_pfw_image_length = 0x0;
153}
154
155/* mkimage glue functions */
156static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
157 struct image_tool_params *params)
158{
159 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
160
161 if (image_size < sizeof(struct zynqmp_header))
162 return -1;
163
164 if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
165 return -1;
166 if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
167 return -1;
168
169 if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
170 return -1;
171
172 return 0;
173}
174
175static void zynqmpimage_print_header(const void *ptr)
176{
177 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
178 int i;
179
d28baea0 180 printf("Image Type : Xilinx ZynqMP Boot Image support\n");
d9b58b30
MS
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));
c85a6b79
MS
185
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));
191
d9b58b30
MS
192 printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
193 printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
194
195 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
196 if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
197 continue;
198
199 printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
200 le32_to_cpu(zynqhdr->interrupt_vectors[i]));
201 }
202
203 for (i = 0; i < HEADER_REGINITS; i++) {
204 if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
205 break;
206
207 if (i == 0)
208 printf("Custom Register Initialization:\n");
209
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));
213 }
c85a6b79
MS
214
215 free(dynamic_header);
d9b58b30
MS
216}
217
218static int zynqmpimage_check_params(struct image_tool_params *params)
219{
220 if (!params)
221 return 0;
222
223 if (params->addr != 0x0) {
224 fprintf(stderr, "Error: Load Address cannot be specified.\n");
225 return -1;
226 }
227
228 /*
229 * If the entry point is specified ensure it is 64 byte aligned.
230 */
231 if (params->eflag && (params->ep % 64 != 0)) {
232 fprintf(stderr,
233 "Error: Entry Point must be aligned to a 64-byte boundary.\n");
234 return -1;
235 }
236
237 return !(params->lflag || params->dflag);
238}
239
240static int zynqmpimage_check_image_types(uint8_t type)
241{
242 if (type == IH_TYPE_ZYNQMPIMAGE)
243 return EXIT_SUCCESS;
244 return EXIT_FAILURE;
245}
246
6d0cbbd5 247static uint32_t fsize(FILE *fp)
c85a6b79 248{
6d0cbbd5
MS
249 int size, ret, origin;
250
251 origin = ftell(fp);
252 if (origin < 0) {
253 fprintf(stderr, "Incorrect file size\n");
254 fclose(fp);
255 exit(2);
256 }
257
258 ret = fseek(fp, 0L, SEEK_END);
259 if (ret) {
260 fprintf(stderr, "Incorrect file SEEK_END\n");
261 fclose(fp);
262 exit(3);
263 }
c85a6b79 264
c85a6b79 265 size = ftell(fp);
6d0cbbd5
MS
266 if (size < 0) {
267 fprintf(stderr, "Incorrect file size\n");
268 fclose(fp);
269 exit(4);
270 }
c85a6b79
MS
271
272 /* going back */
6d0cbbd5
MS
273 ret = fseek(fp, origin, SEEK_SET);
274 if (ret) {
275 fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin);
276 fclose(fp);
277 exit(3);
278 }
c85a6b79
MS
279
280 return size;
281}
282
283static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
284 const char *filename)
285{
286 uint32_t size;
287
288 /* Setup PMU fw size */
289 zynqhdr->pfw_image_length = fsize(fpmu);
290 zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length;
291
292 zynqhdr->image_size -= zynqhdr->pfw_image_length;
293 zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length;
294
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);
300 fclose(fpmu);
301 exit(1);
302 }
303
304 fclose(fpmu);
305}
306
3b646080
ML
307static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
308 const char *filename)
309{
56c7e801 310 FILE *fp;
3b646080
ML
311 struct zynqmp_reginit reginit;
312 unsigned int reg_count = 0;
ebe0f53f 313 int r, err;
56c7e801
MS
314 struct stat path_stat;
315
56c7e801
MS
316 /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
317 fp = fopen(filename, "r");
3b646080
ML
318 if (!fp) {
319 fprintf(stderr, "Cannot open initparams file: %s\n", filename);
320 exit(1);
321 }
ebe0f53f
MS
322
323 err = fstat(fileno(fp), &path_stat);
ac71d410
MS
324 if (err) {
325 fclose(fp);
ebe0f53f 326 return;
ac71d410 327 }
ebe0f53f 328
ac71d410
MS
329 if (!S_ISREG(path_stat.st_mode)) {
330 fclose(fp);
ebe0f53f 331 return;
ac71d410 332 }
ebe0f53f 333
3b646080
ML
334 do {
335 r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
336 if (r == 2) {
337 zynqhdr->register_init[reg_count] = reginit;
338 ++reg_count;
339 }
340 r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
341 } while ((r != EOF) && (reg_count < HEADER_REGINITS));
342 fclose(fp);
343}
344
d9b58b30
MS
345static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
346 struct image_tool_params *params)
347{
348 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
349 zynqmpimage_default_header(zynqhdr);
350
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;
358 if (params->eflag)
359 zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
360
c85a6b79
MS
361 /* PMUFW */
362 if (fpmu)
363 zynqmpimage_pmufw(zynqhdr, params->imagename);
364
3b646080
ML
365 /* User can pass in text file with init list */
366 if (strlen(params->imagename2))
367 zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
368
d9b58b30
MS
369 zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
370}
371
c85a6b79
MS
372static int zynqmpimage_vrec_header(struct image_tool_params *params,
373 struct image_type_params *tparams)
374{
375 struct stat path_stat;
376 char *filename = params->imagename;
377 int err;
378
379 /* Handle static case without PMUFW */
380 tparams->header_size = sizeof(struct zynqmp_header);
381 tparams->hdr = (void *)&zynqmpimage_header;
382
383 /* PMUFW name is passed via params->imagename */
384 if (strlen(filename) == 0)
385 return EXIT_SUCCESS;
386
387 fpmu = fopen(filename, "r");
388 if (!fpmu) {
389 fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
390 return EXIT_FAILURE;
391 }
392
393 err = fstat(fileno(fpmu), &path_stat);
394 if (err) {
395 fclose(fpmu);
396 fpmu = NULL;
397 return EXIT_FAILURE;
398 }
399
400 if (!S_ISREG(path_stat.st_mode)) {
401 fclose(fpmu);
402 fpmu = NULL;
403 return EXIT_FAILURE;
404 }
405
406 /* Increase header size by PMUFW file size */
407 tparams->header_size += fsize(fpmu);
408
409 /* Allocate buffer with space for PMUFW */
410 dynamic_header = calloc(1, tparams->header_size);
411 tparams->hdr = dynamic_header;
412
413 return EXIT_SUCCESS;
414}
415
d9b58b30
MS
416U_BOOT_IMAGE_TYPE(
417 zynqmpimage,
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,
425 NULL,
426 zynqmpimage_check_image_types,
427 NULL,
c85a6b79 428 zynqmpimage_vrec_header
d9b58b30 429);