fdtput.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  3. *
  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.
  8. *
  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.
  13. *
  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,
  17. * MA 02111-1307 USA
  18. */
  19. #include <assert.h>
  20. #include <ctype.h>
  21. #include <getopt.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <libfdt.h>
  26. #include "util.h"
  27. /* These are the operations we support */
  28. enum oper_type {
  29. OPER_WRITE_PROP, /* Write a property in a node */
  30. OPER_CREATE_NODE, /* Create a new node */
  31. };
  32. struct display_info {
  33. enum oper_type oper; /* operation to perform */
  34. int type; /* data type (s/i/u/x or 0 for default) */
  35. int size; /* data size (1/2/4) */
  36. int verbose; /* verbose output */
  37. int auto_path; /* automatically create all path components */
  38. };
  39. /**
  40. * Report an error with a particular node.
  41. *
  42. * @param name Node name to report error on
  43. * @param namelen Length of node name, or -1 to use entire string
  44. * @param err Error number to report (-FDT_ERR_...)
  45. */
  46. static void report_error(const char *name, int namelen, int err)
  47. {
  48. if (namelen == -1)
  49. namelen = strlen(name);
  50. fprintf(stderr, "Error at '%1.*s': %s\n", namelen, name,
  51. fdt_strerror(err));
  52. }
  53. /**
  54. * Encode a series of arguments in a property value.
  55. *
  56. * @param disp Display information / options
  57. * @param arg List of arguments from command line
  58. * @param arg_count Number of arguments (may be 0)
  59. * @param valuep Returns buffer containing value
  60. * @param *value_len Returns length of value encoded
  61. */
  62. static int encode_value(struct display_info *disp, char **arg, int arg_count,
  63. char **valuep, int *value_len)
  64. {
  65. char *value = NULL; /* holding area for value */
  66. int value_size = 0; /* size of holding area */
  67. char *ptr; /* pointer to current value position */
  68. int len; /* length of this cell/string/byte */
  69. int ival;
  70. int upto; /* the number of bytes we have written to buf */
  71. char fmt[3];
  72. upto = 0;
  73. if (disp->verbose)
  74. fprintf(stderr, "Decoding value:\n");
  75. fmt[0] = '%';
  76. fmt[1] = disp->type ? disp->type : 'd';
  77. fmt[2] = '\0';
  78. for (; arg_count > 0; arg++, arg_count--, upto += len) {
  79. /* assume integer unless told otherwise */
  80. if (disp->type == 's')
  81. len = strlen(*arg) + 1;
  82. else
  83. len = disp->size == -1 ? 4 : disp->size;
  84. /* enlarge our value buffer by a suitable margin if needed */
  85. if (upto + len > value_size) {
  86. value_size = (upto + len) + 500;
  87. value = realloc(value, value_size);
  88. if (!value) {
  89. fprintf(stderr, "Out of mmory: cannot alloc "
  90. "%d bytes\n", value_size);
  91. return -1;
  92. }
  93. }
  94. ptr = value + upto;
  95. if (disp->type == 's') {
  96. memcpy(ptr, *arg, len);
  97. if (disp->verbose)
  98. fprintf(stderr, "\tstring: '%s'\n", ptr);
  99. } else {
  100. int *iptr = (int *)ptr;
  101. sscanf(*arg, fmt, &ival);
  102. if (len == 4)
  103. *iptr = cpu_to_fdt32(ival);
  104. else
  105. *ptr = (uint8_t)ival;
  106. if (disp->verbose) {
  107. fprintf(stderr, "\t%s: %d\n",
  108. disp->size == 1 ? "byte" :
  109. disp->size == 2 ? "short" : "int",
  110. ival);
  111. }
  112. }
  113. }
  114. *value_len = upto;
  115. *valuep = value;
  116. if (disp->verbose)
  117. fprintf(stderr, "Value size %d\n", upto);
  118. return 0;
  119. }
  120. static int store_key_value(void *blob, const char *node_name,
  121. const char *property, const char *buf, int len)
  122. {
  123. int node;
  124. int err;
  125. node = fdt_path_offset(blob, node_name);
  126. if (node < 0) {
  127. report_error(node_name, -1, node);
  128. return -1;
  129. }
  130. err = fdt_setprop(blob, node, property, buf, len);
  131. if (err) {
  132. report_error(property, -1, err);
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. /**
  138. * Create paths as needed for all components of a path
  139. *
  140. * Any components of the path that do not exist are created. Errors are
  141. * reported.
  142. *
  143. * @param blob FDT blob to write into
  144. * @param in_path Path to process
  145. * @return 0 if ok, -1 on error
  146. */
  147. static int create_paths(void *blob, const char *in_path)
  148. {
  149. const char *path = in_path;
  150. const char *sep;
  151. int node, offset = 0;
  152. /* skip leading '/' */
  153. while (*path == '/')
  154. path++;
  155. for (sep = path; *sep; path = sep + 1, offset = node) {
  156. /* equivalent to strchrnul(), but it requires _GNU_SOURCE */
  157. sep = strchr(path, '/');
  158. if (!sep)
  159. sep = path + strlen(path);
  160. node = fdt_subnode_offset_namelen(blob, offset, path,
  161. sep - path);
  162. if (node == -FDT_ERR_NOTFOUND) {
  163. node = fdt_add_subnode_namelen(blob, offset, path,
  164. sep - path);
  165. }
  166. if (node < 0) {
  167. report_error(path, sep - path, node);
  168. return -1;
  169. }
  170. }
  171. return 0;
  172. }
  173. /**
  174. * Create a new node in the fdt.
  175. *
  176. * This will overwrite the node_name string. Any error is reported.
  177. *
  178. * TODO: Perhaps create fdt_path_offset_namelen() so we don't need to do this.
  179. *
  180. * @param blob FDT blob to write into
  181. * @param node_name Name of node to create
  182. * @return new node offset if found, or -1 on failure
  183. */
  184. static int create_node(void *blob, const char *node_name)
  185. {
  186. int node = 0;
  187. char *p;
  188. p = strrchr(node_name, '/');
  189. if (!p) {
  190. report_error(node_name, -1, -FDT_ERR_BADPATH);
  191. return -1;
  192. }
  193. *p = '\0';
  194. if (p > node_name) {
  195. node = fdt_path_offset(blob, node_name);
  196. if (node < 0) {
  197. report_error(node_name, -1, node);
  198. return -1;
  199. }
  200. }
  201. node = fdt_add_subnode(blob, node, p + 1);
  202. if (node < 0) {
  203. report_error(p + 1, -1, node);
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. static int do_fdtput(struct display_info *disp, const char *filename,
  209. char **arg, int arg_count)
  210. {
  211. char *value;
  212. char *blob;
  213. int len, ret = 0;
  214. blob = utilfdt_read(filename);
  215. if (!blob)
  216. return -1;
  217. switch (disp->oper) {
  218. case OPER_WRITE_PROP:
  219. /*
  220. * Convert the arguments into a single binary value, then
  221. * store them into the property.
  222. */
  223. assert(arg_count >= 2);
  224. if (disp->auto_path && create_paths(blob, *arg))
  225. return -1;
  226. if (encode_value(disp, arg + 2, arg_count - 2, &value, &len) ||
  227. store_key_value(blob, *arg, arg[1], value, len))
  228. ret = -1;
  229. break;
  230. case OPER_CREATE_NODE:
  231. for (; ret >= 0 && arg_count--; arg++) {
  232. if (disp->auto_path)
  233. ret = create_paths(blob, *arg);
  234. else
  235. ret = create_node(blob, *arg);
  236. }
  237. break;
  238. }
  239. if (ret >= 0)
  240. ret = utilfdt_write(filename, blob);
  241. free(blob);
  242. return ret;
  243. }
  244. static const char *usage_msg =
  245. "fdtput - write a property value to a device tree\n"
  246. "\n"
  247. "The command line arguments are joined together into a single value.\n"
  248. "\n"
  249. "Usage:\n"
  250. " fdtput <options> <dt file> <node> <property> [<value>...]\n"
  251. " fdtput -c <options> <dt file> [<node>...]\n"
  252. "Options:\n"
  253. "\t-c\t\tCreate nodes if they don't already exist\n"
  254. "\t-p\t\tAutomatically create nodes as needed for the node path\n"
  255. "\t-t <type>\tType of data\n"
  256. "\t-v\t\tVerbose: display each value decoded from command line\n"
  257. "\t-h\t\tPrint this help\n\n"
  258. USAGE_TYPE_MSG;
  259. static void usage(const char *msg)
  260. {
  261. if (msg)
  262. fprintf(stderr, "Error: %s\n\n", msg);
  263. fprintf(stderr, "%s", usage_msg);
  264. exit(2);
  265. }
  266. int main(int argc, char *argv[])
  267. {
  268. struct display_info disp;
  269. char *filename = NULL;
  270. memset(&disp, '\0', sizeof(disp));
  271. disp.size = -1;
  272. disp.oper = OPER_WRITE_PROP;
  273. for (;;) {
  274. int c = getopt(argc, argv, "chpt:v");
  275. if (c == -1)
  276. break;
  277. /*
  278. * TODO: add options to:
  279. * - delete property
  280. * - delete node (optionally recursively)
  281. * - rename node
  282. * - pack fdt before writing
  283. * - set amount of free space when writing
  284. * - expand fdt if value doesn't fit
  285. */
  286. switch (c) {
  287. case 'c':
  288. disp.oper = OPER_CREATE_NODE;
  289. break;
  290. case 'h':
  291. case '?':
  292. usage(NULL);
  293. case 'p':
  294. disp.auto_path = 1;
  295. break;
  296. case 't':
  297. if (utilfdt_decode_type(optarg, &disp.type,
  298. &disp.size))
  299. usage("Invalid type string");
  300. break;
  301. case 'v':
  302. disp.verbose = 1;
  303. break;
  304. }
  305. }
  306. if (optind < argc)
  307. filename = argv[optind++];
  308. if (!filename)
  309. usage("Missing filename");
  310. argv += optind;
  311. argc -= optind;
  312. if (disp.oper == OPER_WRITE_PROP) {
  313. if (argc < 1)
  314. usage("Missing node");
  315. if (argc < 2)
  316. usage("Missing property");
  317. }
  318. if (do_fdtput(&disp, filename, argv, argc))
  319. return 1;
  320. return 0;
  321. }