blob: 12fbaaa63af7c7ca595f36bc38c8aa8342517e57 [file] [log] [blame]
Patrick Venturec79faa12018-12-12 13:12:21 -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
Patrick Venture00887592018-12-11 10:57:06 -080017#include "blob_handler.hpp"
18
Patrick Venture0533d0b2018-12-13 08:48:24 -080019#include "blob_errors.hpp"
Patrick Venturec79faa12018-12-12 13:12:21 -080020#include "crc.hpp"
21#include "ipmi_errors.hpp"
22
23#include <array>
24#include <cstring>
25
Patrick Venture9b534f02018-12-13 16:10:02 -080026namespace host_tool
27{
28
29namespace
30{
Patrick Venturec79faa12018-12-12 13:12:21 -080031const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00};
Patrick Venture9b534f02018-12-13 16:10:02 -080032}
Patrick Venturec79faa12018-12-12 13:12:21 -080033
34std::vector<std::uint8_t>
35 BlobHandler::sendIpmiPayload(BlobOEMCommands command,
36 const std::vector<std::uint8_t>& payload)
37{
Patrick Venturee527c202019-01-15 13:52:32 -080038 std::vector<std::uint8_t> request, reply, bytes;
Patrick Venturec79faa12018-12-12 13:12:21 -080039
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);
Patrick Venture7ef13462019-02-01 15:24:41 -080051 auto src = reinterpret_cast<const std::uint8_t*>(&crc);
Patrick Venturec79faa12018-12-12 13:12:21 -080052
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 {
Patrick Venture339dece2018-12-14 18:32:04 -080065 throw BlobException(e.what());
Patrick Venturec79faa12018-12-12 13:12:21 -080066 }
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
Patrick Venturec79faa12018-12-12 13:12:21 -080076 /* This cannot be a response because it's smaller than the smallest
77 * response.
78 */
Patrick Venturee527c202019-01-15 13:52:32 -080079 if (reply.size() < ipmiPhosphorOen.size())
Patrick Venturec79faa12018-12-12 13:12:21 -080080 {
Patrick Venture339dece2018-12-14 18:32:04 -080081 throw BlobException("Invalid response length");
Patrick Venturec79faa12018-12-12 13:12:21 -080082 }
83
84 /* Verify the OEN. */
85 if (std::memcmp(ipmiPhosphorOen.data(), reply.data(),
86 ipmiPhosphorOen.size()) != 0)
87 {
Patrick Venture339dece2018-12-14 18:32:04 -080088 throw BlobException("Invalid OEN received");
Patrick Venturec79faa12018-12-12 13:12:21 -080089 }
90
Patrick Venturee527c202019-01-15 13:52:32 -080091 /* 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
Patrick Venturec79faa12018-12-12 13:12:21 -080098 /* 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
Patrick Venturebcec9c62018-12-14 13:58:41 -0800103 for (const auto& byte : reply)
104 {
105 std::fprintf(stderr, "0x%02x ", byte);
106 }
107 std::fprintf(stderr, "\n");
108
Patrick Venturebcec9c62018-12-14 13:58:41 -0800109 bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end());
Patrick Venturec79faa12018-12-12 13:12:21 -0800110
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);
Patrick Venture339dece2018-12-14 18:32:04 -0800116 throw BlobException("Invalid CRC on received data.");
Patrick Venturec79faa12018-12-12 13:12:21 -0800117 }
118
119 return bytes;
120}
121
122int BlobHandler::getBlobCount()
123{
124 std::uint32_t count;
Patrick Venture339dece2018-12-14 18:32:04 -0800125 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)
Patrick Venturec79faa12018-12-12 13:12:21 -0800137 {
138 return 0;
139 }
140
Patrick Venture339dece2018-12-14 18:32:04 -0800141 std::fprintf(stderr, "BLOB Count: %d\n", count);
Patrick Venturec79faa12018-12-12 13:12:21 -0800142 return count;
143}
144
145std::string BlobHandler::enumerateBlob(std::uint32_t index)
146{
147 std::vector<std::uint8_t> payload;
Patrick Venture7ef13462019-02-01 15:24:41 -0800148 auto data = reinterpret_cast<const std::uint8_t*>(&index);
Patrick Venture339dece2018-12-14 18:32:04 -0800149
Patrick Venturec79faa12018-12-12 13:12:21 -0800150 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
151
Patrick Venture339dece2018-12-14 18:32:04 -0800152 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 }
Patrick Venturec79faa12018-12-12 13:12:21 -0800162}
163
Patrick Venture77c59182019-01-17 14:53:31 -0800164void BlobHandler::writeGeneric(BlobOEMCommands command, std::uint16_t session,
165 std::uint32_t offset,
166 const std::vector<std::uint8_t>& bytes)
Patrick Venture0309f102019-01-15 13:41:05 -0800167{
168 std::vector<std::uint8_t> payload;
169
170 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
171 bytes.size());
172
Patrick Venture7ef13462019-02-01 15:24:41 -0800173 auto data = reinterpret_cast<const std::uint8_t*>(&session);
Patrick Venture0309f102019-01-15 13:41:05 -0800174 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
175
Patrick Venture7ef13462019-02-01 15:24:41 -0800176 data = reinterpret_cast<const std::uint8_t*>(&offset);
Patrick Venture0309f102019-01-15 13:41:05 -0800177 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
Patrick Venture77c59182019-01-17 14:53:31 -0800181 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);
Patrick Venture0309f102019-01-15 13:41:05 -0800195}
196
Patrick Venture00887592018-12-11 10:57:06 -0800197std::vector<std::string> BlobHandler::getBlobList()
198{
Patrick Venturec79faa12018-12-12 13:12:21 -0800199 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;
Patrick Venture00887592018-12-11 10:57:06 -0800213}
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800214
215StatResponse BlobHandler::getStat(const std::string& id)
216{
217 StatResponse meta;
Patrick Venture339dece2018-12-14 18:32:04 -0800218 std::vector<std::uint8_t> name, resp;
219
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800220 std::copy(id.begin(), id.end(), std::back_inserter(name));
Patrick Venture0d88a122018-12-17 07:52:04 -0800221 name.push_back(0x00); /* need to add nul-terminator. */
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800222
Patrick Venture339dece2018-12-14 18:32:04 -0800223 try
224 {
225 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobStat, name);
226 }
227 catch (const BlobException& b)
228 {
229 throw;
230 }
231
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800232 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}
Patrick Venture0533d0b2018-12-13 08:48:24 -0800244
245std::uint16_t
246 BlobHandler::openBlob(const std::string& id,
247 blobs::FirmwareBlobHandler::UpdateFlags handlerFlags)
248{
249 std::uint16_t session;
Patrick Venture339dece2018-12-14 18:32:04 -0800250 std::vector<std::uint8_t> request, resp;
Patrick Venture0533d0b2018-12-13 08:48:24 -0800251 std::uint16_t flags =
252 blobs::FirmwareBlobHandler::UpdateFlags::openWrite | handlerFlags;
Patrick Venture7ef13462019-02-01 15:24:41 -0800253 auto addrFlags = reinterpret_cast<const std::uint8_t*>(&flags);
Patrick Venture339dece2018-12-14 18:32:04 -0800254
Patrick Venture0533d0b2018-12-13 08:48:24 -0800255 std::copy(addrFlags, addrFlags + sizeof(flags),
256 std::back_inserter(request));
257 std::copy(id.begin(), id.end(), std::back_inserter(request));
Patrick Venture0d88a122018-12-17 07:52:04 -0800258 request.push_back(0x00); /* need to add nul-terminator. */
Patrick Venture0533d0b2018-12-13 08:48:24 -0800259
Patrick Venture339dece2018-12-14 18:32:04 -0800260 try
261 {
262 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
263 }
264 catch (const BlobException& b)
265 {
266 throw;
267 }
268
Patrick Venture0533d0b2018-12-13 08:48:24 -0800269 if (resp.size() != sizeof(session))
270 {
271 throw BlobException("Did not receive session.");
272 }
273
274 std::memcpy(&session, resp.data(), sizeof(session));
275 return session;
276}
Patrick Venture9b534f02018-12-13 16:10:02 -0800277
Patrick Venture9a5ce562018-12-14 18:56:04 -0800278void BlobHandler::closeBlob(std::uint16_t session)
279{
280 std::vector<std::uint8_t> request, resp;
Patrick Venture7ef13462019-02-01 15:24:41 -0800281 auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
Patrick Venture9a5ce562018-12-14 18:56:04 -0800282 std::copy(addrSession, addrSession + sizeof(session),
283 std::back_inserter(request));
284
285 try
286 {
287 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request);
288 }
289 catch (const BlobException& b)
290 {
291 std::fprintf(stderr, "Received failure on close: %s\n", b.what());
292 }
293
294 return;
295}
296
Patrick Venture957f0862019-02-01 14:40:06 -0800297std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session,
298 std::uint32_t offset,
299 std::uint32_t length)
300{
301 std::vector<std::uint8_t> payload;
302
303 payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
304 sizeof(std::uint32_t));
305
Patrick Venture7ef13462019-02-01 15:24:41 -0800306 auto data = reinterpret_cast<const std::uint8_t*>(&session);
Patrick Venture957f0862019-02-01 14:40:06 -0800307 std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
308
Patrick Venture7ef13462019-02-01 15:24:41 -0800309 data = reinterpret_cast<const std::uint8_t*>(&offset);
Patrick Venture957f0862019-02-01 14:40:06 -0800310 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
311
Patrick Venture7ef13462019-02-01 15:24:41 -0800312 data = reinterpret_cast<const std::uint8_t*>(&length);
Patrick Venture957f0862019-02-01 14:40:06 -0800313 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
314
315 return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload);
316}
317
Patrick Venture9b534f02018-12-13 16:10:02 -0800318} // namespace host_tool