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