dtc.c revision 238742
1204431Sraj/*
2204431Sraj * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
3204431Sraj *
4204431Sraj *
5204431Sraj * This program is free software; you can redistribute it and/or
6204431Sraj * modify it under the terms of the GNU General Public License as
7204431Sraj * published by the Free Software Foundation; either version 2 of the
8204431Sraj * License, or (at your option) any later version.
9204431Sraj *
10204431Sraj *  This program is distributed in the hope that it will be useful,
11204431Sraj *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12204431Sraj *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13204431Sraj *  General Public License for more details.
14204431Sraj *
15204431Sraj *  You should have received a copy of the GNU General Public License
16204431Sraj *  along with this program; if not, write to the Free Software
17204431Sraj *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18204431Sraj *                                                                   USA
19204431Sraj */
20204431Sraj
21204431Sraj#include "dtc.h"
22204431Sraj#include "srcpos.h"
23204431Sraj
24204431Sraj#include "version_gen.h"
25204431Sraj
26204431Sraj/*
27204431Sraj * Command line options
28204431Sraj */
29204431Srajint quiet;		/* Level of quietness */
30204431Srajint reservenum;		/* Number of memory reservation slots */
31204431Srajint minsize;		/* Minimum blob size */
32204431Srajint padsize;		/* Additional padding to blob */
33204433Srajint phandle_format = PHANDLE_BOTH;	/* Use linux,phandle or phandle properties */
34204431Sraj
35204431Srajstatic void fill_fullpaths(struct node *tree, const char *prefix)
36204431Sraj{
37204431Sraj	struct node *child;
38204431Sraj	const char *unit;
39204431Sraj
40204431Sraj	tree->fullpath = join_path(prefix, tree->name);
41204431Sraj
42204431Sraj	unit = strchr(tree->name, '@');
43204431Sraj	if (unit)
44204431Sraj		tree->basenamelen = unit - tree->name;
45204431Sraj	else
46204431Sraj		tree->basenamelen = strlen(tree->name);
47204431Sraj
48204431Sraj	for_each_child(tree, child)
49204431Sraj		fill_fullpaths(child, tree->fullpath);
50204431Sraj}
51204431Sraj
52204431Srajstatic void  __attribute__ ((noreturn)) usage(void)
53204431Sraj{
54204431Sraj	fprintf(stderr, "Usage:\n");
55204431Sraj	fprintf(stderr, "\tdtc [options] <input file>\n");
56204431Sraj	fprintf(stderr, "\nOptions:\n");
57204431Sraj	fprintf(stderr, "\t-h\n");
58204431Sraj	fprintf(stderr, "\t\tThis help text\n");
59204431Sraj	fprintf(stderr, "\t-q\n");
60204431Sraj	fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
61204431Sraj	fprintf(stderr, "\t-I <input format>\n");
62204431Sraj	fprintf(stderr, "\t\tInput formats are:\n");
63204431Sraj	fprintf(stderr, "\t\t\tdts - device tree source text\n");
64204431Sraj	fprintf(stderr, "\t\t\tdtb - device tree blob\n");
65204431Sraj	fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
66204431Sraj	fprintf(stderr, "\t-o <output file>\n");
67204431Sraj	fprintf(stderr, "\t-O <output format>\n");
68204431Sraj	fprintf(stderr, "\t\tOutput formats are:\n");
69204431Sraj	fprintf(stderr, "\t\t\tdts - device tree source text\n");
70204431Sraj	fprintf(stderr, "\t\t\tdtb - device tree blob\n");
71204431Sraj	fprintf(stderr, "\t\t\tasm - assembler source\n");
72204431Sraj	fprintf(stderr, "\t-V <output version>\n");
73204431Sraj	fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION);
74238742Simp	fprintf(stderr, "\t-d <output dependency file>\n");
75204431Sraj	fprintf(stderr, "\t-R <number>\n");
76204431Sraj	fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
77204431Sraj	fprintf(stderr, "\t-S <bytes>\n");
78204431Sraj	fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n");
79204431Sraj	fprintf(stderr, "\t-p <bytes>\n");
80204431Sraj	fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n");
81204431Sraj	fprintf(stderr, "\t-b <number>\n");
82204431Sraj	fprintf(stderr, "\t\tSet the physical boot cpu\n");
83204431Sraj	fprintf(stderr, "\t-f\n");
84204431Sraj	fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
85238742Simp	fprintf(stderr, "\t-i\n");
86238742Simp	fprintf(stderr, "\t\tAdd a path to search for include files\n");
87238742Simp	fprintf(stderr, "\t-s\n");
88238742Simp	fprintf(stderr, "\t\tSort nodes and properties before outputting (only useful for\n\t\tcomparing trees)\n");
89204431Sraj	fprintf(stderr, "\t-v\n");
90204431Sraj	fprintf(stderr, "\t\tPrint DTC version and exit\n");
91204433Sraj	fprintf(stderr, "\t-H <phandle format>\n");
92204433Sraj	fprintf(stderr, "\t\tphandle formats are:\n");
93204433Sraj	fprintf(stderr, "\t\t\tlegacy - \"linux,phandle\" properties only\n");
94204433Sraj	fprintf(stderr, "\t\t\tepapr - \"phandle\" properties only\n");
95204433Sraj	fprintf(stderr, "\t\t\tboth - Both \"linux,phandle\" and \"phandle\" properties\n");
96238742Simp	fprintf(stderr, "\t-W [no-]<checkname>\n");
97238742Simp	fprintf(stderr, "\t-E [no-]<checkname>\n");
98238742Simp	fprintf(stderr, "\t\t\tenable or disable warnings and errors\n");
99204431Sraj	exit(3);
100204431Sraj}
101204431Sraj
102204431Srajint main(int argc, char *argv[])
103204431Sraj{
104204431Sraj	struct boot_info *bi;
105204431Sraj	const char *inform = "dts";
106204431Sraj	const char *outform = "dts";
107204431Sraj	const char *outname = "-";
108238742Simp	const char *depname = NULL;
109238742Simp	int force = 0, sort = 0;
110204431Sraj	const char *arg;
111204431Sraj	int opt;
112204431Sraj	FILE *outf = NULL;
113204431Sraj	int outversion = DEFAULT_FDT_VERSION;
114204431Sraj	long long cmdline_boot_cpuid = -1;
115204431Sraj
116204431Sraj	quiet      = 0;
117204431Sraj	reservenum = 0;
118204431Sraj	minsize    = 0;
119204431Sraj	padsize    = 0;
120204431Sraj
121238742Simp	while ((opt = getopt(argc, argv, "hI:O:o:V:d:R:S:p:fqb:i:vH:sW:E:"))
122238742Simp			!= EOF) {
123204431Sraj		switch (opt) {
124204431Sraj		case 'I':
125204431Sraj			inform = optarg;
126204431Sraj			break;
127204431Sraj		case 'O':
128204431Sraj			outform = optarg;
129204431Sraj			break;
130204431Sraj		case 'o':
131204431Sraj			outname = optarg;
132204431Sraj			break;
133204431Sraj		case 'V':
134204431Sraj			outversion = strtol(optarg, NULL, 0);
135204431Sraj			break;
136238742Simp		case 'd':
137238742Simp			depname = optarg;
138238742Simp			break;
139204431Sraj		case 'R':
140204431Sraj			reservenum = strtol(optarg, NULL, 0);
141204431Sraj			break;
142204431Sraj		case 'S':
143204431Sraj			minsize = strtol(optarg, NULL, 0);
144204431Sraj			break;
145204431Sraj		case 'p':
146204431Sraj			padsize = strtol(optarg, NULL, 0);
147204431Sraj			break;
148204431Sraj		case 'f':
149204431Sraj			force = 1;
150204431Sraj			break;
151204431Sraj		case 'q':
152204431Sraj			quiet++;
153204431Sraj			break;
154204431Sraj		case 'b':
155204431Sraj			cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
156204431Sraj			break;
157238742Simp		case 'i':
158238742Simp			srcfile_add_search_path(optarg);
159238742Simp			break;
160204431Sraj		case 'v':
161204431Sraj			printf("Version: %s\n", DTC_VERSION);
162204431Sraj			exit(0);
163204433Sraj		case 'H':
164204433Sraj			if (streq(optarg, "legacy"))
165204433Sraj				phandle_format = PHANDLE_LEGACY;
166204433Sraj			else if (streq(optarg, "epapr"))
167204433Sraj				phandle_format = PHANDLE_EPAPR;
168204433Sraj			else if (streq(optarg, "both"))
169204433Sraj				phandle_format = PHANDLE_BOTH;
170204433Sraj			else
171204433Sraj				die("Invalid argument \"%s\" to -H option\n",
172204433Sraj				    optarg);
173204433Sraj			break;
174204433Sraj
175238742Simp		case 's':
176238742Simp			sort = 1;
177238742Simp			break;
178238742Simp
179238742Simp		case 'W':
180238742Simp			parse_checks_option(true, false, optarg);
181238742Simp			break;
182238742Simp
183238742Simp		case 'E':
184238742Simp			parse_checks_option(false, true, optarg);
185238742Simp			break;
186238742Simp
187204431Sraj		case 'h':
188204431Sraj		default:
189204431Sraj			usage();
190204431Sraj		}
191204431Sraj	}
192204431Sraj
193204431Sraj	if (argc > (optind+1))
194204431Sraj		usage();
195204431Sraj	else if (argc < (optind+1))
196204431Sraj		arg = "-";
197204431Sraj	else
198204431Sraj		arg = argv[optind];
199204431Sraj
200204431Sraj	/* minsize and padsize are mutually exclusive */
201204431Sraj	if (minsize && padsize)
202204431Sraj		die("Can't set both -p and -S\n");
203204431Sraj
204204433Sraj	if (minsize)
205204433Sraj		fprintf(stderr, "DTC: Use of \"-S\" is deprecated; it will be removed soon, use \"-p\" instead\n");
206204433Sraj
207238742Simp	if (depname) {
208238742Simp		depfile = fopen(depname, "w");
209238742Simp		if (!depfile)
210238742Simp			die("Couldn't open dependency file %s: %s\n", depname,
211238742Simp			    strerror(errno));
212238742Simp		fprintf(depfile, "%s:", outname);
213238742Simp	}
214204431Sraj
215204431Sraj	if (streq(inform, "dts"))
216204431Sraj		bi = dt_from_source(arg);
217204431Sraj	else if (streq(inform, "fs"))
218204431Sraj		bi = dt_from_fs(arg);
219204431Sraj	else if(streq(inform, "dtb"))
220204431Sraj		bi = dt_from_blob(arg);
221204431Sraj	else
222204431Sraj		die("Unknown input format \"%s\"\n", inform);
223204431Sraj
224238742Simp	if (depfile) {
225238742Simp		fputc('\n', depfile);
226238742Simp		fclose(depfile);
227238742Simp	}
228238742Simp
229204431Sraj	if (cmdline_boot_cpuid != -1)
230204431Sraj		bi->boot_cpuid_phys = cmdline_boot_cpuid;
231204431Sraj
232204431Sraj	fill_fullpaths(bi->dt, "");
233204431Sraj	process_checks(force, bi);
234204431Sraj
235238742Simp	if (sort)
236238742Simp		sort_tree(bi);
237204431Sraj
238204431Sraj	if (streq(outname, "-")) {
239204431Sraj		outf = stdout;
240204431Sraj	} else {
241204431Sraj		outf = fopen(outname, "w");
242204431Sraj		if (! outf)
243204431Sraj			die("Couldn't open output file %s: %s\n",
244204431Sraj			    outname, strerror(errno));
245204431Sraj	}
246204431Sraj
247204431Sraj	if (streq(outform, "dts")) {
248204431Sraj		dt_to_source(outf, bi);
249204431Sraj	} else if (streq(outform, "dtb")) {
250204431Sraj		dt_to_blob(outf, bi, outversion);
251204431Sraj	} else if (streq(outform, "asm")) {
252204431Sraj		dt_to_asm(outf, bi, outversion);
253204431Sraj	} else if (streq(outform, "null")) {
254204431Sraj		/* do nothing */
255204431Sraj	} else {
256204431Sraj		die("Unknown output format \"%s\"\n", outform);
257204431Sraj	}
258204431Sraj
259204431Sraj	exit(0);
260204431Sraj}
261