| 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 | //+build linux |
| 16 | |
| 17 | package smbios |
| 18 | |
| 19 | import ( |
| 20 | "io" |
| 21 | "os" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | // sysfs locations for SMBIOS information. |
| 26 | sysfsDMI = "/sys/firmware/dmi/tables/DMI" |
| 27 | sysfsEntryPoint = "/sys/firmware/dmi/tables/smbios_entry_point" |
| 28 | ) |
| 29 | |
| 30 | // stream opens the SMBIOS entry point and an SMBIOS structure stream. |
| 31 | func stream() (io.ReadCloser, EntryPoint, error) { |
| 32 | // First, check for the sysfs location present in modern kernels. |
| 33 | _, err := os.Stat(sysfsEntryPoint) |
| 34 | switch { |
| 35 | case err == nil: |
| 36 | return sysfsStream(sysfsEntryPoint, sysfsDMI) |
| 37 | case os.IsNotExist(err): |
| 38 | // Fall back to the standard UNIX-like system method. |
| 39 | return devMemStream() |
| 40 | default: |
| 41 | return nil, nil, err |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // sysfsStream reads the SMBIOS entry point and structure stream from |
| 46 | // two files; usually the modern sysfs locations. |
| 47 | func sysfsStream(entryPoint, dmi string) (io.ReadCloser, EntryPoint, error) { |
| 48 | epf, err := os.Open(entryPoint) |
| 49 | if err != nil { |
| 50 | return nil, nil, err |
| 51 | } |
| 52 | defer epf.Close() |
| 53 | |
| 54 | ep, err := ParseEntryPoint(epf) |
| 55 | if err != nil { |
| 56 | return nil, nil, err |
| 57 | } |
| 58 | |
| 59 | sf, err := os.Open(dmi) |
| 60 | if err != nil { |
| 61 | return nil, nil, err |
| 62 | } |
| 63 | |
| 64 | return sf, ep, nil |
| 65 | } |