1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2017 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26#ifndef MICROPY_INCLUDED_EXTMOD_VFS_H
27#define MICROPY_INCLUDED_EXTMOD_VFS_H
28
29#include "py/lexer.h"
30#include "py/obj.h"
31
32// return values of mp_vfs_lookup_path
33// ROOT is 0 so that the default current directory is the root directory
34#define MP_VFS_NONE ((mp_vfs_mount_t *)1)
35#define MP_VFS_ROOT ((mp_vfs_mount_t *)0)
36
37// MicroPython's port-standardized versions of stat constants
38#define MP_S_IFDIR (0x4000)
39#define MP_S_IFREG (0x8000)
40
41// these are the values for mp_vfs_blockdev_t.flags
42#define MP_BLOCKDEV_FLAG_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func
43#define MP_BLOCKDEV_FLAG_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount
44#define MP_BLOCKDEV_FLAG_HAVE_IOCTL (0x0004) // new protocol with ioctl
45#define MP_BLOCKDEV_FLAG_NO_FILESYSTEM (0x0008) // the block device has no filesystem on it
46
47// constants for block protocol ioctl
48#define MP_BLOCKDEV_IOCTL_INIT (1)
49#define MP_BLOCKDEV_IOCTL_DEINIT (2)
50#define MP_BLOCKDEV_IOCTL_SYNC (3)
51#define MP_BLOCKDEV_IOCTL_BLOCK_COUNT (4)
52#define MP_BLOCKDEV_IOCTL_BLOCK_SIZE (5)
53#define MP_BLOCKDEV_IOCTL_BLOCK_ERASE (6)
54
55// At the moment the VFS protocol just has import_stat, but could be extended to other methods
56typedef struct _mp_vfs_proto_t {
57 mp_import_stat_t (*import_stat)(void *self, const char *path);
58} mp_vfs_proto_t;
59
60typedef struct _mp_vfs_blockdev_t {
61 uint16_t flags;
62 size_t block_size;
63 mp_obj_t readblocks[5];
64 mp_obj_t writeblocks[5];
65 // new protocol uses just ioctl, old uses sync (optional) and count
66 union {
67 mp_obj_t ioctl[4];
68 struct {
69 mp_obj_t sync[2];
70 mp_obj_t count[2];
71 } old;
72 } u;
73} mp_vfs_blockdev_t;
74
75typedef struct _mp_vfs_mount_t {
76 const char *str; // mount point with leading /
77 size_t len;
78 mp_obj_t obj;
79 struct _mp_vfs_mount_t *next;
80} mp_vfs_mount_t;
81
82void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev);
83int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf);
84int mp_vfs_blockdev_read_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, uint8_t *buf);
85int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf);
86int mp_vfs_blockdev_write_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, const uint8_t *buf);
87mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg);
88
89mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out);
90mp_import_stat_t mp_vfs_import_stat(const char *path);
91mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
92mp_obj_t mp_vfs_umount(mp_obj_t mnt_in);
93mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
94mp_obj_t mp_vfs_chdir(mp_obj_t path_in);
95mp_obj_t mp_vfs_getcwd(void);
96mp_obj_t mp_vfs_ilistdir(size_t n_args, const mp_obj_t *args);
97mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args);
98mp_obj_t mp_vfs_mkdir(mp_obj_t path_in);
99mp_obj_t mp_vfs_remove(mp_obj_t path_in);
100mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in);
101mp_obj_t mp_vfs_rmdir(mp_obj_t path_in);
102mp_obj_t mp_vfs_stat(mp_obj_t path_in);
103mp_obj_t mp_vfs_statvfs(mp_obj_t path_in);
104
105int mp_vfs_mount_and_chdir_protected(mp_obj_t bdev, mp_obj_t mount_point);
106
107MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj);
108MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj);
109MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj);
110MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj);
111MP_DECLARE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj);
112MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_ilistdir_obj);
113MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj);
114MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj);
115MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_remove_obj);
116MP_DECLARE_CONST_FUN_OBJ_2(mp_vfs_rename_obj);
117MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj);
118MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_stat_obj);
119MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj);
120
121#endif // MICROPY_INCLUDED_EXTMOD_VFS_H
122