diff options
| author | Scott Shawcroft <scott@chickadee.tech> | 2016-10-25 15:06:39 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@chickadee.tech> | 2016-10-25 15:13:43 -0700 |
| commit | b7768a74a72fdb37d6d128bd8b3eafc7159fcd3e (patch) | |
| tree | 2e41dfa446c8b32f2d55b34286f2cd4ced26158b | |
| parent | d05299f57a28f8dfbc34a2c5a1f296b12e817e06 (diff) | |
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.
| -rw-r--r-- | atmel-samd/main.c | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/atmel-samd/main.c b/atmel-samd/main.c index ff970f15c..df259d9c4 100644 --- a/atmel-samd/main.c +++ b/atmel-samd/main.c @@ -47,6 +47,9 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { } } +// TODO(tannewt): Remove these default files in favor a very simple README with +// a url to all of the files that ship on boards. + static const char fresh_boot_py[] = "# boot.py -- run on boot-up\r\n" "# can run arbitrary Python, but best to keep it minimal\r\n" @@ -93,6 +96,11 @@ void init_flash_fs() { if (res == FR_NO_FILESYSTEM) { // no filesystem, or asked to reset it, so create a fresh one + // We are before USB initializes so temporarily undo the USB_WRITEABLE + // requirement. + bool usb_writeable = (vfs->flags & FSUSER_USB_WRITEABLE) > 0; + vfs->flags &= ~FSUSER_USB_WRITEABLE; + res = f_mkfs("/flash", 0, 0); if (res == FR_OK) { // success creating fresh LFS @@ -118,6 +126,35 @@ void init_flash_fs() { f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n); f_close(&fp); + + // Make sure we have a /flash/boot.py. Create it if needed. + FILINFO fno; + #if _USE_LFN + fno.lfname = NULL; + fno.lfsize = 0; + #endif + res = f_stat("/flash/boot.py", &fno); + if (res == FR_OK) { + if (fno.fattrib & AM_DIR) { + // exists as a directory + // TODO handle this case + // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation + } else { + // exists as a file, good! + } + } else { + // doesn't exist, create fresh file + + FIL fp; + f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); + UINT n; + f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); + // TODO check we could write n bytes + f_close(&fp); + } + if (usb_writeable) { + vfs->flags |= FSUSER_USB_WRITEABLE; + } } else if (res == FR_OK) { // mount successful } else { @@ -129,32 +166,6 @@ void init_flash_fs() { // The current directory is used as the boot up directory. // It is set to the internal flash filesystem by default. f_chdrive("/flash"); - - // Make sure we have a /flash/boot.py. Create it if needed. - FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - res = f_stat("/flash/boot.py", &fno); - if (res == FR_OK) { - if (fno.fattrib & AM_DIR) { - // exists as a directory - // TODO handle this case - // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation - } else { - // exists as a file, good! - } - } else { - // doesn't exist, create fresh file - - FIL fp; - f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); - UINT n; - f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); - // TODO check we could write n bytes - f_close(&fp); - } } static char *stack_top; |
