2023-08-27 13:09:30 +00:00
|
|
|
package devicepath
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-09-18 11:55:47 +00:00
|
|
|
pciLength uint16 = 6
|
|
|
|
pciPrefix string = "Pci"
|
2023-08-27 13:09:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2023-09-18 11:55:47 +00:00
|
|
|
if !checkStringFormat(raw, pciPrefix, -1) {
|
2023-08-27 13:09:30 +00:00
|
|
|
return ErrMalformedString
|
|
|
|
}
|
2023-09-18 11:55:47 +00:00
|
|
|
args := strings.Split(raw[len(pciPrefix)+1:len(raw)-1], ",")
|
2023-08-27 13:09:30 +00:00
|
|
|
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 {
|
|
|
|
pcp.function = uint8(function)
|
|
|
|
}
|
|
|
|
if device, err := strconv.ParseUint(args[1], 0, 8); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
pcp.device = uint8(device)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPCIDevicePath(function, device uint8) *PCIDevicePath {
|
|
|
|
return &PCIDevicePath{
|
|
|
|
function: function,
|
|
|
|
device: device,
|
|
|
|
}
|
|
|
|
}
|