xref: /openbmc/phosphor-ipmi-flash/bmc/image_handler.hpp (revision 3d73712fb5f8d411cb8a6fb207df60c5e17afe15)
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>> read(
54         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), handler(std::move(handler))
70     {}
71 
72     HandlerPack() = default;
73     ~HandlerPack() = default;
74     HandlerPack(const HandlerPack&) = delete;
75     HandlerPack& operator=(const HandlerPack&) = delete;
76     HandlerPack(HandlerPack&&) = default;
77     HandlerPack& operator=(HandlerPack&&) = default;
78 
79     std::string blobName;
80     std::unique_ptr<ImageHandlerInterface> handler;
81 };
82 
83 } // namespace ipmi_flash
84