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