Skip to content

extmod/vfs_fat: Remove support for old block-dev proto with sync/count. #3861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions extmod/vfs_fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 1 addition & 9 deletions extmod/vfs_fat.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,14 @@
// 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 {
mp_obj_base_t base;
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;

Expand Down
191 changes: 59 additions & 132 deletions extmod/vfs_fat_diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
/*-----------------------------------------------------------------------*/
Expand Down Expand Up @@ -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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions ports/cc3200/mods/pybflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ 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;
vfs->readblocks[2] = (mp_obj_t)sflash_disk_read; // native version
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;
}
6 changes: 3 additions & 3 deletions ports/stm32/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
6 changes: 3 additions & 3 deletions ports/stm32/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
58 changes: 0 additions & 58 deletions tests/extmod/vfs_fat_oldproto.py

This file was deleted.

3 changes: 0 additions & 3 deletions tests/extmod/vfs_fat_oldproto.py.exp

This file was deleted.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy