blob: 64134aadb9973cbd2453cdf7b9af59489a59f608 [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 */
38
39static int is_power_of_2(int x)
40{
41 return (x > 0) && ((x & (x - 1)) == 0);
42}
David Gibsona4da2e32007-12-18 15:06:42 +110043
David Gibsoned95d742008-08-07 12:24:17 +100044static void fill_fullpaths(struct node *tree, const char *prefix)
David Gibsona4da2e32007-12-18 15:06:42 +110045{
46 struct node *child;
47 const char *unit;
48
49 tree->fullpath = join_path(prefix, tree->name);
50
51 unit = strchr(tree->name, '@');
52 if (unit)
53 tree->basenamelen = unit - tree->name;
54 else
55 tree->basenamelen = strlen(tree->name);
56
57 for_each_child(tree, child)
58 fill_fullpaths(child, tree->fullpath);
59}
60
Grant Likely73ab39b2014-01-21 12:54:49 +000061/* Usage related data. */
62static const char usage_synopsis[] = "dtc [options] <input file>";
Rob Herring6f05afc2017-01-04 10:45:20 -060063static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@Ahv";
Grant Likely73ab39b2014-01-21 12:54:49 +000064static 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'},
Rob Herring6f05afc2017-01-04 10:45:20 -060074 {"align", a_argument, NULL, 'a'},
Grant Likely73ab39b2014-01-21 12:54:49 +000075 {"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'},
Rob Herring6f05afc2017-01-04 10:45:20 -060082 {"symbols", no_argument, NULL, '@'},
83 {"auto-alias", no_argument, NULL, 'A'},
Grant Likely73ab39b2014-01-21 12:54:49 +000084 {"help", no_argument, NULL, 'h'},
85 {"version", no_argument, NULL, 'v'},
86 {NULL, no_argument, NULL, 0x0},
87};
88static 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",
94 "\n\tOutput file",
95 "\n\tOutput formats are:\n"
96 "\t\tdts - device tree source text\n"
97 "\t\tdtb - device tree blob\n"
Rob Herringf8589272018-09-13 08:59:25 -050098#ifndef NO_YAML
99 "\t\tyaml - device tree encoded as YAML\n"
100#endif
Grant Likely73ab39b2014-01-21 12:54:49 +0000101 "\t\tasm - assembler source",
Rob Herring9130ba82018-02-27 17:40:38 -0600102 "\n\tBlob version to produce, defaults to "stringify(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000103 "\n\tOutput dependency file",
Rob Herring47605972015-04-29 16:00:05 -0500104 "\n\tMake space for <number> reserve map entries (for dtb and asm output)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000105 "\n\tMake the blob at least <bytes> long (extra space)",
106 "\n\tAdd padding to the blob of <bytes> long (extra space)",
Rob Herring6f05afc2017-01-04 10:45:20 -0600107 "\n\tMake the blob align to the <bytes> (extra space)",
Grant Likely73ab39b2014-01-21 12:54:49 +0000108 "\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-\")",
Rob Herring6f05afc2017-01-04 10:45:20 -0600118 "\n\tEnable generation of symbols",
119 "\n\tEnable auto-alias of labels",
Grant Likely73ab39b2014-01-21 12:54:49 +0000120 "\n\tPrint this help and exit",
121 "\n\tPrint version and exit",
122 NULL,
123};
David Gibsona4da2e32007-12-18 15:06:42 +1100124
Rob Herring91feabc2016-01-26 09:04:11 -0600125static const char *guess_type_by_name(const char *fname, const char *fallback)
126{
127 const char *s;
128
129 s = strrchr(fname, '.');
130 if (s == NULL)
131 return fallback;
132 if (!strcasecmp(s, ".dts"))
133 return "dts";
Rob Herringf8589272018-09-13 08:59:25 -0500134 if (!strcasecmp(s, ".yaml"))
135 return "yaml";
Rob Herring91feabc2016-01-26 09:04:11 -0600136 if (!strcasecmp(s, ".dtb"))
137 return "dtb";
138 return fallback;
139}
140
141static const char *guess_input_format(const char *fname, const char *fallback)
142{
143 struct stat statbuf;
Rob Herring89d12312017-03-21 09:01:08 -0500144 fdt32_t magic;
Rob Herring91feabc2016-01-26 09:04:11 -0600145 FILE *f;
146
147 if (stat(fname, &statbuf) != 0)
148 return fallback;
149
150 if (S_ISDIR(statbuf.st_mode))
151 return "fs";
152
153 if (!S_ISREG(statbuf.st_mode))
154 return fallback;
155
156 f = fopen(fname, "r");
157 if (f == NULL)
158 return fallback;
159 if (fread(&magic, 4, 1, f) != 1) {
160 fclose(f);
161 return fallback;
162 }
163 fclose(f);
164
Rob Herring89d12312017-03-21 09:01:08 -0500165 if (fdt32_to_cpu(magic) == FDT_MAGIC)
Rob Herring91feabc2016-01-26 09:04:11 -0600166 return "dtb";
167
168 return guess_type_by_name(fname, fallback);
169}
170
David Gibsona4da2e32007-12-18 15:06:42 +1100171int main(int argc, char *argv[])
172{
Rob Herring6f05afc2017-01-04 10:45:20 -0600173 struct dt_info *dti;
Rob Herring91feabc2016-01-26 09:04:11 -0600174 const char *inform = NULL;
175 const char *outform = NULL;
David Gibsona4da2e32007-12-18 15:06:42 +1100176 const char *outname = "-";
Stephen Warren136ec202012-01-10 17:27:52 -0700177 const char *depname = NULL;
Rob Herring47605972015-04-29 16:00:05 -0500178 bool force = false, sort = false;
David Gibsona4da2e32007-12-18 15:06:42 +1100179 const char *arg;
180 int opt;
David Gibsona4da2e32007-12-18 15:06:42 +1100181 FILE *outf = NULL;
182 int outversion = DEFAULT_FDT_VERSION;
David Gibsoned95d742008-08-07 12:24:17 +1000183 long long cmdline_boot_cpuid = -1;
David Gibsona4da2e32007-12-18 15:06:42 +1100184
185 quiet = 0;
186 reservenum = 0;
187 minsize = 0;
188 padsize = 0;
Rob Herring6f05afc2017-01-04 10:45:20 -0600189 alignsize = 0;
David Gibsona4da2e32007-12-18 15:06:42 +1100190
Grant Likely73ab39b2014-01-21 12:54:49 +0000191 while ((opt = util_getopt_long()) != EOF) {
David Gibsona4da2e32007-12-18 15:06:42 +1100192 switch (opt) {
193 case 'I':
194 inform = optarg;
195 break;
196 case 'O':
197 outform = optarg;
198 break;
199 case 'o':
200 outname = optarg;
201 break;
202 case 'V':
203 outversion = strtol(optarg, NULL, 0);
204 break;
Stephen Warren136ec202012-01-10 17:27:52 -0700205 case 'd':
206 depname = optarg;
207 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100208 case 'R':
209 reservenum = strtol(optarg, NULL, 0);
210 break;
211 case 'S':
212 minsize = strtol(optarg, NULL, 0);
213 break;
214 case 'p':
215 padsize = strtol(optarg, NULL, 0);
216 break;
Rob Herring6f05afc2017-01-04 10:45:20 -0600217 case 'a':
218 alignsize = strtol(optarg, NULL, 0);
219 if (!is_power_of_2(alignsize))
220 die("Invalid argument \"%d\" to -a option\n",
Rob Herring89d12312017-03-21 09:01:08 -0500221 alignsize);
Rob Herring6f05afc2017-01-04 10:45:20 -0600222 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100223 case 'f':
Rob Herring47605972015-04-29 16:00:05 -0500224 force = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100225 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100226 case 'q':
227 quiet++;
228 break;
229 case 'b':
David Gibsoned95d742008-08-07 12:24:17 +1000230 cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100231 break;
Stephen Warrencd296722012-09-28 21:25:59 +0000232 case 'i':
233 srcfile_add_search_path(optarg);
234 break;
David Gibsona4da2e32007-12-18 15:06:42 +1100235 case 'v':
Grant Likely73ab39b2014-01-21 12:54:49 +0000236 util_version();
John Bonesio658f29a2010-11-17 15:28:20 -0800237 case 'H':
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;
244 else
245 die("Invalid argument \"%s\" to -H option\n",
246 optarg);
247 break;
248
249 case 's':
Rob Herring47605972015-04-29 16:00:05 -0500250 sort = true;
John Bonesio658f29a2010-11-17 15:28:20 -0800251 break;
252
Stephen Warrencd296722012-09-28 21:25:59 +0000253 case 'W':
254 parse_checks_option(true, false, optarg);
255 break;
256
257 case 'E':
258 parse_checks_option(false, true, optarg);
259 break;
260
Rob Herring6f05afc2017-01-04 10:45:20 -0600261 case '@':
262 generate_symbols = 1;
263 break;
264 case 'A':
265 auto_label_aliases = 1;
266 break;
267
David Gibsona4da2e32007-12-18 15:06:42 +1100268 case 'h':
Grant Likely73ab39b2014-01-21 12:54:49 +0000269 usage(NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100270 default:
Grant Likely73ab39b2014-01-21 12:54:49 +0000271 usage("unknown option");
David Gibsona4da2e32007-12-18 15:06:42 +1100272 }
273 }
274
275 if (argc > (optind+1))
Grant Likely73ab39b2014-01-21 12:54:49 +0000276 usage("missing files");
David Gibsona4da2e32007-12-18 15:06:42 +1100277 else if (argc < (optind+1))
278 arg = "-";
279 else
280 arg = argv[optind];
281
282 /* minsize and padsize are mutually exclusive */
David Gibsoned95d742008-08-07 12:24:17 +1000283 if (minsize && padsize)
David Gibsona4da2e32007-12-18 15:06:42 +1100284 die("Can't set both -p and -S\n");
David Gibsona4da2e32007-12-18 15:06:42 +1100285
Stephen Warren136ec202012-01-10 17:27:52 -0700286 if (depname) {
287 depfile = fopen(depname, "w");
288 if (!depfile)
289 die("Couldn't open dependency file %s: %s\n", depname,
290 strerror(errno));
291 fprintf(depfile, "%s:", outname);
292 }
293
Rob Herring91feabc2016-01-26 09:04:11 -0600294 if (inform == NULL)
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"))
300 outform = "dtb";
301 else
302 outform = "dts";
303 }
304 }
David Gibsoned95d742008-08-07 12:24:17 +1000305 if (streq(inform, "dts"))
Rob Herring6f05afc2017-01-04 10:45:20 -0600306 dti = dt_from_source(arg);
David Gibsoned95d742008-08-07 12:24:17 +1000307 else if (streq(inform, "fs"))
Rob Herring6f05afc2017-01-04 10:45:20 -0600308 dti = dt_from_fs(arg);
David Gibsoned95d742008-08-07 12:24:17 +1000309 else if(streq(inform, "dtb"))
Rob Herring6f05afc2017-01-04 10:45:20 -0600310 dti = dt_from_blob(arg);
David Gibsoned95d742008-08-07 12:24:17 +1000311 else
David Gibsona4da2e32007-12-18 15:06:42 +1100312 die("Unknown input format \"%s\"\n", inform);
David Gibsona4da2e32007-12-18 15:06:42 +1100313
Rob Herring89d12312017-03-21 09:01:08 -0500314 dti->outname = outname;
315
Stephen Warren136ec202012-01-10 17:27:52 -0700316 if (depfile) {
317 fputc('\n', depfile);
318 fclose(depfile);
319 }
320
David Gibsoned95d742008-08-07 12:24:17 +1000321 if (cmdline_boot_cpuid != -1)
Rob Herring6f05afc2017-01-04 10:45:20 -0600322 dti->boot_cpuid_phys = cmdline_boot_cpuid;
David Gibsona4da2e32007-12-18 15:06:42 +1100323
Rob Herring6f05afc2017-01-04 10:45:20 -0600324 fill_fullpaths(dti->dt, "");
Rob Herring6f05afc2017-01-04 10:45:20 -0600325
326 /* on a plugin, generate by default */
327 if (dti->dtsflags & DTSF_PLUGIN) {
328 generate_fixups = 1;
329 }
330
Rob Herring9130ba82018-02-27 17:40:38 -0600331 process_checks(force, dti);
332
Rob Herring6f05afc2017-01-04 10:45:20 -0600333 if (auto_label_aliases)
334 generate_label_tree(dti, "aliases", false);
335
336 if (generate_symbols)
337 generate_label_tree(dti, "__symbols__", true);
338
339 if (generate_fixups) {
340 generate_fixups_tree(dti, "__fixups__");
341 generate_local_fixups_tree(dti, "__local_fixups__");
342 }
David Gibsona4da2e32007-12-18 15:06:42 +1100343
John Bonesio658f29a2010-11-17 15:28:20 -0800344 if (sort)
Rob Herring6f05afc2017-01-04 10:45:20 -0600345 sort_tree(dti);
David Gibsona4da2e32007-12-18 15:06:42 +1100346
347 if (streq(outname, "-")) {
348 outf = stdout;
349 } else {
Rob Herring47605972015-04-29 16:00:05 -0500350 outf = fopen(outname, "wb");
David Gibsona4da2e32007-12-18 15:06:42 +1100351 if (! outf)
352 die("Couldn't open output file %s: %s\n",
353 outname, strerror(errno));
354 }
355
356 if (streq(outform, "dts")) {
Rob Herring6f05afc2017-01-04 10:45:20 -0600357 dt_to_source(outf, dti);
Rob Herringf8589272018-09-13 08:59:25 -0500358#ifndef NO_YAML
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);
363#endif
David Gibsona4da2e32007-12-18 15:06:42 +1100364 } else if (streq(outform, "dtb")) {
Rob Herring6f05afc2017-01-04 10:45:20 -0600365 dt_to_blob(outf, dti, outversion);
David Gibsona4da2e32007-12-18 15:06:42 +1100366 } else if (streq(outform, "asm")) {
Rob Herring6f05afc2017-01-04 10:45:20 -0600367 dt_to_asm(outf, dti, outversion);
David Gibsona4da2e32007-12-18 15:06:42 +1100368 } else if (streq(outform, "null")) {
369 /* do nothing */
370 } else {
371 die("Unknown output format \"%s\"\n", outform);
372 }
373
374 exit(0);
375}