fdtget.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  3. *
  4. * Portions from U-Boot cmd_fdt.c (C) Copyright 2007
  5. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  6. * Based on code written by:
  7. * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
  8. * Matthew McClintock <msm@freescale.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <getopt.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <libfdt.h>
  32. #include "util.h"
  33. enum display_mode {
  34. MODE_SHOW_VALUE, /* show values for node properties */
  35. MODE_LIST_PROPS, /* list the properties for a node */
  36. MODE_LIST_SUBNODES, /* list the subnodes of a node */
  37. };
  38. /* Holds information which controls our output and options */
  39. struct display_info {
  40. int type; /* data type (s/i/u/x or 0 for default) */
  41. int size; /* data size (1/2/4) */
  42. enum display_mode mode; /* display mode that we are using */
  43. const char *default_val; /* default value if node/property not found */
  44. };
  45. static void report_error(const char *where, int err)
  46. {
  47. fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
  48. }
  49. /**
  50. * Displays data of a given length according to selected options
  51. *
  52. * If a specific data type is provided in disp, then this is used. Otherwise
  53. * we try to guess the data type / size from the contents.
  54. *
  55. * @param disp Display information / options
  56. * @param data Data to display
  57. * @param len Maximum length of buffer
  58. * @return 0 if ok, -1 if data does not match format
  59. */
  60. static int show_data(struct display_info *disp, const char *data, int len)
  61. {
  62. int i, size;
  63. const uint8_t *p = (const uint8_t *)data;
  64. const char *s;
  65. int value;
  66. int is_string;
  67. char fmt[3];
  68. /* no data, don't print */
  69. if (len == 0)
  70. return 0;
  71. is_string = (disp->type) == 's' ||
  72. (!disp->type && util_is_printable_string(data, len));
  73. if (is_string) {
  74. if (data[len - 1] != '\0') {
  75. fprintf(stderr, "Unterminated string\n");
  76. return -1;
  77. }
  78. for (s = data; s - data < len; s += strlen(s) + 1) {
  79. if (s != data)
  80. printf(" ");
  81. printf("%s", (const char *)s);
  82. }
  83. return 0;
  84. }
  85. size = disp->size;
  86. if (size == -1) {
  87. size = (len % 4) == 0 ? 4 : 1;
  88. } else if (len % size) {
  89. fprintf(stderr, "Property length must be a multiple of "
  90. "selected data size\n");
  91. return -1;
  92. }
  93. fmt[0] = '%';
  94. fmt[1] = disp->type ? disp->type : 'd';
  95. fmt[2] = '\0';
  96. for (i = 0; i < len; i += size, p += size) {
  97. if (i)
  98. printf(" ");
  99. value = size == 4 ? fdt32_to_cpu(*(const uint32_t *)p) :
  100. size == 2 ? (*p << 8) | p[1] : *p;
  101. printf(fmt, value);
  102. }
  103. return 0;
  104. }
  105. /**
  106. * List all properties in a node, one per line.
  107. *
  108. * @param blob FDT blob
  109. * @param node Node to display
  110. * @return 0 if ok, or FDT_ERR... if not.
  111. */
  112. static int list_properties(const void *blob, int node)
  113. {
  114. const struct fdt_property *data;
  115. const char *name;
  116. int prop;
  117. prop = fdt_first_property_offset(blob, node);
  118. do {
  119. /* Stop silently when there are no more properties */
  120. if (prop < 0)
  121. return prop == -FDT_ERR_NOTFOUND ? 0 : prop;
  122. data = fdt_get_property_by_offset(blob, prop, NULL);
  123. name = fdt_string(blob, fdt32_to_cpu(data->nameoff));
  124. if (name)
  125. puts(name);
  126. prop = fdt_next_property_offset(blob, prop);
  127. } while (1);
  128. }
  129. #define MAX_LEVEL 32 /* how deeply nested we will go */
  130. /**
  131. * List all subnodes in a node, one per line
  132. *
  133. * @param blob FDT blob
  134. * @param node Node to display
  135. * @return 0 if ok, or FDT_ERR... if not.
  136. */
  137. static int list_subnodes(const void *blob, int node)
  138. {
  139. int nextoffset; /* next node offset from libfdt */
  140. uint32_t tag; /* current tag */
  141. int level = 0; /* keep track of nesting level */
  142. const char *pathp;
  143. int depth = 1; /* the assumed depth of this node */
  144. while (level >= 0) {
  145. tag = fdt_next_tag(blob, node, &nextoffset);
  146. switch (tag) {
  147. case FDT_BEGIN_NODE:
  148. pathp = fdt_get_name(blob, node, NULL);
  149. if (level <= depth) {
  150. if (pathp == NULL)
  151. pathp = "/* NULL pointer error */";
  152. if (*pathp == '\0')
  153. pathp = "/"; /* root is nameless */
  154. if (level == 1)
  155. puts(pathp);
  156. }
  157. level++;
  158. if (level >= MAX_LEVEL) {
  159. printf("Nested too deep, aborting.\n");
  160. return 1;
  161. }
  162. break;
  163. case FDT_END_NODE:
  164. level--;
  165. if (level == 0)
  166. level = -1; /* exit the loop */
  167. break;
  168. case FDT_END:
  169. return 1;
  170. case FDT_PROP:
  171. break;
  172. default:
  173. if (level <= depth)
  174. printf("Unknown tag 0x%08X\n", tag);
  175. return 1;
  176. }
  177. node = nextoffset;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * Show the data for a given node (and perhaps property) according to the
  183. * display option provided.
  184. *
  185. * @param blob FDT blob
  186. * @param disp Display information / options
  187. * @param node Node to display
  188. * @param property Name of property to display, or NULL if none
  189. * @return 0 if ok, -ve on error
  190. */
  191. static int show_data_for_item(const void *blob, struct display_info *disp,
  192. int node, const char *property)
  193. {
  194. const void *value = NULL;
  195. int len, err = 0;
  196. switch (disp->mode) {
  197. case MODE_LIST_PROPS:
  198. err = list_properties(blob, node);
  199. break;
  200. case MODE_LIST_SUBNODES:
  201. err = list_subnodes(blob, node);
  202. break;
  203. default:
  204. assert(property);
  205. value = fdt_getprop(blob, node, property, &len);
  206. if (value) {
  207. if (show_data(disp, value, len))
  208. err = -1;
  209. else
  210. printf("\n");
  211. } else if (disp->default_val) {
  212. puts(disp->default_val);
  213. } else {
  214. report_error(property, len);
  215. err = -1;
  216. }
  217. break;
  218. }
  219. return err;
  220. }
  221. /**
  222. * Run the main fdtget operation, given a filename and valid arguments
  223. *
  224. * @param disp Display information / options
  225. * @param filename Filename of blob file
  226. * @param arg List of arguments to process
  227. * @param arg_count Number of arguments
  228. * @param return 0 if ok, -ve on error
  229. */
  230. static int do_fdtget(struct display_info *disp, const char *filename,
  231. char **arg, int arg_count, int args_per_step)
  232. {
  233. char *blob;
  234. const char *prop;
  235. int i, node;
  236. blob = utilfdt_read(filename);
  237. if (!blob)
  238. return -1;
  239. for (i = 0; i + args_per_step <= arg_count; i += args_per_step) {
  240. node = fdt_path_offset(blob, arg[i]);
  241. if (node < 0) {
  242. if (disp->default_val) {
  243. puts(disp->default_val);
  244. continue;
  245. } else {
  246. report_error(arg[i], node);
  247. return -1;
  248. }
  249. }
  250. prop = args_per_step == 1 ? NULL : arg[i + 1];
  251. if (show_data_for_item(blob, disp, node, prop))
  252. return -1;
  253. }
  254. return 0;
  255. }
  256. static const char *usage_msg =
  257. "fdtget - read values from device tree\n"
  258. "\n"
  259. "Each value is printed on a new line.\n\n"
  260. "Usage:\n"
  261. " fdtget <options> <dt file> [<node> <property>]...\n"
  262. " fdtget -p <options> <dt file> [<node> ]...\n"
  263. "Options:\n"
  264. "\t-t <type>\tType of data\n"
  265. "\t-p\t\tList properties for each node\n"
  266. "\t-l\t\tList subnodes for each node\n"
  267. "\t-d\t\tDefault value to display when the property is "
  268. "missing\n"
  269. "\t-h\t\tPrint this help\n\n"
  270. USAGE_TYPE_MSG;
  271. static void usage(const char *msg)
  272. {
  273. if (msg)
  274. fprintf(stderr, "Error: %s\n\n", msg);
  275. fprintf(stderr, "%s", usage_msg);
  276. exit(2);
  277. }
  278. int main(int argc, char *argv[])
  279. {
  280. char *filename = NULL;
  281. struct display_info disp;
  282. int args_per_step = 2;
  283. /* set defaults */
  284. memset(&disp, '\0', sizeof(disp));
  285. disp.size = -1;
  286. disp.mode = MODE_SHOW_VALUE;
  287. for (;;) {
  288. int c = getopt(argc, argv, "d:hlpt:");
  289. if (c == -1)
  290. break;
  291. switch (c) {
  292. case 'h':
  293. case '?':
  294. usage(NULL);
  295. case 't':
  296. if (utilfdt_decode_type(optarg, &disp.type,
  297. &disp.size))
  298. usage("Invalid type string");
  299. break;
  300. case 'p':
  301. disp.mode = MODE_LIST_PROPS;
  302. args_per_step = 1;
  303. break;
  304. case 'l':
  305. disp.mode = MODE_LIST_SUBNODES;
  306. args_per_step = 1;
  307. break;
  308. case 'd':
  309. disp.default_val = optarg;
  310. break;
  311. }
  312. }
  313. if (optind < argc)
  314. filename = argv[optind++];
  315. if (!filename)
  316. usage("Missing filename");
  317. argv += optind;
  318. argc -= optind;
  319. /* Allow no arguments, and silently succeed */
  320. if (!argc)
  321. return 0;
  322. /* Check for node, property arguments */
  323. if (args_per_step == 2 && (argc % 2))
  324. usage("Must have an even number of arguments");
  325. if (do_fdtget(&disp, filename, argv, argc, args_per_step))
  326. return 1;
  327. return 0;
  328. }