2014-12-27 15:20:51 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-11-24 17:43:11 -05:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#if MICROPY_FSUSERMOUNT
|
2016-02-09 17:51:47 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2015-11-24 17:43:11 -05:00
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
2015-03-04 15:35:41 -05:00
|
|
|
#include "lib/fatfs/ff.h"
|
2014-12-27 15:20:51 -05:00
|
|
|
#include "fsusermount.h"
|
|
|
|
|
2016-02-13 15:51:21 -05:00
|
|
|
fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) {
|
2014-12-27 15:20:51 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
{ MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_obj_t device = pos_args[0];
|
|
|
|
mp_obj_t mount_point = pos_args[1];
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
// get the mount point
|
|
|
|
mp_uint_t mnt_len;
|
|
|
|
const char *mnt_str = mp_obj_str_get_data(mount_point, &mnt_len);
|
|
|
|
|
|
|
|
if (device == mp_const_none) {
|
|
|
|
// umount
|
|
|
|
FRESULT res = FR_NO_FILESYSTEM;
|
2016-02-10 11:32:57 -05:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) {
|
|
|
|
fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i];
|
|
|
|
if (vfs != NULL && !memcmp(mnt_str, vfs->str, mnt_len + 1)) {
|
|
|
|
res = f_mount(NULL, vfs->str, 0);
|
|
|
|
if (vfs->flags & FSUSER_FREE_OBJ) {
|
|
|
|
m_del_obj(fs_user_mount_t, vfs);
|
|
|
|
}
|
|
|
|
MP_STATE_PORT(fs_user_mount)[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-27 15:20:51 -05:00
|
|
|
}
|
|
|
|
if (res != FR_OK) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount"));
|
|
|
|
}
|
2016-02-13 15:51:21 -05:00
|
|
|
return NULL;
|
2014-12-27 15:20:51 -05:00
|
|
|
} else {
|
|
|
|
// mount
|
2016-02-10 11:32:57 -05:00
|
|
|
size_t i = 0;
|
|
|
|
for (; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) {
|
|
|
|
if (MP_STATE_PORT(fs_user_mount)[i] == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "too many devices mounted"));
|
2014-12-27 15:20:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// create new object
|
2016-02-06 15:25:16 -05:00
|
|
|
fs_user_mount_t *vfs;
|
2016-02-10 11:32:57 -05:00
|
|
|
MP_STATE_PORT(fs_user_mount)[i] = vfs = m_new_obj(fs_user_mount_t);
|
2016-02-06 15:25:16 -05:00
|
|
|
vfs->str = mnt_str;
|
|
|
|
vfs->len = mnt_len;
|
2016-02-10 11:32:57 -05:00
|
|
|
vfs->flags = FSUSER_FREE_OBJ;
|
2014-12-27 15:20:51 -05:00
|
|
|
|
|
|
|
// load block protocol methods
|
2016-02-06 15:25:16 -05:00
|
|
|
mp_load_method(device, MP_QSTR_readblocks, vfs->readblocks);
|
|
|
|
mp_load_method_maybe(device, MP_QSTR_writeblocks, vfs->writeblocks);
|
2016-02-09 09:28:50 -05:00
|
|
|
mp_load_method_maybe(device, MP_QSTR_ioctl, vfs->u.ioctl);
|
|
|
|
if (vfs->u.ioctl[0] != MP_OBJ_NULL) {
|
|
|
|
// device supports new block protocol, so indicate it
|
2016-02-10 11:32:57 -05:00
|
|
|
vfs->flags |= FSUSER_HAVE_IOCTL;
|
2016-02-09 09:28:50 -05:00
|
|
|
} else {
|
|
|
|
// no ioctl method, so assume the device uses the old block protocol
|
|
|
|
mp_load_method_maybe(device, MP_QSTR_sync, vfs->u.old.sync);
|
|
|
|
mp_load_method(device, MP_QSTR_count, vfs->u.old.count);
|
|
|
|
}
|
2014-12-27 15:20:51 -05:00
|
|
|
|
|
|
|
// Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
|
|
|
|
// User can specify read-only device by:
|
|
|
|
// 1. readonly=True keyword argument
|
|
|
|
// 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
|
|
|
|
if (args[0].u_bool) {
|
2016-02-06 15:25:16 -05:00
|
|
|
vfs->writeblocks[0] = MP_OBJ_NULL;
|
2014-12-27 15:20:51 -05:00
|
|
|
}
|
|
|
|
|
2016-02-09 17:50:07 -05:00
|
|
|
// mount the block device (if mkfs, only pre-mount)
|
|
|
|
FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs);
|
2014-12-27 15:20:51 -05:00
|
|
|
// check the result
|
|
|
|
if (res == FR_OK) {
|
2016-02-09 17:50:07 -05:00
|
|
|
if (mkfs) {
|
|
|
|
goto mkfs;
|
|
|
|
}
|
2014-12-27 15:20:51 -05:00
|
|
|
} else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
|
2016-02-09 17:50:07 -05:00
|
|
|
mkfs:
|
2016-02-06 15:25:16 -05:00
|
|
|
res = f_mkfs(vfs->str, 1, 0);
|
2014-12-27 15:20:51 -05:00
|
|
|
if (res != FR_OK) {
|
2016-02-09 17:50:07 -05:00
|
|
|
mkfs_error:
|
2014-12-27 15:20:51 -05:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs"));
|
|
|
|
}
|
2016-02-09 17:50:07 -05:00
|
|
|
if (mkfs) {
|
|
|
|
// If requested to only mkfs, unmount pre-mounted device
|
|
|
|
res = f_mount(NULL, vfs->str, 0);
|
|
|
|
if (res != FR_OK) {
|
|
|
|
goto mkfs_error;
|
|
|
|
}
|
2016-02-10 11:32:57 -05:00
|
|
|
MP_STATE_PORT(fs_user_mount)[i] = NULL;
|
2016-02-13 15:51:21 -05:00
|
|
|
return NULL;
|
2016-02-09 17:50:07 -05:00
|
|
|
}
|
2014-12-27 15:20:51 -05:00
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-02-06 15:25:16 -05:00
|
|
|
if (vfs->writeblocks[0] == MP_OBJ_NULL) {
|
2014-12-27 15:20:51 -05:00
|
|
|
printf("mounted read-only");
|
|
|
|
} else {
|
|
|
|
printf("mounted read-write");
|
|
|
|
}
|
|
|
|
DWORD nclst;
|
|
|
|
FATFS *fatfs;
|
2016-02-06 15:25:16 -05:00
|
|
|
f_getfree(vfs->str, &nclst, &fatfs);
|
|
|
|
printf(" on %s with %u bytes free\n", vfs->str, (uint)(nclst * fatfs->csize * 512));
|
2014-12-27 15:20:51 -05:00
|
|
|
*/
|
2016-02-13 15:51:21 -05:00
|
|
|
return vfs;
|
2014-12-27 15:20:51 -05:00
|
|
|
}
|
|
|
|
}
|
2016-02-09 17:50:07 -05:00
|
|
|
|
2016-02-14 09:21:27 -05:00
|
|
|
STATIC mp_obj_t fatfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2016-02-13 15:51:21 -05:00
|
|
|
fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
|
|
|
|
return mp_const_none;
|
2016-02-09 17:50:07 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);
|
|
|
|
|
2016-02-09 17:51:47 -05:00
|
|
|
STATIC mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in) {
|
2016-02-10 11:32:57 -05:00
|
|
|
size_t i = 0;
|
2016-02-09 17:51:47 -05:00
|
|
|
if (MP_OBJ_IS_STR(bdev_or_path_in)) {
|
|
|
|
mp_uint_t mnt_len;
|
|
|
|
const char *mnt_str = mp_obj_str_get_data(bdev_or_path_in, &mnt_len);
|
2016-02-10 11:32:57 -05:00
|
|
|
for (; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) {
|
|
|
|
fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i];
|
2016-02-13 14:53:43 -05:00
|
|
|
if (vfs != NULL && !memcmp(mnt_str, vfs->str, mnt_len + 1)) {
|
2016-02-10 11:32:57 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-02-09 17:51:47 -05:00
|
|
|
}
|
2016-02-10 11:32:57 -05:00
|
|
|
} else {
|
|
|
|
for (; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) {
|
|
|
|
fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i];
|
2016-02-13 14:53:43 -05:00
|
|
|
if (vfs != NULL && bdev_or_path_in == vfs->readblocks[1]) {
|
2016-02-10 11:32:57 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) {
|
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(EINVAL)));
|
2016-02-09 17:51:47 -05:00
|
|
|
}
|
|
|
|
|
2016-02-10 11:32:57 -05:00
|
|
|
fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i];
|
|
|
|
FRESULT res = f_mount(NULL, vfs->str, 0);
|
|
|
|
if (vfs->flags & FSUSER_FREE_OBJ) {
|
|
|
|
m_del_obj(fs_user_mount_t, vfs);
|
|
|
|
}
|
|
|
|
MP_STATE_PORT(fs_user_mount)[i] = NULL;
|
2016-02-09 17:51:47 -05:00
|
|
|
if (res != FR_OK) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount"));
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(fsuser_umount_obj, fatfs_umount);
|
|
|
|
|
2016-02-14 09:21:27 -05:00
|
|
|
STATIC mp_obj_t fatfs_mkfs(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2016-02-13 15:51:21 -05:00
|
|
|
fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
|
|
|
|
return mp_const_none;
|
2016-02-09 17:50:07 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs);
|
2015-11-24 17:43:11 -05:00
|
|
|
|
|
|
|
#endif // MICROPY_FSUSERMOUNT
|