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