forked from iDroid-Project/openiBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtd.c
347 lines (280 loc) · 7 KB
/
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* mtd.c - OpeniBoot Menu
*
* Copyright 2010 iDroid Project
*
* This file is part of iDroid. An android distribution for Apple products.
* For more information, please visit http://www.idroidproject.org/.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "mtd.h"
#include "arm/arm.h"
#include "commands.h"
#include "util.h"
static int mtd_init_done = 0;
static LinkedList mtd_list;
#define mtd_get(ptr) (CONTAINER_OF(mtd_t, list_ptr, (ptr)))
#define mtd_get_bdev(ptr) (CONTAINER_OF(mtd_t, bdev, (ptr)))
static error_t mtd_bdev_prepare(block_device_t *_dev)
{
mtd_t *dev = mtd_get_bdev(_dev);
return mtd_prepare(dev);
}
static void mtd_bdev_finish(block_device_t *_dev)
{
mtd_t *dev = mtd_get_bdev(_dev);
mtd_finish(dev);
}
static error_t mtd_bdev_seek(block_device_t *_dev, seek_mode_t _mode, int64_t _amt)
{
mtd_t *dev = mtd_get_bdev(_dev);
switch(_mode)
{
case seek_begin:
dev->bdev_addr = (int64_t)_amt;
break;
case seek_end:
{
int sz = mtd_size(dev);
if(sz <= 0)
return EIO;
dev->bdev_addr = sz - (int64_t)_amt;
break;
}
case seek_offset:
dev->bdev_addr += (int64_t)_amt;
break;
}
return SUCCESS;
}
static error_t mtd_bdev_read(block_device_t *_dev, void *_dest, int _sz)
{
mtd_t *dev = mtd_get_bdev(_dev);
int ret = mtd_read(dev, _dest, dev->bdev_addr, _sz);
if(ret >= 0)
dev->bdev_addr += _sz;
return ret;
}
static error_t mtd_bdev_write(block_device_t *_dev, void *_src, int _sz)
{
mtd_t *dev = mtd_get_bdev(_dev);
int ret = mtd_write(dev, _src, dev->bdev_addr, _sz);
if(ret >= 0)
dev->bdev_addr += _sz;
return ret;
}
static int64_t mtd_bdev_size(block_device_t *_dev)
{
mtd_t *dev = mtd_get_bdev(_dev);
return mtd_size(dev);
}
static int64_t mtd_bdev_block_size(block_device_t *_dev)
{
mtd_t *dev = mtd_get_bdev(_dev);
return mtd_block_size(dev);
}
error_t mtd_init(mtd_t *_mtd)
{
error_t ret = device_init(&_mtd->device);
if(ret != 0)
return ret;
ret = block_device_init(&_mtd->bdev);
if(ret != 0)
return ret;
_mtd->bdev.prepare = mtd_bdev_prepare;
_mtd->bdev.finish = mtd_bdev_finish;
_mtd->bdev.read = mtd_bdev_read;
_mtd->bdev.write = mtd_bdev_write;
_mtd->bdev.seek = mtd_bdev_seek;
_mtd->bdev.size = mtd_bdev_size;
_mtd->bdev.block_size = mtd_bdev_block_size;
_mtd->list_ptr.next = NULL;
_mtd->list_ptr.prev = NULL;
_mtd->prepare_count = 0;
_mtd->bdev_addr = 0;
return SUCCESS;
}
void mtd_cleanup(mtd_t *_mtd)
{
}
error_t mtd_register(mtd_t *_mtd)
{
error_t ret = device_register(&_mtd->device);
if(FAILED(ret))
return ret;
EnterCriticalSection();
if(!mtd_init_done)
{
mtd_init_done = 1;
mtd_list.prev = &mtd_list;
mtd_list.next = &mtd_list;
}
LinkedList *prev = mtd_list.prev;
_mtd->list_ptr.prev = prev;
_mtd->list_ptr.next = &mtd_list;
prev->next = &_mtd->list_ptr;
mtd_list.prev = &_mtd->list_ptr;
LeaveCriticalSection();
bufferPrintf("mtd: New device, '%s', registered.\n", _mtd->device.name);
if(_mtd->usage == mtd_filesystem)
{
ret = block_device_register(&_mtd->bdev);
if(FAILED(ret))
return ret;
}
return SUCCESS;
}
void mtd_unregister(mtd_t *_mtd)
{
if(_mtd->usage == mtd_filesystem)
block_device_unregister(&_mtd->bdev);
if(_mtd->list_ptr.next != NULL && _mtd->list_ptr.prev != NULL)
{
EnterCriticalSection();
LinkedList *prev = _mtd->list_ptr.prev;
LinkedList *next = _mtd->list_ptr.next;
next->prev = prev;
prev->next = next;
_mtd->list_ptr.prev = NULL;
_mtd->list_ptr.next = NULL;
LeaveCriticalSection();
}
device_unregister(&_mtd->device);
}
mtd_t *mtd_find(mtd_t *_prev)
{
LinkedList *ptr;
if(_prev == NULL)
ptr = mtd_list.next;
else
ptr = _prev->list_ptr.next;
if(ptr == &mtd_list)
return NULL;
return mtd_get(ptr);
}
error_t mtd_prepare(mtd_t *_mtd)
{
int ret = 0;
if(_mtd->prepare_count == 0 && _mtd->prepare != NULL)
ret = _mtd->prepare(_mtd);
_mtd->prepare_count++;
return SUCCESS;
}
void mtd_finish(mtd_t *_mtd)
{
if(_mtd->prepare_count == 1 && _mtd->finish != NULL)
_mtd->finish(_mtd);
_mtd->prepare_count--;
if(_mtd->prepare_count < 0)
_mtd->prepare_count = 0;
}
int64_t mtd_size(mtd_t *_mtd)
{
if(_mtd->size)
return _mtd->size(_mtd);
return -1;
}
int64_t mtd_block_size(mtd_t *_mtd)
{
if(_mtd->block_size)
return _mtd->block_size(_mtd);
return -1;
}
error_t mtd_read(mtd_t *_mtd, void *_dest, uint64_t _off, int _sz)
{
if(_mtd->read == NULL)
return ENOENT;
return _mtd->read(_mtd, _dest, _off, _sz);
}
error_t mtd_write(mtd_t *_mtd, void *_src, uint64_t _off, int _sz)
{
if(_mtd->write == NULL)
return ENOENT;
return _mtd->write(_mtd, _src, _off, _sz);
}
error_t mtd_list_devices()
{
mtd_t *mtd = NULL;
int i = 0;
bufferPrintf("mtd: Listing Devices:\n");
while((mtd = mtd_find(mtd)))
{
bufferPrintf("mtd: Device #%d '%s'.\n", i, mtd->device.name);
i++;
}
return 0;
}
static error_t cmd_mtd_list(int argc, char **argv)
{
return mtd_list_devices();
}
COMMAND("mtd_list", "List all MTD devices.", cmd_mtd_list);
static error_t cmd_mtd_read(int argc, char **argv)
{
if(argc != 5)
{
bufferPrintf("Usage: %s [device] [destination] [offset] [size].\n", argv[0]);
return EINVAL;
}
int idx = parseNumber(argv[1]);
mtd_t *dev = mtd_find(NULL);
while(idx > 0 && dev != NULL) {
dev = mtd_find(dev);
idx--;
}
if(!dev)
{
bufferPrintf("Invalid MTD index.\n");
return ENOENT;
}
bufferPrintf("OK, we are going to read from device '%s'\r\n", dev->device.name);
void *dest = (void*)parseNumber(argv[2]);
uint32_t offset = parseNumber(argv[3]);
int len = parseNumber(argv[4]);
mtd_prepare(dev);
error_t ret = mtd_read(dev, dest, offset, len);
mtd_finish(dev);
return ret;
}
COMMAND("mtd_read", "Read from a MTD device.", cmd_mtd_read);
static error_t cmd_mtd_write(int argc, char **argv)
{
if(argc != 5)
{
bufferPrintf("Usage: %s [device] [source] [offset] [size].\n", argv[0]);
return EINVAL;
}
int idx = parseNumber(argv[1]);
mtd_t *dev = mtd_find(NULL);
while(idx > 0 && dev != NULL) {
dev = mtd_find(dev);
idx--;
}
if(!dev)
{
bufferPrintf("Invalid MTD index.\n");
return ENOENT;
}
bufferPrintf("OK, we are going to write to device '%s'\r\n", dev->device.name);
void *src = (void*)parseNumber(argv[2]);
uint32_t offset = parseNumber(argv[3]);
int len = parseNumber(argv[4]);
mtd_prepare(dev);
error_t ret = mtd_write(dev, src, offset, len);
mtd_finish(dev);
return ret;
}
COMMAND("mtd_write", "Write to a MTD device.", cmd_mtd_write);