|
35 | 35 | #include "esp_system.h" |
36 | 36 | #include "esp_task.h" |
37 | 37 | #include "soc/cpu.h" |
| 38 | +#include "rom/rtc.h" |
38 | 39 |
|
39 | 40 | #include "sha2017_ota.h" |
40 | 41 | #include "esprtcmem.h" |
|
56 | 57 | #include "badge_base.h" |
57 | 58 | #include "badge_first_run.h" |
58 | 59 | #include <badge_input.h> |
| 60 | +#include <badge_button.h> |
59 | 61 | #include <badge.h> |
60 | 62 |
|
61 | 63 | // MicroPython runs as a task under FreeRTOS |
|
64 | 66 | #define MP_TASK_STACK_LEN (MP_TASK_STACK_SIZE / sizeof(StackType_t)) |
65 | 67 | #define MP_TASK_HEAP_SIZE (88 * 1024) |
66 | 68 |
|
| 69 | +//define BUTTON_SAFE_MODE ((1 << BADGE_BUTTON_A) || (1 << BADGE_BUTTON_B)) |
| 70 | +#define BUTTON_SAFE_MODE ((1 << BADGE_BUTTON_START)) |
| 71 | + |
67 | 72 | STATIC StaticTask_t mp_task_tcb; |
68 | 73 | STATIC StackType_t mp_task_stack[MP_TASK_STACK_LEN] __attribute__((aligned (8))); |
69 | 74 | STATIC uint8_t mp_task_heap[MP_TASK_HEAP_SIZE]; |
70 | 75 |
|
71 | 76 | extern uint32_t reset_cause; |
| 77 | +extern bool in_safe_mode; |
| 78 | + |
| 79 | +static const char *import_blacklist[] = { |
| 80 | + "/lib/json", |
| 81 | + "/lib/os", |
| 82 | + "/lib/socket", |
| 83 | + "/lib/struct", |
| 84 | + "/lib/time", |
| 85 | +}; |
| 86 | + |
| 87 | +mp_import_stat_t |
| 88 | +mp_import_stat(const char *path) { |
| 89 | + if (in_safe_mode) { |
| 90 | + // be more strict in which modules we would like to load |
| 91 | + if (strncmp(path, "/lib/", 5) != 0) { |
| 92 | + return MP_IMPORT_STAT_NO_EXIST; |
| 93 | + } |
| 94 | + |
| 95 | + /* check blacklist */ |
| 96 | + int i; |
| 97 | + for (i=0; i<sizeof(import_blacklist)/sizeof(const char *); i++) { |
| 98 | + if (strcmp(path, import_blacklist[i]) == 0) { |
| 99 | + return MP_IMPORT_STAT_NO_EXIST; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const char *x = index(&path[5], '/'); |
| 104 | + if (x == NULL) { |
| 105 | + // only allow directories |
| 106 | + mp_import_stat_t res = mp_vfs_import_stat(path); |
| 107 | + if (res != MP_IMPORT_STAT_DIR) { |
| 108 | + return MP_IMPORT_STAT_NO_EXIST; |
| 109 | + } |
| 110 | + return res; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return mp_vfs_import_stat(path); |
| 115 | +} |
72 | 116 |
|
73 | 117 | void mp_task(void *pvParameter) { |
74 | 118 | volatile uint32_t sp = (uint32_t)get_sp(); |
@@ -96,7 +140,7 @@ void mp_task(void *pvParameter) { |
96 | 140 | // run boot-up scripts |
97 | 141 | pyexec_frozen_module("_boot.py"); |
98 | 142 | if (pyexec_mode_kind != PYEXEC_MODE_RAW_REPL) { |
99 | | - pyexec_file("boot.py"); |
| 143 | + pyexec_frozen_module("boot.py"); |
100 | 144 | } |
101 | 145 | // if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { |
102 | 146 | // pyexec_file("main.py"); |
@@ -176,6 +220,13 @@ void app_main(void) { |
176 | 220 | } |
177 | 221 |
|
178 | 222 | } else { |
| 223 | + uint32_t reset_cause = rtc_get_reset_reason(0); |
| 224 | + if (reset_cause != DEEPSLEEP_RESET) { |
| 225 | + badge_init(); |
| 226 | + if ((badge_input_button_state & BUTTON_SAFE_MODE) == BUTTON_SAFE_MODE) { |
| 227 | + in_safe_mode = true; |
| 228 | + } |
| 229 | + } |
179 | 230 | xTaskCreateStaticPinnedToCore(mp_task, "mp_task", MP_TASK_STACK_LEN, NULL, MP_TASK_PRIORITY, |
180 | 231 | &mp_task_stack[0], &mp_task_tcb, 0); |
181 | 232 | } |
|
0 commit comments