Skip to content

Commit 87ddd64

Browse files
committed
Factor out fake partition
1 parent 47212ee commit 87ddd64

File tree

5 files changed

+120
-106
lines changed

5 files changed

+120
-106
lines changed

ports/atmel-samd/supervisor/internal_flash.c

Lines changed: 34 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ uint32_t supervisor_flash_get_block_size(void) {
7070
}
7171

7272
uint32_t supervisor_flash_get_block_count(void) {
73-
return INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS;
73+
return INTERNAL_FLASH_PART1_NUM_BLOCKS;
7474
}
7575

7676
void supervisor_flash_flush(void) {
@@ -80,116 +80,54 @@ void flash_flush(void) {
8080
supervisor_flash_flush();
8181
}
8282

83-
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
84-
buf[0] = boot;
85-
86-
if (num_blocks == 0) {
87-
buf[1] = 0;
88-
buf[2] = 0;
89-
buf[3] = 0;
90-
} else {
91-
buf[1] = 0xff;
92-
buf[2] = 0xff;
93-
buf[3] = 0xff;
94-
}
95-
96-
buf[4] = type;
97-
98-
if (num_blocks == 0) {
99-
buf[5] = 0;
100-
buf[6] = 0;
101-
buf[7] = 0;
102-
} else {
103-
buf[5] = 0xff;
104-
buf[6] = 0xff;
105-
buf[7] = 0xff;
106-
}
107-
108-
buf[8] = start_block;
109-
buf[9] = start_block >> 8;
110-
buf[10] = start_block >> 16;
111-
buf[11] = start_block >> 24;
112-
113-
buf[12] = num_blocks;
114-
buf[13] = num_blocks >> 8;
115-
buf[14] = num_blocks >> 16;
116-
buf[15] = num_blocks >> 24;
117-
}
118-
11983
static int32_t convert_block_to_flash_addr(uint32_t block) {
120-
if (INTERNAL_FLASH_PART1_START_BLOCK <= block && block < INTERNAL_FLASH_PART1_START_BLOCK + INTERNAL_FLASH_PART1_NUM_BLOCKS) {
84+
if (0 <= block && block < INTERNAL_FLASH_PART1_NUM_BLOCKS) {
12185
// a block in partition 1
122-
block -= INTERNAL_FLASH_PART1_START_BLOCK;
12386
return INTERNAL_FLASH_MEM_SEG1_START_ADDR + block * FILESYSTEM_BLOCK_SIZE;
12487
}
12588
// bad block
12689
return -1;
12790
}
12891

12992
bool supervisor_flash_read_block(uint8_t *dest, uint32_t block) {
130-
if (block == 0) {
131-
// fake the MBR so we can decide on our own partition table
132-
133-
for (int i = 0; i < 446; i++) {
134-
dest[i] = 0;
135-
}
136-
137-
build_partition(dest + 446, 0, 0x01 /* FAT12 */, INTERNAL_FLASH_PART1_START_BLOCK, INTERNAL_FLASH_PART1_NUM_BLOCKS);
138-
build_partition(dest + 462, 0, 0, 0, 0);
139-
build_partition(dest + 478, 0, 0, 0, 0);
140-
build_partition(dest + 494, 0, 0, 0, 0);
141-
142-
dest[510] = 0x55;
143-
dest[511] = 0xaa;
144-
145-
return true;
146-
147-
} else {
148-
// non-MBR block, get data from flash memory
149-
int32_t src = convert_block_to_flash_addr(block);
150-
if (src == -1) {
151-
// bad block number
152-
return false;
153-
}
154-
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
155-
return error_code == ERR_NONE;
93+
// non-MBR block, get data from flash memory
94+
int32_t src = convert_block_to_flash_addr(block);
95+
if (src == -1) {
96+
// bad block number
97+
return false;
15698
}
99+
int32_t error_code = flash_read(&supervisor_flash_desc, src, dest, FILESYSTEM_BLOCK_SIZE);
100+
return error_code == ERR_NONE;
157101
}
158102

159103
bool supervisor_flash_write_block(const uint8_t *src, uint32_t block) {
160-
if (block == 0) {
161-
// can't write MBR, but pretend we did
162-
return true;
163-
164-
} else {
165-
#ifdef MICROPY_HW_LED_MSC
166-
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
167-
#endif
168-
temp_status_color(ACTIVE_WRITE);
169-
// non-MBR block, copy to cache
170-
int32_t dest = convert_block_to_flash_addr(block);
171-
if (dest == -1) {
172-
// bad block number
173-
return false;
174-
}
175-
int32_t error_code;
176-
error_code = flash_erase(&supervisor_flash_desc,
177-
dest,
178-
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
179-
if (error_code != ERR_NONE) {
180-
return false;
181-
}
104+
#ifdef MICROPY_HW_LED_MSC
105+
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
106+
#endif
107+
temp_status_color(ACTIVE_WRITE);
108+
// non-MBR block, copy to cache
109+
int32_t dest = convert_block_to_flash_addr(block);
110+
if (dest == -1) {
111+
// bad block number
112+
return false;
113+
}
114+
int32_t error_code;
115+
error_code = flash_erase(&supervisor_flash_desc,
116+
dest,
117+
FILESYSTEM_BLOCK_SIZE / flash_get_page_size(&supervisor_flash_desc));
118+
if (error_code != ERR_NONE) {
119+
return false;
120+
}
182121

183-
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
184-
if (error_code != ERR_NONE) {
185-
return false;
186-
}
187-
clear_temp_status();
188-
#ifdef MICROPY_HW_LED_MSC
189-
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
190-
#endif
191-
return true;
122+
error_code = flash_append(&supervisor_flash_desc, dest, src, FILESYSTEM_BLOCK_SIZE);
123+
if (error_code != ERR_NONE) {
124+
return false;
192125
}
126+
clear_temp_status();
127+
#ifdef MICROPY_HW_LED_MSC
128+
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
129+
#endif
130+
return true;
193131
}
194132

195133
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {

ports/atmel-samd/supervisor/internal_flash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#endif
4242

4343
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (FLASH_SIZE - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
44-
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
4544
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)
4645

4746
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms

ports/nrf/supervisor/internal_flash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
#include "py/mpconfig.h"
3333

34-
#define FLASH_ROOT_POINTERS
35-
3634
#define FLASH_PAGE_SIZE 0x1000
3735
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
3836

supervisor/shared/filesystem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void filesystem_init(bool create_allowed, bool force_create) {
5959
// Flush the new file system to make sure it's repaired immediately.
6060
supervisor_flash_flush();
6161
if (res != FR_OK) {
62-
asm("bkpt");
62+
//asm("bkpt");
6363
return;
6464
}
6565

@@ -75,7 +75,7 @@ void filesystem_init(bool create_allowed, bool force_create) {
7575
// and ensure everything is flushed
7676
supervisor_flash_flush();
7777
} else if (res != FR_OK) {
78-
asm("bkpt");
78+
//asm("bkpt");
7979
return;
8080
}
8181
mp_vfs_mount_t *vfs = &_mp_vfs;

supervisor/shared/flash.c

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#define VFS_INDEX 0
3333

34+
#define PART1_START_BLOCK (0x1)
35+
3436
void supervisor_flash_set_usb_writable(bool usb_writable) {
3537
mp_vfs_mount_t* current_mount = MP_STATE_VM(vfs_mount_table);
3638
for (uint8_t i = 0; current_mount != NULL; i++) {
@@ -63,18 +65,95 @@ STATIC mp_obj_t supervisor_flash_obj_make_new(const mp_obj_type_t *type, size_t
6365
return (mp_obj_t)&supervisor_flash_obj;
6466
}
6567

68+
uint32_t flash_get_block_count(void) {
69+
return PART1_START_BLOCK + supervisor_flash_get_block_count();
70+
}
71+
72+
static void build_partition(uint8_t *buf, int boot, int type, uint32_t start_block, uint32_t num_blocks) {
73+
buf[0] = boot;
74+
75+
if (num_blocks == 0) {
76+
buf[1] = 0;
77+
buf[2] = 0;
78+
buf[3] = 0;
79+
} else {
80+
buf[1] = 0xff;
81+
buf[2] = 0xff;
82+
buf[3] = 0xff;
83+
}
84+
85+
buf[4] = type;
86+
87+
if (num_blocks == 0) {
88+
buf[5] = 0;
89+
buf[6] = 0;
90+
buf[7] = 0;
91+
} else {
92+
buf[5] = 0xff;
93+
buf[6] = 0xff;
94+
buf[7] = 0xff;
95+
}
96+
97+
buf[8] = start_block;
98+
buf[9] = start_block >> 8;
99+
buf[10] = start_block >> 16;
100+
buf[11] = start_block >> 24;
101+
102+
buf[12] = num_blocks;
103+
buf[13] = num_blocks >> 8;
104+
buf[14] = num_blocks >> 16;
105+
buf[15] = num_blocks >> 24;
106+
}
107+
108+
mp_uint_t flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
109+
if (block_num == 0) {
110+
if (block_num > 1) {
111+
return 1; // error
112+
}
113+
// fake the MBR so we can decide on our own partition table
114+
115+
for (int i = 0; i < 446; i++) {
116+
dest[i] = 0;
117+
}
118+
119+
build_partition(dest + 446, 0, 0x01 /* FAT12 */, PART1_START_BLOCK, supervisor_flash_get_block_count());
120+
build_partition(dest + 462, 0, 0, 0, 0);
121+
build_partition(dest + 478, 0, 0, 0, 0);
122+
build_partition(dest + 494, 0, 0, 0, 0);
123+
124+
dest[510] = 0x55;
125+
dest[511] = 0xaa;
126+
127+
return 0; // ok
128+
129+
}
130+
return supervisor_flash_read_blocks(dest, block_num - PART1_START_BLOCK, num_blocks);
131+
}
132+
133+
mp_uint_t flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
134+
if (block_num == 0) {
135+
if (num_blocks > 1) {
136+
return 1; // error
137+
}
138+
// can't write MBR, but pretend we did
139+
return 0;
140+
} else {
141+
return supervisor_flash_write_blocks(src, block_num - PART1_START_BLOCK, num_blocks);
142+
}
143+
}
144+
66145
STATIC mp_obj_t supervisor_flash_obj_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
67146
mp_buffer_info_t bufinfo;
68147
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
69-
mp_uint_t ret = supervisor_flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
148+
mp_uint_t ret = flash_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
70149
return MP_OBJ_NEW_SMALL_INT(ret);
71150
}
72151
STATIC MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_readblocks_obj, supervisor_flash_obj_readblocks);
73152

74153
STATIC mp_obj_t supervisor_flash_obj_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
75154
mp_buffer_info_t bufinfo;
76155
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
77-
mp_uint_t ret = supervisor_flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
156+
mp_uint_t ret = flash_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FILESYSTEM_BLOCK_SIZE);
78157
return MP_OBJ_NEW_SMALL_INT(ret);
79158
}
80159
STATIC MP_DEFINE_CONST_FUN_OBJ_3(supervisor_flash_obj_writeblocks_obj, supervisor_flash_obj_writeblocks);
@@ -85,7 +164,7 @@ STATIC mp_obj_t supervisor_flash_obj_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_ob
85164
case BP_IOCTL_INIT: supervisor_flash_init(); return MP_OBJ_NEW_SMALL_INT(0);
86165
case BP_IOCTL_DEINIT: supervisor_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0); // TODO properly
87166
case BP_IOCTL_SYNC: supervisor_flash_flush(); return MP_OBJ_NEW_SMALL_INT(0);
88-
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(supervisor_flash_get_block_count());
167+
case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(flash_get_block_count());
89168
case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(supervisor_flash_get_block_size());
90169
default: return mp_const_none;
91170
}
@@ -114,10 +193,10 @@ void supervisor_flash_init_vfs(fs_user_mount_t *vfs) {
114193
vfs->fatfs.part = 1; // flash filesystem lives on first partition
115194
vfs->readblocks[0] = (mp_obj_t)&supervisor_flash_obj_readblocks_obj;
116195
vfs->readblocks[1] = (mp_obj_t)&supervisor_flash_obj;
117-
vfs->readblocks[2] = (mp_obj_t)supervisor_flash_read_blocks; // native version
196+
vfs->readblocks[2] = (mp_obj_t)flash_read_blocks; // native version
118197
vfs->writeblocks[0] = (mp_obj_t)&supervisor_flash_obj_writeblocks_obj;
119198
vfs->writeblocks[1] = (mp_obj_t)&supervisor_flash_obj;
120-
vfs->writeblocks[2] = (mp_obj_t)supervisor_flash_write_blocks; // native version
199+
vfs->writeblocks[2] = (mp_obj_t)flash_write_blocks; // native version
121200
vfs->u.ioctl[0] = (mp_obj_t)&supervisor_flash_obj_ioctl_obj;
122201
vfs->u.ioctl[1] = (mp_obj_t)&supervisor_flash_obj;
123202
}

0 commit comments

Comments
 (0)