github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/ectool/io.go (about)

     1  // These should all implement io.ReadAt, with the address as the Offset; same for WriteAt.
     2  package main
     3  
     4  import (
     5  	"encoding/binary"
     6  	"os"
     7  )
     8  
     9  type ioaddr uint16
    10  
    11  type ioport interface {
    12  	Outb(ioaddr, uint8) error
    13  	Outw(ioaddr, uint16) error
    14  	Outl(ioaddr, uint32) error
    15  	Outs(ioaddr, []uint8) (int, error)
    16  	// For later, at some point, we may go with this.
    17  	// Not yet.
    18  	//Out(ioaddr, interface{}) (int, error)
    19  	Inb(ioaddr) (uint8, error)
    20  	Inw(ioaddr) (uint16, error)
    21  	Inl(ioaddr) (uint32, error)
    22  	Ins(ioaddr, int) ([]uint8, error)
    23  	//In(ioaddr, interface{}) (int, error)
    24  }
    25  
    26  type debugf func(string, ...interface{})
    27  
    28  func nodebugf(string, ...interface{}) {}
    29  
    30  type devports struct {
    31  	*os.File
    32  	debugf
    33  }
    34  
    35  func newDevPorts(d debugf) (ioport, error) {
    36  	f, err := os.Create("/dev/port")
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	p := &devports{File: f, debugf: nodebugf}
    41  	if d != nil {
    42  		p.debugf = d
    43  	}
    44  
    45  	return p, nil
    46  }
    47  
    48  func (p *devports) Outb(i ioaddr, d uint8) error {
    49  	_, err := p.WriteAt([]byte{d}, int64(i))
    50  	p.debugf("Write 0x%x @ 0x%x\n", d, i)
    51  	return err
    52  }
    53  
    54  func (p *devports) Outs(i ioaddr, d []uint8) (int, error) {
    55  	amt := 0
    56  	for n, b := range d {
    57  		err := p.Outb(i+ioaddr(n), b)
    58  		if err != nil {
    59  			return amt, err
    60  		}
    61  		amt++
    62  	}
    63  	return amt, nil
    64  }
    65  
    66  func (p *devports) Inb(i ioaddr) (uint8, error) {
    67  	d := [1]byte{}
    68  	_, err := p.ReadAt(d[:], int64(i))
    69  	p.debugf("Read 0x%x @ 0x%x\n", d[0], i)
    70  	return d[0], err
    71  }
    72  
    73  func (p *devports) Ins(i ioaddr, amt int) ([]uint8, error) {
    74  	d := make([]byte, amt)
    75  	var err error
    76  	for n := range d {
    77  		d[n], err = p.Inb(i + ioaddr(n))
    78  		if err != nil {
    79  			return nil, err
    80  		}
    81  	}
    82  	return d, nil
    83  }
    84  
    85  func (p *devports) Outw(i ioaddr, d uint16) error {
    86  	b := [2]byte{}
    87  	binary.LittleEndian.PutUint16(b[:], d)
    88  	_, err := p.WriteAt(b[:], int64(i))
    89  	p.debugf("Write 0x%x @ 0x%x\n", d, i)
    90  	return err
    91  }
    92  
    93  func (p *devports) Inw(i ioaddr) (uint16, error) {
    94  	b := [2]byte{}
    95  	_, err := p.ReadAt(b[:], int64(i))
    96  	d := binary.LittleEndian.Uint16(b[:])
    97  	p.debugf("Read 0x%x @ 0x%x\n", d, i)
    98  	return d, err
    99  }
   100  
   101  func (p *devports) Outl(i ioaddr, d uint32) error {
   102  	b := [4]byte{}
   103  	binary.LittleEndian.PutUint32(b[:], d)
   104  	_, err := p.WriteAt(b[:], int64(i))
   105  	p.debugf("Write 0x%x @ 0x%x\n", d, i)
   106  	return err
   107  }
   108  
   109  func (p *devports) Inl(i ioaddr) (uint32, error) {
   110  	b := [4]byte{}
   111  	_, err := p.ReadAt(b[:], int64(i))
   112  	d := binary.LittleEndian.Uint32(b[:])
   113  	p.debugf("Read 0x%x @ 0x%x\n", d, i)
   114  	return d, err
   115  }