dt-bindings: Pass binding directory to validation tools
[linux-2.6-block.git] / scripts / dtc / dtc.c
CommitLineData
a4da2e3e
DG
1/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
91feabc2
RH
21#include <sys/stat.h>
22
a4da2e3e
DG
23#include "dtc.h"
24#include "srcpos.h"
25
a4da2e3e
DG
26/*
27 * Command line options
28 */
29int quiet; /* Level of quietness */
30int reservenum; /* Number of memory reservation slots */
31int minsize; /* Minimum blob size */
32int padsize; /* Additional padding to blob */
6f05afcb 33int alignsize; /* Additional padding to blob accroding to the alignsize */
4201d057 34int phandle_format = PHANDLE_EPAPR; /* Use linux,phandle or phandle properties */
6f05afcb
RH
35int generate_symbols; /* enable symbols & fixup support */
36int generate_fixups; /* suppress generation of fixups on symbol support */
37int auto_label_aliases; /* auto generate labels -> aliases */
c2e7075c
RH
38int annotate; /* Level of annotation: 1 for input source location
39 >1 for full input source location. */
6f05afcb
RH
40
41static int is_power_of_2(int x)
42{
43 return (x > 0) && ((x & (x - 1)) == 0);
44}
a4da2e3e 45
ed95d745 46static void fill_fullpaths(struct node *tree, const char *prefix)
a4da2e3e
DG
47{
48 struct node *child;
49 const char *unit;
50
51 tree->fullpath = join_path(prefix, tree->name);
52
53 unit = strchr(tree->name, '@');
54 if (unit)
55 tree->basenamelen = unit - tree->name;
56 else
57 tree->basenamelen = strlen(tree->name);
58
59 for_each_child(tree, child)
60 fill_fullpaths(child, tree->fullpath);
61}
62
73ab39b1
GL
63/* Usage related data. */
64static const char usage_synopsis[] = "dtc [options] <input file>";
c2e7075c 65static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@AThv";
73ab39b1
GL
66static struct option const usage_long_opts[] = {
67 {"quiet", no_argument, NULL, 'q'},
68 {"in-format", a_argument, NULL, 'I'},
69 {"out", a_argument, NULL, 'o'},
70 {"out-format", a_argument, NULL, 'O'},
71 {"out-version", a_argument, NULL, 'V'},
72 {"out-dependency", a_argument, NULL, 'd'},
73 {"reserve", a_argument, NULL, 'R'},
74 {"space", a_argument, NULL, 'S'},
75 {"pad", a_argument, NULL, 'p'},
6f05afcb 76 {"align", a_argument, NULL, 'a'},
73ab39b1
GL
77 {"boot-cpu", a_argument, NULL, 'b'},
78 {"force", no_argument, NULL, 'f'},
79 {"include", a_argument, NULL, 'i'},
80 {"sort", no_argument, NULL, 's'},
81 {"phandle", a_argument, NULL, 'H'},
82 {"warning", a_argument, NULL, 'W'},
83 {"error", a_argument, NULL, 'E'},
6f05afcb
RH
84 {"symbols", no_argument, NULL, '@'},
85 {"auto-alias", no_argument, NULL, 'A'},
c2e7075c 86 {"annotate", no_argument, NULL, 'T'},
73ab39b1
GL
87 {"help", no_argument, NULL, 'h'},
88 {"version", no_argument, NULL, 'v'},
89 {NULL, no_argument, NULL, 0x0},
90};
91static const char * const usage_opts_help[] = {
92 "\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
93 "\n\tInput formats are:\n"
94 "\t\tdts - device tree source text\n"
95 "\t\tdtb - device tree blob\n"
96 "\t\tfs - /proc/device-tree style directory",
97 "\n\tOutput file",
98 "\n\tOutput formats are:\n"
99 "\t\tdts - device tree source text\n"
100 "\t\tdtb - device tree blob\n"
f858927f
RH
101#ifndef NO_YAML
102 "\t\tyaml - device tree encoded as YAML\n"
103#endif
73ab39b1 104 "\t\tasm - assembler source",
9130ba88 105 "\n\tBlob version to produce, defaults to "stringify(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
73ab39b1 106 "\n\tOutput dependency file",
47605971 107 "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
73ab39b1
GL
108 "\n\tMake the blob at least <bytes> long (extra space)",
109 "\n\tAdd padding to the blob of <bytes> long (extra space)",
6f05afcb 110 "\n\tMake the blob align to the <bytes> (extra space)",
73ab39b1
GL
111 "\n\tSet the physical boot cpu",
112 "\n\tTry to produce output even if the input tree has errors",
113 "\n\tAdd a path to search for include files",
114 "\n\tSort nodes and properties before outputting (useful for comparing trees)",
115 "\n\tValid phandle formats are:\n"
116 "\t\tlegacy - \"linux,phandle\" properties only\n"
117 "\t\tepapr - \"phandle\" properties only\n"
118 "\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
119 "\n\tEnable/disable warnings (prefix with \"no-\")",
120 "\n\tEnable/disable errors (prefix with \"no-\")",
6f05afcb
RH
121 "\n\tEnable generation of symbols",
122 "\n\tEnable auto-alias of labels",
c2e7075c 123 "\n\tAnnotate output .dts with input source file and line (-T -T for more details)",
73ab39b1
GL
124 "\n\tPrint this help and exit",
125 "\n\tPrint version and exit",
126 NULL,
127};
a4da2e3e 128
91feabc2
RH
129static const char *guess_type_by_name(const char *fname, const char *fallback)
130{
131 const char *s;
132
133 s = strrchr(fname, '.');
134 if (s == NULL)
135 return fallback;
136 if (!strcasecmp(s, ".dts"))
137 return "dts";
f858927f
RH
138 if (!strcasecmp(s, ".yaml"))
139 return "yaml";
91feabc2
RH
140 if (!strcasecmp(s, ".dtb"))
141 return "dtb";
142 return fallback;
143}
144
145static const char *guess_input_format(const char *fname, const char *fallback)
146{
147 struct stat statbuf;
89d12310 148 fdt32_t magic;
91feabc2
RH
149 FILE *f;
150
151 if (stat(fname, &statbuf) != 0)
152 return fallback;
153
154 if (S_ISDIR(statbuf.st_mode))
155 return "fs";
156
157 if (!S_ISREG(statbuf.st_mode))
158 return fallback;
159
160 f = fopen(fname, "r");
161 if (f == NULL)
162 return fallback;
163 if (fread(&magic, 4, 1, f) != 1) {
164 fclose(f);
165 return fallback;
166 }
167 fclose(f);
168
89d12310 169 if (fdt32_to_cpu(magic) == FDT_MAGIC)
91feabc2
RH
170 return "dtb";
171
172 return guess_type_by_name(fname, fallback);
173}
174
a4da2e3e
DG
175int main(int argc, char *argv[])
176{
6f05afcb 177 struct dt_info *dti;
91feabc2
RH
178 const char *inform = NULL;
179 const char *outform = NULL;
a4da2e3e 180 const char *outname = "-";
136ec204 181 const char *depname = NULL;
47605971 182 bool force = false, sort = false;
a4da2e3e
DG
183 const char *arg;
184 int opt;
a4da2e3e
DG
185 FILE *outf = NULL;
186 int outversion = DEFAULT_FDT_VERSION;
ed95d745 187 long long cmdline_boot_cpuid = -1;
a4da2e3e
DG
188
189 quiet = 0;
190 reservenum = 0;
191 minsize = 0;
192 padsize = 0;
6f05afcb 193 alignsize = 0;
a4da2e3e 194
73ab39b1 195 while ((opt = util_getopt_long()) != EOF) {
a4da2e3e
DG
196 switch (opt) {
197 case 'I':
198 inform = optarg;
199 break;
200 case 'O':
201 outform = optarg;
202 break;
203 case 'o':
204 outname = optarg;
205 break;
206 case 'V':
207 outversion = strtol(optarg, NULL, 0);
208 break;
136ec204
SW
209 case 'd':
210 depname = optarg;
211 break;
a4da2e3e
DG
212 case 'R':
213 reservenum = strtol(optarg, NULL, 0);
214 break;
215 case 'S':
216 minsize = strtol(optarg, NULL, 0);
217 break;
218 case 'p':
219 padsize = strtol(optarg, NULL, 0);
220 break;
6f05afcb
RH
221 case 'a':
222 alignsize = strtol(optarg, NULL, 0);
223 if (!is_power_of_2(alignsize))
224 die("Invalid argument \"%d\" to -a option\n",
89d12310 225 alignsize);
6f05afcb 226 break;
a4da2e3e 227 case 'f':
47605971 228 force = true;
a4da2e3e 229 break;
a4da2e3e
DG
230 case 'q':
231 quiet++;
232 break;
233 case 'b':
ed95d745 234 cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
a4da2e3e 235 break;
cd296721
SW
236 case 'i':
237 srcfile_add_search_path(optarg);
238 break;
a4da2e3e 239 case 'v':
73ab39b1 240 util_version();
658f29a5
JB
241 case 'H':
242 if (streq(optarg, "legacy"))
243 phandle_format = PHANDLE_LEGACY;
244 else if (streq(optarg, "epapr"))
245 phandle_format = PHANDLE_EPAPR;
246 else if (streq(optarg, "both"))
247 phandle_format = PHANDLE_BOTH;
248 else
249 die("Invalid argument \"%s\" to -H option\n",
250 optarg);
251 break;
252
253 case 's':
47605971 254 sort = true;
658f29a5
JB
255 break;
256
cd296721
SW
257 case 'W':
258 parse_checks_option(true, false, optarg);
259 break;
260
261 case 'E':
262 parse_checks_option(false, true, optarg);
263 break;
264
6f05afcb
RH
265 case '@':
266 generate_symbols = 1;
267 break;
268 case 'A':
269 auto_label_aliases = 1;
270 break;
c2e7075c
RH
271 case 'T':
272 annotate++;
273 break;
6f05afcb 274
a4da2e3e 275 case 'h':
73ab39b1 276 usage(NULL);
a4da2e3e 277 default:
73ab39b1 278 usage("unknown option");
a4da2e3e
DG
279 }
280 }
281
282 if (argc > (optind+1))
73ab39b1 283 usage("missing files");
a4da2e3e
DG
284 else if (argc < (optind+1))
285 arg = "-";
286 else
287 arg = argv[optind];
288
289 /* minsize and padsize are mutually exclusive */
ed95d745 290 if (minsize && padsize)
a4da2e3e 291 die("Can't set both -p and -S\n");
a4da2e3e 292
136ec204
SW
293 if (depname) {
294 depfile = fopen(depname, "w");
295 if (!depfile)
296 die("Couldn't open dependency file %s: %s\n", depname,
297 strerror(errno));
298 fprintf(depfile, "%s:", outname);
299 }
300
91feabc2
RH
301 if (inform == NULL)
302 inform = guess_input_format(arg, "dts");
303 if (outform == NULL) {
304 outform = guess_type_by_name(outname, NULL);
305 if (outform == NULL) {
306 if (streq(inform, "dts"))
307 outform = "dtb";
308 else
309 outform = "dts";
310 }
311 }
c2e7075c
RH
312 if (annotate && (!streq(inform, "dts") || !streq(outform, "dts")))
313 die("--annotate requires -I dts -O dts\n");
ed95d745 314 if (streq(inform, "dts"))
6f05afcb 315 dti = dt_from_source(arg);
ed95d745 316 else if (streq(inform, "fs"))
6f05afcb 317 dti = dt_from_fs(arg);
ed95d745 318 else if(streq(inform, "dtb"))
6f05afcb 319 dti = dt_from_blob(arg);
ed95d745 320 else
a4da2e3e 321 die("Unknown input format \"%s\"\n", inform);
a4da2e3e 322
89d12310
RH
323 dti->outname = outname;
324
136ec204
SW
325 if (depfile) {
326 fputc('\n', depfile);
327 fclose(depfile);
328 }
329
ed95d745 330 if (cmdline_boot_cpuid != -1)
6f05afcb
RH
331 dti->boot_cpuid_phys = cmdline_boot_cpuid;
332
333 fill_fullpaths(dti->dt, "");
6f05afcb
RH
334
335 /* on a plugin, generate by default */
336 if (dti->dtsflags & DTSF_PLUGIN) {
337 generate_fixups = 1;
338 }
a4da2e3e 339
9130ba88
RH
340 process_checks(force, dti);
341
6f05afcb
RH
342 if (auto_label_aliases)
343 generate_label_tree(dti, "aliases", false);
344
345 if (generate_symbols)
346 generate_label_tree(dti, "__symbols__", true);
347
348 if (generate_fixups) {
349 generate_fixups_tree(dti, "__fixups__");
350 generate_local_fixups_tree(dti, "__local_fixups__");
351 }
a4da2e3e 352
658f29a5 353 if (sort)
6f05afcb 354 sort_tree(dti);
a4da2e3e
DG
355
356 if (streq(outname, "-")) {
357 outf = stdout;
358 } else {
47605971 359 outf = fopen(outname, "wb");
a4da2e3e
DG
360 if (! outf)
361 die("Couldn't open output file %s: %s\n",
362 outname, strerror(errno));
363 }
364
365 if (streq(outform, "dts")) {
6f05afcb 366 dt_to_source(outf, dti);
f858927f
RH
367#ifndef NO_YAML
368 } else if (streq(outform, "yaml")) {
369 if (!streq(inform, "dts"))
370 die("YAML output format requires dts input format\n");
371 dt_to_yaml(outf, dti);
372#endif
a4da2e3e 373 } else if (streq(outform, "dtb")) {
6f05afcb 374 dt_to_blob(outf, dti, outversion);
a4da2e3e 375 } else if (streq(outform, "asm")) {
6f05afcb 376 dt_to_asm(outf, dti, outversion);
a4da2e3e
DG
377 } else if (streq(outform, "null")) {
378 /* do nothing */
379 } else {
380 die("Unknown output format \"%s\"\n", outform);
381 }
382
383 exit(0);
384}