Skip to content

Commit cdfdfd5

Browse files
authored
Merge pull request arduino#82 from bcmi-labs/bd_default
QSPI flash default partitioning proposal
2 parents 03ea7be + e58c522 commit cdfdfd5

File tree

7 files changed

+256
-118
lines changed

7 files changed

+256
-118
lines changed

libraries/SSLClient/examples/CertificateUploader/CertificateUploader.ino

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
#include "QSPIFlashBlockDevice.h"
1+
/*
2+
Portenta C33 - Certificate uploader
3+
4+
The sketch uploads and saves network certificates on the system
5+
partition of the QSPI flash
6+
7+
This example code is in the public domain.
8+
*/
9+
10+
#include "BlockDevice.h"
11+
#include "MBRBlockDevice.h"
212
#include "FATFileSystem.h"
313
#include "certificates.h"
414

5-
QSPIFlashBlockDevice root(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
6-
FATFileSystem root_fs("wlan");
15+
BlockDevice* root = BlockDevice::get_default_instance();
16+
MBRBlockDevice sys_bd(root, 1);
17+
FATFileSystem sys_fs("sys");
718

819
long getFileSize(FILE *fp) {
920
fseek(fp, 0, SEEK_END);
@@ -32,33 +43,33 @@ void setup() {
3243
Serial.begin(115200);
3344
while (!Serial);
3445

35-
int err = root_fs.mount(&root);
46+
int err = sys_fs.mount(&sys_bd);
3647
if (err) {
3748
// Reformat if we can't mount the filesystem
3849
// this should only happen on the first boot
3950
Serial.println("No filesystem containing the WiFi firmware was found.");
4051
Serial.println("Usually that means that the WiFi firmware has not been installed yet"
4152
" or was overwritten with another firmware.\n");
4253
Serial.println("Formatting the filsystem to install the firmware and certificates...\n");
43-
err = root_fs.reformat(&root);
54+
err = sys_fs.reformat(&sys_bd);
4455
}
4556

4657
DIR *dir;
4758
struct dirent *ent;
4859

49-
if ((dir = opendir("/wlan")) != NULL) {
60+
if ((dir = opendir("/sys")) != NULL) {
5061
/* print all the files and directories within directory */
5162
while ((ent = readdir (dir)) != NULL) {
5263
Serial.println("Searching for WiFi firmware file " + String(ent->d_name) + " ...");
53-
String fullname = "/wlan/" + String(ent->d_name);
54-
if (fullname == "/wlan/cacert.pem") {
64+
String fullname = "/sys/" + String(ent->d_name);
65+
if (fullname == "/sys/cacert.pem") {
5566
Serial.println("A WiFi firmware is already installed. "
5667
"Do you want to install the firmware anyway? Y/[n]");
5768
while (1) {
5869
if (Serial.available()) {
5970
int c = Serial.read();
6071
if (c == 'Y' || c == 'y') {
61-
root_fs.reformat(&root);
72+
sys_fs.reformat(&sys_bd);
6273
break;
6374
}
6475
if (c == 'N' || c == 'n') {
@@ -76,7 +87,7 @@ void setup() {
7687

7788
int chunck_size = 128;
7889
int byte_count = 0;
79-
FILE* fp = fopen("/wlan/cacert.pem", "wb");
90+
FILE* fp = fopen("/sys/cacert.pem", "wb");
8091

8192
Serial.println("Flashing certificates");
8293
printProgress(byte_count, cacert_pem_len, 10, true);
@@ -93,7 +104,7 @@ void setup() {
93104
}
94105
fclose(fp);
95106

96-
fp = fopen("/wlan/cacert.pem", "rb");
107+
fp = fopen("/sys/cacert.pem", "rb");
97108
char buffer[128];
98109
int ret = fread(buffer, 1, 128, fp);
99110
Serial.write(buffer, ret);

libraries/Storage/examples/FatFsOnQSPIFlash/FatFsOnQSPIFlash.ino

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <vector>
1616
#include <string>
1717
#include "QSPIFlashBlockDevice.h"
18+
#include <BlockDevice.h>
19+
#include <MBRBlockDevice.h>
1820
#include "FATFileSystem.h"
1921

2022
#define TEST_FS_NAME "qspi"
@@ -23,7 +25,8 @@
2325
#define DELETE_FILE_DIMENSION 150
2426

2527

26-
QSPIFlashBlockDevice block_device(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3);
28+
BlockDevice* block_device = BlockDevice::get_default_instance();
29+
MBRBlockDevice mbr(block_device, 2);
2730
FATFileSystem fs(TEST_FS_NAME);
2831

2932
std::string root_folder = std::string("/") + std::string(TEST_FS_NAME);
@@ -51,12 +54,12 @@ void setup() {
5154
*/
5255

5356
Serial.println("Mounting QSPI FLASH...");
54-
int err = fs.mount(&block_device);
57+
int err = fs.mount(&mbr);
5558
if (err) {
5659
// Reformat if we can't mount the filesystem
5760
// this should only happen on the first boot
5861
Serial.println("No filesystem found, formatting... ");
59-
err = fs.reformat(&block_device);
62+
err = fs.reformat(&mbr);
6063
}
6164

6265
if (err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Portenta C33 - QSPI Read partitions
3+
4+
The sketch reads the partition table of QSPI flash and prints the
5+
results on the serial monitor
6+
7+
This example code is in the public domain.
8+
*/
9+
10+
#include <BlockDevice.h>
11+
12+
struct __attribute__((packed)) mbrEntry {
13+
uint8_t status;
14+
uint8_t chsStart[3];
15+
uint8_t type;
16+
uint8_t chsStop[3];
17+
uint32_t lbaOffset;
18+
uint32_t lbaSize;
19+
};
20+
21+
struct __attribute__((packed)) mbrTable {
22+
mbrEntry entries[4];
23+
uint8_t signature[2];
24+
};
25+
26+
unsigned long allocatedSpace {};
27+
28+
void setup()
29+
{
30+
Serial.begin(9600);
31+
for (const auto timeout = millis() + 2500; !Serial && millis() < timeout; delay(250))
32+
;
33+
34+
auto bd = BlockDevice::get_default_instance();
35+
auto ret = bd->init();
36+
if (ret) {
37+
Serial.println("ERROR! Unable to read the Block Device.");
38+
while (true)
39+
;
40+
}
41+
42+
// Allocate smallest buffer necessary to write MBR
43+
auto buffer_size = std::max<uint32_t>(bd->get_program_size(), sizeof(mbrTable));
44+
45+
// Prevent alignment issues
46+
if (buffer_size % bd->get_program_size() != 0) {
47+
buffer_size += bd->get_program_size() - (buffer_size % bd->get_program_size());
48+
}
49+
50+
auto buffer = new uint8_t[buffer_size];
51+
52+
// Check for existing MBR
53+
ret = bd->read(buffer, 512 - buffer_size, buffer_size);
54+
if (ret) {
55+
Serial.println("ERROR! Unable to read the Master Boot Record");
56+
57+
delete[] buffer;
58+
while (true)
59+
;
60+
}
61+
62+
auto table_start_offset = buffer_size - sizeof(mbrTable);
63+
auto table = reinterpret_cast<mbrTable*>(&buffer[table_start_offset]);
64+
65+
Serial.println();
66+
Serial.print("Looking for Partitions on the Flash Memory... ");
67+
68+
if (table->signature[0] != 0x55 || table->signature[1] != 0xAA) {
69+
Serial.println("MBR Not Found");
70+
Serial.println("Flash Memory doesn't have partitions.");
71+
} else {
72+
73+
Serial.println("MBR Found");
74+
Serial.print("Boot Signature: 0x");
75+
Serial.print(table->signature[0], HEX);
76+
Serial.println(table->signature[1], HEX);
77+
78+
Serial.println();
79+
Serial.println("Printing Partitions Table and Info...");
80+
81+
auto part { 1u };
82+
for (auto const& entry : table->entries) {
83+
Serial.println("================================");
84+
Serial.print("Partition: ");
85+
Serial.println(part++);
86+
87+
Serial.print("Bootable: ");
88+
Serial.println(entry.status == 0 ? "No" : "Yes");
89+
90+
Serial.print("Type: 0x");
91+
if (entry.type < 0x10)
92+
Serial.print(0);
93+
Serial.println(entry.type, HEX);
94+
95+
if (entry.type == 0x00)
96+
continue;
97+
98+
Serial.print("Size [KBytes]: ");
99+
Serial.println((entry.lbaSize * 4096) >> 10);
100+
101+
allocatedSpace += entry.lbaSize * 4096;
102+
103+
Serial.print("Start [C/H/S]: ");
104+
Serial.print(entry.chsStart[0]);
105+
Serial.print("/");
106+
Serial.print(entry.chsStart[1]);
107+
Serial.print("/");
108+
Serial.println(entry.chsStart[2]);
109+
110+
Serial.print("Stop [C/H/S]: ");
111+
Serial.print(entry.chsStop[0]);
112+
Serial.print("/");
113+
Serial.print(entry.chsStop[1]);
114+
Serial.print("/");
115+
Serial.println(entry.chsStop[2]);
116+
117+
Serial.println();
118+
}
119+
120+
Serial.println();
121+
Serial.println("No more partitions are present.");
122+
}
123+
124+
Serial.println();
125+
Serial.print("Total Space [KBytes]: ");
126+
Serial.println(bd->size() >> 10);
127+
Serial.print("Allocated Space [KBytes]: ");
128+
Serial.println(allocatedSpace >> 10);
129+
Serial.print("Unallocated Space [KBytes]: ");
130+
Serial.println((bd->size() - allocatedSpace) >> 10);
131+
}
132+
133+
void loop()
134+
{
135+
delay(10000);
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Portenta C33 - QSPI Flash format
3+
4+
The sketch formats the board QSPI flash as follow:
5+
6+
* Partition 1 5MB: used for network certificates and OTA
7+
* Partition 2 11MB: general purpose
8+
9+
This example code is in the public domain.
10+
*/
11+
12+
#include "BlockDevice.h"
13+
#include "MBRBlockDevice.h"
14+
#include "LittleFileSystem.h"
15+
#include "FATFileSystem.h"
16+
17+
BlockDevice* root = BlockDevice::get_default_instance();
18+
MBRBlockDevice sys_bd(root, 1);
19+
MBRBlockDevice user_bd(root, 2);
20+
FATFileSystem sys_fs("sys");
21+
FileSystem * user_data_fs;
22+
23+
bool waitResponse() {
24+
bool confirmation = false;
25+
while (confirmation == false) {
26+
if (Serial.available()) {
27+
char choice = Serial.read();
28+
switch (choice) {
29+
case 'y':
30+
case 'Y':
31+
confirmation = true;
32+
return true;
33+
break;
34+
case 'n':
35+
case 'N':
36+
confirmation = true;
37+
return false;
38+
break;
39+
default:
40+
continue;
41+
}
42+
}
43+
}
44+
}
45+
46+
void setup() {
47+
48+
Serial.begin(115200);
49+
while (!Serial);
50+
51+
Serial.println("\nWARNING! Running the sketch all the content of the QSPI flash will be erased.");
52+
Serial.println("Do you want to proceed? Y/[n]");
53+
54+
if (true == waitResponse()) {
55+
MBRBlockDevice::partition(root, 1, 0x0B, 0, 5 * 1024 * 1024);
56+
MBRBlockDevice::partition(root, 2, 0x0B, 5 * 1024 * 1024, 16 * 1024 * 1024);
57+
58+
int err = sys_fs.reformat(&sys_bd);
59+
if (err) {
60+
Serial.println("Error formatting sys partition");
61+
}
62+
63+
Serial.println("\nDo you want to use LittleFS to format user data partition? Y/[n]");
64+
Serial.println("If No, FatFS will be used to format user partition.");
65+
66+
if (true == waitResponse()) {
67+
Serial.println("Formatting user partition with LittleFS.");
68+
user_data_fs = new LittleFileSystem("user");
69+
} else {
70+
Serial.println("Formatting user partition with FatFS.");
71+
user_data_fs = new FATFileSystem("user");
72+
}
73+
74+
err = user_data_fs->reformat(&user_bd);
75+
if (err) {
76+
Serial.println("Error formatting user partition");
77+
}
78+
79+
Serial.println("\nQSPI Flash formatted!");
80+
}
81+
82+
Serial.println("It's now safe to reboot or disconnect your board.");
83+
}
84+
85+
void loop() {
86+
87+
}

0 commit comments

Comments
 (0)