test_switch_ota.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * Tests for switching between partitions: factory, OTAx, test.
  8. */
  9. #include <esp_types.h>
  10. #include <stdio.h>
  11. #include "string.h"
  12. #include <inttypes.h>
  13. #include "sdkconfig.h"
  14. #include "esp_rom_spiflash.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/semphr.h"
  18. #include "freertos/queue.h"
  19. #include "unity.h"
  20. #include "bootloader_common.h"
  21. #include "../bootloader_flash/include/bootloader_flash_priv.h"
  22. #include "esp_err.h"
  23. #include "esp_log.h"
  24. #include "esp_ota_ops.h"
  25. #include "esp_partition.h"
  26. #include "esp_flash_partitions.h"
  27. #include "esp_image_format.h"
  28. #include "nvs_flash.h"
  29. #include "driver/gpio.h"
  30. #include "esp_sleep.h"
  31. #include "test_utils.h"
  32. #define BOOT_COUNT_NAMESPACE "boot_count"
  33. static const char *TAG = "ota_test";
  34. static void set_boot_count_in_nvs(uint8_t boot_count)
  35. {
  36. nvs_handle_t boot_count_handle;
  37. TEST_ESP_OK(nvs_open(BOOT_COUNT_NAMESPACE, NVS_READWRITE, &boot_count_handle));
  38. TEST_ESP_OK(nvs_set_u8(boot_count_handle, "boot_count", boot_count));
  39. TEST_ESP_OK(nvs_commit(boot_count_handle));
  40. nvs_close(boot_count_handle);
  41. }
  42. static uint8_t get_boot_count_from_nvs(void)
  43. {
  44. nvs_handle_t boot_count_handle;
  45. esp_err_t err = nvs_open(BOOT_COUNT_NAMESPACE, NVS_READONLY, &boot_count_handle);
  46. if (err == ESP_ERR_NVS_NOT_FOUND) {
  47. set_boot_count_in_nvs(0);
  48. }
  49. uint8_t boot_count;
  50. TEST_ESP_OK(nvs_get_u8(boot_count_handle, "boot_count", &boot_count));
  51. nvs_close(boot_count_handle);
  52. return boot_count;
  53. }
  54. /* @brief Copies a current app to next partition using handle.
  55. *
  56. * @param[in] update_handle - Handle of API ota.
  57. * @param[in] cur_app - Current app.
  58. */
  59. static void copy_app_partition(esp_ota_handle_t update_handle, const esp_partition_t *curr_app)
  60. {
  61. const void *partition_bin = NULL;
  62. esp_partition_mmap_handle_t data_map;
  63. ESP_LOGI(TAG, "start the copy process");
  64. TEST_ESP_OK(esp_partition_mmap(curr_app, 0, curr_app->size, ESP_PARTITION_MMAP_DATA, &partition_bin, &data_map));
  65. TEST_ESP_OK(esp_ota_write(update_handle, (const void *)partition_bin, curr_app->size));
  66. esp_partition_munmap(data_map);
  67. ESP_LOGI(TAG, "finish the copy process");
  68. }
  69. /* @brief Copies a current app to next partition using handle.
  70. *
  71. * @param[in] update_handle - Handle of API ota.
  72. * @param[in] cur_app - Current app.
  73. */
  74. static void copy_app_partition_with_offset(esp_ota_handle_t update_handle, const esp_partition_t *curr_app)
  75. {
  76. const void *partition_bin = NULL;
  77. esp_partition_mmap_handle_t data_map;
  78. ESP_LOGI(TAG, "start the copy process");
  79. uint32_t offset = 0, bytes_to_write = curr_app->size;
  80. uint32_t write_bytes;
  81. while (bytes_to_write > 0) {
  82. write_bytes = (bytes_to_write > (4 * 1024)) ? (4 * 1024) : bytes_to_write;
  83. TEST_ESP_OK(esp_partition_mmap(curr_app, offset, write_bytes, ESP_PARTITION_MMAP_DATA, &partition_bin, &data_map));
  84. TEST_ESP_OK(esp_ota_write_with_offset(update_handle, (const void *)partition_bin, write_bytes, offset));
  85. esp_partition_munmap(data_map);
  86. bytes_to_write -= write_bytes;
  87. offset += write_bytes;
  88. }
  89. ESP_LOGI(TAG, "finish the copy process");
  90. }
  91. #if defined(CONFIG_BOOTLOADER_FACTORY_RESET) || defined(CONFIG_BOOTLOADER_APP_TEST)
  92. /* @brief Copies partition from source partition to destination partition.
  93. *
  94. * Partitions can be of any types and subtypes.
  95. * @param[in] dst_partition - Destination partition
  96. * @param[in] src_partition - Source partition
  97. */
  98. static void copy_partition(const esp_partition_t *dst_partition, const esp_partition_t *src_partition)
  99. {
  100. const void *partition_bin = NULL;
  101. esp_partition_mmap_handle_t data_map;
  102. TEST_ESP_OK(esp_partition_mmap(src_partition, 0, src_partition->size, ESP_PARTITION_MMAP_DATA, &partition_bin, &data_map));
  103. TEST_ESP_OK(esp_partition_erase_range(dst_partition, 0, dst_partition->size));
  104. TEST_ESP_OK(esp_partition_write(dst_partition, 0, (const void *)partition_bin, dst_partition->size));
  105. esp_partition_munmap(data_map);
  106. }
  107. #endif
  108. /* @brief Get the next partition of OTA for the update.
  109. *
  110. * @return The next partition of OTA(OTA0-15).
  111. */
  112. static const esp_partition_t * get_next_update_partition(void)
  113. {
  114. const esp_partition_t *update_partition = esp_ota_get_next_update_partition(NULL);
  115. TEST_ASSERT_NOT_EQUAL(NULL, update_partition);
  116. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%"PRIx32, update_partition->subtype, update_partition->address);
  117. return update_partition;
  118. }
  119. /* @brief Copies a current app to next partition (OTA0-15) and then configure OTA data for a new boot partition.
  120. *
  121. * @param[in] cur_app_partition - Current app.
  122. * @param[in] next_app_partition - Next app for boot.
  123. */
  124. static void copy_current_app_to_next_part(const esp_partition_t *cur_app_partition, const esp_partition_t *next_app_partition)
  125. {
  126. esp_ota_get_next_update_partition(NULL);
  127. TEST_ASSERT_NOT_EQUAL(NULL, next_app_partition);
  128. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%"PRIx32, next_app_partition->subtype, next_app_partition->address);
  129. esp_ota_handle_t update_handle = 0;
  130. TEST_ESP_OK(esp_ota_begin(next_app_partition, OTA_SIZE_UNKNOWN, &update_handle));
  131. copy_app_partition(update_handle, cur_app_partition);
  132. TEST_ESP_OK(esp_ota_end(update_handle));
  133. TEST_ESP_OK(esp_ota_set_boot_partition(next_app_partition));
  134. }
  135. /* @brief Copies a current app to next partition (OTA0-15) and then configure OTA data for a new boot partition.
  136. *
  137. * @param[in] cur_app_partition - Current app.
  138. * @param[in] next_app_partition - Next app for boot.
  139. */
  140. static void copy_current_app_to_next_part_with_offset(const esp_partition_t *cur_app_partition, const esp_partition_t *next_app_partition)
  141. {
  142. esp_ota_get_next_update_partition(NULL);
  143. TEST_ASSERT_NOT_EQUAL(NULL, next_app_partition);
  144. ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%"PRIx32, next_app_partition->subtype, next_app_partition->address);
  145. esp_ota_handle_t update_handle = 0;
  146. TEST_ESP_OK(esp_ota_begin(next_app_partition, OTA_SIZE_UNKNOWN, &update_handle));
  147. copy_app_partition_with_offset(update_handle, cur_app_partition);
  148. TEST_ESP_OK(esp_ota_end(update_handle));
  149. TEST_ESP_OK(esp_ota_set_boot_partition(next_app_partition));
  150. }
  151. /* @brief Erase otadata partition
  152. */
  153. static void erase_ota_data(void)
  154. {
  155. const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
  156. TEST_ASSERT_NOT_EQUAL(NULL, data_partition);
  157. TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * SPI_FLASH_SEC_SIZE));
  158. }
  159. /* @brief Reboots ESP using mode deep sleep. This mode guaranty that RTC_DATA_ATTR variables is not reset.
  160. */
  161. static void reboot_as_deep_sleep(void)
  162. {
  163. ESP_LOGI(TAG, "reboot as deep sleep");
  164. esp_deep_sleep(20000);
  165. TEST_FAIL_MESSAGE("Should never be reachable except when sleep is rejected, abort");
  166. }
  167. /* @brief Copies a current app to next partition (OTA0-15), after that ESP is rebooting and run this (the next) OTAx.
  168. */
  169. static void copy_current_app_to_next_part_and_reboot(void)
  170. {
  171. const esp_partition_t *cur_app = esp_ota_get_running_partition();
  172. ESP_LOGI(TAG, "copy current app to next part");
  173. copy_current_app_to_next_part(cur_app, get_next_update_partition());
  174. reboot_as_deep_sleep();
  175. }
  176. /* @brief Copies a current app to next partition (OTA0-15) using esp_ota_write_with_offest(), after that ESP is rebooting and run this (the next) OTAx.
  177. */
  178. static void copy_current_app_to_next_part_with_offset_and_reboot(void)
  179. {
  180. const esp_partition_t *cur_app = esp_ota_get_running_partition();
  181. ESP_LOGI(TAG, "copy current app to next part");
  182. copy_current_app_to_next_part_with_offset(cur_app, get_next_update_partition());
  183. reboot_as_deep_sleep();
  184. }
  185. /* @brief Get running app.
  186. *
  187. * @return The next partition of OTA(OTA0-15).
  188. */
  189. static const esp_partition_t* get_running_firmware(void)
  190. {
  191. const esp_partition_t *configured = esp_ota_get_boot_partition();
  192. const esp_partition_t *running = esp_ota_get_running_partition();
  193. ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08"PRIx32")",
  194. running->type, running->subtype, running->address);
  195. ESP_LOGI(TAG, "Configured partition type %d subtype %d (offset 0x%08"PRIx32")",
  196. configured->type, configured->subtype, configured->address);
  197. TEST_ASSERT_NOT_EQUAL(NULL, configured);
  198. TEST_ASSERT_NOT_EQUAL(NULL, running);
  199. if (running->subtype != ESP_PARTITION_SUBTYPE_APP_TEST) {
  200. TEST_ASSERT_EQUAL_PTR(running, configured);
  201. }
  202. return running;
  203. }
  204. // type of a corrupt ota_data
  205. typedef enum {
  206. CORR_CRC_1_SECTOR_OTA_DATA = (1 << 0), /*!< Corrupt CRC only 1 sector of ota_data */
  207. CORR_CRC_2_SECTOR_OTA_DATA = (1 << 1), /*!< Corrupt CRC only 2 sector of ota_data */
  208. } corrupt_ota_data_t;
  209. /* @brief Get two copies ota_data from otadata partition.
  210. *
  211. * @param[in] otadata_partition - otadata partition.
  212. * @param[out] ota_data_0 - First copy from otadata_partition.
  213. * @param[out] ota_data_1 - Second copy from otadata_partition.
  214. */
  215. static void get_ota_data(const esp_partition_t *otadata_partition, esp_ota_select_entry_t *ota_data_0, esp_ota_select_entry_t *ota_data_1)
  216. {
  217. uint32_t offset = otadata_partition->address;
  218. uint32_t size = otadata_partition->size;
  219. if (offset != 0) {
  220. const esp_ota_select_entry_t *ota_select_map;
  221. ota_select_map = bootloader_mmap(offset, size);
  222. TEST_ASSERT_NOT_EQUAL(NULL, ota_select_map);
  223. memcpy(ota_data_0, ota_select_map, sizeof(esp_ota_select_entry_t));
  224. memcpy(ota_data_1, (uint8_t *)ota_select_map + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t));
  225. bootloader_munmap(ota_select_map);
  226. }
  227. }
  228. /* @brief Writes a ota_data into required sector of otadata_partition.
  229. *
  230. * @param[in] otadata_partition - Partition information otadata.
  231. * @param[in] ota_data - otadata structure.
  232. * @param[in] sec_id - Sector number 0 or 1.
  233. */
  234. static void write_ota_data(const esp_partition_t *otadata_partition, esp_ota_select_entry_t *ota_data, int sec_id)
  235. {
  236. esp_partition_write(otadata_partition, SPI_FLASH_SEC_SIZE * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t));
  237. }
  238. /* @brief Makes a corrupt of ota_data.
  239. * @param[in] err - type error
  240. */
  241. static void corrupt_ota_data(corrupt_ota_data_t err)
  242. {
  243. esp_ota_select_entry_t ota_data[2];
  244. const esp_partition_t *otadata_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
  245. TEST_ASSERT_NOT_EQUAL(NULL, otadata_partition);
  246. get_ota_data(otadata_partition, &ota_data[0], &ota_data[1]);
  247. if (err & CORR_CRC_1_SECTOR_OTA_DATA) {
  248. ota_data[0].crc = 0;
  249. }
  250. if (err & CORR_CRC_2_SECTOR_OTA_DATA) {
  251. ota_data[1].crc = 0;
  252. }
  253. TEST_ESP_OK(esp_partition_erase_range(otadata_partition, 0, otadata_partition->size));
  254. write_ota_data(otadata_partition, &ota_data[0], 0);
  255. write_ota_data(otadata_partition, &ota_data[1], 1);
  256. }
  257. #if defined(CONFIG_BOOTLOADER_FACTORY_RESET) || defined(CONFIG_BOOTLOADER_APP_TEST)
  258. /* @brief Sets the pin number to output and sets output level as low. After reboot (deep sleep) this pin keep the same level.
  259. *
  260. * The output level of the pad will be force locked and can not be changed.
  261. * Power down or call gpio_hold_dis will disable this function.
  262. *
  263. * @param[in] num_pin - Pin number
  264. */
  265. static void set_output_pin(uint32_t num_pin)
  266. {
  267. TEST_ESP_OK(gpio_hold_dis(num_pin));
  268. gpio_config_t io_conf;
  269. io_conf.intr_type = GPIO_INTR_DISABLE;
  270. io_conf.mode = GPIO_MODE_OUTPUT;
  271. io_conf.pin_bit_mask = (1ULL << num_pin);
  272. io_conf.pull_down_en = 0;
  273. io_conf.pull_up_en = 0;
  274. TEST_ESP_OK(gpio_config(&io_conf));
  275. TEST_ESP_OK(gpio_set_level(num_pin, 0));
  276. TEST_ESP_OK(gpio_hold_en(num_pin));
  277. }
  278. /* @brief Unset the pin number hold function.
  279. */
  280. static void reset_output_pin(uint32_t num_pin)
  281. {
  282. TEST_ESP_OK(gpio_hold_dis(num_pin));
  283. TEST_ESP_OK(gpio_reset_pin(num_pin));
  284. }
  285. #endif
  286. static void mark_app_valid(void)
  287. {
  288. #ifdef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  289. TEST_ESP_OK(esp_ota_mark_app_valid_cancel_rollback());
  290. #endif
  291. }
  292. /* @brief Checks and prepares the partition so that the factory app is launched after that.
  293. */
  294. static void start_test(void)
  295. {
  296. ESP_LOGI(TAG, "boot count 1 - reset");
  297. set_boot_count_in_nvs(1);
  298. erase_ota_data();
  299. ESP_LOGI(TAG, "ota_data erased");
  300. reboot_as_deep_sleep();
  301. }
  302. static void test_flow1(void)
  303. {
  304. uint8_t boot_count = get_boot_count_from_nvs();
  305. boot_count++;
  306. set_boot_count_in_nvs(boot_count);
  307. ESP_LOGI(TAG, "boot count %d", boot_count);
  308. const esp_partition_t *cur_app = get_running_firmware();
  309. switch (boot_count) {
  310. case 2:
  311. ESP_LOGI(TAG, "Factory");
  312. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  313. copy_current_app_to_next_part_and_reboot();
  314. break;
  315. case 3:
  316. ESP_LOGI(TAG, "OTA0");
  317. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  318. mark_app_valid();
  319. copy_current_app_to_next_part_and_reboot();
  320. break;
  321. case 4:
  322. ESP_LOGI(TAG, "OTA1");
  323. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_1, cur_app->subtype);
  324. mark_app_valid();
  325. copy_current_app_to_next_part_and_reboot();
  326. break;
  327. case 5:
  328. ESP_LOGI(TAG, "OTA0");
  329. mark_app_valid();
  330. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  331. erase_ota_data();
  332. break;
  333. default:
  334. erase_ota_data();
  335. TEST_FAIL_MESSAGE("Unexpected stage");
  336. break;
  337. }
  338. }
  339. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  340. // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//--
  341. // 3 Stage: run OTA0 -> check it -> copy OTA0 to OTA1 -> reboot --//--
  342. // 4 Stage: run OTA1 -> check it -> copy OTA1 to OTA0 -> reboot --//--
  343. // 5 Stage: run OTA0 -> check it -> erase OTA_DATA for next tests -> PASS
  344. TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, OTA1, OTA0", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow1, test_flow1, test_flow1, test_flow1);
  345. static void test_flow2(void)
  346. {
  347. uint8_t boot_count = get_boot_count_from_nvs();
  348. boot_count++;
  349. set_boot_count_in_nvs(boot_count);
  350. ESP_LOGI(TAG, "boot count %d", boot_count);
  351. const esp_partition_t *cur_app = get_running_firmware();
  352. switch (boot_count) {
  353. case 2:
  354. ESP_LOGI(TAG, "Factory");
  355. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  356. copy_current_app_to_next_part_and_reboot();
  357. break;
  358. case 3:
  359. ESP_LOGI(TAG, "OTA0");
  360. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  361. mark_app_valid();
  362. copy_current_app_to_next_part(cur_app, get_next_update_partition());
  363. corrupt_ota_data(CORR_CRC_1_SECTOR_OTA_DATA);
  364. reboot_as_deep_sleep();
  365. break;
  366. case 4:
  367. ESP_LOGI(TAG, "Factory");
  368. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  369. erase_ota_data();
  370. break;
  371. default:
  372. erase_ota_data();
  373. TEST_FAIL_MESSAGE("Unexpected stage");
  374. break;
  375. }
  376. }
  377. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  378. // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//--
  379. // 3 Stage: run OTA0 -> check it -> corrupt ota data -> reboot --//--
  380. // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS
  381. TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, corrupt ota_sec1, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow2, test_flow2, test_flow2);
  382. static void test_flow3(void)
  383. {
  384. uint8_t boot_count = get_boot_count_from_nvs();
  385. boot_count++;
  386. set_boot_count_in_nvs(boot_count);
  387. ESP_LOGI(TAG, "boot count %d", boot_count);
  388. const esp_partition_t *cur_app = get_running_firmware();
  389. switch (boot_count) {
  390. case 2:
  391. ESP_LOGI(TAG, "Factory");
  392. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  393. copy_current_app_to_next_part_and_reboot();
  394. break;
  395. case 3:
  396. ESP_LOGI(TAG, "OTA0");
  397. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  398. mark_app_valid();
  399. copy_current_app_to_next_part_and_reboot();
  400. break;
  401. case 4:
  402. ESP_LOGI(TAG, "OTA1");
  403. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_1, cur_app->subtype);
  404. mark_app_valid();
  405. copy_current_app_to_next_part(cur_app, get_next_update_partition());
  406. corrupt_ota_data(CORR_CRC_2_SECTOR_OTA_DATA);
  407. reboot_as_deep_sleep();
  408. break;
  409. case 5:
  410. ESP_LOGI(TAG, "OTA0");
  411. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  412. erase_ota_data();
  413. break;
  414. default:
  415. erase_ota_data();
  416. TEST_FAIL_MESSAGE("Unexpected stage");
  417. break;
  418. }
  419. }
  420. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  421. // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//--
  422. // 3 Stage: run OTA0 -> check it -> copy OTA0 to OTA1 -> reboot --//--
  423. // 3 Stage: run OTA1 -> check it -> corrupt ota sector2 -> reboot --//--
  424. // 4 Stage: run OTA0 -> check it -> erase OTA_DATA for next tests -> PASS
  425. TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, OTA1, corrupt ota_sec2, OTA0", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow3, test_flow3, test_flow3, test_flow3);
  426. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
  427. static void test_flow4(void)
  428. {
  429. uint8_t boot_count = get_boot_count_from_nvs();
  430. boot_count++;
  431. set_boot_count_in_nvs(boot_count);
  432. ESP_LOGI(TAG, "boot count %d", boot_count);
  433. const esp_partition_t *cur_app = get_running_firmware();
  434. switch (boot_count) {
  435. case 2:
  436. ESP_LOGI(TAG, "Factory");
  437. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  438. copy_current_app_to_next_part_and_reboot();
  439. break;
  440. case 3:
  441. ESP_LOGI(TAG, "OTA0");
  442. #ifdef BOOTLOADER_RESERVE_RTC_MEM
  443. TEST_ASSERT_FALSE(bootloader_common_get_rtc_retain_mem_factory_reset_state());
  444. #endif
  445. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  446. mark_app_valid();
  447. set_output_pin(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET);
  448. esp_restart();
  449. break;
  450. case 4:
  451. reset_output_pin(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET);
  452. ESP_LOGI(TAG, "Factory");
  453. #ifdef BOOTLOADER_RESERVE_RTC_MEM
  454. TEST_ASSERT_TRUE(bootloader_common_get_rtc_retain_mem_factory_reset_state());
  455. TEST_ASSERT_FALSE(bootloader_common_get_rtc_retain_mem_factory_reset_state());
  456. #endif
  457. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  458. erase_ota_data();
  459. break;
  460. default:
  461. erase_ota_data();
  462. TEST_FAIL_MESSAGE("Unexpected stage");
  463. break;
  464. }
  465. }
  466. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  467. // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//--
  468. // 3 Stage: run OTA0 -> check it -> set_pin_factory_reset -> reboot
  469. // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS
  470. TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0, sets pin_factory_reset, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, SW_CPU_RESET, DEEPSLEEP_RESET]", start_test, test_flow4, test_flow4, test_flow4);
  471. #endif
  472. #ifdef CONFIG_BOOTLOADER_APP_TEST
  473. static void test_flow5(void)
  474. {
  475. uint8_t boot_count = get_boot_count_from_nvs();
  476. boot_count++;
  477. set_boot_count_in_nvs(boot_count);
  478. ESP_LOGI(TAG, "boot count %d", boot_count);
  479. const esp_partition_t *cur_app = get_running_firmware();
  480. switch (boot_count) {
  481. case 2:
  482. ESP_LOGI(TAG, "Factory");
  483. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  484. set_output_pin(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST);
  485. copy_partition(esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEST, NULL), cur_app);
  486. esp_restart();
  487. break;
  488. case 3:
  489. reset_output_pin(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST);
  490. ESP_LOGI(TAG, "Test");
  491. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_TEST, cur_app->subtype);
  492. esp_restart();
  493. break;
  494. case 4:
  495. ESP_LOGI(TAG, "Factory");
  496. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  497. erase_ota_data();
  498. break;
  499. default:
  500. reset_output_pin(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST);
  501. erase_ota_data();
  502. TEST_FAIL_MESSAGE("Unexpected stage");
  503. break;
  504. }
  505. }
  506. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  507. // 2 Stage: run factory -> check it -> copy factory to Test and set pin_test_app -> reboot
  508. // 3 Stage: run test -> check it -> reset pin_test_app -> reboot
  509. // 4 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS
  510. TEST_CASE_MULTIPLE_STAGES("Switching between factory, test, factory", "[app_update][timeout=90][reset=SW_CPU_RESET, SW_CPU_RESET, DEEPSLEEP_RESET]", start_test, test_flow5, test_flow5, test_flow5);
  511. #endif
  512. static const esp_partition_t* app_update(void)
  513. {
  514. const esp_partition_t *cur_app = get_running_firmware();
  515. const esp_partition_t* update_partition = esp_ota_get_next_update_partition(NULL);
  516. TEST_ASSERT_NOT_NULL(update_partition);
  517. esp_ota_handle_t update_handle = 0;
  518. TEST_ESP_OK(esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle));
  519. copy_app_partition(update_handle, cur_app);
  520. TEST_ESP_OK(esp_ota_end(update_handle));
  521. TEST_ESP_OK(esp_ota_set_boot_partition(update_partition));
  522. return update_partition;
  523. }
  524. static void test_rollback1(void)
  525. {
  526. uint8_t boot_count = get_boot_count_from_nvs();
  527. boot_count++;
  528. set_boot_count_in_nvs(boot_count);
  529. ESP_LOGI(TAG, "boot count %d", boot_count);
  530. const esp_partition_t *cur_app = get_running_firmware();
  531. esp_ota_img_states_t ota_state = 0x5555AAAA;
  532. const esp_partition_t* update_partition = NULL;
  533. switch (boot_count) {
  534. case 2:
  535. ESP_LOGI(TAG, "Factory");
  536. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  537. TEST_ASSERT_NULL(esp_ota_get_last_invalid_partition());
  538. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_ota_get_state_partition(cur_app, &ota_state));
  539. update_partition = app_update();
  540. TEST_ESP_OK(esp_ota_get_state_partition(update_partition, &ota_state));
  541. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  542. TEST_ASSERT_EQUAL(ESP_OTA_IMG_UNDEFINED, ota_state);
  543. #else
  544. TEST_ASSERT_EQUAL(ESP_OTA_IMG_NEW, ota_state);
  545. #endif
  546. reboot_as_deep_sleep();
  547. break;
  548. case 3:
  549. ESP_LOGI(TAG, "OTA0");
  550. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  551. TEST_ASSERT_NULL(esp_ota_get_last_invalid_partition());
  552. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  553. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  554. TEST_ASSERT_EQUAL(ESP_OTA_IMG_UNDEFINED, ota_state);
  555. #else
  556. TEST_ASSERT_EQUAL(ESP_OTA_IMG_PENDING_VERIFY, ota_state);
  557. #endif
  558. TEST_ESP_OK(esp_ota_mark_app_valid_cancel_rollback());
  559. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  560. TEST_ASSERT_EQUAL(ESP_OTA_IMG_VALID, ota_state);
  561. reboot_as_deep_sleep();
  562. break;
  563. case 4:
  564. ESP_LOGI(TAG, "OTA0");
  565. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  566. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  567. TEST_ASSERT_EQUAL(ESP_OTA_IMG_VALID, ota_state);
  568. TEST_ESP_OK(esp_ota_mark_app_invalid_rollback_and_reboot());
  569. break;
  570. default:
  571. erase_ota_data();
  572. TEST_FAIL_MESSAGE("Unexpected stage");
  573. break;
  574. }
  575. }
  576. static void test_rollback1_1(void)
  577. {
  578. set_boot_count_in_nvs(5);
  579. esp_ota_img_states_t ota_state = 0x5555AAAA;
  580. uint8_t boot_count = get_boot_count_from_nvs();
  581. ESP_LOGI(TAG, "boot count %d", boot_count);
  582. const esp_partition_t *cur_app = get_running_firmware();
  583. ESP_LOGI(TAG, "Factory");
  584. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  585. const esp_partition_t *invalid_partition = esp_ota_get_last_invalid_partition();
  586. const esp_partition_t* next_update_partition = esp_ota_get_next_update_partition(NULL);
  587. TEST_ASSERT_NOT_NULL(invalid_partition);
  588. TEST_ASSERT_NOT_NULL(next_update_partition);
  589. TEST_ASSERT_EQUAL_PTR(invalid_partition, next_update_partition);
  590. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_ota_get_state_partition(cur_app, &ota_state));
  591. TEST_ESP_OK(esp_ota_get_state_partition(invalid_partition, &ota_state));
  592. TEST_ASSERT_EQUAL(ESP_OTA_IMG_INVALID, ota_state);
  593. erase_ota_data();
  594. }
  595. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  596. // 2 Stage: run factory -> check it -> copy factory to next app slot -> reboot --//--
  597. // 3 Stage: run OTA0 -> check it -> esp_ota_mark_app_valid_cancel_rollback() -> reboot --//--
  598. // 4 Stage: run OTA0 -> check it -> esp_ota_mark_app_invalid_rollback_and_reboot() -> reboot
  599. // 5 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS
  600. TEST_CASE_MULTIPLE_STAGES("Test rollback. factory, OTA0, OTA0, rollback -> factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_rollback1, test_rollback1, test_rollback1, test_rollback1_1);
  601. static void test_rollback2(void)
  602. {
  603. uint8_t boot_count = get_boot_count_from_nvs();
  604. boot_count++;
  605. set_boot_count_in_nvs(boot_count);
  606. ESP_LOGI(TAG, "boot count %d", boot_count);
  607. const esp_partition_t *cur_app = get_running_firmware();
  608. esp_ota_img_states_t ota_state = 0x5555AAAA;
  609. const esp_partition_t* update_partition = NULL;
  610. switch (boot_count) {
  611. case 2:
  612. ESP_LOGI(TAG, "Factory");
  613. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  614. TEST_ASSERT_NULL(esp_ota_get_last_invalid_partition());
  615. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_ota_get_state_partition(cur_app, &ota_state));
  616. update_partition = app_update();
  617. TEST_ESP_OK(esp_ota_get_state_partition(update_partition, &ota_state));
  618. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  619. TEST_ASSERT_EQUAL(ESP_OTA_IMG_UNDEFINED, ota_state);
  620. #else
  621. TEST_ASSERT_EQUAL(ESP_OTA_IMG_NEW, ota_state);
  622. #endif
  623. reboot_as_deep_sleep();
  624. break;
  625. case 3:
  626. ESP_LOGI(TAG, "OTA0");
  627. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  628. TEST_ASSERT_NULL(esp_ota_get_last_invalid_partition());
  629. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  630. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  631. TEST_ASSERT_EQUAL(ESP_OTA_IMG_UNDEFINED, ota_state);
  632. #else
  633. TEST_ASSERT_EQUAL(ESP_OTA_IMG_PENDING_VERIFY, ota_state);
  634. #endif
  635. TEST_ESP_OK(esp_ota_mark_app_valid_cancel_rollback());
  636. TEST_ASSERT_NULL(esp_ota_get_last_invalid_partition());
  637. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  638. TEST_ASSERT_EQUAL(ESP_OTA_IMG_VALID, ota_state);
  639. update_partition = app_update();
  640. TEST_ESP_OK(esp_ota_get_state_partition(update_partition, &ota_state));
  641. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  642. TEST_ASSERT_EQUAL(ESP_OTA_IMG_UNDEFINED, ota_state);
  643. #else
  644. TEST_ASSERT_EQUAL(ESP_OTA_IMG_NEW, ota_state);
  645. #endif
  646. reboot_as_deep_sleep();
  647. break;
  648. case 4:
  649. ESP_LOGI(TAG, "OTA1");
  650. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_1, cur_app->subtype);
  651. TEST_ASSERT_NULL(esp_ota_get_last_invalid_partition());
  652. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  653. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  654. TEST_ASSERT_EQUAL(ESP_OTA_IMG_UNDEFINED, ota_state);
  655. TEST_ESP_OK(esp_ota_mark_app_invalid_rollback_and_reboot());
  656. #else
  657. TEST_ASSERT_EQUAL(ESP_OTA_IMG_PENDING_VERIFY, ota_state);
  658. reboot_as_deep_sleep();
  659. #endif
  660. break;
  661. default:
  662. erase_ota_data();
  663. TEST_FAIL_MESSAGE("Unexpected stage");
  664. break;
  665. }
  666. }
  667. static void test_rollback2_1(void)
  668. {
  669. set_boot_count_in_nvs(5);
  670. uint8_t boot_count = get_boot_count_from_nvs();
  671. esp_ota_img_states_t ota_state = 0x5555AAAA;
  672. ESP_LOGI(TAG, "boot count %d", boot_count);
  673. const esp_partition_t *cur_app = get_running_firmware();
  674. ESP_LOGI(TAG, "OTA0");
  675. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  676. const esp_partition_t *invalid_partition = esp_ota_get_last_invalid_partition();
  677. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_1, invalid_partition->subtype);
  678. const esp_partition_t* next_update_partition = esp_ota_get_next_update_partition(NULL);
  679. TEST_ASSERT_NOT_NULL(invalid_partition);
  680. TEST_ASSERT_NOT_NULL(next_update_partition);
  681. TEST_ASSERT_EQUAL_PTR(invalid_partition, next_update_partition);
  682. TEST_ESP_OK(esp_ota_get_state_partition(cur_app, &ota_state));
  683. TEST_ASSERT_EQUAL(ESP_OTA_IMG_VALID, ota_state);
  684. TEST_ESP_OK(esp_ota_get_state_partition(invalid_partition, &ota_state));
  685. #ifndef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
  686. TEST_ASSERT_EQUAL(ESP_OTA_IMG_INVALID, ota_state);
  687. #else
  688. TEST_ASSERT_EQUAL(ESP_OTA_IMG_ABORTED, ota_state);
  689. #endif
  690. erase_ota_data();
  691. }
  692. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  693. // 2 Stage: run factory -> check it -> copy factory to next app slot -> reboot --//--
  694. // 3 Stage: run OTA0 -> check it -> esp_ota_mark_app_valid_cancel_rollback(), copy to next app slot -> reboot --//--
  695. // 4 Stage: run OTA1 -> check it -> PENDING_VERIFY/esp_ota_mark_app_invalid_rollback_and_reboot() -> reboot
  696. // 5 Stage: run OTA0(rollback) -> check it -> erase OTA_DATA for next tests -> PASS
  697. TEST_CASE_MULTIPLE_STAGES("Test rollback. factory, OTA0, OTA1, rollback -> OTA0", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_rollback2, test_rollback2, test_rollback2, test_rollback2_1);
  698. static void test_erase_last_app_flow(void)
  699. {
  700. uint8_t boot_count = get_boot_count_from_nvs();
  701. boot_count++;
  702. set_boot_count_in_nvs(boot_count);
  703. ESP_LOGI(TAG, "boot count %d", boot_count);
  704. const esp_partition_t *cur_app = get_running_firmware();
  705. switch (boot_count) {
  706. case 2:
  707. ESP_LOGI(TAG, "Factory");
  708. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  709. app_update();
  710. reboot_as_deep_sleep();
  711. break;
  712. case 3:
  713. ESP_LOGI(TAG, "OTA0");
  714. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  715. mark_app_valid();
  716. app_update();
  717. reboot_as_deep_sleep();
  718. break;
  719. case 4:
  720. ESP_LOGI(TAG, "OTA1");
  721. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_1, cur_app->subtype);
  722. TEST_ESP_OK(esp_ota_erase_last_boot_app_partition());
  723. TEST_ESP_OK(esp_ota_mark_app_invalid_rollback_and_reboot());
  724. reboot_as_deep_sleep();
  725. break;
  726. default:
  727. erase_ota_data();
  728. TEST_FAIL_MESSAGE("Unexpected stage");
  729. break;
  730. }
  731. }
  732. static void test_erase_last_app_rollback(void)
  733. {
  734. set_boot_count_in_nvs(5);
  735. uint8_t boot_count = get_boot_count_from_nvs();
  736. ESP_LOGI(TAG, "boot count %d", boot_count);
  737. const esp_partition_t *cur_app = get_running_firmware();
  738. ESP_LOGI(TAG, "erase_last_app");
  739. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  740. TEST_ESP_ERR(ESP_FAIL, esp_ota_erase_last_boot_app_partition());
  741. erase_ota_data();
  742. }
  743. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  744. // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//--
  745. // 3 Stage: run OTA0 -> check it -> copy factory to OTA1 -> reboot --//--
  746. // 4 Stage: run OTA1 -> check it -> erase OTA0 and rollback -> reboot
  747. // 5 Stage: run factory -> check it -> erase OTA_DATA for next tests -> PASS
  748. TEST_CASE_MULTIPLE_STAGES("Test erase_last_boot_app_partition. factory, OTA1, OTA0, factory", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET, DEEPSLEEP_RESET, SW_CPU_RESET]", start_test, test_erase_last_app_flow, test_erase_last_app_flow, test_erase_last_app_flow, test_erase_last_app_rollback);
  749. static void test_flow6(void)
  750. {
  751. uint8_t boot_count = get_boot_count_from_nvs();
  752. boot_count++;
  753. set_boot_count_in_nvs(boot_count);
  754. ESP_LOGI(TAG, "boot count %d", boot_count);
  755. const esp_partition_t *cur_app = get_running_firmware();
  756. switch (boot_count) {
  757. case 2:
  758. ESP_LOGI(TAG, "Factory");
  759. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_FACTORY, cur_app->subtype);
  760. copy_current_app_to_next_part_with_offset_and_reboot();
  761. break;
  762. case 3:
  763. ESP_LOGI(TAG, "OTA0");
  764. TEST_ASSERT_EQUAL(ESP_PARTITION_SUBTYPE_APP_OTA_0, cur_app->subtype);
  765. mark_app_valid();
  766. erase_ota_data();
  767. break;
  768. default:
  769. erase_ota_data();
  770. TEST_FAIL_MESSAGE("Unexpected stage");
  771. break;
  772. }
  773. }
  774. // 1 Stage: After POWER_RESET erase OTA_DATA for this test -> reboot through deep sleep.
  775. // 2 Stage: run factory -> check it -> copy factory to OTA0 -> reboot --//--
  776. // 3 Stage: run OTA0 -> check it -> erase OTA_DATA for next tests -> PASS
  777. TEST_CASE_MULTIPLE_STAGES("Switching between factory, OTA0 using esp_ota_write_with_offset", "[app_update][timeout=90][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET]", start_test, test_flow6, test_flow6);
  778. //IDF-5145
  779. TEST_CASE("Test bootloader_common_get_sha256_of_partition returns ESP_ERR_IMAGE_INVALID when image is ivalid", "[partitions]")
  780. {
  781. const esp_partition_t *cur_app = esp_ota_get_running_partition();
  782. ESP_LOGI(TAG, "copy current app to next part");
  783. const esp_partition_t *other_app = get_next_update_partition();
  784. copy_current_app_to_next_part(cur_app, other_app);
  785. erase_ota_data();
  786. uint8_t sha_256_cur_app[32];
  787. uint8_t sha_256_other_app[32];
  788. TEST_ESP_OK(bootloader_common_get_sha256_of_partition(cur_app->address, cur_app->size, cur_app->type, sha_256_cur_app));
  789. TEST_ESP_OK(bootloader_common_get_sha256_of_partition(other_app->address, other_app->size, other_app->type, sha_256_other_app));
  790. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha_256_cur_app, sha_256_other_app, sizeof(sha_256_cur_app), "must be the same");
  791. uint32_t data = 0;
  792. bootloader_flash_write(other_app->address + 0x50, &data, sizeof(data), false);
  793. TEST_ESP_ERR(ESP_ERR_IMAGE_INVALID, bootloader_common_get_sha256_of_partition(other_app->address, other_app->size, other_app->type, sha_256_other_app));
  794. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha_256_cur_app, sha_256_other_app, sizeof(sha_256_cur_app), "must be the same");
  795. }