Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | ac11ae9 | 2019-01-16 12:43:00 -0800 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 5 | #include "data_handler.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 6 | #include "image_handler.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 7 | #include "status.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 8 | #include "util.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 9 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 10 | #include <blobs-ipmid/blobs.hpp> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
Patrick Venture | 192d60f | 2018-11-06 11:11:59 -0800 | [diff] [blame] | 13 | #include <cstdint> |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 14 | #include <map> |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 15 | #include <memory> |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 16 | #include <string> |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 17 | #include <unordered_map> |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 18 | #include <vector> |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 19 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 20 | namespace ipmi_flash |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 21 | { |
| 22 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 23 | /** |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 24 | * Given a firmware name, provide a set of triggerable action interfaces |
| 25 | * associated with that firmware type. |
| 26 | */ |
| 27 | struct ActionPack |
| 28 | { |
| 29 | /** The name of the action pack, something like image, or tarball, or bios. |
| 30 | * The firmware blob id is parsed to pull the "filename" portion from the |
| 31 | * path, and matched against the key to a map of these. |
| 32 | */ |
| 33 | std::unique_ptr<TriggerableActionInterface> preparation; |
| 34 | std::unique_ptr<TriggerableActionInterface> verification; |
| 35 | std::unique_ptr<TriggerableActionInterface> update; |
| 36 | }; |
| 37 | |
| 38 | using ActionMap = |
| 39 | std::unordered_map<std::string, std::unique_ptr<ipmi_flash::ActionPack>>; |
| 40 | |
| 41 | /** |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 42 | * Representation of a session, includes how to read/write data. |
| 43 | */ |
| 44 | struct Session |
| 45 | { |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 46 | /** |
Patrick Venture | edd55b5 | 2018-11-15 11:05:13 -0800 | [diff] [blame] | 47 | * Built a session object. |
| 48 | * |
| 49 | * @param[in] the active path to which this corresponds. |
| 50 | */ |
| 51 | explicit Session(const std::string& path) : |
Patrick Venture | d816b23 | 2019-06-05 13:30:32 -0700 | [diff] [blame] | 52 | dataHandler(nullptr), imageHandler(nullptr), flags(0), activePath(path) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 53 | {} |
Patrick Venture | edd55b5 | 2018-11-15 11:05:13 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 56 | * Pointer to the correct Data handler interface. (nullptr on BT (or KCS)) |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 57 | */ |
| 58 | DataInterface* dataHandler; |
| 59 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Pointer to the correct image handler interface. (nullptr on hash |
| 62 | * blob_id) |
| 63 | */ |
Jason Ling | ded66d0 | 2020-10-23 14:13:03 -0700 | [diff] [blame] | 64 | ipmi_flash::ImageHandlerInterface* imageHandler; |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 65 | |
| 66 | /** The flags used to open the session. */ |
| 67 | std::uint16_t flags; |
Patrick Venture | 32ba9dd | 2018-11-09 16:22:53 -0800 | [diff] [blame] | 68 | |
Patrick Venture | edd55b5 | 2018-11-15 11:05:13 -0800 | [diff] [blame] | 69 | /** The active path. */ |
| 70 | std::string activePath; |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 74 | * Register only one firmware blob handler that will manage all sessions. |
| 75 | */ |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 76 | class FirmwareBlobHandler : public blobs::GenericBlobInterface |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 77 | { |
| 78 | public: |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 79 | /** The state of the firmware update process. */ |
Patrick Venture | 88bc26e | 2019-05-15 12:02:05 -0700 | [diff] [blame] | 80 | enum class UpdateState |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 81 | { |
| 82 | /** The initial state. */ |
| 83 | notYetStarted = 0, |
Patrick Venture | 12233c5 | 2019-05-16 09:26:59 -0700 | [diff] [blame] | 84 | /** The BMC is expecting to receive bytes. */ |
| 85 | uploadInProgress, |
| 86 | /** The BMC is ready for verification or more bytes. */ |
| 87 | verificationPending, |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 88 | /** The verification process has started, no more writes allowed. */ |
Patrick Venture | 12233c5 | 2019-05-16 09:26:59 -0700 | [diff] [blame] | 89 | verificationStarted, |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 90 | /** The verification process has completed. */ |
Patrick Venture | 12233c5 | 2019-05-16 09:26:59 -0700 | [diff] [blame] | 91 | verificationCompleted, |
Patrick Venture | 02922e4 | 2019-05-22 09:49:31 -0700 | [diff] [blame] | 92 | /** The update process is pending. */ |
| 93 | updatePending, |
| 94 | /** The update process has started. */ |
| 95 | updateStarted, |
| 96 | /** The update has completed (optional state to reach) */ |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 97 | updateCompleted, |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Create a FirmwareBlobHandler. |
| 102 | * |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 103 | * @param[in] firmwares - list of firmware blob_ids to support. |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 104 | * @param[in] transports - list of transports to support. |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 105 | * @param[in] verification - pointer to object for triggering verification |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 106 | * @param[in] update - point to object for triggering the update |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 107 | */ |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 108 | static std::unique_ptr<blobs::GenericBlobInterface> |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame] | 109 | CreateFirmwareBlobHandler(std::vector<HandlerPack>&& firmwares, |
| 110 | std::vector<DataHandlerPack>&& transports, |
| 111 | ActionMap&& actionPacks); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 112 | |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Create a FirmwareBlobHandler. |
| 115 | * |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 116 | * @param[in] firmwares - list of firmware types and their handlers |
| 117 | * @param[in] blobs - list of blobs_ids to support |
| 118 | * @param[in] transports - list of transport types and their handlers |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 119 | * @param[in] verification - pointer to object for triggering verification |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 120 | * @param[in] update - point to object for triggering the update |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 121 | */ |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 122 | FirmwareBlobHandler(std::vector<HandlerPack>&& firmwares, |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 123 | const std::vector<std::string>& blobs, |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame] | 124 | std::vector<DataHandlerPack>&& transports, |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 125 | ActionMap&& actionPacks) : |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 126 | handlers(std::move(firmwares)), blobIDs(blobs), |
| 127 | transports(std::move(transports)), activeImage(activeImageBlobId), |
| 128 | activeHash(activeHashBlobId), verifyImage(verifyBlobId), |
| 129 | updateImage(updateBlobId), lookup(), state(UpdateState::notYetStarted), |
| 130 | actionPacks(std::move(actionPacks)) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 131 | {} |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 132 | ~FirmwareBlobHandler() = default; |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 133 | FirmwareBlobHandler(const FirmwareBlobHandler&) = delete; |
| 134 | FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 135 | FirmwareBlobHandler(FirmwareBlobHandler&&) = default; |
| 136 | FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default; |
| 137 | |
| 138 | bool canHandleBlob(const std::string& path) override; |
| 139 | std::vector<std::string> getBlobIds() override; |
| 140 | bool deleteBlob(const std::string& path) override; |
Patrick Venture | e312f39 | 2019-06-04 07:44:37 -0700 | [diff] [blame] | 141 | bool stat(const std::string& path, blobs::BlobMeta* meta) override; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 142 | bool open(uint16_t session, uint16_t flags, |
| 143 | const std::string& path) override; |
| 144 | std::vector<uint8_t> read(uint16_t session, uint32_t offset, |
| 145 | uint32_t requestedSize) override; |
| 146 | bool write(uint16_t session, uint32_t offset, |
| 147 | const std::vector<uint8_t>& data) override; |
| 148 | bool writeMeta(uint16_t session, uint32_t offset, |
| 149 | const std::vector<uint8_t>& data) override; |
| 150 | bool commit(uint16_t session, const std::vector<uint8_t>& data) override; |
| 151 | bool close(uint16_t session) override; |
Patrick Venture | e312f39 | 2019-06-04 07:44:37 -0700 | [diff] [blame] | 152 | bool stat(uint16_t session, blobs::BlobMeta* meta) override; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 153 | bool expire(uint16_t session) override; |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 154 | |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 155 | void abortProcess(); |
| 156 | |
| 157 | void abortVerification(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 158 | bool triggerVerification(); |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 159 | void abortUpdate(); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 160 | bool triggerUpdate(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 161 | |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 162 | /** Allow grabbing the current state. */ |
| 163 | UpdateState getCurrentState() const |
| 164 | { |
| 165 | return state; |
| 166 | }; |
| 167 | |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 168 | /** Provide for any state change triggers in convenience handler. */ |
| 169 | void changeState(UpdateState next); |
| 170 | |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 171 | private: |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Given the current session type, grab the ActionPack (likely will be |
| 174 | * worked into the Session for lookup). |
| 175 | */ |
| 176 | ActionPack* getActionPack() |
| 177 | { |
| 178 | if (openedFirmwareType.empty()) |
| 179 | { |
| 180 | /* No firmware type has been opened, but we're triggering |
| 181 | * verification, or preparing. This can happen if they open the hash |
| 182 | * before the image, which is possible. |
| 183 | */ |
| 184 | return nullptr; |
| 185 | } |
| 186 | |
| 187 | /* TODO: Once the actionPacks and supportedFirmwares are merged this'll |
| 188 | * be less dangerous |
| 189 | */ |
| 190 | return actionPacks[openedFirmwareType].get(); |
| 191 | } |
| 192 | |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 193 | void addBlobId(const std::string& blob) |
| 194 | { |
Patrick Williams | a942346 | 2023-10-20 11:19:36 -0500 | [diff] [blame] | 195 | auto blobIdMatch = std::find_if( |
| 196 | blobIDs.begin(), blobIDs.end(), |
| 197 | [&blob](const std::string& iter) { return (iter == blob); }); |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 198 | if (blobIdMatch == blobIDs.end()) |
| 199 | { |
| 200 | blobIDs.push_back(blob); |
| 201 | } |
| 202 | } |
| 203 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 204 | void removeBlobId(const std::string& blob) |
| 205 | { |
| 206 | blobIDs.erase(std::remove(blobIDs.begin(), blobIDs.end(), blob), |
| 207 | blobIDs.end()); |
| 208 | } |
| 209 | |
Brandon Kim | 8fc6087 | 2019-10-18 15:15:50 -0700 | [diff] [blame] | 210 | inline bool fileOpen() |
| 211 | { |
| 212 | return !lookup.empty(); |
| 213 | } |
| 214 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 215 | ActionStatus getVerifyStatus(); |
| 216 | ActionStatus getActionStatus(); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 217 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 218 | /** List of handlers by type. */ |
| 219 | std::vector<HandlerPack> handlers; |
| 220 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 221 | /** Active list of blobIDs. */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 222 | std::vector<std::string> blobIDs; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 223 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 224 | /** List of handlers by transport type. */ |
| 225 | std::vector<DataHandlerPack> transports; |
| 226 | |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 227 | /** Active image session. */ |
| 228 | Session activeImage; |
| 229 | |
| 230 | /** Active hash session. */ |
| 231 | Session activeHash; |
| 232 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 233 | /** Session for verification. */ |
| 234 | Session verifyImage; |
| 235 | |
Patrick Venture | 26e241d | 2019-05-20 18:39:01 -0700 | [diff] [blame] | 236 | /** Session for update. */ |
| 237 | Session updateImage; |
| 238 | |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 239 | /** A quick method for looking up a session's mechanisms and details. */ |
| 240 | std::map<std::uint16_t, Session*> lookup; |
| 241 | |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 242 | /** The firmware update state. */ |
| 243 | UpdateState state; |
| 244 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 245 | /** Track what firmware blobid they opened to start this sequence. */ |
| 246 | std::string openedFirmwareType; |
| 247 | |
Patrick Venture | 6d7735d | 2019-06-21 10:03:19 -0700 | [diff] [blame] | 248 | /* preparation is triggered once we go into uploadInProgress(), but only |
| 249 | * once per full cycle, going back to notYetStarted resets this. |
| 250 | */ |
| 251 | bool preparationTriggered = false; |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 252 | ActionMap actionPacks; |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 253 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 254 | ActionStatus lastVerificationStatus = ActionStatus::unknown; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 255 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 256 | ActionStatus lastUpdateStatus = ActionStatus::unknown; |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 257 | |
| 258 | /** Portion of "flags" argument to open() which specifies the desired |
| 259 | * transport type |
| 260 | */ |
| 261 | static constexpr std::uint16_t transportMask = 0xff00; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 264 | } // namespace ipmi_flash |