forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkhiclient_linux.go
284 lines (265 loc) · 8.61 KB
/
mkhiclient_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
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
// Copyright 2020 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 mei
import (
"encoding/binary"
"errors"
"fmt"
"log"
"github.com/u-root/u-root/pkg/pci"
)
// Intel MEI PCI dev IDs,
// from https://elixir.bootlin.com/linux/latest/source/drivers/misc/mei/hw-me-regs.h
var meiDevIDs = map[string]uint16{
// awk '/MEI_DEV_ID/ {print "\t\"" $2 "\": " $3 ","}' hw-me-regs.h
"MEI_DEV_ID_82946GZ": 0x2974,
"MEI_DEV_ID_82G35": 0x2984,
"MEI_DEV_ID_82Q965": 0x2994,
"MEI_DEV_ID_82G965": 0x29A4,
"MEI_DEV_ID_82GM965": 0x2A04,
"MEI_DEV_ID_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x1A9A,
"MEI_DEV_ID_APL_I": 0x5A9A,
"MEI_DEV_ID_DNV_IE": 0x19E5,
"MEI_DEV_ID_GLK": 0x319A,
"MEI_DEV_ID_KBP": 0xA2BA,
"MEI_DEV_ID_KBP_2": 0xA2BB,
"MEI_DEV_ID_CNP_LP": 0x9DE0,
"MEI_DEV_ID_CNP_LP_4": 0x9DE4,
"MEI_DEV_ID_CNP_H": 0xA360,
"MEI_DEV_ID_CNP_H_4": 0xA364,
"MEI_DEV_ID_CMP_LP": 0x02e0,
"MEI_DEV_ID_CMP_LP_3": 0x02e4,
"MEI_DEV_ID_CMP_V": 0xA3BA,
"MEI_DEV_ID_CMP_H": 0x06e0,
"MEI_DEV_ID_CMP_H_3": 0x06e4,
"MEI_DEV_ID_CDF": 0x18D3,
"MEI_DEV_ID_ICP_LP": 0x34E0,
"MEI_DEV_ID_JSP_N": 0x4DE0,
"MEI_DEV_ID_TGP_LP": 0xA0E0,
"MEI_DEV_ID_MCC": 0x4B70,
"MEI_DEV_ID_MCC_4": 0x4B75,
}
// various ME-related constants
const (
// ME - current working state set to normal
meHfs1CwsNormal = 0x5
// ME - current operation mode set to normal
meHfs1ComNormal = 0x0
// ME - CSE's firmware SKU is custom
meHfs3FwSkuCustom = 0x5
pciMEHfsts1 = 0x40
pciMEHfsts3 = 0x60
)
// MKHIClient is a client to send MKHI commands via MEI.
type MKHIClient struct {
MEI *MEI
}
// MKHI command groups, see
// https://github.com/coreboot/coreboot/blob/b8b8ec832360ada5a313f10938bb6cfc310a11eb/src/soc/intel/common/block/include/intelblocks/cse.h#L22
const (
mkhiGroupIDCbm = 0x0
mkhiGroupIDHMRFPO = 0x5
mkhiGroupIDGen = 0xff
)
// MKHI HMRFPO command IDs, see
// https://github.com/coreboot/coreboot/blob/b8b8ec832360ada5a313f10938bb6cfc310a11eb/src/soc/intel/common/block/include/intelblocks/cse.h#L33
const (
mkhiHMRFPOEnable = 0x1
mkhiHMRFPOGetStatus = 0x3
)
// OpenMKHI opens an MKHI client connection.
func OpenMKHI(meiPath string) (*MKHIClient, error) {
m, err := OpenMEI(meiPath, MKHIGuid)
if err != nil {
return nil, fmt.Errorf("failed to open MKHI client: %v", err)
}
return &MKHIClient{MEI: m}, nil
}
// Close closes an MKHI client connection.
func (m *MKHIClient) Close() error {
return m.MEI.Close()
}
// IsHMRFPOEnableAllowed queries whether the HMRFPO enable is allowed.
func (m *MKHIClient) IsHMRFPOEnableAllowed() (bool, error) {
/* This is a reimplementation of cse_is_hmrfpo_enable_allowed from
* coreboot/src/soc/intel/common/block/cse/cse.c . The below comment also
* comes from that function.
*
* Allow sending HMRFPO ENABLE command only if:
* - CSE's current working state is Normal and current operation mode is Normal
* - (or) cse's current working state is normal and current operation mode is
* Soft Temp Disable if CSE's Firmware SKU is Custom
*
*/
meiDev, err := GetMeiPciDevice()
if err != nil {
return false, fmt.Errorf("failed to get PCI MEI device: %v", err)
}
log.Printf("MEI Device found: %s", meiDev)
// check that CSE's current working state is normal
cs, err := meiDev.ReadConfigRegister(pciMEHfsts1, 32)
if err != nil {
return false, fmt.Errorf("PCI config read failed: %v", err)
}
// check that the current working state is ME_HFS1_CWS_NORMAL (0x05) and
// current operation mode is ME_HFS1_COM_NORMAL (0x0).
// `working_state` is bits 1-4 and `operation_state` is bits 7-9.
if (cs&0xf) != meHfs1CwsNormal || ((cs>>6)&0x7) != meHfs1ComNormal {
return false, nil
}
// check that CSE's firmware SKU is not custom, and if it is, that the
// current operation mode is Soft Temp Disable.
cs, err = meiDev.ReadConfigRegister(pciMEHfsts3, 32)
if err != nil {
return false, fmt.Errorf("PCI config read failed: %v", err)
}
// fw_sku is in bits 5-7 . ME_HFS3_FW_SKU_CUSTOM is 0x5
if (cs>>4)&0x7 == meHfs3FwSkuCustom {
// TODO implement the same as coreboot's cse_is_hfs1_com_soft_temp_disable()
return false, errors.New("IsHMRFPOEnableAllowed does not support checking for Soft Temp Disable yet")
}
return true, nil
}
type hmrfpoEnableMsg struct {
header mkhiHdr
nonce uint32
}
func (hem hmrfpoEnableMsg) ToBytes() []byte {
var buf []byte
buf = append(buf, hem.header[:]...)
var nonce [4]byte
binary.LittleEndian.PutUint32(nonce[:], hem.nonce)
return append(buf, nonce[:]...)
}
type hmrfpoEnableResponse struct {
Header mkhiHdr
FctBase uint32
FctLimit uint32
Status uint8
reserved [3]byte
}
func hmrfpoEnableResponseFromBytes(b []byte) (*hmrfpoEnableResponse, error) {
var resp hmrfpoEnableResponse
minlen := len(resp.Header)
maxlen := minlen +
4 /* FctBase */ +
4 /* FctLimit */ +
1 /* Status */ +
3 /* reserved bytes */
if len(b) != minlen && len(b) != maxlen {
return nil, fmt.Errorf("size mismatch, want %d/%d bytes, got %d", minlen, maxlen, len(b))
}
copy(resp.Header[:], b[:4])
if len(b) == minlen {
// don't parse the rest, we got a partial response
return &resp, nil
}
// TODO this could use u-root's pkg/uio
resp.FctBase = binary.LittleEndian.Uint32(b[4:8])
resp.FctLimit = binary.LittleEndian.Uint32(b[8:12])
resp.Status = b[12]
return &resp, nil
}
// EnableHMRFPO enables the HMRFPO (Host ME Region Flash Protection Override) via CSE,
// see cse_hmrfpo_enable at
// https://github.com/coreboot/coreboot/blob/b8b8ec832360ada5a313f10938bb6cfc310a11eb/src/soc/intel/common/block/include/intelblocks/cse.h#L64
func (m *MKHIClient) EnableHMRFPO() error {
var hdr mkhiHdr
hdr.SetGroupID(mkhiGroupIDHMRFPO)
hdr.SetCommand(mkhiHMRFPOEnable)
canEnable, err := m.IsHMRFPOEnableAllowed()
if err != nil {
return fmt.Errorf("enabling HMRFPO failed: %v", err)
}
if !canEnable {
return fmt.Errorf("enabling HMRFPO is not allowed")
}
msg := hmrfpoEnableMsg{
header: hdr,
nonce: 0,
}
if _, err := m.MEI.Write(msg.ToBytes()); err != nil {
return fmt.Errorf("write to MEI failed: %v", err)
}
buf := make([]byte, m.MEI.ClientProperties.MaxMsgLength())
n, err := m.MEI.Read(buf)
if err != nil {
return fmt.Errorf("read from MEI failed: %v", err)
}
resp, err := hmrfpoEnableResponseFromBytes(buf[:n])
if err != nil {
return fmt.Errorf("failed to parse HMRFPOEnableResponse: %v", err)
}
if resp.Header.Result() != 0 {
return fmt.Errorf("failed to enable HMRFPO, request result is 0x%02x, want 0x0", resp.Header.Result())
}
if resp.Status != 0 {
return fmt.Errorf("failed to enable HMRFPO, request status is 0x%02x, want 0x0", resp.Status)
}
return nil
}
// GetMeiPciDevice will return the MEI PCI device object after scanning the PCI
// bus.
func GetMeiPciDevice() (*pci.PCI, error) {
br, err := pci.NewBusReader()
if err != nil {
return nil, fmt.Errorf("failed to create PCI bus reader: %v", err)
}
devices, err := br.Read()
if err != nil {
return nil, fmt.Errorf("failed to scan PCI bus: %v", err)
}
for _, device := range devices {
// look for vendor ID 8086 (Intel)
if device.Vendor != 0x8086 {
continue
}
// look for a known MEI product ID
for _, devID := range meiDevIDs {
if devID == device.Device {
device.SetVendorDeviceName()
// there is only one MEI device, right?
return device, nil
}
}
}
return nil, errors.New("no MEI device found")
}