Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f20f033

Browse files
committedMar 28, 2025·
add flash formatter
1 parent d4e6b08 commit f20f033

10 files changed

+5821
-0
lines changed
 

‎.github/workflows/compile-examples.yml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
- examples/customCborDecoder
3030
- examples/customCborEncoder
3131
- examples/timedBlink
32+
- examples/flashFormatter
3233
SKETCHES_REPORTS_PATH: sketches-reports
3334

3435
strategy:
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <Arduino_FlashFormatter.h>
12+
13+
FlashFormatter flashFormatter;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
while(!Serial);
18+
19+
if(!flashFormatter.checkandFormatPartition()){
20+
Serial.println("Failed to format partition");
21+
}
22+
}
23+
24+
void loop() { }

‎src/Arduino_FlashFormatter.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#pragma once
9+
#include "./flashFormatter/FlashFormatter.h"
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#if defined(ARDUINO_PORTENTA_C33)
9+
#include "C33FlashFormatter.h"
10+
#define BD_ERROR_OK 0
11+
12+
C33FlashFormatter::C33FlashFormatter():
13+
_root(BlockDevice::get_default_instance()),
14+
_sys_bd(_root, 1),
15+
_sys_fs("sys"),
16+
_user_bd(_root, 2),
17+
_kvStore_bd(_root, 3) {
18+
}
19+
20+
bool C33FlashFormatter::checkPartition()
21+
{
22+
if (_root->init() != BD_ERROR_OK)
23+
{
24+
return false;
25+
}
26+
27+
if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
28+
{
29+
return false;
30+
}
31+
32+
_sys_fs.unmount();
33+
_sys_bd.deinit();
34+
35+
if (_user_bd.init() != BD_ERROR_OK)
36+
{
37+
return false;
38+
}
39+
40+
_user_bd.deinit();
41+
42+
if (_kvStore_bd.init() != BD_ERROR_OK)
43+
{
44+
return false;
45+
}
46+
47+
_kvStore_bd.deinit();
48+
_root->deinit();
49+
50+
return true;
51+
}
52+
53+
bool C33FlashFormatter::formatPartition() {
54+
MBRBlockDevice::partition(_root, 1, 0x0B, 0, 5 * 1024 * 1024);
55+
MBRBlockDevice::partition(_root, 2, 0x0B, 5 * 1024 * 1024, 15 * 1024 * 1024);
56+
MBRBlockDevice::partition(_root, 3, 0x0B, 15 * 1024 * 1024, 16 * 1024 * 1024);
57+
58+
int err = _sys_fs.reformat(&_sys_bd);
59+
if (err) {
60+
return false;
61+
}
62+
63+
_sys_fs.unmount();
64+
_user_data_fs = new LittleFileSystem("user");
65+
err = _user_data_fs->reformat(&_user_bd);
66+
if (err) {
67+
return false;
68+
}
69+
_user_data_fs->unmount();
70+
_root->deinit();
71+
return true;
72+
}
73+
74+
#endif
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#pragma once
9+
#include "FlashFormatterInterface.h"
10+
#include "BlockDevice.h"
11+
#include "MBRBlockDevice.h"
12+
#include "LittleFileSystem.h"
13+
#include "FATFileSystem.h"
14+
15+
class C33FlashFormatter : public FlashFormatterClass {
16+
public:
17+
C33FlashFormatter();
18+
protected:
19+
bool checkPartition() override;
20+
bool formatPartition() override;
21+
private:
22+
BlockDevice* _root;
23+
MBRBlockDevice _sys_bd;
24+
MBRBlockDevice _user_bd;
25+
FATFileSystem _sys_fs;
26+
FileSystem * _user_data_fs;
27+
MBRBlockDevice _kvStore_bd;
28+
};

