2013-01-22 17:49:51 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2013 David Chisnall
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by SRI International and the University of
|
|
|
|
* Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
|
|
|
|
* ("CTSRD"), as part of the DARPA CRASH research programme.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2013-01-23 08:54:34 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-01-22 17:49:51 +00:00
|
|
|
|
|
|
|
#include "fdt.hh"
|
|
|
|
#include "checking.hh"
|
2016-10-07 12:57:35 +00:00
|
|
|
#include "util.hh"
|
2013-01-22 17:49:51 +00:00
|
|
|
|
|
|
|
using namespace dtc;
|
2016-10-07 12:57:35 +00:00
|
|
|
using std::string;
|
2013-01-22 17:49:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current major version of the tool.
|
|
|
|
*/
|
|
|
|
int version_major = 0;
|
|
|
|
/**
|
|
|
|
* The current minor version of the tool.
|
|
|
|
*/
|
2017-04-17 17:23:19 +00:00
|
|
|
int version_minor = 5;
|
2013-01-22 17:49:51 +00:00
|
|
|
/**
|
|
|
|
* The current patch level of the tool.
|
|
|
|
*/
|
|
|
|
int version_patch = 0;
|
|
|
|
|
2016-10-07 12:57:35 +00:00
|
|
|
static void usage(const string &argv0)
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage:\n"
|
2017-04-17 17:23:19 +00:00
|
|
|
"\t%s\t[-fhsv@] [-b boot_cpu_id] [-d dependency_file]"
|
2013-01-22 17:49:51 +00:00
|
|
|
"[-E [no-]checker_name]\n"
|
|
|
|
"\t\t[-H phandle_format] [-I input_format]"
|
|
|
|
"[-O output_format]\n"
|
|
|
|
"\t\t[-o output_file] [-R entries] [-S bytes] [-p bytes]"
|
|
|
|
"[-V blob_version]\n"
|
2016-10-07 12:57:35 +00:00
|
|
|
"\t\t-W [no-]checker_name] input_file\n", basename(argv0).c_str());
|
2013-01-22 17:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints the current version of this program..
|
|
|
|
*/
|
|
|
|
static void version(const char* progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Version: %s %d.%d.%d\n", progname, version_major,
|
|
|
|
version_minor, version_patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
using fdt::device_tree;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int outfile = fileno(stdout);
|
|
|
|
const char *outfile_name = "-";
|
|
|
|
const char *in_file = "-";
|
|
|
|
FILE *depfile = 0;
|
|
|
|
bool debug_mode = false;
|
2016-10-07 12:57:35 +00:00
|
|
|
auto write_fn = &device_tree::write_binary;
|
|
|
|
auto read_fn = &device_tree::parse_dts;
|
2013-01-22 17:49:51 +00:00
|
|
|
uint32_t boot_cpu;
|
|
|
|
bool boot_cpu_specified = false;
|
|
|
|
bool keep_going = false;
|
|
|
|
bool sort = false;
|
|
|
|
clock_t c0 = clock();
|
|
|
|
class device_tree tree;
|
|
|
|
fdt::checking::check_manager checks;
|
2017-04-17 17:23:19 +00:00
|
|
|
const char *options = "@hqI:O:o:V:d:R:S:p:b:fi:svH:W:E:DP:";
|
2013-01-22 17:49:51 +00:00
|
|
|
|
|
|
|
// Don't forget to update the man page if any more options are added.
|
|
|
|
while ((ch = getopt(argc, argv, options)) != -1)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'v':
|
|
|
|
version(argv[0]);
|
|
|
|
return EXIT_SUCCESS;
|
2017-04-17 17:23:19 +00:00
|
|
|
case '@':
|
|
|
|
tree.write_symbols = true;
|
|
|
|
break;
|
2013-01-22 17:49:51 +00:00
|
|
|
case 'I':
|
|
|
|
{
|
2016-10-07 12:57:35 +00:00
|
|
|
string arg(optarg);
|
|
|
|
if (arg == "dtb")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
read_fn = &device_tree::parse_dtb;
|
|
|
|
}
|
2016-10-07 12:57:35 +00:00
|
|
|
else if (arg == "dts")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
read_fn = &device_tree::parse_dts;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown input format: %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'O':
|
|
|
|
{
|
2016-10-07 12:57:35 +00:00
|
|
|
string arg(optarg);
|
|
|
|
if (arg == "dtb")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
write_fn = &device_tree::write_binary;
|
|
|
|
}
|
2016-10-07 12:57:35 +00:00
|
|
|
else if (arg == "asm")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
write_fn = &device_tree::write_asm;
|
|
|
|
}
|
2016-10-07 12:57:35 +00:00
|
|
|
else if (arg == "dts")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
write_fn = &device_tree::write_dts;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown output format: %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
{
|
|
|
|
outfile_name = optarg;
|
|
|
|
outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
|
|
|
if (outfile == -1)
|
|
|
|
{
|
|
|
|
perror("Unable to open output file");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'D':
|
|
|
|
debug_mode = true;
|
|
|
|
break;
|
|
|
|
case 'V':
|
2016-10-07 12:57:35 +00:00
|
|
|
if (string(optarg) != "17")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown output format version: %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
{
|
|
|
|
if (depfile != 0)
|
|
|
|
{
|
|
|
|
fclose(depfile);
|
|
|
|
}
|
2016-10-07 12:57:35 +00:00
|
|
|
if (string(optarg) == "-")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
depfile = stdout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
|
|
|
|
if (depfile == 0)
|
|
|
|
{
|
|
|
|
perror("Unable to open dependency file");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'H':
|
|
|
|
{
|
2016-10-07 12:57:35 +00:00
|
|
|
string arg(optarg);
|
|
|
|
if (arg == "both")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
tree.set_phandle_format(device_tree::BOTH);
|
|
|
|
}
|
2016-10-07 12:57:35 +00:00
|
|
|
else if (arg == "epapr")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
tree.set_phandle_format(device_tree::EPAPR);
|
|
|
|
}
|
2016-10-07 12:57:35 +00:00
|
|
|
else if (arg == "linux")
|
2013-01-22 17:49:51 +00:00
|
|
|
{
|
|
|
|
tree.set_phandle_format(device_tree::LINUX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown phandle format: %s\n", optarg);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'b':
|
|
|
|
// Don't bother to check if strtoll fails, just
|
|
|
|
// use the 0 it returns.
|
|
|
|
boot_cpu = (uint32_t)strtoll(optarg, 0, 10);
|
|
|
|
boot_cpu_specified = true;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
keep_going = true;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
case 'E':
|
|
|
|
{
|
2016-10-07 12:57:35 +00:00
|
|
|
string arg(optarg);
|
2013-01-22 17:49:51 +00:00
|
|
|
if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
|
|
|
|
{
|
|
|
|
arg = string(optarg+3);
|
|
|
|
if (!checks.disable_checker(arg))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!checks.enable_checker(arg))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
sort = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
tree.add_include_path(optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Should quiet warnings, but for now is silently ignored.
|
|
|
|
case 'q':
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
tree.set_blob_padding(strtoll(optarg, 0, 10));
|
|
|
|
break;
|
2013-08-19 12:37:13 +00:00
|
|
|
case 'P':
|
|
|
|
if (!tree.parse_define(optarg))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid predefine value %s\n",
|
|
|
|
optarg);
|
|
|
|
}
|
|
|
|
break;
|
2013-01-22 17:49:51 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown option %c\n", ch);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
in_file = argv[optind];
|
|
|
|
}
|
|
|
|
if (depfile != 0)
|
|
|
|
{
|
|
|
|
fputs(outfile_name, depfile);
|
|
|
|
fputs(": ", depfile);
|
|
|
|
fputs(in_file, depfile);
|
|
|
|
}
|
|
|
|
clock_t c1 = clock();
|
|
|
|
(tree.*read_fn)(in_file, depfile);
|
|
|
|
// Override the boot CPU found in the header, if we're loading from dtb
|
|
|
|
if (boot_cpu_specified)
|
|
|
|
{
|
|
|
|
tree.set_boot_cpu(boot_cpu);
|
|
|
|
}
|
|
|
|
if (sort)
|
|
|
|
{
|
|
|
|
tree.sort();
|
|
|
|
}
|
|
|
|
if (depfile != 0)
|
|
|
|
{
|
|
|
|
putc('\n', depfile);
|
|
|
|
fclose(depfile);
|
|
|
|
}
|
|
|
|
if (!(tree.is_valid() || keep_going))
|
|
|
|
{
|
2016-10-07 12:57:35 +00:00
|
|
|
fprintf(stderr, "Failed to parse tree.\n");
|
2013-01-22 17:49:51 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
clock_t c2 = clock();
|
|
|
|
if (!(checks.run_checks(&tree, true) || keep_going))
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
clock_t c3 = clock();
|
|
|
|
(tree.*write_fn)(outfile);
|
|
|
|
close(outfile);
|
|
|
|
clock_t c4 = clock();
|
|
|
|
|
|
|
|
if (debug_mode)
|
|
|
|
{
|
|
|
|
struct rusage r;
|
|
|
|
|
|
|
|
getrusage(RUSAGE_SELF, &r);
|
|
|
|
fprintf(stderr, "Peak memory usage: %ld bytes\n", r.ru_maxrss);
|
|
|
|
fprintf(stderr, "Setup and option parsing took %f seconds\n",
|
|
|
|
((double)(c1-c0))/CLOCKS_PER_SEC);
|
|
|
|
fprintf(stderr, "Parsing took %f seconds\n",
|
|
|
|
((double)(c2-c1))/CLOCKS_PER_SEC);
|
|
|
|
fprintf(stderr, "Checking took %f seconds\n",
|
|
|
|
((double)(c3-c2))/CLOCKS_PER_SEC);
|
|
|
|
fprintf(stderr, "Generating output took %f seconds\n",
|
|
|
|
((double)(c4-c3))/CLOCKS_PER_SEC);
|
|
|
|
fprintf(stderr, "Total time: %f seconds\n",
|
|
|
|
((double)(c4-c0))/CLOCKS_PER_SEC);
|
|
|
|
// This is not needed, but keeps valgrind quiet.
|
|
|
|
fclose(stdin);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|