Skip to content

Commit fd5c648

Browse files
committed
add check if ca certificates file is present
1 parent 55a50dc commit fd5c648

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

src/flashFormatter/C33FlashFormatter.cpp

+61-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#if defined(ARDUINO_PORTENTA_C33)
99
#include "C33FlashFormatter.h"
1010
#define BD_ERROR_OK 0
11+
#include "certificates.h"
1112

1213
C33FlashFormatter::C33FlashFormatter():
1314
_root(BlockDevice::get_default_instance()),
@@ -24,14 +25,11 @@ bool C33FlashFormatter::checkPartition()
2425
return false;
2526
}
2627

27-
if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
28+
if(!checkCACertificatesPartition())
2829
{
2930
return false;
3031
}
3132

32-
_sys_fs.unmount();
33-
_sys_bd.deinit();
34-
3533
if (_user_bd.init() != BD_ERROR_OK)
3634
{
3735
return false;
@@ -55,14 +53,13 @@ bool C33FlashFormatter::formatPartition() {
5553
MBRBlockDevice::partition(_root, 2, 0x0B, 5 * 1024 * 1024, 15 * 1024 * 1024);
5654
MBRBlockDevice::partition(_root, 3, 0x0B, 15 * 1024 * 1024, 16 * 1024 * 1024);
5755

58-
int err = _sys_fs.reformat(&_sys_bd);
59-
if (err) {
56+
if(!flashCACertificates())
57+
{
6058
return false;
6159
}
6260

63-
_sys_fs.unmount();
6461
_user_data_fs = new LittleFileSystem("user");
65-
err = _user_data_fs->reformat(&_user_bd);
62+
int err = _user_data_fs->reformat(&_user_bd);
6663
if (err) {
6764
return false;
6865
}
@@ -71,4 +68,60 @@ bool C33FlashFormatter::formatPartition() {
7168
return true;
7269
}
7370

71+
bool C33FlashFormatter::checkCACertificatesPartition()
72+
{
73+
/*Inspired by the CertificateUploader.ino example for Portenta C33*/
74+
if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
75+
{
76+
return false;
77+
}
78+
79+
DIR *dir;
80+
struct dirent *ent;
81+
82+
if ((dir = opendir("/sys")) == NULL) {
83+
return false;
84+
}
85+
86+
bool foundCert = false;
87+
while ((ent = readdir (dir)) != NULL) {
88+
String fullname = "/sys/" + String(ent->d_name);
89+
if (fullname == "/sys/cacert.pem") {
90+
foundCert = true;
91+
break;
92+
}
93+
}
94+
closedir (dir);
95+
96+
_sys_fs.unmount();
97+
_sys_bd.deinit();
98+
return foundCert;
99+
}
100+
101+
bool C33FlashFormatter::flashCACertificates()
102+
{
103+
int err = _sys_fs.reformat(&_sys_bd);
104+
if (err) {
105+
return false;
106+
}
107+
108+
int chunck_size = 128;
109+
int byte_count = 0;
110+
FILE* fp = fopen("/sys/cacert.pem", "wb");
111+
while (byte_count < cacert_pem_len) {
112+
if(byte_count + chunck_size > cacert_pem_len)
113+
chunck_size = cacert_pem_len - byte_count;
114+
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
115+
if (ret != 1) {
116+
return false;
117+
}
118+
byte_count += chunck_size;
119+
}
120+
fclose(fp);
121+
122+
_sys_fs.unmount();
123+
124+
return true;
125+
}
126+
74127
#endif

src/flashFormatter/C33FlashFormatter.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class C33FlashFormatter : public FlashFormatterClass {
1919
bool checkPartition() override;
2020
bool formatPartition() override;
2121
private:
22+
bool checkCACertificatesPartition();
23+
bool flashCACertificates();
2224
BlockDevice* _root;
2325
MBRBlockDevice _sys_bd;
2426
MBRBlockDevice _user_bd;

src/flashFormatter/H7FlashFormatter.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,28 @@ bool MBEDH7FlashFormatter::checkWifiPartition() {
108108
return false;
109109
}
110110

111-
bool found = false;
111+
bool foundBin = false;
112112
while ((ent = readdir (dir)) != NULL) {
113113
String fullname = "/wlan/" + String(ent->d_name);
114114
if (fullname == "/wlan/4343WA1.BIN") {
115-
found = true;
115+
foundBin = true;
116+
break;
117+
}
118+
}
119+
120+
// Check if the ca certificates file is present
121+
bool foundCert = false;
122+
while ((ent = readdir (dir)) != NULL) {
123+
String fullname = "/wlan/" + String(ent->d_name);
124+
if (fullname == "/wlan/cacert.pem") {
125+
foundCert = true;
116126
break;
117127
}
118128
}
119129

120130
closedir (dir);
121131
_wifi_data_fs.unmount();
122-
return found;
132+
return foundBin & foundCert;
123133
}
124134

125135
bool MBEDH7FlashFormatter::formatWifiPartition() {

0 commit comments

Comments
 (0)