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 | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 7 | #include <memory> |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 11 | namespace blobs |
| 12 | { |
| 13 | |
Patrick Venture | 21be45a | 2018-11-06 12:08:52 -0800 | [diff] [blame] | 14 | const std::string FirmwareBlobHandler::hashBlobID = "/flash/hash"; |
Patrick Venture | 7b9256f | 2018-11-06 15:06:04 -0800 | [diff] [blame] | 15 | const std::string FirmwareBlobHandler::activeImageBlobID = |
| 16 | "/flash/active/image"; |
| 17 | const std::string FirmwareBlobHandler::activeHashBlobID = "/flash/active/hash"; |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 18 | |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 19 | std::unique_ptr<GenericBlobInterface> |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 20 | FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 21 | const std::vector<HandlerPack>& firmwares, |
| 22 | const std::vector<DataHandlerPack>& transports) |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 23 | { |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 24 | /* There must be at least one. */ |
| 25 | if (!firmwares.size()) |
| 26 | { |
| 27 | return nullptr; |
| 28 | } |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 29 | if (!transports.size()) |
| 30 | { |
| 31 | return nullptr; |
| 32 | } |
Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 33 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 34 | std::vector<std::string> blobs; |
| 35 | for (const auto& item : firmwares) |
| 36 | { |
| 37 | blobs.push_back(item.blobName); |
| 38 | } |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 39 | blobs.push_back(hashBlobID); |
| 40 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 41 | std::uint16_t bitmask = 0; |
| 42 | for (const auto& item : transports) |
| 43 | { |
| 44 | /* TODO: can use std::accumulate() unless I'm mistaken. :D */ |
| 45 | bitmask |= item.bitmask; |
| 46 | } |
| 47 | |
| 48 | return std::make_unique<FirmwareBlobHandler>(firmwares, blobs, transports, |
| 49 | bitmask); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 52 | bool FirmwareBlobHandler::canHandleBlob(const std::string& path) |
| 53 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 54 | /* Check if the path is in our supported list (or active list). */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 55 | if (std::count(blobIDs.begin(), blobIDs.end(), path)) |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 56 | { |
| 57 | return true; |
| 58 | } |
| 59 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 62 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 63 | std::vector<std::string> FirmwareBlobHandler::getBlobIds() |
| 64 | { |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Grab the list of supported firmware. |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 67 | * |
| 68 | * If there's an open firmware session, it'll already be present in the |
| 69 | * list as "/flash/active/image", and if the hash has started, |
| 70 | * "/flash/active/hash" regardless of mechanism. This is done in the open |
| 71 | * comamnd, no extra work is required here. |
Patrick Venture | fa6c4d9 | 2018-11-02 18:34:53 -0700 | [diff] [blame] | 72 | */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 73 | return blobIDs; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 74 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 75 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 76 | bool FirmwareBlobHandler::deleteBlob(const std::string& path) |
| 77 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Per the design, this mean abort, and this will trigger whatever |
| 80 | * appropriate actions are required to abort the process. |
| 81 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 84 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 85 | bool FirmwareBlobHandler::stat(const std::string& path, struct BlobMeta* meta) |
| 86 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 87 | /* |
| 88 | * Stat on the files will return information such as what supported |
| 89 | * transport mechanisms are available. |
| 90 | * |
| 91 | * Stat on an active file or hash will return information such as the size |
| 92 | * of the data cached, and any additional pertinent information. The |
| 93 | * blob_state on the active files will return the state of the update. |
| 94 | */ |
Patrick Venture | 46637c8 | 2018-11-06 15:20:24 -0800 | [diff] [blame] | 95 | |
| 96 | /* We know we support this path because canHandle is called ahead */ |
| 97 | if (path == FirmwareBlobHandler::activeImageBlobID) |
| 98 | { |
| 99 | /* We need to return information for the image that's staged. */ |
| 100 | } |
| 101 | else if (path == FirmwareBlobHandler::activeHashBlobID) |
| 102 | { |
| 103 | /* We need to return information for the hash that's staged. */ |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | /* They are requesting information about the generic blob_id. */ |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 108 | meta->blobState = bitmask; |
Patrick Venture | 46637c8 | 2018-11-06 15:20:24 -0800 | [diff] [blame] | 109 | meta->size = 0; |
| 110 | |
| 111 | /* The generic blob_ids state is only the bits related to the transport |
| 112 | * mechanisms. */ |
| 113 | return true; |
| 114 | } |
| 115 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 118 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 119 | /* |
| 120 | * If you open /flash/image or /flash/tarball, or /flash/hash it will |
| 121 | * interpret the open flags and perform whatever actions are required for |
| 122 | * that update process. The session returned can be used immediately for |
| 123 | * sending data down, without requiring one to open the new active file. |
| 124 | * |
| 125 | * If you open the active flash image or active hash it will let you |
| 126 | * overwrite pieces, depending on the state. |
| 127 | * |
| 128 | * Once the verification process has started the active files cannot be |
| 129 | * opened. |
| 130 | * |
| 131 | * You can only have one open session at a time. Which means, you can only |
| 132 | * have one file open at a time. Trying to open the hash blob_id while you |
| 133 | * still have the flash image blob_id open will fail. Opening the flash |
| 134 | * blob_id when it is already open will fail. |
| 135 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 136 | bool FirmwareBlobHandler::open(uint16_t session, uint16_t flags, |
| 137 | const std::string& path) |
| 138 | { |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 139 | /* Check that they've opened for writing - read back not supported. */ |
| 140 | if ((flags & OpenFlags::write) == 0) |
| 141 | { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /* TODO: Is the verification process underway? */ |
| 146 | |
| 147 | /* Is there an open session already? We only allow one at a time. |
| 148 | * TODO: Temporarily using a simple boolean flag until there's a full |
| 149 | * session object to check. |
Patrick Venture | 7c8b97e | 2018-11-08 09:14:30 -0800 | [diff] [blame] | 150 | * |
| 151 | * Further on this, if there's an active session to the hash we don't allow |
| 152 | * re-opening the image, and if we have the image open, we don't allow |
| 153 | * opening the hash. This design decision may be re-evaluated, and changed |
| 154 | * to only allow one session per object type (of the two types). But, |
| 155 | * consider if the hash is open, do we want to allow writing to the image? |
| 156 | * And why would we? But, really, the point of no-return is once the |
| 157 | * verification process has begun -- which is done via commit() on the hash |
| 158 | * blob_id, we no longer want to allow updating the contents. |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 159 | */ |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 160 | if (fileOpen) |
| 161 | { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | /* There are two abstractions at play, how you get the data and how you |
| 166 | * handle that data. such that, whether the data comes from the PCI bridge |
| 167 | * or LPC bridge is not connected to whether the data goes into a static |
| 168 | * layout flash update or a UBI tarball. |
| 169 | */ |
| 170 | |
| 171 | /* Check the flags for the transport mechanism: if none match we don't |
| 172 | * support what they request. */ |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 173 | if ((flags & bitmask) == 0) |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | /* 2) there isn't, so what are they opening? */ |
| 179 | if (path == activeImageBlobID) |
| 180 | { |
| 181 | /* 2a) are they opening the active image? this can only happen if they |
| 182 | * already started one (due to canHandleBlob's behavior). */ |
| 183 | } |
| 184 | else if (path == activeHashBlobID) |
| 185 | { |
| 186 | /* 2b) are they opening the active hash? this can only happen if they |
| 187 | * already started one (due to canHandleBlob's behavior). */ |
| 188 | } |
| 189 | else if (path == hashBlobID) |
| 190 | { |
| 191 | /* 2c) are they opening the /flash/hash ? (to start the process) */ |
Patrick Venture | 7c8b97e | 2018-11-08 09:14:30 -0800 | [diff] [blame] | 192 | |
| 193 | /* Add active hash blob_id to canHandle list. */ |
| 194 | blobIDs.push_back(activeHashBlobID); |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 195 | } |
| 196 | else |
| 197 | { |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 198 | /* How are they expecting to copy this data? */ |
| 199 | auto d = std::find_if( |
| 200 | transports.begin(), transports.end(), |
| 201 | [&flags](const auto& iter) { return (iter.bitmask & flags); }); |
Patrick Venture | bf3399d | 2018-11-07 09:20:20 -0800 | [diff] [blame] | 202 | if (d == transports.end()) |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 203 | { |
Patrick Venture | bf3399d | 2018-11-07 09:20:20 -0800 | [diff] [blame] | 204 | return false; |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Patrick Venture | bf3399d | 2018-11-07 09:20:20 -0800 | [diff] [blame] | 207 | /* We found the transport handler they requested, no surprise since |
| 208 | * above we verify they selected at least one we wanted. |
| 209 | */ |
| 210 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 211 | /* 2d) are they opening the /flash/tarball ? (to start the UBI process) |
| 212 | */ |
| 213 | /* 2e) are they opening the /flash/image ? (to start the process) */ |
| 214 | /* 2...) are they opening the /flash/... ? (to start the process) */ |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 215 | |
| 216 | auto h = std::find_if( |
| 217 | handlers.begin(), handlers.end(), |
| 218 | [&path](const auto& iter) { return (iter.blobName == path); }); |
Patrick Venture | bf3399d | 2018-11-07 09:20:20 -0800 | [diff] [blame] | 219 | if (h == handlers.end()) |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 220 | { |
Patrick Venture | bf3399d | 2018-11-07 09:20:20 -0800 | [diff] [blame] | 221 | return false; |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Patrick Venture | bf3399d | 2018-11-07 09:20:20 -0800 | [diff] [blame] | 224 | /* Ok, so we found a handler that matched, so call open() */ |
| 225 | if (!h->handler->open(path)) |
| 226 | { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /* open() succeeded. */ |
| 231 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 232 | /* TODO: Actually handle storing this information. */ |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 233 | |
Patrick Venture | 7c8b97e | 2018-11-08 09:14:30 -0800 | [diff] [blame] | 234 | /* add active image blob_id to canHandle list. */ |
| 235 | blobIDs.push_back(activeImageBlobID); |
| 236 | |
Patrick Venture | 72388d7 | 2018-11-07 15:06:36 -0800 | [diff] [blame] | 237 | return true; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 242 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 243 | std::vector<uint8_t> FirmwareBlobHandler::read(uint16_t session, |
| 244 | uint32_t offset, |
| 245 | uint32_t requestedSize) |
| 246 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 247 | /* |
| 248 | * Currently, the design does not provide this with a function, however, |
| 249 | * it will likely change to support reading data back. |
| 250 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 251 | return {}; |
| 252 | } |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 253 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 254 | bool FirmwareBlobHandler::write(uint16_t session, uint32_t offset, |
| 255 | const std::vector<uint8_t>& data) |
| 256 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 257 | /* |
| 258 | * This will do whatever behavior is expected by mechanism - likely will |
| 259 | * just call the specific write handler. |
| 260 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | bool FirmwareBlobHandler::writeMeta(uint16_t session, uint32_t offset, |
| 264 | const std::vector<uint8_t>& data) |
| 265 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 266 | /* |
| 267 | * If the active session (image or hash) is over LPC, this allows |
| 268 | * configuring it. This option is only available before you start |
| 269 | * writing data for the given item (image or hash). This will return |
| 270 | * false at any other part. |
| 271 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | bool FirmwareBlobHandler::commit(uint16_t session, |
| 275 | const std::vector<uint8_t>& data) |
| 276 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 277 | /* |
| 278 | * If this command is called on the session for the hash image, it'll |
| 279 | * trigger a systemd service `verify_image.service` to attempt to verify |
| 280 | * the image. Before doing this, if the transport mechanism is not IPMI |
| 281 | * BT, it'll shut down the mechanism used for transport preventing the |
| 282 | * host from updating anything. |
| 283 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | bool FirmwareBlobHandler::close(uint16_t session) |
| 287 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 288 | /* |
| 289 | * Close must be called on the firmware image before triggering |
| 290 | * verification via commit. Once the verification is complete, you can |
| 291 | * then close the hash file. |
| 292 | * |
| 293 | * If the `verify_image.service` returned success, closing the hash file |
| 294 | * will have a specific behavior depending on the update. If it's UBI, |
| 295 | * it'll perform the install. If it's static layout, it'll do nothing. The |
| 296 | * verify_image service in the static layout case is responsible for placing |
| 297 | * the file in the correct staging position. |
| 298 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 299 | return false; |
| 300 | } |
| 301 | bool FirmwareBlobHandler::stat(uint16_t session, struct BlobMeta* meta) |
| 302 | { |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 303 | /* |
Patrick Venture | 46637c8 | 2018-11-06 15:20:24 -0800 | [diff] [blame] | 304 | * Return session specific information. |
Patrick Venture | 5397796 | 2018-11-02 18:59:35 -0700 | [diff] [blame] | 305 | */ |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | bool FirmwareBlobHandler::expire(uint16_t session) |
| 309 | { |
| 310 | return false; |
| 311 | } |
| 312 | } // namespace blobs |