diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 4af836b2d0d5a..207dfcbc7c2e9 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -74,15 +74,7 @@ STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_ // load block protocol methods mp_load_method(args[0], MP_QSTR_readblocks, vfs->readblocks); mp_load_method_maybe(args[0], MP_QSTR_writeblocks, vfs->writeblocks); - mp_load_method_maybe(args[0], MP_QSTR_ioctl, vfs->u.ioctl); - if (vfs->u.ioctl[0] != MP_OBJ_NULL) { - // device supports new block protocol, so indicate it - vfs->flags |= FSUSER_HAVE_IOCTL; - } else { - // no ioctl method, so assume the device uses the old block protocol - mp_load_method_maybe(args[0], MP_QSTR_sync, vfs->u.old.sync); - mp_load_method(args[0], MP_QSTR_count, vfs->u.old.count); - } + mp_load_method(args[0], MP_QSTR_ioctl, vfs->ioctl); // mount the block device so the VFS methods can be used FRESULT res = f_mount(&vfs->fatfs); diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index ba2915386fdb4..fd265897f2080 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -34,7 +34,6 @@ // these are the values for fs_user_mount_t.flags #define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func #define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount -#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl #define FSUSER_NO_FILESYSTEM (0x0008) // the block device has no filesystem on it typedef struct _fs_user_mount_t { @@ -42,14 +41,7 @@ typedef struct _fs_user_mount_t { uint16_t flags; mp_obj_t readblocks[4]; mp_obj_t writeblocks[4]; - // new protocol uses just ioctl, old uses sync (optional) and count - union { - mp_obj_t ioctl[4]; - struct { - mp_obj_t sync[2]; - mp_obj_t count[2]; - } old; - } u; + mp_obj_t ioctl[4]; FATFS fatfs; } fs_user_mount_t; diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 712038f3e97ad..135ef3a6df865 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -53,59 +53,6 @@ STATIC fs_user_mount_t *disk_get_device(void *bdev) { return (fs_user_mount_t*)bdev; } -/*-----------------------------------------------------------------------*/ -/* Initialize a Drive */ -/*-----------------------------------------------------------------------*/ - -STATIC -DSTATUS disk_initialize ( - bdev_t pdrv /* Physical drive nmuber (0..) */ -) -{ - fs_user_mount_t *vfs = disk_get_device(pdrv); - if (vfs == NULL) { - return STA_NOINIT; - } - - if (vfs->flags & FSUSER_HAVE_IOCTL) { - // new protocol with ioctl; call ioctl(INIT, 0) - vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_INIT); - vfs->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused - mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->u.ioctl); - if (ret != mp_const_none && MP_OBJ_SMALL_INT_VALUE(ret) != 0) { - // error initialising - return STA_NOINIT; - } - } - - if (vfs->writeblocks[0] == MP_OBJ_NULL) { - return STA_PROTECT; - } else { - return 0; - } -} - -/*-----------------------------------------------------------------------*/ -/* Get Disk Status */ -/*-----------------------------------------------------------------------*/ - -STATIC -DSTATUS disk_status ( - bdev_t pdrv /* Physical drive nmuber (0..) */ -) -{ - fs_user_mount_t *vfs = disk_get_device(pdrv); - if (vfs == NULL) { - return STA_NOINIT; - } - - if (vfs->writeblocks[0] == MP_OBJ_NULL) { - return STA_PROTECT; - } else { - return 0; - } -} - /*-----------------------------------------------------------------------*/ /* Read Sector(s) */ /*-----------------------------------------------------------------------*/ @@ -191,93 +138,73 @@ DRESULT disk_ioctl ( return RES_PARERR; } - if (vfs->flags & FSUSER_HAVE_IOCTL) { - // new protocol with ioctl - switch (cmd) { - case CTRL_SYNC: - vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SYNC); - vfs->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused - mp_call_method_n_kw(2, 0, vfs->u.ioctl); - return RES_OK; + switch (cmd) { + case CTRL_SYNC: + vfs->ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SYNC); + vfs->ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused + mp_call_method_n_kw(2, 0, vfs->ioctl); + return RES_OK; + + case GET_SECTOR_COUNT: { + vfs->ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SEC_COUNT); + vfs->ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused + mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->ioctl); + *((DWORD*)buff) = mp_obj_get_int(ret); + return RES_OK; + } - case GET_SECTOR_COUNT: { - vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SEC_COUNT); - vfs->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused - mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->u.ioctl); - *((DWORD*)buff) = mp_obj_get_int(ret); - return RES_OK; + case GET_SECTOR_SIZE: { + vfs->ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SEC_SIZE); + vfs->ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused + mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->ioctl); + if (ret == mp_const_none) { + // Default sector size + *((WORD*)buff) = 512; + } else { + *((WORD*)buff) = mp_obj_get_int(ret); } + #if _MAX_SS != _MIN_SS + // need to store ssize because we use it in disk_read/disk_write + vfs->fatfs.ssize = *((WORD*)buff); + #endif + return RES_OK; + } - case GET_SECTOR_SIZE: { - vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SEC_SIZE); - vfs->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused - mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->u.ioctl); - if (ret == mp_const_none) { - // Default sector size - *((WORD*)buff) = 512; - } else { - *((WORD*)buff) = mp_obj_get_int(ret); - } - #if _MAX_SS != _MIN_SS - // need to store ssize because we use it in disk_read/disk_write - vfs->fatfs.ssize = *((WORD*)buff); - #endif - return RES_OK; + case GET_BLOCK_SIZE: + *((DWORD*)buff) = 1; // erase block size in units of sector size + return RES_OK; + + case IOCTL_INIT: { + // call ioctl(INIT, 0) + DSTATUS status; + vfs->ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_INIT); + vfs->ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused + mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->ioctl); + if (ret != mp_const_none && MP_OBJ_SMALL_INT_VALUE(ret) != 0) { + // error initialising + status = STA_NOINIT; + } else if (vfs->writeblocks[0] == MP_OBJ_NULL) { + status = STA_PROTECT; + } else { + status = 0; } - - case GET_BLOCK_SIZE: - *((DWORD*)buff) = 1; // erase block size in units of sector size - return RES_OK; - - case IOCTL_INIT: - *((DSTATUS*)buff) = disk_initialize(pdrv); - return RES_OK; - - case IOCTL_STATUS: - *((DSTATUS*)buff) = disk_status(pdrv); - return RES_OK; - - default: - return RES_PARERR; + *((DSTATUS*)buff) = status; + return RES_OK; } - } else { - // old protocol with sync and count - switch (cmd) { - case CTRL_SYNC: - if (vfs->u.old.sync[0] != MP_OBJ_NULL) { - mp_call_method_n_kw(0, 0, vfs->u.old.sync); - } - return RES_OK; - case GET_SECTOR_COUNT: { - mp_obj_t ret = mp_call_method_n_kw(0, 0, vfs->u.old.count); - *((DWORD*)buff) = mp_obj_get_int(ret); - return RES_OK; + case IOCTL_STATUS: { + DSTATUS status; + if (vfs->writeblocks[0] == MP_OBJ_NULL) { + status = STA_PROTECT; + } else { + status = 0; } - - case GET_SECTOR_SIZE: - *((WORD*)buff) = 512; // old protocol had fixed sector size - #if _MAX_SS != _MIN_SS - // need to store ssize because we use it in disk_read/disk_write - vfs->fatfs.ssize = 512; - #endif - return RES_OK; - - case GET_BLOCK_SIZE: - *((DWORD*)buff) = 1; // erase block size in units of sector size - return RES_OK; - - case IOCTL_INIT: - *((DSTATUS*)buff) = disk_initialize(pdrv); - return RES_OK; - - case IOCTL_STATUS: - *((DSTATUS*)buff) = disk_status(pdrv); - return RES_OK; - - default: - return RES_PARERR; + *((DSTATUS*)buff) = status; + return RES_OK; } + + default: + return RES_PARERR; } } diff --git a/ports/cc3200/mods/pybflash.c b/ports/cc3200/mods/pybflash.c index 51f4cb5172784..ef5b589cce5b5 100644 --- a/ports/cc3200/mods/pybflash.c +++ b/ports/cc3200/mods/pybflash.c @@ -96,7 +96,7 @@ const mp_obj_type_t pyb_flash_type = { void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->base.type = &mp_fat_vfs_type; - vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->flags |= FSUSER_NATIVE; vfs->fatfs.drv = vfs; vfs->readblocks[0] = (mp_obj_t)&pyb_flash_readblocks_obj; vfs->readblocks[1] = (mp_obj_t)&pyb_flash_obj; @@ -104,6 +104,6 @@ void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->writeblocks[0] = (mp_obj_t)&pyb_flash_writeblocks_obj; vfs->writeblocks[1] = (mp_obj_t)&pyb_flash_obj; vfs->writeblocks[2] = (mp_obj_t)sflash_disk_write; // native version - vfs->u.ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj; - vfs->u.ioctl[1] = (mp_obj_t)&pyb_flash_obj; + vfs->ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj; + vfs->ioctl[1] = (mp_obj_t)&pyb_flash_obj; } diff --git a/ports/stm32/sdcard.c b/ports/stm32/sdcard.c index 27e7a34b2645b..8eb3dd4316b13 100644 --- a/ports/stm32/sdcard.c +++ b/ports/stm32/sdcard.c @@ -573,7 +573,7 @@ const mp_obj_type_t pyb_sdcard_type = { void sdcard_init_vfs(fs_user_mount_t *vfs, int part) { vfs->base.type = &mp_fat_vfs_type; - vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->flags |= FSUSER_NATIVE; vfs->fatfs.drv = vfs; vfs->fatfs.part = part; vfs->readblocks[0] = (mp_obj_t)&pyb_sdcard_readblocks_obj; @@ -582,8 +582,8 @@ void sdcard_init_vfs(fs_user_mount_t *vfs, int part) { vfs->writeblocks[0] = (mp_obj_t)&pyb_sdcard_writeblocks_obj; vfs->writeblocks[1] = (mp_obj_t)&pyb_sdcard_obj; vfs->writeblocks[2] = (mp_obj_t)sdcard_write_blocks; // native version - vfs->u.ioctl[0] = (mp_obj_t)&pyb_sdcard_ioctl_obj; - vfs->u.ioctl[1] = (mp_obj_t)&pyb_sdcard_obj; + vfs->ioctl[0] = (mp_obj_t)&pyb_sdcard_ioctl_obj; + vfs->ioctl[1] = (mp_obj_t)&pyb_sdcard_obj; } #endif // MICROPY_HW_HAS_SDCARD diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 761db0b525e41..7d5632490fe38 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -274,7 +274,7 @@ const mp_obj_type_t pyb_flash_type = { void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->base.type = &mp_fat_vfs_type; - vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->flags |= FSUSER_NATIVE; vfs->fatfs.drv = vfs; vfs->fatfs.part = 1; // flash filesystem lives on first partition vfs->readblocks[0] = (mp_obj_t)&pyb_flash_readblocks_obj; @@ -283,8 +283,8 @@ void pyb_flash_init_vfs(fs_user_mount_t *vfs) { vfs->writeblocks[0] = (mp_obj_t)&pyb_flash_writeblocks_obj; vfs->writeblocks[1] = (mp_obj_t)&pyb_flash_obj; vfs->writeblocks[2] = (mp_obj_t)storage_write_blocks; // native version - vfs->u.ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj; - vfs->u.ioctl[1] = (mp_obj_t)&pyb_flash_obj; + vfs->ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj; + vfs->ioctl[1] = (mp_obj_t)&pyb_flash_obj; } #endif diff --git a/tests/extmod/vfs_fat_oldproto.py b/tests/extmod/vfs_fat_oldproto.py deleted file mode 100644 index 3caaa368db009..0000000000000 --- a/tests/extmod/vfs_fat_oldproto.py +++ /dev/null @@ -1,58 +0,0 @@ -try: - import uerrno - import uos -except ImportError: - print("SKIP") - raise SystemExit - -try: - uos.VfsFat -except AttributeError: - print("SKIP") - raise SystemExit - -class RAMFS_OLD: - - SEC_SIZE = 512 - - def __init__(self, blocks): - self.data = bytearray(blocks * self.SEC_SIZE) - - def readblocks(self, n, buf): - #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) - for i in range(len(buf)): - buf[i] = self.data[n * self.SEC_SIZE + i] - - def writeblocks(self, n, buf): - #print("writeblocks(%s, %x)" % (n, id(buf))) - for i in range(len(buf)): - self.data[n * self.SEC_SIZE + i] = buf[i] - - def sync(self): - pass - - def count(self): - return len(self.data) // self.SEC_SIZE - - -try: - bdev = RAMFS_OLD(50) -except MemoryError: - print("SKIP") - raise SystemExit - -uos.VfsFat.mkfs(bdev) -vfs = uos.VfsFat(bdev) -uos.mount(vfs, "/ramdisk") - -# file io -with vfs.open("file.txt", "w") as f: - f.write("hello!") - -print(list(vfs.ilistdir())) - -with vfs.open("file.txt", "r") as f: - print(f.read()) - -vfs.remove("file.txt") -print(list(vfs.ilistdir())) diff --git a/tests/extmod/vfs_fat_oldproto.py.exp b/tests/extmod/vfs_fat_oldproto.py.exp deleted file mode 100644 index b974683167c17..0000000000000 --- a/tests/extmod/vfs_fat_oldproto.py.exp +++ /dev/null @@ -1,3 +0,0 @@ -[('file.txt', 32768, 0, 6)] -hello! -[]