/
bios_directory_table.go
230 lines (202 loc) · 7.63 KB
/
bios_directory_table.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
// Copyright 2019 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 manifest
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"strings"
bytes2 "github.com/linuxboot/fiano/pkg/bytes"
)
// Refer to: AMD Platform Security Processor BIOS Architecture Design Guide for AMD Family 17h and Family 19h
// Processors (NDA), Publication # 55758 Revision: 1.11 Issue Date: August 2020 (1)
// BIOSDirectoryTableCookie is a special identifier of BIOS Directory table level 1
const BIOSDirectoryTableCookie = 0x44484224 // $BHD
// BIOSDirectoryTableLevel2Cookie is a special identifier of BIOS Directory table level 2
const BIOSDirectoryTableLevel2Cookie = 0x324C4224 // $BL2
// BIOSDirectoryTableEntryType is an entry type of BIOS Directory table
type BIOSDirectoryTableEntryType uint8
const (
// APCBDataEntry represents APCB data entry in BIOS Directory table
APCBDataEntry BIOSDirectoryTableEntryType = 0x60
// APOBBinaryEntry denotes APOB binary in BIOS Directory table
APOBBinaryEntry BIOSDirectoryTableEntryType = 0x61
// BIOSRTMVolumeEntry represents BIOS RTM Volume entry in BIOS Directory table
BIOSRTMVolumeEntry BIOSDirectoryTableEntryType = 0x62
// PMUFirmwareInstructionsEntry represents the instruction portion of PMU firmware
PMUFirmwareInstructionsEntry BIOSDirectoryTableEntryType = 0x64
// PMUFirmwareDataEntry represents the data portion of PMU firmware
PMUFirmwareDataEntry BIOSDirectoryTableEntryType = 0x65
// MicrocodePatchEntry represents the microcode patch file location
MicrocodePatchEntry BIOSDirectoryTableEntryType = 0x66
// APCBDataBackupEntry contains a backup copy of APCB data
APCBDataBackupEntry BIOSDirectoryTableEntryType = 0x68
// VideoInterpreterEntry interpreter binary that displays the video image
VideoInterpreterEntry BIOSDirectoryTableEntryType = 0x69
// BIOSDirectoryTableLevel2Entry denotes an entry that points to BIOS Directory table level 2
BIOSDirectoryTableLevel2Entry BIOSDirectoryTableEntryType = 0x70
)
// BIOSDirectoryTableEntry represents a single entry in BIOS Directory Table
// Table 12 from (1)
type BIOSDirectoryTableEntry struct {
Type BIOSDirectoryTableEntryType
RegionType uint8
ResetImage bool
CopyImage bool
ReadOnly bool
Compressed bool
Instance uint8
Subprogram uint8
RomID uint8
Size uint32
SourceAddress uint64
DestinationAddress uint64
}
const BIOSDirectoryTableEntrySize = 16
// BIOSDirectoryTableHeader represents a BIOS Directory Table Header
// Table 11 from (1)
type BIOSDirectoryTableHeader struct {
BIOSCookie uint32
Checksum uint32
TotalEntries uint32
Reserved uint32
}
// BIOSDirectoryTable represents a BIOS Directory Table Header with all entries
// Table 11 & Table 12 from (1)
type BIOSDirectoryTable struct {
BIOSDirectoryTableHeader
Entries []BIOSDirectoryTableEntry
}
func (b BIOSDirectoryTable) String() string {
var s strings.Builder
cookieBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(cookieBytes, b.BIOSCookie)
fmt.Fprintf(&s, "BIOS Cookie: 0x%x (%s)\n", b.BIOSCookie, cookieBytes)
fmt.Fprintf(&s, "Checksum: %d\n", b.Checksum)
fmt.Fprintf(&s, "Total Entries: %d\n", b.TotalEntries)
fmt.Fprintf(&s, "%-5s | %-10s | %-10s | %-9s | %-8s | %-10s | %-8s | %-10s | %-5s | %-6s | %-13s | %-18s\n",
"Type",
"RegionType",
"ResetImage",
"CopyImage",
"ReadOnly",
"Compressed",
"Instance",
"Subprogram",
"RomID",
"Size",
"SourceAddress",
"DestinationAddress")
fmt.Fprintf(&s, "%s\n", "----------------------------------------------------------------------------------------------------------------------------------------------------------------")
for _, entry := range b.Entries {
fmt.Fprintf(&s, "0x%-3x | 0x%-8x | %-10v | %-9v | %-8v | %-10v | 0x%-6x | 0x%-8x | 0x%-3x | %-6d | 0x%-11x | 0x%-18x\n",
entry.Type,
entry.RegionType,
entry.ResetImage,
entry.CopyImage,
entry.ReadOnly,
entry.Compressed,
entry.Instance,
entry.Subprogram,
entry.RomID,
entry.Size,
entry.SourceAddress,
entry.DestinationAddress)
}
return s.String()
}
// FindBIOSDirectoryTable scans firmware for BIOSDirectoryTableCookie
// and treats remaining bytes as BIOSDirectoryTable
func FindBIOSDirectoryTable(image []byte) (*BIOSDirectoryTable, bytes2.Range, error) {
// there is no predefined address, search through the whole memory
var cookieBytes [4]byte
binary.LittleEndian.PutUint32(cookieBytes[:], BIOSDirectoryTableCookie)
var offset uint64
for {
idx := bytes.Index(image, cookieBytes[:])
if idx == -1 {
break
}
table, bytesRead, err := ParseBIOSDirectoryTable(image[idx:])
if err != nil {
shift := uint64(idx + len(cookieBytes))
image = image[shift:]
offset += shift
continue
}
return table, bytes2.Range{Offset: offset + uint64(idx), Length: bytesRead}, err
}
return nil, bytes2.Range{}, fmt.Errorf("BIOSDirectoryTable is not found")
}
// ParseBIOSDirectoryTable converts input bytes into BIOSDirectoryTable
func ParseBIOSDirectoryTable(data []byte) (*BIOSDirectoryTable, uint64, error) {
var table BIOSDirectoryTable
var totalLength uint64
r := bytes.NewBuffer(data)
if err := readAndCountSize(r, binary.LittleEndian, &table.BIOSCookie, &totalLength); err != nil {
return nil, 0, err
}
if table.BIOSCookie != BIOSDirectoryTableCookie && table.BIOSCookie != BIOSDirectoryTableLevel2Cookie {
return nil, 0, fmt.Errorf("incorrect cookie: %d", table.BIOSCookie)
}
if err := readAndCountSize(r, binary.LittleEndian, &table.Checksum, &totalLength); err != nil {
return nil, 0, err
}
if err := readAndCountSize(r, binary.LittleEndian, &table.TotalEntries, &totalLength); err != nil {
return nil, 0, err
}
if err := readAndCountSize(r, binary.LittleEndian, &table.Reserved, &totalLength); err != nil {
return nil, 0, err
}
sizeRequired := uint64(table.TotalEntries) * BIOSDirectoryTableEntrySize
if uint64(r.Len()) < sizeRequired {
return nil, 0, fmt.Errorf("not enough data, required: %d, actual: %d", sizeRequired+totalLength, len(data))
}
table.Entries = make([]BIOSDirectoryTableEntry, 0, table.TotalEntries)
for idx := uint32(0); idx < table.TotalEntries; idx++ {
entry, length, err := ParseBIOSDirectoryTableEntry(r)
if err != nil {
return nil, 0, err
}
table.Entries = append(table.Entries, *entry)
totalLength += length
}
return &table, totalLength, nil
}
// ParseBIOSDirectoryTableEntry converts input bytes into BIOSDirectoryTableEntry
func ParseBIOSDirectoryTableEntry(r io.Reader) (*BIOSDirectoryTableEntry, uint64, error) {
var entry BIOSDirectoryTableEntry
var length uint64
if err := readAndCountSize(r, binary.LittleEndian, &entry.Type, &length); err != nil {
return nil, 0, err
}
if err := readAndCountSize(r, binary.LittleEndian, &entry.RegionType, &length); err != nil {
return nil, 0, err
}
var flags uint8
if err := readAndCountSize(r, binary.LittleEndian, &flags, &length); err != nil {
return nil, 0, err
}
entry.ResetImage = flags&0x1 != 0
entry.CopyImage = (flags>>1)&0x1 != 0
entry.ReadOnly = (flags>>2)&0x1 != 0
entry.Compressed = (flags>>3)&0x1 != 0
entry.Instance = flags >> 4
if err := readAndCountSize(r, binary.LittleEndian, &flags, &length); err != nil {
return nil, 0, err
}
entry.Subprogram = flags & 7
entry.RomID = (flags >> 3) & 0x3
if err := readAndCountSize(r, binary.LittleEndian, &entry.Size, &length); err != nil {
return nil, 0, err
}
if err := readAndCountSize(r, binary.LittleEndian, &entry.SourceAddress, &length); err != nil {
return nil, 0, err
}
if err := readAndCountSize(r, binary.LittleEndian, &entry.DestinationAddress, &length); err != nil {
return nil, 0, err
}
return &entry, length, nil
}