blob: f527e08569d9f775d5ce0d0124253062a36a06e7 [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");
84 fprintf(stderr, "\t-I <input format>\n");
85 fprintf(stderr, "\t\tInput formats are:\n");
86 fprintf(stderr, "\t\t\tdts - device tree source text\n");
87 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
88 fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
89 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");
95 fprintf(stderr, "\t\tBlob version to produce, defaults to 3 (relevant for dtb\n\t\tand asm output only)\n");
96 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");
98 fprintf(stderr, "\t-f\n");
99 fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
100 exit(2);
101}
102
103int main(int argc, char *argv[])
104{
105 struct node *dt;
106 char *inform = "dts";
107 char *outform = "dts";
108 char *outname = "-";
109 int force = 0;
110 char *arg;
111 int opt;
112 FILE *inf = NULL;
113 FILE *outf = NULL;
114 int outversion = 3;
115 int reservenum = 1;
116
117 while ((opt = getopt(argc, argv, "I:O:o:V:R:f")) != EOF) {
118 switch (opt) {
119 case 'I':
120 inform = optarg;
121 break;
122 case 'O':
123 outform = optarg;
124 break;
125 case 'o':
126 outname = optarg;
127 break;
128 case 'V':
129 outversion = strtol(optarg, NULL, 0);
130 break;
131 case 'R':
132 reservenum = strtol(optarg, NULL, 0);
133 break;
134 case 'f':
135 force = 1;
136 break;
137 default:
138 usage();
139 }
140 }
141
142 if (argc > (optind+1))
143 usage();
144 else if (argc < (optind+1))
145 arg = "-";
146 else
147 arg = argv[optind];
148
149 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
150 inform, outform, arg);
151
152 if (streq(inform, "dts")) {
153 inf = dtc_open_file(arg);
154 dt = dt_from_source(inf);
155 } else if (streq(inform, "fs")) {
156 dt = dt_from_fs(arg);
157 } else if(streq(inform, "dtb")) {
158 inf = dtc_open_file(arg);
159 dt = dt_from_blob(inf);
160 } else {
161 die("Unknown input format \"%s\"\n", inform);
162 }
163
164 if (inf && (inf != stdin))
165 fclose(inf);
166
167 if (! dt)
168 die("Couldn't read input tree\n");
169
170 if (! check_device_tree(dt)) {
171 fprintf(stderr, "Input tree has errors\n");
172 if (! force)
173 exit(1);
174 }
175
176 if (streq(outname, "-")) {
177 outf = stdout;
178 } else {
179 outf = fopen(outname, "w");
180 if (! outf)
181 die("Couldn't open output file %s: %s\n",
182 outname, strerror(errno));
183 }
184
185 if (streq(outform, "dts")) {
186 write_tree_source(outf, dt, 0);
187 } else if (streq(outform, "dtb")) {
188 write_dt_blob(outf, dt, outversion, reservenum);
189 } else if (streq(outform, "asm")) {
190 write_dt_asm(outf, dt, outversion, reservenum);
191 } else if (streq(outform, "null")) {
192 /* do nothing */
193 } else {
194 die("Unknown output format \"%s\"\n", outform);
195 }
196
197 exit(0);
198}