blob: bbef8298fd70af3e8cc332229bbf128f3955c267 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * 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.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
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
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
Jon Loeligera657ce82007-07-07 13:52:25 -050024#include "version_gen.h"
25
Jerry Van Baren4384b232007-04-04 22:04:33 -040026/*
27 * Command line options
28 */
29int quiet; /* Level of quietness */
30int reservenum; /* Number of memory reservation slots */
31int minsize; /* Minimum blob size */
32
David Gibsonfc14dad2005-06-08 17:18:34 +100033char *join_path(char *path, char *name)
34{
35 int lenp = strlen(path);
36 int lenn = strlen(name);
37 int len;
38 int needslash = 1;
39 char *str;
40
41 len = lenp + lenn + 2;
42 if ((lenp > 0) && (path[lenp-1] == '/')) {
43 needslash = 0;
44 len--;
45 }
46
47 str = xmalloc(len);
48 memcpy(str, path, lenp);
49 if (needslash) {
50 str[lenp] = '/';
51 lenp++;
52 }
53 memcpy(str+lenp, name, lenn+1);
54 return str;
55}
56
57void fill_fullpaths(struct node *tree, char *prefix)
58{
59 struct node *child;
60 char *unit;
61
62 tree->fullpath = join_path(prefix, tree->name);
63
64 unit = strchr(tree->name, '@');
65 if (unit)
66 tree->basenamelen = unit - tree->name;
67 else
68 tree->basenamelen = strlen(tree->name);
69
70 for_each_child(tree, child)
71 fill_fullpaths(child, tree->fullpath);
72}
73
David Gibsonbf944972007-08-31 16:21:23 +100074static void __attribute__ ((noreturn)) usage(void)
David Gibsonfc14dad2005-06-08 17:18:34 +100075{
76 fprintf(stderr, "Usage:\n");
77 fprintf(stderr, "\tdtc [options] <input file>\n");
78 fprintf(stderr, "\nOptions:\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040079 fprintf(stderr, "\t-h\n");
80 fprintf(stderr, "\t\tThis help text\n");
81 fprintf(stderr, "\t-q\n");
82 fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100083 fprintf(stderr, "\t-I <input format>\n");
84 fprintf(stderr, "\t\tInput formats are:\n");
85 fprintf(stderr, "\t\t\tdts - device tree source text\n");
86 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
87 fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
Jerry Van Baren4f5370a2007-04-14 18:16:47 -040088 fprintf(stderr, "\t-o <output file>\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100089 fprintf(stderr, "\t-O <output format>\n");
90 fprintf(stderr, "\t\tOutput formats are:\n");
91 fprintf(stderr, "\t\t\tdts - device tree source text\n");
92 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
93 fprintf(stderr, "\t\t\tasm - assembler source\n");
94 fprintf(stderr, "\t-V <output version>\n");
David Gibsonfb7c7ac2007-09-26 13:11:05 +100095 fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION);
David Gibsonfc14dad2005-06-08 17:18:34 +100096 fprintf(stderr, "\t-R <number>\n");
97 fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
Jerry Van Baren4384b232007-04-04 22:04:33 -040098 fprintf(stderr, "\t-S <bytes>\n");
99 fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n");
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000100 fprintf(stderr, "\t-b <number>\n");
101 fprintf(stderr, "\t\tSet the physical boot cpu\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000102 fprintf(stderr, "\t-f\n");
103 fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
Jon Loeligera657ce82007-07-07 13:52:25 -0500104 fprintf(stderr, "\t-v\n");
105 fprintf(stderr, "\t\tPrint DTC version and exit\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000106 exit(2);
107}
108
109int main(int argc, char *argv[])
110{
David Gibsonf0517db2005-07-15 17:14:24 +1000111 struct boot_info *bi;
David Gibsonfc14dad2005-06-08 17:18:34 +1000112 char *inform = "dts";
113 char *outform = "dts";
114 char *outname = "-";
David Gibson169f0b12007-10-18 17:22:30 +1000115 int force = 0, check = 0;
David Gibsonfc14dad2005-06-08 17:18:34 +1000116 char *arg;
117 int opt;
118 FILE *inf = NULL;
119 FILE *outf = NULL;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000120 int outversion = DEFAULT_FDT_VERSION;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000121 int boot_cpuid_phys = 0xfeedbeef;
David Gibson169f0b12007-10-18 17:22:30 +1000122 int structure_ok;
David Gibsonfc14dad2005-06-08 17:18:34 +1000123
Jerry Van Baren4384b232007-04-04 22:04:33 -0400124 quiet = 0;
125 reservenum = 0;
126 minsize = 0;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400127
David Gibson169f0b12007-10-18 17:22:30 +1000128 while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:fcqb:v")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000129 switch (opt) {
130 case 'I':
131 inform = optarg;
132 break;
133 case 'O':
134 outform = optarg;
135 break;
136 case 'o':
137 outname = optarg;
138 break;
139 case 'V':
140 outversion = strtol(optarg, NULL, 0);
141 break;
142 case 'R':
143 reservenum = strtol(optarg, NULL, 0);
144 break;
Jerry Van Baren4384b232007-04-04 22:04:33 -0400145 case 'S':
146 minsize = strtol(optarg, NULL, 0);
147 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000148 case 'f':
149 force = 1;
150 break;
David Gibson169f0b12007-10-18 17:22:30 +1000151 case 'c':
152 check = 1;
153 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400154 case 'q':
155 quiet++;
156 break;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000157 case 'b':
158 boot_cpuid_phys = strtol(optarg, NULL, 0);
159 break;
Jon Loeligera657ce82007-07-07 13:52:25 -0500160 case 'v':
161 printf("Version: %s\n", DTC_VERSION);
162 exit(0);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400163 case 'h':
David Gibsonfc14dad2005-06-08 17:18:34 +1000164 default:
165 usage();
166 }
167 }
168
169 if (argc > (optind+1))
170 usage();
171 else if (argc < (optind+1))
172 arg = "-";
173 else
174 arg = argv[optind];
175
176 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
177 inform, outform, arg);
178
179 if (streq(inform, "dts")) {
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500180 bi = dt_from_source(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000181 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000182 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000183 } else if(streq(inform, "dtb")) {
184 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000185 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000186 } else {
187 die("Unknown input format \"%s\"\n", inform);
188 }
189
190 if (inf && (inf != stdin))
191 fclose(inf);
192
David Gibsonf0517db2005-07-15 17:14:24 +1000193 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000194 die("Couldn't read input tree\n");
195
David Gibson169f0b12007-10-18 17:22:30 +1000196 structure_ok = check_structure(bi->dt);
197 if (!structure_ok) {
198 if (!force) {
199 fprintf(stderr, "ERROR: Input tree has structural errors, aborting (use -f to force output)\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000200 exit(1);
David Gibson169f0b12007-10-18 17:22:30 +1000201 } else if (quiet < 3) {
202 fprintf(stderr, "Warning: Input tree has structural errors, output forced\n");
203 }
204 }
205
206 fixup_references(bi->dt);
207
208 if (check) {
209 if (!structure_ok) {
210 fprintf(stderr, "Warning: Skipping semantic checks due to structural errors\n");
211 } else {
212 if (!check_semantics(bi->dt, outversion,
213 boot_cpuid_phys))
214 fprintf(stderr, "Warning: Input tree has semantic errors\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -0400215 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000216 }
217
218 if (streq(outname, "-")) {
219 outf = stdout;
220 } else {
221 outf = fopen(outname, "w");
222 if (! outf)
223 die("Couldn't open output file %s: %s\n",
224 outname, strerror(errno));
225 }
226
227 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000228 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000229 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000230 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000231 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000232 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000233 } else if (streq(outform, "null")) {
234 /* do nothing */
235 } else {
236 die("Unknown output format \"%s\"\n", outform);
237 }
David Gibson63dc9c72007-09-18 11:44:04 +1000238
David Gibsonfc14dad2005-06-08 17:18:34 +1000239 exit(0);
240}