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