1 #pragma once
2 
3 #include <cstdint>
4 #include <fstream>
5 #include <memory>
6 #include <optional>
7 #include <string>
8 #include <vector>
9 
10 namespace ipmi_flash
11 {
12 
13 /**
14  * Each image update mechanism must implement the ImageHandlerInterface.
15  */
16 class ImageHandlerInterface
17 {
18   public:
19     virtual ~ImageHandlerInterface() = default;
20 
21     /**
22      * open the firmware update mechanism.
23      *
24      * @param[in] path - the path passed to the handler (the blob_id).
25      * @return bool - returns true on success.
26      */
27     virtual bool open(const std::string& path,
28                       std::ios_base::openmode mode) = 0;
29 
30     /**
31      * close the image.
32      */
33     virtual void close() = 0;
34 
35     /**
36      * write data to the staged file.
37      *
38      * @param[in] offset - 0-based offset into the file.
39      * @param[in] data - the data to write.
40      * @return bool - returns true on success.
41      */
42     virtual bool write(std::uint32_t offset,
43                        const std::vector<std::uint8_t>& data) = 0;
44     /**
45      * read data from a file.
46      *
47      * @param[in] offset - 0-based offset into the file.
48      * @param[in] size - the number of bytes
49      * @return std::optional<std::vector<std::uint8_t>> - returns std::nullopt
50      * on failure otherwise returns a vector filled with the bytes read.
51      *
52      */
53     virtual std::optional<std::vector<std::uint8_t>>
54         read(std::uint32_t offset, std::uint32_t size) = 0;
55 
56     /**
57      * return the size of the file (if that notion makes sense).
58      *
59      * @return the size in bytes of the image staged.
60      */
61     virtual int getSize() = 0;
62 };
63 
64 class HandlerPack
65 {
66   public:
HandlerPack(const std::string & name,std::unique_ptr<ImageHandlerInterface> handler)67     HandlerPack(const std::string& name,
68                 std::unique_ptr<ImageHandlerInterface> handler) :
69         blobName(name),
70         handler(std::move(handler))
71     {}
72 
73     HandlerPack() = default;
74     ~HandlerPack() = default;
75     HandlerPack(const HandlerPack&) = delete;
76     HandlerPack& operator=(const HandlerPack&) = delete;
77     HandlerPack(HandlerPack&&) = default;
78     HandlerPack& operator=(HandlerPack&&) = default;
79 
80     std::string blobName;
81     std::unique_ptr<ImageHandlerInterface> handler;
82 };
83 
84 } // namespace ipmi_flash
85