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