Here is the example of what I'm using to force boot to factory partition (in
bootloader_start.c, function
get_selected_boot_partition(const bootloader_state_t *bs)):
Code: Select all
/** LoBo
* @function : ForceFactoryBoot
* @description: Add by Imtiaz @ Syrp for failsafe mode
*
* @inputs: void
*/
const uint32_t GPIO_PIN_MUX_REG[GPIO_PIN_COUNT] = {
IO_MUX_GPIO0_REG,
IO_MUX_GPIO1_REG,
IO_MUX_GPIO2_REG,
IO_MUX_GPIO3_REG,
IO_MUX_GPIO4_REG,
IO_MUX_GPIO5_REG,
IO_MUX_GPIO6_REG,
IO_MUX_GPIO7_REG,
IO_MUX_GPIO8_REG,
IO_MUX_GPIO9_REG,
IO_MUX_GPIO10_REG,
IO_MUX_GPIO11_REG,
IO_MUX_GPIO12_REG,
IO_MUX_GPIO13_REG,
IO_MUX_GPIO14_REG,
IO_MUX_GPIO15_REG,
IO_MUX_GPIO16_REG,
IO_MUX_GPIO17_REG,
IO_MUX_GPIO18_REG,
IO_MUX_GPIO19_REG,
0,
IO_MUX_GPIO21_REG,
IO_MUX_GPIO22_REG,
IO_MUX_GPIO23_REG,
0,
IO_MUX_GPIO25_REG,
IO_MUX_GPIO26_REG,
IO_MUX_GPIO27_REG,
0,
0,
0,
0,
IO_MUX_GPIO32_REG,
IO_MUX_GPIO33_REG,
IO_MUX_GPIO34_REG,
IO_MUX_GPIO35_REG,
IO_MUX_GPIO36_REG,
IO_MUX_GPIO37_REG,
IO_MUX_GPIO38_REG,
IO_MUX_GPIO39_REG,
};
static uint8_t ForceFactoryBoot(void)
{
#if CONFIG_GPIO_INPUT_FORCE_FACTORY
uint8_t timer = 0;
uint32_t mux_reg = GPIO_PIN_MUX_REG[CONFIG_GPIO_PIN_FORCE_FACTORY];
if (mux_reg == 0) return 0;
/*if (CONFIG_GPIO_PIN_FORCE_FACTORY < 28)*/ gpio_pad_select_gpio(CONFIG_GPIO_PIN_FORCE_FACTORY);
//output disable
if (CONFIG_GPIO_PIN_FORCE_FACTORY < 32) gpio_output_set(0,0,0, 1<<CONFIG_GPIO_PIN_FORCE_FACTORY);
else gpio_output_set_high(0,0,0, 1<<(CONFIG_GPIO_PIN_FORCE_FACTORY-32));
// Enable input
PIN_INPUT_ENABLE(mux_reg);
ets_delay_us(10000); //delay 10 msecs
ESP_LOGI(TAG, "Check force Factory boot Pin %d for level %d [current=%d]",
CONFIG_GPIO_PIN_FORCE_FACTORY, CONFIG_GPIO_LEVEL_FORCE_FACTORY, GPIO_INPUT_GET(CONFIG_GPIO_PIN_FORCE_FACTORY));
while (GPIO_INPUT_GET(CONFIG_GPIO_PIN_FORCE_FACTORY) == CONFIG_GPIO_LEVEL_FORCE_FACTORY) {
if ((timer % 5) == 0) {
ESP_LOGE(TAG, "Force Factory boot Pin is Active!");
}
ets_delay_us(100000); // wait 100 ms
timer++;
if (timer == 30) {
ESP_LOGE(TAG, "Forcing boot to Factory partition");
return 1;
}
}
ESP_LOGW(TAG, "Normal boot");
#endif
return 0;
}
/* Return the index of the selected boot partition.
This is the preferred boot partition, as determined by the partition table &
any OTA sequence number found in OTA data.
This partition will only be booted if it contains a valid app image, otherwise load_boot_image() will search
for a valid partition using this selection as the starting point.
*/
static int get_selected_boot_partition(const bootloader_state_t *bs)
{
esp_ota_select_entry_t sa,sb;
const esp_ota_select_entry_t *ota_select_map;
if (bs->ota_info.offset != 0 && !ForceFactoryBoot()) { // LoBo
// partition table has OTA data partition
GPIO_PIN_FORCE_FACTORY &
GPIO_LEVEL_FORCE_FACTORY are defined in application
Kconfig.projbuild:
Code: Select all
config GPIO_PIN_FORCE_FACTORY
int "GPIO to force boot from Factory partition"
depends on GPIO_INPUT_FORCE_FACTORY
range 0 39
default 0
help
Set GPIO pin to be used to force boot from Factory partition
config GPIO_LEVEL_FORCE_FACTORY
depends on GPIO_INPUT_FORCE_FACTORY
int "GPIO level to force boot from Factory partition"
range 0 1
default 0
help
Set GPIO pin level to force boot from Factory partition
It is tested and works without problem.
Similar code can be used to set the LED to some level on bootloader start:
Code: Select all
/**
* @function : bootloader_main
* @description: entry function of 2nd bootloader
*
* @inputs: void
*/
void bootloader_main()
{
vddsdio_configure();
flash_gpio_configure();
bootloader_clock_configure();
uart_console_configure();
wdt_reset_check();
#if CONFIG_BOOT_SET_LED >= 0
gpio_pad_select_gpio(CONFIG_BOOT_SET_LED);
if (CONFIG_BOOT_SET_LED < 32) gpio_output_set(CONFIG_BOOT_LED_ON << CONFIG_BOOT_SET_LED, (CONFIG_BOOT_LED_ON ? 0 : 1) << CONFIG_BOOT_SET_LED, 1<<CONFIG_BOOT_SET_LED,0);
else gpio_output_set_high(CONFIG_BOOT_LED_ON << (CONFIG_BOOT_SET_LED-32), (CONFIG_BOOT_LED_ON ? 0 : 1) << (CONFIG_BOOT_SET_LED-32), 1<<(CONFIG_BOOT_SET_LED-32),0);
ESP_LOGI(TAG, "BOOT LED set to %d", CONFIG_BOOT_LED_ON);
#endif
In
Kconfig.projbuild:
Code: Select all
config BOOT_SET_LED
int "Activate LED (gpio) on boot"
range -1 33
default -1
help
Set to GPIO pin to use as LED indication during boot
not used if set to -1
config BOOT_LED_ON
depends on BOOT_SET_LED >= 0
int "Set boot LED On level"
range 0 1
default 0
help
Set to GPIO pin level for boot LED On state