Auto-create SPIFFS partition if not found.
The heuristics could probably be improved a lot, but this is better than nothing at least.
This commit is contained in:
parent
60339b812b
commit
d369953328
|
@ -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);
|
||||
|
||||
|
||||
// *****************************************************************************
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include "platform.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
}
|
||||
// TODO: try to automatically append a spiffs partition; needs support
|
||||
// over in platform_partition.c for that too
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue