Skip to content

Commit

Permalink
fix: return UUID in middle endian only on SMBIOS >= 2.6
Browse files Browse the repository at this point in the history
Changes the conversion logic used to return the UUID. Now only
converts to middle endian on smbios >= 2.6.

Signed-off-by: Brandon Nason <brandon.nason@gmail.com>
  • Loading branch information
bnason authored and talos-bot committed Apr 22, 2021
1 parent fb425d4 commit d3a32be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
9 changes: 6 additions & 3 deletions smbios/smbios.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import (

// SMBIOS represents the Sysytem Management BIOS.
type SMBIOS struct {
Version string
Version struct {
Major int
Minor int
Revision int
}
Structures []*smbios.Structure

BIOSInformationStructure BIOSInformationStructure
Expand Down Expand Up @@ -47,8 +51,7 @@ func New() (*SMBIOS, error) {

s := &SMBIOS{}

major, minor, rev := ep.Version()
s.Version = fmt.Sprintf("%d.%d.%d", major, minor, rev)
s.Version.Major, s.Version.Minor, s.Version.Revision = ep.Version()

d := smbios.NewDecoder(rc)

Expand Down
34 changes: 28 additions & 6 deletions smbios/system_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// SystemInformationStructure represents the SMBIOS system information structure.
type SystemInformationStructure struct {
smbios.Structure
smbios *SMBIOS
}

// WakeUp defines the Wake-up Type enum.
Expand Down Expand Up @@ -70,6 +71,8 @@ func (w WakeUp) String() string {

// SystemInformation returns a `SystemInformationStructure`.
func (s SMBIOS) SystemInformation() SystemInformationStructure {
s.SystemInformationStructure.smbios = &s

return s.SystemInformationStructure
}

Expand Down Expand Up @@ -110,23 +113,42 @@ func (s SystemInformationStructure) WakeUpType() string {
}

// UUID returns the system UUID.
// Return middle endian only if SBIOS version >= 2.6.
// Reference: http://dnaeon.github.io/convert-big-endian-uuid-to-middle-endian/
func (s SystemInformationStructure) UUID() (uid uuid.UUID, err error) {
b, err := toMiddleEndian(s.Formatted)
if err != nil {
return uid, fmt.Errorf("failed to convernt to middle endian: %w", err)
var b []byte
if s.smbios.Version.Major >= 3 || (s.smbios.Version.Major == 2 && s.smbios.Version.Minor >= 6) {
b, err = toMiddleEndian(s.Formatted)
if err != nil {
return uid, fmt.Errorf("failed to convert to middle endian: %w", err)
}
} else {
b, err = toBigEndian(s.Formatted)
if err != nil {
return uid, fmt.Errorf("failed to convert to big endian: %w", err)
}
}

uid, err = uuid.FromBytes(b)
if err != nil {
return uid, fmt.Errorf("invalid UUID: %w", err)
}

// TODO(andrewrynhard): Return middle endian only if SBIOS version > 2.6.
// Reference: http://dnaeon.github.io/convert-big-endian-uuid-to-middle-endian/

return uid, nil
}

func toBigEndian(formatted []byte) (b []byte, err error) {
buf := bytes.NewBuffer(make([]byte, 0, 16))

if err := binary.Write(buf, binary.BigEndian, formatted[4:20]); err != nil {
return nil, err
}

b = buf.Bytes()

return b, nil
}

func toMiddleEndian(formatted []byte) (b []byte, err error) {
buf := bytes.NewBuffer(make([]byte, 0, 16))

Expand Down

0 comments on commit d3a32be

Please sign in to comment.