-
Notifications
You must be signed in to change notification settings - Fork 3
/
io.cpp
157 lines (128 loc) · 4.12 KB
/
io.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "io.hpp"
#include "internal/sys.hpp"
#include <fcntl.h>
#include <cstdint>
#include <cstring>
#include <string>
namespace host_tool
{
const std::string DevMemDevice::devMemPath = "/dev/mem";
bool DevMemDevice::read(const std::size_t offset, const std::size_t length,
void* const destination)
{
devMemFd = sys->open(devMemPath.c_str(), O_RDONLY);
if (devMemFd < 0)
{
return false;
}
/* Map based on aligned addresses - behind the scenes. */
const std::size_t alignedDiff = offset % sys->getpagesize();
const std::size_t alignedOffset = offset - alignedDiff;
const std::size_t alignedSize = length + alignedDiff;
// addr, length, prot, flags, fd, offset
devMemMapped = sys->mmap(0, alignedSize, PROT_READ, MAP_SHARED, devMemFd,
alignedOffset);
if (devMemMapped == MAP_FAILED)
{
std::fprintf(stderr, "Failed to mmap at offset: 0x%zx, length: %zu\n",
offset, length);
sys->close(devMemFd);
return false;
}
void* alignedSource = static_cast<std::uint8_t*>(devMemMapped) +
alignedDiff;
/* Copy the bytes. */
std::memcpy(destination, alignedSource, length);
/* Close the map between reads for now. */
sys->munmap(devMemMapped, length);
sys->close(devMemFd);
return true;
}
bool DevMemDevice::write(const std::size_t offset, const std::size_t length,
const void* const source)
{
devMemFd = sys->open(devMemPath.c_str(), O_RDWR);
if (devMemFd < 0)
{
std::fprintf(stderr, "Failed to open /dev/mem for writing\n");
return false;
}
/* Map based on aligned addresses - behind the scenes. */
const std::size_t alignedDiff = offset % sys->getpagesize();
const std::size_t alignedOffset = offset - alignedDiff;
const std::size_t alignedSize = length + alignedDiff;
// addr, length, prot, flags, fd, offset
devMemMapped = sys->mmap(0, alignedSize, PROT_WRITE, MAP_SHARED, devMemFd,
alignedOffset);
if (devMemMapped == MAP_FAILED)
{
std::fprintf(stderr, "Failed to mmap at offset: 0x%zx, length: %zu\n",
offset, length);
sys->close(devMemFd);
return false;
}
void* alignedDestination = static_cast<std::uint8_t*>(devMemMapped) +
alignedDiff;
/* Copy the bytes. */
std::memcpy(alignedDestination, source, length);
/* Close the map between writes for now. */
sys->munmap(devMemMapped, length);
sys->close(devMemFd);
return true;
}
PpcMemDevice::~PpcMemDevice()
{
// Attempt to close in case reads or writes didn't close themselves
close();
}
void PpcMemDevice::close()
{
if (ppcMemFd >= 0)
{
sys->close(ppcMemFd);
ppcMemFd = -1;
}
}
bool PpcMemDevice::read(const std::size_t offset, const std::size_t length,
void* const destination)
{
ppcMemFd = sys->open(ppcMemPath.c_str(), O_RDWR);
if (ppcMemFd < 0)
{
std::fprintf(stderr, "Failed to open PPC LPC access path: %s",
ppcMemPath.c_str());
return false;
}
int ret = sys->pread(ppcMemFd, destination, length, offset);
if (ret < 0)
{
std::fprintf(stderr, "IO read failed at offset: 0x%zx, length: %zu\n",
offset, length);
close();
return false;
}
close();
return true;
}
bool PpcMemDevice::write(const std::size_t offset, const std::size_t length,
const void* const source)
{
ppcMemFd = sys->open(ppcMemPath.c_str(), O_RDWR);
if (ppcMemFd < 0)
{
std::fprintf(stderr, "Failed to open PPC LPC access path: %s",
ppcMemPath.c_str());
return false;
}
ssize_t ret = sys->pwrite(ppcMemFd, source, length, offset);
if (ret < 0)
{
std::fprintf(stderr, "IO write failed at offset: 0x%zx, length: %zu\n",
offset, length);
close();
return false;
}
close();
return true;
}
} // namespace host_tool