Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "internal/sys.hpp" |
| 4 | |
| 5 | #include <cstdint> |
| 6 | #include <string> |
| 7 | |
| 8 | namespace host_tool |
| 9 | { |
| 10 | |
| 11 | class HostIoInterface |
| 12 | { |
| 13 | public: |
| 14 | virtual ~HostIoInterface() = default; |
| 15 | |
| 16 | /** |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 17 | * Attempt to read bytes from offset to the destination from the host |
| 18 | * memory device. |
| 19 | * |
| 20 | * @param[in] offset - offset into the host memory device. |
| 21 | * @param[in] length - the number of bytes to copy from source. |
| 22 | * @param[in] destination - where to write the bytes. |
| 23 | * @return true on success, false on failure (such as unable to initialize |
| 24 | * device). |
| 25 | */ |
| 26 | virtual bool read(const std::size_t offset, const std::size_t length, |
| 27 | void* const destination) = 0; |
| 28 | |
| 29 | /** |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 30 | * Attempt to write bytes from source to offset into the host memory device. |
| 31 | * |
| 32 | * @param[in] offset - offset into the host memory device. |
| 33 | * @param[in] length - the number of bytes to copy from source. |
| 34 | * @param[in] source - the souce of the bytes to copy to the memory device. |
| 35 | * @return true on success, false on failure (such as unable to initialize |
| 36 | * device). |
| 37 | */ |
| 38 | virtual bool write(const std::size_t offset, const std::size_t length, |
| 39 | const void* const source) = 0; |
| 40 | }; |
| 41 | |
| 42 | class DevMemDevice : public HostIoInterface |
| 43 | { |
| 44 | public: |
| 45 | explicit DevMemDevice(const internal::Sys* sys = &internal::sys_impl) : |
| 46 | sys(sys) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 47 | {} |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 48 | |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 49 | ~DevMemDevice() = default; |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 50 | |
Brandon Kim | cbf4740 | 2019-11-26 17:27:47 -0800 | [diff] [blame] | 51 | /* Don't allow copying, assignment or move assignment, only moving. */ |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 52 | DevMemDevice(const DevMemDevice&) = delete; |
| 53 | DevMemDevice& operator=(const DevMemDevice&) = delete; |
| 54 | DevMemDevice(DevMemDevice&&) = default; |
Brandon Kim | cbf4740 | 2019-11-26 17:27:47 -0800 | [diff] [blame] | 55 | DevMemDevice& operator=(DevMemDevice&&) = delete; |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 56 | |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 57 | bool read(const std::size_t offset, const std::size_t length, |
| 58 | void* const destination) override; |
| 59 | |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 60 | bool write(const std::size_t offset, const std::size_t length, |
| 61 | const void* const source) override; |
| 62 | |
| 63 | private: |
| 64 | static const std::string devMemPath; |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 65 | int devMemFd = -1; |
| 66 | void* devMemMapped = nullptr; |
| 67 | const internal::Sys* sys; |
| 68 | }; |
| 69 | |
Brandon Kim | 286cc6a | 2019-11-05 16:39:47 -0800 | [diff] [blame] | 70 | class PpcMemDevice : public HostIoInterface |
| 71 | { |
| 72 | public: |
Rui Zhang | d8515a6 | 2020-03-24 16:46:44 -0700 | [diff] [blame] | 73 | explicit PpcMemDevice(const std::string& ppcMemPath, |
Brandon Kim | 286cc6a | 2019-11-05 16:39:47 -0800 | [diff] [blame] | 74 | const internal::Sys* sys = &internal::sys_impl) : |
| 75 | ppcMemPath(ppcMemPath), |
| 76 | sys(sys) |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 77 | {} |
Brandon Kim | 286cc6a | 2019-11-05 16:39:47 -0800 | [diff] [blame] | 78 | |
| 79 | ~PpcMemDevice() override; |
| 80 | |
| 81 | /* Don't allow copying or assignment, only moving. */ |
| 82 | PpcMemDevice(const PpcMemDevice&) = delete; |
| 83 | PpcMemDevice& operator=(const PpcMemDevice&) = delete; |
| 84 | PpcMemDevice(PpcMemDevice&&) = default; |
| 85 | PpcMemDevice& operator=(PpcMemDevice&&) = default; |
| 86 | |
| 87 | bool read(const std::size_t offset, const std::size_t length, |
| 88 | void* const destination) override; |
| 89 | |
| 90 | bool write(const std::size_t offset, const std::size_t length, |
| 91 | const void* const source) override; |
| 92 | |
| 93 | private: |
Patrick Venture | ea0e470 | 2020-10-05 12:22:59 -0700 | [diff] [blame] | 94 | void close(); |
| 95 | |
Brandon Kim | 286cc6a | 2019-11-05 16:39:47 -0800 | [diff] [blame] | 96 | int ppcMemFd = -1; |
| 97 | const std::string ppcMemPath; |
| 98 | const internal::Sys* sys; |
| 99 | }; |
| 100 | |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 101 | } // namespace host_tool |