-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher Meis <[email protected]>
- Loading branch information
Showing
10 changed files
with
1,305 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package acpi_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/bobuhiro11/gokvm/acpi" | ||
) | ||
|
||
func TestCalcPkgLength(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tt := range []struct { | ||
name string | ||
size uint32 | ||
exp []byte | ||
err error | ||
}{ | ||
{ | ||
name: "1ByteSize", | ||
size: 62, | ||
exp: []byte{63}, | ||
}, | ||
{ | ||
name: "2ByteSize", | ||
size: 64, | ||
exp: []byte{1<<6 | (66 & 0xf), 66 >> 4}, | ||
}, | ||
{ | ||
name: "3ByteSize", | ||
size: 4096, | ||
exp: []byte{2<<6 | (4099 & 0xf), 0, 1}, | ||
}, | ||
{ | ||
name: "4ByteSize", | ||
size: 536870912, | ||
exp: []byte{3<<6 | (536870916 & 0xf), 0, 0, 0}, | ||
}, | ||
} { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
val := acpi.CalcPkgLength(tt.size, true) | ||
if !bytes.Equal(val, tt.exp) { | ||
t.Fatalf("byte not match. Have: 0x%x, want: 0x%x", val, tt.exp) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package acpi | ||
|
||
type Signature string | ||
|
||
func (s Signature) ToBytes() [4]byte { | ||
var ret [4]byte | ||
|
||
for i := 0; i < 3; i++ { | ||
ret[i] = s[i] | ||
} | ||
|
||
return ret | ||
} | ||
|
||
const ( | ||
SigAEST Signature = "AEST" | ||
SigAPIC Signature = "APIC" | ||
SigBDAT Signature = "BDAT" | ||
SigBERT Signature = "BERT" | ||
SigBGRT Signature = "BGRT" | ||
SigBOOT Signature = "BOOT" | ||
SigCDIT Signature = "CDIT" | ||
SigCEDT Signature = "CEDT" | ||
SigCPEP Signature = "CPEP" | ||
SigCRAT Signature = "CRAT" | ||
SigCSRT Signature = "CSRT" | ||
SigDBGP Signature = "DBGP" | ||
SigDBG2 Signature = "DBG2" | ||
SigDMAR Signature = "DMAR" | ||
SigDRTM Signature = "DRTM" | ||
SigDSDT Signature = "DSDT" | ||
SigECDT Signature = "ECDT" | ||
SigETDT Signature = "ETDT" | ||
SigEINJ Signature = "EINJ" | ||
SigERST Signature = "ERST" | ||
SigFACP Signature = "FACP" | ||
SigFACS Signature = "FACS" | ||
SigFPDT Signature = "FPDT" | ||
SigGTDT Signature = "GTDT" | ||
SigHPET Signature = "HPET" | ||
SigHEST Signature = "HEST" | ||
SigIBFT Signature = "IBFT" | ||
SigIORT Signature = "IORT" | ||
SigIVRS Signature = "IVRS" | ||
SigLPIT Signature = "LPIT" | ||
SigMCFG Signature = "MCFG" | ||
SigMCHI Signature = "MCHI" | ||
SigMPAM Signature = "MPAM" | ||
SigMSCT Signature = "MSCT" | ||
SigMSDM Signature = "MSDM" | ||
SigMPST Signature = "MPST" | ||
SigNFIT Signature = "NFIT" | ||
SigOEMx Signature = "OEMx" | ||
SigPCCT Signature = "PCCT" | ||
SigPHAT Signature = "PHAT" | ||
SigPMTT Signature = "PMTT" | ||
SigPRMT Signature = "PRMT" | ||
SigPSDT Signature = "PSDT" | ||
SigRASF Signature = "RASF" | ||
SigRGRT Signature = "RGRT" | ||
SigRSDT Signature = "RSDT" | ||
SigSBST Signature = "SBST" | ||
SigSDEI Signature = "SDEI" | ||
SigSDEV Signature = "SDEV" | ||
SigSLIC Signature = "SLIC" | ||
SigSLIT Signature = "SLIT" | ||
SigSPCR Signature = "SPCR" | ||
SigSPMI Signature = "SPMI" | ||
SigSRAT Signature = "SRAT" | ||
SigSSDT Signature = "SSDT" | ||
SigSTAO Signature = "STAO" | ||
SigSVKL Signature = "SVKL" | ||
SigTCPA Signature = "TCPA" | ||
SigTPM2 Signature = "TPM2" | ||
SigUEFI Signature = "UEFI" | ||
SigVIOT Signature = "VIOT" | ||
SigWAET Signature = "WAET" | ||
SigWDAT Signature = "WDAT" | ||
SigWDRT Signature = "WDRT" | ||
SigWDBT Signature = "WDBT" | ||
SigWSMT Signature = "WSMT" | ||
SigXENV Signature = "XENV" | ||
SigXSDT Signature = "XSDT" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package acpi | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
) | ||
|
||
type DSDT struct { | ||
Header | ||
*AML | ||
} | ||
|
||
func NewDSDT(oemid, oemtableid string) DSDT { | ||
h := newHeader(SigDSDT, 36, 6, oemid, oemtableid) | ||
a := NewAML() | ||
|
||
return DSDT{h, a} | ||
} | ||
|
||
func (d *DSDT) ToBytes() ([]byte, error) { | ||
var buf bytes.Buffer | ||
if err := binary.Write(&buf, binary.LittleEndian, d.Header); err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err := buf.Write(d.AML.ToBytes()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package acpi | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
) | ||
|
||
type FADTFeatureFlag uint32 | ||
|
||
const ( | ||
WBINVD FADTFeatureFlag = 1<<0 + iota | ||
WBINVDFlush | ||
ProcC1 | ||
PLvL2Up | ||
PwrButton | ||
SleepButton | ||
FixRTC | ||
RTCS4 | ||
TmrValExt | ||
DCKCap | ||
ResetRegSup | ||
SealedCase | ||
Headless | ||
CPUSwSleep | ||
PCIExpWak | ||
UsePlatformClock | ||
S4RTCSTSValid | ||
RemotePowerOnCapable | ||
ForceAPICCluterModel | ||
ForceAPICPhysicalDestMode | ||
HwReducedACPI | ||
LowPowerS0IdleCapable | ||
) | ||
|
||
type FADT struct { | ||
Header | ||
FirmwareCTRL uint32 | ||
DSDTAddr uint32 | ||
_ uint8 | ||
PrefPMProfile uint8 | ||
SCIInt uint16 | ||
SMICmd uint32 | ||
ACPIEnable uint8 | ||
ACPIDisable uint8 | ||
S4BIOSReq uint8 | ||
PStateCnt uint8 | ||
PM1aEvtBlk uint32 | ||
PM1bEvtBlk uint32 | ||
PM1aCntBlk uint32 | ||
PM1bCntBlk uint32 | ||
PM2CntBlk uint32 | ||
PMTmrBlk uint32 | ||
GPE0Blk uint32 | ||
GPE1Blk uint32 | ||
PM1EvtLen uint8 | ||
PM1CntLen uint8 | ||
PM2CntLen uint8 | ||
PMTmrLen uint8 | ||
GPE0BlkLen uint8 | ||
GPE1BlkLen uint8 | ||
GPE1Base uint8 | ||
CstCnt uint8 | ||
PLvL2Lat uint16 | ||
PLvL3Lat uint16 | ||
FlushSize uint16 | ||
FlushStride uint16 | ||
DutyOffset uint8 | ||
DutyWidth uint8 | ||
DayALRM uint8 | ||
MonALRM uint8 | ||
Century uint8 | ||
IAPCBootArch uint16 | ||
_ uint8 | ||
FADTFeatureFlag | ||
ResetReg [12]uint8 | ||
ResetValue uint8 | ||
ARMBootArch uint16 | ||
MinorVersion uint8 | ||
XFirmwareCntl uint64 | ||
XDSDT uint64 | ||
XPM1aEvtBlk [12]uint8 | ||
XPM1bEvtBlk [12]uint8 | ||
XPM1aCntBlk [12]uint8 | ||
XPM1bCntBlk [12]uint8 | ||
XPM2CntBlk [12]uint8 | ||
XPMTmrBlk [12]uint8 | ||
XGPE0Blk [12]uint8 | ||
XGPE1Blk [12]uint8 | ||
SleepCtlReg [12]uint8 | ||
SleepStatReg [12]uint8 | ||
HyperVendorID [8]uint8 | ||
} | ||
|
||
func NewFADT(oemid, oemtableid, creatorid string) FADT { | ||
h := newHeader(SigFACP, 276, 6, oemid, oemtableid) | ||
|
||
return FADT{ | ||
Header: h, | ||
} | ||
} | ||
|
||
func (f *FADT) ToBytes() ([]byte, error) { | ||
var buf bytes.Buffer | ||
|
||
if err := binary.Write(&buf, binary.LittleEndian, f); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func (f *FADT) Checksum() error { | ||
f.Header.Checksum = 0 | ||
|
||
data, err := f.ToBytes() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cks := uint8(0) | ||
|
||
for _, b := range data { | ||
cks += b | ||
} | ||
|
||
f.Header.Checksum = cks | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package acpi | ||
|
||
type Header struct { | ||
Signature [4]byte | ||
Length uint32 | ||
Rev uint8 | ||
Checksum uint8 | ||
OEMId [6]byte | ||
OEMTableID [8]byte | ||
OEMRev uint32 | ||
CreatorID [4]byte | ||
CreatorRev uint32 | ||
} | ||
|
||
func convertOEMID(oemID string) [6]byte { | ||
var id [6]byte | ||
|
||
for i := 0; i < 6; i++ { | ||
id[i] = oemID[i] | ||
} | ||
|
||
return id | ||
} | ||
|
||
func convertOEMTableID(oemTableID string) [8]byte { | ||
var id [8]byte | ||
|
||
for i := 0; i < 8; i++ { | ||
id[i] = oemTableID[i] | ||
} | ||
|
||
return id | ||
} | ||
|
||
func convertCreatorID(creatorID string) [4]byte { | ||
var id [4]byte | ||
|
||
for i := 0; i < 4; i++ { | ||
id[i] = creatorID[i] | ||
} | ||
|
||
return id | ||
} | ||
|
||
func newHeader(sig Signature, length uint32, rev uint8, oemID, oemTableID string) Header { | ||
creatorID := "GACT" // Go ACPI Tables. | ||
|
||
oid := convertOEMID(oemID) | ||
otid := convertOEMTableID(oemTableID) | ||
cid := convertCreatorID(creatorID) | ||
|
||
return Header{ | ||
Signature: sig.ToBytes(), | ||
Length: length, | ||
Rev: rev, | ||
OEMId: oid, | ||
OEMTableID: otid, | ||
CreatorID: cid, | ||
CreatorRev: 1, | ||
} | ||
} |
Oops, something went wrong.