xref: /openbmc/phosphor-ipmi-flash/tools/io.hpp (revision d6b337e6)
1 #pragma once
2 
3 #include "internal/sys.hpp"
4 #include "io_interface.hpp"
5 
6 #include <cstdint>
7 #include <string>
8 
9 namespace host_tool
10 {
11 
12 class DevMemDevice : public HostIoInterface
13 {
14   public:
DevMemDevice(const internal::Sys * sys=& internal::sys_impl)15     explicit DevMemDevice(const internal::Sys* sys = &internal::sys_impl) :
16         sys(sys)
17     {}
18 
19     ~DevMemDevice() = default;
20 
21     /* Don't allow copying, assignment or move assignment, only moving. */
22     DevMemDevice(const DevMemDevice&) = delete;
23     DevMemDevice& operator=(const DevMemDevice&) = delete;
24     DevMemDevice(DevMemDevice&&) = default;
25     DevMemDevice& operator=(DevMemDevice&&) = delete;
26 
27     bool read(const std::size_t offset, const std::size_t length,
28               void* const destination) override;
29 
30     bool write(const std::size_t offset, const std::size_t length,
31                const void* const source) override;
32 
33   private:
34     static const std::string devMemPath;
35     int devMemFd = -1;
36     void* devMemMapped = nullptr;
37     const internal::Sys* sys;
38 };
39 
40 class PpcMemDevice : public HostIoInterface
41 {
42   public:
PpcMemDevice(const std::string & ppcMemPath,const internal::Sys * sys=& internal::sys_impl)43     explicit PpcMemDevice(const std::string& ppcMemPath,
44                           const internal::Sys* sys = &internal::sys_impl) :
45         ppcMemPath(ppcMemPath),
46         sys(sys)
47     {}
48 
49     ~PpcMemDevice() override;
50 
51     /* Don't allow copying or assignment, only moving. */
52     PpcMemDevice(const PpcMemDevice&) = delete;
53     PpcMemDevice& operator=(const PpcMemDevice&) = delete;
54     PpcMemDevice(PpcMemDevice&&) = default;
55     PpcMemDevice& operator=(PpcMemDevice&&) = default;
56 
57     bool read(const std::size_t offset, const std::size_t length,
58               void* const destination) override;
59 
60     bool write(const std::size_t offset, const std::size_t length,
61                const void* const source) override;
62 
63   private:
64     void close();
65 
66     int ppcMemFd = -1;
67     const std::string ppcMemPath;
68     const internal::Sys* sys;
69 };
70 
71 } // namespace host_tool
72