2 * Copyright (c) 2013 David Chisnall
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/resource.h>
44 #include "checking.hh"
49 * The current major version of the tool.
51 int version_major = 0;
53 * The current minor version of the tool.
55 int version_minor = 4;
57 * The current patch level of the tool.
59 int version_patch = 0;
61 static void usage(const char* argv0)
63 fprintf(stderr, "Usage:\n"
64 "\t%s\t[-fhsv] [-b boot_cpu_id] [-d dependency_file]"
65 "[-E [no-]checker_name]\n"
66 "\t\t[-H phandle_format] [-I input_format]"
67 "[-O output_format]\n"
68 "\t\t[-o output_file] [-R entries] [-S bytes] [-p bytes]"
70 "\t\t-W [no-]checker_name] input_file\n", basename(argv0));
74 * Prints the current version of this program..
76 static void version(const char* progname)
78 fprintf(stderr, "Version: %s %d.%d.%d\n", progname, version_major,
79 version_minor, version_patch);
82 using fdt::device_tree;
85 main(int argc, char **argv)
88 int outfile = fileno(stdout);
89 const char *outfile_name = "-";
90 const char *in_file = "-";
92 bool debug_mode = false;
93 void (device_tree::*write_fn)(int) = &device_tree::write_binary;
94 void (device_tree::*read_fn)(const char*, FILE*) =
95 &device_tree::parse_dts;
97 bool boot_cpu_specified = false;
98 bool keep_going = false;
100 clock_t c0 = clock();
101 class device_tree tree;
102 fdt::checking::check_manager checks;
103 const char *options = "hqI:O:o:V:d:R:S:p:b:fisvH:W:E:DP:";
105 // Don't forget to update the man page if any more options are added.
106 while ((ch = getopt(argc, argv, options)) != -1)
118 string arg = string(optarg);
119 if (arg == string("dtb"))
121 read_fn = &device_tree::parse_dtb;
123 else if (arg == string("dts"))
125 read_fn = &device_tree::parse_dts;
129 fprintf(stderr, "Unknown input format: %s\n", optarg);
136 string arg = string(optarg);
137 if (arg == string("dtb"))
139 write_fn = &device_tree::write_binary;
141 else if (arg == string("asm"))
143 write_fn = &device_tree::write_asm;
145 else if (arg == string("dts"))
147 write_fn = &device_tree::write_dts;
151 fprintf(stderr, "Unknown output format: %s\n", optarg);
158 outfile_name = optarg;
159 outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
162 perror("Unable to open output file");
171 if (string(optarg) != string("17"))
173 fprintf(stderr, "Unknown output format version: %s\n", optarg);
183 if (string(optarg) == string("-"))
189 depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
192 perror("Unable to open dependency file");
200 string arg = string(optarg);
201 if (arg == string("both"))
203 tree.set_phandle_format(device_tree::BOTH);
205 else if (arg == string("epapr"))
207 tree.set_phandle_format(device_tree::EPAPR);
209 else if (arg == string("linux"))
211 tree.set_phandle_format(device_tree::LINUX);
215 fprintf(stderr, "Unknown phandle format: %s\n", optarg);
221 // Don't bother to check if strtoll fails, just
222 // use the 0 it returns.
223 boot_cpu = (uint32_t)strtoll(optarg, 0, 10);
224 boot_cpu_specified = true;
232 string arg = string(optarg);
233 if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
235 arg = string(optarg+3);
236 if (!checks.disable_checker(arg))
238 fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
242 if (!checks.enable_checker(arg))
244 fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
255 tree.add_include_path(optarg);
258 // Should quiet warnings, but for now is silently ignored.
262 tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
265 tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
268 tree.set_blob_padding(strtoll(optarg, 0, 10));
271 if (!tree.parse_define(optarg))
273 fprintf(stderr, "Invalid predefine value %s\n",
278 fprintf(stderr, "Unknown option %c\n", ch);
284 in_file = argv[optind];
288 fputs(outfile_name, depfile);
289 fputs(": ", depfile);
290 fputs(in_file, depfile);
292 clock_t c1 = clock();
293 (tree.*read_fn)(in_file, depfile);
294 // Override the boot CPU found in the header, if we're loading from dtb
295 if (boot_cpu_specified)
297 tree.set_boot_cpu(boot_cpu);
308 if (!(tree.is_valid() || keep_going))
310 fprintf(stderr, "Failed to parse tree. Unhappy face!\n");
313 clock_t c2 = clock();
314 if (!(checks.run_checks(&tree, true) || keep_going))
318 clock_t c3 = clock();
319 (tree.*write_fn)(outfile);
321 clock_t c4 = clock();
327 getrusage(RUSAGE_SELF, &r);
328 fprintf(stderr, "Peak memory usage: %ld bytes\n", r.ru_maxrss);
329 fprintf(stderr, "Setup and option parsing took %f seconds\n",
330 ((double)(c1-c0))/CLOCKS_PER_SEC);
331 fprintf(stderr, "Parsing took %f seconds\n",
332 ((double)(c2-c1))/CLOCKS_PER_SEC);
333 fprintf(stderr, "Checking took %f seconds\n",
334 ((double)(c3-c2))/CLOCKS_PER_SEC);
335 fprintf(stderr, "Generating output took %f seconds\n",
336 ((double)(c4-c3))/CLOCKS_PER_SEC);
337 fprintf(stderr, "Total time: %f seconds\n",
338 ((double)(c4-c0))/CLOCKS_PER_SEC);
339 // This is not needed, but keeps valgrind quiet.