blob: 897606c4b8fdc78c30cf84c97fcbf87e6f96c3d1 [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 Venture9158dcf2018-11-08 09:44:28 -080074 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080075 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
76 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
77 };
78
Patrick Ventureffcc5502018-11-16 12:32:38 -080079 /* TODO: All of the states may not be required - if we add abort() commands
80 * appropriately.
81 */
Patrick Venture92106df2018-11-09 09:26:30 -080082 /** The state of the firmware update process. */
83 enum UpdateState
84 {
85 /** The initial state. */
86 notYetStarted = 0,
87 /**
88 * The upload process has started, but verification has not started.
89 */
90 uploadInProgress = 1,
91 /** The verification process has started, no more writes allowed. */
92 verificationStarted = 2,
93 /** The verification process has completed. */
94 verificationCompleted = 3,
95 };
96
Patrick Venture79c08bf2018-11-09 15:33:54 -080097 /** The return values for verification. */
98 enum VerifyCheckResponses : std::uint8_t
99 {
100 running = 0,
101 success = 1,
102 failed = 2,
103 other = 3,
104 };
105
Patrick Venture137ad2c2018-11-06 11:30:43 -0800106 /**
107 * Create a FirmwareBlobHandler.
108 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800109 * @param[in] bus - an sdbusplus handler for a bus to use.
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800110 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -0800111 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -0800112 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800113 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture4eb55952018-11-16 15:36:24 -0800114 sdbusplus::bus::bus&& bus, const std::vector<HandlerPack>& firmwares,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800115 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800116
Patrick Venture137ad2c2018-11-06 11:30:43 -0800117 /**
118 * Create a FirmwareBlobHandler.
119 *
Patrick Venture4eb55952018-11-16 15:36:24 -0800120 * @param[in] bus - an sdbusplus handler for a bus to use
Patrick Venture1cde5f92018-11-07 08:26:47 -0800121 * @param[in] firmwares - list of firmware types and their handlers
122 * @param[in] blobs - list of blobs_ids to support
123 * @param[in] transports - list of transport types and their handlers
124 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -0800125 */
Patrick Venture4eb55952018-11-16 15:36:24 -0800126 FirmwareBlobHandler(sdbusplus::bus::bus&& bus,
127 const std::vector<HandlerPack>& firmwares,
Patrick Venturea78e39f2018-11-06 18:37:06 -0800128 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800129 const std::vector<DataHandlerPack>& transports,
130 std::uint16_t bitmask) :
Patrick Venture4eb55952018-11-16 15:36:24 -0800131 bus(std::move(bus)),
132 handlers(firmwares), blobIDs(blobs), transports(transports),
133 bitmask(bitmask), activeImage(activeImageBlobID),
134 activeHash(activeHashBlobID), verifyImage(verifyBlobID), lookup(),
135 state(UpdateState::notYetStarted)
Patrick Venture148cd652018-11-06 10:59:47 -0800136 {
137 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700138 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800139 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
140 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700141 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
142 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
143
144 bool canHandleBlob(const std::string& path) override;
145 std::vector<std::string> getBlobIds() override;
146 bool deleteBlob(const std::string& path) override;
147 bool stat(const std::string& path, struct BlobMeta* meta) override;
148 bool open(uint16_t session, uint16_t flags,
149 const std::string& path) override;
150 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
151 uint32_t requestedSize) override;
152 bool write(uint16_t session, uint32_t offset,
153 const std::vector<uint8_t>& data) override;
154 bool writeMeta(uint16_t session, uint32_t offset,
155 const std::vector<uint8_t>& data) override;
156 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
157 bool close(uint16_t session) override;
158 bool stat(uint16_t session, struct BlobMeta* meta) override;
159 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800160
Patrick Ventureffcc5502018-11-16 12:32:38 -0800161 bool triggerVerification();
162
163 static const std::string verifyBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800164 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -0800165 static const std::string activeImageBlobID;
166 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800167
Patrick Venture92106df2018-11-09 09:26:30 -0800168 /** Allow grabbing the current state. */
169 UpdateState getCurrentState() const
170 {
171 return state;
172 };
173
Patrick Venture148cd652018-11-06 10:59:47 -0800174 private:
Patrick Venture4eb55952018-11-16 15:36:24 -0800175 sdbusplus::bus::bus bus;
176
Patrick Venturea78e39f2018-11-06 18:37:06 -0800177 /** List of handlers by type. */
178 std::vector<HandlerPack> handlers;
179
Patrick Venturec02849b2018-11-06 17:31:15 -0800180 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800181 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800182
Patrick Venture1cde5f92018-11-07 08:26:47 -0800183 /** List of handlers by transport type. */
184 std::vector<DataHandlerPack> transports;
185
Patrick Venturec02849b2018-11-06 17:31:15 -0800186 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800187 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800188
Patrick Ventured9fb6132018-11-08 09:56:10 -0800189 /** Active image session. */
190 Session activeImage;
191
192 /** Active hash session. */
193 Session activeHash;
194
Patrick Ventureffcc5502018-11-16 12:32:38 -0800195 /** Session for verification. */
196 Session verifyImage;
197
Patrick Ventured9fb6132018-11-08 09:56:10 -0800198 /** A quick method for looking up a session's mechanisms and details. */
199 std::map<std::uint16_t, Session*> lookup;
200
Patrick Venture92106df2018-11-09 09:26:30 -0800201 /** The firmware update state. */
202 UpdateState state;
203
Patrick Venturec02849b2018-11-06 17:31:15 -0800204 /** Temporary variable to track whether a blob is open. */
205 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700206};
207
208} // namespace blobs