-
Notifications
You must be signed in to change notification settings - Fork 74
/
ipmb.py
219 lines (170 loc) · 6.34 KB
/
ipmb.py
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
# Copyright (c) 2015 Kontron Europe GmbH
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from array import array
from ..logger import log
from ..msgs import (create_message, create_request_by_name,
encode_message, decode_message, constants)
from ..utils import check_completion_code
def checksum(data):
csum = 0
for b in data:
csum += b
return -csum % 256
class IpmbHeader(object):
"""Representation of the IPMI message header.
Request:
*-------*--------------*----------*-------*---------------*--------*
| rs_sa | netfn/rs_lun | checksum | rq_sa | rq_seq/rq_lun | cmd_id |
*-------*--------------*----------*-------*---------------*--------*
Response:
*-------*--------------*----------*-------*---------------*--------*
| rq_sa | netfn/rq_lun | checksum | rs_sa | rq_seq/rs_lun | cmd_id |
*-------*--------------*----------*-------*---------------*--------*
"""
rs_sa = None
rs_lun = None
rq_sa = None
rq_lun = None
rq_seq = None
netfn = None
cmd_id = None
checksum = None
class IpmbHeaderReq(IpmbHeader):
def encode(self):
data = array('B')
data.append(self.rs_sa)
data.append(self.netfn << 2 | self.rs_lun)
data.append(checksum((self.rs_sa, data[1])))
data.append(self.rq_sa)
data.append(self.rq_seq << 2 | self.rq_lun)
data.append(self.cmd_id)
return data.tostring()
def decode(self):
raise NotImplementedError()
class IpmbHeaderRsp(IpmbHeader):
def encode(self):
raise NotImplementedError()
def decode(self, data):
data = array('B', data)
self.rq_sa = data[0]
self.netfn = data[1] >> 2
self.rq_lun = data[1] & 3
self.checksum = data[2]
self.rs_sa = data[3]
self.rq_seq = data[4] >> 2
self.rs_lun = data[4] & 3
self.cmd_id = data[5]
def encode_ipmb_msg(header, data):
"""Encode an IPMB message.
header: IPMB header object
data: IPMI message data as bytestring
Returns the message as bytestring.
"""
msg = array('B')
msg.fromstring(header.encode())
if data is not None:
a = array('B')
a.fromstring(data)
msg.extend(a)
msg.append(checksum(msg[3:]))
return msg.tostring()
def encode_send_message(payload, rq_sa, rs_sa, channel, seq, tracking=1):
"""Encode a send message command and embedd the message to be send.
payload: the message to be send as bytestring
rq_sa: the requester source address
rs_sa: the responder source address
channel: the channel
seq: the sequence number
tracking: tracking
Returns an encode send message as bytestring
"""
req = create_request_by_name('SendMessage')
req.channel.number = channel
req.channel.tracking = tracking
data = encode_message(req)
header = IpmbHeaderReq()
header.netfn = req.__netfn__
header.rs_lun = 0
header.rs_sa = rs_sa
header.rq_seq = seq
header.rq_lun = 0
header.rq_sa = rq_sa
header.cmd_id = req.__cmdid__
return encode_ipmb_msg(header, data + payload)
def encode_bridged_message(routing, header, payload, seq):
"""Encode a (multi-)bridged command and embedd the message to be send.
routing:
payload: the message to be send as bytestring
header:
seq: the sequence number
Returns the encoded send message as bytestring
"""
# change header requester addresses for bridging
header.rq_sa = routing[-1].rq_sa
header.rs_sa = routing[-1].rs_sa
tx_data = encode_ipmb_msg(header, payload)
for bridge in reversed(routing[:-1]):
tx_data = encode_send_message(tx_data,
rq_sa=bridge.rq_sa,
rs_sa=bridge.rs_sa,
channel=bridge.channel,
seq=seq)
return tx_data
def decode_bridged_message(rx_data):
"""Decode a (multi-)bridged command.
rx_data: the received message as bytestring
Returns the decoded message as bytestring
"""
while array('B', rx_data)[5] == constants.CMDID_SEND_MESSAGE:
rsp = create_message(constants.NETFN_APP + 1,
constants.CMDID_SEND_MESSAGE, None)
decode_message(rsp, rx_data[6:])
check_completion_code(rsp.completion_code)
rx_data = rx_data[7:-1]
if len(rx_data) < 6:
break
return rx_data
def rx_filter(header, data):
"""Check if the message in rx_data matches to the information in header.
The following checks are done:
- Header checksum
- Payload checksum
- NetFn matching
- LUN matching
- Command Id matching
header: the header to compare with
data: the received message as bytestring
"""
rsp_header = IpmbHeaderRsp()
rsp_header.decode(data)
data = array('B', data)
checks = [
(checksum(data[0:3]), 0, 'Header checksum failed'),
(checksum(data[3:]), 0, 'payload checksum failed'),
# rsp_header.rq_sa, header.rq_sa, 'slave address mismatch'),
(rsp_header.netfn, header.netfn | 1, 'NetFn mismatch'),
# rsp_header.rs_sa, header.rs_sa, 'target address mismatch'),
# rsp_header.rq_lun, header.rq_lun, 'request LUN mismatch'),
(rsp_header.rs_lun, header.rs_lun, 'responder LUN mismatch'),
(rsp_header.rq_seq, header.rq_seq, 'sequence number mismatch'),
(rsp_header.cmd_id, header.cmd_id, 'command id mismatch'),
]
match = True
for left, right, msg in checks:
if left != right:
log().debug('{:s}: {:d} {:d}'.format(msg, left, right))
match = False
return match