blob: 051a68b8f26f7a7b49ea3ad8eed809bd99fe0721 [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"
22
23char *join_path(char *path, char *name)
24{
25 int lenp = strlen(path);
26 int lenn = strlen(name);
27 int len;
28 int needslash = 1;
29 char *str;
30
31 len = lenp + lenn + 2;
32 if ((lenp > 0) && (path[lenp-1] == '/')) {
33 needslash = 0;
34 len--;
35 }
36
37 str = xmalloc(len);
38 memcpy(str, path, lenp);
39 if (needslash) {
40 str[lenp] = '/';
41 lenp++;
42 }
43 memcpy(str+lenp, name, lenn+1);
44 return str;
45}
46
47void fill_fullpaths(struct node *tree, char *prefix)
48{
49 struct node *child;
50 char *unit;
51
52 tree->fullpath = join_path(prefix, tree->name);
53
54 unit = strchr(tree->name, '@');
55 if (unit)
56 tree->basenamelen = unit - tree->name;
57 else
58 tree->basenamelen = strlen(tree->name);
59
60 for_each_child(tree, child)
61 fill_fullpaths(child, tree->fullpath);
62}
63
64static FILE *dtc_open_file(char *fname)
65{
66 FILE *f;
67
68 if (streq(fname, "-"))
69 f = stdin;
70 else
71 f = fopen(fname, "r");
72
73 if (! f)
74 die("Couldn't open \"%s\": %s\n", fname, strerror(errno));
75
76 return f;
77}
78
79static void usage(void)
80{
81 fprintf(stderr, "Usage:\n");
82 fprintf(stderr, "\tdtc [options] <input file>\n");
83 fprintf(stderr, "\nOptions:\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040084 fprintf(stderr, "\t-h\n");
85 fprintf(stderr, "\t\tThis help text\n");
86 fprintf(stderr, "\t-q\n");
87 fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100088 fprintf(stderr, "\t-I <input format>\n");
89 fprintf(stderr, "\t\tInput formats are:\n");
90 fprintf(stderr, "\t\t\tdts - device tree source text\n");
91 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
92 fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
93 fprintf(stderr, "\t-O <output format>\n");
94 fprintf(stderr, "\t\tOutput formats are:\n");
95 fprintf(stderr, "\t\t\tdts - device tree source text\n");
96 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
97 fprintf(stderr, "\t\t\tasm - assembler source\n");
98 fprintf(stderr, "\t-V <output version>\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040099 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 +1000100 fprintf(stderr, "\t-R <number>\n");
101 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 +1000102 fprintf(stderr, "\t-b <number>\n");
103 fprintf(stderr, "\t\tSet the physical boot cpu\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000104 fprintf(stderr, "\t-f\n");
105 fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
106 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 = "-";
115 int force = 0;
116 char *arg;
117 int opt;
118 FILE *inf = NULL;
119 FILE *outf = NULL;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400120 int outversion = OF_DEFAULT_VERSION;
David Gibsonfc14dad2005-06-08 17:18:34 +1000121 int reservenum = 1;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000122 int boot_cpuid_phys = 0xfeedbeef;
David Gibsonfc14dad2005-06-08 17:18:34 +1000123
Jerry Van Barencd1da872007-03-18 16:49:24 -0400124 quiet = 0;
125
126 while ((opt = getopt(argc, argv, "hI:O:o:V:R:fqb:")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000127 switch (opt) {
128 case 'I':
129 inform = optarg;
130 break;
131 case 'O':
132 outform = optarg;
133 break;
134 case 'o':
135 outname = optarg;
136 break;
137 case 'V':
138 outversion = strtol(optarg, NULL, 0);
139 break;
140 case 'R':
141 reservenum = strtol(optarg, NULL, 0);
142 break;
143 case 'f':
144 force = 1;
145 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400146 case 'q':
147 quiet++;
148 break;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000149 case 'b':
150 boot_cpuid_phys = strtol(optarg, NULL, 0);
151 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400152 case 'h':
David Gibsonfc14dad2005-06-08 17:18:34 +1000153 default:
154 usage();
155 }
156 }
157
158 if (argc > (optind+1))
159 usage();
160 else if (argc < (optind+1))
161 arg = "-";
162 else
163 arg = argv[optind];
164
165 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
166 inform, outform, arg);
167
168 if (streq(inform, "dts")) {
169 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000170 bi = dt_from_source(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000171 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000172 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000173 } else if(streq(inform, "dtb")) {
174 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000175 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000176 } else {
177 die("Unknown input format \"%s\"\n", inform);
178 }
179
180 if (inf && (inf != stdin))
181 fclose(inf);
182
David Gibsonf0517db2005-07-15 17:14:24 +1000183 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000184 die("Couldn't read input tree\n");
185
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000186 if (! check_device_tree(bi->dt, outversion, boot_cpuid_phys)) {
Jerry Van Barencd1da872007-03-18 16:49:24 -0400187 if ((force) && (quiet < 3))
188 fprintf(stderr, "Input tree has errors, output forced\n");
189 if (! force) {
190 fprintf(stderr, "Input tree has errors, not writing output (use -f to force output)\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000191 exit(1);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400192 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000193 }
194
195 if (streq(outname, "-")) {
196 outf = stdout;
197 } else {
198 outf = fopen(outname, "w");
199 if (! outf)
200 die("Couldn't open output file %s: %s\n",
201 outname, strerror(errno));
202 }
203
204 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000205 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000206 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000207 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000208 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000209 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000210 } else if (streq(outform, "null")) {
211 /* do nothing */
212 } else {
213 die("Unknown output format \"%s\"\n", outform);
214 }
215
216 exit(0);
217}