-
Notifications
You must be signed in to change notification settings - Fork 17
/
libhoth_mtd.c
282 lines (237 loc) · 6.88 KB
/
libhoth_mtd.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2023 Google LLC
//
// 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 "libhoth_mtd.h"
#include <errno.h>
#include <fcntl.h>
#include <linux/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "libhoth.h"
#include "libhoth_ec.h"
struct libhoth_mtd_device {
int fd;
unsigned int mailbox_address;
};
int libhoth_mtd_send_request(struct libhoth_device* dev, const void* request,
size_t request_size);
int libhoth_mtd_receive_response(struct libhoth_device* dev, void* response,
size_t max_response_size, size_t* actual_size,
int timeout_ms);
int libhoth_mtd_close(struct libhoth_device* dev);
static int mtd_read(int fd, unsigned int address, void* data, size_t data_len) {
if (fd < 0 || !data) {
return LIBHOTH_ERR_INVALID_PARAMETER;
}
if (!data_len) {
return LIBHOTH_OK;
}
if (lseek(fd, address, SEEK_SET) < 0) {
return LIBHOTH_ERR_FAIL;
}
while (data_len > 0) {
int ret = read(fd, data, data_len);
if (ret < 0) {
if (errno == EINTR) {
continue; // interrupted; try again
}
return LIBHOTH_ERR_FAIL;
} else if (ret == 0) {
// EOF hit
return LIBHOTH_ERR_IN_OVERFLOW;
} else {
// ret > 0
data += ret;
data_len -= ret;
}
}
return LIBHOTH_OK;
}
static int mtd_write(int fd, unsigned int address, const void* data,
size_t data_len) {
if (fd < 0 || !data || !data_len) return LIBHOTH_ERR_INVALID_PARAMETER;
if (lseek(fd, address, SEEK_SET) < 0) {
return LIBHOTH_ERR_FAIL;
}
ssize_t ret = -1;
do {
ret = write(fd, data, data_len);
// Retry if interrupted
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
return LIBHOTH_ERR_FAIL;
}
if (ret != data_len) {
// Fail on incomplete writes because they can confuse Hoth
return LIBHOTH_ERR_OUT_UNDERFLOW;
}
return LIBHOTH_OK;
}
static int libhoth_mtd_claim(struct libhoth_device* dev) {
// no-op
return LIBHOTH_OK;
}
static int libhoth_mtd_release(struct libhoth_device* dev) {
// no-op
return LIBHOTH_OK;
}
static int mtd_open(const char* path, const char* name) {
if (strlen(path) > 0) {
return open(path, O_RDWR);
}
FILE* fp;
char* line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/proc/mtd", "r");
if (fp == NULL) {
return -1;
}
int status;
int mtd_id = -1;
char mtd_size[20] = "";
char mtd_erasesize[20] = "";
char mtd_name[20] = "";
char resolved_path[20] = "";
while ((read = getline(&line, &len, fp)) != -1) {
status = sscanf(line, "mtd%d: %s %s %s", &mtd_id, mtd_size, mtd_erasesize,
mtd_name);
if (status != 4 || mtd_id < 0) {
continue;
}
// Strip '"' if any.
if (mtd_name[0] == '"') {
int i;
int name_len = strlen(mtd_name);
for (i = 0; i < name_len - 2; ++i) {
mtd_name[i] = mtd_name[i + 1];
mtd_name[i + 1] = 0;
}
}
if (strcmp(mtd_name, name) == 0) {
sprintf(resolved_path, "/dev/mtd%d", mtd_id);
return open(resolved_path, O_RDWR);
}
}
return -1;
}
int libhoth_mtd_open(const struct libhoth_mtd_device_init_options* options,
struct libhoth_device** out) {
if (out == NULL || options == NULL || options->path == NULL ||
options->name == NULL) {
return LIBHOTH_ERR_INVALID_PARAMETER;
}
int status;
int fd = -1;
struct libhoth_device* dev = NULL;
struct libhoth_mtd_device* mtd_dev = NULL;
fd = mtd_open(options->path, options->name);
if (fd < 0) {
status = LIBHOTH_ERR_INTERFACE_NOT_FOUND;
goto err_out;
}
dev = calloc(1, sizeof(struct libhoth_device));
if (dev == NULL) {
status = LIBHOTH_ERR_MALLOC_FAILED;
goto err_out;
}
mtd_dev = calloc(1, sizeof(struct libhoth_mtd_device));
if (mtd_dev == NULL) {
status = LIBHOTH_ERR_MALLOC_FAILED;
goto err_out;
}
mtd_dev->fd = fd;
mtd_dev->mailbox_address = options->mailbox;
dev->send = libhoth_mtd_send_request;
dev->receive = libhoth_mtd_receive_response;
dev->close = libhoth_mtd_close;
dev->claim = libhoth_mtd_claim;
dev->release = libhoth_mtd_release;
dev->user_ctx = mtd_dev;
*out = dev;
return LIBHOTH_OK;
err_out:
if (fd >= 0) {
close(fd);
}
if (dev != NULL) {
free(dev);
}
if (mtd_dev != NULL) {
free(mtd_dev);
}
return status;
}
int libhoth_mtd_send_request(struct libhoth_device* dev, const void* request,
size_t request_size) {
if (dev == NULL) {
return LIBHOTH_ERR_INVALID_PARAMETER;
}
struct libhoth_mtd_device* mtd_dev =
(struct libhoth_mtd_device*)dev->user_ctx;
return mtd_write(mtd_dev->fd, mtd_dev->mailbox_address, request,
request_size);
}
int libhoth_mtd_receive_response(struct libhoth_device* dev, void* response,
size_t max_response_size, size_t* actual_size,
int timeout_ms) {
if (dev == NULL) {
return LIBHOTH_ERR_INVALID_PARAMETER;
}
if (max_response_size < 8) {
return LIBHOTH_ERR_INVALID_PARAMETER;
}
size_t total_bytes;
int status;
struct ec_host_response host_response;
struct libhoth_mtd_device* mtd_dev =
(struct libhoth_mtd_device*)dev->user_ctx;
// Read Header From Mailbox
status = mtd_read(mtd_dev->fd, mtd_dev->mailbox_address, response, 8);
if (status != LIBHOTH_OK) {
return status;
}
total_bytes = 8;
memcpy(&host_response, response, sizeof(host_response));
if (actual_size) {
*actual_size = total_bytes;
}
if (max_response_size < (total_bytes + host_response.data_len)) {
return LIBHOTH_ERR_RESPONSE_BUFFER_OVERFLOW;
}
// Read remainder of data based on header length
uint8_t* const data_start = (uint8_t*)response + total_bytes;
status = mtd_read(mtd_dev->fd, mtd_dev->mailbox_address + total_bytes,
data_start, host_response.data_len);
if (status != LIBHOTH_OK) {
return status;
}
if (actual_size) {
*actual_size += host_response.data_len;
}
return LIBHOTH_OK;
}
int libhoth_mtd_close(struct libhoth_device* dev) {
if (dev == NULL) {
return LIBHOTH_ERR_INVALID_PARAMETER;
}
struct libhoth_mtd_device* mtd_dev =
(struct libhoth_mtd_device*)dev->user_ctx;
close(mtd_dev->fd);
free(dev->user_ctx);
return LIBHOTH_OK;
}