]>
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 */ | |
5847084f VC |
54 | if (params->datafile) { |
55 | time_t time = imagetool_get_source_date(params, sbuf.st_mtime); | |
56 | ret = fit_set_timestamp(ptr, 0, time); | |
57 | } | |
a9468115 SG |
58 | |
59 | if (!ret) { | |
60 | ret = fit_add_verification_data(params->keydir, dest_blob, ptr, | |
61 | params->comment, | |
f1ca1fde GM |
62 | params->require_keys, |
63 | params->engine_id); | |
a9468115 SG |
64 | } |
65 | ||
66 | if (dest_blob) { | |
67 | munmap(dest_blob, destfd_size); | |
68 | close(destfd); | |
69 | } | |
70 | ||
71 | err_keydest: | |
72 | munmap(ptr, sbuf.st_size); | |
73 | close(tfd); | |
74 | ||
75 | return ret; | |
76 | } | |
77 | ||
8e35bb07 SG |
78 | /** |
79 | * fit_calc_size() - Calculate the approximate size of the FIT we will generate | |
80 | */ | |
81 | static int fit_calc_size(struct image_tool_params *params) | |
82 | { | |
fb4cce0f | 83 | struct content_info *cont; |
8e35bb07 SG |
84 | int size, total_size; |
85 | ||
86 | size = imagetool_get_filesize(params, params->datafile); | |
87 | if (size < 0) | |
88 | return -1; | |
8e35bb07 | 89 | total_size = size; |
0f7c6cdc TV |
90 | |
91 | if (params->fit_ramdisk) { | |
92 | size = imagetool_get_filesize(params, params->fit_ramdisk); | |
93 | if (size < 0) | |
94 | return -1; | |
95 | total_size += size; | |
96 | } | |
97 | ||
fb4cce0f SG |
98 | for (cont = params->content_head; cont; cont = cont->next) { |
99 | size = imagetool_get_filesize(params, cont->fname); | |
100 | if (size < 0) | |
101 | return -1; | |
8e35bb07 | 102 | |
fb4cce0f SG |
103 | /* Add space for properties */ |
104 | total_size += size + 300; | |
105 | } | |
8e35bb07 SG |
106 | |
107 | /* Add plenty of space for headers, properties, nodes, etc. */ | |
108 | total_size += 4096; | |
109 | ||
110 | return total_size; | |
111 | } | |
112 | ||
113 | static int fdt_property_file(struct image_tool_params *params, | |
114 | void *fdt, const char *name, const char *fname) | |
115 | { | |
116 | struct stat sbuf; | |
117 | void *ptr; | |
118 | int ret; | |
119 | int fd; | |
120 | ||
121 | fd = open(fname, O_RDWR | O_BINARY); | |
122 | if (fd < 0) { | |
123 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
124 | params->cmdname, fname, strerror(errno)); | |
125 | return -1; | |
126 | } | |
127 | ||
128 | if (fstat(fd, &sbuf) < 0) { | |
129 | fprintf(stderr, "%s: Can't stat %s: %s\n", | |
130 | params->cmdname, fname, strerror(errno)); | |
131 | goto err; | |
132 | } | |
133 | ||
134 | ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr); | |
135 | if (ret) | |
3bd3a54a | 136 | goto err; |
8e35bb07 SG |
137 | ret = read(fd, ptr, sbuf.st_size); |
138 | if (ret != sbuf.st_size) { | |
139 | fprintf(stderr, "%s: Can't read %s: %s\n", | |
140 | params->cmdname, fname, strerror(errno)); | |
141 | goto err; | |
142 | } | |
3bd3a54a | 143 | close(fd); |
8e35bb07 SG |
144 | |
145 | return 0; | |
146 | err: | |
147 | close(fd); | |
148 | return -1; | |
149 | } | |
150 | ||
151 | static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...) | |
152 | { | |
153 | char str[100]; | |
154 | va_list ptr; | |
155 | ||
156 | va_start(ptr, fmt); | |
157 | vsnprintf(str, sizeof(str), fmt, ptr); | |
158 | va_end(ptr); | |
159 | return fdt_property_string(fdt, name, str); | |
160 | } | |
161 | ||
fb4cce0f SG |
162 | static void get_basename(char *str, int size, const char *fname) |
163 | { | |
164 | const char *p, *start, *end; | |
165 | int len; | |
166 | ||
167 | /* | |
168 | * Use the base name as the 'name' field. So for example: | |
169 | * | |
170 | * "arch/arm/dts/sun7i-a20-bananapro.dtb" | |
171 | * becomes "sun7i-a20-bananapro" | |
172 | */ | |
173 | p = strrchr(fname, '/'); | |
174 | start = p ? p + 1 : fname; | |
175 | p = strrchr(fname, '.'); | |
176 | end = p ? p : fname + strlen(fname); | |
177 | len = end - start; | |
178 | if (len >= size) | |
179 | len = size - 1; | |
180 | memcpy(str, start, len); | |
181 | str[len] = '\0'; | |
182 | } | |
183 | ||
8e35bb07 SG |
184 | /** |
185 | * fit_write_images() - Write out a list of images to the FIT | |
186 | * | |
fb4cce0f SG |
187 | * We always include the main image (params->datafile). If there are device |
188 | * tree files, we include an fdt@ node for each of those too. | |
8e35bb07 SG |
189 | */ |
190 | static int fit_write_images(struct image_tool_params *params, char *fdt) | |
191 | { | |
fb4cce0f | 192 | struct content_info *cont; |
8e35bb07 SG |
193 | const char *typename; |
194 | char str[100]; | |
fb4cce0f | 195 | int upto; |
8e35bb07 SG |
196 | int ret; |
197 | ||
198 | fdt_begin_node(fdt, "images"); | |
199 | ||
200 | /* First the main image */ | |
201 | typename = genimg_get_type_short_name(params->fit_image_type); | |
202 | snprintf(str, sizeof(str), "%s@1", typename); | |
203 | fdt_begin_node(fdt, str); | |
204 | fdt_property_string(fdt, "description", params->imagename); | |
205 | fdt_property_string(fdt, "type", typename); | |
3a45f38d SG |
206 | fdt_property_string(fdt, "arch", |
207 | genimg_get_arch_short_name(params->arch)); | |
8e35bb07 SG |
208 | fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os)); |
209 | fdt_property_string(fdt, "compression", | |
210 | genimg_get_comp_short_name(params->comp)); | |
211 | fdt_property_u32(fdt, "load", params->addr); | |
212 | fdt_property_u32(fdt, "entry", params->ep); | |
213 | ||
214 | /* | |
215 | * Put data last since it is large. SPL may only load the first part | |
216 | * of the DT, so this way it can access all the above fields. | |
217 | */ | |
218 | ret = fdt_property_file(params, fdt, "data", params->datafile); | |
219 | if (ret) | |
220 | return ret; | |
221 | fdt_end_node(fdt); | |
222 | ||
fb4cce0f SG |
223 | /* Now the device tree files if available */ |
224 | upto = 0; | |
225 | for (cont = params->content_head; cont; cont = cont->next) { | |
226 | if (cont->type != IH_TYPE_FLATDT) | |
227 | continue; | |
228 | snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto); | |
229 | fdt_begin_node(fdt, str); | |
230 | ||
231 | get_basename(str, sizeof(str), cont->fname); | |
232 | fdt_property_string(fdt, "description", str); | |
233 | ret = fdt_property_file(params, fdt, "data", cont->fname); | |
234 | if (ret) | |
235 | return ret; | |
236 | fdt_property_string(fdt, "type", typename); | |
237 | fdt_property_string(fdt, "arch", | |
238 | genimg_get_arch_short_name(params->arch)); | |
239 | fdt_property_string(fdt, "compression", | |
240 | genimg_get_comp_short_name(IH_COMP_NONE)); | |
241 | fdt_end_node(fdt); | |
242 | } | |
243 | ||
0f7c6cdc TV |
244 | /* And a ramdisk file if available */ |
245 | if (params->fit_ramdisk) { | |
246 | fdt_begin_node(fdt, FIT_RAMDISK_PROP "@1"); | |
247 | ||
248 | fdt_property_string(fdt, "type", FIT_RAMDISK_PROP); | |
249 | fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os)); | |
250 | ||
251 | ret = fdt_property_file(params, fdt, "data", params->fit_ramdisk); | |
252 | if (ret) | |
253 | return ret; | |
254 | ||
255 | fdt_end_node(fdt); | |
256 | } | |
257 | ||
8e35bb07 SG |
258 | fdt_end_node(fdt); |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | /** | |
264 | * fit_write_configs() - Write out a list of configurations to the FIT | |
265 | * | |
fb4cce0f SG |
266 | * If there are device tree files, we include a configuration for each, which |
267 | * selects the main image (params->datafile) and its corresponding device | |
268 | * tree file. | |
269 | * | |
270 | * Otherwise we just create a configuration with the main image in it. | |
8e35bb07 SG |
271 | */ |
272 | static void fit_write_configs(struct image_tool_params *params, char *fdt) | |
273 | { | |
fb4cce0f | 274 | struct content_info *cont; |
8e35bb07 SG |
275 | const char *typename; |
276 | char str[100]; | |
fb4cce0f | 277 | int upto; |
8e35bb07 SG |
278 | |
279 | fdt_begin_node(fdt, "configurations"); | |
280 | fdt_property_string(fdt, "default", "conf@1"); | |
281 | ||
fb4cce0f SG |
282 | upto = 0; |
283 | for (cont = params->content_head; cont; cont = cont->next) { | |
284 | if (cont->type != IH_TYPE_FLATDT) | |
285 | continue; | |
286 | typename = genimg_get_type_short_name(cont->type); | |
287 | snprintf(str, sizeof(str), "conf@%d", ++upto); | |
288 | fdt_begin_node(fdt, str); | |
289 | ||
290 | get_basename(str, sizeof(str), cont->fname); | |
291 | fdt_property_string(fdt, "description", str); | |
292 | ||
293 | typename = genimg_get_type_short_name(params->fit_image_type); | |
294 | snprintf(str, sizeof(str), "%s@1", typename); | |
295 | fdt_property_string(fdt, typename, str); | |
296 | ||
0f7c6cdc TV |
297 | if (params->fit_ramdisk) |
298 | fdt_property_string(fdt, FIT_RAMDISK_PROP, | |
299 | FIT_RAMDISK_PROP "@1"); | |
300 | ||
fb4cce0f SG |
301 | snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto); |
302 | fdt_property_string(fdt, FIT_FDT_PROP, str); | |
303 | fdt_end_node(fdt); | |
304 | } | |
0f7c6cdc | 305 | |
fb4cce0f SG |
306 | if (!upto) { |
307 | fdt_begin_node(fdt, "conf@1"); | |
308 | typename = genimg_get_type_short_name(params->fit_image_type); | |
309 | snprintf(str, sizeof(str), "%s@1", typename); | |
310 | fdt_property_string(fdt, typename, str); | |
0f7c6cdc TV |
311 | |
312 | if (params->fit_ramdisk) | |
313 | fdt_property_string(fdt, FIT_RAMDISK_PROP, | |
314 | FIT_RAMDISK_PROP "@1"); | |
315 | ||
fb4cce0f SG |
316 | fdt_end_node(fdt); |
317 | } | |
8e35bb07 SG |
318 | |
319 | fdt_end_node(fdt); | |
320 | } | |
321 | ||
322 | static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) | |
323 | { | |
324 | int ret; | |
325 | ||
326 | ret = fdt_create(fdt, size); | |
327 | if (ret) | |
328 | return ret; | |
329 | fdt_finish_reservemap(fdt); | |
330 | fdt_begin_node(fdt, ""); | |
331 | fdt_property_strf(fdt, "description", | |
332 | "%s image with one or more FDT blobs", | |
333 | genimg_get_type_name(params->fit_image_type)); | |
334 | fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); | |
335 | fdt_property_u32(fdt, "#address-cells", 1); | |
336 | ret = fit_write_images(params, fdt); | |
337 | if (ret) | |
338 | return ret; | |
339 | fit_write_configs(params, fdt); | |
340 | fdt_end_node(fdt); | |
341 | ret = fdt_finish(fdt); | |
342 | if (ret) | |
343 | return ret; | |
344 | ||
345 | return fdt_totalsize(fdt); | |
346 | } | |
347 | ||
348 | static int fit_build(struct image_tool_params *params, const char *fname) | |
349 | { | |
350 | char *buf; | |
351 | int size; | |
352 | int ret; | |
353 | int fd; | |
354 | ||
355 | size = fit_calc_size(params); | |
356 | if (size < 0) | |
357 | return -1; | |
358 | buf = malloc(size); | |
359 | if (!buf) { | |
360 | fprintf(stderr, "%s: Out of memory (%d bytes)\n", | |
361 | params->cmdname, size); | |
362 | return -1; | |
363 | } | |
364 | ret = fit_build_fdt(params, buf, size); | |
365 | if (ret < 0) { | |
366 | fprintf(stderr, "%s: Failed to build FIT image\n", | |
367 | params->cmdname); | |
7b0bbd88 | 368 | goto err_buf; |
8e35bb07 SG |
369 | } |
370 | size = ret; | |
371 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); | |
372 | if (fd < 0) { | |
373 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
374 | params->cmdname, fname, strerror(errno)); | |
375 | goto err; | |
376 | } | |
377 | ret = write(fd, buf, size); | |
378 | if (ret != size) { | |
379 | fprintf(stderr, "%s: Can't write %s: %s\n", | |
380 | params->cmdname, fname, strerror(errno)); | |
8e35bb07 SG |
381 | goto err; |
382 | } | |
383 | close(fd); | |
7b0bbd88 | 384 | free(buf); |
8e35bb07 SG |
385 | |
386 | return 0; | |
387 | err: | |
7b0bbd88 SG |
388 | close(fd); |
389 | err_buf: | |
8e35bb07 SG |
390 | free(buf); |
391 | return -1; | |
392 | } | |
393 | ||
722ebc8f SG |
394 | /** |
395 | * fit_extract_data() - Move all data outside the FIT | |
396 | * | |
397 | * This takes a normal FIT file and removes all the 'data' properties from it. | |
398 | * The data is placed in an area after the FIT so that it can be accessed | |
399 | * using an offset into that area. The 'data' properties turn into | |
400 | * 'data-offset' properties. | |
401 | * | |
402 | * This function cannot cope with FITs with 'data-offset' properties. All | |
403 | * data must be in 'data' properties on entry. | |
404 | */ | |
405 | static int fit_extract_data(struct image_tool_params *params, const char *fname) | |
406 | { | |
407 | void *buf; | |
408 | int buf_ptr; | |
409 | int fit_size, new_size; | |
410 | int fd; | |
411 | struct stat sbuf; | |
412 | void *fdt; | |
413 | int ret; | |
414 | int images; | |
415 | int node; | |
416 | ||
417 | fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false); | |
418 | if (fd < 0) | |
419 | return -EIO; | |
420 | fit_size = fdt_totalsize(fdt); | |
421 | ||
422 | /* Allocate space to hold the image data we will extract */ | |
423 | buf = malloc(fit_size); | |
424 | if (!buf) { | |
425 | ret = -ENOMEM; | |
b97d71e2 | 426 | goto err_munmap; |
722ebc8f SG |
427 | } |
428 | buf_ptr = 0; | |
429 | ||
430 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); | |
431 | if (images < 0) { | |
432 | debug("%s: Cannot find /images node: %d\n", __func__, images); | |
433 | ret = -EINVAL; | |
b97d71e2 | 434 | goto err_munmap; |
722ebc8f SG |
435 | } |
436 | ||
437 | for (node = fdt_first_subnode(fdt, images); | |
438 | node >= 0; | |
439 | node = fdt_next_subnode(fdt, node)) { | |
440 | const char *data; | |
441 | int len; | |
442 | ||
443 | data = fdt_getprop(fdt, node, "data", &len); | |
444 | if (!data) | |
445 | continue; | |
446 | memcpy(buf + buf_ptr, data, len); | |
447 | debug("Extracting data size %x\n", len); | |
448 | ||
449 | ret = fdt_delprop(fdt, node, "data"); | |
450 | if (ret) { | |
451 | ret = -EPERM; | |
b97d71e2 | 452 | goto err_munmap; |
722ebc8f | 453 | } |
f8f9107d TR |
454 | if (params->external_offset > 0) { |
455 | /* An external offset positions the data absolutely. */ | |
456 | fdt_setprop_u32(fdt, node, "data-position", | |
457 | params->external_offset + buf_ptr); | |
458 | } else { | |
459 | fdt_setprop_u32(fdt, node, "data-offset", buf_ptr); | |
460 | } | |
722ebc8f SG |
461 | fdt_setprop_u32(fdt, node, "data-size", len); |
462 | ||
463 | buf_ptr += (len + 3) & ~3; | |
464 | } | |
465 | ||
466 | /* Pack the FDT and place the data after it */ | |
467 | fdt_pack(fdt); | |
468 | ||
469 | debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); | |
470 | debug("External data size %x\n", buf_ptr); | |
471 | new_size = fdt_totalsize(fdt); | |
472 | new_size = (new_size + 3) & ~3; | |
473 | munmap(fdt, sbuf.st_size); | |
474 | ||
475 | if (ftruncate(fd, new_size)) { | |
476 | debug("%s: Failed to truncate file: %s\n", __func__, | |
477 | strerror(errno)); | |
478 | ret = -EIO; | |
479 | goto err; | |
480 | } | |
f8f9107d TR |
481 | |
482 | /* Check if an offset for the external data was set. */ | |
483 | if (params->external_offset > 0) { | |
484 | if (params->external_offset < new_size) { | |
485 | debug("External offset %x overlaps FIT length %x", | |
486 | params->external_offset, new_size); | |
487 | ret = -EINVAL; | |
488 | goto err; | |
489 | } | |
490 | new_size = params->external_offset; | |
491 | } | |
722ebc8f SG |
492 | if (lseek(fd, new_size, SEEK_SET) < 0) { |
493 | debug("%s: Failed to seek to end of file: %s\n", __func__, | |
494 | strerror(errno)); | |
495 | ret = -EIO; | |
496 | goto err; | |
497 | } | |
498 | if (write(fd, buf, buf_ptr) != buf_ptr) { | |
499 | debug("%s: Failed to write external data to file %s\n", | |
500 | __func__, strerror(errno)); | |
501 | ret = -EIO; | |
502 | goto err; | |
503 | } | |
b97d71e2 SG |
504 | close(fd); |
505 | return 0; | |
722ebc8f | 506 | |
b97d71e2 SG |
507 | err_munmap: |
508 | munmap(fdt, sbuf.st_size); | |
722ebc8f | 509 | err: |
21c2975a SG |
510 | if (buf) |
511 | free(buf); | |
722ebc8f SG |
512 | close(fd); |
513 | return ret; | |
514 | } | |
515 | ||
529fd188 SG |
516 | static int fit_import_data(struct image_tool_params *params, const char *fname) |
517 | { | |
518 | void *fdt, *old_fdt; | |
519 | int fit_size, new_size, size, data_base; | |
520 | int fd; | |
521 | struct stat sbuf; | |
522 | int ret; | |
523 | int images; | |
524 | int node; | |
525 | ||
526 | fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false); | |
527 | if (fd < 0) | |
528 | return -EIO; | |
529 | fit_size = fdt_totalsize(old_fdt); | |
530 | data_base = (fit_size + 3) & ~3; | |
531 | ||
532 | /* Allocate space to hold the new FIT */ | |
533 | size = sbuf.st_size + 16384; | |
534 | fdt = malloc(size); | |
535 | if (!fdt) { | |
536 | fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n", | |
537 | __func__, size); | |
538 | ret = -ENOMEM; | |
539 | goto err; | |
540 | } | |
541 | ret = fdt_open_into(old_fdt, fdt, size); | |
542 | if (ret) { | |
543 | debug("%s: Failed to expand FIT: %s\n", __func__, | |
544 | fdt_strerror(errno)); | |
545 | ret = -EINVAL; | |
546 | goto err; | |
547 | } | |
548 | ||
549 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); | |
550 | if (images < 0) { | |
551 | debug("%s: Cannot find /images node: %d\n", __func__, images); | |
552 | ret = -EINVAL; | |
553 | goto err; | |
554 | } | |
555 | ||
556 | for (node = fdt_first_subnode(fdt, images); | |
557 | node >= 0; | |
558 | node = fdt_next_subnode(fdt, node)) { | |
559 | int buf_ptr; | |
560 | int len; | |
561 | ||
562 | buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1); | |
563 | len = fdtdec_get_int(fdt, node, "data-size", -1); | |
564 | if (buf_ptr == -1 || len == -1) | |
565 | continue; | |
566 | debug("Importing data size %x\n", len); | |
567 | ||
568 | ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr, | |
569 | len); | |
570 | if (ret) { | |
571 | debug("%s: Failed to write property: %s\n", __func__, | |
572 | fdt_strerror(ret)); | |
573 | ret = -EINVAL; | |
574 | goto err; | |
575 | } | |
576 | } | |
577 | ||
9cff2d1e | 578 | munmap(old_fdt, sbuf.st_size); |
529fd188 SG |
579 | close(fd); |
580 | ||
581 | /* Pack the FDT and place the data after it */ | |
582 | fdt_pack(fdt); | |
583 | ||
584 | new_size = fdt_totalsize(fdt); | |
585 | debug("Size expanded from %x to %x\n", fit_size, new_size); | |
586 | ||
587 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); | |
588 | if (fd < 0) { | |
589 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
590 | params->cmdname, fname, strerror(errno)); | |
5b15a259 SG |
591 | free(fdt); |
592 | return -EIO; | |
529fd188 SG |
593 | } |
594 | if (write(fd, fdt, new_size) != new_size) { | |
595 | debug("%s: Failed to write external data to file %s\n", | |
596 | __func__, strerror(errno)); | |
597 | ret = -EIO; | |
598 | goto err; | |
599 | } | |
529fd188 SG |
600 | |
601 | ret = 0; | |
602 | ||
603 | err: | |
6e0ffce6 | 604 | free(fdt); |
529fd188 SG |
605 | close(fd); |
606 | return ret; | |
607 | } | |
608 | ||
89a4d6b1 PW |
609 | /** |
610 | * fit_handle_file - main FIT file processing function | |
611 | * | |
612 | * fit_handle_file() runs dtc to convert .its to .itb, includes | |
613 | * binary data, updates timestamp property and calculates hashes. | |
614 | * | |
615 | * datafile - .its file | |
616 | * imagefile - .itb file | |
617 | * | |
618 | * returns: | |
619 | * only on success, otherwise calls exit (EXIT_FAILURE); | |
620 | */ | |
f86ed6a8 | 621 | static int fit_handle_file(struct image_tool_params *params) |
89a4d6b1 PW |
622 | { |
623 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; | |
624 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; | |
a9468115 SG |
625 | size_t size_inc; |
626 | int ret; | |
89a4d6b1 PW |
627 | |
628 | /* Flattened Image Tree (FIT) format handling */ | |
629 | debug ("FIT format handling\n"); | |
630 | ||
631 | /* call dtc to include binary properties into the tmp file */ | |
632 | if (strlen (params->imagefile) + | |
633 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { | |
634 | fprintf (stderr, "%s: Image file name (%s) too long, " | |
635 | "can't create tmpfile", | |
636 | params->imagefile, params->cmdname); | |
637 | return (EXIT_FAILURE); | |
638 | } | |
639 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); | |
640 | ||
95d77b44 | 641 | /* We either compile the source file, or use the existing FIT image */ |
8e35bb07 SG |
642 | if (params->auto_its) { |
643 | if (fit_build(params, tmpfile)) { | |
644 | fprintf(stderr, "%s: failed to build FIT\n", | |
645 | params->cmdname); | |
646 | return EXIT_FAILURE; | |
647 | } | |
648 | *cmd = '\0'; | |
649 | } else if (params->datafile) { | |
95d77b44 SG |
650 | /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ |
651 | snprintf(cmd, sizeof(cmd), "%s %s %s > %s", | |
652 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); | |
653 | debug("Trying to execute \"%s\"\n", cmd); | |
654 | } else { | |
655 | snprintf(cmd, sizeof(cmd), "cp %s %s", | |
656 | params->imagefile, tmpfile); | |
657 | } | |
8e35bb07 | 658 | if (*cmd && system(cmd) == -1) { |
89a4d6b1 PW |
659 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
660 | params->cmdname, cmd, strerror(errno)); | |
aa6d6db4 | 661 | goto err_system; |
89a4d6b1 PW |
662 | } |
663 | ||
529fd188 SG |
664 | /* Move the data so it is internal to the FIT, if needed */ |
665 | ret = fit_import_data(params, tmpfile); | |
666 | if (ret) | |
667 | goto err_system; | |
668 | ||
a9468115 SG |
669 | /* |
670 | * Set hashes for images in the blob. Unfortunately we may need more | |
671 | * space in either FDT, so keep trying until we succeed. | |
672 | * | |
673 | * Note: this is pretty inefficient for signing, since we must | |
674 | * calculate the signature every time. It would be better to calculate | |
675 | * all the data and then store it in a separate step. However, this | |
676 | * would be considerably more complex to implement. Generally a few | |
677 | * steps of this loop is enough to sign with several keys. | |
678 | */ | |
679 | for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) { | |
680 | ret = fit_add_file_data(params, size_inc, tmpfile); | |
681 | if (!ret || ret != -ENOSPC) | |
682 | break; | |
e29495d3 SG |
683 | } |
684 | ||
a9468115 | 685 | if (ret) { |
655cc696 SG |
686 | fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n", |
687 | params->cmdname, ret); | |
a9468115 | 688 | goto err_system; |
e29495d3 | 689 | } |
89a4d6b1 | 690 | |
722ebc8f SG |
691 | /* Move the data so it is external to the FIT, if requested */ |
692 | if (params->external_data) { | |
693 | ret = fit_extract_data(params, tmpfile); | |
694 | if (ret) | |
695 | goto err_system; | |
696 | } | |
697 | ||
89a4d6b1 PW |
698 | if (rename (tmpfile, params->imagefile) == -1) { |
699 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", | |
700 | params->cmdname, tmpfile, params->imagefile, | |
701 | strerror (errno)); | |
702 | unlink (tmpfile); | |
703 | unlink (params->imagefile); | |
a9468115 | 704 | return EXIT_FAILURE; |
89a4d6b1 | 705 | } |
a9468115 | 706 | return EXIT_SUCCESS; |
aa6d6db4 | 707 | |
aa6d6db4 SG |
708 | err_system: |
709 | unlink(tmpfile); | |
710 | return -1; | |
89a4d6b1 PW |
711 | } |
712 | ||
39931f96 GMF |
713 | /** |
714 | * fit_image_extract - extract a FIT component image | |
715 | * @fit: pointer to the FIT format image header | |
716 | * @image_noffset: offset of the component image node | |
717 | * @file_name: name of the file to store the FIT sub-image | |
718 | * | |
719 | * returns: | |
720 | * zero in case of success or a negative value if fail. | |
721 | */ | |
722 | static int fit_image_extract( | |
723 | const void *fit, | |
724 | int image_noffset, | |
725 | const char *file_name) | |
726 | { | |
727 | const void *file_data; | |
728 | size_t file_size = 0; | |
729 | ||
730 | /* get the "data" property of component at offset "image_noffset" */ | |
731 | fit_image_get_data(fit, image_noffset, &file_data, &file_size); | |
732 | ||
733 | /* save the "file_data" into the file specified by "file_name" */ | |
734 | return imagetool_save_subimage(file_name, (ulong) file_data, file_size); | |
735 | } | |
736 | ||
737 | /** | |
738 | * fit_extract_contents - retrieve a sub-image component from the FIT image | |
739 | * @ptr: pointer to the FIT format image header | |
740 | * @params: command line parameters | |
741 | * | |
742 | * returns: | |
743 | * zero in case of success or a negative value if fail. | |
744 | */ | |
745 | static int fit_extract_contents(void *ptr, struct image_tool_params *params) | |
746 | { | |
747 | int images_noffset; | |
748 | int noffset; | |
749 | int ndepth; | |
750 | const void *fit = ptr; | |
751 | int count = 0; | |
752 | const char *p; | |
753 | ||
754 | /* Indent string is defined in header image.h */ | |
755 | p = IMAGE_INDENT_STRING; | |
756 | ||
757 | if (!fit_check_format(fit)) { | |
758 | printf("Bad FIT image format\n"); | |
759 | return -1; | |
760 | } | |
761 | ||
762 | /* Find images parent node offset */ | |
763 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); | |
764 | if (images_noffset < 0) { | |
765 | printf("Can't find images parent node '%s' (%s)\n", | |
766 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); | |
767 | return -1; | |
768 | } | |
769 | ||
770 | /* Avoid any overrun */ | |
771 | count = fit_get_subimage_count(fit, images_noffset); | |
772 | if ((params->pflag < 0) || (count <= params->pflag)) { | |
773 | printf("No such component at '%d'\n", params->pflag); | |
774 | return -1; | |
775 | } | |
776 | ||
777 | /* Process its subnodes, extract the desired component from image */ | |
778 | for (ndepth = 0, count = 0, | |
779 | noffset = fdt_next_node(fit, images_noffset, &ndepth); | |
780 | (noffset >= 0) && (ndepth > 0); | |
781 | noffset = fdt_next_node(fit, noffset, &ndepth)) { | |
782 | if (ndepth == 1) { | |
783 | /* | |
784 | * Direct child node of the images parent node, | |
785 | * i.e. component image node. | |
786 | */ | |
787 | if (params->pflag == count) { | |
788 | printf("Extracted:\n%s Image %u (%s)\n", p, | |
789 | count, fit_get_name(fit, noffset, NULL)); | |
790 | ||
791 | fit_image_print(fit, noffset, p); | |
792 | ||
793 | return fit_image_extract(fit, noffset, | |
794 | params->outfile); | |
795 | } | |
796 | ||
797 | count++; | |
798 | } | |
799 | } | |
800 | ||
801 | return 0; | |
802 | } | |
803 | ||
f86ed6a8 | 804 | static int fit_check_params(struct image_tool_params *params) |
89a4d6b1 | 805 | { |
8e35bb07 SG |
806 | if (params->auto_its) |
807 | return 0; | |
89a4d6b1 PW |
808 | return ((params->dflag && (params->fflag || params->lflag)) || |
809 | (params->fflag && (params->dflag || params->lflag)) || | |
810 | (params->lflag && (params->dflag || params->fflag))); | |
811 | } | |
812 | ||
a93648d1 GMF |
813 | U_BOOT_IMAGE_TYPE( |
814 | fitimage, | |
815 | "FIT Image support", | |
816 | sizeof(image_header_t), | |
817 | (void *)&header, | |
818 | fit_check_params, | |
819 | fit_verify_header, | |
820 | fit_print_contents, | |
821 | NULL, | |
39931f96 | 822 | fit_extract_contents, |
a93648d1 GMF |
823 | fit_check_image_types, |
824 | fit_handle_file, | |
825 | NULL /* FIT images use DTB header */ | |
826 | ); |