1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013, Google Inc.
4 * Written by Simon Glass <sjg@chromium.org>
6 * Perform a grep of an FDT either displaying the source subset or producing
7 * a new .dtb subset which can be used as required.
20 #include <fdt_region.h>
23 #include "libfdt_internal.h"
25 /* Define DEBUG to get some debugging output on stderr */
27 #define debug(a, b...) fprintf(stderr, a, ## b)
29 #define debug(a, b...)
32 /* A linked list of values we are grepping for */
34 int type; /* Types this value matches (FDT_IS... mask) */
35 int include; /* 1 to include matches, 0 to exclude */
36 const char *string; /* String to match */
37 struct value_node *next; /* Pointer to next node, or NULL */
40 /* Output formats we support */
42 OUT_DTS, /* Device tree source */
43 OUT_DTB, /* Valid device tree binary */
44 OUT_BIN, /* Fragment of .dtb, for hashing */
47 /* Holds information which controls our output and options */
49 enum output_t output; /* Output format */
50 int add_aliases; /* Add aliases node to output */
51 int all; /* Display all properties/nodes */
52 int colour; /* Display output in ANSI colour */
53 int region_list; /* Output a region list */
54 int flags; /* Flags (FDT_REG_...) */
55 int list_strings; /* List strings in string table */
56 int show_offset; /* Show offset */
57 int show_addr; /* Show address */
58 int header; /* Output an FDT header */
59 int diff; /* Show +/- diff markers */
60 int include_root; /* Include the root node and all properties */
61 int remove_strings; /* Remove unused strings */
62 int show_dts_version; /* Put '/dts-v1/;' on the first line */
63 int types_inc; /* Mask of types that we include (FDT_IS...) */
64 int types_exc; /* Mask of types that we exclude (FDT_IS...) */
65 int invert; /* Invert polarity of match */
66 int props_up; /* Imply properties up to supernodes */
67 struct value_node *value_head; /* List of values to match */
68 const char *output_fname; /* Output filename */
69 FILE *fout; /* File to write dts/dtb output */
72 static void report_error(const char *where, int err)
74 fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
77 /* Supported ANSI colours */
92 * print_ansi_colour() - Print out the ANSI sequence for a colour
95 * @col: Colour to output (COL_...), or COL_NONE to reset colour
97 static void print_ansi_colour(FILE *fout, int col)
100 fprintf(fout, "\033[0m");
102 fprintf(fout, "\033[1;%dm", col + 30);
107 * value_add() - Add a new value to our list of things to grep for
109 * @disp: Display structure, holding info about our options
110 * @headp: Pointer to header pointer of list
111 * @type: Type of this value (FDT_IS_...)
112 * @include: 1 if we want to include matches, 0 to exclude
113 * @str: String value to match
115 static int value_add(struct display_info *disp, struct value_node **headp,
116 int type, int include, const char *str)
118 struct value_node *node;
121 * Keep track of which types we are excluding/including. We don't
122 * allow both including and excluding things, because it doesn't make
123 * sense. 'Including' means that everything not mentioned is
124 * excluded. 'Excluding' means that everything not mentioned is
125 * included. So using the two together would be meaningless.
128 disp->types_inc |= type;
130 disp->types_exc |= type;
131 if (disp->types_inc & disp->types_exc & type) {
133 "Cannot use both include and exclude for '%s'\n", str);
140 node = malloc(sizeof(*node));
145 node->include = include;
151 fprintf(stderr, "Out of memory\n");
155 static bool util_is_printable_string(const void *data, int len)
157 const char *s = data;
160 /* zero length is not */
164 /* must terminate with zero */
165 if (s[len - 1] != '\0')
172 while (s < se && *s && isprint((unsigned char)*s))
175 /* not zero, or not done yet */
176 if (*s != '\0' || s == ss)
185 static void utilfdt_print_data(const char *data, int len)
188 const char *p = data;
191 /* no data, don't print */
195 if (util_is_printable_string(data, len)) {
204 } while (s < data + len);
206 } else if ((len % 4) == 0) {
207 const uint32_t *cell = (const uint32_t *)data;
210 for (i = 0, len /= 4; i < len; i++)
211 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
212 i < (len - 1) ? " " : "");
216 for (i = 0; i < len; i++)
217 printf("%02x%s", (unsigned char)*p++, i < len - 1 ? " " : "");
223 * display_fdt_by_regions() - Display regions of an FDT source
225 * This dumps an FDT as source, but only certain regions of it. This is the
226 * final stage of the grep - we have a list of regions we want to display,
227 * and this function displays them.
229 * @disp: Display structure, holding info about our options
230 * @blob: FDT blob to display
231 * @region: List of regions to display
232 * @count: Number of regions
234 static int display_fdt_by_regions(struct display_info *disp, const void *blob,
235 struct fdt_region region[], int count)
237 struct fdt_region *reg = region, *reg_end = region + count;
238 uint32_t off_mem_rsvmap = fdt_off_mem_rsvmap(blob);
239 int base = fdt_off_dt_struct(blob);
240 int version = fdt_version(blob);
241 int offset, nextoffset;
242 int tag, depth, shift;
243 FILE *f = disp->fout;
249 if (disp->show_dts_version)
250 fprintf(f, "/dts-v1/;\n");
253 fprintf(f, "// magic:\t\t0x%x\n", fdt_magic(blob));
254 fprintf(f, "// totalsize:\t\t0x%x (%d)\n", fdt_totalsize(blob),
255 fdt_totalsize(blob));
256 fprintf(f, "// off_dt_struct:\t0x%x\n",
257 fdt_off_dt_struct(blob));
258 fprintf(f, "// off_dt_strings:\t0x%x\n",
259 fdt_off_dt_strings(blob));
260 fprintf(f, "// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
261 fprintf(f, "// version:\t\t%d\n", version);
262 fprintf(f, "// last_comp_version:\t%d\n",
263 fdt_last_comp_version(blob));
265 fprintf(f, "// boot_cpuid_phys:\t0x%x\n",
266 fdt_boot_cpuid_phys(blob));
269 fprintf(f, "// size_dt_strings:\t0x%x\n",
270 fdt_size_dt_strings(blob));
273 fprintf(f, "// size_dt_struct:\t0x%x\n",
274 fdt_size_dt_struct(blob));
279 if (disp->flags & FDT_REG_ADD_MEM_RSVMAP) {
280 const struct fdt_reserve_entry *p_rsvmap;
282 p_rsvmap = (const struct fdt_reserve_entry *)
283 ((const char *)blob + off_mem_rsvmap);
285 addr = fdt64_to_cpu(p_rsvmap[i].address);
286 size = fdt64_to_cpu(p_rsvmap[i].size);
287 if (addr == 0 && size == 0)
290 fprintf(f, "/memreserve/ %llx %llx;\n",
291 (unsigned long long)addr,
292 (unsigned long long)size);
298 shift = 4; /* 4 spaces per indent */
300 const struct fdt_property *prop;
308 * Work out the file offset of this offset, and decide
309 * whether it is in the region list or not
311 file_ofs = base + offset;
312 if (reg < reg_end && file_ofs >= reg->offset + reg->size)
314 in_region = reg < reg_end && file_ofs >= reg->offset &&
315 file_ofs < reg->offset + reg->size;
316 tag = fdt_next_tag(blob, offset, &nextoffset);
320 show = in_region || disp->all;
321 if (show && disp->diff)
322 fprintf(f, "%c", in_region ? '+' : '-');
325 /* Do this here to avoid 'if (show)' in every 'case' */
326 if (tag == FDT_BEGIN_NODE)
328 else if (tag == FDT_END_NODE)
332 if (tag != FDT_END) {
334 fprintf(f, "%4x: ", file_ofs);
335 if (disp->show_offset)
336 fprintf(f, "%4x: ", file_ofs - base);
339 /* Green means included, red means excluded */
341 print_ansi_colour(f, in_region ? COL_GREEN : COL_RED);
345 prop = fdt_get_property_by_offset(blob, offset, NULL);
346 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
347 fprintf(f, "%*s%s", depth * shift, "", name);
348 utilfdt_print_data(prop->data,
349 fdt32_to_cpu(prop->len));
354 fprintf(f, "%*s// [NOP]", depth * shift, "");
358 name = fdt_get_name(blob, offset, &len);
359 fprintf(f, "%*s%s {", depth++ * shift, "",
364 fprintf(f, "%*s};", --depth * shift, "");
368 /* Reset colour back to normal before end of line */
370 print_ansi_colour(f, COL_NONE);
374 /* Print a list of strings if requested */
375 if (disp->list_strings) {
377 int str_base = fdt_off_dt_strings(blob);
380 offset < (int)fdt_size_dt_strings(blob);
381 offset += strlen(str) + 1) {
382 str = fdt_string(blob, offset);
383 int len = strlen(str) + 1;
386 /* Only print strings that are in the region */
387 file_ofs = str_base + offset;
388 in_region = reg < reg_end &&
389 file_ofs >= reg->offset &&
390 file_ofs + len < reg->offset +
392 show = in_region || disp->all;
393 if (show && disp->diff)
394 printf("%c", in_region ? '+' : '-');
396 printf("%4x: ", file_ofs);
397 if (disp->show_offset)
398 printf("%4x: ", offset);
407 * dump_fdt_regions() - Dump regions of an FDT as binary data
409 * This dumps an FDT as binary, but only certain regions of it. This is the
410 * final stage of the grep - we have a list of regions we want to dump,
411 * and this function dumps them.
413 * The output of this function may or may not be a valid FDT. To ensure it
414 * is, these disp->flags must be set:
416 * FDT_REG_SUPERNODES: ensures that subnodes are preceded by their
417 * parents. Without this option, fragments of subnode data may be
418 * output without the supernodes above them. This is useful for
419 * hashing but cannot produce a valid FDT.
420 * FDT_REG_ADD_STRING_TAB: Adds a string table to the end of the FDT.
421 * Without this none of the properties will have names
422 * FDT_REG_ADD_MEM_RSVMAP: Adds a mem_rsvmap table - an FDT is invalid
425 * @disp: Display structure, holding info about our options
426 * @blob: FDT blob to display
427 * @region: List of regions to display
428 * @count: Number of regions
429 * @out: Output destination
431 static int dump_fdt_regions(struct display_info *disp, const void *blob,
432 struct fdt_region region[], int count, char *out)
434 struct fdt_header *fdt;
435 int size, struct_start;
439 /* Set up a basic header (even if we don't actually write it) */
440 fdt = (struct fdt_header *)out;
441 memset(fdt, '\0', sizeof(*fdt));
442 fdt_set_magic(fdt, FDT_MAGIC);
443 struct_start = sizeof(struct fdt_header);
444 fdt_set_off_mem_rsvmap(fdt, struct_start);
445 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
446 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
449 * Calculate the total size of the regions we are writing out. The
450 * first will be the mem_rsvmap if the FDT_REG_ADD_MEM_RSVMAP flag
451 * is set. The last will be the string table if FDT_REG_ADD_STRING_TAB
454 for (i = size = 0; i < count; i++)
455 size += region[i].size;
457 /* Bring in the mem_rsvmap section from the old file if requested */
458 if (count > 0 && (disp->flags & FDT_REG_ADD_MEM_RSVMAP)) {
459 struct_start += region[0].size;
460 size -= region[0].size;
462 fdt_set_off_dt_struct(fdt, struct_start);
464 /* Update the header to have the correct offsets/sizes */
465 if (count >= 2 && (disp->flags & FDT_REG_ADD_STRING_TAB)) {
468 str_size = region[count - 1].size;
469 fdt_set_size_dt_struct(fdt, size - str_size);
470 fdt_set_off_dt_strings(fdt, struct_start + size - str_size);
471 fdt_set_size_dt_strings(fdt, str_size);
472 fdt_set_totalsize(fdt, struct_start + size);
475 /* Write the header if required */
479 while (ptr < fdt_off_mem_rsvmap(fdt))
483 /* Output all the nodes including any mem_rsvmap/string table */
484 for (i = 0; i < count; i++) {
485 struct fdt_region *reg = ®ion[i];
487 memcpy(out + ptr, (const char *)blob + reg->offset, reg->size);
495 * show_region_list() - Print out a list of regions
497 * The list includes the region offset (absolute offset from start of FDT
498 * blob in bytes) and size
500 * @reg: List of regions to print
501 * @count: Number of regions
503 static void show_region_list(struct fdt_region *reg, int count)
507 printf("Regions: %d\n", count);
508 for (i = 0; i < count; i++, reg++) {
509 printf("%d: %-10x %-10x\n", i, reg->offset,
510 reg->offset + reg->size);
514 static int check_type_include(void *priv, int type, const char *data, int size)
516 struct display_info *disp = priv;
517 struct value_node *val;
518 int match, none_match = FDT_IS_ANY;
520 /* If none of our conditions mention this type, we know nothing */
521 debug("type=%x, data=%s\n", type, data ? data : "(null)");
522 if (!((disp->types_inc | disp->types_exc) & type)) {
523 debug(" - not in any condition\n");
528 * Go through the list of conditions. For inclusive conditions, we
529 * return 1 at the first match. For exclusive conditions, we must
530 * check that there are no matches.
533 for (val = disp->value_head; val; val = val->next) {
534 if (!(type & val->type))
536 match = fdt_stringlist_contains(data, size,
538 debug(" - val->type=%x, str='%s', match=%d\n",
539 val->type, val->string, match);
540 if (match && val->include) {
541 debug(" - match inc %s\n", val->string);
545 none_match &= ~val->type;
550 * If this is an exclusive condition, and nothing matches, then we
553 if ((type & disp->types_exc) && (none_match & type)) {
554 debug(" - match exc\n");
556 * Allow FDT_IS_COMPAT to make the final decision in the
557 * case where there is no specific type
559 if (type == FDT_IS_NODE && disp->types_exc == FDT_ANY_GLOBAL) {
560 debug(" - supressed exc node\n");
567 * Allow FDT_IS_COMPAT to make the final decision in the
568 * case where there is no specific type (inclusive)
570 if (type == FDT_IS_NODE && disp->types_inc == FDT_ANY_GLOBAL)
573 debug(" - no match, types_inc=%x, types_exc=%x, none_match=%x\n",
574 disp->types_inc, disp->types_exc, none_match);
580 * check_props() - Check if a node has properties that we want to include
582 * Calls check_type_include() for each property in the nodn, returning 1 if
583 * that function returns 1 for any of them
585 * @disp: Display structure, holding info about our options
586 * @fdt: Devicetree blob to check
587 * @node: Node offset to check
588 * @inc: Current value of the 'include' variable (see h_include())
589 * Return: 0 to exclude, 1 to include, -1 if no information is available
591 static int check_props(struct display_info *disp, const void *fdt, int node,
596 for (offset = fdt_first_property_offset(fdt, node);
597 offset > 0 && inc != 1;
598 offset = fdt_next_property_offset(fdt, offset)) {
599 const struct fdt_property *prop;
602 prop = fdt_get_property_by_offset(fdt, offset, NULL);
605 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
606 inc = check_type_include(disp, FDT_NODE_HAS_PROP, str,
610 /* if requested, check all subnodes for this property too */
611 if (inc != 1 && disp->props_up) {
614 for (subnode = fdt_first_subnode(fdt, node);
615 subnode > 0 && inc != 1;
616 subnode = fdt_next_subnode(fdt, subnode))
617 inc = check_props(disp, fdt, subnode, inc);
624 * h_include() - Include handler function for fdt_first_region()
626 * This function decides whether to include or exclude a node, property or
627 * compatible string. The function is defined by fdt_first_region().
629 * The algorithm is documented in the code - disp->invert is 0 for normal
630 * operation, and 1 to invert the sense of all matches.
632 * @priv: Private pointer as passed to fdtgrep_find_regions()
633 * @fdt: Pointer to FDT blob
634 * @offset: Offset of this node / property
635 * @type: Type of this part, FDT_IS_...
636 * @data: Pointer to data (node name, property name, compatible string)
637 * @size: Size of data, or 0 if none
638 * Return: 0 to exclude, 1 to include, -1 if no information is available
640 static int h_include(void *priv, const void *fdt, int offset, int type,
641 const char *data, int size)
643 struct display_info *disp = priv;
646 inc = check_type_include(priv, type, data, size);
647 if (disp->include_root && type == FDT_IS_PROP && offset == 0 && inc)
651 * If the node name does not tell us anything, check the
654 if (inc == -1 && type == FDT_IS_NODE) {
655 debug(" - checking compatible2\n");
656 data = fdt_getprop(fdt, offset, "compatible", &len);
657 inc = check_type_include(priv, FDT_IS_COMPAT, data, len);
660 /* If we still have no idea, check for properties in the node */
661 if (inc != 1 && type == FDT_IS_NODE &&
662 (disp->types_inc & FDT_NODE_HAS_PROP)) {
663 debug(" - checking node '%s'\n",
664 fdt_get_name(fdt, offset, NULL));
665 inc = check_props(disp, fdt, offset, inc);
670 if (inc != -1 && disp->invert)
672 debug(" - returning %d\n", inc);
677 static int h_cmp_region(const void *v1, const void *v2)
679 const struct fdt_region *region1 = v1, *region2 = v2;
681 return region1->offset - region2->offset;
684 static int fdtgrep_find_regions(const void *fdt,
685 int (*include_func)(void *priv, const void *fdt, int offset,
686 int type, const char *data, int size),
687 struct display_info *disp, struct fdt_region *region,
688 int max_regions, char *path, int path_len, int flags)
690 struct fdt_region_state state;
695 ret = fdt_first_region(fdt, include_func, disp,
696 ®ion[count++], path, path_len,
697 disp->flags, &state);
699 ret = fdt_next_region(fdt, include_func, disp,
700 count < max_regions ? ®ion[count] : NULL,
701 path, path_len, disp->flags, &state);
705 if (ret && ret != -FDT_ERR_NOTFOUND)
708 /* Find all the aliases and add those regions back in */
709 if (disp->add_aliases && count < max_regions) {
712 new_count = fdt_add_alias_regions(fdt, region, count,
713 max_regions, &state);
714 if (new_count == -FDT_ERR_NOTFOUND) {
715 /* No alias node found */
716 } else if (new_count < 0) {
718 } else if (new_count <= max_regions) {
720 * The alias regions will now be at the end of the list.
721 * Sort the regions by offset to get things into the
725 qsort(region, count, sizeof(struct fdt_region),
733 int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
735 int fd = 0; /* assume stdin */
737 off_t bufsize = 1024, offset = 0;
741 if (strcmp(filename, "-") != 0) {
742 fd = open(filename, O_RDONLY);
747 /* Loop until we have read everything */
748 buf = malloc(bufsize);
754 /* Expand the buffer to hold the next chunk */
755 if (offset == bufsize) {
757 buf = realloc(buf, bufsize);
764 ret = read(fd, &buf[offset], bufsize - offset);
772 /* Clean up, including closing stdin; return errno on error */
782 int utilfdt_read_err(const char *filename, char **buffp)
785 return utilfdt_read_err_len(filename, buffp, &len);
788 char *utilfdt_read_len(const char *filename, off_t *len)
791 int ret = utilfdt_read_err_len(filename, &buff, len);
794 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
798 /* Successful read */
802 char *utilfdt_read(const char *filename)
805 return utilfdt_read_len(filename, &len);
809 * Run the main fdtgrep operation, given a filename and valid arguments
811 * @param disp Display information / options
812 * @param filename Filename of blob file
813 * @param return 0 if ok, -ve on error
815 static int do_fdtgrep(struct display_info *disp, const char *filename)
817 struct fdt_region *region = NULL;
824 blob = utilfdt_read(filename);
827 ret = fdt_check_header(blob);
829 fprintf(stderr, "Error: %s\n", fdt_strerror(ret));
833 /* Allow old files, but they are untested */
834 if (fdt_version(blob) < 17 && disp->value_head) {
836 "Warning: fdtgrep does not fully support version %d files\n",
841 * We do two passes, since we don't know how many regions we need.
842 * The first pass will count the regions, but if it is too many,
843 * we do another pass to actually record them.
845 for (i = 0; i < 2; i++) {
846 region = realloc(region, count * sizeof(struct fdt_region));
848 fprintf(stderr, "Out of memory for %d regions\n",
853 count = fdtgrep_find_regions(blob,
855 region, max_regions, path, sizeof(path),
858 report_error("fdtgrep_find_regions", count);
862 if (count <= max_regions)
865 if (count > max_regions) {
867 fprintf(stderr, "Internal error with fdtgrep_find_region()\n");
871 /* Optionally print a list of regions */
872 if (disp->region_list)
873 show_region_list(region, count);
875 /* Output either source .dts or binary .dtb */
876 if (disp->output == OUT_DTS) {
877 ret = display_fdt_by_regions(disp, blob, region, count);
880 /* Allow reserved memory section to expand slightly */
881 int size = fdt_totalsize(blob) + 16;
885 fprintf(stderr, "Out_of_memory\n");
889 size = dump_fdt_regions(disp, blob, region, count, fdt);
890 if (disp->remove_strings) {
895 fprintf(stderr, "Out_of_memory\n");
899 ret = fdt_remove_unused_strings(fdt, out);
902 "Failed to remove unused strings: err=%d\n",
910 fprintf(stderr, "Failed to pack: err=%d\n",
914 size = fdt_totalsize(fdt);
917 if ((size_t)size != fwrite(fdt, 1, size, disp->fout)) {
918 fprintf(stderr, "Write failure, %d bytes\n", size);
932 static const char usage_synopsis[] =
933 "fdtgrep - extract portions from device tree\n"
936 " fdtgrep <options> <dt file>|-\n\n"
937 "Output formats are:\n"
938 "\tdts - device tree soure text\n"
939 "\tdtb - device tree blob (sets -Hmt automatically)\n"
940 "\tbin - device tree fragment (may not be a valid .dtb)";
942 /* Helper for usage_short_opts string constant */
943 #define USAGE_COMMON_SHORT_OPTS "hV"
945 /* Helper for aligning long_opts array */
946 #define a_argument required_argument
948 /* Helper for usage_long_opts option array */
949 #define USAGE_COMMON_LONG_OPTS \
950 {"help", no_argument, NULL, 'h'}, \
951 {"version", no_argument, NULL, 'V'}, \
952 {NULL, no_argument, NULL, 0x0}
954 /* Helper for usage_opts_help array */
955 #define USAGE_COMMON_OPTS_HELP \
956 "Print this help and exit", \
957 "Print version and exit", \
960 /* Helper for getopt case statements */
961 #define case_USAGE_COMMON_FLAGS \
962 case 'h': usage(NULL); \
964 case 'V': util_version(); \
966 case '?': usage("unknown option");
968 static const char usage_short_opts[] =
969 "haAc:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTuv"
970 USAGE_COMMON_SHORT_OPTS;
971 static const struct option usage_long_opts[] = {
972 {"show-address", no_argument, NULL, 'a'},
973 {"colour", no_argument, NULL, 'A'},
974 {"include-node-with-prop", a_argument, NULL, 'b'},
975 {"include-compat", a_argument, NULL, 'c'},
976 {"exclude-compat", a_argument, NULL, 'C'},
977 {"diff", no_argument, NULL, 'd'},
978 {"enter-node", no_argument, NULL, 'e'},
979 {"show-offset", no_argument, NULL, 'f'},
980 {"include-match", a_argument, NULL, 'g'},
981 {"exclude-match", a_argument, NULL, 'G'},
982 {"show-header", no_argument, NULL, 'H'},
983 {"show-version", no_argument, NULL, 'I'},
984 {"list-regions", no_argument, NULL, 'l'},
985 {"list-strings", no_argument, NULL, 'L'},
986 {"include-mem", no_argument, NULL, 'm'},
987 {"include-node", a_argument, NULL, 'n'},
988 {"exclude-node", a_argument, NULL, 'N'},
989 {"out", a_argument, NULL, 'o'},
990 {"out-format", a_argument, NULL, 'O'},
991 {"include-prop", a_argument, NULL, 'p'},
992 {"exclude-prop", a_argument, NULL, 'P'},
993 {"remove-strings", no_argument, NULL, 'r'},
994 {"include-root", no_argument, NULL, 'R'},
995 {"show-subnodes", no_argument, NULL, 's'},
996 {"skip-supernodes", no_argument, NULL, 'S'},
997 {"show-stringtab", no_argument, NULL, 't'},
998 {"show-aliases", no_argument, NULL, 'T'},
999 {"props-up-to-supernode", no_argument, NULL, 'u'},
1000 {"invert-match", no_argument, NULL, 'v'},
1001 USAGE_COMMON_LONG_OPTS,
1003 static const char * const usage_opts_help[] = {
1005 "Show all nodes/tags, colour those that match",
1006 "Include contains containing property",
1007 "Compatible nodes to include in grep",
1008 "Compatible nodes to exclude in grep",
1009 "Diff: Mark matching nodes with +, others with -",
1010 "Enter direct subnode names of matching nodes",
1012 "Node/property/compatible string to include in grep",
1013 "Node/property/compatible string to exclude in grep",
1015 "Put \"/dts-v1/;\" on first line of dts output",
1016 "Output a region list",
1017 "List strings in string table",
1018 "Include mem_rsvmap section in binary output",
1019 "Node to include in grep",
1020 "Node to exclude in grep",
1022 "-O <output format>",
1023 "Property to include in grep",
1024 "Property to exclude in grep",
1025 "Remove unused strings from string table",
1026 "Include root node and all properties",
1027 "Show all subnodes matching nodes",
1028 "Don't include supernodes of matching nodes",
1029 "Include string table in binary output",
1030 "Include matching aliases in output",
1031 "Add -p properties to supernodes too",
1032 "Invert the sense of matching (select non-matching lines)",
1033 USAGE_COMMON_OPTS_HELP
1037 * Call getopt_long() with standard options
1039 * Since all util code runs getopt in the same way, provide a helper.
1041 #define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
1042 usage_long_opts, NULL)
1044 void util_usage(const char *errmsg, const char *synopsis,
1045 const char *short_opts, struct option const long_opts[],
1046 const char * const opts_help[])
1048 FILE *fp = errmsg ? stderr : stdout;
1049 const char a_arg[] = "<arg>";
1050 size_t a_arg_len = strlen(a_arg) + 1;
1057 "Options: -[%s]\n", synopsis, short_opts);
1059 /* prescan the --long opt length to auto-align */
1061 for (i = 0; long_opts[i].name; ++i) {
1062 /* +1 is for space between --opt and help text */
1063 int l = strlen(long_opts[i].name) + 1;
1064 if (long_opts[i].has_arg == a_argument)
1070 for (i = 0; long_opts[i].name; ++i) {
1071 /* helps when adding new applets or options */
1072 assert(opts_help[i] != NULL);
1074 /* first output the short flag if it has one */
1075 if (long_opts[i].val > '~')
1078 fprintf(fp, " -%c, ", long_opts[i].val);
1080 /* then the long flag */
1081 if (long_opts[i].has_arg == no_argument) {
1082 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
1084 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
1085 (int)(optlen - strlen(long_opts[i].name) -
1089 /* finally the help text */
1090 fprintf(fp, "%s\n", opts_help[i]);
1094 fprintf(fp, "\nError: %s\n", errmsg);
1102 * Show usage and exit
1104 * If you name all your usage variables with usage_xxx, then you can call this
1105 * help macro rather than expanding all arguments yourself.
1107 * @param errmsg If non-NULL, an error message to display
1109 #define usage(errmsg) \
1110 util_usage(errmsg, usage_synopsis, usage_short_opts, \
1111 usage_long_opts, usage_opts_help)
1113 void util_version(void)
1115 printf("Version: %s\n", "(U-Boot)");
1119 static void scan_args(struct display_info *disp, int argc, char *argv[])
1123 while ((opt = util_getopt_long()) != EOF) {
1128 case_USAGE_COMMON_FLAGS
1131 disp->show_addr = 1;
1137 type = FDT_NODE_HAS_PROP;
1143 type = FDT_IS_COMPAT;
1149 disp->flags |= FDT_REG_DIRECT_SUBNODES;
1152 disp->show_offset = 1;
1158 type = FDT_ANY_GLOBAL;
1164 disp->show_dts_version = 1;
1167 disp->region_list = 1;
1170 disp->list_strings = 1;
1173 disp->flags |= FDT_REG_ADD_MEM_RSVMAP;
1182 disp->output_fname = optarg;
1185 if (!strcmp(optarg, "dtb"))
1186 disp->output = OUT_DTB;
1187 else if (!strcmp(optarg, "dts"))
1188 disp->output = OUT_DTS;
1189 else if (!strcmp(optarg, "bin"))
1190 disp->output = OUT_BIN;
1192 usage("Unknown output format");
1201 disp->remove_strings = 1;
1204 disp->include_root = 1;
1207 disp->flags |= FDT_REG_ALL_SUBNODES;
1210 disp->flags &= ~FDT_REG_SUPERNODES;
1213 disp->flags |= FDT_REG_ADD_STRING_TAB;
1216 disp->add_aliases = 1;
1226 if (type && value_add(disp, &disp->value_head, type, inc,
1228 usage("Cannot add value");
1231 if (disp->invert && disp->types_exc)
1232 usage("-v has no meaning when used with 'exclude' conditions");
1235 int main(int argc, char *argv[])
1237 char *filename = NULL;
1238 struct display_info disp;
1242 memset(&disp, '\0', sizeof(disp));
1243 disp.flags = FDT_REG_SUPERNODES; /* Default flags */
1245 scan_args(&disp, argc, argv);
1247 /* Show matched lines in colour if we can */
1248 disp.colour = disp.all && isatty(0);
1250 /* Any additional arguments can match anything, just like -g */
1251 while (optind < argc - 1) {
1252 if (value_add(&disp, &disp.value_head, FDT_IS_ANY, 1,
1254 usage("Cannot add value");
1258 filename = argv[optind++];
1260 usage("Missing filename");
1262 /* If a valid .dtb is required, set flags to ensure we get one */
1263 if (disp.output == OUT_DTB) {
1265 disp.flags |= FDT_REG_ADD_MEM_RSVMAP | FDT_REG_ADD_STRING_TAB;
1268 if (disp.output_fname) {
1269 disp.fout = fopen(disp.output_fname, "w");
1271 usage("Cannot open output file");
1276 /* Run the grep and output the results */
1277 ret = do_fdtgrep(&disp, filename);
1278 if (disp.output_fname)