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