github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/memio/ports_linux.go (about) 1 // Copyright 2012-2020 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 //go:build (linux && amd64) || (linux && 386) 6 // +build linux,amd64 linux,386 7 8 package memio 9 10 import ( 11 "fmt" 12 "os" 13 ) 14 15 var ( 16 linuxPath = "/dev/port" 17 ) 18 19 // LinuxPort implements ReadWriteCloser for Linux devices. 20 type LinuxPort struct { 21 ReadWriteCloser 22 } 23 24 var _ PortReadWriter = &LinuxPort{} 25 26 // In reads data from the x86 port at address addr. Data must be Uint8, Uint16, 27 // Uint32, but not Uint64. 28 func (p *LinuxPort) In(addr uint16, data UintN) error { 29 if _, ok := data.(*Uint8); !ok { 30 return fmt.Errorf("/dev/port data must be 8 bits on Linux") 31 } 32 return p.ReadWriteCloser.Read(data, int64(addr)) 33 } 34 35 // Out writes data to the x86 port at address addr. data must be Uint8, Uint16 36 // uint32, but not Uint64. 37 func (p *LinuxPort) Out(addr uint16, data UintN) error { 38 if _, ok := data.(*Uint8); !ok { 39 return fmt.Errorf("/dev/port data must be 8 bits on Linux") 40 } 41 return p.ReadWriteCloser.Write(data, int64(addr)) 42 43 } 44 45 // Close implements Close. 46 func (p *LinuxPort) Close() error { 47 return p.ReadWriteCloser.Close() 48 } 49 50 // NewPort returns a new instance of LinuxPort for read/write operations on /dev/port 51 func NewPort() (*LinuxPort, error) { 52 var _ PortReadWriter = &LinuxPort{} 53 f, err := os.OpenFile(linuxPath, os.O_RDWR, 0) 54 if err != nil { 55 return nil, err 56 } 57 memPort := NewMemIOPort(f) 58 return &LinuxPort{ 59 ReadWriteCloser: memPort, 60 }, nil 61 } 62 63 // In is deprecated. Only here for compatibility. Use NewPort() and the interface functions instead. 64 func In(addr uint16, data UintN) error { 65 port, err := NewPort() 66 if err != nil { 67 return err 68 } 69 defer port.Close() 70 return port.In(addr, data) 71 } 72 73 // Out is deprecated. Only here for compatibility. Use NewPort() and the interface functions instead. 74 func Out(addr uint16, data UintN) error { 75 port, err := NewPort() 76 if err != nil { 77 return err 78 } 79 defer port.Close() 80 return port.Out(addr, data) 81 }