forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry64.go
58 lines (53 loc) · 1.63 KB
/
entry64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2016-2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package smbios
import (
"bytes"
"encoding/binary"
"fmt"
)
// Entry64 is the SMBIOS 64-Bit entry point structure, described in DSP0134 5.2.2.
type Entry64 struct {
Anchor [5]uint8
Checksum uint8
Length uint8
SMBIOSMajorVersion uint8
SMBIOSMinorVersion uint8
SMBIOSDocRev uint8
Revision uint8
Reserved uint8
StructMaxSize uint32
StructTableAddr uint64
}
// UnmarshalBinary unmarshals the SMBIOS 64-Bit entry point structure from binary data.
func (e *Entry64) UnmarshalBinary(data []byte) error {
if len(data) < 0x18 {
return fmt.Errorf("invalid entry point stucture length %d", len(data))
}
if err := binary.Read(bytes.NewReader(data), binary.LittleEndian, e); err != nil {
return err
}
if !bytes.Equal(e.Anchor[:], []byte("_SM3_")) {
return fmt.Errorf("invalid anchor string %q", string(e.Anchor[:]))
}
if int(e.Length) != 0x18 {
return fmt.Errorf("length mismatch: %d vs %d", e.Length, len(data))
}
cs := calcChecksum(data[:e.Length], 5)
if e.Checksum != cs {
return fmt.Errorf("checksum mismatch: 0x%02x vs 0x%02x", e.Checksum, cs)
}
return nil
}
// MarshalBinary marshals the SMBIOS 64-Bit entry point structure to binary data.
func (e *Entry64) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(nil)
if err := binary.Write(buf, binary.LittleEndian, e); err != nil {
return nil, err
}
// Adjust checksum.
data := buf.Bytes()
data[5] = calcChecksum(data, 5)
return data, nil
}