blob: 2795a2b9a778f616bceb0a2f70a6286a20ad8df1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simekfa124662016-04-27 14:03:29 +02002/*
Michal Simeka8c94362023-07-10 14:35:49 +02003 * Copyright (C) 2016 Michal Simek <michal.simek@amd.com>
Michal Simekfa124662016-04-27 14:03:29 +02004 * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
5 *
Michal Simekfa124662016-04-27 14:03:29 +02006 * The following Boot Header format/structures and values are defined in the
7 * following documents:
Jean-Francois Dagenais52b1a502017-03-23 07:39:14 -04008 * * ug1085 ZynqMP TRM doc v1.4 (Chapter 11, Table 11-4)
Alexander Graf7392bb82018-04-13 14:18:49 +02009 * * ug1137 ZynqMP Software Developer Guide v6.0 (Chapter 16)
Michal Simekfa124662016-04-27 14:03:29 +020010 *
11 * Expected Header Size = 0x9C0
12 * Forced as 'little' endian, 32-bit words
13 *
14 * 0x 0 - Interrupt table (8 words)
15 * ... (Default value = 0xeafffffe)
16 * 0x 1f
17 * 0x 20 - Width detection
18 * * DEFAULT_WIDTHDETECTION 0xaa995566
19 * 0x 24 - Image identifier
20 * * DEFAULT_IMAGEIDENTIFIER 0x584c4e58
21 * 0x 28 - Encryption
22 * * 0x00000000 - None
23 * * 0xa5c3c5a3 - eFuse
24 * * 0xa5c3c5a7 - obfuscated key in eFUSE
25 * * 0x3a5c3c5a - bbRam
26 * * 0xa35c7ca5 - obfuscated key in boot header
27 * 0x 2C - Image load
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
36 * ...
37 * 0x 68
38 * 0x 6c - Reserved
39 * 0x 70 - User defined
40 * ...
41 * 0x 9c
42 * 0x a0 - Secure header initialization vector
43 * ...
44 * 0x a8
45 * 0x ac - Obfuscated key initialization vector
46 * ...
47 * 0x b4
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
51 * 0x8b4
52 * 0x8b8 - Reserved
53 * ...
54 * 0x9bf
55 * 0x9c0 - Data/Image starts here or above
56 */
57
58#include "imagetool.h"
59#include "mkimage.h"
Alexander Graf46e3a002018-04-13 14:18:50 +020060#include "zynqmpimage.h"
Michal Simekfa124662016-04-27 14:03:29 +020061#include <image.h>
62
Michal Simekfa124662016-04-27 14:03:29 +020063static struct zynqmp_header zynqmpimage_header;
Michal Simekdde5a882016-10-21 12:58:17 +020064static void *dynamic_header;
65static FILE *fpmu;
Michal Simekfa124662016-04-27 14:03:29 +020066
67static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
68{
69 uint32_t checksum = 0;
70
71 if (ptr == NULL)
72 return 0;
73
74 checksum += le32_to_cpu(ptr->width_detection);
75 checksum += le32_to_cpu(ptr->image_identifier);
76 checksum += le32_to_cpu(ptr->encryption);
77 checksum += le32_to_cpu(ptr->image_load);
78 checksum += le32_to_cpu(ptr->image_offset);
79 checksum += le32_to_cpu(ptr->pfw_image_length);
80 checksum += le32_to_cpu(ptr->total_pfw_image_length);
81 checksum += le32_to_cpu(ptr->image_size);
82 checksum += le32_to_cpu(ptr->image_stored_size);
83 checksum += le32_to_cpu(ptr->image_attributes);
84 checksum = ~checksum;
85
86 return cpu_to_le32(checksum);
87}
88
Alexander Graf5329d672018-04-13 14:18:52 +020089void zynqmpimage_default_header(struct zynqmp_header *ptr)
Michal Simekfa124662016-04-27 14:03:29 +020090{
91 int i;
92
93 if (ptr == NULL)
94 return;
95
96 ptr->width_detection = HEADER_WIDTHDETECTION;
Alexander Graf7392bb82018-04-13 14:18:49 +020097 ptr->image_attributes = HEADER_CPU_SELECT_A53_64BIT;
Michal Simekfa124662016-04-27 14:03:29 +020098 ptr->image_identifier = HEADER_IMAGEIDENTIFIER;
99 ptr->encryption = cpu_to_le32(ENCRYPTION_NONE);
100
101 /* Setup not-supported/constant/reserved fields */
102 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
103 ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
104
105 for (i = 0; i < HEADER_REGINITS; i++) {
106 ptr->register_init[i].address = HEADER_REGINIT_NULL;
107 ptr->register_init[i].data = 0;
108 }
109
110 /*
111 * Certain reserved fields are required to be set to 0, ensure they are
112 * set as such.
113 */
114 ptr->pfw_image_length = 0x0;
115 ptr->total_pfw_image_length = 0x0;
116}
117
118/* mkimage glue functions */
119static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
120 struct image_tool_params *params)
121{
122 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
123
124 if (image_size < sizeof(struct zynqmp_header))
125 return -1;
126
127 if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
128 return -1;
129 if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
130 return -1;
131
132 if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
133 return -1;
134
135 return 0;
136}
137
Alexander Graf7392bb82018-04-13 14:18:49 +0200138static void print_partition(const void *ptr, const struct partition_header *ph)
139{
140 uint32_t attr = le32_to_cpu(ph->attributes);
141 unsigned long len = le32_to_cpu(ph->len) * 4;
Brandon Maiera2fbda02024-01-04 18:50:07 +0000142 unsigned long len_enc = le32_to_cpu(ph->len_enc) * 4;
143 unsigned long len_unenc = le32_to_cpu(ph->len_unenc) * 4;
Alexander Graf7392bb82018-04-13 14:18:49 +0200144 const char *part_owner;
145 const char *dest_devs[0x8] = {
146 "none", "PS", "PL", "PMU", "unknown", "unknown", "unknown",
147 "unknown"
148 };
149
150 switch (attr & PART_ATTR_PART_OWNER_MASK) {
151 case PART_ATTR_PART_OWNER_FSBL:
152 part_owner = "FSBL";
153 break;
154 case PART_ATTR_PART_OWNER_UBOOT:
155 part_owner = "U-Boot";
156 break;
157 default:
158 part_owner = "Unknown";
159 break;
160 }
161
162 printf("%s payload on CPU %s (%s):\n", part_owner,
163 dest_cpus[(attr & PART_ATTR_DEST_CPU_MASK) >> 8],
164 dest_devs[(attr & PART_ATTR_DEST_DEVICE_MASK) >> 4]);
165
166 printf(" Offset : 0x%08x\n", le32_to_cpu(ph->offset) * 4);
167 printf(" Size : %lu (0x%lx) bytes\n", len, len);
Brandon Maiera2fbda02024-01-04 18:50:07 +0000168 if (len != len_unenc)
169 printf(" Size Data : %lu (0x%lx) bytes\n", len_unenc, len_unenc);
170 if (len_unenc != len_enc)
171 printf(" Size Enc : %lu (0x%lx) bytes\n", len_unenc, len_unenc);
Alexander Graf7392bb82018-04-13 14:18:49 +0200172 printf(" Load : 0x%08llx",
173 (unsigned long long)le64_to_cpu(ph->load_address));
174 if (ph->load_address != ph->entry_point)
175 printf(" (entry=0x%08llx)\n",
176 (unsigned long long)le64_to_cpu(ph->entry_point));
177 else
178 printf("\n");
179 printf(" Attributes : ");
180
181 if (attr & PART_ATTR_VEC_LOCATION)
182 printf("vec ");
183
184 if (attr & PART_ATTR_ENCRYPTED)
185 printf("encrypted ");
186
187 switch (attr & PART_ATTR_CHECKSUM_MASK) {
188 case PART_ATTR_CHECKSUM_MD5:
189 printf("md5 ");
190 break;
191 case PART_ATTR_CHECKSUM_SHA2:
192 printf("sha2 ");
193 break;
194 case PART_ATTR_CHECKSUM_SHA3:
195 printf("sha3 ");
196 break;
197 }
198
199 if (attr & PART_ATTR_BIG_ENDIAN)
200 printf("BigEndian ");
201
202 if (attr & PART_ATTR_RSA_SIG)
203 printf("RSA ");
204
205 if (attr & PART_ATTR_A53_EXEC_AARCH32)
206 printf("AArch32 ");
207
208 if (attr & PART_ATTR_TARGET_EL_MASK)
209 printf("EL%d ", (attr & PART_ATTR_TARGET_EL_MASK) >> 1);
210
211 if (attr & PART_ATTR_TZ_SECURE)
212 printf("secure ");
213 printf("\n");
214
215 printf(" Checksum : 0x%08x\n", le32_to_cpu(ph->checksum));
216}
217
Pali Rohár0ed41e22023-03-29 21:25:54 +0200218void zynqmpimage_print_header(const void *ptr, struct image_tool_params *params)
Michal Simekfa124662016-04-27 14:03:29 +0200219{
220 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
221 int i;
222
Michal Simeke0942e02018-03-14 11:02:24 +0100223 printf("Image Type : Xilinx ZynqMP Boot Image support\n");
Michal Simekfa124662016-04-27 14:03:29 +0200224 printf("Image Offset : 0x%08x\n", le32_to_cpu(zynqhdr->image_offset));
225 printf("Image Size : %lu bytes (%lu bytes packed)\n",
226 (unsigned long)le32_to_cpu(zynqhdr->image_size),
227 (unsigned long)le32_to_cpu(zynqhdr->image_stored_size));
Michal Simekdde5a882016-10-21 12:58:17 +0200228
229 if (zynqhdr->pfw_image_length)
230 printf("PMUFW Size : %lu bytes (%lu bytes packed)\n",
231 (unsigned long)le32_to_cpu(zynqhdr->pfw_image_length),
232 (unsigned long)le32_to_cpu(
233 zynqhdr->total_pfw_image_length));
234
Michal Simekfa124662016-04-27 14:03:29 +0200235 printf("Image Load : 0x%08x\n", le32_to_cpu(zynqhdr->image_load));
236 printf("Checksum : 0x%08x\n", le32_to_cpu(zynqhdr->checksum));
237
238 for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
239 if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
240 continue;
241
242 printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
243 le32_to_cpu(zynqhdr->interrupt_vectors[i]));
244 }
245
246 for (i = 0; i < HEADER_REGINITS; i++) {
247 if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
248 break;
249
250 if (i == 0)
251 printf("Custom Register Initialization:\n");
252
253 printf(" @ 0x%08x -> 0x%08x\n",
254 le32_to_cpu(zynqhdr->register_init[i].address),
255 le32_to_cpu(zynqhdr->register_init[i].data));
256 }
Michal Simekdde5a882016-10-21 12:58:17 +0200257
Alexander Graf7392bb82018-04-13 14:18:49 +0200258 if (zynqhdr->image_header_table_offset) {
259 struct image_header_table *iht = (void *)ptr +
260 zynqhdr->image_header_table_offset;
261 struct partition_header *ph;
262 uint32_t ph_offset;
263 uint32_t next;
264 int i;
265
266 ph_offset = le32_to_cpu(iht->partition_header_offset) * 4;
267 ph = (void *)ptr + ph_offset;
268 for (i = 0; i < le32_to_cpu(iht->nr_parts); i++) {
269 next = le32_to_cpu(ph->next_partition_offset) * 4;
270
Brandon Maierc234c912024-01-04 18:50:06 +0000271 print_partition(ptr, ph);
Alexander Graf7392bb82018-04-13 14:18:49 +0200272
273 ph = (void *)ptr + next;
274 }
275 }
276
Michal Simekdde5a882016-10-21 12:58:17 +0200277 free(dynamic_header);
Michal Simekfa124662016-04-27 14:03:29 +0200278}
279
280static int zynqmpimage_check_params(struct image_tool_params *params)
281{
282 if (!params)
283 return 0;
284
285 if (params->addr != 0x0) {
286 fprintf(stderr, "Error: Load Address cannot be specified.\n");
287 return -1;
288 }
289
290 /*
291 * If the entry point is specified ensure it is 64 byte aligned.
292 */
293 if (params->eflag && (params->ep % 64 != 0)) {
294 fprintf(stderr,
295 "Error: Entry Point must be aligned to a 64-byte boundary.\n");
296 return -1;
297 }
298
299 return !(params->lflag || params->dflag);
300}
301
302static int zynqmpimage_check_image_types(uint8_t type)
303{
304 if (type == IH_TYPE_ZYNQMPIMAGE)
305 return EXIT_SUCCESS;
306 return EXIT_FAILURE;
307}
308
Michal Simek4902bf82017-12-05 15:42:25 +0100309static uint32_t fsize(FILE *fp)
Michal Simekdde5a882016-10-21 12:58:17 +0200310{
Michal Simek4902bf82017-12-05 15:42:25 +0100311 int size, ret, origin;
Michal Simekdde5a882016-10-21 12:58:17 +0200312
Michal Simek4902bf82017-12-05 15:42:25 +0100313 origin = ftell(fp);
314 if (origin < 0) {
315 fprintf(stderr, "Incorrect file size\n");
316 fclose(fp);
317 exit(2);
318 }
319
320 ret = fseek(fp, 0L, SEEK_END);
321 if (ret) {
322 fprintf(stderr, "Incorrect file SEEK_END\n");
323 fclose(fp);
324 exit(3);
325 }
326
Michal Simekdde5a882016-10-21 12:58:17 +0200327 size = ftell(fp);
Michal Simek4902bf82017-12-05 15:42:25 +0100328 if (size < 0) {
329 fprintf(stderr, "Incorrect file size\n");
330 fclose(fp);
331 exit(4);
332 }
Michal Simekdde5a882016-10-21 12:58:17 +0200333
334 /* going back */
Michal Simek4902bf82017-12-05 15:42:25 +0100335 ret = fseek(fp, origin, SEEK_SET);
336 if (ret) {
337 fprintf(stderr, "Incorrect file SEEK_SET to %d\n", origin);
338 fclose(fp);
339 exit(3);
340 }
Michal Simekdde5a882016-10-21 12:58:17 +0200341
342 return size;
343}
344
345static void zynqmpimage_pmufw(struct zynqmp_header *zynqhdr,
346 const char *filename)
347{
348 uint32_t size;
349
350 /* Setup PMU fw size */
351 zynqhdr->pfw_image_length = fsize(fpmu);
352 zynqhdr->total_pfw_image_length = zynqhdr->pfw_image_length;
353
354 zynqhdr->image_size -= zynqhdr->pfw_image_length;
355 zynqhdr->image_stored_size -= zynqhdr->total_pfw_image_length;
356
357 /* Read the whole PMUFW to the header */
358 size = fread(&zynqhdr->__reserved4[66], 1,
359 zynqhdr->pfw_image_length, fpmu);
360 if (size != zynqhdr->pfw_image_length) {
361 fprintf(stderr, "Cannot read PMUFW file: %s\n", filename);
362 fclose(fpmu);
363 exit(1);
364 }
365
366 fclose(fpmu);
367}
368
Mike Looijmans96e706f2016-09-20 11:37:24 +0200369static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
370 const char *filename)
371{
Michal Simeke0724a32016-10-21 13:16:13 +0200372 FILE *fp;
Mike Looijmans96e706f2016-09-20 11:37:24 +0200373 struct zynqmp_reginit reginit;
374 unsigned int reg_count = 0;
Michal Simekf0203052016-12-06 17:17:01 +0100375 int r, err;
Michal Simeke0724a32016-10-21 13:16:13 +0200376 struct stat path_stat;
Mike Looijmans96e706f2016-09-20 11:37:24 +0200377
Michal Simeke0724a32016-10-21 13:16:13 +0200378 /* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
379 fp = fopen(filename, "r");
Mike Looijmans96e706f2016-09-20 11:37:24 +0200380 if (!fp) {
381 fprintf(stderr, "Cannot open initparams file: %s\n", filename);
382 exit(1);
383 }
Michal Simekf0203052016-12-06 17:17:01 +0100384
385 err = fstat(fileno(fp), &path_stat);
Michal Simeke98faa22016-12-20 09:58:31 +0100386 if (err) {
387 fclose(fp);
Michal Simekf0203052016-12-06 17:17:01 +0100388 return;
Michal Simeke98faa22016-12-20 09:58:31 +0100389 }
Michal Simekf0203052016-12-06 17:17:01 +0100390
Michal Simeke98faa22016-12-20 09:58:31 +0100391 if (!S_ISREG(path_stat.st_mode)) {
392 fclose(fp);
Michal Simekf0203052016-12-06 17:17:01 +0100393 return;
Michal Simeke98faa22016-12-20 09:58:31 +0100394 }
Michal Simekf0203052016-12-06 17:17:01 +0100395
Mike Looijmans96e706f2016-09-20 11:37:24 +0200396 do {
397 r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
398 if (r == 2) {
399 zynqhdr->register_init[reg_count] = reginit;
400 ++reg_count;
401 }
402 r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
403 } while ((r != EOF) && (reg_count < HEADER_REGINITS));
404 fclose(fp);
405}
406
Michal Simekfa124662016-04-27 14:03:29 +0200407static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
408 struct image_tool_params *params)
409{
410 struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
411 zynqmpimage_default_header(zynqhdr);
412
413 /* place image directly after header */
414 zynqhdr->image_offset =
415 cpu_to_le32((uint32_t)sizeof(struct zynqmp_header));
416 zynqhdr->image_size = cpu_to_le32(params->file_size -
417 sizeof(struct zynqmp_header));
418 zynqhdr->image_stored_size = zynqhdr->image_size;
419 zynqhdr->image_load = 0xfffc0000;
420 if (params->eflag)
421 zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
422
Michal Simekdde5a882016-10-21 12:58:17 +0200423 /* PMUFW */
424 if (fpmu)
425 zynqmpimage_pmufw(zynqhdr, params->imagename);
426
Mike Looijmans96e706f2016-09-20 11:37:24 +0200427 /* User can pass in text file with init list */
428 if (strlen(params->imagename2))
429 zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
430
Michal Simekfa124662016-04-27 14:03:29 +0200431 zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
432}
433
Michal Simekdde5a882016-10-21 12:58:17 +0200434static int zynqmpimage_vrec_header(struct image_tool_params *params,
435 struct image_type_params *tparams)
436{
437 struct stat path_stat;
438 char *filename = params->imagename;
439 int err;
440
441 /* Handle static case without PMUFW */
442 tparams->header_size = sizeof(struct zynqmp_header);
443 tparams->hdr = (void *)&zynqmpimage_header;
444
445 /* PMUFW name is passed via params->imagename */
446 if (strlen(filename) == 0)
447 return EXIT_SUCCESS;
448
449 fpmu = fopen(filename, "r");
450 if (!fpmu) {
451 fprintf(stderr, "Cannot open PMUFW file: %s\n", filename);
452 return EXIT_FAILURE;
453 }
454
455 err = fstat(fileno(fpmu), &path_stat);
456 if (err) {
457 fclose(fpmu);
458 fpmu = NULL;
459 return EXIT_FAILURE;
460 }
461
462 if (!S_ISREG(path_stat.st_mode)) {
463 fclose(fpmu);
464 fpmu = NULL;
465 return EXIT_FAILURE;
466 }
467
468 /* Increase header size by PMUFW file size */
469 tparams->header_size += fsize(fpmu);
470
471 /* Allocate buffer with space for PMUFW */
472 dynamic_header = calloc(1, tparams->header_size);
473 tparams->hdr = dynamic_header;
474
475 return EXIT_SUCCESS;
476}
477
Michal Simekfa124662016-04-27 14:03:29 +0200478U_BOOT_IMAGE_TYPE(
479 zynqmpimage,
480 "Xilinx ZynqMP Boot Image support",
481 sizeof(struct zynqmp_header),
482 (void *)&zynqmpimage_header,
483 zynqmpimage_check_params,
484 zynqmpimage_verify_header,
485 zynqmpimage_print_header,
486 zynqmpimage_set_header,
487 NULL,
488 zynqmpimage_check_image_types,
489 NULL,
Michal Simekdde5a882016-10-21 12:58:17 +0200490 zynqmpimage_vrec_header
Michal Simekfa124662016-04-27 14:03:29 +0200491);