-
Notifications
You must be signed in to change notification settings - Fork 1
/
smbios_linux.go
85 lines (76 loc) · 1.92 KB
/
smbios_linux.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2016-2021 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 (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var systabPath = "/sys/firmware/efi/systab"
// EntryBase returns SMBIOS base pointer, which points to the entry point address.
func EntryBase() (int64, int64, error) {
base, size, err := EntryBaseFromEFI()
if err != nil {
base, size, err = EntryBaseFromLegacy()
if err != nil {
return 0, 0, err
}
}
return base, size, nil
}
// EntryBaseFromLegacy searches in SMBIOS entry point address in F0000 segment.
//
// NOTE: Legacy BIOS will store their SMBIOS in this region.
func EntryBaseFromLegacy() (int64, int64, error) {
f, err := os.Open("/dev/mem")
if err != nil {
return 0, 0, err
}
defer f.Close()
return getMemBase(f, 0xf0000, 0x100000)
}
// EntryBaseFromEFI finds the SMBIOS entry point address in the EFI System Table.
func EntryBaseFromEFI() (base int64, size int64, err error) {
return entryBaseFromEFI(systabPath)
}
func entryBaseFromEFI(path string) (base int64, size int64, err error) {
file, err := os.Open(path)
if err != nil {
return 0, 0, err
}
defer file.Close()
const (
smbios3 = "SMBIOS3="
smbios = "SMBIOS="
)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
start := ""
size := int64(0)
if strings.HasPrefix(line, smbios3) {
start = strings.TrimPrefix(line, smbios3)
size = smbios3HeaderSize
}
if strings.HasPrefix(line, smbios) {
start = strings.TrimPrefix(line, smbios)
size = smbios2HeaderSize
}
if start == "" {
continue
}
base, err := strconv.ParseInt(start, 0, 63)
if err != nil {
continue
}
return base, size, nil
}
if err := scanner.Err(); err != nil {
log.Printf("error while reading EFI systab: %v", err)
}
return 0, 0, fmt.Errorf("invalid /sys/firmware/efi/systab file")
}