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