Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -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 "updater.hpp" |
| 18 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 19 | #include "firmware_handler.hpp" |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 20 | #include "status.hpp" |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 21 | #include "tool_errors.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 22 | #include "util.hpp" |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 23 | |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 24 | #include <algorithm> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 25 | #include <blobs-ipmid/blobs.hpp> |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 26 | #include <cstring> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 27 | #include <ipmiblob/blob_errors.hpp> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 28 | #include <memory> |
Patrick Venture | 2a927e8 | 2019-02-01 07:29:47 -0800 | [diff] [blame] | 29 | #include <string> |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 30 | #include <thread> |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 31 | #include <vector> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 32 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 33 | namespace host_tool |
| 34 | { |
| 35 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 36 | bool UpdateHandler::checkAvailable(const std::string& goalFirmware) |
| 37 | { |
| 38 | std::vector<std::string> blobs = blob->getBlobList(); |
| 39 | |
| 40 | auto blobInst = std::find_if( |
| 41 | blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) { |
| 42 | /* Running into weird scenarios where the string comparison doesn't |
| 43 | * work. TODO: revisit. |
| 44 | */ |
| 45 | return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(), |
| 46 | goalFirmware.length())); |
| 47 | // return (goalFirmware.compare(iter)); |
| 48 | }); |
| 49 | if (blobInst == blobs.end()) |
| 50 | { |
| 51 | std::fprintf(stderr, "%s not found\n", goalFirmware.c_str()); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /* Call stat on /flash/image (or /flash/tarball) and check if data interface |
| 56 | * is supported. |
| 57 | */ |
| 58 | ipmiblob::StatResponse stat; |
| 59 | |
| 60 | try |
| 61 | { |
| 62 | stat = blob->getStat(goalFirmware); |
| 63 | } |
| 64 | catch (const ipmiblob::BlobException& b) |
| 65 | { |
| 66 | std::fprintf(stderr, "Received exception '%s' on getStat\n", b.what()); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | auto supported = handler->supportedType(); |
| 71 | if ((stat.blob_state & supported) == 0) |
| 72 | { |
| 73 | std::fprintf(stderr, "data interface selected not supported.\n"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void UpdateHandler::sendFile(const std::string& target, const std::string& path) |
| 81 | { |
| 82 | std::uint16_t session; |
| 83 | auto supported = handler->supportedType(); |
| 84 | |
| 85 | try |
| 86 | { |
| 87 | session = blob->openBlob( |
| 88 | target, static_cast<std::uint16_t>(supported) | |
| 89 | static_cast<std::uint16_t>(blobs::OpenFlags::write)); |
| 90 | } |
| 91 | catch (const ipmiblob::BlobException& b) |
| 92 | { |
| 93 | throw ToolException("blob exception received: " + |
| 94 | std::string(b.what())); |
| 95 | } |
| 96 | |
| 97 | if (!handler->sendContents(path, session)) |
| 98 | { |
| 99 | /* Need to close the session on failure, or it's stuck open (until the |
| 100 | * blob handler timeout is implemented, and even then, why make it wait. |
| 101 | */ |
| 102 | blob->closeBlob(session); |
| 103 | throw ToolException("Failed to send contents of " + path); |
| 104 | } |
| 105 | |
| 106 | blob->closeBlob(session); |
| 107 | } |
| 108 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 109 | /* Poll an open verification session. Handling closing the session is not yet |
| 110 | * owned by this method. */ |
| 111 | bool pollVerificationStatus(std::uint16_t session, |
| 112 | ipmiblob::BlobInterface* blob) |
| 113 | { |
| 114 | using namespace std::chrono_literals; |
| 115 | |
| 116 | static constexpr auto verificationSleep = 5s; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 117 | ipmi_flash::VerifyCheckResponses result = |
| 118 | ipmi_flash::VerifyCheckResponses::other; |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 119 | |
| 120 | try |
| 121 | { |
Patrick Venture | c674474 | 2019-05-28 15:57:21 -0700 | [diff] [blame] | 122 | static constexpr int commandAttempts = 20; |
| 123 | int attempts = 0; |
| 124 | bool exitLoop = false; |
| 125 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 126 | /* Reach back the current status from the verification service output. |
| 127 | */ |
| 128 | while (attempts++ < commandAttempts) |
| 129 | { |
| 130 | ipmiblob::StatResponse resp = blob->getStat(session); |
| 131 | |
| 132 | if (resp.metadata.size() != sizeof(std::uint8_t)) |
| 133 | { |
| 134 | /* TODO: How do we want to handle the verification failures, |
| 135 | * because closing the session to the verify blob has a special |
| 136 | * as-of-yet not fully defined behavior. |
| 137 | */ |
| 138 | std::fprintf(stderr, "Received invalid metadata response!!!\n"); |
| 139 | } |
| 140 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 141 | result = |
| 142 | static_cast<ipmi_flash::VerifyCheckResponses>(resp.metadata[0]); |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 143 | |
| 144 | switch (result) |
| 145 | { |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 146 | case ipmi_flash::VerifyCheckResponses::failed: |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 147 | std::fprintf(stderr, "failed\n"); |
| 148 | exitLoop = true; |
| 149 | break; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 150 | case ipmi_flash::VerifyCheckResponses::other: |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 151 | std::fprintf(stderr, "other\n"); |
| 152 | break; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 153 | case ipmi_flash::VerifyCheckResponses::running: |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 154 | std::fprintf(stderr, "running\n"); |
| 155 | break; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 156 | case ipmi_flash::VerifyCheckResponses::success: |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 157 | std::fprintf(stderr, "success\n"); |
| 158 | exitLoop = true; |
| 159 | break; |
| 160 | default: |
| 161 | std::fprintf(stderr, "wat\n"); |
| 162 | } |
| 163 | |
| 164 | if (exitLoop) |
| 165 | { |
| 166 | break; |
| 167 | } |
| 168 | std::this_thread::sleep_for(verificationSleep); |
| 169 | } |
| 170 | } |
| 171 | catch (const ipmiblob::BlobException& b) |
| 172 | { |
| 173 | throw ToolException("blob exception received: " + |
| 174 | std::string(b.what())); |
| 175 | } |
| 176 | |
| 177 | /* TODO: If this is reached and it's not success, it may be worth just |
| 178 | * throwing a ToolException with a timeout message specifying the final |
| 179 | * read's value. |
| 180 | * |
| 181 | * TODO: Given that excepting from certain points leaves the BMC update |
| 182 | * state machine in an inconsistent state, we need to carefully evaluate |
| 183 | * which exceptions from the lower layers allow one to try and delete the |
| 184 | * blobs to rollback the state and progress. |
| 185 | */ |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 186 | return (result == ipmi_flash::VerifyCheckResponses::success); |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 189 | bool UpdateHandler::verifyFile(const std::string& target) |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 190 | { |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 191 | std::uint16_t session; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 192 | bool success = false; |
| 193 | |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 194 | try |
| 195 | { |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 196 | session = blob->openBlob( |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 197 | target, static_cast<std::uint16_t>(blobs::OpenFlags::write)); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 198 | } |
| 199 | catch (const ipmiblob::BlobException& b) |
| 200 | { |
| 201 | throw ToolException("blob exception received: " + |
| 202 | std::string(b.what())); |
| 203 | } |
| 204 | |
| 205 | std::fprintf( |
| 206 | stderr, |
| 207 | "Committing to verification file to trigger verification service\n"); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 208 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 209 | try |
| 210 | { |
| 211 | blob->commit(session, {}); |
| 212 | } |
| 213 | catch (const ipmiblob::BlobException& b) |
| 214 | { |
| 215 | throw ToolException("blob exception received: " + |
| 216 | std::string(b.what())); |
| 217 | } |
| 218 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 219 | std::fprintf(stderr, |
| 220 | "Calling stat on verification session to check status\n"); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 221 | |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 222 | if (pollVerificationStatus(session, blob)) |
| 223 | { |
| 224 | std::fprintf(stderr, "Verification returned success\n"); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 225 | success = true; |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 226 | } |
| 227 | else |
| 228 | { |
| 229 | std::fprintf(stderr, "Verification returned non-success (could still " |
| 230 | "be running (unlikely))\n"); |
| 231 | } |
| 232 | |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 233 | blob->closeBlob(session); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 234 | return (success == true); |
| 235 | } |
| 236 | |
| 237 | void updaterMain(UpdateHandler* updater, const std::string& imagePath, |
| 238 | const std::string& signaturePath) |
| 239 | { |
| 240 | /* TODO(venture): Add optional parameter to specify the flash type, default |
| 241 | * to legacy for now. |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 242 | */ |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 243 | bool goalSupported = |
| 244 | updater->checkAvailable(ipmi_flash::staticLayoutBlobId); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 245 | if (!goalSupported) |
| 246 | { |
| 247 | throw ToolException("Goal firmware or interface not supported"); |
| 248 | } |
| 249 | |
| 250 | /* Yay, our data handler is supported. */ |
| 251 | |
| 252 | /* Send over the firmware image. */ |
| 253 | std::fprintf(stderr, "Sending over the firmware image.\n"); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 254 | updater->sendFile(ipmi_flash::staticLayoutBlobId, imagePath); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 255 | |
| 256 | /* Send over the hash contents. */ |
| 257 | std::fprintf(stderr, "Sending over the hash file.\n"); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 258 | updater->sendFile(ipmi_flash::hashBlobId, signaturePath); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 259 | |
| 260 | /* Trigger the verification by opening the verify file. */ |
| 261 | std::fprintf(stderr, "Opening the verification file\n"); |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 262 | if (updater->verifyFile(ipmi_flash::verifyBlobId)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 263 | { |
| 264 | std::fprintf(stderr, "succeeded\n"); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | std::fprintf(stderr, "failed\n"); |
| 269 | } |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 270 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 271 | |
| 272 | } // namespace host_tool |