blob: c87e635284b1cb567b7b493abfbc632e9b4af1d0 [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 Venture148cd652018-11-06 10:59:47 -080010#include <string>
11#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070012
13namespace blobs
14{
15
Patrick Venturec7ca2912018-11-02 11:38:33 -070016/**
Patrick Venture2d3a2542018-11-08 09:23:24 -080017 * Representation of a session, includes how to read/write data.
18 */
19struct Session
20{
Patrick Ventured6461d62018-11-09 17:30:25 -080021 /**
Patrick Ventureedd55b52018-11-15 11:05:13 -080022 * Built a session object.
23 *
24 * @param[in] the active path to which this corresponds.
25 */
26 explicit Session(const std::string& path) :
27 dataHandler(nullptr), imageHandler(nullptr), flags(0),
28 state(State::closed), activePath(path)
29 {
30 }
31
32 /**
Patrick Ventured6461d62018-11-09 17:30:25 -080033 * Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
Patrick Venture2d3a2542018-11-08 09:23:24 -080034 */
35 DataInterface* dataHandler;
36
Patrick Ventured6461d62018-11-09 17:30:25 -080037 /**
38 * Pointer to the correct image handler interface. (nullptr on hash
39 * blob_id)
40 */
Patrick Venture2d3a2542018-11-08 09:23:24 -080041 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080042
43 /** The flags used to open the session. */
44 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080045
46 /** A sesion can be for an image (or tarball) or the hash. */
47 enum State
48 {
49 open = 0,
50 closed = 1,
51 };
52
53 /** The current state of this session. */
54 State state;
Patrick Ventureedd55b52018-11-15 11:05:13 -080055
56 /** The active path. */
57 std::string activePath;
Patrick Venture2d3a2542018-11-08 09:23:24 -080058};
59
Patrick Venture18235e62018-11-08 10:21:09 -080060struct ExtChunkHdr
61{
62 std::uint32_t length; /* Length of the data queued (little endian). */
63} __attribute__((packed));
64
Patrick Venture2d3a2542018-11-08 09:23:24 -080065/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070066 * Register only one firmware blob handler that will manage all sessions.
67 */
68class FirmwareBlobHandler : public GenericBlobInterface
69{
70 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080071 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080072 {
Patrick Venture9158dcf2018-11-08 09:44:28 -080073 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080074 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
75 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
76 };
77
Patrick Venture92106df2018-11-09 09:26:30 -080078 /** The state of the firmware update process. */
79 enum UpdateState
80 {
81 /** The initial state. */
82 notYetStarted = 0,
83 /**
84 * The upload process has started, but verification has not started.
85 */
86 uploadInProgress = 1,
87 /** The verification process has started, no more writes allowed. */
88 verificationStarted = 2,
89 /** The verification process has completed. */
90 verificationCompleted = 3,
91 };
92
Patrick Venture79c08bf2018-11-09 15:33:54 -080093 /** The return values for verification. */
94 enum VerifyCheckResponses : std::uint8_t
95 {
96 running = 0,
97 success = 1,
98 failed = 2,
99 other = 3,
100 };
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 Venture137ad2c2018-11-06 11:30:43 -0800107 */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800108 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
109 const std::vector<HandlerPack>& firmwares,
110 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -0800111
Patrick Venture137ad2c2018-11-06 11:30:43 -0800112 /**
113 * Create a FirmwareBlobHandler.
114 *
Patrick Venture1cde5f92018-11-07 08:26:47 -0800115 * @param[in] firmwares - list of firmware types and their handlers
116 * @param[in] blobs - list of blobs_ids to support
117 * @param[in] transports - list of transport types and their handlers
118 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -0800119 */
Patrick Venturea78e39f2018-11-06 18:37:06 -0800120 FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares,
121 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800122 const std::vector<DataHandlerPack>& transports,
123 std::uint16_t bitmask) :
Patrick Venturea78e39f2018-11-06 18:37:06 -0800124 handlers(firmwares),
Patrick Ventureedd55b52018-11-15 11:05:13 -0800125 blobIDs(blobs), transports(transports), bitmask(bitmask),
126 activeImage(activeImageBlobID), activeHash(activeHashBlobID), lookup(),
127 state(UpdateState::notYetStarted)
Patrick Venture148cd652018-11-06 10:59:47 -0800128 {
129 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700130 ~FirmwareBlobHandler() = default;
131 FirmwareBlobHandler(const FirmwareBlobHandler&) = default;
132 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = default;
133 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
134 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
135
136 bool canHandleBlob(const std::string& path) override;
137 std::vector<std::string> getBlobIds() override;
138 bool deleteBlob(const std::string& path) override;
139 bool stat(const std::string& path, struct BlobMeta* meta) override;
140 bool open(uint16_t session, uint16_t flags,
141 const std::string& path) override;
142 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
143 uint32_t requestedSize) override;
144 bool write(uint16_t session, uint32_t offset,
145 const std::vector<uint8_t>& data) override;
146 bool writeMeta(uint16_t session, uint32_t offset,
147 const std::vector<uint8_t>& data) override;
148 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
149 bool close(uint16_t session) override;
150 bool stat(uint16_t session, struct BlobMeta* meta) override;
151 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800152
Patrick Venture21be45a2018-11-06 12:08:52 -0800153 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -0800154 static const std::string activeImageBlobID;
155 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800156
Patrick Venture92106df2018-11-09 09:26:30 -0800157 /** Allow grabbing the current state. */
158 UpdateState getCurrentState() const
159 {
160 return state;
161 };
162
Patrick Venture148cd652018-11-06 10:59:47 -0800163 private:
Patrick Venturea78e39f2018-11-06 18:37:06 -0800164 /** List of handlers by type. */
165 std::vector<HandlerPack> handlers;
166
Patrick Venturec02849b2018-11-06 17:31:15 -0800167 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800168 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800169
Patrick Venture1cde5f92018-11-07 08:26:47 -0800170 /** List of handlers by transport type. */
171 std::vector<DataHandlerPack> transports;
172
Patrick Venturec02849b2018-11-06 17:31:15 -0800173 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800174 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800175
Patrick Ventured9fb6132018-11-08 09:56:10 -0800176 /** Active image session. */
177 Session activeImage;
178
179 /** Active hash session. */
180 Session activeHash;
181
182 /** A quick method for looking up a session's mechanisms and details. */
183 std::map<std::uint16_t, Session*> lookup;
184
Patrick Venture92106df2018-11-09 09:26:30 -0800185 /** The firmware update state. */
186 UpdateState state;
187
Patrick Venturec02849b2018-11-06 17:31:15 -0800188 /** Temporary variable to track whether a blob is open. */
189 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700190};
191
192} // namespace blobs