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