esp_https_ota.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include <errno.h>
  20. #include <sys/param.h>
  21. #define IMAGE_HEADER_SIZE sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t) + 1
  22. #define DEFAULT_OTA_BUF_SIZE IMAGE_HEADER_SIZE
  23. static const char *TAG = "esp_https_ota";
  24. typedef enum {
  25. ESP_HTTPS_OTA_INIT,
  26. ESP_HTTPS_OTA_BEGIN,
  27. ESP_HTTPS_OTA_IN_PROGRESS,
  28. ESP_HTTPS_OTA_SUCCESS,
  29. } esp_https_ota_state;
  30. struct esp_https_ota_handle {
  31. esp_ota_handle_t update_handle;
  32. const esp_partition_t *update_partition;
  33. esp_http_client_handle_t http_client;
  34. char *ota_upgrade_buf;
  35. size_t ota_upgrade_buf_size;
  36. int binary_file_len;
  37. esp_https_ota_state state;
  38. bool bulk_flash_erase;
  39. };
  40. typedef struct esp_https_ota_handle esp_https_ota_t;
  41. static bool process_again(int status_code)
  42. {
  43. switch (status_code) {
  44. case HttpStatus_MovedPermanently:
  45. case HttpStatus_Found:
  46. case HttpStatus_TemporaryRedirect:
  47. case HttpStatus_Unauthorized:
  48. return true;
  49. default:
  50. return false;
  51. }
  52. return false;
  53. }
  54. static esp_err_t _http_handle_response_code(esp_http_client_handle_t http_client, int status_code)
  55. {
  56. esp_err_t err;
  57. if (status_code == HttpStatus_MovedPermanently || status_code == HttpStatus_Found || status_code == HttpStatus_TemporaryRedirect) {
  58. err = esp_http_client_set_redirection(http_client);
  59. if (err != ESP_OK) {
  60. ESP_LOGE(TAG, "URL redirection Failed");
  61. return err;
  62. }
  63. } else if (status_code == HttpStatus_Unauthorized) {
  64. esp_http_client_add_auth(http_client);
  65. } else if(status_code == HttpStatus_NotFound || status_code == HttpStatus_Forbidden) {
  66. ESP_LOGE(TAG, "File not found(%d)", status_code);
  67. return ESP_FAIL;
  68. } else if (status_code >= HttpStatus_BadRequest && status_code < HttpStatus_InternalError) {
  69. ESP_LOGE(TAG, "Client error (%d)", status_code);
  70. return ESP_FAIL;
  71. } else if (status_code >= HttpStatus_InternalError) {
  72. ESP_LOGE(TAG, "Server error (%d)", status_code);
  73. return ESP_FAIL;
  74. }
  75. char upgrade_data_buf[DEFAULT_OTA_BUF_SIZE];
  76. // process_again() returns true only in case of redirection.
  77. if (process_again(status_code)) {
  78. while (1) {
  79. /*
  80. * In case of redirection, esp_http_client_read() is called
  81. * to clear the response buffer of http_client.
  82. */
  83. int data_read = esp_http_client_read(http_client, upgrade_data_buf, DEFAULT_OTA_BUF_SIZE);
  84. if (data_read <= 0) {
  85. return ESP_OK;
  86. }
  87. }
  88. }
  89. return ESP_OK;
  90. }
  91. static esp_err_t _http_connect(esp_http_client_handle_t http_client)
  92. {
  93. esp_err_t err = ESP_FAIL;
  94. int status_code, header_ret;
  95. do {
  96. err = esp_http_client_open(http_client, 0);
  97. if (err != ESP_OK) {
  98. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  99. return err;
  100. }
  101. header_ret = esp_http_client_fetch_headers(http_client);
  102. if (header_ret < 0) {
  103. return header_ret;
  104. }
  105. status_code = esp_http_client_get_status_code(http_client);
  106. err = _http_handle_response_code(http_client, status_code);
  107. if (err != ESP_OK) {
  108. return err;
  109. }
  110. } while (process_again(status_code));
  111. return err;
  112. }
  113. static void _http_cleanup(esp_http_client_handle_t client)
  114. {
  115. esp_http_client_close(client);
  116. esp_http_client_cleanup(client);
  117. }
  118. static esp_err_t _ota_write(esp_https_ota_t *https_ota_handle, const void *buffer, size_t buf_len)
  119. {
  120. if (buffer == NULL || https_ota_handle == NULL) {
  121. return ESP_FAIL;
  122. }
  123. esp_err_t err = esp_ota_write(https_ota_handle->update_handle, buffer, buf_len);
  124. if (err != ESP_OK) {
  125. ESP_LOGE(TAG, "Error: esp_ota_write failed! err=0x%x", err);
  126. } else {
  127. https_ota_handle->binary_file_len += buf_len;
  128. ESP_LOGD(TAG, "Written image length %d", https_ota_handle->binary_file_len);
  129. err = ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  130. }
  131. return err;
  132. }
  133. esp_err_t esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle)
  134. {
  135. esp_err_t err;
  136. if (handle == NULL || ota_config == NULL || ota_config->http_config == NULL) {
  137. ESP_LOGE(TAG, "esp_https_ota_begin: Invalid argument");
  138. if (handle) {
  139. *handle = NULL;
  140. }
  141. return ESP_ERR_INVALID_ARG;
  142. }
  143. #if !CONFIG_OTA_ALLOW_HTTP
  144. if (!ota_config->http_config->cert_pem) {
  145. ESP_LOGE(TAG, "Server certificate not found in esp_http_client config");
  146. *handle = NULL;
  147. return ESP_ERR_INVALID_ARG;
  148. }
  149. #endif
  150. esp_https_ota_t *https_ota_handle = calloc(1, sizeof(esp_https_ota_t));
  151. if (!https_ota_handle) {
  152. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  153. *handle = NULL;
  154. return ESP_ERR_NO_MEM;
  155. }
  156. /* Initiate HTTP Connection */
  157. https_ota_handle->http_client = esp_http_client_init(ota_config->http_config);
  158. if (https_ota_handle->http_client == NULL) {
  159. ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  160. err = ESP_FAIL;
  161. goto failure;
  162. }
  163. if (ota_config->http_client_init_cb) {
  164. err = ota_config->http_client_init_cb(https_ota_handle->http_client);
  165. if (err != ESP_OK) {
  166. ESP_LOGE(TAG, "http_client_init_cb returned 0x%x", err);
  167. goto failure;
  168. }
  169. }
  170. err = _http_connect(https_ota_handle->http_client);
  171. if (err != ESP_OK) {
  172. ESP_LOGE(TAG, "Failed to establish HTTP connection");
  173. goto http_cleanup;
  174. }
  175. https_ota_handle->update_partition = NULL;
  176. ESP_LOGI(TAG, "Starting OTA...");
  177. https_ota_handle->update_partition = esp_ota_get_next_update_partition(NULL);
  178. if (https_ota_handle->update_partition == NULL) {
  179. ESP_LOGE(TAG, "Passive OTA partition not found");
  180. err = ESP_FAIL;
  181. goto http_cleanup;
  182. }
  183. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
  184. https_ota_handle->update_partition->subtype, https_ota_handle->update_partition->address);
  185. const int alloc_size = MAX(ota_config->http_config->buffer_size, DEFAULT_OTA_BUF_SIZE);
  186. https_ota_handle->ota_upgrade_buf = (char *)malloc(alloc_size);
  187. if (!https_ota_handle->ota_upgrade_buf) {
  188. ESP_LOGE(TAG, "Couldn't allocate memory to upgrade data buffer");
  189. err = ESP_ERR_NO_MEM;
  190. goto http_cleanup;
  191. }
  192. https_ota_handle->ota_upgrade_buf_size = alloc_size;
  193. https_ota_handle->bulk_flash_erase = ota_config->bulk_flash_erase;
  194. https_ota_handle->binary_file_len = 0;
  195. *handle = (esp_https_ota_handle_t)https_ota_handle;
  196. https_ota_handle->state = ESP_HTTPS_OTA_BEGIN;
  197. return ESP_OK;
  198. http_cleanup:
  199. _http_cleanup(https_ota_handle->http_client);
  200. failure:
  201. free(https_ota_handle);
  202. *handle = NULL;
  203. return err;
  204. }
  205. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info)
  206. {
  207. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  208. if (handle == NULL || new_app_info == NULL) {
  209. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid argument");
  210. return ESP_ERR_INVALID_ARG;
  211. }
  212. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  213. ESP_LOGE(TAG, "esp_https_ota_read_img_desc: Invalid state");
  214. return ESP_FAIL;
  215. }
  216. /*
  217. * `data_read_size` holds number of bytes needed to read complete header.
  218. * `bytes_read` holds number of bytes read.
  219. */
  220. int data_read_size = IMAGE_HEADER_SIZE;
  221. int data_read = 0, bytes_read = 0;
  222. /*
  223. * while loop is added to download complete image headers, even if the headers
  224. * are not sent in a single packet.
  225. */
  226. while (data_read_size > 0 && !esp_https_ota_is_complete_data_received(https_ota_handle)) {
  227. data_read = esp_http_client_read(handle->http_client,
  228. (handle->ota_upgrade_buf + bytes_read),
  229. data_read_size);
  230. /*
  231. * As esp_http_client_read doesn't return negative error code if select fails, we rely on
  232. * `errno` to check for underlying transport connectivity closure if any
  233. */
  234. if (errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED || data_read < 0) {
  235. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  236. break;
  237. }
  238. data_read_size -= data_read;
  239. bytes_read += data_read;
  240. }
  241. if (data_read_size > 0) {
  242. ESP_LOGE(TAG, "Complete headers were not received");
  243. return ESP_FAIL;
  244. }
  245. handle->binary_file_len = bytes_read;
  246. memcpy(new_app_info, &handle->ota_upgrade_buf[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
  247. return ESP_OK;
  248. }
  249. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle)
  250. {
  251. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  252. if (handle == NULL) {
  253. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid argument");
  254. return ESP_ERR_INVALID_ARG;
  255. }
  256. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  257. ESP_LOGE(TAG, "esp_https_ota_perform: Invalid state");
  258. return ESP_FAIL;
  259. }
  260. esp_err_t err;
  261. int data_read;
  262. const int erase_size = handle->bulk_flash_erase ? OTA_SIZE_UNKNOWN : OTA_WITH_SEQUENTIAL_WRITES;
  263. switch (handle->state) {
  264. case ESP_HTTPS_OTA_BEGIN:
  265. err = esp_ota_begin(handle->update_partition, erase_size, &handle->update_handle);
  266. if (err != ESP_OK) {
  267. ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
  268. return err;
  269. }
  270. handle->state = ESP_HTTPS_OTA_IN_PROGRESS;
  271. /* In case `esp_https_ota_read_img_desc` was invoked first,
  272. then the image data read there should be written to OTA partition
  273. */
  274. if (handle->binary_file_len) {
  275. return _ota_write(handle, (const void *)handle->ota_upgrade_buf, handle->binary_file_len);
  276. }
  277. /* falls through */
  278. case ESP_HTTPS_OTA_IN_PROGRESS:
  279. data_read = esp_http_client_read(handle->http_client,
  280. handle->ota_upgrade_buf,
  281. handle->ota_upgrade_buf_size);
  282. if (data_read == 0) {
  283. /*
  284. * esp_https_ota_is_complete_data_received is added to check whether
  285. * complete image is received.
  286. */
  287. bool is_recv_complete = esp_https_ota_is_complete_data_received(https_ota_handle);
  288. /*
  289. * As esp_http_client_read doesn't return negative error code if select fails, we rely on
  290. * `errno` to check for underlying transport connectivity closure if any.
  291. * Incase the complete data has not been received but the server has sent
  292. * an ENOTCONN or ECONNRESET, failure is returned. We close with success
  293. * if complete data has been received.
  294. */
  295. if ((errno == ENOTCONN || errno == ECONNRESET || errno == ECONNABORTED) && !is_recv_complete) {
  296. ESP_LOGE(TAG, "Connection closed, errno = %d", errno);
  297. return ESP_FAIL;
  298. } else if (!is_recv_complete) {
  299. return ESP_ERR_HTTPS_OTA_IN_PROGRESS;
  300. }
  301. ESP_LOGI(TAG, "Connection closed");
  302. } else if (data_read > 0) {
  303. return _ota_write(handle, (const void *)handle->ota_upgrade_buf, data_read);
  304. } else {
  305. return ESP_FAIL;
  306. }
  307. handle->state = ESP_HTTPS_OTA_SUCCESS;
  308. break;
  309. default:
  310. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  311. return ESP_FAIL;
  312. break;
  313. }
  314. return ESP_OK;
  315. }
  316. bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle)
  317. {
  318. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  319. return esp_http_client_is_complete_data_received(handle->http_client);
  320. }
  321. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle)
  322. {
  323. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  324. if (handle == NULL) {
  325. return ESP_ERR_INVALID_ARG;
  326. }
  327. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  328. return ESP_FAIL;
  329. }
  330. esp_err_t err = ESP_OK;
  331. switch (handle->state) {
  332. case ESP_HTTPS_OTA_SUCCESS:
  333. case ESP_HTTPS_OTA_IN_PROGRESS:
  334. err = esp_ota_end(handle->update_handle);
  335. /* falls through */
  336. case ESP_HTTPS_OTA_BEGIN:
  337. if (handle->ota_upgrade_buf) {
  338. free(handle->ota_upgrade_buf);
  339. }
  340. if (handle->http_client) {
  341. _http_cleanup(handle->http_client);
  342. }
  343. break;
  344. default:
  345. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  346. break;
  347. }
  348. if ((err == ESP_OK) && (handle->state == ESP_HTTPS_OTA_SUCCESS)) {
  349. esp_err_t err = esp_ota_set_boot_partition(handle->update_partition);
  350. if (err != ESP_OK) {
  351. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed! err=0x%x", err);
  352. }
  353. }
  354. free(handle);
  355. return err;
  356. }
  357. esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle)
  358. {
  359. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  360. if (handle == NULL) {
  361. return ESP_ERR_INVALID_ARG;
  362. }
  363. if (handle->state < ESP_HTTPS_OTA_BEGIN) {
  364. return ESP_FAIL;
  365. }
  366. esp_err_t err = ESP_OK;
  367. switch (handle->state) {
  368. case ESP_HTTPS_OTA_SUCCESS:
  369. case ESP_HTTPS_OTA_IN_PROGRESS:
  370. err = esp_ota_abort(handle->update_handle);
  371. /* falls through */
  372. case ESP_HTTPS_OTA_BEGIN:
  373. if (handle->ota_upgrade_buf) {
  374. free(handle->ota_upgrade_buf);
  375. }
  376. if (handle->http_client) {
  377. _http_cleanup(handle->http_client);
  378. }
  379. break;
  380. default:
  381. err = ESP_ERR_INVALID_STATE;
  382. ESP_LOGE(TAG, "Invalid ESP HTTPS OTA State");
  383. break;
  384. }
  385. free(handle);
  386. return err;
  387. }
  388. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle)
  389. {
  390. esp_https_ota_t *handle = (esp_https_ota_t *)https_ota_handle;
  391. if (handle == NULL) {
  392. return -1;
  393. }
  394. if (handle->state < ESP_HTTPS_OTA_IN_PROGRESS) {
  395. return -1;
  396. }
  397. return handle->binary_file_len;
  398. }
  399. esp_err_t esp_https_ota(const esp_http_client_config_t *config)
  400. {
  401. if (!config) {
  402. ESP_LOGE(TAG, "esp_http_client config not found");
  403. return ESP_ERR_INVALID_ARG;
  404. }
  405. esp_https_ota_config_t ota_config = {
  406. .http_config = config,
  407. };
  408. esp_https_ota_handle_t https_ota_handle = NULL;
  409. esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle);
  410. if (https_ota_handle == NULL) {
  411. return ESP_FAIL;
  412. }
  413. while (1) {
  414. err = esp_https_ota_perform(https_ota_handle);
  415. if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
  416. break;
  417. }
  418. }
  419. if (err != ESP_OK) {
  420. esp_https_ota_abort(https_ota_handle);
  421. return err;
  422. }
  423. esp_err_t ota_finish_err = esp_https_ota_finish(https_ota_handle);
  424. if (ota_finish_err != ESP_OK) {
  425. return ota_finish_err;
  426. }
  427. return ESP_OK;
  428. }