Skip to content

Add flash formatter #27

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

Merged
merged 3 commits into from
Apr 15, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- examples/customCborDecoder
- examples/customCborEncoder
- examples/timedBlink
- examples/flashFormatter
SKETCHES_REPORTS_PATH: sketches-reports

strategy:
Expand Down
26 changes: 26 additions & 0 deletions examples/flashFormatter/flashFormatter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file is part of the Arduino_CloudUtils library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <Arduino_FlashFormatter.h>

FlashFormatter flashFormatter;

void setup() {
Serial.begin(9600);
while(!Serial);

if(!flashFormatter.checkandFormatPartition()){
Serial.println("Failed to format partition");
} else {
Serial.println("Partition formatted successfully");
}
}

void loop() { }
9 changes: 9 additions & 0 deletions src/Arduino_FlashFormatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "./flashFormatter/FlashFormatter.h"
74 changes: 74 additions & 0 deletions src/flashFormatter/C33FlashFormatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#if defined(ARDUINO_PORTENTA_C33)
#include "C33FlashFormatter.h"
#define BD_ERROR_OK 0

C33FlashFormatter::C33FlashFormatter():
_root(BlockDevice::get_default_instance()),
_sys_bd(_root, 1),
_sys_fs("sys"),
_user_bd(_root, 2),
_kvStore_bd(_root, 3) {
}

bool C33FlashFormatter::checkPartition()
{
if (_root->init() != BD_ERROR_OK)
{
return false;
}

if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
{
return false;
}

_sys_fs.unmount();
_sys_bd.deinit();

if (_user_bd.init() != BD_ERROR_OK)
{
return false;
}

_user_bd.deinit();

if (_kvStore_bd.init() != BD_ERROR_OK)
{
return false;
}

_kvStore_bd.deinit();
_root->deinit();

return true;
}

bool C33FlashFormatter::formatPartition() {
MBRBlockDevice::partition(_root, 1, 0x0B, 0, 5 * 1024 * 1024);
MBRBlockDevice::partition(_root, 2, 0x0B, 5 * 1024 * 1024, 15 * 1024 * 1024);
MBRBlockDevice::partition(_root, 3, 0x0B, 15 * 1024 * 1024, 16 * 1024 * 1024);

int err = _sys_fs.reformat(&_sys_bd);
if (err) {
return false;
}

_sys_fs.unmount();
_user_data_fs = new LittleFileSystem("user");
err = _user_data_fs->reformat(&_user_bd);
if (err) {
return false;
}
_user_data_fs->unmount();
_root->deinit();
return true;
}

#endif
28 changes: 28 additions & 0 deletions src/flashFormatter/C33FlashFormatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "FlashFormatterBase.h"
#include "BlockDevice.h"
#include "MBRBlockDevice.h"
#include "LittleFileSystem.h"
#include "FATFileSystem.h"

class C33FlashFormatter : public FlashFormatterClass {
public:
C33FlashFormatter();
protected:
bool checkPartition() override;
bool formatPartition() override;
private:
BlockDevice* _root;
MBRBlockDevice _sys_bd;
MBRBlockDevice _user_bd;
FATFileSystem _sys_fs;
FileSystem * _user_data_fs;
MBRBlockDevice _kvStore_bd;
};
19 changes: 19 additions & 0 deletions src/flashFormatter/FlashFormatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \
|| defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
#include "H7FlashFormatter.h"
using FlashFormatter = MBEDH7FlashFormatter;
#elif defined(ARDUINO_PORTENTA_C33)
#include "C33FlashFormatter.h"
using FlashFormatter = C33FlashFormatter;
#else
#include "FlashFormatterBase.h"
using FlashFormatter = FlashFormatterClass;
#endif
30 changes: 30 additions & 0 deletions src/flashFormatter/FlashFormatterBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <Arduino.h>

class FlashFormatterClass {
public:
virtual ~FlashFormatterClass() = default;
virtual bool checkandFormatPartition() {
if(checkPartition()){
return true;
}

if(!formatPartition()){
return false;
}

return checkPartition();
}

protected:
virtual bool checkPartition() { return true; };
virtual bool formatPartition() { return true; };
};

