blob: 695e1f789fc716f7c17929d40285863131eb1fff [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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
Rob Herring91feabc2016-01-26 09:04:11 -060021#include <sys/stat.h>
22
David Gibsona4da2e32007-12-18 15:06:42 +110023#include "dtc.h"
24#include "srcpos.h"
25
David Gibsona4da2e32007-12-18 15:06:42 +110026/*
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 */
Rob Herring6f05afc2017-01-04 10:45:20 -060033int alignsize; /* Additional padding to blob accroding to the alignsize */
Rob Herring4201d052017-10-03 11:37:04 -050034int phandle_format = PHANDLE_EPAPR; /* Use linux,phandle or phandle properties */
Rob Herring6f05afc2017-01-04 10:45:20 -060035int generate_symbols; /* enable symbols & fixup support */
36int generate_fixups; /* suppress generation of fixups on symbol support */
37int auto_label_aliases; /* auto generate labels -> aliases */
Rob Herringc2e70752018-11-28 18:37:35 -060038int annotate; /* Level of annotation: 1 for input source location
39 >1 for full input source location. */
Rob Herring6f05afc2017-01-04 10:45:20 -060040
41static int is_power_of_2(int x)
42{
43 return (x > 0) && ((x & (x - 1)) == 0);
44}
David Gibsona4da2e32007-12-18 15:06:42 +110045
David Gibsoned95d742008-08-07 12:24:17 +100046static void fill_fullpaths(struct node *tree, const char *prefix)
David Gibsona4da2e32007-12-18 15:06:42 +110047{
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
Grant Likely73ab39b2014-01-21 12:54:49 +000063/* Usage related data. */
64static const char usage_synopsis[] = "dtc [options] <input file>";
Rob Herringc2e70752018-11-28 18:37:35 -060065static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@AThv";
Grant Likely73ab39b2014-01-21 12:54:49 +000066static 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'},
Rob Herring6f05afc2017-01-04 10:45:20 -060076 {"align", a_argument, NULL, 'a'},
Grant Likely73ab39b2014-01-21 12:54:49 +000077 {"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'},
Rob Herring6f05afc2017-01-04 10:45:20 -060084 {"symbols", no_argument, NULL, '@'},
85 {"auto-alias", no_argument, NULL, 'A'},
Rob Herringc2e70752018-11-28 18:37:35 -060086 {"annotate", no_argument, NULL, 'T'},
Grant Likely73ab39b2014-01-21 12:54:49 +000087 {"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"
Rob Herringf8589272018-09-13 08:59:25 -0500101#ifndef NO_YAML
102 "\t\tyaml - device tree encoded as YAML\n"
103#endif
Grant Likely73ab39b2014-01-21 12:54:49 +0000104 "\t\tasm - assembler source",
Rob Herring9130ba82018-02-27 17:40:38 -0600105 "\n\tBlob version to produce, defaults to "stringify(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000106 "\n\tOutput dependency file",
Rob Herring47605972015-04-29 16:00:05 -0500107 "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000108 "\n\tMake the blob at least <bytes> long (extra space)",
109 "\n\tAdd padding to the blob of <bytes> long (extra space)",
Rob Herring6f05afc2017-01-04 10:45:20 -0600110 "\n\tMake the blob align to the <bytes> (extra space)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000111 "\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-\")",
Rob Herring6f05afc2017-01-04 10:45:20 -0600121 "\n\tEnable generation of symbols",
122 "\n\tEnable auto-alias of labels",
Rob Herringc2e70752018-11-28 18:37:35 -0600123 "\n\tAnnotate output .dts with input source file and line (-T -T for more details)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000124 "\n\tPrint this help and exit",
125 "\n\tPrint version and exit",
126 NULL,
127};
David Gibsona4da2e32007-12-18 15:06:42 +1100128
Rob Herring91feabc2016-01-26 09:04:11 -0600129static 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";
Rob Herringf8589272018-09-13 08:59:25 -0500138 if (!strcasecmp(s, ".yaml"))
139 return "yaml";
Rob Herring91feabc2016-01-26 09:04:11 -0600140 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;
Rob Herring89d12312017-03-21 09:01:08 -0500148 fdt32_t magic;
Rob Herring91feabc2016-01-26 09:04:11 -0600149 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
Rob Herring89d12312017-03-21 09:01:08 -0500169 if (fdt32_to_cpu(magic) == FDT_MAGIC)
Rob Herring91feabc2016-01-26 09:04:11 -0600170 return "dtb";
171
172 return guess_type_by_name(fname, fallback);
173}
174
David Gibsona4da2e32007-12-18 15:06:42 +1100175int main(int argc, char *argv[])
176{
Rob Herring6f05afc2017-01-04 10:45:20 -0600177 struct dt_info *dti;
Rob Herring91feabc2016-01-26 09:04:11 -0600178 const char *inform = NULL;
179 const char *outform = NULL;
David Gibsona4da2e32007-12-18 15:06:42 +1100180 const char *outname = "-";
Stephen Warren136ec202012-01-10 17:27:52 -0700181 const char *depname = NULL;
Rob Herring47605972015-04-29 16:00:05 -0500182 bool force = false, sort = false;
David Gibsona4da2e32007-12-18 15:06:42 +1100183 const char *arg;
184 int opt;
David Gibsona4da2e32007-12-18 15:06:42 +1100185 FILE *outf = NULL;
186 int outversion = DEFAULT_FDT_VERSION;
David Gibsoned95d742008-08-07 12:24:17 +1000187 long long cmdline_boot_cpuid = -1;
David Gibsona4da2e32007-12-18 15:06:42 +1100188
189 quiet = 0;
190 reservenum = 0;
191 minsize = 0;
192 padsize = 0;
Rob Herring6f05afc2017-01-04 10:45:20 -0600193 alignsize = 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100194
Grant Likely73ab39b2014-01-21 12:54:49 +0000195 while ((opt = util_getopt_long()) != EOF) {
David Gibsona4da2e32007-12-18 15:06:42 +1100196 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;
Stephen Warren136ec202012-01-10 17:27:52 -0700209 case 'd':
210 depname = optarg;
211 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100212 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;
Rob Herring6f05afc2017-01-04 10:45:20 -0600221 case 'a':
222 alignsize = strtol(optarg, NULL, 0);
223 if (!is_power_of_2(alignsize))
224 die("Invalid argument \"%d\" to -a option\n",
Rob Herring89d12312017-03-21 09:01:08 -0500225 alignsize);
Rob Herring6f05afc2017-01-04 10:45:20 -0600226 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100227 case 'f':
Rob Herring47605972015-04-29 16:00:05 -0500228 force = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100229 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100230 case 'q':
231 quiet++;
232 break;
233 case 'b':
David Gibsoned95d742008-08-07 12:24:17 +1000234 cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100235 break;
Stephen Warrencd296722012-09-28 21:25:59 +0000236 case 'i':
237 srcfile_add_search_path(optarg);
238 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100239 case 'v':
Grant Likely73ab39b2014-01-21 12:54:49 +0000240 util_version();
John Bonesio658f29a2010-11-17 15:28:20 -0800241 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':
Rob Herring47605972015-04-29 16:00:05 -0500254 sort = true;
John Bonesio658f29a2010-11-17 15:28:20 -0800255 break;
256
Stephen Warrencd296722012-09-28 21:25:59 +0000257 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
Rob Herring6f05afc2017-01-04 10:45:20 -0600265 case '@':
266 generate_symbols = 1;
267 break;
268 case 'A':
269 auto_label_aliases = 1;
270 break;
Rob Herringc2e70752018-11-28 18:37:35 -0600271 case 'T':
272 annotate++;
273 break;
Rob Herring6f05afc2017-01-04 10:45:20 -0600274
David Gibsona4da2e32007-12-18 15:06:42 +1100275 case 'h':
Grant Likely73ab39b2014-01-21 12:54:49 +0000276 usage(NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100277 default:
Grant Likely73ab39b2014-01-21 12:54:49 +0000278 usage("unknown option");
David Gibsona4da2e32007-12-18 15:06:42 +1100279 }
280 }
281
282 if (argc > (optind+1))
Grant Likely73ab39b2014-01-21 12:54:49 +0000283 usage("missing files");
David Gibsona4da2e32007-12-18 15:06:42 +1100284 else if (argc < (optind+1))
285 arg = "-";
286 else
287 arg = argv[optind];
288
289 /* minsize and padsize are mutually exclusive */
David Gibsoned95d742008-08-07 12:24:17 +1000290 if (minsize && padsize)
David Gibsona4da2e32007-12-18 15:06:42 +1100291 die("Can't set both -p and -S\n");
David Gibsona4da2e32007-12-18 15:06:42 +1100292
Stephen Warren136ec202012-01-10 17:27:52 -0700293 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
Rob Herring91feabc2016-01-26 09:04:11 -0600301 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 }
Rob Herringc2e70752018-11-28 18:37:35 -0600312 if (annotate && (!streq(inform, "dts") || !streq(outform, "dts")))
313 die("--annotate requires -I dts -O dts\n");
David Gibsoned95d742008-08-07 12:24:17 +1000314 if (streq(inform, "dts"))
Rob Herring6f05afc2017-01-04 10:45:20 -0600315 dti = dt_from_source(arg);
David Gibsoned95d742008-08-07 12:24:17 +1000316 else if (streq(inform, "fs"))
Rob Herring6f05afc2017-01-04 10:45:20 -0600317 dti = dt_from_fs(arg);
David Gibsoned95d742008-08-07 12:24:17 +1000318 else if(streq(inform, "dtb"))
Rob Herring6f05afc2017-01-04 10:45:20 -0600319 dti = dt_from_blob(arg);
David Gibsoned95d742008-08-07 12:24:17 +1000320 else
David Gibsona4da2e32007-12-18 15:06:42 +1100321 die("Unknown input format \"%s\"\n", inform);
David Gibsona4da2e32007-12-18 15:06:42 +1100322
Rob Herring89d12312017-03-21 09:01:08 -0500323 dti->outname = outname;
324
Stephen Warren136ec202012-01-10 17:27:52 -0700325 if (depfile) {
326 fputc('\n', depfile);
327 fclose(depfile);
328 }
329
David Gibsoned95d742008-08-07 12:24:17 +1000330 if (cmdline_boot_cpuid != -1)
Rob Herring6f05afc2017-01-04 10:45:20 -0600331 dti->boot_cpuid_phys = cmdline_boot_cpuid;
David Gibsona4da2e32007-12-18 15:06:42 +1100332
Rob Herring6f05afc2017-01-04 10:45:20 -0600333 fill_fullpaths(dti->dt, "");
Rob Herring6f05afc2017-01-04 10:45:20 -0600334
335 /* on a plugin, generate by default */
336 if (dti->dtsflags & DTSF_PLUGIN) {
337 generate_fixups = 1;
338 }
339
Rob Herring9130ba82018-02-27 17:40:38 -0600340 process_checks(force, dti);
341
Rob Herring6f05afc2017-01-04 10:45:20 -0600342 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 }
David Gibsona4da2e32007-12-18 15:06:42 +1100352
John Bonesio658f29a2010-11-17 15:28:20 -0800353 if (sort)
Rob Herring6f05afc2017-01-04 10:45:20 -0600354 sort_tree(dti);
David Gibsona4da2e32007-12-18 15:06:42 +1100355
356 if (streq(outname, "-")) {
357 outf = stdout;
358 } else {
Rob Herring47605972015-04-29 16:00:05 -0500359 outf = fopen(outname, "wb");
David Gibsona4da2e32007-12-18 15:06:42 +1100360 if (! outf)
361 die("Couldn't open output file %s: %s\n",
362 outname, strerror(errno));
363 }
364
365 if (streq(outform, "dts")) {
Rob Herring6f05afc2017-01-04 10:45:20 -0600366 dt_to_source(outf, dti);
Rob Herringf8589272018-09-13 08:59:25 -0500367#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
David Gibsona4da2e32007-12-18 15:06:42 +1100373 } else if (streq(outform, "dtb")) {
Rob Herring6f05afc2017-01-04 10:45:20 -0600374 dt_to_blob(outf, dti, outversion);
David Gibsona4da2e32007-12-18 15:06:42 +1100375 } else if (streq(outform, "asm")) {
Rob Herring6f05afc2017-01-04 10:45:20 -0600376 dt_to_asm(outf, dti, outversion);
David Gibsona4da2e32007-12-18 15:06:42 +1100377 } else if (streq(outform, "null")) {
378 /* do nothing */
379 } else {
380 die("Unknown output format \"%s\"\n", outform);
381 }
382
383 exit(0);
384}