1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2019-2020 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 
27 #include "py/runtime.h"
28 #include "py/mphal.h"
29 #include "shared/timeutils/timeutils.h"
30 #include "extmod/vfs.h"
31 #include "extmod/vfs_lfs.h"
32 
33 #if MICROPY_VFS && (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2)
34 
35 enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead, LFS_MAKE_ARG_mtime };
36 
37 static const mp_arg_t lfs_make_allowed_args[] = {
38     { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
39     { MP_QSTR_readsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
40     { MP_QSTR_progsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
41     { MP_QSTR_lookahead, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
42     { MP_QSTR_mtime, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
43 };
44 
45 #if MICROPY_VFS_LFS1
46 
47 #include "lib/littlefs/lfs1.h"
48 
49 #define LFS_BUILD_VERSION (1)
50 #define LFSx_MACRO(s) LFS1##s
51 #define LFSx_API(s) lfs1_##s
52 #define MP_VFS_LFSx(s) mp_vfs_lfs1_##s
53 #define MP_OBJ_VFS_LFSx mp_obj_vfs_lfs1_t
54 #define MP_OBJ_VFS_LFSx_FILE mp_obj_vfs_lfs1_file_t
55 #define MP_TYPE_VFS_LFSx mp_type_vfs_lfs1
56 #define MP_TYPE_VFS_LFSx_(s) mp_type_vfs_lfs1##s
57 
58 typedef struct _mp_obj_vfs_lfs1_t {
59     mp_obj_base_t base;
60     mp_vfs_blockdev_t blockdev;
61     vstr_t cur_dir;
62     struct lfs1_config config;
63     lfs1_t lfs;
64 } mp_obj_vfs_lfs1_t;
65 
66 typedef struct _mp_obj_vfs_lfs1_file_t {
67     mp_obj_base_t base;
68     mp_obj_vfs_lfs1_t *vfs;
69     lfs1_file_t file;
70     struct lfs1_file_config cfg;
71     uint8_t file_buffer[0];
72 } mp_obj_vfs_lfs1_file_t;
73 
74 const char *mp_vfs_lfs1_make_path(mp_obj_vfs_lfs1_t *self, mp_obj_t path_in);
75 mp_obj_t mp_vfs_lfs1_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in);
76 
77 #include "extmod/vfs_lfsx.c"
78 #include "extmod/vfs_lfsx_file.c"
79 
80 #undef LFS_BUILD_VERSION
81 #undef LFSx_MACRO
82 #undef LFSx_API
83 #undef MP_VFS_LFSx
84 #undef MP_OBJ_VFS_LFSx
85 #undef MP_OBJ_VFS_LFSx_FILE
86 #undef MP_TYPE_VFS_LFSx
87 #undef MP_TYPE_VFS_LFSx_
88 
89 #endif // MICROPY_VFS_LFS1
90 
91 #if MICROPY_VFS_LFS2
92 
93 #include "lib/littlefs/lfs2.h"
94 
95 #define LFS_BUILD_VERSION (2)
96 #define LFSx_MACRO(s) LFS2##s
97 #define LFSx_API(s) lfs2_##s
98 #define MP_VFS_LFSx(s) mp_vfs_lfs2_##s
99 #define MP_OBJ_VFS_LFSx mp_obj_vfs_lfs2_t
100 #define MP_OBJ_VFS_LFSx_FILE mp_obj_vfs_lfs2_file_t
101 #define MP_TYPE_VFS_LFSx mp_type_vfs_lfs2
102 #define MP_TYPE_VFS_LFSx_(s) mp_type_vfs_lfs2##s
103 
104 // Attribute ids for lfs2_attr.type.
105 #define LFS_ATTR_MTIME (1) // 64-bit little endian, nanoseconds since 1970/1/1
106 
107 typedef struct _mp_obj_vfs_lfs2_t {
108     mp_obj_base_t base;
109     mp_vfs_blockdev_t blockdev;
110     bool enable_mtime;
111     vstr_t cur_dir;
112     struct lfs2_config config;
113     lfs2_t lfs;
114 } mp_obj_vfs_lfs2_t;
115 
116 typedef struct _mp_obj_vfs_lfs2_file_t {
117     mp_obj_base_t base;
118     mp_obj_vfs_lfs2_t *vfs;
119     uint8_t mtime[8];
120     lfs2_file_t file;
121     struct lfs2_file_config cfg;
122     struct lfs2_attr attrs[1];
123     uint8_t file_buffer[0];
124 } mp_obj_vfs_lfs2_file_t;
125 
126 const char *mp_vfs_lfs2_make_path(mp_obj_vfs_lfs2_t *self, mp_obj_t path_in);
127 mp_obj_t mp_vfs_lfs2_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in);
128 
lfs_get_mtime(uint8_t buf[8])129 STATIC void lfs_get_mtime(uint8_t buf[8]) {
130     // On-disk storage of timestamps uses 1970 as the Epoch, so convert from host's Epoch.
131     uint64_t ns = timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(mp_hal_time_ns());
132     // Store "ns" to "buf" in little-endian format (essentially htole64).
133     for (size_t i = 0; i < 8; ++i) {
134         buf[i] = ns;
135         ns >>= 8;
136     }
137 }
138 
139 #include "extmod/vfs_lfsx.c"
140 #include "extmod/vfs_lfsx_file.c"
141 
142 #endif // MICROPY_VFS_LFS2
143 
144 #endif // MICROPY_VFS && (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2)
145