esp32/main: Auto detect the size of flash and auto create vfs partition.

Currently in the esp32 port the size of the SPI flash must be configured at
build time, eg 4MiB, 8MiB, etc.  Also, the esp32 partition table must be
configured at build time, which depends on the size of the SPI flash.  A
bigger flash means more can be allocated to the user filesystem.

This commit makes it so the SPI flash size is automatically determined at
runtime, and the filesystem size is automatically set to take up as much
room as possible (a "vfs" partition is created automatically if it doesn't
exist).

This works by:
- Setting the SPI flash size to be 4MiB in the build (or some other value,
  as long as the firmware app fits).
- Removing the vfs partition from the esp32 partition table (only nvs,
  phy_init and firmware, and maybe romfs, remain in the partition table).
- At boot, query the physical size of the SPI flash and use that as the
  actual size in the code.
- If it doesn't already exist, automatically create a "vfs" partition which
  takes up the flash from the end of all existing partitions to the end of
  flash.

This allows simplifying a lot of board configurations, and removing some
board variants that just change the flash size (to be done in a following
commit).

It's also fully backwards compatible, in the following sense:
- Existing boards with MicroPython firmware will continue to work with the
  same filesystem, ie the filesystem won't be erased when the firmware is
  updated.
- If a user has a custom esp32 partition table and installs MicroPython as
  a bare app into the app partition, the new MicroPython firmware will
  honour the esp32 partition table and use either "vfs" or "ffat"
  partitions as the filesystem.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2025-05-20 10:33:21 +10:00
parent 398da22492
commit 2a0e2b5782

View File

@@ -38,6 +38,7 @@
#include "nvs_flash.h"
#include "esp_task.h"
#include "esp_event.h"
#include "esp_flash.h"
#include "esp_log.h"
#include "esp_memory_utils.h"
#include "esp_psram.h"
@@ -214,11 +215,38 @@ void boardctrl_startup(void) {
nvs_flash_erase();
nvs_flash_init();
}
// Query the physical size of the SPI flash and store it in the size
// variable of the global, default SPI flash handle.
esp_flash_get_physical_size(NULL, &esp_flash_default_chip->size);
// If there is no filesystem partition (no "vfs" or "ffat"), add a "vfs" partition
// that extends from the end of the application partition up to the end of flash.
if (esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "vfs") == NULL
&& esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "ffat") == NULL) {
// No "vfs" or "ffat" partition, so try to create one.
// Find the end of the last partition that exists in the partition table.
size_t offset = 0;
esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (iter != NULL) {
const esp_partition_t *part = esp_partition_get(iter);
offset = MAX(offset, part->address + part->size);
iter = esp_partition_next(iter);
}
// If we found the application partition and there is some space between the end of
// that and the end of flash, create a "vfs" partition taking up all of that space.
if (offset > 0 && esp_flash_default_chip->size > offset) {
size_t size = esp_flash_default_chip->size - offset;
esp_partition_register_external(esp_flash_default_chip, offset, size, "vfs", ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
}
}
}
void MICROPY_ESP_IDF_ENTRY(void) {
// Hook for a board to run code at start up.
// This defaults to initialising NVS.
// This defaults to initialising NVS and detecting the flash size.
MICROPY_BOARD_STARTUP();
// Create and transfer control to the MicroPython task.