]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/dtc/dtc.cc
Update to bmake-201802222
[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 namespace {
53
54 /**
55  * The current major version of the tool.
56  */
57 int version_major = 0;
58 int version_major_compatible = 1;
59 /**
60  * The current minor version of the tool.
61  */
62 int version_minor = 5;
63 int version_minor_compatible = 4;
64 /**
65  * The current patch level of the tool.
66  */
67 int version_patch = 0;
68 int version_patch_compatible = 0;
69
70 void usage(const string &argv0)
71 {
72         fprintf(stderr, "Usage:\n"
73                 "\t%s\t[-fhsv@] [-b boot_cpu_id] [-d dependency_file]"
74                         "[-E [no-]checker_name]\n"
75                 "\t\t[-H phandle_format] [-I input_format]"
76                         "[-O output_format]\n"
77                 "\t\t[-o output_file] [-R entries] [-S bytes] [-p bytes]"
78                         "[-V blob_version]\n"
79                 "\t\t-W [no-]checker_name] input_file\n", basename(argv0).c_str());
80 }
81
82 /**
83  * Prints the current version of this program..
84  */
85 void version(const char* progname)
86 {
87         fprintf(stdout, "Version: %s %d.%d.%d compatible with gpl dtc %d.%d.%d\n", progname,
88                 version_major, version_minor, version_patch,
89                 version_major_compatible, version_minor_compatible,
90                 version_patch_compatible);
91 }
92
93 } // Anonymous namespace
94
95 using fdt::device_tree;
96
97 int
98 main(int argc, char **argv)
99 {
100         int ch;
101         int outfile = fileno(stdout);
102         const char *outfile_name = "-";
103         const char *in_file = "-";
104         FILE *depfile = 0;
105         bool debug_mode = false;
106         auto write_fn = &device_tree::write_binary;
107         auto read_fn = &device_tree::parse_dts;
108         uint32_t boot_cpu;
109         bool boot_cpu_specified = false;
110         bool keep_going = false;
111         bool sort = false;
112         clock_t c0 = clock();
113         class device_tree tree;
114         fdt::checking::check_manager checks;
115         const char *options = "@hqI:O:o:V:d:R:S:p:b:fi:svH:W:E:DP:";
116
117         // Don't forget to update the man page if any more options are added.
118         while ((ch = getopt(argc, argv, options)) != -1)
119         {
120                 switch (ch)
121                 {
122                 case 'h':
123                         usage(argv[0]);
124                         return EXIT_SUCCESS;
125                 case 'v':
126                         version(argv[0]);
127                         return EXIT_SUCCESS;
128                 case '@':
129                         tree.write_symbols = true;
130                         break;
131                 case 'I':
132                 {
133                         string arg(optarg);
134                         if (arg == "dtb")
135                         {
136                                 read_fn = &device_tree::parse_dtb;
137                         }
138                         else if (arg == "dts")
139                         {
140                                 read_fn = &device_tree::parse_dts;
141                         }
142                         else
143                         {
144                                 fprintf(stderr, "Unknown input format: %s\n", optarg);
145                                 return EXIT_FAILURE;
146                         }
147                         break;
148                 }
149                 case 'O':
150                 {
151                         string arg(optarg);
152                         if (arg == "dtb")
153                         {
154                                 write_fn = &device_tree::write_binary;
155                         }
156                         else if (arg == "asm")
157                         {
158                                 write_fn = &device_tree::write_asm;
159                         }
160                         else if (arg == "dts")
161                         {
162                                 write_fn = &device_tree::write_dts;
163                         }
164                         else
165                         {
166                                 fprintf(stderr, "Unknown output format: %s\n", optarg);
167                                 return EXIT_FAILURE;
168                         }
169                         break;
170                 }
171                 case 'o':
172                 {
173                         outfile_name = optarg;
174                         outfile = open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666);
175                         if (outfile == -1)
176                         {
177                                 perror("Unable to open output file");
178                                 return EXIT_FAILURE;
179                         }
180                         break;
181                 }
182                 case 'D':
183                         debug_mode = true;
184                         break;
185                 case 'V':
186                         if (string(optarg) != "17")
187                         {
188                                 fprintf(stderr, "Unknown output format version: %s\n", optarg);
189                                 return EXIT_FAILURE;
190                         }
191                         break;
192                 case 'd':
193                 {
194                         if (depfile != 0)
195                         {
196                                 fclose(depfile);
197                         }
198                         if (string(optarg) == "-")
199                         {
200                                 depfile = stdout;
201                         }
202                         else
203                         {
204                                 depfile = fdopen(open(optarg, O_CREAT | O_TRUNC | O_WRONLY, 0666), "w");
205                                 if (depfile == 0)
206                                 {
207                                         perror("Unable to open dependency file");
208                                         return EXIT_FAILURE;
209                                 }
210                         }
211                         break;
212                 }
213                 case 'H':
214                 {
215                         string arg(optarg);
216                         if (arg == "both")
217                         {
218                                 tree.set_phandle_format(device_tree::BOTH);
219                         }
220                         else if (arg == "epapr")
221                         {
222                                 tree.set_phandle_format(device_tree::EPAPR);
223                         }
224                         else if (arg == "linux")
225                         {
226                                 tree.set_phandle_format(device_tree::LINUX);
227                         }
228                         else
229                         {
230                                 fprintf(stderr, "Unknown phandle format: %s\n", optarg);
231                                 return EXIT_FAILURE;
232                         }
233                         break;
234                 }
235                 case 'b':
236                         // Don't bother to check if strtoll fails, just
237                         // use the 0 it returns.
238                         boot_cpu = (uint32_t)strtoll(optarg, 0, 10);
239                         boot_cpu_specified = true;
240                         break;
241                 case 'f':
242                         keep_going = true;
243                         break;
244                 case 'W':
245                 case 'E':
246                 {
247                         string arg(optarg);
248                         if ((arg.size() > 3) && (strncmp(optarg, "no-", 3) == 0))
249                         {
250                                 arg = string(optarg+3);
251                                 if (!checks.disable_checker(arg))
252                                 {
253                                         fprintf(stderr, "Checker %s either does not exist or is already disabled\n", optarg+3);
254                                 }
255                                 break;
256                         }
257                         if (!checks.enable_checker(arg))
258                         {
259                                 fprintf(stderr, "Checker %s either does not exist or is already enabled\n", optarg);
260                         }
261                         break;
262                 }
263                 case 's':
264                 {
265                         sort = true;
266                         break;
267                 }
268                 case 'i':
269                 {
270                         tree.add_include_path(optarg);
271                         break;
272                 }
273                 // Should quiet warnings, but for now is silently ignored.
274                 case 'q':
275                         break;
276                 case 'R':
277                         tree.set_empty_reserve_map_entries(strtoll(optarg, 0, 10));
278                         break;
279                 case 'S':
280                         tree.set_blob_minimum_size(strtoll(optarg, 0, 10));
281                         break;
282                 case 'p':
283                         tree.set_blob_padding(strtoll(optarg, 0, 10));
284                         break;
285                 case 'P':
286                         if (!tree.parse_define(optarg))
287                         {
288                                 fprintf(stderr, "Invalid predefine value %s\n",
289                                         optarg);
290                         }
291                         break;
292                 default:
293                         fprintf(stderr, "Unknown option %c\n", ch);
294                         return EXIT_FAILURE;
295                 }
296         }
297         if (optind < argc)
298         {
299                 in_file = argv[optind];
300         }
301         if (depfile != 0)
302         {
303                 fputs(outfile_name, depfile);
304                 fputs(": ", depfile);
305                 fputs(in_file, depfile);
306         }
307         clock_t c1 = clock();
308         (tree.*read_fn)(in_file, depfile);
309         // Override the boot CPU found in the header, if we're loading from dtb
310         if (boot_cpu_specified)
311         {
312                 tree.set_boot_cpu(boot_cpu);
313         }
314         if (sort)
315         {
316                 tree.sort();
317         }
318         if (depfile != 0)
319         {
320                 putc('\n', depfile);
321                 fclose(depfile);
322         }
323         if (!(tree.is_valid() || keep_going))
324         {
325                 fprintf(stderr, "Failed to parse tree.\n");
326                 return EXIT_FAILURE;
327         }
328         clock_t c2 = clock();
329         if (!(checks.run_checks(&tree, true) || keep_going))
330         {
331                 return EXIT_FAILURE;
332         }
333         clock_t c3 = clock();
334         (tree.*write_fn)(outfile);
335         close(outfile);
336         clock_t c4 = clock();
337
338         if (debug_mode)
339         {
340                 struct rusage r;
341
342                 getrusage(RUSAGE_SELF, &r);
343                 fprintf(stderr, "Peak memory usage: %ld bytes\n", r.ru_maxrss);
344                 fprintf(stderr, "Setup and option parsing took %f seconds\n",
345                                 ((double)(c1-c0))/CLOCKS_PER_SEC);
346                 fprintf(stderr, "Parsing took %f seconds\n",
347                                 ((double)(c2-c1))/CLOCKS_PER_SEC);
348                 fprintf(stderr, "Checking took %f seconds\n",
349                                 ((double)(c3-c2))/CLOCKS_PER_SEC);
350                 fprintf(stderr, "Generating output took %f seconds\n",
351                                 ((double)(c4-c3))/CLOCKS_PER_SEC);
352                 fprintf(stderr, "Total time: %f seconds\n",
353                                 ((double)(c4-c0))/CLOCKS_PER_SEC);
354                 // This is not needed, but keeps valgrind quiet.
355                 fclose(stdin);
356         }
357         return EXIT_SUCCESS;
358 }
359