blob: 57678340e756b0161c1f0e517748e1dffe0b65d5 [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
Jerry Van Baren4384b232007-04-04 22:04:33 -040024/*
25 * Command line options
26 */
27int quiet; /* Level of quietness */
28int reservenum; /* Number of memory reservation slots */
29int minsize; /* Minimum blob size */
30
David Gibsonfc14dad2005-06-08 17:18:34 +100031char *join_path(char *path, char *name)
32{
33 int lenp = strlen(path);
34 int lenn = strlen(name);
35 int len;
36 int needslash = 1;
37 char *str;
38
39 len = lenp + lenn + 2;
40 if ((lenp > 0) && (path[lenp-1] == '/')) {
41 needslash = 0;
42 len--;
43 }
44
45 str = xmalloc(len);
46 memcpy(str, path, lenp);
47 if (needslash) {
48 str[lenp] = '/';
49 lenp++;
50 }
51 memcpy(str+lenp, name, lenn+1);
52 return str;
53}
54
55void fill_fullpaths(struct node *tree, char *prefix)
56{
57 struct node *child;
58 char *unit;
59
60 tree->fullpath = join_path(prefix, tree->name);
61
62 unit = strchr(tree->name, '@');
63 if (unit)
64 tree->basenamelen = unit - tree->name;
65 else
66 tree->basenamelen = strlen(tree->name);
67
68 for_each_child(tree, child)
69 fill_fullpaths(child, tree->fullpath);
70}
71
David Gibsonfc14dad2005-06-08 17:18:34 +100072static void usage(void)
73{
74 fprintf(stderr, "Usage:\n");
75 fprintf(stderr, "\tdtc [options] <input file>\n");
76 fprintf(stderr, "\nOptions:\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040077 fprintf(stderr, "\t-h\n");
78 fprintf(stderr, "\t\tThis help text\n");
79 fprintf(stderr, "\t-q\n");
80 fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100081 fprintf(stderr, "\t-I <input format>\n");
82 fprintf(stderr, "\t\tInput formats are:\n");
83 fprintf(stderr, "\t\t\tdts - device tree source text\n");
84 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
85 fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
Jerry Van Baren4f5370a2007-04-14 18:16:47 -040086 fprintf(stderr, "\t-o <output file>\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100087 fprintf(stderr, "\t-O <output format>\n");
88 fprintf(stderr, "\t\tOutput formats are:\n");
89 fprintf(stderr, "\t\t\tdts - device tree source text\n");
90 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
91 fprintf(stderr, "\t\t\tasm - assembler source\n");
92 fprintf(stderr, "\t-V <output version>\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040093 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 +100094 fprintf(stderr, "\t-R <number>\n");
95 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 -040096 fprintf(stderr, "\t-S <bytes>\n");
97 fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\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;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400116 int outversion = OF_DEFAULT_VERSION;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000117 int boot_cpuid_phys = 0xfeedbeef;
David Gibsonfc14dad2005-06-08 17:18:34 +1000118
Jerry Van Baren4384b232007-04-04 22:04:33 -0400119 quiet = 0;
120 reservenum = 0;
121 minsize = 0;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400122
Jerry Van Baren4384b232007-04-04 22:04:33 -0400123 while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:fqb:")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000124 switch (opt) {
125 case 'I':
126 inform = optarg;
127 break;
128 case 'O':
129 outform = optarg;
130 break;
131 case 'o':
132 outname = optarg;
133 break;
134 case 'V':
135 outversion = strtol(optarg, NULL, 0);
136 break;
137 case 'R':
138 reservenum = strtol(optarg, NULL, 0);
139 break;
Jerry Van Baren4384b232007-04-04 22:04:33 -0400140 case 'S':
141 minsize = strtol(optarg, NULL, 0);
142 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000143 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")) {
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500169 bi = dt_from_source(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000170 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000171 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000172 } else if(streq(inform, "dtb")) {
173 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000174 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000175 } else {
176 die("Unknown input format \"%s\"\n", inform);
177 }
178
179 if (inf && (inf != stdin))
180 fclose(inf);
181
David Gibsonf0517db2005-07-15 17:14:24 +1000182 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000183 die("Couldn't read input tree\n");
184
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000185 if (! check_device_tree(bi->dt, outversion, boot_cpuid_phys)) {
Jerry Van Barencd1da872007-03-18 16:49:24 -0400186 if ((force) && (quiet < 3))
187 fprintf(stderr, "Input tree has errors, output forced\n");
188 if (! force) {
189 fprintf(stderr, "Input tree has errors, not writing output (use -f to force output)\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000190 exit(1);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400191 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000192 }
193
194 if (streq(outname, "-")) {
195 outf = stdout;
196 } else {
197 outf = fopen(outname, "w");
198 if (! outf)
199 die("Couldn't open output file %s: %s\n",
200 outname, strerror(errno));
201 }
202
203 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000204 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000205 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000206 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000207 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000208 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000209 } else if (streq(outform, "null")) {
210 /* do nothing */
211 } else {
212 die("Unknown output format \"%s\"\n", outform);
213 }
214
215 exit(0);
216}