blob: 88cb95753c46621fcf071c995fa46e5c542f5333 [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 Venture7dad86f2019-05-17 08:52:20 -07007#include "util.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -07008#include "verify.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -08009
Patrick Venturec7ca2912018-11-02 11:38:33 -070010#include <blobs-ipmid/blobs.hpp>
Patrick Venture192d60f2018-11-06 11:11:59 -080011#include <cstdint>
Patrick Ventured9fb6132018-11-08 09:56:10 -080012#include <map>
Patrick Venture68cf64f2018-11-06 10:46:51 -080013#include <memory>
Patrick Venture148cd652018-11-06 10:59:47 -080014#include <string>
15#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070016
17namespace blobs
18{
19
Patrick Venturec7ca2912018-11-02 11:38:33 -070020/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080021 * Representation of a session, includes how to read/write data.
22 */
23struct Session
24{
Patrick Ventured6461d62018-11-09 17:30:25 -080025 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080026 * Built a session object.
27 *
28 * @param[in] the active path to which this corresponds.
29 */
30 explicit Session(const std::string& path) :
31 dataHandler(nullptr), imageHandler(nullptr), flags(0),
32 state(State::closed), activePath(path)
33 {
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
50 /** A sesion can be for an image (or tarball) or the hash. */
Patrick Venture9420ad22019-05-15 14:34:12 -070051 enum class State
Patrick Venture32ba9dd2018-11-09 16:22:53 -080052 {
53 open = 0,
54 closed = 1,
55 };
56
57 /** The current state of this session. */
58 State state;
Patrick Ventureedd55b52018-11-15 11:05:13 -080059
60 /** The active path. */
61 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080062};
63
Patrick Venture18235e62018-11-08 10:21:09 -080064struct ExtChunkHdr
65{
66 std::uint32_t length; /* Length of the data queued (little endian). */
67} __attribute__((packed));
68
Patrick Venture2d3a2542018-11-08 09:23:24 -080069/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070070 * Register only one firmware blob handler that will manage all sessions.
71 */
72class FirmwareBlobHandler : public GenericBlobInterface
73{
74 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080075 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080076 {
Patrick Venture3ca43622018-12-13 09:15:11 -080077 openRead = (1 << 0), /* Flag for reading. */
78 openWrite = (1 << 1), /* Flag for writing. */
Patrick Venture9158dcf2018-11-08 09:44:28 -080079 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080080 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
81 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
82 };
83
Patrick Ventureffcc5502018-11-16 12:32:38 -080084 /* TODO: All of the states may not be required - if we add abort() commands
85 * appropriately.
86 */
Patrick Venture92106df2018-11-09 09:26:30 -080087 /** The state of the firmware update process. */
Patrick Venture88bc26e2019-05-15 12:02:05 -070088 enum class UpdateState
Patrick Venture92106df2018-11-09 09:26:30 -080089 {
90 /** The initial state. */
91 notYetStarted = 0,
Patrick Venture12233c52019-05-16 09:26:59 -070092 /** The BMC is expecting to receive bytes. */
93 uploadInProgress,
94 /** The BMC is ready for verification or more bytes. */
95 verificationPending,
Patrick Venture92106df2018-11-09 09:26:30 -080096 /** The verification process has started, no more writes allowed. */
Patrick Venture12233c52019-05-16 09:26:59 -070097 verificationStarted,
Patrick Venture92106df2018-11-09 09:26:30 -080098 /** The verification process has completed. */
Patrick Venture12233c52019-05-16 09:26:59 -070099 verificationCompleted,
Patrick Venture92106df2018-11-09 09:26:30 -0800100 };
101
Patrick Venture137ad2c2018-11-06 11:30:43 -0800102 /**
103 * Create a FirmwareBlobHandler.
104 *
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800105 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -0800106 * @param[in] transports - list of transports to support.
Patrick Venture3ecb3502019-05-17 11:03:51 -0700107 * @param[in] verification - pointer to object for triggering verification
Patrick Venture137ad2c2018-11-06 11:30:43 -0800108 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800109 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700110 const std::vector<HandlerPack>& firmwares,
Patrick Venture74059d62019-05-17 10:40:26 -0700111 const std::vector<DataHandlerPack>& transports,
Patrick Venture3ecb3502019-05-17 11:03:51 -0700112 std::unique_ptr<VerificationInterface> verification);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800113
Patrick Venture137ad2c2018-11-06 11:30:43 -0800114 /**
115 * Create a FirmwareBlobHandler.
116 *
Patrick Venture1cde5f92018-11-07 08:26:47 -0800117 * @param[in] firmwares - list of firmware types and their handlers
118 * @param[in] blobs - list of blobs_ids to support
119 * @param[in] transports - list of transport types and their handlers
120 * @param[in] bitmask - bitmask of transports to support
Patrick Venture3ecb3502019-05-17 11:03:51 -0700121 * @param[in] verification - pointer to object for triggering verification
Patrick Venture137ad2c2018-11-06 11:30:43 -0800122 */
Patrick Venture3ecb3502019-05-17 11:03:51 -0700123 FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares,
Patrick Venturea78e39f2018-11-06 18:37:06 -0800124 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800125 const std::vector<DataHandlerPack>& transports,
Patrick Venture74059d62019-05-17 10:40:26 -0700126 std::uint16_t bitmask,
Patrick Venture3ecb3502019-05-17 11:03:51 -0700127 std::unique_ptr<VerificationInterface> verification) :
128 handlers(firmwares),
129 blobIDs(blobs), transports(transports), bitmask(bitmask),
130 activeImage(activeImageBlobId), activeHash(activeHashBlobId),
131 verifyImage(verifyBlobId), lookup(), state(UpdateState::notYetStarted),
132 verification(std::move(verification))
Patrick Venture148cd652018-11-06 10:59:47 -0800133 {
134 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700135 ~FirmwareBlobHandler() = default;
Patrick Venture4eb55952018-11-16 15:36:24 -0800136 FirmwareBlobHandler(const FirmwareBlobHandler&) = delete;
137 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = delete;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700138 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
139 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
140
141 bool canHandleBlob(const std::string& path) override;
142 std::vector<std::string> getBlobIds() override;
143 bool deleteBlob(const std::string& path) override;
144 bool stat(const std::string& path, struct BlobMeta* meta) override;
145 bool open(uint16_t session, uint16_t flags,
146 const std::string& path) override;
147 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
148 uint32_t requestedSize) override;
149 bool write(uint16_t session, uint32_t offset,
150 const std::vector<uint8_t>& data) override;
151 bool writeMeta(uint16_t session, uint32_t offset,
152 const std::vector<uint8_t>& data) override;
153 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
154 bool close(uint16_t session) override;
155 bool stat(uint16_t session, struct BlobMeta* meta) override;
156 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800157
Patrick Ventureffcc5502018-11-16 12:32:38 -0800158 bool triggerVerification();
159
Patrick Venture92106df2018-11-09 09:26:30 -0800160 /** Allow grabbing the current state. */
161 UpdateState getCurrentState() const
162 {
163 return state;
164 };
165
Patrick Venture148cd652018-11-06 10:59:47 -0800166 private:
Patrick Venturea78e39f2018-11-06 18:37:06 -0800167 /** List of handlers by type. */
168 std::vector<HandlerPack> handlers;
169
Patrick Venturec02849b2018-11-06 17:31:15 -0800170 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800171 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800172
Patrick Venture1cde5f92018-11-07 08:26:47 -0800173 /** List of handlers by transport type. */
174 std::vector<DataHandlerPack> transports;
175
Patrick Venturec02849b2018-11-06 17:31:15 -0800176 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800177 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800178
Patrick Ventured9fb6132018-11-08 09:56:10 -0800179 /** Active image session. */
180 Session activeImage;
181
182 /** Active hash session. */
183 Session activeHash;
184
Patrick Ventureffcc5502018-11-16 12:32:38 -0800185 /** Session for verification. */
186 Session verifyImage;
187
Patrick Ventured9fb6132018-11-08 09:56:10 -0800188 /** A quick method for looking up a session's mechanisms and details. */
189 std::map<std::uint16_t, Session*> lookup;
190
Patrick Venture92106df2018-11-09 09:26:30 -0800191 /** The firmware update state. */
192 UpdateState state;
193
Patrick Venture3ecb3502019-05-17 11:03:51 -0700194 std::unique_ptr<VerificationInterface> verification;
Patrick Venture74059d62019-05-17 10:40:26 -0700195
Patrick Venturec02849b2018-11-06 17:31:15 -0800196 /** Temporary variable to track whether a blob is open. */
197 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700198};
199
200} // namespace blobs