devicepath/base.go

75 lines
1.6 KiB
Go
Raw Normal View History

2023-08-27 09:31:03 +00:00
package devicepath
2023-08-27 13:09:30 +00:00
import (
"errors"
"strings"
)
2023-08-27 09:31:03 +00:00
2023-08-27 13:09:30 +00:00
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 (
2023-09-18 11:47:44 +00:00
SATAMessaging DeviceNodeSubType = 18
MacMessagingSubType DeviceNodeSubType = 11
IPv4MessagingSubType DeviceNodeSubType = 12
2023-08-27 13:09:30 +00:00
)
const EndOfEntireDevicePath DeviceNodeSubType = 0xFF
const ACPISubType DeviceNodeSubType = 1
2023-09-16 15:21:48 +00:00
const (
HardDriveSubType DeviceNodeSubType = 1
FileSubType DeviceNodeSubType = 4
)
2023-09-16 15:00:46 +00:00
2023-08-27 13:09:30 +00:00
type DevicePathNode interface {
Type() DeviceNodeType
SubType() DeviceNodeSubType
ParseString(raw string) error
2023-08-27 09:31:03 +00:00
String() string
2023-08-27 13:09:30 +00:00
Bytes() []byte
}
var ErrPathComplete = errors.New("device path already complete")
type DevicePath struct {
2023-09-18 12:07:03 +00:00
Nodes []DevicePathNode
2023-08-27 13:09:30 +00:00
}
func (dp *DevicePath) complete() bool {
2023-09-18 12:07:03 +00:00
return len(dp.Nodes) > 0 && dp.Nodes[len(dp.Nodes)-1].Type() == EndOfHardwareDevicePath
2023-08-27 13:09:30 +00:00
}
func (dp *DevicePath) PushNode(node DevicePathNode) error {
if dp.complete() {
return ErrPathComplete
}
2023-09-18 12:07:03 +00:00
dp.Nodes = append(dp.Nodes, node)
2023-08-27 13:09:30 +00:00
return nil
}
func (dp *DevicePath) String() string {
var res []string
2023-09-18 12:07:03 +00:00
for _, node := range dp.Nodes {
2023-08-27 13:09:30 +00:00
if node.String() != "" {
res = append(res, node.String())
}
}
return strings.Join(res, "/")
2023-08-27 09:31:03 +00:00
}