xref: /freebsd/usr.bin/dtc/dtc.cc (revision bbe31b709a653884e18995a1c97cdafd7392999a)
1 /*-
2  * Copyright (c) 2013 David Chisnall
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <sys/resource.h>
34 #include <fcntl.h>
35 #include <libgen.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <time.h>
40 #include <unistd.h>
41 
42 
43 #include "fdt.hh"
44 #include "checking.hh"
45 #include "util.hh"
46 
47 using namespace dtc;
48 using std::string;
49 
50 /**
51  * The current major version of the tool.
52  */
53 int version_major = 0;
54 /**
55  * The current minor version of the tool.
56  */
57 int version_minor = 4;
58 /**
59  * The current patch level of the tool.
60  */
61 int version_patch = 0;
62 
63 static void usage(const string &argv0)
64 {
65 	fprintf(stderr, "Usage:\n"
66 		"\t%s\t[-fhsv] [-b boot_cpu_id] [-d dependency_file]"
67 			"[-E [no-]checker_name]\n"
68 		"\t\t[-H phandle_format] [-I input_format]"
69 			"[-O output_format]\n"
70 		"\t\t[-o output_file] [-R entries] [-S bytes] [-p bytes]"
71 			"[-V blob_version]\n"
72 		"\t\t-W [no-]checker_name] input_file\n", basename(argv0).c_str());
73 }
74 
75 /**
76  * Prints the current version of this program..
77  */
78 static void version(const char* progname)
79 {
80 	fprintf(stderr, "Version: %s %d.%d.%d\n", progname, version_major,
81 			version_minor, version_patch);
82 }
83 
84 using fdt::device_tree;
85 
86 int
87 main(int argc, char **argv)
88 {
89 	int ch;
90 	int outfile = fileno(stdout);
91 	const char *outfile_name = "-";
92 	const char *in_file = "-";
93 	FILE *depfile = 0;
94 	bool debug_mode = false;
95 	auto write_fn = &device_tree::write_binary;
96 	auto read_fn = &device_tree::parse_dts;
97 	uint32_t boot_cpu;
98 	bool boot_cpu_specified = false;
99 	bool keep_going = false;
100 	bool sort = false;
101 	clock_t c0 = clock();
102 	class device_tree tree;
103 	fdt::checking::check_manager checks;
104 	const char *options = "hqI:O:o:V:d:R:S:p:b:fi:svH:W:E:DP:";
105 
106 	// Don't forget to update the man page if any more options are added.
107 	while ((ch = getopt(argc, argv, options)) != -1)
108 	{
109 		switch (ch)
110 		{
111 		case 'h':
112 			usage(argv[0]);
113 			return EXIT_SUCCESS;
114 		case 'v':
115 			version(argv[0]);
116 			return EXIT_SUCCESS;
117 		case 'I':
118 		{
119 			string arg(optarg);
120 			if (arg == "dtb")
121 			{
122 				read_fn = &device_tree::parse_dtb;
123 			}
124 			else if (arg == "dts")
125 			{
126 				read_fn = &device_tree::parse_dts;
127 			}
128 			else
129 			{
130 				fprintf(stderr, "Unknown input format: %s\n", optarg);
131 				return EXIT_FAILURE;
132 			}
133 			break;
134 		}
135 		case 'O':
136 		{
137 			string arg(optarg);
138 			if (arg == "dtb")
139 			{
140 				write_fn = &device_tree::write_binary;
141 			}
142 			else if (arg == "asm")
143 			{
144 				write_fn = &device_tree::write_asm;
145 			}
146 			else if (arg == "dts")
147 			{
148 				write_fn = &device_tree::write_dts;
149 			}
150 			else
151 			{
152 				fprintf(stderr, "Unknown output format: %s\n", optarg);
153 				return EXIT_FAILURE;
154 			}
155 			break;
156 		}
157 		case 'o':
158 		{
159 			outfile_name = optarg;
160 			outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
161 			if (outfile == -1)
162 			{
163 				perror("Unable to open output file");
164 				return EXIT_FAILURE;
165 			}
166 			break;
167 		}
168 		case 'D':
169 			debug_mode = true;
170 			break;
171 		case 'V':
172 			if (string(optarg) != "17")
173 			{
174 				fprintf(stderr, "Unknown output format version: %s\n", optarg);
175 				return EXIT_FAILURE;
176 			}
177 			break;
178 		case 'd':
179 		{
180 			if (depfile != 0)
181 			{
182 				fclose(depfile);
183 			}
184 			if (string(optarg) == "-")
185 			{
186 				depfile = stdout;
187 			}
188 			else
189 			{
190 				depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
191 				if (depfile == 0)
192 				{
193 					perror("Unable to open dependency file");
194 					return EXIT_FAILURE;
195 				}
196 			}
197 			break;
198 		}
199 		case 'H':
200 		{
201 			string arg(optarg);
202 			if (arg == "both")
203 			{
204 				tree.set_phandle_format(device_tree::BOTH);
205 			}
206 			else if (arg == "epapr")
207 			{
208 				tree.set_phandle_format(device_tree::EPAPR);
209 			}
210 			else if (arg == "linux")
211 			{
212 				tree.set_phandle_format(device_tree::LINUX);
213 			}
214 			else
215 			{
216 				fprintf(stderr, "Unknown phandle format: %s\n", optarg);
217 				return EXIT_FAILURE;
218 			}
219 			break;
220 		}
221 		case 'b':
222 			// Don't bother to check if strtoll fails, just
223 			// use the 0 it returns.
224 			boot_cpu = (uint32_t)strtoll(optarg, 0, 10);
225 			boot_cpu_specified = true;
226 			break;
227 		case 'f':
228 			keep_going = true;
229 			break;
230 		case 'W':
231 		case 'E':
232 		{
233 			string arg(optarg);
234 			if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
235 			{
236 				arg = string(optarg+3);
237 				if (!checks.disable_checker(arg))
238 				{
239 					fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
240 				}
241 				break;
242 			}
243 			if (!checks.enable_checker(arg))
244 			{
245 				fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
246 			}
247 			break;
248 		}
249 		case 's':
250 		{
251 			sort = true;
252 			break;
253 		}
254 		case 'i':
255 		{
256 			tree.add_include_path(optarg);
257 			break;
258 		}
259 		// Should quiet warnings, but for now is silently ignored.
260 		case 'q':
261 			break;
262 		case 'R':
263 			tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
264 			break;
265 		case 'S':
266 			tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
267 			break;
268 		case 'p':
269 			tree.set_blob_padding(strtoll(optarg, 0, 10));
270 			break;
271 		case 'P':
272 			if (!tree.parse_define(optarg))
273 			{
274 				fprintf(stderr, "Invalid predefine value %s\n",
275 				        optarg);
276 			}
277 			break;
278 		default:
279 			fprintf(stderr, "Unknown option %c\n", ch);
280 			return EXIT_FAILURE;
281 		}
282 	}
283 	if (optind < argc)
284 	{
285 		in_file = argv[optind];
286 	}
287 	if (depfile != 0)
288 	{
289 		fputs(outfile_name, depfile);
290 		fputs(": ", depfile);
291 		fputs(in_file, depfile);
292 	}
293 	clock_t c1 = clock();
294 	(tree.*read_fn)(in_file, depfile);
295 	// Override the boot CPU found in the header, if we're loading from dtb
296 	if (boot_cpu_specified)
297 	{
298 		tree.set_boot_cpu(boot_cpu);
299 	}
300 	if (sort)
301 	{
302 		tree.sort();
303 	}
304 	if (depfile != 0)
305 	{
306 		putc('\n', depfile);
307 		fclose(depfile);
308 	}
309 	if (!(tree.is_valid() || keep_going))
310 	{
311 		fprintf(stderr, "Failed to parse tree.\n");
312 		return EXIT_FAILURE;
313 	}
314 	clock_t c2 = clock();
315 	if (!(checks.run_checks(&tree, true) || keep_going))
316 	{
317 		return EXIT_FAILURE;
318 	}
319 	clock_t c3 = clock();
320 	(tree.*write_fn)(outfile);
321 	close(outfile);
322 	clock_t c4 = clock();
323 
324 	if (debug_mode)
325 	{
326 		struct rusage r;
327 
328 		getrusage(RUSAGE_SELF, &r);
329 		fprintf(stderr, "Peak memory usage: %ld bytes\n", r.ru_maxrss);
330 		fprintf(stderr, "Setup and option parsing took %f seconds\n",
331 				((double)(c1-c0))/CLOCKS_PER_SEC);
332 		fprintf(stderr, "Parsing took %f seconds\n",
333 				((double)(c2-c1))/CLOCKS_PER_SEC);
334 		fprintf(stderr, "Checking took %f seconds\n",
335 				((double)(c3-c2))/CLOCKS_PER_SEC);
336 		fprintf(stderr, "Generating output took %f seconds\n",
337 				((double)(c4-c3))/CLOCKS_PER_SEC);
338 		fprintf(stderr, "Total time: %f seconds\n",
339 				((double)(c4-c0))/CLOCKS_PER_SEC);
340 		// This is not needed, but keeps valgrind quiet.
341 		fclose(stdin);
342 	}
343 	return EXIT_SUCCESS;
344 }
345 
346