Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -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 | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 17 | #include "firmware_handler.hpp" |
| 18 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 19 | #include "data.hpp" |
| 20 | #include "flags.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 21 | #include "image_handler.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 22 | #include "status.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 23 | #include "util.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 24 | |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 25 | #include <blobs-ipmid/blobs.hpp> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 26 | |
| 27 | #include <algorithm> |
Patrick Venture | 192d60f | 2018-11-06 11:11:59 -0800 | [diff] [blame] | 28 | #include <cstdint> |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 29 | #include <cstring> |
Patrick Venture | b3b4db7 | 2019-05-15 11:30:24 -0700 | [diff] [blame] | 30 | #include <fstream> |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 31 | #include <memory> |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 35 | namespace ipmi_flash |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 36 | { |
Patrick Venture | b3b4db7 | 2019-05-15 11:30:24 -0700 | [diff] [blame] | 37 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 38 | std::unique_ptr<blobs::GenericBlobInterface> |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 39 | FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 40 | std::vector<HandlerPack>&& firmwares, |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 41 | const std::vector<DataHandlerPack>& transports, ActionMap&& actionPacks) |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 42 | { |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 43 | /* There must be at least one in addition to the hash blob handler. */ |
| 44 | if (firmwares.size() < 2) |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 45 | { |
Patrick Venture | 1ab824a | 2020-05-26 19:29:57 -0700 | [diff] [blame] | 46 | std::fprintf(stderr, "Must provide at least two firmware handlers."); |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 47 | return nullptr; |
| 48 | } |
Patrick Venture | 4e2a143 | 2019-11-06 20:06:07 -0800 | [diff] [blame] | 49 | if (transports.empty()) |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 50 | { |
| 51 | return nullptr; |
| 52 | } |
Patrick Venture | 1626538 | 2019-11-06 19:31:36 -0800 | [diff] [blame] | 53 | if (actionPacks.empty()) |
| 54 | { |
| 55 | return nullptr; |
| 56 | } |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 57 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 58 | std::vector<std::string> blobs; |
| 59 | for (const auto& item : firmwares) |
| 60 | { |
| 61 | blobs.push_back(item.blobName); |
| 62 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 63 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 64 | if (0 == std::count(blobs.begin(), blobs.end(), hashBlobId)) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 65 | { |
| 66 | return nullptr; |
| 67 | } |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 68 | |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 69 | return std::make_unique<FirmwareBlobHandler>( |
| 70 | std::move(firmwares), blobs, transports, std::move(actionPacks)); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 73 | /* Check if the path is in our supported list (or active list). */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 74 | bool FirmwareBlobHandler::canHandleBlob(const std::string& path) |
| 75 | { |
Patrick Venture | 6032dc0 | 2019-05-17 11:01:44 -0700 | [diff] [blame] | 76 | return (std::count(blobIDs.begin(), blobIDs.end(), path) > 0); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 77 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 78 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 79 | /* |
| 80 | * Grab the list of supported firmware. |
| 81 | * |
| 82 | * If there's an open firmware session, it'll already be present in the |
| 83 | * list as "/flash/active/image", and if the hash has started, |
| 84 | * "/flash/active/hash" regardless of mechanism. This is done in the open |
| 85 | * comamnd, no extra work is required here. |
| 86 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 87 | std::vector<std::string> FirmwareBlobHandler::getBlobIds() |
| 88 | { |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 89 | return blobIDs; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 90 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 91 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 92 | /* |
| 93 | * Per the design, this mean abort, and this will trigger whatever |
| 94 | * appropriate actions are required to abort the process. |
| 95 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 96 | bool FirmwareBlobHandler::deleteBlob(const std::string& path) |
| 97 | { |
Patrick Venture | 7267676 | 2019-06-17 11:22:38 -0700 | [diff] [blame] | 98 | switch (state) |
| 99 | { |
| 100 | case UpdateState::notYetStarted: |
| 101 | /* Trying to delete anything at this point has no effect and returns |
| 102 | * false. |
| 103 | */ |
| 104 | return false; |
| 105 | case UpdateState::verificationPending: |
Patrick Venture | 2ca6652 | 2019-06-17 11:58:38 -0700 | [diff] [blame] | 106 | abortProcess(); |
| 107 | return true; |
Patrick Venture | 7267676 | 2019-06-17 11:22:38 -0700 | [diff] [blame] | 108 | case UpdateState::updatePending: |
Patrick Venture | c9f6239 | 2019-06-17 12:17:26 -0700 | [diff] [blame] | 109 | abortProcess(); |
| 110 | return true; |
Patrick Venture | 7267676 | 2019-06-17 11:22:38 -0700 | [diff] [blame] | 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 115 | return false; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 116 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 117 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 118 | /* |
| 119 | * Stat on the files will return information such as what supported |
| 120 | * transport mechanisms are available. |
| 121 | * |
| 122 | * Stat on an active file or hash will return information such as the size |
| 123 | * of the data cached, and any additional pertinent information. The |
| 124 | * blob_state on the active files will return the state of the update. |
| 125 | */ |
Patrick Venture | e312f39 | 2019-06-04 07:44:37 -0700 | [diff] [blame] | 126 | bool FirmwareBlobHandler::stat(const std::string& path, blobs::BlobMeta* meta) |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 127 | { |
Patrick Venture | 46637c8 | 2018-11-06 15:20:24 -0800 | [diff] [blame] | 128 | /* We know we support this path because canHandle is called ahead */ |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 129 | if (path == verifyBlobId || path == activeImageBlobId || |
Patrick Venture | 5f56269 | 2019-05-30 16:49:46 -0700 | [diff] [blame] | 130 | path == activeHashBlobId || path == updateBlobId) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 131 | { |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 132 | /* These blobs are placeholders that indicate things, or allow actions, |
| 133 | * but are not stat-able as-is. |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 134 | */ |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 135 | return false; |
Patrick Venture | 46637c8 | 2018-11-06 15:20:24 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 138 | /* They are requesting information about the generic blob_id. */ |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 139 | |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 140 | /* Older host tools expect the blobState to contain a bitmask of available |
| 141 | * transport backends, so report that we support all of them in order to |
| 142 | * preserve backwards compatibility. |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 143 | */ |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 144 | meta->blobState = transportMask; |
| 145 | meta->size = 0; |
Patrick Venture | d342a95 | 2019-05-29 09:09:10 -0700 | [diff] [blame] | 146 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 147 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 148 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 149 | ActionStatus FirmwareBlobHandler::getActionStatus() |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 150 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 151 | ActionStatus value = ActionStatus::unknown; |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 152 | auto* pack = getActionPack(); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 153 | |
| 154 | switch (state) |
| 155 | { |
| 156 | case UpdateState::verificationPending: |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 157 | value = ActionStatus::unknown; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 158 | break; |
| 159 | case UpdateState::verificationStarted: |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 160 | /* If we got here, there must be data AND a hash, not just a hash, |
| 161 | * therefore pack will be known. */ |
| 162 | if (!pack) |
| 163 | { |
| 164 | break; |
| 165 | } |
| 166 | value = pack->verification->status(); |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 167 | lastVerificationStatus = value; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 168 | break; |
| 169 | case UpdateState::verificationCompleted: |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 170 | value = lastVerificationStatus; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 171 | break; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 172 | case UpdateState::updatePending: |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 173 | value = ActionStatus::unknown; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 174 | break; |
| 175 | case UpdateState::updateStarted: |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 176 | if (!pack) |
| 177 | { |
| 178 | break; |
| 179 | } |
| 180 | value = pack->update->status(); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 181 | lastUpdateStatus = value; |
| 182 | break; |
| 183 | case UpdateState::updateCompleted: |
| 184 | value = lastUpdateStatus; |
| 185 | break; |
| 186 | default: |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | return value; |
| 191 | } |
| 192 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 193 | /* |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 194 | * Return stat information on an open session. It therefore must be an active |
| 195 | * handle to either the active image or active hash. |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 196 | */ |
Patrick Venture | e312f39 | 2019-06-04 07:44:37 -0700 | [diff] [blame] | 197 | bool FirmwareBlobHandler::stat(uint16_t session, blobs::BlobMeta* meta) |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 198 | { |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 199 | auto item = lookup.find(session); |
| 200 | if (item == lookup.end()) |
| 201 | { |
| 202 | return false; |
| 203 | } |
| 204 | |
Patrick Venture | b3b4db7 | 2019-05-15 11:30:24 -0700 | [diff] [blame] | 205 | /* The size here refers to the size of the file -- of something analagous. |
| 206 | */ |
| 207 | meta->size = (item->second->imageHandler) |
| 208 | ? item->second->imageHandler->getSize() |
| 209 | : 0; |
| 210 | |
| 211 | meta->metadata.clear(); |
| 212 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 213 | if (item->second->activePath == verifyBlobId || |
| 214 | item->second->activePath == updateBlobId) |
Patrick Venture | b3b4db7 | 2019-05-15 11:30:24 -0700 | [diff] [blame] | 215 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 216 | ActionStatus value = getActionStatus(); |
Patrick Venture | 699750d | 2019-05-15 09:24:09 -0700 | [diff] [blame] | 217 | |
Patrick Venture | e955e07 | 2019-05-15 16:16:46 -0700 | [diff] [blame] | 218 | meta->metadata.push_back(static_cast<std::uint8_t>(value)); |
| 219 | |
| 220 | /* Change the firmware handler's state and the blob's stat value |
| 221 | * depending. |
| 222 | */ |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 223 | if (value == ActionStatus::success || value == ActionStatus::failed) |
Patrick Venture | e955e07 | 2019-05-15 16:16:46 -0700 | [diff] [blame] | 224 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 225 | if (item->second->activePath == verifyBlobId) |
Patrick Venture | e955e07 | 2019-05-15 16:16:46 -0700 | [diff] [blame] | 226 | { |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 227 | changeState(UpdateState::verificationCompleted); |
Patrick Venture | e955e07 | 2019-05-15 16:16:46 -0700 | [diff] [blame] | 228 | } |
| 229 | else |
| 230 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 231 | /* item->second->activePath == updateBlobId */ |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 232 | changeState(UpdateState::updateCompleted); |
Patrick Venture | e955e07 | 2019-05-15 16:16:46 -0700 | [diff] [blame] | 233 | } |
Patrick Venture | 40d7ffc | 2019-05-30 17:12:06 -0700 | [diff] [blame] | 234 | |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 235 | item->second->flags &= ~blobs::StateFlags::committing; |
| 236 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 237 | if (value == ActionStatus::success) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 238 | { |
| 239 | item->second->flags |= blobs::StateFlags::committed; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | item->second->flags |= blobs::StateFlags::commit_error; |
| 244 | } |
| 245 | } |
Patrick Venture | 40d7ffc | 2019-05-30 17:12:06 -0700 | [diff] [blame] | 246 | } |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 247 | |
Patrick Venture | e955e07 | 2019-05-15 16:16:46 -0700 | [diff] [blame] | 248 | /* The blobState here relates to an active sesion, so we should return the |
| 249 | * flags used to open this session. |
| 250 | */ |
| 251 | meta->blobState = item->second->flags; |
| 252 | |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 253 | /* The metadata blob returned comes from the data handler... it's used for |
| 254 | * instance, in P2A bridging to get required information about the mapping, |
| 255 | * and is the "opposite" of the lpc writemeta requirement. |
| 256 | */ |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 257 | if (item->second->dataHandler) |
| 258 | { |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 259 | auto bytes = item->second->dataHandler->readMeta(); |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 260 | meta->metadata.insert(meta->metadata.begin(), bytes.begin(), |
| 261 | bytes.end()); |
| 262 | } |
| 263 | |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 264 | return true; |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 268 | * If you open /flash/image or /flash/tarball, or /flash/hash it will |
| 269 | * interpret the open flags and perform whatever actions are required for |
| 270 | * that update process. The session returned can be used immediately for |
| 271 | * sending data down, without requiring one to open the new active file. |
| 272 | * |
| 273 | * If you open the active flash image or active hash it will let you |
| 274 | * overwrite pieces, depending on the state. |
| 275 | * |
| 276 | * Once the verification process has started the active files cannot be |
| 277 | * opened. |
| 278 | * |
| 279 | * You can only have one open session at a time. Which means, you can only |
| 280 | * have one file open at a time. Trying to open the hash blob_id while you |
| 281 | * still have the flash image blob_id open will fail. Opening the flash |
| 282 | * blob_id when it is already open will fail. |
| 283 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 284 | bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags, |
| 285 | const std::string& path) |
| 286 | { |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 287 | /* Is there an open session already? We only allow one at a time. |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 288 | * |
Patrick Venture | 7c8b97e | 2018-11-08 09:14:30 -0800 | [diff] [blame] | 289 | * Further on this, if there's an active session to the hash we don't allow |
| 290 | * re-opening the image, and if we have the image open, we don't allow |
| 291 | * opening the hash. This design decision may be re-evaluated, and changed |
| 292 | * to only allow one session per object type (of the two types). But, |
| 293 | * consider if the hash is open, do we want to allow writing to the image? |
| 294 | * And why would we? But, really, the point of no-return is once the |
| 295 | * verification process has begun -- which is done via commit() on the hash |
| 296 | * blob_id, we no longer want to allow updating the contents. |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 297 | */ |
Brandon Kim | 8fc6087 | 2019-10-18 15:15:50 -0700 | [diff] [blame] | 298 | if (fileOpen()) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 299 | { |
| 300 | return false; |
| 301 | } |
| 302 | |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 303 | /* The active blobs are only meant to indicate status that something has |
| 304 | * opened the image file or the hash file. |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 305 | */ |
Patrick Venture | 19f5d88 | 2019-05-30 14:34:55 -0700 | [diff] [blame] | 306 | if (path == activeImageBlobId || path == activeHashBlobId) |
| 307 | { |
| 308 | /* 2a) are they opening the active image? this can only happen if they |
| 309 | * already started one (due to canHandleBlob's behavior). |
| 310 | */ |
| 311 | /* 2b) are they opening the active hash? this can only happen if they |
| 312 | * already started one (due to canHandleBlob's behavior). |
| 313 | */ |
| 314 | return false; |
| 315 | } |
| 316 | |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 317 | /* Check that they've opened for writing - read back not currently |
| 318 | * supported. |
| 319 | */ |
| 320 | if ((flags & blobs::OpenFlags::write) == 0) |
| 321 | { |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | /* Because canHandleBlob is called before open, we know that if they try to |
| 326 | * open the verifyBlobId, they're in a state where it's present. |
| 327 | */ |
| 328 | |
| 329 | switch (state) |
| 330 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 331 | case UpdateState::notYetStarted: |
| 332 | /* Only hashBlobId and firmware BlobIds present. */ |
| 333 | break; |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 334 | case UpdateState::uploadInProgress: |
| 335 | /* Unreachable code because if it's started a file is open. */ |
| 336 | break; |
| 337 | case UpdateState::verificationPending: |
| 338 | /* Handle opening the verifyBlobId --> we know the image and hash |
Brandon Kim | 8fc6087 | 2019-10-18 15:15:50 -0700 | [diff] [blame] | 339 | * aren't open because of the fileOpen() check. They can still open |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 340 | * other files from this state to transition back into |
| 341 | * uploadInProgress. |
| 342 | * |
| 343 | * The file must be opened for writing, but no transport mechanism |
| 344 | * specified since it's irrelevant. |
| 345 | */ |
| 346 | if (path == verifyBlobId) |
| 347 | { |
| 348 | verifyImage.flags = flags; |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 349 | |
| 350 | lookup[session] = &verifyImage; |
| 351 | |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | break; |
| 355 | case UpdateState::verificationStarted: |
| 356 | case UpdateState::verificationCompleted: |
| 357 | /* Unreachable code because if it's started a file is open. */ |
| 358 | return false; |
| 359 | case UpdateState::updatePending: |
| 360 | { |
| 361 | /* When in this state, they can only open the updateBlobId */ |
| 362 | if (path == updateBlobId) |
| 363 | { |
| 364 | updateImage.flags = flags; |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 365 | |
| 366 | lookup[session] = &updateImage; |
| 367 | |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 368 | return true; |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | case UpdateState::updateStarted: |
| 376 | case UpdateState::updateCompleted: |
| 377 | /* Unreachable code because if it's started a file is open. */ |
| 378 | break; |
| 379 | default: |
| 380 | break; |
| 381 | } |
| 382 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 383 | /* To support multiple firmware options, we need to make sure they're |
| 384 | * opening the one they already opened during this update sequence, or it's |
| 385 | * the first time they're opening it. |
| 386 | */ |
| 387 | if (path != hashBlobId) |
| 388 | { |
| 389 | /* If they're not opening the hashBlobId they must be opening a firmware |
| 390 | * handler. |
| 391 | */ |
| 392 | if (openedFirmwareType.empty()) |
| 393 | { |
| 394 | /* First time for this sequence. */ |
| 395 | openedFirmwareType = path; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | if (openedFirmwareType != path) |
| 400 | { |
| 401 | /* Previously, in this sequence they opened /flash/image, and |
| 402 | * now they're opening /flash/bios without finishing out |
| 403 | * /flash/image (for example). |
| 404 | */ |
| 405 | std::fprintf(stderr, "Trying to open alternate firmware while " |
| 406 | "unfinished with other firmware.\n"); |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Patrick Venture | 723113f | 2019-06-05 09:38:35 -0700 | [diff] [blame] | 412 | /* There are two abstractions at play, how you get the data and how you |
| 413 | * handle that data. such that, whether the data comes from the PCI bridge |
| 414 | * or LPC bridge is not connected to whether the data goes into a static |
| 415 | * layout flash update or a UBI tarball. |
| 416 | */ |
| 417 | |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 418 | std::uint16_t transportFlag = flags & transportMask; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 419 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 420 | /* How are they expecting to copy this data? */ |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 421 | auto d = std::find_if(transports.begin(), transports.end(), |
| 422 | [&transportFlag](const auto& iter) { |
| 423 | return (iter.bitmask == transportFlag); |
| 424 | }); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 425 | if (d == transports.end()) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 426 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 430 | /* We found the transport handler they requested */ |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 431 | |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 432 | /* Elsewhere I do this check by checking "if ::ipmi" because that's the |
| 433 | * only non-external data pathway -- but this is just a more generic |
| 434 | * approach to that. |
| 435 | */ |
| 436 | if (d->handler) |
| 437 | { |
| 438 | /* If the data handler open call fails, open fails. */ |
| 439 | if (!d->handler->open()) |
| 440 | { |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 445 | /* Do we have a file handler for the type of file they're opening. |
| 446 | * Note: This should only fail if something is somehow crazy wrong. |
| 447 | * Since the canHandle() said yes, and that's tied into the list of explicit |
| 448 | * firmware handers (and file handlers, like this'll know where to write the |
| 449 | * tarball, etc). |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 450 | */ |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 451 | auto h = std::find_if( |
| 452 | handlers.begin(), handlers.end(), |
| 453 | [&path](const auto& iter) { return (iter.blobName == path); }); |
| 454 | if (h == handlers.end()) |
| 455 | { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | /* Ok, so we found a handler that matched, so call open() */ |
| 460 | if (!h->handler->open(path)) |
| 461 | { |
| 462 | return false; |
| 463 | } |
| 464 | |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 465 | Session* curr; |
| 466 | const std::string* active; |
| 467 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 468 | if (path == hashBlobId) |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 469 | { |
| 470 | /* 2c) are they opening the /flash/hash ? (to start the process) */ |
| 471 | curr = &activeHash; |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 472 | active = &activeHashBlobId; |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 473 | } |
| 474 | else |
| 475 | { |
| 476 | curr = &activeImage; |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 477 | active = &activeImageBlobId; |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 480 | curr->flags = flags; |
| 481 | curr->dataHandler = d->handler; |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 482 | curr->imageHandler = h->handler.get(); |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 483 | |
| 484 | lookup[session] = curr; |
| 485 | |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 486 | addBlobId(*active); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 487 | removeBlobId(verifyBlobId); |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 488 | |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 489 | changeState(UpdateState::uploadInProgress); |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 490 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 491 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 492 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 493 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 494 | /** |
| 495 | * The write command really just grabs the data from wherever it is and sends it |
| 496 | * to the image handler. It's the image handler's responsibility to deal with |
| 497 | * the data provided. |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 498 | * |
| 499 | * This receives a session from the blob manager, therefore it is always called |
| 500 | * between open() and close(). |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 501 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 502 | bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset, |
| 503 | const std::vector<uint8_t>& data) |
| 504 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 505 | auto item = lookup.find(session); |
| 506 | if (item == lookup.end()) |
| 507 | { |
| 508 | return false; |
| 509 | } |
| 510 | |
Patrick Venture | b1b68fc | 2018-11-09 09:37:04 -0800 | [diff] [blame] | 511 | /* Prevent writing during verification. */ |
| 512 | if (state == UpdateState::verificationStarted) |
| 513 | { |
| 514 | return false; |
| 515 | } |
| 516 | |
Patrick Venture | 4e2fdcd | 2019-05-30 14:58:57 -0700 | [diff] [blame] | 517 | /* Prevent writing to the verification or update blobs. */ |
| 518 | if (item->second->activePath == verifyBlobId || |
| 519 | item->second->activePath == updateBlobId) |
Patrick Venture | 8af15eb | 2019-05-15 09:39:22 -0700 | [diff] [blame] | 520 | { |
| 521 | return false; |
| 522 | } |
Patrick Venture | 699750d | 2019-05-15 09:24:09 -0700 | [diff] [blame] | 523 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 524 | std::vector<std::uint8_t> bytes; |
| 525 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 526 | if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 527 | { |
| 528 | bytes = data; |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | /* little endian required per design, and so on, but TODO: do endianness |
| 533 | * with boost. |
| 534 | */ |
| 535 | struct ExtChunkHdr header; |
| 536 | |
| 537 | if (data.size() != sizeof(header)) |
| 538 | { |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | std::memcpy(&header, data.data(), data.size()); |
| 543 | bytes = item->second->dataHandler->copyFrom(header.length); |
| 544 | } |
| 545 | |
| 546 | return item->second->imageHandler->write(offset, bytes); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 547 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 548 | |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 549 | /* |
| 550 | * If the active session (image or hash) is over LPC, this allows |
| 551 | * configuring it. This option is only available before you start |
| 552 | * writing data for the given item (image or hash). This will return |
| 553 | * false at any other part. -- the lpc handler portion will know to return |
| 554 | * false. |
| 555 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 556 | bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset, |
| 557 | const std::vector<uint8_t>& data) |
| 558 | { |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 559 | auto item = lookup.find(session); |
| 560 | if (item == lookup.end()) |
| 561 | { |
| 562 | return false; |
| 563 | } |
| 564 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 565 | if (item->second->flags & FirmwareFlags::UpdateFlags::ipmi) |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 566 | { |
| 567 | return false; |
| 568 | } |
| 569 | |
Patrick Venture | d56097a | 2019-05-15 09:47:55 -0700 | [diff] [blame] | 570 | /* Prevent writing meta to the verification blob (it has no data handler). |
| 571 | */ |
| 572 | if (item->second->dataHandler) |
| 573 | { |
| 574 | return item->second->dataHandler->writeMeta(data); |
| 575 | } |
Patrick Venture | 699750d | 2019-05-15 09:24:09 -0700 | [diff] [blame] | 576 | |
Patrick Venture | d56097a | 2019-05-15 09:47:55 -0700 | [diff] [blame] | 577 | return false; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 578 | } |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 579 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 580 | /* |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 581 | * If this command is called on the session for the verifyBlobId, it'll |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 582 | * trigger a systemd service `verify_image.service` to attempt to verify |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 583 | * the image. |
| 584 | * |
| 585 | * For this file to have opened, the other two must be closed, which means any |
| 586 | * out-of-band transport mechanism involved is closed. |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 587 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 588 | bool FirmwareBlobHandler::commit(uint16_t session, |
| 589 | const std::vector<uint8_t>& data) |
| 590 | { |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 591 | auto item = lookup.find(session); |
| 592 | if (item == lookup.end()) |
| 593 | { |
| 594 | return false; |
| 595 | } |
| 596 | |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 597 | /* You can only commit on the verifyBlodId or updateBlobId */ |
| 598 | if (item->second->activePath != verifyBlobId && |
| 599 | item->second->activePath != updateBlobId) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 600 | { |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 601 | std::fprintf(stderr, "path: '%s' not expected for commit\n", |
| 602 | item->second->activePath.c_str()); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 606 | switch (state) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 607 | { |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 608 | case UpdateState::verificationPending: |
| 609 | /* Set state to committing. */ |
| 610 | item->second->flags |= blobs::StateFlags::committing; |
| 611 | return triggerVerification(); |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 612 | case UpdateState::verificationStarted: |
| 613 | /* Calling repeatedly has no effect within an update process. */ |
| 614 | return true; |
| 615 | case UpdateState::verificationCompleted: |
| 616 | /* Calling after the verification process has completed returns |
| 617 | * failure. */ |
| 618 | return false; |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 619 | case UpdateState::updatePending: |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 620 | item->second->flags |= blobs::StateFlags::committing; |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 621 | return triggerUpdate(); |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 622 | case UpdateState::updateStarted: |
| 623 | /* Calling repeatedly has no effect within an update process. */ |
| 624 | return true; |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 625 | default: |
| 626 | return false; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 627 | } |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 628 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 629 | |
| 630 | /* |
| 631 | * Close must be called on the firmware image before triggering |
| 632 | * verification via commit. Once the verification is complete, you can |
| 633 | * then close the hash file. |
| 634 | * |
| 635 | * If the `verify_image.service` returned success, closing the hash file |
| 636 | * will have a specific behavior depending on the update. If it's UBI, |
| 637 | * it'll perform the install. If it's static layout, it'll do nothing. The |
| 638 | * verify_image service in the static layout case is responsible for placing |
| 639 | * the file in the correct staging position. |
| 640 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 641 | bool FirmwareBlobHandler::close(uint16_t session) |
| 642 | { |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 643 | auto item = lookup.find(session); |
| 644 | if (item == lookup.end()) |
| 645 | { |
| 646 | return false; |
| 647 | } |
| 648 | |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 649 | switch (state) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 650 | { |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 651 | case UpdateState::uploadInProgress: |
| 652 | /* They are closing a data pathway (image, tarball, hash). */ |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 653 | changeState(UpdateState::verificationPending); |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 654 | |
Patrick Venture | 1999eef | 2019-07-01 11:44:09 -0700 | [diff] [blame] | 655 | /* Add verify blob ID now that we can expect it, IIF they also wrote |
| 656 | * some data. |
| 657 | */ |
| 658 | if (std::count(blobIDs.begin(), blobIDs.end(), activeImageBlobId)) |
| 659 | { |
| 660 | addBlobId(verifyBlobId); |
| 661 | } |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 662 | break; |
| 663 | case UpdateState::verificationPending: |
| 664 | /* They haven't triggered, therefore closing is uninteresting. |
| 665 | */ |
| 666 | break; |
| 667 | case UpdateState::verificationStarted: |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 668 | /* Abort without checking to see if it happened to finish. Require |
| 669 | * the caller to stat() deliberately. |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 670 | */ |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 671 | abortVerification(); |
| 672 | abortProcess(); |
| 673 | break; |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 674 | case UpdateState::verificationCompleted: |
| 675 | if (lastVerificationStatus == ActionStatus::success) |
| 676 | { |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 677 | changeState(UpdateState::updatePending); |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 678 | addBlobId(updateBlobId); |
| 679 | removeBlobId(verifyBlobId); |
| 680 | } |
| 681 | else |
| 682 | { |
Patrick Venture | 5d9fa02 | 2019-06-17 13:13:30 -0700 | [diff] [blame] | 683 | /* Verification failed, and the host-tool knows this by calling |
| 684 | * stat(), which triggered the state change to |
| 685 | * verificationCompleted. |
| 686 | * |
| 687 | * Therefore, let's abort the process at this point. |
| 688 | */ |
| 689 | abortProcess(); |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 690 | } |
| 691 | break; |
| 692 | case UpdateState::updatePending: |
| 693 | /* They haven't triggered the update, therefore this is |
| 694 | * uninteresting. */ |
| 695 | break; |
| 696 | case UpdateState::updateStarted: |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 697 | /* Abort without checking to see if it happened to finish. Require |
| 698 | * the caller to stat() deliberately. |
| 699 | */ |
| 700 | abortUpdate(); |
| 701 | abortProcess(); |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 702 | break; |
| 703 | case UpdateState::updateCompleted: |
| 704 | if (lastUpdateStatus == ActionStatus::failed) |
| 705 | { |
| 706 | /* TODO: lOG something? */ |
Patrick Venture | 4c7a420 | 2019-06-17 13:06:55 -0700 | [diff] [blame] | 707 | std::fprintf(stderr, "Update failed\n"); |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 708 | } |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 709 | |
Patrick Venture | 4c7a420 | 2019-06-17 13:06:55 -0700 | [diff] [blame] | 710 | abortProcess(); |
Patrick Venture | e95dbb6 | 2019-06-05 09:59:29 -0700 | [diff] [blame] | 711 | break; |
| 712 | default: |
| 713 | break; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Brandon Kim | a9f674c | 2019-10-18 15:18:49 -0700 | [diff] [blame] | 716 | if (!lookup.empty()) |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 717 | { |
Brandon Kim | a9f674c | 2019-10-18 15:18:49 -0700 | [diff] [blame] | 718 | if (item->second->dataHandler) |
| 719 | { |
| 720 | item->second->dataHandler->close(); |
| 721 | } |
| 722 | if (item->second->imageHandler) |
| 723 | { |
| 724 | item->second->imageHandler->close(); |
| 725 | } |
| 726 | lookup.erase(item); |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 727 | } |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 728 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 729 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 730 | |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 731 | void FirmwareBlobHandler::changeState(UpdateState next) |
| 732 | { |
| 733 | state = next; |
Patrick Venture | 6d7735d | 2019-06-21 10:03:19 -0700 | [diff] [blame] | 734 | |
| 735 | if (state == UpdateState::notYetStarted) |
| 736 | { |
| 737 | /* Going back to notyetstarted, let them trigger preparation again. */ |
| 738 | preparationTriggered = false; |
| 739 | } |
| 740 | else if (state == UpdateState::uploadInProgress) |
| 741 | { |
| 742 | /* Store this transition logic here instead of ::open() */ |
| 743 | if (!preparationTriggered) |
| 744 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 745 | auto* pack = getActionPack(); |
| 746 | if (pack) |
| 747 | { |
| 748 | pack->preparation->trigger(); |
| 749 | preparationTriggered = true; |
| 750 | } |
Patrick Venture | 6d7735d | 2019-06-21 10:03:19 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 755 | bool FirmwareBlobHandler::expire(uint16_t session) |
| 756 | { |
Patrick Venture | 92f2615 | 2020-05-26 19:47:36 -0700 | [diff] [blame] | 757 | abortProcess(); |
| 758 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 759 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * Currently, the design does not provide this with a function, however, |
| 763 | * it will likely change to support reading data back. |
| 764 | */ |
| 765 | std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session, |
| 766 | uint32_t offset, |
| 767 | uint32_t requestedSize) |
| 768 | { |
| 769 | return {}; |
| 770 | } |
| 771 | |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 772 | void FirmwareBlobHandler::abortProcess() |
| 773 | { |
| 774 | /* Closing of open files is handled from close() -- Reaching here from |
| 775 | * delete may never be supported. |
| 776 | */ |
| 777 | removeBlobId(verifyBlobId); |
| 778 | removeBlobId(updateBlobId); |
| 779 | removeBlobId(activeImageBlobId); |
| 780 | removeBlobId(activeHashBlobId); |
| 781 | |
Brandon Kim | a9f674c | 2019-10-18 15:18:49 -0700 | [diff] [blame] | 782 | for (auto item : lookup) |
| 783 | { |
| 784 | if (item.second->dataHandler) |
| 785 | { |
| 786 | item.second->dataHandler->close(); |
| 787 | } |
| 788 | if (item.second->imageHandler) |
| 789 | { |
| 790 | item.second->imageHandler->close(); |
| 791 | } |
| 792 | } |
| 793 | lookup.clear(); |
| 794 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 795 | openedFirmwareType = ""; |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 796 | changeState(UpdateState::notYetStarted); |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | void FirmwareBlobHandler::abortVerification() |
| 800 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 801 | auto* pack = getActionPack(); |
| 802 | if (pack) |
| 803 | { |
| 804 | pack->verification->abort(); |
| 805 | } |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 808 | bool FirmwareBlobHandler::triggerVerification() |
| 809 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 810 | auto* pack = getActionPack(); |
| 811 | if (!pack) |
| 812 | { |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | bool result = pack->verification->trigger(); |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 817 | if (result) |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 818 | { |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 819 | changeState(UpdateState::verificationStarted); |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 820 | } |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 821 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 822 | return result; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 825 | void FirmwareBlobHandler::abortUpdate() |
| 826 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 827 | auto* pack = getActionPack(); |
| 828 | if (pack) |
| 829 | { |
| 830 | pack->update->abort(); |
| 831 | } |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 834 | bool FirmwareBlobHandler::triggerUpdate() |
| 835 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 836 | auto* pack = getActionPack(); |
| 837 | if (!pack) |
| 838 | { |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | bool result = pack->update->trigger(); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 843 | if (result) |
| 844 | { |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 845 | changeState(UpdateState::updateStarted); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | return result; |
| 849 | } |
| 850 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 851 | } // namespace ipmi_flash |