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