esp_https_ota.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <esp_https_ota.h>
  10. #include <esp_log.h>
  11. #include <esp_ota_ops.h>
  12. #include <errno.h>
  13. #include <sys/param.h>
  14. #define IMAGE_HEADER_SIZE (1024)
  15. /* This is kept sufficiently large enough to cover image format headers
  16. * and also this defines default minimum OTA buffer chunk size */
  17. #define DEFAULT_OTA_BUF_SIZE (IMAGE_HEADER_SIZE)
  18. _Static_assert(DEFAULT_OTA_BUF_SIZE > (sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1), "OTA data buffer too small");
  19. #define DEFAULT_REQUEST_SIZE (64 * 1024)
  20. static const char *TAG = "esp_https_ota";
  21. typedef enum {
  22. ESP_HTTPS_OTA_INIT,
  23. ESP_HTTPS_OTA_BEGIN,
  24. ESP_HTTPS_OTA_IN_PROGRESS,
  25. ESP_HTTPS_OTA_SUCCESS,
  26. } esp_https_ota_state;
  27. struct esp_https_ota_handle {
  28. esp_ota_handle_t update_handle;
  29. const esp_partition_t *update_partition;
  30. esp_http_client_handle_t http_client;
  31. char *ota_upgrade_buf;
  32. size_t ota_upgrade_buf_size;
  33. int binary_file_len;
  34. int image_length;
  35. int max_http_request_size;
  36. esp_https_ota_state state;
  37. bool bulk_flash_erase;
  38. bool partial_http_download;
  39. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  40. decrypt_cb_t decrypt_cb;
  41. #endif
  42. };
  43. typedef struct esp_https_ota_handle esp_https_ota_t;
  44. static bool process_again(int status_code)
  45. {
  46. switch (status_code) {
  47. case HttpStatus_MovedPermanently:
  48. case HttpStatus_Found:
  49. case HttpStatus_TemporaryRedirect:
  50. case HttpStatus_Unauthorized:
  51. return true;
  52. default:
  53. return false;
  54. }
  55. return false;
  56. }
  57. static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code)
  58. {
  59. esp_err_t err;
  60. if (status_code == HttpStatus_MovedPermanently || status_code == HttpStatus_Found || status_code == HttpStatus_TemporaryRedirect) {
  61. err = esp_http_client_set_redirection(http_client);
  62. if (err != ESP_OK) {
  63. ESP_LOGE(TAG, "URL redirection Failed");
  64. return err;
  65. }
  66. } else if (status_code == HttpStatus_Unauthorized) {
  67. esp_http_client_add_auth(http_client);
  68. } else if(status_code == HttpStatus_NotFound || status_code == HttpStatus_Forbidden) {
  69. ESP_LOGE(TAG, "File not found(%d)", status_code);
  70. return ESP_FAIL;
  71. } else if (status_code >= HttpStatus_BadRequest && status_code < HttpStatus_InternalError) {
  72. ESP_LOGE(TAG, "Client error (%d)", status_code);
  73. return ESP_FAIL;
  74. } else if (status_code >= HttpStatus_InternalError) {
  75. ESP_LOGE(TAG, "Server error (%d)", status_code);
  76. return ESP_FAIL;
  77. }
  78. char upgrade_data_buf[256];
  79. // process_again() returns true only in case of redirection.
  80. if (process_again(status_code)) {
  81. while (1) {
  82. /*
  83. * In case of redirection, esp_http_client_read() is called
  84. * to clear the response buffer of http_client.
  85. */
  86. int data_read = esp_http_client_read(http_client, upgrade_data_buf, sizeof(upgrade_data_buf));
  87. if (data_read <= 0) {
  88. return ESP_OK;
  89. }
  90. }
  91. }
  92. return ESP_OK;
  93. }
  94. static esp_err_t _http_connect(esp_http_client_handle_t http_client)
  95. {
  96. esp_err_t err = ESP_FAIL;
  97. int status_code, header_ret;
  98. do {
  99. char *post_data = NULL;
  100. /* Send POST request if body is set.
  101. * Note: Sending POST request is not supported if partial_http_download
  102. * is enabled
  103. */
  104. int post_len = esp_http_client_get_post_field(http_client, &post_data);
  105. err = esp_http_client_open(http_client, post_len);
  106. if (err != ESP_OK) {
  107. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  108. return err;
  109. }
  110. if (post_len) {
  111. int write_len = 0;
  112. while (post_len > 0) {
  113. write_len = esp_http_client_write(http_client, post_data, post_len);
  114. if (write_len < 0) {
  115. ESP_LOGE(TAG, "Write failed");
  116. return ESP_FAIL;
  117. }
  118. post_len -= write_len;
  119. post_data += write_len;
  120. }
  121. }
  122. header_ret = esp_http_client_fetch_headers(http_client);
  123. if (header_ret < 0) {
  124. return header_ret;
  125. }
  126. status_code = esp_http_client_get_status_code(http_client);
  127. err = _http_handle_response_code(http_client, status_code);
  128. if (err != ESP_OK) {
  129. return err;
  130. }
  131. } while (process_again(status_code));
  132. return err;
  133. }
  134. static void _http_cleanup(esp_http_client_handle_t client)
  135. {
  136. esp_http_client_close(client);
  137. esp_http_client_cleanup(client);
  138. }
  139. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  140. static esp_err_t esp_https_ota_decrypt_cb(esp_https_ota_t *handle, decrypt_cb_arg_t *args)
  141. {
  142. esp_err_t ret = handle->decrypt_cb(args);
  143. if (ret != ESP_OK) {
  144. ESP_LOGE(TAG, "Decrypt callback failed %d", ret);
  145. return ret;
  146. }
  147. if (args->data_out_len > 0) {
  148. return ESP_OK;
  149. } else {
  150. return ESP_HTTPS_OTA_IN_PROGRESS;
  151. }
  152. }
  153. static void esp_https_ota_decrypt_cb_free_buf(void *buffer)
  154. {
  155. free(buffer);
  156. }
  157. #endif // CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  158. static esp_err_t _ota_write(esp_https_ota_t *https_ota_handle, const void *buffer, size_t buf_len)
  159. {
  160. if (buffer == NULL || https_ota_handle == NULL) {
  161. return ESP_FAIL;
  162. }
  163. esp_err_t err = esp_ota_write(https_ota_handle->update_handle, buffer, buf_len);
  164. if (err != ESP_OK) {
  165. ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%x", err);
  166. } else {
  167. https_ota_handle->binary_file_len += buf_len;
  168. ESP_LOGD(TAG, "Written image length %d", https_ota_handle->binary_file_len);
  169. err = ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  170. }
  171. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  172. esp_https_ota_decrypt_cb_free_buf((void *) buffer);
  173. #endif
  174. return err;
  175. }
  176. static bool is_server_verification_enabled(esp_https_ota_config_t *ota_config) {
  177. return (ota_config->http_config->cert_pem
  178. || ota_config->http_config->use_global_ca_store
  179. || ota_config->http_config->crt_bundle_attach != NULL);
  180. }
  181. esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle)
  182. {
  183. esp_err_t err;
  184. if (handle == NULL || ota_config == NULL || ota_config->http_config == NULL) {
  185. ESP_LOGE(TAG, "esp_https_ota_begin: Invalid argument");
  186. if (handle) {
  187. *handle = NULL;
  188. }
  189. return ESP_ERR_INVALID_ARG;
  190. }
  191. if (!is_server_verification_enabled(ota_config)) {
  192. #if CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP
  193. ESP_LOGW(TAG, "Continuing with insecure option because CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP is set.");
  194. #else
  195. ESP_LOGE(TAG, "No option for server verification is enabled in esp_http_client config.");
  196. *handle = NULL;
  197. return ESP_ERR_INVALID_ARG;
  198. #endif
  199. }
  200. esp_https_ota_t *https_ota_handle = calloc(1, sizeof(esp_https_ota_t));
  201. if (!https_ota_handle) {
  202. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  203. *handle = NULL;
  204. return ESP_ERR_NO_MEM;
  205. }
  206. https_ota_handle->partial_http_download = ota_config->partial_http_download;
  207. https_ota_handle->max_http_request_size = (ota_config->max_http_request_size == 0) ? DEFAULT_REQUEST_SIZE : ota_config->max_http_request_size;
  208. /* Initiate HTTP Connection */
  209. https_ota_handle->http_client = esp_http_client_init(ota_config->http_config);
  210. if (https_ota_handle->http_client == NULL) {
  211. ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  212. err = ESP_FAIL;
  213. goto failure;
  214. }
  215. if (https_ota_handle->partial_http_download) {
  216. esp_http_client_set_method(https_ota_handle->http_client, HTTP_METHOD_HEAD);
  217. err = esp_http_client_perform(https_ota_handle->http_client);
  218. if (err == ESP_OK) {
  219. int status = esp_http_client_get_status_code(https_ota_handle->http_client);
  220. if (status != HttpStatus_Ok) {
  221. ESP_LOGE(TAG, "Received incorrect http status %d", status);
  222. err = ESP_FAIL;
  223. goto http_cleanup;
  224. }
  225. } else {
  226. ESP_LOGE(TAG, "ESP HTTP client perform failed: %d", err);
  227. goto http_cleanup;
  228. }
  229. https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client);
  230. esp_http_client_close(https_ota_handle->http_client);
  231. if (https_ota_handle->image_length > https_ota_handle->max_http_request_size) {
  232. char *header_val = NULL;
  233. asprintf(&header_val, "bytes=0-%d", https_ota_handle->max_http_request_size - 1);
  234. if (header_val == NULL) {
  235. ESP_LOGE(TAG, "Failed to allocate memory for HTTP header");
  236. err = ESP_ERR_NO_MEM;
  237. goto http_cleanup;
  238. }
  239. esp_http_client_set_header(https_ota_handle->http_client, "Range", header_val);
  240. free(header_val);
  241. }
  242. esp_http_client_set_method(https_ota_handle->http_client, HTTP_METHOD_GET);
  243. }
  244. if (ota_config->http_client_init_cb) {
  245. err = ota_config->http_client_init_cb(https_ota_handle->http_client);
  246. if (err != ESP_OK) {
  247. ESP_LOGE(TAG, "http_client_init_cb returned 0x%x", err);
  248. goto http_cleanup;
  249. }
  250. }
  251. err = _http_connect(https_ota_handle->http_client);
  252. if (err != ESP_OK) {
  253. ESP_LOGE(TAG, "Failed to establish HTTP connection");
  254. goto http_cleanup;
  255. }
  256. if (!https_ota_handle->partial_http_download) {
  257. https_ota_handle->image_length = esp_http_client_get_content_length(https_ota_handle->http_client);
  258. }
  259. https_ota_handle->update_partition = NULL;
  260. ESP_LOGI(TAG, "Starting OTA...");
  261. https_ota_handle->update_partition = esp_ota_get_next_update_partition(NULL);
  262. if (https_ota_handle->update_partition == NULL) {
  263. ESP_LOGE(TAG, "Passive OTA partition not found");
  264. err = ESP_FAIL;
  265. goto http_cleanup;
  266. }
  267. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
  268. https_ota_handle->update_partition->subtype, https_ota_handle->update_partition->address);
  269. const int alloc_size = MAX(ota_config->http_config->buffer_size, DEFAULT_OTA_BUF_SIZE);
  270. https_ota_handle->ota_upgrade_buf = (char *)malloc(alloc_size);
  271. if (!https_ota_handle->ota_upgrade_buf) {
  272. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  273. err = ESP_ERR_NO_MEM;
  274. goto http_cleanup;
  275. }
  276. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  277. if (ota_config->decrypt_cb == NULL) {
  278. err = ESP_ERR_INVALID_ARG;
  279. goto http_cleanup;
  280. }
  281. https_ota_handle->decrypt_cb = ota_config->decrypt_cb;
  282. #endif
  283. https_ota_handle->ota_upgrade_buf_size = alloc_size;
  284. https_ota_handle->bulk_flash_erase = ota_config->bulk_flash_erase;
  285. https_ota_handle->binary_file_len = 0;
  286. *handle = (esp_https_ota_handle_t)https_ota_handle;
  287. https_ota_handle->state = ESP_HTTPS_OTA_BEGIN;
  288. return ESP_OK;
  289. http_cleanup:
  290. _http_cleanup(https_ota_handle->http_client);
  291. failure:
  292. free(https_ota_handle);
  293. *handle = NULL;
  294. return err;
  295. }
  296. static esp_err_t read_header(esp_https_ota_t *handle)
  297. {
  298. /*
  299. * `data_read_size` holds number of bytes needed to read complete header.
  300. * `bytes_read` holds number of bytes read.
  301. */
  302. int data_read_size = IMAGE_HEADER_SIZE;
  303. int data_read = 0, bytes_read = 0;
  304. /*
  305. * while loop is added to download complete image headers, even if the headers
  306. * are not sent in a single packet.
  307. */
  308. while (data_read_size > 0 && !esp_http_client_is_complete_data_received(handle->http_client)) {
  309. data_read = esp_http_client_read(handle->http_client,
  310. (handle->ota_upgrade_buf + bytes_read),
  311. data_read_size);
  312. /*
  313. * As esp_http_client_read doesn't return negative error code if select fails, we rely on
  314. * `errno` to check for underlying transport connectivity closure if any
  315. */
  316. if (errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED || data_read < 0) {
  317. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  318. break;
  319. }
  320. data_read_size -= data_read;
  321. bytes_read += data_read;
  322. }
  323. if (data_read_size > 0) {
  324. ESP_LOGE(TAG, "Complete headers were not received");
  325. return ESP_FAIL;
  326. }
  327. handle->binary_file_len = bytes_read;
  328. return ESP_OK;
  329. }
  330. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
  331. {
  332. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  333. if (handle == NULL || new_app_info == NULL) {
  334. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
  335. return ESP_ERR_INVALID_ARG;
  336. }
  337. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  338. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
  339. return ESP_ERR_INVALID_STATE;
  340. }
  341. if (read_header(handle) != ESP_OK) {
  342. return ESP_FAIL;
  343. }
  344. const int app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
  345. esp_app_desc_t *app_info = (esp_app_desc_t *) &handle->ota_upgrade_buf[app_desc_offset];
  346. if (app_info->magic_word != ESP_APP_DESC_MAGIC_WORD) {
  347. ESP_LOGE(TAG, "Incorrect app descriptor magic");
  348. return ESP_FAIL;
  349. }
  350. memcpy(new_app_info, app_info, sizeof(esp_app_desc_t));
  351. return ESP_OK;
  352. }
  353. static esp_err_t esp_ota_verify_chip_id(const void *arg)
  354. {
  355. esp_image_header_t *data = (esp_image_header_t *)(arg);
  356. if (data->chip_id != CONFIG_IDF_FIRMWARE_CHIP_ID) {
  357. ESP_LOGE(TAG, "Mismatch chip id, expected %d, found %d", CONFIG_IDF_FIRMWARE_CHIP_ID, data->chip_id);
  358. return ESP_ERR_INVALID_VERSION;
  359. }
  360. return ESP_OK;
  361. }
  362. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
  363. {
  364. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  365. if (handle == NULL) {
  366. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid argument");
  367. return ESP_ERR_INVALID_ARG;
  368. }
  369. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  370. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid state");
  371. return ESP_FAIL;
  372. }
  373. esp_err_t err;
  374. int data_read;
  375. const int erase_size = handle->bulk_flash_erase ? OTA_SIZE_UNKNOWN : OTA_WITH_SEQUENTIAL_WRITES;
  376. switch (handle->state) {
  377. case ESP_HTTPS_OTA_BEGIN:
  378. err = esp_ota_begin(handle->update_partition, erase_size, &handle->update_handle);
  379. if (err != ESP_OK) {
  380. ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
  381. return err;
  382. }
  383. handle->state = ESP_HTTPS_OTA_IN_PROGRESS;
  384. /* In case `esp_https_ota_read_img_desc` was invoked first,
  385. then the image data read there should be written to OTA partition
  386. */
  387. int binary_file_len = 0;
  388. if (handle->binary_file_len) {
  389. /*
  390. * Header length gets added to handle->binary_file_len in _ota_write
  391. * Clear handle->binary_file_len to avoid additional 289 bytes in binary_file_len
  392. */
  393. binary_file_len = handle->binary_file_len;
  394. handle->binary_file_len = 0;
  395. } else {
  396. if (read_header(handle) != ESP_OK) {
  397. return ESP_FAIL;
  398. }
  399. binary_file_len = IMAGE_HEADER_SIZE;
  400. }
  401. const void *data_buf = (const void *) handle->ota_upgrade_buf;
  402. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  403. decrypt_cb_arg_t args = {};
  404. args.data_in = handle->ota_upgrade_buf;
  405. args.data_in_len = binary_file_len;
  406. err = esp_https_ota_decrypt_cb(handle, &args);
  407. if (err == ESP_OK) {
  408. data_buf = args.data_out;
  409. binary_file_len = args.data_out_len;
  410. } else {
  411. ESP_LOGE(TAG, "Decryption of image header failed");
  412. return ESP_FAIL;
  413. }
  414. #endif // CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  415. err = esp_ota_verify_chip_id(data_buf);
  416. if (err != ESP_OK) {
  417. return err;
  418. }
  419. return _ota_write(handle, data_buf, binary_file_len);
  420. case ESP_HTTPS_OTA_IN_PROGRESS:
  421. data_read = esp_http_client_read(handle->http_client,
  422. handle->ota_upgrade_buf,
  423. handle->ota_upgrade_buf_size);
  424. if (data_read == 0) {
  425. /*
  426. * esp_http_client_is_complete_data_received is added to check whether
  427. * complete image is received.
  428. */
  429. bool is_recv_complete = esp_http_client_is_complete_data_received(handle->http_client);
  430. /*
  431. * As esp_http_client_read doesn't return negative error code if select fails, we rely on
  432. * `errno` to check for underlying transport connectivity closure if any.
  433. * Incase the complete data has not been received but the server has sent
  434. * an ENOTCONN or ECONNRESET, failure is returned. We close with success
  435. * if complete data has been received.
  436. */
  437. if ((errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) && !is_recv_complete) {
  438. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  439. return ESP_FAIL;
  440. } else if (!is_recv_complete) {
  441. return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  442. }
  443. ESP_LOGD(TAG, "Connection closed");
  444. } else if (data_read > 0) {
  445. const void *data_buf = (const void *) handle->ota_upgrade_buf;
  446. int data_len = data_read;
  447. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  448. decrypt_cb_arg_t args = {};
  449. args.data_in = handle->ota_upgrade_buf;
  450. args.data_in_len = data_read;
  451. err = esp_https_ota_decrypt_cb(handle, &args);
  452. if (err == ESP_OK) {
  453. data_buf = args.data_out;
  454. data_len = args.data_out_len;
  455. } else {
  456. return err;
  457. }
  458. #endif // CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  459. return _ota_write(handle, data_buf, data_len);
  460. } else {
  461. ESP_LOGE(TAG, "data read %d, errno %d", data_read, errno);
  462. return ESP_FAIL;
  463. }
  464. if (!handle->partial_http_download || (handle->partial_http_download && handle->image_length == handle->binary_file_len)) {
  465. handle->state = ESP_HTTPS_OTA_SUCCESS;
  466. }
  467. break;
  468. default:
  469. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  470. return ESP_FAIL;
  471. break;
  472. }
  473. if (handle->partial_http_download) {
  474. if (handle->state == ESP_HTTPS_OTA_IN_PROGRESS && handle->image_length > handle->binary_file_len) {
  475. esp_http_client_close(handle->http_client);
  476. char *header_val = NULL;
  477. if ((handle->image_length - handle->binary_file_len) > handle->max_http_request_size) {
  478. asprintf(&header_val, "bytes=%d-%d", handle->binary_file_len, (handle->binary_file_len + handle->max_http_request_size - 1));
  479. } else {
  480. asprintf(&header_val, "bytes=%d-", handle->binary_file_len);
  481. }
  482. if (header_val == NULL) {
  483. ESP_LOGE(TAG, "Failed to allocate memory for HTTP header");
  484. return ESP_ERR_NO_MEM;
  485. }
  486. esp_http_client_set_header(handle->http_client, "Range", header_val);
  487. free(header_val);
  488. err = _http_connect(handle->http_client);
  489. if (err != ESP_OK) {
  490. ESP_LOGE(TAG, "Failed to establish HTTP connection");
  491. return ESP_FAIL;
  492. }
  493. ESP_LOGD(TAG, "Connection start");
  494. return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  495. }
  496. }
  497. return ESP_OK;
  498. }
  499. bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle)
  500. {
  501. bool ret = false;
  502. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  503. if (handle->partial_http_download) {
  504. ret = (handle->image_length == handle->binary_file_len);
  505. } else {
  506. ret = esp_http_client_is_complete_data_received(handle->http_client);
  507. }
  508. return ret;
  509. }
  510. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
  511. {
  512. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  513. if (handle == NULL) {
  514. return ESP_ERR_INVALID_ARG;
  515. }
  516. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  517. return ESP_FAIL;
  518. }
  519. esp_err_t err = ESP_OK;
  520. switch (handle->state) {
  521. case ESP_HTTPS_OTA_SUCCESS:
  522. case ESP_HTTPS_OTA_IN_PROGRESS:
  523. err = esp_ota_end(handle->update_handle);
  524. /* falls through */
  525. case ESP_HTTPS_OTA_BEGIN:
  526. if (handle->ota_upgrade_buf) {
  527. free(handle->ota_upgrade_buf);
  528. }
  529. if (handle->http_client) {
  530. _http_cleanup(handle->http_client);
  531. }
  532. break;
  533. default:
  534. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  535. break;
  536. }
  537. if ((err == ESP_OK) && (handle->state == ESP_HTTPS_OTA_SUCCESS)) {
  538. esp_err_t err = esp_ota_set_boot_partition(handle->update_partition);
  539. if (err != ESP_OK) {
  540. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%x", err);
  541. }
  542. }
  543. free(handle);
  544. return err;
  545. }
  546. esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle)
  547. {
  548. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  549. if (handle == NULL) {
  550. return ESP_ERR_INVALID_ARG;
  551. }
  552. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  553. return ESP_FAIL;
  554. }
  555. esp_err_t err = ESP_OK;
  556. switch (handle->state) {
  557. case ESP_HTTPS_OTA_SUCCESS:
  558. case ESP_HTTPS_OTA_IN_PROGRESS:
  559. err = esp_ota_abort(handle->update_handle);
  560. /* falls through */
  561. case ESP_HTTPS_OTA_BEGIN:
  562. if (handle->ota_upgrade_buf) {
  563. free(handle->ota_upgrade_buf);
  564. }
  565. if (handle->http_client) {
  566. _http_cleanup(handle->http_client);
  567. }
  568. break;
  569. default:
  570. err = ESP_ERR_INVALID_STATE;
  571. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  572. break;
  573. }
  574. free(handle);
  575. return err;
  576. }
  577. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)
  578. {
  579. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  580. if (handle == NULL) {
  581. return -1;
  582. }
  583. if (handle->state < ESP_HTTPS_OTA_IN_PROGRESS) {
  584. return -1;
  585. }
  586. return handle->binary_file_len;
  587. }
  588. int esp_https_ota_get_image_size(esp_https_ota_handle_t https_ota_handle)
  589. {
  590. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  591. if (handle == NULL) {
  592. return -1;
  593. }
  594. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  595. return -1;
  596. }
  597. return handle->image_length;
  598. }
  599. esp_err_t esp_https_ota(const esp_http_client_config_t *config)
  600. {
  601. if (!config) {
  602. ESP_LOGE(TAG, "esp_http_client config not found");
  603. return ESP_ERR_INVALID_ARG;
  604. }
  605. esp_https_ota_config_t ota_config = {
  606. .http_config = config,
  607. };
  608. esp_https_ota_handle_t https_ota_handle = NULL;
  609. esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle);
  610. if (https_ota_handle == NULL) {
  611. return ESP_FAIL;
  612. }
  613. while (1) {
  614. err = esp_https_ota_perform(https_ota_handle);
  615. if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
  616. break;
  617. }
  618. }
  619. if (err != ESP_OK) {
  620. esp_https_ota_abort(https_ota_handle);
  621. return err;
  622. }
  623. esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
  624. if (ota_finish_err != ESP_OK) {
  625. return ota_finish_err;
  626. }
  627. return ESP_OK;
  628. }