main/updates.c

changeset 67
4badc7e7d629
parent 54
7b134c27fadb
child 70
d6838a268020
equal deleted inserted replaced
65:dd7019356c8c 67:4badc7e7d629
34 esp_ota_handle_t update_handle = 0; 34 esp_ota_handle_t update_handle = 0;
35 35
36 TFT_setFont(DEJAVU18_FONT, NULL); 36 TFT_setFont(DEJAVU18_FONT, NULL);
37 _fg = TFT_CYAN; 37 _fg = TFT_CYAN;
38 const esp_partition_t *running = esp_ota_get_running_partition(); 38 const esp_partition_t *running = esp_ota_get_running_partition();
39 snprintf(temp, 63, "Running part.type %d sub %d,\r\nat offset 0x%08x\r\n", 39 // snprintf(temp, 63, "Running part.type %d sub %d,\r\nat offset 0x%08x\r\n",
40 running->type, running->subtype, running->address); 40 // running->type, running->subtype, running->address);
41 TFT_print(temp, 0, LASTY); 41 // TFT_print(temp, 0, LASTY);
42 42
43 /* 43 /*
44 * Don't use https because it costs more then 100K memory. 44 * Don't use https because it costs more then 100K memory.
45 */ 45 */
46 esp_http_client_config_t update = { 46 esp_http_client_config_t update = {
80 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); 80 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
81 http_cleanup(client); 81 http_cleanup(client);
82 goto updateerr; 82 goto updateerr;
83 } 83 }
84 84
85 TFT_print((char *)"Begin download.\r\n", 0, LASTY); 85 TFT_print((char *)"Begin firmware download.\r\n", 0, LASTY);
86 ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length); 86 ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length);
87 int binary_file_length = 0; 87 int binary_file_length = 0;
88 /*deal with all receive packet*/ 88 /*deal with all receive packet*/
89 bool image_header_was_checked = false;
89 while (1) { 90 while (1) {
90 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE); 91 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
91 if (data_read < 0) { 92 if (data_read < 0) {
92 ESP_LOGE(TAG, "Error: data read error"); 93 ESP_LOGE(TAG, "Error: data read error");
93 http_cleanup(client); 94 http_cleanup(client);
94 goto updateerr;; 95 goto updateerr;;
95 } else if (data_read > 0) { 96 } else if (data_read > 0) {
97 if (image_header_was_checked == false) {
98 esp_app_desc_t new_app_info;
99 if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
100 // check current version with downloading
101 memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
102
103 esp_app_desc_t running_app_info;
104 if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
105 ESP_LOGI(TAG, "Running firmware version: %s, %s %s", running_app_info.version, running_app_info.date, running_app_info.time);
106 }
107
108 const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
109 esp_app_desc_t invalid_app_info;
110 if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
111 ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
112 }
113
114 // check current sha256 with last invalid partition
115 if (last_invalid_app != NULL) {
116 if (memcmp(invalid_app_info.app_elf_sha256, new_app_info.app_elf_sha256, 32) == 0) {
117 ESP_LOGW(TAG, "New version is the same as invalid version.");
118 ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
119 ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
120 http_cleanup(client);
121 goto updateok;
122 }
123 }
124
125 if (memcmp(new_app_info.app_elf_sha256, running_app_info.app_elf_sha256, 32) == 0) {
126 ESP_LOGI(TAG, "Current running version is the same as a new.");
127 http_cleanup(client);
128 goto updateok;
129 }
130
131 image_header_was_checked = true;
132
133 err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
134 if (err != ESP_OK) {
135 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
136 http_cleanup(client);
137 goto updateerr;
138 }
139 ESP_LOGI(TAG, "Continue upgrade application");
140 } else {
141 ESP_LOGE(TAG, "Received package is not fit len");
142 http_cleanup(client);
143 goto updateerr;
144 }
145 }
96 err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read); 146 err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
97 if (err != ESP_OK) { 147 if (err != ESP_OK) {
98 http_cleanup(client); 148 http_cleanup(client);
99 goto updateerr;; 149 goto updateerr;;
100 } 150 }
101 binary_file_length += data_read; 151 binary_file_length += data_read;
102 // ESP_LOGD(TAG, "Written image length %d", binary_file_length); 152 ESP_LOGD(TAG, "Written image length %d", binary_file_length);
103 } else if (data_read == 0) { 153 } else if (data_read == 0) {
104 break; 154 break;
105 } 155 }
106 } 156 }
107 157
108 ESP_LOGI(TAG, "Download complete, binary data length: %d", binary_file_length); 158 ESP_LOGI(TAG, "Download complete, binary data length: %d", binary_file_length);
109 http_cleanup(client); 159 http_cleanup(client);
110 160
161 if (binary_file_length != content_length) {
162 ESP_LOGE(TAG, "Incomplete file");
163 goto updateerr;
164 }
111 if (esp_ota_end(update_handle) != ESP_OK) { 165 if (esp_ota_end(update_handle) != ESP_OK) {
112 ESP_LOGE(TAG, "esp_ota_end failed!"); 166 ESP_LOGE(TAG, "esp_ota_end failed!");
113 goto updateerr; 167 goto updateerr;
114 } 168 }
115 snprintf(temp, 63, "Received image %d bytes\r\n", binary_file_length); 169 snprintf(temp, 63, "Received image %d bytes\r\n", binary_file_length);
116 TFT_print(temp, 0, LASTY); 170 TFT_print(temp, 0, LASTY);
117 171
118 if (esp_partition_check_identity(esp_ota_get_running_partition(), update_partition) == true) {
119 ESP_LOGI(TAG, "Already the latest version");
120 TFT_print((char *)"Already the latest version.\r\n", LASTX, LASTY);
121 goto updateok;
122 }
123
124 /* 172 /*
125 * Here we have a different and hopefully newer version, install and boot it. 173 * Here we have new version, install and boot it.
126 */ 174 */
127 err = esp_ota_set_boot_partition(update_partition); 175 err = esp_ota_set_boot_partition(update_partition);
128 if (err != ESP_OK) { 176 if (err != ESP_OK) {
129 ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); 177 ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
130 goto updateerr; 178 goto updateerr;
132 180
133 ESP_LOGI(TAG, "Prepare to restart system!"); 181 ESP_LOGI(TAG, "Prepare to restart system!");
134 TFT_print((char *)"Rebooting ...", 0, LASTY); 182 TFT_print((char *)"Rebooting ...", 0, LASTY);
135 vTaskDelay(1000 / portTICK_PERIOD_MS); 183 vTaskDelay(1000 / portTICK_PERIOD_MS);
136 esp_restart(); 184 esp_restart();
137 return ; 185 return;
138 186
139 updateerr: 187 updateerr:
140 _fg = TFT_RED; 188 _fg = TFT_RED;
141 TFT_print((char *)"Error\r\n", 0, LASTY); 189 TFT_print((char *)"Error\r\n", 0, LASTY);
142 190
304 if (rc < 0) { 352 if (rc < 0) {
305 ESP_LOGE(TAG, "Updates failed"); 353 ESP_LOGE(TAG, "Updates failed");
306 fclose(f); 354 fclose(f);
307 goto spiffs_error; 355 goto spiffs_error;
308 } 356 }
309 // vTaskDelay(10 / portTICK_PERIOD_MS);
310 } 357 }
311 fclose(f); 358 fclose(f);
312 unlink("/spiffs/version.old"); 359 unlink("/spiffs/version.old");
313 TFT_print((char *)"updated\r\n", LASTX, LASTY); 360 TFT_print((char *)"updated\r\n", LASTX, LASTY);
314 return; 361 return;
315 362
316 spiffs_error: 363 spiffs_error:
317 _fg = TFT_RED; 364 _fg = TFT_RED;
318 TFT_print((char *)"error\r\n", LASTX, LASTY); 365 TFT_print((char *)"error\r\n", LASTX, LASTY);
319
320 } 366 }
321 367
322 368
323 369
324 /* 370 /*

mercurial