blob: 728fdcadc79cc9bb2a768878d15edd595d8d6683 [file] [log] [blame]
Patrick Venturec7ca2912018-11-02 11:38:33 -07001#pragma once
2
Patrick Venture1cde5f92018-11-07 08:26:47 -08003#include "data_handler.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -08004#include "image_handler.hpp"
5
Patrick Venturec7ca2912018-11-02 11:38:33 -07006#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -08007#include <cstdint>
Patrick Ventured9fb6132018-11-08 09:56:10 -08008#include <map>
Patrick Venture68cf64f2018-11-06 10:46:51 -08009#include <memory>
Patrick Venture4eb55952018-11-16 15:36:24 -080010#include <sdbusplus/bus.hpp>
Patrick Venture148cd652018-11-06 10:59:47 -080011#include <string>
12#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070013
14namespace blobs
15{
16
Patrick Venturec7ca2912018-11-02 11:38:33 -070017/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080018 * Representation of a session, includes how to read/write data.
19 */
20struct Session
21{
Patrick Ventured6461d62018-11-09 17:30:25 -080022 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080023 * Built a session object.
24 *
25 * @param[in] the active path to which this corresponds.
26 */
27 explicit Session(const std::string& path) :
28 dataHandler(nullptr), imageHandler(nullptr), flags(0),
29 state(State::closed), activePath(path)
30 {
31 }
32
33 /**
Patrick Ventured6461d62018-11-09 17:30:25 -080034 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080035 */
36 DataInterface* dataHandler;
37
Patrick Ventured6461d62018-11-09 17:30:25 -080038 /**
39 * Pointer to the correct image handler interface. (nullptr on hash
40 * blob_id)
41 */
Patrick Venture2d3a2542018-11-08 09:23:24 -080042 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080043
44 /** The flags used to open the session. */
45 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080046
47 /** A sesion can be for an image (or tarball) or the hash. */
48 enum State
49 {
50 open = 0,
51 closed = 1,
52 };
53
54 /** The current state of this session. */
55 State state;
Patrick Ventureedd55b52018-11-15 11:05:13 -080056
57 /** The active path. */
58 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080059};
60
Patrick Venture18235e62018-11-08 10:21:09 -080061struct ExtChunkHdr
62{
63 std::uint32_t length; /* Length of the data queued (little endian). */
64} __attribute__((packed));
65
Patrick Venture2d3a2542018-11-08 09:23:24 -080066/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070067 * Register only one firmware blob handler that will manage all sessions.
68 */
69class FirmwareBlobHandler : public GenericBlobInterface
70{
71 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080072 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080073 {
Patrick Venture3ca43622018-12-13 09:15:11 -080074 openRead = (1 << 0), /* Flag for reading. */
75 openWrite = (1 << 1), /* Flag for writing. */
Patrick Venture9158dcf2018-11-08 09:44:28 -080076 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080077 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
78 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
79 };
80
Patrick Ventureffcc5502018-11-16 12:32:38 -080081 /* TODO: All of the states may not be required - if we add abort() commands
82 * appropriately.
83 */
Patrick Venture92106df2018-11-09 09:26:30 -080084 /** The state of the firmware update process. */
85 enum UpdateState
86 {
87 /** The initial state. */
88 notYetStarted = 0,
89 /**
90 * The upload process has started, but verification has not started.
91 */
92 uploadInProgress = 1,
93 /** The verification process has started, no more writes allowed. */
94 verificationStarted = 2,
95 /** The verification process has completed. */
96 verificationCompleted = 3,
97 };
98
Patrick Venture79c08bf2018-11-09 15:33:54 -080099 /** The return values for verification. */
100 enum VerifyCheckResponses : std::uint8_t
101 {
102 running = 0,
103 success = 1,
104 failed = 2,
105 other = 3,
106 };
107
Patrick Venture137ad2c2018-11-06 11:30:43 -0800108 /**
109 * Create a FirmwareBlobHandler.
110 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800111 * @param[in] bus - an sdbusplus handler for a bus to use.
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800112 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -0800113 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -0800114 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800115 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -0800116 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800117 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800118
Patrick Venture137ad2c2018-11-06 11:30:43 -0800119 /**
120 * Create a FirmwareBlobHandler.
121 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800122 * @param[in] bus - an sdbusplus handler for a bus to use
Patrick Venture1cde5f92018-11-07 08:26:47 -0800123 * @param[in] firmwares - list of firmware types and their handlers
124 * @param[in] blobs - list of blobs_ids to support
125 * @param[in] transports - list of transport types and their handlers
126 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -0800127 */
Patrick Venture4eb55952018-11-16 15:36:24 -0800128 FirmwareBlobHandler(sdbusplus::bus::bus&& bus,
129 const std::vector<HandlerPack>& firmwares,
Patrick Venturea78e39f2018-11-06 18:37:06 -0800130 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800131 const std::vector<DataHandlerPack>& transports,
132 std::uint16_t bitmask) :
Patrick Venture4eb55952018-11-16 15:36:24 -0800133 bus(std::move(bus)),
134 handlers(firmwares), blobIDs(blobs), transports(transports),
135 bitmask(bitmask), activeImage(activeImageBlobID),
136 activeHash(activeHashBlobID), verifyImage(verifyBlobID), lookup(),
137 state(UpdateState::notYetStarted)
Patrick Venture148cd652018-11-06 10:59:47 -0800138 {
139 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700140 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800141 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
142 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700143 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
144 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
145
146 bool canHandleBlob(const std::string& path) override;
147 std::vector<std::string> getBlobIds() override;
148 bool deleteBlob(const std::string& path) override;
149 bool stat(const std::string& path, struct BlobMeta* meta) override;
150 bool open(uint16_t session, uint16_t flags,
151 const std::string& path) override;
152 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
153 uint32_t requestedSize) override;
154 bool write(uint16_t session, uint32_t offset,
155 const std::vector<uint8_t>& data) override;
156 bool writeMeta(uint16_t session, uint32_t offset,
157 const std::vector<uint8_t>& data) override;
158 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
159 bool close(uint16_t session) override;
160 bool stat(uint16_t session, struct BlobMeta* meta) override;
161 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800162
Patrick Ventureffcc5502018-11-16 12:32:38 -0800163 bool triggerVerification();
164
165 static const std::string verifyBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800166 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -0800167 static const std::string activeImageBlobID;
168 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800169
Patrick Venture92106df2018-11-09 09:26:30 -0800170 /** Allow grabbing the current state. */
171 UpdateState getCurrentState() const
172 {
173 return state;
174 };
175
Patrick Venture148cd652018-11-06 10:59:47 -0800176 private:
Patrick Venture4eb55952018-11-16 15:36:24 -0800177 sdbusplus::bus::bus bus;
178
Patrick Venturea78e39f2018-11-06 18:37:06 -0800179 /** List of handlers by type. */
180 std::vector<HandlerPack> handlers;
181
Patrick Venturec02849b2018-11-06 17:31:15 -0800182 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800183 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800184
Patrick Venture1cde5f92018-11-07 08:26:47 -0800185 /** List of handlers by transport type. */
186 std::vector<DataHandlerPack> transports;
187
Patrick Venturec02849b2018-11-06 17:31:15 -0800188 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800189 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800190
Patrick Ventured9fb6132018-11-08 09:56:10 -0800191 /** Active image session. */
192 Session activeImage;
193
194 /** Active hash session. */
195 Session activeHash;
196
Patrick Ventureffcc5502018-11-16 12:32:38 -0800197 /** Session for verification. */
198 Session verifyImage;
199
Patrick Ventured9fb6132018-11-08 09:56:10 -0800200 /** A quick method for looking up a session's mechanisms and details. */
201 std::map<std::uint16_t, Session*> lookup;
202
Patrick Venture92106df2018-11-09 09:26:30 -0800203 /** The firmware update state. */
204 UpdateState state;
205
Patrick Venturec02849b2018-11-06 17:31:15 -0800206 /** Temporary variable to track whether a blob is open. */
207 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700208};
209
210} // namespace blobs