1
2
3
4
5
6
7
8
9
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 <unistd.h>
18
19#include "../include/libfdt.h"
20#include "libfdt_internal.h"
21
22
23#ifdef DEBUG
24#define debug(a, b...) fprintf(stderr, a, ## b)
25#else
26#define debug(a, b...)
27#endif
28
29
30struct value_node {
31 int type;
32 int include;
33 const char *string;
34 struct value_node *next;
35};
36
37
38enum output_t {
39 OUT_DTS,
40 OUT_DTB,
41 OUT_BIN,
42};
43
44
45struct display_info {
46 enum output_t output;
47 int add_aliases;
48 int all;
49 int colour;
50 int region_list;
51 int flags;
52 int list_strings;
53 int show_offset;
54 int show_addr;
55 int header;
56 int diff;
57 int include_root;
58 int remove_strings;
59 int show_dts_version;
60 int types_inc;
61 int types_exc;
62 int invert;
63 struct value_node *value_head;
64 const char *output_fname;
65 FILE *fout;
66};
67
68static void report_error(const char *where, int err)
69{
70 fprintf(stderr, "Error at '%s': %s\n", where, fdt_strerror(err));
71}
72
73
74enum {
75 COL_BLACK,
76 COL_RED,
77 COL_GREEN,
78 COL_YELLOW,
79 COL_BLUE,
80 COL_MAGENTA,
81 COL_CYAN,
82 COL_WHITE,
83
84 COL_NONE = -1,
85};
86
87
88
89
90
91
92
93static void print_ansi_colour(FILE *fout, int col)
94{
95 if (col == COL_NONE)
96 fprintf(fout, "\033[0m");
97 else
98 fprintf(fout, "\033[1;%dm", col + 30);
99}
100
101
102
103
104
105
106
107
108
109
110
111static int value_add(struct display_info *disp, struct value_node **headp,
112 int type, int include, const char *str)
113{
114 struct value_node *node;
115
116
117
118
119
120
121
122
123 if (include)
124 disp->types_inc |= type;
125 else
126 disp->types_exc |= type;
127 if (disp->types_inc & disp->types_exc & type) {
128 fprintf(stderr,
129 "Cannot use both include and exclude for '%s'\n", str);
130 return -1;
131 }
132
133 str = strdup(str);
134 node = malloc(sizeof(*node));
135 if (!str || !node) {
136 fprintf(stderr, "Out of memory\n");
137 return -1;
138 }
139 node->next = *headp;
140 node->type = type;
141 node->include = include;
142 node->string = str;
143 *headp = node;
144
145 return 0;
146}
147
148static bool util_is_printable_string(const void *data, int len)
149{
150 const char *s = data;
151 const char *ss, *se;
152
153
154 if (len == 0)
155 return 0;
156
157
158 if (s[len - 1] != '\0')
159 return 0;
160
161 se = s + len;
162
163 while (s < se) {
164 ss = s;
165 while (s < se && *s && isprint((unsigned char)*s))
166 s++;
167
168
169 if (*s != '\0' || s == ss)
170 return 0;
171
172 s++;
173 }
174
175 return 1;
176}
177
178static void utilfdt_print_data(const char *data, int len)
179{
180 int i;
181 const char *p = data;
182 const char *s;
183
184
185 if (len == 0)
186 return;
187
188 if (util_is_printable_string(data, len)) {
189 printf(" = ");
190
191 s = data;
192 do {
193 printf("\"%s\"", s);
194 s += strlen(s) + 1;
195 if (s < data + len)
196 printf(", ");
197 } while (s < data + len);
198
199 } else if ((len % 4) == 0) {
200 const uint32_t *cell = (const uint32_t *)data;
201
202 printf(" = <");
203 for (i = 0, len /= 4; i < len; i++)
204 printf("0x%08x%s", fdt32_to_cpu(cell[i]),
205 i < (len - 1) ? " " : "");
206 printf(">");
207 } else {
208 printf(" = [");
209 for (i = 0; i < len; i++)
210 printf("%02x%s", *p++, i < len - 1 ? " " : "");
211 printf("]");
212 }
213}
214
215
216
217
218
219
220
221
222
223
224
225
226
227static int display_fdt_by_regions(struct display_info *disp, const void *blob,
228 struct fdt_region region[], int count)
229{
230 struct fdt_region *reg = region, *reg_end = region + count;
231 uint32_t off_mem_rsvmap = fdt_off_mem_rsvmap(blob);
232 int base = fdt_off_dt_struct(blob);
233 int version = fdt_version(blob);
234 int offset, nextoffset;
235 int tag, depth, shift;
236 FILE *f = disp->fout;
237 uint64_t addr, size;
238 int in_region;
239 int file_ofs;
240 int i;
241
242 if (disp->show_dts_version)
243 fprintf(f, "/dts-v1/;\n");
244
245 if (disp->header) {
246 fprintf(f, "// magic:\t\t0x%x\n", fdt_magic(blob));
247 fprintf(f, "// totalsize:\t\t0x%x (%d)\n", fdt_totalsize(blob),
248 fdt_totalsize(blob));
249 fprintf(f, "// off_dt_struct:\t0x%x\n",
250 fdt_off_dt_struct(blob));
251 fprintf(f, "// off_dt_strings:\t0x%x\n",
252 fdt_off_dt_strings(blob));
253 fprintf(f, "// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
254 fprintf(f, "// version:\t\t%d\n", version);
255 fprintf(f, "// last_comp_version:\t%d\n",
256 fdt_last_comp_version(blob));
257 if (version >= 2) {
258 fprintf(f, "// boot_cpuid_phys:\t0x%x\n",
259 fdt_boot_cpuid_phys(blob));
260 }
261 if (version >= 3) {
262 fprintf(f, "// size_dt_strings:\t0x%x\n",
263 fdt_size_dt_strings(blob));
264 }
265 if (version >= 17) {
266 fprintf(f, "// size_dt_struct:\t0x%x\n",
267 fdt_size_dt_struct(blob));
268 }
269 fprintf(f, "\n");
270 }
271
272 if (disp->flags & FDT_REG_ADD_MEM_RSVMAP) {
273 const struct fdt_reserve_entry *p_rsvmap;
274
275 p_rsvmap = (const struct fdt_reserve_entry *)
276 ((const char *)blob + off_mem_rsvmap);
277 for (i = 0; ; i++) {
278 addr = fdt64_to_cpu(p_rsvmap[i].address);
279 size = fdt64_to_cpu(p_rsvmap[i].size);
280 if (addr == 0 && size == 0)
281 break;
282
283 fprintf(f, "/memreserve/ %llx %llx;\n",
284 (unsigned long long)addr,
285 (unsigned long long)size);
286 }
287 }
288
289 depth = 0;
290 nextoffset = 0;
291 shift = 4;
292 do {
293 const struct fdt_property *prop;
294 const char *name;
295 int show;
296 int len;
297
298 offset = nextoffset;
299
300
301
302
303
304 file_ofs = base + offset;
305 if (reg < reg_end && file_ofs >= reg->offset + reg->size)
306 reg++;
307 in_region = reg < reg_end && file_ofs >= reg->offset &&
308 file_ofs < reg->offset + reg->size;
309 tag = fdt_next_tag(blob, offset, &nextoffset);
310
311 if (tag == FDT_END)
312 break;
313 show = in_region || disp->all;
314 if (show && disp->diff)
315 fprintf(f, "%c", in_region ? '+' : '-');
316
317 if (!show) {
318
319 if (tag == FDT_BEGIN_NODE)
320 depth++;
321 else if (tag == FDT_END_NODE)
322 depth--;
323 continue;
324 }
325 if (tag != FDT_END) {
326 if (disp->show_addr)
327 fprintf(f, "%4x: ", file_ofs);
328 if (disp->show_offset)
329 fprintf(f, "%4x: ", file_ofs - base);
330 }
331
332
333 if (disp->colour)
334 print_ansi_colour(f, in_region ? COL_GREEN : COL_RED);
335
336 switch (tag) {
337 case FDT_PROP:
338 prop = fdt_get_property_by_offset(blob, offset, NULL);
339 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
340 fprintf(f, "%*s%s", depth * shift, "", name);
341 utilfdt_print_data(prop->data,
342 fdt32_to_cpu(prop->len));
343 fprintf(f, ";");
344 break;
345
346 case FDT_NOP:
347 fprintf(f, "%*s// [NOP]", depth * shift, "");
348 break;
349
350 case FDT_BEGIN_NODE:
351 name = fdt_get_name(blob, offset, &len);
352 fprintf(f, "%*s%s {", depth++ * shift, "",
353 *name ? name : "/");
354 break;
355
356 case FDT_END_NODE:
357 fprintf(f, "%*s};", --depth * shift, "");
358 break;
359 }
360
361
362 if (disp->colour)
363 print_ansi_colour(f, COL_NONE);
364 fprintf(f, "\n");
365 } while (1);
366
367
368 if (disp->list_strings) {
369 const char *str;
370 int str_base = fdt_off_dt_strings(blob);
371
372 for (offset = 0; offset < fdt_size_dt_strings(blob);
373 offset += strlen(str) + 1) {
374 str = fdt_string(blob, offset);
375 int len = strlen(str) + 1;
376 int show;
377
378
379 file_ofs = str_base + offset;
380 in_region = reg < reg_end &&
381 file_ofs >= reg->offset &&
382 file_ofs + len < reg->offset +
383 reg->size;
384 show = in_region || disp->all;
385 if (show && disp->diff)
386 printf("%c", in_region ? '+' : '-');
387 if (disp->show_addr)
388 printf("%4x: ", file_ofs);
389 if (disp->show_offset)
390 printf("%4x: ", offset);
391 printf("%s\n", str);
392 }
393 }
394
395 return 0;
396}
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423static int dump_fdt_regions(struct display_info *disp, const void *blob,
424 struct fdt_region region[], int count, char *out)
425{
426 struct fdt_header *fdt;
427 int size, struct_start;
428 int ptr;
429 int i;
430
431
432 fdt = (struct fdt_header *)out;
433 memset(fdt, '\0', sizeof(*fdt));
434 fdt_set_magic(fdt, FDT_MAGIC);
435 struct_start = FDT_ALIGN(sizeof(struct fdt_header),
436 sizeof(struct fdt_reserve_entry));
437 fdt_set_off_mem_rsvmap(fdt, struct_start);
438 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
439 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
440
441
442
443
444
445
446
447 for (i = size = 0; i < count; i++)
448 size += region[i].size;
449
450
451 if (count > 0 && (disp->flags & FDT_REG_ADD_MEM_RSVMAP)) {
452 struct_start += region[0].size;
453 size -= region[0].size;
454 }
455 fdt_set_off_dt_struct(fdt, struct_start);
456
457
458 if (count >= 2 && (disp->flags & FDT_REG_ADD_STRING_TAB)) {
459 int str_size;
460
461 str_size = region[count - 1].size;
462 fdt_set_size_dt_struct(fdt, size - str_size);
463 fdt_set_off_dt_strings(fdt, struct_start + size - str_size);
464 fdt_set_size_dt_strings(fdt, str_size);
465 fdt_set_totalsize(fdt, struct_start + size);
466 }
467
468
469 ptr = 0;
470 if (disp->header) {
471 ptr = sizeof(*fdt);
472 while (ptr < fdt_off_mem_rsvmap(fdt))
473 out[ptr++] = '\0';
474 }
475
476
477 for (i = 0; i < count; i++) {
478 struct fdt_region *reg = ®ion[i];
479
480 memcpy(out + ptr, (const char *)blob + reg->offset, reg->size);
481 ptr += reg->size;
482 }
483
484 return ptr;
485}
486
487
488
489
490
491
492
493
494
495
496static void show_region_list(struct fdt_region *reg, int count)
497{
498 int i;
499
500 printf("Regions: %d\n", count);
501 for (i = 0; i < count; i++, reg++) {
502 printf("%d: %-10x %-10x\n", i, reg->offset,
503 reg->offset + reg->size);
504 }
505}
506
507static int check_type_include(void *priv, int type, const char *data, int size)
508{
509 struct display_info *disp = priv;
510 struct value_node *val;
511 int match, none_match = FDT_IS_ANY;
512
513
514 debug("type=%x, data=%s\n", type, data ? data : "(null)");
515 if (!((disp->types_inc | disp->types_exc) & type)) {
516 debug(" - not in any condition\n");
517 return -1;
518 }
519
520
521
522
523
524
525 if (data) {
526 for (val = disp->value_head; val; val = val->next) {
527 if (!(type & val->type))
528 continue;
529 match = fdt_stringlist_contains(data, size,
530 val->string);
531 debug(" - val->type=%x, str='%s', match=%d\n",
532 val->type, val->string, match);
533 if (match && val->include) {
534 debug(" - match inc %s\n", val->string);
535 return 1;
536 }
537 if (match)
538 none_match &= ~val->type;
539 }
540 }
541
542
543
544
545
546 if ((type & disp->types_exc) && (none_match & type)) {
547 debug(" - match exc\n");
548
549
550
551
552 if (type == FDT_IS_NODE && disp->types_exc == FDT_ANY_GLOBAL) {
553 debug(" - supressed exc node\n");
554 return -1;
555 }
556 return 1;
557 }
558
559
560
561
562
563 if (type == FDT_IS_NODE && disp->types_inc == FDT_ANY_GLOBAL)
564 return -1;
565
566 debug(" - no match, types_inc=%x, types_exc=%x, none_match=%x\n",
567 disp->types_inc, disp->types_exc, none_match);
568
569 return 0;
570}
571
572
573
574
575
576
577
578
579
580
581
582
583static int h_include(void *priv, const void *fdt, int offset, int type,
584 const char *data, int size)
585{
586 struct display_info *disp = priv;
587 int inc, len;
588
589 inc = check_type_include(priv, type, data, size);
590 if (disp->include_root && type == FDT_IS_PROP && offset == 0 && inc)
591 return 1;
592
593
594
595
596
597 if (inc == -1 && type == FDT_IS_NODE) {
598 debug(" - checking compatible2\n");
599 data = fdt_getprop(fdt, offset, "compatible", &len);
600 inc = check_type_include(priv, FDT_IS_COMPAT, data, len);
601 }
602
603
604 if (inc != 1 && type == FDT_IS_NODE &&
605 (disp->types_inc & FDT_NODE_HAS_PROP)) {
606 debug(" - checking node '%s'\n",
607 fdt_get_name(fdt, offset, NULL));
608 for (offset = fdt_first_property_offset(fdt, offset);
609 offset > 0 && inc != 1;
610 offset = fdt_next_property_offset(fdt, offset)) {
611 const struct fdt_property *prop;
612 const char *str;
613
614 prop = fdt_get_property_by_offset(fdt, offset, NULL);
615 if (!prop)
616 continue;
617 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
618 inc = check_type_include(priv, FDT_NODE_HAS_PROP, str,
619 strlen(str));
620 }
621 if (inc == -1)
622 inc = 0;
623 }
624
625 switch (inc) {
626 case 1:
627 inc = !disp->invert;
628 break;
629 case 0:
630 inc = disp->invert;
631 break;
632 }
633 debug(" - returning %d\n", inc);
634
635 return inc;
636}
637
638static int h_cmp_region(const void *v1, const void *v2)
639{
640 const struct fdt_region *region1 = v1, *region2 = v2;
641
642 return region1->offset - region2->offset;
643}
644
645static int fdtgrep_find_regions(const void *fdt,
646 int (*include_func)(void *priv, const void *fdt, int offset,
647 int type, const char *data, int size),
648 struct display_info *disp, struct fdt_region *region,
649 int max_regions, char *path, int path_len, int flags)
650{
651 struct fdt_region_state state;
652 int count;
653 int ret;
654
655 count = 0;
656 ret = fdt_first_region(fdt, include_func, disp,
657 ®ion[count++], path, path_len,
658 disp->flags, &state);
659 while (ret == 0) {
660 ret = fdt_next_region(fdt, include_func, disp,
661 count < max_regions ? ®ion[count] : NULL,
662 path, path_len, disp->flags, &state);
663 if (!ret)
664 count++;
665 }
666 if (ret && ret != -FDT_ERR_NOTFOUND)
667 return ret;
668
669
670 if (disp->add_aliases && count < max_regions) {
671 int new_count;
672
673 new_count = fdt_add_alias_regions(fdt, region, count,
674 max_regions, &state);
675 if (new_count == -FDT_ERR_NOTFOUND) {
676
677 } else if (new_count < 0) {
678 return new_count;
679 } else if (new_count <= max_regions) {
680
681
682
683
684
685 count = new_count;
686 qsort(region, count, sizeof(struct fdt_region),
687 h_cmp_region);
688 }
689 }
690
691 return count;
692}
693
694int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
695{
696 int fd = 0;
697 char *buf = NULL;
698 off_t bufsize = 1024, offset = 0;
699 int ret = 0;
700
701 *buffp = NULL;
702 if (strcmp(filename, "-") != 0) {
703 fd = open(filename, O_RDONLY);
704 if (fd < 0)
705 return errno;
706 }
707
708
709 buf = malloc(bufsize);
710 if (!buf)
711 return -ENOMEM;
712 do {
713
714 if (offset == bufsize) {
715 bufsize *= 2;
716 buf = realloc(buf, bufsize);
717 if (!buf)
718 return -ENOMEM;
719 }
720
721 ret = read(fd, &buf[offset], bufsize - offset);
722 if (ret < 0) {
723 ret = errno;
724 break;
725 }
726 offset += ret;
727 } while (ret != 0);
728
729
730 close(fd);
731 if (ret)
732 free(buf);
733 else
734 *buffp = buf;
735 *len = bufsize;
736 return ret;
737}
738
739int utilfdt_read_err(const char *filename, char **buffp)
740{
741 off_t len;
742 return utilfdt_read_err_len(filename, buffp, &len);
743}
744
745char *utilfdt_read_len(const char *filename, off_t *len)
746{
747 char *buff;
748 int ret = utilfdt_read_err_len(filename, &buff, len);
749
750 if (ret) {
751 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
752 strerror(ret));
753 return NULL;
754 }
755
756 return buff;
757}
758
759char *utilfdt_read(const char *filename)
760{
761 off_t len;
762 return utilfdt_read_len(filename, &len);
763}
764
765
766
767
768
769
770
771
772static int do_fdtgrep(struct display_info *disp, const char *filename)
773{
774 struct fdt_region *region;
775 int max_regions;
776 int count = 100;
777 char path[1024];
778 char *blob;
779 int i, ret;
780
781 blob = utilfdt_read(filename);
782 if (!blob)
783 return -1;
784 ret = fdt_check_header(blob);
785 if (ret) {
786 fprintf(stderr, "Error: %s\n", fdt_strerror(ret));
787 return ret;
788 }
789
790
791 if (fdt_version(blob) < 17 && disp->value_head) {
792 fprintf(stderr,
793 "Warning: fdtgrep does not fully support version %d files\n",
794 fdt_version(blob));
795 }
796
797
798
799
800
801
802 for (i = 0; i < 3; i++) {
803 region = malloc(count * sizeof(struct fdt_region));
804 if (!region) {
805 fprintf(stderr, "Out of memory for %d regions\n",
806 count);
807 return -1;
808 }
809 max_regions = count;
810 count = fdtgrep_find_regions(blob,
811 h_include, disp,
812 region, max_regions, path, sizeof(path),
813 disp->flags);
814 if (count < 0) {
815 report_error("fdt_find_regions", count);
816 return -1;
817 }
818 if (count <= max_regions)
819 break;
820 free(region);
821 }
822
823
824 if (disp->region_list)
825 show_region_list(region, count);
826
827
828 if (disp->output == OUT_DTS) {
829 ret = display_fdt_by_regions(disp, blob, region, count);
830 } else {
831 void *fdt;
832
833 int size = fdt_totalsize(blob) + 16;
834
835 fdt = malloc(size);
836 if (!fdt) {
837 fprintf(stderr, "Out_of_memory\n");
838 ret = -1;
839 goto err;
840 }
841 size = dump_fdt_regions(disp, blob, region, count, fdt);
842 if (disp->remove_strings) {
843 void *out;
844
845 out = malloc(size);
846 if (!out) {
847 fprintf(stderr, "Out_of_memory\n");
848 ret = -1;
849 goto err;
850 }
851 ret = fdt_remove_unused_strings(fdt, out);
852 if (ret < 0) {
853 fprintf(stderr,
854 "Failed to remove unused strings: err=%d\n",
855 ret);
856 goto err;
857 }
858 free(fdt);
859 fdt = out;
860 ret = fdt_pack(fdt);
861 if (ret < 0) {
862 fprintf(stderr, "Failed to pack: err=%d\n",
863 ret);
864 goto err;
865 }
866 size = fdt_totalsize(fdt);
867 }
868
869 if (size != fwrite(fdt, 1, size, disp->fout)) {
870 fprintf(stderr, "Write failure, %d bytes\n", size);
871 free(fdt);
872 ret = 1;
873 goto err;
874 }
875 free(fdt);
876 }
877err:
878 free(blob);
879 free(region);
880
881 return ret;
882}
883
884static const char usage_synopsis[] =
885 "fdtgrep - extract portions from device tree\n"
886 "\n"
887 "Usage:\n"
888 " fdtgrep <options> <dt file>|-\n\n"
889 "Output formats are:\n"
890 "\tdts - device tree soure text\n"
891 "\tdtb - device tree blob (sets -Hmt automatically)\n"
892 "\tbin - device tree fragment (may not be a valid .dtb)";
893
894
895#define USAGE_COMMON_SHORT_OPTS "hV"
896
897
898#define a_argument required_argument
899
900
901#define USAGE_COMMON_LONG_OPTS \
902 {"help", no_argument, NULL, 'h'}, \
903 {"version", no_argument, NULL, 'V'}, \
904 {NULL, no_argument, NULL, 0x0}
905
906
907#define USAGE_COMMON_OPTS_HELP \
908 "Print this help and exit", \
909 "Print version and exit", \
910 NULL
911
912
913#define case_USAGE_COMMON_FLAGS \
914 case 'h': usage(NULL); \
915 case 'V': util_version(); \
916 case '?': usage("unknown option");
917
918static const char usage_short_opts[] =
919 "haAc:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTv"
920 USAGE_COMMON_SHORT_OPTS;
921static struct option const usage_long_opts[] = {
922 {"show-address", no_argument, NULL, 'a'},
923 {"colour", no_argument, NULL, 'A'},
924 {"include-node-with-prop", a_argument, NULL, 'b'},
925 {"include-compat", a_argument, NULL, 'c'},
926 {"exclude-compat", a_argument, NULL, 'C'},
927 {"diff", no_argument, NULL, 'd'},
928 {"enter-node", no_argument, NULL, 'e'},
929 {"show-offset", no_argument, NULL, 'f'},
930 {"include-match", a_argument, NULL, 'g'},
931 {"exclude-match", a_argument, NULL, 'G'},
932 {"show-header", no_argument, NULL, 'H'},
933 {"show-version", no_argument, NULL, 'I'},
934 {"list-regions", no_argument, NULL, 'l'},
935 {"list-strings", no_argument, NULL, 'L'},
936 {"include-mem", no_argument, NULL, 'm'},
937 {"include-node", a_argument, NULL, 'n'},
938 {"exclude-node", a_argument, NULL, 'N'},
939 {"include-prop", a_argument, NULL, 'p'},
940 {"exclude-prop", a_argument, NULL, 'P'},
941 {"remove-strings", no_argument, NULL, 'r'},
942 {"include-root", no_argument, NULL, 'R'},
943 {"show-subnodes", no_argument, NULL, 's'},
944 {"skip-supernodes", no_argument, NULL, 'S'},
945 {"show-stringtab", no_argument, NULL, 't'},
946 {"show-aliases", no_argument, NULL, 'T'},
947 {"out", a_argument, NULL, 'o'},
948 {"out-format", a_argument, NULL, 'O'},
949 {"invert-match", no_argument, NULL, 'v'},
950 USAGE_COMMON_LONG_OPTS,
951};
952static const char * const usage_opts_help[] = {
953 "Display address",
954 "Show all nodes/tags, colour those that match",
955 "Include contains containing property",
956 "Compatible nodes to include in grep",
957 "Compatible nodes to exclude in grep",
958 "Diff: Mark matching nodes with +, others with -",
959 "Enter direct subnode names of matching nodes",
960 "Display offset",
961 "Node/property/compatible string to include in grep",
962 "Node/property/compatible string to exclude in grep",
963 "Output a header",
964 "Put \"/dts-v1/;\" on first line of dts output",
965 "Output a region list",
966 "List strings in string table",
967 "Include mem_rsvmap section in binary output",
968 "Node to include in grep",
969 "Node to exclude in grep",
970 "Property to include in grep",
971 "Property to exclude in grep",
972 "Remove unused strings from string table",
973 "Include root node and all properties",
974 "Show all subnodes matching nodes",
975 "Don't include supernodes of matching nodes",
976 "Include string table in binary output",
977 "Include matching aliases in output",
978 "-o <output file>",
979 "-O <output format>",
980 "Invert the sense of matching (select non-matching lines)",
981 USAGE_COMMON_OPTS_HELP
982};
983
984
985
986
987
988
989#define util_getopt_long() getopt_long(argc, argv, usage_short_opts, \
990 usage_long_opts, NULL)
991
992void util_usage(const char *errmsg, const char *synopsis,
993 const char *short_opts, struct option const long_opts[],
994 const char * const opts_help[])
995{
996 FILE *fp = errmsg ? stderr : stdout;
997 const char a_arg[] = "<arg>";
998 size_t a_arg_len = strlen(a_arg) + 1;
999 size_t i;
1000 int optlen;
1001
1002 fprintf(fp,
1003 "Usage: %s\n"
1004 "\n"
1005 "Options: -[%s]\n", synopsis, short_opts);
1006
1007
1008 optlen = 0;
1009 for (i = 0; long_opts[i].name; ++i) {
1010
1011 int l = strlen(long_opts[i].name) + 1;
1012 if (long_opts[i].has_arg == a_argument)
1013 l += a_arg_len;
1014 if (optlen < l)
1015 optlen = l;
1016 }
1017
1018 for (i = 0; long_opts[i].name; ++i) {
1019
1020 assert(opts_help[i] != NULL);
1021
1022
1023 if (long_opts[i].val > '~')
1024 fprintf(fp, " ");
1025 else
1026 fprintf(fp, " -%c, ", long_opts[i].val);
1027
1028
1029 if (long_opts[i].has_arg == no_argument) {
1030 fprintf(fp, "--%-*s", optlen, long_opts[i].name);
1031 } else {
1032 fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
1033 (int)(optlen - strlen(long_opts[i].name) -
1034 a_arg_len), "");
1035 }
1036
1037
1038 fprintf(fp, "%s\n", opts_help[i]);
1039 }
1040
1041 if (errmsg) {
1042 fprintf(fp, "\nError: %s\n", errmsg);
1043 exit(EXIT_FAILURE);
1044 } else {
1045 exit(EXIT_SUCCESS);
1046 }
1047}
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057#define usage(errmsg) \
1058 util_usage(errmsg, usage_synopsis, usage_short_opts, \
1059 usage_long_opts, usage_opts_help)
1060
1061void util_version(void)
1062{
1063 printf("Version: %s\n", "(U-Boot)");
1064 exit(0);
1065}
1066
1067static void scan_args(struct display_info *disp, int argc, char *argv[])
1068{
1069 int opt;
1070
1071 while ((opt = util_getopt_long()) != EOF) {
1072 int type = 0;
1073 int inc = 1;
1074
1075 switch (opt) {
1076 case_USAGE_COMMON_FLAGS
1077 case 'a':
1078 disp->show_addr = 1;
1079 break;
1080 case 'A':
1081 disp->all = 1;
1082 break;
1083 case 'b':
1084 type = FDT_NODE_HAS_PROP;
1085 break;
1086 case 'C':
1087 inc = 0;
1088
1089 case 'c':
1090 type = FDT_IS_COMPAT;
1091 break;
1092 case 'd':
1093 disp->diff = 1;
1094 break;
1095 case 'e':
1096 disp->flags |= FDT_REG_DIRECT_SUBNODES;
1097 break;
1098 case 'f':
1099 disp->show_offset = 1;
1100 break;
1101 case 'G':
1102 inc = 0;
1103
1104 case 'g':
1105 type = FDT_ANY_GLOBAL;
1106 break;
1107 case 'H':
1108 disp->header = 1;
1109 break;
1110 case 'l':
1111 disp->region_list = 1;
1112 break;
1113 case 'L':
1114 disp->list_strings = 1;
1115 break;
1116 case 'm':
1117 disp->flags |= FDT_REG_ADD_MEM_RSVMAP;
1118 break;
1119 case 'N':
1120 inc = 0;
1121
1122 case 'n':
1123 type = FDT_IS_NODE;
1124 break;
1125 case 'o':
1126 disp->output_fname = optarg;
1127 break;
1128 case 'O':
1129 if (!strcmp(optarg, "dtb"))
1130 disp->output = OUT_DTB;
1131 else if (!strcmp(optarg, "dts"))
1132 disp->output = OUT_DTS;
1133 else if (!strcmp(optarg, "bin"))
1134 disp->output = OUT_BIN;
1135 else
1136 usage("Unknown output format");
1137 break;
1138 case 'P':
1139 inc = 0;
1140
1141 case 'p':
1142 type = FDT_IS_PROP;
1143 break;
1144 case 'r':
1145 disp->remove_strings = 1;
1146 break;
1147 case 'R':
1148 disp->include_root = 1;
1149 break;
1150 case 's':
1151 disp->flags |= FDT_REG_ALL_SUBNODES;
1152 break;
1153 case 'S':
1154 disp->flags &= ~FDT_REG_SUPERNODES;
1155 break;
1156 case 't':
1157 disp->flags |= FDT_REG_ADD_STRING_TAB;
1158 break;
1159 case 'T':
1160 disp->add_aliases = 1;
1161 break;
1162 case 'v':
1163 disp->invert = 1;
1164 break;
1165 case 'I':
1166 disp->show_dts_version = 1;
1167 break;
1168 }
1169
1170 if (type && value_add(disp, &disp->value_head, type, inc,
1171 optarg))
1172 usage("Cannot add value");
1173 }
1174
1175 if (disp->invert && disp->types_exc)
1176 usage("-v has no meaning when used with 'exclude' conditions");
1177}
1178
1179int main(int argc, char *argv[])
1180{
1181 char *filename = NULL;
1182 struct display_info disp;
1183 int ret;
1184
1185
1186 memset(&disp, '\0', sizeof(disp));
1187 disp.flags = FDT_REG_SUPERNODES;
1188
1189 scan_args(&disp, argc, argv);
1190
1191
1192 disp.colour = disp.all && isatty(0);
1193
1194
1195 while (optind < argc - 1) {
1196 if (value_add(&disp, &disp.value_head, FDT_IS_ANY, 1,
1197 argv[optind++]))
1198 usage("Cannot add value");
1199 }
1200
1201 if (optind < argc)
1202 filename = argv[optind++];
1203 if (!filename)
1204 usage("Missing filename");
1205
1206
1207 if (disp.output == OUT_DTB) {
1208 disp.header = 1;
1209 disp.flags |= FDT_REG_ADD_MEM_RSVMAP | FDT_REG_ADD_STRING_TAB;
1210 }
1211
1212 if (disp.output_fname) {
1213 disp.fout = fopen(disp.output_fname, "w");
1214 if (!disp.fout)
1215 usage("Cannot open output file");
1216 } else {
1217 disp.fout = stdout;
1218 }
1219
1220
1221 ret = do_fdtgrep(&disp, filename);
1222 if (disp.output_fname)
1223 fclose(disp.fout);
1224 if (ret)
1225 return 1;
1226
1227 return 0;
1228}
1229