github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/acpi/rsdp_unix.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  	"fmt"
     9  	"os"
    10  
    11  	"github.com/u-root/u-root/pkg/boot/ebda"
    12  	"github.com/u-root/u-root/pkg/memio"
    13  )
    14  
    15  // " RSD PTR" in hex, 8 bytes.
    16  const rsdpTag = 0x2052545020445352
    17  
    18  // GetRSDPEBDA finds the RSDP in the EBDA.
    19  func GetRSDPEBDA() (*RSDP, error) {
    20  	f, err := os.OpenFile("/dev/mem", os.O_RDONLY, 0)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	defer f.Close()
    25  
    26  	e, err := ebda.ReadEBDA(f)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return getRSDPMem(int64(e.BaseOffset), int64(e.BaseOffset+e.Length))
    32  }
    33  
    34  func getRSDPMem(start, end int64) (*RSDP, error) {
    35  	for base := start; base < end; base += 16 {
    36  		var r memio.Uint64
    37  		if err := memio.Read(int64(base), &r); err != nil {
    38  			continue
    39  		}
    40  		if r != rsdpTag {
    41  			continue
    42  		}
    43  		rsdp, err := readRSDP(base)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		return rsdp, nil
    48  	}
    49  	return nil, fmt.Errorf("could not find ACPI RSDP via /dev/mem from %#08x to %#08x", start, end)
    50  }
    51  
    52  // GetRSDPMem is the option of last choice, it just grovels through
    53  // the e0000-ffff0 area, 16 bytes at a time, trying to find an RSDP.
    54  // These are well-known addresses for 20+ years.
    55  func GetRSDPMem() (*RSDP, error) {
    56  	return getRSDPMem(0xe0000, 0xffff0)
    57  }