forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathflash.c
236 lines (206 loc) · 7.99 KB
/
flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft
//
// SPDX-License-Identifier: MIT
#include "supervisor/flash.h"
#include "extmod/vfs_fat.h"
#include "py/runtime.h"
#include "lib/oofatfs/diskio.h"
#include "lib/oofatfs/ff.h"
#include "supervisor/flash.h"
#include "supervisor/shared/tick.h"
#define VFS_INDEX 0
#define PART1_START_BLOCK (0x1)
// there is a singleton Flash object
const mp_obj_type_t supervisor_flash_type;
static const mp_obj_base_t supervisor_flash_obj = {&supervisor_flash_type};
static mp_obj_t supervisor_flash_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// check arguments
mp_arg_check_num(n_args, n_kw, 0, 0, false);
// return singleton object
return (mp_obj_t)&supervisor_flash_obj;
}
static bool flash_ioctl(mp_obj_t self_in, size_t cmd, size_t arg, mp_int_t *out_value) {
if (out_value != NULL) {
*out_value = 0;
}
mp_vfs_blockdev_t *self = (mp_vfs_blockdev_t *)self_in;
switch (cmd) {
case MP_BLOCKDEV_IOCTL_INIT:
supervisor_flash_init();
break;
case MP_BLOCKDEV_IOCTL_DEINIT:
supervisor_flash_flush();
break; // TODO properly
case MP_BLOCKDEV_IOCTL_SYNC:
supervisor_flash_flush();
break;
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
*out_value = PART1_START_BLOCK;
#if CIRCUITPY_SAVES_PARTITION_SIZE > 0
if (self->size > 0) {
*out_value += self->size / self->block_size;
} else {
*out_value += supervisor_flash_get_block_count() - (self->offset / self->block_size);
}
#else
*out_value += supervisor_flash_get_block_count();
#endif
break;
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
*out_value = self->block_size;
break;
default:
return false;
}
return true;
}
static mp_obj_t supervisor_flash_obj_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
mp_int_t cmd = mp_obj_get_int(cmd_in);
mp_int_t arg = mp_obj_get_int(arg_in);
mp_int_t out_value;
if (flash_ioctl(self, cmd, arg, &out_value)) {
return MP_OBJ_NEW_SMALL_INT(out_value);
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_ioctl_obj, supervisor_flash_obj_ioctl);
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
buf[0] = boot;
if (num_blocks == 0) {
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
} else {
buf[1] = 0xff;
buf[2] = 0xff;
buf[3] = 0xff;
}
buf[4] = type;
if (num_blocks == 0) {
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
} else {
buf[5] = 0xff;
buf[6] = 0xff;
buf[7] = 0xff;
}
buf[8] = start_block;
buf[9] = start_block >> 8;
buf[10] = start_block >> 16;
buf[11] = start_block >> 24;
buf[12] = num_blocks;
buf[13] = num_blocks >> 8;
buf[14] = num_blocks >> 16;
buf[15] = num_blocks >> 24;
}
static mp_uint_t flash_read_blocks(mp_obj_t self_in, uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
if (block_num == 0) {
// fake the MBR so we can decide on our own partition table
for (int i = 0; i < 446; i++) {
dest[i] = 0;
}
mp_int_t block_count;
flash_ioctl(self_in, MP_BLOCKDEV_IOCTL_BLOCK_COUNT, 0, &block_count);
// Specifying "Big FAT12/16 CHS" allows mounting by Android
build_partition(dest + 446, 0, 0x06 /* Big FAT12/16 CHS */, PART1_START_BLOCK, block_count - PART1_START_BLOCK);
build_partition(dest + 462, 0, 0, 0, 0);
build_partition(dest + 478, 0, 0, 0, 0);
build_partition(dest + 494, 0, 0, 0, 0);
dest[510] = 0x55;
dest[511] = 0xaa;
if (num_blocks > 1) {
dest += 512;
num_blocks -= 1;
block_num += 1;
// Fall through and do a read from flash.
} else {
return 0; // Done and ok.
}
}
block_num -= PART1_START_BLOCK;
#if CIRCUITPY_SAVES_PARTITION_SIZE > 0
mp_vfs_blockdev_t *self = (mp_vfs_blockdev_t *)self_in;
block_num += self->offset / self->block_size;
#endif
return supervisor_flash_read_blocks(dest, block_num, num_blocks);
}
static volatile bool filesystem_dirty = false;
static mp_uint_t flash_write_blocks(mp_obj_t self_in, const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
if (block_num == 0) {
if (num_blocks > 1) {
return 1; // error
}
// can't write MBR, but pretend we did
return 0;
} else {
if (!filesystem_dirty) {
// Turn on ticks so that we can flush after a period of time elapses.
supervisor_enable_tick();
filesystem_dirty = true;
}
block_num -= PART1_START_BLOCK;
#if CIRCUITPY_SAVES_PARTITION_SIZE > 0
mp_vfs_blockdev_t *self = (mp_vfs_blockdev_t *)self_in;
block_num += self->offset / self->block_size;
#endif
return supervisor_flash_write_blocks(src, block_num, num_blocks);
}
}
void PLACE_IN_ITCM(supervisor_flash_flush)(void) {
#if INTERNAL_FLASH_FILESYSTEM
port_internal_flash_flush();
#else
supervisor_external_flash_flush();
#endif
// Turn off ticks now that our filesystem has been flushed.
if (filesystem_dirty) {
supervisor_disable_tick();
}
filesystem_dirty = false;
}
static mp_obj_t supervisor_flash_obj_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
mp_uint_t ret = flash_read_blocks(self, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
return MP_OBJ_NEW_SMALL_INT(ret);
}
static MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_readblocks_obj, supervisor_flash_obj_readblocks);
static mp_obj_t supervisor_flash_obj_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
mp_uint_t ret = flash_write_blocks(self, bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
return MP_OBJ_NEW_SMALL_INT(ret);
}
static MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_writeblocks_obj, supervisor_flash_obj_writeblocks);
static const mp_rom_map_elem_t supervisor_flash_obj_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_readblocks), MP_ROM_PTR(&supervisor_flash_obj_readblocks_obj) },
{ MP_ROM_QSTR(MP_QSTR_writeblocks), MP_ROM_PTR(&supervisor_flash_obj_writeblocks_obj) },
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&supervisor_flash_obj_ioctl_obj) },
};
static MP_DEFINE_CONST_DICT(supervisor_flash_obj_locals_dict, supervisor_flash_obj_locals_dict_table);
MP_DEFINE_CONST_OBJ_TYPE(
supervisor_flash_type,
MP_QSTR_Flash,
MP_TYPE_FLAG_NONE,
make_new, supervisor_flash_obj_make_new,
locals_dict, &supervisor_flash_obj_locals_dict
);
void supervisor_flash_init_vfs(fs_user_mount_t *vfs) {
vfs->base.type = &mp_fat_vfs_type;
vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NATIVE | MP_BLOCKDEV_FLAG_HAVE_IOCTL;
vfs->blockdev.block_size = supervisor_flash_get_block_size();
vfs->fatfs.drv = vfs;
vfs->fatfs.part = 1; // flash filesystem lives on first fake partition
vfs->blockdev.readblocks[0] = mp_const_none;
vfs->blockdev.readblocks[1] = (mp_obj_t)&vfs->blockdev;
vfs->blockdev.readblocks[2] = (mp_obj_t)flash_read_blocks; // native version
vfs->blockdev.writeblocks[0] = mp_const_none;
vfs->blockdev.writeblocks[1] = (mp_obj_t)&vfs->blockdev;
vfs->blockdev.writeblocks[2] = (mp_obj_t)flash_write_blocks; // native version
vfs->blockdev.u.ioctl[0] = mp_const_none;
vfs->blockdev.u.ioctl[1] = (mp_obj_t)&vfs->blockdev;
vfs->blockdev.u.ioctl[2] = (mp_obj_t)flash_ioctl; // native version
}