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