2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 /* These are the operations we support */
33 OPER_WRITE_PROP
, /* Write a property in a node */
34 OPER_CREATE_NODE
, /* Create a new node */
38 enum oper_type oper
; /* operation to perform */
39 int type
; /* data type (s/i/u/x or 0 for default) */
40 int size
; /* data size (1/2/4) */
41 int verbose
; /* verbose output */
42 int auto_path
; /* automatically create all path components */
47 * Report an error with a particular node.
49 * @param name Node name to report error on
50 * @param namelen Length of node name, or -1 to use entire string
51 * @param err Error number to report (-FDT_ERR_...)
53 static void report_error(const char *name
, int namelen
, int err
)
56 namelen
= strlen(name
);
57 fprintf(stderr
, "Error at '%1.*s': %s\n", namelen
, name
,
62 * Encode a series of arguments in a property value.
64 * @param disp Display information / options
65 * @param arg List of arguments from command line
66 * @param arg_count Number of arguments (may be 0)
67 * @param valuep Returns buffer containing value
68 * @param *value_len Returns length of value encoded
70 static int encode_value(struct display_info
*disp
, char **arg
, int arg_count
,
71 char **valuep
, int *value_len
)
73 char *value
= NULL
; /* holding area for value */
74 int value_size
= 0; /* size of holding area */
75 char *ptr
; /* pointer to current value position */
76 int len
; /* length of this cell/string/byte */
78 int upto
; /* the number of bytes we have written to buf */
84 fprintf(stderr
, "Decoding value:\n");
87 fmt
[1] = disp
->type
? disp
->type
: 'd';
89 for (; arg_count
> 0; arg
++, arg_count
--, upto
+= len
) {
90 /* assume integer unless told otherwise */
91 if (disp
->type
== 's')
92 len
= strlen(*arg
) + 1;
94 len
= disp
->size
== -1 ? 4 : disp
->size
;
96 /* enlarge our value buffer by a suitable margin if needed */
97 if (upto
+ len
> value_size
) {
98 value_size
= (upto
+ len
) + 500;
99 value
= realloc(value
, value_size
);
101 fprintf(stderr
, "Out of mmory: cannot alloc "
102 "%d bytes\n", value_size
);
108 if (disp
->type
== 's') {
109 memcpy(ptr
, *arg
, len
);
111 fprintf(stderr
, "\tstring: '%s'\n", ptr
);
113 int *iptr
= (int *)ptr
;
114 sscanf(*arg
, fmt
, &ival
);
116 *iptr
= cpu_to_fdt32(ival
);
118 *ptr
= (uint8_t)ival
;
120 fprintf(stderr
, "\t%s: %d\n",
121 disp
->size
== 1 ? "byte" :
122 disp
->size
== 2 ? "short" : "int",
130 fprintf(stderr
, "Value size %d\n", upto
);
134 static int store_key_value(void *blob
, const char *node_name
,
135 const char *property
, const char *buf
, int len
)
140 node
= fdt_path_offset(blob
, node_name
);
142 report_error(node_name
, -1, node
);
146 err
= fdt_setprop(blob
, node
, property
, buf
, len
);
148 report_error(property
, -1, err
);
155 * Create paths as needed for all components of a path
157 * Any components of the path that do not exist are created. Errors are
160 * @param blob FDT blob to write into
161 * @param in_path Path to process
162 * @return 0 if ok, -1 on error
164 static int create_paths(void *blob
, const char *in_path
)
166 const char *path
= in_path
;
168 int node
, offset
= 0;
170 /* skip leading '/' */
174 for (sep
= path
; *sep
; path
= sep
+ 1, offset
= node
) {
175 /* equivalent to strchrnul(), but it requires _GNU_SOURCE */
176 sep
= strchr(path
, '/');
178 sep
= path
+ strlen(path
);
180 node
= fdt_subnode_offset_namelen(blob
, offset
, path
,
182 if (node
== -FDT_ERR_NOTFOUND
) {
183 node
= fdt_add_subnode_namelen(blob
, offset
, path
,
187 report_error(path
, sep
- path
, node
);
196 * Create a new node in the fdt.
198 * This will overwrite the node_name string. Any error is reported.
200 * TODO: Perhaps create fdt_path_offset_namelen() so we don't need to do this.
202 * @param blob FDT blob to write into
203 * @param node_name Name of node to create
204 * @return new node offset if found, or -1 on failure
206 static int create_node(void *blob
, const char *node_name
)
211 p
= strrchr(node_name
, '/');
213 report_error(node_name
, -1, -FDT_ERR_BADPATH
);
219 node
= fdt_path_offset(blob
, node_name
);
221 report_error(node_name
, -1, node
);
226 node
= fdt_add_subnode(blob
, node
, p
+ 1);
228 report_error(p
+ 1, -1, node
);
235 static int do_fdtput(struct display_info
*disp
, const char *filename
,
236 char **arg
, int arg_count
)
242 blob
= utilfdt_read(filename
);
246 switch (disp
->oper
) {
247 case OPER_WRITE_PROP
:
249 * Convert the arguments into a single binary value, then
250 * store them into the property.
252 assert(arg_count
>= 2);
253 if (disp
->auto_path
&& create_paths(blob
, *arg
))
255 if (encode_value(disp
, arg
+ 2, arg_count
- 2, &value
, &len
) ||
256 store_key_value(blob
, *arg
, arg
[1], value
, len
))
259 case OPER_CREATE_NODE
:
260 for (; ret
>= 0 && arg_count
--; arg
++) {
262 ret
= create_paths(blob
, *arg
);
264 ret
= create_node(blob
, *arg
);
269 ret
= utilfdt_write(filename
, blob
);
275 static const char *usage_msg
=
276 "fdtput - write a property value to a device tree\n"
278 "The command line arguments are joined together into a single value.\n"
281 " fdtput <options> <dt file> <node> <property> [<value>...]\n"
282 " fdtput -c <options> <dt file> [<node>...]\n"
284 "\t-c\t\tCreate nodes if they don't already exist\n"
285 "\t-p\t\tAutomatically create nodes as needed for the node path\n"
286 "\t-t <type>\tType of data\n"
287 "\t-v\t\tVerbose: display each value decoded from command line\n"
288 "\t-h\t\tPrint this help\n\n"
291 static void usage(const char *msg
)
294 fprintf(stderr
, "Error: %s\n\n", msg
);
296 fprintf(stderr
, "%s", usage_msg
);
300 int main(int argc
, char *argv
[])
302 struct display_info disp
;
303 char *filename
= NULL
;
305 memset(&disp
, '\0', sizeof(disp
));
307 disp
.oper
= OPER_WRITE_PROP
;
309 int c
= getopt(argc
, argv
, "chpt:v");
314 * TODO: add options to:
316 * - delete node (optionally recursively)
318 * - pack fdt before writing
319 * - set amount of free space when writing
320 * - expand fdt if value doesn't fit
324 disp
.oper
= OPER_CREATE_NODE
;
333 if (utilfdt_decode_type(optarg
, &disp
.type
,
335 usage("Invalid type string");
345 filename
= argv
[optind
++];
347 usage("Missing filename");
352 if (disp
.oper
== OPER_WRITE_PROP
) {
354 usage("Missing node");
356 usage("Missing property");
359 if (do_fdtput(&disp
, filename
, argv
, argc
))