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