Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Storage partition issues #2548

Merged
merged 5 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Sming/Components/Storage/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ To customise the hardware configuration for a project, for example 'my_project':

To rebuild these manually type::

make partbuild
make buildpart

These will be removed when ``make clean`` is run, but you can also clean them separately thus::

Expand Down Expand Up @@ -338,14 +338,13 @@ This is a C++ interface. Some examples::
}

// Enumerate all partitions
for(auto it = Storage::findPartition(); it; ++it) {
auto part = *it;
for(auto part: Storage::findPartition()) {
debugf("Found '%s' at 0x%08x, size 0x%08x", part.name().c_str(), part.address(), part.size());
}

// Enumerate all SPIFFS partitions
for(auto it = Storage::findPartition(Partition::SubType::Data::spiffs; it; it++) {
debugf("Found '%s' at 0x%08x, size 0x%08x", it->name().c_str(), it->address(), it->size());
for(auto part: Storage::findPartition(Storage::Partition::SubType::Data::spiffs)) {
debugf("Found '%s' at 0x%08x, size 0x%08x", part.name().c_str(), part.address(), part.size());
}


Expand Down
2 changes: 1 addition & 1 deletion Sming/Components/Storage/Tools/hwconfig/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def dict(self):
res['name'] = self.name
if hasattr(self, 'comment'):
res['comment'] = self.comment
res['arch'] = self.arch;
res['arch'] = self.arch
res['options'] = self.options
res['bootloader_size'] = size_format(self.bootloader_size)
res['partition_table_offset'] = addr_format(self.partition_table_offset)
Expand Down
2 changes: 2 additions & 0 deletions Sming/Components/Storage/Tools/hwconfig/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ def parse_dict(self, data, devices):

def resolve_expressions(self):
try:
SMING_ARCH = os.environ['SMING_ARCH']
SMING_SOC = os.environ['SMING_SOC']
self.address = eval(str(self.address))
except Exception:
self.address = parse_int(self.address)
Expand Down
2 changes: 2 additions & 0 deletions Sming/Components/Storage/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ PARTITION_TOOLS := $(PARTITION_PATH)/Tools
HWCONFIG_SCHEMA := $(PARTITION_PATH)/schema.json
HWCONFIG_VARS := \
SMING_HOME \
SMING_ARCH \
SMING_SOC \
OUT_BASE \
HWCONFIG_DIRS \
HWCONFIG_OPTS \
Expand Down
4 changes: 2 additions & 2 deletions Sming/Components/Storage/src/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ void listPartitions(Print& out)
{
out.println();
out.println(_F("Registered partitions:"));
for(auto it = Storage::findPartition(); it; ++it) {
for(auto part : Storage::findPartition()) {
out.print("- ");
printPartition(out, *it);
printPartition(out, part);
}
out.println();
}
Expand Down
17 changes: 3 additions & 14 deletions Sming/Components/Storage/src/Iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@

namespace Storage
{
Iterator::Iterator(Device& device, uint8_t partitionIndex)
: mSearch{&device, Partition::Type::any, Partition::SubType::any}, mDevice(&device), mPos(partitionIndex)

{
if(partitionIndex >= device.partitions().count()) {
mDevice = nullptr;
mPos = afterEnd;
}
}

Iterator::Iterator(Partition::Type type, uint8_t subtype) : mSearch{nullptr, type, subtype}
Iterator::Iterator(Partition::Type type, uint8_t subtype) : mSearch{nullptr, type, subtype}, mDevice(spiFlash)
{
mDevice = spiFlash;
next();
}

Expand All @@ -46,16 +35,16 @@ bool Iterator::next()
return true;
}

mPos = afterEnd;
if(mSearch.device != nullptr) {
mDevice = nullptr;
break;
}

mDevice = mDevice->getNext();
mPos = beforeStart;
}

mDevice = nullptr;
mPos = afterEnd;
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions Sming/Components/Storage/src/Partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ bool Partition::allowWrite()
return true;
}

bool Partition::read(size_t offset, void* dst, size_t size)
bool Partition::read(uint32_t offset, void* dst, size_t size)
{
if(!allowRead()) {
return false;
Expand All @@ -220,7 +220,7 @@ bool Partition::read(size_t offset, void* dst, size_t size)
return mDevice ? mDevice->read(addr, dst, size) : false;
}

bool Partition::write(size_t offset, const void* src, size_t size)
bool Partition::write(uint32_t offset, const void* src, size_t size)
{
if(!allowWrite()) {
return false;
Expand All @@ -234,7 +234,7 @@ bool Partition::write(size_t offset, const void* src, size_t size)
return mDevice ? mDevice->write(addr, src, size) : false;
}

bool Partition::erase_range(size_t offset, size_t size)
bool Partition::erase_range(uint32_t offset, size_t size)
{
if(!allowWrite()) {
return false;
Expand Down
22 changes: 19 additions & 3 deletions Sming/Components/Storage/src/include/Storage/Iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class Device;
class Iterator : public std::iterator<std::forward_iterator_tag, Partition>
{
public:
Iterator(Device& device, uint8_t partitionIndex);
Iterator(Device& device) : mSearch{&device, Partition::Type::any, Partition::SubType::any}, mDevice(&device)
{
next();
}

Iterator(Device& device, Partition::Type type, uint8_t subtype) : mSearch{&device, type, subtype}
Iterator(Device& device, Partition::Type type, uint8_t subtype) : mSearch{&device, type, subtype}, mDevice(&device)
{
mDevice = &device;
next();
}

Expand Down Expand Up @@ -58,10 +60,24 @@ class Iterator : public std::iterator<std::forward_iterator_tag, Partition>

Partition operator*() const;

Iterator begin()
{
return mSearch.device ? Iterator(*mSearch.device) : Iterator(mSearch.type, mSearch.subType);
}

Iterator end()
{
return Iterator();
}

private:
static constexpr int8_t beforeStart{-1};
static constexpr int8_t afterEnd{0x7f};

Iterator() : mPos(afterEnd)
{
}

bool seek(uint8_t pos);
bool next();

Expand Down
8 changes: 4 additions & 4 deletions Sming/Components/Storage/src/include/Storage/Partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ class Partition
* @param size Size of data to be read, in bytes.
* @retval bool true on success, false on error
*/
bool read(size_t offset, void* dst, size_t size);
bool read(uint32_t offset, void* dst, size_t size);

template <typename T> typename std::enable_if<std::is_pod<T>::value, bool>::type read(size_t offset, T& value)
template <typename T> typename std::enable_if<std::is_pod<T>::value, bool>::type read(uint32_t offset, T& value)
{
return read(offset, &value, sizeof(value));
}
Expand All @@ -224,7 +224,7 @@ class Partition
* @retval bool true on success, false on error
* @note Flash region must be erased first
*/
bool write(size_t offset, const void* src, size_t size);
bool write(uint32_t offset, const void* src, size_t size);

/**
* @brief Erase part of the partition
Expand All @@ -233,7 +233,7 @@ class Partition
* @retval bool true on success, false on error
* @note Both offset and size must be aligned to flash sector size (4Kbytes)
*/
bool erase_range(size_t offset, size_t size);
bool erase_range(uint32_t offset, size_t size);

/**
* @brief Obtain partition type
Expand Down
4 changes: 2 additions & 2 deletions Sming/Components/Storage/src/include/Storage/PartitionTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ class PartitionTable

Iterator begin() const
{
return Iterator(mDevice, 0);
return Iterator(mDevice);
}

Iterator end() const
{
return Iterator(mDevice, mCount);
return Iterator(mDevice).end();
}

uint8_t count() const
Expand Down
6 changes: 3 additions & 3 deletions docs/source/information/rboot-ota.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ To mount your SPIFFS at boot time add the following code to init:

.. code-block:: c++

int slot = rboot_get_current_rom();
// Find the n'th SPIFFS partition
auto part = PartitionTable().find(Partition::SubType::Data::spiffs, slot);
String name = F("spiffs");
name += rboot_get_current_rom();
auto part = Storage::findPartition(name);
if(part) {
//debugf("trying to mount SPIFFS at %x, length %d", part.address(), part.size());
spiffs_mount(part);
Expand Down
4 changes: 2 additions & 2 deletions samples/Basic_Ota/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Ota::Network::HttpUpgrader* otaUpdater;
Storage::Partition spiffsPartition;
OtaUpgrader ota;

Storage::Partition findSpiffsPartition(Storage::Partition partition)
Storage::Partition findSpiffsPartition(Storage::Partition appPart)
{
String name = F("spiffs");
name += ota.getSlot(partition);
name += ota.getSlot(appPart);
auto part = Storage::findPartition(name);
if(!part) {
debug_w("Partition '%s' not found", name.c_str());
Expand Down
9 changes: 9 additions & 0 deletions samples/Basic_Ota/ota.hw
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
},
"rom1": {
"subtype": "ota_1"
},
"spiffs0": {
"size": "512K"
},
"spiffs1": {
"address": "0x380000 if SMING_ARCH == 'Esp32' else 0x280000",
"size": "512K",
"type": "data",
"subtype": "spiffs"
}
}
}
6 changes: 3 additions & 3 deletions samples/Basic_Storage/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ IMPORT_FSTR(FS_app, PROJECT_DIR "/app/application.cpp")
void listSpiffsPartitions()
{
Serial.println(_F("** Enumerate registered SPIFFS partitions"));
for(auto it = Storage::findPartition(Storage::Partition::SubType::Data::spiffs); it; ++it) {
for(auto part : Storage::findPartition(Storage::Partition::SubType::Data::spiffs)) {
Serial.print(F(">> Mounting '"));
Serial.print((*it).name());
Serial.print(part.name());
Serial.println("' ...");
bool ok = spiffs_mount(*it);
bool ok = spiffs_mount(part);
Serial.println(ok ? "OK, listing files:" : "Mount failed!");
if(ok) {
Directory dir;
Expand Down
3 changes: 1 addition & 2 deletions tests/HostTests/modules/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class PartitionTest : public TestGroup

void listPartitions()
{
for(auto it = Storage::findPartition(); it; ++it) {
auto part = *it;
for(auto part : Storage::findPartition()) {
Serial.print("* ");
Storage::Debug::printPartition(Serial, part);

Expand Down