1 | /* $Id: RTIasl.cpp 108016 2025-02-01 19:21:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - iasl (acpica) like utility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2025 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/acpi.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 | * IASL command options.
|
---|
61 | */
|
---|
62 | typedef struct RTCMDIASLOPTS
|
---|
63 | {
|
---|
64 | /** The input format. */
|
---|
65 | RTACPITBLTYPE enmInType;
|
---|
66 | /** The output format. */
|
---|
67 | RTACPITBLTYPE enmOutType;
|
---|
68 | /** The output filename */
|
---|
69 | const char *pszOutFile;
|
---|
70 | /** Output blob version. */
|
---|
71 | uint32_t u32VersionBlobOut;
|
---|
72 | } RTCMDIASLOPTS;
|
---|
73 | /** Pointer to const IASL options. */
|
---|
74 | typedef RTCMDIASLOPTS const *PCRTCMDIASLOPTS;
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Opens the input file.
|
---|
79 | *
|
---|
80 | * @returns Command exit, error messages written using RTMsg*.
|
---|
81 | *
|
---|
82 | * @param pszFile The input filename.
|
---|
83 | * @param phVfsIos Where to return the input stream handle.
|
---|
84 | */
|
---|
85 | static RTEXITCODE rtCmdIaslOpenInput(const char *pszFile, PRTVFSIOSTREAM phVfsIos)
|
---|
86 | {
|
---|
87 | int rc;
|
---|
88 |
|
---|
89 | if (!strcmp(pszFile, "-"))
|
---|
90 | {
|
---|
91 | rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_INPUT,
|
---|
92 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
|
---|
93 | true /*fLeaveOpen*/,
|
---|
94 | phVfsIos);
|
---|
95 | if (RT_FAILURE(rc))
|
---|
96 | return RTMsgErrorExitFailure("Error opening standard input: %Rrc", rc);
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | uint32_t offError = 0;
|
---|
101 | RTERRINFOSTATIC ErrInfo;
|
---|
102 | rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
|
---|
103 | phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | return RTVfsChainMsgErrorExitFailure("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
|
---|
106 | }
|
---|
107 |
|
---|
108 | return RTEXITCODE_SUCCESS;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Opens the output file.
|
---|
114 | *
|
---|
115 | * @returns IPRT status code.
|
---|
116 | *
|
---|
117 | * @param pszFile The input filename.
|
---|
118 | * @param phVfsIos Where to return the input stream handle.
|
---|
119 | */
|
---|
120 | static int rtCmdIaslOpenOutput(const char *pszFile, PRTVFSIOSTREAM phVfsIos)
|
---|
121 | {
|
---|
122 | int rc;
|
---|
123 |
|
---|
124 | if (!strcmp(pszFile, "-"))
|
---|
125 | {
|
---|
126 | rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT,
|
---|
127 | RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
|
---|
128 | true /*fLeaveOpen*/,
|
---|
129 | phVfsIos);
|
---|
130 | if (RT_FAILURE(rc))
|
---|
131 | return RTMsgErrorRc(rc, "Error opening standard output: %Rrc", rc);
|
---|
132 | }
|
---|
133 | else
|
---|
134 | {
|
---|
135 | uint32_t offError = 0;
|
---|
136 | RTERRINFOSTATIC ErrInfo;
|
---|
137 | rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE,
|
---|
138 | phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
|
---|
139 | if (RT_FAILURE(rc))
|
---|
140 | {
|
---|
141 | RTVfsChainMsgError("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
|
---|
142 | return rc;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | return VINF_SUCCESS;
|
---|
147 |
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Processes the given input according to the options.
|
---|
153 | *
|
---|
154 | * @returns Command exit code, error messages written using RTMsg*.
|
---|
155 | * @param pOpts The command options.
|
---|
156 | * @param hVfsSrc VFS I/O stream handle of the input.
|
---|
157 | */
|
---|
158 | static RTEXITCODE rtCmdIaslProcess(PCRTCMDIASLOPTS pOpts, RTVFSIOSTREAM hVfsSrc)
|
---|
159 | {
|
---|
160 | if (pOpts->enmInType == RTACPITBLTYPE_INVALID)
|
---|
161 | return RTMsgErrorExitFailure("iASL input format wasn't given");
|
---|
162 | if (pOpts->enmOutType == RTACPITBLTYPE_INVALID)
|
---|
163 | return RTMsgErrorExitFailure("iASL output format wasn't given");
|
---|
164 |
|
---|
165 | RTERRINFOSTATIC ErrInfo;
|
---|
166 | RTVFSIOSTREAM hVfsIosDst = NIL_RTVFSIOSTREAM;
|
---|
167 | int rc = rtCmdIaslOpenOutput(pOpts->pszOutFile, &hVfsIosDst);
|
---|
168 | if (RT_SUCCESS(rc))
|
---|
169 | {
|
---|
170 | rc = RTAcpiTblConvertFromVfsIoStrm(hVfsIosDst, pOpts->enmOutType, hVfsSrc, pOpts->enmInType, RTErrInfoInitStatic(&ErrInfo));
|
---|
171 | if (RT_FAILURE(rc) && RTErrInfoIsSet(&ErrInfo.Core))
|
---|
172 | rc = RTMsgErrorRc(rc, "Disassembling the ACPI table failed: %Rrc - %s", rc, ErrInfo.Core.pszMsg);
|
---|
173 | else if (RT_FAILURE(rc))
|
---|
174 | rc = RTMsgErrorRc(rc, "Writing the disassembled ACPI table failed: %Rrc\n", rc);
|
---|
175 | RTVfsIoStrmRelease(hVfsIosDst);
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (RT_FAILURE(rc))
|
---|
179 | return RTEXITCODE_FAILURE;
|
---|
180 |
|
---|
181 | return RTEXITCODE_SUCCESS;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * A iasl clone.
|
---|
187 | *
|
---|
188 | * @returns Program exit code.
|
---|
189 | *
|
---|
190 | * @param cArgs The number of arguments.
|
---|
191 | * @param papszArgs The argument vector. (Note that this may be
|
---|
192 | * reordered, so the memory must be writable.)
|
---|
193 | */
|
---|
194 | static RTEXITCODE RTCmdIasl(unsigned cArgs, char **papszArgs)
|
---|
195 | {
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * Parse the command line.
|
---|
199 | */
|
---|
200 | static const RTGETOPTDEF s_aOptions[] =
|
---|
201 | {
|
---|
202 | { "--disassemble", 'd', RTGETOPT_REQ_NOTHING },
|
---|
203 | { "--out", 'o', RTGETOPT_REQ_STRING },
|
---|
204 | { "--help", 'h', RTGETOPT_REQ_NOTHING },
|
---|
205 | { "--version", 'v', RTGETOPT_REQ_NOTHING },
|
---|
206 |
|
---|
207 | };
|
---|
208 |
|
---|
209 | RTCMDIASLOPTS Opts;
|
---|
210 | Opts.enmInType = RTACPITBLTYPE_ASL;
|
---|
211 | Opts.enmOutType = RTACPITBLTYPE_AML;
|
---|
212 |
|
---|
213 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
214 | RTGETOPTSTATE GetState;
|
---|
215 | int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
216 | RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | bool fContinue = true;
|
---|
220 | do
|
---|
221 | {
|
---|
222 | RTGETOPTUNION ValueUnion;
|
---|
223 | int chOpt = RTGetOpt(&GetState, &ValueUnion);
|
---|
224 | switch (chOpt)
|
---|
225 | {
|
---|
226 | case VINF_GETOPT_NOT_OPTION:
|
---|
227 | {
|
---|
228 | RTVFSIOSTREAM hVfsSrc;
|
---|
229 | RTEXITCODE rcExit2 = rtCmdIaslOpenInput(ValueUnion.psz, &hVfsSrc);
|
---|
230 | if (rcExit2 == RTEXITCODE_SUCCESS)
|
---|
231 | {
|
---|
232 | rcExit2 = rtCmdIaslProcess(&Opts, hVfsSrc);
|
---|
233 | RTVfsIoStrmRelease(hVfsSrc);
|
---|
234 | }
|
---|
235 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
236 | rcExit = rcExit2;
|
---|
237 | fContinue = false;
|
---|
238 | break;
|
---|
239 | }
|
---|
240 |
|
---|
241 | case 'd':
|
---|
242 | Opts.enmInType = RTACPITBLTYPE_AML;
|
---|
243 | Opts.enmOutType = RTACPITBLTYPE_ASL;
|
---|
244 | break;
|
---|
245 |
|
---|
246 | case 'o':
|
---|
247 | Opts.pszOutFile = ValueUnion.psz;
|
---|
248 | break;
|
---|
249 |
|
---|
250 | case 'h':
|
---|
251 | RTPrintf("Usage: to be written\nOption dump:\n");
|
---|
252 | for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
|
---|
253 | RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
|
---|
254 | fContinue = false;
|
---|
255 | break;
|
---|
256 |
|
---|
257 | case 'v':
|
---|
258 | RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
|
---|
259 | fContinue = false;
|
---|
260 | break;
|
---|
261 |
|
---|
262 | default:
|
---|
263 | rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
|
---|
264 | fContinue = false;
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | } while (fContinue);
|
---|
268 | }
|
---|
269 | else
|
---|
270 | rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
|
---|
271 | return rcExit;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | int main(int argc, char **argv)
|
---|
276 | {
|
---|
277 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
278 | if (RT_FAILURE(rc))
|
---|
279 | return RTMsgInitFailure(rc);
|
---|
280 | return RTCmdIasl(argc, argv);
|
---|
281 | }
|
---|
282 |
|
---|