168 changes: 168 additions & 0 deletions src/flashFormatter/H7FlashFormatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
Copyright (c) 2025 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \
|| defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
#include "H7FlashFormatter.h"
#include "wiced_resource.h"
#include "certificates.h"

MBEDH7FlashFormatter::MBEDH7FlashFormatter():
_root(mbed::BlockDevice::get_default_instance()),
_wifi_data(_root, 1),
_wifi_data_fs("wlan"),
_ota_data(_root, 2),
_ota_data_fs("fs"),
_kvstore_data(_root, 3)
{
}

bool MBEDH7FlashFormatter::checkPartition()
{
if (_root->init() != mbed::BD_ERROR_OK)
{
return false;
}

if (!checkWifiPartition())
{
return false;
}

if (_ota_data.init() != mbed::BD_ERROR_OK || _ota_data_fs.mount(&_ota_data) != 0)
{
return false;
}

if (_ota_data.size() < 5 * 1024 * 1024)
{
return false;
}
_ota_data_fs.unmount();
_ota_data.deinit();

if (_kvstore_data.init() != mbed::BD_ERROR_OK)
{
return false;
}

_kvstore_data.deinit();
_root->deinit();

return true;
}

bool MBEDH7FlashFormatter::formatPartition() {
_root->erase(0x0, _root->get_erase_size());
mbed::MBRBlockDevice::partition(_root, 1, 0x0B, 0, 1024 * 1024);
mbed::MBRBlockDevice::partition(_root, 2, 0x0B, 1024 * 1024, 13 * 1024 * 1024);
mbed::MBRBlockDevice::partition(_root, 3, 0x0B, 13 * 1024 * 1024, 14 * 1024 * 1024);

if(_ota_data_fs.mount(&_ota_data) != 0) {
if(_ota_data_fs.reformat(&_ota_data) != 0) {
return false;
}
}else {
_ota_data_fs.unmount();
}

if(!formatWifiPartition()) {
return false;
}
_root->deinit();
return true;
}

long MBEDH7FlashFormatter::getFileSize(FILE *fp) {
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
return size;
}

bool MBEDH7FlashFormatter::checkWifiPartition() {
int err = _wifi_data_fs.mount(&_wifi_data);
if (err) {
return false;
}

DIR *dir;
struct dirent *ent;


if ((dir = opendir("/wlan")) == NULL) {
return false;
}

bool found = false;
while ((ent = readdir (dir)) != NULL) {
String fullname = "/wlan/" + String(ent->d_name);
if (fullname == "/wlan/4343WA1.BIN") {
found = true;
break;
}
}

closedir (dir);
_wifi_data_fs.unmount();
return found;
}

bool MBEDH7FlashFormatter::formatWifiPartition() {
_wifi_data_fs.reformat(&_wifi_data);
extern const unsigned char wifi_firmware_image_data[];
extern const resource_hnd_t wifi_firmware_image;
FILE* fp = fopen("/wlan/4343WA1.BIN", "wb");
const int file_size = 421098;
int chunck_size = 1024;
int byte_count = 0;

while (byte_count < file_size) {
if(byte_count + chunck_size > file_size)
chunck_size = file_size - byte_count;
int ret = fwrite(&wifi_firmware_image_data[byte_count], chunck_size, 1, fp);
if (ret != 1) {
return false;
}
byte_count += chunck_size;
}
fclose(fp);

chunck_size = 1024;
byte_count = 0;
const uint32_t offset = 15 * 1024 * 1024 + 1024 * 512;

while (byte_count < file_size) {
if(byte_count + chunck_size > file_size)
chunck_size = file_size - byte_count;
int ret = _root->program(wifi_firmware_image_data, offset + byte_count, chunck_size);
if (ret != 0) {
return false;
}
byte_count += chunck_size;
}

chunck_size = 128;
byte_count = 0;
fp = fopen("/wlan/cacert.pem", "wb");

while (byte_count < cacert_pem_len) {
if(byte_count + chunck_size > cacert_pem_len)
chunck_size = cacert_pem_len - byte_count;
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
if (ret != 1) {
return false;
}
byte_count += chunck_size;
}

fclose(fp);
_wifi_data_fs.unmount();
return true;
}

#endif
Loading
Loading