/
biosentries.go
285 lines (254 loc) · 9.09 KB
/
biosentries.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2023 the LinuxBoot 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 psb
import (
"errors"
"fmt"
"os"
amd_manifest "github.com/linuxboot/fiano/pkg/amd/manifest"
bytes2 "github.com/linuxboot/fiano/pkg/bytes"
"github.com/jedib0t/go-pretty/v6/table"
)
// BIOSEntryType defines the type to hold BIOS Entry Type fields
type BIOSEntryType uint8
/*
* Nicely output human-readable names for BIOS Entry Types
*
* This doesn't have all the entries mapped, there are still
* several more pages left. It does have all the types
* encountered in the firmware images used to test
* however.
*
*/
func (_type BIOSEntryType) String() string {
switch _type {
case 0x05:
return "BIOS_PUBLIC_KEY"
case 0x07:
return "BIOS_RTM_SIGNATURE"
case 0x60:
return "AGESA_PSP_CUSTOMIZATION_BLOCK"
case 0x61:
return "AGESA_PSP_OUTPUT_BLOCK"
case 0x62:
return "BIOS_BINARY"
case 0x63:
return "AGESA_PSP_OUTPUT_BLOCK_NV_COPY"
case 0x64:
return "PMU_FIRMWARE_INSTRUCTION_PORTION"
case 0x65:
return "PMU_FIRMWARE_DATA_PORTION"
case 0x66:
return "MICROCODE_PATCH"
case 0x67:
return "CORE_MACHINE_EXCEPTION_DATA"
case 0x68:
return "BACKUP_AGESA_PSP_CUSTOMIZATION_BLOCK"
case 0x69:
return "INTERPRETER_BINARY_VIDEO"
case 0x6A:
return "MP2_FIRMWARE_CONFIG"
case 0x6B:
return "MAIN_MEMORY"
case 0x6C:
return "MPM_CONFIG"
case 0x70:
return "BIOS_DIRECTORY_TABLE_LEVEL_2"
}
return "UNKNOWN"
}
func getBIOSTable(pspFirmware *amd_manifest.PSPFirmware, biosLevel uint) (*amd_manifest.BIOSDirectoryTable, error) {
switch biosLevel {
case 1:
return pspFirmware.BIOSDirectoryLevel1, nil
case 2:
return pspFirmware.BIOSDirectoryLevel2, nil
}
return nil, fmt.Errorf("cannot extract key database, invalid BIOS Directory Level requested: %d", biosLevel)
}
// OutputBIOSEntries outputs the BIOS entries in an ASCII table format
func OutputBIOSEntries(amdFw *amd_manifest.AMDFirmware) error {
biosDirectoryLevel1Table, err := getBIOSTable(amdFw.PSPFirmware(), 1)
if err != nil {
return fmt.Errorf("unable to retrieve BIOS Directory Level 1 Entries: %w", err)
}
biosDirectoryLevel2Table, err := getBIOSTable(amdFw.PSPFirmware(), 2)
if err != nil {
return fmt.Errorf("unable to retrieve BIOS Directory Level 2 Entries: %w", err)
}
biosDirectories := []amd_manifest.BIOSDirectoryTable{*biosDirectoryLevel1Table, *biosDirectoryLevel2Table}
for idx, directory := range biosDirectories {
// BIOS Header
h := table.NewWriter()
h.SetOutputMirror(os.Stdout)
h.SetTitle("BIOS Directory Level %d Header", idx+1)
biosCookie := fmt.Sprintf("0x%x", directory.BIOSCookie)
biosChecksum := directory.Checksum
biosTotalEntries := directory.TotalEntries
h.AppendHeader(table.Row{"BIOS Cookie", "Checksum", "Total Entries"})
h.AppendRow([]interface{}{biosCookie, biosChecksum, biosTotalEntries})
h.Render()
// BIOS Entries
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetTitle("BIOS Directory Level %d", idx+1)
t.AppendHeader(table.Row{
"Type",
"Type Hex",
"RegionType",
"ResetImage",
"CopyImage",
"ReadOnly",
"Compressed",
"Instance",
"Subprogram",
"RomID",
"Size",
"Source Address",
"Destination Address",
})
for _, entry := range directory.Entries {
entryType := BIOSEntryType(entry.Type)
entryTypeHex := fmt.Sprintf("0x%-3x", entry.Type)
entryRegionType := fmt.Sprintf("0x%-8x", entry.RegionType)
entryResetImage := fmt.Sprintf("%-10v", entry.ResetImage)
entryCopyImage := fmt.Sprintf("%-9v", entry.CopyImage)
entryReadOnly := fmt.Sprintf("%-8v", entry.ReadOnly)
entryCompressed := fmt.Sprintf("%-10v", entry.Compressed)
entryInstance := fmt.Sprintf("0x%-6x", entry.Instance)
entrySubprogram := fmt.Sprintf("0x%-8x", entry.Subprogram)
entryRomID := fmt.Sprintf("0x%-3x", entry.RomID)
entrySize := fmt.Sprintf("%-6d", entry.Size)
entrySourceAddress := fmt.Sprintf("0x%-11x", entry.SourceAddress)
entryDestinationAddress := fmt.Sprintf("0x%-18x", entry.DestinationAddress)
t.AppendRow([]interface{}{
entryType,
entryTypeHex,
entryRegionType,
entryResetImage,
entryCopyImage,
entryReadOnly,
entryCompressed,
entryInstance,
entrySubprogram,
entryRomID,
entrySize,
entrySourceAddress,
entryDestinationAddress,
})
}
t.Render()
}
return nil
}
// ValidateRTM validates signature of RTM volume and BIOS directory table concatenated
func ValidateRTM(amdFw *amd_manifest.AMDFirmware, biosLevel uint) (*SignatureValidationResult, error) {
pspFw := amdFw.PSPFirmware()
// Get the byte range we'll need on the BIOS depending on the level
var biosDirectoryRange bytes2.Range
var directory DirectoryType
switch biosLevel {
case 1:
biosDirectoryRange = pspFw.BIOSDirectoryLevel1Range
directory = BIOSDirectoryLevel1
case 2:
biosDirectoryRange = pspFw.BIOSDirectoryLevel2Range
directory = BIOSDirectoryLevel2
default:
return nil, fmt.Errorf("cannot extract raw BIOS entry, invalid BIOS Directory Level requested: %d", biosLevel)
}
// extract RTM Volume and signature
rtmVolume, err := ExtractBIOSEntry(amdFw, biosLevel, BIOSRTMVolumeEntry, 0)
if err != nil {
return nil, fmt.Errorf("could not extract BIOS entry corresponding to RTM volume (%x): %w", BIOSRTMVolumeEntry, err)
}
oemKey, err := GetPSBSignBIOSKey(amdFw, biosLevel)
if err != nil {
return nil, err
}
rtmVolumeSignature, err := ExtractBIOSEntry(amdFw, biosLevel, BIOSRTMSignatureEntry, 0)
if err != nil {
return nil, fmt.Errorf("could not extract BIOS entry corresponding to RTM volume signature (%x): %w", BIOSRTMSignatureEntry, err)
}
// signature of RTM volume is calculated over the concatenation of RTM volume itself and
// BIOS directory table
firmwareBytes := amdFw.Firmware().ImageBytes()
biosDirectoryStart := biosDirectoryRange.Offset
biosDirectoryEnd := biosDirectoryStart + biosDirectoryRange.Length
if err := checkBoundaries(biosDirectoryStart, biosDirectoryEnd, firmwareBytes); err != nil {
return nil, newErrInvalidFormatWithItem(newDirectoryItem(directory),
fmt.Errorf("could not extract BIOS Level %d directory, boundary check error: %w", biosLevel, err))
}
/**
* This is needed due to the fact in the Level 2 BIOS Directory Table,
* instead of RTM Volume + Level 2 Header for the signed data, it's actually
* RTM Volume + Level 1 Header + Level 2 Header
*/
if biosLevel == 2 {
biosDirectoryLevel1Start := pspFw.BIOSDirectoryLevel1Range.Offset
biosDirectoryLevel1End := biosDirectoryLevel1Start + pspFw.BIOSDirectoryLevel1Range.Length
if err := checkBoundaries(biosDirectoryLevel1Start, biosDirectoryLevel1End, firmwareBytes); err != nil {
return nil, newErrInvalidFormatWithItem(newDirectoryItem(BIOSDirectoryLevel1),
fmt.Errorf("could not extract BIOS Level 1 directory, boundary check error: %w", err))
}
biosDirectoryTableBytes := firmwareBytes[biosDirectoryLevel1Start:biosDirectoryLevel1End]
rtmVolume = append(rtmVolume, biosDirectoryTableBytes...)
}
biosDirectoryTableBytes := firmwareBytes[biosDirectoryStart:biosDirectoryEnd]
rtmVolume = append(rtmVolume, biosDirectoryTableBytes...)
_, err = NewSignedBlob(reverse(rtmVolumeSignature), rtmVolume, oemKey)
return &SignatureValidationResult{signedElement: "RTM Volume concatenated with BIOS Directory", signingKey: oemKey, err: err}, nil
}
// GetPSBSignBIOSKey returns and OEM Key that is used to sign BIOS during PSB enabled
func GetPSBSignBIOSKey(amdFw *amd_manifest.AMDFirmware, biosLevel uint) (*Key, error) {
keySet, err := GetKeys(amdFw, biosLevel)
if err != nil {
return nil, fmt.Errorf("could not extract key from firmware: %w", err)
}
oemKeySet, err := keySet.KeysetFromType(OEMKey)
if err != nil {
return nil, addFirmwareItemToError(err, newBIOSDirectoryEntryItem(uint8(biosLevel), OEMSigningKeyEntry, 0))
}
oemKeys := oemKeySet.AllKeyIDs()
switch len(oemKeys) {
case 0:
return nil, newErrNotFound(newBIOSDirectoryEntryItem(uint8(biosLevel), OEMSigningKeyEntry, 0))
case 1:
// should be only 1 OEM key
default:
return nil, newErrInvalidFormatWithItem(
newBIOSDirectoryEntryItem(uint8(biosLevel), OEMSigningKeyEntry, 0),
fmt.Errorf("multiple '%d' OEM keys", len(oemKeys)),
)
}
oemKey := keySet.GetKey(oemKeys[0])
if oemKey.data.KeyUsageFlag != PSBSignBIOS {
return nil, newErrInvalidFormatWithItem(
newBIOSDirectoryEntryItem(uint8(biosLevel), OEMSigningKeyEntry, 0),
fmt.Errorf("incorrect key usage '%d', expected: '%d'", oemKey.data.KeyUsageFlag, PSBSignBIOS),
)
}
return oemKey, nil
}
// IsPSBEnabled checks if firmware has PSB enabled
func IsPSBEnabled(amdFw *amd_manifest.AMDFirmware) (bool, error) {
checkPSBEnabled := func(biosLevel uint) (bool, error) {
_, err := GetBIOSEntry(amdFw.PSPFirmware(), 2, OEMSigningKeyEntry, 0)
if err == nil {
return true, nil
}
if errors.As(err, &ErrNotFound{}) {
return false, nil
}
return false, err
}
if amdFw.PSPFirmware().BIOSDirectoryLevel2 != nil {
return checkPSBEnabled(2)
}
if amdFw.PSPFirmware().BIOSDirectoryLevel1 != nil {
return checkPSBEnabled(1)
}
// Can happen in pre-ODM firmware: no bios directories -> no PSB :)
return false, nil
}