blob: a94a402fddff73f79d48d5625ef964835f6ce949 [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");
86 fprintf(stderr, "\t-O <output format>\n");
87 fprintf(stderr, "\t\tOutput formats are:\n");
88 fprintf(stderr, "\t\t\tdts - device tree source text\n");
89 fprintf(stderr, "\t\t\tdtb - device tree blob\n");
90 fprintf(stderr, "\t\t\tasm - assembler source\n");
91 fprintf(stderr, "\t-V <output version>\n");
Jerry Van Barencd1da872007-03-18 16:49:24 -040092 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 +100093 fprintf(stderr, "\t-R <number>\n");
94 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 -040095 fprintf(stderr, "\t-S <bytes>\n");
96 fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n");
Michael Neuling38e8f8f2006-05-31 08:31:51 +100097 fprintf(stderr, "\t-b <number>\n");
98 fprintf(stderr, "\t\tSet the physical boot cpu\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100099 fprintf(stderr, "\t-f\n");
100 fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
101 exit(2);
102}
103
104int main(int argc, char *argv[])
105{
David Gibsonf0517db2005-07-15 17:14:24 +1000106 struct boot_info *bi;
David Gibsonfc14dad2005-06-08 17:18:34 +1000107 char *inform = "dts";
108 char *outform = "dts";
109 char *outname = "-";
110 int force = 0;
111 char *arg;
112 int opt;
113 FILE *inf = NULL;
114 FILE *outf = NULL;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400115 int outversion = OF_DEFAULT_VERSION;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000116 int boot_cpuid_phys = 0xfeedbeef;
David Gibsonfc14dad2005-06-08 17:18:34 +1000117
Jerry Van Baren4384b232007-04-04 22:04:33 -0400118 quiet = 0;
119 reservenum = 0;
120 minsize = 0;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400121
Jerry Van Baren4384b232007-04-04 22:04:33 -0400122 while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:fqb:")) != EOF) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000123 switch (opt) {
124 case 'I':
125 inform = optarg;
126 break;
127 case 'O':
128 outform = optarg;
129 break;
130 case 'o':
131 outname = optarg;
132 break;
133 case 'V':
134 outversion = strtol(optarg, NULL, 0);
135 break;
136 case 'R':
137 reservenum = strtol(optarg, NULL, 0);
138 break;
Jerry Van Baren4384b232007-04-04 22:04:33 -0400139 case 'S':
140 minsize = strtol(optarg, NULL, 0);
141 break;
David Gibsonfc14dad2005-06-08 17:18:34 +1000142 case 'f':
143 force = 1;
144 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400145 case 'q':
146 quiet++;
147 break;
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000148 case 'b':
149 boot_cpuid_phys = strtol(optarg, NULL, 0);
150 break;
Jerry Van Barencd1da872007-03-18 16:49:24 -0400151 case 'h':
David Gibsonfc14dad2005-06-08 17:18:34 +1000152 default:
153 usage();
154 }
155 }
156
157 if (argc > (optind+1))
158 usage();
159 else if (argc < (optind+1))
160 arg = "-";
161 else
162 arg = argv[optind];
163
164 fprintf(stderr, "DTC: %s->%s on file \"%s\"\n",
165 inform, outform, arg);
166
167 if (streq(inform, "dts")) {
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500168 bi = dt_from_source(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000169 } else if (streq(inform, "fs")) {
David Gibsonf0517db2005-07-15 17:14:24 +1000170 bi = dt_from_fs(arg);
David Gibsonfc14dad2005-06-08 17:18:34 +1000171 } else if(streq(inform, "dtb")) {
172 inf = dtc_open_file(arg);
David Gibsonf0517db2005-07-15 17:14:24 +1000173 bi = dt_from_blob(inf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000174 } else {
175 die("Unknown input format \"%s\"\n", inform);
176 }
177
178 if (inf && (inf != stdin))
179 fclose(inf);
180
David Gibsonf0517db2005-07-15 17:14:24 +1000181 if (! bi || ! bi->dt)
David Gibsonfc14dad2005-06-08 17:18:34 +1000182 die("Couldn't read input tree\n");
183
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000184 if (! check_device_tree(bi->dt, outversion, boot_cpuid_phys)) {
Jerry Van Barencd1da872007-03-18 16:49:24 -0400185 if ((force) && (quiet < 3))
186 fprintf(stderr, "Input tree has errors, output forced\n");
187 if (! force) {
188 fprintf(stderr, "Input tree has errors, not writing output (use -f to force output)\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000189 exit(1);
Jerry Van Barencd1da872007-03-18 16:49:24 -0400190 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000191 }
192
193 if (streq(outname, "-")) {
194 outf = stdout;
195 } else {
196 outf = fopen(outname, "w");
197 if (! outf)
198 die("Couldn't open output file %s: %s\n",
199 outname, strerror(errno));
200 }
201
202 if (streq(outform, "dts")) {
David Gibson712e52e2005-10-26 16:56:26 +1000203 dt_to_source(outf, bi);
David Gibsonfc14dad2005-06-08 17:18:34 +1000204 } else if (streq(outform, "dtb")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000205 dt_to_blob(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000206 } else if (streq(outform, "asm")) {
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000207 dt_to_asm(outf, bi, outversion, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000208 } else if (streq(outform, "null")) {
209 /* do nothing */
210 } else {
211 die("Unknown output format \"%s\"\n", outform);
212 }
213
214 exit(0);
215}