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 #define ALIGN(x) (((x) + (FDT_TAGSIZE) - 1) & ~((FDT_TAGSIZE) - 1))
136 static char *_realloc_fdt(char *fdt, int delta)
138 int new_sz = fdt_totalsize(fdt) + delta;
139 fdt = xrealloc(fdt, new_sz);
140 fdt_open_into(fdt, fdt, new_sz);
144 static char *realloc_node(char *fdt, const char *name)
147 /* FDT_BEGIN_NODE, node name in off_struct and FDT_END_NODE */
148 delta = sizeof(struct fdt_node_header) + ALIGN(strlen(name) + 1)
150 return _realloc_fdt(fdt, delta);
153 static char *realloc_property(char *fdt, int nodeoffset,
154 const char *name, int newlen)
159 if (!fdt_get_property(fdt, nodeoffset, name, &oldlen))
160 /* strings + property header */
161 delta = sizeof(struct fdt_property) + strlen(name) + 1;
164 /* actual value in off_struct */
165 delta += ALIGN(newlen) - ALIGN(oldlen);
167 return _realloc_fdt(fdt, delta);
170 static int store_key_value(char **blob, const char *node_name,
171 const char *property, const char *buf, int len)
176 node = fdt_path_offset(*blob, node_name);
178 report_error(node_name, -1, node);
182 err = fdt_setprop(*blob, node, property, buf, len);
183 if (err == -FDT_ERR_NOSPACE) {
184 *blob = realloc_property(*blob, node, property, len);
185 err = fdt_setprop(*blob, node, property, buf, len);
188 report_error(property, -1, err);
195 * Create paths as needed for all components of a path
197 * Any components of the path that do not exist are created. Errors are
200 * @param blob FDT blob to write into
201 * @param in_path Path to process
202 * @return 0 if ok, -1 on error
204 static int create_paths(char **blob, const char *in_path)
206 const char *path = in_path;
208 int node, offset = 0;
210 /* skip leading '/' */
214 for (sep = path; *sep; path = sep + 1, offset = node) {
215 /* equivalent to strchrnul(), but it requires _GNU_SOURCE */
216 sep = strchr(path, '/');
218 sep = path + strlen(path);
220 node = fdt_subnode_offset_namelen(*blob, offset, path,
222 if (node == -FDT_ERR_NOTFOUND) {
223 *blob = realloc_node(*blob, path);
224 node = fdt_add_subnode_namelen(*blob, offset, path,
228 report_error(path, sep - path, node);
237 * Create a new node in the fdt.
239 * This will overwrite the node_name string. Any error is reported.
241 * TODO: Perhaps create fdt_path_offset_namelen() so we don't need to do this.
243 * @param blob FDT blob to write into
244 * @param node_name Name of node to create
245 * @return new node offset if found, or -1 on failure
247 static int create_node(char **blob, const char *node_name)
252 p = strrchr(node_name, '/');
254 report_error(node_name, -1, -FDT_ERR_BADPATH);
259 *blob = realloc_node(*blob, p + 1);
262 node = fdt_path_offset(*blob, node_name);
264 report_error(node_name, -1, node);
269 node = fdt_add_subnode(*blob, node, p + 1);
271 report_error(p + 1, -1, node);
278 static int do_fdtput(struct display_info *disp, const char *filename,
279 char **arg, int arg_count)
285 blob = utilfdt_read(filename);
289 switch (disp->oper) {
290 case OPER_WRITE_PROP:
292 * Convert the arguments into a single binary value, then
293 * store them into the property.
295 assert(arg_count >= 2);
296 if (disp->auto_path && create_paths(&blob, *arg))
298 if (encode_value(disp, arg + 2, arg_count - 2, &value, &len) ||
299 store_key_value(&blob, *arg, arg[1], value, len))
302 case OPER_CREATE_NODE:
303 for (; ret >= 0 && arg_count--; arg++) {
305 ret = create_paths(&blob, *arg);
307 ret = create_node(&blob, *arg);
313 ret = utilfdt_write(filename, blob);
320 /* Usage related data. */
321 static const char usage_synopsis[] =
322 "write a property value to a device tree\n"
323 " fdtput <options> <dt file> <node> <property> [<value>...]\n"
324 " fdtput -c <options> <dt file> [<node>...]\n"
326 "The command line arguments are joined together into a single value.\n"
328 static const char usage_short_opts[] = "cpt:v" USAGE_COMMON_SHORT_OPTS;
329 static struct option const usage_long_opts[] = {
330 {"create", no_argument, NULL, 'c'},
331 {"auto-path", no_argument, NULL, 'p'},
332 {"type", a_argument, NULL, 't'},
333 {"verbose", no_argument, NULL, 'v'},
334 USAGE_COMMON_LONG_OPTS,
336 static const char * const usage_opts_help[] = {
337 "Create nodes if they don't already exist",
338 "Automatically create nodes as needed for the node path",
340 "Display each value decoded from command line",
341 USAGE_COMMON_OPTS_HELP
344 int main(int argc, char *argv[])
347 struct display_info disp;
348 char *filename = NULL;
350 memset(&disp, '\0', sizeof(disp));
352 disp.oper = OPER_WRITE_PROP;
353 while ((opt = util_getopt_long()) != EOF) {
355 * TODO: add options to:
357 * - delete node (optionally recursively)
359 * - pack fdt before writing
360 * - set amount of free space when writing
363 case_USAGE_COMMON_FLAGS
366 disp.oper = OPER_CREATE_NODE;
372 if (utilfdt_decode_type(optarg, &disp.type,
374 usage("Invalid type string");
384 filename = argv[optind++];
386 usage("missing filename");
391 if (disp.oper == OPER_WRITE_PROP) {
393 usage("missing node");
395 usage("missing property");
398 if (do_fdtput(&disp, filename, argv, argc))