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 | { |
| 183 | /* |
| 184 | * Return session specific information. |
| 185 | */ |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | /* |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 190 | * If you open /flash/image or /flash/tarball, or /flash/hash it will |
| 191 | * interpret the open flags and perform whatever actions are required for |
| 192 | * that update process. The session returned can be used immediately for |
| 193 | * sending data down, without requiring one to open the new active file. |
| 194 | * |
| 195 | * If you open the active flash image or active hash it will let you |
| 196 | * overwrite pieces, depending on the state. |
| 197 | * |
| 198 | * Once the verification process has started the active files cannot be |
| 199 | * opened. |
| 200 | * |
| 201 | * You can only have one open session at a time. Which means, you can only |
| 202 | * have one file open at a time. Trying to open the hash blob_id while you |
| 203 | * still have the flash image blob_id open will fail. Opening the flash |
| 204 | * blob_id when it is already open will fail. |
| 205 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 206 | bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags, |
| 207 | const std::string& path) |
| 208 | { |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 209 | /* Check that they've opened for writing - read back not currently |
| 210 | * supported. |
| 211 | */ |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 212 | if ((flags & OpenFlags::write) == 0) |
| 213 | { |
| 214 | return false; |
| 215 | } |
| 216 | |
Patrick Venture | b1b68fc | 2018-11-09 09:37:04 -0800 | [diff] [blame] | 217 | /* Is the verification process underway? */ |
| 218 | if (state == UpdateState::verificationStarted) |
| 219 | { |
| 220 | return false; |
| 221 | } |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 222 | |
| 223 | /* 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] | 224 | * |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 225 | * TODO: Temporarily using a simple boolean flag until there's a full |
| 226 | * session object to check. |
Patrick Venture | 7c8b97e | 2018-11-08 09:14:30 -0800 | [diff] [blame] | 227 | * |
| 228 | * Further on this, if there's an active session to the hash we don't allow |
| 229 | * re-opening the image, and if we have the image open, we don't allow |
| 230 | * opening the hash. This design decision may be re-evaluated, and changed |
| 231 | * to only allow one session per object type (of the two types). But, |
| 232 | * consider if the hash is open, do we want to allow writing to the image? |
| 233 | * And why would we? But, really, the point of no-return is once the |
| 234 | * verification process has begun -- which is done via commit() on the hash |
| 235 | * blob_id, we no longer want to allow updating the contents. |
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 | if (fileOpen) |
| 238 | { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | /* There are two abstractions at play, how you get the data and how you |
| 243 | * handle that data. such that, whether the data comes from the PCI bridge |
| 244 | * or LPC bridge is not connected to whether the data goes into a static |
| 245 | * layout flash update or a UBI tarball. |
| 246 | */ |
| 247 | |
| 248 | /* 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] | 249 | * support what they request. |
| 250 | */ |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 251 | if ((flags & bitmask) == 0) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 252 | { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | /* 2) there isn't, so what are they opening? */ |
| 257 | if (path == activeImageBlobID) |
| 258 | { |
| 259 | /* 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] | 260 | * already started one (due to canHandleBlob's behavior). |
| 261 | */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 262 | return false; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 263 | } |
| 264 | else if (path == activeHashBlobID) |
| 265 | { |
| 266 | /* 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] | 267 | * already started one (due to canHandleBlob's behavior). |
| 268 | */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 269 | return false; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 270 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 271 | |
| 272 | /* How are they expecting to copy this data? */ |
| 273 | auto d = std::find_if( |
| 274 | transports.begin(), transports.end(), |
| 275 | [&flags](const auto& iter) { return (iter.bitmask & flags); }); |
| 276 | if (d == transports.end()) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 277 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
| 281 | /* We found the transport handler they requested, no surprise since |
| 282 | * above we verify they selected at least one we wanted. |
| 283 | */ |
| 284 | Session* curr; |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 285 | const std::string* active; |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 286 | |
| 287 | if (path == hashBlobID) |
| 288 | { |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 289 | /* 2c) are they opening the /flash/hash ? (to start the process) */ |
Patrick Venture | 21c62c1 | 2018-11-09 17:46:58 -0800 | [diff] [blame] | 290 | curr = &activeHash; |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 291 | active = &activeHashBlobID; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 292 | } |
| 293 | else |
| 294 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 295 | curr = &activeImage; |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 296 | active = &activeImageBlobID; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Patrick Venture | 6e307a7 | 2018-11-09 18:21:17 -0800 | [diff] [blame] | 299 | /* Elsewhere I do this check by checking "if ::ipmi" because that's the |
| 300 | * only non-external data pathway -- but this is just a more generic |
| 301 | * approach to that. |
| 302 | */ |
| 303 | if (d->handler) |
| 304 | { |
| 305 | /* If the data handler open call fails, open fails. */ |
| 306 | if (!d->handler->open()) |
| 307 | { |
| 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 312 | /* 2d) are they opening the /flash/tarball ? (to start the UBI process) |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 313 | * 2e) are they opening the /flash/image ? (to start the process) |
| 314 | * 2...) are they opening the /flash/... ? (to start the process) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 315 | */ |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 316 | auto h = std::find_if( |
| 317 | handlers.begin(), handlers.end(), |
| 318 | [&path](const auto& iter) { return (iter.blobName == path); }); |
| 319 | if (h == handlers.end()) |
| 320 | { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | /* Ok, so we found a handler that matched, so call open() */ |
| 325 | if (!h->handler->open(path)) |
| 326 | { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | curr->flags = flags; |
| 331 | curr->dataHandler = d->handler; |
| 332 | curr->imageHandler = h->handler; |
Patrick Venture | cd31022 | 2018-11-09 18:47:13 -0800 | [diff] [blame] | 333 | curr->state = Session::State::open; |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 334 | |
| 335 | lookup[session] = curr; |
| 336 | |
Patrick Venture | db253bf | 2018-11-09 19:36:03 -0800 | [diff] [blame] | 337 | blobIDs.push_back(*active); |
| 338 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 339 | fileOpen = true; |
| 340 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 341 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 342 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 343 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 344 | /** |
| 345 | * The write command really just grabs the data from wherever it is and sends it |
| 346 | * to the image handler. It's the image handler's responsibility to deal with |
| 347 | * the data provided. |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 348 | * |
| 349 | * This receives a session from the blob manager, therefore it is always called |
| 350 | * between open() and close(). |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 351 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 352 | bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset, |
| 353 | const std::vector<uint8_t>& data) |
| 354 | { |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 355 | auto item = lookup.find(session); |
| 356 | if (item == lookup.end()) |
| 357 | { |
| 358 | return false; |
| 359 | } |
| 360 | |
Patrick Venture | b1b68fc | 2018-11-09 09:37:04 -0800 | [diff] [blame] | 361 | /* Prevent writing during verification. */ |
| 362 | if (state == UpdateState::verificationStarted) |
| 363 | { |
| 364 | return false; |
| 365 | } |
| 366 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 367 | std::vector<std::uint8_t> bytes; |
| 368 | |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 369 | if (item->second->flags & UpdateFlags::ipmi) |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 370 | { |
| 371 | bytes = data; |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | /* little endian required per design, and so on, but TODO: do endianness |
| 376 | * with boost. |
| 377 | */ |
| 378 | struct ExtChunkHdr header; |
| 379 | |
| 380 | if (data.size() != sizeof(header)) |
| 381 | { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | std::memcpy(&header, data.data(), data.size()); |
| 386 | bytes = item->second->dataHandler->copyFrom(header.length); |
| 387 | } |
| 388 | |
| 389 | return item->second->imageHandler->write(offset, bytes); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 390 | } |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 391 | |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 392 | /* |
| 393 | * If the active session (image or hash) is over LPC, this allows |
| 394 | * configuring it. This option is only available before you start |
| 395 | * writing data for the given item (image or hash). This will return |
| 396 | * false at any other part. -- the lpc handler portion will know to return |
| 397 | * false. |
| 398 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 399 | bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset, |
| 400 | const std::vector<uint8_t>& data) |
| 401 | { |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 402 | auto item = lookup.find(session); |
| 403 | if (item == lookup.end()) |
| 404 | { |
| 405 | return false; |
| 406 | } |
| 407 | |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 408 | if (item->second->flags & UpdateFlags::ipmi) |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 409 | { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return item->second->dataHandler->write(data); |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 414 | } |
Patrick Venture | 8c53533 | 2018-11-08 15:58:00 -0800 | [diff] [blame] | 415 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 416 | /* |
| 417 | * If this command is called on the session for the hash image, it'll |
| 418 | * trigger a systemd service `verify_image.service` to attempt to verify |
| 419 | * the image. Before doing this, if the transport mechanism is not IPMI |
| 420 | * BT, it'll shut down the mechanism used for transport preventing the |
| 421 | * host from updating anything. |
| 422 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 423 | bool FirmwareBlobHandler::commit(uint16_t session, |
| 424 | const std::vector<uint8_t>& data) |
| 425 | { |
| 426 | return false; |
| 427 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Close must be called on the firmware image before triggering |
| 431 | * verification via commit. Once the verification is complete, you can |
| 432 | * then close the hash file. |
| 433 | * |
| 434 | * If the `verify_image.service` returned success, closing the hash file |
| 435 | * will have a specific behavior depending on the update. If it's UBI, |
| 436 | * it'll perform the install. If it's static layout, it'll do nothing. The |
| 437 | * verify_image service in the static layout case is responsible for placing |
| 438 | * the file in the correct staging position. |
| 439 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 440 | bool FirmwareBlobHandler::close(uint16_t session) |
| 441 | { |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 442 | auto item = lookup.find(session); |
| 443 | if (item == lookup.end()) |
| 444 | { |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | if (item->second->dataHandler) |
| 449 | { |
| 450 | item->second->dataHandler->close(); |
| 451 | } |
| 452 | item->second->imageHandler->close(); |
| 453 | item->second->state = Session::State::closed; |
| 454 | /* Do not delete the active blob_id from the list of blob_ids, because that |
| 455 | * blob_id indicates there is data stored. Delete will destroy it. |
| 456 | */ |
| 457 | |
| 458 | lookup.erase(item); |
| 459 | |
Patrick Venture | 991910a | 2018-11-09 19:43:48 -0800 | [diff] [blame] | 460 | fileOpen = false; |
| 461 | |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 462 | /* TODO: implement other aspects of closing out a session, such as changing |
| 463 | * global firmware state. |
| 464 | */ |
| 465 | return true; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 466 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 467 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 468 | bool FirmwareBlobHandler::expire(uint16_t session) |
| 469 | { |
| 470 | return false; |
| 471 | } |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | * Currently, the design does not provide this with a function, however, |
| 475 | * it will likely change to support reading data back. |
| 476 | */ |
| 477 | std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session, |
| 478 | uint32_t offset, |
| 479 | uint32_t requestedSize) |
| 480 | { |
| 481 | return {}; |
| 482 | } |
| 483 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 484 | } // namespace blobs |