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