Skip to content

Commit

Permalink
Make it possible to display the well known string representation of F…
Browse files Browse the repository at this point in the history
…vFile, if there is one
  • Loading branch information
chrisccoulson committed Jun 12, 2024
1 parent 869e0b7 commit 1444637
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
37 changes: 36 additions & 1 deletion devicepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/canonical/go-efilib/internal/ioerr"
"github.com/canonical/go-efilib/internal/uefi"
Expand Down Expand Up @@ -1112,7 +1113,18 @@ func NewFilePathDevicePathNode(path string) (out FilePathDevicePathNode) {
// MediaFvFileDevicePathNode corresponds to a firmware volume file device path node.
type MediaFvFileDevicePathNode GUID

func (d MediaFvFileDevicePathNode) ToString(_ DevicePathToStringFlags) string {
func (d MediaFvFileDevicePathNode) ToString(flags DevicePathToStringFlags) string {
if flags.DisplayOnly() {
fvFileNameLookupMu.Lock()
defer fvFileNameLookupMu.Unlock()

if fvFileNameLookup != nil {
name, known := fvFileNameLookup(GUID(d))
if known {
return fmt.Sprintf("FvFile(%s)", name)
}
}
}
return fmt.Sprintf("FvFile(%s)", GUID(d))
}

Expand All @@ -1131,6 +1143,29 @@ func (d MediaFvFileDevicePathNode) Write(w io.Writer) error {
return binary.Write(w, binary.LittleEndian, &data)
}

var (
fvFileNameLookupMu sync.Mutex
fvFileNameLookup func(GUID) (string, bool)
)

// RegisterMediaFvFileNameLookup registers a function that can map guids to
// strings for well known names, and these will be displayed by
// [MediaFvFileDevicePathNode.String] and [MediaFvFileDevicePathNode.ToString]
// if the flags argument is marked as display only. Note that this does make the
// string representation of the path unparseable, if the string is being used
// in such a way (this package doesn't yet have any ways of parsing device paths
// that are in string form).
//
// Just importing [github.com/canonical/go-efilib/guids] is sufficient to register
// a function that does this. It's included in a separate and optional package for
// systems that are concerned about binary size.
func RegisterMediaFvFileNameLookup(fn func(GUID) (string, bool)) {
fvFileNameLookupMu.Lock()
defer fvFileNameLookupMu.Unlock()

fvFileNameLookup = fn
}

// MediaFvDevicePathNode corresponds to a firmware volume device path node.
type MediaFvDevicePathNode GUID

Expand Down
4 changes: 4 additions & 0 deletions guids/guids.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ func ListAllKnown() []efi.GUID {
sort.Slice(out, func(i, j int) bool { return bytes.Compare(out[i][:], out[j][:]) < 0 })
return out
}

func init() {
efi.RegisterMediaFvFileNameLookup(FileNameString)
}
5 changes: 5 additions & 0 deletions guids/guids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ func (s *guidsSuite) TestListAll(c *C) {
guids := ListAllKnown()
c.Check(guids, DeepEquals, allGuids)
}

func (s *guidsSuite) TestFvFileIntegration(c *C) {
file := efi.MediaFvFileDevicePathNode(efi.MakeGUID(0x821aca26, 0x29ea, 0x4993, 0x839f, [...]byte{0x59, 0x7f, 0xc0, 0x21, 0x70, 0x8d}))
c.Check(file.String(), Equals, "FvFile(AbsoluteAbtInstaller)")
}

0 comments on commit 1444637

Please sign in to comment.