File size: 3,414 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# go-linux-lowlevel-hw — Usage Documentation

Cleanroom usage docs extracted from the repository README of
`9elements/go-linux-lowlevel-hw`.

`go-linux-lowlevel-hw` provides low level access to common hardware on UNIX-like
platforms.

## Description

This package provides low level access to certain hardware typically found on
modern x86 PCs. Some information is only available when run as the most
privileged user. Thus this library is to be used in preproduction and testing
environments with relaxed kernel security.

**Be warned, you could brick your system.**

## How to use this library

```go
package main

import (
	"github.com/9elements/go-linux-lowlevel-hw/pkg/hwapi"
)

func main() {
	h := hwapi.GetAPI()

	//...
}
```

Import path: `github.com/9elements/go-linux-lowlevel-hw/pkg/hwapi`
Entry point: `hwapi.GetAPI()` returns the API interface described below.

## Interfaces

The `GetAPI()` call returns an interface providing the following methods
(public API reference):

```go
	// cpuid
	VersionString() string
	HasSMX() bool
	HasVMX() bool
	HasMTRR() bool
	ProcessorBrandName() string
	CPUSignature() uint32
	CPULogCount() uint32

	// e820
	IsReservedInE820(start uint64, end uint64) (bool, error)

	// iommu
	LookupIOAddress(addr uint64, regs VTdRegisters) ([]uint64, error)
	AddressRangesIsDMAProtected(first, end uint64) (bool, error)

	// msr
	ReadMSR(msr int64) (uint64, error)
	ReadMSRAllCores(msr int64) (uint64, error)

	// msr_intel
	HasSMRR() (bool, error)
	GetSMRRInfo() (SMRR, error)
	IA32FeatureControlIsLocked() (bool, error)
	IA32PlatformID() (uint64, error)
	AllowsVMXInSMX() (bool, error)
	TXTLeavesAreEnabled() (bool, error)
	IA32DebugInterfaceEnabledOrLocked() (*IA32Debug, error)

	// pci
	PCIEnumerateVisibleDevices(cb func(d PCIDevice) (abort bool)) (err error)
	PCIReadConfig8(d PCIDevice, off int) (uint8, error)
	PCIReadConfig16(d PCIDevice, off int) (uint16, error)
	PCIReadConfig32(d PCIDevice, off int) (uint32, error)
	PCIWriteConfig8(d PCIDevice, off int, val uint8) error
	PCIWriteConfig16(d PCIDevice, off int, val uint16) error
	PCIWriteConfig32(d PCIDevice, off int, val uint32) error
	PCIReadVendorID(d PCIDevice) (uint16, error)
	PCIReadDeviceID(d PCIDevice) (uint16, error)

	// hostbridge
	ReadHostBridgeTseg() (uint32, uint32, error)
	ReadHostBridgeDPR() (DMAProtectedRange, error)

	// phys
	ReadPhys(addr int64, data UintN) error
	ReadPhysBuf(addr int64, buf []byte) error
	WritePhys(addr int64, data UintN) error

	// tpm
	NewTPM() (*TPM, error)
	NVLocked(tpmCon *TPM) (bool, error)
	ReadNVPublic(tpmCon *TPM, index uint32) ([]byte, error)
	NVReadValue(tpmCon *TPM, index uint32, password string, size, offhandle uint32) ([]byte, error)
	ReadPCR(tpmCon *TPM, pcr uint32) ([]byte, error)

	// acpi
	GetACPITableDevMem(n string) ([]byte, error)
	GetACPITableSysFS(n string) ([]byte, error)

	// smbios
	IterateOverSMBIOSTables(n uint8, callback func(s *smbios.Structure) bool) (ret bool, err error)
	IterateOverSMBIOSTablesType0(callback func(t0 *SMBIOSType0) bool) (ret bool, err error)
```

## Requirements / notes

- Go module: `github.com/9elements/go-linux-lowlevel-hw` (see `go.mod` in the
  repository for the exact Go version and dependencies).
- Many operations (MSR reads, physical memory access, PCI config space,
  TPM access) require root or otherwise elevated privileges on Linux.
- Intended for preproduction and testing environments only.