Skip to content

Commit 106a83d

Browse files
committed
esp32/esp32_partition: Add support for specifying block_size.
To support filesystems that use a block size different from the native erase-page size. Signed-off-by: Damien George <[email protected]>
1 parent 5935fa2 commit 106a83d

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

docs/library/esp32.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,21 @@ Flash partitions
6868
This class gives access to the partitions in the device's flash memory and includes
6969
methods to enable over-the-air (OTA) updates.
7070

71-
.. class:: Partition(id)
71+
.. class:: Partition(id, block_size=4096)
7272

7373
Create an object representing a partition. *id* can be a string which is the label
7474
of the partition to retrieve, or one of the constants: ``BOOT`` or ``RUNNING``.
75+
*block_size* specifies the byte size of an individual block.
7576

76-
.. classmethod:: Partition.find(type=TYPE_APP, subtype=0xff, label=None)
77+
.. classmethod:: Partition.find(type=TYPE_APP, subtype=0xff, label=None, block_size=4096)
7778

7879
Find a partition specified by *type*, *subtype* and *label*. Returns a
7980
(possibly empty) list of Partition objects. Note: ``subtype=0xff`` matches any subtype
8081
and ``label=None`` matches any label.
8182

83+
*block_size* specifies the byte size of an individual block used by the returned
84+
objects.
85+
8286
.. method:: Partition.info()
8387

8488
Returns a 6-tuple ``(type, subtype, addr, size, label, encrypted)``.

ports/esp32/esp32_partition.c

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
#include "esp_ota_ops.h"
3535

3636
// esp_partition_read and esp_partition_write can operate on arbitrary bytes
37-
// but esp_partition_erase_range operates on 4k blocks. But to make a partition
38-
// implement the standard block protocol all operations are done on 4k blocks.
39-
#define BLOCK_SIZE_BYTES (4096)
37+
// but esp_partition_erase_range operates on 4k blocks. The default block size
38+
// for a Partition object is therefore 4k, to make writes efficient, and also
39+
// make it work well with filesystems like littlefs. The Partition object also
40+
// supports smaller block sizes, in which case a cache is used and writes may
41+
// be less efficient.
42+
#define NATIVE_BLOCK_SIZE_BYTES (4096)
4043

