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