github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/memio/memio_ports.go (about) 1 // Copyright 2012-2021 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 memio 6 7 import ( 8 "encoding/binary" 9 "io" 10 "os" 11 12 "github.com/mvdan/u-root-coreutils/pkg/ubinary" 13 ) 14 15 // Reader is the interface for reading from memory and IO ports. 16 type Reader interface { 17 Read(UintN, int64) error 18 } 19 20 // Writer is the interface for writing to memory and IO ports. 21 type Writer interface { 22 Write(UintN, int64) error 23 } 24 25 // ReadWriteCloser implements io.ReadWriteCloser 26 type ReadWriteCloser interface { 27 Reader 28 Writer 29 io.Closer 30 } 31 32 // Port implements memory and IO port access via an os.File. 33 type Port struct { 34 *os.File 35 } 36 37 var _ ReadWriteCloser = &Port{} 38 39 // Read implements Reader for a Port 40 func (m *Port) Read(out UintN, addr int64) error { 41 if _, err := m.File.Seek(addr, io.SeekStart); err != nil { 42 return err 43 } 44 return binary.Read(m.File, ubinary.NativeEndian, out) 45 } 46 47 // Write implements Writer for a Port 48 func (m *Port) Write(in UintN, addr int64) error { 49 if _, err := m.File.Seek(addr, io.SeekStart); err != nil { 50 return err 51 } 52 return binary.Write(m.File, ubinary.NativeEndian, in) 53 } 54 55 // Close implements Close. 56 func (m *Port) Close() error { 57 return m.File.Close() 58 } 59 60 // NewMemIOPort returns a Port, given an os.File. 61 func NewMemIOPort(f *os.File) *Port { 62 return &Port{ 63 File: f, 64 } 65 }