‎src/flashFormatter/FlashFormatter.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#pragma once
9+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \
10+
|| defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
11+
#include "H7FlashFormatter.h"
12+
using FlashFormatter = MBEDH7FlashFormatter;
13+
#elif defined(ARDUINO_PORTENTA_C33)
14+
#include "C33FlashFormatter.h"
15+
using FlashFormatter = C33FlashFormatter;
16+
#else
17+
#include "FlashFormatterInterface.h"
18+
using FlashFormatter = FlashFormatterClass;
19+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#pragma once
9+
#include <Arduino.h>
10+
11+
class FlashFormatterClass {
12+
public:
13+
virtual ~FlashFormatterClass() = default;
14+
virtual bool checkandFormatPartition() {
15+
if(checkPartition()){
16+
return true;
17+
}
18+
19+
if(!formatPartition()){
20+
return false;
21+
}
22+
23+
return checkPartition();
24+
}
25+
26+
protected:
27+
virtual bool checkPartition() { return true; };
28+
virtual bool formatPartition() { return true; };
29+
};
30+
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \
9+
|| defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
10+
#include "H7FlashFormatter.h"
11+
#include "wiced_resource.h"
12+
#include "certificates.h"
13+
14+
//QSPIFBlockDevice _root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000);
15+
16+
MBEDH7FlashFormatter::MBEDH7FlashFormatter():
17+
_root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000),
18+
_wifi_data(&_root, 1),
19+
_wifi_data_fs("wlan"),
20+
_ota_data(&_root, 2),
21+
_ota_data_fs("fs"),
22+
_kvstore_data(&_root, 3)
23+
{
24+
}
25+
26+
bool MBEDH7FlashFormatter::checkPartition()
27+
{
28+
if (_root.init() != QSPIF_BD_ERROR_OK)
29+
{
30+
return false;
31+
}
32+
33+
if (!checkWifiPartition())
34+
{
35+
return false;
36+
}
37+
38+
if (_ota_data.init() != QSPIF_BD_ERROR_OK || _ota_data_fs.mount(&_ota_data) != 0)
39+
{
40+
return false;
41+
}
42+
43+
if (_ota_data.size() < 5 * 1024 * 1024)
44+
{
45+
return false;
46+
}
47+
_ota_data_fs.unmount();
48+
_ota_data.deinit();
49+
50+
if (_kvstore_data.init() != QSPIF_BD_ERROR_OK)
51+
{
52+
return false;
53+
}
54+
55+
_kvstore_data.deinit();
56+
_root.deinit();
57+
58+
return true;
59+
}
60+
61+
bool MBEDH7FlashFormatter::formatPartition() {
62+
_root.erase(0x0, _root.get_erase_size());
63+
mbed::MBRBlockDevice::partition(&_root, 1, 0x0B, 0, 1024 * 1024);
64+
mbed::MBRBlockDevice::partition(&_root, 2, 0x0B, 1024 * 1024, 13 * 1024 * 1024);
65+
mbed::MBRBlockDevice::partition(&_root, 3, 0x0B, 13 * 1024 * 1024, 14 * 1024 * 1024);
66+
67+
if(_ota_data_fs.mount(&_ota_data) != 0) {
68+
if(_ota_data_fs.reformat(&_ota_data) != 0) {
69+
return false;
70+
}
71+
}else {
72+
_ota_data_fs.unmount();
73+
}
74+
75+
if(!formatWifiPartition()) {
76+
return false;
77+
}
78+
_root.deinit();
79+
return true;
80+
}
81+
82+
long MBEDH7FlashFormatter::getFileSize(FILE *fp) {
83+
fseek(fp, 0, SEEK_END);
84+
int size = ftell(fp);
85+
fseek(fp, 0, SEEK_SET);
86+
return size;
87+
}
88+
89+
bool MBEDH7FlashFormatter::checkWifiPartition() {
90+
int err = _wifi_data_fs.mount(&_wifi_data);
91+
if (err) {
92+
return false;
93+
}
94+
95+
DIR *dir;
96+
struct dirent *ent;
97+
98+
99+
if ((dir = opendir("/wlan")) == NULL) {
100+
return false;
101+
}
102+
103+
bool found = false;
104+
while ((ent = readdir (dir)) != NULL) {
105+
String fullname = "/wlan/" + String(ent->d_name);
106+
if (fullname == "/wlan/4343WA1.BIN") {
107+
found = true;
108+
break;
109+
}
110+
}
111+
112+
closedir (dir);
113+
_wifi_data_fs.unmount();
114+
return found;
115+
}
116+
117+
bool MBEDH7FlashFormatter::formatWifiPartition() {
118+
_wifi_data_fs.reformat(&_wifi_data);
119+
extern const unsigned char wifi_firmware_image_data[];
120+
extern const resource_hnd_t wifi_firmware_image;
121+
FILE* fp = fopen("/wlan/4343WA1.BIN", "wb");
122+
const int file_size = 421098;
123+
int chunck_size = 1024;
124+
int byte_count = 0;
125+
126+
while (byte_count < file_size) {
127+
if(byte_count + chunck_size > file_size)
128+
chunck_size = file_size - byte_count;
129+
int ret = fwrite(&wifi_firmware_image_data[byte_count], chunck_size, 1, fp);
130+
if (ret != 1) {
131+
return false;
132+
}
133+
byte_count += chunck_size;
134+
}
135+
fclose(fp);
136+
137+
chunck_size = 1024;
138+
byte_count = 0;
139+
const uint32_t offset = 15 * 1024 * 1024 + 1024 * 512;
140+
141+
while (byte_count < file_size) {
142+
if(byte_count + chunck_size > file_size)
143+
chunck_size = file_size - byte_count;
144+
int ret = _root.program(wifi_firmware_image_data, offset + byte_count, chunck_size);
145+
if (ret != 0) {
146+
return false;
147+
}
148+
byte_count += chunck_size;
149+
}
150+
151+
chunck_size = 128;
152+
byte_count = 0;
153+
fp = fopen("/wlan/cacert.pem", "wb");
154+
155+
while (byte_count < cacert_pem_len) {
156+
if(byte_count + chunck_size > cacert_pem_len)
157+
chunck_size = cacert_pem_len - byte_count;
158+
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
159+
if (ret != 1) {
160+
return false;
161+
}
162+
byte_count += chunck_size;
163+
}
164+
165+
fclose(fp);
166+
_wifi_data_fs.unmount();
167+
return true;
168+
}
169+
170+
#endif

‎src/flashFormatter/H7FlashFormatter.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#pragma once
9+
#include "FlashFormatterInterface.h"
10+
#include "QSPIFBlockDevice.h"
11+
#include "MBRBlockDevice.h"
12+
#include "LittleFileSystem.h"
13+
#include "FATFileSystem.h"
14+
15+
class MBEDH7FlashFormatter : public FlashFormatterClass {
16+
public:
17+
MBEDH7FlashFormatter();
18+
19+
protected:
20+
bool checkPartition() override;
21+
bool formatPartition() override;
22+
private:
23+
QSPIFBlockDevice _root;
24+
mbed::MBRBlockDevice _wifi_data;
25+
mbed::FATFileSystem _wifi_data_fs;
26+
mbed::MBRBlockDevice _ota_data;
27+
mbed::FATFileSystem _ota_data_fs;
28+
mbed::MBRBlockDevice _kvstore_data;
29+
30+
long getFileSize(FILE *fp);
31+
bool checkWifiPartition();
32+
bool formatWifiPartition();
33+
};

‎src/flashFormatter/certificates.h

+5,433
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.