blob: 3467ccd7115903a769b0a6ebf396eb58aa52c7b6 [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 Venture74059d62019-05-17 10:40:26 -0700129 * @param[in[ verificationPath - path to check for verification output
Patrick Venture137ad2c2018-11-06 11:30:43 -0800130 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800131 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -0800132 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -0700133 const std::vector<DataHandlerPack>& transports,
134 const std::string& verificationPath);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800135
Patrick Venture137ad2c2018-11-06 11:30:43 -0800136 /**
137 * Create a FirmwareBlobHandler.
138 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800139 * @param[in] bus - an sdbusplus handler for a bus to use
Patrick Venture1cde5f92018-11-07 08:26:47 -0800140 * @param[in] firmwares - list of firmware types and their handlers
141 * @param[in] blobs - list of blobs_ids to support
142 * @param[in] transports - list of transport types and their handlers
143 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -0800144 */
Patrick Venture4eb55952018-11-16 15:36:24 -0800145 FirmwareBlobHandler(sdbusplus::bus::bus&& bus,
146 const std::vector<HandlerPack>& firmwares,
Patrick Venturea78e39f2018-11-06 18:37:06 -0800147 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800148 const std::vector<DataHandlerPack>& transports,
Patrick Venture74059d62019-05-17 10:40:26 -0700149 std::uint16_t bitmask,
150 const std::string& verificationPath) :
Patrick Venture4eb55952018-11-16 15:36:24 -0800151 bus(std::move(bus)),
152 handlers(firmwares), blobIDs(blobs), transports(transports),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700153 bitmask(bitmask), activeImage(activeImageBlobId),
154 activeHash(activeHashBlobId), verifyImage(verifyBlobId), lookup(),
Patrick Venture74059d62019-05-17 10:40:26 -0700155 state(UpdateState::notYetStarted), verificationPath(verificationPath)
Patrick Venture148cd652018-11-06 10:59:47 -0800156 {
157 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700158 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800159 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
160 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700161 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
162 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
163
164 bool canHandleBlob(const std::string& path) override;
165 std::vector<std::string> getBlobIds() override;
166 bool deleteBlob(const std::string& path) override;
167 bool stat(const std::string& path, struct BlobMeta* meta) override;
168 bool open(uint16_t session, uint16_t flags,
169 const std::string& path) override;
170 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
171 uint32_t requestedSize) override;
172 bool write(uint16_t session, uint32_t offset,
173 const std::vector<uint8_t>& data) override;
174 bool writeMeta(uint16_t session, uint32_t offset,
175 const std::vector<uint8_t>& data) override;
176 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
177 bool close(uint16_t session) override;
178 bool stat(uint16_t session, struct BlobMeta* meta) override;
179 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800180
Patrick Ventureffcc5502018-11-16 12:32:38 -0800181 bool triggerVerification();
182
Patrick Venture92106df2018-11-09 09:26:30 -0800183 /** Allow grabbing the current state. */
184 UpdateState getCurrentState() const
185 {
186 return state;
187 };
188
Patrick Venture148cd652018-11-06 10:59:47 -0800189 private:
Patrick Venture4eb55952018-11-16 15:36:24 -0800190 sdbusplus::bus::bus bus;
191
Patrick Venturea78e39f2018-11-06 18:37:06 -0800192 /** List of handlers by type. */
193 std::vector<HandlerPack> handlers;
194
Patrick Venturec02849b2018-11-06 17:31:15 -0800195 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800196 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800197
Patrick Venture1cde5f92018-11-07 08:26:47 -0800198 /** List of handlers by transport type. */
199 std::vector<DataHandlerPack> transports;
200
Patrick Venturec02849b2018-11-06 17:31:15 -0800201 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800202 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800203
Patrick Ventured9fb6132018-11-08 09:56:10 -0800204 /** Active image session. */
205 Session activeImage;
206
207 /** Active hash session. */
208 Session activeHash;
209
Patrick Ventureffcc5502018-11-16 12:32:38 -0800210 /** Session for verification. */
211 Session verifyImage;
212
Patrick Ventured9fb6132018-11-08 09:56:10 -0800213 /** A quick method for looking up a session's mechanisms and details. */
214 std::map<std::uint16_t, Session*> lookup;
215
Patrick Venture92106df2018-11-09 09:26:30 -0800216 /** The firmware update state. */
217 UpdateState state;
218
Patrick Venture74059d62019-05-17 10:40:26 -0700219 const std::string verificationPath;
220
Patrick Venturec02849b2018-11-06 17:31:15 -0800221 /** Temporary variable to track whether a blob is open. */
222 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700223};
224
225} // namespace blobs