31
31
#include "esp_log.h"
32
32
#include "esp_ota_ops.h"
33
33
34
- esp_ota_handle_t update_handle = 0 ;
35
- const esp_partition_t * update_partition = NULL ;
34
+ static const esp_partition_t * update_partition = NULL ;
35
+ static esp_ota_handle_t update_handle = 0 ;
36
36
37
+ static bool ota_inited = false;
37
38
static const char * TAG = "OTA" ;
38
39
40
+ static void ota_reset (void ) {
41
+ update_handle = 0 ;
42
+ update_partition = NULL ;
43
+ ota_inited = false;
44
+ }
45
+
39
46
static void __attribute__((noreturn )) task_fatal_error (void ) {
40
47
ESP_LOGE (TAG , "Exiting task due to fatal error..." );
41
48
mp_raise_RuntimeError (translate ("OTA Update Failed" ));
@@ -44,56 +51,61 @@ static void __attribute__((noreturn)) task_fatal_error(void) {
44
51
void common_hal_ota_flash (const void * buf , const size_t len ) {
45
52
esp_err_t err ;
46
53
47
- update_partition = esp_ota_get_next_update_partition (NULL );
48
-
49
54
const esp_partition_t * running = esp_ota_get_running_partition ();
50
55
const esp_partition_t * last_invalid = esp_ota_get_last_invalid_partition ();
51
56
52
- ESP_LOGI ( TAG , "Running partition type %d subtype %d (offset 0x%08x)" ,
53
- running -> type , running -> subtype , running -> address );
57
+ if ( update_partition == NULL ) {
58
+ update_partition = esp_ota_get_next_update_partition ( NULL );
54
59
55
- ESP_LOGI (TAG , "Writing partition type %d subtype %d (offset 0x%08x)\n " ,
56
- update_partition -> type , update_partition -> subtype , update_partition -> address );
60
+ ESP_LOGI (TAG , "Running partition type %d subtype %d (offset 0x%08x)" ,
61
+ running -> type , running -> subtype , running -> address );
57
62
58
- assert (update_partition != NULL );
63
+ ESP_LOGI (TAG , "Writing partition type %d subtype %d (offset 0x%08x)\n" ,
64
+ update_partition -> type , update_partition -> subtype , update_partition -> address );
59
65
60
- if (len > sizeof (esp_image_header_t ) + sizeof (esp_image_segment_header_t ) + sizeof (esp_app_desc_t )) {
61
- esp_app_desc_t new_app_info ;
62
- memcpy (& new_app_info , & ((char * )buf )[sizeof (esp_image_header_t ) + sizeof (esp_image_segment_header_t )], sizeof (esp_app_desc_t ));
63
- ESP_LOGI (TAG , "New firmware version: %s" , new_app_info .version );
66
+ assert (update_partition != NULL );
67
+ }
64
68
65
- esp_app_desc_t running_app_info ;
66
- if (esp_ota_get_partition_description (running , & running_app_info ) == ESP_OK ) {
67
- ESP_LOGI (TAG , "Running firmware version: %s" , running_app_info .version );
68
- }
69
+ if (!ota_inited ) {
70
+ if (len > sizeof (esp_image_header_t ) + sizeof (esp_image_segment_header_t ) + sizeof (esp_app_desc_t )) {
71
+ esp_app_desc_t new_app_info ;
72
+ memcpy (& new_app_info , & ((char * )buf )[sizeof (esp_image_header_t ) + sizeof (esp_image_segment_header_t )], sizeof (esp_app_desc_t ));
73
+ ESP_LOGI (TAG , "New firmware version: %s" , new_app_info .version );
69
74
70
- esp_app_desc_t invalid_app_info ;
71
- if (esp_ota_get_partition_description (last_invalid , & invalid_app_info ) == ESP_OK ) {
72
- ESP_LOGI (TAG , "Last invalid firmware version: %s" , invalid_app_info .version );
73
- }
75
+ esp_app_desc_t running_app_info ;
76
+ if (esp_ota_get_partition_description (running , & running_app_info ) == ESP_OK ) {
77
+ ESP_LOGI (TAG , "Running firmware version: %s" , running_app_info .version );
78
+ }
74
79
75
- // check new version with running version
76
- if (memcmp (new_app_info .version , running_app_info .version , sizeof (new_app_info .version )) == 0 ) {
77
- ESP_LOGW (TAG , "New version is the same as running version." );
78
- task_fatal_error ();
79
- }
80
+ esp_app_desc_t invalid_app_info ;
81
+ if (esp_ota_get_partition_description (last_invalid , & invalid_app_info ) == ESP_OK ) {
82
+ ESP_LOGI (TAG , "Last invalid firmware version: %s" , invalid_app_info .version );
83
+ }
80
84
81
- // check new version with last invalid partition
82
- if (last_invalid != NULL ) {
83
- if (memcmp (new_app_info .version , invalid_app_info .version , sizeof (new_app_info .version )) == 0 ) {
84
- ESP_LOGW (TAG , "New version is the same as invalid version." );
85
+ // check new version with running version
86
+ if (memcmp (new_app_info .version , running_app_info .version , sizeof (new_app_info .version )) == 0 ) {
87
+ ESP_LOGW (TAG , "New version is the same as running version." );
85
88
task_fatal_error ();
86
89
}
87
- }
88
90
89
- err = esp_ota_begin (update_partition , OTA_WITH_SEQUENTIAL_WRITES , & update_handle );
90
- if (err != ESP_OK ) {
91
- ESP_LOGE (TAG , "esp_ota_begin failed (%s)" , esp_err_to_name (err ));
91
+ // check new version with last invalid partition
92
+ if (last_invalid != NULL ) {
93
+ if (memcmp (new_app_info .version , invalid_app_info .version , sizeof (new_app_info .version )) == 0 ) {
94
+ ESP_LOGW (TAG , "New version is the same as invalid version." );
95
+ task_fatal_error ();
96
+ }
97
+ }
98
+
99
+ err = esp_ota_begin (update_partition , OTA_WITH_SEQUENTIAL_WRITES , & update_handle );
100
+ if (err != ESP_OK ) {
101
+ ESP_LOGE (TAG , "esp_ota_begin failed (%s)" , esp_err_to_name (err ));
102
+ task_fatal_error ();
103
+ }
104
+ ota_inited = true;
105
+ } else {
106
+ ESP_LOGE (TAG , "received package is not fit len" );
92
107
task_fatal_error ();
93
108
}
94
- } else {
95
- ESP_LOGE (TAG , "received package is not fit len" );
96
- task_fatal_error ();
97
109
}
98
110
99
111
err = esp_ota_write ( update_handle , buf , len );
@@ -104,20 +116,24 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
104
116
}
105
117
106
118
void common_hal_ota_finish (void ) {
107
- esp_err_t err ;
119
+ if (ota_inited ) {
120
+ esp_err_t err ;
108
121
109
- err = esp_ota_end (update_handle );
110
- if (err != ESP_OK ) {
111
- if (err == ESP_ERR_OTA_VALIDATE_FAILED ) {
112
- ESP_LOGE (TAG , "Image validation failed, image is corrupted" );
122
+ err = esp_ota_end (update_handle );
123
+ if (err != ESP_OK ) {
124
+ if (err == ESP_ERR_OTA_VALIDATE_FAILED ) {
125
+ ESP_LOGE (TAG , "Image validation failed, image is corrupted" );
126
+ }
127
+ ESP_LOGE (TAG , "esp_ota_end failed (%s)!" , esp_err_to_name (err ));
128
+ task_fatal_error ();
113
129
}
114
- ESP_LOGE (TAG , "esp_ota_end failed (%s)!" , esp_err_to_name (err ));
115
- task_fatal_error ();
116
- }
117
130
118
- err = esp_ota_set_boot_partition (update_partition );
119
- if (err != ESP_OK ) {
120
- ESP_LOGE (TAG , "esp_ota_set_boot_partition failed (%s)!" , esp_err_to_name (err ));
121
- task_fatal_error ();
131
+ err = esp_ota_set_boot_partition (update_partition );
132
+ if (err != ESP_OK ) {
133
+ ESP_LOGE (TAG , "esp_ota_set_boot_partition failed (%s)!" , esp_err_to_name (err ));
134
+ task_fatal_error ();
135
+ }
136
+
137
+ ota_reset ();
122
138
}
123
139
}
0 commit comments