Lorenz Brun | f9c65e9 | 2022-11-22 12:50:56 +0000 | [diff] [blame] | 1 | package smbios |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | const ( |
| 8 | structTypeBIOSInformation = 0 |
| 9 | structTypeSystemInformation = 1 |
| 10 | structTypeBaseboardInformation = 2 |
| 11 | structTypeSystemSlot = 9 |
| 12 | structTypeMemoryDevice = 17 |
| 13 | ) |
| 14 | |
| 15 | // BIOSInformationRaw contains decoded data from the BIOS Information structure |
| 16 | // (SMBIOS Type 0). See Table 6 in the specification for detailed documentation |
| 17 | // about the individual fields. Note that structure versions 2.1 and 2.2 are |
| 18 | // "invented" here as both characteristics extensions bytes were optional |
| 19 | // between 2.0 and 2.4. |
| 20 | type BIOSInformationRaw struct { |
| 21 | Handle uint16 |
| 22 | StructureVersion Version |
| 23 | Vendor string |
| 24 | BIOSVersion string |
| 25 | BIOSStartingAddressSegment uint16 |
| 26 | BIOSReleaseDate string |
| 27 | BIOSROMSize uint8 |
| 28 | BIOSCharacteristics uint64 |
| 29 | BIOSCharacteristicsExtensionByte1 uint8 `smbios_min_ver:"2.1"` |
| 30 | BIOSCharacteristicsExtensionByte2 uint8 `smbios_min_ver:"2.2"` |
| 31 | SystemBIOSMajorRelease uint8 `smbios_min_ver:"2.4"` |
| 32 | SystemBIOSMinorRelease uint8 |
| 33 | EmbeddedControllerFirmwareMajorRelease uint8 |
| 34 | EmbeddedControllerFirmwareMinorRelease uint8 |
| 35 | ExtendedBIOSROMSize uint16 `smbios_min_ver:"3.1"` |
| 36 | } |
| 37 | |
| 38 | // ROMSizeBytes returns the ROM size in bytes |
| 39 | func (rb *BIOSInformationRaw) ROMSizeBytes() uint64 { |
| 40 | if rb.StructureVersion.AtLeast(3, 1) && rb.BIOSROMSize == 0xFF { |
| 41 | // Top 2 bits are SI prefix (starting at mega, i.e. 1024^2), lower 14 |
| 42 | // are value. x*1024^n => x << log2(1024)*n => x << 10*n |
| 43 | return uint64(rb.ExtendedBIOSROMSize&0x3fff) << 10 * uint64(rb.ExtendedBIOSROMSize&0xc00+2) |
| 44 | } else { |
| 45 | // (n+1) * 64KiB |
| 46 | return (uint64(rb.BIOSROMSize) + 1) * (64 * 1024) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // ReleaseDate returns the release date of the BIOS as a time.Time value. |
| 51 | func (rb *BIOSInformationRaw) ReleaseDate() (time.Time, error) { |
| 52 | return time.Parse("01/02/2006", rb.BIOSReleaseDate) |
| 53 | } |
| 54 | |
| 55 | // SystemInformationRaw contains decoded data from the System Information |
| 56 | // structure (SMBIOS Type 1). See Table 10 in the specification for detailed |
| 57 | // documentation about the individual fields. |
| 58 | type SystemInformationRaw struct { |
| 59 | Handle uint16 |
| 60 | StructureVersion Version |
| 61 | Manufacturer string |
| 62 | ProductName string |
| 63 | Version string |
| 64 | SerialNumber string |
| 65 | UUID [16]byte `smbios_min_ver:"2.1"` |
| 66 | WakeupType uint8 |
| 67 | SKUNumber string `smbios_min_ver:"2.4"` |
| 68 | Family string |
| 69 | } |
| 70 | |
| 71 | // BaseboardInformationRaw contains decoded data from the BIOS Information |
| 72 | // structure (SMBIOS Type 3). See Table 13 in the specification for detailed |
| 73 | // documentation about the individual fields. |
| 74 | type BaseboardInformationRaw struct { |
| 75 | Handle uint16 |
| 76 | StructureVersion Version |
| 77 | Manufacturer string |
| 78 | Product string |
| 79 | Version string |
| 80 | SerialNumber string |
| 81 | AssetTag string `smbios_min_ver:"2.1"` |
| 82 | FeatureFlags uint8 |
| 83 | LocationInChassis string |
| 84 | ChassisHandle uint16 |
| 85 | BoardType uint8 |
| 86 | NumberOfContainedObjectHandles uint8 |
| 87 | ContainedObjectHandles []uint16 `smbios_repeat:"NumberOfContainedObjectHandles"` |
| 88 | } |
| 89 | |
| 90 | // SystemSlotRaw contains decoded data from the System Slot structure |
| 91 | // (SMBIOS Type 9). See Table 44 in the specification for detailed documentation |
| 92 | // about the individual fields. |
| 93 | type SystemSlotRaw struct { |
| 94 | Handle uint16 |
| 95 | StructureVersion Version |
| 96 | SlotDesignation string |
| 97 | SlotType uint8 |
| 98 | SlotDataBusWidth uint8 |
| 99 | CurrentUsage uint8 |
| 100 | SlotLength uint8 |
| 101 | SlotID uint16 |
| 102 | SlotCharacteristics1 uint8 |
| 103 | SlotCharacteristics2 uint8 `smbios_min_ver:"2.1"` |
| 104 | SegmentGroupNumber uint16 `smbios_min_ver:"2.6"` |
| 105 | BusNumber uint8 |
| 106 | DeviceFunctionNumber uint8 |
| 107 | DataBusWidth uint8 `smbios_min_ver:"3.2"` |
| 108 | PeerGroupingCount uint8 |
| 109 | PeerGroups []SystemSlotPeerRaw `smbios_repeat:"PeerGroupingCount"` |
| 110 | SlotInformation uint8 `smbios_min_ver:"3.4"` |
| 111 | SlotPhysicalWidth uint8 |
| 112 | SlotPitch uint16 |
| 113 | SlotHeight uint8 `smbios_min_ver:"3.5"` |
| 114 | } |
| 115 | |
| 116 | type SystemSlotPeerRaw struct { |
| 117 | SegmentGroupNumber uint16 |
| 118 | BusNumber uint8 |
| 119 | DeviceFunctionNumber uint8 |
| 120 | DataBusWidth uint8 |
| 121 | } |
| 122 | |
| 123 | // MemoryDeviceRaw contains decoded data from the BIOS Information structure |
| 124 | // (SMBIOS Type 17). See Table 76 in the specification for detailed |
| 125 | // documentation about the individual fields. |
| 126 | type MemoryDeviceRaw struct { |
| 127 | Handle uint16 |
| 128 | StructureVersion Version |
| 129 | PhysicalMemoryArrayHandle uint16 `smbios_min_ver:"2.1"` |
| 130 | MemoryErrorInformationHandle uint16 |
| 131 | TotalWidth uint16 |
| 132 | DataWidth uint16 |
| 133 | Size uint16 |
| 134 | FormFactor uint8 |
| 135 | DeviceSet uint8 |
| 136 | DeviceLocator string |
| 137 | BankLocator string |
| 138 | MemoryType uint8 |
| 139 | TypeDetail uint16 |
| 140 | Speed uint16 `smbios_min_ver:"2.3"` |
| 141 | Manufacturer string |
| 142 | SerialNumber string |
| 143 | AssetTag string |
| 144 | PartNumber string |
| 145 | Attributes uint8 `smbios_min_ver:"2.6"` |
| 146 | ExtendedSize uint32 `smbios_min_ver:"2.7"` |
| 147 | ConfiguredMemorySpeed uint16 |
| 148 | MinimumVoltage uint16 `smbios_min_ver:"2.8"` |
| 149 | MaximumVoltage uint16 |
| 150 | ConfiguredVoltage uint16 |
| 151 | MemoryTechnology uint8 `smbios_min_ver:"3.2"` |
| 152 | MemoryOperatingModeCapability uint16 |
| 153 | FirmwareVersion uint8 |
| 154 | ModuleManufacturerID uint16 |
| 155 | ModuleProductID uint16 |
| 156 | MemorySubsystemControllerManufacturerID uint16 |
| 157 | MemorySubsystemControllerProductID uint16 |
| 158 | NonVolatileSize uint64 |
| 159 | VolatileSize uint64 |
| 160 | CacheSize uint64 |
| 161 | LogicalSize uint64 |
| 162 | ExtendedSpeed uint32 `smbios_min_ver:"3.3"` |
| 163 | ExtendedConfiguredMemorySpeed uint32 |
| 164 | } |
| 165 | |
| 166 | func (md *MemoryDeviceRaw) SizeBytes() (uint64, bool) { |
| 167 | if md.Size == 0 || md.Size == 0xFFFF { |
| 168 | // Device unpopulated / unknown memory, return ok false |
| 169 | return 0, false |
| 170 | } |
| 171 | if md.Size == 0x7FFF && md.StructureVersion.AtLeast(2, 7) { |
| 172 | // Bit 31 is reserved, rest is memory size in MiB |
| 173 | return uint64(md.ExtendedSize&0x7FFFFFFF) * (1024 * 1024), true |
| 174 | } |
| 175 | // Bit 15 flips between KiB and MiB, rest is size |
| 176 | return uint64(md.Size&0x7FFF) << 10 * uint64(md.Size&0x8000+1), true |
| 177 | } |