/
main.c
358 lines (285 loc) · 8.29 KB
/
main.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
348
349
350
351
352
353
354
355
356
357
358
/**
* @file main.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#include <limits.h>
static struct fs_command_t cmd_ls;
static struct fs_command_t cmd_open;
static struct fs_command_t cmd_close;
static struct fs_command_t cmd_read;
static struct fs_command_t cmd_write;
static struct shell_t shell;
static struct spi_driver_t spi;
static struct sd_driver_t sd;
static struct fat16_t fs;
static int is_file_open = 0;
static struct fat16_file_t file;
static int print_dir_name(void *chan_p,
const struct fat16_dir_entry_t *entry_p,
int width)
{
size_t size;
char b;
size = strlen(entry_p->name);
chan_write(chan_p, entry_p->name, size);
if (entry_p->is_dir == 1) {
b = '/';
chan_write(chan_p, &b, sizeof(b));
size++;
}
while (size < width) {
b = ' ';
chan_write(chan_p, &b, sizeof(b));
size++;
}
return (0);
}
static int format_entry(void *chan_p,
const struct fat16_dir_entry_t *entry_p)
{
/* Print file name with possible blank fill */
print_dir_name(chan_p, entry_p, 14);
/* Print modify date/time if requested */
std_fprintf(chan_p,
FSTR("%04u-%02u-%02u %02u:%02u:%02u"),
entry_p->latest_mod_date.year,
entry_p->latest_mod_date.month,
entry_p->latest_mod_date.day,
entry_p->latest_mod_date.hour,
entry_p->latest_mod_date.minute,
entry_p->latest_mod_date.second);
/* Print size if requested */
if (entry_p->is_dir != 1) {
std_fprintf(chan_p, FSTR(" %lu"), (unsigned long)entry_p->size);
}
std_fprintf(chan_p, FSTR("\r\n"));
return (0);
}
static int cmd_ls_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
UNUSED(in_p);
struct fat16_dir_t dir;
struct fat16_dir_entry_t entry;
const char *path_p;
if (argc != 2) {
std_fprintf(out_p, FSTR("Usage: %s <path>\r\n"), argv[0]);
return (1);
}
path_p = argv[1];
/* Re-open the directory with read option set. */
if (fat16_dir_open(&fs, &dir, path_p, O_READ) != 0) {
std_fprintf(out_p, FSTR("%s: directory not found\r\n"), path_p);
return (-1);
}
while (fat16_dir_read(&dir, &entry) == 1) {
format_entry(out_p, &entry);
}
fat16_dir_close(&dir);
return (0);
}
static int cmd_open_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
UNUSED(in_p);
const char *path_p;
int flags;
if (argc != 3) {
std_fprintf(out_p, FSTR("Usage: %s <path> <r | w>\r\n"), argv[0]);
return (1);
}
if (is_file_open == 1) {
std_fprintf(out_p,
FSTR("Cannot have more than one file open. Please close "
"current file before opening a new one.\r\n"));
return (1);
}
path_p = argv[1];
if (argv[2][0] == 'w') {
flags = (O_CREAT | O_WRITE | O_SYNC);
} else {
flags = O_READ;
}
/* Re-open the directory with read option set. */
if (fat16_file_open(&fs, &file, path_p, flags) != 0) {
std_fprintf(out_p, FSTR("%s: file not found\r\n"), path_p);
return (1);
}
is_file_open = 1;
return (0);
}
static int cmd_close_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
UNUSED(in_p);
if (argc != 1) {
std_fprintf(out_p, FSTR("Usage: %s\r\n"), argv[0]);
return (1);
}
if (is_file_open == 0) {
std_fprintf(out_p, FSTR("No file is open.\r\n"));
return (1);
}
fat16_file_close(&file);
is_file_open = 0;
return (0);
}
static int cmd_read_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
UNUSED(in_p);
ssize_t read, n;
long size;
char buf[64];
if (argc == 1) {
size = INT_MAX;
} else if (argc == 2) {
if (std_strtol(argv[1], &size) == NULL) {
std_fprintf(out_p, FSTR("%s: expected posotove integer\r\n"), argv[1]);
return (1);
}
} else {
std_fprintf(out_p, FSTR("Usage: %s [<size to read>]\r\n"), argv[0]);
return (1);
}
/* A file must be open. */
if (is_file_open == 0) {
std_fprintf(out_p, FSTR("The file must be opened before it can be read from.\r\n"));
return (1);
}
/* Read from file. */
read = 0;
while (read < size) {
if ((size - read) < sizeof(buf)) {
n = (size - read);
} else {
n = sizeof(buf);
}
if ((n = fat16_file_read(&file, buf, n)) <= 0) {
break;
}
chan_write(out_p, buf, n);
read += n;
}
return (0);
}
static int cmd_write_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
UNUSED(in_p);
if (argc != 2) {
std_fprintf(out_p, FSTR("Usage: %s <data to write>\r\n"), argv[0]);
return (1);
}
/* A file must be open. */
if (is_file_open == 0) {
std_fprintf(out_p, FSTR("The file must be opened before it can be written to.\r\n"));
return (1);
}
fat16_file_write(&file, argv[1], strlen(argv[1]));
return (0);
}
static int init(void)
{
sys_start();
fs_command_init(&cmd_ls,
FSTR("/ls"),
cmd_ls_cb,
NULL);
fs_command_register(&cmd_ls);
fs_command_init(&cmd_open,
FSTR("/open"),
cmd_open_cb,
NULL);
fs_command_register(&cmd_open);
fs_command_init(&cmd_close,
FSTR("/close"),
cmd_close_cb,
NULL);
fs_command_register(&cmd_close);
fs_command_init(&cmd_read,
FSTR("/read"),
cmd_read_cb,
NULL);
fs_command_register(&cmd_read);
fs_command_init(&cmd_write,
FSTR("/write"),
cmd_write_cb,
NULL);
fs_command_register(&cmd_write);
std_printf(sys_get_info());
spi_init(&spi,
&spi_device[0],
&pin_d6_dev,
SPI_MODE_MASTER,
SPI_SPEED_1MBPS,
0,
0);
sd_init(&sd, &spi);
if (sd_start(&sd) != 0) {
std_printf(FSTR("Failed to start the SD card.\r\n"));
return (-1);
}
std_printf(FSTR("SD card started.\r\n"));
fat16_init(&fs,
(fat16_read_t)sd_read_block,
(fat16_write_t)sd_write_block,
&sd,
0);
if (fat16_mount(&fs) != 0) {
std_printf(FSTR("Failed to mount FAT16 file system.\r\n"));
return (-1);
}
std_printf(FSTR("fat16 file system mounted\r\n"));
return (0);
}
int main()
{
if (init() != 0) {
return (-1);
}
shell_init(&shell,
sys_get_stdin(),
sys_get_stdout(),
NULL,
NULL,
NULL,
NULL);
shell_main(&shell);
return (0);
}