blob: 59944c63dbe2c1f29bdd903f4a368441ad293d64 [file] [log] [blame]
Patrick Venture25528b42018-11-06 18:29:43 -08001#pragma once
2
Patrick Venture400c6362018-11-06 20:06:11 -08003#include <cstdint>
Jason Lingded66d02020-10-23 14:13:03 -07004#include <fstream>
Patrick Venturea78e39f2018-11-06 18:37:06 -08005#include <memory>
Jason Ling56a22732020-10-23 19:53:17 -07006#include <optional>
Patrick Venturea78e39f2018-11-06 18:37:06 -08007#include <string>
Patrick Venture400c6362018-11-06 20:06:11 -08008#include <vector>
Patrick Venturea78e39f2018-11-06 18:37:06 -08009
Patrick Venture1d5a31c2019-05-20 11:38:22 -070010namespace ipmi_flash
Patrick Venture25528b42018-11-06 18:29:43 -080011{
12
13/**
14 * Each image update mechanism must implement the ImageHandlerInterface.
15 */
16class ImageHandlerInterface
17{
18 public:
19 virtual ~ImageHandlerInterface() = default;
20
21 /**
22 * open the firmware update mechanism.
23 *
Patrick Venturea78e39f2018-11-06 18:37:06 -080024 * @param[in] path - the path passed to the handler (the blob_id).
Patrick Venture25528b42018-11-06 18:29:43 -080025 * @return bool - returns true on success.
26 */
Jason Ling56a22732020-10-23 19:53:17 -070027 virtual bool open(const std::string& path,
28 std::ios_base::openmode mode) = 0;
Patrick Venture400c6362018-11-06 20:06:11 -080029
30 /**
Patrick Venture68bb1432018-11-09 20:08:48 -080031 * close the image.
32 */
33 virtual void close() = 0;
34
35 /**
Patrick Venture400c6362018-11-06 20:06:11 -080036 * 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;
Jason Ling56a22732020-10-23 19:53:17 -070044 /**
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;
Patrick Venturecc7d1602018-11-15 13:58:33 -080055
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;
Patrick Venturea78e39f2018-11-06 18:37:06 -080062};
63
Patrick Ventured4e20de2019-07-18 12:48:05 -070064class HandlerPack
Patrick Venturea78e39f2018-11-06 18:37:06 -080065{
Patrick Ventured4e20de2019-07-18 12:48:05 -070066 public:
67 HandlerPack(const std::string& name,
68 std::unique_ptr<ImageHandlerInterface> handler) :
Patrick Williams42a44c22024-08-16 15:21:32 -040069 blobName(name), handler(std::move(handler))
Patrick Venture9b37b092020-05-28 20:58:57 -070070 {}
Patrick Ventured4e20de2019-07-18 12:48:05 -070071
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
Patrick Venturea78e39f2018-11-06 18:37:06 -080079 std::string blobName;
Patrick Ventured4e20de2019-07-18 12:48:05 -070080 std::unique_ptr<ImageHandlerInterface> handler;
Patrick Venture25528b42018-11-06 18:29:43 -080081};
82
Patrick Venture1d5a31c2019-05-20 11:38:22 -070083} // namespace ipmi_flash