xref: /openbmc/phosphor-ipmi-flash/bmc/version-handler/version_handler.hpp (revision 6ee93c6d0add031e38fdbca093d9458549ca2e73)
1 #pragma once
2 #include "buildjson.hpp"
3 #include "image_handler.hpp"
4 #include "status.hpp"
5 #include "util.hpp"
6 
7 #include <blobs-ipmid/blobs.hpp>
8 
9 #include <algorithm>
10 #include <cstdint>
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16 namespace ipmi_flash
17 {
18 struct VersionActionPack
19 {
20   public:
21     VersionActionPack(std::unique_ptr<TriggerableActionInterface> openAction) :
22         onOpen(std::move(openAction)){};
23     VersionActionPack() = default;
24     /** Only file operation action supported currently */
25     std::unique_ptr<TriggerableActionInterface> onOpen;
26 };
27 
28 /*
29  * All the information associated with a version blob
30  */
31 struct VersionInfoPack
32 {
33   public:
34     VersionInfoPack(const std::string& blobId,
35                     std::unique_ptr<VersionActionPack> actionPackIn,
36                     std::unique_ptr<ImageHandlerInterface> imageHandler) :
37         blobId(blobId),
38         actionPack(std::move(actionPackIn)),
39         imageHandler(std::move(imageHandler)),
40         blobState(static_cast<blobs::StateFlags>(0)){};
41     VersionInfoPack() = default;
42 
43     std::string blobId;
44     std::unique_ptr<VersionActionPack> actionPack;
45     std::unique_ptr<ImageHandlerInterface> imageHandler;
46     blobs::StateFlags blobState;
47 };
48 
49 /* convenience alias: the key (std::string) is blobId, same as
50  * VersionInfoPack.blobId */
51 using VersionInfoMap = std::unordered_map<std::string, VersionInfoPack>;
52 
53 class VersionBlobHandler : public blobs::GenericBlobInterface
54 {
55   public:
56     /**
57      * Factory to create a BlobHandler for use by phosphor-ipmi-blobs
58      *
59      * @param[in] versionMap - blob names to VersionInfoPack which contains
60      * triggers, file handlers, state and blobID.
61      *
62      * @returns a unique_ptr to a GenericBlobInterface which is used by
63      * phosphor-ipmi-blobs. The underlying implementation (VersionBlobHandler)
64      * implements all the blob operations for the blobs owned by
65      * VersionBlobHandler.
66      */
67     static std::unique_ptr<blobs::GenericBlobInterface>
68         create(VersionInfoMap&& versionMap);
69     /**
70      * Create a VersionBlobHandler.
71      *
72      * @param[in] blobs - list of blobs_ids to support
73      * @param[in] actions - a map of blobId to VersionInfoPack
74      */
75     VersionBlobHandler(std::vector<std::string>&& blobs,
76                        VersionInfoMap&& handlerMap) :
77         blobIds(blobs),
78         versionInfoMap(std::move(handlerMap))
79     {}
80     ~VersionBlobHandler() = default;
81     VersionBlobHandler(const VersionBlobHandler&) = delete;
82     VersionBlobHandler& operator=(const VersionBlobHandler&) = delete;
83     VersionBlobHandler(VersionBlobHandler&&) = default;
84     VersionBlobHandler& operator=(VersionBlobHandler&&) = default;
85 
86     bool canHandleBlob(const std::string& path) override;
87     std::vector<std::string> getBlobIds() override;
88     bool deleteBlob(const std::string& path) override;
89     bool stat(const std::string&, blobs::BlobMeta* meta) override;
90     bool open(uint16_t session, uint16_t flags,
91               const std::string& path) override;
92     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
93                               uint32_t requestedSize) override;
94     bool write(uint16_t session, uint32_t offset,
95                const std::vector<uint8_t>& data) override
96     {
97         return false; /* not supported */
98     };
99     bool writeMeta(uint16_t session, uint32_t offset,
100                    const std::vector<uint8_t>& data) override
101     {
102         return false; /* not supported */
103     }
104     bool commit(uint16_t session, const std::vector<uint8_t>& data) override
105     {
106         return false; // not supported
107     }
108     bool close(uint16_t session) override;
109     bool stat(uint16_t session, blobs::BlobMeta* meta) override;
110     bool expire(uint16_t session) override;
111     bool cleanup(uint16_t session);
112 
113   private:
114     std::vector<std::string> blobIds;
115     VersionInfoMap versionInfoMap;
116     std::unordered_map<uint16_t, std::string> sessionToBlob;
117 };
118 } // namespace ipmi_flash
119