VirtualBox

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

Last change on this file since 100006 was 100006, checked in by vboxsync, 2 years ago

Runtime: Start implementing the RTFdt* API to read and write flattened devicetrees, bugref:10401 (laptop to desktop)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/* $Id: RTDtc.cpp 100006 2023-05-30 06:27:25Z 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.215389.xyz.
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);
209 if (RT_FAILURE(rc))
210 rc = RTMsgErrorRc(rc, "Writing the devicetree failed: %Rrc\n", rc);
211 RTVfsIoStrmRelease(hVfsIosDst);
212 }
213 RTFdtDestroy(hFdt);
214 }
215
216 if (RT_FAILURE(rc))
217 return RTEXITCODE_FAILURE;
218
219 return RTEXITCODE_SUCCESS;
220}
221
222
223/**
224 * A dtc clone.
225 *
226 * @returns Program exit code.
227 *
228 * @param cArgs The number of arguments.
229 * @param papszArgs The argument vector. (Note that this may be
230 * reordered, so the memory must be writable.)
231 */
232static RTEXITCODE RTCmdDtc(unsigned cArgs, char **papszArgs)
233{
234
235 /*
236 * Parse the command line.
237 */
238 static const RTGETOPTDEF s_aOptions[] =
239 {
240 { "--in-format", 'I', RTGETOPT_REQ_STRING },
241 { "--out", 'o', RTGETOPT_REQ_STRING },
242 { "--out-format", 'O', RTGETOPT_REQ_STRING },
243 { "--out-version", 'V', RTGETOPT_REQ_UINT32 },
244 { "--help", 'h', RTGETOPT_REQ_NOTHING },
245 { "--version", 'v', RTGETOPT_REQ_NOTHING },
246
247 };
248
249 RTCMDDTCOPTS Opts;
250 Opts.enmInType = RTFDTTYPE_INVALID;
251 Opts.enmOutType = RTFDTTYPE_INVALID;
252
253 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
254 RTGETOPTSTATE GetState;
255 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
256 RTGETOPTINIT_FLAGS_OPTS_FIRST);
257 if (RT_SUCCESS(rc))
258 {
259 bool fContinue = true;
260 do
261 {
262 RTGETOPTUNION ValueUnion;
263 int chOpt = RTGetOpt(&GetState, &ValueUnion);
264 switch (chOpt)
265 {
266 case VINF_GETOPT_NOT_OPTION:
267 {
268 RTVFSIOSTREAM hVfsSrc;
269 RTEXITCODE rcExit2 = rtCmdDtcOpenInput(ValueUnion.psz, &hVfsSrc);
270 if (rcExit2 == RTEXITCODE_SUCCESS)
271 {
272 rcExit2 = rtCmdDtcProcess(&Opts, hVfsSrc);
273 RTVfsIoStrmRelease(hVfsSrc);
274 }
275 if (rcExit2 != RTEXITCODE_SUCCESS)
276 rcExit = rcExit2;
277 break;
278 }
279
280 case 'I':
281 Opts.enmInType = rtCmdDtcParseInputType(ValueUnion.psz);
282 if (Opts.enmInType == RTFDTTYPE_INVALID)
283 {
284 RTMsgErrorExitFailure("Invalid input type given: %s", ValueUnion.psz);
285 fContinue = false;
286 }
287 break;
288
289 case 'O':
290 Opts.enmOutType = rtCmdDtcParseOutputType(ValueUnion.psz);
291 if (Opts.enmOutType == RTFDTTYPE_INVALID)
292 {
293 RTMsgErrorExitFailure("Invalid output type given: %s", ValueUnion.psz);
294 fContinue = false;
295 }
296 break;
297
298 case 'h':
299 RTPrintf("Usage: to be written\nOption dump:\n");
300 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
301 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
302 fContinue = false;
303 break;
304
305 case 'v':
306 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
307 fContinue = false;
308 break;
309
310 default:
311 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
312 fContinue = false;
313 break;
314 }
315 } while (fContinue);
316 }
317 else
318 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
319 return rcExit;
320}
321
322
323int main(int argc, char **argv)
324{
325 int rc = RTR3InitExe(argc, &argv, 0);
326 if (RT_FAILURE(rc))
327 return RTMsgInitFailure(rc);
328 return RTCmdDtc(argc, argv);
329}
330
Note: See TracBrowser for help on using the repository browser.

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