1111#include " AIoTC_Config.h"
1212#if defined(BOARD_STM32H7) && OTA_ENABLED
1313#include " OTASTM32H7.h"
14-
15- #include " utility/watchdog/Watchdog.h"
1614#include < STM32H747_System.h>
1715
18- static bool findProgramLength (DIR * dir, uint32_t & program_length);
19-
20- const char STM32H7OTACloudProcess::UPDATE_FILE_NAME[] = " /fs/UPDATE.BIN" ;
21-
2216STM32H7OTACloudProcess::STM32H7OTACloudProcess (MessageStream *ms, Client* client)
2317: OTADefaultCloudProcessInterface(ms, client)
2418, decompressed(nullptr )
2519, _bd_raw_qspi(nullptr )
26- , _program_length(0 )
2720, _bd(nullptr )
28- , _fs(nullptr ) {
21+ , _fs(nullptr )
22+ , _filename(" /" + String(STM32H747OTA::FOLDER) + "/" + String(STM32H747OTA::NAME)) {
2923
3024}
3125
@@ -44,7 +38,6 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::resume(Message* msg) {
4438
4539void STM32H7OTACloudProcess::update () {
4640 OTADefaultCloudProcessInterface::update ();
47- watchdog_reset (); // FIXME this should npot be performed here
4841}
4942
5043int STM32H7OTACloudProcess::writeFlash (uint8_t * const buffer, size_t len) {
@@ -65,10 +58,13 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::startOTA() {
6558 }
6659
6760 // this could be useless, since we are writing over it
68- remove (UPDATE_FILE_NAME );
61+ remove (_filename. c_str () );
6962
70- decompressed = fopen (UPDATE_FILE_NAME , " wb" );
63+ decompressed = fopen (_filename. c_str () , " wb" );
7164
65+ if (decompressed == nullptr ) {
66+ return ErrorOpenUpdateFileFail;
67+ }
7268 // start the download if the setup for ota storage is successful
7369 return OTADefaultCloudProcessInterface::startOTA ();
7470}
@@ -78,18 +74,20 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::flashOTA() {
7874 fclose (decompressed);
7975 decompressed = nullptr ;
8076
77+ uint32_t updateLength = 0 ;
78+
8179 /* Schedule the firmware update. */
82- if (!storageOpen ( )) {
80+ if (!findProgramLength (updateLength )) {
8381 return OtaStorageOpenFail;
8482 }
8583
8684 storageClean ();
8785
8886 // this sets the registries in RTC to load the firmware from the storage selected at the next reboot
89- STM32H747::writeBackupRegister (RTCBackup::DR0, 0x07AA );
90- STM32H747::writeBackupRegister (RTCBackup::DR1, storage );
91- STM32H747::writeBackupRegister (RTCBackup::DR2, data_offset );
92- STM32H747::writeBackupRegister (RTCBackup::DR3, _program_length );
87+ STM32H747::writeBackupRegister (RTCBackup::DR0, STM32H747OTA::MAGIC );
88+ STM32H747::writeBackupRegister (RTCBackup::DR1, STM32H747OTA::STORAGE_TYPE );
89+ STM32H747::writeBackupRegister (RTCBackup::DR2, STM32H747OTA::PARTITION );
90+ STM32H747::writeBackupRegister (RTCBackup::DR3, updateLength );
9391
9492 return Reboot;
9593}
@@ -106,7 +104,7 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::reboot() {
106104void STM32H7OTACloudProcess::reset () {
107105 OTADefaultCloudProcessInterface::reset ();
108106
109- remove (UPDATE_FILE_NAME );
107+ remove (_filename. c_str () );
110108
111109 storageClean ();
112110}
@@ -156,14 +154,9 @@ bool STM32H7OTACloudProcess::storageInit() {
156154 }
157155 }
158156
159- if (storage == portenta::QSPI_FLASH_FATFS) {
160- _fs = new mbed::FATFileSystem (" fs" );
161- err_mount = _fs->mount (_bd_raw_qspi);
162- } else if (storage == portenta::QSPI_FLASH_FATFS_MBR) {
163- _bd = new mbed::MBRBlockDevice (_bd_raw_qspi, data_offset);
164- _fs = new mbed::FATFileSystem (" fs" );
165- err_mount = _fs->mount (_bd);
166- }
157+ _bd = new mbed::MBRBlockDevice (_bd_raw_qspi, STM32H747OTA::PARTITION);
158+ _fs = new mbed::FATFileSystem (STM32H747OTA::FOLDER);
159+ err_mount = _fs->mount (_bd);
167160
168161 if (!err_mount) {
169162 return true ;
@@ -172,43 +165,34 @@ bool STM32H7OTACloudProcess::storageInit() {
172165 return false ;
173166}
174167
175- bool STM32H7OTACloudProcess::storageOpen ( ) {
168+ bool STM32H7OTACloudProcess::findProgramLength ( uint32_t & program_length ) {
176169 DIR * dir = NULL ;
177- if ((dir = opendir (" /fs" )) != NULL )
178- {
179- if (findProgramLength (dir, _program_length))
180- {
181- closedir (dir);
182- return true ;
183- }
184- closedir (dir);
185- }
170+ struct dirent * entry = NULL ;
171+ String dirName = " /" + String (STM32H747OTA::FOLDER);
172+ bool found = false ;
186173
187- return false ;
188- }
174+ if ((dir = opendir (dirName.c_str ())) == NULL ) {
175+ return false ;
176+ }
189177
190- bool findProgramLength (DIR * dir, uint32_t & program_length) {
191- struct dirent * entry = NULL ;
192178 while ((entry = readdir (dir)) != NULL ) {
193- if (strcmp (entry->d_name , " UPDATE.BIN " ) == 0 ) { // FIXME use constants
179+ if (strcmp (entry->d_name , STM32H747OTA::NAME ) == 0 ) {
194180 struct stat stat_buf;
195- stat (" /fs/UPDATE.BIN " , &stat_buf);
181+ stat (_filename. c_str () , &stat_buf);
196182 program_length = stat_buf.st_size ;
197- return true ;
183+ found = true ;
198184 }
199185 }
200-
201- return false ;
186+ closedir (dir);
187+ return found ;
202188}
203189
204- // extern uint32_t __stext = ~0;
205190extern uint32_t __etext;
206191extern uint32_t _sdata;
207192extern uint32_t _edata;
208193
209194void * STM32H7OTACloudProcess::appStartAddress () {
210195 return (void *)0x8040000 ;
211- // return &__stext;
212196}
213197
214198uint32_t STM32H7OTACloudProcess::appSize () {
0 commit comments