Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Venture | 22fcc84 | 2019-05-13 09:06:24 -0700 | [diff] [blame] | 22 | #include "ipmi_interface.hpp" |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 23 | |
| 24 | #include <array> |
| 25 | #include <cstring> |
Patrick Venture | 800f06d | 2019-05-14 14:43:58 -0700 | [diff] [blame] | 26 | #include <limits> |
Patrick Venture | 22fcc84 | 2019-05-13 09:06:24 -0700 | [diff] [blame] | 27 | #include <memory> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 28 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 29 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 30 | { |
| 31 | |
| 32 | namespace |
| 33 | { |
| 34 | const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00}; |
| 35 | } |
| 36 | |
Patrick Venture | 22fcc84 | 2019-05-13 09:06:24 -0700 | [diff] [blame] | 37 | std::unique_ptr<BlobInterface> |
| 38 | BlobHandler::CreateBlobHandler(std::unique_ptr<IpmiInterface> ipmi) |
| 39 | { |
| 40 | return std::make_unique<BlobHandler>(std::move(ipmi)); |
| 41 | } |
| 42 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 43 | std::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 Venture | 4447464 | 2019-05-20 19:11:04 -0700 | [diff] [blame] | 51 | request.push_back(static_cast<std::uint8_t>(command)); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 52 | |
| 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 | { |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame^] | 70 | reply = ipmi->sendPacket(ipmiOEMNetFn, ipmiOEMBlobCmd, request); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 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 Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 112 | 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 | |
| 125 | int 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 Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 144 | return count; |
| 145 | } |
| 146 | |
| 147 | std::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 Venture | 8865e40 | 2019-05-14 13:29:10 -0700 | [diff] [blame] | 166 | void 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 Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 190 | void 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 | |
| 210 | void 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 | |
| 217 | void 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 | |
| 223 | std::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 Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 241 | StatResponse BlobHandler::statGeneric(BlobOEMCommands command, |
| 242 | const std::vector<std::uint8_t>& request) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 243 | { |
| 244 | StatResponse meta; |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 245 | std::vector<std::uint8_t> resp; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 246 | |
| 247 | try |
| 248 | { |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 249 | resp = sendIpmiPayload(command, request); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 250 | } |
| 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 Venture | d39b6f8 | 2019-05-07 11:04:35 -0700 | [diff] [blame] | 262 | std::copy(resp.begin() + offset + sizeof(len), resp.end(), |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 263 | std::back_inserter(meta.metadata)); |
| 264 | } |
| 265 | |
| 266 | return meta; |
| 267 | } |
| 268 | |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 269 | StatResponse 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 Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 278 | StatResponse BlobHandler::getStat(std::uint16_t session) |
| 279 | { |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 280 | 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 Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 285 | return statGeneric(BlobOEMCommands::bmcBlobSessionStat, request); |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 288 | std::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 | |
| 318 | void 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 | |
| 337 | std::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 Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 358 | } // namespace ipmiblob |