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