]>
Commit | Line | Data |
---|---|---|
89a4d6b1 PW |
1 | /* |
2 | * (C) Copyright 2008 Semihalf | |
3 | * | |
4 | * (C) Copyright 2000-2004 | |
5 | * DENX Software Engineering | |
6 | * Wolfgang Denk, [email protected] | |
7 | * | |
8 | * Updated-by: Prafulla Wadaskar <[email protected]> | |
9 | * FIT image specific code abstracted from mkimage.c | |
10 | * some functions added to address abstraction | |
11 | * | |
12 | * All rights reserved. | |
13 | * | |
1a459660 | 14 | * SPDX-License-Identifier: GPL-2.0+ |
89a4d6b1 PW |
15 | */ |
16 | ||
f86ed6a8 | 17 | #include "imagetool.h" |
6bf4ca07 | 18 | #include "fit_common.h" |
89a4d6b1 PW |
19 | #include "mkimage.h" |
20 | #include <image.h> | |
8e35bb07 SG |
21 | #include <stdarg.h> |
22 | #include <version.h> | |
89a4d6b1 PW |
23 | #include <u-boot/crc.h> |
24 | ||
25 | static image_header_t header; | |
26 | ||
a9468115 SG |
27 | static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, |
28 | const char *tmpfile) | |
29 | { | |
30 | int tfd, destfd = 0; | |
31 | void *dest_blob = NULL; | |
32 | off_t destfd_size = 0; | |
33 | struct stat sbuf; | |
34 | void *ptr; | |
35 | int ret = 0; | |
36 | ||
37 | tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true); | |
38 | if (tfd < 0) | |
39 | return -EIO; | |
40 | ||
41 | if (params->keydest) { | |
42 | struct stat dest_sbuf; | |
43 | ||
44 | destfd = mmap_fdt(params->cmdname, params->keydest, size_inc, | |
45 | &dest_blob, &dest_sbuf, false); | |
46 | if (destfd < 0) { | |
47 | ret = -EIO; | |
48 | goto err_keydest; | |
49 | } | |
50 | destfd_size = dest_sbuf.st_size; | |
51 | } | |
52 | ||
53 | /* for first image creation, add a timestamp at offset 0 i.e., root */ | |
54 | if (params->datafile) | |
55 | ret = fit_set_timestamp(ptr, 0, sbuf.st_mtime); | |
56 | ||
57 | if (!ret) { | |
58 | ret = fit_add_verification_data(params->keydir, dest_blob, ptr, | |
59 | params->comment, | |
60 | params->require_keys); | |
61 | } | |
62 | ||
63 | if (dest_blob) { | |
64 | munmap(dest_blob, destfd_size); | |
65 | close(destfd); | |
66 | } | |
67 | ||
68 | err_keydest: | |
69 | munmap(ptr, sbuf.st_size); | |
70 | close(tfd); | |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
8e35bb07 SG |
75 | /** |
76 | * fit_calc_size() - Calculate the approximate size of the FIT we will generate | |
77 | */ | |
78 | static int fit_calc_size(struct image_tool_params *params) | |
79 | { | |
80 | int size, total_size; | |
81 | ||
82 | size = imagetool_get_filesize(params, params->datafile); | |
83 | if (size < 0) | |
84 | return -1; | |
85 | ||
86 | total_size = size; | |
87 | ||
88 | /* TODO([email protected]): Add in the size of any other files */ | |
89 | ||
90 | /* Add plenty of space for headers, properties, nodes, etc. */ | |
91 | total_size += 4096; | |
92 | ||
93 | return total_size; | |
94 | } | |
95 | ||
96 | static int fdt_property_file(struct image_tool_params *params, | |
97 | void *fdt, const char *name, const char *fname) | |
98 | { | |
99 | struct stat sbuf; | |
100 | void *ptr; | |
101 | int ret; | |
102 | int fd; | |
103 | ||
104 | fd = open(fname, O_RDWR | O_BINARY); | |
105 | if (fd < 0) { | |
106 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
107 | params->cmdname, fname, strerror(errno)); | |
108 | return -1; | |
109 | } | |
110 | ||
111 | if (fstat(fd, &sbuf) < 0) { | |
112 | fprintf(stderr, "%s: Can't stat %s: %s\n", | |
113 | params->cmdname, fname, strerror(errno)); | |
114 | goto err; | |
115 | } | |
116 | ||
117 | ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr); | |
118 | if (ret) | |
119 | return ret; | |
120 | ret = read(fd, ptr, sbuf.st_size); | |
121 | if (ret != sbuf.st_size) { | |
122 | fprintf(stderr, "%s: Can't read %s: %s\n", | |
123 | params->cmdname, fname, strerror(errno)); | |
124 | goto err; | |
125 | } | |
126 | ||
127 | return 0; | |
128 | err: | |
129 | close(fd); | |
130 | return -1; | |
131 | } | |
132 | ||
133 | static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...) | |
134 | { | |
135 | char str[100]; | |
136 | va_list ptr; | |
137 | ||
138 | va_start(ptr, fmt); | |
139 | vsnprintf(str, sizeof(str), fmt, ptr); | |
140 | va_end(ptr); | |
141 | return fdt_property_string(fdt, name, str); | |
142 | } | |
143 | ||
144 | /** | |
145 | * fit_write_images() - Write out a list of images to the FIT | |
146 | * | |
147 | * Include the main image (params->datafile). | |
148 | */ | |
149 | static int fit_write_images(struct image_tool_params *params, char *fdt) | |
150 | { | |
151 | const char *typename; | |
152 | char str[100]; | |
153 | int ret; | |
154 | ||
155 | fdt_begin_node(fdt, "images"); | |
156 | ||
157 | /* First the main image */ | |
158 | typename = genimg_get_type_short_name(params->fit_image_type); | |
159 | snprintf(str, sizeof(str), "%s@1", typename); | |
160 | fdt_begin_node(fdt, str); | |
161 | fdt_property_string(fdt, "description", params->imagename); | |
162 | fdt_property_string(fdt, "type", typename); | |
163 | fdt_property_string(fdt, "arch", genimg_get_arch_name(params->arch)); | |
164 | fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os)); | |
165 | fdt_property_string(fdt, "compression", | |
166 | genimg_get_comp_short_name(params->comp)); | |
167 | fdt_property_u32(fdt, "load", params->addr); | |
168 | fdt_property_u32(fdt, "entry", params->ep); | |
169 | ||
170 | /* | |
171 | * Put data last since it is large. SPL may only load the first part | |
172 | * of the DT, so this way it can access all the above fields. | |
173 | */ | |
174 | ret = fdt_property_file(params, fdt, "data", params->datafile); | |
175 | if (ret) | |
176 | return ret; | |
177 | fdt_end_node(fdt); | |
178 | ||
179 | fdt_end_node(fdt); | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
184 | /** | |
185 | * fit_write_configs() - Write out a list of configurations to the FIT | |
186 | * | |
187 | * Create a configuration with the main image in it. | |
188 | */ | |
189 | static void fit_write_configs(struct image_tool_params *params, char *fdt) | |
190 | { | |
191 | const char *typename; | |
192 | char str[100]; | |
193 | ||
194 | fdt_begin_node(fdt, "configurations"); | |
195 | fdt_property_string(fdt, "default", "conf@1"); | |
196 | ||
197 | fdt_begin_node(fdt, "conf@1"); | |
198 | typename = genimg_get_type_short_name(params->fit_image_type); | |
199 | snprintf(str, sizeof(str), "%s@1", typename); | |
200 | fdt_property_string(fdt, typename, str); | |
201 | fdt_end_node(fdt); | |
202 | ||
203 | fdt_end_node(fdt); | |
204 | } | |
205 | ||
206 | static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) | |
207 | { | |
208 | int ret; | |
209 | ||
210 | ret = fdt_create(fdt, size); | |
211 | if (ret) | |
212 | return ret; | |
213 | fdt_finish_reservemap(fdt); | |
214 | fdt_begin_node(fdt, ""); | |
215 | fdt_property_strf(fdt, "description", | |
216 | "%s image with one or more FDT blobs", | |
217 | genimg_get_type_name(params->fit_image_type)); | |
218 | fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); | |
219 | fdt_property_u32(fdt, "#address-cells", 1); | |
220 | ret = fit_write_images(params, fdt); | |
221 | if (ret) | |
222 | return ret; | |
223 | fit_write_configs(params, fdt); | |
224 | fdt_end_node(fdt); | |
225 | ret = fdt_finish(fdt); | |
226 | if (ret) | |
227 | return ret; | |
228 | ||
229 | return fdt_totalsize(fdt); | |
230 | } | |
231 | ||
232 | static int fit_build(struct image_tool_params *params, const char *fname) | |
233 | { | |
234 | char *buf; | |
235 | int size; | |
236 | int ret; | |
237 | int fd; | |
238 | ||
239 | size = fit_calc_size(params); | |
240 | if (size < 0) | |
241 | return -1; | |
242 | buf = malloc(size); | |
243 | if (!buf) { | |
244 | fprintf(stderr, "%s: Out of memory (%d bytes)\n", | |
245 | params->cmdname, size); | |
246 | return -1; | |
247 | } | |
248 | ret = fit_build_fdt(params, buf, size); | |
249 | if (ret < 0) { | |
250 | fprintf(stderr, "%s: Failed to build FIT image\n", | |
251 | params->cmdname); | |
252 | goto err; | |
253 | } | |
254 | size = ret; | |
255 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); | |
256 | if (fd < 0) { | |
257 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
258 | params->cmdname, fname, strerror(errno)); | |
259 | goto err; | |
260 | } | |
261 | ret = write(fd, buf, size); | |
262 | if (ret != size) { | |
263 | fprintf(stderr, "%s: Can't write %s: %s\n", | |
264 | params->cmdname, fname, strerror(errno)); | |
265 | close(fd); | |
266 | goto err; | |
267 | } | |
268 | close(fd); | |
269 | ||
270 | return 0; | |
271 | err: | |
272 | free(buf); | |
273 | return -1; | |
274 | } | |
275 | ||
89a4d6b1 PW |
276 | /** |
277 | * fit_handle_file - main FIT file processing function | |
278 | * | |
279 | * fit_handle_file() runs dtc to convert .its to .itb, includes | |
280 | * binary data, updates timestamp property and calculates hashes. | |
281 | * | |
282 | * datafile - .its file | |
283 | * imagefile - .itb file | |
284 | * | |
285 | * returns: | |
286 | * only on success, otherwise calls exit (EXIT_FAILURE); | |
287 | */ | |
f86ed6a8 | 288 | static int fit_handle_file(struct image_tool_params *params) |
89a4d6b1 PW |
289 | { |
290 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; | |
291 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; | |
a9468115 SG |
292 | size_t size_inc; |
293 | int ret; | |
89a4d6b1 PW |
294 | |
295 | /* Flattened Image Tree (FIT) format handling */ | |
296 | debug ("FIT format handling\n"); | |
297 | ||
298 | /* call dtc to include binary properties into the tmp file */ | |
299 | if (strlen (params->imagefile) + | |
300 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { | |
301 | fprintf (stderr, "%s: Image file name (%s) too long, " | |
302 | "can't create tmpfile", | |
303 | params->imagefile, params->cmdname); | |
304 | return (EXIT_FAILURE); | |
305 | } | |
306 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); | |
307 | ||
95d77b44 | 308 | /* We either compile the source file, or use the existing FIT image */ |
8e35bb07 SG |
309 | if (params->auto_its) { |
310 | if (fit_build(params, tmpfile)) { | |
311 | fprintf(stderr, "%s: failed to build FIT\n", | |
312 | params->cmdname); | |
313 | return EXIT_FAILURE; | |
314 | } | |
315 | *cmd = '\0'; | |
316 | } else if (params->datafile) { | |
95d77b44 SG |
317 | /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ |
318 | snprintf(cmd, sizeof(cmd), "%s %s %s > %s", | |
319 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); | |
320 | debug("Trying to execute \"%s\"\n", cmd); | |
321 | } else { | |
322 | snprintf(cmd, sizeof(cmd), "cp %s %s", | |
323 | params->imagefile, tmpfile); | |
324 | } | |
8e35bb07 | 325 | if (*cmd && system(cmd) == -1) { |
89a4d6b1 PW |
326 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
327 | params->cmdname, cmd, strerror(errno)); | |
aa6d6db4 | 328 | goto err_system; |
89a4d6b1 PW |
329 | } |
330 | ||
a9468115 SG |
331 | /* |
332 | * Set hashes for images in the blob. Unfortunately we may need more | |
333 | * space in either FDT, so keep trying until we succeed. | |
334 | * | |
335 | * Note: this is pretty inefficient for signing, since we must | |
336 | * calculate the signature every time. It would be better to calculate | |
337 | * all the data and then store it in a separate step. However, this | |
338 | * would be considerably more complex to implement. Generally a few | |
339 | * steps of this loop is enough to sign with several keys. | |
340 | */ | |
341 | for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) { | |
342 | ret = fit_add_file_data(params, size_inc, tmpfile); | |
343 | if (!ret || ret != -ENOSPC) | |
344 | break; | |
e29495d3 SG |
345 | } |
346 | ||
a9468115 | 347 | if (ret) { |
399c744b SG |
348 | fprintf(stderr, "%s Can't add hashes to FIT blob\n", |
349 | params->cmdname); | |
a9468115 | 350 | goto err_system; |
e29495d3 | 351 | } |
89a4d6b1 PW |
352 | |
353 | if (rename (tmpfile, params->imagefile) == -1) { | |
354 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", | |
355 | params->cmdname, tmpfile, params->imagefile, | |
356 | strerror (errno)); | |
357 | unlink (tmpfile); | |
358 | unlink (params->imagefile); | |
a9468115 | 359 | return EXIT_FAILURE; |
89a4d6b1 | 360 | } |
a9468115 | 361 | return EXIT_SUCCESS; |
aa6d6db4 | 362 | |
aa6d6db4 SG |
363 | err_system: |
364 | unlink(tmpfile); | |
365 | return -1; | |
89a4d6b1 PW |
366 | } |
367 | ||
39931f96 GMF |
368 | /** |
369 | * fit_image_extract - extract a FIT component image | |
370 | * @fit: pointer to the FIT format image header | |
371 | * @image_noffset: offset of the component image node | |
372 | * @file_name: name of the file to store the FIT sub-image | |
373 | * | |
374 | * returns: | |
375 | * zero in case of success or a negative value if fail. | |
376 | */ | |
377 | static int fit_image_extract( | |
378 | const void *fit, | |
379 | int image_noffset, | |
380 | const char *file_name) | |
381 | { | |
382 | const void *file_data; | |
383 | size_t file_size = 0; | |
384 | ||
385 | /* get the "data" property of component at offset "image_noffset" */ | |
386 | fit_image_get_data(fit, image_noffset, &file_data, &file_size); | |
387 | ||
388 | /* save the "file_data" into the file specified by "file_name" */ | |
389 | return imagetool_save_subimage(file_name, (ulong) file_data, file_size); | |
390 | } | |
391 | ||
392 | /** | |
393 | * fit_extract_contents - retrieve a sub-image component from the FIT image | |
394 | * @ptr: pointer to the FIT format image header | |
395 | * @params: command line parameters | |
396 | * | |
397 | * returns: | |
398 | * zero in case of success or a negative value if fail. | |
399 | */ | |
400 | static int fit_extract_contents(void *ptr, struct image_tool_params *params) | |
401 | { | |
402 | int images_noffset; | |
403 | int noffset; | |
404 | int ndepth; | |
405 | const void *fit = ptr; | |
406 | int count = 0; | |
407 | const char *p; | |
408 | ||
409 | /* Indent string is defined in header image.h */ | |
410 | p = IMAGE_INDENT_STRING; | |
411 | ||
412 | if (!fit_check_format(fit)) { | |
413 | printf("Bad FIT image format\n"); | |
414 | return -1; | |
415 | } | |
416 | ||
417 | /* Find images parent node offset */ | |
418 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); | |
419 | if (images_noffset < 0) { | |
420 | printf("Can't find images parent node '%s' (%s)\n", | |
421 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); | |
422 | return -1; | |
423 | } | |
424 | ||
425 | /* Avoid any overrun */ | |
426 | count = fit_get_subimage_count(fit, images_noffset); | |
427 | if ((params->pflag < 0) || (count <= params->pflag)) { | |
428 | printf("No such component at '%d'\n", params->pflag); | |
429 | return -1; | |
430 | } | |
431 | ||
432 | /* Process its subnodes, extract the desired component from image */ | |
433 | for (ndepth = 0, count = 0, | |
434 | noffset = fdt_next_node(fit, images_noffset, &ndepth); | |
435 | (noffset >= 0) && (ndepth > 0); | |
436 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
437 | if (ndepth == 1) { | |
438 | /* | |
439 | * Direct child node of the images parent node, | |
440 | * i.e. component image node. | |
441 | */ | |
442 | if (params->pflag == count) { | |
443 | printf("Extracted:\n%s Image %u (%s)\n", p, | |
444 | count, fit_get_name(fit, noffset, NULL)); | |
445 | ||
446 | fit_image_print(fit, noffset, p); | |
447 | ||
448 | return fit_image_extract(fit, noffset, | |
449 | params->outfile); | |
450 | } | |
451 | ||
452 | count++; | |
453 | } | |
454 | } | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
f86ed6a8 | 459 | static int fit_check_params(struct image_tool_params *params) |
89a4d6b1 | 460 | { |
8e35bb07 SG |
461 | if (params->auto_its) |
462 | return 0; | |
89a4d6b1 PW |
463 | return ((params->dflag && (params->fflag || params->lflag)) || |
464 | (params->fflag && (params->dflag || params->lflag)) || | |
465 | (params->lflag && (params->dflag || params->fflag))); | |
466 | } | |
467 | ||
a93648d1 GMF |
468 | U_BOOT_IMAGE_TYPE( |
469 | fitimage, | |
470 | "FIT Image support", | |
471 | sizeof(image_header_t), | |
472 | (void *)&header, | |
473 | fit_check_params, | |
474 | fit_verify_header, | |
475 | fit_print_contents, | |
476 | NULL, | |
39931f96 | 477 | fit_extract_contents, |
a93648d1 GMF |
478 | fit_check_image_types, |
479 | fit_handle_file, | |
480 | NULL /* FIT images use DTB header */ | |
481 | ); |