/src/barebox/commands/of_property.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2013 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | /* of_property.c - device tree property handling support */ |
5 | | |
6 | | #include <common.h> |
7 | | #include <libfile.h> |
8 | | #include <environment.h> |
9 | | #include <fdt.h> |
10 | | #include <of.h> |
11 | | #include <command.h> |
12 | | #include <fs.h> |
13 | | #include <malloc.h> |
14 | | #include <complete.h> |
15 | | #include <linux/ctype.h> |
16 | | #include <asm/byteorder.h> |
17 | | #include <errno.h> |
18 | | #include <getopt.h> |
19 | | #include <init.h> |
20 | | #include <xfuncs.h> |
21 | | |
22 | | /* |
23 | | * With @resolve false, node paths are only checked for syntax and |
24 | | * converted to 0 instead of a phandle. |
25 | | */ |
26 | | static int of_parse_prop_cells(struct device_node *root, bool resolve, |
27 | | char * const *newval, int count, char *data, int *len) |
28 | 0 | { |
29 | 0 | char *cp; |
30 | 0 | unsigned long tmp; /* holds converted values */ |
31 | 0 | int stridx = 0; |
32 | 0 | char *newp = newval[0]; |
33 | |
|
34 | 0 | newp++; |
35 | |
|
36 | 0 | while (1) { |
37 | 0 | if (*newp == '>') |
38 | 0 | return 0; |
39 | | |
40 | 0 | if (*newp == '\0') { |
41 | 0 | newp = newval[++stridx]; |
42 | |
|
43 | 0 | if (stridx == count) { |
44 | 0 | printf("missing '>'\n"); |
45 | 0 | return -EINVAL; |
46 | 0 | } |
47 | | |
48 | 0 | continue; |
49 | 0 | } |
50 | | |
51 | 0 | cp = newp; |
52 | |
|
53 | 0 | if (isdigit(*cp)) { |
54 | 0 | tmp = simple_strtoul(cp, &newp, 0); |
55 | 0 | } else { |
56 | 0 | struct device_node *n; |
57 | 0 | char *str; |
58 | 0 | int len = 0; |
59 | |
|
60 | 0 | str = cp; |
61 | 0 | while (*str && *str != '>' && *str != ' ') { |
62 | 0 | str++; |
63 | 0 | len++; |
64 | 0 | } |
65 | |
|
66 | 0 | if (!resolve) { |
67 | 0 | tmp = 0; |
68 | 0 | newp += len; |
69 | 0 | goto next; |
70 | 0 | } |
71 | | |
72 | 0 | str = xzalloc(len + 1); |
73 | 0 | strncpy(str, cp, len); |
74 | |
|
75 | 0 | n = of_find_node_by_path_or_alias(root, str); |
76 | 0 | if (!n) |
77 | 0 | printf("Cannot find node '%s'\n", str); |
78 | |
|
79 | 0 | free(str); |
80 | |
|
81 | 0 | if (!n) |
82 | 0 | return -EINVAL; |
83 | | |
84 | 0 | tmp = of_node_create_phandle(n); |
85 | 0 | newp += len; |
86 | 0 | } |
87 | 0 | next: |
88 | 0 | *(__be32 *)data = __cpu_to_be32(tmp); |
89 | 0 | data += 4; |
90 | 0 | *len += 4; |
91 | | |
92 | | /* If the ptr didn't advance, something went wrong */ |
93 | 0 | if ((newp - cp) <= 0) { |
94 | 0 | printf("cannot convert \"%s\"\n", cp); |
95 | 0 | return -EINVAL; |
96 | 0 | } |
97 | | |
98 | 0 | while (*newp == ' ') |
99 | 0 | newp++; |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | | static int of_parse_prop_stream(char * const *newval, int count, char *data, int *len) |
104 | 0 | { |
105 | 0 | char *cp; |
106 | 0 | unsigned long tmp; /* holds converted values */ |
107 | 0 | int stridx = 0; |
108 | 0 | char *newp = newval[0]; |
109 | |
|
110 | 0 | newp++; |
111 | |
|
112 | 0 | while (1) { |
113 | 0 | if (*newp == ']') |
114 | 0 | return 0; |
115 | | |
116 | 0 | while (*newp == ' ') |
117 | 0 | newp++; |
118 | |
|
119 | 0 | if (*newp == '\0') { |
120 | 0 | newp = newval[++stridx]; |
121 | |
|
122 | 0 | if (stridx == count) { |
123 | 0 | printf("missing ']'\n"); |
124 | 0 | return -EINVAL; |
125 | 0 | } |
126 | | |
127 | 0 | continue; |
128 | 0 | } |
129 | | |
130 | 0 | cp = newp; |
131 | 0 | tmp = simple_strtoul(newp, &newp, 16); |
132 | 0 | *data++ = tmp & 0xff; |
133 | 0 | *len = *len + 1; |
134 | | |
135 | | /* If the ptr didn't advance, something went wrong */ |
136 | 0 | if ((newp - cp) <= 0) { |
137 | 0 | printf("cannot convert \"%s\"\n", cp); |
138 | 0 | return -EINVAL; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | static int of_parse_prop_string(char * const *newval, int count, char *data, int *len) |
144 | 0 | { |
145 | 0 | int stridx = 0; |
146 | 0 | char *newp = newval[0]; |
147 | | |
148 | | /* |
149 | | * Assume it is one or more strings. Copy it into our |
150 | | * data area for convenience (including the |
151 | | * terminating '\0's). |
152 | | */ |
153 | 0 | while (stridx < count) { |
154 | 0 | size_t length = strlen(newp) + 1; |
155 | |
|
156 | 0 | strcpy(data, newp); |
157 | 0 | data += length; |
158 | 0 | *len += length; |
159 | 0 | newp = newval[++stridx]; |
160 | 0 | } |
161 | |
|
162 | 0 | return 0; |
163 | 0 | } |
164 | | |
165 | | /* |
166 | | * Parse the user's input, partially heuristic. Valid formats: |
167 | | * <0x00112233 4 05> - an array of cells. Numbers follow standard |
168 | | * C conventions. |
169 | | * [00 11 22 .. nn] - byte stream |
170 | | * "string" - If the the value doesn't start with "<" or "[", it is |
171 | | * treated as a string. Note that the quotes are |
172 | | * stripped by the parser before we get the string. |
173 | | * newval: An array of strings containing the new property as specified |
174 | | * on the command line |
175 | | * count: The number of strings in the array |
176 | | * data: A bytestream to be placed in the property |
177 | | * len: The length of the resulting bytestream |
178 | | */ |
179 | | static int of_parse_prop(struct device_node *root, bool resolve, |
180 | | char * const *newval, int count, char *data, int *len) |
181 | 0 | { |
182 | 0 | char *newp; /* temporary newval char pointer */ |
183 | |
|
184 | 0 | *len = 0; |
185 | |
|
186 | 0 | if (!count) |
187 | 0 | return 0; |
188 | | |
189 | 0 | newp = newval[0]; |
190 | |
|
191 | 0 | switch (*newp) { |
192 | 0 | case '<': |
193 | 0 | return of_parse_prop_cells(root, resolve, newval, count, data, len); |
194 | 0 | case '[': |
195 | 0 | return of_parse_prop_stream(newval, count, data, len); |
196 | 0 | default: |
197 | 0 | return of_parse_prop_string(newval, count, data, len); |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | struct of_fixup_property_data { |
202 | | const char *path; |
203 | | const char *propname; |
204 | | char **values; |
205 | | int count; |
206 | | }; |
207 | | |
208 | | static int do_of_property_delete_now(struct device_node *root, const char *path, |
209 | | const char *propname); |
210 | | static int do_of_property_set_now(struct device_node *root, const char *path, |
211 | | const char *propname, void *data, int len); |
212 | | |
213 | | static int of_fixup_property_set(struct device_node *root, void *context) |
214 | 0 | { |
215 | 0 | struct of_fixup_property_data *fixup = context; |
216 | 0 | void *data; |
217 | 0 | int len, ret; |
218 | | |
219 | | /* parse now, so node paths resolve against the tree being booted */ |
220 | 0 | data = xmalloc(1024); |
221 | |
|
222 | 0 | ret = of_parse_prop(root, true, fixup->values, fixup->count, data, &len); |
223 | 0 | if (!ret) |
224 | 0 | ret = do_of_property_set_now(root, fixup->path, fixup->propname, |
225 | 0 | data, len); |
226 | |
|
227 | 0 | free(data); |
228 | |
|
229 | 0 | return ret; |
230 | 0 | } |
231 | | |
232 | | static int of_fixup_property_delete(struct device_node *root, void *context) |
233 | 0 | { |
234 | 0 | struct of_fixup_property_data *fixup = context; |
235 | |
|
236 | 0 | return do_of_property_delete_now(root, fixup->path, fixup->propname); |
237 | 0 | } |
238 | | |
239 | | static int do_of_property_set_fixup(const char *path, const char *propname, |
240 | | char * const *values, int count) |
241 | 0 | { |
242 | 0 | struct of_fixup_property_data *fixup; |
243 | 0 | int i; |
244 | |
|
245 | 0 | fixup = xzalloc(sizeof(*fixup)); |
246 | 0 | fixup->path = xstrdup_const(path); |
247 | 0 | fixup->propname = xstrdup_const(propname); |
248 | 0 | fixup->values = xzalloc(count * sizeof(*fixup->values)); |
249 | 0 | fixup->count = count; |
250 | |
|
251 | 0 | for (i = 0; i < count; i++) |
252 | 0 | fixup->values[i] = xstrdup(values[i]); |
253 | |
|
254 | 0 | return of_register_fixup(of_fixup_property_set, (void *)fixup); |
255 | 0 | } |
256 | | |
257 | | static int do_of_property_delete_fixup(const char *path, const char *propname) |
258 | 0 | { |
259 | 0 | struct of_fixup_property_data *fixup; |
260 | |
|
261 | 0 | fixup = xzalloc(sizeof(*fixup)); |
262 | 0 | fixup->path = xstrdup_const(path); |
263 | 0 | fixup->propname = xstrdup_const(propname); |
264 | |
|
265 | 0 | return of_register_fixup(of_fixup_property_delete, (void *)fixup); |
266 | 0 | } |
267 | | |
268 | | static int do_of_property_set_now(struct device_node *root, const char *path, |
269 | | const char *propname, void *data, int len) |
270 | 0 | { |
271 | 0 | struct device_node *node = of_find_node_by_path_or_alias(root, path); |
272 | 0 | struct property *pp; |
273 | |
|
274 | 0 | if (!node) { |
275 | 0 | printf("Cannot find nodepath %s\n", path); |
276 | 0 | return -ENOENT; |
277 | 0 | } |
278 | | |
279 | 0 | pp = of_find_property(node, propname, NULL); |
280 | |
|
281 | 0 | if (pp) { |
282 | 0 | free(pp->value); |
283 | 0 | pp->value_const = NULL; |
284 | |
|
285 | 0 | if (len) |
286 | 0 | pp->value = xmemdup(data, len); |
287 | 0 | else |
288 | 0 | pp->value = NULL; |
289 | |
|
290 | 0 | pp->length = len; |
291 | 0 | } else { |
292 | 0 | pp = of_new_property(node, propname, data, len); |
293 | 0 | if (!pp) { |
294 | 0 | printf("Cannot create property %s\n", propname); |
295 | 0 | return -ENOMEM; |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | 0 | return 0; |
300 | 0 | } |
301 | | |
302 | | static int do_of_property_delete_now(struct device_node *root, const char *path, |
303 | | const char *propname) |
304 | 0 | { |
305 | 0 | struct device_node *node = of_find_node_by_path_or_alias(root, path); |
306 | 0 | struct property *pp; |
307 | |
|
308 | 0 | if (!node) { |
309 | 0 | printf("Cannot find nodepath %s\n", path); |
310 | 0 | return -ENOENT; |
311 | 0 | } |
312 | | |
313 | 0 | pp = of_find_property(node, propname, NULL); |
314 | 0 | if (!pp) { |
315 | 0 | printf("Cannot find property %s\n", propname); |
316 | 0 | return -ENOENT; |
317 | 0 | } |
318 | | |
319 | 0 | of_delete_property(pp); |
320 | |
|
321 | 0 | return 0; |
322 | 0 | } |
323 | | |
324 | | static int do_of_property(int argc, char *argv[]) |
325 | 0 | { |
326 | 0 | int opt; |
327 | 0 | int delete = 0; |
328 | 0 | int set = 0; |
329 | 0 | int fixup = 0; |
330 | 0 | char *path, *propname; |
331 | 0 | char *dtbfile = NULL; |
332 | 0 | int ret = 0; |
333 | 0 | struct fdt_header *fdt = NULL; |
334 | 0 | struct device_node *root = NULL; |
335 | |
|
336 | 0 | while ((opt = getopt(argc, argv, "dsfe:")) > 0) { |
337 | 0 | switch (opt) { |
338 | 0 | case 'd': |
339 | 0 | delete = 1; |
340 | 0 | break; |
341 | 0 | case 's': |
342 | 0 | set = 1; |
343 | 0 | break; |
344 | 0 | case 'f': |
345 | 0 | fixup = 1; |
346 | 0 | break; |
347 | 0 | case 'e': |
348 | 0 | dtbfile = optarg; |
349 | 0 | break; |
350 | 0 | default: |
351 | 0 | return COMMAND_ERROR_USAGE; |
352 | 0 | } |
353 | 0 | } |
354 | | |
355 | 0 | if (argc - optind < 2) |
356 | 0 | return COMMAND_ERROR_USAGE; |
357 | | |
358 | 0 | path = argv[optind]; |
359 | 0 | propname = argv[optind + 1]; |
360 | |
|
361 | 0 | debug("path: %s propname: %s\n", path, propname); |
362 | |
|
363 | 0 | if ( !(set || delete)) |
364 | 0 | return COMMAND_ERROR_USAGE; |
365 | | |
366 | | /* fixups apply to the booted tree; -e is accepted but ignored */ |
367 | 0 | if (dtbfile && !fixup) { |
368 | 0 | root = of_read_file(dtbfile); |
369 | 0 | if (IS_ERR(root)) |
370 | 0 | return PTR_ERR(root); |
371 | 0 | } |
372 | | |
373 | 0 | if (set) { |
374 | 0 | int len; |
375 | 0 | void *data; |
376 | | |
377 | | /* |
378 | | * standard console buffer size. The result won't be bigger than the |
379 | | * string input. |
380 | | */ |
381 | 0 | data = malloc(1024); |
382 | 0 | if (!data) |
383 | 0 | return -ENOMEM; |
384 | | |
385 | 0 | ret = of_parse_prop(root, !fixup, &argv[optind + 2], |
386 | 0 | argc - optind - 2, data, &len); |
387 | 0 | if (ret) { |
388 | 0 | free(data); |
389 | 0 | goto out; |
390 | 0 | } |
391 | | |
392 | 0 | if (fixup) |
393 | 0 | ret = do_of_property_set_fixup(path, propname, |
394 | 0 | &argv[optind + 2], argc - optind - 2); |
395 | 0 | else |
396 | 0 | ret = do_of_property_set_now(root, path, propname, data, len); |
397 | |
|
398 | 0 | free(data); |
399 | |
|
400 | 0 | if (ret) |
401 | 0 | goto out; |
402 | |
|
403 | 0 | } else if (delete) { |
404 | 0 | if (fixup) |
405 | 0 | ret = do_of_property_delete_fixup(path, propname); |
406 | 0 | else |
407 | 0 | ret = do_of_property_delete_now(root, path, propname); |
408 | |
|
409 | 0 | if (ret) |
410 | 0 | goto out; |
411 | 0 | } |
412 | | |
413 | 0 | if (root && !fixup) { |
414 | 0 | fdt = of_flatten_dtb(root); |
415 | 0 | if (!fdt) { |
416 | 0 | ret = -EINVAL; |
417 | 0 | goto out; |
418 | 0 | } |
419 | | |
420 | 0 | ret = write_file(dtbfile, fdt, fdt32_to_cpu(fdt->totalsize)); |
421 | |
|
422 | 0 | free(fdt); |
423 | 0 | } |
424 | | |
425 | 0 | out: |
426 | |
|
427 | 0 | of_delete_node(root); |
428 | |
|
429 | 0 | return ret; |
430 | 0 | } |
431 | | |
432 | | BAREBOX_CMD_HELP_START(of_property) |
433 | | BAREBOX_CMD_HELP_TEXT("Options:") |
434 | | BAREBOX_CMD_HELP_OPT ("-s", "set property to value") |
435 | | BAREBOX_CMD_HELP_OPT ("-d", "delete property") |
436 | | BAREBOX_CMD_HELP_OPT ("-f", "set/delete as a fixup (defer the action until booting)") |
437 | | BAREBOX_CMD_HELP_OPT ("-e dtb", "set/delete in external dtb (ignored with -f)") |
438 | | BAREBOX_CMD_HELP_TEXT("") |
439 | | BAREBOX_CMD_HELP_TEXT("Valid formats for values:") |
440 | | BAREBOX_CMD_HELP_TEXT("<0x00112233 4 05> - an array of cells. cells not beginning with a digit are") |
441 | | BAREBOX_CMD_HELP_TEXT(" interpreted as node paths and converted to phandles.") |
442 | | BAREBOX_CMD_HELP_TEXT(" With -f, the paths are looked up in the booted tree") |
443 | | BAREBOX_CMD_HELP_TEXT("[00 11 22 .. nn] - byte stream") |
444 | | BAREBOX_CMD_HELP_TEXT("If the value does not start with '<' or '[' it is interpreted as string") |
445 | | BAREBOX_CMD_HELP_END |
446 | | |
447 | | BAREBOX_CMD_START(of_property) |
448 | | .cmd = do_of_property, |
449 | | BAREBOX_CMD_DESC("handle device tree properties") |
450 | | BAREBOX_CMD_OPTS("[-sd] [-e] [-f] NODE [PROPERTY] [VALUES]") |
451 | | BAREBOX_CMD_GROUP(CMD_GRP_MISC) |
452 | | BAREBOX_CMD_COMPLETE(devicetree_file_complete) |
453 | | BAREBOX_CMD_HELP(cmd_of_property_help) |
454 | | BAREBOX_CMD_END |