Skip to content

Commit 53437f5

Browse files
committed
Check for certificates before starting the OTA
download function uses the certificates saved in /wlan flash partition.
1 parent f14877a commit 53437f5

4 files changed

+42
-8
lines changed

src/Arduino_Portenta_OTA.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Arduino_Portenta_OTA::Error Arduino_Portenta_OTA::begin()
6767
if (!isOtaCapable())
6868
return Error::NoCapableBootloader;
6969

70+
if (!caStorageInit())
71+
return Error::CaStorageInit;
72+
73+
if (!caStorageOpen())
74+
return Error::CaStorageOpen;
75+
7076
if (!init())
7177
return Error::OtaStorageInit;
7278

@@ -131,3 +137,34 @@ void Arduino_Portenta_OTA::write()
131137
HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR2, _data_offset);
132138
HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR3, _program_length);
133139
}
140+
141+
bool Arduino_Portenta_OTA::caStorageInit()
142+
{
143+
_bd_raw_qspi = mbed::BlockDevice::get_default_instance();
144+
145+
if (_bd_raw_qspi->init() != QSPIF_BD_ERROR_OK) {
146+
Debug.print(DBG_ERROR, F("Error: QSPI init failure."));
147+
return false;
148+
}
149+
150+
mbed::MBRBlockDevice * cert_bd_qspi = new mbed::MBRBlockDevice(_bd_raw_qspi, 1);
151+
mbed::FATFileSystem * cert_fs_qspi = new mbed::FATFileSystem("wlan");
152+
int const err_mount = cert_fs_qspi->mount(cert_bd_qspi);
153+
if (err_mount) {
154+
Debug.print(DBG_ERROR, F("Error while mounting the certificate filesystem. Err = %d"), err_mount);
155+
return false;
156+
}
157+
return true;
158+
}
159+
160+
bool Arduino_Portenta_OTA::caStorageOpen()
161+
{
162+
FILE* fp = fopen("/wlan/cacert.pem", "r");
163+
if (!fp) {
164+
Debug.print(DBG_ERROR, F("Error while opening the certificate file."));
165+
return false;
166+
}
167+
fclose(fp);
168+
169+
return true;
170+
}

src/Arduino_Portenta_OTA.h

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Arduino_Portenta_OTA
8484
OtaHeaderLength = -5,
8585
OtaHeaderCrc = -6,
8686
OtaHeaterMagicNumber = -7,
87+
CaStorageInit = -8,
88+
CaStorageOpen = -9,
8789
};
8890

8991
Arduino_Portenta_OTA(StorageTypePortenta const storage_type, uint32_t const data_offset);
@@ -109,6 +111,7 @@ class Arduino_Portenta_OTA
109111
StorageTypePortenta _storage_type;
110112
uint32_t _data_offset;
111113
uint32_t _program_length;
114+
mbed::BlockDevice * _bd_raw_qspi;
112115

113116
virtual bool init() = 0;
114117
virtual bool open() = 0;
@@ -118,6 +121,8 @@ class Arduino_Portenta_OTA
118121
private:
119122

120123
void write();
124+
bool caStorageInit();
125+
bool caStorageOpen();
121126
ArduinoPortentaOtaWatchdogResetFuncPointer _feed_watchdog_func = 0;
122127

123128
};

src/Arduino_Portenta_OTA_QSPI.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ Arduino_Portenta_OTA_QSPI::Arduino_Portenta_OTA_QSPI(StorageTypePortenta const s
4646

4747
bool Arduino_Portenta_OTA_QSPI::init()
4848
{
49-
_bd_raw_qspi = mbed::BlockDevice::get_default_instance();
50-
if (_bd_raw_qspi->init() != QSPIF_BD_ERROR_OK) {
51-
Debug.print(DBG_ERROR, F("Error: QSPI init failure."));
52-
return false;
53-
}
54-
5549
if(_storage_type == QSPI_FLASH_FATFS)
5650
{
5751
_fs_qspi = new mbed::FATFileSystem("fs");
@@ -75,7 +69,6 @@ bool Arduino_Portenta_OTA_QSPI::init()
7569
}
7670
return true;
7771
}
78-
7972
return false;
8073
}
8174

src/Arduino_Portenta_OTA_QSPI.h

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class Arduino_Portenta_OTA_QSPI : public Arduino_Portenta_OTA
4848

4949
private:
5050

51-
mbed::BlockDevice * _bd_raw_qspi;
5251
mbed::BlockDevice * _bd_qspi;
5352
mbed::FATFileSystem * _fs_qspi;
5453
};

0 commit comments

Comments
 (0)