VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTDtc.cpp@ 100249

Last change on this file since 100249 was 100022, checked in by vboxsync, 19 months ago

Runtime/RTFdt: Implement parsing Devicetree blobs (DTB) and output a DTS, bugref:10401

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* $Id: RTDtc.cpp 100022 2023-05-31 08:40:43Z vboxsync $ */
2/** @file
3 * IPRT - dtc (devicetree compiler) like utility.
4 */
5
6/*
7 * Copyright (C) 2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/fdt.h>
42
43#include <iprt/buildconfig.h>
44#include <iprt/errcore.h>
45#include <iprt/file.h>
46#include <iprt/getopt.h>
47#include <iprt/initterm.h>
48#include <iprt/message.h>
49#include <iprt/param.h>
50#include <iprt/path.h>
51#include <iprt/stream.h>
52#include <iprt/string.h>
53#include <iprt/vfs.h>
54
55
56/*********************************************************************************************************************************
57* Structures and Typedefs *
58*********************************************************************************************************************************/
59/**
60 * CAT command options.
61 */
62typedef struct RTCMDDTCOPTS
63{
64 /** The input format. */
65 RTFDTTYPE enmInType;
66 /** The output format. */
67 RTFDTTYPE enmOutType;
68 /** The output filename */
69 const char *pszOutFile;
70 /** Output blob version. */
71 uint32_t u32VersionBlobOut;
72} RTCMDDTCOPTS;
73/** Pointer to const DTC options. */
74typedef RTCMDDTCOPTS const *PCRTCMDDTCOPTS;
75
76
77/**
78 * Parses the given input type string returning the matching enum.
79 *
80 * @returns The matching RTFDTTYPE enum or RTFDTTYPE_INVALID if not matching.
81 * @param pszType The FDT tpe as string.
82 */
83DECLINLINE(RTFDTTYPE) rtCmdDtcParseInputType(const char *pszType)
84{
85 if (!strcmp(pszType, "dtb"))
86 return RTFDTTYPE_DTB;
87 else if (!strcmp(pszType, "dts"))
88 return RTFDTTYPE_DTS;
89
90 return RTFDTTYPE_INVALID;
91}
92
93
94/**
95 * Parses the given output type string returning the matching enum.
96 *
97 * @returns The matching RTFDTTYPE enum or RTFDTTYPE_INVALID if not matching.
98 * @param pszType The FDT tpe as string.
99 */
100DECLINLINE(RTFDTTYPE) rtCmdDtcParseOutputType(const char *pszType)
101{
102 if (!strcmp(pszType, "dtb"))
103 return RTFDTTYPE_DTB;
104 else if (!strcmp(pszType, "dts"))
105 return RTFDTTYPE_DTS;
106
107 return RTFDTTYPE_INVALID;
108}
109
110
111/**
112 * Opens the input file.
113 *
114 * @returns Command exit, error messages written using RTMsg*.
115 *
116 * @param pszFile The input filename.
117 * @param phVfsIos Where to return the input stream handle.
118 */
119static RTEXITCODE rtCmdDtcOpenInput(const char *pszFile, PRTVFSIOSTREAM phVfsIos)
120{
121 int rc;
122
123 if (!strcmp(pszFile, "-"))
124 {
125 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_INPUT,
126 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
127 true /*fLeaveOpen*/,
128 phVfsIos);
129 if (RT_FAILURE(rc))
130 return RTMsgErrorExitFailure("Error opening standard input: %Rrc", rc);
131 }
132 else
133 {
134 uint32_t offError = 0;
135 RTERRINFOSTATIC ErrInfo;
136 rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
137 phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
138 if (RT_FAILURE(rc))
139 return RTVfsChainMsgErrorExitFailure("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
140 }
141
142 return RTEXITCODE_SUCCESS;
143}
144
145
146/**
147 * Opens the output file.
148 *
149 * @returns IPRT status code.
150 *
151 * @param pszFile The input filename.
152 * @param phVfsIos Where to return the input stream handle.
153 */
154static int rtCmdDtcOpenOutput(const char *pszFile, PRTVFSIOSTREAM phVfsIos)
155{
156 int rc;
157
158 if (!strcmp(pszFile, "-"))
159 {
160 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT,
161 RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
162 true /*fLeaveOpen*/,
163 phVfsIos);
164 if (RT_FAILURE(rc))
165 return RTMsgErrorRc(rc, "Error opening standard output: %Rrc", rc);
166 }
167 else
168 {
169 uint32_t offError = 0;
170 RTERRINFOSTATIC ErrInfo;
171 rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE,
172 phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
173 if (RT_FAILURE(rc))
174 {
175 RTVfsChainMsgError("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
176 return rc;
177 }
178 }
179
180 return VINF_SUCCESS;
181
182}
183
184
185/**
186 * Processes the given input according to the options.
187 *
188 * @returns Command exit code, error messages written using RTMsg*.
189 * @param pOpts The command options.
190 * @param hVfsSrc VFS I/O stream handle of the input.
191 */
192static RTEXITCODE rtCmdDtcProcess(PCRTCMDDTCOPTS pOpts, RTVFSIOSTREAM hVfsSrc)
193{
194 if (pOpts->enmInType == RTFDTTYPE_INVALID)
195 return RTMsgErrorExitFailure("Devicetree input format wasn't given");
196 if (pOpts->enmOutType == RTFDTTYPE_INVALID)
197 return RTMsgErrorExitFailure("Devicetree output format wasn't given");
198
199 RTFDT hFdt = NIL_RTFDT;
200 RTERRINFOSTATIC ErrInfo;
201 int rc = RTFdtCreateFromVfsIoStrm(&hFdt, hVfsSrc, pOpts->enmInType, RTErrInfoInitStatic(&ErrInfo));
202 if (RT_SUCCESS(rc))
203 {
204 RTVFSIOSTREAM hVfsIosDst = NIL_RTVFSIOSTREAM;
205 rc = rtCmdDtcOpenOutput(pOpts->pszOutFile, &hVfsIosDst);
206 if (RT_SUCCESS(rc))
207 {
208 rc = RTFdtDumpToVfsIoStrm(hFdt, pOpts->enmOutType, 0 /*fFlags*/, hVfsIosDst, RTErrInfoInitStatic(&ErrInfo));
209 if (RT_FAILURE(rc) && RTErrInfoIsSet(&ErrInfo.Core))
210 rc = RTMsgErrorRc(rc, "Writing the devicetree failed: %Rrc - %s", rc, ErrInfo.Core.pszMsg);
211 else if (RT_FAILURE(rc))
212 rc = RTMsgErrorRc(rc, "Writing the devicetree failed: %Rrc\n", rc);
213 RTVfsIoStrmRelease(hVfsIosDst);
214 }
215 RTFdtDestroy(hFdt);
216 }
217 else
218 {
219 if (RTErrInfoIsSet(&ErrInfo.Core))
220 rc = RTMsgErrorRc(rc, "Loading the devicetree blob failed: %Rrc - %s", rc, ErrInfo.Core.pszMsg);
221 else
222 rc = RTMsgErrorRc(rc, "Loading the devicetree blob failed: %Rrc\n", rc);
223 }
224
225 if (RT_FAILURE(rc))
226 return RTEXITCODE_FAILURE;
227
228 return RTEXITCODE_SUCCESS;
229}
230
231
232/**
233 * A dtc clone.
234 *
235 * @returns Program exit code.
236 *
237 * @param cArgs The number of arguments.
238 * @param papszArgs The argument vector. (Note that this may be
239 * reordered, so the memory must be writable.)
240 */
241static RTEXITCODE RTCmdDtc(unsigned cArgs, char **papszArgs)
242{
243
244 /*
245 * Parse the command line.
246 */
247 static const RTGETOPTDEF s_aOptions[] =
248 {
249 { "--in-format", 'I', RTGETOPT_REQ_STRING },
250 { "--out", 'o', RTGETOPT_REQ_STRING },
251 { "--out-format", 'O', RTGETOPT_REQ_STRING },
252 { "--out-version", 'V', RTGETOPT_REQ_UINT32 },
253 { "--help", 'h', RTGETOPT_REQ_NOTHING },
254 { "--version", 'v', RTGETOPT_REQ_NOTHING },
255
256 };
257
258 RTCMDDTCOPTS Opts;
259 Opts.enmInType = RTFDTTYPE_INVALID;
260 Opts.enmOutType = RTFDTTYPE_INVALID;
261
262 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
263 RTGETOPTSTATE GetState;
264 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
265 RTGETOPTINIT_FLAGS_OPTS_FIRST);
266 if (RT_SUCCESS(rc))
267 {
268 bool fContinue = true;
269 do
270 {
271 RTGETOPTUNION ValueUnion;
272 int chOpt = RTGetOpt(&GetState, &ValueUnion);
273 switch (chOpt)
274 {
275 case VINF_GETOPT_NOT_OPTION:
276 {
277 RTVFSIOSTREAM hVfsSrc;
278 RTEXITCODE rcExit2 = rtCmdDtcOpenInput(ValueUnion.psz, &hVfsSrc);
279 if (rcExit2 == RTEXITCODE_SUCCESS)
280 {
281 rcExit2 = rtCmdDtcProcess(&Opts, hVfsSrc);
282 RTVfsIoStrmRelease(hVfsSrc);
283 }
284 if (rcExit2 != RTEXITCODE_SUCCESS)
285 rcExit = rcExit2;
286 fContinue = false;
287 break;
288 }
289
290 case 'I':
291 Opts.enmInType = rtCmdDtcParseInputType(ValueUnion.psz);
292 if (Opts.enmInType == RTFDTTYPE_INVALID)
293 {
294 RTMsgErrorExitFailure("Invalid input type given: %s", ValueUnion.psz);
295 fContinue = false;
296 }
297 break;
298
299 case 'o':
300 Opts.pszOutFile = ValueUnion.psz;
301 break;
302
303 case 'O':
304 Opts.enmOutType = rtCmdDtcParseOutputType(ValueUnion.psz);
305 if (Opts.enmOutType == RTFDTTYPE_INVALID)
306 {
307 RTMsgErrorExitFailure("Invalid output type given: %s", ValueUnion.psz);
308 fContinue = false;
309 }
310 break;
311
312 case 'h':
313 RTPrintf("Usage: to be written\nOption dump:\n");
314 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
315 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
316 fContinue = false;
317 break;
318
319 case 'v':
320 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
321 fContinue = false;
322 break;
323
324 default:
325 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
326 fContinue = false;
327 break;
328 }
329 } while (fContinue);
330 }
331 else
332 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
333 return rcExit;
334}
335
336
337int main(int argc, char **argv)
338{
339 int rc = RTR3InitExe(argc, &argv, 0);
340 if (RT_FAILURE(rc))
341 return RTMsgInitFailure(rc);
342 return RTCmdDtc(argc, argv);
343}
344
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette