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