blob: 3dd49782edfed29bf5b47fb5e61d2649cb20f519 [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 Ventureefba42d2019-05-24 10:48:16 -070010#include <algorithm>
Patrick Venturec7ca2912018-11-02 11:38:33 -070011#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080012#include <cstdint>
Patrick Ventured9fb6132018-11-08 09:56:10 -080013#include <map>
Patrick Venture68cf64f2018-11-06 10:46:51 -080014#include <memory>
Patrick Venture148cd652018-11-06 10:59:47 -080015#include <string>
16#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070017
Patrick Venture1d5a31c2019-05-20 11:38:22 -070018namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070019{
20
Patrick Venturec7ca2912018-11-02 11:38:33 -070021/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080022 * Representation of a session, includes how to read/write data.
23 */
24struct Session
25{
Patrick Ventured6461d62018-11-09 17:30:25 -080026 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080027 * Built a session object.
28 *
29 * @param[in] the active path to which this corresponds.
30 */
31 explicit Session(const std::string& path) :
Patrick Ventured816b232019-06-05 13:30:32 -070032 dataHandler(nullptr), imageHandler(nullptr), flags(0), activePath(path)
Patrick Ventureedd55b52018-11-15 11:05:13 -080033 {
34 }
35
36 /**
Patrick Ventured6461d62018-11-09 17:30:25 -080037 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080038 */
39 DataInterface* dataHandler;
40
Patrick Ventured6461d62018-11-09 17:30:25 -080041 /**
42 * Pointer to the correct image handler interface. (nullptr on hash
43 * blob_id)
44 */
Patrick Venture2d3a2542018-11-08 09:23:24 -080045 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080046
47 /** The flags used to open the session. */
48 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080049
Patrick Ventureedd55b52018-11-15 11:05:13 -080050 /** The active path. */
51 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080052};
53
54/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070055 * Register only one firmware blob handler that will manage all sessions.
56 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -070057class FirmwareBlobHandler : public blobs::GenericBlobInterface
Patrick Venturec7ca2912018-11-02 11:38:33 -070058{
59 public:
Patrick Venture92106df2018-11-09 09:26:30 -080060 /** The state of the firmware update process. */
Patrick Venture88bc26e2019-05-15 12:02:05 -070061 enum class UpdateState
Patrick Venture92106df2018-11-09 09:26:30 -080062 {
63 /** The initial state. */
64 notYetStarted = 0,
Patrick Venture12233c52019-05-16 09:26:59 -070065 /** The BMC is expecting to receive bytes. */
66 uploadInProgress,
67 /** The BMC is ready for verification or more bytes. */
68 verificationPending,
Patrick Venture92106df2018-11-09 09:26:30 -080069 /** The verification process has started, no more writes allowed. */
Patrick Venture12233c52019-05-16 09:26:59 -070070 verificationStarted,
Patrick Venture92106df2018-11-09 09:26:30 -080071 /** The verification process has completed. */
Patrick Venture12233c52019-05-16 09:26:59 -070072 verificationCompleted,
Patrick Venture02922e42019-05-22 09:49:31 -070073 /** The update process is pending. */
74 updatePending,
75 /** The update process has started. */
76 updateStarted,
77 /** The update has completed (optional state to reach) */
Patrick Venturef1f0f652019-06-03 09:10:19 -070078 updateCompleted,
Patrick Venture92106df2018-11-09 09:26:30 -080079 };
80
Patrick Venture137ad2c2018-11-06 11:30:43 -080081 /**
82 * Create a FirmwareBlobHandler.
83 *
Patrick Venture4cceb8e2018-11-06 11:56:48 -080084 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -080085 * @param[in] transports - list of transports to support.
Patrick Venture3ecb3502019-05-17 11:03:51 -070086 * @param[in] verification - pointer to object for triggering verification
Patrick Venture27ac5822019-05-20 17:39:31 -070087 * @param[in] update - point to object for triggering the update
Patrick Venture137ad2c2018-11-06 11:30:43 -080088 */
Patrick Venture9efef5d2019-06-19 08:45:44 -070089 static std::unique_ptr<blobs::GenericBlobInterface>
90 CreateFirmwareBlobHandler(
91 const std::vector<HandlerPack>& firmwares,
92 const std::vector<DataHandlerPack>& transports,
Patrick Venture6d7735d2019-06-21 10:03:19 -070093 std::unique_ptr<TriggerableActionInterface> preparation,
Patrick Venture9efef5d2019-06-19 08:45:44 -070094 std::unique_ptr<TriggerableActionInterface> verification,
95 std::unique_ptr<TriggerableActionInterface> update);
Patrick Venture68cf64f2018-11-06 10:46:51 -080096
Patrick Venture137ad2c2018-11-06 11:30:43 -080097 /**
98 * Create a FirmwareBlobHandler.
99 *
Patrick Venture1cde5f92018-11-07 08:26:47 -0800100 * @param[in] firmwares - list of firmware types and their handlers
101 * @param[in] blobs - list of blobs_ids to support
102 * @param[in] transports - list of transport types and their handlers
103 * @param[in] bitmask - bitmask of transports to support
Patrick Venture3ecb3502019-05-17 11:03:51 -0700104 * @param[in] verification - pointer to object for triggering verification
Patrick Venture27ac5822019-05-20 17:39:31 -0700105 * @param[in] update - point to object for triggering the update
Patrick Venture137ad2c2018-11-06 11:30:43 -0800106 */
Patrick Venture1d66fe62019-06-03 14:57:27 -0700107 FirmwareBlobHandler(
108 const std::vector<HandlerPack>& firmwares,
109 const std::vector<std::string>& blobs,
110 const std::vector<DataHandlerPack>& transports, std::uint16_t bitmask,
Patrick Venture6d7735d2019-06-21 10:03:19 -0700111 std::unique_ptr<TriggerableActionInterface> preparation,
Patrick Venture1d66fe62019-06-03 14:57:27 -0700112 std::unique_ptr<TriggerableActionInterface> verification,
113 std::unique_ptr<TriggerableActionInterface> update) :
Patrick Venture3ecb3502019-05-17 11:03:51 -0700114 handlers(firmwares),
115 blobIDs(blobs), transports(transports), bitmask(bitmask),
116 activeImage(activeImageBlobId), activeHash(activeHashBlobId),
Patrick Venture26e241d2019-05-20 18:39:01 -0700117 verifyImage(verifyBlobId), updateImage(updateBlobId), lookup(),
Patrick Venture6d7735d2019-06-21 10:03:19 -0700118 state(UpdateState::notYetStarted), preparation(std::move(preparation)),
Patrick Venture27ac5822019-05-20 17:39:31 -0700119 verification(std::move(verification)), update(std::move(update))
Patrick Venture148cd652018-11-06 10:59:47 -0800120 {
121 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700122 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800123 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
124 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700125 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
126 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
127
128 bool canHandleBlob(const std::string& path) override;
129 std::vector<std::string> getBlobIds() override;
130 bool deleteBlob(const std::string& path) override;
Patrick Venturee312f392019-06-04 07:44:37 -0700131 bool stat(const std::string& path, blobs::BlobMeta* meta) override;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700132 bool open(uint16_t session, uint16_t flags,
133 const std::string& path) override;
134 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
135 uint32_t requestedSize) override;
136 bool write(uint16_t session, uint32_t offset,
137 const std::vector<uint8_t>& data) override;
138 bool writeMeta(uint16_t session, uint32_t offset,
139 const std::vector<uint8_t>& data) override;
140 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
141 bool close(uint16_t session) override;
Patrick Venturee312f392019-06-04 07:44:37 -0700142 bool stat(uint16_t session, blobs::BlobMeta* meta) override;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700143 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800144
Patrick Ventured5741022019-06-17 09:08:35 -0700145 void abortProcess();
146
147 void abortVerification();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800148 bool triggerVerification();
Patrick Venture49731712019-06-17 10:04:02 -0700149 void abortUpdate();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700150 bool triggerUpdate();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800151
Patrick Venture92106df2018-11-09 09:26:30 -0800152 /** Allow grabbing the current state. */
153 UpdateState getCurrentState() const
154 {
155 return state;
156 };
157
Patrick Venture75c0c272019-06-21 09:15:08 -0700158 /** Provide for any state change triggers in convenience handler. */
159 void changeState(UpdateState next);
160
Patrick Venture148cd652018-11-06 10:59:47 -0800161 private:
Patrick Ventureefba42d2019-05-24 10:48:16 -0700162 void addBlobId(const std::string& blob)
163 {
Patrick Venturef8119952019-06-06 13:47:34 -0700164 auto blobIdMatch = std::find_if(
165 blobIDs.begin(), blobIDs.end(),
166 [&blob](const std::string& iter) { return (iter == blob); });
Patrick Ventureefba42d2019-05-24 10:48:16 -0700167 if (blobIdMatch == blobIDs.end())
168 {
169 blobIDs.push_back(blob);
170 }
171 }
172
Patrick Venture930c7b72019-05-24 11:11:08 -0700173 void removeBlobId(const std::string& blob)
174 {
175 blobIDs.erase(std::remove(blobIDs.begin(), blobIDs.end(), blob),
176 blobIDs.end());
177 }
178
Patrick Ventureda66fd82019-06-03 11:11:24 -0700179 ActionStatus getVerifyStatus();
180 ActionStatus getActionStatus();
Patrick Venturea2d82392019-06-03 10:55:17 -0700181
Patrick Venturea78e39f2018-11-06 18:37:06 -0800182 /** List of handlers by type. */
183 std::vector<HandlerPack> handlers;
184
Patrick Venturec02849b2018-11-06 17:31:15 -0800185 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800186 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800187
Patrick Venture1cde5f92018-11-07 08:26:47 -0800188 /** List of handlers by transport type. */
189 std::vector<DataHandlerPack> transports;
190
Patrick Venturec02849b2018-11-06 17:31:15 -0800191 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800192 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800193
Patrick Ventured9fb6132018-11-08 09:56:10 -0800194 /** Active image session. */
195 Session activeImage;
196
197 /** Active hash session. */
198 Session activeHash;
199
Patrick Ventureffcc5502018-11-16 12:32:38 -0800200 /** Session for verification. */
201 Session verifyImage;
202
Patrick Venture26e241d2019-05-20 18:39:01 -0700203 /** Session for update. */
204 Session updateImage;
205
Patrick Ventured9fb6132018-11-08 09:56:10 -0800206 /** A quick method for looking up a session's mechanisms and details. */
207 std::map<std::uint16_t, Session*> lookup;
208
Patrick Venture92106df2018-11-09 09:26:30 -0800209 /** The firmware update state. */
210 UpdateState state;
211
Patrick Venture6d7735d2019-06-21 10:03:19 -0700212 /* preparation is triggered once we go into uploadInProgress(), but only
213 * once per full cycle, going back to notYetStarted resets this.
214 */
215 bool preparationTriggered = false;
216 std::unique_ptr<TriggerableActionInterface> preparation;
217
Patrick Venture1d66fe62019-06-03 14:57:27 -0700218 std::unique_ptr<TriggerableActionInterface> verification;
Patrick Venture74059d62019-05-17 10:40:26 -0700219
Patrick Venture1d66fe62019-06-03 14:57:27 -0700220 std::unique_ptr<TriggerableActionInterface> update;
Patrick Venture27ac5822019-05-20 17:39:31 -0700221
Patrick Venturec02849b2018-11-06 17:31:15 -0800222 /** Temporary variable to track whether a blob is open. */
223 bool fileOpen = false;
Patrick Venture615c69e2019-05-29 10:49:54 -0700224
Patrick Ventureda66fd82019-06-03 11:11:24 -0700225 ActionStatus lastVerificationStatus = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700226
Patrick Ventureda66fd82019-06-03 11:11:24 -0700227 ActionStatus lastUpdateStatus = ActionStatus::unknown;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700228};
229
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700230} // namespace ipmi_flash