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 | { |
| 171 | /* We need to return information for the verify state -- did they call |
| 172 | * commit() did things start? |
| 173 | */ |
| 174 | } |
| 175 | else if (path == activeImageBlobID) |
Patrick Venture | 46637c8 | 2018-11-06 15:20:24 -0800 | [diff] [blame] | 176 | { |
| 177 | /* We need to return information for the image that's staged. */ |
| 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 | { |
| 181 | /* We need to return information for the hash that's staged. */ |
| 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; |
| 219 | /* The size here refers to the size of the file -- of something analagous. |
| 220 | */ |
| 221 | meta->size = item->second->imageHandler->getSize(); |
| 222 | |
| 223 | /* The metadata blob returned comes from the data handler... it's used for |
| 224 | * instance, in P2A bridging to get required information about the mapping, |
| 225 | * and is the "opposite" of the lpc writemeta requirement. |
| 226 | */ |
| 227 | meta->metadata.clear(); |
| 228 | if (item->second->dataHandler) |
| 229 | { |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 230 | auto bytes = item->second->dataHandler->readMeta(); |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 231 | meta->metadata.insert(meta->metadata.begin(), bytes.begin(), |
| 232 | bytes.end()); |
| 233 | } |
| 234 | |
| 235 | /* TODO: During things like verification, etc, we can report the state as |
| 236 | * committed, etc, so we'll need to do that. |
| 237 | */ |
| 238 | |
| 239 | return true; |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 243 | * If you open /flash/image or /flash/tarball, or /flash/hash it will |
| 244 | * interpret the open flags and perform whatever actions are required for |
| 245 | * that update process. The session returned can be used immediately for |
| 246 | * sending data down, without requiring one to open the new active file. |
| 247 | * |
| 248 | * If you open the active flash image or active hash it will let you |
| 249 | * overwrite pieces, depending on the state. |
| 250 | * |
| 251 | * Once the verification process has started the active files cannot be |
| 252 | * opened. |
| 253 | * |
| 254 | * You can only have one open session at a time. Which means, you can only |
| 255 | * have one file open at a time. Trying to open the hash blob_id while you |
| 256 | * still have the flash image blob_id open will fail. Opening the flash |
| 257 | * blob_id when it is already open will fail. |
| 258 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 259 | bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags, |
| 260 | const std::string& path) |
| 261 | { |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 262 | /* Check that they've opened for writing - read back not currently |
| 263 | * supported. |
| 264 | */ |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 265 | if ((flags & OpenFlags::write) == 0) |
| 266 | { |
| 267 | return false; |
| 268 | } |
| 269 | |
Patrick Venture | b1b68fc | 2018-11-09 09:37:04 -0800 | [diff] [blame] | 270 | /* Is the verification process underway? */ |
| 271 | if (state == UpdateState::verificationStarted) |
| 272 | { |
| 273 | return false; |
| 274 | } |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 275 | |
| 276 | /* 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] | 277 | * |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 278 | * TODO: Temporarily using a simple boolean flag until there's a full |
| 279 | * session object to check. |
Patrick Venture | 7c8b97e | 2018-11-08 09:14:30 -0800 | [diff] [blame] | 280 | * |
| 281 | * Further on this, if there's an active session to the hash we don't allow |
| 282 | * re-opening the image, and if we have the image open, we don't allow |
| 283 | * opening the hash. This design decision may be re-evaluated, and changed |
| 284 | * to only allow one session per object type (of the two types). But, |
| 285 | * consider if the hash is open, do we want to allow writing to the image? |
| 286 | * And why would we? But, really, the point of no-return is once the |
| 287 | * verification process has begun -- which is done via commit() on the hash |
| 288 | * blob_id, we no longer want to allow updating the contents. |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 289 | */ |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 290 | if (fileOpen) |
| 291 | { |
| 292 | return false; |
| 293 | } |
| 294 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 295 | /* Handle opening the verifyBlobId --> we know the image and hash aren't |
| 296 | * open because of the fileOpen check. |
| 297 | * |
| 298 | * The file must be opened for writing, but no transport mechanism specified |
| 299 | * since it's irrelevant. |
| 300 | */ |
| 301 | if (path == verifyBlobID) |
| 302 | { |
| 303 | /* In this case, there's no image handler to use, or data handler, |
| 304 | * simply set up a session. |
| 305 | */ |
| 306 | verifyImage.flags = flags; |
| 307 | verifyImage.state = Session::State::open; |
| 308 | |
| 309 | lookup[session] = &verifyImage; |
| 310 | |
| 311 | fileOpen = true; |
| 312 | |
| 313 | return true; |
| 314 | } |
| 315 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 316 | /* There are two abstractions at play, how you get the data and how you |
| 317 | * handle that data. such that, whether the data comes from the PCI bridge |
| 318 | * or LPC bridge is not connected to whether the data goes into a static |
| 319 | * layout flash update or a UBI tarball. |
| 320 | */ |
| 321 | |
| 322 | /* 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] | 323 | * support what they request. |
| 324 | */ |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 325 | if ((flags & bitmask) == 0) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 326 | { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | /* 2) there isn't, so what are they opening? */ |
| 331 | if (path == activeImageBlobID) |
| 332 | { |
| 333 | /* 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] | 334 | * already started one (due to canHandleBlob's behavior). |
| 335 | */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 336 | return false; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 337 | } |
| 338 | else if (path == activeHashBlobID) |
| 339 | { |
| 340 | /* 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] | 341 | * already started one (due to canHandleBlob's behavior). |
| 342 | */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 343 | return false; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 344 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 345 | |
| 346 | /* How are they expecting to copy this data? */ |
| 347 | auto d = std::find_if( |
| 348 | transports.begin(), transports.end(), |
| 349 | [&flags](const auto& iter) { return (iter.bitmask & flags); }); |
| 350 | if (d == transports.end()) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 351 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 352 | return false; |
| 353 | } |
| 354 | |
| 355 | /* We found the transport handler they requested, no surprise since |
| 356 | * above we verify they selected at least one we wanted. |
| 357 | */ |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 358 | |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 359 | /* Elsewhere I do this check by checking "if ::ipmi" because that's the |
| 360 | * only non-external data pathway -- but this is just a more generic |
| 361 | * approach to that. |
| 362 | */ |
| 363 | if (d->handler) |
| 364 | { |
| 365 | /* If the data handler open call fails, open fails. */ |
| 366 | if (!d->handler->open()) |
| 367 | { |
| 368 | return false; |
| 369 | } |
| 370 | } |
| 371 | |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 372 | /* Do we have a file handler for the type of file they're opening. |
| 373 | * Note: This should only fail if something is somehow crazy wrong. |
| 374 | * Since the canHandle() said yes, and that's tied into the list of explicit |
| 375 | * firmware handers (and file handlers, like this'll know where to write the |
| 376 | * tarball, etc). |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 377 | */ |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 378 | auto h = std::find_if( |
| 379 | handlers.begin(), handlers.end(), |
| 380 | [&path](const auto& iter) { return (iter.blobName == path); }); |
| 381 | if (h == handlers.end()) |
| 382 | { |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | /* Ok, so we found a handler that matched, so call open() */ |
| 387 | if (!h->handler->open(path)) |
| 388 | { |
| 389 | return false; |
| 390 | } |
| 391 | |
Patrick Venture | 70e30bf | 2019-01-17 10:29:28 -0800 | [diff] [blame] | 392 | Session* curr; |
| 393 | const std::string* active; |
| 394 | |
| 395 | if (path == hashBlobID) |
| 396 | { |
| 397 | /* 2c) are they opening the /flash/hash ? (to start the process) */ |
| 398 | curr = &activeHash; |
| 399 | active = &activeHashBlobID; |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | curr = &activeImage; |
| 404 | active = &activeImageBlobID; |
| 405 | } |
| 406 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 407 | curr->flags = flags; |
| 408 | curr->dataHandler = d->handler; |
| 409 | curr->imageHandler = h->handler; |
Patrick Venture | cd31022 | 2018-11-09 18:47:13 -0800 | [diff] [blame] | 410 | curr->state = Session::State::open; |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 411 | |
| 412 | lookup[session] = curr; |
| 413 | |
Patrick Venture | daf4707 | 2019-05-10 15:21:55 -0700 | [diff] [blame^] | 414 | /* This may be them re-opening a blob, so let's only push it onto the list |
| 415 | * when appropriate. |
| 416 | */ |
| 417 | auto blobIdMatch = |
| 418 | std::find_if(blobIDs.begin(), blobIDs.end(), |
| 419 | [active](const auto& iter) { return (iter == *active); }); |
| 420 | if (blobIdMatch == blobIDs.end()) |
| 421 | { |
| 422 | blobIDs.push_back(*active); |
| 423 | } |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 424 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 425 | fileOpen = true; |
| 426 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 427 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 428 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 429 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 430 | /** |
| 431 | * The write command really just grabs the data from wherever it is and sends it |
| 432 | * to the image handler. It's the image handler's responsibility to deal with |
| 433 | * the data provided. |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 434 | * |
| 435 | * This receives a session from the blob manager, therefore it is always called |
| 436 | * between open() and close(). |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 437 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 438 | bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset, |
| 439 | const std::vector<uint8_t>& data) |
| 440 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 441 | auto item = lookup.find(session); |
| 442 | if (item == lookup.end()) |
| 443 | { |
| 444 | return false; |
| 445 | } |
| 446 | |
Patrick Venture | b1b68fc | 2018-11-09 09:37:04 -0800 | [diff] [blame] | 447 | /* Prevent writing during verification. */ |
| 448 | if (state == UpdateState::verificationStarted) |
| 449 | { |
| 450 | return false; |
| 451 | } |
| 452 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 453 | std::vector<std::uint8_t> bytes; |
| 454 | |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 455 | if (item->second->flags & UpdateFlags::ipmi) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 456 | { |
| 457 | bytes = data; |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | /* little endian required per design, and so on, but TODO: do endianness |
| 462 | * with boost. |
| 463 | */ |
| 464 | struct ExtChunkHdr header; |
| 465 | |
| 466 | if (data.size() != sizeof(header)) |
| 467 | { |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | std::memcpy(&header, data.data(), data.size()); |
| 472 | bytes = item->second->dataHandler->copyFrom(header.length); |
| 473 | } |
| 474 | |
| 475 | return item->second->imageHandler->write(offset, bytes); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 476 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 477 | |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 478 | /* |
| 479 | * If the active session (image or hash) is over LPC, this allows |
| 480 | * configuring it. This option is only available before you start |
| 481 | * writing data for the given item (image or hash). This will return |
| 482 | * false at any other part. -- the lpc handler portion will know to return |
| 483 | * false. |
| 484 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 485 | bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset, |
| 486 | const std::vector<uint8_t>& data) |
| 487 | { |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 488 | auto item = lookup.find(session); |
| 489 | if (item == lookup.end()) |
| 490 | { |
| 491 | return false; |
| 492 | } |
| 493 | |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 494 | if (item->second->flags & UpdateFlags::ipmi) |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 495 | { |
| 496 | return false; |
| 497 | } |
| 498 | |
Patrick Venture | 7430464 | 2019-01-17 09:31:04 -0800 | [diff] [blame] | 499 | return item->second->dataHandler->writeMeta(data); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 500 | } |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 501 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 502 | /* |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 503 | * 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] | 504 | * trigger a systemd service `verify_image.service` to attempt to verify |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 505 | * the image. |
| 506 | * |
| 507 | * For this file to have opened, the other two must be closed, which means any |
| 508 | * out-of-band transport mechanism involved is closed. |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 509 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 510 | bool FirmwareBlobHandler::commit(uint16_t session, |
| 511 | const std::vector<uint8_t>& data) |
| 512 | { |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 513 | auto item = lookup.find(session); |
| 514 | if (item == lookup.end()) |
| 515 | { |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | /* You can only commit on the verifyBlodId */ |
| 520 | if (item->second->activePath != verifyBlobID) |
| 521 | { |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | /* Can only be called once per verification. */ |
| 526 | if (state == UpdateState::verificationStarted) |
| 527 | { |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | /* Set state to committing. */ |
| 532 | item->second->flags |= StateFlags::committing; |
| 533 | |
| 534 | return triggerVerification(); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 535 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Close must be called on the firmware image before triggering |
| 539 | * verification via commit. Once the verification is complete, you can |
| 540 | * then close the hash file. |
| 541 | * |
| 542 | * If the `verify_image.service` returned success, closing the hash file |
| 543 | * will have a specific behavior depending on the update. If it's UBI, |
| 544 | * it'll perform the install. If it's static layout, it'll do nothing. The |
| 545 | * verify_image service in the static layout case is responsible for placing |
| 546 | * the file in the correct staging position. |
| 547 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 548 | bool FirmwareBlobHandler::close(uint16_t session) |
| 549 | { |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 550 | auto item = lookup.find(session); |
| 551 | if (item == lookup.end()) |
| 552 | { |
| 553 | return false; |
| 554 | } |
| 555 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 556 | /* Are you closing the verify blob? */ |
| 557 | if (item->second->activePath == verifyBlobID) |
| 558 | { |
| 559 | /* If they close this blob before verification finishes, that's an |
| 560 | * abort. |
| 561 | * TODO: implement this, for now just let them close the file. |
| 562 | */ |
| 563 | if (state == UpdateState::verificationStarted) |
| 564 | { |
| 565 | return false; |
| 566 | } |
| 567 | } |
| 568 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 569 | if (item->second->dataHandler) |
| 570 | { |
| 571 | item->second->dataHandler->close(); |
| 572 | } |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 573 | if (item->second->imageHandler) |
| 574 | { |
| 575 | item->second->imageHandler->close(); |
| 576 | } |
| 577 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 578 | item->second->state = Session::State::closed; |
| 579 | /* Do not delete the active blob_id from the list of blob_ids, because that |
| 580 | * blob_id indicates there is data stored. Delete will destroy it. |
| 581 | */ |
| 582 | |
| 583 | lookup.erase(item); |
| 584 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 585 | fileOpen = false; |
| 586 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 587 | /* TODO: implement other aspects of closing out a session, such as changing |
| 588 | * global firmware state. |
| 589 | */ |
| 590 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 591 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 592 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 593 | bool FirmwareBlobHandler::expire(uint16_t session) |
| 594 | { |
| 595 | return false; |
| 596 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 597 | |
| 598 | /* |
| 599 | * Currently, the design does not provide this with a function, however, |
| 600 | * it will likely change to support reading data back. |
| 601 | */ |
| 602 | std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session, |
| 603 | uint32_t offset, |
| 604 | uint32_t requestedSize) |
| 605 | { |
| 606 | return {}; |
| 607 | } |
| 608 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 609 | bool FirmwareBlobHandler::triggerVerification() |
| 610 | { |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 611 | auto method = bus.new_method_call(systemdService, systemdRoot, |
| 612 | systemdInterface, "StartUnit"); |
| 613 | method.append(verifyTarget); |
| 614 | method.append("replace"); |
| 615 | |
| 616 | try |
| 617 | { |
| 618 | bus.call_noreply(method); |
Patrick Venture | 453f06a | 2019-01-17 10:02:12 -0800 | [diff] [blame] | 619 | state = UpdateState::verificationStarted; |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 620 | } |
| 621 | catch (const sdbusplus::exception::SdBusError& ex) |
| 622 | { |
| 623 | /* TODO: Once logging supports unit-tests, add a log message to test |
| 624 | * this failure. |
| 625 | */ |
| 626 | return false; |
| 627 | } |
| 628 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 629 | return true; |
| 630 | } |
| 631 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 632 | } // namespace blobs |