blob: e1c3f85053aada0f2919ff27bed14f52150696f5 [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 Venture2d3a2542018-11-08 09:23:24 -080031};
32
Patrick Venture18235e62018-11-08 10:21:09 -080033struct ExtChunkHdr
34{
35 std::uint32_t length; /* Length of the data queued (little endian). */
36} __attribute__((packed));
37
Patrick Venture2d3a2542018-11-08 09:23:24 -080038/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070039 * Register only one firmware blob handler that will manage all sessions.
40 */
41class FirmwareBlobHandler : public GenericBlobInterface
42{
43 public:
Patrick Venture05abf7e2018-11-09 11:02:56 -080044 enum UpdateFlags : std::uint16_t
Patrick Venturefc3857b2018-11-07 08:14:55 -080045 {
Patrick Venture9158dcf2018-11-08 09:44:28 -080046 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080047 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
48 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
49 };
50
Patrick Venture92106df2018-11-09 09:26:30 -080051 /** The state of the firmware update process. */
52 enum UpdateState
53 {
54 /** The initial state. */
55 notYetStarted = 0,
56 /**
57 * The upload process has started, but verification has not started.
58 */
59 uploadInProgress = 1,
60 /** The verification process has started, no more writes allowed. */
61 verificationStarted = 2,
62 /** The verification process has completed. */
63 verificationCompleted = 3,
64 };
65
Patrick Venture137ad2c2018-11-06 11:30:43 -080066 /**
67 * Create a FirmwareBlobHandler.
68 *
Patrick Venture4cceb8e2018-11-06 11:56:48 -080069 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -080070 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -080071 */
Patrick Venture1cde5f92018-11-07 08:26:47 -080072 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
73 const std::vector<HandlerPack>& firmwares,
74 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -080075
Patrick Venture137ad2c2018-11-06 11:30:43 -080076 /**
77 * Create a FirmwareBlobHandler.
78 *
Patrick Venture1cde5f92018-11-07 08:26:47 -080079 * @param[in] firmwares - list of firmware types and their handlers
80 * @param[in] blobs - list of blobs_ids to support
81 * @param[in] transports - list of transport types and their handlers
82 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -080083 */
Patrick Venturea78e39f2018-11-06 18:37:06 -080084 FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares,
85 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -080086 const std::vector<DataHandlerPack>& transports,
87 std::uint16_t bitmask) :
Patrick Venturea78e39f2018-11-06 18:37:06 -080088 handlers(firmwares),
Patrick Ventured9fb6132018-11-08 09:56:10 -080089 blobIDs(blobs), transports(transports), bitmask(bitmask), activeImage(),
Patrick Venture92106df2018-11-09 09:26:30 -080090 activeHash(), lookup(), state(UpdateState::notYetStarted)
Patrick Venture148cd652018-11-06 10:59:47 -080091 {
92 }
Patrick Venturec7ca2912018-11-02 11:38:33 -070093 ~FirmwareBlobHandler() = default;
94 FirmwareBlobHandler(const FirmwareBlobHandler&) = default;
95 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = default;
96 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
97 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
98
99 bool canHandleBlob(const std::string& path) override;
100 std::vector<std::string> getBlobIds() override;
101 bool deleteBlob(const std::string& path) override;
102 bool stat(const std::string& path, struct BlobMeta* meta) override;
103 bool open(uint16_t session, uint16_t flags,
104 const std::string& path) override;
105 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
106 uint32_t requestedSize) override;
107 bool write(uint16_t session, uint32_t offset,
108 const std::vector<uint8_t>& data) override;
109 bool writeMeta(uint16_t session, uint32_t offset,
110 const std::vector<uint8_t>& data) override;
111 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
112 bool close(uint16_t session) override;
113 bool stat(uint16_t session, struct BlobMeta* meta) override;
114 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -0800115
Patrick Venture21be45a2018-11-06 12:08:52 -0800116 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -0800117 static const std::string activeImageBlobID;
118 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -0800119
Patrick Venture92106df2018-11-09 09:26:30 -0800120 /** Allow grabbing the current state. */
121 UpdateState getCurrentState() const
122 {
123 return state;
124 };
125
Patrick Venture148cd652018-11-06 10:59:47 -0800126 private:
Patrick Venturea78e39f2018-11-06 18:37:06 -0800127 /** List of handlers by type. */
128 std::vector<HandlerPack> handlers;
129
Patrick Venturec02849b2018-11-06 17:31:15 -0800130 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800131 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800132
Patrick Venture1cde5f92018-11-07 08:26:47 -0800133 /** List of handlers by transport type. */
134 std::vector<DataHandlerPack> transports;
135
Patrick Venturec02849b2018-11-06 17:31:15 -0800136 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800137 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800138
Patrick Ventured9fb6132018-11-08 09:56:10 -0800139 /** Active image session. */
140 Session activeImage;
141
142 /** Active hash session. */
143 Session activeHash;
144
145 /** A quick method for looking up a session's mechanisms and details. */
146 std::map<std::uint16_t, Session*> lookup;
147
Patrick Venture92106df2018-11-09 09:26:30 -0800148 /** The firmware update state. */
149 UpdateState state;
150
Patrick Venturec02849b2018-11-06 17:31:15 -0800151 /** Temporary variable to track whether a blob is open. */
152 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700153};
154
155} // namespace blobs