/src/barebox/common/bootm-fit.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | |
3 | | #include <bootm.h> |
4 | | #include <image-fit.h> |
5 | | #include <bootm-fit.h> |
6 | | #include <globalvar.h> |
7 | | #include <memory.h> |
8 | | #include <string.h> |
9 | | #include <zero_page.h> |
10 | | #include <filetype.h> |
11 | | #include <fs.h> |
12 | | |
13 | | static struct loadable *loadable_from_fit(struct fit_handle *fit, |
14 | | void *config, |
15 | | const char *image_name, |
16 | | int index, |
17 | | enum loadable_type type); |
18 | | |
19 | | /* |
20 | | * loadable_from_fit_os() - create OS loadable from FIT |
21 | | * @data: image data context |
22 | | * @fit: handle of FIT image |
23 | | * @config: config to look up kernel in |
24 | | * |
25 | | * This creates a loadable for the OS. |
26 | | */ |
27 | | static void loadable_from_fit_os(struct image_data *data, |
28 | | struct fit_handle *fit, |
29 | | void *config) |
30 | 0 | { |
31 | 0 | loadable_release(&data->os); |
32 | 0 | data->os = loadable_from_fit(fit, config, "kernel", 0, LOADABLE_KERNEL); |
33 | 0 | } |
34 | | |
35 | | /* |
36 | | * loadable_from_fit_initrd() - create initrd loadable from FIT |
37 | | * @data: image data context |
38 | | * @fit: handle of FIT image |
39 | | * @config: config to look up kernel in |
40 | | * |
41 | | * This creates loadables for all initial ram disks in the config and chains them. |
42 | | * |
43 | | * Return: true if initrd booting is supported and a ramdisk exists or |
44 | | * false otherwise. |
45 | | */ |
46 | | static bool loadable_from_fit_initrd(struct image_data *data, |
47 | | struct fit_handle *fit, |
48 | | void *config) |
49 | 0 | { |
50 | 0 | int nramdisks; |
51 | |
|
52 | 0 | if (!IS_ENABLED(CONFIG_BOOTM_INITRD)) |
53 | 0 | return false; |
54 | | |
55 | 0 | nramdisks = fit_count_images(fit, config, "ramdisk"); |
56 | 0 | if (nramdisks < 0) |
57 | 0 | return false; |
58 | | |
59 | 0 | loadable_release(&data->initrd); |
60 | |
|
61 | 0 | for (int i = 0; i < nramdisks; i++) |
62 | 0 | loadable_chain(&data->initrd, loadable_from_fit(fit, config, "ramdisk", |
63 | 0 | i, LOADABLE_INITRD)); |
64 | |
|
65 | 0 | return nramdisks > 0; |
66 | 0 | } |
67 | | |
68 | | /* |
69 | | * loadable_from_fit_oftree() - create devicetree loadable from FIT |
70 | | * @data: image data context |
71 | | * @fit: handle of FIT image |
72 | | * @config: config to look up kernel in |
73 | | * |
74 | | * This creates a loadable for the first fdt in the config. |
75 | | * |
76 | | * Return: true if a FDT exists or |
77 | | * false otherwise. |
78 | | */ |
79 | | static bool loadable_from_fit_oftree(struct image_data *data, |
80 | | struct fit_handle *fit, |
81 | | void *config) |
82 | 0 | { |
83 | 0 | if (!fit_has_image(fit, config, "fdt")) |
84 | 0 | return false; |
85 | | |
86 | 0 | loadable_release(&data->oftree); |
87 | 0 | data->oftree = loadable_from_fit(fit, config, "fdt", 0, LOADABLE_FDT); |
88 | 0 | return true; |
89 | 0 | } |
90 | | |
91 | | static bool bootm_fit_config_valid(struct fit_handle *fit, |
92 | | struct device_node *config) |
93 | 0 | { |
94 | | /* |
95 | | * Consider only FIT configurations which do provide a loadable kernel |
96 | | * image. |
97 | | */ |
98 | 0 | return !!fit_has_image(fit, config, "kernel"); |
99 | 0 | } |
100 | | |
101 | | static void bootm_fit_config_cmdline(struct device_node *config) |
102 | 0 | { |
103 | 0 | const char *cmdline; |
104 | |
|
105 | 0 | if (!IS_ENABLED(CONFIG_FLEXIBLE_BOOTARGS)) |
106 | 0 | return; |
107 | | |
108 | 0 | if (of_property_read_string(config, "cmdline", &cmdline)) |
109 | 0 | return; |
110 | | |
111 | 0 | globalvar_add_simple(LINUX_BOOTARGS_BOOTM_EARLY "fit", cmdline); |
112 | |
|
113 | 0 | pr_info("Using command line from FIT configuration: %s\n", cmdline); |
114 | 0 | } |
115 | | |
116 | | static enum filetype bootm_fit_update_os_header(struct image_data *data) |
117 | 0 | { |
118 | 0 | size_t size; |
119 | 0 | const void *header; |
120 | 0 | enum filetype os_type; |
121 | |
|
122 | 0 | header = loadable_view(data->os, &size); |
123 | 0 | if (IS_ERR(header)) |
124 | 0 | return filetype_unknown; |
125 | | |
126 | 0 | if (size >= PAGE_SIZE) |
127 | 0 | os_type = file_detect_type(header, size); |
128 | 0 | else |
129 | 0 | os_type = filetype_unknown; |
130 | |
|
131 | 0 | free(data->os_header); |
132 | 0 | data->os_header = xmemdup(header, min_t(size_t, size, PAGE_SIZE)); |
133 | |
|
134 | 0 | loadable_view_free(data->os, header, size); |
135 | |
|
136 | 0 | return os_type; |
137 | 0 | } |
138 | | |
139 | | int bootm_open_fit(struct image_data *data, bool override) |
140 | 0 | { |
141 | 0 | struct fit_handle *fit; |
142 | 0 | void *fit_config; |
143 | 0 | int ret; |
144 | |
|
145 | 0 | fit = fit_open(data->os_file, data->verbose, data->verify); |
146 | 0 | if (IS_ERR(fit)) { |
147 | 0 | pr_err("Loading FIT image %s failed with: %pe\n", data->os_file, fit); |
148 | 0 | return PTR_ERR(fit); |
149 | 0 | } |
150 | | |
151 | 0 | fit_config = fit_open_configuration(fit, data->os_part, bootm_fit_config_valid); |
152 | 0 | if (IS_ERR(fit_config)) { |
153 | 0 | pr_err("Cannot open FIT image configuration '%s'\n", |
154 | 0 | data->os_part ?: "default"); |
155 | 0 | ret = PTR_ERR(fit_config); |
156 | 0 | goto err; |
157 | 0 | } |
158 | | |
159 | 0 | if (fit_has_image(fit, fit_config, "tee")) { |
160 | 0 | pr_err("Late-loaded tee is insecure and no longer supported\n"); |
161 | 0 | ret = -ENOSYS; |
162 | 0 | goto err; |
163 | 0 | } |
164 | | |
165 | 0 | bootm_fit_config_cmdline(fit_config); |
166 | |
|
167 | 0 | loadable_from_fit_os(data, fit, fit_config); |
168 | 0 | if (override) |
169 | 0 | data->is_override.os = true; |
170 | 0 | if (loadable_from_fit_initrd(data, fit, fit_config) && override) |
171 | 0 | data->is_override.initrd = true; |
172 | 0 | if (loadable_from_fit_oftree(data, fit, fit_config) && override) |
173 | 0 | data->is_override.oftree = true; |
174 | |
|
175 | 0 | data->kernel_type = bootm_fit_update_os_header(data); |
176 | |
|
177 | 0 | if (data->os_address == UIMAGE_SOME_ADDRESS) { |
178 | 0 | ret = fit_get_image_address(fit, fit_config, "kernel", |
179 | 0 | "load", &data->os_address); |
180 | 0 | if (!ret) |
181 | 0 | pr_info("Load address from FIT '%s': 0x%lx\n", |
182 | 0 | "kernel", data->os_address); |
183 | | /* Note: Error case uses default value. */ |
184 | 0 | } |
185 | 0 | if (data->os_entry == UIMAGE_SOME_ADDRESS) { |
186 | 0 | unsigned long entry; |
187 | 0 | ret = fit_get_image_address(fit, fit_config, "kernel", |
188 | 0 | "entry", &entry); |
189 | 0 | if (!ret) { |
190 | 0 | data->os_entry = entry - data->os_address; |
191 | 0 | pr_info("Entry address from FIT '%s': 0x%lx\n", |
192 | 0 | "kernel", entry); |
193 | 0 | } |
194 | | /* Note: Error case uses default value. */ |
195 | 0 | } |
196 | | |
197 | | /* Each loadable now holds a reference to the FIT, so close our original |
198 | | * reference, so the FIT is completely reclaimed if bootm fails. |
199 | | */ |
200 | 0 | fit_close(fit); |
201 | |
|
202 | 0 | return 0; |
203 | 0 | err: |
204 | 0 | fit_close(fit); |
205 | 0 | return ret; |
206 | 0 | } |
207 | | |
208 | | /* === Loadable implementation for FIT images === */ |
209 | | |
210 | | struct fit_loadable_priv { |
211 | | struct fit_handle *fit; |
212 | | struct device_node *config; |
213 | | const char *image_name; |
214 | | int index; |
215 | | const void *data; |
216 | | unsigned long size; |
217 | | }; |
218 | | |
219 | | /* |
220 | | * fit_loadable_open() - verify and decompress the image on first use |
221 | | * @priv: FIT loadable private data |
222 | | * |
223 | | * The result is cached, so hashing and decompression happen only once. |
224 | | * The data is owned by the FIT handle, which we hold a reference to. |
225 | | */ |
226 | | static int fit_loadable_open(struct fit_loadable_priv *priv) |
227 | 0 | { |
228 | 0 | if (priv->data) |
229 | 0 | return 0; |
230 | | |
231 | 0 | return fit_open_image(priv->fit, priv->config, priv->image_name, |
232 | 0 | priv->index, &priv->data, &priv->size); |
233 | 0 | } |
234 | | |
235 | | static int fit_loadable_get_info(struct loadable *l, struct loadable_info *info) |
236 | 0 | { |
237 | 0 | struct fit_loadable_priv *priv = l->priv; |
238 | 0 | int ret; |
239 | | |
240 | | /* Open image to get size */ |
241 | 0 | ret = fit_loadable_open(priv); |
242 | 0 | if (ret) |
243 | 0 | return ret; |
244 | | |
245 | | /* TODO: This will trigger an uncompression currently.. */ |
246 | 0 | info->final_size = priv->size; |
247 | |
|
248 | 0 | return 0; |
249 | 0 | } |
250 | | |
251 | | static const void *fit_loadable_mmap(struct loadable *l, size_t *size) |
252 | 0 | { |
253 | 0 | struct fit_loadable_priv *priv = l->priv; |
254 | 0 | int ret; |
255 | |
|
256 | 0 | ret = fit_loadable_open(priv); |
257 | 0 | if (ret) |
258 | 0 | return MAP_FAILED; |
259 | | |
260 | 0 | *size = priv->size; |
261 | 0 | return priv->data; |
262 | 0 | } |
263 | | |
264 | | /** |
265 | | * fit_loadable_extract_into_buf - load FIT image data to target address |
266 | | * @l: loadable representing FIT image component |
267 | | * @load_addr: physical address to load data to |
268 | | * @buf_size: size of buffer at load_addr (0 = no limit check) |
269 | | * @offset: how many bytes to skip at the start of the uncompressed input |
270 | | * @flags: A bitmask of OR-ed LOADABLE_EXTRACT_ flags |
271 | | * |
272 | | * Commits the FIT image component to the specified memory address. This |
273 | | * involves: |
274 | | * 1. Opening the FIT image to get decompressed data |
275 | | * 2. Checking buffer size |
276 | | * 3. Copying data to target address |
277 | | * |
278 | | * The FIT data is already decompressed by fit_open_image(), so this just |
279 | | * performs a memcpy to the target address. |
280 | | * |
281 | | * Return: actual number of bytes written on success, negative errno on error |
282 | | * -ENOSPC if buf_size is specified and too small |
283 | | * -ENOMEM if failed to register SDRAM region |
284 | | */ |
285 | | static ssize_t fit_loadable_extract_into_buf(struct loadable *l, void *load_addr, |
286 | | size_t buf_size, loff_t offset, |
287 | | unsigned flags) |
288 | 0 | { |
289 | 0 | struct fit_loadable_priv *priv = l->priv; |
290 | 0 | const void *data; |
291 | 0 | unsigned long size; |
292 | 0 | int ret; |
293 | | |
294 | | /* TODO: optimize, so it decompresses directly to load address */ |
295 | | |
296 | | /* Open image to get data */ |
297 | 0 | ret = fit_loadable_open(priv); |
298 | 0 | if (ret) |
299 | 0 | return ret; |
300 | | |
301 | 0 | data = priv->data; |
302 | 0 | size = priv->size; |
303 | | |
304 | | /* Check if buffer is large enough */ |
305 | 0 | if (offset > size) |
306 | 0 | return 0; |
307 | | |
308 | 0 | if (!(flags & LOADABLE_EXTRACT_PARTIAL) && buf_size < size - offset) |
309 | 0 | return -ENOSPC; |
310 | | |
311 | 0 | size = min_t(size_t, size - offset, buf_size); |
312 | |
|
313 | 0 | if (unlikely(zero_page_contains((ulong)load_addr))) |
314 | 0 | zero_page_memcpy(load_addr, data + offset, size); |
315 | 0 | else |
316 | 0 | memcpy(load_addr, data + offset, size); |
317 | |
|
318 | 0 | return size; /* Return actual bytes written */ |
319 | 0 | } |
320 | | |
321 | | static void fit_loadable_release(struct loadable *l) |
322 | 0 | { |
323 | 0 | struct fit_loadable_priv *priv = l->priv; |
324 | |
|
325 | 0 | fit_close(priv->fit); |
326 | 0 | free_const(priv->image_name); |
327 | 0 | free(priv); |
328 | 0 | } |
329 | | |
330 | | static const struct loadable_ops fit_loadable_ops = { |
331 | | .get_info = fit_loadable_get_info, |
332 | | .extract_into_buf = fit_loadable_extract_into_buf, |
333 | | .mmap = fit_loadable_mmap, |
334 | | .release = fit_loadable_release, |
335 | | }; |
336 | | |
337 | | /** |
338 | | * loadable_from_fit - create a loadable from FIT image component |
339 | | * @fit: opened FIT image handle |
340 | | * @config: FIT configuration device node |
341 | | * @image_name: name of image in FIT (e.g., "kernel", "ramdisk", "fdt") |
342 | | * @index: index for multi-image types (e.g., ramdisk-0, ramdisk-1) |
343 | | * @type: type of loadable (LOADABLE_KERNEL, LOADABLE_INITRD, etc.) |
344 | | * |
345 | | * Creates a loadable structure that wraps access to a component within a |
346 | | * FIT image. The loadable uses the FIT handle to access decompressed image |
347 | | * data on demand during commit. |
348 | | * |
349 | | * The created loadable must be freed with loadable_release() when done. |
350 | | * The FIT handle itself is managed by the caller and must remain valid |
351 | | * until the loadable is released. |
352 | | * |
353 | | * Return: pointer to allocated loadable on success. Function never fails. |
354 | | */ |
355 | | static struct loadable *loadable_from_fit(struct fit_handle *fit, |
356 | | void *config, |
357 | | const char *image_name, |
358 | | int index, |
359 | | enum loadable_type type) |
360 | 0 | { |
361 | 0 | struct loadable *l; |
362 | 0 | struct fit_loadable_priv *priv; |
363 | |
|
364 | 0 | l = xzalloc(sizeof(*l)); |
365 | 0 | priv = xzalloc(sizeof(*priv)); |
366 | |
|
367 | 0 | priv->fit = fit_open_handle(fit); |
368 | 0 | priv->config = config; |
369 | 0 | priv->image_name = xstrdup_const(image_name); |
370 | 0 | priv->index = index; |
371 | | |
372 | | /* Create descriptive name */ |
373 | 0 | if (index) |
374 | 0 | l->name = xasprintf("FIT(%s, %s/%s, %d)", fit->filename, |
375 | 0 | fit_config_get_name(fit, config), |
376 | 0 | image_name, index); |
377 | 0 | else |
378 | 0 | l->name = xasprintf("FIT(%s, %s/%s)", fit->filename, |
379 | 0 | fit_config_get_name(fit, config), |
380 | 0 | image_name); |
381 | 0 | l->ops = &fit_loadable_ops; |
382 | 0 | l->type = type; |
383 | 0 | l->priv = priv; |
384 | 0 | loadable_init(l); |
385 | |
|
386 | 0 | return l; |
387 | 0 | } |