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