/src/barebox/drivers/of/overlay.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * Functions for working with device tree overlays |
4 | | * |
5 | | * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com> |
6 | | * Copyright (C) 2012 Texas Instruments Inc. |
7 | | * Copyright (C) 2019 Pengutronix, Michael Tretter <m.tretter@pengutronix.de> |
8 | | */ |
9 | | #define pr_fmt(fmt) "of_overlay: " fmt |
10 | | |
11 | | #include <bootm.h> |
12 | | #include <common.h> |
13 | | #include <of.h> |
14 | | #include <errno.h> |
15 | | #include <globalvar.h> |
16 | | #include <image-fit.h> |
17 | | #include <magicvar.h> |
18 | | #include <string.h> |
19 | | #include <libfile.h> |
20 | | #include <fs.h> |
21 | | #include <libbb.h> |
22 | | #include <fnmatch.h> |
23 | | #include <fuzz.h> |
24 | | |
25 | | /* @optional: only an overlay applied on request has to fit the tree */ |
26 | | static struct device_node *find_target(struct device_node *root, |
27 | | struct device_node *fragment, |
28 | | bool optional) |
29 | 0 | { |
30 | 0 | struct device_node *node; |
31 | 0 | const char *path; |
32 | 0 | u32 phandle; |
33 | 0 | int ret; |
34 | |
|
35 | 0 | ret = of_property_read_u32(fragment, "target", &phandle); |
36 | 0 | if (!ret) { |
37 | 0 | node = of_find_node_by_phandle_from(phandle, root); |
38 | 0 | if (!node) |
39 | 0 | __pr_printk(optional ? MSG_DEBUG : MSG_ERR, |
40 | 0 | pr_fmt("fragment %pOF: phandle 0x%x not found\n"), |
41 | 0 | fragment, phandle); |
42 | 0 | return node; |
43 | 0 | } |
44 | | |
45 | 0 | ret = of_property_read_string(fragment, "target-path", &path); |
46 | 0 | if (!ret) { |
47 | 0 | node = of_find_node_by_path_from(root, path); |
48 | 0 | if (!node) |
49 | 0 | __pr_printk(optional ? MSG_DEBUG : MSG_ERR, |
50 | 0 | pr_fmt("fragment %pOF: path '%s' not found\n"), |
51 | 0 | fragment, path); |
52 | 0 | return node; |
53 | 0 | } |
54 | | |
55 | 0 | pr_err("fragment %pOF: no target property\n", fragment); |
56 | |
|
57 | 0 | return NULL; |
58 | 0 | } |
59 | | |
60 | | static int of_overlay_apply(struct device_node *target, |
61 | | const struct device_node *overlay) |
62 | 0 | { |
63 | 0 | struct device_node *child; |
64 | 0 | struct device_node *target_child; |
65 | 0 | struct property *prop; |
66 | 0 | int err; |
67 | |
|
68 | 0 | if (target == NULL || overlay == NULL) |
69 | 0 | return -EINVAL; |
70 | | |
71 | 0 | list_for_each_entry(prop, &overlay->properties, list) { |
72 | 0 | if (of_prop_cmp(prop->name, "name") == 0) |
73 | 0 | continue; |
74 | | |
75 | 0 | if (of_prop_cmp(prop->name, "phandle") == 0) |
76 | 0 | target->phandle = be32_to_cpup(prop->value); |
77 | |
|
78 | 0 | err = of_set_property(target, prop->name, prop->value, |
79 | 0 | prop->length, true); |
80 | 0 | if (err) |
81 | 0 | return err; |
82 | 0 | } |
83 | | |
84 | 0 | for_each_child_of_node(overlay, child) { |
85 | 0 | target_child = of_get_child_by_name(target, child->name); |
86 | 0 | if (!target_child) |
87 | 0 | target_child = of_new_node(target, child->name); |
88 | 0 | if (!target_child) |
89 | 0 | return -ENOMEM; |
90 | | |
91 | 0 | err = of_overlay_apply(target_child, child); |
92 | 0 | if (err) |
93 | 0 | return err; |
94 | 0 | } |
95 | | |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | | static char *of_overlay_fix_path(struct device_node *root, |
100 | | struct device_node *overlay, const char *path) |
101 | 0 | { |
102 | 0 | struct device_node *overlay_node; |
103 | 0 | struct device_node *fragment; |
104 | 0 | struct device_node *target; |
105 | 0 | const char *path_tail; |
106 | 0 | const char *prefix; |
107 | 0 | size_t prefix_len; |
108 | |
|
109 | 0 | fragment = of_find_node_by_path_from(overlay, path); |
110 | 0 | while ((fragment = of_get_parent(fragment)) != NULL) { |
111 | 0 | overlay_node = of_get_child_by_name(fragment, "__overlay__"); |
112 | 0 | if (overlay_node != NULL) |
113 | 0 | break; |
114 | 0 | } |
115 | |
|
116 | 0 | if (!fragment) { |
117 | 0 | pr_debug("could not find __overlay__ node for %s\n", path); |
118 | 0 | return NULL; |
119 | 0 | } |
120 | | |
121 | 0 | prefix = overlay_node->full_name; |
122 | 0 | prefix_len = str_has_prefix(path, prefix); |
123 | 0 | if (!prefix_len || !strchr("/", path[prefix_len])) { |
124 | 0 | pr_debug("%s is not below %pOF\n", path, overlay_node); |
125 | 0 | return NULL; |
126 | 0 | } |
127 | | |
128 | 0 | pr_debug("using fragment %pOF\n", fragment); |
129 | |
|
130 | 0 | target = find_target(root, fragment, false); |
131 | 0 | if (!target) |
132 | 0 | return ERR_PTR(-EINVAL); |
133 | | |
134 | 0 | path_tail = path + prefix_len; |
135 | |
|
136 | 0 | return basprintf("%pOF%s", target, path_tail); |
137 | 0 | } |
138 | | |
139 | | static int of_overlay_apply_symbols(struct device_node *root, |
140 | | struct device_node *overlay) |
141 | 222 | { |
142 | 222 | const char *old_path; |
143 | 222 | char *new_path; |
144 | 222 | struct property *prop; |
145 | 222 | struct device_node *root_symbols; |
146 | 222 | struct device_node *overlay_symbols; |
147 | | |
148 | 222 | root_symbols = of_get_child_by_name(root, "__symbols__"); |
149 | 222 | overlay_symbols = of_get_child_by_name(overlay, "__symbols__"); |
150 | | |
151 | 222 | if (!overlay_symbols) { |
152 | 222 | pr_debug("overlay doesn't have a __symbols__ node\n"); |
153 | 222 | return 0; |
154 | 222 | } |
155 | | |
156 | 0 | if (!root_symbols) { |
157 | 0 | pr_info("root doesn't have a __symbols__ node\n"); |
158 | 0 | return 0; |
159 | 0 | } |
160 | | |
161 | 0 | list_for_each_entry(prop, &overlay_symbols->properties, list) { |
162 | 0 | if (of_prop_cmp(prop->name, "name") == 0) |
163 | 0 | continue; |
164 | | |
165 | 0 | old_path = of_property_get_value(prop); |
166 | 0 | new_path = of_overlay_fix_path(root, overlay, old_path); |
167 | 0 | if (IS_ERR(new_path)) |
168 | 0 | return PTR_ERR(new_path); |
169 | 0 | if (!new_path) |
170 | 0 | continue; |
171 | | |
172 | 0 | pr_debug("add symbol %s with new path %s (was %s)\n", |
173 | 0 | prop->name, new_path, old_path); |
174 | 0 | of_property_write_string(root_symbols, prop->name, new_path); |
175 | 0 | free(new_path); |
176 | 0 | } |
177 | | |
178 | 0 | return 0; |
179 | 0 | } |
180 | | |
181 | | static int of_overlay_apply_fragment(struct device_node *root, |
182 | | struct device_node *fragment, |
183 | | bool optional) |
184 | 2.77k | { |
185 | 2.77k | struct device_node *target; |
186 | 2.77k | struct device_node *overlay; |
187 | | |
188 | 2.77k | overlay = of_get_child_by_name(fragment, "__overlay__"); |
189 | 2.77k | if (!overlay) |
190 | 2.77k | return 0; |
191 | | |
192 | 0 | target = find_target(root, fragment, optional); |
193 | 0 | if (!target) |
194 | 0 | return -ENOENT; |
195 | | |
196 | 0 | return of_overlay_apply(target, overlay); |
197 | 0 | } |
198 | | |
199 | | static char *of_overlay_compatible; |
200 | | |
201 | | /* |
202 | | * A built-in overlay is applied to whatever device tree barebox was started |
203 | | * with, so a fragment that finds no target is just skipped. What the overlay |
204 | | * can't do without goes into barebox,assert-available and drops it as a whole. |
205 | | */ |
206 | | static int of_overlay_assert_available(struct device_node *root, |
207 | | struct device_node *overlay) |
208 | 0 | { |
209 | 0 | struct device_node *node; |
210 | 0 | struct property *prop; |
211 | 0 | const char *path; |
212 | |
|
213 | 0 | if (!IS_ENABLED(CONFIG_OF_OVERLAY_BUILTIN)) |
214 | 0 | return 0; |
215 | | |
216 | 0 | of_property_for_each_string(overlay, "barebox,assert-available", |
217 | 0 | prop, path) { |
218 | 0 | node = of_find_node_by_path_from(root, path); |
219 | 0 | if (!node || !of_device_is_available(node)) { |
220 | 0 | pr_debug("asserted %s is missing or disabled\n", path); |
221 | 0 | return -ENODEV; |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | 0 | return 0; |
226 | 0 | } |
227 | | |
228 | | static int __of_overlay_apply_tree(struct device_node *root, |
229 | | struct device_node *overlay, |
230 | | bool builtin) |
231 | 345 | { |
232 | 345 | struct device_node *resolved; |
233 | 345 | struct device_node *fragment; |
234 | 345 | int err = 0; |
235 | | |
236 | 345 | resolved = of_resolve_phandles(root, overlay); |
237 | 345 | if (!resolved) |
238 | 123 | return -EINVAL; |
239 | | |
240 | 222 | if (builtin) { |
241 | 0 | err = of_overlay_assert_available(root, resolved); |
242 | 0 | if (err) |
243 | 0 | goto out_err; |
244 | 0 | } |
245 | | |
246 | 222 | err = of_overlay_pre_load_firmware(root, resolved); |
247 | 222 | if (err) |
248 | 0 | goto out_err; |
249 | | |
250 | | /* Copy symbols from resolved overlay to base device tree */ |
251 | 222 | err = of_overlay_apply_symbols(root, resolved); |
252 | 222 | if (err) |
253 | 0 | goto out_err; |
254 | | |
255 | | /* Copy nodes and properties from resolved overlay to root */ |
256 | 2.77k | for_each_child_of_node(resolved, fragment) { |
257 | 2.77k | int ret = of_overlay_apply_fragment(root, fragment, builtin); |
258 | 2.77k | if (!ret) |
259 | 2.77k | continue; |
260 | | |
261 | | /* A built-in overlay applies the fragments that do fit */ |
262 | 0 | if (builtin && ret == -ENOENT) |
263 | 0 | continue; |
264 | | |
265 | 0 | pr_warn("failed to apply %s\n", fragment->name); |
266 | 0 | err = err ?: ret; |
267 | 0 | } |
268 | | |
269 | | /* We are patching the live tree, reload aliases */ |
270 | 222 | if (root == of_get_root_node()) |
271 | 0 | of_alias_scan(); |
272 | | |
273 | 222 | out_err: |
274 | 222 | of_delete_node(resolved); |
275 | | |
276 | 222 | return err; |
277 | 222 | } |
278 | | |
279 | | /** |
280 | | * Apply the overlay on the passed devicetree root |
281 | | * @root: the devicetree onto which the overlay will be applied |
282 | | * @overlay: the devicetree to apply as an overlay |
283 | | */ |
284 | | int of_overlay_apply_tree(struct device_node *root, |
285 | | struct device_node *overlay) |
286 | 345 | { |
287 | 345 | return __of_overlay_apply_tree(root, overlay, false); |
288 | 345 | } |
289 | | |
290 | | static char *of_overlay_filter; |
291 | | |
292 | | static LIST_HEAD(of_overlay_filters); |
293 | | |
294 | | static struct of_overlay_filter *of_overlay_find_filter(const char *name) |
295 | 0 | { |
296 | 0 | struct of_overlay_filter *f; |
297 | |
|
298 | 0 | list_for_each_entry(f, &of_overlay_filters, list) |
299 | 0 | if (!strcmp(f->name, name)) |
300 | 0 | return f; |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | | static bool of_overlay_matches_filter(const char *pattern, struct device_node *ovl) |
305 | 0 | { |
306 | 0 | struct of_overlay_filter *filter; |
307 | 0 | char *p, *path, *n; |
308 | 0 | bool apply = false; |
309 | 0 | bool have_pattern_filter = false; |
310 | 0 | bool have_content_filter = false; |
311 | |
|
312 | 0 | p = path = strdup(of_overlay_filter); |
313 | |
|
314 | 0 | while ((n = strsep_unescaped(&p, " ", NULL))) { |
315 | 0 | int score = 0; |
316 | |
|
317 | 0 | if (!*n) |
318 | 0 | continue; |
319 | | |
320 | 0 | filter = of_overlay_find_filter(n); |
321 | 0 | if (!filter) { |
322 | 0 | pr_err("Ignoring unknown filter %s\n", n); |
323 | 0 | continue; |
324 | 0 | } |
325 | | |
326 | 0 | if (filter->filter_pattern || filter->filter_filename) |
327 | 0 | have_pattern_filter = true; |
328 | 0 | if (filter->filter_content) |
329 | 0 | have_content_filter = true; |
330 | |
|
331 | 0 | if (pattern) { |
332 | 0 | if ((filter->filter_pattern && |
333 | 0 | filter->filter_pattern(filter, kbasename(pattern))) || |
334 | 0 | (filter->filter_filename && |
335 | 0 | filter->filter_filename(filter, kbasename(pattern)))) |
336 | 0 | score++; |
337 | 0 | } else { |
338 | 0 | score++; |
339 | 0 | } |
340 | |
|
341 | 0 | if (ovl) { |
342 | 0 | if (filter->filter_content && |
343 | 0 | filter->filter_content(filter, ovl)) |
344 | 0 | score++; |
345 | 0 | } else { |
346 | 0 | score++; |
347 | 0 | } |
348 | |
|
349 | 0 | if (score == 2) { |
350 | 0 | apply = true; |
351 | 0 | break; |
352 | 0 | } |
353 | 0 | } |
354 | |
|
355 | 0 | free(path); |
356 | | |
357 | | /* No filter found at all, no match */ |
358 | 0 | if (!have_pattern_filter && !have_content_filter) { |
359 | 0 | pr_debug("No match, because no filter was found\n"); |
360 | 0 | return false; |
361 | 0 | } |
362 | | |
363 | | /* Want to match pattern, but we do not have a filename_filter */ |
364 | 0 | if (pattern && !have_pattern_filter) { |
365 | 0 | pr_debug("Pattern match requested, but no pattern filter found\n"); |
366 | 0 | return true; |
367 | 0 | } |
368 | | |
369 | | /* Want to match content, but we do not have a content_filter */ |
370 | 0 | if (ovl && !have_content_filter) { |
371 | 0 | pr_debug("Content match requested, but no content filter found\n"); |
372 | 0 | return true; |
373 | 0 | } |
374 | | |
375 | 0 | if (apply) |
376 | 0 | pr_debug("pattern %s, overlay %p: match against filter %s\n", |
377 | 0 | pattern ?: "<NONE>", |
378 | 0 | ovl, filter->name); |
379 | 0 | else |
380 | 0 | pr_debug("pattern %s, overlay %p: no match\n", |
381 | 0 | pattern ?: "<NONE>", ovl); |
382 | |
|
383 | 0 | return apply; |
384 | 0 | } |
385 | | |
386 | | int of_overlay_apply_file(struct device_node *root, const char *filename, |
387 | | bool filter) |
388 | 0 | { |
389 | 0 | struct device_node *ovl; |
390 | 0 | int ret; |
391 | |
|
392 | 0 | if (filter && !of_overlay_matches_filter(filename, NULL)) |
393 | 0 | return 0; |
394 | | |
395 | 0 | ovl = of_read_file(filename); |
396 | 0 | if (IS_ERR(ovl)) |
397 | 0 | return PTR_ERR(ovl); |
398 | | |
399 | 0 | if (filter && !of_overlay_matches_filter(NULL, ovl)) |
400 | 0 | return 0; |
401 | | |
402 | 0 | ret = of_overlay_apply_tree(root, ovl); |
403 | 0 | if (ret == -ENODEV) |
404 | 0 | pr_debug("Not applied %s (not compatible)\n", filename); |
405 | 0 | else if (ret) |
406 | 0 | pr_err("Cannot apply %s: %pe\n", filename, ERR_PTR(ret)); |
407 | 0 | else |
408 | 0 | pr_info("Applied %s\n", filename); |
409 | |
|
410 | 0 | of_delete_node(ovl); |
411 | |
|
412 | 0 | return ret; |
413 | 0 | } |
414 | | |
415 | | int of_overlay_apply_dtbo(struct device_node *root, const void *dtbo) |
416 | 0 | { |
417 | 0 | struct device_node *overlay; |
418 | 0 | int ret; |
419 | |
|
420 | 0 | overlay = of_unflatten_dtb(dtbo, INT_MAX); |
421 | 0 | if (IS_ERR(overlay)) |
422 | 0 | return PTR_ERR(overlay); |
423 | | |
424 | 0 | ret = of_overlay_apply_tree(root, overlay); |
425 | 0 | of_delete_node(overlay); |
426 | |
|
427 | 0 | return ret; |
428 | 0 | } |
429 | | |
430 | | /* Picked like a driver for a device: any compatible in common is a match */ |
431 | | static const char *of_overlay_matches_machine(struct device_node *root, |
432 | | struct device_node *overlay) |
433 | 0 | { |
434 | 0 | const char *compat; |
435 | 0 | struct property *prop; |
436 | 0 |
|
437 | 0 | of_property_for_each_string(overlay, "compatible", prop, compat) |
438 | 0 | if (of_device_is_compatible(root, compat)) |
439 | 0 | return compat; |
440 | 0 |
|
441 | 0 | return NULL; |
442 | 0 | } |
443 | | |
444 | | extern const void * const __barebox_of_overlay_start[]; |
445 | | extern const void * const __barebox_of_overlay_end[]; |
446 | | |
447 | | static __maybe_unused int of_overlay_apply_builtin_one(struct device_node *root, |
448 | | const void *dtbo) |
449 | 0 | { |
450 | 0 | struct device_node *overlay; |
451 | 0 | const char *compat; |
452 | 0 | int ret; |
453 | 0 |
|
454 | 0 | overlay = of_unflatten_dtb(dtbo, INT_MAX); |
455 | 0 | if (IS_ERR(overlay)) |
456 | 0 | return PTR_ERR(overlay); |
457 | 0 |
|
458 | 0 | ret = 0; |
459 | 0 |
|
460 | 0 | compat = of_overlay_matches_machine(root, overlay); |
461 | 0 | if (!compat) |
462 | 0 | goto out; |
463 | 0 |
|
464 | 0 | /* A tree from the boot firmware has no __symbols__ to resolve against */ |
465 | 0 | if (of_get_child_by_name(overlay, "__fixups__") && |
466 | 0 | !of_get_child_by_name(root, "__symbols__")) { |
467 | 0 | pr_info("skipping %s overlay: devicetree has no __symbols__\n", |
468 | 0 | compat); |
469 | 0 | goto out; |
470 | 0 | } |
471 | 0 |
|
472 | 0 | ret = __of_overlay_apply_tree(root, overlay, true); |
473 | 0 | if (ret == -ENODEV) { |
474 | 0 | pr_debug("skipping %s overlay: hardware not available\n", compat); |
475 | 0 | ret = 0; |
476 | 0 | } else if (ret) { |
477 | 0 | pr_err("cannot apply %s overlay: %pe\n", compat, ERR_PTR(ret)); |
478 | 0 | } |
479 | 0 | out: |
480 | 0 | of_delete_node(overlay); |
481 | 0 |
|
482 | 0 | return ret; |
483 | 0 | } |
484 | | |
485 | | #ifdef CONFIG_OF_OVERLAY_BUILTIN |
486 | | /** |
487 | | * of_overlay_apply_builtin - apply the overlay-y overlays @root asks for |
488 | | * @root: the devicetree to patch, normally the live tree |
489 | | */ |
490 | | int of_overlay_apply_builtin(struct device_node *root) |
491 | | { |
492 | | const void * const *dtbo; |
493 | | int err, ret = 0; |
494 | | |
495 | | for (dtbo = __barebox_of_overlay_start; |
496 | | dtbo < __barebox_of_overlay_end; dtbo++) { |
497 | | err = of_overlay_apply_builtin_one(root, *dtbo); |
498 | | if (err) |
499 | | ret = err; |
500 | | } |
501 | | |
502 | | return ret; |
503 | | } |
504 | | |
505 | | #endif |
506 | | |
507 | | static int of_overlay_fixup(struct device_node *root, void *data) |
508 | 0 | { |
509 | 0 | struct device_node *overlay = data; |
510 | |
|
511 | 0 | return of_overlay_apply_tree(root, overlay); |
512 | 0 | } |
513 | | |
514 | | /** |
515 | | * Iterate the overlay and call process for each fragment |
516 | | * |
517 | | * If process() fails for any fragment, the function will stop to process |
518 | | * other fragments and return the error of the failed process() call. |
519 | | */ |
520 | | int of_process_overlay(struct device_node *root, |
521 | | struct device_node *overlay, |
522 | | int (*process)(struct device_node *target, |
523 | | struct device_node *overlay, void *data), |
524 | | void *data) |
525 | 222 | { |
526 | 222 | struct device_node *fragment; |
527 | 222 | int err = 0; |
528 | | |
529 | 2.77k | for_each_child_of_node(overlay, fragment) { |
530 | 2.77k | struct device_node *ovl; |
531 | 2.77k | struct device_node *target; |
532 | | |
533 | 2.77k | ovl = of_get_child_by_name(fragment, "__overlay__"); |
534 | 2.77k | if (!ovl) |
535 | 2.77k | continue; |
536 | | |
537 | | /* A scan, not the apply: a missing target is reported there */ |
538 | 0 | target = find_target(root, fragment, true); |
539 | 0 | if (!target) |
540 | 0 | pr_debug("cannot find target for fragment %s\n", |
541 | 0 | fragment->name); |
542 | |
|
543 | 0 | err = process(target, ovl, data); |
544 | 0 | if (err) { |
545 | 0 | pr_warn("failed to process overlay for %s\n", |
546 | 0 | target ? target->name : "unknown"); |
547 | 0 | break; |
548 | 0 | } |
549 | 0 | } |
550 | | |
551 | 222 | return err; |
552 | 222 | } |
553 | | |
554 | | /** |
555 | | * Register a devicetree overlay |
556 | | * |
557 | | * The overlay is not applied to the live device tree, but registered as fixup |
558 | | * for the fixed up device tree. Therefore, drivers relying on the overlay |
559 | | * must use the fixed device tree. |
560 | | * |
561 | | * The fixup takes ownership of the overlay. |
562 | | */ |
563 | | int of_register_overlay(struct device_node *overlay) |
564 | 0 | { |
565 | 0 | return of_register_fixup(of_overlay_fixup, overlay); |
566 | 0 | } |
567 | | |
568 | | static char *of_overlay_pattern; |
569 | | static char *of_overlay_path; |
570 | | static char *of_overlay_basedir; |
571 | | |
572 | | /** |
573 | | * of_overlay_set_basedir - set the overlay basedir |
574 | | * @path: The new overlay basedir |
575 | | * |
576 | | * This specifies the base directory where overlay files are expected. By |
577 | | * default this is the root directory, but it is overwritten by blspec to |
578 | | * point to the rootfs of the about-to-be-booted system. |
579 | | */ |
580 | | void of_overlay_set_basedir(const char *path) |
581 | 30 | { |
582 | 30 | free(of_overlay_basedir); |
583 | 30 | of_overlay_basedir = strdup(path); |
584 | 30 | } |
585 | | |
586 | | static int of_overlay_apply_dir(struct device_node *root, const char *dirname, |
587 | | bool filter) |
588 | 0 | { |
589 | 0 | int ret = 0; |
590 | 0 | DIR *dir; |
591 | |
|
592 | 0 | if (!dirname || !*dirname) |
593 | 0 | return 0; |
594 | | |
595 | 0 | pr_debug("Applying overlays from %s\n", dirname); |
596 | |
|
597 | 0 | dir = opendir(dirname); |
598 | 0 | if (!dir) |
599 | 0 | return -errno; |
600 | | |
601 | 0 | while (1) { |
602 | 0 | struct dirent *ent; |
603 | 0 | char *filename; |
604 | |
|
605 | 0 | ent = readdir(dir); |
606 | 0 | if (!ent) |
607 | 0 | break; |
608 | | |
609 | 0 | if (!strcmp(dir->d.d_name, ".") || !strcmp(dir->d.d_name, "..")) |
610 | 0 | continue; |
611 | | |
612 | 0 | filename = basprintf("%s/%s", dirname, dir->d.d_name); |
613 | |
|
614 | 0 | of_overlay_apply_file(root, filename, filter); |
615 | |
|
616 | 0 | free(filename); |
617 | 0 | } |
618 | |
|
619 | 0 | closedir(dir); |
620 | |
|
621 | 0 | return ret; |
622 | 0 | } |
623 | | |
624 | | static int of_overlay_apply_fit(struct device_node *root, struct fit_handle *fit, |
625 | | struct device_node *config) |
626 | 0 | { |
627 | 0 | const char *name = config->name; |
628 | 0 | struct device_node *overlay; |
629 | 0 | unsigned long ovl_sz; |
630 | 0 | const void *ovl; |
631 | 0 | int ret; |
632 | |
|
633 | 0 | pr_debug("Process FIT config \"%s\"\n", name); |
634 | |
|
635 | 0 | if (!of_overlay_matches_filter(name, NULL)) |
636 | 0 | return 0; |
637 | | |
638 | 0 | ret = fit_open_image(fit, config, "fdt", 0, &ovl, &ovl_sz); |
639 | 0 | if (ret) |
640 | 0 | return ret; |
641 | | |
642 | 0 | overlay = of_unflatten_dtb(ovl, ovl_sz); |
643 | 0 | if (IS_ERR(overlay)) |
644 | 0 | return PTR_ERR(overlay); |
645 | | |
646 | 0 | if (!of_overlay_matches_filter(NULL, overlay)) { |
647 | 0 | ret = 0; |
648 | 0 | goto out; |
649 | 0 | } |
650 | | |
651 | 0 | ret = of_overlay_apply_tree(root, overlay); |
652 | 0 | if (ret == -ENODEV) |
653 | 0 | pr_debug("Not applied %s (not compatible)\n", name); |
654 | 0 | else if (ret) |
655 | 0 | pr_err("Cannot apply %s: %s\n", name, strerror(-ret)); |
656 | 0 | else |
657 | 0 | pr_info("Applied %s\n", name); |
658 | |
|
659 | 0 | out: |
660 | 0 | of_delete_node(overlay); |
661 | |
|
662 | 0 | return ret; |
663 | 0 | } |
664 | | |
665 | | static bool of_overlay_valid_config(struct fit_handle *fit, |
666 | | struct device_node *config) |
667 | 0 | { |
668 | | /* |
669 | | * Either kernel or firmware is marked as mandatory by U-Boot |
670 | | * (doc/usage/fit/source_file_format.rst) except for overlays |
671 | | * (doc/usage/fit/overlay-fdt-boot.rst). Therefore we need to ensure |
672 | | * that only "fdt" config nodes are recognized as overlay config node. |
673 | | */ |
674 | 0 | if (!fit_has_image(fit, config, "fdt") || |
675 | 0 | fit_has_image(fit, config, "kernel") || |
676 | 0 | fit_has_image(fit, config, "firmware")) |
677 | 0 | return false; |
678 | | |
679 | 0 | return true; |
680 | 0 | } |
681 | | |
682 | | static int of_overlay_global_fixup_fit(struct device_node *root, |
683 | | const char *fit_path) |
684 | 0 | { |
685 | 0 | enum bootm_verify verify = bootm_get_verify_mode(); |
686 | 0 | struct device_node *conf_node; |
687 | 0 | struct fit_handle *fit; |
688 | 0 | int ret = 0; |
689 | |
|
690 | 0 | if (!IS_ENABLED(CONFIG_FITIMAGE)) { |
691 | 0 | pr_err("FIT based overlay handling requested while CONFIG_FITIMAGE=n\n"); |
692 | 0 | return -EINVAL; |
693 | 0 | } |
694 | | |
695 | 0 | fit = fit_open(fit_path, 0, verify); |
696 | 0 | if (IS_ERR(fit)) { |
697 | 0 | pr_err("Loading FIT image %s failed with: %pe\n", fit_path, fit); |
698 | 0 | return PTR_ERR(fit); |
699 | 0 | } |
700 | | |
701 | 0 | for_each_child_of_node(fit->configurations, conf_node) { |
702 | 0 | if (!of_overlay_valid_config(fit, conf_node)) |
703 | 0 | continue; |
704 | | |
705 | 0 | ret = fit_config_verify_signature(fit, conf_node); |
706 | 0 | if (ret) |
707 | 0 | goto out; |
708 | | |
709 | 0 | ret = of_overlay_apply_fit(root, fit, conf_node); |
710 | 0 | if (ret) |
711 | 0 | goto out; |
712 | 0 | } |
713 | | |
714 | 0 | out: |
715 | 0 | fit_close(fit); |
716 | 0 | return ret; |
717 | 0 | } |
718 | | |
719 | | static int of_overlay_global_fixup(struct device_node *root, void *data) |
720 | 0 | { |
721 | 0 | struct stat s; |
722 | 0 | char *dir; |
723 | 0 | int ret; |
724 | |
|
725 | 0 | if (isempty(of_overlay_path)) |
726 | 0 | return 0; |
727 | | |
728 | 0 | if (*of_overlay_path == '/') |
729 | 0 | dir = xstrdup(of_overlay_path); |
730 | 0 | else |
731 | 0 | dir = concat_path_file(of_overlay_basedir, of_overlay_path); |
732 | | |
733 | |
|
734 | 0 | if (stat(dir, &s)) { |
735 | 0 | pr_err("Cannot stat global.of.overlay.path (%s): %m\n", dir); |
736 | 0 | ret = -errno; |
737 | 0 | goto out; |
738 | 0 | } |
739 | | |
740 | 0 | if (S_ISDIR(s.st_mode)) { |
741 | 0 | ret = of_overlay_apply_dir(root, dir, true); |
742 | 0 | goto out; |
743 | 0 | } |
744 | | |
745 | | /* Assume a FIT image if of_overlay_path points to a file */ |
746 | 0 | ret = of_overlay_global_fixup_fit(root, dir); |
747 | |
|
748 | 0 | out: |
749 | 0 | free(dir); |
750 | 0 | return ret; |
751 | 0 | } |
752 | | |
753 | | /** |
754 | | * of_overlay_register_filter - register a new overlay filter |
755 | | * @filter: The new filter |
756 | | * |
757 | | * Register a new overlay filter. A filter can either match on |
758 | | * a pattern or on the content of an overlay, but not on both. |
759 | | * If that's desired two filters have to be registered. |
760 | | * |
761 | | * @return: 0 for success, negative error code otherwise |
762 | | */ |
763 | | int of_overlay_register_filter(struct of_overlay_filter *filter) |
764 | 90 | { |
765 | 90 | if ((filter->filter_pattern || filter->filter_filename) && |
766 | 60 | filter->filter_content) |
767 | 0 | return -EINVAL; |
768 | | |
769 | 90 | list_add_tail(&filter->list, &of_overlay_filters); |
770 | | |
771 | 90 | return 0; |
772 | 90 | } |
773 | | |
774 | | /** |
775 | | * of_overlay_filter_pattern - A filter that matches on the filename or |
776 | | * FIT config-node name of an overlay |
777 | | * @f: The filter |
778 | | * @pattern: The filename or FIT config-node name of the overlay |
779 | | * |
780 | | * This filter matches when the filename or FIT config-node name matches one of |
781 | | * the patterns given in global.of.overlay.pattern. global.of.overlay.pattern |
782 | | * shall contain a space separated list of wildcard patterns. |
783 | | * |
784 | | * @return: True when the overlay shall be applied, false otherwise. |
785 | | */ |
786 | | static bool of_overlay_filter_pattern(struct of_overlay_filter *f, |
787 | | const char *pattern) |
788 | 0 | { |
789 | 0 | char *p, *path, *n; |
790 | 0 | int ret; |
791 | 0 | bool apply; |
792 | |
|
793 | 0 | p = path = strdup(of_overlay_pattern); |
794 | |
|
795 | 0 | while ((n = strsep_unescaped(&p, " ", NULL))) { |
796 | 0 | if (!*n) |
797 | 0 | continue; |
798 | | |
799 | 0 | ret = fnmatch(n, pattern, 0); |
800 | |
|
801 | 0 | if (!ret) { |
802 | 0 | apply = true; |
803 | 0 | goto out; |
804 | 0 | } |
805 | 0 | } |
806 | | |
807 | 0 | apply = false; |
808 | 0 | out: |
809 | 0 | free(path); |
810 | |
|
811 | 0 | return apply; |
812 | 0 | } |
813 | | |
814 | | static struct of_overlay_filter of_overlay_pattern_filter = { |
815 | | .name = "pattern", |
816 | | .filter_pattern = of_overlay_filter_pattern, |
817 | | }; |
818 | | |
819 | | static bool of_overlay_filter_filename(struct of_overlay_filter *f, |
820 | | const char *filename) |
821 | 0 | { |
822 | 0 | pr_warn("'filepattern' filter is marked as deprecated, convert to 'pattern' filter\n"); |
823 | 0 | return of_overlay_filter_pattern(f, filename); |
824 | 0 | } |
825 | | |
826 | | static struct of_overlay_filter of_overlay_filepattern_filter = { |
827 | | .name = "filepattern", |
828 | | .filter_pattern = of_overlay_filter_filename, |
829 | | }; |
830 | | |
831 | | /** |
832 | | * of_overlay_filter_compatible - A filter that matches on the compatible of |
833 | | * an overlay |
834 | | * @f: The filter |
835 | | * @ovl: The overlay |
836 | | * |
837 | | * This filter matches when the compatible of an overlay matches to one |
838 | | * of the compatibles given in global.of.overlay.compatible. When the |
839 | | * overlay doesn't contain a compatible entry it is considered matching. |
840 | | * Also when no compatibles are given in global.of.overlay.compatible |
841 | | * all overlays will match. |
842 | | * |
843 | | * @return: True when the overlay shall be applied, false otherwise. |
844 | | */ |
845 | | static bool of_overlay_filter_compatible(struct of_overlay_filter *f, |
846 | | struct device_node *ovl) |
847 | 0 | { |
848 | 0 | char *p, *n, *compatibles; |
849 | 0 | bool res = false; |
850 | |
|
851 | 0 | if (!of_overlay_compatible || !*of_overlay_compatible) |
852 | 0 | return true; |
853 | 0 | if (!of_find_property(ovl, "compatible", NULL)) |
854 | 0 | return true; |
855 | | |
856 | 0 | p = compatibles = xstrdup(of_overlay_compatible); |
857 | |
|
858 | 0 | while ((n = strsep_unescaped(&p, " ", NULL))) { |
859 | 0 | if (!*n) |
860 | 0 | continue; |
861 | | |
862 | 0 | if (of_device_is_compatible(ovl, n)) { |
863 | 0 | res = true; |
864 | 0 | break; |
865 | 0 | } |
866 | 0 | } |
867 | |
|
868 | 0 | free(compatibles); |
869 | |
|
870 | 0 | return res; |
871 | 0 | } |
872 | | |
873 | | static struct of_overlay_filter of_overlay_compatible_filter = { |
874 | | .name = "compatible", |
875 | | .filter_content = of_overlay_filter_compatible, |
876 | | }; |
877 | | |
878 | | static int fuzz_dtb_overlay(const u8 *data, size_t size) |
879 | 978 | { |
880 | 978 | struct device_node *overlay, *root; |
881 | | |
882 | 978 | overlay = of_unflatten_dtb(data, size); |
883 | 978 | if (IS_ERR(overlay)) |
884 | 633 | return 0; |
885 | | |
886 | 345 | root = of_new_node(NULL, NULL); |
887 | 345 | if (!root) { |
888 | 0 | of_delete_node(overlay); |
889 | 0 | return 0; |
890 | 0 | } |
891 | | |
892 | | /* |
893 | | * Applying to a tree of our own rather than to the live tree, so |
894 | | * that phandle resolution, symbol merging and fragment application |
895 | | * are all exercised without the result being visible elsewhere. |
896 | | */ |
897 | 345 | of_overlay_apply_tree(root, overlay); |
898 | | |
899 | 345 | of_delete_node(overlay); |
900 | 345 | of_delete_node(root); |
901 | | |
902 | 345 | return 0; |
903 | 345 | } |
904 | | fuzz_test("dtb-overlay", fuzz_dtb_overlay); |
905 | | |
906 | | static int of_overlay_init(void) |
907 | 30 | { |
908 | 30 | of_overlay_pattern = strdup("*"); |
909 | 30 | of_overlay_filter = strdup("pattern compatible"); |
910 | 30 | of_overlay_set_basedir("/"); |
911 | | |
912 | 30 | globalvar_add_simple_string("of.overlay.compatible", &of_overlay_compatible); |
913 | 30 | globalvar_add_simple_string("of.overlay.pattern", &of_overlay_pattern); |
914 | 30 | globalvar_add_simple_string("of.overlay.filter", &of_overlay_filter); |
915 | 30 | globalvar_add_simple_string("of.overlay.path", &of_overlay_path); |
916 | | |
917 | 30 | globalvar_alias_deprecated("of.overlay.filepattern", "of.overlay.pattern"); |
918 | 30 | globalvar_alias_deprecated("of.overlay.dir", "of.overlay.path"); |
919 | | |
920 | 30 | of_overlay_register_filter(&of_overlay_pattern_filter); |
921 | 30 | of_overlay_register_filter(&of_overlay_filepattern_filter); |
922 | 30 | of_overlay_register_filter(&of_overlay_compatible_filter); |
923 | | |
924 | 30 | of_register_fixup(of_overlay_global_fixup, NULL); |
925 | | |
926 | 30 | return 0; |
927 | 30 | } |
928 | | device_initcall(of_overlay_init); |
929 | | |
930 | | BAREBOX_MAGICVAR(global.of.overlay.compatible, "space separated list of compatibles an overlay must match"); |
931 | | BAREBOX_MAGICVAR(global.of.overlay.pattern, "space separated list of filename or fit config-node name patterns an overlay must match"); |
932 | | BAREBOX_MAGICVAR(global.of.overlay.path, "Path to look for dt overlays or a path to a FIT image"); |
933 | | BAREBOX_MAGICVAR(global.of.overlay.filter, "space separated list of filters"); |