Skip to content

Commit f3ff722

Browse files
zaolinChriMarMe
authored andcommitted
Add bgheader pkg for header detection
Signed-off-by: Philipp Deppenwiese <zaolin.daisuki@gmail.com>
1 parent 905a56e commit f3ff722

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package bgheader
2+
3+
import (
4+
"encoding/binary"
5+
"fmt"
6+
"io"
7+
)
8+
9+
var (
10+
binaryOrder = binary.LittleEndian
11+
)
12+
13+
type structInfo struct {
14+
ID structureID `json:"StructInfoID"`
15+
Version uint8 `json:"StructInfoVersion"`
16+
}
17+
18+
type structureID [8]byte
19+
20+
type BootGuardVersion uint8
21+
22+
const (
23+
Version10 BootGuardVersion = 1
24+
Version20 BootGuardVersion = 2
25+
)
26+
27+
func (bgv BootGuardVersion) String() string {
28+
switch bgv {
29+
case Version10:
30+
return "1.0"
31+
case Version20:
32+
return "2.0"
33+
}
34+
return "unknown"
35+
}
36+
37+
func DetectBGV(r io.Reader) (BootGuardVersion, error) {
38+
var s structInfo
39+
var bgv BootGuardVersion
40+
41+
err := binary.Read(r, binaryOrder, s.ID[:])
42+
if err != nil {
43+
return 0, fmt.Errorf("unable to read field 'ID': %w", err)
44+
}
45+
err = binary.Read(r, binaryOrder, bgv)
46+
if err != nil {
47+
return 0, fmt.Errorf("unable to read field 'Version': %w", err)
48+
}
49+
50+
return bgv, nil
51+
}

0 commit comments

Comments
 (0)