1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
17 /* These are the operations we support */
19 OPER_WRITE_PROP
, /* Write a property in a node */
20 OPER_CREATE_NODE
, /* Create a new node */
24 enum oper_type oper
; /* operation to perform */
25 int type
; /* data type (s/i/u/x or 0 for default) */
26 int size
; /* data size (1/2/4) */
27 int verbose
; /* verbose output */
28 int auto_path
; /* automatically create all path components */
33 * Report an error with a particular node.
35 * @param name Node name to report error on
36 * @param namelen Length of node name, or -1 to use entire string
37 * @param err Error number to report (-FDT_ERR_...)
39 static void report_error(const char *name
, int namelen
, int err
)
42 namelen
= strlen(name
);
43 fprintf(stderr
, "Error at '%1.*s': %s\n", namelen
, name
,
48 * Encode a series of arguments in a property value.
50 * @param disp Display information / options
51 * @param arg List of arguments from command line
52 * @param arg_count Number of arguments (may be 0)
53 * @param valuep Returns buffer containing value
54 * @param *value_len Returns length of value encoded
56 static int encode_value(struct display_info
*disp
, char **arg
, int arg_count
,
57 char **valuep
, int *value_len
)
59 char *value
= NULL
; /* holding area for value */
60 int value_size
= 0; /* size of holding area */
61 char *ptr
; /* pointer to current value position */
62 int len
; /* length of this cell/string/byte */
64 int upto
; /* the number of bytes we have written to buf */
70 fprintf(stderr
, "Decoding value:\n");
73 fmt
[1] = disp
->type
? disp
->type
: 'd';
75 for (; arg_count
> 0; arg
++, arg_count
--, upto
+= len
) {
76 /* assume integer unless told otherwise */
77 if (disp
->type
== 's')
78 len
= strlen(*arg
) + 1;
80 len
= disp
->size
== -1 ? 4 : disp
->size
;
82 /* enlarge our value buffer by a suitable margin if needed */
83 if (upto
+ len
> value_size
) {
84 value_size
= (upto
+ len
) + 500;
85 value
= realloc(value
, value_size
);
87 fprintf(stderr
, "Out of mmory: cannot alloc "
88 "%d bytes\n", value_size
);
94 if (disp
->type
== 's') {
95 memcpy(ptr
, *arg
, len
);
97 fprintf(stderr
, "\tstring: '%s'\n", ptr
);
99 int *iptr
= (int *)ptr
;
100 sscanf(*arg
, fmt
, &ival
);
102 *iptr
= cpu_to_fdt32(ival
);
104 *ptr
= (uint8_t)ival
;
106 fprintf(stderr
, "\t%s: %d\n",
107 disp
->size
== 1 ? "byte" :
108 disp
->size
== 2 ? "short" : "int",
116 fprintf(stderr
, "Value size %d\n", upto
);
120 static int store_key_value(void *blob
, const char *node_name
,
121 const char *property
, const char *buf
, int len
)
126 node
= fdt_path_offset(blob
, node_name
);
128 report_error(node_name
, -1, node
);
132 err
= fdt_setprop(blob
, node
, property
, buf
, len
);
134 report_error(property
, -1, err
);
141 * Create paths as needed for all components of a path
143 * Any components of the path that do not exist are created. Errors are
146 * @param blob FDT blob to write into
147 * @param in_path Path to process
148 * @return 0 if ok, -1 on error
150 static int create_paths(void *blob
, const char *in_path
)
152 const char *path
= in_path
;
154 int node
, offset
= 0;
156 /* skip leading '/' */
160 for (sep
= path
; *sep
; path
= sep
+ 1, offset
= node
) {
161 /* equivalent to strchrnul(), but it requires _GNU_SOURCE */
162 sep
= strchr(path
, '/');
164 sep
= path
+ strlen(path
);
166 node
= fdt_subnode_offset_namelen(blob
, offset
, path
,
168 if (node
== -FDT_ERR_NOTFOUND
) {
169 node
= fdt_add_subnode_namelen(blob
, offset
, path
,
173 report_error(path
, sep
- path
, node
);
182 * Create a new node in the fdt.
184 * This will overwrite the node_name string. Any error is reported.
186 * TODO: Perhaps create fdt_path_offset_namelen() so we don't need to do this.
188 * @param blob FDT blob to write into
189 * @param node_name Name of node to create
190 * @return new node offset if found, or -1 on failure
192 static int create_node(void *blob
, const char *node_name
)
197 p
= strrchr(node_name
, '/');
199 report_error(node_name
, -1, -FDT_ERR_BADPATH
);
205 node
= fdt_path_offset(blob
, node_name
);
207 report_error(node_name
, -1, node
);
212 node
= fdt_add_subnode(blob
, node
, p
+ 1);
214 report_error(p
+ 1, -1, node
);
221 static int do_fdtput(struct display_info
*disp
, const char *filename
,
222 char **arg
, int arg_count
)
228 blob
= utilfdt_read(filename
);
232 switch (disp
->oper
) {
233 case OPER_WRITE_PROP
:
235 * Convert the arguments into a single binary value, then
236 * store them into the property.
238 assert(arg_count
>= 2);
239 if (disp
->auto_path
&& create_paths(blob
, *arg
))
241 if (encode_value(disp
, arg
+ 2, arg_count
- 2, &value
, &len
) ||
242 store_key_value(blob
, *arg
, arg
[1], value
, len
))
245 case OPER_CREATE_NODE
:
246 for (; ret
>= 0 && arg_count
--; arg
++) {
248 ret
= create_paths(blob
, *arg
);
250 ret
= create_node(blob
, *arg
);
255 ret
= utilfdt_write(filename
, blob
);
261 static const char *usage_msg
=
262 "fdtput - write a property value to a device tree\n"
264 "The command line arguments are joined together into a single value.\n"
267 " fdtput <options> <dt file> <node> <property> [<value>...]\n"
268 " fdtput -c <options> <dt file> [<node>...]\n"
270 "\t-c\t\tCreate nodes if they don't already exist\n"
271 "\t-p\t\tAutomatically create nodes as needed for the node path\n"
272 "\t-t <type>\tType of data\n"
273 "\t-v\t\tVerbose: display each value decoded from command line\n"
274 "\t-h\t\tPrint this help\n\n"
277 static void usage(const char *msg
)
280 fprintf(stderr
, "Error: %s\n\n", msg
);
282 fprintf(stderr
, "%s", usage_msg
);
286 int main(int argc
, char *argv
[])
288 struct display_info disp
;
289 char *filename
= NULL
;
291 memset(&disp
, '\0', sizeof(disp
));
293 disp
.oper
= OPER_WRITE_PROP
;
295 int c
= getopt(argc
, argv
, "chpt:v");
300 * TODO: add options to:
302 * - delete node (optionally recursively)
304 * - pack fdt before writing
305 * - set amount of free space when writing
306 * - expand fdt if value doesn't fit
310 disp
.oper
= OPER_CREATE_NODE
;
319 if (utilfdt_decode_type(optarg
, &disp
.type
,
321 usage("Invalid type string");
331 filename
= argv
[optind
++];
333 usage("Missing filename");
338 if (disp
.oper
== OPER_WRITE_PROP
) {
340 usage("Missing node");
342 usage("Missing property");
345 if (do_fdtput(&disp
, filename
, argv
, argc
))