blob: 646b814e9cee472786dfb02cd6677a8c44c71770 [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");
Michael Neuling38e8f8f2006-05-31 08:31:51 +100098 fprintf(stderr, "\t-b <number>\n");
99 fprintf(stderr, "\t\tSet the physical boot cpu\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000100 fprintf(stderr, "\t-f\n");
101 fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
102 exit(2);
103}
104
105int main(int argc, char *argv[])
106{
David Gibsonf0517db2005-07-15 17:14:24 +1000107 struct boot_info *bi;
David Gibsonfc14dad2005-06-08 17:18:34 +1000108 char *inform = "dts";
109 char *outform = "dts";
110 char *outname = "-";
111 int force = 0;
112 char *arg;
113 int opt;
114 FILE *inf = NULL;
115 FILE *outf = NULL;
116 int outversion = 3;
117 int reservenum = 1;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000118 int boot_cpuid_phys = 0xfeedbeef;
David Gibsonfc14dad2005-06-08 17:18:34 +1000119
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000120 while ((opt = getopt(argc, argv, "I:O:o:V:R:fb:")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000121 switch (opt) {
122 case 'I':
123 inform = optarg;
124 break;
125 case 'O':
126 outform = optarg;
127 break;
128 case 'o':
129 outname = optarg;
130 break;
131 case 'V':
132 outversion = strtol(optarg, NULL, 0);
133 break;
134 case 'R':
135 reservenum = strtol(optarg, NULL, 0);
136 break;
137 case 'f':
138 force = 1;
139 break;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000140 case 'b':
141 boot_cpuid_phys = strtol(optarg, NULL, 0);
142 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000143 default:
144 usage();
145 }
146 }
147
148 if (argc > (optind+1))
149 usage();
150 else if (argc < (optind+1))
151 arg = "-";
152 else
153 arg = argv[optind];
154
155 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
156 inform, outform, arg);
157
158 if (streq(inform, "dts")) {
159 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000160 bi = dt_from_source(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000161 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000162 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000163 } else if(streq(inform, "dtb")) {
164 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000165 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000166 } else {
167 die("Unknown input format \"%s\"\n", inform);
168 }
169
170 if (inf && (inf != stdin))
171 fclose(inf);
172
David Gibsonf0517db2005-07-15 17:14:24 +1000173 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000174 die("Couldn't read input tree\n");
175
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000176 if (! check_device_tree(bi->dt, outversion, boot_cpuid_phys)) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000177 fprintf(stderr, "Input tree has errors\n");
178 if (! force)
179 exit(1);
180 }
181
182 if (streq(outname, "-")) {
183 outf = stdout;
184 } else {
185 outf = fopen(outname, "w");
186 if (! outf)
187 die("Couldn't open output file %s: %s\n",
188 outname, strerror(errno));
189 }
190
191 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000192 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000193 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000194 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000195 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000196 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000197 } else if (streq(outform, "null")) {
198 /* do nothing */
199 } else {
200 die("Unknown output format \"%s\"\n", outform);
201 }
202
203 exit(0);
204}