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