@@ -40,31 +40,23 @@ static void __attribute__((noreturn)) task_fatal_error(void) {
40
40
41
41
void common_hal_ota_flash (const void * buf , const size_t len ) {
42
42
esp_err_t err ;
43
- /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */
43
+
44
44
esp_ota_handle_t update_handle = 0 ;
45
45
const esp_partition_t * update_partition = NULL ;
46
+ update_partition = esp_ota_get_next_update_partition (NULL );
46
47
47
- ESP_LOGI (TAG , "Starting update" );
48
-
49
- const esp_partition_t * configured = esp_ota_get_boot_partition ();
50
48
const esp_partition_t * running = esp_ota_get_running_partition ();
49
+ const esp_partition_t * last_invalid = esp_ota_get_last_invalid_partition ();
51
50
52
- if (configured != running ) {
53
- ESP_LOGW (TAG , "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x" ,
54
- configured -> address , running -> address );
55
- ESP_LOGW (TAG , "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)" );
56
- }
57
51
ESP_LOGI (TAG , "Running partition type %d subtype %d (offset 0x%08x)" ,
58
52
running -> type , running -> subtype , running -> address );
59
53
60
- update_partition = esp_ota_get_next_update_partition (NULL );
61
54
ESP_LOGI (TAG , "Writing partition type %d subtype %d (offset 0x%08x)\n" ,
62
55
update_partition -> type , update_partition -> subtype , update_partition -> address );
63
- assert (update_partition != NULL );
64
56
57
+ assert (update_partition != NULL );
65
58
66
59
if (len > sizeof (esp_image_header_t ) + sizeof (esp_image_segment_header_t ) + sizeof (esp_app_desc_t )) {
67
- // check current version with downloading
68
60
esp_app_desc_t new_app_info ;
69
61
memcpy (& new_app_info , & ((char * )buf )[sizeof (esp_image_header_t ) + sizeof (esp_image_segment_header_t )], sizeof (esp_app_desc_t ));
70
62
ESP_LOGI (TAG , "New firmware version: %s" , new_app_info .version );
@@ -74,33 +66,30 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
74
66
ESP_LOGI (TAG , "Running firmware version: %s" , running_app_info .version );
75
67
}
76
68
77
- const esp_partition_t * last_invalid_app = esp_ota_get_last_invalid_partition ();
78
69
esp_app_desc_t invalid_app_info ;
79
- if (esp_ota_get_partition_description (last_invalid_app , & invalid_app_info ) == ESP_OK ) {
70
+ if (esp_ota_get_partition_description (last_invalid , & invalid_app_info ) == ESP_OK ) {
80
71
ESP_LOGI (TAG , "Last invalid firmware version: %s" , invalid_app_info .version );
81
72
}
82
73
83
- // check current version with last invalid partition
84
- if (last_invalid_app != NULL ) {
85
- if (memcmp (invalid_app_info .version , new_app_info .version , sizeof (new_app_info .version )) == 0 ) {
74
+ // check new version with running version
75
+ if (memcmp (new_app_info .version , running_app_info .version , sizeof (new_app_info .version )) == 0 ) {
76
+ ESP_LOGW (TAG , "New version is the same as running version." );
77
+ task_fatal_error ();
78
+ }
79
+
80
+ // check new version with last invalid partition
81
+ if (last_invalid != NULL ) {
82
+ if (memcmp (new_app_info .version , invalid_app_info .version , sizeof (new_app_info .version )) == 0 ) {
86
83
ESP_LOGW (TAG , "New version is the same as invalid version." );
87
- ESP_LOGW (TAG , "Previously, there was an attempt to launch the firmware with %s version, but it failed." , invalid_app_info .version );
88
- ESP_LOGW (TAG , "The firmware has been rolled back to the previous version." );
89
84
task_fatal_error ();
90
85
}
91
86
}
92
87
93
- if (memcmp (new_app_info .version , running_app_info .version , sizeof (new_app_info .version )) == 0 ) {
94
- ESP_LOGW (TAG , "Current running version is the same as a new. We will not continue the update." );
95
- task_fatal_error ();
96
- }
97
-
98
88
err = esp_ota_begin (update_partition , OTA_WITH_SEQUENTIAL_WRITES , & update_handle );
99
89
if (err != ESP_OK ) {
100
90
ESP_LOGE (TAG , "esp_ota_begin failed (%s)" , esp_err_to_name (err ));
101
91
task_fatal_error ();
102
92
}
103
- ESP_LOGI (TAG , "esp_ota_begin succeeded" );
104
93
} else {
105
94
ESP_LOGE (TAG , "received package is not fit len" );
106
95
task_fatal_error ();
@@ -111,7 +100,6 @@ void common_hal_ota_flash(const void *buf, const size_t len) {
111
100
ESP_LOGE (TAG , "esp_ota_write failed (%s)" , esp_err_to_name (err ));
112
101
task_fatal_error ();
113
102
}
114
- ESP_LOGI (TAG , "Total Write binary data length: %d" , len );
115
103
116
104
err = esp_ota_end (update_handle );
117
105
if (err != ESP_OK ) {
0 commit comments