diff --git a/src/flashFormatter/C33FlashFormatter.cpp b/src/flashFormatter/C33FlashFormatter.cpp index da57b2f..43c31a3 100644 --- a/src/flashFormatter/C33FlashFormatter.cpp +++ b/src/flashFormatter/C33FlashFormatter.cpp @@ -8,6 +8,7 @@ #if defined(ARDUINO_PORTENTA_C33) #include "C33FlashFormatter.h" #define BD_ERROR_OK 0 +#include "certificates.h" C33FlashFormatter::C33FlashFormatter(): _root(BlockDevice::get_default_instance()), @@ -24,14 +25,11 @@ bool C33FlashFormatter::checkPartition() return false; } - if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK) + if(!checkCACertificatesPartition()) { return false; } - _sys_fs.unmount(); - _sys_bd.deinit(); - if (_user_bd.init() != BD_ERROR_OK) { return false; @@ -55,14 +53,13 @@ bool C33FlashFormatter::formatPartition() { 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) { + if(!flashCACertificates()) + { return false; } - _sys_fs.unmount(); _user_data_fs = new LittleFileSystem("user"); - err = _user_data_fs->reformat(&_user_bd); + int err = _user_data_fs->reformat(&_user_bd); if (err) { return false; } @@ -71,4 +68,60 @@ bool C33FlashFormatter::formatPartition() { return true; } +bool C33FlashFormatter::checkCACertificatesPartition() +{ + /*Inspired by the CertificateUploader.ino example for Portenta C33*/ + if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK) + { + return false; + } + + DIR *dir; + struct dirent *ent; + + if ((dir = opendir("/sys")) == NULL) { + return false; + } + + bool foundCert = false; + while ((ent = readdir (dir)) != NULL) { + String fullname = "/sys/" + String(ent->d_name); + if (fullname == "/sys/cacert.pem") { + foundCert = true; + break; + } + } + closedir (dir); + + _sys_fs.unmount(); + _sys_bd.deinit(); + return foundCert; +} + +bool C33FlashFormatter::flashCACertificates() +{ + int err = _sys_fs.reformat(&_sys_bd); + if (err) { + return false; + } + + int chunck_size = 128; + int byte_count = 0; + FILE* fp = fopen("/sys/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); + + _sys_fs.unmount(); + + return true; +} + #endif diff --git a/src/flashFormatter/C33FlashFormatter.h b/src/flashFormatter/C33FlashFormatter.h index a13d388..a6df7ab 100644 --- a/src/flashFormatter/C33FlashFormatter.h +++ b/src/flashFormatter/C33FlashFormatter.h @@ -19,6 +19,8 @@ class C33FlashFormatter : public FlashFormatterClass { bool checkPartition() override; bool formatPartition() override; private: + bool checkCACertificatesPartition(); + bool flashCACertificates(); BlockDevice* _root; MBRBlockDevice _sys_bd; MBRBlockDevice _user_bd; diff --git a/src/flashFormatter/H7FlashFormatter.cpp b/src/flashFormatter/H7FlashFormatter.cpp index 005d95c..b67ac7c 100644 --- a/src/flashFormatter/H7FlashFormatter.cpp +++ b/src/flashFormatter/H7FlashFormatter.cpp @@ -108,18 +108,28 @@ bool MBEDH7FlashFormatter::checkWifiPartition() { return false; } - bool found = false; + bool foundBin = false; while ((ent = readdir (dir)) != NULL) { String fullname = "/wlan/" + String(ent->d_name); if (fullname == "/wlan/4343WA1.BIN") { - found = true; + foundBin = true; + break; + } + } + + // Check if the ca certificates file is present + bool foundCert = false; + while ((ent = readdir (dir)) != NULL) { + String fullname = "/wlan/" + String(ent->d_name); + if (fullname == "/wlan/cacert.pem") { + foundCert = true; break; } } closedir (dir); _wifi_data_fs.unmount(); - return found; + return foundBin & foundCert; } bool MBEDH7FlashFormatter::formatWifiPartition() {