mtd.cpp
9.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
/****************************************************************************
*
* Copyright (c) 2013-2014 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file mtd.c
*
* mtd service and utility app.
*
* @author Lorenz Meier <lm@inf.ethz.ch>
*/
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/log.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/spi.h>
#include <px4_platform_common/px4_mtd.h>
#include <px4_platform_common/getopt.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <nuttx/spi/spi.h>
#include <nuttx/mtd/mtd.h>
#include <nuttx/fs/nxffs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/drivers/drivers.h>
#include <arch/board/board.h>
#include "systemlib/px4_macros.h"
#include <parameters/param.h>
#include <board_config.h>
extern "C" __EXPORT int mtd_main(int argc, char *argv[]);
static int mtd_status(void)
{
int ret = 0;
bool running = false;
unsigned int num_instances;
const mtd_instance_s *instances = px4_mtd_get_instances(&num_instances);
if (instances) {
for (unsigned int i = 0; i < num_instances; ++i) {
if (instances[i].mtd_dev) {
unsigned long blocksize;
unsigned long erasesize;
unsigned long neraseblocks;
unsigned int blkpererase;
unsigned int nblocks;
unsigned int partsize;
ret = px4_mtd_get_geometry(&instances[i], &blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize);
if (ret == 0) {
PX4_INFO("Flash Geometry of instance %i:", i);
printf(" blocksize: %lu\n", blocksize);
printf(" erasesize: %lu\n", erasesize);
printf(" neraseblocks: %lu\n", neraseblocks);
printf(" No. partitions: %u\n", instances[i].n_partitions_current);
unsigned int totalnblocks = 0;
unsigned int totalpartsize = 0;
for (unsigned int p = 0; p < instances[i].n_partitions_current; p++) {
FAR struct mtd_geometry_s geo;
ret = instances[i].part_dev[p]->ioctl(instances[i].part_dev[p], MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo));
printf(" partition: %u:\n", p);
printf(" name: %s\n", instances[i].partition_names[p]);
printf(" blocks: %u (%u bytes)\n", geo.neraseblocks, erasesize * geo.neraseblocks);
totalnblocks += geo.neraseblocks;
totalpartsize += erasesize * geo.neraseblocks;
}
printf(" Device size: %u Blocks (%u bytes)\n", totalnblocks, totalpartsize);
printf(" TOTAL SIZE: %u KiB\n", totalpartsize / 1024);
}
running |= true;
}
}
}
if (!running) {
PX4_INFO("MTD driver not started");
return 1;
}
return ret;
}
static void print_usage(void)
{
#if !defined(CONSTRAINED_FLASH)
PRINT_MODULE_DESCRIPTION("Utility to mount and test partitions (based on FRAM/EEPROM storage as defined by the board)");
PRINT_MODULE_USAGE_NAME("mtd", "command");
PRINT_MODULE_USAGE_COMMAND_DESCR("status", "Print status information");
PRINT_MODULE_USAGE_COMMAND_DESCR("readtest", "Perform read test");
PRINT_MODULE_USAGE_COMMAND_DESCR("rwtest", "Perform read-write test");
PRINT_MODULE_USAGE_COMMAND_DESCR("erase", "Erase partition(s)");
PRINT_MODULE_USAGE_PARAM_COMMENT("The commands 'readtest' and 'rwtest' have an optional instance index:");
PRINT_MODULE_USAGE_PARAM_INT('i', 0, 0, 1, "storage index (if the board has multiple storages)", true);
PRINT_MODULE_USAGE_PARAM_COMMENT("The commands 'readtest', 'rwtest' and 'erase' have an optional parameter:");
PRINT_MODULE_USAGE_ARG("<partition_name1> [<partition_name2> ...]",
"Partition names (eg. /fs/mtd_params), use system default if not provided", true);
#endif
}
int mtd_erase(mtd_instance_s &instance)
{
uint8_t v[64];
memset(v, 0xFF, sizeof(v));
for (uint8_t i = 0; i < instance.n_partitions_current; i++) {
uint32_t count = 0;
printf("Erasing %s\n", instance.partition_names[i]);
int fd = open(instance.partition_names[i], O_WRONLY);
if (fd == -1) {
PX4_ERR("Failed to open partition");
return 1;
}
while (write(fd, v, sizeof(v)) == sizeof(v)) {
count += sizeof(v);
}
printf("Erased %lu bytes\n", (unsigned long)count);
close(fd);
}
return 0;
}
#if !defined(CONSTRAINED_FLASH)
/*
readtest is useful during startup to validate the device is
responding on the bus. It relies on the driver returning an error on
bad reads (the ramtron driver does return an error)
*/
int mtd_readtest(const mtd_instance_s &instance)
{
uint8_t v[128];
for (uint8_t i = 0; i < instance.n_partitions_current; i++) {
ssize_t count = 0;
ssize_t expected_size = px4_mtd_get_partition_size(&instance, instance.partition_names[i]);
if (expected_size == 0) {
PX4_ERR("Failed partition size is 0");
return 1;
}
printf("reading %s expecting %u bytes\n", instance.partition_names[i], expected_size);
int fd = open(instance.partition_names[i], O_RDONLY);
if (fd == -1) {
PX4_ERR("Failed to open partition");
return 1;
}
while (read(fd, v, sizeof(v)) == sizeof(v)) {
count += sizeof(v);
}
if (count != expected_size) {
PX4_ERR("Failed to read partition - got %u/%u bytes", count, expected_size);
return 1;
}
close(fd);
}
printf("readtest OK\n");
return 0;
}
/*
rwtest is useful during startup to validate the device is
responding on the bus for both reads and writes. It reads data in
blocks and writes the data back, then reads it again, failing if the
data isn't the same
*/
int mtd_rwtest(const mtd_instance_s &instance)
{
uint8_t v[128], v2[128];
for (uint8_t i = 0; i < instance.n_partitions_current; i++) {
ssize_t count = 0;
off_t offset = 0;
ssize_t expected_size = px4_mtd_get_partition_size(&instance, instance.partition_names[i]);
if (expected_size == 0) {
PX4_ERR("Failed partition size is 0");
return 1;
}
printf("rwtest %s testing %u bytes\n", instance.partition_names[i], expected_size);
int fd = open(instance.partition_names[i], O_RDWR);
if (fd == -1) {
PX4_ERR("Failed to open partition");
return 1;
}
while (read(fd, v, sizeof(v)) == sizeof(v)) {
count += sizeof(v);
if (lseek(fd, offset, SEEK_SET) != offset) {
PX4_ERR("seek failed");
return 1;
}
if (write(fd, v, sizeof(v)) != sizeof(v)) {
PX4_ERR("write failed");
return 1;
}
if (lseek(fd, offset, SEEK_SET) != offset) {
PX4_ERR("seek failed");
return 1;
}
if (read(fd, v2, sizeof(v2)) != sizeof(v2)) {
PX4_ERR("read failed");
return 1;
}
if (memcmp(v, v2, sizeof(v2)) != 0) {
PX4_ERR("memcmp failed");
return 1;
}
offset += sizeof(v);
}
if (count != expected_size) {
PX4_ERR("Failed to read partition - got %u/%u bytes", count, expected_size);
return 1;
}
close(fd);
}
printf("rwtest OK\n");
return 0;
}
#endif
int mtd_main(int argc, char *argv[])
{
int myoptind = 1;
const char *myoptarg = NULL;
int ch;
int instance = 0;
while ((ch = px4_getopt(argc, argv, "i:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'i':
instance = atoi(myoptarg);
break;
default:
print_usage();
return -1;
break;
}
}
if (myoptind < argc) {
unsigned int num_instances;
mtd_instance_s *instances = px4_mtd_get_instances(&num_instances);
if (instances == nullptr) {
PX4_ERR("Driver not running");
return -1;
}
if (instance < 0 || (unsigned) instance >= num_instances) {
PX4_ERR("invalid instance");
return -1;
}
#if !defined(CONSTRAINED_FLASH)
if (!strcmp(argv[myoptind], "readtest")) {
return mtd_readtest(instances[instance]);
}
if (!strcmp(argv[myoptind], "rwtest")) {
return mtd_rwtest(instances[instance]);
}
#endif
if (!strcmp(argv[myoptind], "status")) {
return mtd_status();
}
if (!strcmp(argv[myoptind], "erase")) {
return mtd_erase(instances[instance]);
}
}
print_usage();
return 1;
}