1
1
/*
2
- * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2
+ * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
@@ -29,6 +29,29 @@ _Static_assert(DEFAULT_OTA_BUF_SIZE > (sizeof(esp_image_header_t) + sizeof(esp_i
29
29
/* Setting event group */
30
30
#define MSC_CONNECT (1 << 0) // MSC device connect
31
31
32
+ /**
33
+ * @brief To prevent the VFS from being unregistered during file read and write operations.
34
+ *
35
+ * @param semaphore Semaphore to take.
36
+ * @return esp_err_t
37
+ * - ESP_OK on success or if the semaphore is NULL.
38
+ * - ESP_FAIL if taking the semaphore fails.
39
+ */
40
+ #define SEMAPHORE_TAKE (semaphore ) \
41
+ ((semaphore == NULL) ? ESP_OK : \
42
+ ((xSemaphoreTake(semaphore, pdMS_TO_TICKS(10)) == pdTRUE) ? ESP_OK : ESP_FAIL))
43
+
44
+ /**
45
+ * @brief To prevent the VFS from being unregistered during file read and write operations.
46
+ *
47
+ * @param semaphore Semaphore to give.
48
+ * @return esp_err_t
49
+ * - ESP_OK on success or if the semaphore is NULL.
50
+ */
51
+ #define SEMAPHORE_GIVE (semaphore ) \
52
+ ((semaphore == NULL) ? ESP_OK : \
53
+ (xSemaphoreGive(semaphore), ESP_OK))
54
+
32
55
typedef struct {
33
56
EventGroupHandle_t mscEventGroup ;
34
57
bool bulk_flash_erase ;
@@ -107,6 +130,7 @@ esp_msc_ota_status_t esp_msc_ota_get_status(esp_msc_ota_handle_t handle)
107
130
108
131
static esp_err_t _read_header (esp_msc_ota_t * msc_ota )
109
132
{
133
+ esp_err_t err = ESP_OK ;
110
134
EventBits_t bits = xEventGroupWaitBits (msc_ota -> mscEventGroup , MSC_CONNECT , pdFALSE , pdFALSE , 0 );
111
135
MSC_OTA_CHECK (bits & MSC_CONNECT , "msc can't be disconnect" , ESP_ERR_INVALID_STATE );
112
136
/*
@@ -115,6 +139,8 @@ static esp_err_t _read_header(esp_msc_ota_t *msc_ota)
115
139
int data_read_size = IMAGE_HEADER_SIZE ;
116
140
int data_read = 0 ;
117
141
142
+ err = SEMAPHORE_TAKE (msc_read_semaphore );
143
+ MSC_OTA_CHECK (err == ESP_OK , "take msc_read_semaphore failed" , ESP_FAIL );
118
144
FILE * file = fopen (msc_ota -> ota_bin_path , "rb" );
119
145
MSC_OTA_CHECK (file != NULL , "Failed to open file for reading" , ESP_ERR_NOT_FOUND );
120
146
@@ -125,27 +151,28 @@ static esp_err_t _read_header(esp_msc_ota_t *msc_ota)
125
151
data_read_size = data_read_size > fileLength ? fileLength : data_read_size ;
126
152
ESP_LOGI (TAG , "Reading file %s, size: %d, total size: %" PRIu32 "" , msc_ota -> ota_bin_path , data_read_size , fileLength );
127
153
128
- BaseType_t ret = xSemaphoreTake (msc_read_semaphore , pdMS_TO_TICKS (10 ));
129
- MSC_OTA_CHECK (ret == pdTRUE , "take msc_read_semaphore failed" , ESP_FAIL );
130
154
data_read = fread (msc_ota -> ota_upgrade_buf , 1 , data_read_size , file );
131
- xSemaphoreGive (msc_read_semaphore );
132
155
133
156
if (data_read == data_read_size ) {
134
157
msc_ota -> binary_file_read_len = data_read ;
135
- fclose ( file ) ;
136
- return ESP_OK ;
158
+ err = ESP_OK ;
159
+ goto file_close ;
137
160
} else if (data_read > 0 && data_read < data_read_size ) {
138
161
msc_ota -> binary_file_read_len = data_read ;
139
- fclose ( file ) ;
140
- return ESP_OK ;
162
+ err = ESP_OK ;
163
+ goto file_close ;
141
164
} else if (ferror (file )) {
165
+ err = ESP_FAIL ;
142
166
ESP_LOGI (TAG , "Error reading from file" );
143
167
} else if (feof (file )) {
144
- ESP_LOGI (TAG , "End of file reached.\n" );
168
+ err = ESP_FAIL ;
169
+ ESP_LOGI (TAG , "End of file reached." );
145
170
}
146
171
172
+ file_close :
147
173
fclose (file );
148
- return ESP_FAIL ;
174
+ SEMAPHORE_GIVE (msc_read_semaphore );
175
+ return err ;
149
176
}
150
177
151
178
static esp_err_t _ota_verify_chip_id (const void * arg )
@@ -198,9 +225,14 @@ esp_err_t esp_msc_ota_begin(esp_msc_ota_config_t *config, esp_msc_ota_handle_t *
198
225
199
226
esp_event_handler_register (ESP_MSC_HOST_EVENT , ESP_EVENT_ANY_ID , & _esp_msc_host_handler , msc_ota );
200
227
201
- ESP_LOGI (TAG , "Waiting for MSC device to connect..." );
202
- EventBits_t bits = xEventGroupWaitBits (msc_ota -> mscEventGroup , MSC_CONNECT , pdFALSE , pdFALSE , config -> wait_msc_connect );
203
- MSC_OTA_CHECK_GOTO (bits & MSC_CONNECT , "TIMEOUT: MSC device not connected" , msc_cleanup );
228
+ if (!config -> skip_msc_connect_wait ) {
229
+ ESP_LOGI (TAG , "Waiting for MSC device to connect..." );
230
+ EventBits_t bits = xEventGroupWaitBits (msc_ota -> mscEventGroup , MSC_CONNECT , pdFALSE , pdFALSE , config -> wait_msc_connect );
231
+ MSC_OTA_CHECK_GOTO (bits & MSC_CONNECT , "TIMEOUT: MSC device not connected" , msc_cleanup );
232
+ } else {
233
+ xEventGroupSetBits (msc_ota -> mscEventGroup , MSC_CONNECT );
234
+ ESP_LOGI (TAG , "skip_msc_connect_wait is true, set MSC_CONNECT event directly" );
235
+ }
204
236
205
237
msc_ota -> update_partition = NULL ;
206
238
ESP_LOGI (TAG , "Starting OTA..." );
@@ -272,20 +304,23 @@ esp_err_t esp_msc_ota_perform(esp_msc_ota_handle_t handle)
272
304
MSC_OTA_CHECK (bits & MSC_CONNECT , "msc can't be disconnect" , ESP_ERR_INVALID_STATE );
273
305
if (msc_ota -> file == NULL ) {
274
306
307
+ err = SEMAPHORE_TAKE (msc_read_semaphore );
308
+ MSC_OTA_CHECK (err == ESP_OK , "take msc_read_semaphore failed" , ESP_FAIL );
275
309
FILE * file = fopen (msc_ota -> ota_bin_path , "rb" );
276
310
MSC_OTA_CHECK (file != NULL , "Failed to open file for reading" , ESP_ERR_NOT_FOUND );
277
311
fseek (file , msc_ota -> binary_file_read_len , SEEK_CUR );
312
+ SEMAPHORE_GIVE (msc_read_semaphore );
278
313
msc_ota -> file = file ;
279
314
}
280
315
uint32_t * fileLength = & msc_ota -> binary_file_read_len ;
281
316
uint32_t * totalLength = & msc_ota -> binary_file_len ;
282
317
if (* fileLength < * totalLength ) {
283
318
size_t readLength = * fileLength > msc_ota -> ota_upgrade_buf_size ? msc_ota -> ota_upgrade_buf_size : * fileLength ;
284
319
285
- BaseType_t ret = xSemaphoreTake (msc_read_semaphore , pdMS_TO_TICKS ( 10 ) );
286
- MSC_OTA_CHECK (ret == pdTRUE , "take msc_read_semaphore failed" , ESP_FAIL );
320
+ err = SEMAPHORE_TAKE (msc_read_semaphore );
321
+ MSC_OTA_CHECK (err == ESP_OK , "take msc_read_semaphore failed" , ESP_FAIL );
287
322
data_read = fread (msc_ota -> ota_upgrade_buf , 1 , readLength , msc_ota -> file );
288
- xSemaphoreGive (msc_read_semaphore );
323
+ SEMAPHORE_GIVE (msc_read_semaphore );
289
324
290
325
MSC_OTA_CHECK (data_read > 0 , "Failed to read file" , ESP_ERR_INVALID_SIZE );
291
326
err = esp_ota_write (msc_ota -> update_handle , (const void * )msc_ota -> ota_upgrade_buf , data_read );
@@ -299,7 +334,10 @@ esp_err_t esp_msc_ota_perform(esp_msc_ota_handle_t handle)
299
334
}
300
335
if (* fileLength >= * totalLength ) {
301
336
msc_ota -> status = ESP_MSC_OTA_SUCCESS ;
337
+ err = SEMAPHORE_TAKE (msc_read_semaphore );
338
+ MSC_OTA_CHECK (err == ESP_OK , "take msc_read_semaphore failed" , ESP_FAIL );
302
339
fclose (msc_ota -> file );
340
+ SEMAPHORE_GIVE (msc_read_semaphore );
303
341
return ESP_OK ;
304
342
}
305
343
return ESP_OK ;
@@ -338,6 +376,7 @@ esp_err_t esp_msc_ota_end(esp_msc_ota_handle_t handle)
338
376
}
339
377
}
340
378
esp_event_handler_unregister (ESP_MSC_HOST_EVENT , ESP_EVENT_ANY_ID , & _esp_msc_host_handler );
379
+ vEventGroupDelete (msc_ota -> mscEventGroup );
341
380
free (msc_ota );
342
381
esp_msc_ota_dispatch_event (ESP_MSC_OTA_FINISH , NULL , 0 );
343
382
return err ;
@@ -353,7 +392,10 @@ esp_err_t esp_msc_ota_abort(esp_msc_ota_handle_t handle)
353
392
case ESP_MSC_OTA_SUCCESS :
354
393
case ESP_MSC_OTA_IN_PROGRESS :
355
394
if (msc_ota -> file ) {
395
+ err = SEMAPHORE_TAKE (msc_read_semaphore );
396
+ MSC_OTA_CHECK (err == ESP_OK , "take msc_read_semaphore failed" , ESP_FAIL );
356
397
fclose (msc_ota -> file );
398
+ SEMAPHORE_GIVE (msc_read_semaphore );
357
399
}
358
400
err = esp_ota_abort (msc_ota -> update_handle );
359
401
[[fallthrough ]];
0 commit comments