blob: 06d11e5cf88d52b204eb03ec65f17a7974bbb5d5 [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{
38 std::vector<std::uint8_t> request, reply;
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 std::uint8_t* src = reinterpret_cast<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 {
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 Venture28abae72018-12-14 09:44:02 -080076 std::size_t headerSize = ipmiPhosphorOen.size() + sizeof(std::uint16_t);
Patrick Venturec79faa12018-12-12 13:12:21 -080077
78 /* This cannot be a response because it's smaller than the smallest
79 * response.
80 */
81 if (reply.size() < headerSize)
82 {
Patrick Venture339dece2018-12-14 18:32:04 -080083 throw BlobException("Invalid response length");
Patrick Venturec79faa12018-12-12 13:12:21 -080084 }
85
86 /* Verify the OEN. */
87 if (std::memcmp(ipmiPhosphorOen.data(), reply.data(),
88 ipmiPhosphorOen.size()) != 0)
89 {
Patrick Venture339dece2018-12-14 18:32:04 -080090 throw BlobException("Invalid OEN received");
Patrick Venturec79faa12018-12-12 13:12:21 -080091 }
92
93 /* Validate CRC. */
94 std::uint16_t crc;
95 auto ptr = reinterpret_cast<std::uint8_t*>(&crc);
96 std::memcpy(ptr, &reply[ipmiPhosphorOen.size()], sizeof(crc));
97
Patrick Venturebcec9c62018-12-14 13:58:41 -080098 for (const auto& byte : reply)
99 {
100 std::fprintf(stderr, "0x%02x ", byte);
101 }
102 std::fprintf(stderr, "\n");
103
Patrick Venturec79faa12018-12-12 13:12:21 -0800104 std::vector<std::uint8_t> bytes;
Patrick Venturebcec9c62018-12-14 13:58:41 -0800105 bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end());
Patrick Venturec79faa12018-12-12 13:12:21 -0800106
107 auto computed = generateCrc(bytes);
108 if (crc != computed)
109 {
110 std::fprintf(stderr, "Invalid CRC, received: 0x%x, computed: 0x%x\n",
111 crc, computed);
Patrick Venture339dece2018-12-14 18:32:04 -0800112 throw BlobException("Invalid CRC on received data.");
Patrick Venturec79faa12018-12-12 13:12:21 -0800113 }
114
115 return bytes;
116}
117
118int BlobHandler::getBlobCount()
119{
120 std::uint32_t count;
Patrick Venture339dece2018-12-14 18:32:04 -0800121 try
122 {
123 auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobGetCount, {});
124 if (resp.size() != sizeof(count))
125 {
126 return 0;
127 }
128
129 /* LE to LE (need to make this portable as some point. */
130 std::memcpy(&count, resp.data(), sizeof(count));
131 }
132 catch (const BlobException& b)
Patrick Venturec79faa12018-12-12 13:12:21 -0800133 {
134 return 0;
135 }
136
Patrick Venture339dece2018-12-14 18:32:04 -0800137 std::fprintf(stderr, "BLOB Count: %d\n", count);
Patrick Venturec79faa12018-12-12 13:12:21 -0800138 return count;
139}
140
141std::string BlobHandler::enumerateBlob(std::uint32_t index)
142{
143 std::vector<std::uint8_t> payload;
144 std::uint8_t* data = reinterpret_cast<std::uint8_t*>(&index);
Patrick Venture339dece2018-12-14 18:32:04 -0800145
Patrick Venturec79faa12018-12-12 13:12:21 -0800146 std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
147
Patrick Venture339dece2018-12-14 18:32:04 -0800148 try
149 {
150 auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
151 return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
152 : "";
153 }
154 catch (const BlobException& b)
155 {
156 return "";
157 }
Patrick Venturec79faa12018-12-12 13:12:21 -0800158}
159
Patrick Venture00887592018-12-11 10:57:06 -0800160std::vector<std::string> BlobHandler::getBlobList()
161{
Patrick Venturec79faa12018-12-12 13:12:21 -0800162 std::vector<std::string> list;
163 int blobCount = getBlobCount();
164
165 for (int i = 0; i < blobCount; i++)
166 {
167 auto name = enumerateBlob(i);
168 /* Currently ignore failures. */
169 if (!name.empty())
170 {
171 list.push_back(name);
172 }
173 }
174
175 return list;
Patrick Venture00887592018-12-11 10:57:06 -0800176}
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800177
178StatResponse BlobHandler::getStat(const std::string& id)
179{
180 StatResponse meta;
Patrick Venture339dece2018-12-14 18:32:04 -0800181 std::vector<std::uint8_t> name, resp;
182
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800183 std::copy(id.begin(), id.end(), std::back_inserter(name));
Patrick Venture0d88a122018-12-17 07:52:04 -0800184 name.push_back(0x00); /* need to add nul-terminator. */
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800185
Patrick Venture339dece2018-12-14 18:32:04 -0800186 try
187 {
188 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobStat, name);
189 }
190 catch (const BlobException& b)
191 {
192 throw;
193 }
194
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800195 std::memcpy(&meta.blob_state, &resp[0], sizeof(meta.blob_state));
196 std::memcpy(&meta.size, &resp[sizeof(meta.blob_state)], sizeof(meta.size));
197 int offset = sizeof(meta.blob_state) + sizeof(meta.size);
198 std::uint8_t len = resp[offset];
199 if (len > 0)
200 {
201 std::copy(&resp[offset + 1], &resp[resp.size()],
202 std::back_inserter(meta.metadata));
203 }
204
205 return meta;
206}
Patrick Venture0533d0b2018-12-13 08:48:24 -0800207
208std::uint16_t
209 BlobHandler::openBlob(const std::string& id,
210 blobs::FirmwareBlobHandler::UpdateFlags handlerFlags)
211{
212 std::uint16_t session;
Patrick Venture339dece2018-12-14 18:32:04 -0800213 std::vector<std::uint8_t> request, resp;
Patrick Venture0533d0b2018-12-13 08:48:24 -0800214 std::uint16_t flags =
215 blobs::FirmwareBlobHandler::UpdateFlags::openWrite | handlerFlags;
216 auto addrFlags = reinterpret_cast<std::uint8_t*>(&flags);
Patrick Venture339dece2018-12-14 18:32:04 -0800217
Patrick Venture0533d0b2018-12-13 08:48:24 -0800218 std::copy(addrFlags, addrFlags + sizeof(flags),
219 std::back_inserter(request));
220 std::copy(id.begin(), id.end(), std::back_inserter(request));
Patrick Venture0d88a122018-12-17 07:52:04 -0800221 request.push_back(0x00); /* need to add nul-terminator. */
Patrick Venture0533d0b2018-12-13 08:48:24 -0800222
Patrick Venture339dece2018-12-14 18:32:04 -0800223 try
224 {
225 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
226 }
227 catch (const BlobException& b)
228 {
229 throw;
230 }
231
Patrick Venture0533d0b2018-12-13 08:48:24 -0800232 if (resp.size() != sizeof(session))
233 {
234 throw BlobException("Did not receive session.");
235 }
236
237 std::memcpy(&session, resp.data(), sizeof(session));
238 return session;
239}
Patrick Venture9b534f02018-12-13 16:10:02 -0800240
Patrick Venture9a5ce562018-12-14 18:56:04 -0800241void BlobHandler::closeBlob(std::uint16_t session)
242{
243 std::vector<std::uint8_t> request, resp;
244 auto addrSession = reinterpret_cast<std::uint8_t*>(&session);
245 std::copy(addrSession, addrSession + sizeof(session),
246 std::back_inserter(request));
247
248 try
249 {
250 resp = sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request);
251 }
252 catch (const BlobException& b)
253 {
254 std::fprintf(stderr, "Received failure on close: %s\n", b.what());
255 }
256
257 return;
258}
259
Patrick Venture9b534f02018-12-13 16:10:02 -0800260} // namespace host_tool