11
11
#include " AIoTC_Config.h"
12
12
#if defined(BOARD_STM32H7) && OTA_ENABLED
13
13
#include " OTASTM32H7.h"
14
-
15
- #include " utility/watchdog/Watchdog.h"
16
14
#include < STM32H747_System.h>
17
15
18
- static bool findProgramLength (DIR * dir, uint32_t & program_length);
19
-
20
- const char STM32H7OTACloudProcess::UPDATE_FILE_NAME[] = " /fs/UPDATE.BIN" ;
21
-
22
16
STM32H7OTACloudProcess::STM32H7OTACloudProcess (MessageStream *ms, Client* client)
23
17
: OTADefaultCloudProcessInterface(ms, client)
24
18
, decompressed(nullptr )
25
19
, _bd_raw_qspi(nullptr )
26
- , _program_length(0 )
27
20
, _bd(nullptr )
28
- , _fs(nullptr ) {
21
+ , _fs(nullptr )
22
+ , _filename(" /" + String(STM32H747OTA::FOLDER) + "/" + String(STM32H747OTA::NAME)) {
29
23
30
24
}
31
25
@@ -44,7 +38,6 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::resume(Message* msg) {
44
38
45
39
void STM32H7OTACloudProcess::update () {
46
40
OTADefaultCloudProcessInterface::update ();
47
- watchdog_reset (); // FIXME this should npot be performed here
48
41
}
49
42
50
43
int STM32H7OTACloudProcess::writeFlash (uint8_t * const buffer, size_t len) {
@@ -65,10 +58,13 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::startOTA() {
65
58
}
66
59
67
60
// this could be useless, since we are writing over it
68
- remove (UPDATE_FILE_NAME );
61
+ remove (_filename. c_str () );
69
62
70
- decompressed = fopen (UPDATE_FILE_NAME , " wb" );
63
+ decompressed = fopen (_filename. c_str () , " wb" );
71
64
65
+ if (decompressed == nullptr ) {
66
+ return ErrorOpenUpdateFileFail;
67
+ }
72
68
// start the download if the setup for ota storage is successful
73
69
return OTADefaultCloudProcessInterface::startOTA ();
74
70
}
@@ -78,18 +74,20 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::flashOTA() {
78
74
fclose (decompressed);
79
75
decompressed = nullptr ;
80
76
77
+ uint32_t updateLength = 0 ;
78
+
81
79
/* Schedule the firmware update. */
82
- if (!storageOpen ( )) {
80
+ if (!findProgramLength (updateLength )) {
83
81
return OtaStorageOpenFail;
84
82
}
85
83
86
84
storageClean ();
87
85
88
86
// 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 );
93
91
94
92
return Reboot;
95
93
}
@@ -106,7 +104,7 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::reboot() {
106
104
void STM32H7OTACloudProcess::reset () {
107
105
OTADefaultCloudProcessInterface::reset ();
108
106
109
- remove (UPDATE_FILE_NAME );
107
+ remove (_filename. c_str () );
110
108
111
109
storageClean ();
112
110
}
@@ -156,14 +154,9 @@ bool STM32H7OTACloudProcess::storageInit() {
156
154
}
157
155
}
158
156
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);
167
160
168
161
if (!err_mount) {
169
162
return true ;
@@ -172,43 +165,34 @@ bool STM32H7OTACloudProcess::storageInit() {
172
165
return false ;
173
166
}
174
167
175
- bool STM32H7OTACloudProcess::storageOpen ( ) {
168
+ bool STM32H7OTACloudProcess::findProgramLength ( uint32_t & program_length ) {
176
169
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 ;
186
173
187
- return false ;
188
- }
174
+ if ((dir = opendir (dirName.c_str ())) == NULL ) {
175
+ return false ;
176
+ }
189
177
190
- bool findProgramLength (DIR * dir, uint32_t & program_length) {
191
- struct dirent * entry = NULL ;
192
178
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 ) {
194
180
struct stat stat_buf;
195
- stat (" /fs/UPDATE.BIN " , &stat_buf);
181
+ stat (_filename. c_str () , &stat_buf);
196
182
program_length = stat_buf.st_size ;
197
- return true ;
183
+ found = true ;
198
184
}
199
185
}
200
-
201
- return false ;
186
+ closedir (dir);
187
+ return found ;
202
188
}
203
189
204
- // extern uint32_t __stext = ~0;
205
190
extern uint32_t __etext;
206
191
extern uint32_t _sdata;
207
192
extern uint32_t _edata;
208
193
209
194
void * STM32H7OTACloudProcess::appStartAddress () {
210
195
return (void *)0x8040000 ;
211
- // return &__stext;
212
196
}
213
197
214
198
uint32_t STM32H7OTACloudProcess::appSize () {
0 commit comments