From 08ff7799ab4bc8b00629f8a1a98501b6ee412c64 Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sat, 16 Sep 2023 17:21:48 +0200 Subject: [PATCH] add support for file nodes --- base.go | 5 ++++- file.go | 43 +++++++++++++++++++++++++++++++++++++++++++ file_test.go | 18 ++++++++++++++++++ parser.go | 1 + parser_test.go | 4 ++-- 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 file.go create mode 100644 file_test.go diff --git a/base.go b/base.go index 5b8e5b5..42c119f 100644 --- a/base.go +++ b/base.go @@ -30,7 +30,10 @@ const EndOfEntireDevicePath DeviceNodeSubType = 0xFF const ACPISubType DeviceNodeSubType = 1 -const HardDriveSubType DeviceNodeSubType = 1 +const ( + HardDriveSubType DeviceNodeSubType = 1 + FileSubType DeviceNodeSubType = 4 +) type DevicePathNode interface { Type() DeviceNodeType diff --git a/file.go b/file.go new file mode 100644 index 0000000..0838c34 --- /dev/null +++ b/file.go @@ -0,0 +1,43 @@ +package devicepath + +import ( + "errors" + "fmt" + "strings" +) + +const ( + filePrefix = "File" +) + +type FileDevicePath struct { + path string +} + +func (fp *FileDevicePath) Type() DeviceNodeType { + return MediaDevicePath +} + +func (fp *FileDevicePath) SubType() DeviceNodeSubType { + return FileSubType +} + +func (fp *FileDevicePath) Bytes() []byte { + return nil +} + +func (fp *FileDevicePath) String() string { + return fmt.Sprintf("%s(%s)", filePrefix, fp.path) +} + +func (fp *FileDevicePath) ParseString(raw string) error { + if !checkStringFormat(raw, filePrefix, -1) { + return ErrMalformedString + } + args := strings.Split(raw[len(filePrefix)+1:len(raw)-1], ",") + if len(args) != 1 { + return errors.New("unexpected number of arguments") + } + fp.path = args[0] + return nil +} diff --git a/file_test.go b/file_test.go new file mode 100644 index 0000000..f2a3e80 --- /dev/null +++ b/file_test.go @@ -0,0 +1,18 @@ +package devicepath_test + +import ( + "testing" + + "git.faercol.me/faercol/devicepath" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParseFileString(t *testing.T) { + t.Run("ok", func(t *testing.T) { + var fileDev devicepath.FileDevicePath + expected := `File(\\EFI\\HTTPBOOT\\HTTP_BOOT_CLIENT.EFI)` + require.NoError(t, fileDev.ParseString(expected)) + assert.Equal(t, expected, fileDev.String()) + }) +} diff --git a/parser.go b/parser.go index c7ac1c7..142a495 100644 --- a/parser.go +++ b/parser.go @@ -35,6 +35,7 @@ var prefixTypeAssociation = map[string]nodeGenerator{ pciPrefix: func() DevicePathNode { return &PCIDevicePath{} }, pciRootPrefix: func() DevicePathNode { return &PCIRootDevicePath{} }, hdPrefix: func() DevicePathNode { return &HardDriveDevicePath{} }, + filePrefix: func() DevicePathNode { return &FileDevicePath{} }, } func Parsenode(raw string) (DevicePathNode, error) { diff --git a/parser_test.go b/parser_test.go index 12842da..371dcc2 100644 --- a/parser_test.go +++ b/parser_test.go @@ -18,8 +18,8 @@ func TestOK(t *testing.T) { } func TestParseDevicePathOK(t *testing.T) { - input := "PciRoot(0x0)/Pci(0x1,0x2)/Pci(0x0,0x1)/Sata(0,65535,0)" + input := "PciRoot(0x0)/Pci(0x1,0x2)/Pci(0x0,0x1)/Sata(0,65535,0)/HD(1,GPT,16f06d01-50da-6544-86bd-f3457f980086,0x1000,0x96000)/File(\\EFI\\HTTPBOOT\\HTTP_BOOT_CLIENT.EFI)" res, err := devicepath.ParseDevicePath(input) require.NoError(t, err) - assert.Equal(t, "PciRoot(0x0)/Pci(0x1,0x2)/Pci(0x0,0x1)/Sata(0,65535,0)", res.String()) + assert.Equal(t, "PciRoot(0x0)/Pci(0x1,0x2)/Pci(0x0,0x1)/Sata(0,65535,0)/HD(1,GPT,16f06d01-50da-6544-86bd-f3457f980086,0x1000,0x96000)/File(\\EFI\\HTTPBOOT\\HTTP_BOOT_CLIENT.EFI)", res.String()) }