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