2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 David Chisnall
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/resource.h>
46 #include "checking.hh"
55 * The current major version of the tool.
57 int version_major = 0;
58 int version_major_compatible = 1;
60 * The current minor version of the tool.
62 int version_minor = 5;
63 int version_minor_compatible = 4;
65 * The current patch level of the tool.
67 int version_patch = 0;
68 int version_patch_compatible = 0;
70 void usage(const string &argv0)
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]"
79 "\t\t-W [no-]checker_name] input_file\n", basename(argv0).c_str());
83 * Prints the current version of this program..
85 void version(const char* progname)
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);
93 } // Anonymous namespace
95 using fdt::device_tree;
98 main(int argc, char **argv)
101 int outfile = fileno(stdout);
102 const char *outfile_name = "-";
103 const char *in_file = "-";
105 bool debug_mode = false;
106 auto write_fn = &device_tree::write_binary;
107 auto read_fn = &device_tree::parse_dts;
109 bool boot_cpu_specified = false;
110 bool keep_going = 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:";
117 // Don't forget to update the man page if any more options are added.
118 while ((ch = getopt(argc, argv, options)) != -1)
129 tree.write_symbols = true;
136 read_fn = &device_tree::parse_dtb;
138 else if (arg == "dts")
140 read_fn = &device_tree::parse_dts;
144 fprintf(stderr, "Unknown input format: %s\n", optarg);
154 write_fn = &device_tree::write_binary;
156 else if (arg == "asm")
158 write_fn = &device_tree::write_asm;
160 else if (arg == "dts")
162 write_fn = &device_tree::write_dts;
166 fprintf(stderr, "Unknown output format: %s\n", optarg);
173 outfile_name = optarg;
174 if (strcmp(outfile_name, "-") != 0)
176 outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
179 perror("Unable to open output file");
189 if (string(optarg) != "17")
191 fprintf(stderr, "Unknown output format version: %s\n", optarg);
201 if (string(optarg) == "-")
207 depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
210 perror("Unable to open dependency file");
221 tree.set_phandle_format(device_tree::BOTH);
223 else if (arg == "epapr")
225 tree.set_phandle_format(device_tree::EPAPR);
227 else if (arg == "linux")
229 tree.set_phandle_format(device_tree::LINUX);
233 fprintf(stderr, "Unknown phandle format: %s\n", optarg);
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;
251 if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
253 arg = string(optarg+3);
254 if (!checks.disable_checker(arg))
256 fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
260 if (!checks.enable_checker(arg))
262 fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
273 tree.add_include_path(optarg);
276 // Should quiet warnings, but for now is silently ignored.
280 tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
283 tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
286 tree.set_blob_padding(strtoll(optarg, 0, 10));
289 if (!tree.parse_define(optarg))
291 fprintf(stderr, "Invalid predefine value %s\n",
296 fprintf(stderr, "Unknown option %c\n", ch);
302 in_file = argv[optind];
306 fputs(outfile_name, depfile);
307 fputs(": ", depfile);
308 fputs(in_file, depfile);
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)
315 tree.set_boot_cpu(boot_cpu);
326 if (!(tree.is_valid() || keep_going))
328 fprintf(stderr, "Failed to parse tree.\n");
331 clock_t c2 = clock();
332 if (!(checks.run_checks(&tree, true) || keep_going))
336 clock_t c3 = clock();
337 (tree.*write_fn)(outfile);
339 clock_t c4 = clock();
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.