73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
|
package devicepath
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
pciLength uint16 = 6
|
||
|
pciPrefix string = "Pci"
|
||
|
pciStringLength int = 12
|
||
|
)
|
||
|
|
||
|
type PCIDevicePath struct {
|
||
|
function uint8
|
||
|
device uint8
|
||
|
}
|
||
|
|
||
|
func (pcp *PCIDevicePath) Type() DeviceNodeType {
|
||
|
return HardwareDevicePath
|
||
|
}
|
||
|
|
||
|
func (pcp *PCIDevicePath) SubType() DeviceNodeSubType {
|
||
|
return PCIHardware
|
||
|
}
|
||
|
|
||
|
func (pcp *PCIDevicePath) Bytes() []byte {
|
||
|
res := make([]byte, pciLength)
|
||
|
res[0] = byte(pcp.Type())
|
||
|
res[1] = byte(pcp.SubType())
|
||
|
binary.LittleEndian.PutUint16(res[2:3], pciLength)
|
||
|
res[4] = pcp.function
|
||
|
res[5] = pcp.device
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func (pcp *PCIDevicePath) String() string {
|
||
|
return fmt.Sprintf("%s(0x%x,0x%x)", pciPrefix, pcp.function, pcp.device)
|
||
|
}
|
||
|
|
||
|
func (pcp *PCIDevicePath) ParseString(raw string) error {
|
||
|
if !checkStringFormat(raw, pciPrefix, pciStringLength) {
|
||
|
return ErrMalformedString
|
||
|
}
|
||
|
args := strings.Split(raw[len(pciPrefix)+1:pciStringLength-1], ",")
|
||
|
if len(args) != 2 {
|
||
|
return errors.New("unexpected number of arguments")
|
||
|
}
|
||
|
if function, err := strconv.ParseUint(args[0], 0, 8); err != nil {
|
||
|
return err
|
||
|
} else {
|
||
|
fmt.Println("function", function)
|
||
|
pcp.function = uint8(function)
|
||
|
}
|
||
|
if device, err := strconv.ParseUint(args[1], 0, 8); err != nil {
|
||
|
return err
|
||
|
} else {
|
||
|
fmt.Println("device", device)
|
||
|
pcp.device = uint8(device)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NewPCIDevicePath(function, device uint8) *PCIDevicePath {
|
||
|
return &PCIDevicePath{
|
||
|
function: function,
|
||
|
device: device,
|
||
|
}
|
||
|
}
|