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