Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UUID to only return Middle Endian on SMBIOS Versions >= 2.6 #8

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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