esp_https_ota.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <esp_https_ota.h>
  17. #include <esp_log.h>
  18. #include <esp_ota_ops.h>
  19. #define IMAGE_HEADER_SIZE sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1
  20. #define DEFAULT_OTA_BUF_SIZE IMAGE_HEADER_SIZE
  21. static const char *TAG = "esp_https_ota";
  22. typedef enum {
  23. ESP_HTTPS_OTA_INIT,
  24. ESP_HTTPS_OTA_BEGIN,
  25. ESP_HTTPS_OTA_IN_PROGRESS,
  26. ESP_HTTPS_OTA_SUCCESS,
  27. } esp_https_ota_state;
  28. struct esp_https_ota_handle {
  29. esp_ota_handle_t update_handle;
  30. const esp_partition_t *update_partition;
  31. esp_http_client_handle_t http_client;
  32. char *ota_upgrade_buf;
  33. size_t ota_upgrade_buf_size;
  34. int binary_file_len;
  35. esp_https_ota_state state;
  36. };
  37. typedef struct esp_https_ota_handle esp_https_ota_t;
  38. static bool process_again(int status_code)
  39. {
  40. switch (status_code) {
  41. case HttpStatus_MovedPermanently:
  42. case HttpStatus_Found:
  43. case HttpStatus_Unauthorized:
  44. return true;
  45. default:
  46. return false;
  47. }
  48. return false;
  49. }
  50. static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code)
  51. {
  52. esp_err_t err;
  53. if (status_code == HttpStatus_MovedPermanently || status_code == HttpStatus_Found) {
  54. err = esp_http_client_set_redirection(http_client);
  55. if (err != ESP_OK) {
  56. ESP_LOGE(TAG, "URL redirection Failed");
  57. return err;
  58. }
  59. } else if (status_code == HttpStatus_Unauthorized) {
  60. esp_http_client_add_auth(http_client);
  61. }
  62. char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE];
  63. if (process_again(status_code)) {
  64. while (1) {
  65. int data_read = esp_http_client_read(http_client, upgrade_data_buf, DEFAULT_OTA_BUF_SIZE);
  66. if (data_read < 0) {
  67. ESP_LOGE(TAG, "Error: SSL data read error");
  68. return ESP_FAIL;
  69. } else if (data_read == 0) {
  70. return ESP_OK;
  71. }
  72. }
  73. }
  74. return ESP_OK;
  75. }
  76. static esp_err_t _http_connect(esp_http_client_handle_t http_client)
  77. {
  78. esp_err_t err = ESP_FAIL;
  79. int status_code, header_ret;
  80. do {
  81. err = esp_http_client_open(http_client, 0);
  82. if (err != ESP_OK) {
  83. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  84. return err;
  85. }
  86. header_ret = esp_http_client_fetch_headers(http_client);
  87. if (header_ret < 0) {
  88. return header_ret;
  89. }
  90. status_code = esp_http_client_get_status_code(http_client);
  91. err = _http_handle_response_code(http_client, status_code);
  92. if (err != ESP_OK) {
  93. return err;
  94. }
  95. } while (process_again(status_code));
  96. return err;
  97. }
  98. static void _http_cleanup(esp_http_client_handle_t client)
  99. {
  100. esp_http_client_close(client);
  101. esp_http_client_cleanup(client);
  102. }
  103. static esp_err_t _ota_write(esp_https_ota_t *https_ota_handle, const void *buffer, size_t buf_len)
  104. {
  105. if (buffer == NULL || https_ota_handle == NULL) {
  106. return ESP_FAIL;
  107. }
  108. esp_err_t err = esp_ota_write(https_ota_handle->update_handle, buffer, buf_len);
  109. if (err != ESP_OK) {
  110. ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%d", err);
  111. } else {
  112. https_ota_handle->binary_file_len += buf_len;
  113. ESP_LOGD(TAG, "Written image length %d", https_ota_handle->binary_file_len);
  114. err = ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  115. }
  116. return err;
  117. }
  118. esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle)
  119. {
  120. esp_err_t err;
  121. if (handle == NULL || ota_config == NULL || ota_config->http_config == NULL) {
  122. ESP_LOGE(TAG, "esp_https_ota_begin: Invalid argument");
  123. if (handle) {
  124. *handle = NULL;
  125. }
  126. return ESP_ERR_INVALID_ARG;
  127. }
  128. #if !CONFIG_OTA_ALLOW_HTTP
  129. if (!ota_config->http_config->cert_pem) {
  130. ESP_LOGE(TAG, "Server certificate not found in esp_http_client config");
  131. *handle = NULL;
  132. return ESP_ERR_INVALID_ARG;
  133. }
  134. #endif
  135. esp_https_ota_t *https_ota_handle = calloc(1, sizeof(esp_https_ota_t));
  136. if (!https_ota_handle) {
  137. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  138. *handle = NULL;
  139. return ESP_ERR_NO_MEM;
  140. }
  141. /* Initiate HTTP Connection */
  142. https_ota_handle->http_client = esp_http_client_init(ota_config->http_config);
  143. if (https_ota_handle->http_client == NULL) {
  144. ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  145. err = ESP_FAIL;
  146. goto failure;
  147. }
  148. err = _http_connect(https_ota_handle->http_client);
  149. if (err != ESP_OK) {
  150. ESP_LOGE(TAG, "Failed to establish HTTP connection");
  151. goto http_cleanup;
  152. }
  153. https_ota_handle->update_partition = NULL;
  154. ESP_LOGI(TAG, "Starting OTA...");
  155. https_ota_handle->update_partition = esp_ota_get_next_update_partition(NULL);
  156. if (https_ota_handle->update_partition == NULL) {
  157. ESP_LOGE(TAG, "Passive OTA partition not found");
  158. err = ESP_FAIL;
  159. goto http_cleanup;
  160. }
  161. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
  162. https_ota_handle->update_partition->subtype, https_ota_handle->update_partition->address);
  163. const int alloc_size = (ota_config->http_config->buffer_size > DEFAULT_OTA_BUF_SIZE) ?
  164. ota_config->http_config->buffer_size : DEFAULT_OTA_BUF_SIZE;
  165. https_ota_handle->ota_upgrade_buf = (char *)malloc(alloc_size);
  166. if (!https_ota_handle->ota_upgrade_buf) {
  167. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  168. err = ESP_ERR_NO_MEM;
  169. goto http_cleanup;
  170. }
  171. https_ota_handle->ota_upgrade_buf_size = alloc_size;
  172. https_ota_handle->binary_file_len = 0;
  173. *handle = (esp_https_ota_handle_t)https_ota_handle;
  174. https_ota_handle->state = ESP_HTTPS_OTA_BEGIN;
  175. return ESP_OK;
  176. http_cleanup:
  177. _http_cleanup(https_ota_handle->http_client);
  178. failure:
  179. free(https_ota_handle);
  180. *handle = NULL;
  181. return err;
  182. }
  183. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
  184. {
  185. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  186. if (handle == NULL || new_app_info == NULL) {
  187. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
  188. return ESP_ERR_INVALID_ARG;
  189. }
  190. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  191. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
  192. return ESP_FAIL;
  193. }
  194. int data_read_size = IMAGE_HEADER_SIZE;
  195. int data_read = esp_http_client_read(handle->http_client,
  196. handle->ota_upgrade_buf,
  197. data_read_size);
  198. if (data_read < 0) {
  199. return ESP_FAIL;
  200. }
  201. if (data_read >= sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
  202. memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
  203. handle->binary_file_len += data_read;
  204. } else {
  205. return ESP_FAIL;
  206. }
  207. return ESP_OK;
  208. }
  209. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
  210. {
  211. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  212. if (handle == NULL) {
  213. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid argument");
  214. return ESP_ERR_INVALID_ARG;
  215. }
  216. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  217. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid state");
  218. return ESP_FAIL;
  219. }
  220. esp_err_t err;
  221. int data_read;
  222. switch (handle->state) {
  223. case ESP_HTTPS_OTA_BEGIN:
  224. err = esp_ota_begin(handle->update_partition, OTA_SIZE_UNKNOWN, &handle->update_handle);
  225. if (err != ESP_OK) {
  226. ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
  227. return err;
  228. }
  229. handle->state = ESP_HTTPS_OTA_IN_PROGRESS;
  230. /* In case `esp_https_ota_read_img_desc` was invoked first,
  231. then the image data read there should be written to OTA partition
  232. */
  233. if (handle->binary_file_len) {
  234. return _ota_write(handle, (const void *)handle->ota_upgrade_buf, handle->binary_file_len);
  235. }
  236. /* falls through */
  237. case ESP_HTTPS_OTA_IN_PROGRESS:
  238. data_read = esp_http_client_read(handle->http_client,
  239. handle->ota_upgrade_buf,
  240. handle->ota_upgrade_buf_size);
  241. if (data_read == 0) {
  242. ESP_LOGI(TAG, "Connection closed");
  243. } else if (data_read < 0) {
  244. ESP_LOGE(TAG, "Error: SSL data read error");
  245. return ESP_FAIL;
  246. } else if (data_read > 0) {
  247. return _ota_write(handle, (const void *)handle->ota_upgrade_buf, data_read);
  248. }
  249. handle->state = ESP_HTTPS_OTA_SUCCESS;
  250. break;
  251. default:
  252. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  253. return ESP_FAIL;
  254. break;
  255. }
  256. return ESP_OK;
  257. }
  258. bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle)
  259. {
  260. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  261. return esp_http_client_is_complete_data_received(handle->http_client);
  262. }
  263. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
  264. {
  265. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  266. if (handle == NULL) {
  267. return ESP_ERR_INVALID_ARG;
  268. }
  269. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  270. return ESP_FAIL;
  271. }
  272. esp_err_t err = ESP_OK;
  273. switch (handle->state) {
  274. case ESP_HTTPS_OTA_SUCCESS:
  275. case ESP_HTTPS_OTA_IN_PROGRESS:
  276. err = esp_ota_end(handle->update_handle);
  277. /* falls through */
  278. case ESP_HTTPS_OTA_BEGIN:
  279. if (handle->ota_upgrade_buf) {
  280. free(handle->ota_upgrade_buf);
  281. }
  282. if (handle->http_client) {
  283. _http_cleanup(handle->http_client);
  284. }
  285. break;
  286. default:
  287. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  288. break;
  289. }
  290. if ((err == ESP_OK) && (handle->state == ESP_HTTPS_OTA_SUCCESS)) {
  291. esp_err_t err = esp_ota_set_boot_partition(handle->update_partition);
  292. if (err != ESP_OK) {
  293. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%d", err);
  294. }
  295. }
  296. free(handle);
  297. return err;
  298. }
  299. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)
  300. {
  301. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  302. if (handle == NULL) {
  303. return -1;
  304. }
  305. if (handle->state < ESP_HTTPS_OTA_IN_PROGRESS) {
  306. return -1;
  307. }
  308. return handle->binary_file_len;
  309. }
  310. esp_err_t esp_https_ota(const esp_http_client_config_t *config)
  311. {
  312. if (!config) {
  313. ESP_LOGE(TAG, "esp_http_client config not found");
  314. return ESP_ERR_INVALID_ARG;
  315. }
  316. esp_https_ota_config_t ota_config = {
  317. .http_config = config,
  318. };
  319. esp_https_ota_handle_t https_ota_handle = NULL;
  320. esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle);
  321. if (https_ota_handle == NULL) {
  322. return ESP_FAIL;
  323. }
  324. while (1) {
  325. err = esp_https_ota_perform(https_ota_handle);
  326. if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
  327. break;
  328. }
  329. }
  330. esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
  331. if (err != ESP_OK) {
  332. /* If there was an error in esp_https_ota_perform(),
  333. then it is given more precedence than error in esp_https_ota_finish()
  334. */
  335. return err;
  336. } else if (ota_finish_err != ESP_OK) {
  337. return ota_finish_err;
  338. }
  339. return ESP_OK;
  340. }