]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/dtc/dtc.cc
Merge llvm trunk r321414 to contrib/llvm.
[FreeBSD/FreeBSD.git] / usr.bin / dtc / dtc.cc
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 David Chisnall
5  * All rights reserved.
6  *
7  * This software was developed by SRI International and the University of
8  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9  * ("CTSRD"), as part of the DARPA CRASH research programme.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34
35 #include <sys/resource.h>
36 #include <fcntl.h>
37 #include <libgen.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <unistd.h>
43
44
45 #include "fdt.hh"
46 #include "checking.hh"
47 #include "util.hh"
48
49 using namespace dtc;
50 using std::string;
51
52 /**
53  * The current major version of the tool.
54  */
55 int version_major = 0;
56 int version_major_compatible = 1;
57 /**
58  * The current minor version of the tool.
59  */
60 int version_minor = 5;
61 int version_minor_compatible = 4;
62 /**
63  * The current patch level of the tool.
64  */
65 int version_patch = 0;
66 int version_patch_compatible = 0;
67
68 static void usage(const string &argv0)
69 {
70         fprintf(stderr, "Usage:\n"
71                 "\t%s\t[-fhsv@] [-b boot_cpu_id] [-d dependency_file]"
72                         "[-E [no-]checker_name]\n"
73                 "\t\t[-H phandle_format] [-I input_format]"
74                         "[-O output_format]\n"
75                 "\t\t[-o output_file] [-R entries] [-S bytes] [-p bytes]"
76                         "[-V blob_version]\n"
77                 "\t\t-W [no-]checker_name] input_file\n", basename(argv0).c_str());
78 }
79
80 /**
81  * Prints the current version of this program..
82  */
83 static void version(const char* progname)
84 {
85         fprintf(stdout, "Version: %s %d.%d.%d compatible with gpl dtc %d.%d.%d\n", progname,
86                 version_major, version_minor, version_patch,
87                 version_major_compatible, version_minor_compatible,
88                 version_patch_compatible);
89 }
90
91 using fdt::device_tree;
92
93 int
94 main(int argc, char **argv)
95 {
96         int ch;
97         int outfile = fileno(stdout);
98         const char *outfile_name = "-";
99         const char *in_file = "-";
100         FILE *depfile = 0;
101         bool debug_mode = false;
102         auto write_fn = &device_tree::write_binary;
103         auto read_fn = &device_tree::parse_dts;
104         uint32_t boot_cpu;
105         bool boot_cpu_specified = false;
106         bool keep_going = false;
107         bool sort = false;
108         clock_t c0 = clock();
109         class device_tree tree;
110         fdt::checking::check_manager checks;
111         const char *options = "@hqI:O:o:V:d:R:S:p:b:fi:svH:W:E:DP:";
112
113         // Don't forget to update the man page if any more options are added.
114         while ((ch = getopt(argc, argv, options)) != -1)
115         {
116                 switch (ch)
117                 {
118                 case 'h':
119                         usage(argv[0]);
120                         return EXIT_SUCCESS;
121                 case 'v':
122                         version(argv[0]);
123                         return EXIT_SUCCESS;
124                 case '@':
125                         tree.write_symbols = true;
126                         break;
127                 case 'I':
128                 {
129                         string arg(optarg);
130                         if (arg == "dtb")
131                         {
132                                 read_fn = &device_tree::parse_dtb;
133                         }
134                         else if (arg == "dts")
135                         {
136                                 read_fn = &device_tree::parse_dts;
137                         }
138                         else
139                         {
140                                 fprintf(stderr, "Unknown input format: %s\n", optarg);
141                                 return EXIT_FAILURE;
142                         }
143                         break;
144                 }
145                 case 'O':
146                 {
147                         string arg(optarg);
148                         if (arg == "dtb")
149                         {
150                                 write_fn = &device_tree::write_binary;
151                         }
152                         else if (arg == "asm")
153                         {
154                                 write_fn = &device_tree::write_asm;
155                         }
156                         else if (arg == "dts")
157                         {
158                                 write_fn = &device_tree::write_dts;
159                         }
160                         else
161                         {
162                                 fprintf(stderr, "Unknown output format: %s\n", optarg);
163                                 return EXIT_FAILURE;
164                         }
165                         break;
166                 }
167                 case 'o':
168                 {
169                         outfile_name = optarg;
170                         outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
171                         if (outfile == -1)
172                         {
173                                 perror("Unable to open output file");
174                                 return EXIT_FAILURE;
175                         }
176                         break;
177                 }
178                 case 'D':
179                         debug_mode = true;
180                         break;
181                 case 'V':
182                         if (string(optarg) != "17")
183                         {
184                                 fprintf(stderr, "Unknown output format version: %s\n", optarg);
185                                 return EXIT_FAILURE;
186                         }
187                         break;
188                 case 'd':
189                 {
190                         if (depfile != 0)
191                         {
192                                 fclose(depfile);
193                         }
194                         if (string(optarg) == "-")
195                         {
196                                 depfile = stdout;
197                         }
198                         else
199                         {
200                                 depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
201                                 if (depfile == 0)
202                                 {
203                                         perror("Unable to open dependency file");
204                                         return EXIT_FAILURE;
205                                 }
206                         }
207                         break;
208                 }
209                 case 'H':
210                 {
211                         string arg(optarg);
212                         if (arg == "both")
213                         {
214                                 tree.set_phandle_format(device_tree::BOTH);
215                         }
216                         else if (arg == "epapr")
217                         {
218                                 tree.set_phandle_format(device_tree::EPAPR);
219                         }
220                         else if (arg == "linux")
221                         {
222                                 tree.set_phandle_format(device_tree::LINUX);
223                         }
224                         else
225                         {
226                                 fprintf(stderr, "Unknown phandle format: %s\n", optarg);
227                                 return EXIT_FAILURE;
228                         }
229                         break;
230                 }
231                 case 'b':
232                         // Don't bother to check if strtoll fails, just
233                         // use the 0 it returns.
234                         boot_cpu = (uint32_t)strtoll(optarg, 0, 10);
235                         boot_cpu_specified = true;
236                         break;
237                 case 'f':
238                         keep_going = true;
239                         break;
240                 case 'W':
241                 case 'E':
242                 {
243                         string arg(optarg);
244                         if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
245                         {
246                                 arg = string(optarg+3);
247                                 if (!checks.disable_checker(arg))
248                                 {
249                                         fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
250                                 }
251                                 break;
252                         }
253                         if (!checks.enable_checker(arg))
254                         {
255                                 fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
256                         }
257                         break;
258                 }
259                 case 's':
260                 {
261                         sort = true;
262                         break;
263                 }
264                 case 'i':
265                 {
266                         tree.add_include_path(optarg);
267                         break;
268                 }
269                 // Should quiet warnings, but for now is silently ignored.
270                 case 'q':
271                         break;
272                 case 'R':
273                         tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
274                         break;
275                 case 'S':
276                         tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
277                         break;
278                 case 'p':
279                         tree.set_blob_padding(strtoll(optarg, 0, 10));
280                         break;
281                 case 'P':
282                         if (!tree.parse_define(optarg))
283                         {
284                                 fprintf(stderr, "Invalid predefine value %s\n",
285                                         optarg);
286                         }
287                         break;
288                 default:
289                         fprintf(stderr, "Unknown option %c\n", ch);
290                         return EXIT_FAILURE;
291                 }
292         }
293         if (optind < argc)
294         {
295                 in_file = argv[optind];
296         }
297         if (depfile != 0)
298         {
299                 fputs(outfile_name, depfile);
300                 fputs(": ", depfile);
301                 fputs(in_file, depfile);
302         }
303         clock_t c1 = clock();
304         (tree.*read_fn)(in_file, depfile);
305         // Override the boot CPU found in the header, if we're loading from dtb
306         if (boot_cpu_specified)
307         {
308                 tree.set_boot_cpu(boot_cpu);
309         }
310         if (sort)
311         {
312                 tree.sort();
313         }
314         if (depfile != 0)
315         {
316                 putc('\n', depfile);
317                 fclose(depfile);
318         }
319         if (!(tree.is_valid() || keep_going))
320         {
321                 fprintf(stderr, "Failed to parse tree.\n");
322                 return EXIT_FAILURE;
323         }
324         clock_t c2 = clock();
325         if (!(checks.run_checks(&tree, true) || keep_going))
326         {
327                 return EXIT_FAILURE;
328         }
329         clock_t c3 = clock();
330         (tree.*write_fn)(outfile);
331         close(outfile);
332         clock_t c4 = clock();
333
334         if (debug_mode)
335         {
336                 struct rusage r;
337
338                 getrusage(RUSAGE_SELF, &r);
339                 fprintf(stderr, "Peak memory usage: %ld bytes\n", r.ru_maxrss);
340                 fprintf(stderr, "Setup and option parsing took %f seconds\n",
341                                 ((double)(c1-c0))/CLOCKS_PER_SEC);
342                 fprintf(stderr, "Parsing took %f seconds\n",
343                                 ((double)(c2-c1))/CLOCKS_PER_SEC);
344                 fprintf(stderr, "Checking took %f seconds\n",
345                                 ((double)(c3-c2))/CLOCKS_PER_SEC);
346                 fprintf(stderr, "Generating output took %f seconds\n",
347                                 ((double)(c4-c3))/CLOCKS_PER_SEC);
348                 fprintf(stderr, "Total time: %f seconds\n",
349                                 ((double)(c4-c0))/CLOCKS_PER_SEC);
350                 // This is not needed, but keeps valgrind quiet.
351                 fclose(stdin);
352         }
353         return EXIT_SUCCESS;
354 }
355