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 | */ |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 64 | 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> |
| 109 | CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 110 | std::vector<HandlerPack>&& firmwares, |
Patrick Venture | 9efef5d | 2019-06-19 08:45:44 -0700 | [diff] [blame] | 111 | const std::vector<DataHandlerPack>& transports, |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 112 | ActionMap&& actionPacks); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 113 | |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Create a FirmwareBlobHandler. |
| 116 | * |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 117 | * @param[in] firmwares - list of firmware types and their handlers |
| 118 | * @param[in] blobs - list of blobs_ids to support |
| 119 | * @param[in] transports - list of transport types and their handlers |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 120 | * @param[in] verification - pointer to object for triggering verification |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 121 | * @param[in] update - point to object for triggering the update |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 122 | */ |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 123 | FirmwareBlobHandler(std::vector<HandlerPack>&& firmwares, |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 124 | const std::vector<std::string>& blobs, |
| 125 | const std::vector<DataHandlerPack>& transports, |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 126 | ActionMap&& actionPacks) : |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 127 | handlers(std::move(firmwares)), |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 128 | blobIDs(blobs), transports(transports), activeImage(activeImageBlobId), |
| 129 | activeHash(activeHashBlobId), verifyImage(verifyBlobId), |
| 130 | updateImage(updateBlobId), lookup(), state(UpdateState::notYetStarted), |
| 131 | actionPacks(std::move(actionPacks)) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame^] | 132 | {} |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 133 | ~FirmwareBlobHandler() = default; |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 134 | FirmwareBlobHandler(const FirmwareBlobHandler&) = delete; |
| 135 | FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 136 | FirmwareBlobHandler(FirmwareBlobHandler&&) = default; |
| 137 | FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default; |
| 138 | |
| 139 | bool canHandleBlob(const std::string& path) override; |
| 140 | std::vector<std::string> getBlobIds() override; |
| 141 | bool deleteBlob(const std::string& path) override; |
Patrick Venture | e312f39 | 2019-06-04 07:44:37 -0700 | [diff] [blame] | 142 | bool stat(const std::string& path, blobs::BlobMeta* meta) override; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 143 | bool open(uint16_t session, uint16_t flags, |
| 144 | const std::string& path) override; |
| 145 | std::vector<uint8_t> read(uint16_t session, uint32_t offset, |
| 146 | uint32_t requestedSize) override; |
| 147 | bool write(uint16_t session, uint32_t offset, |
| 148 | const std::vector<uint8_t>& data) override; |
| 149 | bool writeMeta(uint16_t session, uint32_t offset, |
| 150 | const std::vector<uint8_t>& data) override; |
| 151 | bool commit(uint16_t session, const std::vector<uint8_t>& data) override; |
| 152 | bool close(uint16_t session) override; |
Patrick Venture | e312f39 | 2019-06-04 07:44:37 -0700 | [diff] [blame] | 153 | bool stat(uint16_t session, blobs::BlobMeta* meta) override; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 154 | bool expire(uint16_t session) override; |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 155 | |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 156 | void abortProcess(); |
| 157 | |
| 158 | void abortVerification(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 159 | bool triggerVerification(); |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 160 | void abortUpdate(); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 161 | bool triggerUpdate(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 162 | |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 163 | /** Allow grabbing the current state. */ |
| 164 | UpdateState getCurrentState() const |
| 165 | { |
| 166 | return state; |
| 167 | }; |
| 168 | |
Patrick Venture | 75c0c27 | 2019-06-21 09:15:08 -0700 | [diff] [blame] | 169 | /** Provide for any state change triggers in convenience handler. */ |
| 170 | void changeState(UpdateState next); |
| 171 | |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 172 | private: |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Given the current session type, grab the ActionPack (likely will be |
| 175 | * worked into the Session for lookup). |
| 176 | */ |
| 177 | ActionPack* getActionPack() |
| 178 | { |
| 179 | if (openedFirmwareType.empty()) |
| 180 | { |
| 181 | /* No firmware type has been opened, but we're triggering |
| 182 | * verification, or preparing. This can happen if they open the hash |
| 183 | * before the image, which is possible. |
| 184 | */ |
| 185 | return nullptr; |
| 186 | } |
| 187 | |
| 188 | /* TODO: Once the actionPacks and supportedFirmwares are merged this'll |
| 189 | * be less dangerous |
| 190 | */ |
| 191 | return actionPacks[openedFirmwareType].get(); |
| 192 | } |
| 193 | |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 194 | void addBlobId(const std::string& blob) |
| 195 | { |
Patrick Venture | f811995 | 2019-06-06 13:47:34 -0700 | [diff] [blame] | 196 | auto blobIdMatch = std::find_if( |
| 197 | blobIDs.begin(), blobIDs.end(), |
| 198 | [&blob](const std::string& iter) { return (iter == blob); }); |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 199 | if (blobIdMatch == blobIDs.end()) |
| 200 | { |
| 201 | blobIDs.push_back(blob); |
| 202 | } |
| 203 | } |
| 204 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 205 | void removeBlobId(const std::string& blob) |
| 206 | { |
| 207 | blobIDs.erase(std::remove(blobIDs.begin(), blobIDs.end(), blob), |
| 208 | blobIDs.end()); |
| 209 | } |
| 210 | |
Brandon Kim | 8fc6087 | 2019-10-18 15:15:50 -0700 | [diff] [blame] | 211 | inline bool fileOpen() |
| 212 | { |
| 213 | return !lookup.empty(); |
| 214 | } |
| 215 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 216 | ActionStatus getVerifyStatus(); |
| 217 | ActionStatus getActionStatus(); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 218 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 219 | /** List of handlers by type. */ |
| 220 | std::vector<HandlerPack> handlers; |
| 221 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 222 | /** Active list of blobIDs. */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 223 | std::vector<std::string> blobIDs; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 224 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 225 | /** List of handlers by transport type. */ |
| 226 | std::vector<DataHandlerPack> transports; |
| 227 | |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 228 | /** Active image session. */ |
| 229 | Session activeImage; |
| 230 | |
| 231 | /** Active hash session. */ |
| 232 | Session activeHash; |
| 233 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 234 | /** Session for verification. */ |
| 235 | Session verifyImage; |
| 236 | |
Patrick Venture | 26e241d | 2019-05-20 18:39:01 -0700 | [diff] [blame] | 237 | /** Session for update. */ |
| 238 | Session updateImage; |
| 239 | |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 240 | /** A quick method for looking up a session's mechanisms and details. */ |
| 241 | std::map<std::uint16_t, Session*> lookup; |
| 242 | |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 243 | /** The firmware update state. */ |
| 244 | UpdateState state; |
| 245 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 246 | /** Track what firmware blobid they opened to start this sequence. */ |
| 247 | std::string openedFirmwareType; |
| 248 | |
Patrick Venture | 6d7735d | 2019-06-21 10:03:19 -0700 | [diff] [blame] | 249 | /* preparation is triggered once we go into uploadInProgress(), but only |
| 250 | * once per full cycle, going back to notYetStarted resets this. |
| 251 | */ |
| 252 | bool preparationTriggered = false; |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 253 | ActionMap actionPacks; |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 254 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 255 | ActionStatus lastVerificationStatus = ActionStatus::unknown; |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 256 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 257 | ActionStatus lastUpdateStatus = ActionStatus::unknown; |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 258 | |
| 259 | /** Portion of "flags" argument to open() which specifies the desired |
| 260 | * transport type |
| 261 | */ |
| 262 | static constexpr std::uint16_t transportMask = 0xff00; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 265 | } // namespace ipmi_flash |