-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathec.cpp
More file actions
220 lines (190 loc) · 6.4 KB
/
ec.cpp
File metadata and controls
220 lines (190 loc) · 6.4 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <map>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <windows.h>
#include "ec.hpp"
#include "driver.hpp"
EmbeddedController::EmbeddedController(
BYTE scPort,
BYTE dataPort,
BYTE endianness,
UINT16 retry,
UINT16 timeout)
{
this->scPort = scPort;
this->dataPort = dataPort;
this->endianness = endianness;
this->retry = retry;
this->timeout = timeout;
this->driver = Driver();
if (this->driver.initialize())
this->driverLoaded = TRUE;
this->driverFileExist = driver.driverFileExist;
}
VOID EmbeddedController::close()
{
this->driver.deinitialize();
this->driverLoaded = FALSE;
}
EC_DUMP EmbeddedController::dump()
{
EC_DUMP _dump;
for (UINT16 column = 0x00; column <= 0xF0; column += 0x10)
for (UINT16 row = 0x00; row <= 0x0F; row++)
{
UINT16 address = column + row;
_dump.insert(std::pair<BYTE, BYTE>(address, this->readByte(address)));
}
return _dump;
}
VOID EmbeddedController::printDump()
{
std::stringstream stream;
stream << std::hex << std::uppercase << std::setfill('0')
<< " # | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F" << std::endl
<< "---|------------------------------------------------" << std::endl
<< "00 | ";
for (auto const &[address, value] : this->dump())
{
UINT16 nextAddress = address + 0x01;
stream << std::setw(2) << (UINT16)value << " ";
if (nextAddress % 0x10 == 0x00) // End of row
stream << std::endl
<< nextAddress << " | ";
}
std::string result = stream.str();
std::cout << std::endl
<< result.substr(0, result.size() - 7) // Removing last 7 characters
<< std::endl;
}
VOID EmbeddedController::saveDump(std::string output)
{
std::ofstream file(output, std::ios::out | std::ios::binary);
if (file)
{
for (auto const &[address, value] : this->dump())
file << this->readByte(address);
file.close();
}
}
BYTE EmbeddedController::readByte(BYTE bRegister)
{
BYTE result = 0x00;
this->operation(READ, bRegister, &result);
return result;
}
WORD EmbeddedController::readWord(BYTE bRegister)
{
BYTE firstByte = 0x00;
BYTE secondByte = 0x00;
WORD result = 0x00;
if (this->operation(READ, bRegister, &firstByte) &&
this->operation(READ, bRegister + 0x01, &secondByte))
{
if (endianness == BIG_ENDIAN)
std::swap(firstByte, secondByte);
result = firstByte | (secondByte << 8);
}
return result;
}
DWORD EmbeddedController::readDword(BYTE bRegister)
{
BYTE firstByte = 0x00;
BYTE secondByte = 0x00;
BYTE thirdByte = 0x00;
BYTE fourthByte = 0x00;
DWORD result = 0x00;
if (this->operation(READ, bRegister, &firstByte) &&
this->operation(READ, bRegister + 0x01, &secondByte) &&
this->operation(READ, bRegister + 0x02, &thirdByte) &&
this->operation(READ, bRegister + 0x03, &fourthByte))
{
if (endianness == BIG_ENDIAN)
{
std::swap(firstByte, fourthByte);
std::swap(secondByte, thirdByte);
}
result = firstByte |
(secondByte << 8) |
(thirdByte << 16) |
(fourthByte << 24);
}
return result;
}
BOOL EmbeddedController::writeByte(BYTE bRegister, BYTE value)
{
return this->operation(WRITE, bRegister, &value);
}
BOOL EmbeddedController::writeWord(BYTE bRegister, WORD value)
{
BYTE firstByte = value & 0xFF;
BYTE secondByte = value >> 8;
if (endianness == BIG_ENDIAN)
std::swap(firstByte, secondByte);
if (this->operation(WRITE, bRegister, &firstByte) &&
this->operation(WRITE, bRegister + 0x01, &secondByte))
return TRUE;
return FALSE;
}
BOOL EmbeddedController::writeDword(BYTE bRegister, DWORD value)
{
BYTE firstByte = value & 0xFF;
BYTE secondByte = (value >> 8) & 0xFF;
BYTE thirdByte = (value >> 16) & 0xFF;
BYTE fourthByte = value >> 24;
if (endianness == BIG_ENDIAN)
{
std::swap(firstByte, fourthByte);
std::swap(secondByte, thirdByte);
}
if (this->operation(WRITE, bRegister, &firstByte) &&
this->operation(WRITE, bRegister + 0x01, &secondByte) &&
this->operation(WRITE, bRegister + 0x02, &thirdByte) &&
this->operation(WRITE, bRegister + 0x03, &fourthByte))
return TRUE;
return FALSE;
}
BOOL EmbeddedController::operation(BYTE mode, BYTE bRegister, BYTE *value)
{
BOOL isRead = mode == READ;
BYTE operationType = isRead ? RD_EC : WR_EC;
for (UINT16 i = 0; i < this->retry; i++)
if (this->status(EC_IBF)) // Wait until IBF is free
{
this->driver.writeIoPortByte(this->scPort, operationType); // Write operation type to the Status/Command port
if (this->status(EC_IBF)) // Wait until IBF is free
{
this->driver.writeIoPortByte(this->dataPort, bRegister); // Write register address to the Data port
if (this->status(EC_IBF)) // Wait until IBF is free
if (isRead)
{
if (this->status(EC_OBF)) // Wait until OBF is full
{
*value = this->driver.readIoPortByte(this->dataPort); // Read from the Data port
return TRUE;
}
}
else
{
this->driver.writeIoPortByte(this->dataPort, *value); // Write to the Data port
return TRUE;
}
}
}
return FALSE;
}
BOOL EmbeddedController::status(BYTE flag)
{
BOOL done = flag == EC_OBF ? 0x01 : 0x00;
for (UINT16 i = 0; i < this->timeout; i++)
{
BYTE result = this->driver.readIoPortByte(this->scPort);
// First and second bit of returned value represent
// the status of OBF and IBF flags respectively
if (((done ? ~result : result) & flag) == 0)
return TRUE;
}
return FALSE;
}