1 #pragma once
2 
3 #include <blobs-ipmid/blobs.hpp>
4 
5 #include <cstdint>
6 #include <memory>
7 #include <string>
8 #include <vector>
9 
10 namespace blobs
11 {
12 
13 class SmbiosBlobHandler : public GenericBlobInterface
14 {
15   public:
16     SmbiosBlobHandler() = default;
17     ~SmbiosBlobHandler() = default;
18     SmbiosBlobHandler(const SmbiosBlobHandler&) = delete;
19     SmbiosBlobHandler& operator=(const SmbiosBlobHandler&) = delete;
20     SmbiosBlobHandler(SmbiosBlobHandler&&) = default;
21     SmbiosBlobHandler& operator=(SmbiosBlobHandler&&) = default;
22 
23     struct SmbiosBlob
24     {
25         SmbiosBlob(uint16_t id, const std::string& path, uint16_t flags) :
26             sessionId(id), blobId(path), state(0)
27         {
28             if (flags & blobs::OpenFlags::write)
29             {
30                 state |= blobs::StateFlags::open_write;
31             }
32 
33             /* Pre-allocate the buffer.capacity() with maxBufferSize */
34             buffer.reserve(maxBufferSize);
35         }
36 
37         /* The blob handler session id. */
38         uint16_t sessionId;
39 
40         /* The identifier for the blob */
41         std::string blobId;
42 
43         /* The current state. */
44         uint16_t state;
45 
46         /* The staging buffer. */
47         std::vector<uint8_t> buffer;
48     };
49 
50     bool canHandleBlob(const std::string& path) override;
51     std::vector<std::string> getBlobIds() override;
52     bool deleteBlob(const std::string& path) override;
53     bool stat(const std::string& path, struct BlobMeta* meta) override;
54     bool open(uint16_t session, uint16_t flags,
55               const std::string& path) override;
56     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
57                               uint32_t requestedSize) override;
58     bool write(uint16_t session, uint32_t offset,
59                const std::vector<uint8_t>& data) override;
60     bool writeMeta(uint16_t session, uint32_t offset,
61                    const std::vector<uint8_t>& data) override;
62     bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
63     bool close(uint16_t session) override;
64     bool stat(uint16_t session, struct BlobMeta* meta) override;
65     bool expire(uint16_t session) override;
66 
67   private:
68     static constexpr char blobId[] = "/smbios";
69 
70     /* SMBIOS table storage size */
71     static constexpr uint32_t maxBufferSize = 64 * 1024;
72 
73     /* The handler only allows one open blob. */
74     std::unique_ptr<SmbiosBlob> blobPtr = nullptr;
75 };
76 
77 } // namespace blobs
78