blob: 8d41ba6c308d1769d5a609d6add4d0a4b5c5116b [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"
7
Patrick Venturec7ca2912018-11-02 11:38:33 -07008#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -08009#include <cstdint>
Patrick Ventured9fb6132018-11-08 09:56:10 -080010#include <map>
Patrick Venture68cf64f2018-11-06 10:46:51 -080011#include <memory>
Patrick Ventureac11ae92019-01-16 12:43:00 -080012#if HAVE_SDBUSPLUS
Patrick Venture4eb55952018-11-16 15:36:24 -080013#include <sdbusplus/bus.hpp>
Patrick Ventureac11ae92019-01-16 12:43:00 -080014#else
15namespace sdbusplus
16{
17namespace bus
18{
19class bus
20{
21};
22} // namespace bus
23} // namespace sdbusplus
24#endif
Patrick Venture148cd652018-11-06 10:59:47 -080025#include <string>
26#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070027
28namespace blobs
29{
30
Patrick Venturec7ca2912018-11-02 11:38:33 -070031/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080032 * Representation of a session, includes how to read/write data.
33 */
34struct Session
35{
Patrick Ventured6461d62018-11-09 17:30:25 -080036 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080037 * 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 Ventured6461d62018-11-09 17:30:25 -080048 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080049 */
50 DataInterface* dataHandler;
51
Patrick Ventured6461d62018-11-09 17:30:25 -080052 /**
53 * Pointer to the correct image handler interface. (nullptr on hash
54 * blob_id)
55 */
Patrick Venture2d3a2542018-11-08 09:23:24 -080056 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080057
58 /** The flags used to open the session. */
59 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080060
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 Ventureedd55b52018-11-15 11:05:13 -080070
71 /** The active path. */
72 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080073};
74
Patrick Venture18235e62018-11-08 10:21:09 -080075struct ExtChunkHdr
76{
77 std::uint32_t length; /* Length of the data queued (little endian). */
78} __attribute__((packed));
79
Patrick Venture2d3a2542018-11-08 09:23:24 -080080/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070081 * Register only one firmware blob handler that will manage all sessions.
82 */
83class FirmwareBlobHandler : public GenericBlobInterface
84{
85 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080086 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080087 {
Patrick Venture3ca43622018-12-13 09:15:11 -080088 openRead = (1 << 0), /* Flag for reading. */
89 openWrite = (1 << 1), /* Flag for writing. */
Patrick Venture9158dcf2018-11-08 09:44:28 -080090 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080091 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
92 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
93 };
94
Patrick Ventureffcc5502018-11-16 12:32:38 -080095 /* TODO: All of the states may not be required - if we add abort() commands
96 * appropriately.
97 */
Patrick Venture92106df2018-11-09 09:26:30 -080098 /** The state of the firmware update process. */
Patrick Venture88bc26e2019-05-15 12:02:05 -070099 enum class UpdateState
Patrick Venture92106df2018-11-09 09:26:30 -0800100 {
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 Venture79c08bf2018-11-09 15:33:54 -0800113 /** The return values for verification. */
Patrick Ventureb3b4db72019-05-15 11:30:24 -0700114 enum class VerifyCheckResponses : std::uint8_t
Patrick Venture79c08bf2018-11-09 15:33:54 -0800115 {
116 running = 0,
117 success = 1,
118 failed = 2,
119 other = 3,
120 };
121
Patrick Venture137ad2c2018-11-06 11:30:43 -0800122 /**
123 * Create a FirmwareBlobHandler.
124 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800125 * @param[in] bus - an sdbusplus handler for a bus to use.
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800126 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -0800127 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -0800128 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800129 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -0800130 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800131 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800132
Patrick Venture137ad2c2018-11-06 11:30:43 -0800133 /**
134 * Create a FirmwareBlobHandler.
135 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800136 * @param[in] bus - an sdbusplus handler for a bus to use
Patrick Venture1cde5f92018-11-07 08:26:47 -0800137 * @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 Venture137ad2c2018-11-06 11:30:43 -0800141 */
Patrick Venture4eb55952018-11-16 15:36:24 -0800142 FirmwareBlobHandler(sdbusplus::bus::bus&& bus,
143 const std::vector<HandlerPack>& firmwares,
Patrick Venturea78e39f2018-11-06 18:37:06 -0800144 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800145 const std::vector<DataHandlerPack>& transports,
146 std::uint16_t bitmask) :
Patrick Venture4eb55952018-11-16 15:36:24 -0800147 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 Venture148cd652018-11-06 10:59:47 -0800152 {
153 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700154 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800155 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
156 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700157 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 Venture148cd652018-11-06 10:59:47 -0800176
Patrick Ventureffcc5502018-11-16 12:32:38 -0800177 bool triggerVerification();
178
179 static const std::string verifyBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800180 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -0800181 static const std::string activeImageBlobID;
182 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800183
Patrick Venture92106df2018-11-09 09:26:30 -0800184 /** Allow grabbing the current state. */
185 UpdateState getCurrentState() const
186 {
187 return state;
188 };
189
Patrick Venture148cd652018-11-06 10:59:47 -0800190 private:
Patrick Venture4eb55952018-11-16 15:36:24 -0800191 sdbusplus::bus::bus bus;
192
Patrick Venturea78e39f2018-11-06 18:37:06 -0800193 /** List of handlers by type. */
194 std::vector<HandlerPack> handlers;
195
Patrick Venturec02849b2018-11-06 17:31:15 -0800196 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800197 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800198
Patrick Venture1cde5f92018-11-07 08:26:47 -0800199 /** List of handlers by transport type. */
200 std::vector<DataHandlerPack> transports;
201
Patrick Venturec02849b2018-11-06 17:31:15 -0800202 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800203 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800204
Patrick Ventured9fb6132018-11-08 09:56:10 -0800205 /** Active image session. */
206 Session activeImage;
207
208 /** Active hash session. */
209 Session activeHash;
210
Patrick Ventureffcc5502018-11-16 12:32:38 -0800211 /** Session for verification. */
212 Session verifyImage;
213
Patrick Ventured9fb6132018-11-08 09:56:10 -0800214 /** A quick method for looking up a session's mechanisms and details. */
215 std::map<std::uint16_t, Session*> lookup;
216
Patrick Venture92106df2018-11-09 09:26:30 -0800217 /** The firmware update state. */
218 UpdateState state;
219
Patrick Venturec02849b2018-11-06 17:31:15 -0800220 /** Temporary variable to track whether a blob is open. */
221 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700222};
223
224} // namespace blobs