2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * Command line options
29 int quiet
; /* Level of quietness */
30 int reservenum
; /* Number of memory reservation slots */
31 int minsize
; /* Minimum blob size */
32 int padsize
; /* Additional padding to blob */
33 int alignsize
; /* Additional padding to blob accroding to the alignsize */
34 int phandle_format
= PHANDLE_EPAPR
; /* Use linux,phandle or phandle properties */
35 int generate_symbols
; /* enable symbols & fixup support */
36 int generate_fixups
; /* suppress generation of fixups on symbol support */
37 int auto_label_aliases
; /* auto generate labels -> aliases */
39 static int is_power_of_2(int x
)
41 return (x
> 0) && ((x
& (x
- 1)) == 0);
44 static void fill_fullpaths(struct node
*tree
, const char *prefix
)
49 tree
->fullpath
= join_path(prefix
, tree
->name
);
51 unit
= strchr(tree
->name
, '@');
53 tree
->basenamelen
= unit
- tree
->name
;
55 tree
->basenamelen
= strlen(tree
->name
);
57 for_each_child(tree
, child
)
58 fill_fullpaths(child
, tree
->fullpath
);
61 /* Usage related data. */
62 static const char usage_synopsis
[] = "dtc [options] <input file>";
63 static const char usage_short_opts
[] = "qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@Ahv";
64 static struct option
const usage_long_opts
[] = {
65 {"quiet", no_argument
, NULL
, 'q'},
66 {"in-format", a_argument
, NULL
, 'I'},
67 {"out", a_argument
, NULL
, 'o'},
68 {"out-format", a_argument
, NULL
, 'O'},
69 {"out-version", a_argument
, NULL
, 'V'},
70 {"out-dependency", a_argument
, NULL
, 'd'},
71 {"reserve", a_argument
, NULL
, 'R'},
72 {"space", a_argument
, NULL
, 'S'},
73 {"pad", a_argument
, NULL
, 'p'},
74 {"align", a_argument
, NULL
, 'a'},
75 {"boot-cpu", a_argument
, NULL
, 'b'},
76 {"force", no_argument
, NULL
, 'f'},
77 {"include", a_argument
, NULL
, 'i'},
78 {"sort", no_argument
, NULL
, 's'},
79 {"phandle", a_argument
, NULL
, 'H'},
80 {"warning", a_argument
, NULL
, 'W'},
81 {"error", a_argument
, NULL
, 'E'},
82 {"symbols", no_argument
, NULL
, '@'},
83 {"auto-alias", no_argument
, NULL
, 'A'},
84 {"help", no_argument
, NULL
, 'h'},
85 {"version", no_argument
, NULL
, 'v'},
86 {NULL
, no_argument
, NULL
, 0x0},
88 static const char * const usage_opts_help
[] = {
89 "\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
90 "\n\tInput formats are:\n"
91 "\t\tdts - device tree source text\n"
92 "\t\tdtb - device tree blob\n"
93 "\t\tfs - /proc/device-tree style directory",
95 "\n\tOutput formats are:\n"
96 "\t\tdts - device tree source text\n"
97 "\t\tdtb - device tree blob\n"
99 "\t\tyaml - device tree encoded as YAML\n"
101 "\t\tasm - assembler source",
102 "\n\tBlob version to produce, defaults to "stringify(DEFAULT_FDT_VERSION
)" (for dtb and asm output)",
103 "\n\tOutput dependency file",
104 "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
105 "\n\tMake the blob at least <bytes> long (extra space)",
106 "\n\tAdd padding to the blob of <bytes> long (extra space)",
107 "\n\tMake the blob align to the <bytes> (extra space)",
108 "\n\tSet the physical boot cpu",
109 "\n\tTry to produce output even if the input tree has errors",
110 "\n\tAdd a path to search for include files",
111 "\n\tSort nodes and properties before outputting (useful for comparing trees)",
112 "\n\tValid phandle formats are:\n"
113 "\t\tlegacy - \"linux,phandle\" properties only\n"
114 "\t\tepapr - \"phandle\" properties only\n"
115 "\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
116 "\n\tEnable/disable warnings (prefix with \"no-\")",
117 "\n\tEnable/disable errors (prefix with \"no-\")",
118 "\n\tEnable generation of symbols",
119 "\n\tEnable auto-alias of labels",
120 "\n\tPrint this help and exit",
121 "\n\tPrint version and exit",
125 static const char *guess_type_by_name(const char *fname
, const char *fallback
)
129 s
= strrchr(fname
, '.');
132 if (!strcasecmp(s
, ".dts"))
134 if (!strcasecmp(s
, ".yaml"))
136 if (!strcasecmp(s
, ".dtb"))
141 static const char *guess_input_format(const char *fname
, const char *fallback
)
147 if (stat(fname
, &statbuf
) != 0)
150 if (S_ISDIR(statbuf
.st_mode
))
153 if (!S_ISREG(statbuf
.st_mode
))
156 f
= fopen(fname
, "r");
159 if (fread(&magic
, 4, 1, f
) != 1) {
165 if (fdt32_to_cpu(magic
) == FDT_MAGIC
)
168 return guess_type_by_name(fname
, fallback
);
171 int main(int argc
, char *argv
[])
174 const char *inform
= NULL
;
175 const char *outform
= NULL
;
176 const char *outname
= "-";
177 const char *depname
= NULL
;
178 bool force
= false, sort
= false;
182 int outversion
= DEFAULT_FDT_VERSION
;
183 long long cmdline_boot_cpuid
= -1;
191 while ((opt
= util_getopt_long()) != EOF
) {
203 outversion
= strtol(optarg
, NULL
, 0);
209 reservenum
= strtol(optarg
, NULL
, 0);
212 minsize
= strtol(optarg
, NULL
, 0);
215 padsize
= strtol(optarg
, NULL
, 0);
218 alignsize
= strtol(optarg
, NULL
, 0);
219 if (!is_power_of_2(alignsize
))
220 die("Invalid argument \"%d\" to -a option\n",
230 cmdline_boot_cpuid
= strtoll(optarg
, NULL
, 0);
233 srcfile_add_search_path(optarg
);
238 if (streq(optarg
, "legacy"))
239 phandle_format
= PHANDLE_LEGACY
;
240 else if (streq(optarg
, "epapr"))
241 phandle_format
= PHANDLE_EPAPR
;
242 else if (streq(optarg
, "both"))
243 phandle_format
= PHANDLE_BOTH
;
245 die("Invalid argument \"%s\" to -H option\n",
254 parse_checks_option(true, false, optarg
);
258 parse_checks_option(false, true, optarg
);
262 generate_symbols
= 1;
265 auto_label_aliases
= 1;
271 usage("unknown option");
275 if (argc
> (optind
+1))
276 usage("missing files");
277 else if (argc
< (optind
+1))
282 /* minsize and padsize are mutually exclusive */
283 if (minsize
&& padsize
)
284 die("Can't set both -p and -S\n");
287 depfile
= fopen(depname
, "w");
289 die("Couldn't open dependency file %s: %s\n", depname
,
291 fprintf(depfile
, "%s:", outname
);
295 inform
= guess_input_format(arg
, "dts");
296 if (outform
== NULL
) {
297 outform
= guess_type_by_name(outname
, NULL
);
298 if (outform
== NULL
) {
299 if (streq(inform
, "dts"))
305 if (streq(inform
, "dts"))
306 dti
= dt_from_source(arg
);
307 else if (streq(inform
, "fs"))
308 dti
= dt_from_fs(arg
);
309 else if(streq(inform
, "dtb"))
310 dti
= dt_from_blob(arg
);
312 die("Unknown input format \"%s\"\n", inform
);
314 dti
->outname
= outname
;
317 fputc('\n', depfile
);
321 if (cmdline_boot_cpuid
!= -1)
322 dti
->boot_cpuid_phys
= cmdline_boot_cpuid
;
324 fill_fullpaths(dti
->dt
, "");
326 /* on a plugin, generate by default */
327 if (dti
->dtsflags
& DTSF_PLUGIN
) {
331 process_checks(force
, dti
);
333 if (auto_label_aliases
)
334 generate_label_tree(dti
, "aliases", false);
336 if (generate_symbols
)
337 generate_label_tree(dti
, "__symbols__", true);
339 if (generate_fixups
) {
340 generate_fixups_tree(dti
, "__fixups__");
341 generate_local_fixups_tree(dti
, "__local_fixups__");
347 if (streq(outname
, "-")) {
350 outf
= fopen(outname
, "wb");
352 die("Couldn't open output file %s: %s\n",
353 outname
, strerror(errno
));
356 if (streq(outform
, "dts")) {
357 dt_to_source(outf
, dti
);
359 } else if (streq(outform
, "yaml")) {
360 if (!streq(inform
, "dts"))
361 die("YAML output format requires dts input format\n");
362 dt_to_yaml(outf
, dti
);
364 } else if (streq(outform
, "dtb")) {
365 dt_to_blob(outf
, dti
, outversion
);
366 } else if (streq(outform
, "asm")) {
367 dt_to_asm(outf
, dti
, outversion
);
368 } else if (streq(outform
, "null")) {
371 die("Unknown output format \"%s\"\n", outform
);