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