esp_https_ota.c 14 KB

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