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