dtc.c revision 261215
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/*
25204431Sraj * Command line options
26204431Sraj */
27204431Srajint quiet;		/* Level of quietness */
28204431Srajint reservenum;		/* Number of memory reservation slots */
29204431Srajint minsize;		/* Minimum blob size */
30204431Srajint padsize;		/* Additional padding to blob */
31204433Srajint phandle_format = PHANDLE_BOTH;	/* Use linux,phandle or phandle properties */
32204431Sraj
33204431Srajstatic void fill_fullpaths(struct node *tree, const char *prefix)
34204431Sraj{
35204431Sraj	struct node *child;
36204431Sraj	const char *unit;
37204431Sraj
38204431Sraj	tree->fullpath = join_path(prefix, tree->name);
39204431Sraj
40204431Sraj	unit = strchr(tree->name, '@');
41204431Sraj	if (unit)
42204431Sraj		tree->basenamelen = unit - tree->name;
43204431Sraj	else
44204431Sraj		tree->basenamelen = strlen(tree->name);
45204431Sraj
46204431Sraj	for_each_child(tree, child)
47204431Sraj		fill_fullpaths(child, tree->fullpath);
48204431Sraj}
49204431Sraj
50261215Simp/* Usage related data. */
51261215Simpstatic const char usage_synopsis[] = "dtc [options] <input file>";
52261215Simpstatic const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:fb:i:H:sW:E:hv";
53261215Simpstatic struct option const usage_long_opts[] = {
54261215Simp	{"quiet",            no_argument, NULL, 'q'},
55261215Simp	{"in-format",         a_argument, NULL, 'I'},
56261215Simp	{"out",               a_argument, NULL, 'o'},
57261215Simp	{"out-format",        a_argument, NULL, 'O'},
58261215Simp	{"out-version",       a_argument, NULL, 'V'},
59261215Simp	{"out-dependency",    a_argument, NULL, 'd'},
60261215Simp	{"reserve",           a_argument, NULL, 'R'},
61261215Simp	{"space",             a_argument, NULL, 'S'},
62261215Simp	{"pad",               a_argument, NULL, 'p'},
63261215Simp	{"boot-cpu",          a_argument, NULL, 'b'},
64261215Simp	{"force",            no_argument, NULL, 'f'},
65261215Simp	{"include",           a_argument, NULL, 'i'},
66261215Simp	{"sort",             no_argument, NULL, 's'},
67261215Simp	{"phandle",           a_argument, NULL, 'H'},
68261215Simp	{"warning",           a_argument, NULL, 'W'},
69261215Simp	{"error",             a_argument, NULL, 'E'},
70261215Simp	{"help",             no_argument, NULL, 'h'},
71261215Simp	{"version",          no_argument, NULL, 'v'},
72261215Simp	{NULL,               no_argument, NULL, 0x0},
73261215Simp};
74261215Simpstatic const char * const usage_opts_help[] = {
75261215Simp	"\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
76261215Simp	"\n\tInput formats are:\n"
77261215Simp	 "\t\tdts - device tree source text\n"
78261215Simp	 "\t\tdtb - device tree blob\n"
79261215Simp	 "\t\tfs  - /proc/device-tree style directory",
80261215Simp	"\n\tOutput file",
81261215Simp	"\n\tOutput formats are:\n"
82261215Simp	 "\t\tdts - device tree source text\n"
83261215Simp	 "\t\tdtb - device tree blob\n"
84261215Simp	 "\t\tasm - assembler source",
85261215Simp	"\n\tBlob version to produce, defaults to %d (for dtb and asm output)", //, DEFAULT_FDT_VERSION);
86261215Simp	"\n\tOutput dependency file",
87261215Simp	"\n\ttMake space for <number> reserve map entries (for dtb and asm output)",
88261215Simp	"\n\tMake the blob at least <bytes> long (extra space)",
89261215Simp	"\n\tAdd padding to the blob of <bytes> long (extra space)",
90261215Simp	"\n\tSet the physical boot cpu",
91261215Simp	"\n\tTry to produce output even if the input tree has errors",
92261215Simp	"\n\tAdd a path to search for include files",
93261215Simp	"\n\tSort nodes and properties before outputting (useful for comparing trees)",
94261215Simp	"\n\tValid phandle formats are:\n"
95261215Simp	 "\t\tlegacy - \"linux,phandle\" properties only\n"
96261215Simp	 "\t\tepapr  - \"phandle\" properties only\n"
97261215Simp	 "\t\tboth   - Both \"linux,phandle\" and \"phandle\" properties",
98261215Simp	"\n\tEnable/disable warnings (prefix with \"no-\")",
99261215Simp	"\n\tEnable/disable errors (prefix with \"no-\")",
100261215Simp	"\n\tPrint this help and exit",
101261215Simp	"\n\tPrint version and exit",
102261215Simp	NULL,
103261215Simp};
104204431Sraj
105204431Srajint main(int argc, char *argv[])
106204431Sraj{
107204431Sraj	struct boot_info *bi;
108204431Sraj	const char *inform = "dts";
109204431Sraj	const char *outform = "dts";
110204431Sraj	const char *outname = "-";
111238742Simp	const char *depname = NULL;
112261215Simp	bool force = false, sort = false;
113204431Sraj	const char *arg;
114204431Sraj	int opt;
115204431Sraj	FILE *outf = NULL;
116204431Sraj	int outversion = DEFAULT_FDT_VERSION;
117204431Sraj	long long cmdline_boot_cpuid = -1;
118204431Sraj
119204431Sraj	quiet      = 0;
120204431Sraj	reservenum = 0;
121204431Sraj	minsize    = 0;
122204431Sraj	padsize    = 0;
123204431Sraj
124261215Simp	while ((opt = util_getopt_long()) != EOF) {
125204431Sraj		switch (opt) {
126204431Sraj		case 'I':
127204431Sraj			inform = optarg;
128204431Sraj			break;
129204431Sraj		case 'O':
130204431Sraj			outform = optarg;
131204431Sraj			break;
132204431Sraj		case 'o':
133204431Sraj			outname = optarg;
134204431Sraj			break;
135204431Sraj		case 'V':
136204431Sraj			outversion = strtol(optarg, NULL, 0);
137204431Sraj			break;
138238742Simp		case 'd':
139238742Simp			depname = optarg;
140238742Simp			break;
141204431Sraj		case 'R':
142204431Sraj			reservenum = strtol(optarg, NULL, 0);
143204431Sraj			break;
144204431Sraj		case 'S':
145204431Sraj			minsize = strtol(optarg, NULL, 0);
146204431Sraj			break;
147204431Sraj		case 'p':
148204431Sraj			padsize = strtol(optarg, NULL, 0);
149204431Sraj			break;
150204431Sraj		case 'f':
151261215Simp			force = true;
152204431Sraj			break;
153204431Sraj		case 'q':
154204431Sraj			quiet++;
155204431Sraj			break;
156204431Sraj		case 'b':
157204431Sraj			cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
158204431Sraj			break;
159238742Simp		case 'i':
160238742Simp			srcfile_add_search_path(optarg);
161238742Simp			break;
162204431Sraj		case 'v':
163261215Simp			util_version();
164204433Sraj		case 'H':
165204433Sraj			if (streq(optarg, "legacy"))
166204433Sraj				phandle_format = PHANDLE_LEGACY;
167204433Sraj			else if (streq(optarg, "epapr"))
168204433Sraj				phandle_format = PHANDLE_EPAPR;
169204433Sraj			else if (streq(optarg, "both"))
170204433Sraj				phandle_format = PHANDLE_BOTH;
171204433Sraj			else
172204433Sraj				die("Invalid argument \"%s\" to -H option\n",
173204433Sraj				    optarg);
174204433Sraj			break;
175204433Sraj
176238742Simp		case 's':
177261215Simp			sort = true;
178238742Simp			break;
179238742Simp
180238742Simp		case 'W':
181238742Simp			parse_checks_option(true, false, optarg);
182238742Simp			break;
183238742Simp
184238742Simp		case 'E':
185238742Simp			parse_checks_option(false, true, optarg);
186238742Simp			break;
187238742Simp
188204431Sraj		case 'h':
189261215Simp			usage(NULL);
190204431Sraj		default:
191261215Simp			usage("unknown option");
192204431Sraj		}
193204431Sraj	}
194204431Sraj
195204431Sraj	if (argc > (optind+1))
196261215Simp		usage("missing files");
197204431Sraj	else if (argc < (optind+1))
198204431Sraj		arg = "-";
199204431Sraj	else
200204431Sraj		arg = argv[optind];
201204431Sraj
202204431Sraj	/* minsize and padsize are mutually exclusive */
203204431Sraj	if (minsize && padsize)
204204431Sraj		die("Can't set both -p and -S\n");
205204431Sraj
206238742Simp	if (depname) {
207238742Simp		depfile = fopen(depname, "w");
208238742Simp		if (!depfile)
209238742Simp			die("Couldn't open dependency file %s: %s\n", depname,
210238742Simp			    strerror(errno));
211238742Simp		fprintf(depfile, "%s:", outname);
212238742Simp	}
213204431Sraj
214204431Sraj	if (streq(inform, "dts"))
215204431Sraj		bi = dt_from_source(arg);
216204431Sraj	else if (streq(inform, "fs"))
217204431Sraj		bi = dt_from_fs(arg);
218204431Sraj	else if(streq(inform, "dtb"))
219204431Sraj		bi = dt_from_blob(arg);
220204431Sraj	else
221204431Sraj		die("Unknown input format \"%s\"\n", inform);
222204431Sraj
223238742Simp	if (depfile) {
224238742Simp		fputc('\n', depfile);
225238742Simp		fclose(depfile);
226238742Simp	}
227238742Simp
228204431Sraj	if (cmdline_boot_cpuid != -1)
229204431Sraj		bi->boot_cpuid_phys = cmdline_boot_cpuid;
230204431Sraj
231204431Sraj	fill_fullpaths(bi->dt, "");
232204431Sraj	process_checks(force, bi);
233204431Sraj
234238742Simp	if (sort)
235238742Simp		sort_tree(bi);
236204431Sraj
237204431Sraj	if (streq(outname, "-")) {
238204431Sraj		outf = stdout;
239204431Sraj	} else {
240204431Sraj		outf = fopen(outname, "w");
241204431Sraj		if (! outf)
242204431Sraj			die("Couldn't open output file %s: %s\n",
243204431Sraj			    outname, strerror(errno));
244204431Sraj	}
245204431Sraj
246204431Sraj	if (streq(outform, "dts")) {
247204431Sraj		dt_to_source(outf, bi);
248204431Sraj	} else if (streq(outform, "dtb")) {
249204431Sraj		dt_to_blob(outf, bi, outversion);
250204431Sraj	} else if (streq(outform, "asm")) {
251204431Sraj		dt_to_asm(outf, bi, outversion);
252204431Sraj	} else if (streq(outform, "null")) {
253204431Sraj		/* do nothing */
254204431Sraj	} else {
255204431Sraj		die("Unknown output format \"%s\"\n", outform);
256204431Sraj	}
257204431Sraj
258204431Sraj	exit(0);
259204431Sraj}
260