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 struct value_node *value_head; /* List of values to match */
67 const char *output_fname; /* Output filename */
68 FILE *fout; /* File to write dts/dtb output */
71 static void report_error(const char *where, int err)
73 fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
76 /* Supported ANSI colours */
91 * print_ansi_colour() - Print out the ANSI sequence for a colour
94 * @col: Colour to output (COL_...), or COL_NONE to reset colour
96 static void print_ansi_colour(FILE *fout, int col)
99 fprintf(fout, "\033[0m");
101 fprintf(fout, "\033[1;%dm", col + 30);
106 * value_add() - Add a new value to our list of things to grep for
108 * @disp: Display structure, holding info about our options
109 * @headp: Pointer to header pointer of list
110 * @type: Type of this value (FDT_IS_...)
111 * @include: 1 if we want to include matches, 0 to exclude
112 * @str: String value to match
114 static int value_add(struct display_info *disp, struct value_node **headp,
115 int type, int include, const char *str)
117 struct value_node *node;
120 * Keep track of which types we are excluding/including. We don't
121 * allow both including and excluding things, because it doesn't make
122 * sense. 'Including' means that everything not mentioned is
123 * excluded. 'Excluding' means that everything not mentioned is
124 * included. So using the two together would be meaningless.
127 disp->types_inc |= type;
129 disp->types_exc |= type;
130 if (disp->types_inc & disp->types_exc & type) {
132 "Cannot use both include and exclude for '%s'\n", str);
139 node = malloc(sizeof(*node));
144 node->include = include;
150 fprintf(stderr, "Out of memory\n");
154 static bool util_is_printable_string(const void *data, int len)
156 const char *s = data;
159 /* zero length is not */
163 /* must terminate with zero */
164 if (s[len - 1] != '\0')
171 while (s < se && *s && isprint((unsigned char)*s))
174 /* not zero, or not done yet */
175 if (*s != '\0' || s == ss)
184 static void utilfdt_print_data(const char *data, int len)
187 const char *p = data;
190 /* no data, don't print */
194 if (util_is_printable_string(data, len)) {
203 } while (s < data + len);
205 } else if ((len % 4) == 0) {
206 const uint32_t *cell = (const uint32_t *)data;
209 for (i = 0, len /= 4; i < len; i++)
210 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
211 i < (len - 1) ? " " : "");
215 for (i = 0; i < len; i++)
216 printf("%02x%s", (unsigned char)*p++, i < len - 1 ? " " : "");
222 * display_fdt_by_regions() - Display regions of an FDT source
224 * This dumps an FDT as source, but only certain regions of it. This is the
225 * final stage of the grep - we have a list of regions we want to display,
226 * and this function displays them.
228 * @disp: Display structure, holding info about our options
229 * @blob: FDT blob to display
230 * @region: List of regions to display
231 * @count: Number of regions
233 static int display_fdt_by_regions(struct display_info *disp, const void *blob,
234 struct fdt_region region[], int count)
236 struct fdt_region *reg = region, *reg_end = region + count;
237 uint32_t off_mem_rsvmap = fdt_off_mem_rsvmap(blob);
238 int base = fdt_off_dt_struct(blob);
239 int version = fdt_version(blob);
240 int offset, nextoffset;
241 int tag, depth, shift;
242 FILE *f = disp->fout;
248 if (disp->show_dts_version)
249 fprintf(f, "/dts-v1/;\n");
252 fprintf(f, "// magic:\t\t0x%x\n", fdt_magic(blob));
253 fprintf(f, "// totalsize:\t\t0x%x (%d)\n", fdt_totalsize(blob),
254 fdt_totalsize(blob));
255 fprintf(f, "// off_dt_struct:\t0x%x\n",
256 fdt_off_dt_struct(blob));
257 fprintf(f, "// off_dt_strings:\t0x%x\n",
258 fdt_off_dt_strings(blob));
259 fprintf(f, "// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
260 fprintf(f, "// version:\t\t%d\n", version);
261 fprintf(f, "// last_comp_version:\t%d\n",
262 fdt_last_comp_version(blob));
264 fprintf(f, "// boot_cpuid_phys:\t0x%x\n",
265 fdt_boot_cpuid_phys(blob));
268 fprintf(f, "// size_dt_strings:\t0x%x\n",
269 fdt_size_dt_strings(blob));
272 fprintf(f, "// size_dt_struct:\t0x%x\n",
273 fdt_size_dt_struct(blob));
278 if (disp->flags & FDT_REG_ADD_MEM_RSVMAP) {
279 const struct fdt_reserve_entry *p_rsvmap;
281 p_rsvmap = (const struct fdt_reserve_entry *)
282 ((const char *)blob + off_mem_rsvmap);
284 addr = fdt64_to_cpu(p_rsvmap[i].address);
285 size = fdt64_to_cpu(p_rsvmap[i].size);
286 if (addr == 0 && size == 0)
289 fprintf(f, "/memreserve/ %llx %llx;\n",
290 (unsigned long long)addr,
291 (unsigned long long)size);
297 shift = 4; /* 4 spaces per indent */
299 const struct fdt_property *prop;
307 * Work out the file offset of this offset, and decide
308 * whether it is in the region list or not
310 file_ofs = base + offset;
311 if (reg < reg_end && file_ofs >= reg->offset + reg->size)
313 in_region = reg < reg_end && file_ofs >= reg->offset &&
314 file_ofs < reg->offset + reg->size;
315 tag = fdt_next_tag(blob, offset, &nextoffset);
319 show = in_region || disp->all;
320 if (show && disp->diff)
321 fprintf(f, "%c", in_region ? '+' : '-');
324 /* Do this here to avoid 'if (show)' in every 'case' */
325 if (tag == FDT_BEGIN_NODE)
327 else if (tag == FDT_END_NODE)
331 if (tag != FDT_END) {
333 fprintf(f, "%4x: ", file_ofs);
334 if (disp->show_offset)
335 fprintf(f, "%4x: ", file_ofs - base);
338 /* Green means included, red means excluded */
340 print_ansi_colour(f, in_region ? COL_GREEN : COL_RED);
344 prop = fdt_get_property_by_offset(blob, offset, NULL);
345 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
346 fprintf(f, "%*s%s", depth * shift, "", name);
347 utilfdt_print_data(prop->data,
348 fdt32_to_cpu(prop->len));
353 fprintf(f, "%*s// [NOP]", depth * shift, "");
357 name = fdt_get_name(blob, offset, &len);
358 fprintf(f, "%*s%s {", depth++ * shift, "",
363 fprintf(f, "%*s};", --depth * shift, "");
367 /* Reset colour back to normal before end of line */
369 print_ansi_colour(f, COL_NONE);
373 /* Print a list of strings if requested */
374 if (disp->list_strings) {
376 int str_base = fdt_off_dt_strings(blob);
378 for (offset = 0; offset < fdt_size_dt_strings(blob);
379 offset += strlen(str) + 1) {
380 str = fdt_string(blob, offset);
381 int len = strlen(str) + 1;
384 /* Only print strings that are in the region */
385 file_ofs = str_base + offset;
386 in_region = reg < reg_end &&
387 file_ofs >= reg->offset &&
388 file_ofs + len < reg->offset +
390 show = in_region || disp->all;
391 if (show && disp->diff)
392 printf("%c", in_region ? '+' : '-');
394 printf("%4x: ", file_ofs);
395 if (disp->show_offset)
396 printf("%4x: ", offset);
405 * dump_fdt_regions() - Dump regions of an FDT as binary data
407 * This dumps an FDT as binary, but only certain regions of it. This is the
408 * final stage of the grep - we have a list of regions we want to dump,
409 * and this function dumps them.
411 * The output of this function may or may not be a valid FDT. To ensure it
412 * is, these disp->flags must be set:
414 * FDT_REG_SUPERNODES: ensures that subnodes are preceded by their
415 * parents. Without this option, fragments of subnode data may be
416 * output without the supernodes above them. This is useful for
417 * hashing but cannot produce a valid FDT.
418 * FDT_REG_ADD_STRING_TAB: Adds a string table to the end of the FDT.
419 * Without this none of the properties will have names
420 * FDT_REG_ADD_MEM_RSVMAP: Adds a mem_rsvmap table - an FDT is invalid
423 * @disp: Display structure, holding info about our options
424 * @blob: FDT blob to display
425 * @region: List of regions to display
426 * @count: Number of regions
427 * @out: Output destination
429 static int dump_fdt_regions(struct display_info *disp, const void *blob,
430 struct fdt_region region[], int count, char *out)
432 struct fdt_header *fdt;
433 int size, struct_start;
437 /* Set up a basic header (even if we don't actually write it) */
438 fdt = (struct fdt_header *)out;
439 memset(fdt, '\0', sizeof(*fdt));
440 fdt_set_magic(fdt, FDT_MAGIC);
441 struct_start = sizeof(struct fdt_header);
442 fdt_set_off_mem_rsvmap(fdt, struct_start);
443 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
444 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
447 * Calculate the total size of the regions we are writing out. The
448 * first will be the mem_rsvmap if the FDT_REG_ADD_MEM_RSVMAP flag
449 * is set. The last will be the string table if FDT_REG_ADD_STRING_TAB
452 for (i = size = 0; i < count; i++)
453 size += region[i].size;
455 /* Bring in the mem_rsvmap section from the old file if requested */
456 if (count > 0 && (disp->flags & FDT_REG_ADD_MEM_RSVMAP)) {
457 struct_start += region[0].size;
458 size -= region[0].size;
460 fdt_set_off_dt_struct(fdt, struct_start);
462 /* Update the header to have the correct offsets/sizes */
463 if (count >= 2 && (disp->flags & FDT_REG_ADD_STRING_TAB)) {
466 str_size = region[count - 1].size;
467 fdt_set_size_dt_struct(fdt, size - str_size);
468 fdt_set_off_dt_strings(fdt, struct_start + size - str_size);
469 fdt_set_size_dt_strings(fdt, str_size);
470 fdt_set_totalsize(fdt, struct_start + size);
473 /* Write the header if required */
477 while (ptr < fdt_off_mem_rsvmap(fdt))
481 /* Output all the nodes including any mem_rsvmap/string table */
482 for (i = 0; i < count; i++) {
483 struct fdt_region *reg = ®ion[i];
485 memcpy(out + ptr, (const char *)blob + reg->offset, reg->size);
493 * show_region_list() - Print out a list of regions
495 * The list includes the region offset (absolute offset from start of FDT
496 * blob in bytes) and size
498 * @reg: List of regions to print
499 * @count: Number of regions
501 static void show_region_list(struct fdt_region *reg, int count)
505 printf("Regions: %d\n", count);
506 for (i = 0; i < count; i++, reg++) {
507 printf("%d: %-10x %-10x\n", i, reg->offset,
508 reg->offset + reg->size);
512 static int check_type_include(void *priv, int type, const char *data, int size)
514 struct display_info *disp = priv;
515 struct value_node *val;
516 int match, none_match = FDT_IS_ANY;
518 /* If none of our conditions mention this type, we know nothing */
519 debug("type=%x, data=%s\n", type, data ? data : "(null)");
520 if (!((disp->types_inc | disp->types_exc) & type)) {
521 debug(" - not in any condition\n");
526 * Go through the list of conditions. For inclusive conditions, we
527 * return 1 at the first match. For exclusive conditions, we must
528 * check that there are no matches.
531 for (val = disp->value_head; val; val = val->next) {
532 if (!(type & val->type))
534 match = fdt_stringlist_contains(data, size,
536 debug(" - val->type=%x, str='%s', match=%d\n",
537 val->type, val->string, match);
538 if (match && val->include) {
539 debug(" - match inc %s\n", val->string);
543 none_match &= ~val->type;
548 * If this is an exclusive condition, and nothing matches, then we
551 if ((type & disp->types_exc) && (none_match & type)) {
552 debug(" - match exc\n");
554 * Allow FDT_IS_COMPAT to make the final decision in the
555 * case where there is no specific type
557 if (type == FDT_IS_NODE && disp->types_exc == FDT_ANY_GLOBAL) {
558 debug(" - supressed exc node\n");
565 * Allow FDT_IS_COMPAT to make the final decision in the
566 * case where there is no specific type (inclusive)
568 if (type == FDT_IS_NODE && disp->types_inc == FDT_ANY_GLOBAL)
571 debug(" - no match, types_inc=%x, types_exc=%x, none_match=%x\n",
572 disp->types_inc, disp->types_exc, none_match);
578 * h_include() - Include handler function for fdt_find_regions()
580 * This function decides whether to include or exclude a node, property or
581 * compatible string. The function is defined by fdt_find_regions().
583 * The algorithm is documented in the code - disp->invert is 0 for normal
584 * operation, and 1 to invert the sense of all matches.
588 static int h_include(void *priv, const void *fdt, int offset, int type,
589 const char *data, int size)
591 struct display_info *disp = priv;
594 inc = check_type_include(priv, type, data, size);
595 if (disp->include_root && type == FDT_IS_PROP && offset == 0 && inc)
599 * If the node name does not tell us anything, check the
602 if (inc == -1 && type == FDT_IS_NODE) {
603 debug(" - checking compatible2\n");
604 data = fdt_getprop(fdt, offset, "compatible", &len);
605 inc = check_type_include(priv, FDT_IS_COMPAT, data, len);
608 /* If we still have no idea, check for properties in the node */
609 if (inc != 1 && type == FDT_IS_NODE &&
610 (disp->types_inc & FDT_NODE_HAS_PROP)) {
611 debug(" - checking node '%s'\n",
612 fdt_get_name(fdt, offset, NULL));
613 for (offset = fdt_first_property_offset(fdt, offset);
614 offset > 0 && inc != 1;
615 offset = fdt_next_property_offset(fdt, offset)) {
616 const struct fdt_property *prop;
619 prop = fdt_get_property_by_offset(fdt, offset, NULL);
622 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
623 inc = check_type_include(priv, FDT_NODE_HAS_PROP, str,
638 debug(" - returning %d\n", inc);
643 static int h_cmp_region(const void *v1, const void *v2)
645 const struct fdt_region *region1 = v1, *region2 = v2;
647 return region1->offset - region2->offset;
650 static int fdtgrep_find_regions(const void *fdt,
651 int (*include_func)(void *priv, const void *fdt, int offset,
652 int type, const char *data, int size),
653 struct display_info *disp, struct fdt_region *region,
654 int max_regions, char *path, int path_len, int flags)
656 struct fdt_region_state state;
661 ret = fdt_first_region(fdt, include_func, disp,
662 ®ion[count++], path, path_len,
663 disp->flags, &state);
665 ret = fdt_next_region(fdt, include_func, disp,
666 count < max_regions ? ®ion[count] : NULL,
667 path, path_len, disp->flags, &state);
671 if (ret && ret != -FDT_ERR_NOTFOUND)
674 /* Find all the aliases and add those regions back in */
675 if (disp->add_aliases && count < max_regions) {
678 new_count = fdt_add_alias_regions(fdt, region, count,
679 max_regions, &state);
680 if (new_count == -FDT_ERR_NOTFOUND) {
681 /* No alias node found */
682 } else if (new_count < 0) {
684 } else if (new_count <= max_regions) {
686 * The alias regions will now be at the end of the list.
687 * Sort the regions by offset to get things into the
691 qsort(region, count, sizeof(struct fdt_region),
699 int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
701 int fd = 0; /* assume stdin */
703 off_t bufsize = 1024, offset = 0;
707 if (strcmp(filename, "-") != 0) {
708 fd = open(filename, O_RDONLY);
713 /* Loop until we have read everything */
714 buf = malloc(bufsize);
718 /* Expand the buffer to hold the next chunk */
719 if (offset == bufsize) {
721 buf = realloc(buf, bufsize);
726 ret = read(fd, &buf[offset], bufsize - offset);
734 /* Clean up, including closing stdin; return errno on error */
744 int utilfdt_read_err(const char *filename, char **buffp)
747 return utilfdt_read_err_len(filename, buffp, &len);
750 char *utilfdt_read_len(const char *filename, off_t *len)
753 int ret = utilfdt_read_err_len(filename, &buff, len);
756 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
760 /* Successful read */
764 char *utilfdt_read(const char *filename)
767 return utilfdt_read_len(filename, &len);
771 * Run the main fdtgrep operation, given a filename and valid arguments
773 * @param disp Display information / options
774 * @param filename Filename of blob file
775 * @param return 0 if ok, -ve on error
777 static int do_fdtgrep(struct display_info *disp, const char *filename)
779 struct fdt_region *region = NULL;
786 blob = utilfdt_read(filename);
789 ret = fdt_check_header(blob);
791 fprintf(stderr, "Error: %s\n", fdt_strerror(ret));
795 /* Allow old files, but they are untested */
796 if (fdt_version(blob) < 17 && disp->value_head) {
798 "Warning: fdtgrep does not fully support version %d files\n",
803 * We do two passes, since we don't know how many regions we need.
804 * The first pass will count the regions, but if it is too many,
805 * we do another pass to actually record them.
807 for (i = 0; i < 2; i++) {
808 region = realloc(region, count * sizeof(struct fdt_region));
810 fprintf(stderr, "Out of memory for %d regions\n",
815 count = fdtgrep_find_regions(blob,
817 region, max_regions, path, sizeof(path),
820 report_error("fdt_find_regions", count);
824 if (count <= max_regions)
827 if (count > max_regions) {
829 fprintf(stderr, "Internal error with fdtgrep_find_region()\n");
833 /* Optionally print a list of regions */
834 if (disp->region_list)
835 show_region_list(region, count);
837 /* Output either source .dts or binary .dtb */
838 if (disp->output == OUT_DTS) {
839 ret = display_fdt_by_regions(disp, blob, region, count);
842 /* Allow reserved memory section to expand slightly */
843 int size = fdt_totalsize(blob) + 16;
847 fprintf(stderr, "Out_of_memory\n");
851 size = dump_fdt_regions(disp, blob, region, count, fdt);
852 if (disp->remove_strings) {
857 fprintf(stderr, "Out_of_memory\n");
861 ret = fdt_remove_unused_strings(fdt, out);
864 "Failed to remove unused strings: err=%d\n",
872 fprintf(stderr, "Failed to pack: err=%d\n",
876 size = fdt_totalsize(fdt);
879 if (size != fwrite(fdt, 1, size, disp->fout)) {
880 fprintf(stderr, "Write failure, %d bytes\n", size);
894 static const char usage_synopsis[] =
895 "fdtgrep - extract portions from device tree\n"
898 " fdtgrep <options> <dt file>|-\n\n"
899 "Output formats are:\n"
900 "\tdts - device tree soure text\n"
901 "\tdtb - device tree blob (sets -Hmt automatically)\n"
902 "\tbin - device tree fragment (may not be a valid .dtb)";
904 /* Helper for usage_short_opts string constant */
905 #define USAGE_COMMON_SHORT_OPTS "hV"
907 /* Helper for aligning long_opts array */
908 #define a_argument required_argument
910 /* Helper for usage_long_opts option array */
911 #define USAGE_COMMON_LONG_OPTS \
912 {"help", no_argument, NULL, 'h'}, \
913 {"version", no_argument, NULL, 'V'}, \
914 {NULL, no_argument, NULL, 0x0}
916 /* Helper for usage_opts_help array */
917 #define USAGE_COMMON_OPTS_HELP \
918 "Print this help and exit", \
919 "Print version and exit", \
922 /* Helper for getopt case statements */
923 #define case_USAGE_COMMON_FLAGS \
924 case 'h': usage(NULL); \
926 case 'V': util_version(); \
928 case '?': usage("unknown option");
930 static const char usage_short_opts[] =
931 "haAc:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTv"
932 USAGE_COMMON_SHORT_OPTS;
933 static struct option const usage_long_opts[] = {
934 {"show-address", no_argument, NULL, 'a'},
935 {"colour", no_argument, NULL, 'A'},
936 {"include-node-with-prop", a_argument, NULL, 'b'},
937 {"include-compat", a_argument, NULL, 'c'},
938 {"exclude-compat", a_argument, NULL, 'C'},
939 {"diff", no_argument, NULL, 'd'},
940 {"enter-node", no_argument, NULL, 'e'},
941 {"show-offset", no_argument, NULL, 'f'},
942 {"include-match", a_argument, NULL, 'g'},
943 {"exclude-match", a_argument, NULL, 'G'},
944 {"show-header", no_argument, NULL, 'H'},
945 {"show-version", no_argument, NULL, 'I'},
946 {"list-regions", no_argument, NULL, 'l'},
947 {"list-strings", no_argument, NULL, 'L'},
948 {"include-mem", no_argument, NULL, 'm'},
949 {"include-node", a_argument, NULL, 'n'},
950 {"exclude-node", a_argument, NULL, 'N'},
951 {"include-prop", a_argument, NULL, 'p'},
952 {"exclude-prop", a_argument, NULL, 'P'},
953 {"remove-strings", no_argument, NULL, 'r'},
954 {"include-root", no_argument, NULL, 'R'},
955 {"show-subnodes", no_argument, NULL, 's'},
956 {"skip-supernodes", no_argument, NULL, 'S'},
957 {"show-stringtab", no_argument, NULL, 't'},
958 {"show-aliases", no_argument, NULL, 'T'},
959 {"out", a_argument, NULL, 'o'},
960 {"out-format", a_argument, NULL, 'O'},
961 {"invert-match", no_argument, NULL, 'v'},
962 USAGE_COMMON_LONG_OPTS,
964 static const char * const usage_opts_help[] = {
966 "Show all nodes/tags, colour those that match",
967 "Include contains containing property",
968 "Compatible nodes to include in grep",
969 "Compatible nodes to exclude in grep",
970 "Diff: Mark matching nodes with +, others with -",
971 "Enter direct subnode names of matching nodes",
973 "Node/property/compatible string to include in grep",
974 "Node/property/compatible string to exclude in grep",
976 "Put \"/dts-v1/;\" on first line of dts output",
977 "Output a region list",
978 "List strings in string table",
979 "Include mem_rsvmap section in binary output",
980 "Node to include in grep",
981 "Node to exclude in grep",
982 "Property to include in grep",
983 "Property to exclude in grep",
984 "Remove unused strings from string table",
985 "Include root node and all properties",
986 "Show all subnodes matching nodes",
987 "Don't include supernodes of matching nodes",
988 "Include string table in binary output",
989 "Include matching aliases in output",
991 "-O <output format>",
992 "Invert the sense of matching (select non-matching lines)",
993 USAGE_COMMON_OPTS_HELP
997 * Call getopt_long() with standard options
999 * Since all util code runs getopt in the same way, provide a helper.
1001 #define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
1002 usage_long_opts, NULL)
1004 void util_usage(const char *errmsg, const char *synopsis,
1005 const char *short_opts, struct option const long_opts[],
1006 const char * const opts_help[])
1008 FILE *fp = errmsg ? stderr : stdout;
1009 const char a_arg[] = "<arg>";
1010 size_t a_arg_len = strlen(a_arg) + 1;
1017 "Options: -[%s]\n", synopsis, short_opts);
1019 /* prescan the --long opt length to auto-align */
1021 for (i = 0; long_opts[i].name; ++i) {
1022 /* +1 is for space between --opt and help text */
1023 int l = strlen(long_opts[i].name) + 1;
1024 if (long_opts[i].has_arg == a_argument)
1030 for (i = 0; long_opts[i].name; ++i) {
1031 /* helps when adding new applets or options */
1032 assert(opts_help[i] != NULL);
1034 /* first output the short flag if it has one */
1035 if (long_opts[i].val > '~')
1038 fprintf(fp, " -%c, ", long_opts[i].val);
1040 /* then the long flag */
1041 if (long_opts[i].has_arg == no_argument) {
1042 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
1044 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
1045 (int)(optlen - strlen(long_opts[i].name) -
1049 /* finally the help text */
1050 fprintf(fp, "%s\n", opts_help[i]);
1054 fprintf(fp, "\nError: %s\n", errmsg);
1062 * Show usage and exit
1064 * If you name all your usage variables with usage_xxx, then you can call this
1065 * help macro rather than expanding all arguments yourself.
1067 * @param errmsg If non-NULL, an error message to display
1069 #define usage(errmsg) \
1070 util_usage(errmsg, usage_synopsis, usage_short_opts, \
1071 usage_long_opts, usage_opts_help)
1073 void util_version(void)
1075 printf("Version: %s\n", "(U-Boot)");
1079 static void scan_args(struct display_info *disp, int argc, char *argv[])
1083 while ((opt = util_getopt_long()) != EOF) {
1088 case_USAGE_COMMON_FLAGS
1091 disp->show_addr = 1;
1097 type = FDT_NODE_HAS_PROP;
1103 type = FDT_IS_COMPAT;
1109 disp->flags |= FDT_REG_DIRECT_SUBNODES;
1112 disp->show_offset = 1;
1118 type = FDT_ANY_GLOBAL;
1124 disp->region_list = 1;
1127 disp->list_strings = 1;
1130 disp->flags |= FDT_REG_ADD_MEM_RSVMAP;
1139 disp->output_fname = optarg;
1142 if (!strcmp(optarg, "dtb"))
1143 disp->output = OUT_DTB;
1144 else if (!strcmp(optarg, "dts"))
1145 disp->output = OUT_DTS;
1146 else if (!strcmp(optarg, "bin"))
1147 disp->output = OUT_BIN;
1149 usage("Unknown output format");
1158 disp->remove_strings = 1;
1161 disp->include_root = 1;
1164 disp->flags |= FDT_REG_ALL_SUBNODES;
1167 disp->flags &= ~FDT_REG_SUPERNODES;
1170 disp->flags |= FDT_REG_ADD_STRING_TAB;
1173 disp->add_aliases = 1;
1179 disp->show_dts_version = 1;
1183 if (type && value_add(disp, &disp->value_head, type, inc,
1185 usage("Cannot add value");
1188 if (disp->invert && disp->types_exc)
1189 usage("-v has no meaning when used with 'exclude' conditions");
1192 int main(int argc, char *argv[])
1194 char *filename = NULL;
1195 struct display_info disp;
1199 memset(&disp, '\0', sizeof(disp));
1200 disp.flags = FDT_REG_SUPERNODES; /* Default flags */
1202 scan_args(&disp, argc, argv);
1204 /* Show matched lines in colour if we can */
1205 disp.colour = disp.all && isatty(0);
1207 /* Any additional arguments can match anything, just like -g */
1208 while (optind < argc - 1) {
1209 if (value_add(&disp, &disp.value_head, FDT_IS_ANY, 1,
1211 usage("Cannot add value");
1215 filename = argv[optind++];
1217 usage("Missing filename");
1219 /* If a valid .dtb is required, set flags to ensure we get one */
1220 if (disp.output == OUT_DTB) {
1222 disp.flags |= FDT_REG_ADD_MEM_RSVMAP | FDT_REG_ADD_STRING_TAB;
1225 if (disp.output_fname) {
1226 disp.fout = fopen(disp.output_fname, "w");
1228 usage("Cannot open output file");
1233 /* Run the grep and output the results */
1234 ret = do_fdtgrep(&disp, filename);
1235 if (disp.output_fname)