blob: 505f1a78be1540cfaf69d9b424a98c93e2918170 [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 Venture1d66fe62019-06-03 14:57:27 -07007#include "status.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07008#include "util.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -08009
Patrick Venturec7ca2912018-11-02 11:38:33 -070010#include <blobs-ipmid/blobs.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070011
12#include <algorithm>
Patrick Venture192d60f2018-11-06 11:11:59 -080013#include <cstdint>
Patrick Ventured9fb6132018-11-08 09:56:10 -080014#include <map>
Patrick Venture68cf64f2018-11-06 10:46:51 -080015#include <memory>
Patrick Venture148cd652018-11-06 10:59:47 -080016#include <string>
Patrick Venturefa06a5f2019-07-01 09:22:38 -070017#include <unordered_map>
Patrick Venture148cd652018-11-06 10:59:47 -080018#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070019
Patrick Venture1d5a31c2019-05-20 11:38:22 -070020namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070021{
22
Patrick Venturec7ca2912018-11-02 11:38:33 -070023/**
Patrick Venturefa06a5f2019-07-01 09:22:38 -070024 * Given a firmware name, provide a set of triggerable action interfaces
25 * associated with that firmware type.
26 */
27struct ActionPack
28{
29 /** The name of the action pack, something like image, or tarball, or bios.
30 * The firmware blob id is parsed to pull the "filename" portion from the
31 * path, and matched against the key to a map of these.
32 */
33 std::unique_ptr<TriggerableActionInterface> preparation;
34 std::unique_ptr<TriggerableActionInterface> verification;
35 std::unique_ptr<TriggerableActionInterface> update;
36};
37
38using ActionMap =
39 std::unordered_map<std::string, std::unique_ptr<ipmi_flash::ActionPack>>;
40
41/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080042 * Representation of a session, includes how to read/write data.
43 */
44struct Session
45{
Patrick Ventured6461d62018-11-09 17:30:25 -080046 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080047 * Built a session object.
48 *
49 * @param[in] the active path to which this corresponds.
50 */
51 explicit Session(const std::string& path) :
Patrick Ventured816b232019-06-05 13:30:32 -070052 dataHandler(nullptr), imageHandler(nullptr), flags(0), activePath(path)
Patrick Venture9b37b092020-05-28 20:58:57 -070053 {}
Patrick Ventureedd55b52018-11-15 11:05:13 -080054
55 /**
Patrick Ventured6461d62018-11-09 17:30:25 -080056 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080057 */
58 DataInterface* dataHandler;
59
Patrick Ventured6461d62018-11-09 17:30:25 -080060 /**
61 * Pointer to the correct image handler interface. (nullptr on hash
62 * blob_id)
63 */
Jason Lingded66d02020-10-23 14:13:03 -070064 ipmi_flash::ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080065
66 /** The flags used to open the session. */
67 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080068
Patrick Ventureedd55b52018-11-15 11:05:13 -080069 /** The active path. */
70 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080071};
72
73/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070074 * Register only one firmware blob handler that will manage all sessions.
75 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -070076class FirmwareBlobHandler : public blobs::GenericBlobInterface
Patrick Venturec7ca2912018-11-02 11:38:33 -070077{
78 public:
Patrick Venture92106df2018-11-09 09:26:30 -080079 /** The state of the firmware update process. */
Patrick Venture88bc26e2019-05-15 12:02:05 -070080 enum class UpdateState
Patrick Venture92106df2018-11-09 09:26:30 -080081 {
82 /** The initial state. */
83 notYetStarted = 0,
Patrick Venture12233c52019-05-16 09:26:59 -070084 /** The BMC is expecting to receive bytes. */
85 uploadInProgress,
86 /** The BMC is ready for verification or more bytes. */
87 verificationPending,
Patrick Venture92106df2018-11-09 09:26:30 -080088 /** The verification process has started, no more writes allowed. */
Patrick Venture12233c52019-05-16 09:26:59 -070089 verificationStarted,
Patrick Venture92106df2018-11-09 09:26:30 -080090 /** The verification process has completed. */
Patrick Venture12233c52019-05-16 09:26:59 -070091 verificationCompleted,
Patrick Venture02922e42019-05-22 09:49:31 -070092 /** The update process is pending. */
93 updatePending,
94 /** The update process has started. */
95 updateStarted,
96 /** The update has completed (optional state to reach) */
Patrick Venturef1f0f652019-06-03 09:10:19 -070097 updateCompleted,
Patrick Venture92106df2018-11-09 09:26:30 -080098 };
99
Patrick Venture137ad2c2018-11-06 11:30:43 -0800100 /**
101 * Create a FirmwareBlobHandler.
102 *
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800103 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -0800104 * @param[in] transports - list of transports to support.
Patrick Venture3ecb3502019-05-17 11:03:51 -0700105 * @param[in] verification - pointer to object for triggering verification
Patrick Venture27ac5822019-05-20 17:39:31 -0700106 * @param[in] update - point to object for triggering the update
Patrick Venture137ad2c2018-11-06 11:30:43 -0800107 */
Patrick Venture9efef5d2019-06-19 08:45:44 -0700108 static std::unique_ptr<blobs::GenericBlobInterface>
Patrick Venture7b783432020-09-22 15:55:08 -0700109 CreateFirmwareBlobHandler(std::vector<HandlerPack>&& firmwares,
110 std::vector<DataHandlerPack>&& transports,
111 ActionMap&& actionPacks);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800112
Patrick Venture137ad2c2018-11-06 11:30:43 -0800113 /**
114 * Create a FirmwareBlobHandler.
115 *
Patrick Venture1cde5f92018-11-07 08:26:47 -0800116 * @param[in] firmwares - list of firmware types and their handlers
117 * @param[in] blobs - list of blobs_ids to support
118 * @param[in] transports - list of transport types and their handlers
Patrick Venture3ecb3502019-05-17 11:03:51 -0700119 * @param[in] verification - pointer to object for triggering verification
Patrick Venture27ac5822019-05-20 17:39:31 -0700120 * @param[in] update - point to object for triggering the update
Patrick Venture137ad2c2018-11-06 11:30:43 -0800121 */
Patrick Ventured4e20de2019-07-18 12:48:05 -0700122 FirmwareBlobHandler(std::vector<HandlerPack>&& firmwares,
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700123 const std::vector<std::string>& blobs,
Patrick Venture7b783432020-09-22 15:55:08 -0700124 std::vector<DataHandlerPack>&& transports,
Benjamin Fair12901982019-11-12 13:55:46 -0800125 ActionMap&& actionPacks) :
Patrick Ventured4e20de2019-07-18 12:48:05 -0700126 handlers(std::move(firmwares)),
Patrick Venture7b783432020-09-22 15:55:08 -0700127 blobIDs(blobs), transports(std::move(transports)),
128 activeImage(activeImageBlobId), activeHash(activeHashBlobId),
129 verifyImage(verifyBlobId), updateImage(updateBlobId), lookup(),
130 state(UpdateState::notYetStarted), actionPacks(std::move(actionPacks))
Patrick Venture9b37b092020-05-28 20:58:57 -0700131 {}
Patrick Venturec7ca2912018-11-02 11:38:33 -0700132 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800133 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
134 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700135 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
136 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
137
138 bool canHandleBlob(const std::string& path) override;
139 std::vector<std::string> getBlobIds() override;
140 bool deleteBlob(const std::string& path) override;
Patrick Venturee312f392019-06-04 07:44:37 -0700141 bool stat(const std::string& path, blobs::BlobMeta* meta) override;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700142 bool open(uint16_t session, uint16_t flags,
143 const std::string& path) override;
144 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
145 uint32_t requestedSize) override;
146 bool write(uint16_t session, uint32_t offset,
147 const std::vector<uint8_t>& data) override;
148 bool writeMeta(uint16_t session, uint32_t offset,
149 const std::vector<uint8_t>& data) override;
150 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
151 bool close(uint16_t session) override;
Patrick Venturee312f392019-06-04 07:44:37 -0700152 bool stat(uint16_t session, blobs::BlobMeta* meta) override;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700153 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800154
Patrick Ventured5741022019-06-17 09:08:35 -0700155 void abortProcess();
156
157 void abortVerification();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800158 bool triggerVerification();
Patrick Venture49731712019-06-17 10:04:02 -0700159 void abortUpdate();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700160 bool triggerUpdate();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800161
Patrick Venture92106df2018-11-09 09:26:30 -0800162 /** Allow grabbing the current state. */
163 UpdateState getCurrentState() const
164 {
165 return state;
166 };
167
Patrick Venture75c0c272019-06-21 09:15:08 -0700168 /** Provide for any state change triggers in convenience handler. */
169 void changeState(UpdateState next);
170
Patrick Venture148cd652018-11-06 10:59:47 -0800171 private:
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700172 /**
173 * Given the current session type, grab the ActionPack (likely will be
174 * worked into the Session for lookup).
175 */
176 ActionPack* getActionPack()
177 {
178 if (openedFirmwareType.empty())
179 {
180 /* No firmware type has been opened, but we're triggering
181 * verification, or preparing. This can happen if they open the hash
182 * before the image, which is possible.
183 */
184 return nullptr;
185 }
186
187 /* TODO: Once the actionPacks and supportedFirmwares are merged this'll
188 * be less dangerous
189 */
190 return actionPacks[openedFirmwareType].get();
191 }
192
Patrick Ventureefba42d2019-05-24 10:48:16 -0700193 void addBlobId(const std::string& blob)
194 {
Patrick Venturef8119952019-06-06 13:47:34 -0700195 auto blobIdMatch = std::find_if(
196 blobIDs.begin(), blobIDs.end(),
197 [&blob](const std::string& iter) { return (iter == blob); });
Patrick Ventureefba42d2019-05-24 10:48:16 -0700198 if (blobIdMatch == blobIDs.end())
199 {
200 blobIDs.push_back(blob);
201 }
202 }
203
Patrick Venture930c7b72019-05-24 11:11:08 -0700204 void removeBlobId(const std::string& blob)
205 {
206 blobIDs.erase(std::remove(blobIDs.begin(), blobIDs.end(), blob),
207 blobIDs.end());
208 }
209
Brandon Kim8fc60872019-10-18 15:15:50 -0700210 inline bool fileOpen()
211 {
212 return !lookup.empty();
213 }
214
Patrick Ventureda66fd82019-06-03 11:11:24 -0700215 ActionStatus getVerifyStatus();
216 ActionStatus getActionStatus();
Patrick Venturea2d82392019-06-03 10:55:17 -0700217
Patrick Venturea78e39f2018-11-06 18:37:06 -0800218 /** List of handlers by type. */
219 std::vector<HandlerPack> handlers;
220
Patrick Venturec02849b2018-11-06 17:31:15 -0800221 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800222 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800223
Patrick Venture1cde5f92018-11-07 08:26:47 -0800224 /** List of handlers by transport type. */
225 std::vector<DataHandlerPack> transports;
226
Patrick Ventured9fb6132018-11-08 09:56:10 -0800227 /** Active image session. */
228 Session activeImage;
229
230 /** Active hash session. */
231 Session activeHash;
232
Patrick Ventureffcc5502018-11-16 12:32:38 -0800233 /** Session for verification. */
234 Session verifyImage;
235
Patrick Venture26e241d2019-05-20 18:39:01 -0700236 /** Session for update. */
237 Session updateImage;
238
Patrick Ventured9fb6132018-11-08 09:56:10 -0800239 /** A quick method for looking up a session's mechanisms and details. */
240 std::map<std::uint16_t, Session*> lookup;
241
Patrick Venture92106df2018-11-09 09:26:30 -0800242 /** The firmware update state. */
243 UpdateState state;
244
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700245 /** Track what firmware blobid they opened to start this sequence. */
246 std::string openedFirmwareType;
247
Patrick Venture6d7735d2019-06-21 10:03:19 -0700248 /* preparation is triggered once we go into uploadInProgress(), but only
249 * once per full cycle, going back to notYetStarted resets this.
250 */
251 bool preparationTriggered = false;
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700252 ActionMap actionPacks;
Patrick Venture27ac5822019-05-20 17:39:31 -0700253
Patrick Ventureda66fd82019-06-03 11:11:24 -0700254 ActionStatus lastVerificationStatus = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700255
Patrick Ventureda66fd82019-06-03 11:11:24 -0700256 ActionStatus lastUpdateStatus = ActionStatus::unknown;
Benjamin Fair12901982019-11-12 13:55:46 -0800257
258 /** Portion of "flags" argument to open() which specifies the desired
259 * transport type
260 */
261 static constexpr std::uint16_t transportMask = 0xff00;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700262};
263
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700264} // namespace ipmi_flash