Skip to content

Commit 2e2facc

Browse files
authored
Merge pull request #27 from fabik111/flash-formatter
Add flash formatter
2 parents 6eb3214 + 09503cf commit 2e2facc

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:
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
} else {
22+
Serial.println("Partition formatted successfully");
23+
}
24+
}
25+
26+
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 "FlashFormatterBase.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 "FlashFormatterBase.h"
18+
using FlashFormatter = FlashFormatterClass;
19+
#endif
+30
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+
+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
MBEDH7FlashFormatter::MBEDH7FlashFormatter():
15+
_root(mbed::BlockDevice::get_default_instance()),
16+
_wifi_data(_root, 1),
17+
_wifi_data_fs("wlan"),
18+
_ota_data(_root, 2),
19+
_ota_data_fs("fs"),
20+
_kvstore_data(_root, 3)
21+
{
22+
}
23+
24+
bool MBEDH7FlashFormatter::checkPartition()
25+
{
26+
if (_root->init() != mbed::BD_ERROR_OK)
27+
{
28+
return false;
29+
}
30+
31+
if (!checkWifiPartition())
32+
{
33+
return false;
34+
}
35+
36+
if (_ota_data.init() != mbed::BD_ERROR_OK || _ota_data_fs.mount(&_ota_data) != 0)
37+
{
38+
return false;
39+
}
40+
41+
if (_ota_data.size() < 5 * 1024 * 1024)
42+
{
43+
return false;
44+
}
45+
_ota_data_fs.unmount();
46+
_ota_data.deinit();
47+
48+
if (_kvstore_data.init() != mbed::BD_ERROR_OK)
49+
{
50+
return false;
51+
}
52+
53+
_kvstore_data.deinit();
54+
_root->deinit();
55+
56+
return true;
57+
}
58+
59+
bool MBEDH7FlashFormatter::formatPartition() {
60+
_root->erase(0x0, _root->get_erase_size());
61+
mbed::MBRBlockDevice::partition(_root, 1, 0x0B, 0, 1024 * 1024);
62+
mbed::MBRBlockDevice::partition(_root, 2, 0x0B, 1024 * 1024, 13 * 1024 * 1024);
63+
mbed::MBRBlockDevice::partition(_root, 3, 0x0B, 13 * 1024 * 1024, 14 * 1024 * 1024);
64+
65+
if(_ota_data_fs.mount(&_ota_data) != 0) {
66+
if(_ota_data_fs.reformat(&_ota_data) != 0) {
67+
return false;
68+
}
69+
}else {
70+
_ota_data_fs.unmount();
71+
}
72+
73+
if(!formatWifiPartition()) {
74+
return false;
75+
}
76+
_root->deinit();
77+
return true;
78+
}
79+
80+
long MBEDH7FlashFormatter::getFileSize(FILE *fp) {
81+
fseek(fp, 0, SEEK_END);
82+
int size = ftell(fp);
83+
fseek(fp, 0, SEEK_SET);
84+
return size;
85+
}
86+
87+
bool MBEDH7FlashFormatter::checkWifiPartition() {
88+
int err = _wifi_data_fs.mount(&_wifi_data);
89+
if (err) {
90+
return false;
91+
}
92+
93+
DIR *dir;
94+
struct dirent *ent;
95+
96+
97+
if ((dir = opendir("/wlan")) == NULL) {
98+
return false;
99+
}
100+
101+
bool found = false;
102+
while ((ent = readdir (dir)) != NULL) {
103+
String fullname = "/wlan/" + String(ent->d_name);
104+
if (fullname == "/wlan/4343WA1.BIN") {
105+
found = true;
106+
break;
107+
}
108+
}
109+
110+
closedir (dir);
111+
_wifi_data_fs.unmount();
112+
return found;
113+
}
114+
115+
bool MBEDH7FlashFormatter::formatWifiPartition() {
116+
_wifi_data_fs.reformat(&_wifi_data);
117+
extern const unsigned char wifi_firmware_image_data[];
118+
extern const resource_hnd_t wifi_firmware_image;
119+
FILE* fp = fopen("/wlan/4343WA1.BIN", "wb");
120+
const int file_size = 421098;
121+
int chunck_size = 1024;
122+
int byte_count = 0;
123+
124+
while (byte_count < file_size) {
125+
if(byte_count + chunck_size > file_size)
126+
chunck_size = file_size - byte_count;
127+
int ret = fwrite(&wifi_firmware_image_data[byte_count], chunck_size, 1, fp);
128+
if (ret != 1) {
129+
return false;
130+
}
131+
byte_count += chunck_size;
132+
}
133+
fclose(fp);
134+
135+
chunck_size = 1024;
136+
byte_count = 0;
137+
const uint32_t offset = 15 * 1024 * 1024 + 1024 * 512;
138+
139+
while (byte_count < file_size) {
140+
if(byte_count + chunck_size > file_size)
141+
chunck_size = file_size - byte_count;
142+
int ret = _root->program(wifi_firmware_image_data, offset + byte_count, chunck_size);
143+
if (ret != 0) {
144+
return false;
145+
}
146+
byte_count += chunck_size;
147+
}
148+
149+
chunck_size = 128;
150+
byte_count = 0;
151+
fp = fopen("/wlan/cacert.pem", "wb");
152+
153+
while (byte_count < cacert_pem_len) {
154+
if(byte_count + chunck_size > cacert_pem_len)
155+
chunck_size = cacert_pem_len - byte_count;
156+
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
157+
if (ret != 1) {
158+
return false;
159+
}
160+
byte_count += chunck_size;
161+
}
162+
163+
fclose(fp);
164+
_wifi_data_fs.unmount();
165+
return true;
166+
}
167+
168+
#endif

0 commit comments

Comments
 (0)