VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/acpi.cpp@ 107058

Last change on this file since 107058 was 107058, checked in by vboxsync, 2 months ago

Runtime/RTAcpi*: Add new methods for adding if else statements, binary operands and referencing arguments, locals and appending UUIDs, bugref:10733 [scm]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.1 KB
Line 
1/* $Id: acpi.cpp 107058 2024-11-20 11:39:11Z vboxsync $ */
2/** @file
3 * IPRT - Advanced Configuration and Power Interface (ACPI) Table generation API.
4 */
5
6/*
7 * Copyright (C) 2024 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#define LOG_GROUP RTLOGGROUP_ACPI
42#include <iprt/acpi.h>
43#include <iprt/asm.h>
44#include <iprt/file.h>
45#include <iprt/mem.h>
46#include <iprt/string.h>
47#include <iprt/uuid.h>
48
49#include <iprt/formats/acpi-aml.h>
50#include <iprt/formats/acpi-resources.h>
51
52
53/*********************************************************************************************************************************
54* Defined Constants And Macros *
55*********************************************************************************************************************************/
56
57
58
59/*********************************************************************************************************************************
60* Structures and Typedefs *
61*********************************************************************************************************************************/
62
63/**
64 * Package stack element.
65 */
66typedef struct RTACPITBLSTACKELEM
67{
68 /** Pointer to the table buffer memory where the PkgLength object starts. */
69 uint8_t *pbPkgLength;
70 /** Current size of the package in bytes, without the PkgLength object. */
71 uint32_t cbPkg;
72 /** The operator creating the package, UINT8_MAX denotes the special root operator. */
73 uint8_t bOp;
74} RTACPITBLSTACKELEM;
75/** Pointer to a package stack element. */
76typedef RTACPITBLSTACKELEM *PRTACPITBLSTACKELEM;
77/** Pointer to a const package stack element. */
78typedef const RTACPITBLSTACKELEM *PCRTACPITBLSTACKELEM;
79
80
81/**
82 * ACPI table generator instance.
83 */
84typedef struct RTACPITBLINT
85{
86 /** Pointer to the ACPI table header, needed when finalizing the table. */
87 PACPITBLHDR pHdr;
88 /** Byte buffer holding the actual table. */
89 uint8_t *pbTblBuf;
90 /** Size of the table buffer. */
91 uint32_t cbTblBuf;
92 /** Current offset into the table buffer. */
93 uint32_t offTblBuf;
94 /** Flag whether the table is finalized. */
95 bool fFinalized;
96 /** First error code encountered. */
97 int rcErr;
98 /** Pointer to the package element stack. */
99 PRTACPITBLSTACKELEM paPkgStack;
100 /** Number of elements the package stack can hold. */
101 uint32_t cPkgStackElems;
102 /** Index of the current package in the package stack. */
103 uint32_t idxPkgStackElem;
104} RTACPITBLINT;
105/** Pointer to an ACPI table generator instance. */
106typedef RTACPITBLINT *PRTACPITBLINT;
107
108
109/**
110 * ACPI resource builder instance.
111 */
112typedef struct RTACPIRESINT
113{
114 /** Byte buffer holding the resource. */
115 uint8_t *pbResBuf;
116 /** Size of the resource buffer. */
117 size_t cbResBuf;
118 /** Current offset into the resource buffer. */
119 uint32_t offResBuf;
120 /** Flag whether the resource is sealed. */
121 bool fSealed;
122 /** First error code encountered. */
123 int rcErr;
124} RTACPIRESINT;
125/** Pointer to an ACPI resource builder instance. */
126typedef RTACPIRESINT *PRTACPIRESINT;
127
128
129/*********************************************************************************************************************************
130* Global Variables *
131*********************************************************************************************************************************/
132
133
134/*********************************************************************************************************************************
135* Internal Functions *
136*********************************************************************************************************************************/
137
138
139/**
140 * Copies the given string into the given buffer padding the remainder with the given character.
141 *
142 * @param pbId The destination to copy the string to.
143 * @param cbId Size of the buffer in bytes.
144 * @param pszStr The string to copy.
145 * @param chPad The character to pad with.
146 */
147static void rtAcpiTblCopyStringPadWith(uint8_t *pbId, size_t cbId, const char *pszStr, char chPad)
148{
149 Assert(strlen(pszStr) <= cbId);
150
151 uint32_t idx = 0;
152 while (*pszStr != '\0')
153 pbId[idx++] = (uint8_t)*pszStr++;
154
155 while (idx < cbId)
156 pbId[idx++] = chPad;
157}
158
159
160/**
161 * Updates the package length of the current package in the stack
162 *
163 * @param pThis The ACPI table instance.
164 * @param cbAdd How many bytes to add to the package length.
165 */
166DECL_FORCE_INLINE(void) rtAcpiTblUpdatePkgLength(PRTACPITBLINT pThis, uint32_t cbAdd)
167{
168 PRTACPITBLSTACKELEM pPkgElem = &pThis->paPkgStack[pThis->idxPkgStackElem];
169 pPkgElem->cbPkg += cbAdd;
170}
171
172
173/**
174 * Ensures there is the given amount of room in the ACPI table buffer returning the pointer.
175 *
176 * @returns The pointer to the free space on success or NULL if out of memory.
177 * @param pThis The ACPI table instance.
178 * @param cbReq Amount of bytes requested.
179 */
180static uint8_t *rtAcpiTblBufEnsureSpace(PRTACPITBLINT pThis, uint32_t cbReq)
181{
182 if (RT_LIKELY(pThis->cbTblBuf - pThis->offTblBuf >= cbReq))
183 {
184 uint8_t *pb = &pThis->pbTblBuf[pThis->offTblBuf];
185 pThis->offTblBuf += cbReq;
186 return pb;
187 }
188
189 uint32_t const cbNew = RT_ALIGN_32(pThis->cbTblBuf + cbReq, _4K);
190 uint8_t *pbNew = (uint8_t *)RTMemRealloc(pThis->pbTblBuf, cbNew);
191 if (RT_UNLIKELY(!pbNew))
192 {
193 pThis->rcErr = VERR_NO_MEMORY;
194 return NULL;
195 }
196
197 pThis->pbTblBuf = pbNew;
198 pThis->cbTblBuf = cbNew;
199
200 uint8_t *pb = &pThis->pbTblBuf[pThis->offTblBuf];
201 pThis->offTblBuf += cbReq;
202 return pb;
203}
204
205
206/**
207 * Appends a new package in the given ACPI table instance package stack.
208 *
209 * @returns IPRT status code.
210 * @retval VERR_NO_MEMORY if allocating additional resources to hold the new package failed.
211 * @param pThis The ACPI table instance.
212 * @param bOp The opcode byte the package starts with (for verification purposes when finalizing the package).
213 * @param pbPkgBuf The Start of the package buffer.
214 */
215static int rtAcpiTblPkgAppendEx(PRTACPITBLINT pThis, uint8_t bOp, uint8_t *pbPkgBuf)
216{
217 /* Get a new stack element. */
218 if (pThis->idxPkgStackElem + 1 == pThis->cPkgStackElems)
219 {
220 uint32_t const cPkgElemsNew = pThis->cPkgStackElems + 8;
221 PRTACPITBLSTACKELEM paPkgStackNew = (PRTACPITBLSTACKELEM)RTMemRealloc(pThis->paPkgStack, cPkgElemsNew * sizeof(*paPkgStackNew));
222 if (!paPkgStackNew)
223 {
224 pThis->rcErr = VERR_NO_MEMORY;
225 return VERR_NO_MEMORY;
226 }
227
228 pThis->paPkgStack = paPkgStackNew;
229 pThis->cPkgStackElems = cPkgElemsNew;
230 }
231
232 PRTACPITBLSTACKELEM pStackElem = &pThis->paPkgStack[++pThis->idxPkgStackElem];
233 pStackElem->pbPkgLength = pbPkgBuf;
234 pStackElem->cbPkg = 0;
235 pStackElem->bOp = bOp;
236 return VINF_SUCCESS;
237}
238
239
240/**
241 * Starts a new ACPI package in the given ACPI table instance.
242 *
243 * @returns IPRT status code.
244 * @retval VERR_NO_MEMORY if allocating additional resources to hold the new package failed.
245 * @param pThis The ACPI table instance.
246 * @param bOp The opcode byte identifying the package content.
247 */
248static int rtAcpiTblPkgStart(PRTACPITBLINT pThis, uint8_t bOp)
249{
250 /*
251 * Allocate 1 byte for opcode + always 4 bytes for the PkgLength, as we don't know how much we will need upfront.
252 * This will be corrected when the package is finalized.
253 */
254 uint8_t *pbPkg = rtAcpiTblBufEnsureSpace(pThis, 5);
255 if (!pbPkg)
256 {
257 pThis->rcErr = VERR_NO_MEMORY;
258 return VERR_NO_MEMORY;
259 }
260
261 *pbPkg = bOp;
262 /*
263 * Update the package length of the outer package for the opcode,
264 * the PkgLength object's final length will be added in rtAcpiTblPkgFinish().
265 */
266 rtAcpiTblUpdatePkgLength(pThis, sizeof(bOp));
267 return rtAcpiTblPkgAppendEx(pThis, bOp, pbPkg + 1);
268}
269
270
271/**
272 * Starts a new ACPI package in the given ACPI table instance. This is for opcodes prefixed with
273 * ACPI_AML_BYTE_CODE_PREFIX_EXT_O, which will be added automatically.
274 *
275 * @returns IPRT status code.
276 * @retval VERR_NO_MEMORY if allocating additional resources to hold the new package failed.
277 * @param pThis The ACPI table instance.
278 * @param bOp The opcode byte identifying the package content.
279 */
280static int rtAcpiTblPkgStartExt(PRTACPITBLINT pThis, uint8_t bOp)
281{
282 /*
283 * Allocate 2 bytes for ExtOpPrefix opcode + always 4 bytes for the PkgLength, as we don't know how much we will need upfront.
284 * This will be corrected when the package is finalized.
285 */
286 uint8_t *pbPkg = rtAcpiTblBufEnsureSpace(pThis, 6);
287 if (!pbPkg)
288 {
289 pThis->rcErr = VERR_NO_MEMORY;
290 return VERR_NO_MEMORY;
291 }
292
293 pbPkg[0] = ACPI_AML_BYTE_CODE_PREFIX_EXT_OP;
294 pbPkg[1] = bOp;
295
296 /*
297 * Update the package length of the outer package for the opcode,
298 * the PkgLength object's final length will be added in rtAcpiTblPkgFinish().
299 */
300 rtAcpiTblUpdatePkgLength(pThis, sizeof(uint8_t) + sizeof(bOp));
301 return rtAcpiTblPkgAppendEx(pThis, bOp, pbPkg + 2);
302}
303
304
305/**
306 * Finishes the current package on the top of the package stack, setting the
307 * package length accordingly.
308 *
309 * @returns IPRT status code.
310 * @retval VERR_INVALID_STATE if bOp doesn't match the opcode the package was started with (asserted in debug builds).
311 * @retval VERR_BUFFER_OVERFLOW if the package length exceeds what can be encoded in the package length field.
312 * @param pThis The ACPI table instance.
313 * @param bOp The opcode byte identifying the package content the package was started with.
314 */
315static int rtAcpiTblPkgFinish(PRTACPITBLINT pThis, uint8_t bOp)
316{
317 /* Ensure the op matches what is current on the top of the stack. */
318 AssertReturn(pThis->paPkgStack[pThis->idxPkgStackElem].bOp == bOp, VERR_INVALID_STATE);
319
320 /* Pop the topmost stack element from the stack. */
321 PRTACPITBLSTACKELEM pPkgElem = &pThis->paPkgStack[pThis->idxPkgStackElem--];
322
323 /*
324 * Determine how many bytes we actually need for the PkgLength and re-arrange the ACPI table.
325 *
326 * Note! PkgLength will also include its own length.
327 */
328 uint8_t *pbPkgLength = pPkgElem->pbPkgLength;
329 uint32_t cbThisPkg = pPkgElem->cbPkg;
330 if (cbThisPkg + 1 <= 63)
331 {
332 /* Remove the gap. */
333 memmove(pbPkgLength + 1, pbPkgLength + 4, cbThisPkg);
334 pThis->offTblBuf -= 3;
335
336 /* PkgLength only consists of the package lead byte. */
337 cbThisPkg += 1;
338 *pbPkgLength = (cbThisPkg & 0x3f);
339 }
340 else if (cbThisPkg + 2 < RT_BIT_32(12))
341 {
342 /* Remove the gap. */
343 memmove(pbPkgLength + 2, pbPkgLength + 4, cbThisPkg);
344 pThis->offTblBuf -= 2;
345
346 cbThisPkg += 2;
347 pbPkgLength[0] = (1 << 6) | (cbThisPkg & 0xf);
348 pbPkgLength[1] = (cbThisPkg >> 4) & 0xff;
349 }
350 else if (cbThisPkg + 3 < RT_BIT_32(20))
351 {
352 /* Remove the gap. */
353 memmove(pbPkgLength + 3, pbPkgLength + 4, cbThisPkg);
354 pThis->offTblBuf -= 1;
355
356 cbThisPkg += 3;
357 pbPkgLength[0] = (2 << 6) | (cbThisPkg & 0xf);
358 pbPkgLength[1] = (cbThisPkg >> 4) & 0xff;
359 pbPkgLength[2] = (cbThisPkg >> 12) & 0xff;
360 }
361 else if (cbThisPkg + 4 < RT_BIT_32(28))
362 {
363 cbThisPkg += 4;
364 pbPkgLength[0] = (3 << 6) | (cbThisPkg & 0xf);
365 pbPkgLength[1] = (cbThisPkg >> 4) & 0xff;
366 pbPkgLength[2] = (cbThisPkg >> 12) & 0xff;
367 pbPkgLength[3] = (cbThisPkg >> 20) & 0xff;
368 }
369 else
370 return VERR_BUFFER_OVERFLOW;
371
372 /* Update the size of the outer package. */
373 pThis->paPkgStack[pThis->idxPkgStackElem].cbPkg += cbThisPkg;
374
375 return VINF_SUCCESS;
376}
377
378
379/**
380 * Appends the given byte to the ACPI table, updating the package length of the current package.
381 *
382 * @param pThis The ACPI table instance.
383 * @param bData The byte data to append.
384 */
385DECLINLINE(void) rtAcpiTblAppendByte(PRTACPITBLINT pThis, uint8_t bData)
386{
387 uint8_t *pb = rtAcpiTblBufEnsureSpace(pThis, sizeof(bData));
388 if (pb)
389 {
390 *pb = bData;
391 rtAcpiTblUpdatePkgLength(pThis, sizeof(bData));
392 }
393}
394
395
396/**
397 * Appends the given date to the ACPI table, updating the package length of the current package.
398 *
399 * @param pThis The ACPI table instance.
400 * @param pvData The data to append.
401 * @param cbData Size of the data in bytes.
402 */
403DECLINLINE(void) rtAcpiTblAppendData(PRTACPITBLINT pThis, const void *pvData, uint32_t cbData)
404{
405 uint8_t *pb = rtAcpiTblBufEnsureSpace(pThis, cbData);
406 if (pb)
407 {
408 memcpy(pb, pvData, cbData);
409 rtAcpiTblUpdatePkgLength(pThis, cbData);
410 }
411}
412
413
414/**
415 * Appends the given namestring to the ACPI table, updating the package length of the current package
416 * and padding the name with _ if too short.
417 *
418 * @param pThis The ACPI table instance.
419 * @param pszName The name to append, maximum is 4 bytes (or 5 if \\ is the first character).
420 */
421DECLINLINE(void) rtAcpiTblAppendNameString(PRTACPITBLINT pThis, const char *pszName)
422{
423 uint32_t cbName = *pszName == '\\' ? 5 : 4;
424 uint8_t *pb = rtAcpiTblBufEnsureSpace(pThis, cbName);
425 if (pb)
426 {
427 rtAcpiTblCopyStringPadWith(pb, cbName, pszName, '_');
428 rtAcpiTblUpdatePkgLength(pThis, cbName);
429 }
430}
431
432
433RTDECL(uint8_t) RTAcpiChecksumGenerate(const void *pvData, size_t cbData)
434{
435 uint8_t const *pbSrc = (uint8_t const *)pvData;
436 uint8_t bSum = 0;
437 for (size_t i = 0; i < cbData; ++i)
438 bSum += pbSrc[i];
439
440 return -bSum;
441}
442
443
444RTDECL(void) RTAcpiTblHdrChecksumGenerate(PACPITBLHDR pTbl, size_t cbTbl)
445{
446 pTbl->bChkSum = 0;
447 pTbl->bChkSum = RTAcpiChecksumGenerate(pTbl, cbTbl);
448}
449
450
451RTDECL(int) RTAcpiTblCreate(PRTACPITBL phAcpiTbl, uint32_t u32TblSig, uint8_t bRevision, const char *pszOemId,
452 const char *pszOemTblId, uint32_t u32OemRevision, const char *pszCreatorId,
453 uint32_t u32CreatorRevision)
454{
455 AssertPtrReturn(phAcpiTbl, VERR_INVALID_POINTER);
456 AssertPtrReturn(pszOemId, VERR_INVALID_POINTER);
457 AssertPtrReturn(pszOemTblId, VERR_INVALID_POINTER);
458 AssertReturn(strlen(pszOemId) <= 6, VERR_INVALID_PARAMETER);
459 AssertReturn(strlen(pszOemTblId) <= 8, VERR_INVALID_PARAMETER);
460 AssertReturn(!pszCreatorId || strlen(pszCreatorId) <= 4, VERR_INVALID_PARAMETER);
461
462 PRTACPITBLINT pThis = (PRTACPITBLINT)RTMemAllocZ(sizeof(*pThis));
463 if (pThis)
464 {
465 pThis->pbTblBuf = (uint8_t *)RTMemAlloc(_4K);
466 if (pThis->pbTblBuf)
467 {
468 pThis->pHdr = (PACPITBLHDR)pThis->pbTblBuf;
469 pThis->offTblBuf = sizeof(*pThis->pHdr);
470 pThis->cbTblBuf = _4K;
471 pThis->fFinalized = false;
472 pThis->rcErr = VINF_SUCCESS;
473 pThis->paPkgStack = NULL;
474 pThis->cPkgStackElems = 0;
475 pThis->idxPkgStackElem = 0;
476
477 /* Add the root stack element for the table, aka DefinitionBlock() in ASL. */
478 uint32_t const cPkgElemsInitial = 8;
479 pThis->paPkgStack = (PRTACPITBLSTACKELEM)RTMemAlloc(cPkgElemsInitial * sizeof(*pThis->paPkgStack));
480 if (pThis->paPkgStack)
481 {
482 pThis->cPkgStackElems = cPkgElemsInitial;
483
484 PRTACPITBLSTACKELEM pStackElem = &pThis->paPkgStack[pThis->idxPkgStackElem];
485 pStackElem->pbPkgLength = pThis->pbTblBuf; /* Starts with the header. */
486 pStackElem->cbPkg = sizeof(*pThis->pHdr);
487 pStackElem->bOp = UINT8_MAX;
488
489 /* Init the table header with static things. */
490 pThis->pHdr->u32Signature = u32TblSig;
491 pThis->pHdr->bRevision = bRevision;
492 pThis->pHdr->u32OemRevision = RT_H2LE_U32(u32OemRevision);
493 pThis->pHdr->u32CreatorRevision = RT_H2LE_U32(u32CreatorRevision);
494
495 rtAcpiTblCopyStringPadWith(&pThis->pHdr->abOemId[0], sizeof(pThis->pHdr->abOemId), pszOemId, ' ');
496 rtAcpiTblCopyStringPadWith(&pThis->pHdr->abOemTblId[0], sizeof(pThis->pHdr->abOemTblId), pszOemTblId, ' ');
497 rtAcpiTblCopyStringPadWith(&pThis->pHdr->abCreatorId[0], sizeof(pThis->pHdr->abCreatorId),
498 pszCreatorId ? pszCreatorId : "IPRT", ' ');
499
500 *phAcpiTbl = pThis;
501 return VINF_SUCCESS;
502 }
503
504 RTMemFree(pThis->pbTblBuf);
505 }
506
507 RTMemFree(pThis);
508 }
509
510 return VERR_NO_MEMORY;
511}
512
513
514RTDECL(void) RTAcpiTblDestroy(RTACPITBL hAcpiTbl)
515{
516 PRTACPITBLINT pThis = hAcpiTbl;
517 AssertPtrReturnVoid(pThis);
518
519 RTMemFree(pThis->paPkgStack);
520 RTMemFree(pThis->pbTblBuf);
521 pThis->pHdr = NULL;
522 pThis->pbTblBuf = NULL;
523 pThis->cbTblBuf = 0;
524 pThis->offTblBuf = 0;
525 pThis->paPkgStack = NULL;
526 pThis->cPkgStackElems = 0;
527 pThis->idxPkgStackElem = 0;
528 RTMemFree(pThis);
529}
530
531
532RTDECL(int) RTAcpiTblFinalize(RTACPITBL hAcpiTbl)
533{
534 PRTACPITBLINT pThis = hAcpiTbl;
535 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
536 AssertRCReturn(pThis->rcErr, pThis->rcErr);
537 AssertReturn(!pThis->fFinalized, VERR_INVALID_PARAMETER);
538 AssertReturn(pThis->idxPkgStackElem == 0, VERR_INVALID_STATE); /** @todo Better status code. */
539 AssertReturn(pThis->paPkgStack[0].bOp == UINT8_MAX, VERR_INVALID_STATE);
540
541 pThis->pHdr->cbTbl = RT_H2LE_U32(pThis->paPkgStack[0].cbPkg);
542 RTAcpiTblHdrChecksumGenerate(pThis->pHdr, pThis->paPkgStack[0].cbPkg);
543
544 pThis->fFinalized = true;
545 return VINF_SUCCESS;
546}
547
548
549RTDECL(uint32_t) RTAcpiTblGetSize(RTACPITBL hAcpiTbl)
550{
551 PRTACPITBLINT pThis = hAcpiTbl;
552 AssertPtrReturn(pThis, 0);
553 AssertRCReturn(pThis->rcErr, 0);
554 AssertReturn(pThis->fFinalized, 0);
555
556 return pThis->paPkgStack[0].cbPkg;
557}
558
559
560RTDECL(int) RTAcpiTblDumpToVfsIoStrm(RTACPITBL hAcpiTbl, RTVFSIOSTREAM hVfsIos)
561{
562 PRTACPITBLINT pThis = hAcpiTbl;
563 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
564 AssertRCReturn(pThis->rcErr, 0);
565
566 return RTVfsIoStrmWrite(hVfsIos, pThis->pbTblBuf, pThis->paPkgStack[0].cbPkg,
567 true /*fBlocking*/, NULL /*pcbWritten*/);
568}
569
570
571RTDECL(int) RTAcpiTblDumpToFile(RTACPITBL hAcpiTbl, const char *pszFilename)
572{
573 RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
574 int rc = RTVfsChainOpenIoStream(pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE,
575 &hVfsIos, NULL /*poffError*/, NULL);
576 if (RT_FAILURE(rc))
577 return rc;
578
579 rc = RTAcpiTblDumpToVfsIoStrm(hAcpiTbl, hVfsIos);
580 RTVfsIoStrmRelease(hVfsIos);
581 return rc;
582}
583
584
585RTDECL(int) RTAcpiTblScopeFinalize(RTACPITBL hAcpiTbl)
586{
587 PRTACPITBLINT pThis = hAcpiTbl;
588 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
589
590 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_SCOPE);
591}
592
593
594RTDECL(int) RTAcpiTblScopeStart(RTACPITBL hAcpiTbl, const char *pszName)
595{
596 PRTACPITBLINT pThis = hAcpiTbl;
597 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
598
599 rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_SCOPE);
600 rtAcpiTblAppendNameString(pThis, pszName);
601 return pThis->rcErr;
602}
603
604
605RTDECL(int) RTAcpiTblPackageStart(RTACPITBL hAcpiTbl, uint8_t cElements)
606{
607 PRTACPITBLINT pThis = hAcpiTbl;
608 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
609
610 rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_PACKAGE);
611 rtAcpiTblAppendByte(pThis, cElements);
612 return pThis->rcErr;
613}
614
615
616RTDECL(int) RTAcpiTblPackageFinalize(RTACPITBL hAcpiTbl)
617{
618 PRTACPITBLINT pThis = hAcpiTbl;
619 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
620
621 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_PACKAGE);
622}
623
624
625RTDECL(int) RTAcpiTblDeviceStart(RTACPITBL hAcpiTbl, const char *pszName)
626{
627 PRTACPITBLINT pThis = hAcpiTbl;
628 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
629
630 rtAcpiTblPkgStartExt(pThis, ACPI_AML_BYTE_CODE_EXT_OP_DEVICE);
631 rtAcpiTblAppendNameString(pThis, pszName);
632 return pThis->rcErr;
633}
634
635
636RTDECL(int) RTAcpiTblDeviceStartF(RTACPITBL hAcpiTbl, const char *pszNameFmt, ...)
637{
638 va_list va;
639 va_start(va, pszNameFmt);
640 int rc = RTAcpiTblDeviceStartV(hAcpiTbl, pszNameFmt, va);
641 va_end(va);
642 return rc;
643}
644
645
646RTDECL(int) RTAcpiTblDeviceStartV(RTACPITBL hAcpiTbl, const char *pszNameFmt, va_list va)
647{
648 char szName[5];
649 ssize_t cch = RTStrPrintf2V(&szName[0], sizeof(szName), pszNameFmt, va);
650 if (cch <= 0)
651 return VERR_BUFFER_OVERFLOW;
652
653 return RTAcpiTblDeviceStart(hAcpiTbl, &szName[0]);
654}
655
656
657RTDECL(int) RTAcpiTblDeviceFinalize(RTACPITBL hAcpiTbl)
658{
659 PRTACPITBLINT pThis = hAcpiTbl;
660 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
661
662 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_EXT_OP_DEVICE);
663}
664
665
666RTDECL(int) RTAcpiTblMethodStart(RTACPITBL hAcpiTbl, const char *pszName, uint8_t cArgs, uint32_t fFlags, uint8_t uSyncLvl)
667{
668 PRTACPITBLINT pThis = hAcpiTbl;
669 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
670 AssertReturn(cArgs < 8, VERR_INVALID_PARAMETER);
671 AssertReturn(uSyncLvl < 0x10, VERR_INVALID_PARAMETER);
672
673 rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_METHOD);
674 rtAcpiTblAppendNameString(pThis, pszName);
675
676 uint8_t bFlags = cArgs;
677 bFlags |= fFlags & RTACPI_METHOD_F_SERIALIZED ? RT_BIT(3) : 0;
678 bFlags |= uSyncLvl << 4;
679
680 rtAcpiTblAppendByte(pThis, bFlags);
681 return pThis->rcErr;
682}
683
684
685RTDECL(int) RTAcpiTblMethodFinalize(RTACPITBL hAcpiTbl)
686{
687 PRTACPITBLINT pThis = hAcpiTbl;
688 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
689
690 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_METHOD);
691}
692
693
694RTDECL(int) RTAcpiTblNameAppend(RTACPITBL hAcpiTbl, const char *pszName)
695{
696 PRTACPITBLINT pThis = hAcpiTbl;
697 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
698
699 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_NAME);
700 rtAcpiTblAppendNameString(pThis, pszName);
701 return pThis->rcErr;
702}
703
704
705RTDECL(int) RTAcpiTblStringAppend(RTACPITBL hAcpiTbl, const char *psz)
706{
707 PRTACPITBLINT pThis = hAcpiTbl;
708 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
709
710 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_STRING);
711 rtAcpiTblAppendData(pThis, psz, (uint32_t)strlen(psz) + 1);
712 return pThis->rcErr;
713}
714
715
716RTDECL(int) RTAcpiTblIntegerAppend(RTACPITBL hAcpiTbl, uint64_t u64)
717{
718 PRTACPITBLINT pThis = hAcpiTbl;
719 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
720
721 if (!u64)
722 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_ZERO);
723 else if (u64 == 1)
724 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_ONE);
725 else if (u64 <= UINT8_MAX)
726 {
727 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_BYTE);
728 rtAcpiTblAppendByte(pThis, (uint8_t)u64);
729 }
730 else if (u64 <= UINT16_MAX)
731 {
732 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_WORD);
733 rtAcpiTblAppendByte(pThis, (uint8_t)u64);
734 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 8));
735 }
736 else if (u64 <= UINT32_MAX)
737 {
738 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_DWORD);
739 rtAcpiTblAppendByte(pThis, (uint8_t)u64);
740 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 8));
741 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 16));
742 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 24));
743 }
744 else
745 {
746 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_PREFIX_QWORD);
747 rtAcpiTblAppendByte(pThis, (uint8_t)u64);
748 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 8));
749 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 16));
750 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 24));
751 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 32));
752 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 40));
753 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 48));
754 rtAcpiTblAppendByte(pThis, (uint8_t)(u64 >> 56));
755 }
756 return pThis->rcErr;
757}
758
759
760RTDECL(int) RTAcpiTblBufferAppend(RTACPITBL hAcpiTbl, const void *pvBuf, size_t cbBuf)
761{
762 PRTACPITBLINT pThis = hAcpiTbl;
763 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
764 AssertReturn(!cbBuf || RT_VALID_PTR(pvBuf), VERR_INVALID_PARAMETER);
765 AssertReturn(cbBuf <= UINT32_MAX, VERR_BUFFER_OVERFLOW);
766
767 rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_BUFFER);
768 RTAcpiTblIntegerAppend(hAcpiTbl, cbBuf);
769 if (pvBuf)
770 rtAcpiTblAppendData(pThis, pvBuf, (uint32_t)cbBuf);
771 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_BUFFER);
772}
773
774
775RTDECL(int) RTAcpiTblResourceAppend(RTACPITBL hAcpiTbl, RTACPIRES hAcpiRes)
776{
777 PRTACPITBLINT pThis = hAcpiTbl;
778 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
779 AssertRCReturn(pThis->rcErr, pThis->rcErr);
780
781 const void *pvRes = NULL;
782 size_t cbRes = 0;
783 int rc = RTAcpiResourceQueryBuffer(hAcpiRes, &pvRes, &cbRes);
784 if (RT_SUCCESS(rc))
785 rc = RTAcpiTblBufferAppend(pThis, pvRes, cbRes);
786
787 return rc;
788}
789
790
791RTDECL(int) RTAcpiTblStmtSimpleAppend(RTACPITBL hAcpiTbl, RTACPISTMT enmStmt)
792{
793 PRTACPITBLINT pThis = hAcpiTbl;
794 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
795
796 uint8_t bOp;
797 switch (enmStmt)
798 {
799 case kAcpiStmt_Return: bOp = ACPI_AML_BYTE_CODE_OP_RETURN; break;
800 case kAcpiStmt_Breakpoint: bOp = ACPI_AML_BYTE_CODE_OP_BREAK_POINT; break;
801 case kAcpiStmt_Nop: bOp = ACPI_AML_BYTE_CODE_OP_NOOP; break;
802 case kAcpiStmt_Break: bOp = ACPI_AML_BYTE_CODE_OP_BREAK; break;
803 case kAcpiStmt_Continue: bOp = ACPI_AML_BYTE_CODE_OP_CONTINUE; break;
804 default:
805 AssertFailedReturn(VERR_INVALID_PARAMETER);
806 }
807 rtAcpiTblAppendByte(pThis, bOp);
808 return pThis->rcErr;
809}
810
811
812RTDECL(int) RTAcpiTblIfStart(RTACPITBL hAcpiTbl)
813{
814 PRTACPITBLINT pThis = hAcpiTbl;
815 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
816
817 rtAcpiTblPkgStart(pThis, ACPI_AML_BYTE_CODE_OP_IF);
818 return pThis->rcErr;
819}
820
821
822RTDECL(int) RTAcpiTblIfFinalize(RTACPITBL hAcpiTbl)
823{
824 PRTACPITBLINT pThis = hAcpiTbl;
825 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
826
827 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_IF);
828}
829
830
831RTDECL(int) RTAcpiTblElseStart(RTACPITBL hAcpiTbl)
832{
833 PRTACPITBLINT pThis = hAcpiTbl;
834 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
835
836 /* Makes only sense inside an IfOp package. */
837 AssertReturn(pThis->paPkgStack[pThis->idxPkgStackElem].bOp == ACPI_AML_BYTE_CODE_OP_IF, VERR_INVALID_STATE);
838
839 rtAcpiTblPkgStartExt(pThis, ACPI_AML_BYTE_CODE_OP_ELSE);
840 return pThis->rcErr;
841}
842
843
844RTDECL(int) RTAcpiTblElseFinalize(RTACPITBL hAcpiTbl)
845{
846 PRTACPITBLINT pThis = hAcpiTbl;
847 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
848
849 return rtAcpiTblPkgFinish(pThis, ACPI_AML_BYTE_CODE_OP_ELSE);
850}
851
852
853RTDECL(int) RTAcpiTblBinaryOpAppend(RTACPITBL hAcpiTbl, RTACPIBINARYOP enmBinaryOp)
854{
855 PRTACPITBLINT pThis = hAcpiTbl;
856 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
857
858 uint8_t bOp;
859 switch (enmBinaryOp)
860 {
861 case kAcpiBinaryOp_LAnd: bOp = ACPI_AML_BYTE_CODE_OP_LAND; break;
862 case kAcpiBinaryOp_LEqual: bOp = ACPI_AML_BYTE_CODE_OP_LEQUAL; break;
863 case kAcpiBinaryOp_LGreater: bOp = ACPI_AML_BYTE_CODE_OP_LGREATER; break;
864 case kAcpiBinaryOp_LLess: bOp = ACPI_AML_BYTE_CODE_OP_LLESS; break;
865 case kAcpiBinaryOp_LGreaterEqual:
866 case kAcpiBinaryOp_LLessEqual:
867 case kAcpiBinaryOp_LNotEqual:
868 bOp = ACPI_AML_BYTE_CODE_OP_LNOT;
869 break;
870 default:
871 AssertFailedReturn(VERR_INVALID_PARAMETER);
872 }
873 rtAcpiTblAppendByte(pThis, bOp);
874 switch (enmBinaryOp)
875 {
876 case kAcpiBinaryOp_LGreaterEqual: bOp = ACPI_AML_BYTE_CODE_OP_LLESS; break;
877 case kAcpiBinaryOp_LLessEqual: bOp = ACPI_AML_BYTE_CODE_OP_LGREATER; break;
878 case kAcpiBinaryOp_LNotEqual: bOp = ACPI_AML_BYTE_CODE_OP_LEQUAL; break;
879 default:
880 bOp = 0x00;
881 }
882 if (bOp != 0x00)
883 rtAcpiTblAppendByte(pThis, bOp);
884 return pThis->rcErr;
885}
886
887
888RTDECL(int) RTAcpiTblArgOpAppend(RTACPITBL hAcpiTbl, uint8_t idArg)
889{
890 PRTACPITBLINT pThis = hAcpiTbl;
891 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
892 AssertReturn(idArg <= 6, VERR_INVALID_PARAMETER);
893
894 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_ARG_0 + idArg);
895 return pThis->rcErr;
896}
897
898
899RTDECL(int) RTAcpiTblLocalOpAppend(RTACPITBL hAcpiTbl, uint8_t idLocal)
900{
901 PRTACPITBLINT pThis = hAcpiTbl;
902 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
903 AssertReturn(idLocal <= 7, VERR_INVALID_PARAMETER);
904
905 rtAcpiTblAppendByte(pThis, ACPI_AML_BYTE_CODE_OP_LOCAL_0 + idLocal);
906 return pThis->rcErr;
907}
908
909
910RTDECL(int) RTAcpiTblUuidAppend(RTACPITBL hAcpiTbl, PCRTUUID pUuid)
911{
912 /* UUIDs are stored as a buffer object. */
913 /** @todo Needs conversion on big endian machines. */
914 return RTAcpiTblBufferAppend(hAcpiTbl, &pUuid->au8[0], sizeof(*pUuid));
915}
916
917
918RTDECL(int) RTAcpiTblUuidAppendFromStr(RTACPITBL hAcpiTbl, const char *pszUuid)
919{
920 PRTACPITBLINT pThis = hAcpiTbl;
921 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
922
923 RTUUID Uuid;
924 pThis->rcErr = RTUuidFromStr(&Uuid, pszUuid);
925 if (RT_SUCCESS(pThis->rcErr))
926 return RTAcpiTblUuidAppend(pThis, &Uuid);
927
928 return pThis->rcErr;
929}
930
931
932/**
933 * Ensures there is at least the given amount of space in the given ACPI resource.
934 *
935 * @returns Pointer to the free buffer space or NULL if out of memory.
936 * @param pThis The ACPI resource instance.
937 * @param cbReq Number of free bytes required.
938 */
939static uint8_t *rtAcpiResBufEnsureSpace(PRTACPIRESINT pThis, uint32_t cbReq)
940{
941 if (RT_LIKELY(pThis->cbResBuf - pThis->offResBuf >= cbReq))
942 {
943 uint8_t *pb = &pThis->pbResBuf[pThis->offResBuf];
944 pThis->offResBuf += cbReq;
945 return pb;
946 }
947
948 size_t const cbNew = RT_ALIGN_Z(pThis->cbResBuf + cbReq, _4K);
949 uint8_t *pbNew = (uint8_t *)RTMemRealloc(pThis->pbResBuf, cbNew);
950 if (RT_UNLIKELY(!pbNew))
951 {
952 pThis->rcErr = VERR_NO_MEMORY;
953 return NULL;
954 }
955
956 pThis->pbResBuf = pbNew;
957 pThis->cbResBuf = cbNew;
958
959 uint8_t *pb = &pThis->pbResBuf[pThis->offResBuf];
960 pThis->offResBuf += cbReq;
961 return pb;
962}
963
964
965/**
966 * Encodes an ACPI 16-bit integer in the given byte buffer.
967 *
968 * @returns Pointer to after the encoded integer.
969 * @param pb Where to encode the integer into.
970 * @param u16 The 16-bit unsigned integere to encode.
971 */
972DECLINLINE(uint8_t *) rtAcpiResEncode16BitInteger(uint8_t *pb, uint16_t u16)
973{
974 *pb++ = (uint8_t)u16;
975 *pb++ = (uint8_t)(u16 >> 8);
976 return pb;
977}
978
979
980/**
981 * Encodes an ACPI 32-bit integer in the given byte buffer.
982 *
983 * @returns Pointer to after the encoded integer.
984 * @param pb Where to encode the integer into.
985 * @param u32 The 32-bit unsigned integere to encode.
986 */
987DECLINLINE(uint8_t *) rtAcpiResEncode32BitInteger(uint8_t *pb, uint32_t u32)
988{
989 *pb++ = (uint8_t)u32;
990 *pb++ = (uint8_t)(u32 >> 8);
991 *pb++ = (uint8_t)(u32 >> 16);
992 *pb++ = (uint8_t)(u32 >> 24);
993 return pb;
994}
995
996/**
997 * Encodes an ACPI 64-bit integer in the given byte buffer.
998 *
999 * @returns Pointer to after the encoded integer.
1000 * @param pb Where to encode the integer into.
1001 * @param u64 The 64-bit unsigned integere to encode.
1002 */
1003
1004DECLINLINE(uint8_t *) rtAcpiResEncode64BitInteger(uint8_t *pb, uint64_t u64)
1005{
1006 *pb++ = (uint8_t)u64;
1007 *pb++ = (uint8_t)(u64 >> 8);
1008 *pb++ = (uint8_t)(u64 >> 16);
1009 *pb++ = (uint8_t)(u64 >> 24);
1010 *pb++ = (uint8_t)(u64 >> 32);
1011 *pb++ = (uint8_t)(u64 >> 40);
1012 *pb++ = (uint8_t)(u64 >> 48);
1013 *pb++ = (uint8_t)(u64 >> 56);
1014 return pb;
1015}
1016
1017
1018RTDECL(int) RTAcpiResourceCreate(PRTACPIRES phAcpiRes)
1019{
1020 AssertPtrReturn(phAcpiRes, VERR_INVALID_POINTER);
1021
1022 PRTACPIRESINT pThis = (PRTACPIRESINT)RTMemAllocZ(sizeof(*pThis));
1023 if (pThis)
1024 {
1025 pThis->pbResBuf = (uint8_t *)RTMemAlloc(64);
1026 if (pThis->pbResBuf)
1027 {
1028 pThis->offResBuf = 0;
1029 pThis->cbResBuf = 64;
1030 pThis->fSealed = false;
1031 pThis->rcErr = VINF_SUCCESS;
1032
1033 *phAcpiRes = pThis;
1034 return VINF_SUCCESS;
1035 }
1036
1037 RTMemFree(pThis);
1038 }
1039
1040 return VERR_NO_MEMORY;
1041}
1042
1043
1044RTDECL(void) RTAcpiResourceDestroy(RTACPIRES hAcpiRes)
1045{
1046 PRTACPIRESINT pThis = hAcpiRes;
1047 AssertPtrReturnVoid(pThis);
1048
1049 RTMemFree(pThis->pbResBuf);
1050 pThis->pbResBuf = NULL;
1051 pThis->cbResBuf = 0;
1052 pThis->offResBuf = 0;
1053 RTMemFree(pThis);
1054}
1055
1056
1057RTDECL(void) RTAcpiResourceReset(RTACPIRES hAcpiRes)
1058{
1059 PRTACPIRESINT pThis = hAcpiRes;
1060 AssertPtrReturnVoid(pThis);
1061
1062 pThis->offResBuf = 0;
1063 pThis->fSealed = false;
1064 pThis->rcErr = VINF_SUCCESS;
1065}
1066
1067
1068RTDECL(int) RTAcpiResourceSeal(RTACPIRES hAcpiRes)
1069{
1070 PRTACPIRESINT pThis = hAcpiRes;
1071 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1072 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1073 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1074
1075 /* Add the end tag. */
1076 uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 2);
1077 if (!pb)
1078 return VERR_NO_MEMORY;
1079
1080 *pb++ = ACPI_RSRCS_TAG_END;
1081 /*
1082 * Generate checksum, we could just write 0 here which will be treated as checksum operation succeeded,
1083 * but having this might catch some bugs.
1084 *
1085 * Checksum algorithm is the same as with the ACPI tables.
1086 */
1087 *pb = RTAcpiChecksumGenerate(pThis->pbResBuf, pThis->offResBuf - 1); /* Exclude the checksum field. */
1088
1089 pThis->fSealed = true;
1090 return VINF_SUCCESS;
1091}
1092
1093
1094RTDECL(int) RTAcpiResourceQueryBuffer(RTACPIRES hAcpiRes, const void **ppvRes, size_t *pcbRes)
1095{
1096 PRTACPIRESINT pThis = hAcpiRes;
1097 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1098 AssertReturn(pThis->fSealed, VERR_INVALID_STATE);
1099 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1100
1101 *ppvRes = pThis->pbResBuf;
1102 *pcbRes = pThis->offResBuf;
1103 return VINF_SUCCESS;
1104}
1105
1106
1107RTDECL(int) RTAcpiResourceAdd32BitFixedMemoryRange(RTACPIRES hAcpiRes, uint32_t u32AddrBase, uint32_t cbRange,
1108 bool fRw)
1109{
1110 PRTACPIRESINT pThis = hAcpiRes;
1111 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1112 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1113 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1114
1115 uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 12);
1116 if (!pb)
1117 return VERR_NO_MEMORY;
1118
1119 pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_32BIT_FIXED_MEMORY_RANGE; /* Tag */
1120 pb[1] = 9; /* Length[7:0] */
1121 pb[2] = 0; /* Length[15:8] */
1122 pb[3] = fRw ? 1 : 0; /* Information */
1123 rtAcpiResEncode32BitInteger(&pb[4], u32AddrBase);
1124 rtAcpiResEncode32BitInteger(&pb[8], cbRange);
1125 return VINF_SUCCESS;
1126}
1127
1128
1129RTDECL(int) RTAcpiResourceAddExtendedInterrupt(RTACPIRES hAcpiRes, bool fConsumer, bool fEdgeTriggered, bool fActiveLow, bool fShared,
1130 bool fWakeCapable, uint8_t cIntrs, uint32_t *pau32Intrs)
1131{
1132 PRTACPIRESINT pThis = hAcpiRes;
1133 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1134 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1135 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1136
1137 uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 2 + cIntrs * sizeof(uint32_t));
1138 if (!pb)
1139 return VERR_NO_MEMORY;
1140
1141 pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_EXTENDED_INTERRUPT; /* Tag */
1142 rtAcpiResEncode16BitInteger(&pb[1], 2 + cIntrs * sizeof(uint32_t)); /* Length[15:0] */
1143 pb[3] = (fConsumer ? ACPI_RSRCS_EXT_INTR_VEC_F_CONSUMER : ACPI_RSRCS_EXT_INTR_VEC_F_PRODUCER)
1144 | (fEdgeTriggered ? ACPI_RSRCS_EXT_INTR_VEC_F_EDGE_TRIGGERED : ACPI_RSRCS_EXT_INTR_VEC_F_LEVEL_TRIGGERED)
1145 | (fActiveLow ? ACPI_RSRCS_EXT_INTR_VEC_F_ACTIVE_LOW : ACPI_RSRCS_EXT_INTR_VEC_F_ACTIVE_HIGH)
1146 | (fShared ? ACPI_RSRCS_EXT_INTR_VEC_F_SHARED : ACPI_RSRCS_EXT_INTR_VEC_F_EXCLUSIVE)
1147 | (fWakeCapable ? ACPI_RSRCS_EXT_INTR_VEC_F_WAKE_CAP : ACPI_RSRCS_EXT_INTR_VEC_F_NOT_WAKE_CAP);
1148 pb[4] = cIntrs;
1149 pb = &pb[5];
1150 for (uint32_t i = 0; i < cIntrs; i++)
1151 pb = rtAcpiResEncode32BitInteger(pb, pau32Intrs[i]);
1152
1153 return VINF_SUCCESS;
1154}
1155
1156
1157/**
1158 * Common worker for encoding a new quad word (64-bit) address range.
1159 *
1160 * @returns IPRT status code
1161 * @retval VERR_NO_MEMORY if not enough memory could be reserved in the ACPI resource descriptor.
1162 * @param pThis The ACPI resource instance.
1163 * @param bType The ACPI address range type.
1164 * @param fAddrSpace Combination of RTACPI_RESOURCE_ADDR_RANGE_F_XXX.
1165 * @param fType The range flags returned from rtAcpiResourceMemoryRangeToTypeFlags().
1166 * @param u64AddrMin The start address of the memory range.
1167 * @param u64AddrMax Last valid address of the range.
1168 * @param u64OffTrans Translation offset being applied to the address (for a PCIe bridge or IOMMU for example).
1169 * @param u64Granularity The access granularity of the range in bytes.
1170 * @param u64Length Length of the memory range in bytes.
1171 */
1172static int rtAcpiResourceAddQWordAddressRange(PRTACPIRESINT pThis, uint8_t bType, uint32_t fAddrSpace, uint8_t fType,
1173 uint64_t u64AddrMin, uint64_t u64AddrMax, uint64_t u64OffTrans,
1174 uint64_t u64Granularity, uint64_t u64Length)
1175{
1176 uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 43);
1177 if (!pb)
1178 return VERR_NO_MEMORY;
1179
1180 pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_QWORD_ADDR_SPACE; /* Tag */
1181 pb[1] = 43; /* Length[7:0] */
1182 pb[2] = 0; /* Length[15:8] */
1183 pb[3] = bType;
1184 pb[4] = (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_DECODE_TYPE_SUB ? ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_SUB : ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_POS)
1185 | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MIN_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_CHANGEABLE)
1186 | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MAX_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_CHANGEABLE);
1187 pb[5] = fType;
1188
1189 pb = rtAcpiResEncode64BitInteger(&pb[6], u64Granularity);
1190 pb = rtAcpiResEncode64BitInteger(pb, u64AddrMin);
1191 pb = rtAcpiResEncode64BitInteger(pb, u64AddrMax);
1192 pb = rtAcpiResEncode64BitInteger(pb, u64OffTrans);
1193 rtAcpiResEncode64BitInteger(pb, u64Length);
1194 return VINF_SUCCESS;
1195}
1196
1197
1198/**
1199 * Common worker for encoding a new double word (32-bit) address range.
1200 *
1201 * @returns IPRT status code
1202 * @retval VERR_NO_MEMORY if not enough memory could be reserved in the ACPI resource descriptor.
1203 * @param pThis The ACPI resource instance.
1204 * @param bType The ACPI address range type.
1205 * @param fAddrSpace Combination of RTACPI_RESOURCE_ADDR_RANGE_F_XXX.
1206 * @param fType The range flags returned from rtAcpiResourceMemoryRangeToTypeFlags().
1207 * @param u32AddrMin The start address of the memory range.
1208 * @param u32AddrMax Last valid address of the range.
1209 * @param u32OffTrans Translation offset being applied to the address (for a PCIe bridge or IOMMU for example).
1210 * @param u32Granularity The access granularity of the range in bytes.
1211 * @param u32Length Length of the memory range in bytes.
1212 */
1213static int rtAcpiResourceAddDWordAddressRange(PRTACPIRESINT pThis, uint8_t bType, uint32_t fAddrSpace, uint8_t fType,
1214 uint32_t u32AddrMin, uint32_t u32AddrMax, uint32_t u32OffTrans,
1215 uint32_t u32Granularity, uint32_t u32Length)
1216{
1217 uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 23);
1218 if (!pb)
1219 return VERR_NO_MEMORY;
1220
1221 pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_DWORD_ADDR_SPACE; /* Tag */
1222 pb[1] = 23; /* Length[7:0] */
1223 pb[2] = 0; /* Length[15:8] */
1224 pb[3] = bType;
1225 pb[4] = (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_DECODE_TYPE_SUB ? ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_SUB : ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_POS)
1226 | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MIN_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_CHANGEABLE)
1227 | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MAX_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_CHANGEABLE);
1228 pb[5] = fType;
1229
1230 pb = rtAcpiResEncode32BitInteger(&pb[6], u32Granularity);
1231 pb = rtAcpiResEncode32BitInteger(pb, u32AddrMin);
1232 pb = rtAcpiResEncode32BitInteger(pb, u32AddrMax);
1233 pb = rtAcpiResEncode32BitInteger(pb, u32OffTrans);
1234 rtAcpiResEncode32BitInteger(pb, u32Length);
1235 return VINF_SUCCESS;
1236}
1237
1238
1239/**
1240 * Converts the given cacheability, range type and R/W flag to the ACPI resource flags.
1241 *
1242 * @returns Converted ACPI resource flags.
1243 * @param enmCacheability The cacheability enum to convert.
1244 * @param enmType THe memory range type enum to convert.
1245 * @param fRw The read/write flag.
1246 */
1247DECLINLINE(uint8_t) rtAcpiResourceMemoryRangeToTypeFlags(RTACPIRESMEMRANGECACHEABILITY enmCacheability, RTACPIRESMEMRANGETYPE enmType,
1248 bool fRw)
1249{
1250 uint8_t fType = fRw ? ACPI_RSRCS_ADDR_SPACE_MEM_F_RW : ACPI_RSRCS_ADDR_SPACE_MEM_F_RO;
1251
1252 switch (enmCacheability)
1253 {
1254 case kAcpiResMemRangeCacheability_NonCacheable:
1255 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_NON_CACHEABLE;
1256 break;
1257 case kAcpiResMemRangeCacheability_Cacheable:
1258 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_CACHEABLE;
1259 break;
1260 case kAcpiResMemRangeCacheability_CacheableWriteCombining:
1261 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_CACHEABLE_WR_COMB;
1262 break;
1263 case kAcpiResMemRangeCacheability_CacheablePrefetchable:
1264 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_CACHE_CACHEABLE_PREFETCHABLE;
1265 break;
1266 case kAcpiResMemRangeCacheability_Invalid:
1267 default:
1268 AssertFailedReturn(0);
1269 }
1270
1271 switch (enmType)
1272 {
1273 case kAcpiResMemType_Memory:
1274 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_MEMORY;
1275 break;
1276 case kAcpiResMemType_Reserved:
1277 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_RESERVED;
1278 break;
1279 case kAcpiResMemType_Acpi:
1280 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_ACPI;
1281 break;
1282 case kAcpiResMemType_Nvs:
1283 fType |= ACPI_RSRCS_ADDR_SPACE_MEM_F_ATTR_NVS;
1284 break;
1285 case kAcpiResMemType_Invalid:
1286 default:
1287 AssertFailedReturn(0);
1288 }
1289
1290 return fType;
1291}
1292
1293
1294RTDECL(int) RTAcpiResourceAddQWordMemoryRange(RTACPIRES hAcpiRes, RTACPIRESMEMRANGECACHEABILITY enmCacheability,
1295 RTACPIRESMEMRANGETYPE enmType, bool fRw, uint32_t fAddrSpace,
1296 uint64_t u64AddrMin, uint64_t u64AddrMax, uint64_t u64OffTrans,
1297 uint64_t u64Granularity, uint64_t u64Length)
1298{
1299 PRTACPIRESINT pThis = hAcpiRes;
1300 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1301 AssertReturn(enmCacheability != kAcpiResMemRangeCacheability_Invalid, VERR_INVALID_PARAMETER);
1302 AssertReturn(enmType != kAcpiResMemType_Invalid, VERR_INVALID_PARAMETER);
1303 AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
1304 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1305 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1306
1307 uint8_t fType = rtAcpiResourceMemoryRangeToTypeFlags(enmCacheability, enmType, fRw);
1308 return rtAcpiResourceAddQWordAddressRange(pThis, ACPI_RSRCS_ADDR_SPACE_TYPE_MEMORY, fAddrSpace, fType,
1309 u64AddrMin, u64AddrMax, u64OffTrans, u64Granularity, u64Length);
1310}
1311
1312
1313RTDECL(int) RTAcpiResourceAddDWordMemoryRange(RTACPIRES hAcpiRes, RTACPIRESMEMRANGECACHEABILITY enmCacheability,
1314 RTACPIRESMEMRANGETYPE enmType, bool fRw, uint32_t fAddrSpace,
1315 uint32_t u32AddrMin, uint32_t u32AddrMax, uint32_t u32OffTrans,
1316 uint32_t u32Granularity, uint32_t u32Length)
1317{
1318 PRTACPIRESINT pThis = hAcpiRes;
1319 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1320 AssertReturn(enmCacheability != kAcpiResMemRangeCacheability_Invalid, VERR_INVALID_PARAMETER);
1321 AssertReturn(enmType != kAcpiResMemType_Invalid, VERR_INVALID_PARAMETER);
1322 AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
1323 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1324 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1325
1326 uint8_t fType = rtAcpiResourceMemoryRangeToTypeFlags(enmCacheability, enmType, fRw);
1327 return rtAcpiResourceAddDWordAddressRange(pThis, ACPI_RSRCS_ADDR_SPACE_TYPE_MEMORY, fAddrSpace, fType,
1328 u32AddrMin, u32AddrMax, u32OffTrans, u32Granularity, u32Length);
1329}
1330
1331
1332RTDECL(int) RTAcpiResourceAddQWordIoRange(RTACPIRES hAcpiRes, RTACPIRESIORANGETYPE enmIoType, RTACPIRESIORANGE enmIoRange,
1333 uint32_t fAddrSpace, uint64_t u64AddrMin, uint64_t u64AddrMax, uint64_t u64OffTrans,
1334 uint64_t u64Granularity, uint64_t u64Length)
1335{
1336 PRTACPIRESINT pThis = hAcpiRes;
1337 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1338 AssertReturn(enmIoType != kAcpiResIoRangeType_Invalid, VERR_INVALID_PARAMETER);
1339 AssertReturn(enmIoRange != kAcpiResIoRange_Invalid, VERR_INVALID_PARAMETER);
1340 AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
1341 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1342 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1343
1344 uint8_t fType = 0;
1345 switch (enmIoType)
1346 {
1347 case kAcpiResIoRangeType_Static:
1348 fType = ACPI_RSRCS_ADDR_SPACE_IO_F_TYPE_STATIC;
1349 break;
1350 case kAcpiResIoRangeType_Translation_Sparse:
1351 fType = ACPI_RSRCS_ADDR_SPACE_IO_F_TYPE_TRANSLATION | ACPI_RSRCS_ADDR_SPACE_IO_F_TRANSLATION_SPARSE;
1352 break;
1353 case kAcpiResIoRangeType_Translation_Dense:
1354 fType = ACPI_RSRCS_ADDR_SPACE_IO_F_TYPE_TRANSLATION | ACPI_RSRCS_ADDR_SPACE_IO_F_TRANSLATION_DENSE;
1355 break;
1356 case kAcpiResIoRangeType_Invalid:
1357 default:
1358 AssertFailedReturn(VERR_INVALID_PARAMETER);
1359 }
1360
1361 switch (enmIoRange)
1362 {
1363 case kAcpiResIoRange_NonIsaOnly:
1364 fType |= ACPI_RSRCS_ADDR_SPACE_IO_F_RANGE_NON_ISA_ONLY;
1365 break;
1366 case kAcpiResIoRange_IsaOnly:
1367 fType |= ACPI_RSRCS_ADDR_SPACE_IO_F_RANGE_ISA_ONLY;
1368 break;
1369 case kAcpiResIoRange_Whole:
1370 fType |= ACPI_RSRCS_ADDR_SPACE_IO_F_RANGE_WHOLE;
1371 break;
1372 case kAcpiResIoRange_Invalid:
1373 default:
1374 AssertFailedReturn(VERR_INVALID_PARAMETER);
1375 }
1376
1377 return rtAcpiResourceAddQWordAddressRange(pThis, ACPI_RSRCS_ADDR_SPACE_TYPE_IO, fAddrSpace, fType,
1378 u64AddrMin, u64AddrMax, u64OffTrans, u64Granularity, u64Length);
1379}
1380
1381
1382RTDECL(int) RTAcpiResourceAddWordBusNumber(RTACPIRES hAcpiRes, uint32_t fAddrSpace, uint16_t u16BusMin, uint16_t u16BusMax,
1383 uint16_t u16OffTrans, uint16_t u16Granularity, uint16_t u16Length)
1384{
1385 PRTACPIRESINT pThis = hAcpiRes;
1386 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1387 AssertReturn(!(fAddrSpace & ~RTACPI_RESOURCE_ADDR_RANGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
1388 AssertReturn(!pThis->fSealed, VERR_INVALID_STATE);
1389 AssertRCReturn(pThis->rcErr, pThis->rcErr);
1390
1391 uint8_t *pb = rtAcpiResBufEnsureSpace(pThis, 3 + 13);
1392 if (!pb)
1393 return VERR_NO_MEMORY;
1394
1395 pb[0] = ACPI_RSRCS_LARGE_TYPE | ACPI_RSRCS_ITEM_WORD_ADDR_SPACE; /* Tag */
1396 pb[1] = 13; /* Length[7:0] */
1397 pb[2] = 0; /* Length[15:8] */
1398 pb[3] = ACPI_RSRCS_ADDR_SPACE_TYPE_BUS_NUM_RANGE;
1399 pb[4] = (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_DECODE_TYPE_SUB ? ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_SUB : ACPI_RSRCS_ADDR_SPACE_F_DECODE_TYPE_POS)
1400 | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MIN_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MIN_ADDR_CHANGEABLE)
1401 | (fAddrSpace & RTACPI_RESOURCE_ADDR_RANGE_F_MAX_ADDR_FIXED ? ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_FIXED : ACPI_RSRCS_ADDR_SPACE_F_MAX_ADDR_CHANGEABLE);
1402 pb[5] = 0;
1403
1404 pb = rtAcpiResEncode16BitInteger(&pb[6], u16Granularity);
1405 pb = rtAcpiResEncode16BitInteger(pb, u16BusMin);
1406 pb = rtAcpiResEncode16BitInteger(pb, u16BusMax);
1407 pb = rtAcpiResEncode16BitInteger(pb, u16OffTrans);
1408 rtAcpiResEncode16BitInteger(pb, u16Length);
1409 return VINF_SUCCESS;
1410
1411}
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