4144
enum {
4245
ESP32_PARTITION_BOOT,
@@ -46,15 +49,23 @@ enum {
4649
typedef struct _esp32_partition_obj_t {
4750
mp_obj_base_t base;
4851
const esp_partition_t *part;
52+
uint8_t *cache;
53+
uint16_t block_size;
4954
} esp32_partition_obj_t;
5055

51-
STATIC esp32_partition_obj_t *esp32_partition_new(const esp_partition_t *part) {
56+
STATIC esp32_partition_obj_t *esp32_partition_new(const esp_partition_t *part, uint16_t block_size) {
5257
if (part == NULL) {
5358
mp_raise_OSError(MP_ENOENT);
5459
}
5560
esp32_partition_obj_t *self = m_new_obj(esp32_partition_obj_t);
5661
self->base.type = &esp32_partition_type;
5762
self->part = part;
63+
self->block_size = block_size;
64+
if (self->block_size < NATIVE_BLOCK_SIZE_BYTES) {
65+
self->cache = m_new(uint8_t, NATIVE_BLOCK_SIZE_BYTES);
66+
} else {
67+
self->cache = NULL;
68+
}
5869
return self;
5970
}
6071

@@ -69,7 +80,7 @@ STATIC void esp32_partition_print(const mp_print_t *print, mp_obj_t self_in, mp_
6980

7081
STATIC mp_obj_t esp32_partition_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
7182
// Check args
72-
mp_arg_check_num(n_args, n_kw, 1, 1, false);
83+
mp_arg_check_num(n_args, n_kw, 1, 2, false);
7384

7485
// Get requested partition
7586
const esp_partition_t *part;
@@ -94,17 +105,24 @@ STATIC mp_obj_t esp32_partition_make_new(const mp_obj_type_t *type, size_t n_arg
94105
}
95106
}
96107

108+
// Get block size if given
109+
uint16_t block_size = NATIVE_BLOCK_SIZE_BYTES;
110+
if (n_args == 2) {
111+
block_size = mp_obj_get_int(all_args[1]);
112+
}
113+
97114
// Return new object
98-
return MP_OBJ_FROM_PTR(esp32_partition_new(part));
115+
return MP_OBJ_FROM_PTR(esp32_partition_new(part, block_size));
99116
}
100117

101118
STATIC mp_obj_t esp32_partition_find(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
102119
// Parse args
103-
enum { ARG_type, ARG_subtype, ARG_label };
120+
enum { ARG_type, ARG_subtype, ARG_label, ARG_block_size };
104121
static const mp_arg_t allowed_args[] = {
105122
{ MP_QSTR_type, MP_ARG_INT, {.u_int = ESP_PARTITION_TYPE_APP} },
106123
{ MP_QSTR_subtype, MP_ARG_INT, {.u_int = ESP_PARTITION_SUBTYPE_ANY} },
107124
{ MP_QSTR_label, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
125+
{ MP_QSTR_block_size, MP_ARG_INT, {.u_int = NATIVE_BLOCK_SIZE_BYTES} },
108126
};
109127
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
110128
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -115,11 +133,14 @@ STATIC mp_obj_t esp32_partition_find(size_t n_args, const mp_obj_t *pos_args, mp
115133
label = mp_obj_str_get_str(args[ARG_label].u_obj);
116134
}
117135

136+
// Get block size
137+
uint16_t block_size = args[ARG_block_size].u_int;
138+
118139
// Build list of matching partitions
119140
mp_obj_t list = mp_obj_new_list(0, NULL);
120141
esp_partition_iterator_t iter = esp_partition_find(args[ARG_type].u_int, args[ARG_subtype].u_int, label);
121142
while (iter != NULL) {
122-
mp_obj_list_append(list, MP_OBJ_FROM_PTR(esp32_partition_new(esp_partition_get(iter))));
143+
mp_obj_list_append(list, MP_OBJ_FROM_PTR(esp32_partition_new(esp_partition_get(iter), block_size)));
123144
iter = esp_partition_next(iter);
124145
}
125146
esp_partition_iterator_release(iter);
@@ -145,7 +166,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_partition_info_obj, esp32_partition_info)
145166

146167
STATIC mp_obj_t esp32_partition_readblocks(size_t n_args, const mp_obj_t *args) {
147168
esp32_partition_obj_t *self = MP_OBJ_TO_PTR(args[0]);
148-
uint32_t offset = mp_obj_get_int(args[1]) * BLOCK_SIZE_BYTES;
169+
uint32_t offset = mp_obj_get_int(args[1]) * self->block_size;
149170
mp_buffer_info_t bufinfo;
150171
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
151172
if (n_args == 4) {
@@ -158,12 +179,36 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_partition_readblocks_obj, 3, 4,
158179

159180
STATIC mp_obj_t esp32_partition_writeblocks(size_t n_args, const mp_obj_t *args) {
160181
esp32_partition_obj_t *self = MP_OBJ_TO_PTR(args[0]);
161-
uint32_t offset = mp_obj_get_int(args[1]) * BLOCK_SIZE_BYTES;
182+
uint32_t offset = mp_obj_get_int(args[1]) * self->block_size;
162183
mp_buffer_info_t bufinfo;
163184
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
164185
if (n_args == 3) {
165-
check_esp_err(esp_partition_erase_range(self->part, offset, bufinfo.len));
186+
// A simple write, which requires erasing first.
187+
if (self->block_size >= NATIVE_BLOCK_SIZE_BYTES) {
188+
// Block size is at least native erase-page size, so do an efficient erase.
189+
check_esp_err(esp_partition_erase_range(self->part, offset, bufinfo.len));
190+
} else {
191+
// Block size is less than native erase-page size, so do erase in sections.
192+
uint32_t addr = (offset / NATIVE_BLOCK_SIZE_BYTES) * NATIVE_BLOCK_SIZE_BYTES;
193+
uint32_t o = offset % NATIVE_BLOCK_SIZE_BYTES;
194+
uint32_t top_addr = offset + bufinfo.len;
195+
while (addr < top_addr) {
196+
if (o > 0 || top_addr < addr + NATIVE_BLOCK_SIZE_BYTES) {
197+
check_esp_err(esp_partition_read(self->part, addr, self->cache, NATIVE_BLOCK_SIZE_BYTES));
198+
}
199+
check_esp_err(esp_partition_erase_range(self->part, addr, NATIVE_BLOCK_SIZE_BYTES));
200+
if (o > 0) {
201+
check_esp_err(esp_partition_write(self->part, addr, self->cache, o));
202+
}
203+
if (top_addr < addr + NATIVE_BLOCK_SIZE_BYTES) {
204+
check_esp_err(esp_partition_write(self->part, top_addr, self->cache, addr + NATIVE_BLOCK_SIZE_BYTES - top_addr));
205+
}
206+
o = 0;
207+
addr += NATIVE_BLOCK_SIZE_BYTES;
208+
}
209+
}
166210
} else {
211+
// An extended write, erasing must have been done explicitly before this write.
167212
offset += mp_obj_get_int(args[3]);
168213
}
169214
check_esp_err(esp_partition_write(self->part, offset, bufinfo.buf, bufinfo.len));
@@ -182,12 +227,15 @@ STATIC mp_obj_t esp32_partition_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_
182227
case MP_BLOCKDEV_IOCTL_SYNC:
183228
return MP_OBJ_NEW_SMALL_INT(0);
184229
case MP_BLOCKDEV_IOCTL_BLOCK_COUNT:
185-
return MP_OBJ_NEW_SMALL_INT(self->part->size / BLOCK_SIZE_BYTES);
230+
return MP_OBJ_NEW_SMALL_INT(self->part->size / self->block_size);
186231
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE:
187-
return MP_OBJ_NEW_SMALL_INT(BLOCK_SIZE_BYTES);
232+
return MP_OBJ_NEW_SMALL_INT(self->block_size);
188233
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: {
189-
uint32_t offset = mp_obj_get_int(arg_in) * BLOCK_SIZE_BYTES;
190-
check_esp_err(esp_partition_erase_range(self->part, offset, BLOCK_SIZE_BYTES));
234+
if (self->block_size != NATIVE_BLOCK_SIZE_BYTES) {
235+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
236+
}
237+
uint32_t offset = mp_obj_get_int(arg_in) * NATIVE_BLOCK_SIZE_BYTES;
238+
check_esp_err(esp_partition_erase_range(self->part, offset, NATIVE_BLOCK_SIZE_BYTES));
191239
return MP_OBJ_NEW_SMALL_INT(0);
192240
}
193241
default:
@@ -205,7 +253,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_partition_set_boot_obj, esp32_partition_s
205253

206254
STATIC mp_obj_t esp32_partition_get_next_update(mp_obj_t self_in) {
207255
esp32_partition_obj_t *self = MP_OBJ_TO_PTR(self_in);
208-
return MP_OBJ_FROM_PTR(esp32_partition_new(esp_ota_get_next_update_partition(self->part)));
256+
return MP_OBJ_FROM_PTR(esp32_partition_new(esp_ota_get_next_update_partition(self->part), NATIVE_BLOCK_SIZE_BYTES));
209257
}
210258
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_partition_get_next_update_obj, esp32_partition_get_next_update);
211259

0 commit comments

Comments
 (0)