Skip to content

Commit b7768a7

Browse files
committed
atmel-samd: Fix file system init.
Adding the USB write protection prevented file system reset from working. Since it happens before USB start we temporarily set the volume to writeable and then set it back to read-only before USB is started.
1 parent d05299f commit b7768a7

1 file changed

Lines changed: 37 additions & 26 deletions

File tree

atmel-samd/main.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
4747
}
4848
}
4949

50+
// TODO(tannewt): Remove these default files in favor a very simple README with
51+
// a url to all of the files that ship on boards.
52+
5053
static const char fresh_boot_py[] =
5154
"# boot.py -- run on boot-up\r\n"
5255
"# can run arbitrary Python, but best to keep it minimal\r\n"
@@ -93,6 +96,11 @@ void init_flash_fs() {
9396
if (res == FR_NO_FILESYSTEM) {
9497
// no filesystem, or asked to reset it, so create a fresh one
9598

99+
// We are before USB initializes so temporarily undo the USB_WRITEABLE
100+
// requirement.
101+
bool usb_writeable = (vfs->flags & FSUSER_USB_WRITEABLE) > 0;
102+
vfs->flags &= ~FSUSER_USB_WRITEABLE;
103+
96104
res = f_mkfs("/flash", 0, 0);
97105
if (res == FR_OK) {
98106
// success creating fresh LFS
@@ -118,6 +126,35 @@ void init_flash_fs() {
118126
f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS);
119127
f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n);
120128
f_close(&fp);
129+
130+
// Make sure we have a /flash/boot.py. Create it if needed.
131+
FILINFO fno;
132+
#if _USE_LFN
133+
fno.lfname = NULL;
134+
fno.lfsize = 0;
135+
#endif
136+
res = f_stat("/flash/boot.py", &fno);
137+
if (res == FR_OK) {
138+
if (fno.fattrib & AM_DIR) {
139+
// exists as a directory
140+
// TODO handle this case
141+
// see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
142+
} else {
143+
// exists as a file, good!
144+
}
145+
} else {
146+
// doesn't exist, create fresh file
147+
148+
FIL fp;
149+
f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
150+
UINT n;
151+
f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
152+
// TODO check we could write n bytes
153+
f_close(&fp);
154+
}
155+
if (usb_writeable) {
156+
vfs->flags |= FSUSER_USB_WRITEABLE;
157+
}
121158
} else if (res == FR_OK) {
122159
// mount successful
123160
} else {
@@ -129,32 +166,6 @@ void init_flash_fs() {
129166
// The current directory is used as the boot up directory.
130167
// It is set to the internal flash filesystem by default.
131168
f_chdrive("/flash");
132-
133-
// Make sure we have a /flash/boot.py. Create it if needed.
134-
FILINFO fno;
135-
#if _USE_LFN
136-
fno.lfname = NULL;
137-
fno.lfsize = 0;
138-
#endif
139-
res = f_stat("/flash/boot.py", &fno);
140-
if (res == FR_OK) {
141-
if (fno.fattrib & AM_DIR) {
142-
// exists as a directory
143-
// TODO handle this case
144-
// see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
145-
} else {
146-
// exists as a file, good!
147-
}
148-
} else {
149-
// doesn't exist, create fresh file
150-
151-
FIL fp;
152-
f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
153-
UINT n;
154-
f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
155-
// TODO check we could write n bytes
156-
f_close(&fp);
157-
}
158169
}
159170

160171
static char *stack_top;

0 commit comments

Comments
 (0)