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> |
Brandon Kim | d3c06f9 | 2022-07-20 17:36:53 +0000 | [diff] [blame^] | 25 | #include <cinttypes> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 26 | #include <cstring> |
Patrick Venture | 800f06d | 2019-05-14 14:43:58 -0700 | [diff] [blame] | 27 | #include <limits> |
Patrick Venture | 22fcc84 | 2019-05-13 09:06:24 -0700 | [diff] [blame] | 28 | #include <memory> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 29 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 30 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 31 | { |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00}; |
| 36 | } |
| 37 | |
Patrick Venture | 22fcc84 | 2019-05-13 09:06:24 -0700 | [diff] [blame] | 38 | std::unique_ptr<BlobInterface> |
| 39 | BlobHandler::CreateBlobHandler(std::unique_ptr<IpmiInterface> ipmi) |
| 40 | { |
| 41 | return std::make_unique<BlobHandler>(std::move(ipmi)); |
| 42 | } |
| 43 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 44 | std::vector<std::uint8_t> |
| 45 | BlobHandler::sendIpmiPayload(BlobOEMCommands command, |
| 46 | const std::vector<std::uint8_t>& payload) |
| 47 | { |
| 48 | std::vector<std::uint8_t> request, reply, bytes; |
| 49 | |
| 50 | std::copy(ipmiPhosphorOen.begin(), ipmiPhosphorOen.end(), |
| 51 | std::back_inserter(request)); |
Patrick Venture | 4447464 | 2019-05-20 19:11:04 -0700 | [diff] [blame] | 52 | request.push_back(static_cast<std::uint8_t>(command)); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 53 | |
Patrick Venture | bae7664 | 2020-11-16 14:02:39 -0800 | [diff] [blame] | 54 | if (!payload.empty()) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 55 | { |
| 56 | /* Grow the vector to hold the bytes. */ |
| 57 | request.reserve(request.size() + sizeof(std::uint16_t)); |
| 58 | |
| 59 | /* CRC required. */ |
| 60 | std::uint16_t crc = generateCrc(payload); |
| 61 | auto src = reinterpret_cast<const std::uint8_t*>(&crc); |
| 62 | |
| 63 | std::copy(src, src + sizeof(crc), std::back_inserter(request)); |
| 64 | |
| 65 | /* Copy the payload. */ |
| 66 | std::copy(payload.begin(), payload.end(), std::back_inserter(request)); |
| 67 | } |
| 68 | |
| 69 | try |
| 70 | { |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame] | 71 | reply = ipmi->sendPacket(ipmiOEMNetFn, ipmiOEMBlobCmd, request); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 72 | } |
| 73 | catch (const IpmiException& e) |
| 74 | { |
| 75 | throw BlobException(e.what()); |
| 76 | } |
| 77 | |
| 78 | /* IPMI_CC was OK, and it returned no bytes, so let's be happy with that for |
| 79 | * now. |
| 80 | */ |
Patrick Venture | bae7664 | 2020-11-16 14:02:39 -0800 | [diff] [blame] | 81 | if (reply.empty()) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 82 | { |
| 83 | return reply; |
| 84 | } |
| 85 | |
| 86 | /* This cannot be a response because it's smaller than the smallest |
| 87 | * response. |
| 88 | */ |
| 89 | if (reply.size() < ipmiPhosphorOen.size()) |
| 90 | { |
| 91 | throw BlobException("Invalid response length"); |
| 92 | } |
| 93 | |
| 94 | /* Verify the OEN. */ |
| 95 | if (std::memcmp(ipmiPhosphorOen.data(), reply.data(), |
| 96 | ipmiPhosphorOen.size()) != 0) |
| 97 | { |
| 98 | throw BlobException("Invalid OEN received"); |
| 99 | } |
| 100 | |
| 101 | /* In this case there was no data, as there was no CRC. */ |
| 102 | std::size_t headerSize = ipmiPhosphorOen.size() + sizeof(std::uint16_t); |
| 103 | if (reply.size() < headerSize) |
| 104 | { |
| 105 | return {}; |
| 106 | } |
| 107 | |
| 108 | /* Validate CRC. */ |
| 109 | std::uint16_t crc; |
| 110 | auto ptr = reinterpret_cast<std::uint8_t*>(&crc); |
| 111 | std::memcpy(ptr, &reply[ipmiPhosphorOen.size()], sizeof(crc)); |
| 112 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 113 | bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end()); |
| 114 | |
| 115 | auto computed = generateCrc(bytes); |
| 116 | if (crc != computed) |
| 117 | { |
| 118 | std::fprintf(stderr, "Invalid CRC, received: 0x%x, computed: 0x%x\n", |
| 119 | crc, computed); |
| 120 | throw BlobException("Invalid CRC on received data."); |
| 121 | } |
| 122 | |
| 123 | return bytes; |
| 124 | } |
| 125 | |
| 126 | int BlobHandler::getBlobCount() |
| 127 | { |
| 128 | std::uint32_t count; |
| 129 | try |
| 130 | { |
| 131 | auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobGetCount, {}); |
| 132 | if (resp.size() != sizeof(count)) |
| 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* LE to LE (need to make this portable as some point. */ |
| 138 | std::memcpy(&count, resp.data(), sizeof(count)); |
| 139 | } |
| 140 | catch (const BlobException& b) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 145 | return count; |
| 146 | } |
| 147 | |
| 148 | std::string BlobHandler::enumerateBlob(std::uint32_t index) |
| 149 | { |
| 150 | std::vector<std::uint8_t> payload; |
| 151 | auto data = reinterpret_cast<const std::uint8_t*>(&index); |
| 152 | |
| 153 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 154 | |
| 155 | try |
| 156 | { |
| 157 | auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload); |
Patrick Venture | bae7664 | 2020-11-16 14:02:39 -0800 | [diff] [blame] | 158 | return (resp.empty()) ? "" |
| 159 | : std::string(&resp[0], &resp[resp.size() - 1]); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 160 | } |
| 161 | catch (const BlobException& b) |
| 162 | { |
| 163 | return ""; |
| 164 | } |
| 165 | } |
| 166 | |
Patrick Venture | 8865e40 | 2019-05-14 13:29:10 -0700 | [diff] [blame] | 167 | void BlobHandler::commit(std::uint16_t session, |
| 168 | const std::vector<std::uint8_t>& bytes) |
| 169 | { |
| 170 | std::vector<std::uint8_t> request; |
| 171 | auto addrSession = reinterpret_cast<const std::uint8_t*>(&session); |
| 172 | std::copy(addrSession, addrSession + sizeof(session), |
| 173 | std::back_inserter(request)); |
| 174 | |
| 175 | /* You have one byte to describe the length. */ |
| 176 | if (bytes.size() > std::numeric_limits<std::uint8_t>::max()) |
| 177 | { |
| 178 | throw BlobException("Commit data length greater than 8-bit limit\n"); |
| 179 | } |
| 180 | |
| 181 | std::uint8_t length = static_cast<std::uint8_t>(bytes.size()); |
| 182 | auto addrLength = reinterpret_cast<const std::uint8_t*>(&length); |
| 183 | std::copy(addrLength, addrLength + sizeof(length), |
| 184 | std::back_inserter(request)); |
| 185 | |
| 186 | std::copy(bytes.begin(), bytes.end(), std::back_inserter(request)); |
| 187 | |
| 188 | sendIpmiPayload(BlobOEMCommands::bmcBlobCommit, request); |
| 189 | } |
| 190 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 191 | void BlobHandler::writeGeneric(BlobOEMCommands command, std::uint16_t session, |
| 192 | std::uint32_t offset, |
| 193 | const std::vector<std::uint8_t>& bytes) |
| 194 | { |
| 195 | std::vector<std::uint8_t> payload; |
| 196 | |
| 197 | payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) + |
| 198 | bytes.size()); |
| 199 | |
| 200 | auto data = reinterpret_cast<const std::uint8_t*>(&session); |
| 201 | std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload)); |
| 202 | |
| 203 | data = reinterpret_cast<const std::uint8_t*>(&offset); |
| 204 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 205 | |
| 206 | std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload)); |
| 207 | |
Brandon Kim | 0106f57 | 2019-10-18 10:13:55 -0700 | [diff] [blame] | 208 | sendIpmiPayload(command, payload); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void BlobHandler::writeMeta(std::uint16_t session, std::uint32_t offset, |
| 212 | const std::vector<std::uint8_t>& bytes) |
| 213 | { |
Brandon Kim | 00f39b8 | 2019-10-18 11:00:00 -0700 | [diff] [blame] | 214 | writeGeneric(BlobOEMCommands::bmcBlobWriteMeta, session, offset, bytes); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void BlobHandler::writeBytes(std::uint16_t session, std::uint32_t offset, |
| 218 | const std::vector<std::uint8_t>& bytes) |
| 219 | { |
Brandon Kim | 00f39b8 | 2019-10-18 11:00:00 -0700 | [diff] [blame] | 220 | writeGeneric(BlobOEMCommands::bmcBlobWrite, session, offset, bytes); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 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; |
Brandon Kim | 714e388 | 2020-06-22 12:36:32 -0700 | [diff] [blame] | 245 | static constexpr std::size_t blobStateSize = sizeof(meta.blob_state); |
| 246 | static constexpr std::size_t metaSize = sizeof(meta.size); |
| 247 | static constexpr std::size_t metaOffset = blobStateSize + metaSize; |
| 248 | static constexpr std::size_t minRespSize = |
| 249 | metaOffset + sizeof(std::uint8_t); |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 250 | std::vector<std::uint8_t> resp; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 251 | |
| 252 | try |
| 253 | { |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 254 | resp = sendIpmiPayload(command, request); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 255 | } |
| 256 | catch (const BlobException& b) |
| 257 | { |
| 258 | throw; |
| 259 | } |
| 260 | |
Brandon Kim | 714e388 | 2020-06-22 12:36:32 -0700 | [diff] [blame] | 261 | // Avoid out of bounds memcpy below |
| 262 | if (resp.size() < minRespSize) |
| 263 | { |
| 264 | std::fprintf(stderr, |
Brandon Kim | d3c06f9 | 2022-07-20 17:36:53 +0000 | [diff] [blame^] | 265 | "Invalid response length, Got %zu which is less than " |
| 266 | "minRespSize %zu\n", |
Brandon Kim | 714e388 | 2020-06-22 12:36:32 -0700 | [diff] [blame] | 267 | resp.size(), minRespSize); |
| 268 | throw BlobException("Invalid response length"); |
| 269 | } |
| 270 | |
| 271 | std::memcpy(&meta.blob_state, &resp[0], blobStateSize); |
| 272 | std::memcpy(&meta.size, &resp[blobStateSize], metaSize); |
| 273 | std::uint8_t len = resp[metaOffset]; |
| 274 | |
| 275 | auto metaDataLength = resp.size() - minRespSize; |
| 276 | if (metaDataLength != len) |
| 277 | { |
| 278 | std::fprintf(stderr, |
Brandon Kim | d3c06f9 | 2022-07-20 17:36:53 +0000 | [diff] [blame^] | 279 | "Metadata length did not match actual length, Got %zu " |
| 280 | "which does not equal expected length %" PRIu8 "\n", |
Brandon Kim | 714e388 | 2020-06-22 12:36:32 -0700 | [diff] [blame] | 281 | metaDataLength, len); |
| 282 | throw BlobException("Metadata length did not match actual length"); |
| 283 | } |
| 284 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 285 | if (len > 0) |
| 286 | { |
Brandon Kim | 714e388 | 2020-06-22 12:36:32 -0700 | [diff] [blame] | 287 | meta.metadata.resize(len); |
| 288 | std::copy(resp.begin() + minRespSize, resp.end(), |
| 289 | meta.metadata.begin()); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | return meta; |
| 293 | } |
| 294 | |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 295 | StatResponse BlobHandler::getStat(const std::string& id) |
| 296 | { |
| 297 | std::vector<std::uint8_t> name; |
| 298 | std::copy(id.begin(), id.end(), std::back_inserter(name)); |
| 299 | name.push_back(0x00); /* need to add nul-terminator. */ |
| 300 | |
| 301 | return statGeneric(BlobOEMCommands::bmcBlobStat, name); |
| 302 | } |
| 303 | |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 304 | StatResponse BlobHandler::getStat(std::uint16_t session) |
| 305 | { |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 306 | std::vector<std::uint8_t> request; |
| 307 | auto addrSession = reinterpret_cast<const std::uint8_t*>(&session); |
| 308 | std::copy(addrSession, addrSession + sizeof(session), |
| 309 | std::back_inserter(request)); |
| 310 | |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 311 | return statGeneric(BlobOEMCommands::bmcBlobSessionStat, request); |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 314 | std::uint16_t BlobHandler::openBlob(const std::string& id, |
| 315 | std::uint16_t handlerFlags) |
| 316 | { |
| 317 | std::uint16_t session; |
| 318 | std::vector<std::uint8_t> request, resp; |
| 319 | auto addrFlags = reinterpret_cast<const std::uint8_t*>(&handlerFlags); |
| 320 | |
| 321 | std::copy(addrFlags, addrFlags + sizeof(handlerFlags), |
| 322 | std::back_inserter(request)); |
| 323 | std::copy(id.begin(), id.end(), std::back_inserter(request)); |
| 324 | request.push_back(0x00); /* need to add nul-terminator. */ |
| 325 | |
| 326 | try |
| 327 | { |
| 328 | resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request); |
| 329 | } |
| 330 | catch (const BlobException& b) |
| 331 | { |
| 332 | throw; |
| 333 | } |
| 334 | |
| 335 | if (resp.size() != sizeof(session)) |
| 336 | { |
| 337 | throw BlobException("Did not receive session."); |
| 338 | } |
| 339 | |
| 340 | std::memcpy(&session, resp.data(), sizeof(session)); |
| 341 | return session; |
| 342 | } |
| 343 | |
| 344 | void BlobHandler::closeBlob(std::uint16_t session) |
| 345 | { |
| 346 | std::vector<std::uint8_t> request; |
| 347 | auto addrSession = reinterpret_cast<const std::uint8_t*>(&session); |
| 348 | std::copy(addrSession, addrSession + sizeof(session), |
| 349 | std::back_inserter(request)); |
| 350 | |
| 351 | try |
| 352 | { |
| 353 | sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request); |
| 354 | } |
| 355 | catch (const BlobException& b) |
| 356 | { |
| 357 | std::fprintf(stderr, "Received failure on close: %s\n", b.what()); |
| 358 | } |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 359 | } |
| 360 | |
William A. Kennington III | d46530f | 2021-11-06 17:12:07 -0700 | [diff] [blame] | 361 | bool BlobHandler::deleteBlob(const std::string& id) |
Brandon Kim | cc4ef0c | 2019-10-18 10:08:37 -0700 | [diff] [blame] | 362 | { |
| 363 | std::vector<std::uint8_t> name; |
| 364 | std::copy(id.begin(), id.end(), std::back_inserter(name)); |
| 365 | name.push_back(0x00); /* need to add nul-terminator. */ |
| 366 | |
| 367 | try |
| 368 | { |
| 369 | sendIpmiPayload(BlobOEMCommands::bmcBlobDelete, name); |
William A. Kennington III | d46530f | 2021-11-06 17:12:07 -0700 | [diff] [blame] | 370 | return true; |
Brandon Kim | cc4ef0c | 2019-10-18 10:08:37 -0700 | [diff] [blame] | 371 | } |
| 372 | catch (const BlobException& b) |
| 373 | { |
| 374 | std::fprintf(stderr, "Received failure on delete: %s\n", b.what()); |
| 375 | } |
William A. Kennington III | d46530f | 2021-11-06 17:12:07 -0700 | [diff] [blame] | 376 | return false; |
Brandon Kim | cc4ef0c | 2019-10-18 10:08:37 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 379 | std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session, |
| 380 | std::uint32_t offset, |
| 381 | std::uint32_t length) |
| 382 | { |
| 383 | std::vector<std::uint8_t> payload; |
| 384 | |
| 385 | payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) + |
| 386 | sizeof(std::uint32_t)); |
| 387 | |
| 388 | auto data = reinterpret_cast<const std::uint8_t*>(&session); |
| 389 | std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload)); |
| 390 | |
| 391 | data = reinterpret_cast<const std::uint8_t*>(&offset); |
| 392 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 393 | |
| 394 | data = reinterpret_cast<const std::uint8_t*>(&length); |
| 395 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 396 | |
| 397 | return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload); |
| 398 | } |
| 399 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 400 | } // namespace ipmiblob |