fsusermount.c 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * 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.
 */

27
28
29
#include "py/mpconfig.h"
#if MICROPY_FSUSERMOUNT

30
31
#include "py/nlr.h"
#include "py/runtime.h"
32
#include "lib/fatfs/ff.h"
33
34
#include "fsusermount.h"

35
STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    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;
54
55
56
57
        if (MP_STATE_PORT(fs_user_mount) != NULL) {
            res = f_mount(NULL, MP_STATE_PORT(fs_user_mount)->str, 0);
            m_del_obj(fs_user_mount_t, MP_STATE_PORT(fs_user_mount));
            MP_STATE_PORT(fs_user_mount) = NULL;
58
59
60
61
62
63
        }
        if (res != FR_OK) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount"));
        }
    } else {
        // mount
64
        if (MP_STATE_PORT(fs_user_mount) != NULL) {
65
66
67
68
            nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "device already mounted"));
        }

        // create new object
69
70
71
72
        fs_user_mount_t *vfs;
        MP_STATE_PORT(fs_user_mount) = vfs = m_new_obj(fs_user_mount_t);
        vfs->str = mnt_str;
        vfs->len = mnt_len;
73
74

        // load block protocol methods
75
76
77
78
        mp_load_method(device, MP_QSTR_readblocks, vfs->readblocks);
        mp_load_method_maybe(device, MP_QSTR_writeblocks, vfs->writeblocks);
        mp_load_method_maybe(device, MP_QSTR_sync, vfs->sync);
        mp_load_method(device, MP_QSTR_count, vfs->count);
79
80
81
82
83
84

        // 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) {
85
            vfs->writeblocks[0] = MP_OBJ_NULL;
86
87
        }

88
89
        // mount the block device (if mkfs, only pre-mount)
        FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs);
90
91
        // check the result
        if (res == FR_OK) {
92
93
94
            if (mkfs) {
                goto mkfs;
            }
95
        } else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
96
mkfs:
97
            res = f_mkfs(vfs->str, 1, 0);
98
            if (res != FR_OK) {
99
mkfs_error:
100
101
                nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs"));
            }
102
103
104
105
106
107
108
109
            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;
                }
                MP_STATE_PORT(fs_user_mount) = NULL;
            }
110
111
112
113
114
        } else {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount"));
        }

        /*
115
        if (vfs->writeblocks[0] == MP_OBJ_NULL) {
116
117
118
119
120
121
            printf("mounted read-only");
        } else {
            printf("mounted read-write");
        }
        DWORD nclst;
        FATFS *fatfs;
122
123
        f_getfree(vfs->str, &nclst, &fatfs);
        printf(" on %s with %u bytes free\n", vfs->str, (uint)(nclst * fatfs->csize * 512));
124
125
126
127
        */
    }
    return mp_const_none;
}
128
129
130
131
132
133
134
135
136
137

STATIC mp_obj_t fatfs_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    return fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
}
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);

STATIC mp_obj_t fatfs_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    return fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
}
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs);
138
139

#endif // MICROPY_FSUSERMOUNT