devicepath/base.go

75 lines
1.6 KiB
Go

package devicepath
import (
"errors"
"strings"
)
type DeviceNodeType uint8
const (
HardwareDevicePath DeviceNodeType = 0x01
ACPIDevicePath DeviceNodeType = 0x02
MessagingDevicePath DeviceNodeType = 0x03
MediaDevicePath DeviceNodeType = 0x04
BIOSBootSpecificationsDevicePath DeviceNodeType = 0x05
EndOfHardwareDevicePath DeviceNodeType = 0x7F
)
type DeviceNodeSubType uint8
const (
PCIHardware DeviceNodeSubType = 0x01
)
const (
SATAMessaging DeviceNodeSubType = 18
MacMessagingSubType DeviceNodeSubType = 11
IPv4MessagingSubType DeviceNodeSubType = 12
IPv6MessagingSubType DeviceNodeSubType = 13
)
const EndOfEntireDevicePath DeviceNodeSubType = 0xFF
const ACPISubType DeviceNodeSubType = 1
const (
HardDriveSubType DeviceNodeSubType = 1
FileSubType DeviceNodeSubType = 4
)
type DevicePathNode interface {
Type() DeviceNodeType
SubType() DeviceNodeSubType
ParseString(raw string) error
String() string
Bytes() []byte
}
var ErrPathComplete = errors.New("device path already complete")
type DevicePath struct {
Nodes []DevicePathNode
}
func (dp *DevicePath) complete() bool {
return len(dp.Nodes) > 0 && dp.Nodes[len(dp.Nodes)-1].Type() == EndOfHardwareDevicePath
}
func (dp *DevicePath) PushNode(node DevicePathNode) error {
if dp.complete() {
return ErrPathComplete
}
dp.Nodes = append(dp.Nodes, node)
return nil
}
func (dp *DevicePath) String() string {
var res []string
for _, node := range dp.Nodes {
if node.String() != "" {
res = append(res, node.String())
}
}
return strings.Join(res, "/")
}