blob: ecd9fe564d02c0247c08c85723d94ec90e9e7b56 [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
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 Gibsonfc14dad2005-06-08 17:18:34 +100074static void usage(void)
75{
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");
Jerry Van Barencd1da872007-03-18 16:49:24 -040095 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 +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 = "-";
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;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000121 int boot_cpuid_phys = 0xfeedbeef;
David Gibsonfc14dad2005-06-08 17:18:34 +1000122
Jerry Van Baren4384b232007-04-04 22:04:33 -0400123 quiet = 0;
124 reservenum = 0;
125 minsize = 0;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400126
Jon Loeligera657ce82007-07-07 13:52:25 -0500127 while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:fqb:v")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000128 switch (opt) {
129 case 'I':
130 inform = optarg;
131 break;
132 case 'O':
133 outform = optarg;
134 break;
135 case 'o':
136 outname = optarg;
137 break;
138 case 'V':
139 outversion = strtol(optarg, NULL, 0);
140 break;
141 case 'R':
142 reservenum = strtol(optarg, NULL, 0);
143 break;
Jerry Van Baren4384b232007-04-04 22:04:33 -0400144 case 'S':
145 minsize = strtol(optarg, NULL, 0);
146 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000147 case 'f':
148 force = 1;
149 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400150 case 'q':
151 quiet++;
152 break;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000153 case 'b':
154 boot_cpuid_phys = strtol(optarg, NULL, 0);
155 break;
Jon Loeligera657ce82007-07-07 13:52:25 -0500156 case 'v':
157 printf("Version: %s\n", DTC_VERSION);
158 exit(0);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400159 case 'h':
David Gibsonfc14dad2005-06-08 17:18:34 +1000160 default:
161 usage();
162 }
163 }
164
165 if (argc > (optind+1))
166 usage();
167 else if (argc < (optind+1))
168 arg = "-";
169 else
170 arg = argv[optind];
171
172 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
173 inform, outform, arg);
174
175 if (streq(inform, "dts")) {
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500176 bi = dt_from_source(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000177 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000178 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000179 } else if(streq(inform, "dtb")) {
180 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000181 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000182 } else {
183 die("Unknown input format \"%s\"\n", inform);
184 }
185
186 if (inf && (inf != stdin))
187 fclose(inf);
188
David Gibsonf0517db2005-07-15 17:14:24 +1000189 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000190 die("Couldn't read input tree\n");
191
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000192 if (! check_device_tree(bi->dt, outversion, boot_cpuid_phys)) {
Jerry Van Barencd1da872007-03-18 16:49:24 -0400193 if ((force) && (quiet < 3))
194 fprintf(stderr, "Input tree has errors, output forced\n");
195 if (! force) {
196 fprintf(stderr, "Input tree has errors, not writing output (use -f to force output)\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000197 exit(1);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400198 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000199 }
200
201 if (streq(outname, "-")) {
202 outf = stdout;
203 } else {
204 outf = fopen(outname, "w");
205 if (! outf)
206 die("Couldn't open output file %s: %s\n",
207 outname, strerror(errno));
208 }
209
210 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000211 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000212 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000213 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000214 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000215 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000216 } else if (streq(outform, "null")) {
217 /* do nothing */
218 } else {
219 die("Unknown output format \"%s\"\n", outform);
220 }
221
222 exit(0);
223}