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