forked from DangerousPrototypes/BusPirate5-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.c
423 lines (359 loc) · 12.5 KB
/
storage.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "pirate.h"
#include "system_config.h"
#include "opt_args.h"
#include "hardware/timer.h"
#include "fatfs/ff.h"
#include "fatfs/tf_card.h"
#include "bio.h"
#include "ui/ui_prompt.h"
#include "ui/ui_parse.h"
#include "ui/ui_term.h"
#include "mjson/mjson.h"
#include "storage.h"
FATFS fs; /* FatFs work area needed for each volume */
FIL fil; /* File object needed for each open file */
// Size of read/write.
#define BUF_SIZE 512
// Insure 4-byte alignment.
uint32_t buf32[(BUF_SIZE + 3)/4];
uint8_t* buf = (uint8_t*)buf32;
FRESULT fr; /* FatFs return code */
UINT br;
UINT bw;
const char *fresult_msg[]={
[FR_OK]="ok", /* (0) Succeeded */
[FR_DISK_ERR]="disk error", /* (1) A hard error occurred in the low level disk I/O layer */
[FR_INT_ERR]="assertion failed", /* (2) Assertion failed */
[FR_NOT_READY]="drive not ready", /* (3) The physical drive cannot work */
[FR_NO_FILE]="file not found", /* (4) Could not find the file */
[FR_NO_PATH]="path not found", /* (5) Could not find the path */
[FR_INVALID_NAME]="invalid path", /* (6) The path name format is invalid */
[FR_DENIED]="access denied", /* (7) Access denied due to prohibited access or directory full */
[FR_EXIST]="access denied", /* (8) Access denied due to prohibited access */
[FR_INVALID_OBJECT]="invalid object", /* (9) The file/directory object is invalid */
[FR_WRITE_PROTECTED]="write prohibited", /* (10) The physical drive is write protected */
[FR_INVALID_DRIVE]="invalid drive", /* (11) The logical drive number is invalid */
[FR_NOT_ENABLED]="drive not enabled", /* (12) The volume has no work area */
[FR_NO_FILESYSTEM]="filesystem not found", /* (13) There is no valid FAT volume */
[FR_MKFS_ABORTED]="format aborted", /* (14) The f_mkfs() aborted due to any problem */
[FR_TIMEOUT]="timeout", /* (15) Could not get a grant to access the volume within defined period */
[FR_LOCKED]="file locked", /* (16) The operation is rejected according to the file sharing policy */
[FR_NOT_ENOUGH_CORE]="buffer full", /* (17) LFN working buffer could not be allocated */
[FR_TOO_MANY_OPEN_FILES]="too many open files", /* (18) Number of open files > FF_FS_LOCK */
[FR_INVALID_PARAMETER]="invalid parameter" /* (19) Given parameter is invalid */
};
void storage_init(void)
{
// Output Pins
gpio_set_function(TFCARD_CS, GPIO_FUNC_SIO);
gpio_put(TFCARD_CS, 1);
gpio_set_dir(TFCARD_CS, GPIO_OUT);
}
bool storage_detect(void)
{
//TF flash card detect is measued through the analog mux for lack of IO pins....
//if we have low, storage not previously available, and we didn't error out, try to mount
if(hw_adc_raw[HW_ADC_MUX_CARD_DETECT]<100 &&
system_config.storage_available==false &&
system_config.storage_mount_error==0
)
{
//show a status message (TF flash card mounted: XXGB FAT32)
fr = f_mount(&fs, "", 1);
if (fr != FR_OK) {
system_config.storage_available=false;
system_config.storage_mount_error=fr;
printf("Mount error %d\r\n", fr);
}
else
{
system_config.storage_available=true;
system_config.storage_fat_type=fs.fs_type;
system_config.storage_size=fs.csize * fs.n_fatent * 512E-9;
printf("TF flash card mounted: %7.2f GB %s\r\n\r\n", system_config.storage_size,storage_fat_type_labels[system_config.storage_fat_type-1]);
}
}
//card removed, unmount, look for fresh insert next time
if(hw_adc_raw[HW_ADC_MUX_CARD_DETECT]>=100 &&
(system_config.storage_available==true || system_config.storage_mount_error!=0)
)
{
system_config.storage_available=false;
system_config.storage_mount_error=0;
printf("TF flash card removed\r\n");
}
return true;
}
bool storage_mount(void)
{
fr = f_mount(&fs, "", 1);
if (fr != FR_OK) {
//printf("Mount error %d\r\n", fr);
system_config.storage_available=0;
system_config.storage_mount_error=fr;
return false;
}
else
{
system_config.storage_available=1;
system_config.storage_fat_type=fs.fs_type;
system_config.storage_size=fs.csize * fs.n_fatent * 512E-9;
return true;
/*printf("Mount ok\r\n");
switch (fs.fs_type) {
case FS_FAT12:
printf("Type is FAT12\r\n");
break;
case FS_FAT16:
printf("Type is FAT16\r\n");
break;
case FS_FAT32:
printf("Type is FAT32\r\n");
break;
case FS_EXFAT:
printf("Type is EXFAT\r\n");
break;
default:
printf("Type is unknown\r\n");
break;
}
printf("Card size: %7.2f GB (GB = 1E9 bytes)\r\n\r\n", fs.csize * fs.n_fatent * 512E-9);
*/
}
}
uint32_t storage_new_file(void){
fr = f_open(&fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS);
if (fr == FR_OK) {
f_write(&fil, "It works!\r\n", 11, &bw);
fr = f_close(&fil);
if (fr == FR_OK && bw == 11) {
return 1;
}else{
return 0;
}
}
}
uint32_t getint(const char *buf, int len, const char *path, uint32_t defvalue, int *found) {
double dv;
*found = mjson_get_number(buf, len, path, &dv);
if(found)
{
return dv;
}
else
{
return defvalue;
}
}
uint32_t storage_save_mode(const char *filename, struct _mode_config_t *config_t, uint8_t count)
{
if(system_config.storage_available==0)
{
return 0;
}
fr = f_open(&fil, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (fr != FR_OK) {
//no config file or TF flash card
//printf("File not found: %d\r\n", fr);
//return (int)fr;
return 0;
}
f_printf(&fil,"{\n");
for(uint8_t i=0; i<count; i++)
{
f_printf(&fil, "\"%s\": %d%s\n",&config_t[i].tag[2], *config_t[i].config, (i==(count-1)?"":",") );
}
f_printf(&fil,"}");
/* Close the file */
f_close(&fil);
return 1;
}
void file_error(FRESULT res)
{
if(res>0)
{
printf("%sError:%s %s%s", ui_term_color_error(),ui_term_color_info(), fresult_msg[res], ui_term_color_reset());
}
}
void cat(opt_args (*args), struct command_result *res)
{
char file[512];
/*if(system_config.storage_available==0)
{
return;
}*/
fr = f_open(&fil, args[0].c, FA_READ);
if (fr != FR_OK) {
//no config file or TF flash card
file_error(fr);
//return (int)fr;
return;
}
/* Read every line and display it */
while (f_gets(file, sizeof(file), &fil)) {
printf(file);
}
printf("\r\n");
/* Close the file */
f_close(&fil);
return;
}
void make_dir(opt_args (*args), struct command_result *res)
{
FRESULT fr;
fr = f_mkdir(args[0].c);
file_error(fr);
}
void change_dir(opt_args (*args), struct command_result *res)
{
FRESULT fr;
fr = f_chdir(args[0].c);
if(fr)
{
file_error(fr);
}
else
{
TCHAR str[128];
fr = f_getcwd(str, 128); /* Get current directory path */
printf("%s\r\n",str);
}
}
void storage_unlink(opt_args (*args), struct command_result *res)
{
FRESULT fr;
fr = f_unlink(args[0].c);
file_error(fr);
}
void list_dir(opt_args (*args), struct command_result *res)
{
FRESULT fr;
DIR dir;
FILINFO fno;
int nfile, ndir;
fr = f_opendir(&dir, args[0].c); /* Open the directory */
//TCHAR str[128];
//fr = f_getcwd(str, 128); /* Get current directory path */
//printf("%s\r\n",str);
if (fr == FR_OK) {
nfile = ndir = 0;
for(;;)
{
fr = f_readdir(&dir, &fno); /* Read a directory item */
if(fr != FR_OK || fno.fname[0] == 0) break; /* Error or end of dir */
if(fno.fattrib & AM_DIR)
{ /* Directory */
printf("%s <DIR> %s%s%s\r\n",ui_term_color_prompt(), ui_term_color_info(), fno.fname, ui_term_color_reset());
ndir++;
}
else
{ /* File */
printf("%s%10llu %s%s%s\r\n",
ui_term_color_prompt(),fno.fsize, ui_term_color_info(),fno.fname, ui_term_color_reset());
nfile++;
}
}
f_closedir(&dir);
printf("%s%d dirs, %d files%s\r\n", ui_term_color_info(), ndir, nfile, ui_term_color_reset());
} else {
//printf("Failed to open \"%s\". (%s)\r\n", args[0].c, fresult_msg[fr]);
file_error(fr);
}
return; // (uint32_t) res;
}
uint32_t storage_load_mode(const char *filename, struct _mode_config_t *config_t, uint8_t count)
{
char json[512];
if(system_config.storage_available==0)
{
return 0;
}
fr = f_open(&fil, filename, FA_READ);
if (fr != FR_OK) {
//no config file or TF flash card
//printf("File not found: %d\r\n", fr);
//return (int)fr;
return 0;
}
//FIL* fp, /* [IN] File object */
//void* buff, /* [OUT] Buffer to store read data */
//UINT btr, /* [IN] Number of bytes to read */
//UINT* br /* [OUT] Number of bytes read */
fr = f_read ( &fil, &json, sizeof(json), &br);
int found;
for(uint8_t i=0; i<count; i++)
{
uint32_t val=getint(json, br, config_t[i].tag, 0, &found);
if(found)
{
//printf("%s: %d\r\n",config_t[i].tag, val);
*config_t[i].config=val;
}
else
{
//printf("%s: not found\r\n",config_t[i].tag);
}
}
/* Close the file */
f_close(&fil);
return 1;
}
const char system_config_file[]="bpconfig.bp";
struct _mode_config_t system_config_json[]={
{"$.terminal_language", &system_config.terminal_language},
{"$.terminal_ansi_color", &system_config.terminal_ansi_color},
{"$.terminal_ansi_statusbar", &system_config.terminal_ansi_statusbar},
{"$.display_format", &system_config.display_format},
{"$.lcd_screensaver_active", &system_config.lcd_screensaver_active},
{"$.lcd_timeout", &system_config.lcd_timeout},
{"$.led_effect", &system_config.led_effect},
{"$.led_color", &system_config.led_color},
{"$.led_brightness", &system_config.led_brightness},
{"$.terminal_usb_enable", &system_config.terminal_usb_enable},
{"$.terminal_uart_enable", &system_config.terminal_uart_enable},
{"$.terminal_uart_number", &system_config.terminal_uart_number},
{"$.debug_uart_enable", &system_config.debug_uart_enable},
{"$.debug_uart_number", &system_config.debug_uart_number},
};
uint32_t storage_save_config(void)
{
return storage_save_mode(system_config_file, system_config_json, count_of(system_config_json));
}
uint32_t storage_load_config(void)
{
return storage_load_mode(system_config_file, system_config_json, count_of(system_config_json));
}
#if 0
// ls/dir directory file list
FRESULT res;
DIR dir;
UINT i;
static FILINFO fno;
char path[256];
path[0]= '/';
path[1]=0x00;
res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (fno.fattrib & AM_DIR) { /* It is a directory */
/*i = strlen(path);
sprintf(&path[i], "/%s", fno.fname);
res = scan_files(path);
if (res != FR_OK) break;
path[i] = 0;*/
} else { /* It is a file. */
printf("%s/%s\r\n", path, fno.fname);
}
}
f_closedir(&dir);
}
else
{
printf("Directory error: %d", res);
}
#endif