Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 1 | #include "io.hpp" |
| 2 | |
| 3 | #include "internal/sys.hpp" |
| 4 | |
| 5 | #include <fcntl.h> |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <cstring> |
| 9 | #include <string> |
| 10 | |
| 11 | namespace host_tool |
| 12 | { |
| 13 | |
| 14 | const std::string DevMemDevice::devMemPath = "/dev/mem"; |
| 15 | |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 16 | bool DevMemDevice::read(const std::size_t offset, const std::size_t length, |
| 17 | void* const destination) |
| 18 | { |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 19 | devMemFd = sys->open(devMemPath.c_str(), O_RDONLY); |
| 20 | if (devMemFd < 0) |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 21 | { |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 22 | return false; |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 25 | /* Map based on aligned addresses - behind the scenes. */ |
| 26 | const std::size_t alignedDiff = offset % sys->getpagesize(); |
| 27 | const std::size_t alignedOffset = offset - alignedDiff; |
| 28 | const std::size_t alignedSize = length + alignedDiff; |
| 29 | |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 30 | // addr, length, prot, flags, fd, offset |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 31 | devMemMapped = sys->mmap(0, alignedSize, PROT_READ, MAP_SHARED, devMemFd, |
| 32 | alignedOffset); |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 33 | if (devMemMapped == MAP_FAILED) |
| 34 | { |
Patrick Venture | 213f2db | 2019-05-15 10:47:30 -0700 | [diff] [blame] | 35 | std::fprintf(stderr, "Failed to mmap at offset: 0x%zx, length: %zu\n", |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 36 | offset, length); |
| 37 | sys->close(devMemFd); |
| 38 | return false; |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 41 | void* alignedSource = |
| 42 | static_cast<std::uint8_t*>(devMemMapped) + alignedDiff; |
| 43 | |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 44 | /* Copy the bytes. */ |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 45 | std::memcpy(destination, alignedSource, length); |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 46 | |
| 47 | /* Close the map between reads for now. */ |
| 48 | sys->munmap(devMemMapped, length); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 49 | sys->close(devMemFd); |
Patrick Venture | ac4ff97 | 2019-05-03 17:35:00 -0700 | [diff] [blame] | 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 54 | bool DevMemDevice::write(const std::size_t offset, const std::size_t length, |
| 55 | const void* const source) |
| 56 | { |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 57 | devMemFd = sys->open(devMemPath.c_str(), O_RDWR); |
| 58 | if (devMemFd < 0) |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 59 | { |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 60 | std::fprintf(stderr, "Failed to open /dev/mem for writing\n"); |
| 61 | return false; |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 64 | /* Map based on aligned addresses - behind the scenes. */ |
| 65 | const std::size_t alignedDiff = offset % sys->getpagesize(); |
| 66 | const std::size_t alignedOffset = offset - alignedDiff; |
| 67 | const std::size_t alignedSize = length + alignedDiff; |
| 68 | |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 69 | // addr, length, prot, flags, fd, offset |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 70 | devMemMapped = sys->mmap(0, alignedSize, PROT_WRITE, MAP_SHARED, devMemFd, |
| 71 | alignedOffset); |
| 72 | |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 73 | if (devMemMapped == MAP_FAILED) |
| 74 | { |
Patrick Venture | 213f2db | 2019-05-15 10:47:30 -0700 | [diff] [blame] | 75 | std::fprintf(stderr, "Failed to mmap at offset: 0x%zx, length: %zu\n", |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 76 | offset, length); |
| 77 | sys->close(devMemFd); |
| 78 | return false; |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 81 | void* alignedDestination = |
| 82 | static_cast<std::uint8_t*>(devMemMapped) + alignedDiff; |
| 83 | |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 84 | /* Copy the bytes. */ |
Patrick Venture | 206097b | 2019-05-07 13:37:01 -0700 | [diff] [blame] | 85 | std::memcpy(alignedDestination, source, length); |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 86 | |
| 87 | /* Close the map between writes for now. */ |
| 88 | sys->munmap(devMemMapped, length); |
Patrick Venture | 18bbe3c | 2019-05-14 11:40:57 -0700 | [diff] [blame] | 89 | sys->close(devMemFd); |
Patrick Venture | 030b1a8 | 2019-01-18 08:33:02 -0800 | [diff] [blame] | 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | } // namespace host_tool |