blob: 3e3689784c1ae9e2f436dc0613da0a7fef101bfb [file] [log] [blame]
Patrick Venturec7ca2912018-11-02 11:38:33 -07001#pragma once
2
Patrick Ventureac11ae92019-01-16 12:43:00 -08003#include "config.h"
4
Patrick Venture1cde5f92018-11-07 08:26:47 -08005#include "data_handler.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -08006#include "image_handler.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07007#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -08008
Patrick Venturec7ca2912018-11-02 11:38:33 -07009#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080010#include <cstdint>
Patrick Ventured9fb6132018-11-08 09:56:10 -080011#include <map>
Patrick Venture68cf64f2018-11-06 10:46:51 -080012#include <memory>
Patrick Ventureac11ae92019-01-16 12:43:00 -080013#if HAVE_SDBUSPLUS
Patrick Venture4eb55952018-11-16 15:36:24 -080014#include <sdbusplus/bus.hpp>
Patrick Ventureac11ae92019-01-16 12:43:00 -080015#else
16namespace sdbusplus
17{
18namespace bus
19{
20class bus
21{
22};
23} // namespace bus
24} // namespace sdbusplus
25#endif
Patrick Venture148cd652018-11-06 10:59:47 -080026#include <string>
27#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070028
29namespace blobs
30{
31
Patrick Venturec7ca2912018-11-02 11:38:33 -070032/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080033 * Representation of a session, includes how to read/write data.
34 */
35struct Session
36{
Patrick Ventured6461d62018-11-09 17:30:25 -080037 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080038 * 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 Ventured6461d62018-11-09 17:30:25 -080049 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080050 */
51 DataInterface* dataHandler;
52
Patrick Ventured6461d62018-11-09 17:30:25 -080053 /**
54 * Pointer to the correct image handler interface. (nullptr on hash
55 * blob_id)
56 */
Patrick Venture2d3a2542018-11-08 09:23:24 -080057 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080058
59 /** The flags used to open the session. */
60 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080061
62 /** A sesion can be for an image (or tarball) or the hash. */
Patrick Venture9420ad22019-05-15 14:34:12 -070063 enum class State
Patrick Venture32ba9dd2018-11-09 16:22:53 -080064 {
65 open = 0,
66 closed = 1,
67 };
68
69 /** The current state of this session. */
70 State state;
Patrick Ventureedd55b52018-11-15 11:05:13 -080071
72 /** The active path. */
73 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080074};
75
Patrick Venture18235e62018-11-08 10:21:09 -080076struct ExtChunkHdr
77{
78 std::uint32_t length; /* Length of the data queued (little endian). */
79} __attribute__((packed));
80
Patrick Venture2d3a2542018-11-08 09:23:24 -080081/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070082 * Register only one firmware blob handler that will manage all sessions.
83 */
84class FirmwareBlobHandler : public GenericBlobInterface
85{
86 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080087 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080088 {
Patrick Venture3ca43622018-12-13 09:15:11 -080089 openRead = (1 << 0), /* Flag for reading. */
90 openWrite = (1 << 1), /* Flag for writing. */
Patrick Venture9158dcf2018-11-08 09:44:28 -080091 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080092 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
93 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
94 };
95
Patrick Ventureffcc5502018-11-16 12:32:38 -080096 /* TODO: All of the states may not be required - if we add abort() commands
97 * appropriately.
98 */
Patrick Venture92106df2018-11-09 09:26:30 -080099 /** The state of the firmware update process. */
Patrick Venture88bc26e2019-05-15 12:02:05 -0700100 enum class UpdateState
Patrick Venture92106df2018-11-09 09:26:30 -0800101 {
102 /** The initial state. */
103 notYetStarted = 0,
Patrick Venture12233c52019-05-16 09:26:59 -0700104 /** The BMC is expecting to receive bytes. */
105 uploadInProgress,
106 /** The BMC is ready for verification or more bytes. */
107 verificationPending,
Patrick Venture92106df2018-11-09 09:26:30 -0800108 /** The verification process has started, no more writes allowed. */
Patrick Venture12233c52019-05-16 09:26:59 -0700109 verificationStarted,
Patrick Venture92106df2018-11-09 09:26:30 -0800110 /** The verification process has completed. */
Patrick Venture12233c52019-05-16 09:26:59 -0700111 verificationCompleted,
Patrick Venture92106df2018-11-09 09:26:30 -0800112 };
113
Patrick Venture79c08bf2018-11-09 15:33:54 -0800114 /** The return values for verification. */
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700115 enum class VerifyCheckResponses : std::uint8_t
Patrick Venture79c08bf2018-11-09 15:33:54 -0800116 {
117 running = 0,
118 success = 1,
119 failed = 2,
120 other = 3,
121 };
122
Patrick Venture137ad2c2018-11-06 11:30:43 -0800123 /**
124 * Create a FirmwareBlobHandler.
125 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800126 * @param[in] bus - an sdbusplus handler for a bus to use.
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800127 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -0800128 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -0800129 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800130 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -0800131 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800132 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800133
Patrick Venture137ad2c2018-11-06 11:30:43 -0800134 /**
135 * Create a FirmwareBlobHandler.
136 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800137 * @param[in] bus - an sdbusplus handler for a bus to use
Patrick Venture1cde5f92018-11-07 08:26:47 -0800138 * @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 Venture137ad2c2018-11-06 11:30:43 -0800142 */
Patrick Venture4eb55952018-11-16 15:36:24 -0800143 FirmwareBlobHandler(sdbusplus::bus::bus&& bus,
144 const std::vector<HandlerPack>& firmwares,
Patrick Venturea78e39f2018-11-06 18:37:06 -0800145 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800146 const std::vector<DataHandlerPack>& transports,
147 std::uint16_t bitmask) :
Patrick Venture4eb55952018-11-16 15:36:24 -0800148 bus(std::move(bus)),
149 handlers(firmwares), blobIDs(blobs), transports(transports),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700150 bitmask(bitmask), activeImage(activeImageBlobId),
151 activeHash(activeHashBlobId), verifyImage(verifyBlobId), lookup(),
Patrick Venture4eb55952018-11-16 15:36:24 -0800152 state(UpdateState::notYetStarted)
Patrick Venture148cd652018-11-06 10:59:47 -0800153 {
154 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700155 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800156 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
157 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700158 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 Venture148cd652018-11-06 10:59:47 -0800177
Patrick Ventureffcc5502018-11-16 12:32:38 -0800178 bool triggerVerification();
179
Patrick Venture92106df2018-11-09 09:26:30 -0800180 /** Allow grabbing the current state. */
181 UpdateState getCurrentState() const
182 {
183 return state;
184 };
185
Patrick Venture148cd652018-11-06 10:59:47 -0800186 private:
Patrick Venture4eb55952018-11-16 15:36:24 -0800187 sdbusplus::bus::bus bus;
188
Patrick Venturea78e39f2018-11-06 18:37:06 -0800189 /** List of handlers by type. */
190 std::vector<HandlerPack> handlers;
191
Patrick Venturec02849b2018-11-06 17:31:15 -0800192 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800193 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800194
Patrick Venture1cde5f92018-11-07 08:26:47 -0800195 /** List of handlers by transport type. */
196 std::vector<DataHandlerPack> transports;
197
Patrick Venturec02849b2018-11-06 17:31:15 -0800198 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800199 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800200
Patrick Ventured9fb6132018-11-08 09:56:10 -0800201 /** Active image session. */
202 Session activeImage;
203
204 /** Active hash session. */
205 Session activeHash;
206
Patrick Ventureffcc5502018-11-16 12:32:38 -0800207 /** Session for verification. */
208 Session verifyImage;
209
Patrick Ventured9fb6132018-11-08 09:56:10 -0800210 /** A quick method for looking up a session's mechanisms and details. */
211 std::map<std::uint16_t, Session*> lookup;
212
Patrick Venture92106df2018-11-09 09:26:30 -0800213 /** The firmware update state. */
214 UpdateState state;
215
Patrick Venturec02849b2018-11-06 17:31:15 -0800216 /** Temporary variable to track whether a blob is open. */
217 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700218};
219
220} // namespace blobs