fdtget.c 7.9 KB

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