Skip to content

Commit f5d0475

Browse files
committed
stmhal: Put fs_user_mount pointer in root ptr section of global state.
Should fix issue adafruit#1393.
1 parent 92e9a5e commit f5d0475

6 files changed

Lines changed: 45 additions & 48 deletions

File tree

stmhal/diskio.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ DSTATUS disk_initialize (
7878
#endif
7979

8080
case PD_USER:
81-
if (fs_user_mount == NULL) {
81+
if (MP_STATE_PORT(fs_user_mount) == NULL) {
8282
return STA_NODISK;
8383
}
84-
if (fs_user_mount->writeblocks[0] == MP_OBJ_NULL) {
84+
if (MP_STATE_PORT(fs_user_mount)->writeblocks[0] == MP_OBJ_NULL) {
8585
return STA_PROTECT;
8686
}
8787
return 0;
@@ -110,10 +110,10 @@ DSTATUS disk_status (
110110
#endif
111111

112112
case PD_USER:
113-
if (fs_user_mount == NULL) {
113+
if (MP_STATE_PORT(fs_user_mount) == NULL) {
114114
return STA_NODISK;
115115
}
116-
if (fs_user_mount->writeblocks[0] == MP_OBJ_NULL) {
116+
if (MP_STATE_PORT(fs_user_mount)->writeblocks[0] == MP_OBJ_NULL) {
117117
return STA_PROTECT;
118118
}
119119
return 0;
@@ -151,13 +151,13 @@ DRESULT disk_read (
151151
#endif
152152

153153
case PD_USER:
154-
if (fs_user_mount == NULL) {
154+
if (MP_STATE_PORT(fs_user_mount) == NULL) {
155155
// nothing mounted
156156
return RES_ERROR;
157157
}
158-
fs_user_mount->readblocks[2] = MP_OBJ_NEW_SMALL_INT(sector);
159-
fs_user_mount->readblocks[3] = mp_obj_new_bytearray_by_ref(count * 512, buff);
160-
mp_call_method_n_kw(2, 0, fs_user_mount->readblocks);
158+
MP_STATE_PORT(fs_user_mount)->readblocks[2] = MP_OBJ_NEW_SMALL_INT(sector);
159+
MP_STATE_PORT(fs_user_mount)->readblocks[3] = mp_obj_new_bytearray_by_ref(count * 512, buff);
160+
mp_call_method_n_kw(2, 0, MP_STATE_PORT(fs_user_mount)->readblocks);
161161
return RES_OK;
162162
}
163163

@@ -194,17 +194,17 @@ DRESULT disk_write (
194194
#endif
195195

196196
case PD_USER:
197-
if (fs_user_mount == NULL) {
197+
if (MP_STATE_PORT(fs_user_mount) == NULL) {
198198
// nothing mounted
199199
return RES_ERROR;
200200
}
201-
if (fs_user_mount->writeblocks[0] == MP_OBJ_NULL) {
201+
if (MP_STATE_PORT(fs_user_mount)->writeblocks[0] == MP_OBJ_NULL) {
202202
// read-only block device
203203
return RES_ERROR;
204204
}
205-
fs_user_mount->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(sector);
206-
fs_user_mount->writeblocks[3] = mp_obj_new_bytearray_by_ref(count * 512, (void*)buff);
207-
mp_call_method_n_kw(2, 0, fs_user_mount->writeblocks);
205+
MP_STATE_PORT(fs_user_mount)->writeblocks[2] = MP_OBJ_NEW_SMALL_INT(sector);
206+
MP_STATE_PORT(fs_user_mount)->writeblocks[3] = mp_obj_new_bytearray_by_ref(count * 512, (void*)buff);
207+
mp_call_method_n_kw(2, 0, MP_STATE_PORT(fs_user_mount)->writeblocks);
208208
return RES_OK;
209209
}
210210

@@ -251,14 +251,14 @@ DRESULT disk_ioctl (
251251
#endif
252252

253253
case PD_USER:
254-
if (fs_user_mount == NULL) {
254+
if (MP_STATE_PORT(fs_user_mount) == NULL) {
255255
// nothing mounted
256256
return RES_ERROR;
257257
}
258258
switch (cmd) {
259259
case CTRL_SYNC:
260-
if (fs_user_mount->sync[0] != MP_OBJ_NULL) {
261-
mp_call_method_n_kw(0, 0, fs_user_mount->sync);
260+
if (MP_STATE_PORT(fs_user_mount)->sync[0] != MP_OBJ_NULL) {
261+
mp_call_method_n_kw(0, 0, MP_STATE_PORT(fs_user_mount)->sync);
262262
}
263263
return RES_OK;
264264

@@ -267,7 +267,7 @@ DRESULT disk_ioctl (
267267
return RES_OK;
268268

269269
case GET_SECTOR_COUNT: {
270-
mp_obj_t ret = mp_call_method_n_kw(0, 0, fs_user_mount->count);
270+
mp_obj_t ret = mp_call_method_n_kw(0, 0, MP_STATE_PORT(fs_user_mount)->count);
271271
*((DWORD*)buff) = mp_obj_get_int(ret);
272272
return RES_OK;
273273
}

stmhal/ffconf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <string.h>
2828

29-
#include "py/obj.h"
29+
#include "py/mpstate.h"
3030
#include "lib/fatfs/ff.h"
3131
#include "ffconf.h"
3232
#include "fsusermount.h"
@@ -63,7 +63,7 @@ int ff_get_ldnumber (const TCHAR **path) {
6363
return 0;
6464
} else if (check_path(path, "/sd", 3)) {
6565
return 1;
66-
} else if (fs_user_mount != NULL && check_path(path, fs_user_mount->str, fs_user_mount->len)) {
66+
} else if (MP_STATE_PORT(fs_user_mount) != NULL && check_path(path, MP_STATE_PORT(fs_user_mount)->str, MP_STATE_PORT(fs_user_mount)->len)) {
6767
return 2;
6868
} else {
6969
return -1;
@@ -78,7 +78,7 @@ void ff_get_volname(BYTE vol, TCHAR **dest) {
7878
memcpy(*dest, "/sd", 3);
7979
*dest += 3;
8080
} else {
81-
memcpy(*dest, fs_user_mount->str, fs_user_mount->len);
82-
*dest += fs_user_mount->len;
81+
memcpy(*dest, MP_STATE_PORT(fs_user_mount)->str, MP_STATE_PORT(fs_user_mount)->len);
82+
*dest += MP_STATE_PORT(fs_user_mount)->len;
8383
}
8484
}

stmhal/fsusermount.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
#include "lib/fatfs/ff.h"
3030
#include "fsusermount.h"
3131

32-
// for user-mountable block device
33-
fs_user_mount_t *fs_user_mount;
34-
3532
STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
3633
static const mp_arg_t allowed_args[] = {
3734
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
@@ -51,46 +48,46 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
5148
if (device == mp_const_none) {
5249
// umount
5350
FRESULT res = FR_NO_FILESYSTEM;
54-
if (fs_user_mount != NULL) {
55-
res = f_mount(NULL, fs_user_mount->str, 0);
56-
m_del_obj(fs_user_mount_t, fs_user_mount);
57-
fs_user_mount = NULL;
51+
if (MP_STATE_PORT(fs_user_mount) != NULL) {
52+
res = f_mount(NULL, MP_STATE_PORT(fs_user_mount)->str, 0);
53+
m_del_obj(fs_user_mount_t, MP_STATE_PORT(fs_user_mount));
54+
MP_STATE_PORT(fs_user_mount) = NULL;
5855
}
5956
if (res != FR_OK) {
6057
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount"));
6158
}
6259
} else {
6360
// mount
64-
if (fs_user_mount != NULL) {
61+
if (MP_STATE_PORT(fs_user_mount) != NULL) {
6562
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "device already mounted"));
6663
}
6764

6865
// create new object
69-
fs_user_mount = m_new_obj(fs_user_mount_t);
70-
fs_user_mount->str = mnt_str;
71-
fs_user_mount->len = mnt_len;
66+
MP_STATE_PORT(fs_user_mount) = m_new_obj(fs_user_mount_t);
67+
MP_STATE_PORT(fs_user_mount)->str = mnt_str;
68+
MP_STATE_PORT(fs_user_mount)->len = mnt_len;
7269

7370
// load block protocol methods
74-
mp_load_method(device, MP_QSTR_readblocks, fs_user_mount->readblocks);
75-
mp_load_method_maybe(device, MP_QSTR_writeblocks, fs_user_mount->writeblocks);
76-
mp_load_method_maybe(device, MP_QSTR_sync, fs_user_mount->sync);
77-
mp_load_method(device, MP_QSTR_count, fs_user_mount->count);
71+
mp_load_method(device, MP_QSTR_readblocks, MP_STATE_PORT(fs_user_mount)->readblocks);
72+
mp_load_method_maybe(device, MP_QSTR_writeblocks, MP_STATE_PORT(fs_user_mount)->writeblocks);
73+
mp_load_method_maybe(device, MP_QSTR_sync, MP_STATE_PORT(fs_user_mount)->sync);
74+
mp_load_method(device, MP_QSTR_count, MP_STATE_PORT(fs_user_mount)->count);
7875

7976
// Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
8077
// User can specify read-only device by:
8178
// 1. readonly=True keyword argument
8279
// 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
8380
if (args[0].u_bool) {
84-
fs_user_mount->writeblocks[0] = MP_OBJ_NULL;
81+
MP_STATE_PORT(fs_user_mount)->writeblocks[0] = MP_OBJ_NULL;
8582
}
8683

8784
// mount the block device
88-
FRESULT res = f_mount(&fs_user_mount->fatfs, fs_user_mount->str, 1);
85+
FRESULT res = f_mount(&MP_STATE_PORT(fs_user_mount)->fatfs, MP_STATE_PORT(fs_user_mount)->str, 1);
8986

9087
// check the result
9188
if (res == FR_OK) {
9289
} else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
93-
res = f_mkfs(fs_user_mount->str, 1, 0);
90+
res = f_mkfs(MP_STATE_PORT(fs_user_mount)->str, 1, 0);
9491
if (res != FR_OK) {
9592
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs"));
9693
}
@@ -99,15 +96,15 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
9996
}
10097

10198
/*
102-
if (fs_user_mount->writeblocks[0] == MP_OBJ_NULL) {
99+
if (MP_STATE_PORT(fs_user_mount)->writeblocks[0] == MP_OBJ_NULL) {
103100
printf("mounted read-only");
104101
} else {
105102
printf("mounted read-write");
106103
}
107104
DWORD nclst;
108105
FATFS *fatfs;
109-
f_getfree(fs_user_mount.str, &nclst, &fatfs);
110-
printf(" on %s with %u bytes free\n", fs_user_mount.str, (uint)(nclst * fatfs->csize * 512));
106+
f_getfree(MP_STATE_PORT(fs_user_mount)->str, &nclst, &fatfs);
107+
printf(" on %s with %u bytes free\n", MP_STATE_PORT(fs_user_mount)->str, (uint)(nclst * fatfs->csize * 512));
111108
*/
112109
}
113110
return mp_const_none;

stmhal/fsusermount.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,4 @@ typedef struct _fs_user_mount_t {
3434
FATFS fatfs;
3535
} fs_user_mount_t;
3636

37-
extern fs_user_mount_t *fs_user_mount;
38-
3937
MP_DECLARE_CONST_FUN_OBJ(pyb_mount_obj);

stmhal/moduos.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
#include <stdint.h>
2828
#include <string.h>
2929

30-
#include "py/nlr.h"
31-
#include "py/obj.h"
30+
#include "py/mpstate.h"
3231
#include "py/objtuple.h"
3332
#include "py/objstr.h"
3433
#include "genhdr/mpversion.h"
@@ -148,8 +147,8 @@ STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) {
148147
if (sd_in_root()) {
149148
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_sd));
150149
}
151-
if (fs_user_mount != NULL) {
152-
mp_obj_list_append(dir_list, mp_obj_new_str(fs_user_mount->str + 1, fs_user_mount->len - 1, false));
150+
if (MP_STATE_PORT(fs_user_mount) != NULL) {
151+
mp_obj_list_append(dir_list, mp_obj_new_str(MP_STATE_PORT(fs_user_mount)->str + 1, MP_STATE_PORT(fs_user_mount)->len - 1, false));
153152
}
154153
return dir_list;
155154
}

stmhal/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ extern const struct _mp_obj_module_t mp_module_network;
166166
/* pointers to all CAN objects (if they have been created) */ \
167167
struct _pyb_can_obj_t *pyb_can_obj_all[2]; \
168168
\
169+
/* for user-mountable block device */ \
170+
struct _fs_user_mount_t *fs_user_mount; \
171+
\
169172
/* list of registered NICs */ \
170173
mp_obj_list_t mod_network_nic_list; \
171174

0 commit comments

Comments
 (0)