forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pci.go
121 lines (110 loc) · 3.11 KB
/
pci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2012-2017 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.
package pci
import (
"encoding/binary"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// PCI is a PCI device. We will fill this in as we add options.
// For now it just holds two uint16 per the PCI spec.
type PCI struct {
Addr string
Vendor string `pci:"vendor"`
Device string `pci:"device"`
VendorName string
DeviceName string
FullPath string
ExtraInfo []string
}
// String concatenates PCI address, Vendor, and Device and other information
// to make a useful display for the user.
func (p *PCI) String() string {
return strings.Join(append([]string{fmt.Sprintf("%s: %v %v", p.Addr, p.VendorName, p.DeviceName)}, p.ExtraInfo...), "\n")
}
// SetVendorDeviceName changes VendorName and DeviceName from a name to a number,
// if possible.
func (p *PCI) SetVendorDeviceName() {
ids = newIDs()
p.VendorName, p.DeviceName = Lookup(ids, p.Vendor, p.Device)
}
// ReadConfig reads the config space and adds it to ExtraInfo as a hexdump.
func (p *PCI) ReadConfig() error {
c, err := ioutil.ReadFile(filepath.Join(p.FullPath, "config"))
if err != nil {
return err
}
p.ExtraInfo = append(p.ExtraInfo, hex.Dump(c))
return nil
}
type barreg struct {
offset int64
*os.File
}
func (r *barreg) Read(b []byte) (int, error) {
return r.ReadAt(b, r.offset)
}
func (w *barreg) Write(b []byte) (int, error) {
return w.WriteAt(b, w.offset)
}
// ReadConfigRegister reads a configuration register of size 8, 16, 32, or 64.
// It will only work on little-endian machines.
func (p *PCI) ReadConfigRegister(offset, size int64) (uint64, error) {
f, err := os.Open(filepath.Join(p.FullPath, "config"))
if err != nil {
return 0, err
}
defer f.Close()
var reg uint64
r := &barreg{offset: offset, File: f}
switch size {
default:
return 0, fmt.Errorf("%d is not valid: only options are 8, 16, 32, 64", size)
case 64:
err = binary.Read(r, binary.LittleEndian, ®)
case 32:
var val uint32
err = binary.Read(r, binary.LittleEndian, &val)
reg = uint64(val)
case 16:
var val uint16
err = binary.Read(r, binary.LittleEndian, &val)
reg = uint64(val)
case 8:
var val uint8
err = binary.Read(r, binary.LittleEndian, &val)
reg = uint64(val)
}
return reg, err
}
// WriteConfigRegister writes a configuration register of size 8, 16, 32, or 64.
// It will only work on little-endian machines.
func (p *PCI) WriteConfigRegister(offset, size int64, val uint64) error {
f, err := os.OpenFile(filepath.Join(p.FullPath, "config"), os.O_WRONLY, 0)
if err != nil {
return err
}
defer f.Close()
w := &barreg{offset: offset, File: f}
switch size {
default:
return fmt.Errorf("%d is not valid: only options are 8, 16, 32, 64", size)
case 64:
err = binary.Write(w, binary.LittleEndian, &val)
case 32:
var v = uint32(val)
err = binary.Write(w, binary.LittleEndian, &v)
case 16:
var v = uint16(val)
err = binary.Write(w, binary.LittleEndian, &v)
case 8:
var v = uint8(val)
err = binary.Write(w, binary.LittleEndian, &v)
}
return err
}