blob: 2ea927aa57a678c194e82de4e54fe6b62cc9e13c [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
141 std::fprintf(stderr, "BLOB Count: %d\n", count);
142 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
245std::uint16_t BlobHandler::openBlob(const std::string& id,
246 std::uint16_t handlerFlags)
247{
248 std::uint16_t session;
249 std::vector<std::uint8_t> request, resp;
250 auto addrFlags = reinterpret_cast<const std::uint8_t*>(&handlerFlags);
251
252 std::copy(addrFlags, addrFlags + sizeof(handlerFlags),
253 std::back_inserter(request));
254 std::copy(id.begin(), id.end(), std::back_inserter(request));
255 request.push_back(0x00); /* need to add nul-terminator. */
256
257 try
258 {
259 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
260 }
261 catch (const BlobException& b)
262 {
263 throw;
264 }
265
266 if (resp.size() != sizeof(session))
267 {
268 throw BlobException("Did not receive session.");
269 }
270
271 std::memcpy(&session, resp.data(), sizeof(session));
272 return session;
273}
274
275void BlobHandler::closeBlob(std::uint16_t session)
276{
277 std::vector<std::uint8_t> request;
278 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
279 std::copy(addrSession, addrSession + sizeof(session),
280 std::back_inserter(request));
281
282 try
283 {
284 sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request);
285 }
286 catch (const BlobException& b)
287 {
288 std::fprintf(stderr, "Received failure on close: %s\n", b.what());
289 }
290
291 return;
292}
293
294std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session,
295 std::uint32_t offset,
296 std::uint32_t length)
297{
298 std::vector<std::uint8_t> payload;
299
300 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
301 sizeof(std::uint32_t));
302
303 auto data = reinterpret_cast<const std::uint8_t*>(&session);
304 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
305
306 data = reinterpret_cast<const std::uint8_t*>(&offset);
307 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
308
309 data = reinterpret_cast<const std::uint8_t*>(&length);
310 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
311
312 return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload);
313}
314
Patrick Venture1470bec2019-03-06 07:33:12 -0800315} // namespace ipmiblob