esp_https_ota.c 14 KB

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