최신 ACPI 스펙은 UEFI 스펙 페이지()에서 찾을 수 있다.
현재 최신 사양은 "ACPI Specification Version 6.4 (released January 2021"()이다.
SMBIOS 테이블에 사용한 것과 동일한 방법을 사용하여 ACPI Entry point 테이블 주소를 출력하겠다.
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
for (UINTN i=0; i<SystemTable->NumberOfTableEntries; i++) {
if (CompareGuid(&(SystemTable->ConfigurationTable[i].VendorGuid), &gEfiAcpi20TableGuid)) {
Print(L"ACPI table is placed at %p\n\n", SystemTable->ConfigurationTable[i].VendorTable);
}
}
return EFI_SUCCESS;
}
출력된 ACPI 테이블 주소를 바탕으로 dmem을 사용하여 메모리 내부 값을 확인하겠다.
FS0:\> AcpiInfo.efi
ACPI table is placed at 7B7E014
FS0:\> dmem 7B7E014 30
Memory Address 0000000007B7E014 30 Bytes
07B7E014: 52 53 44 20 50 54 52 20-4E 42 4F 43 48 53 20 02 *RSD PTR NBOCHS .*
07B7E024: 74 D0 B7 07 24 00 00 00-E8 D0 B7 07 00 00 00 00 *t...$...........*
07B7E034: 66 00 00 00 AF AF AF AF-AF AF AF AF AF AF AF AF *f...............*
FS0:\>
여기에는 RSDT 및 XSDT 테이블에 대한 주소가 포함되며 오프셋을 계산시 메모리 덤프에서 다음과 같은 주소를 얻을 수 있다.
XSDT=0x07B7D0E8
RSDT=0x07B7D074
이러한 테이블은 실제로 OS에 유용한 데이터를 포함하는 다른 ACPI 테이블에 대한 포인터를 포함한다.
규격에 따르면 "플랫폼은 ACPI 1.0 운영체제와 호환성을 가능하게 하는 RSDT를 제공합니다. XSDT는 RSDT 기능을 대체합니다." 따라서 dmem을 사용하여 이러한 주소를 출력하면 테이블 내용은 테이블 서명을 제외하고는 거의 동일하다. 따라서 XSDT 테이블 데이터를 파싱하겠다.
이제 코드를 작성해보겠다. ACPI 구조에 대한 정의는 아래의 헤더 파일들에 존재하니 확인하는 것도 좋다.
EFI_ACPI_6_3_ROOT_SYSTEM_DESCRIPTION_POINTER* RSDP = NULL;
for (UINTN i=0; i<SystemTable->NumberOfTableEntries; i++) {
if (CompareGuid(&(SystemTable->ConfigurationTable[i].VendorGuid), &gEfiAcpi20TableGuid)) {
Print(L"RSDP table is placed at %p\n\n", SystemTable->ConfigurationTable[i].VendorTable);
RSDP = SystemTable->ConfigurationTable[i].VendorTable;
}
}
if (!RSDP) {
Print(L"No ACPI2.0 table was found in the system\n");
return EFI_SUCCESS;
}
if (((CHAR8)((RSDP->Signature >> 0) & 0xFF) != 'R') ||
((CHAR8)((RSDP->Signature >> 8) & 0xFF) != 'S') ||
((CHAR8)((RSDP->Signature >> 16) & 0xFF) != 'D') ||
((CHAR8)((RSDP->Signature >> 24) & 0xFF) != ' ') ||
((CHAR8)((RSDP->Signature >> 32) & 0xFF) != 'P') ||
((CHAR8)((RSDP->Signature >> 40) & 0xFF) != 'T') ||
((CHAR8)((RSDP->Signature >> 48) & 0xFF) != 'R') ||
((CHAR8)((RSDP->Signature >> 56) & 0xFF) != ' ')) {
Print(L"Error! RSDP signature is not valid!\n");
return EFI_SUCCESS;
}
Print(L"System description tables:\n");
Print(L"\tRSDT table is placed at address %p\n", RSDP->RsdtAddress);
Print(L"\tXSDT table is placed at address %p\n", RSDP->XsdtAddress);
Print(L"\n");
위의 구조가 설명되어 있는 부분에서 XSDT에 대한 설명도 또한찾아볼 수 있다.
//
// Extended System Description Table
// No definition needed as it is a common description table header, the same with
// EFI_ACPI_DESCRIPTION_HEADER, followed by a variable number of UINT64 table pointers.
//
여기서 한가지 확인하고 넘어가야 할 것이 있다. 일부 ACPi 테이블은 다른 ACPI 테이블에 대한 포인터를 포함할 수 있다. 예를들어 Fixed ACPI Description Table(FADT)에는 DSDT 및 FACS 테이블에 대한 포인터가 포함될 수 있다.
아래에서 나올 설명은 위의 FADT 구조의 필드이기 때문에 구조를 확인하는 것을 권장한다.
FADT의 FirmwareCtrl 필드에는 FACS 테이블에 대한 포인터가 포함되며 Dsdt 필드에는 DSDT 테이블에 대한 포인터가 포함된다.
CheckSubtables 함수를 작성하여 해당 ACPI 테이블이 FADT인지 그리고 하위 테이블을 찾을 수 있는지 확인해 보겠다.
FS0:\> AcpiInfo.efi
RSDP table is placed at 7B7E014
System description tables:
RSDT table is placed at address 7B7D074
XSDT table is placed at address 7B7D0E8
Main ACPI tables:
FACP table is placed at address 7B7A000 with length 0x74
DSDT table is placed at address 7B7B000 with length 0x140B
FACS table is placed at address 7BDD000 with length 0x40
APIC table is placed at address 7B79000 with length 0x78
HPET table is placed at address 7B78000 with length 0x38
BGRT table is placed at address 7B77000 with length 0x38
해당 프로토콜에서 우리는 OpenFileByName/WriteFile/CloseFile 3가지 함수를 사용한다.
EFI_SHELL_PROTOCOL.OpenFileByName()
Summary:
Opens a file or a directory by file name.
Prototype:i
typdef
EFI_STATUS
(EFIAPI *EFI_SHELL_OPEN_FILE_BY_NAME) (
IN CONST CHAR16 *FileName,
OUT SHELL_FILE_HANDLE *FileHandle,
IN UINT64 OpenMode
);
Parameters:
FileName Points to the null-terminated UCS-2 encoded file name.
FileHandle On return, points to the file handle.
OpenMode File open mode.
Description:
This function opens the specified file in the specified OpenMode and returns a file handle.
EFI_SHELL_PROTOCOL.WriteFile()
Summary:
Writes data to the file.
Prototype:
typedef
EFI_STATUS
(EFIAPI EFI_SHELL_WRITE_FILE)(
IN SHELL_FILE_HANDLE FileHandle,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
);
Parameters:
FileHandle The opened file handle for writing.
BufferSize On input, size of Buffer.
Buffer The buffer in which data to write.
Description:
This function writes the specified number of bytes to the file at the current file position. The current file position is advanced the actual number of bytes
written, which is returned in BufferSize. Partial writes only occur when there has been a data error during the write attempt (such as “volume space full”).
The file automatically grows to hold the data, if required.
EFI_SHELL_PROTOCOL.CloseFile()
Summary:
Closes the file handle.
Prototype:
typedef
EFI_STATUS
(EFIAPI *EFI_SHELL_CLOSE_FILE)(
IN SHELL_FILE_HANDLE FileHandle
);
Parameters:
FileHandle The file handle to be closed
Description This function closes a specified file handle. All “dirty” cached file data is flushed
to the device, and the file is closed. In all cases, the handle is closed.
해당 함수들을 이용하기 위해서 c 파일에 헤더를 추가 및 inf 파일에 프로토콜 GUID 값을 넣어야 한다.
#include <Protocol/Shell.h>
[Protocols]
gEfiShellProtocolGuid
이후 프로그램에서 EFI_SHELLPROTOCOL을 사용하기 위해서 BootServices의 LocateProtocol 기능을 통해 프로토콜을획득한다.
EFI_SHELL_PROTOCOL* ShellProtocol;
EFI_STATUS Status = gBS->LocateProtocol(
&gEfiShellProtocolGuid,
NULL,
(VOID **)&ShellProtocol
);
if (EFI_ERROR(Status)) {
Print(L"Can't open EFI_SHELL_PROTOCOL: %r\n", Status);
return EFI_SUCCESS;
}
SaveACPITable 함수를 만들어 파일 저장 기능에 대해서 한번에 처리할 수 있도록 만들어 주겠다.
또한 DSDT 및 FACS 테이블을 저장하기 위해 CheckSubtable 기능에 추가하는 것을 잊으면 안된다.\
SaveACPITable 함수를 호출할 때마다 EFI_SHELL_PROTOCOL* ShellProtocol을 사용하므로 모든 곳의 매개변수로 전달하거나 ShellProtocol을 전역변수로 옮기면 된다.
이제 SaveACPITable 함수를 작성해야한다. ACPI 테이블 데이터를 .aml 파일에 저장한다. ACPI 언어 소스 파일에는 일반적으로 .asl/.dsl 확장자(ACPI 소스 언어)가 있고 컴파일된 파일에는 .aml확장자(ACPI 기계 언어)가 있기 때문에 파일에 .aml확장자를 사용한다.
EFI_STATUS SaveACPITable(UINT32 Signature, VOID* addr, UINTN size) {
CHAR16 TableName[5];
TableName[0] = (CHAR16)((Signature>> 0)&0xFF);
TableName[1] = (CHAR16)((Signature>> 8)&0xFF);
TableName[2] = (CHAR16)((Signature>>16)&0xFF);
TableName[3] = (CHAR16)((Signature>>24)&0xFF);
TableName[4] = 0;
CHAR16 FileName[9] = {0};
StrCpyS(FileName, 9, TableName);
StrCatS(FileName, 9, L".aml");
SHELL_FILE_HANDLE FileHandle;
EFI_STATUS Status = ShellProtocol->OpenFileByName(FileName,
&FileHandle,
EFI_FILE_MODE_CREATE |
EFI_FILE_MODE_WRITE |
EFI_FILE_MODE_READ);
if (!EFI_ERROR(Status)) {
Status = ShellProtocol->WriteFile(FileHandle, &size, addr);
if (EFI_ERROR(Status)) {
Print(L"Error in WriteFile: %r\n", Status);
}
Status = ShellProtocol->CloseFile(FileHandle);
if (EFI_ERROR(Status)) {
Print(L"Error in CloseFile: %r\n", Status);
}
} else {
Print(L"Error in OpenFileByName: %r\n", Status);
}
return Status;
}
앱을 빌드하고 OVMF에서 실행하면 UEF_disk 공유 폴더에 4개의 파일이 생길 것이다.
$ ls -1 ~/UEFI_disk/*.aml
/home/kostr/UEFI_disk/apic.aml
/home/kostr/UEFI_disk/bgrt.aml
/home/kostr/UEFI_disk/dsdt.aml
/home/kostr/UEFI_disk/facp.aml
/home/kostr/UEFI_disk/facs.aml
/home/kostr/UEFI_disk/hpet.aml
iasl 컴파일러를 사용하여 ACPI 테이블 데이터를 disassemble 할 수 있다.
$ iasl -d ~/UEFI_disk/*.aml
Intel ACPI Component Architecture
ASL+ Optimizing Compiler/Disassembler version 20190509
Copyright (c) 2000 - 2019 Intel Corporation
File appears to be binary: found 81 non-ASCII characters, disassembling
Binary file appears to be a valid ACPI table, disassembling
Input file /home/kostr/UEFI_disk/apic.aml, Length 0x78 (120) bytes
ACPI: APIC 0x0000000000000000 000078 (v01 BOCHS BXPCAPIC 00000001 BXPC 00000001)
Acpi Data Table [APIC] decoded
Formatted output: /home/kostr/UEFI_disk/apic.dsl - 4939 bytes
File appears to be binary: found 31 non-ASCII characters, disassembling
Binary file appears to be a valid ACPI table, disassembling
Input file /home/kostr/UEFI_disk/bgrt.aml, Length 0x38 (56) bytes
ACPI: BGRT 0x0000000000000000 000038 (v01 INTEL EDK2 00000002 01000013)
Acpi Data Table [BGRT] decoded
Formatted output: /home/kostr/UEFI_disk/bgrt.dsl - 1632 bytes
File appears to be binary: found 1630 non-ASCII characters, disassembling
Binary file appears to be a valid ACPI table, disassembling
Input file /home/kostr/UEFI_disk/dsdt.aml, Length 0x140B (5131) bytes
ACPI: DSDT 0x0000000000000000 00140B (v01 BOCHS BXPCDSDT 00000001 BXPC 00000001)
Pass 1 parse of [DSDT]
Pass 2 parse of [DSDT]
Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)
Parsing completed
Disassembly completed
ASL Output: /home/kostr/UEFI_disk/dsdt.dsl - 43444 bytes
File appears to be binary: found 91 non-ASCII characters, disassembling
Binary file appears to be a valid ACPI table, disassembling
Input file /home/kostr/UEFI_disk/facp.aml, Length 0x74 (116) bytes
ACPI: FACP 0x0000000000000000 000074 (v01 BOCHS BXPCFACP 00000001 BXPC 00000001)
Acpi Data Table [FACP] decoded
Formatted output: /home/kostr/UEFI_disk/facp.dsl - 4896 bytes
File appears to be binary: found 59 non-ASCII characters, disassembling
Binary file appears to be a valid ACPI table, disassembling
Input file /home/kostr/UEFI_disk/facs.aml, Length 0x40 (64) bytes
ACPI: FACS 0x0000000000000000 000040
Acpi Data Table [FACS] decoded
Formatted output: /home/kostr/UEFI_disk/facs.dsl - 1394 bytes
File appears to be binary: found 33 non-ASCII characters, disassembling
Binary file appears to be a valid ACPI table, disassembling
Input file /home/kostr/UEFI_disk/hpet.aml, Length 0x38 (56) bytes
ACPI: HPET 0x0000000000000000 000038 (v01 BOCHS BXPCHPET 00000001 BXPC 00000001)
Acpi Data Table [HPET] decoded
Formatted output: /home/kostr/UEFI_disk/hpet.dsl - 1891 bytes
시그니처 값인 RSP PTR은 RSDP(Root System Description Pointer (RSDP) Structure)를 나타낸다.
최신 ACPI 표준 헤더 파일에서 RSDP 구조에 대한 정의를 살펴보겠다.
해당 설명에서 나온 EFI_ACPI_DESCRIPTION_HEADER에 대한 정의는 아래와 같다.
FADT에 대한 구조와 설명은 아래의 링크에 언급되어 있다.
Fixed ACPI Description Table (FACP) -
Differentiated System Description Table (DSDT) -
Firmware ACPI Control Structure (FACS) -
Multiple APIC Description Table (MADT) -
IA-PC High Precision Event Timer Table (HPET) - - ACPI 사양에는 없지만 페이지와 별도의 문서에 존재한다.
Boot Graphics Resource Table (BGRT) -
SMBIOS 테이블과 마찬가지로 프로토콜을 사용하여 동일한 데이터를 얻을 수 있음을 명심해야한다. EFI_ACPI_SDT_PROTOCOL의 GetAcpiTable()함수는 동일한 정보를 얻는데 도움이 될 수 있다. 해당 프로토콜은 또한 UEFI PI 사양에 의해 정의된다.
이제 메모리에서 파일로 ACPI 테이블을 저장해 보겠다.
이를 위해서 UEFI 쉘 사양에 정의된 EFI_SHELL_PROTOCOL을 활용할 수 있다. File I/O를 위한 많은 기능이 존재하기 때문에 도움이 될 수 있다.
파일 이름을 문자열로 생성하기 위해 StrCatS 및 StrCpyS 함수를 사용한다. 이들은 C++에서 사용되는 strcat_s/strcpy_s와 유사한 문자열 연결/복사 함수의 안전한 버전이다.