blob: 039b7e0ded9a32b77b3e5607840c643429c82b9b [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"
Patrick Venture22fcc842019-05-13 09:06:24 -070022#include "ipmi_interface.hpp"
Patrick Venture123b5c02019-03-05 14:01:00 -080023
24#include <array>
25#include <cstring>
Patrick Venture800f06d2019-05-14 14:43:58 -070026#include <limits>
Patrick Venture22fcc842019-05-13 09:06:24 -070027#include <memory>
Patrick Venture123b5c02019-03-05 14:01:00 -080028
Patrick Venture1470bec2019-03-06 07:33:12 -080029namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080030{
31
32namespace
33{
34const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00};
35}
36
Patrick Venture22fcc842019-05-13 09:06:24 -070037std::unique_ptr<BlobInterface>
38 BlobHandler::CreateBlobHandler(std::unique_ptr<IpmiInterface> ipmi)
39{
40 return std::make_unique<BlobHandler>(std::move(ipmi));
41}
42
Patrick Venture123b5c02019-03-05 14:01:00 -080043std::vector<std::uint8_t>
44 BlobHandler::sendIpmiPayload(BlobOEMCommands command,
45 const std::vector<std::uint8_t>& payload)
46{
47 std::vector<std::uint8_t> request, reply, bytes;
48
49 std::copy(ipmiPhosphorOen.begin(), ipmiPhosphorOen.end(),
50 std::back_inserter(request));
Patrick Venture44474642019-05-20 19:11:04 -070051 request.push_back(static_cast<std::uint8_t>(command));
Patrick Venture123b5c02019-03-05 14:01:00 -080052
53 if (payload.size() > 0)
54 {
55 /* Grow the vector to hold the bytes. */
56 request.reserve(request.size() + sizeof(std::uint16_t));
57
58 /* CRC required. */
59 std::uint16_t crc = generateCrc(payload);
60 auto src = reinterpret_cast<const std::uint8_t*>(&crc);
61
62 std::copy(src, src + sizeof(crc), std::back_inserter(request));
63
64 /* Copy the payload. */
65 std::copy(payload.begin(), payload.end(), std::back_inserter(request));
66 }
67
68 try
69 {
70 reply = ipmi->sendPacket(request);
71 }
72 catch (const IpmiException& e)
73 {
74 throw BlobException(e.what());
75 }
76
77 /* IPMI_CC was OK, and it returned no bytes, so let's be happy with that for
78 * now.
79 */
80 if (reply.size() == 0)
81 {
82 return reply;
83 }
84
85 /* This cannot be a response because it's smaller than the smallest
86 * response.
87 */
88 if (reply.size() < ipmiPhosphorOen.size())
89 {
90 throw BlobException("Invalid response length");
91 }
92
93 /* Verify the OEN. */
94 if (std::memcmp(ipmiPhosphorOen.data(), reply.data(),
95 ipmiPhosphorOen.size()) != 0)
96 {
97 throw BlobException("Invalid OEN received");
98 }
99
100 /* In this case there was no data, as there was no CRC. */
101 std::size_t headerSize = ipmiPhosphorOen.size() + sizeof(std::uint16_t);
102 if (reply.size() < headerSize)
103 {
104 return {};
105 }
106
107 /* Validate CRC. */
108 std::uint16_t crc;
109 auto ptr = reinterpret_cast<std::uint8_t*>(&crc);
110 std::memcpy(ptr, &reply[ipmiPhosphorOen.size()], sizeof(crc));
111
Patrick Venture123b5c02019-03-05 14:01:00 -0800112 bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end());
113
114 auto computed = generateCrc(bytes);
115 if (crc != computed)
116 {
117 std::fprintf(stderr, "Invalid CRC, received: 0x%x, computed: 0x%x\n",
118 crc, computed);
119 throw BlobException("Invalid CRC on received data.");
120 }
121
122 return bytes;
123}
124
125int BlobHandler::getBlobCount()
126{
127 std::uint32_t count;
128 try
129 {
130 auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobGetCount, {});
131 if (resp.size() != sizeof(count))
132 {
133 return 0;
134 }
135
136 /* LE to LE (need to make this portable as some point. */
137 std::memcpy(&count, resp.data(), sizeof(count));
138 }
139 catch (const BlobException& b)
140 {
141 return 0;
142 }
143
Patrick Venture123b5c02019-03-05 14:01:00 -0800144 return count;
145}
146
147std::string BlobHandler::enumerateBlob(std::uint32_t index)
148{
149 std::vector<std::uint8_t> payload;
150 auto data = reinterpret_cast<const std::uint8_t*>(&index);
151
152 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
153
154 try
155 {
156 auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
157 return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
158 : "";
159 }
160 catch (const BlobException& b)
161 {
162 return "";
163 }
164}
165
Patrick Venture8865e402019-05-14 13:29:10 -0700166void BlobHandler::commit(std::uint16_t session,
167 const std::vector<std::uint8_t>& bytes)
168{
169 std::vector<std::uint8_t> request;
170 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
171 std::copy(addrSession, addrSession + sizeof(session),
172 std::back_inserter(request));
173
174 /* You have one byte to describe the length. */
175 if (bytes.size() > std::numeric_limits<std::uint8_t>::max())
176 {
177 throw BlobException("Commit data length greater than 8-bit limit\n");
178 }
179
180 std::uint8_t length = static_cast<std::uint8_t>(bytes.size());
181 auto addrLength = reinterpret_cast<const std::uint8_t*>(&length);
182 std::copy(addrLength, addrLength + sizeof(length),
183 std::back_inserter(request));
184
185 std::copy(bytes.begin(), bytes.end(), std::back_inserter(request));
186
187 sendIpmiPayload(BlobOEMCommands::bmcBlobCommit, request);
188}
189
Patrick Venture123b5c02019-03-05 14:01:00 -0800190void BlobHandler::writeGeneric(BlobOEMCommands command, std::uint16_t session,
191 std::uint32_t offset,
192 const std::vector<std::uint8_t>& bytes)
193{
194 std::vector<std::uint8_t> payload;
195
196 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
197 bytes.size());
198
199 auto data = reinterpret_cast<const std::uint8_t*>(&session);
200 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
201
202 data = reinterpret_cast<const std::uint8_t*>(&offset);
203 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
204
205 std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload));
206
207 auto resp = sendIpmiPayload(command, payload);
208}
209
210void BlobHandler::writeMeta(std::uint16_t session, std::uint32_t offset,
211 const std::vector<std::uint8_t>& bytes)
212{
213 return writeGeneric(BlobOEMCommands::bmcBlobWriteMeta, session, offset,
214 bytes);
215}
216
217void BlobHandler::writeBytes(std::uint16_t session, std::uint32_t offset,
218 const std::vector<std::uint8_t>& bytes)
219{
220 return writeGeneric(BlobOEMCommands::bmcBlobWrite, session, offset, bytes);
221}
222
223std::vector<std::string> BlobHandler::getBlobList()
224{
225 std::vector<std::string> list;
226 int blobCount = getBlobCount();
227
228 for (int i = 0; i < blobCount; i++)
229 {
230 auto name = enumerateBlob(i);
231 /* Currently ignore failures. */
232 if (!name.empty())
233 {
234 list.push_back(name);
235 }
236 }
237
238 return list;
239}
240
Patrick Venture17186ae2019-05-06 10:30:55 -0700241StatResponse BlobHandler::statGeneric(BlobOEMCommands command,
242 const std::vector<std::uint8_t>& request)
Patrick Venture123b5c02019-03-05 14:01:00 -0800243{
244 StatResponse meta;
Patrick Venture17186ae2019-05-06 10:30:55 -0700245 std::vector<std::uint8_t> resp;
Patrick Venture123b5c02019-03-05 14:01:00 -0800246
247 try
248 {
Patrick Venture17186ae2019-05-06 10:30:55 -0700249 resp = sendIpmiPayload(command, request);
Patrick Venture123b5c02019-03-05 14:01:00 -0800250 }
251 catch (const BlobException& b)
252 {
253 throw;
254 }
255
256 std::memcpy(&meta.blob_state, &resp[0], sizeof(meta.blob_state));
257 std::memcpy(&meta.size, &resp[sizeof(meta.blob_state)], sizeof(meta.size));
258 int offset = sizeof(meta.blob_state) + sizeof(meta.size);
259 std::uint8_t len = resp[offset];
260 if (len > 0)
261 {
Patrick Ventured39b6f82019-05-07 11:04:35 -0700262 std::copy(resp.begin() + offset + sizeof(len), resp.end(),
Patrick Venture123b5c02019-03-05 14:01:00 -0800263 std::back_inserter(meta.metadata));
264 }
265
266 return meta;
267}
268
Patrick Venture17186ae2019-05-06 10:30:55 -0700269StatResponse BlobHandler::getStat(const std::string& id)
270{
271 std::vector<std::uint8_t> name;
272 std::copy(id.begin(), id.end(), std::back_inserter(name));
273 name.push_back(0x00); /* need to add nul-terminator. */
274
275 return statGeneric(BlobOEMCommands::bmcBlobStat, name);
276}
277
Patrick Venture16a99a62019-05-03 17:21:30 -0700278StatResponse BlobHandler::getStat(std::uint16_t session)
279{
Patrick Venture16a99a62019-05-03 17:21:30 -0700280 std::vector<std::uint8_t> request;
281 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
282 std::copy(addrSession, addrSession + sizeof(session),
283 std::back_inserter(request));
284
Patrick Venture17186ae2019-05-06 10:30:55 -0700285 return statGeneric(BlobOEMCommands::bmcBlobSessionStat, request);
Patrick Venture16a99a62019-05-03 17:21:30 -0700286}
287
Patrick Venture123b5c02019-03-05 14:01:00 -0800288std::uint16_t BlobHandler::openBlob(const std::string& id,
289 std::uint16_t handlerFlags)
290{
291 std::uint16_t session;
292 std::vector<std::uint8_t> request, resp;
293 auto addrFlags = reinterpret_cast<const std::uint8_t*>(&handlerFlags);
294
295 std::copy(addrFlags, addrFlags + sizeof(handlerFlags),
296 std::back_inserter(request));
297 std::copy(id.begin(), id.end(), std::back_inserter(request));
298 request.push_back(0x00); /* need to add nul-terminator. */
299
300 try
301 {
302 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
303 }
304 catch (const BlobException& b)
305 {
306 throw;
307 }
308
309 if (resp.size() != sizeof(session))
310 {
311 throw BlobException("Did not receive session.");
312 }
313
314 std::memcpy(&session, resp.data(), sizeof(session));
315 return session;
316}
317
318void BlobHandler::closeBlob(std::uint16_t session)
319{
320 std::vector<std::uint8_t> request;
321 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
322 std::copy(addrSession, addrSession + sizeof(session),
323 std::back_inserter(request));
324
325 try
326 {
327 sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request);
328 }
329 catch (const BlobException& b)
330 {
331 std::fprintf(stderr, "Received failure on close: %s\n", b.what());
332 }
333
334 return;
335}
336
337std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session,
338 std::uint32_t offset,
339 std::uint32_t length)
340{
341 std::vector<std::uint8_t> payload;
342
343 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
344 sizeof(std::uint32_t));
345
346 auto data = reinterpret_cast<const std::uint8_t*>(&session);
347 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
348
349 data = reinterpret_cast<const std::uint8_t*>(&offset);
350 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
351
352 data = reinterpret_cast<const std::uint8_t*>(&length);
353 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
354
355 return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload);
356}
357
Patrick Venture1470bec2019-03-06 07:33:12 -0800358} // namespace ipmiblob