Skip to content

Commit

Permalink
fix: replace deprecated ioutils with os
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Walter <[email protected]>
  • Loading branch information
walterchris committed Aug 12, 2024
1 parent 730eacd commit 26a7e01
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
25 changes: 9 additions & 16 deletions pkg/hwapi/acpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
Expand All @@ -21,7 +20,7 @@ const (
ebdaTop = 0xa0000
)

//ACPIRsdpRev1 as defined in ACPI Spec 1
// ACPIRsdpRev1 as defined in ACPI Spec 1
type ACPIRsdpRev1 struct {
Signature [8]uint8
Checksum uint8
Expand All @@ -30,7 +29,7 @@ type ACPIRsdpRev1 struct {
RSDTPtr uint32
}

//ACPIRsdp as defined in ACPI Spec 6.2 "5.2.5.3 Root System Description Pointer (RSDP) Structure"
// ACPIRsdp as defined in ACPI Spec 6.2 "5.2.5.3 Root System Description Pointer (RSDP) Structure"
type ACPIRsdp struct {
ACPIRsdpRev1

Expand All @@ -52,20 +51,20 @@ type acpiHeader struct {
CreatorRevision uint32
}

//ACPIRsdt as defined in ACPI Spec 6.2 "5.2.7 Root System Description Table (RSDT)"
// ACPIRsdt as defined in ACPI Spec 6.2 "5.2.7 Root System Description Table (RSDT)"
type acpiRsdt struct {
acpiHeader
//Entry []uint32 count depend on Length field
// Entry []uint32 count depend on Length field
}

//ACPIXsdt as defined in ACPI Spec 6.2 "5.2.8 Extended System Description Table (XSDT)"
// ACPIXsdt as defined in ACPI Spec 6.2 "5.2.8 Extended System Description Table (XSDT)"
type acpiXsdt struct {
acpiHeader
//Entry []uint64 count depend on Length field
// Entry []uint64 count depend on Length field
}

func GetACPITableSysFS(h LowLevelHardwareInterfaces, n string) ([]byte, error) {
buf, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", acpiSysfsPath, n))
buf, err := os.ReadFile(fmt.Sprintf("%s/%s", acpiSysfsPath, n))
if err != nil {
return nil, fmt.Errorf("cannot access sysfs path %s: %s", acpiSysfsPath, err)
}
Expand Down Expand Up @@ -229,7 +228,6 @@ func parseSystab(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {
}

func scanLowMem(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {

var rsdp ACPIRsdp

buf := make([]byte, binary.Size(rsdp))
Expand All @@ -254,7 +252,6 @@ func scanLowMem(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {
}

func scanEBDA(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {

var rsdp ACPIRsdp

buf := make([]byte, binary.Size(rsdp))
Expand All @@ -280,7 +277,6 @@ func scanEBDA(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {
}

func scanReservedMem(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {

var rsdp ACPIRsdp

buf := make([]byte, binary.Size(rsdp))
Expand Down Expand Up @@ -311,14 +307,12 @@ func scanReservedMem(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {
}

func verifyRSDP(buf []byte, rsdp ACPIRsdp) error {

var old ACPIRsdpRev1

if rsdp.Revision == 0 {
if rsdp.RSDPLen != uint32(binary.Size(old)) {
return fmt.Errorf("ACPI RSDP has unexpected length %d", rsdp.RSDPLen)
}

}
/* Validate first checksum */
chksum := byte(0)
Expand Down Expand Up @@ -353,7 +347,6 @@ func verifyRSDP(buf []byte, rsdp ACPIRsdp) error {
}

func getACPITableDevMemRSDP(l LowLevelHardwareInterfaces) ([]byte, ACPIRsdp, error) {

var rsdp ACPIRsdp

if string(backupRSDP.Signature[:]) == "RSD PTR " {
Expand Down Expand Up @@ -475,7 +468,7 @@ func GetACPITableDevMem(h LowLevelHardwareInterfaces, n string) ([]byte, error)
}

for k, v := range acpitables {
if v == n { //FIXME: Handle duplicated entries like SSDT
if v == n { // FIXME: Handle duplicated entries like SSDT
var header acpiHeader

err := h.ReadPhysBuf(int64(k), buf)
Expand All @@ -498,7 +491,7 @@ func GetACPITableDevMem(h LowLevelHardwareInterfaces, n string) ([]byte, error)
return nil, fmt.Errorf("ACPI table not found")
}

//GetACPITable returns the requested ACPI table, for DSDT use argument "DSDT"
// GetACPITable returns the requested ACPI table, for DSDT use argument "DSDT"
func (h HwAPI) GetACPITable(n string) ([]byte, error) {
if n == "" || len(n) > 6 {
return nil, fmt.Errorf("invalid ACPI name")
Expand Down
10 changes: 4 additions & 6 deletions pkg/hwapi/e820.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package hwapi

import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)

//iterateOverE820Ranges iterates over all e820 entries and invokes the callback for every matching type
// iterateOverE820Ranges iterates over all e820 entries and invokes the callback for every matching type
func (h HwAPI) IterateOverE820Ranges(target string, callback func(start uint64, end uint64) bool) (bool, error) {

dir, err := os.Open("/sys/firmware/memmap")
if err != nil {
return false, fmt.Errorf("cannot access e820 table: %s", err)
Expand All @@ -25,7 +23,7 @@ func (h HwAPI) IterateOverE820Ranges(target string, callback func(start uint64,
if subdir.IsDir() {

path := fmt.Sprintf("/sys/firmware/memmap/%s/type", subdir.Name())
buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
continue
}
Expand Down Expand Up @@ -76,7 +74,7 @@ func UsableMemoryBelow4G(l LowLevelHardwareInterfaces) (size uint64, err error)
return
}

//IsReservedInE820 reads the e820 table exported via /sys/firmware/memmap and checks whether
// IsReservedInE820 reads the e820 table exported via /sys/firmware/memmap and checks whether
// the range [start; end] is marked as reserved. Returns true if it is reserved,
// false if not.
func IsReservedInE820(l LowLevelHardwareInterfaces, start uint64, end uint64) (bool, error) {
Expand All @@ -97,7 +95,7 @@ func IsReservedInE820(l LowLevelHardwareInterfaces, start uint64, end uint64) (b
}

func readHexInteger(path string) (uint64, error) {
buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
return 0, err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/hwapi/iommu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"strconv"
"unsafe"
)

//VTdRegisters represents the IOMMIO space
// VTdRegisters represents the IOMMIO space
type VTdRegisters struct {
Version uint32 // Architecture version supported by the implementation.
Reserved1 uint32 // Reserved
Expand Down Expand Up @@ -111,7 +110,7 @@ func readVTdRegs(l LowLevelHardwareInterfaces) (VTdRegisters, error) {

for _, subdir := range subdirs {
path := fmt.Sprintf("/sys/class/iommu/%s/intel-iommu/address", subdir.Name())
addrBuf, err := ioutil.ReadFile(path)
addrBuf, err := os.ReadFile(path)
if err != nil {
continue
}
Expand Down Expand Up @@ -139,7 +138,7 @@ func readVTdRegs(l LowLevelHardwareInterfaces) (VTdRegisters, error) {
return regs, fmt.Errorf("no IOMMU found: /sys/class/iommu/*/intel-iommu/address does not exists or is malformed")
}

//LookupIOAddress returns the address of the root Tbl
// LookupIOAddress returns the address of the root Tbl
func (h HwAPI) LookupIOAddress(addr uint64, regs VTdRegisters) ([]uint64, error) {
rootTblAddr := regs.RootTableAddress & 0xffffffffffff000
ttm := (regs.RootTableAddress >> 10) & 3
Expand Down
13 changes: 5 additions & 8 deletions pkg/hwapi/tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

tpm1 "github.com/google/go-tpm/tpm"
tpm2 "github.com/google/go-tpm/legacy/tpm2"
tpm1 "github.com/google/go-tpm/tpm"
tpmutil "github.com/google/go-tpm/tpmutil"
)

// TCGVendorID TPM manufacturer id
type TCGVendorID uint32

func (id TCGVendorID) String() string {

s, ok := vendors[id]
if !ok {
return fmt.Sprintf("unknown TPM vendor (%d)", id)
Expand Down Expand Up @@ -161,7 +159,7 @@ const (
func probeSystemTPMs() ([]probedTPM, error) {
var tpms []probedTPM

tpmDevs, err := ioutil.ReadDir(tpmRoot)
tpmDevs, err := os.ReadDir(tpmRoot)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
Expand Down Expand Up @@ -210,7 +208,7 @@ func newTPM(pTPM probedTPM) (*TPM, error) {
// If the TPM has a kernel-provided resource manager, we should
// use that instead of communicating directly.
devPath := filepath.Join("/dev", filepath.Base(pTPM.Path))
f, err := ioutil.ReadDir(filepath.Join(pTPM.Path, "device", "tpmrm"))
f, err := os.ReadDir(filepath.Join(pTPM.Path, "device", "tpmrm"))
if err != nil {
if !os.IsNotExist(err) {
return nil, err
Expand All @@ -237,11 +235,11 @@ func newTPM(pTPM probedTPM) (*TPM, error) {
// MeasurementLog reads the TCPA eventlog in binary format
// from the Linux kernel
func (t *TPM) MeasurementLog() ([]byte, error) {
return ioutil.ReadFile("/sys/kernel/security/tpm0/binary_bios_measurements")
return os.ReadFile("/sys/kernel/security/tpm0/binary_bios_measurements")
}

func nvRead12(rwc io.ReadWriteCloser, index, offset, len uint32, auth string) ([]byte, error) {
var ownAuth [20]byte //owner well known
var ownAuth [20]byte // owner well known
if auth != "" {
ownAuth = sha1.Sum([]byte(auth))
}
Expand Down Expand Up @@ -274,7 +272,6 @@ func nvRead20(rwc io.ReadWriteCloser, index, authHandle tpmutil.Handle, password
}

func readTPM12Information(rwc io.ReadWriter) (TPMInfo, error) {

manufacturerRaw, err := tpm1.GetManufacturer(rwc)
if err != nil {
return TPMInfo{}, err
Expand Down

0 comments on commit 26a7e01

Please sign in to comment.