fdtput.c 7.7 KB

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