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