| 1 | // Copyright 2017-2018 DigitalOcean. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package smbios |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "encoding/binary" |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "io/ioutil" |
| 24 | "syscall" |
| 25 | "unsafe" |
| 26 | ) |
| 27 | |
| 28 | // firmwareTableProviderSigRSMB is the identifier for the raw SMBIOS firmware table |
| 29 | // provider. |
| 30 | // It is equal to the ASCII characters 'RSMB' packed into a uint32. |
| 31 | // In the C++ example code in the MSDN documentation, this is specified using |
| 32 | // multi-byte character literals, which are automatically coerced to an integer by |
| 33 | // the C++ compiler. |
| 34 | const firmwareTableProviderSigRSMB uint32 = 0x52534d42 |
| 35 | |
| 36 | // smbiosDataHeaderSize is size of the "header" (non-variable) part of the |
| 37 | // RawSMBIOSData struct. This serves as both the offset to the actual |
| 38 | // SMBIOS table data, and the minimum possible size of a valid RawSMBIOSDATA |
| 39 | // struct (with a table length of 0). |
| 40 | const rawSMBIOSDataHeaderSize = 8 |
| 41 | |
| 42 | var ( |
| 43 | libKernel32 = syscall.NewLazyDLL("kernel32.dll") |
| 44 | |
| 45 | // MSDN Documentation for GetSystemFirmwareTable: |
| 46 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724379(v=vs.85).aspx |
| 47 | procGetSystemFirmwareTable = libKernel32.NewProc("GetSystemFirmwareTable") |
| 48 | ) |
| 49 | |
| 50 | // nativeEndian returns the native byte order of this system. |
| 51 | func nativeEndian() binary.ByteOrder { |
| 52 | // Determine endianness by interpreting a uint16 as a byte slice. |
| 53 | v := uint16(1) |
| 54 | b := *(*[2]byte)(unsafe.Pointer(&v)) |
| 55 | |
| 56 | if b[0] == 1 { |
| 57 | return binary.LittleEndian |
| 58 | } |
| 59 | |
| 60 | return binary.BigEndian |
| 61 | } |
| 62 | |
| 63 | // windowsStream parses the data returned from GetSystemFirmwareTable('RSMB',...) |
| 64 | // and returns a stream and entrypoint that can be used to decode the system's |
| 65 | // SMBIOS table. |
| 66 | // |
| 67 | // When calling GetSystemFirmwareTable with FirmwareTableProviderSignature = "RSMB", |
| 68 | // Windows will write a RawSMBIOSData struct into the output buffer. |
| 69 | // Thus, `buf` is expected to contain a valid RawSMBIOSData structure. |
| 70 | // |
| 71 | // From windows.h: |
| 72 | // |
| 73 | // struct RawSMBIOSData { |
| 74 | // BYTE Used20CallingMethod; |
| 75 | // BYTE SMBIOSMajorVersion; |
| 76 | // BYTE SMBIOSMinorVersion; |
| 77 | // BYTE DMIRevision; |
| 78 | // DWORD Length; |
| 79 | // BYTE SMBIOSTableData[]; |
| 80 | // } |
| 81 | // |
| 82 | // Note: a DWORD is equivalent to a uint32 |
| 83 | // See: https://msdn.microsoft.com/en-us/library/cc230318.aspx |
| 84 | func windowsStream(buf []byte) (io.ReadCloser, EntryPoint, error) { |
| 85 | bufLen := uint32(len(buf)) |
| 86 | |
| 87 | // Do an additional check to make sure the actual amount written is sane. |
| 88 | if bufLen < rawSMBIOSDataHeaderSize { |
| 89 | return nil, nil, fmt.Errorf("GetSystemFirmwareTable wrote less data than expected: wrote %d bytes, expected at least 8 bytes", bufLen) |
| 90 | } |
| 91 | |
| 92 | tableSize := nativeEndian().Uint32(buf[4:8]) |
| 93 | if rawSMBIOSDataHeaderSize+tableSize > bufLen { |
| 94 | return nil, nil, errors.New("reported SMBIOS table size exceeds buffer") |
| 95 | } |
| 96 | |
| 97 | entryPoint := &WindowsEntryPoint{ |
| 98 | MajorVersion: buf[1], |
| 99 | MinorVersion: buf[2], |
| 100 | Revision: buf[3], |
| 101 | Size: tableSize, |
| 102 | } |
| 103 | |
| 104 | tableBuff := buf[rawSMBIOSDataHeaderSize : rawSMBIOSDataHeaderSize+tableSize] |
| 105 | |
| 106 | return ioutil.NopCloser(bytes.NewReader(tableBuff)), entryPoint, nil |
| 107 | } |
| 108 | |
| 109 | func stream() (io.ReadCloser, EntryPoint, error) { |
| 110 | // Call first with empty buffer to get size. |
| 111 | r1, _, err := procGetSystemFirmwareTable.Call( |
| 112 | uintptr(firmwareTableProviderSigRSMB), // FirmwareTableProviderSignature = 'RSMB' |
| 113 | 0, // FirmwareTableID = 0 |
| 114 | 0, // pFirmwareTableBuffer = NULL |
| 115 | 0, // BufferSize = 0 |
| 116 | ) |
| 117 | |
| 118 | // LazyProc.Call will always return err != nil, so we need to check the primary |
| 119 | // return value (r1) to determine whether or not an error occurred. |
| 120 | // In this case, r1 should contain the size of the needed buffer, so it will only |
| 121 | // be 0 if the function call failed for some reason. |
| 122 | // |
| 123 | // Godoc for LazyProc.Call: |
| 124 | // https://golang.org/pkg/syscall/?GOOS=windows&GOARCH=amd64#LazyProc.Call |
| 125 | if r1 == 0 { |
| 126 | return nil, nil, fmt.Errorf("failed to determine size of buffer needed: %v", err) |
| 127 | } |
| 128 | if r1 < rawSMBIOSDataHeaderSize { |
| 129 | return nil, nil, fmt.Errorf("reported buffer size smaller than expected: reported %d, expected >= 8", r1) |
| 130 | } |
| 131 | |
| 132 | bufferSize := uint32(r1) |
| 133 | buffer := make([]byte, bufferSize) |
| 134 | |
| 135 | r1, _, err = procGetSystemFirmwareTable.Call( |
| 136 | uintptr(firmwareTableProviderSigRSMB), // FirmwareTableProviderSignature = 'RSMB' |
| 137 | 0, // FirmwareTableID = 0 |
| 138 | uintptr(unsafe.Pointer(&buffer[0])), // pFirmwareTableBuffer = &buffer |
| 139 | uintptr(bufferSize), // BufferSize = bufferSize |
| 140 | ) |
| 141 | bytesWritten := uint32(r1) |
| 142 | |
| 143 | // Check for the two possible failure cases documented in API: |
| 144 | if bytesWritten > bufferSize { |
| 145 | return nil, nil, fmt.Errorf("buffer size was too small, somehow: have %d bytes, Windows wanted %d bytes", bufferSize, bytesWritten) |
| 146 | } |
| 147 | if bytesWritten == 0 { |
| 148 | return nil, nil, fmt.Errorf("failed to read SMBIOS data: %v", err) |
| 149 | } |
| 150 | |
| 151 | // At this point, bytesWritten <= bufferSize, which means the call succeeded as |
| 152 | // per the MSDN documentation. |
| 153 | |
| 154 | return windowsStream(buffer[:bytesWritten]) |
| 155 | } |