pkg/smbios: fix error calculating memory size
There were two errors in the memory size calculation.
One was that if the high bit is set the unit is KiB, otherwise MiB, not
the other way around.
The other one was that I extracted the bit, but failed to shift it to
the right position. So I took the time to stop this bit twiddling and
added some constants and a good old-fashioned if.
Change-Id: I0bce8f7607981e62120365374458425f3c663b51
Reviewed-on: https://review.monogon.dev/c/monogon/+/1000
Reviewed-by: Sergiusz Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/metropolis/pkg/smbios/structures.go b/metropolis/pkg/smbios/structures.go
index 64ef523..e4bc4d2 100644
--- a/metropolis/pkg/smbios/structures.go
+++ b/metropolis/pkg/smbios/structures.go
@@ -163,6 +163,11 @@
ExtendedConfiguredMemorySpeed uint32
}
+const (
+ kibLeftShift = 10 // 2^10 = 1KiB
+ mibLeftShift = 20 // 2^20 = 1MiB
+)
+
func (md *MemoryDeviceRaw) SizeBytes() (uint64, bool) {
if md.Size == 0 || md.Size == 0xFFFF {
// Device unpopulated / unknown memory, return ok false
@@ -170,8 +175,12 @@
}
if md.Size == 0x7FFF && md.StructureVersion.AtLeast(2, 7) {
// Bit 31 is reserved, rest is memory size in MiB
- return uint64(md.ExtendedSize&0x7FFFFFFF) * (1024 * 1024), true
+ return uint64(md.ExtendedSize&0x7FFFFFFF) << mibLeftShift, true
}
- // Bit 15 flips between KiB and MiB, rest is size
- return uint64(md.Size&0x7FFF) << 10 * uint64(md.Size&0x8000+1), true
+ // Bit 15 flips between MiB and KiB, rest is size
+ var shift uint64 = mibLeftShift
+ if (md.Size & 0x8000) != 0 { // Bit set means KiB
+ shift = kibLeftShift
+ }
+ return uint64(md.Size&0x7FFF) << shift, true
}