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