Skip to content

Commit 46fd60c

Browse files
committed
Prevent infinite display update recursion and fix VFS mounting
Fixes adafruit#1529
1 parent 4e75aae commit 46fd60c

6 files changed

Lines changed: 31 additions & 29 deletions

File tree

extmod/vfs_fat_file.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,15 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar
186186
break;
187187
}
188188
}
189+
assert(vfs != NULL);
190+
if ((vfs->flags & FSUSER_USB_WRITABLE) != 0 && (mode & FA_WRITE) != 0) {
191+
mp_raise_OSError(MP_EROFS);
192+
}
189193

190194
pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t);
191195
o->base.type = type;
192196

193197
const char *fname = mp_obj_str_get_str(args[0].u_obj);
194-
assert(vfs != NULL);
195198
FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode);
196199
if (res != FR_OK) {
197200
m_del_obj(pyb_file_obj_t, o);

main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ void stop_mp(void) {
127127
#if CIRCUITPY_NETWORK
128128
network_module_deinit();
129129
#endif
130+
131+
#if MICROPY_VFS
132+
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
133+
134+
// Unmount all heap allocated vfs mounts.
135+
while (gc_nbytes(vfs) > 0) {
136+
vfs = vfs->next;
137+
}
138+
MP_STATE_VM(vfs_mount_table) = vfs;
139+
MP_STATE_VM(vfs_cur) = vfs;
140+
#endif
141+
142+
// Run any finalizers before we stop using the heap.
143+
gc_sweep_all();
130144
}
131145

132146
#define STRING_LIST(...) {__VA_ARGS__, ""}

py/circuitpy_mpconfig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
#define MICROPY_FATFS_USE_LABEL (1)
124124
#define MICROPY_FATFS_RPATH (2)
125125
#define MICROPY_FATFS_MULTI_PARTITION (1)
126-
#define MICROPY_FATFS_NUM_PERSISTENT (1)
127126

128127
// Only enable this if you really need it. It allocates a byte cache of this size.
129128
// #define MICROPY_FATFS_MAX_SS (4096)

py/runtime.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,31 +131,6 @@ void mp_init(void) {
131131
sizeof(MP_STATE_VM(fs_user_mount)) - MICROPY_FATFS_NUM_PERSISTENT);
132132
#endif
133133

134-
#if MICROPY_VFS
135-
#if MICROPY_FATFS_NUM_PERSISTENT > 0
136-
// We preserve the last MICROPY_FATFS_NUM_PERSISTENT mounts because newer
137-
// mounts are put at the front of the list.
138-
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
139-
// Count how many mounts we have.
140-
uint8_t count = 0;
141-
while (vfs != NULL) {
142-
vfs = vfs->next;
143-
count++;
144-
}
145-
// Find the vfs MICROPY_FATFS_NUM_PERSISTENT mounts from the end.
146-
vfs = MP_STATE_VM(vfs_mount_table);
147-
for (uint8_t j = 0; j < count - MICROPY_FATFS_NUM_PERSISTENT; j++) {
148-
vfs = vfs->next;
149-
}
150-
MP_STATE_VM(vfs_mount_table) = vfs;
151-
MP_STATE_VM(vfs_cur) = vfs;
152-
#else
153-
// initialise the VFS sub-system
154-
MP_STATE_VM(vfs_cur) = NULL;
155-
MP_STATE_VM(vfs_mount_table) = NULL;
156-
#endif
157-
#endif
158-
159134
#if MICROPY_PY_THREAD_GIL
160135
mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
161136
#endif

shared-module/displayio/__init__.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "shared-bindings/displayio/Display.h"
77
#include "shared-bindings/displayio/Group.h"
88
#include "shared-bindings/displayio/Palette.h"
9+
#include "supervisor/shared/autoreload.h"
910
#include "supervisor/shared/display.h"
1011
#include "supervisor/memory.h"
1112
#include "supervisor/usb.h"
@@ -18,7 +19,13 @@ static inline void swap(uint16_t* a, uint16_t* b) {
1819
*b = temp;
1920
}
2021

22+
bool refreshing_displays = false;
23+
2124
void displayio_refresh_displays(void) {
25+
if (refreshing_displays) {
26+
return;
27+
}
28+
refreshing_displays = true;
2229
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
2330
if (displays[i].display.base.type == NULL || displays[i].display.base.type == &mp_type_NoneType) {
2431
continue;
@@ -27,6 +34,7 @@ void displayio_refresh_displays(void) {
2734
displayio_display_update_backlight(display);
2835

2936
if (!displayio_display_frame_queued(display)) {
37+
refreshing_displays = false;
3038
return;
3139
}
3240
if (displayio_display_refresh_queued(display)) {
@@ -89,8 +97,9 @@ void displayio_refresh_displays(void) {
8997
index += 1;
9098
// The buffer is full, send it.
9199
if (index >= buffer_size) {
92-
if (!displayio_display_send_pixels(display, buffer, buffer_size / 2)) {
100+
if (!displayio_display_send_pixels(display, buffer, buffer_size / 2) || reload_requested) {
93101
displayio_display_finish_region_update(display);
102+
refreshing_displays = false;
94103
return;
95104
}
96105
// TODO(tannewt): Make refresh displays faster so we don't starve other
@@ -103,12 +112,14 @@ void displayio_refresh_displays(void) {
103112
// Send the remaining data.
104113
if (index && !displayio_display_send_pixels(display, buffer, index * 2)) {
105114
displayio_display_finish_region_update(display);
115+
refreshing_displays = false;
106116
return;
107117
}
108118
displayio_display_finish_region_update(display);
109119
}
110120
displayio_display_finish_refresh(display);
111121
}
122+
refreshing_displays = false;
112123
}
113124

114125
void common_hal_displayio_release_displays(void) {

supervisor/shared/filesystem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void filesystem_flush(void) {
9595
void filesystem_writable_by_python(bool writable) {
9696
fs_user_mount_t *vfs = &_internal_vfs;
9797

98-
if (writable) {
98+
if (!writable) {
9999
vfs->flags |= FSUSER_USB_WRITABLE;
100100
} else {
101101
vfs->flags &= ~FSUSER_USB_WRITABLE;

0 commit comments

Comments
 (0)