xref: /freebsd/usr.bin/dtc/dtc.cc (revision b00ab754)
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 <time.h>
42 #include <unistd.h>
43 
44 
45 #include "fdt.hh"
46 #include "checking.hh"
47 #include "util.hh"
48 
49 using namespace dtc;
50 using std::string;
51 
52 namespace {
53 
54 /**
55  * The current major version of the tool.
56  */
57 int version_major = 0;
58 int version_major_compatible = 1;
59 /**
60  * The current minor version of the tool.
61  */
62 int version_minor = 5;
63 int version_minor_compatible = 4;
64 /**
65  * The current patch level of the tool.
66  */
67 int version_patch = 0;
68 int version_patch_compatible = 0;
69 
70 void usage(const string &argv0)
71 {
72 	fprintf(stderr, "Usage:\n"
73 		"\t%s\t[-fhsv@] [-b boot_cpu_id] [-d dependency_file]"
74 			"[-E [no-]checker_name]\n"
75 		"\t\t[-H phandle_format] [-I input_format]"
76 			"[-O output_format]\n"
77 		"\t\t[-o output_file] [-R entries] [-S bytes] [-p bytes]"
78 			"[-V blob_version]\n"
79 		"\t\t-W [no-]checker_name] input_file\n", basename(argv0).c_str());
80 }
81 
82 /**
83  * Prints the current version of this program..
84  */
85 void version(const char* progname)
86 {
87 	fprintf(stdout, "Version: %s %d.%d.%d compatible with gpl dtc %d.%d.%d\n", progname,
88 		version_major, version_minor, version_patch,
89 		version_major_compatible, version_minor_compatible,
90 		version_patch_compatible);
91 }
92 
93 } // Anonymous namespace
94 
95 using fdt::device_tree;
96 
97 int
98 main(int argc, char **argv)
99 {
100 	int ch;
101 	int outfile = fileno(stdout);
102 	const char *outfile_name = "-";
103 	const char *in_file = "-";
104 	FILE *depfile = 0;
105 	bool debug_mode = false;
106 	auto write_fn = &device_tree::write_binary;
107 	auto read_fn = &device_tree::parse_dts;
108 	uint32_t boot_cpu;
109 	bool boot_cpu_specified = false;
110 	bool keep_going = false;
111 	bool sort = false;
112 	clock_t c0 = clock();
113 	class device_tree tree;
114 	fdt::checking::check_manager checks;
115 	const char *options = "@hqI:O:o:V:d:R:S:p:b:fi:svH:W:E:DP:";
116 
117 	// Don't forget to update the man page if any more options are added.
118 	while ((ch = getopt(argc, argv, options)) != -1)
119 	{
120 		switch (ch)
121 		{
122 		case 'h':
123 			usage(argv[0]);
124 			return EXIT_SUCCESS;
125 		case 'v':
126 			version(argv[0]);
127 			return EXIT_SUCCESS;
128 		case '@':
129 			tree.write_symbols = true;
130 			break;
131 		case 'I':
132 		{
133 			string arg(optarg);
134 			if (arg == "dtb")
135 			{
136 				read_fn = &device_tree::parse_dtb;
137 			}
138 			else if (arg == "dts")
139 			{
140 				read_fn = &device_tree::parse_dts;
141 			}
142 			else
143 			{
144 				fprintf(stderr, "Unknown input format: %s\n", optarg);
145 				return EXIT_FAILURE;
146 			}
147 			break;
148 		}
149 		case 'O':
150 		{
151 			string arg(optarg);
152 			if (arg == "dtb")
153 			{
154 				write_fn = &device_tree::write_binary;
155 			}
156 			else if (arg == "asm")
157 			{
158 				write_fn = &device_tree::write_asm;
159 			}
160 			else if (arg == "dts")
161 			{
162 				write_fn = &device_tree::write_dts;
163 			}
164 			else
165 			{
166 				fprintf(stderr, "Unknown output format: %s\n", optarg);
167 				return EXIT_FAILURE;
168 			}
169 			break;
170 		}
171 		case 'o':
172 		{
173 			outfile_name = optarg;
174 			if (strcmp(outfile_name, "-") != 0)
175 			{
176 				outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
177 				if (outfile == -1)
178 				{
179 					perror("Unable to open output file");
180 					return EXIT_FAILURE;
181 				}
182 			}
183 			break;
184 		}
185 		case 'D':
186 			debug_mode = true;
187 			break;
188 		case 'V':
189 			if (string(optarg) != "17")
190 			{
191 				fprintf(stderr, "Unknown output format version: %s\n", optarg);
192 				return EXIT_FAILURE;
193 			}
194 			break;
195 		case 'd':
196 		{
197 			if (depfile != 0)
198 			{
199 				fclose(depfile);
200 			}
201 			if (string(optarg) == "-")
202 			{
203 				depfile = stdout;
204 			}
205 			else
206 			{
207 				depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
208 				if (depfile == 0)
209 				{
210 					perror("Unable to open dependency file");
211 					return EXIT_FAILURE;
212 				}
213 			}
214 			break;
215 		}
216 		case 'H':
217 		{
218 			string arg(optarg);
219 			if (arg == "both")
220 			{
221 				tree.set_phandle_format(device_tree::BOTH);
222 			}
223 			else if (arg == "epapr")
224 			{
225 				tree.set_phandle_format(device_tree::EPAPR);
226 			}
227 			else if (arg == "linux")
228 			{
229 				tree.set_phandle_format(device_tree::LINUX);
230 			}
231 			else
232 			{
233 				fprintf(stderr, "Unknown phandle format: %s\n", optarg);
234 				return EXIT_FAILURE;
235 			}
236 			break;
237 		}
238 		case 'b':
239 			// Don't bother to check if strtoll fails, just
240 			// use the 0 it returns.
241 			boot_cpu = (uint32_t)strtoll(optarg, 0, 10);
242 			boot_cpu_specified = true;
243 			break;
244 		case 'f':
245 			keep_going = true;
246 			break;
247 		case 'W':
248 		case 'E':
249 		{
250 			string arg(optarg);
251 			if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
252 			{
253 				arg = string(optarg+3);
254 				if (!checks.disable_checker(arg))
255 				{
256 					fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
257 				}
258 				break;
259 			}
260 			if (!checks.enable_checker(arg))
261 			{
262 				fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
263 			}
264 			break;
265 		}
266 		case 's':
267 		{
268 			sort = true;
269 			break;
270 		}
271 		case 'i':
272 		{
273 			tree.add_include_path(optarg);
274 			break;
275 		}
276 		// Should quiet warnings, but for now is silently ignored.
277 		case 'q':
278 			break;
279 		case 'R':
280 			tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
281 			break;
282 		case 'S':
283 			tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
284 			break;
285 		case 'p':
286 			tree.set_blob_padding(strtoll(optarg, 0, 10));
287 			break;
288 		case 'P':
289 			if (!tree.parse_define(optarg))
290 			{
291 				fprintf(stderr, "Invalid predefine value %s\n",
292 				        optarg);
293 			}
294 			break;
295 		default:
296 			fprintf(stderr, "Unknown option %c\n", ch);
297 			return EXIT_FAILURE;
298 		}
299 	}
300 	if (optind < argc)
301 	{
302 		in_file = argv[optind];
303 	}
304 	if (depfile != 0)
305 	{
306 		fputs(outfile_name, depfile);
307 		fputs(": ", depfile);
308 		fputs(in_file, depfile);
309 	}
310 	clock_t c1 = clock();
311 	(tree.*read_fn)(in_file, depfile);
312 	// Override the boot CPU found in the header, if we're loading from dtb
313 	if (boot_cpu_specified)
314 	{
315 		tree.set_boot_cpu(boot_cpu);
316 	}
317 	if (sort)
318 	{
319 		tree.sort();
320 	}
321 	if (depfile != 0)
322 	{
323 		putc('\n', depfile);
324 		fclose(depfile);
325 	}
326 	if (!(tree.is_valid() || keep_going))
327 	{
328 		fprintf(stderr, "Failed to parse tree.\n");
329 		return EXIT_FAILURE;
330 	}
331 	clock_t c2 = clock();
332 	if (!(checks.run_checks(&tree, true) || keep_going))
333 	{
334 		return EXIT_FAILURE;
335 	}
336 	clock_t c3 = clock();
337 	(tree.*write_fn)(outfile);
338 	close(outfile);
339 	clock_t c4 = clock();
340 
341 	if (debug_mode)
342 	{
343 		struct rusage r;
344 
345 		getrusage(RUSAGE_SELF, &r);
346 		fprintf(stderr, "Peak memory usage: %ld bytes\n", r.ru_maxrss);
347 		fprintf(stderr, "Setup and option parsing took %f seconds\n",
348 				((double)(c1-c0))/CLOCKS_PER_SEC);
349 		fprintf(stderr, "Parsing took %f seconds\n",
350 				((double)(c2-c1))/CLOCKS_PER_SEC);
351 		fprintf(stderr, "Checking took %f seconds\n",
352 				((double)(c3-c2))/CLOCKS_PER_SEC);
353 		fprintf(stderr, "Generating output took %f seconds\n",
354 				((double)(c4-c3))/CLOCKS_PER_SEC);
355 		fprintf(stderr, "Total time: %f seconds\n",
356 				((double)(c4-c0))/CLOCKS_PER_SEC);
357 		// This is not needed, but keeps valgrind quiet.
358 		fclose(stdin);
359 	}
360 	return EXIT_SUCCESS;
361 }
362 
363