Files
go-linux-lowlevel-hw/pkg/hwapi/phys_be.go
Dmitrii Okunev a82949f8ff Add support for GOARCH=386
`x86` is an invalid `GOARCH`. Changing to `386`.

Also `gofmt` added `//go:build` comments.
2022-05-18 13:11:44 +02:00

100 lines
2.2 KiB
Go

// Copyright 2012-2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build arm || arm64 || ppc64le
// +build arm arm64 ppc64le
package hwapi
import (
"encoding/binary"
"fmt"
"io"
"os"
)
var memPaths = [...]string{"/dev/fmem", "/dev/mem"}
func pathRead(path string, addr int64, data UintN) error {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Seek(addr, io.SeekCurrent); err != nil {
return err
}
return binary.Read(f, binary.BigEndian, data)
}
func selectDevMem() (string, error) {
if len(memPaths) == 0 {
return "", fmt.Errorf("internal error: no /dev/mem device specified")
}
for _, p := range memPaths {
if _, err := os.Stat(p); err == nil {
return p, nil
}
}
return "", fmt.Errorf("no suitable /dev/mem device found. Tried %#v", memPaths)
}
// ReadPhys reads data from physical memory at address addr. On x86 platforms,
// this uses the seek+read syscalls.
func (h HwAPI) ReadPhys(addr int64, data UintN) error {
devMem, err := selectDevMem()
if err != nil {
return err
}
return pathRead(devMem, addr, data)
}
// ReadPhysBuf reads data from physical memory at address addr. On x86 platforms,
// this uses the seek+read syscalls.
func (h HwAPI) ReadPhysBuf(addr int64, buf []byte) error {
devMem, err := selectDevMem()
if err != nil {
return err
}
f, err := os.OpenFile(devMem, os.O_RDONLY, 0)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Seek(addr, io.SeekCurrent); err != nil {
return err
}
return binary.Read(f, binary.BigEndian, buf)
}
func pathWrite(path string, addr int64, data UintN) error {
f, err := os.OpenFile(path, os.O_WRONLY, 0)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Seek(addr, io.SeekCurrent); err != nil {
return err
}
return binary.Write(f, binary.BigEndian, data)
}
// WritePhys writes data to physical memory at address addr. On x86 platforms, this
// uses the seek+read syscalls.
func (h HwAPI) WritePhys(addr int64, data UintN) error {
devMem, err := selectDevMem()
if err != nil {
return err
}
return pathWrite(devMem, addr, data)
}