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