2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2007-12-18 12:06:42 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 23:04:11 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2007-12-18 12:06:42 +08:00
|
|
|
#include "dtc.h"
|
|
|
|
#include "srcpos.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command line options
|
|
|
|
*/
|
|
|
|
int quiet; /* Level of quietness */
|
2021-10-26 00:05:45 +08:00
|
|
|
unsigned int reservenum;/* Number of memory reservation slots */
|
2007-12-18 12:06:42 +08:00
|
|
|
int minsize; /* Minimum blob size */
|
|
|
|
int padsize; /* Additional padding to blob */
|
2017-01-05 00:45:20 +08:00
|
|
|
int alignsize; /* Additional padding to blob accroding to the alignsize */
|
2017-10-04 00:37:04 +08:00
|
|
|
int phandle_format = PHANDLE_EPAPR; /* Use linux,phandle or phandle properties */
|
2017-01-05 00:45:20 +08:00
|
|
|
int generate_symbols; /* enable symbols & fixup support */
|
|
|
|
int generate_fixups; /* suppress generation of fixups on symbol support */
|
|
|
|
int auto_label_aliases; /* auto generate labels -> aliases */
|
2018-11-29 08:37:35 +08:00
|
|
|
int annotate; /* Level of annotation: 1 for input source location
|
|
|
|
>1 for full input source location. */
|
2017-01-05 00:45:20 +08:00
|
|
|
|
|
|
|
static int is_power_of_2(int x)
|
|
|
|
{
|
|
|
|
return (x > 0) && ((x & (x - 1)) == 0);
|
|
|
|
}
|
2007-12-18 12:06:42 +08:00
|
|
|
|
2008-08-07 10:24:17 +08:00
|
|
|
static void fill_fullpaths(struct node *tree, const char *prefix)
|
2007-12-18 12:06:42 +08:00
|
|
|
{
|
|
|
|
struct node *child;
|
|
|
|
const char *unit;
|
|
|
|
|
|
|
|
tree->fullpath = join_path(prefix, tree->name);
|
|
|
|
|
|
|
|
unit = strchr(tree->name, '@');
|
|
|
|
if (unit)
|
|
|
|
tree->basenamelen = unit - tree->name;
|
|
|
|
else
|
|
|
|
tree->basenamelen = strlen(tree->name);
|
|
|
|
|
|
|
|
for_each_child(tree, child)
|
|
|
|
fill_fullpaths(child, tree->fullpath);
|
|
|
|
}
|
|
|
|
|
2014-01-21 20:54:49 +08:00
|
|
|
/* Usage related data. */
|
|
|
|
static const char usage_synopsis[] = "dtc [options] <input file>";
|
2018-11-29 08:37:35 +08:00
|
|
|
static const char usage_short_opts[] = "qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@AThv";
|
2014-01-21 20:54:49 +08:00
|
|
|
static struct option const usage_long_opts[] = {
|
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
|
|
|
{"in-format", a_argument, NULL, 'I'},
|
|
|
|
{"out", a_argument, NULL, 'o'},
|
|
|
|
{"out-format", a_argument, NULL, 'O'},
|
|
|
|
{"out-version", a_argument, NULL, 'V'},
|
|
|
|
{"out-dependency", a_argument, NULL, 'd'},
|
|
|
|
{"reserve", a_argument, NULL, 'R'},
|
|
|
|
{"space", a_argument, NULL, 'S'},
|
|
|
|
{"pad", a_argument, NULL, 'p'},
|
2017-01-05 00:45:20 +08:00
|
|
|
{"align", a_argument, NULL, 'a'},
|
2014-01-21 20:54:49 +08:00
|
|
|
{"boot-cpu", a_argument, NULL, 'b'},
|
|
|
|
{"force", no_argument, NULL, 'f'},
|
|
|
|
{"include", a_argument, NULL, 'i'},
|
|
|
|
{"sort", no_argument, NULL, 's'},
|
|
|
|
{"phandle", a_argument, NULL, 'H'},
|
|
|
|
{"warning", a_argument, NULL, 'W'},
|
|
|
|
{"error", a_argument, NULL, 'E'},
|
2017-01-05 00:45:20 +08:00
|
|
|
{"symbols", no_argument, NULL, '@'},
|
|
|
|
{"auto-alias", no_argument, NULL, 'A'},
|
2018-11-29 08:37:35 +08:00
|
|
|
{"annotate", no_argument, NULL, 'T'},
|
2014-01-21 20:54:49 +08:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{NULL, no_argument, NULL, 0x0},
|
|
|
|
};
|
|
|
|
static const char * const usage_opts_help[] = {
|
|
|
|
"\n\tQuiet: -q suppress warnings, -qq errors, -qqq all",
|
|
|
|
"\n\tInput formats are:\n"
|
|
|
|
"\t\tdts - device tree source text\n"
|
|
|
|
"\t\tdtb - device tree blob\n"
|
|
|
|
"\t\tfs - /proc/device-tree style directory",
|
|
|
|
"\n\tOutput file",
|
|
|
|
"\n\tOutput formats are:\n"
|
|
|
|
"\t\tdts - device tree source text\n"
|
|
|
|
"\t\tdtb - device tree blob\n"
|
2018-09-13 21:59:25 +08:00
|
|
|
#ifndef NO_YAML
|
|
|
|
"\t\tyaml - device tree encoded as YAML\n"
|
|
|
|
#endif
|
2014-01-21 20:54:49 +08:00
|
|
|
"\t\tasm - assembler source",
|
2018-02-28 07:40:38 +08:00
|
|
|
"\n\tBlob version to produce, defaults to "stringify(DEFAULT_FDT_VERSION)" (for dtb and asm output)",
|
2014-01-21 20:54:49 +08:00
|
|
|
"\n\tOutput dependency file",
|
2015-04-30 05:00:05 +08:00
|
|
|
"\n\tMake space for <number> reserve map entries (for dtb and asm output)",
|
2014-01-21 20:54:49 +08:00
|
|
|
"\n\tMake the blob at least <bytes> long (extra space)",
|
|
|
|
"\n\tAdd padding to the blob of <bytes> long (extra space)",
|
2017-01-05 00:45:20 +08:00
|
|
|
"\n\tMake the blob align to the <bytes> (extra space)",
|
2014-01-21 20:54:49 +08:00
|
|
|
"\n\tSet the physical boot cpu",
|
|
|
|
"\n\tTry to produce output even if the input tree has errors",
|
|
|
|
"\n\tAdd a path to search for include files",
|
|
|
|
"\n\tSort nodes and properties before outputting (useful for comparing trees)",
|
|
|
|
"\n\tValid phandle formats are:\n"
|
|
|
|
"\t\tlegacy - \"linux,phandle\" properties only\n"
|
|
|
|
"\t\tepapr - \"phandle\" properties only\n"
|
|
|
|
"\t\tboth - Both \"linux,phandle\" and \"phandle\" properties",
|
|
|
|
"\n\tEnable/disable warnings (prefix with \"no-\")",
|
|
|
|
"\n\tEnable/disable errors (prefix with \"no-\")",
|
2017-01-05 00:45:20 +08:00
|
|
|
"\n\tEnable generation of symbols",
|
|
|
|
"\n\tEnable auto-alias of labels",
|
2018-11-29 08:37:35 +08:00
|
|
|
"\n\tAnnotate output .dts with input source file and line (-T -T for more details)",
|
2014-01-21 20:54:49 +08:00
|
|
|
"\n\tPrint this help and exit",
|
|
|
|
"\n\tPrint version and exit",
|
|
|
|
NULL,
|
|
|
|
};
|
2007-12-18 12:06:42 +08:00
|
|
|
|
2016-01-26 23:04:11 +08:00
|
|
|
static const char *guess_type_by_name(const char *fname, const char *fallback)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = strrchr(fname, '.');
|
|
|
|
if (s == NULL)
|
|
|
|
return fallback;
|
|
|
|
if (!strcasecmp(s, ".dts"))
|
|
|
|
return "dts";
|
2018-09-13 21:59:25 +08:00
|
|
|
if (!strcasecmp(s, ".yaml"))
|
|
|
|
return "yaml";
|
2021-02-04 05:26:03 +08:00
|
|
|
if (!strcasecmp(s, ".dtbo"))
|
|
|
|
return "dtb";
|
2016-01-26 23:04:11 +08:00
|
|
|
if (!strcasecmp(s, ".dtb"))
|
|
|
|
return "dtb";
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *guess_input_format(const char *fname, const char *fallback)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2017-03-21 22:01:08 +08:00
|
|
|
fdt32_t magic;
|
2016-01-26 23:04:11 +08:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (stat(fname, &statbuf) != 0)
|
|
|
|
return fallback;
|
|
|
|
|
|
|
|
if (S_ISDIR(statbuf.st_mode))
|
|
|
|
return "fs";
|
|
|
|
|
|
|
|
if (!S_ISREG(statbuf.st_mode))
|
|
|
|
return fallback;
|
|
|
|
|
|
|
|
f = fopen(fname, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
return fallback;
|
|
|
|
if (fread(&magic, 4, 1, f) != 1) {
|
|
|
|
fclose(f);
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2017-03-21 22:01:08 +08:00
|
|
|
if (fdt32_to_cpu(magic) == FDT_MAGIC)
|
2016-01-26 23:04:11 +08:00
|
|
|
return "dtb";
|
|
|
|
|
|
|
|
return guess_type_by_name(fname, fallback);
|
|
|
|
}
|
|
|
|
|
2007-12-18 12:06:42 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-01-05 00:45:20 +08:00
|
|
|
struct dt_info *dti;
|
2016-01-26 23:04:11 +08:00
|
|
|
const char *inform = NULL;
|
|
|
|
const char *outform = NULL;
|
2007-12-18 12:06:42 +08:00
|
|
|
const char *outname = "-";
|
2012-01-11 08:27:52 +08:00
|
|
|
const char *depname = NULL;
|
2015-04-30 05:00:05 +08:00
|
|
|
bool force = false, sort = false;
|
2007-12-18 12:06:42 +08:00
|
|
|
const char *arg;
|
|
|
|
int opt;
|
|
|
|
FILE *outf = NULL;
|
|
|
|
int outversion = DEFAULT_FDT_VERSION;
|
2008-08-07 10:24:17 +08:00
|
|
|
long long cmdline_boot_cpuid = -1;
|
2007-12-18 12:06:42 +08:00
|
|
|
|
|
|
|
quiet = 0;
|
|
|
|
reservenum = 0;
|
|
|
|
minsize = 0;
|
|
|
|
padsize = 0;
|
2017-01-05 00:45:20 +08:00
|
|
|
alignsize = 0;
|
2007-12-18 12:06:42 +08:00
|
|
|
|
2014-01-21 20:54:49 +08:00
|
|
|
while ((opt = util_getopt_long()) != EOF) {
|
2007-12-18 12:06:42 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'I':
|
|
|
|
inform = optarg;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
outform = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outname = optarg;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
outversion = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
2012-01-11 08:27:52 +08:00
|
|
|
case 'd':
|
|
|
|
depname = optarg;
|
|
|
|
break;
|
2007-12-18 12:06:42 +08:00
|
|
|
case 'R':
|
2021-10-26 00:05:45 +08:00
|
|
|
reservenum = strtoul(optarg, NULL, 0);
|
2007-12-18 12:06:42 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
minsize = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
padsize = strtol(optarg, NULL, 0);
|
|
|
|
break;
|
2017-01-05 00:45:20 +08:00
|
|
|
case 'a':
|
|
|
|
alignsize = strtol(optarg, NULL, 0);
|
|
|
|
if (!is_power_of_2(alignsize))
|
|
|
|
die("Invalid argument \"%d\" to -a option\n",
|
2017-03-21 22:01:08 +08:00
|
|
|
alignsize);
|
2017-01-05 00:45:20 +08:00
|
|
|
break;
|
2007-12-18 12:06:42 +08:00
|
|
|
case 'f':
|
2015-04-30 05:00:05 +08:00
|
|
|
force = true;
|
2007-12-18 12:06:42 +08:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
2008-08-07 10:24:17 +08:00
|
|
|
cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
|
2007-12-18 12:06:42 +08:00
|
|
|
break;
|
2012-09-29 05:25:59 +08:00
|
|
|
case 'i':
|
|
|
|
srcfile_add_search_path(optarg);
|
|
|
|
break;
|
2007-12-18 12:06:42 +08:00
|
|
|
case 'v':
|
2014-01-21 20:54:49 +08:00
|
|
|
util_version();
|
2010-11-18 07:28:20 +08:00
|
|
|
case 'H':
|
|
|
|
if (streq(optarg, "legacy"))
|
|
|
|
phandle_format = PHANDLE_LEGACY;
|
|
|
|
else if (streq(optarg, "epapr"))
|
|
|
|
phandle_format = PHANDLE_EPAPR;
|
|
|
|
else if (streq(optarg, "both"))
|
|
|
|
phandle_format = PHANDLE_BOTH;
|
|
|
|
else
|
|
|
|
die("Invalid argument \"%s\" to -H option\n",
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2015-04-30 05:00:05 +08:00
|
|
|
sort = true;
|
2010-11-18 07:28:20 +08:00
|
|
|
break;
|
|
|
|
|
2012-09-29 05:25:59 +08:00
|
|
|
case 'W':
|
|
|
|
parse_checks_option(true, false, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
parse_checks_option(false, true, optarg);
|
|
|
|
break;
|
|
|
|
|
2017-01-05 00:45:20 +08:00
|
|
|
case '@':
|
|
|
|
generate_symbols = 1;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
auto_label_aliases = 1;
|
|
|
|
break;
|
2018-11-29 08:37:35 +08:00
|
|
|
case 'T':
|
|
|
|
annotate++;
|
|
|
|
break;
|
2017-01-05 00:45:20 +08:00
|
|
|
|
2007-12-18 12:06:42 +08:00
|
|
|
case 'h':
|
2014-01-21 20:54:49 +08:00
|
|
|
usage(NULL);
|
2007-12-18 12:06:42 +08:00
|
|
|
default:
|
2014-01-21 20:54:49 +08:00
|
|
|
usage("unknown option");
|
2007-12-18 12:06:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > (optind+1))
|
2014-01-21 20:54:49 +08:00
|
|
|
usage("missing files");
|
2007-12-18 12:06:42 +08:00
|
|
|
else if (argc < (optind+1))
|
|
|
|
arg = "-";
|
|
|
|
else
|
|
|
|
arg = argv[optind];
|
|
|
|
|
|
|
|
/* minsize and padsize are mutually exclusive */
|
2008-08-07 10:24:17 +08:00
|
|
|
if (minsize && padsize)
|
2007-12-18 12:06:42 +08:00
|
|
|
die("Can't set both -p and -S\n");
|
|
|
|
|
2012-01-11 08:27:52 +08:00
|
|
|
if (depname) {
|
|
|
|
depfile = fopen(depname, "w");
|
|
|
|
if (!depfile)
|
|
|
|
die("Couldn't open dependency file %s: %s\n", depname,
|
|
|
|
strerror(errno));
|
|
|
|
fprintf(depfile, "%s:", outname);
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:04:11 +08:00
|
|
|
if (inform == NULL)
|
|
|
|
inform = guess_input_format(arg, "dts");
|
|
|
|
if (outform == NULL) {
|
|
|
|
outform = guess_type_by_name(outname, NULL);
|
|
|
|
if (outform == NULL) {
|
|
|
|
if (streq(inform, "dts"))
|
|
|
|
outform = "dtb";
|
|
|
|
else
|
|
|
|
outform = "dts";
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 08:37:35 +08:00
|
|
|
if (annotate && (!streq(inform, "dts") || !streq(outform, "dts")))
|
|
|
|
die("--annotate requires -I dts -O dts\n");
|
2008-08-07 10:24:17 +08:00
|
|
|
if (streq(inform, "dts"))
|
2017-01-05 00:45:20 +08:00
|
|
|
dti = dt_from_source(arg);
|
2008-08-07 10:24:17 +08:00
|
|
|
else if (streq(inform, "fs"))
|
2017-01-05 00:45:20 +08:00
|
|
|
dti = dt_from_fs(arg);
|
2008-08-07 10:24:17 +08:00
|
|
|
else if(streq(inform, "dtb"))
|
2017-01-05 00:45:20 +08:00
|
|
|
dti = dt_from_blob(arg);
|
2008-08-07 10:24:17 +08:00
|
|
|
else
|
2007-12-18 12:06:42 +08:00
|
|
|
die("Unknown input format \"%s\"\n", inform);
|
|
|
|
|
2017-03-21 22:01:08 +08:00
|
|
|
dti->outname = outname;
|
|
|
|
|
2012-01-11 08:27:52 +08:00
|
|
|
if (depfile) {
|
|
|
|
fputc('\n', depfile);
|
|
|
|
fclose(depfile);
|
|
|
|
}
|
|
|
|
|
2008-08-07 10:24:17 +08:00
|
|
|
if (cmdline_boot_cpuid != -1)
|
2017-01-05 00:45:20 +08:00
|
|
|
dti->boot_cpuid_phys = cmdline_boot_cpuid;
|
|
|
|
|
|
|
|
fill_fullpaths(dti->dt, "");
|
|
|
|
|
|
|
|
/* on a plugin, generate by default */
|
|
|
|
if (dti->dtsflags & DTSF_PLUGIN) {
|
|
|
|
generate_fixups = 1;
|
|
|
|
}
|
2007-12-18 12:06:42 +08:00
|
|
|
|
2018-02-28 07:40:38 +08:00
|
|
|
process_checks(force, dti);
|
|
|
|
|
2017-01-05 00:45:20 +08:00
|
|
|
if (auto_label_aliases)
|
|
|
|
generate_label_tree(dti, "aliases", false);
|
|
|
|
|
|
|
|
if (generate_symbols)
|
|
|
|
generate_label_tree(dti, "__symbols__", true);
|
|
|
|
|
|
|
|
if (generate_fixups) {
|
|
|
|
generate_fixups_tree(dti, "__fixups__");
|
|
|
|
generate_local_fixups_tree(dti, "__local_fixups__");
|
|
|
|
}
|
2007-12-18 12:06:42 +08:00
|
|
|
|
2010-11-18 07:28:20 +08:00
|
|
|
if (sort)
|
2017-01-05 00:45:20 +08:00
|
|
|
sort_tree(dti);
|
2007-12-18 12:06:42 +08:00
|
|
|
|
|
|
|
if (streq(outname, "-")) {
|
|
|
|
outf = stdout;
|
|
|
|
} else {
|
2015-04-30 05:00:05 +08:00
|
|
|
outf = fopen(outname, "wb");
|
2007-12-18 12:06:42 +08:00
|
|
|
if (! outf)
|
|
|
|
die("Couldn't open output file %s: %s\n",
|
|
|
|
outname, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streq(outform, "dts")) {
|
2017-01-05 00:45:20 +08:00
|
|
|
dt_to_source(outf, dti);
|
2018-09-13 21:59:25 +08:00
|
|
|
#ifndef NO_YAML
|
|
|
|
} else if (streq(outform, "yaml")) {
|
|
|
|
if (!streq(inform, "dts"))
|
|
|
|
die("YAML output format requires dts input format\n");
|
|
|
|
dt_to_yaml(outf, dti);
|
|
|
|
#endif
|
2007-12-18 12:06:42 +08:00
|
|
|
} else if (streq(outform, "dtb")) {
|
2017-01-05 00:45:20 +08:00
|
|
|
dt_to_blob(outf, dti, outversion);
|
2007-12-18 12:06:42 +08:00
|
|
|
} else if (streq(outform, "asm")) {
|
2017-01-05 00:45:20 +08:00
|
|
|
dt_to_asm(outf, dti, outversion);
|
2007-12-18 12:06:42 +08:00
|
|
|
} else if (streq(outform, "null")) {
|
|
|
|
/* do nothing */
|
|
|
|
} else {
|
|
|
|
die("Unknown output format \"%s\"\n", outform);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|