Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [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 "handler.hpp" |
| 18 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 19 | #include "flags.hpp" |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 20 | #include "helper.hpp" |
| 21 | #include "status.hpp" |
| 22 | #include "tool_errors.hpp" |
| 23 | #include "util.hpp" |
| 24 | |
| 25 | #include <algorithm> |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 26 | #include <cstdint> |
| 27 | #include <cstring> |
| 28 | #include <ipmiblob/blob_errors.hpp> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace host_tool |
| 33 | { |
| 34 | |
| 35 | bool UpdateHandler::checkAvailable(const std::string& goalFirmware) |
| 36 | { |
| 37 | std::vector<std::string> blobs = blob->getBlobList(); |
| 38 | |
| 39 | auto blobInst = std::find_if( |
| 40 | blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) { |
| 41 | /* Running into weird scenarios where the string comparison doesn't |
| 42 | * work. TODO: revisit. |
| 43 | */ |
| 44 | return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(), |
| 45 | goalFirmware.length())); |
| 46 | // return (goalFirmware.compare(iter)); |
| 47 | }); |
| 48 | if (blobInst == blobs.end()) |
| 49 | { |
| 50 | std::fprintf(stderr, "%s not found\n", goalFirmware.c_str()); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | /* Call stat on /flash/image (or /flash/tarball) and check if data interface |
| 55 | * is supported. |
| 56 | */ |
| 57 | ipmiblob::StatResponse stat; |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | stat = blob->getStat(goalFirmware); |
| 62 | } |
| 63 | catch (const ipmiblob::BlobException& b) |
| 64 | { |
| 65 | std::fprintf(stderr, "Received exception '%s' on getStat\n", b.what()); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | auto supported = handler->supportedType(); |
| 70 | if ((stat.blob_state & supported) == 0) |
| 71 | { |
| 72 | std::fprintf(stderr, "data interface selected not supported.\n"); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void UpdateHandler::sendFile(const std::string& target, const std::string& path) |
| 80 | { |
| 81 | std::uint16_t session; |
| 82 | auto supported = handler->supportedType(); |
| 83 | |
| 84 | try |
| 85 | { |
| 86 | session = blob->openBlob( |
| 87 | target, static_cast<std::uint16_t>(supported) | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 88 | static_cast<std::uint16_t>( |
| 89 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite)); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 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 | |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 109 | bool UpdateHandler::verifyFile(const std::string& target, bool ignoreStatus) |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 110 | { |
| 111 | std::uint16_t session; |
| 112 | bool success = false; |
| 113 | |
| 114 | try |
| 115 | { |
| 116 | session = blob->openBlob( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 117 | target, static_cast<std::uint16_t>( |
| 118 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite)); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 119 | } |
| 120 | catch (const ipmiblob::BlobException& b) |
| 121 | { |
| 122 | throw ToolException("blob exception received: " + |
| 123 | std::string(b.what())); |
| 124 | } |
| 125 | |
| 126 | std::fprintf(stderr, "Committing to %s to trigger service\n", |
| 127 | target.c_str()); |
| 128 | |
| 129 | try |
| 130 | { |
| 131 | blob->commit(session, {}); |
| 132 | } |
| 133 | catch (const ipmiblob::BlobException& b) |
| 134 | { |
Patrick Venture | de73c3b | 2019-06-20 19:38:17 -0700 | [diff] [blame] | 135 | blob->closeBlob(session); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 136 | throw ToolException("blob exception received: " + |
| 137 | std::string(b.what())); |
| 138 | } |
| 139 | |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 140 | if (ignoreStatus) |
| 141 | { |
| 142 | // Skip checking the blob for status if ignoreStatus is enabled |
| 143 | blob->closeBlob(session); |
| 144 | return true; |
| 145 | } |
| 146 | |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 147 | std::fprintf(stderr, "Calling stat on %s session to check status\n", |
| 148 | target.c_str()); |
| 149 | |
| 150 | if (pollStatus(session, blob)) |
| 151 | { |
| 152 | std::fprintf(stderr, "Returned success\n"); |
| 153 | success = true; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | std::fprintf(stderr, "Returned non-success (could still " |
| 158 | "be running (unlikely))\n"); |
| 159 | } |
| 160 | |
| 161 | blob->closeBlob(session); |
| 162 | return (success == true); |
| 163 | } |
| 164 | |
| 165 | void UpdateHandler::cleanArtifacts() |
| 166 | { |
| 167 | /* open(), commit(), close() */ |
| 168 | std::uint16_t session; |
| 169 | |
| 170 | /* Errors aren't important for this call. */ |
| 171 | try |
| 172 | { |
| 173 | std::fprintf(stderr, "Opening the cleanup blob\n"); |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 174 | session = blob->openBlob( |
| 175 | ipmi_flash::cleanupBlobId, |
| 176 | static_cast<std::uint16_t>( |
| 177 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite)); |
Patrick Venture | de73c3b | 2019-06-20 19:38:17 -0700 | [diff] [blame] | 178 | } |
| 179 | catch (...) |
| 180 | { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | try |
| 185 | { |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 186 | std::fprintf(stderr, "Committing to the cleanup blob\n"); |
| 187 | blob->commit(session, {}); |
| 188 | std::fprintf(stderr, "Closing cleanup blob\n"); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 189 | } |
| 190 | catch (...) |
| 191 | { |
| 192 | } |
Patrick Venture | de73c3b | 2019-06-20 19:38:17 -0700 | [diff] [blame] | 193 | |
| 194 | blob->closeBlob(session); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } // namespace host_tool |