blob: a009605f50e44deaaa180d64ffe7bf74c7a685be [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include "dtc.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
24char *join_path(char *path, char *name)
25{
26 int lenp = strlen(path);
27 int lenn = strlen(name);
28 int len;
29 int needslash = 1;
30 char *str;
31
32 len = lenp + lenn + 2;
33 if ((lenp > 0) && (path[lenp-1] == '/')) {
34 needslash = 0;
35 len--;
36 }
37
38 str = xmalloc(len);
39 memcpy(str, path, lenp);
40 if (needslash) {
41 str[lenp] = '/';
42 lenp++;
43 }
44 memcpy(str+lenp, name, lenn+1);
45 return str;
46}
47
48void fill_fullpaths(struct node *tree, char *prefix)
49{
50 struct node *child;
51 char *unit;
52
53 tree->fullpath = join_path(prefix, tree->name);
54
55 unit = strchr(tree->name, '@');
56 if (unit)
57 tree->basenamelen = unit - tree->name;
58 else
59 tree->basenamelen = strlen(tree->name);
60
61 for_each_child(tree, child)
62 fill_fullpaths(child, tree->fullpath);
63}
64
David Gibsonfc14dad2005-06-08 17:18:34 +100065static void usage(void)
66{
67 fprintf(stderr, "Usage:\n");
68 fprintf(stderr, "\tdtc [options] <input file>\n");
69 fprintf(stderr, "\nOptions:\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040070 fprintf(stderr, "\t-h\n");
71 fprintf(stderr, "\t\tThis help text\n");
72 fprintf(stderr, "\t-q\n");
73 fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100074 fprintf(stderr, "\t-I <input format>\n");
75 fprintf(stderr, "\t\tInput formats are:\n");
76 fprintf(stderr, "\t\t\tdts - device tree source text\n");
77 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
78 fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
79 fprintf(stderr, "\t-O <output format>\n");
80 fprintf(stderr, "\t\tOutput formats are:\n");
81 fprintf(stderr, "\t\t\tdts - device tree source text\n");
82 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
83 fprintf(stderr, "\t\t\tasm - assembler source\n");
84 fprintf(stderr, "\t-V <output version>\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040085 fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", OF_DEFAULT_VERSION);
David Gibsonfc14dad2005-06-08 17:18:34 +100086 fprintf(stderr, "\t-R <number>\n");
87 fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
Michael Neuling38e8f8f2006-05-31 08:31:51 +100088 fprintf(stderr, "\t-b <number>\n");
89 fprintf(stderr, "\t\tSet the physical boot cpu\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100090 fprintf(stderr, "\t-f\n");
91 fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
92 exit(2);
93}
94
95int main(int argc, char *argv[])
96{
David Gibsonf0517db2005-07-15 17:14:24 +100097 struct boot_info *bi;
David Gibsonfc14dad2005-06-08 17:18:34 +100098 char *inform = "dts";
99 char *outform = "dts";
100 char *outname = "-";
101 int force = 0;
102 char *arg;
103 int opt;
104 FILE *inf = NULL;
105 FILE *outf = NULL;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400106 int outversion = OF_DEFAULT_VERSION;
David Gibsonfc14dad2005-06-08 17:18:34 +1000107 int reservenum = 1;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000108 int boot_cpuid_phys = 0xfeedbeef;
David Gibsonfc14dad2005-06-08 17:18:34 +1000109
Jerry Van Barencd1da872007-03-18 16:49:24 -0400110 quiet = 0;
111
112 while ((opt = getopt(argc, argv, "hI:O:o:V:R:fqb:")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000113 switch (opt) {
114 case 'I':
115 inform = optarg;
116 break;
117 case 'O':
118 outform = optarg;
119 break;
120 case 'o':
121 outname = optarg;
122 break;
123 case 'V':
124 outversion = strtol(optarg, NULL, 0);
125 break;
126 case 'R':
127 reservenum = strtol(optarg, NULL, 0);
128 break;
129 case 'f':
130 force = 1;
131 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400132 case 'q':
133 quiet++;
134 break;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000135 case 'b':
136 boot_cpuid_phys = strtol(optarg, NULL, 0);
137 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400138 case 'h':
David Gibsonfc14dad2005-06-08 17:18:34 +1000139 default:
140 usage();
141 }
142 }
143
144 if (argc > (optind+1))
145 usage();
146 else if (argc < (optind+1))
147 arg = "-";
148 else
149 arg = argv[optind];
150
151 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
152 inform, outform, arg);
153
154 if (streq(inform, "dts")) {
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500155 bi = dt_from_source(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000156 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000157 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000158 } else if(streq(inform, "dtb")) {
159 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000160 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000161 } else {
162 die("Unknown input format \"%s\"\n", inform);
163 }
164
165 if (inf && (inf != stdin))
166 fclose(inf);
167
David Gibsonf0517db2005-07-15 17:14:24 +1000168 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000169 die("Couldn't read input tree\n");
170
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000171 if (! check_device_tree(bi->dt, outversion, boot_cpuid_phys)) {
Jerry Van Barencd1da872007-03-18 16:49:24 -0400172 if ((force) && (quiet < 3))
173 fprintf(stderr, "Input tree has errors, output forced\n");
174 if (! force) {
175 fprintf(stderr, "Input tree has errors, not writing output (use -f to force output)\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000176 exit(1);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400177 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000178 }
179
180 if (streq(outname, "-")) {
181 outf = stdout;
182 } else {
183 outf = fopen(outname, "w");
184 if (! outf)
185 die("Couldn't open output file %s: %s\n",
186 outname, strerror(errno));
187 }
188
189 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000190 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000191 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000192 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000193 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000194 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000195 } else if (streq(outform, "null")) {
196 /* do nothing */
197 } else {
198 die("Unknown output format \"%s\"\n", outform);
199 }
200
201 exit(0);
202}