Skip to content

Commit 1db2c09

Browse files
committed
fix: remove duplicated structs and pointer types in FirmwareStatus
Signed-off-by: Michal Gorlas <michal.gorlas@9elements.com>
1 parent 6eeb266 commit 1db2c09

3 files changed

Lines changed: 46 additions & 103 deletions

File tree

pkg/provisioning/bootguard/bootguard.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,13 @@ func (b *BootGuard) IBBsMatchBPMDigest(image []byte) (bool, error) {
988988
func (b *BootGuard) ValidateMEAgainstManifests(fws *FirmwareStatus) (bool, error) {
989989
switch b.Version {
990990
case bgheader.Version10:
991-
if fws.Status6v16.BPMSVN != uint32(b.VData.BGbpm.BPMSVN) {
991+
if fws.Status6.BPMSVN != uint32(b.VData.BGbpm.BPMSVN) {
992992
return false, fmt.Errorf("bpm svn doesn't match me configuration")
993993
}
994-
if fws.Status6v16.KMSVN != uint32(b.VData.BGkm.KMSVN) {
994+
if fws.Status6.KMSVN != uint32(b.VData.BGkm.KMSVN) {
995995
return false, fmt.Errorf("km svn doesn't match me configuration")
996996
}
997-
if fws.Status6v16.KMID != uint32(b.VData.BGkm.KMID) {
997+
if fws.Status6.KMID != uint32(b.VData.BGkm.KMID) {
998998
return false, fmt.Errorf("km KMID doesn't match me configuration")
999999
}
10001000
case bgheader.Version20:
@@ -1005,32 +1005,28 @@ func (b *BootGuard) ValidateMEAgainstManifests(fws *FirmwareStatus) (bool, error
10051005
}
10061006
switch ver {
10071007
case tools.Version16:
1008-
if fws.Status6v16.BPMSVN > uint32(b.VData.CBNTbpm.BPMSVN) {
1008+
if fws.Status6.BPMSVN > uint32(b.VData.CBNTbpm.BPMSVN) {
10091009
return false, fmt.Errorf("bpm svn doesn't match me configuration")
10101010
}
1011-
km, err := b.cbntKM()
1012-
if err != nil {
1013-
return false, err
1014-
}
1015-
if fws.Status6v16.KMSVN != uint32(km.KMSVN) {
1011+
if fws.Status6.KMSVN != uint32(b.VData.CBNTkm.KMSVN) {
10161012
return false, fmt.Errorf("km svn doesn't match me configuration")
10171013
}
1018-
if fws.Status6v16.KMID != uint32(km.KMID) {
1014+
if fws.Status6.KMID != uint32(b.VData.CBNTkm.KMID) {
10191015
return false, fmt.Errorf("km KMID doesn't match me configuration")
10201016
}
10211017
case tools.Version18, tools.Version21:
10221018
// 18.x/21.x do not expose SVNs of BPM and KM, nor KMID.
10231019
// We can still check few useful facts
1024-
if !fws.Status5v21.BgACMStatus {
1020+
if !fws.Status5.BgACMStatus {
10251021
return false, fmt.Errorf("acm is not active")
10261022
}
1027-
if fws.Status5v21.ErrorCode != 0 {
1023+
if fws.Status5.ErrorCode != 0 {
10281024
return false, fmt.Errorf("bg startup failed")
10291025
}
1030-
if !fws.Status5v21.BPMExecStatus {
1026+
if !fws.Status5.BPMExecStatus {
10311027
return false, fmt.Errorf("bpm not executed")
10321028
}
1033-
if fws.Status5v21.BgStatus != 0x01 {
1029+
if fws.Status5.BgStatus != 0x01 {
10341030
return false, fmt.Errorf("bg status is invalid")
10351031
}
10361032
}

pkg/provisioning/bootguard/hfsts.go

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,39 @@ var hfstsOffset = []int{0x40, 0x48, 0x60, 0x64, 0x68, 0x6c}
1212

1313
type FirmwareStatus struct {
1414
// ME 16
15-
Status1v16 FirmwareStatus1v16
16-
Status6v16 FirmwareStatus6v16
15+
Status1 *FirmwareStatus1
16+
Status6 *FirmwareStatus6
1717
// ME 18/21
18-
Status1v21 FirmwareStatus1v21
19-
Status5v21 FirmwareStatus5v21
20-
Status6v21 FirmwareStatus6v21
18+
Status5 *FirmwareStatus5
2119
}
2220

2321
func NewFirmwareStatus(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus, error) {
2422
// ME 16
2523
hwsts1v16, err := getHFSTS1(hw)
2624
if err != nil {
27-
return &FirmwareStatus{}, err
25+
return nil, err
2826
}
2927

30-
hwsts6v16, err := getHFSTS6(hw)
28+
hwsts6, err := getHFSTS6(hw)
3129
if err != nil {
32-
return &FirmwareStatus{}, err
30+
return nil, err
3331
}
3432

35-
// ME 18/21
36-
hwsts1v21, err := getHFSTS121(hw)
37-
if err != nil {
38-
return &FirmwareStatus{}, err
39-
}
40-
hwsts5v21, err := getHFSTS521(hw)
33+
hwsts5, err := getHFSTS5(hw)
4134
if err != nil {
42-
return &FirmwareStatus{}, err
43-
}
44-
45-
hwsts6v21, err := getHFSTS621(hw)
46-
if err != nil {
47-
return &FirmwareStatus{}, err
35+
return nil, err
4836
}
4937

5038
return &FirmwareStatus{
5139
// ME 16
52-
Status1v16: *hwsts1v16,
53-
Status6v16: *hwsts6v16,
40+
Status1: hwsts1v16,
41+
Status6: hwsts6,
5442
// ME 18/21
55-
Status1v21: *hwsts1v21,
56-
Status5v21: *hwsts5v21,
57-
Status6v21: *hwsts6v21,
43+
Status5: hwsts5,
5844
}, nil
5945
}
6046

61-
type FirmwareStatus1v16 struct {
47+
type FirmwareStatus1 struct {
6248
WorkingState uint32
6349
MfgMode bool
6450
FPTBad bool
@@ -75,7 +61,7 @@ type FirmwareStatus1v16 struct {
7561
BISTResetRequest bool
7662
}
7763

78-
type FirmwareStatus6v16 struct {
64+
type FirmwareStatus6 struct {
7965
ForceACMBootPolicy bool
8066
CPUDebugDisabled bool
8167
BSPInitDisabled bool
@@ -97,13 +83,13 @@ type FirmwareStatus6v16 struct {
9783
TXTSupported bool
9884
}
9985

100-
func getHFSTS1(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus1v16, error) {
86+
func getHFSTS1(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus1, error) {
10187
hfsts1, err := readHFSTSFromPCIConfigSpace(hw, 1)
10288
if err != nil {
10389
return nil, fmt.Errorf("couldn't read HFSTS6 from PCI config space: %v", err)
10490
}
10591

106-
firmwareStatus := FirmwareStatus1v16{}
92+
firmwareStatus := FirmwareStatus1{}
10793

10894
configSpace := binary.LittleEndian.Uint32(hfsts1)
10995

@@ -125,13 +111,13 @@ func getHFSTS1(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus1v16, error)
125111
return &firmwareStatus, nil
126112
}
127113

128-
func getHFSTS6(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus6v16, error) {
114+
func getHFSTS6(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus6, error) {
129115
hfsts6, err := readHFSTSFromPCIConfigSpace(hw, 6)
130116
if err != nil {
131117
return nil, fmt.Errorf("couldn't read HFSTS6 from PCI config space: %v", err)
132118
}
133119

134-
firmwareStatus := FirmwareStatus6v16{}
120+
firmwareStatus := FirmwareStatus6{}
135121

136122
configSpace := binary.LittleEndian.Uint32(hfsts6)
137123
firmwareStatus.ForceACMBootPolicy = (configSpace>>0)&1 != 0
@@ -157,13 +143,7 @@ func getHFSTS6(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus6v16, error)
157143
return &firmwareStatus, nil
158144
}
159145

160-
type FirmwareStatus1v21 struct {
161-
WorkingState uint32
162-
MfgMode bool
163-
OperatingMode uint32
164-
}
165-
166-
type FirmwareStatus5v21 struct {
146+
type FirmwareStatus5 struct {
167147
BgACMStatus bool
168148
VLD bool
169149
RCS bool
@@ -175,33 +155,14 @@ type FirmwareStatus5v21 struct {
175155
BgStatus uint32
176156
}
177157

178-
type FirmwareStatus6v21 struct {
179-
FPFLock bool
180-
}
181-
182-
func getHFSTS121(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus1v21, error) {
183-
hfsts1, err := readHFSTSFromPCIConfigSpace(hw, 1)
184-
if err != nil {
185-
return nil, fmt.Errorf("couldn't read HFSTS5 from PCI config space: %v", err)
186-
}
187-
188-
firmwareStatus := FirmwareStatus1v21{}
189-
190-
configSpace := binary.LittleEndian.Uint32(hfsts1)
191-
firmwareStatus.WorkingState = (configSpace >> 0) & 15
192-
firmwareStatus.MfgMode = (configSpace>>4)&1 != 0
193-
firmwareStatus.OperatingMode = (configSpace >> 16) & 15
194-
195-
return &firmwareStatus, nil
196-
}
197-
198-
func getHFSTS521(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus5v21, error) {
158+
// ME 18/21
159+
func getHFSTS5(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus5, error) {
199160
hfsts5, err := readHFSTSFromPCIConfigSpace(hw, 5)
200161
if err != nil {
201162
return nil, fmt.Errorf("couldn't read HFSTS5 from PCI config space: %v", err)
202163
}
203164

204-
firmwareStatus := FirmwareStatus5v21{}
165+
firmwareStatus := FirmwareStatus5{}
205166

206167
configSpace := binary.LittleEndian.Uint32(hfsts5)
207168
firmwareStatus.BgACMStatus = (configSpace>>0)&1 != 0
@@ -217,20 +178,6 @@ func getHFSTS521(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus5v21, erro
217178
return &firmwareStatus, nil
218179
}
219180

220-
func getHFSTS621(hw hwapi.LowLevelHardwareInterfaces) (*FirmwareStatus6v21, error) {
221-
hfsts6, err := readHFSTSFromPCIConfigSpace(hw, 6)
222-
if err != nil {
223-
return nil, fmt.Errorf("couldn't read HFSTS6 from PCI config space: %v", err)
224-
}
225-
226-
firmwareStatus := FirmwareStatus6v21{}
227-
228-
configSpace := binary.LittleEndian.Uint32(hfsts6)
229-
firmwareStatus.FPFLock = (configSpace>>30)&1 != 0
230-
231-
return &firmwareStatus, nil
232-
}
233-
234181
func readHFSTSFromPCIConfigSpace(hw hwapi.LowLevelHardwareInterfaces, offset int) ([]byte, error) {
235182
if offset < 1 || offset > 6 {
236183
return nil, fmt.Errorf("invalid HFSTS offset")

pkg/provisioning/bootguard/me.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GetBGInfo(hw hwapi.LowLevelHardwareInterfaces) (*BGInfo, error) {
5959
}
6060

6161
func StrictSaneBootGuardProvisioning(v bgheader.BootGuardVersion, fws *FirmwareStatus, bgi *BGInfo) (bool, error) {
62-
if fws.Status6v16.ErrorEnforcementPolicy != EnforcementPolicyShutdownImmediately {
62+
if fws.Status6.ErrorEnforcementPolicy != EnforcementPolicyShutdownImmediately {
6363
return false, fmt.Errorf("enforcement policy isn't set to immediate shutdown")
6464
}
6565

@@ -75,20 +75,20 @@ func SaneMEBootGuardProvisioning(v bgheader.BootGuardVersion, fws *FirmwareStatu
7575

7676
switch ver {
7777
case tools.Version16:
78-
if fws.Status6v16.BypassBootPolicy {
78+
if fws.Status6.BypassBootPolicy {
7979
return false, fmt.Errorf("bypass boot policy is active")
8080
}
81-
if fws.Status6v16.BootPolicyInvalid {
81+
if fws.Status6.BootPolicyInvalid {
8282
return false, fmt.Errorf("boot policy is invalid")
8383
}
84-
if !fws.Status6v16.FPFLock {
84+
if !fws.Status6.FPFLock {
8585
return false, fmt.Errorf("FPF isn't locked")
8686
}
87-
if fws.Status6v16.ErrorEnforcementPolicy == EnforcementPolicyDoNothing ||
88-
fws.Status6v16.ErrorEnforcementPolicy == EnforcementPolicyShutdownSomehow {
87+
if fws.Status6.ErrorEnforcementPolicy == EnforcementPolicyDoNothing ||
88+
fws.Status6.ErrorEnforcementPolicy == EnforcementPolicyShutdownSomehow {
8989
return false, fmt.Errorf("enforcement policy is lazy and doesn't stop boot process")
9090
}
91-
if !fws.Status6v16.ProtectBIOSEnvironment {
91+
if !fws.Status6.ProtectBIOSEnvironment {
9292
return false, fmt.Errorf("protected bios enviroment is disabled")
9393
}
9494
if v == bgheader.Version20 && !bgi.ForceAnchorBoot {
@@ -100,32 +100,32 @@ func SaneMEBootGuardProvisioning(v bgheader.BootGuardVersion, fws *FirmwareStatu
100100
if bgi.ModuleRevoked {
101101
return false, fmt.Errorf("one of the the ACM, BPM and KM may be revoked")
102102
}
103-
if fws.Status6v16.BootGuardDisable {
103+
if fws.Status6.BootGuardDisable {
104104
return false, fmt.Errorf("boot guard is disabled")
105105
}
106106
if !bgi.BootGuardCapability {
107107
return false, fmt.Errorf("missing boot guard microcode updates in FIT")
108108
}
109109
case tools.Version18, tools.Version21:
110-
if !fws.Status6v21.FPFLock {
110+
if !fws.Status6.FPFLock {
111111
return false, fmt.Errorf("FPF is not locked")
112112
}
113-
if fws.Status1v21.MfgMode {
113+
if fws.Status1.MfgMode {
114114
return false, fmt.Errorf("debug mode is enabled")
115115
}
116-
if !fws.Status5v21.VLD {
116+
if !fws.Status5.VLD {
117117
return false, fmt.Errorf("bits that follow are invalid")
118118
}
119-
if fws.Status5v21.RCS {
119+
if fws.Status5.RCS {
120120
return false, fmt.Errorf("RCS does not come from ACM")
121121
}
122-
if !fws.Status5v21.CPUDebugDisabled {
122+
if !fws.Status5.CPUDebugDisabled {
123123
return false, fmt.Errorf("cpu debug is enabled")
124124
}
125-
if fws.Status1v21.WorkingState != 0x05 {
125+
if fws.Status1.WorkingState != 0x05 {
126126
return false, fmt.Errorf("invalid working state")
127127
}
128-
if fws.Status1v21.OperatingMode != 0 {
128+
if fws.Status1.OperatingMode != 0 {
129129
return false, fmt.Errorf("invalid operating mode")
130130
}
131131

0 commit comments

Comments
 (0)