Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -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 | |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 17 | #include "blob_handler.hpp" |
| 18 | |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 19 | #include "blob_errors.hpp" |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 20 | #include "crc.hpp" |
| 21 | #include "ipmi_errors.hpp" |
| 22 | |
| 23 | #include <array> |
| 24 | #include <cstring> |
| 25 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 26 | namespace host_tool |
| 27 | { |
| 28 | |
| 29 | namespace |
| 30 | { |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 31 | const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00}; |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 32 | } |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 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; |
| 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 Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 65 | throw BlobException(e.what()); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 76 | std::size_t headerSize = ipmiPhosphorOen.size() + sizeof(std::uint16_t); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 77 | |
| 78 | /* This cannot be a response because it's smaller than the smallest |
| 79 | * response. |
| 80 | */ |
| 81 | if (reply.size() < headerSize) |
| 82 | { |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 83 | throw BlobException("Invalid response length"); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Verify the OEN. */ |
| 87 | if (std::memcmp(ipmiPhosphorOen.data(), reply.data(), |
| 88 | ipmiPhosphorOen.size()) != 0) |
| 89 | { |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 90 | throw BlobException("Invalid OEN received"); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 91 | } |
| 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 Venture | bcec9c6 | 2018-12-14 13:58:41 -0800 | [diff] [blame] | 98 | for (const auto& byte : reply) |
| 99 | { |
| 100 | std::fprintf(stderr, "0x%02x ", byte); |
| 101 | } |
| 102 | std::fprintf(stderr, "\n"); |
| 103 | |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 104 | std::vector<std::uint8_t> bytes; |
Patrick Venture | bcec9c6 | 2018-12-14 13:58:41 -0800 | [diff] [blame] | 105 | bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end()); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 106 | |
| 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 Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 112 | throw BlobException("Invalid CRC on received data."); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return bytes; |
| 116 | } |
| 117 | |
| 118 | int BlobHandler::getBlobCount() |
| 119 | { |
| 120 | std::uint32_t count; |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 121 | 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 Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 137 | std::fprintf(stderr, "BLOB Count: %d\n", count); |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 138 | return count; |
| 139 | } |
| 140 | |
| 141 | std::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 Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 145 | |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 146 | std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload)); |
| 147 | |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 148 | 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 Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 160 | std::vector<std::string> BlobHandler::getBlobList() |
| 161 | { |
Patrick Venture | c79faa1 | 2018-12-12 13:12:21 -0800 | [diff] [blame] | 162 | 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 Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 176 | } |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 177 | |
| 178 | StatResponse BlobHandler::getStat(const std::string& id) |
| 179 | { |
| 180 | StatResponse meta; |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 181 | std::vector<std::uint8_t> name, resp; |
| 182 | |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 183 | std::copy(id.begin(), id.end(), std::back_inserter(name)); |
Patrick Venture | 0d88a12 | 2018-12-17 07:52:04 -0800 | [diff] [blame] | 184 | name.push_back(0x00); /* need to add nul-terminator. */ |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 185 | |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 186 | try |
| 187 | { |
| 188 | resp = sendIpmiPayload(BlobOEMCommands::bmcBlobStat, name); |
| 189 | } |
| 190 | catch (const BlobException& b) |
| 191 | { |
| 192 | throw; |
| 193 | } |
| 194 | |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 195 | 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 Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 207 | |
| 208 | std::uint16_t |
| 209 | BlobHandler::openBlob(const std::string& id, |
| 210 | blobs::FirmwareBlobHandler::UpdateFlags handlerFlags) |
| 211 | { |
| 212 | std::uint16_t session; |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 213 | std::vector<std::uint8_t> request, resp; |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 214 | std::uint16_t flags = |
| 215 | blobs::FirmwareBlobHandler::UpdateFlags::openWrite | handlerFlags; |
| 216 | auto addrFlags = reinterpret_cast<std::uint8_t*>(&flags); |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 217 | |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 218 | std::copy(addrFlags, addrFlags + sizeof(flags), |
| 219 | std::back_inserter(request)); |
| 220 | std::copy(id.begin(), id.end(), std::back_inserter(request)); |
Patrick Venture | 0d88a12 | 2018-12-17 07:52:04 -0800 | [diff] [blame] | 221 | request.push_back(0x00); /* need to add nul-terminator. */ |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 222 | |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 223 | try |
| 224 | { |
| 225 | resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request); |
| 226 | } |
| 227 | catch (const BlobException& b) |
| 228 | { |
| 229 | throw; |
| 230 | } |
| 231 | |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 232 | 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 Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 240 | |
Patrick Venture | 9a5ce56 | 2018-12-14 18:56:04 -0800 | [diff] [blame] | 241 | void 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 Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 260 | } // namespace host_tool |