Debian Codegraph @

smbios/entrypoint.go

raw symbols SCIP
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 "encoding/binary"
20 "fmt"
21 "io"
22 "io/ioutil"
23 )
24
25 // Anchor strings used to detect entry points.
26 var (
27 // Used when searching for an entry point in memory.
28 magicPrefix = []byte("_SM")
29
30 // Used to determine specific entry point types.
31 magic32 = []byte("_SM_")
32 magic64 = []byte("_SM3_")
33 magicDMI = []byte("_DMI_")
34 )
35
36 // An EntryPoint is an SMBIOS entry point. EntryPoints contain various
37 // properties about SMBIOS.
38 //
39 // Use a type assertion to access detailed EntryPoint information.
40 type EntryPoint interface {
41 // Table returns the memory address and maximum size of the SMBIOS table.
42 Table() (address, size int)
43
44 // Version returns the system's SMBIOS version.
45 Version() (major, minor, revision int)
46 }
47
48 // ParseEntryPoint parses an EntryPoint from the input stream.
49 func ParseEntryPoint(r io.Reader) (EntryPoint, error) {
50 // Prevent unbounded reads since this structure should be small.
51 b, err := ioutil.ReadAll(io.LimitReader(r, 64))
52 if err != nil {
53 return nil, err
54 }
55
56 if l := len(b); l < 4 {
57 return nil, fmt.Errorf("too few bytes for SMBIOS entry point magic: %d", l)
58 }
59
60 switch {
61 case bytes.HasPrefix(b, magic32):
62 return parse32(b)
63 case bytes.HasPrefix(b, magic64):
64 return parse64(b)
65 }
66
67 return nil, fmt.Errorf("unrecognized SMBIOS entry point magic: %v", b[0:4])
68 }
69
70 var _ EntryPoint = &EntryPoint32Bit{}
71
72 // EntryPoint32Bit is the SMBIOS 32-bit Entry Point structure, used starting
73 // in SMBIOS 2.1.
74 type EntryPoint32Bit struct {
75 Anchor string
76 Checksum uint8
77 Length uint8
78 Major uint8
79 Minor uint8
80 MaxStructureSize uint16
81 EntryPointRevision uint8
82 FormattedArea [5]byte
83 IntermediateAnchor string
84 IntermediateChecksum uint8
85 StructureTableLength uint16
86 StructureTableAddress uint32
87 NumberStructures uint16
88 BCDRevision uint8
89 }
90
91 // Table implements EntryPoint.
92 func (e *EntryPoint32Bit) Table() (address, size int) {
93 return int(e.StructureTableAddress), int(e.StructureTableLength)
94 }
95
96 // Version implements EntryPoint.
97 func (e *EntryPoint32Bit) Version() (major, minor, revision int) {
98 return int(e.Major), int(e.Minor), 0
99 }
100
101 // parse32 parses an EntryPoint32Bit from b.
102 func parse32(b []byte) (*EntryPoint32Bit, error) {
103 l := len(b)
104
105 // Correct minimum length as of SMBIOS 3.1.1.
106 const expLen = 31
107 if l < expLen {
108 return nil, fmt.Errorf("expected SMBIOS 32-bit entry point minimum length of at least %d, but got: %d", expLen, l)
109 }
110
111 // Allow more data in the buffer than the actual length, for when the
112 // entry point is being read from system memory.
113 length := b[5]
114 if l < int(length) {
115 return nil, fmt.Errorf("expected SMBIOS 32-bit entry point actual length of at least %d, but got: %d", length, l)
116 }
117
118 // Look for intermediate anchor with DMI magic.
119 iAnchor := b[16:21]
120 if !bytes.Equal(iAnchor, magicDMI) {
121 return nil, fmt.Errorf("incorrect DMI magic in SMBIOS 32-bit entry point: %v", iAnchor)
122 }
123
124 // Entry point checksum occurs at index 4, compute and verify it.
125 const epChkIndex = 4
126 epChk := b[epChkIndex]
127 if err := checksum(epChk, epChkIndex, b[:length]); err != nil {
128 return nil, err
129 }
130
131 // Since we already computed the checksum for the outer entry point,
132 // no real need to compute it for the intermediate entry point.
133
134 ep := &EntryPoint32Bit{
135 Anchor: string(b[0:4]),
136 Checksum: epChk,
137 Length: length,
138 Major: b[6],
139 Minor: b[7],
140 MaxStructureSize: binary.LittleEndian.Uint16(b[8:10]),
141 EntryPointRevision: b[10],
142 IntermediateAnchor: string(iAnchor),
143 IntermediateChecksum: b[21],
144 StructureTableLength: binary.LittleEndian.Uint16(b[22:24]),
145 StructureTableAddress: binary.LittleEndian.Uint32(b[24:28]),
146 NumberStructures: binary.LittleEndian.Uint16(b[28:30]),
147 BCDRevision: b[30],
148 }
149 copy(ep.FormattedArea[:], b[10:15])
150
151 return ep, nil
152 }
153
154 var _ EntryPoint = &EntryPoint64Bit{}
155
156 // EntryPoint64Bit is the SMBIOS 64-bit Entry Point structure, used starting
157 // in SMBIOS 3.0.
158 type EntryPoint64Bit struct {
159 Anchor string
160 Checksum uint8
161 Length uint8
162 Major uint8
163 Minor uint8
164 Revision uint8
165 EntryPointRevision uint8
166 Reserved uint8
167 StructureTableMaxSize uint32
168 StructureTableAddress uint64
169 }
170
171 // Table implements EntryPoint.
172 func (e *EntryPoint64Bit) Table() (address, size int) {
173 return int(e.StructureTableAddress), int(e.StructureTableMaxSize)
174 }
175
176 // Version implements EntryPoint.
177 func (e *EntryPoint64Bit) Version() (major, minor, revision int) {
178 return int(e.Major), int(e.Minor), int(e.Revision)
179 }
180
181 const (
182 // expLen64 is the expected minimum length of a 64-bit entry point.
183 // Correct minimum length as of SMBIOS 3.1.1.
184 expLen64 = 24
185
186 // chkIndex64 is the index of the checksum byte in a 64-bit entry point.
187 chkIndex64 = 5
188 )
189
190 // parse64 parses an EntryPoint64Bit from b.
191 func parse64(b []byte) (*EntryPoint64Bit, error) {
192 l := len(b)
193
194 // Ensure expected minimum length.
195 if l < expLen64 {
196 return nil, fmt.Errorf("expected SMBIOS 64-bit entry point minimum length of at least %d, but got: %d", expLen64, l)
197 }
198
199 // Allow more data in the buffer than the actual length, for when the
200 // entry point is being read from system memory.
201 length := b[6]
202 if l < int(length) {
203 return nil, fmt.Errorf("expected SMBIOS 64-bit entry point actual length of at least %d, but got: %d", length, l)
204 }
205
206 // Checksum occurs at index 5, compute and verify it.
207 chk := b[chkIndex64]
208 if err := checksum(chk, chkIndex64, b); err != nil {
209 return nil, err
210 }
211
212 return &EntryPoint64Bit{
213 Anchor: string(b[0:5]),
214 Checksum: chk,
215 Length: length,
216 Major: b[7],
217 Minor: b[8],
218 Revision: b[9],
219 EntryPointRevision: b[10],
220 Reserved: b[11],
221 StructureTableMaxSize: binary.LittleEndian.Uint32(b[12:16]),
222 StructureTableAddress: binary.LittleEndian.Uint64(b[16:24]),
223 }, nil
224 }
225
226 // checksum computes the checksum of b using the starting value of start, and
227 // skipping the checksum byte which occurs at index chkIndex.
228 //
229 // checksum assumes that b has already had its bounds checked.
230 func checksum(start uint8, chkIndex int, b []byte) error {
231 chk := start
232 for i := range b {
233 // Checksum computation does not include index of checksum byte.
234 if i == chkIndex {
235 continue
236 }
237
238 chk += b[i]
239 }
240
241 if chk != 0 {
242 return fmt.Errorf("invalid entry point checksum %#02x from initial checksum %#02x", chk, start)
243 }
244
245 return nil
246 }
247
248 // WindowsEntryPoint contains SMBIOS Table entry point data returned from
249 // GetSystemFirmwareTable. As raw access to the underlying memory is not given,
250 // the full breadth of information is not available.
251 type WindowsEntryPoint struct {
252 Size uint32
253 MajorVersion byte
254 MinorVersion byte
255 Revision byte
256 }
257
258 // Table implements EntryPoint. The returned address will always be 0, as it
259 // is not returned by GetSystemFirmwareTable.
260 func (e *WindowsEntryPoint) Table() (address, size int) {
261 return 0, int(e.Size)
262 }
263
264 // Version implements EntryPoint.
265 func (e *WindowsEntryPoint) Version() (major, minor, revision int) {
266 return int(e.MajorVersion), int(e.MinorVersion), int(e.Revision)
267 }