custom bootloader example error when I try to change index

davidos
Posts: 1
Joined: Mon Oct 02, 2023 10:19 pm

custom bootloader example error when I try to change index

Postby davidos » Mon Oct 02, 2023 10:35 pm

I am using the example custom bootloader (custombootloader_override) in the IDF examples , I have OTA in my app , I am trying to find the simplest way to change from the OTA to factory within the bootloader (if a user keeps a button pressed), I need this in case there is an error in my code in the OTA - which prevents further updates.
I do not have the expertise to mess around with partition tables etc..

here is the code from the examples...how do I change the index so it points to factory if I keep a gpio/button pressed otherwise boot as normal (from either of the 2 OTA partitions)

Many thanks in advance for your help!!
  1. /*
  2.  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3.  *
  4.  * SPDX-License-Identifier: Apache-2.0
  5.  */
  6. #include <stdbool.h>
  7. #include "sdkconfig.h"
  8. #include "esp_log.h"
  9. #include "bootloader_init.h"
  10. #include "bootloader_utility.h"
  11. #include "bootloader_common.h"
  12.  
  13. static const char* TAG = "boot";
  14.  
  15. static int select_partition_number(bootloader_state_t *bs);
  16.  
  17. /*
  18.  * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  19.  * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  20.  * We do have a stack, so we can do the initialization in C.
  21.  */
  22. void __attribute__((noreturn)) call_start_cpu0(void)
  23. {
  24.     // 1. Hardware initialization
  25.     if (bootloader_init() != ESP_OK) {
  26.         bootloader_reset();
  27.     }
  28.  
  29. #ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP
  30.     // If this boot is a wake up from the deep sleep then go to the short way,
  31.     // try to load the application which worked before deep sleep.
  32.     // It skips a lot of checks due to it was done before (while first boot).
  33.     bootloader_utility_load_boot_image_from_deep_sleep();
  34.     // If it is not successful try to load an application as usual.
  35. #endif
  36.  
  37.     // 2. Select the number of boot partition
  38.     bootloader_state_t bs = {0};
  39.     int boot_index = select_partition_number(&bs);
  40.     if (boot_index == INVALID_INDEX) {
  41.         bootloader_reset();
  42.     }
  43.  
  44.     // 2.1 Print a custom message!
  45.     esp_rom_printf("[%s] %s\n", TAG, CONFIG_EXAMPLE_BOOTLOADER_WELCOME_MESSAGE);
  46.  
  47.     // 3. Load the app image for booting
  48.     bootloader_utility_load_boot_image(&bs, boot_index);
  49. }
  50.  
  51. // Select the number of boot partition
  52. static int select_partition_number(bootloader_state_t *bs)
  53. {
  54.     // 1. Load partition table
  55.     if (!bootloader_utility_load_partition_table(bs)) {
  56.         ESP_LOGE(TAG, "load partition table error!");
  57.         return INVALID_INDEX;
  58.     }
  59.  
  60.     // 2. Select the number of boot partition
  61.     return bootloader_utility_get_selected_boot_partition(bs);
  62. }
  63.  
  64. // Return global reent struct if any newlib functions are linked to bootloader
  65. struct _reent *__getreent(void)
  66. {
  67.     return _GLOBAL_REENT;
  68. }

ESP_Mahavir
Posts: 190
Joined: Wed Jan 24, 2018 6:51 am

Re: custom bootloader example error when I try to change index

Postby ESP_Mahavir » Mon Oct 09, 2023 3:58 am

Simplest option here is to erase the "otadata" partition, and that shall let the boot-up fallback to the factory partition. Please refer to this guide: https://docs.espressif.com/projects/esp ... tory-reset

Who is online

Users browsing this forum: rsimpsonbusa and 75 guests