]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/dtc/dtc.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / dtc / dtc.c
1 /*
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 #include "srcpos.h"
23
24 #include "version_gen.h"
25
26 /*
27  * Command line options
28  */
29 int quiet;              /* Level of quietness */
30 int reservenum;         /* Number of memory reservation slots */
31 int minsize;            /* Minimum blob size */
32 int padsize;            /* Additional padding to blob */
33 int phandle_format = PHANDLE_BOTH;      /* Use linux,phandle or phandle properties */
34
35 char *join_path(const char *path, const char *name)
36 {
37         int lenp = strlen(path);
38         int lenn = strlen(name);
39         int len;
40         int needslash = 1;
41         char *str;
42
43         len = lenp + lenn + 2;
44         if ((lenp > 0) && (path[lenp-1] == '/')) {
45                 needslash = 0;
46                 len--;
47         }
48
49         str = xmalloc(len);
50         memcpy(str, path, lenp);
51         if (needslash) {
52                 str[lenp] = '/';
53                 lenp++;
54         }
55         memcpy(str+lenp, name, lenn+1);
56         return str;
57 }
58
59 static void fill_fullpaths(struct node *tree, const char *prefix)
60 {
61         struct node *child;
62         const char *unit;
63
64         tree->fullpath = join_path(prefix, tree->name);
65
66         unit = strchr(tree->name, '@');
67         if (unit)
68                 tree->basenamelen = unit - tree->name;
69         else
70                 tree->basenamelen = strlen(tree->name);
71
72         for_each_child(tree, child)
73                 fill_fullpaths(child, tree->fullpath);
74 }
75
76 static void  __attribute__ ((noreturn)) usage(void)
77 {
78         fprintf(stderr, "Usage:\n");
79         fprintf(stderr, "\tdtc [options] <input file>\n");
80         fprintf(stderr, "\nOptions:\n");
81         fprintf(stderr, "\t-h\n");
82         fprintf(stderr, "\t\tThis help text\n");
83         fprintf(stderr, "\t-q\n");
84         fprintf(stderr, "\t\tQuiet: -q suppress warnings, -qq errors, -qqq all\n");
85         fprintf(stderr, "\t-I <input format>\n");
86         fprintf(stderr, "\t\tInput formats are:\n");
87         fprintf(stderr, "\t\t\tdts - device tree source text\n");
88         fprintf(stderr, "\t\t\tdtb - device tree blob\n");
89         fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
90         fprintf(stderr, "\t-o <output file>\n");
91         fprintf(stderr, "\t-O <output format>\n");
92         fprintf(stderr, "\t\tOutput formats are:\n");
93         fprintf(stderr, "\t\t\tdts - device tree source text\n");
94         fprintf(stderr, "\t\t\tdtb - device tree blob\n");
95         fprintf(stderr, "\t\t\tasm - assembler source\n");
96         fprintf(stderr, "\t-V <output version>\n");
97         fprintf(stderr, "\t\tBlob version to produce, defaults to %d (relevant for dtb\n\t\tand asm output only)\n", DEFAULT_FDT_VERSION);
98         fprintf(stderr, "\t-R <number>\n");
99         fprintf(stderr, "\t\tMake space for <number> reserve map entries (relevant for \n\t\tdtb and asm output only)\n");
100         fprintf(stderr, "\t-S <bytes>\n");
101         fprintf(stderr, "\t\tMake the blob at least <bytes> long (extra space)\n");
102         fprintf(stderr, "\t-p <bytes>\n");
103         fprintf(stderr, "\t\tAdd padding to the blob of <bytes> long (extra space)\n");
104         fprintf(stderr, "\t-b <number>\n");
105         fprintf(stderr, "\t\tSet the physical boot cpu\n");
106         fprintf(stderr, "\t-f\n");
107         fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
108         fprintf(stderr, "\t-v\n");
109         fprintf(stderr, "\t\tPrint DTC version and exit\n");
110         fprintf(stderr, "\t-H <phandle format>\n");
111         fprintf(stderr, "\t\tphandle formats are:\n");
112         fprintf(stderr, "\t\t\tlegacy - \"linux,phandle\" properties only\n");
113         fprintf(stderr, "\t\t\tepapr - \"phandle\" properties only\n");
114         fprintf(stderr, "\t\t\tboth - Both \"linux,phandle\" and \"phandle\" properties\n");
115         exit(3);
116 }
117
118 int main(int argc, char *argv[])
119 {
120         struct boot_info *bi;
121         const char *inform = "dts";
122         const char *outform = "dts";
123         const char *outname = "-";
124         int force = 0, check = 0;
125         const char *arg;
126         int opt;
127         FILE *outf = NULL;
128         int outversion = DEFAULT_FDT_VERSION;
129         long long cmdline_boot_cpuid = -1;
130
131         quiet      = 0;
132         reservenum = 0;
133         minsize    = 0;
134         padsize    = 0;
135
136         while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:p:fcqb:vH:")) != EOF) {
137                 switch (opt) {
138                 case 'I':
139                         inform = optarg;
140                         break;
141                 case 'O':
142                         outform = optarg;
143                         break;
144                 case 'o':
145                         outname = optarg;
146                         break;
147                 case 'V':
148                         outversion = strtol(optarg, NULL, 0);
149                         break;
150                 case 'R':
151                         reservenum = strtol(optarg, NULL, 0);
152                         break;
153                 case 'S':
154                         minsize = strtol(optarg, NULL, 0);
155                         break;
156                 case 'p':
157                         padsize = strtol(optarg, NULL, 0);
158                         break;
159                 case 'f':
160                         force = 1;
161                         break;
162                 case 'c':
163                         check = 1;
164                         break;
165                 case 'q':
166                         quiet++;
167                         break;
168                 case 'b':
169                         cmdline_boot_cpuid = strtoll(optarg, NULL, 0);
170                         break;
171                 case 'v':
172                         printf("Version: %s\n", DTC_VERSION);
173                         exit(0);
174                 case 'H':
175                         if (streq(optarg, "legacy"))
176                                 phandle_format = PHANDLE_LEGACY;
177                         else if (streq(optarg, "epapr"))
178                                 phandle_format = PHANDLE_EPAPR;
179                         else if (streq(optarg, "both"))
180                                 phandle_format = PHANDLE_BOTH;
181                         else
182                                 die("Invalid argument \"%s\" to -H option\n",
183                                     optarg);
184                         break;
185
186                 case 'h':
187                 default:
188                         usage();
189                 }
190         }
191
192         if (argc > (optind+1))
193                 usage();
194         else if (argc < (optind+1))
195                 arg = "-";
196         else
197                 arg = argv[optind];
198
199         /* minsize and padsize are mutually exclusive */
200         if (minsize && padsize)
201                 die("Can't set both -p and -S\n");
202
203         if (minsize)
204                 fprintf(stderr, "DTC: Use of \"-S\" is deprecated; it will be removed soon, use \"-p\" instead\n");
205
206         fprintf(stderr, "DTC: %s->%s  on file \"%s\"\n",
207                 inform, outform, arg);
208
209         if (streq(inform, "dts"))
210                 bi = dt_from_source(arg);
211         else if (streq(inform, "fs"))
212                 bi = dt_from_fs(arg);
213         else if(streq(inform, "dtb"))
214                 bi = dt_from_blob(arg);
215         else
216                 die("Unknown input format \"%s\"\n", inform);
217
218         if (cmdline_boot_cpuid != -1)
219                 bi->boot_cpuid_phys = cmdline_boot_cpuid;
220
221         fill_fullpaths(bi->dt, "");
222         process_checks(force, bi);
223
224
225         if (streq(outname, "-")) {
226                 outf = stdout;
227         } else {
228                 outf = fopen(outname, "w");
229                 if (! outf)
230                         die("Couldn't open output file %s: %s\n",
231                             outname, strerror(errno));
232         }
233
234         if (streq(outform, "dts")) {
235                 dt_to_source(outf, bi);
236         } else if (streq(outform, "dtb")) {
237                 dt_to_blob(outf, bi, outversion);
238         } else if (streq(outform, "asm")) {
239                 dt_to_asm(outf, bi, outversion);
240         } else if (streq(outform, "null")) {
241                 /* do nothing */
242         } else {
243                 die("Unknown output format \"%s\"\n", outform);
244         }
245
246         exit(0);
247 }