blob: 30b450b510a045737f99de3b984abc0446172f13 [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{
21 /** Pointer to the correct Data handler interface. (nullptr on BT (or KCS))
22 */
23 DataInterface* dataHandler;
24
25 /** Pointer to the correct image handler interface. (nullptr on hash
26 * blob_id) */
27 ImageHandlerInterface* imageHandler;
Patrick Venture18235e62018-11-08 10:21:09 -080028
29 /** The flags used to open the session. */
30 std::uint16_t flags;
Patrick Venture32ba9dd2018-11-09 16:22:53 -080031
32 /** A sesion can be for an image (or tarball) or the hash. */
33 enum State
34 {
35 open = 0,
36 closed = 1,
37 };
38
39 /** The current state of this session. */
40 State state;
Patrick Venture2d3a2542018-11-08 09:23:24 -080041};
42
Patrick Venture18235e62018-11-08 10:21:09 -080043struct ExtChunkHdr
44{
45 std::uint32_t length; /* Length of the data queued (little endian). */
46} __attribute__((packed));
47
Patrick Venture2d3a2542018-11-08 09:23:24 -080048/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070049 * Register only one firmware blob handler that will manage all sessions.
50 */
51class FirmwareBlobHandler : public GenericBlobInterface
52{
53 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080054 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080055 {
Patrick Venture9158dcf2018-11-08 09:44:28 -080056 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080057 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
58 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
59 };
60
Patrick Venture92106df2018-11-09 09:26:30 -080061 /** The state of the firmware update process. */
62 enum UpdateState
63 {
64 /** The initial state. */
65 notYetStarted = 0,
66 /**
67 * The upload process has started, but verification has not started.
68 */
69 uploadInProgress = 1,
70 /** The verification process has started, no more writes allowed. */
71 verificationStarted = 2,
72 /** The verification process has completed. */
73 verificationCompleted = 3,
74 };
75
Patrick Venture79c08bf2018-11-09 15:33:54 -080076 /** The return values for verification. */
77 enum VerifyCheckResponses : std::uint8_t
78 {
79 running = 0,
80 success = 1,
81 failed = 2,
82 other = 3,
83 };
84
Patrick Venture137ad2c2018-11-06 11:30:43 -080085 /**
86 * Create a FirmwareBlobHandler.
87 *
Patrick Venture4cceb8e2018-11-06 11:56:48 -080088 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -080089 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -080090 */
Patrick Venture1cde5f92018-11-07 08:26:47 -080091 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
92 const std::vector<HandlerPack>& firmwares,
93 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -080094
Patrick Venture137ad2c2018-11-06 11:30:43 -080095 /**
96 * Create a FirmwareBlobHandler.
97 *
Patrick Venture1cde5f92018-11-07 08:26:47 -080098 * @param[in] firmwares - list of firmware types and their handlers
99 * @param[in] blobs - list of blobs_ids to support
100 * @param[in] transports - list of transport types and their handlers
101 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -0800102 */
Patrick Venturea78e39f2018-11-06 18:37:06 -0800103 FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares,
104 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -0800105 const std::vector<DataHandlerPack>& transports,
106 std::uint16_t bitmask) :
Patrick Venturea78e39f2018-11-06 18:37:06 -0800107 handlers(firmwares),
Patrick Ventured9fb6132018-11-08 09:56:10 -0800108 blobIDs(blobs), transports(transports), bitmask(bitmask), activeImage(),
Patrick Venture92106df2018-11-09 09:26:30 -0800109 activeHash(), lookup(), state(UpdateState::notYetStarted)
Patrick Venture148cd652018-11-06 10:59:47 -0800110 {
111 }
Patrick Venturec7ca2912018-11-02 11:38:33 -0700112 ~FirmwareBlobHandler() = default;
113 FirmwareBlobHandler(const FirmwareBlobHandler&) = default;
114 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = default;
115 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
116 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
117
118 bool canHandleBlob(const std::string& path) override;
119 std::vector<std::string> getBlobIds() override;
120 bool deleteBlob(const std::string& path) override;
121 bool stat(const std::string& path, struct BlobMeta* meta) override;
122 bool open(uint16_t session, uint16_t flags,
123 const std::string& path) override;
124 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
125 uint32_t requestedSize) override;
126 bool write(uint16_t session, uint32_t offset,
127 const std::vector<uint8_t>& data) override;
128 bool writeMeta(uint16_t session, uint32_t offset,
129 const std::vector<uint8_t>& data) override;
130 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
131 bool close(uint16_t session) override;
132 bool stat(uint16_t session, struct BlobMeta* meta) override;
133 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800134
Patrick Venture21be45a2018-11-06 12:08:52 -0800135 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -0800136 static const std::string activeImageBlobID;
137 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800138
Patrick Venture92106df2018-11-09 09:26:30 -0800139 /** Allow grabbing the current state. */
140 UpdateState getCurrentState() const
141 {
142 return state;
143 };
144
Patrick Venture148cd652018-11-06 10:59:47 -0800145 private:
Patrick Venturea78e39f2018-11-06 18:37:06 -0800146 /** List of handlers by type. */
147 std::vector<HandlerPack> handlers;
148
Patrick Venturec02849b2018-11-06 17:31:15 -0800149 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800150 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800151
Patrick Venture1cde5f92018-11-07 08:26:47 -0800152 /** List of handlers by transport type. */
153 std::vector<DataHandlerPack> transports;
154
Patrick Venturec02849b2018-11-06 17:31:15 -0800155 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800156 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800157
Patrick Ventured9fb6132018-11-08 09:56:10 -0800158 /** Active image session. */
159 Session activeImage;
160
161 /** Active hash session. */
162 Session activeHash;
163
164 /** A quick method for looking up a session's mechanisms and details. */
165 std::map<std::uint16_t, Session*> lookup;
166
Patrick Venture92106df2018-11-09 09:26:30 -0800167 /** The firmware update state. */
168 UpdateState state;
169
Patrick Venturec02849b2018-11-06 17:31:15 -0800170 /** Temporary variable to track whether a blob is open. */
171 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700172};
173
174} // namespace blobs