Commit | Line | Data |
---|---|---|
89a4d6b1 PW |
1 | /* |
2 | * (C) Copyright 2008 Semihalf | |
3 | * | |
4 | * (C) Copyright 2000-2004 | |
5 | * DENX Software Engineering | |
6 | * Wolfgang Denk, wd@denx.de | |
7 | * | |
8 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> | |
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" |
89a4d6b1 PW |
18 | #include "mkimage.h" |
19 | #include <image.h> | |
20 | #include <u-boot/crc.h> | |
21 | ||
22 | static image_header_t header; | |
23 | ||
24 | static int fit_verify_header (unsigned char *ptr, int image_size, | |
f86ed6a8 | 25 | struct image_tool_params *params) |
89a4d6b1 | 26 | { |
2f0877c7 | 27 | return fdt_check_header(ptr); |
89a4d6b1 PW |
28 | } |
29 | ||
30 | static int fit_check_image_types (uint8_t type) | |
31 | { | |
32 | if (type == IH_TYPE_FLATDT) | |
33 | return EXIT_SUCCESS; | |
34 | else | |
35 | return EXIT_FAILURE; | |
36 | } | |
37 | ||
f86ed6a8 | 38 | int mmap_fdt(struct image_tool_params *params, const char *fname, void **blobp, |
aa6d6db4 SG |
39 | struct stat *sbuf) |
40 | { | |
41 | void *ptr; | |
42 | int fd; | |
43 | ||
44 | /* Load FIT blob into memory (we need to write hashes/signatures) */ | |
45 | fd = open(fname, O_RDWR | O_BINARY); | |
46 | ||
47 | if (fd < 0) { | |
48 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
49 | params->cmdname, fname, strerror(errno)); | |
50 | unlink(fname); | |
51 | return -1; | |
52 | } | |
53 | ||
54 | if (fstat(fd, sbuf) < 0) { | |
55 | fprintf(stderr, "%s: Can't stat %s: %s\n", | |
56 | params->cmdname, fname, strerror(errno)); | |
57 | unlink(fname); | |
58 | return -1; | |
59 | } | |
60 | ||
61 | ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); | |
62 | if (ptr == MAP_FAILED) { | |
63 | fprintf(stderr, "%s: Can't read %s: %s\n", | |
64 | params->cmdname, fname, strerror(errno)); | |
65 | unlink(fname); | |
66 | return -1; | |
67 | } | |
68 | ||
69 | /* check if ptr has a valid blob */ | |
70 | if (fdt_check_header(ptr)) { | |
71 | fprintf(stderr, "%s: Invalid FIT blob\n", params->cmdname); | |
72 | unlink(fname); | |
73 | return -1; | |
74 | } | |
75 | ||
76 | *blobp = ptr; | |
77 | return fd; | |
78 | } | |
79 | ||
89a4d6b1 PW |
80 | /** |
81 | * fit_handle_file - main FIT file processing function | |
82 | * | |
83 | * fit_handle_file() runs dtc to convert .its to .itb, includes | |
84 | * binary data, updates timestamp property and calculates hashes. | |
85 | * | |
86 | * datafile - .its file | |
87 | * imagefile - .itb file | |
88 | * | |
89 | * returns: | |
90 | * only on success, otherwise calls exit (EXIT_FAILURE); | |
91 | */ | |
f86ed6a8 | 92 | static int fit_handle_file(struct image_tool_params *params) |
89a4d6b1 PW |
93 | { |
94 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; | |
95 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; | |
e29495d3 SG |
96 | int tfd, destfd = 0; |
97 | void *dest_blob = NULL; | |
89a4d6b1 | 98 | struct stat sbuf; |
aa6d6db4 | 99 | void *ptr; |
e29495d3 | 100 | off_t destfd_size = 0; |
89a4d6b1 PW |
101 | |
102 | /* Flattened Image Tree (FIT) format handling */ | |
103 | debug ("FIT format handling\n"); | |
104 | ||
105 | /* call dtc to include binary properties into the tmp file */ | |
106 | if (strlen (params->imagefile) + | |
107 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { | |
108 | fprintf (stderr, "%s: Image file name (%s) too long, " | |
109 | "can't create tmpfile", | |
110 | params->imagefile, params->cmdname); | |
111 | return (EXIT_FAILURE); | |
112 | } | |
113 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); | |
114 | ||
95d77b44 SG |
115 | /* We either compile the source file, or use the existing FIT image */ |
116 | if (params->datafile) { | |
117 | /* dtc -I dts -O dtb -p 500 datafile > tmpfile */ | |
118 | snprintf(cmd, sizeof(cmd), "%s %s %s > %s", | |
119 | MKIMAGE_DTC, params->dtc, params->datafile, tmpfile); | |
120 | debug("Trying to execute \"%s\"\n", cmd); | |
121 | } else { | |
122 | snprintf(cmd, sizeof(cmd), "cp %s %s", | |
123 | params->imagefile, tmpfile); | |
124 | } | |
89a4d6b1 PW |
125 | if (system (cmd) == -1) { |
126 | fprintf (stderr, "%s: system(%s) failed: %s\n", | |
127 | params->cmdname, cmd, strerror(errno)); | |
aa6d6db4 | 128 | goto err_system; |
89a4d6b1 PW |
129 | } |
130 | ||
e29495d3 SG |
131 | if (params->keydest) { |
132 | destfd = mmap_fdt(params, params->keydest, &dest_blob, &sbuf); | |
133 | if (destfd < 0) | |
134 | goto err_keydest; | |
135 | destfd_size = sbuf.st_size; | |
136 | } | |
137 | ||
aa6d6db4 SG |
138 | tfd = mmap_fdt(params, tmpfile, &ptr, &sbuf); |
139 | if (tfd < 0) | |
140 | goto err_mmap; | |
89a4d6b1 PW |
141 | |
142 | /* set hashes for images in the blob */ | |
399c744b SG |
143 | if (fit_add_verification_data(params->keydir, |
144 | dest_blob, ptr, params->comment, | |
145 | params->require_keys)) { | |
146 | fprintf(stderr, "%s Can't add hashes to FIT blob\n", | |
147 | params->cmdname); | |
aa6d6db4 | 148 | goto err_add_hashes; |
89a4d6b1 PW |
149 | } |
150 | ||
95d77b44 SG |
151 | /* for first image creation, add a timestamp at offset 0 i.e., root */ |
152 | if (params->datafile && fit_set_timestamp(ptr, 0, sbuf.st_mtime)) { | |
89a4d6b1 PW |
153 | fprintf (stderr, "%s: Can't add image timestamp\n", |
154 | params->cmdname); | |
aa6d6db4 | 155 | goto err_add_timestamp; |
89a4d6b1 PW |
156 | } |
157 | debug ("Added timestamp successfully\n"); | |
158 | ||
159 | munmap ((void *)ptr, sbuf.st_size); | |
160 | close (tfd); | |
e29495d3 SG |
161 | if (dest_blob) { |
162 | munmap(dest_blob, destfd_size); | |
163 | close(destfd); | |
164 | } | |
89a4d6b1 PW |
165 | |
166 | if (rename (tmpfile, params->imagefile) == -1) { | |
167 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", | |
168 | params->cmdname, tmpfile, params->imagefile, | |
169 | strerror (errno)); | |
170 | unlink (tmpfile); | |
171 | unlink (params->imagefile); | |
172 | return (EXIT_FAILURE); | |
173 | } | |
174 | return (EXIT_SUCCESS); | |
aa6d6db4 SG |
175 | |
176 | err_add_timestamp: | |
177 | err_add_hashes: | |
178 | munmap(ptr, sbuf.st_size); | |
179 | err_mmap: | |
e29495d3 SG |
180 | if (dest_blob) |
181 | munmap(dest_blob, destfd_size); | |
182 | err_keydest: | |
aa6d6db4 SG |
183 | err_system: |
184 | unlink(tmpfile); | |
185 | return -1; | |
89a4d6b1 PW |
186 | } |
187 | ||
f86ed6a8 | 188 | static int fit_check_params(struct image_tool_params *params) |
89a4d6b1 PW |
189 | { |
190 | return ((params->dflag && (params->fflag || params->lflag)) || | |
191 | (params->fflag && (params->dflag || params->lflag)) || | |
192 | (params->lflag && (params->dflag || params->fflag))); | |
193 | } | |
194 | ||
195 | static struct image_type_params fitimage_params = { | |
196 | .name = "FIT Image support", | |
197 | .header_size = sizeof(image_header_t), | |
198 | .hdr = (void*)&header, | |
199 | .verify_header = fit_verify_header, | |
200 | .print_header = fit_print_contents, | |
201 | .check_image_type = fit_check_image_types, | |
202 | .fflag_handle = fit_handle_file, | |
8e1c8966 | 203 | .set_header = NULL, /* FIT images use DTB header */ |
89a4d6b1 PW |
204 | .check_params = fit_check_params, |
205 | }; | |
206 | ||
207 | void init_fit_image_type (void) | |
208 | { | |
f86ed6a8 | 209 | register_image_type(&fitimage_params); |
89a4d6b1 | 210 | } |