github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/acpi/rsdp.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package acpi
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  
    11  	"github.com/mvdan/u-root-coreutils/pkg/memio"
    12  )
    13  
    14  var defaultRSDP = []byte("RSDP PTR U-ROOT\x02")
    15  
    16  // RSDP is the v2 version of the ACPI RSDP struct.
    17  type RSDP struct {
    18  	// base is the base address of the RSDP struct in physical memory.
    19  	base int64
    20  
    21  	data [headerLength]byte
    22  }
    23  
    24  // NewRSDP returns a new and partially initialized RSDP, setting only
    25  // the defaultRSDP values, address, length, and signature.
    26  func NewRSDP(addr uintptr, len uint) []byte {
    27  	var r [headerLength]byte
    28  	copy(r[:], defaultRSDP)
    29  
    30  	// This is a bit of a cheat. All the fields are 0.  So we get a
    31  	// checksum, set up the XSDT fields, get the second checksum.
    32  	r[cSUM1Off] = gencsum(r[:])
    33  	binary.LittleEndian.PutUint32(r[xSDTLenOff:], uint32(len))
    34  	binary.LittleEndian.PutUint64(r[xSDTAddrOff:], uint64(addr))
    35  	r[cSUM2Off] = gencsum(r[:])
    36  	return r[:]
    37  }
    38  
    39  // Len returns the RSDP length
    40  func (r *RSDP) Len() uint32 {
    41  	return uint32(len(r.data))
    42  }
    43  
    44  // AllData returns the RSDP as a []byte
    45  func (r *RSDP) AllData() []byte {
    46  	return r.data[:]
    47  }
    48  
    49  // TableData returns the RSDP table data as a []byte
    50  func (r *RSDP) TableData() []byte {
    51  	return r.data[36:]
    52  }
    53  
    54  // Sig returns the RSDP signature
    55  func (r *RSDP) Sig() string {
    56  	return string(r.data[:8])
    57  }
    58  
    59  // OEMID returns the RSDP OEMID
    60  func (r *RSDP) OEMID() string {
    61  	return string(r.data[9:15])
    62  }
    63  
    64  // RSDPAddr returns the physical base address of the RSDP.
    65  func (r *RSDP) RSDPAddr() int64 {
    66  	return r.base
    67  }
    68  
    69  // SDTAddr returns a base address or the [RX]SDT.
    70  //
    71  // It will preferentially return the XSDT, but if that is
    72  // 0 it will return the RSDT address.
    73  func (r *RSDP) SDTAddr() int64 {
    74  	b := uint64(binary.LittleEndian.Uint32(r.data[16:20]))
    75  	if b != 0 {
    76  		return int64(b)
    77  	}
    78  	return int64(binary.LittleEndian.Uint64(r.data[24:32]))
    79  }
    80  
    81  func readRSDP(base int64) (*RSDP, error) {
    82  	r := &RSDP{}
    83  	r.base = base
    84  
    85  	dat := memio.ByteSlice(make([]byte, len(r.data)))
    86  	if err := memio.Read(int64(base), &dat); err != nil {
    87  		return nil, err
    88  	}
    89  	copy(r.data[:], dat)
    90  	return r, nil
    91  }
    92  
    93  // GetRSDP finds the RSDP pointer and struct. The rsdpgetters must be defined
    94  // in rsdp_$(GOOS).go, since, e.g.,OSX, BSD, and Linux have some intersections
    95  // but some unique aspects too, and Plan 9 has nothing in common with any of them.
    96  //
    97  // It is able to use several methods, because there is no consistency
    98  // about how it is done.
    99  func GetRSDP() (*RSDP, error) {
   100  	for _, f := range rsdpgetters {
   101  		r, err := f()
   102  		if err == nil {
   103  			return r, nil
   104  		}
   105  	}
   106  	return nil, fmt.Errorf("cannot find an RSDP")
   107  }