forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocp.go
325 lines (280 loc) · 8.46 KB
/
ocp.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// 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 ocp implements OCP/Facebook-specific IPMI client functions.
package ocp
import (
"bytes"
"encoding/binary"
"strconv"
"strings"
"unsafe"
"github.com/u-root/u-root/pkg/ipmi"
"github.com/u-root/u-root/pkg/smbios"
)
const (
_IPMI_FB_OEM_NET_FUNCTION1 ipmi.NetFn = 0x30
_IPMI_FB_OEM_NET_FUNCTION2 ipmi.NetFn = 0x36
_FB_OEM_SET_PROC_INFO ipmi.Command = 0x10
_FB_OEM_SET_DIMM_INFO ipmi.Command = 0x12
_FB_OEM_SET_BIOS_BOOT_ORDER ipmi.Command = 0x52
_FB_OEM_GET_BIOS_BOOT_ORDER ipmi.Command = 0x53
_FB_OEM_SET_POST_END ipmi.Command = 0x74
)
type ProcessorInfo struct {
ManufacturerID [3]uint8
Index uint8
ParameterSelector uint8
ProductName [48]byte
CoreNumber uint8
ThreadNumberLSB uint8
ThreadNumberMSB uint8
ProcessorFrequencyLSB uint8
ProcessorFrequencyMSB uint8
Revision1 uint8
Revision2 uint8
}
type DimmInfo struct {
ManufacturerID [3]uint8
Index uint8
ParameterSelector uint8
DIMMPresent uint8
NodeNumber uint8
ChannelNumber uint8
DIMMNumber uint8
DIMMType uint8
DIMMSpeed uint16
DIMMSize uint32
ModulePartNumber [20]byte
ModuleSerialNumber uint32
ModuleManufacturerIDLSB uint8
ModuleManufacturerIDMSB uint8
}
// Maps OEM names to a 3 byte OEM number.
// OENs are typically serialized as the first 3 bytes of a request body.
var OENMap = map[string][3]uint8{
"Wiwynn": {0x0, 0x9c, 0x9c},
}
func SendOemIpmiProcessorInfo(i *ipmi.IPMI, info []ProcessorInfo) error {
for index := 0; index < len(info); index++ {
for param := 1; param <= 2; param++ {
data, err := info[index].marshall(param)
if err != nil {
return err
}
_, err = i.SendRecv(_IPMI_FB_OEM_NET_FUNCTION2, _FB_OEM_SET_PROC_INFO, data)
if err != nil {
return err
}
}
}
return nil
}
func SendOemIpmiDimmInfo(i *ipmi.IPMI, info []DimmInfo) error {
for index := 0; index < len(info); index++ {
for param := 1; param <= 6; param++ {
//If DIMM is not present, only send the information of DIMM location
if info[index].DIMMPresent != 0x01 && param >= 2 {
continue
}
data, err := info[index].marshall(param)
if err != nil {
return err
}
_, err = i.SendRecv(_IPMI_FB_OEM_NET_FUNCTION2, _FB_OEM_SET_DIMM_INFO, data)
if err != nil {
return err
}
}
}
return nil
}
func (p *ProcessorInfo) marshall(param int) ([]byte, error) {
var data []byte
buf := &bytes.Buffer{}
if err := binary.Write(buf, binary.LittleEndian, *p); err != nil {
return nil, err
}
buf.Bytes()[4] = byte(param)
switch param {
case 1:
data = make([]byte, 53)
copy(data[:], buf.Bytes()[:53])
case 2:
data = make([]byte, 12)
copy(data[0:5], buf.Bytes()[0:5])
copy(data[5:12], buf.Bytes()[53:60])
}
return data, nil
}
func (d *DimmInfo) marshall(param int) ([]byte, error) {
var data []byte
buf := &bytes.Buffer{}
if err := binary.Write(buf, binary.LittleEndian, *d); err != nil {
return nil, err
}
buf.Bytes()[4] = byte(param)
switch param {
case 1:
data = make([]byte, 9)
copy(data[:], buf.Bytes()[:9])
case 2:
data = make([]byte, 6)
copy(data[0:5], buf.Bytes()[0:5])
copy(data[5:6], buf.Bytes()[9:10])
case 3:
data = make([]byte, 11)
copy(data[0:5], buf.Bytes()[0:5])
copy(data[5:11], buf.Bytes()[10:16])
case 4:
data = make([]byte, 25)
copy(data[0:5], buf.Bytes()[0:5])
copy(data[5:25], buf.Bytes()[16:36])
case 5:
data = make([]byte, 9)
copy(data[0:5], buf.Bytes()[0:5])
copy(data[5:9], buf.Bytes()[36:40])
case 6:
data = make([]byte, 7)
copy(data[0:5], buf.Bytes()[0:5])
copy(data[5:7], buf.Bytes()[40:42])
}
return data, nil
}
func GetOemIpmiProcessorInfo(si *smbios.Info) ([]ProcessorInfo, error) {
t1, err := si.GetSystemInfo()
if err != nil {
return nil, err
}
t4, err := si.GetProcessorInfo()
if err != nil {
return nil, err
}
info := make([]ProcessorInfo, len(t4))
boardManufacturerID, ok := OENMap[t1.Manufacturer]
for index := 0; index < len(t4); index++ {
if ok {
info[index].ManufacturerID = boardManufacturerID
}
info[index].Index = uint8(index)
copy(info[index].ProductName[:], t4[index].Version)
info[index].CoreNumber = uint8(t4[index].GetCoreCount())
info[index].ThreadNumberLSB = uint8(t4[index].GetThreadCount() & 0x00ff)
info[index].ThreadNumberMSB = uint8(t4[index].GetThreadCount() >> 8)
info[index].ProcessorFrequencyLSB = uint8(t4[index].MaxSpeed & 0x00ff)
info[index].ProcessorFrequencyMSB = uint8(t4[index].MaxSpeed >> 8)
info[index].Revision1 = 0
info[index].Revision2 = 0
}
return info, nil
}
// DIMM type: bit[7:6] for DDR3 00-Normal Voltage(1.5V), 01-Ultra Low Voltage(1.25V), 10-Low Voltage(1.35V), 11-Reserved
// for DDR4 00~10-Reserved, 11-Normal Voltage(1.2V)
// bit[5:0] 0x00=SDRAM, 0x01=DDR1 RAM, 0x02-Rambus, 0x03-DDR2 RAM, 0x04-FBDIMM, 0x05-DDR3 RAM, 0x06-DDR4 RAM
func detectDimmType(meminfo *DimmInfo, t17 *smbios.MemoryDevice) {
if t17.Type == smbios.MemoryDeviceTypeDDR3 {
switch t17.ConfiguredVoltage {
case 1500:
meminfo.DIMMType = 0x05
case 1250:
meminfo.DIMMType = 0x45
case 1350:
meminfo.DIMMType = 0x85
default:
meminfo.DIMMType = 0x05
}
} else {
switch t17.Type {
case smbios.MemoryDeviceTypeSDRAM:
meminfo.DIMMType = 0x00
case smbios.MemoryDeviceTypeDDR:
meminfo.DIMMType = 0x01
case smbios.MemoryDeviceTypeRDRAM:
meminfo.DIMMType = 0x02
case smbios.MemoryDeviceTypeDDR2:
meminfo.DIMMType = 0x03
case smbios.MemoryDeviceTypeDDR2FBDIMM:
meminfo.DIMMType = 0x04
case smbios.MemoryDeviceTypeDDR4:
meminfo.DIMMType = 0xC6
default:
meminfo.DIMMType = 0xC6
}
}
}
func GetOemIpmiDimmInfo(si *smbios.Info) ([]DimmInfo, error) {
t1, err := si.GetSystemInfo()
if err != nil {
return nil, err
}
t17, err := si.GetMemoryDevices()
if err != nil {
return nil, err
}
info := make([]DimmInfo, len(t17))
boardManufacturerID, ok := OENMap[t1.Manufacturer]
for index := 0; index < len(t17); index++ {
if ok {
info[index].ManufacturerID = boardManufacturerID
}
info[index].Index = uint8(index)
if t17[index].AssetTag == "NO DIMM" {
info[index].DIMMPresent = 0xFF // 0xFF - Not Present
} else {
info[index].DIMMPresent = 0x01 // 0x01 - Present
}
data := strings.Split(strings.TrimPrefix(t17[index].BankLocator, "_"), "_")
dimm, _ := strconv.ParseUint(strings.TrimPrefix(data[2], "Dimm"), 16, 8)
channel, _ := strconv.ParseUint(strings.TrimPrefix(data[1], "Channel"), 16, 8)
node, _ := strconv.ParseUint(strings.TrimPrefix(data[0], "Node"), 16, 8)
info[index].DIMMNumber = uint8(dimm)
info[index].ChannelNumber = uint8(channel)
info[index].NodeNumber = uint8(node)
detectDimmType(&info[index], t17[index])
info[index].DIMMSpeed = t17[index].Speed
info[index].DIMMSize = uint32(t17[index].Size)
copy(info[index].ModulePartNumber[:], t17[index].PartNumber)
sn, _ := strconv.ParseUint(t17[index].SerialNumber, 16, 32)
info[index].ModuleSerialNumber = uint32(sn)
memoryDeviceManufacturerID, ok := smbios.MemoryDeviceManufacturer[t17[index].Manufacturer]
if ok {
info[index].ModuleManufacturerIDLSB = uint8(memoryDeviceManufacturerID & 0x00ff)
info[index].ModuleManufacturerIDMSB = uint8(memoryDeviceManufacturerID >> 8)
}
}
return info, nil
}
func SetOemIpmiPostEnd(i *ipmi.IPMI) error {
_, err := i.SendRecv(_IPMI_FB_OEM_NET_FUNCTION1, _FB_OEM_SET_POST_END, nil)
if err != nil {
return err
}
return nil
}
// Get BIOS boot order data and check if CMOS clear bit and valid bit are both set
func IsCMOSClearSet(i *ipmi.IPMI) (bool, []byte, error) {
recv, err := i.SendRecv(_IPMI_FB_OEM_NET_FUNCTION1, _FB_OEM_GET_BIOS_BOOT_ORDER, nil)
if err != nil {
return false, nil, err
}
// recv[1] bit 1: CMOS clear, bit 7: valid bit, check if both are set
if len(recv) > 6 && (recv[1]&0x82) == 0x82 {
return true, recv[1:], nil
}
return false, nil, nil
}
// Set BIOS boot order with both CMOS clear and valid bits cleared
func ClearCMOSClearValidBits(i *ipmi.IPMI, data []byte) error {
// Clear bit 1 and bit 7
data[0] &= 0x7d
msg := ipmi.Msg{
Netfn: _IPMI_FB_OEM_NET_FUNCTION1,
Cmd: _FB_OEM_SET_BIOS_BOOT_ORDER,
Data: unsafe.Pointer(&data[0]),
DataLen: 6,
}
if _, err := i.RawSendRecv(msg); err != nil {
return err
}
return nil
}