| 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 | // Command lsdimms lists memory DIMM information from SMBIOS. |
| 16 | package main |
| 17 | |
| 18 | import ( |
| 19 | "encoding/binary" |
| 20 | "fmt" |
| 21 | "log" |
| 22 | |
| 23 | "github.com/digitalocean/go-smbios/smbios" |
| 24 | ) |
| 25 | |
| 26 | func main() { |
| 27 | // Find SMBIOS data in operating system-specific location. |
| 28 | rc, ep, err := smbios.Stream() |
| 29 | if err != nil { |
| 30 | log.Fatalf("failed to open stream: %v", err) |
| 31 | } |
| 32 | // Be sure to close the stream! |
| 33 | defer rc.Close() |
| 34 | |
| 35 | // Decode SMBIOS structures from the stream. |
| 36 | d := smbios.NewDecoder(rc) |
| 37 | ss, err := d.Decode() |
| 38 | if err != nil { |
| 39 | log.Fatalf("failed to decode structures: %v", err) |
| 40 | } |
| 41 | |
| 42 | major, minor, rev := ep.Version() |
| 43 | fmt.Printf("SMBIOS %d.%d.%d\n", major, minor, rev) |
| 44 | |
| 45 | for _, s := range ss { |
| 46 | // Only look at memory devices. |
| 47 | if s.Header.Type != 17 { |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | // Code based on: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf. |
| 52 | |
| 53 | // TODO: this should go in a new package in go-smbios for parsing specific structures. |
| 54 | |
| 55 | // Only parse the DIMM size. |
| 56 | dimmSize := int(binary.LittleEndian.Uint16(s.Formatted[8:10])) |
| 57 | |
| 58 | if dimmSize == 0 { |
| 59 | fmt.Printf("[% 3s] empty\n", s.Strings[0]) |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | //If the DIMM size is 32GB or greater, we need to parse the extended field. |
| 64 | // Spec says 0x7fff in regular size field means we should parse the extended. |
| 65 | if dimmSize == 0x7fff { |
| 66 | dimmSize = int(binary.LittleEndian.Uint32(s.Formatted[24:28])) |
| 67 | } |
| 68 | |
| 69 | // The granularity in which the value is specified |
| 70 | // depends on the setting of the most-significant bit (bit |
| 71 | // 15). If the bit is 0, the value is specified in megabyte |
| 72 | // units; if the bit is 1, the value is specified in kilobyte |
| 73 | // units. |
| 74 | // |
| 75 | // Little endian MSB for uint16 is in second byte. |
| 76 | unit := "KB" |
| 77 | if s.Formatted[9]&0x80 == 0 { |
| 78 | unit = "MB" |
| 79 | } |
| 80 | |
| 81 | fmt.Printf("[% 3s] DIMM: %d %s\n", s.Strings[0], dimmSize, unit) |
| 82 | } |
| 83 | } |