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 | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 7 | #include "update.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 8 | #include "util.hpp" |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 9 | #include "verify.hpp" |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 10 | |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 12 | #include <blobs-ipmid/blobs.hpp> |
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> |
| 17 | #include <vector> |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 18 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 19 | namespace ipmi_flash |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 20 | { |
| 21 | |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 22 | /** |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 23 | * Representation of a session, includes how to read/write data. |
| 24 | */ |
| 25 | struct Session |
| 26 | { |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 27 | /** |
Patrick Venture | edd55b5 | 2018-11-15 11:05:13 -0800 | [diff] [blame] | 28 | * Built a session object. |
| 29 | * |
| 30 | * @param[in] the active path to which this corresponds. |
| 31 | */ |
| 32 | explicit Session(const std::string& path) : |
| 33 | dataHandler(nullptr), imageHandler(nullptr), flags(0), |
| 34 | state(State::closed), activePath(path) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | /** |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 39 | * Pointer to the correct Data handler interface. (nullptr on BT (or KCS)) |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 40 | */ |
| 41 | DataInterface* dataHandler; |
| 42 | |
Patrick Venture | d6461d6 | 2018-11-09 17:30:25 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Pointer to the correct image handler interface. (nullptr on hash |
| 45 | * blob_id) |
| 46 | */ |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 47 | ImageHandlerInterface* imageHandler; |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 48 | |
| 49 | /** The flags used to open the session. */ |
| 50 | std::uint16_t flags; |
Patrick Venture | 32ba9dd | 2018-11-09 16:22:53 -0800 | [diff] [blame] | 51 | |
| 52 | /** A sesion can be for an image (or tarball) or the hash. */ |
Patrick Venture | 9420ad2 | 2019-05-15 14:34:12 -0700 | [diff] [blame] | 53 | enum class State |
Patrick Venture | 32ba9dd | 2018-11-09 16:22:53 -0800 | [diff] [blame] | 54 | { |
| 55 | open = 0, |
| 56 | closed = 1, |
| 57 | }; |
| 58 | |
| 59 | /** The current state of this session. */ |
| 60 | State state; |
Patrick Venture | edd55b5 | 2018-11-15 11:05:13 -0800 | [diff] [blame] | 61 | |
| 62 | /** The active path. */ |
| 63 | std::string activePath; |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Patrick Venture | 18235e6 | 2018-11-08 10:21:09 -0800 | [diff] [blame] | 66 | struct ExtChunkHdr |
| 67 | { |
| 68 | std::uint32_t length; /* Length of the data queued (little endian). */ |
| 69 | } __attribute__((packed)); |
| 70 | |
Patrick Venture | 2d3a254 | 2018-11-08 09:23:24 -0800 | [diff] [blame] | 71 | /** |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 72 | * Register only one firmware blob handler that will manage all sessions. |
| 73 | */ |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 74 | class FirmwareBlobHandler : public blobs::GenericBlobInterface |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 75 | { |
| 76 | public: |
Patrick Venture | 05abf7e | 2018-11-09 11:02:56 -0800 | [diff] [blame] | 77 | enum UpdateFlags : std::uint16_t |
Patrick Venture | fc3857b | 2018-11-07 08:14:55 -0800 | [diff] [blame] | 78 | { |
Patrick Venture | 3ca4362 | 2018-12-13 09:15:11 -0800 | [diff] [blame] | 79 | openRead = (1 << 0), /* Flag for reading. */ |
| 80 | openWrite = (1 << 1), /* Flag for writing. */ |
Patrick Venture | 9158dcf | 2018-11-08 09:44:28 -0800 | [diff] [blame] | 81 | ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */ |
Patrick Venture | fc3857b | 2018-11-07 08:14:55 -0800 | [diff] [blame] | 82 | p2a = (1 << 9), /* Expect to send contents over P2A bridge. */ |
| 83 | lpc = (1 << 10), /* Expect to send contents over LPC bridge. */ |
| 84 | }; |
| 85 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 86 | /* TODO: All of the states may not be required - if we add abort() commands |
| 87 | * appropriately. |
| 88 | */ |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 89 | /** The state of the firmware update process. */ |
Patrick Venture | 88bc26e | 2019-05-15 12:02:05 -0700 | [diff] [blame] | 90 | enum class UpdateState |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 91 | { |
| 92 | /** The initial state. */ |
| 93 | notYetStarted = 0, |
Patrick Venture | 12233c5 | 2019-05-16 09:26:59 -0700 | [diff] [blame] | 94 | /** The BMC is expecting to receive bytes. */ |
| 95 | uploadInProgress, |
| 96 | /** The BMC is ready for verification or more bytes. */ |
| 97 | verificationPending, |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 98 | /** The verification process has started, no more writes allowed. */ |
Patrick Venture | 12233c5 | 2019-05-16 09:26:59 -0700 | [diff] [blame] | 99 | verificationStarted, |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 100 | /** The verification process has completed. */ |
Patrick Venture | 12233c5 | 2019-05-16 09:26:59 -0700 | [diff] [blame] | 101 | verificationCompleted, |
Patrick Venture | 02922e4 | 2019-05-22 09:49:31 -0700 | [diff] [blame] | 102 | /** The update process is pending. */ |
| 103 | updatePending, |
| 104 | /** The update process has started. */ |
| 105 | updateStarted, |
| 106 | /** The update has completed (optional state to reach) */ |
| 107 | updatedCompleted, |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Create a FirmwareBlobHandler. |
| 112 | * |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 113 | * @param[in] firmwares - list of firmware blob_ids to support. |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 114 | * @param[in] transports - list of transports to support. |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 115 | * @param[in] verification - pointer to object for triggering verification |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 116 | * @param[in] update - point to object for triggering the update |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 117 | */ |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 118 | static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 119 | const std::vector<HandlerPack>& firmwares, |
Patrick Venture | 74059d6 | 2019-05-17 10:40:26 -0700 | [diff] [blame] | 120 | const std::vector<DataHandlerPack>& transports, |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 121 | std::unique_ptr<VerificationInterface> verification, |
| 122 | std::unique_ptr<UpdateInterface> update); |
Patrick Venture | 68cf64f | 2018-11-06 10:46:51 -0800 | [diff] [blame] | 123 | |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 124 | /** |
| 125 | * Create a FirmwareBlobHandler. |
| 126 | * |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 127 | * @param[in] firmwares - list of firmware types and their handlers |
| 128 | * @param[in] blobs - list of blobs_ids to support |
| 129 | * @param[in] transports - list of transport types and their handlers |
| 130 | * @param[in] bitmask - bitmask of transports to support |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 131 | * @param[in] verification - pointer to object for triggering verification |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 132 | * @param[in] update - point to object for triggering the update |
Patrick Venture | 137ad2c | 2018-11-06 11:30:43 -0800 | [diff] [blame] | 133 | */ |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 134 | FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares, |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 135 | const std::vector<std::string>& blobs, |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 136 | const std::vector<DataHandlerPack>& transports, |
Patrick Venture | 74059d6 | 2019-05-17 10:40:26 -0700 | [diff] [blame] | 137 | std::uint16_t bitmask, |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 138 | std::unique_ptr<VerificationInterface> verification, |
| 139 | std::unique_ptr<UpdateInterface> update) : |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 140 | handlers(firmwares), |
| 141 | blobIDs(blobs), transports(transports), bitmask(bitmask), |
| 142 | activeImage(activeImageBlobId), activeHash(activeHashBlobId), |
Patrick Venture | 26e241d | 2019-05-20 18:39:01 -0700 | [diff] [blame] | 143 | verifyImage(verifyBlobId), updateImage(updateBlobId), lookup(), |
| 144 | state(UpdateState::notYetStarted), |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 145 | verification(std::move(verification)), update(std::move(update)) |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 146 | { |
| 147 | } |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 148 | ~FirmwareBlobHandler() = default; |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 149 | FirmwareBlobHandler(const FirmwareBlobHandler&) = delete; |
| 150 | FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 151 | FirmwareBlobHandler(FirmwareBlobHandler&&) = default; |
| 152 | FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default; |
| 153 | |
| 154 | bool canHandleBlob(const std::string& path) override; |
| 155 | std::vector<std::string> getBlobIds() override; |
| 156 | bool deleteBlob(const std::string& path) override; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 157 | bool stat(const std::string& path, struct blobs::BlobMeta* meta) override; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 158 | bool open(uint16_t session, uint16_t flags, |
| 159 | const std::string& path) override; |
| 160 | std::vector<uint8_t> read(uint16_t session, uint32_t offset, |
| 161 | uint32_t requestedSize) override; |
| 162 | bool write(uint16_t session, uint32_t offset, |
| 163 | const std::vector<uint8_t>& data) override; |
| 164 | bool writeMeta(uint16_t session, uint32_t offset, |
| 165 | const std::vector<uint8_t>& data) override; |
| 166 | bool commit(uint16_t session, const std::vector<uint8_t>& data) override; |
| 167 | bool close(uint16_t session) override; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 168 | bool stat(uint16_t session, struct blobs::BlobMeta* meta) override; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 169 | bool expire(uint16_t session) override; |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 170 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 171 | bool triggerVerification(); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame^] | 172 | bool triggerUpdate(); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 173 | |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 174 | /** Allow grabbing the current state. */ |
| 175 | UpdateState getCurrentState() const |
| 176 | { |
| 177 | return state; |
| 178 | }; |
| 179 | |
Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 180 | private: |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 181 | void addBlobId(const std::string& blob) |
| 182 | { |
| 183 | auto blobIdMatch = |
| 184 | std::find_if(blobIDs.begin(), blobIDs.end(), |
| 185 | [&blob](const auto& iter) { return (iter == blob); }); |
| 186 | if (blobIdMatch == blobIDs.end()) |
| 187 | { |
| 188 | blobIDs.push_back(blob); |
| 189 | } |
| 190 | } |
| 191 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 192 | void removeBlobId(const std::string& blob) |
| 193 | { |
| 194 | blobIDs.erase(std::remove(blobIDs.begin(), blobIDs.end(), blob), |
| 195 | blobIDs.end()); |
| 196 | } |
| 197 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 198 | /** List of handlers by type. */ |
| 199 | std::vector<HandlerPack> handlers; |
| 200 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 201 | /** Active list of blobIDs. */ |
Patrick Venture | 4cceb8e | 2018-11-06 11:56:48 -0800 | [diff] [blame] | 202 | std::vector<std::string> blobIDs; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 203 | |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 204 | /** List of handlers by transport type. */ |
| 205 | std::vector<DataHandlerPack> transports; |
| 206 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 207 | /** The bits set indicate what transport mechanisms are supported. */ |
Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 208 | std::uint16_t bitmask; |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 209 | |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 210 | /** Active image session. */ |
| 211 | Session activeImage; |
| 212 | |
| 213 | /** Active hash session. */ |
| 214 | Session activeHash; |
| 215 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 216 | /** Session for verification. */ |
| 217 | Session verifyImage; |
| 218 | |
Patrick Venture | 26e241d | 2019-05-20 18:39:01 -0700 | [diff] [blame] | 219 | /** Session for update. */ |
| 220 | Session updateImage; |
| 221 | |
Patrick Venture | d9fb613 | 2018-11-08 09:56:10 -0800 | [diff] [blame] | 222 | /** A quick method for looking up a session's mechanisms and details. */ |
| 223 | std::map<std::uint16_t, Session*> lookup; |
| 224 | |
Patrick Venture | 92106df | 2018-11-09 09:26:30 -0800 | [diff] [blame] | 225 | /** The firmware update state. */ |
| 226 | UpdateState state; |
| 227 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 228 | std::unique_ptr<VerificationInterface> verification; |
Patrick Venture | 74059d6 | 2019-05-17 10:40:26 -0700 | [diff] [blame] | 229 | |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 230 | std::unique_ptr<UpdateInterface> update; |
| 231 | |
Patrick Venture | c02849b | 2018-11-06 17:31:15 -0800 | [diff] [blame] | 232 | /** Temporary variable to track whether a blob is open. */ |
| 233 | bool fileOpen = false; |
Patrick Venture | 615c69e | 2019-05-29 10:49:54 -0700 | [diff] [blame] | 234 | |
| 235 | VerifyCheckResponses lastVerificationResponse = VerifyCheckResponses::other; |
Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 238 | } // namespace ipmi_flash |