blob: 0d31b3feafb454fdb8af5273774a2fb9233add77 [file] [log] [blame]
Patrick Venture123b5c02019-03-05 14:01:00 -08001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "blob_handler.hpp"
18
19#include "blob_errors.hpp"
20#include "crc.hpp"
21#include "ipmi_errors.hpp"
22
23#include <array>
24#include <cstring>
25
Patrick Venture1470bec2019-03-06 07:33:12 -080026namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080027{
28
29namespace
30{
31const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00};
32}
33
34std::vector<std::uint8_t>
35 BlobHandler::sendIpmiPayload(BlobOEMCommands command,
36 const std::vector<std::uint8_t>& payload)
37{
38 std::vector<std::uint8_t> request, reply, bytes;
39
40 std::copy(ipmiPhosphorOen.begin(), ipmiPhosphorOen.end(),
41 std::back_inserter(request));
42 request.push_back(command);
43
44 if (payload.size() > 0)
45 {
46 /* Grow the vector to hold the bytes. */
47 request.reserve(request.size() + sizeof(std::uint16_t));
48
49 /* CRC required. */
50 std::uint16_t crc = generateCrc(payload);
51 auto src = reinterpret_cast<const std::uint8_t*>(&crc);
52
53 std::copy(src, src + sizeof(crc), std::back_inserter(request));
54
55 /* Copy the payload. */
56 std::copy(payload.begin(), payload.end(), std::back_inserter(request));
57 }
58
59 try
60 {
61 reply = ipmi->sendPacket(request);
62 }
63 catch (const IpmiException& e)
64 {
65 throw BlobException(e.what());
66 }
67
68 /* IPMI_CC was OK, and it returned no bytes, so let's be happy with that for
69 * now.
70 */
71 if (reply.size() == 0)
72 {
73 return reply;
74 }
75
76 /* This cannot be a response because it's smaller than the smallest
77 * response.
78 */
79 if (reply.size() < ipmiPhosphorOen.size())
80 {
81 throw BlobException("Invalid response length");
82 }
83
84 /* Verify the OEN. */
85 if (std::memcmp(ipmiPhosphorOen.data(), reply.data(),
86 ipmiPhosphorOen.size()) != 0)
87 {
88 throw BlobException("Invalid OEN received");
89 }
90
91 /* In this case there was no data, as there was no CRC. */
92 std::size_t headerSize = ipmiPhosphorOen.size() + sizeof(std::uint16_t);
93 if (reply.size() < headerSize)
94 {
95 return {};
96 }
97
98 /* Validate CRC. */
99 std::uint16_t crc;
100 auto ptr = reinterpret_cast<std::uint8_t*>(&crc);
101 std::memcpy(ptr, &reply[ipmiPhosphorOen.size()], sizeof(crc));
102
103 for (const auto& byte : reply)
104 {
105 std::fprintf(stderr, "0x%02x ", byte);
106 }
107 std::fprintf(stderr, "\n");
108
109 bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end());
110
111 auto computed = generateCrc(bytes);
112 if (crc != computed)
113 {
114 std::fprintf(stderr, "Invalid CRC, received: 0x%x, computed: 0x%x\n",
115 crc, computed);
116 throw BlobException("Invalid CRC on received data.");
117 }
118
119 return bytes;
120}
121
122int BlobHandler::getBlobCount()
123{
124 std::uint32_t count;
125 try
126 {
127 auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobGetCount, {});
128 if (resp.size() != sizeof(count))
129 {
130 return 0;
131 }
132
133 /* LE to LE (need to make this portable as some point. */
134 std::memcpy(&count, resp.data(), sizeof(count));
135 }
136 catch (const BlobException& b)
137 {
138 return 0;
139 }
140
Patrick Venturea8356422019-03-15 21:16:33 -0700141 std::fprintf(stderr, "BLOB Count: %u\n", count);
Patrick Venture123b5c02019-03-05 14:01:00 -0800142 return count;
143}
144
145std::string BlobHandler::enumerateBlob(std::uint32_t index)
146{
147 std::vector<std::uint8_t> payload;
148 auto data = reinterpret_cast<const std::uint8_t*>(&index);
149
150 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
151
152 try
153 {
154 auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
155 return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
156 : "";
157 }
158 catch (const BlobException& b)
159 {
160 return "";
161 }
162}
163
164void BlobHandler::writeGeneric(BlobOEMCommands command, std::uint16_t session,
165 std::uint32_t offset,
166 const std::vector<std::uint8_t>& bytes)
167{
168 std::vector<std::uint8_t> payload;
169
170 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
171 bytes.size());
172
173 auto data = reinterpret_cast<const std::uint8_t*>(&session);
174 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
175
176 data = reinterpret_cast<const std::uint8_t*>(&offset);
177 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
178
179 std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload));
180
181 auto resp = sendIpmiPayload(command, payload);
182}
183
184void BlobHandler::writeMeta(std::uint16_t session, std::uint32_t offset,
185 const std::vector<std::uint8_t>& bytes)
186{
187 return writeGeneric(BlobOEMCommands::bmcBlobWriteMeta, session, offset,
188 bytes);
189}
190
191void BlobHandler::writeBytes(std::uint16_t session, std::uint32_t offset,
192 const std::vector<std::uint8_t>& bytes)
193{
194 return writeGeneric(BlobOEMCommands::bmcBlobWrite, session, offset, bytes);
195}
196
197std::vector<std::string> BlobHandler::getBlobList()
198{
199 std::vector<std::string> list;
200 int blobCount = getBlobCount();
201
202 for (int i = 0; i < blobCount; i++)
203 {
204 auto name = enumerateBlob(i);
205 /* Currently ignore failures. */
206 if (!name.empty())
207 {
208 list.push_back(name);
209 }
210 }
211
212 return list;
213}
214
215StatResponse BlobHandler::getStat(const std::string& id)
216{
217 StatResponse meta;
218 std::vector<std::uint8_t> name, resp;
219
220 std::copy(id.begin(), id.end(), std::back_inserter(name));
221 name.push_back(0x00); /* need to add nul-terminator. */
222
223 try
224 {
225 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobStat, name);
226 }
227 catch (const BlobException& b)
228 {
229 throw;
230 }
231
232 std::memcpy(&meta.blob_state, &resp[0], sizeof(meta.blob_state));
233 std::memcpy(&meta.size, &resp[sizeof(meta.blob_state)], sizeof(meta.size));
234 int offset = sizeof(meta.blob_state) + sizeof(meta.size);
235 std::uint8_t len = resp[offset];
236 if (len > 0)
237 {
238 std::copy(&resp[offset + 1], &resp[resp.size()],
239 std::back_inserter(meta.metadata));
240 }
241
242 return meta;
243}
244
Patrick Venture16a99a62019-05-03 17:21:30 -0700245StatResponse BlobHandler::getStat(std::uint16_t session)
246{
247 StatResponse meta;
248 std::vector<std::uint8_t> resp;
249 std::vector<std::uint8_t> request;
250 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
251 std::copy(addrSession, addrSession + sizeof(session),
252 std::back_inserter(request));
253
254 try
255 {
256 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobSessionStat, request);
257 }
258 catch (const BlobException& b)
259 {
260 throw;
261 }
262
263 std::memcpy(&meta.blob_state, &resp[0], sizeof(meta.blob_state));
264 std::memcpy(&meta.size, &resp[sizeof(meta.blob_state)], sizeof(meta.size));
265 int offset = sizeof(meta.blob_state) + sizeof(meta.size);
266 std::uint8_t len = resp[offset];
267 if (len > 0)
268 {
269 std::copy(&resp[offset + 1], &resp[resp.size()],
270 std::back_inserter(meta.metadata));
271 }
272
273 return meta;
274}
275
Patrick Venture123b5c02019-03-05 14:01:00 -0800276std::uint16_t BlobHandler::openBlob(const std::string& id,
277 std::uint16_t handlerFlags)
278{
279 std::uint16_t session;
280 std::vector<std::uint8_t> request, resp;
281 auto addrFlags = reinterpret_cast<const std::uint8_t*>(&handlerFlags);
282
283 std::copy(addrFlags, addrFlags + sizeof(handlerFlags),
284 std::back_inserter(request));
285 std::copy(id.begin(), id.end(), std::back_inserter(request));
286 request.push_back(0x00); /* need to add nul-terminator. */
287
288 try
289 {
290 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
291 }
292 catch (const BlobException& b)
293 {
294 throw;
295 }
296
297 if (resp.size() != sizeof(session))
298 {
299 throw BlobException("Did not receive session.");
300 }
301
302 std::memcpy(&session, resp.data(), sizeof(session));
303 return session;
304}
305
306void BlobHandler::closeBlob(std::uint16_t session)
307{
308 std::vector<std::uint8_t> request;
309 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
310 std::copy(addrSession, addrSession + sizeof(session),
311 std::back_inserter(request));
312
313 try
314 {
315 sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request);
316 }
317 catch (const BlobException& b)
318 {
319 std::fprintf(stderr, "Received failure on close: %s\n", b.what());
320 }
321
322 return;
323}
324
325std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session,
326 std::uint32_t offset,
327 std::uint32_t length)
328{
329 std::vector<std::uint8_t> payload;
330
331 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
332 sizeof(std::uint32_t));
333
334 auto data = reinterpret_cast<const std::uint8_t*>(&session);
335 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
336
337 data = reinterpret_cast<const std::uint8_t*>(&offset);
338 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
339
340 data = reinterpret_cast<const std::uint8_t*>(&length);
341 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
342
343 return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload);
344}
345
Patrick Venture1470bec2019-03-06 07:33:12 -0800346} // namespace ipmiblob