xref: /openbmc/u-boot/tools/zynqmpimage.c (revision 274bced8)
1 /*
2  * Copyright (C) 2016 Michal Simek <michals@xilinx.com>
3  * Copyright (C) 2015 Nathan Rossi <nathan@nathanrossi.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  *
7  * The following Boot Header format/structures and values are defined in the
8  * following documents:
9  *   * ug1085 ZynqMP TRM (Chapter 9, Table 9-3)
10  *
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"
60 #include <image.h>
61 
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))
66 
67 enum {
68 	ENCRYPTION_EFUSE = 0xa5c3c5a3,
69 	ENCRYPTION_OEFUSE = 0xa5c3c5a7,
70 	ENCRYPTION_BBRAM = 0x3a5c3c5a,
71 	ENCRYPTION_OBBRAM = 0xa35c7ca5,
72 	ENCRYPTION_NONE = 0x0,
73 };
74 
75 struct zynqmp_reginit {
76 	uint32_t address;
77 	uint32_t data;
78 };
79 
80 #define HEADER_INTERRUPT_VECTORS	8
81 #define HEADER_REGINITS			256
82 
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 */
99 };
100 
101 static struct zynqmp_header zynqmpimage_header;
102 
103 static uint32_t zynqmpimage_checksum(struct zynqmp_header *ptr)
104 {
105 	uint32_t checksum = 0;
106 
107 	if (ptr == NULL)
108 		return 0;
109 
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;
121 
122 	return cpu_to_le32(checksum);
123 }
124 
125 static void zynqmpimage_default_header(struct zynqmp_header *ptr)
126 {
127 	int i;
128 
129 	if (ptr == NULL)
130 		return;
131 
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);
136 
137 	/* Setup not-supported/constant/reserved fields */
138 	for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++)
139 		ptr->interrupt_vectors[i] = HEADER_INTERRUPT_DEFAULT;
140 
141 	for (i = 0; i < HEADER_REGINITS; i++) {
142 		ptr->register_init[i].address = HEADER_REGINIT_NULL;
143 		ptr->register_init[i].data = 0;
144 	}
145 
146 	/*
147 	 * Certain reserved fields are required to be set to 0, ensure they are
148 	 * set as such.
149 	 */
150 	ptr->pfw_image_length = 0x0;
151 	ptr->total_pfw_image_length = 0x0;
152 }
153 
154 /* mkimage glue functions */
155 static int zynqmpimage_verify_header(unsigned char *ptr, int image_size,
156 		struct image_tool_params *params)
157 {
158 	struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
159 
160 	if (image_size < sizeof(struct zynqmp_header))
161 		return -1;
162 
163 	if (zynqhdr->width_detection != HEADER_WIDTHDETECTION)
164 		return -1;
165 	if (zynqhdr->image_identifier != HEADER_IMAGEIDENTIFIER)
166 		return -1;
167 
168 	if (zynqmpimage_checksum(zynqhdr) != zynqhdr->checksum)
169 		return -1;
170 
171 	return 0;
172 }
173 
174 static void zynqmpimage_print_header(const void *ptr)
175 {
176 	struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
177 	int i;
178 
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));
186 
187 	for (i = 0; i < HEADER_INTERRUPT_VECTORS; i++) {
188 		if (zynqhdr->interrupt_vectors[i] == HEADER_INTERRUPT_DEFAULT)
189 			continue;
190 
191 		printf("Modified Interrupt Vector Address [%d]: 0x%08x\n", i,
192 		       le32_to_cpu(zynqhdr->interrupt_vectors[i]));
193 	}
194 
195 	for (i = 0; i < HEADER_REGINITS; i++) {
196 		if (zynqhdr->register_init[i].address == HEADER_REGINIT_NULL)
197 			break;
198 
199 		if (i == 0)
200 			printf("Custom Register Initialization:\n");
201 
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));
205 	}
206 }
207 
208 static int zynqmpimage_check_params(struct image_tool_params *params)
209 {
210 	if (!params)
211 		return 0;
212 
213 	if (params->addr != 0x0) {
214 		fprintf(stderr, "Error: Load Address cannot be specified.\n");
215 		return -1;
216 	}
217 
218 	/*
219 	 * If the entry point is specified ensure it is 64 byte aligned.
220 	 */
221 	if (params->eflag && (params->ep % 64 != 0)) {
222 		fprintf(stderr,
223 			"Error: Entry Point must be aligned to a 64-byte boundary.\n");
224 		return -1;
225 	}
226 
227 	return !(params->lflag || params->dflag);
228 }
229 
230 static int zynqmpimage_check_image_types(uint8_t type)
231 {
232 	if (type == IH_TYPE_ZYNQMPIMAGE)
233 		return EXIT_SUCCESS;
234 	return EXIT_FAILURE;
235 }
236 
237 static void zynqmpimage_parse_initparams(struct zynqmp_header *zynqhdr,
238 	const char *filename)
239 {
240 	FILE *fp;
241 	struct zynqmp_reginit reginit;
242 	unsigned int reg_count = 0;
243 	int r, err;
244 	struct stat path_stat;
245 
246 	/* Expect a table of register-value pairs, e.g. "0x12345678 0x4321" */
247 	fp = fopen(filename, "r");
248 	if (!fp) {
249 		fprintf(stderr, "Cannot open initparams file: %s\n", filename);
250 		exit(1);
251 	}
252 
253 	err = fstat(fileno(fp), &path_stat);
254 	if (err) {
255 		fclose(fp);
256 		return;
257 	}
258 
259 	if (!S_ISREG(path_stat.st_mode)) {
260 		fclose(fp);
261 		return;
262 	}
263 
264 	do {
265 		r = fscanf(fp, "%x %x", &reginit.address, &reginit.data);
266 		if (r == 2) {
267 			zynqhdr->register_init[reg_count] = reginit;
268 			++reg_count;
269 		}
270 		r = fscanf(fp, "%*[^\n]\n"); /* Skip to next line */
271 	} while ((r != EOF) && (reg_count < HEADER_REGINITS));
272 	fclose(fp);
273 }
274 
275 static void zynqmpimage_set_header(void *ptr, struct stat *sbuf, int ifd,
276 		struct image_tool_params *params)
277 {
278 	struct zynqmp_header *zynqhdr = (struct zynqmp_header *)ptr;
279 	zynqmpimage_default_header(zynqhdr);
280 
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;
288 	if (params->eflag)
289 		zynqhdr->image_load = cpu_to_le32((uint32_t)params->ep);
290 
291 	/* User can pass in text file with init list */
292 	if (strlen(params->imagename2))
293 		zynqmpimage_parse_initparams(zynqhdr, params->imagename2);
294 
295 	zynqhdr->checksum = zynqmpimage_checksum(zynqhdr);
296 }
297 
298 U_BOOT_IMAGE_TYPE(
299 	zynqmpimage,
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,
307 	NULL,
308 	zynqmpimage_check_image_types,
309 	NULL,
310 	NULL
311 );
312