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" |
| 22 | |
| 23 | #include <array> |
| 24 | #include <cstring> |
| 25 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 26 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 27 | { |
| 28 | |
| 29 | namespace |
| 30 | { |
| 31 | const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00}; |
| 32 | } |
| 33 | |
| 34 | std::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 | |
| 122 | int 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 | |
Patrick Venture | a835642 | 2019-03-15 21:16:33 -0700 | [diff] [blame] | 141 | std::fprintf(stderr, "BLOB Count: %u\n", count); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 142 | return count; |
| 143 | } |
| 144 | |
| 145 | std::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 | |
| 164 | void 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 | |
| 184 | void 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 | |
| 191 | void 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 | |
| 197 | std::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 | |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 215 | StatResponse BlobHandler::statGeneric(BlobOEMCommands command, |
| 216 | const std::vector<std::uint8_t>& request) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 217 | { |
| 218 | StatResponse meta; |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 219 | std::vector<std::uint8_t> resp; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 220 | |
| 221 | try |
| 222 | { |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 223 | resp = sendIpmiPayload(command, request); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 224 | } |
| 225 | catch (const BlobException& b) |
| 226 | { |
| 227 | throw; |
| 228 | } |
| 229 | |
| 230 | std::memcpy(&meta.blob_state, &resp[0], sizeof(meta.blob_state)); |
| 231 | std::memcpy(&meta.size, &resp[sizeof(meta.blob_state)], sizeof(meta.size)); |
| 232 | int offset = sizeof(meta.blob_state) + sizeof(meta.size); |
| 233 | std::uint8_t len = resp[offset]; |
| 234 | if (len > 0) |
| 235 | { |
Patrick Venture | d39b6f8 | 2019-05-07 11:04:35 -0700 | [diff] [blame^] | 236 | std::copy(resp.begin() + offset + sizeof(len), resp.end(), |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 237 | std::back_inserter(meta.metadata)); |
| 238 | } |
| 239 | |
| 240 | return meta; |
| 241 | } |
| 242 | |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 243 | StatResponse BlobHandler::getStat(const std::string& id) |
| 244 | { |
| 245 | std::vector<std::uint8_t> name; |
| 246 | std::copy(id.begin(), id.end(), std::back_inserter(name)); |
| 247 | name.push_back(0x00); /* need to add nul-terminator. */ |
| 248 | |
| 249 | return statGeneric(BlobOEMCommands::bmcBlobStat, name); |
| 250 | } |
| 251 | |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 252 | StatResponse BlobHandler::getStat(std::uint16_t session) |
| 253 | { |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 254 | std::vector<std::uint8_t> request; |
| 255 | auto addrSession = reinterpret_cast<const std::uint8_t*>(&session); |
| 256 | std::copy(addrSession, addrSession + sizeof(session), |
| 257 | std::back_inserter(request)); |
| 258 | |
Patrick Venture | 17186ae | 2019-05-06 10:30:55 -0700 | [diff] [blame] | 259 | return statGeneric(BlobOEMCommands::bmcBlobSessionStat, request); |
Patrick Venture | 16a99a6 | 2019-05-03 17:21:30 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 262 | std::uint16_t BlobHandler::openBlob(const std::string& id, |
| 263 | std::uint16_t handlerFlags) |
| 264 | { |
| 265 | std::uint16_t session; |
| 266 | std::vector<std::uint8_t> request, resp; |
| 267 | auto addrFlags = reinterpret_cast<const std::uint8_t*>(&handlerFlags); |
| 268 | |
| 269 | std::copy(addrFlags, addrFlags + sizeof(handlerFlags), |
| 270 | std::back_inserter(request)); |
| 271 | std::copy(id.begin(), id.end(), std::back_inserter(request)); |
| 272 | request.push_back(0x00); /* need to add nul-terminator. */ |
| 273 | |
| 274 | try |
| 275 | { |
| 276 | resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request); |
| 277 | } |
| 278 | catch (const BlobException& b) |
| 279 | { |
| 280 | throw; |
| 281 | } |
| 282 | |
| 283 | if (resp.size() != sizeof(session)) |
| 284 | { |
| 285 | throw BlobException("Did not receive session."); |
| 286 | } |
| 287 | |
| 288 | std::memcpy(&session, resp.data(), sizeof(session)); |
| 289 | return session; |
| 290 | } |
| 291 | |
| 292 | void BlobHandler::closeBlob(std::uint16_t session) |
| 293 | { |
| 294 | std::vector<std::uint8_t> request; |
| 295 | auto addrSession = reinterpret_cast<const std::uint8_t*>(&session); |
| 296 | std::copy(addrSession, addrSession + sizeof(session), |
| 297 | std::back_inserter(request)); |
| 298 | |
| 299 | try |
| 300 | { |
| 301 | sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request); |
| 302 | } |
| 303 | catch (const BlobException& b) |
| 304 | { |
| 305 | std::fprintf(stderr, "Received failure on close: %s\n", b.what()); |
| 306 | } |
| 307 | |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session, |
| 312 | std::uint32_t offset, |
| 313 | std::uint32_t length) |
| 314 | { |
| 315 | std::vector<std::uint8_t> payload; |
| 316 | |
| 317 | payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) + |
| 318 | sizeof(std::uint32_t)); |
| 319 | |
| 320 | auto data = reinterpret_cast<const std::uint8_t*>(&session); |
| 321 | std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload)); |
| 322 | |
| 323 | data = reinterpret_cast<const std::uint8_t*>(&offset); |
| 324 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 325 | |
| 326 | data = reinterpret_cast<const std::uint8_t*>(&length); |
| 327 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 328 | |
| 329 | return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload); |
| 330 | } |
| 331 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 332 | } // namespace ipmiblob |