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
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lpc_aspeed.hpp"
#include "mapper_errors.hpp"
#include "window_hw_interface.hpp"
#include <fcntl.h>
#include <linux/aspeed-lpc-ctrl.h>
#include <linux/kernel.h>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <utility>
namespace ipmi_flash
{
const std::string LpcMapperAspeed::lpcControlPath = "/dev/aspeed-lpc-ctrl";
std::unique_ptr<HardwareMapperInterface>
LpcMapperAspeed::createAspeedMapper(std::uint32_t regionAddress,
std::size_t regionSize)
{
/* NOTE: considered using a joint factory to create one or the other, for
* now, separate factories.
*/
return std::make_unique<LpcMapperAspeed>(regionAddress, regionSize);
}
void LpcMapperAspeed::close()
{
if (mappedRegion)
{
sys->munmap(mappedRegion, regionSize);
mappedRegion = nullptr;
}
if (mappedFd != -1)
{
sys->close(mappedFd);
mappedFd = -1;
}
}
WindowMapResult LpcMapperAspeed::mapWindow(std::uint32_t address,
std::uint32_t length)
{
WindowMapResult result = {};
static const std::uint32_t MASK_64K = 0xFFFFU;
const std::uint32_t offset = address & MASK_64K;
if (offset + length > regionSize)
{
std::fprintf(stderr,
"requested window size %" PRIu32 ", offset %#" PRIx32
" is too large for mem region"
" of size %zu\n",
length, offset, regionSize);
result.response = EFBIG;
result.windowSize = regionSize - offset;
return result;
}
struct aspeed_lpc_ctrl_mapping map = {
.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
.window_id = 0,
.flags = 0,
.addr = address & ~MASK_64K,
.offset = 0,
.size = __ALIGN_KERNEL_MASK(offset + length, MASK_64K),
};
std::fprintf(stderr,
"requesting Aspeed LPC window at %#" PRIx32 " of size %" PRIu32
"\n",
map.addr, map.size);
const auto lpcControlFd = sys->open(lpcControlPath.c_str(), O_RDWR);
if (lpcControlFd == -1)
{
std::fprintf(stderr,
"cannot open Aspeed LPC kernel control dev \"%s\"\n",
lpcControlPath.c_str());
result.response = EINVAL;
return result;
}
if (sys->ioctl(lpcControlFd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1)
{
std::fprintf(stderr, "Failed to ioctl Aspeed LPC map with error %s\n",
std::strerror(errno));
sys->close(lpcControlFd);
result.response = EINVAL;
return result;
}
sys->close(lpcControlFd);
result.response = 0;
result.windowOffset = offset;
result.windowSize = length;
return result;
}
MemorySet LpcMapperAspeed::open()
{
if (mapRegion())
{
MemorySet output;
output.mappedFd = mappedFd;
output.mapped = mappedRegion;
return output;
}
throw MapperException("Unable to memory-map region");
}
bool LpcMapperAspeed::mapRegion()
{
/* Open the file to map. */
mappedFd = sys->open(lpcControlPath.c_str(), O_RDONLY | O_SYNC);
if (mappedFd == -1)
{
std::fprintf(stderr, "ipmiflash: unable to open %s\n",
lpcControlPath.c_str());
return false;
}
mappedRegion = reinterpret_cast<uint8_t*>(
sys->mmap(0, regionSize, PROT_READ, MAP_SHARED, mappedFd, 0));
if (mappedRegion == MAP_FAILED)
{
sys->close(mappedFd);
mappedFd = -1;
std::fprintf(stderr, "Mmap failure: '%s'\n", std::strerror(errno));
return false;
}
/* TOOD: There is no close() method here, to close mappedFd, or mappedRegion
* -- therefore, a good next step will be to evaluate whether or not the
* other pieces should go here...
*/
return true;
}
} // namespace ipmi_flash
|