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