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