blob: 0c3092bae5a8190cef498dd38a3869a97235134e [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;
28};
29
30/**
Patrick Venturec7ca2912018-11-02 11:38:33 -070031 * Register only one firmware blob handler that will manage all sessions.
32 */
33class FirmwareBlobHandler : public GenericBlobInterface
34{
35 public:
Patrick Venturefc3857b2018-11-07 08:14:55 -080036 enum FirmwareUpdateFlags : std::uint16_t
37 {
Patrick Venture9158dcf2018-11-08 09:44:28 -080038 ipmi = (1 << 8), /* Expect to send contents over IPMI BlockTransfer. */
Patrick Venturefc3857b2018-11-07 08:14:55 -080039 p2a = (1 << 9), /* Expect to send contents over P2A bridge. */
40 lpc = (1 << 10), /* Expect to send contents over LPC bridge. */
41 };
42
Patrick Venture137ad2c2018-11-06 11:30:43 -080043 /**
44 * Create a FirmwareBlobHandler.
45 *
Patrick Venture4cceb8e2018-11-06 11:56:48 -080046 * @param[in] firmwares - list of firmware blob_ids to support.
Patrick Venture1cde5f92018-11-07 08:26:47 -080047 * @param[in] transports - list of transports to support.
Patrick Venture137ad2c2018-11-06 11:30:43 -080048 */
Patrick Venture1cde5f92018-11-07 08:26:47 -080049 static std::unique_ptr<GenericBlobInterface> CreateFirmwareBlobHandler(
50 const std::vector<HandlerPack>& firmwares,
51 const std::vector<DataHandlerPack>& transports);
Patrick Venture68cf64f2018-11-06 10:46:51 -080052
Patrick Venture137ad2c2018-11-06 11:30:43 -080053 /**
54 * Create a FirmwareBlobHandler.
55 *
Patrick Venture1cde5f92018-11-07 08:26:47 -080056 * @param[in] firmwares - list of firmware types and their handlers
57 * @param[in] blobs - list of blobs_ids to support
58 * @param[in] transports - list of transport types and their handlers
59 * @param[in] bitmask - bitmask of transports to support
Patrick Venture137ad2c2018-11-06 11:30:43 -080060 */
Patrick Venturea78e39f2018-11-06 18:37:06 -080061 FirmwareBlobHandler(const std::vector<HandlerPack>& firmwares,
62 const std::vector<std::string>& blobs,
Patrick Venture1cde5f92018-11-07 08:26:47 -080063 const std::vector<DataHandlerPack>& transports,
64 std::uint16_t bitmask) :
Patrick Venturea78e39f2018-11-06 18:37:06 -080065 handlers(firmwares),
Patrick Ventured9fb6132018-11-08 09:56:10 -080066 blobIDs(blobs), transports(transports), bitmask(bitmask), activeImage(),
67 activeHash(), lookup()
Patrick Venture148cd652018-11-06 10:59:47 -080068 {
69 }
Patrick Venturec7ca2912018-11-02 11:38:33 -070070 ~FirmwareBlobHandler() = default;
71 FirmwareBlobHandler(const FirmwareBlobHandler&) = default;
72 FirmwareBlobHandler& operator=(const FirmwareBlobHandler&) = default;
73 FirmwareBlobHandler(FirmwareBlobHandler&&) = default;
74 FirmwareBlobHandler& operator=(FirmwareBlobHandler&&) = default;
75
76 bool canHandleBlob(const std::string& path) override;
77 std::vector<std::string> getBlobIds() override;
78 bool deleteBlob(const std::string& path) override;
79 bool stat(const std::string& path, struct BlobMeta* meta) override;
80 bool open(uint16_t session, uint16_t flags,
81 const std::string& path) override;
82 std::vector<uint8_t> read(uint16_t session, uint32_t offset,
83 uint32_t requestedSize) override;
84 bool write(uint16_t session, uint32_t offset,
85 const std::vector<uint8_t>& data) override;
86 bool writeMeta(uint16_t session, uint32_t offset,
87 const std::vector<uint8_t>& data) override;
88 bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
89 bool close(uint16_t session) override;
90 bool stat(uint16_t session, struct BlobMeta* meta) override;
91 bool expire(uint16_t session) override;
Patrick Venture148cd652018-11-06 10:59:47 -080092
Patrick Venture21be45a2018-11-06 12:08:52 -080093 static const std::string hashBlobID;
Patrick Venture7b9256f2018-11-06 15:06:04 -080094 static const std::string activeImageBlobID;
95 static const std::string activeHashBlobID;
Patrick Venture21be45a2018-11-06 12:08:52 -080096
Patrick Venture148cd652018-11-06 10:59:47 -080097 private:
Patrick Venturea78e39f2018-11-06 18:37:06 -080098 /** List of handlers by type. */
99 std::vector<HandlerPack> handlers;
100
Patrick Venturec02849b2018-11-06 17:31:15 -0800101 /** Active list of blobIDs. */
Patrick Venture4cceb8e2018-11-06 11:56:48 -0800102 std::vector<std::string> blobIDs;
Patrick Venturec02849b2018-11-06 17:31:15 -0800103
Patrick Venture1cde5f92018-11-07 08:26:47 -0800104 /** List of handlers by transport type. */
105 std::vector<DataHandlerPack> transports;
106
Patrick Venturec02849b2018-11-06 17:31:15 -0800107 /** The bits set indicate what transport mechanisms are supported. */
Patrick Venture1cde5f92018-11-07 08:26:47 -0800108 std::uint16_t bitmask;
Patrick Venturec02849b2018-11-06 17:31:15 -0800109
Patrick Ventured9fb6132018-11-08 09:56:10 -0800110 /** Active image session. */
111 Session activeImage;
112
113 /** Active hash session. */
114 Session activeHash;
115
116 /** A quick method for looking up a session's mechanisms and details. */
117 std::map<std::uint16_t, Session*> lookup;
118
Patrick Venturec02849b2018-11-06 17:31:15 -0800119 /** Temporary variable to track whether a blob is open. */
120 bool fileOpen = false;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700121};
122
123} // namespace blobs