squeezelite-ota.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* OTA example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_system.h"
  10. #include "esp_event.h"
  11. #include "esp_log.h"
  12. #include "esp_ota_ops.h"
  13. #include "esp_https_ota.h"
  14. #include "string.h"
  15. #include <stdbool.h>
  16. #include "nvs.h"
  17. #include "nvs_flash.h"
  18. #include "cmd_system.h"
  19. #include "esp_err.h"
  20. #include "tcpip_adapter.h"
  21. #include "squeezelite-ota.h"
  22. #include "nvs_utilities.h"
  23. #include <time.h>
  24. #include <sys/time.h>
  25. #include <stdarg.h>
  26. #include "esp_image_format.h"
  27. #include "esp_secure_boot.h"
  28. #include "esp_flash_encrypt.h"
  29. #include "esp_spi_flash.h"
  30. #include "sdkconfig.h"
  31. #include "esp_ota_ops.h"
  32. #define OTA_FLASH_ERASE_BLOCK (1024*100)
  33. static const char *TAG = "squeezelite-ota";
  34. extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
  35. extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
  36. char * cert=NULL;
  37. static struct {
  38. char status_text[81];
  39. uint32_t ota_actual_len;
  40. uint32_t ota_total_len;
  41. char * redirected_url;
  42. char * current_url;
  43. bool bRedirectFound;
  44. bool bOTAStarted;
  45. bool bInitialized;
  46. uint8_t lastpct;
  47. uint8_t newpct;
  48. struct timeval OTA_start;
  49. bool bOTAThreadStarted;
  50. } ota_status;
  51. struct timeval tv;
  52. static esp_http_client_config_t ota_config;
  53. extern void wifi_manager_refresh_ota_json();
  54. void _printMemStats(){
  55. ESP_LOGD(TAG,"Heap internal:%zu (min:%zu) external:%zu (min:%zu)",
  56. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  57. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  58. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  59. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM));
  60. }
  61. void triggerStatusJsonRefresh(bool bDelay,const char * status, ...){
  62. va_list args;
  63. va_start(args, status);
  64. vsnprintf(ota_status.status_text,sizeof(ota_status.status_text)-1,status, args);
  65. va_end(args);
  66. _printMemStats();
  67. wifi_manager_refresh_ota_json();
  68. if(bDelay){
  69. ESP_LOGD(TAG,"Holding task...");
  70. vTaskDelay(200 / portTICK_PERIOD_MS); // wait here for a short amount of time. This will help with refreshing the UI status
  71. ESP_LOGD(TAG,"Done holding task...");
  72. }
  73. else
  74. {
  75. ESP_LOGI(TAG,"%s",ota_status.status_text);
  76. taskYIELD();
  77. }
  78. }
  79. const char * ota_get_status(){
  80. if(!ota_status.bInitialized)
  81. {
  82. memset(ota_status.status_text, 0x00,sizeof(ota_status.status_text));
  83. ota_status.bInitialized = true;
  84. }
  85. return ota_status.status_text;
  86. }
  87. uint8_t ota_get_pct_complete(){
  88. return ota_status.ota_total_len==0?0:
  89. (uint8_t)((float)ota_status.ota_actual_len/(float)ota_status.ota_total_len*100.0f);
  90. }
  91. static void __attribute__((noreturn)) task_fatal_error(void)
  92. {
  93. ESP_LOGE(TAG, "Exiting task due to fatal error...");
  94. (void)vTaskDelete(NULL);
  95. while (1) {
  96. ;
  97. }
  98. }
  99. #define FREE_RESET(p) if(p!=NULL) { free(p); p=NULL; }
  100. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  101. {
  102. // --------------
  103. // Received parameters
  104. //
  105. // esp_http_client_event_id_tevent_id event_id, to know the cause of the event
  106. // esp_http_client_handle_t client
  107. // esp_http_client_handle_t context
  108. // void *data data of the event
  109. // int data_len - data length of data
  110. // void *user_data -- user_data context, from esp_http_client_config_t user_data
  111. // char *header_key For HTTP_EVENT_ON_HEADER event_id, it’s store current http header key
  112. // char *header_value For HTTP_EVENT_ON_HEADER event_id, it’s store current http header value
  113. // --------------
  114. switch (evt->event_id) {
  115. case HTTP_EVENT_ERROR:
  116. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  117. _printMemStats();
  118. //strncpy(ota_status,"HTTP_EVENT_ERROR",sizeof(ota_status)-1);
  119. break;
  120. case HTTP_EVENT_ON_CONNECTED:
  121. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  122. if(ota_status.bOTAStarted) triggerStatusJsonRefresh(true,"Installing...");
  123. ota_status.ota_total_len=0;
  124. ota_status.ota_actual_len=0;
  125. ota_status.lastpct=0;
  126. ota_status.newpct=0;
  127. gettimeofday(&ota_status.OTA_start, NULL);
  128. break;
  129. case HTTP_EVENT_HEADER_SENT:
  130. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  131. break;
  132. case HTTP_EVENT_ON_HEADER:
  133. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, status_code=%d, key=%s, value=%s",esp_http_client_get_status_code(evt->client),evt->header_key, evt->header_value);
  134. if (strcasecmp(evt->header_key, "location") == 0) {
  135. FREE_RESET(ota_status.redirected_url);
  136. ota_status.redirected_url=strdup(evt->header_value);
  137. ESP_LOGW(TAG,"OTA will redirect to url: %s",ota_status.redirected_url);
  138. ota_status.bRedirectFound= true;
  139. }
  140. if (strcasecmp(evt->header_key, "content-length") == 0) {
  141. ota_status.ota_total_len = atol(evt->header_value);
  142. ESP_LOGW(TAG, "Content length found: %s, parsed to %d", evt->header_value, ota_status.ota_total_len);
  143. }
  144. break;
  145. case HTTP_EVENT_ON_DATA:
  146. if(!ota_status.bOTAStarted) {
  147. ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, status_code=%d, len=%d",esp_http_client_get_status_code(evt->client), evt->data_len);
  148. }
  149. else if(ota_status.bOTAStarted && esp_http_client_get_status_code(evt->client) == 200 ){
  150. ota_status.ota_actual_len+=evt->data_len;
  151. if(ota_get_pct_complete()%5 == 0) ota_status.newpct = ota_get_pct_complete();
  152. if(ota_status.lastpct!=ota_status.newpct )
  153. {
  154. gettimeofday(&tv, NULL);
  155. uint32_t elapsed_ms= (tv.tv_sec-ota_status.OTA_start.tv_sec )*1000+(tv.tv_usec-ota_status.OTA_start.tv_usec)/1000;
  156. ESP_LOGI(TAG,"OTA progress : %d/%d (%d pct), %d KB/s", ota_status.ota_actual_len, ota_status.ota_total_len, ota_status.newpct, elapsed_ms>0?ota_status.ota_actual_len*1000/elapsed_ms/1024:0);
  157. wifi_manager_refresh_ota_json();
  158. ota_status.lastpct=ota_status.newpct;
  159. }
  160. }
  161. break;
  162. case HTTP_EVENT_ON_FINISH:
  163. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  164. break;
  165. case HTTP_EVENT_DISCONNECTED:
  166. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  167. break;
  168. }
  169. return ESP_OK;
  170. }
  171. esp_err_t init_config(esp_http_client_config_t * conf, const char * url){
  172. memset(conf, 0x00, sizeof(esp_http_client_config_t));
  173. conf->cert_pem =cert==NULL?(char *)server_cert_pem_start:cert;
  174. conf->event_handler = _http_event_handler;
  175. conf->buffer_size = 2048*4;
  176. conf->disable_auto_redirect=true;
  177. conf->skip_cert_common_name_check = false;
  178. conf->url = strdup(url);
  179. conf->max_redirection_count = 0;
  180. return ESP_OK;
  181. }
  182. esp_err_t _erase_last_boot_app_partition(void)
  183. {
  184. uint16_t num_passes=0;
  185. uint16_t remain_size=0;
  186. const esp_partition_t *ota_partition=NULL;
  187. const esp_partition_t *ota_data_partition=NULL;
  188. esp_err_t err=ESP_OK;
  189. ESP_LOGI(TAG, "Looking for OTA partition.");
  190. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0 , NULL);
  191. if(it == NULL){
  192. ESP_LOGE(TAG,"Unable initialize partition iterator!");
  193. }
  194. else {
  195. ota_partition = (esp_partition_t *) esp_partition_get(it);
  196. if(ota_partition != NULL){
  197. ESP_LOGI(TAG, "Found OTA partition.");
  198. }
  199. else {
  200. ESP_LOGE(TAG,"OTA partition not found! Unable update application.");
  201. }
  202. esp_partition_iterator_release(it);
  203. }
  204. it = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA , NULL);
  205. if(it == NULL){
  206. ESP_LOGE(TAG,"Unable initialize partition iterator!");
  207. }
  208. else {
  209. ota_data_partition = (esp_partition_t *) esp_partition_get(it);
  210. if(ota_data_partition != NULL){
  211. ESP_LOGI(TAG, "Found OTA data partition.");
  212. }
  213. else {
  214. ESP_LOGE(TAG,"OTA data partition not found! Unable update application.");
  215. }
  216. esp_partition_iterator_release(it);
  217. }
  218. if(ota_data_partition==NULL || ota_partition==NULL){
  219. return ESP_FAIL;
  220. }
  221. ESP_LOGI(TAG,"Erasing flash ");
  222. num_passes=ota_partition->size/OTA_FLASH_ERASE_BLOCK;
  223. remain_size=ota_partition->size-(num_passes*OTA_FLASH_ERASE_BLOCK);
  224. for(uint16_t i=0;i<num_passes;i++){
  225. err=esp_partition_erase_range(ota_partition, 0, ota_partition->size);
  226. ESP_LOGD(TAG,"Erasing flash (%u%%)",i/num_passes);
  227. triggerStatusJsonRefresh(i%5==0?true:false,"Erasing flash (%u/%u)",i,num_passes);
  228. taskYIELD();
  229. if(err!=ESP_OK) return err;
  230. }
  231. if(remain_size>0){
  232. err=esp_partition_erase_range(ota_partition, ota_partition->size-remain_size, remain_size);
  233. if(err!=ESP_OK) return err;
  234. }
  235. triggerStatusJsonRefresh(false,"Erasing flash (100%%)");
  236. taskYIELD();
  237. return ESP_OK;
  238. }
  239. void ota_task(void *pvParameter)
  240. {
  241. char * passedURL=(char *)pvParameter;
  242. ota_status.bInitialized = true;
  243. ESP_LOGD(TAG, "HTTP ota Thread started");
  244. triggerStatusJsonRefresh(true,"Initializing...");
  245. ota_status.bRedirectFound=false;
  246. if(passedURL==NULL || strlen(passedURL)==0){
  247. ESP_LOGE(TAG,"HTTP OTA called without a url");
  248. triggerStatusJsonRefresh(true,"Updating needs a URL!");
  249. ota_status.bOTAThreadStarted=false;
  250. vTaskDelete(NULL);
  251. return ;
  252. }
  253. ota_status.current_url= strdup(passedURL);
  254. FREE_RESET(pvParameter);
  255. ESP_LOGW(TAG,"**************** Expecting WATCHDOG errors below during flash erase. This is OK and not to worry about **************** ");
  256. triggerStatusJsonRefresh(true,"Erasing OTA partition");
  257. esp_err_t err=_erase_last_boot_app_partition();
  258. if(err!=ESP_OK){
  259. ESP_LOGE(TAG,"Unable to erase last APP partition. Error: %s",esp_err_to_name(err));
  260. FREE_RESET(ota_status.current_url);
  261. FREE_RESET(ota_status.redirected_url);
  262. vTaskDelete(NULL);
  263. }
  264. ESP_LOGI(TAG,"Calling esp_https_ota");
  265. init_config(&ota_config,ota_status.bRedirectFound?ota_status.redirected_url:ota_status.current_url);
  266. ota_status.bOTAStarted = true;
  267. triggerStatusJsonRefresh(true,"Starting OTA...");
  268. err = esp_https_ota(&ota_config);
  269. if (err == ESP_OK) {
  270. triggerStatusJsonRefresh(true,"Success!");
  271. esp_restart();
  272. } else {
  273. triggerStatusJsonRefresh(true,"Error: %s",esp_err_to_name(err));
  274. wifi_manager_refresh_ota_json();
  275. ESP_LOGE(TAG, "Firmware upgrade failed with error : %s", esp_err_to_name(err));
  276. ota_status.bOTAThreadStarted=false;
  277. }
  278. FREE_RESET(ota_status.current_url);
  279. FREE_RESET(ota_status.redirected_url);
  280. vTaskDelete(NULL);
  281. }
  282. esp_err_t process_recovery_ota(const char * bin_url){
  283. // Initialize NVS.
  284. int ret=0;
  285. esp_err_t err = nvs_flash_init();
  286. if(ota_status.bOTAThreadStarted){
  287. ESP_LOGE(TAG,"OTA Already started. ");
  288. return ESP_FAIL;
  289. }
  290. memset(&ota_status, 0x00, sizeof(ota_status));
  291. ota_status.bOTAThreadStarted=true;
  292. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  293. // todo: If we ever change the size of the nvs partition, we need to figure out a mechanism to enlarge the nvs.
  294. // 1.OTA app partition table has a smaller NVS partition size than the non-OTA
  295. // partition table. This size mismatch may cause NVS initialization to fail.
  296. // 2.NVS partition contains data in new format and cannot be recognized by this version of code.
  297. // If this happens, we erase NVS partition and initialize NVS again.
  298. ESP_LOGW(TAG,"NVS flash size has changed. Formatting nvs");
  299. ESP_ERROR_CHECK(nvs_flash_erase());
  300. err = nvs_flash_init();
  301. }
  302. ESP_ERROR_CHECK(err);
  303. char * urlPtr=strdup(bin_url);
  304. // the first thing we need to do here is to erase the firmware url
  305. // to avoid a boot loop
  306. #ifdef CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1
  307. #define OTA_CORE 0
  308. #warning "OTA will run on core 0"
  309. #else
  310. #warning "OTA will run on core 1"
  311. #define OTA_CORE 1
  312. #endif
  313. ESP_LOGI(TAG, "Starting ota on core %u for : %s", OTA_CORE,urlPtr);
  314. ret=xTaskCreatePinnedToCore(&ota_task, "ota_task", 1024*20, (void *)urlPtr, ESP_TASK_MAIN_PRIO+1, NULL, OTA_CORE);
  315. if (ret != pdPASS) {
  316. ESP_LOGI(TAG, "create thread %s failed", "ota_task");
  317. return ESP_FAIL;
  318. }
  319. return ESP_OK;
  320. }
  321. esp_err_t start_ota(const char * bin_url, bool bFromAppMain)
  322. {
  323. // uint8_t * get_nvs_value_alloc_default(NVS_TYPE_BLOB, "certs", server_cert_pem_start , server_cert_pem_end-server_cert_pem_start);
  324. #if RECOVERY_APPLICATION
  325. return process_recovery_ota(bin_url);
  326. #else
  327. ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
  328. store_nvs_value(NVS_TYPE_STR, "fwurl", bin_url);
  329. ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
  330. return guided_factory();
  331. return ESP_OK;
  332. #endif
  333. }