forked from medallia/goipmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chassis.go
257 lines (223 loc) · 6.31 KB
/
chassis.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
/*
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ipmi
type ChassisControl uint8
type BootDevice uint8
const (
ControlPowerDown = ChassisControl(0x0)
ControlPowerUp = ChassisControl(0x1)
ControlPowerCycle = ChassisControl(0x2)
ControlPowerHardReset = ChassisControl(0x3)
ControlPowerPulseDiag = ChassisControl(0x4)
ControlPowerAcpiSoft = ChassisControl(0x5)
BootDeviceNone = BootDevice(0x00)
BootDevicePxe = BootDevice(0x04)
BootDeviceDisk = BootDevice(0x08)
BootDeviceSafe = BootDevice(0x0c)
BootDeviceDiag = BootDevice(0x10)
BootDeviceCdrom = BootDevice(0x14)
BootDeviceBios = BootDevice(0x18)
BootDeviceRemoteFloppy = BootDevice(0x1c)
BootDeviceRemotePrimary = BootDevice(0x24)
BootDeviceRemoteCdrom = BootDevice(0x20)
BootDeviceRemoteDisk = BootDevice(0x2c)
BootDeviceFloppy = BootDevice(0x3c)
SystemPower = 0x1
PowerOverload = 0x2
PowerInterlock = 0x4
MainPowerFault = 0x8
PowerControlFault = 0x10
PowerRestorePolicyAlwaysOff = 0x0
PowerRestorePolicyPrevious = 0x1
PowerRestorePolicyAlwaysOn = 0x2
PowerRestorePolicyUnknown = 0x3
PowerEventUnknown = 0x0
PowerEventAcFailed = 0x1
PowerEventOverload = 0x2
PowerEventInterlock = 0x4
PowerEventFault = 0x8
PowerEventCommand = 0x10
ChassisIntrusion = 0x1
FrontPanelLockout = 0x2
DriveFault = 0x4
CoolingFanFault = 0x8
SleepButtonDisable = 0x80
DiagButtonDisable = 0x40
ResetButtonDisable = 0x20
PowerButtonDisable = 0x10
SleepButtonDisabled = 0x08
DiagButtonDisabled = 0x04
ResetButtonDisabled = 0x02
PowerButtonDisabled = 0x01
BootParamSetInProgress = 0x0
BootParamSvcPartSelect = 0x1
BootParamSvcPartScan = 0x2
BootParamFlagValid = 0x3
BootParamInfoAck = 0x4
BootParamBootFlags = 0x5
BootParamInitInfo = 0x6
BootParamInitMbox = 0x7
)
// ChassisStatusRequest per section 28.2
type ChassisStatusRequest struct{}
// ChassisStatusResponse per section 28.2
type ChassisStatusResponse struct {
CompletionCode
PowerState uint8
LastPowerEvent uint8
State uint8
FrontControlPanel uint8
}
// ChassisControlRequest per section 28.3
type ChassisControlRequest struct {
ChassisControl
}
// ChassisControlResponse per section 28.3
type ChassisControlResponse struct {
CompletionCode
}
// SetSystemBootOptionsRequest per section 28.12
type SetSystemBootOptionsRequest struct {
Param uint8
Data []uint8
}
// SetSystemBootOptionsResponse per section 28.12
type SetSystemBootOptionsResponse struct {
CompletionCode
}
// SystemBootOptionsRequest per section 28.13
type SystemBootOptionsRequest struct {
Param uint8
Set uint8
Block uint8
}
// SystemBootOptionsResponse per section 28.13
type SystemBootOptionsResponse struct {
CompletionCode
Version uint8
Param uint8
Data []uint8
}
// MarshalBinary implementation to handle variable length Data
func (r *SetSystemBootOptionsRequest) MarshalBinary() ([]byte, error) {
buf := make([]byte, 1+len(r.Data))
buf[0] = r.Param
copy(buf[1:], r.Data)
return buf, nil
}
var validSetBootOptionsDataLength = map[uint8]int{
BootParamInfoAck: 2,
BootParamBootFlags: 5,
}
// UnmarshalBinary implementation to handle variable length Data
func (r *SetSystemBootOptionsRequest) UnmarshalBinary(buf []byte) error {
if len(buf) < 2 {
return ErrShortPacket
}
r.Param = buf[0]
r.Data = buf[1:]
if l, ok := validSetBootOptionsDataLength[r.Param]; ok {
if len(r.Data) < l {
return ErrShortPacket
}
}
return nil
}
// MarshalBinary implementation to handle variable length Data
func (r *SystemBootOptionsResponse) MarshalBinary() ([]byte, error) {
buf := make([]byte, 3+len(r.Data))
buf[0] = byte(r.CompletionCode)
buf[1] = r.Version
buf[2] = r.Param
copy(buf[3:], r.Data)
return buf, nil
}
// UnmarshalBinary implementation to handle variable length Data
func (r *SystemBootOptionsResponse) UnmarshalBinary(buf []byte) error {
if len(buf) < 3 {
return ErrShortPacket
}
r.CompletionCode = CompletionCode(buf[0])
r.Version = buf[1]
r.Param = buf[2]
r.Data = buf[3:]
return nil
}
// UnmarshalBinary implementation to handle variable length Data
func (r *ChassisStatusResponse) UnmarshalBinary(buf []byte) error {
if len(buf) < 4 {
return ErrShortPacket
}
r.CompletionCode = CompletionCode(buf[0])
r.PowerState = buf[1]
r.LastPowerEvent = buf[2]
r.State = buf[3]
if len(buf) > 4 {
r.FrontControlPanel = buf[4]
} else {
r.FrontControlPanel = 0
}
return nil
}
func (r *SystemBootOptionsResponse) BootDeviceSelector() BootDevice {
return BootDevice(((r.Data[1] >> 2) & 0x0f) << 2)
}
func (s *ChassisStatusResponse) IsSystemPowerOn() bool {
return (s.PowerState & SystemPower) == SystemPower
}
func (s *ChassisStatusResponse) String() string {
if s.IsSystemPowerOn() {
return "on"
}
return "off"
}
func (s *ChassisStatusResponse) PowerRestorePolicy() uint8 {
return (s.PowerState & 0x60) >> 5
}
var bootDeviceStrings = map[BootDevice]string{
BootDeviceNone: "none",
BootDevicePxe: "pxe",
BootDeviceDisk: "disk",
BootDeviceSafe: "safe",
BootDeviceDiag: "diag",
BootDeviceCdrom: "cdrom",
BootDeviceBios: "bios",
BootDeviceRemoteFloppy: "rfloppy",
BootDeviceRemotePrimary: "rprimary",
BootDeviceRemoteCdrom: "rcdrom",
BootDeviceRemoteDisk: "rdisk",
BootDeviceFloppy: "floppy",
}
func (d BootDevice) String() string {
if s, ok := bootDeviceStrings[d]; ok {
return s
}
return "unknown"
}
func (c ChassisControl) String() string {
switch c {
case ControlPowerDown:
return "down"
case ControlPowerUp:
return "up"
case ControlPowerCycle:
return "cycle"
case ControlPowerHardReset:
return "reset"
case ControlPowerPulseDiag:
return "diag"
case ControlPowerAcpiSoft:
return "acpi"
}
panic("unknown control")
}