STATIC void mptask_init_sflash_filesystem (void) { FILINFO fno; #if _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif // Initialise the local flash filesystem. // Create it if needed, and mount in on /flash. // try to mount the flash FRESULT res = f_mount(sflash_fatfs, "/flash", 1); if (res == FR_NO_FILESYSTEM) { // no filesystem, so create a fresh one res = f_mkfs("/flash", 1, 0); if (res == FR_OK) { // success creating fresh LFS } else { __fatal_error("failed to create /flash"); } // create empty main.py mptask_create_main_py(); } else if (res == FR_OK) { // mount sucessful if (FR_OK != f_stat("/flash/main.py", &fno)) { // create empty main.py mptask_create_main_py(); } } else { __fatal_error("failed to create /flash"); } // 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. 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 void mptask_init_sflash_filesystem (void) { FILINFO fno; // Initialise the local flash filesystem. // init the vfs object fs_user_mount_t *vfs_fat = sflash_vfs_fat; vfs_fat->flags = 0; pyb_flash_init_vfs(vfs_fat); // Create it if needed, and mount in on /flash. FRESULT res = f_mount(&vfs_fat->fatfs); if (res == FR_NO_FILESYSTEM) { // no filesystem, so create a fresh one uint8_t working_buf[FF_MAX_SS]; res = f_mkfs(&vfs_fat->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); if (res == FR_OK) { // success creating fresh LFS } else { __fatal_error("failed to create /flash"); } // create empty main.py mptask_create_main_py(); } else if (res == FR_OK) { // mount sucessful if (FR_OK != f_stat(&vfs_fat->fatfs, "/main.py", &fno)) { // create empty main.py mptask_create_main_py(); } } else { fail: __fatal_error("failed to create /flash"); } // mount the flash device (there should be no other devices mounted at this point) // we allocate this structure on the heap because vfs->next is a root pointer mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t); if (vfs == NULL) { goto fail; } vfs->str = "/flash"; vfs->len = 6; vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); vfs->next = NULL; MP_STATE_VM(vfs_mount_table) = vfs; // The current directory is used as the boot up directory. // It is set to the internal flash filesystem by default. MP_STATE_PORT(vfs_cur) = vfs; // create /flash/sys, /flash/lib and /flash/cert if they don't exist if (FR_OK != f_chdir(&vfs_fat->fatfs, "/sys")) { f_mkdir(&vfs_fat->fatfs, "/sys"); } if (FR_OK != f_chdir(&vfs_fat->fatfs, "/lib")) { f_mkdir(&vfs_fat->fatfs, "/lib"); } if (FR_OK != f_chdir(&vfs_fat->fatfs, "/cert")) { f_mkdir(&vfs_fat->fatfs, "/cert"); } f_chdir(&vfs_fat->fatfs, "/"); // make sure we have a /flash/boot.py. Create it if needed. res = f_stat(&vfs_fat->fatfs, "/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(&vfs_fat->fatfs, &fp, "/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); } }