| 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 | "errors" |
| 20 | "io" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | // devMem is the UNIX-like system memory device location used to |
| 27 | // find SMBIOS information. |
| 28 | devMem = "/dev/mem" |
| 29 | |
| 30 | // SMBIOS specification indicates that the entry point should exist |
| 31 | // between these two memory addresses. |
| 32 | startAddr = 0x000f0000 |
| 33 | endAddr = 0x000fffff |
| 34 | ) |
| 35 | |
| 36 | // memoryStream reads the SMBIOS entry point and structure stream from |
| 37 | // an io.ReadSeeker (usually system memory). |
| 38 | // |
| 39 | // memoryStream is an entry point for tests. |
| 40 | func memoryStream(rs io.ReadSeeker, startAddr, endAddr int) (io.ReadCloser, EntryPoint, error) { |
| 41 | // Try to find the entry point. |
| 42 | addr, err := findEntryPoint(rs, startAddr, endAddr) |
| 43 | if err != nil { |
| 44 | return nil, nil, err |
| 45 | } |
| 46 | |
| 47 | // Found it; seek to the location of the entry point. |
| 48 | if _, err := rs.Seek(int64(addr), io.SeekStart); err != nil { |
| 49 | return nil, nil, err |
| 50 | } |
| 51 | |
| 52 | // Read the entry point and determine where the SMBIOS table is. |
| 53 | ep, err := ParseEntryPoint(rs) |
| 54 | if err != nil { |
| 55 | return nil, nil, err |
| 56 | } |
| 57 | |
| 58 | // Seek to the start of the SMBIOS table. |
| 59 | tableAddr, tableSize := ep.Table() |
| 60 | if _, err := rs.Seek(int64(tableAddr), io.SeekStart); err != nil { |
| 61 | return nil, nil, err |
| 62 | } |
| 63 | |
| 64 | // Make a copy of the memory so we don't return a handle to system memory |
| 65 | // to the caller. |
| 66 | out := make([]byte, tableSize) |
| 67 | if _, err := io.ReadFull(rs, out); err != nil { |
| 68 | return nil, nil, err |
| 69 | } |
| 70 | |
| 71 | return ioutil.NopCloser(bytes.NewReader(out)), ep, nil |
| 72 | } |
| 73 | |
| 74 | // findEntryPoint attempts to locate the entry point structure in the io.ReadSeeker |
| 75 | // using the start and end bound as hints for its location. |
| 76 | func findEntryPoint(rs io.ReadSeeker, start, end int) (int, error) { |
| 77 | // Begin searching at the start bound. |
| 78 | if _, err := rs.Seek(int64(start), io.SeekStart); err != nil { |
| 79 | return 0, err |
| 80 | } |
| 81 | |
| 82 | // Iterate one "paragraph" of memory at a time until we either find the entry point |
| 83 | // or reach the end bound. |
| 84 | const paragraph = 16 |
| 85 | b := make([]byte, paragraph) |
| 86 | |
| 87 | var ( |
| 88 | addr int |
| 89 | found bool |
| 90 | ) |
| 91 | |
| 92 | for addr = start; addr < end; addr += paragraph { |
| 93 | if _, err := io.ReadFull(rs, b); err != nil { |
| 94 | return 0, err |
| 95 | } |
| 96 | |
| 97 | // Both the 32-bit and 64-bit entry point have a similar prefix. |
| 98 | if bytes.HasPrefix(b, magicPrefix) { |
| 99 | found = true |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if !found { |
| 105 | return 0, errors.New("no SMBIOS entry point found in memory") |
| 106 | } |
| 107 | |
| 108 | // Return the exact memory location of the entry point. |
| 109 | return addr, nil |
| 110 | } |
| 111 | |
| 112 | // devMemStream reads the SMBIOS entry point and structure stream from |
| 113 | // the UNIX-like system /dev/mem device. |
| 114 | // |
| 115 | // This is UNIX-like system specific, but since it doesn't employ any system |
| 116 | // calls or OS-dependent constants, it remains in this file for simplicity. |
| 117 | func devMemStream() (io.ReadCloser, EntryPoint, error) { |
| 118 | mem, err := os.Open(devMem) |
| 119 | if err != nil { |
| 120 | return nil, nil, err |
| 121 | } |
| 122 | defer mem.Close() |
| 123 | |
| 124 | return memoryStream(mem, startAddr, endAddr) |
| 125 | } |