Patrick Venture | 25528b4 | 2018-11-06 18:29:43 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 400c636 | 2018-11-06 20:06:11 -0800 | [diff] [blame] | 3 | #include <cstdint> |
Jason Ling | ded66d0 | 2020-10-23 14:13:03 -0700 | [diff] [blame] | 4 | #include <fstream> |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 5 | #include <memory> |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 6 | #include <optional> |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 7 | #include <string> |
Patrick Venture | 400c636 | 2018-11-06 20:06:11 -0800 | [diff] [blame] | 8 | #include <vector> |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 9 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 10 | namespace ipmi_flash |
Patrick Venture | 25528b4 | 2018-11-06 18:29:43 -0800 | [diff] [blame] | 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 | * |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 24 | * @param[in] path - the path passed to the handler (the blob_id). |
Patrick Venture | 25528b4 | 2018-11-06 18:29:43 -0800 | [diff] [blame] | 25 | * @return bool - returns true on success. |
| 26 | */ |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 27 | virtual bool open(const std::string& path, |
| 28 | std::ios_base::openmode mode) = 0; |
Patrick Venture | 400c636 | 2018-11-06 20:06:11 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
Patrick Venture | 68bb143 | 2018-11-09 20:08:48 -0800 | [diff] [blame] | 31 | * close the image. |
| 32 | */ |
| 33 | virtual void close() = 0; |
| 34 | |
| 35 | /** |
Patrick Venture | 400c636 | 2018-11-06 20:06:11 -0800 | [diff] [blame] | 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; |
Jason Ling | 56a2273 | 2020-10-23 19:53:17 -0700 | [diff] [blame] | 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; |
Patrick Venture | cc7d160 | 2018-11-15 13:58:33 -0800 | [diff] [blame] | 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; |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 64 | class HandlerPack |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 65 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 66 | public: |
| 67 | HandlerPack(const std::string& name, |
| 68 | std::unique_ptr<ImageHandlerInterface> handler) : |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 69 | blobName(name), handler(std::move(handler)) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 70 | {} |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 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 | |
Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 79 | std::string blobName; |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 80 | std::unique_ptr<ImageHandlerInterface> handler; |
Patrick Venture | 25528b4 | 2018-11-06 18:29:43 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 83 | } // namespace ipmi_flash |