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