From d3699533288b892529947dd2e6e2d2902371d566 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Thu, 22 Sep 2016 18:19:39 +1000 Subject: [PATCH] Auto-create SPIFFS partition if not found. The heuristics could probably be improved a lot, but this is better than nothing at least. --- components/platform/include/platform.h | 7 +++++ components/platform/platform_partition.c | 36 ++++++++++++++++++++++++ components/spiffs/spiffs.c | 31 ++++++++++++++++++-- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/components/platform/include/platform.h b/components/platform/include/platform.h index 3b5ee9c2..bf3b5cf3 100644 --- a/components/platform/include/platform.h +++ b/components/platform/include/platform.h @@ -111,6 +111,13 @@ typedef struct { */ bool platform_partition_info (uint8_t idx, platform_partition_t *info); +/** + * Appends a partition entry to the partition table, if possible. + * Intended for auto-creation of a SPIFFS partition. + * @param info The partition definition to append. + * @returns True if the partition could be added, false if not. + */ +bool platform_partition_add (const platform_partition_t *info); // ***************************************************************************** diff --git a/components/platform/platform_partition.c b/components/platform/platform_partition.c index 66c6d9e9..a03a01ba 100644 --- a/components/platform/platform_partition.c +++ b/components/platform/platform_partition.c @@ -34,6 +34,7 @@ #include "platform.h" #include +#include #include "../../bootloader/src/main/bootloader_config.h" #include "esp_spi_flash.h" @@ -66,3 +67,38 @@ bool platform_partition_info (uint8_t idx, platform_partition_t *info) return true; } + +bool platform_partition_add (const platform_partition_t *info) +{ + partition_info_t *part_table = (partition_info_t *)malloc(SPI_FLASH_SEC_SIZE); + if (!part_table) + return false; + esp_err_t err = + spi_flash_read (PARTITION_ADD, (uint32_t *)part_table, SPI_FLASH_SEC_SIZE); + if (err != ESP_OK) + return false; + + uint8_t idx = 0; + for (; possible_idx (idx); ++idx) + if (part_table[idx].magic != PARTITION_MAGIC) + break; + + if (possible_idx (idx)) + { + partition_info_t *slot = &part_table[idx]; + slot->magic = PARTITION_MAGIC; + slot->type = info->type; + slot->subtype = info->subtype; + slot->pos.offset = info->offs; + slot->pos.size = info->size; + memcpy (slot->label, info->label, sizeof (slot->label)); + memset (slot->reserved, 0xff, sizeof (slot->reserved)); + err = spi_flash_erase_sector (PARTITION_ADD / SPI_FLASH_SEC_SIZE); + if (err == ESP_OK) + err = spi_flash_write ( + PARTITION_ADD, (uint32_t *)part_table, SPI_FLASH_SEC_SIZE); + } + + free (part_table); + return err == ESP_OK; +} diff --git a/components/spiffs/spiffs.c b/components/spiffs/spiffs.c index 03f7c596..9f8ac69e 100644 --- a/components/spiffs/spiffs.c +++ b/components/spiffs/spiffs.c @@ -1,5 +1,6 @@ #include #include +#include #include "platform.h" #include "flash_api.h" #include "spiffs.h" @@ -53,6 +54,7 @@ The small 4KB sectors allow for greater flexibility in applications th static bool get_spiffs_partition (spiffs_config *cfg) { platform_partition_t info; + uint32_t next_free_offs = 0; uint8_t i = 0; while (platform_partition_info (i, &info)) { @@ -64,11 +66,34 @@ static bool get_spiffs_partition (spiffs_config *cfg) return true; } else + { ++i; + if ((info.offs + info.size) > next_free_offs) + next_free_offs = info.offs + info.size; + } + } + // Attempt to append a spiffs partition + NODE_ERR("No filesystem partition found, attempting to create it...\n"); + info.type = PLATFORM_PARTITION_TYPE_NODEMCU; + info.subtype = PLATFORM_PARTITION_SUBTYPE_NODEMCU_SPIFFS; + strcpy (info.label, "spiffs"); + info.offs = cfg->phys_addr = next_free_offs; + info.size = cfg->phys_size = flash_safe_get_size_byte () - info.offs; + if (info.size <= 0) + { + NODE_ERR("No space available at end of flash, can't add fs partition!\n"); + return false; + } + if (platform_partition_add (&info)) + { + NODE_ERR("Ok.\n"); + return true; + } + else + { + NODE_ERR("Error rewriting partition table!\n"); + return false; } - // TODO: try to automatically append a spiffs partition; needs support - // over in platform_partition.c for that too - return false; }