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