blob: 9aab59a68485e0bdb3509b5349e8196962aacdd7 [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) :
32 dataHandler(nullptr), imageHandler(nullptr), flags(0),
33 state(State::closed), activePath(path)
34 {
35 }
36
37 /**
Patrick Ventured6461d62018-11-09 17:30:25 -080038 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080039 */
40 DataInterface* dataHandler;
41
Patrick Ventured6461d62018-11-09 17:30:25 -080042 /**
43 * Pointer to the correct image handler interface. (nullptr on hash
44 * blob_id)
45 */
Patrick Venture2d3a2542018-11-08 09:23:24 -080046 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080047
48 /** The flags used to open the session. */
49 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080050
51 /** A sesion can be for an image (or tarball) or the hash. */
Patrick Venture9420ad22019-05-15 14:34:12 -070052 enum class State
Patrick Venture32ba9dd2018-11-09 16:22:53 -080053 {
54 open = 0,
55 closed = 1,
56 };
57
58 /** The current state of this session. */
59 State state;
Patrick Ventureedd55b52018-11-15 11:05:13 -080060
61 /** The active path. */
62 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080063};
64
Patrick Venture18235e62018-11-08 10:21:09 -080065struct ExtChunkHdr
66{
67 std::uint32_t length; /* Length of the data queued (little endian). */
68} __attribute__((packed));
69
Patrick Venture2d3a2542018-11-08 09:23:24 -080070/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070071 * Register only one firmware blob handler that will manage all sessions.
72 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -070073class FirmwareBlobHandler : public blobs::GenericBlobInterface
Patrick Venturec7ca2912018-11-02 11:38:33 -070074{
75 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080076 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080077 {
Patrick Venture3ca43622018-12-13 09:15:11 -080078 openRead = (1 << 0), /* Flag for reading. */
79 openWrite = (1 << 1), /* Flag for writing. */
Patrick Venture9158dcf2018-11-08 09:44:28 -080080 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080081 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
82 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
83 };
84
Patrick Ventureffcc5502018-11-16 12:32:38 -080085 /* TODO: All of the states may not be required - if we add abort() commands
86 * appropriately.
87 */
Patrick Venture92106df2018-11-09 09:26:30 -080088 /** The state of the firmware update process. */
Patrick Venture88bc26e2019-05-15 12:02:05 -070089 enum class UpdateState
Patrick Venture92106df2018-11-09 09:26:30 -080090 {
91 /** The initial state. */
92 notYetStarted = 0,
Patrick Venture12233c52019-05-16 09:26:59 -070093 /** The BMC is expecting to receive bytes. */
94 uploadInProgress,
95 /** The BMC is ready for verification or more bytes. */
96 verificationPending,
Patrick Venture92106df2018-11-09 09:26:30 -080097 /** The verification process has started, no more writes allowed. */
Patrick Venture12233c52019-05-16 09:26:59 -070098 verificationStarted,
Patrick Venture92106df2018-11-09 09:26:30 -080099 /** The verification process has completed. */
Patrick Venture12233c52019-05-16 09:26:59 -0700100 verificationCompleted,
Patrick Venture02922e42019-05-22 09:49:31 -0700101 /** The update process is pending. */
102 updatePending,
103 /** The update process has started. */
104 updateStarted,
105 /** The update has completed (optional state to reach) */
Patrick Venturef1f0f652019-06-03 09:10:19 -0700106 updateCompleted,
Patrick Venture92106df2018-11-09 09:26:30 -0800107 };
108
Patrick Venture137ad2c2018-11-06 11:30:43 -0800109 /**
110 * Create a FirmwareBlobHandler.
111 *
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 Venture3ecb3502019-05-17 11:03:51 -0700114 * @param[in] verification - pointer to object for triggering verification
Patrick Venture27ac5822019-05-20 17:39:31 -0700115 * @param[in] update - point to object for triggering the update
Patrick Venture137ad2c2018-11-06 11:30:43 -0800116 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800117 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700118 const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -0700119 const std::vector<DataHandlerPack>& transports,
Patrick Venture1d66fe62019-06-03 14:57:27 -0700120 std::unique_ptr<TriggerableActionInterface> verification,
121 std::unique_ptr<TriggerableActionInterface> update);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800122
Patrick Venture137ad2c2018-11-06 11:30:43 -0800123 /**
124 * Create a FirmwareBlobHandler.
125 *
Patrick Venture1cde5f92018-11-07 08:26:47 -0800126 * @param[in] firmwares - list of firmware types and their handlers
127 * @param[in] blobs - list of blobs_ids to support
128 * @param[in] transports - list of transport types and their handlers
129 * @param[in] bitmask - bitmask of transports to support
Patrick Venture3ecb3502019-05-17 11:03:51 -0700130 * @param[in] verification - pointer to object for triggering verification
Patrick Venture27ac5822019-05-20 17:39:31 -0700131 * @param[in] update - point to object for triggering the update
Patrick Venture137ad2c2018-11-06 11:30:43 -0800132 */
Patrick Venture1d66fe62019-06-03 14:57:27 -0700133 FirmwareBlobHandler(
134 const std::vector<HandlerPack>& firmwares,
135 const std::vector<std::string>& blobs,
136 const std::vector<DataHandlerPack>& transports, std::uint16_t bitmask,
137 std::unique_ptr<TriggerableActionInterface> verification,
138 std::unique_ptr<TriggerableActionInterface> update) :
Patrick Venture3ecb3502019-05-17 11:03:51 -0700139 handlers(firmwares),
140 blobIDs(blobs), transports(transports), bitmask(bitmask),
141 activeImage(activeImageBlobId), activeHash(activeHashBlobId),
Patrick Venture26e241d2019-05-20 18:39:01 -0700142 verifyImage(verifyBlobId), updateImage(updateBlobId), lookup(),
143 state(UpdateState::notYetStarted),
Patrick Venture27ac5822019-05-20 17:39:31 -0700144 verification(std::move(verification)), update(std::move(update))
Patrick Venture148cd652018-11-06 10:59:47 -0800145 {
146 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700147 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800148 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
149 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700150 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
151 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
152
153 bool canHandleBlob(const std::string& path) override;
154 std::vector<std::string> getBlobIds() override;
155 bool deleteBlob(const std::string& path) override;
Patrick Venturee312f392019-06-04 07:44:37 -0700156 bool stat(const std::string& path, blobs::BlobMeta* meta) override;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700157 bool open(uint16_t session, uint16_t flags,
158 const std::string& path) override;
159 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
160 uint32_t requestedSize) override;
161 bool write(uint16_t session, uint32_t offset,
162 const std::vector<uint8_t>& data) override;
163 bool writeMeta(uint16_t session, uint32_t offset,
164 const std::vector<uint8_t>& data) override;
165 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
166 bool close(uint16_t session) override;
Patrick Venturee312f392019-06-04 07:44:37 -0700167 bool stat(uint16_t session, blobs::BlobMeta* meta) override;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700168 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800169
Patrick Ventureffcc5502018-11-16 12:32:38 -0800170 bool triggerVerification();
Patrick Venture1a406fe2019-05-31 07:29:56 -0700171 bool triggerUpdate();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800172
Patrick Venture92106df2018-11-09 09:26:30 -0800173 /** Allow grabbing the current state. */
174 UpdateState getCurrentState() const
175 {
176 return state;
177 };
178
Patrick Venture148cd652018-11-06 10:59:47 -0800179 private:
Patrick Ventureefba42d2019-05-24 10:48:16 -0700180 void addBlobId(const std::string& blob)
181 {
182 auto blobIdMatch =
183 std::find_if(blobIDs.begin(), blobIDs.end(),
184 [&blob](const auto& iter) { return (iter == blob); });
185 if (blobIdMatch == blobIDs.end())
186 {
187 blobIDs.push_back(blob);
188 }
189 }
190
Patrick Venture930c7b72019-05-24 11:11:08 -0700191 void removeBlobId(const std::string& blob)
192 {
193 blobIDs.erase(std::remove(blobIDs.begin(), blobIDs.end(), blob),
194 blobIDs.end());
195 }
196
Patrick Ventureda66fd82019-06-03 11:11:24 -0700197 ActionStatus getVerifyStatus();
198 ActionStatus getActionStatus();
Patrick Venturea2d82392019-06-03 10:55:17 -0700199
Patrick Venturea78e39f2018-11-06 18:37:06 -0800200 /** List of handlers by type. */
201 std::vector<HandlerPack> handlers;
202
Patrick Venturec02849b2018-11-06 17:31:15 -0800203 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800204 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800205
Patrick Venture1cde5f92018-11-07 08:26:47 -0800206 /** List of handlers by transport type. */
207 std::vector<DataHandlerPack> transports;
208
Patrick Venturec02849b2018-11-06 17:31:15 -0800209 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800210 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800211
Patrick Ventured9fb6132018-11-08 09:56:10 -0800212 /** Active image session. */
213 Session activeImage;
214
215 /** Active hash session. */
216 Session activeHash;
217
Patrick Ventureffcc5502018-11-16 12:32:38 -0800218 /** Session for verification. */
219 Session verifyImage;
220
Patrick Venture26e241d2019-05-20 18:39:01 -0700221 /** Session for update. */
222 Session updateImage;
223
Patrick Ventured9fb6132018-11-08 09:56:10 -0800224 /** A quick method for looking up a session's mechanisms and details. */
225 std::map<std::uint16_t, Session*> lookup;
226
Patrick Venture92106df2018-11-09 09:26:30 -0800227 /** The firmware update state. */
228 UpdateState state;
229
Patrick Venture1d66fe62019-06-03 14:57:27 -0700230 std::unique_ptr<TriggerableActionInterface> verification;
Patrick Venture74059d62019-05-17 10:40:26 -0700231
Patrick Venture1d66fe62019-06-03 14:57:27 -0700232 std::unique_ptr<TriggerableActionInterface> update;
Patrick Venture27ac5822019-05-20 17:39:31 -0700233
Patrick Venturec02849b2018-11-06 17:31:15 -0800234 /** Temporary variable to track whether a blob is open. */
235 bool fileOpen = false;
Patrick Venture615c69e2019-05-29 10:49:54 -0700236
Patrick Ventureda66fd82019-06-03 11:11:24 -0700237 ActionStatus lastVerificationStatus = ActionStatus::unknown;
Patrick Venturea2d82392019-06-03 10:55:17 -0700238
Patrick Ventureda66fd82019-06-03 11:11:24 -0700239 ActionStatus lastUpdateStatus = ActionStatus::unknown;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700240};
241
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700242} // namespace ipmi_flash