Bootloader and NVS

User avatar
fly135
Posts: 396
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Bootloader and NVS

Postby fly135 » Thu Jun 14, 2018 4:56 pm

Looking at the bootloader docs and the source on github it appears that a means to force a boot from the factory partition with a GPIO button/jumper has been implemented in the last month. I'd like to be able to not only force a boot from the factory partition, but also either erase the NVS or set a parameter in NVS so the app can make sure it goes back to default settings.

So my question is 1) does this GPIO factory boot thing work? and 2) can I access the NVS api from the bootloader. From reading the docs I can do this, but just was hoping someone in the know could confirm.

John A

chegewara
Posts: 480
Joined: Wed Jun 14, 2017 9:00 pm

Re: Bootloader and NVS

Postby chegewara » Thu Jun 14, 2018 5:24 pm

You can do it easy without bootloader. Like you mention you could set value in NVS that will be asserted from factory partition and if its set then erase/set default NVS. Just a few lines of code and you have it only in app on factory.

User avatar
fly135
Posts: 396
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Bootloader and NVS

Postby fly135 » Thu Jun 14, 2018 5:31 pm

chegewara wrote:You can do it easy without bootloader. Like you mention you could set value in NVS that will be asserted from factory partition and if its set then erase/set default NVS. Just a few lines of code and you have it only in app on factory.


Not really sure I understand. I do use a parameter in NVS that I use to determine if I'm in normal run mode with wifi, or if I need to run BLE for setup. That way I can free up the Bt resources in run mode. But the reason why I'm asking is if for some reason a bad parameter or something weird happens I can hold a button on boot to get back to the factory partition. The rationale is that if in normal run mode after an OTA update something is messed up I can get back to the factory where it's stable by holding a button during power up and not perhaps rebooting over and over.

John A

chegewara
Posts: 480
Joined: Wed Jun 14, 2017 9:00 pm

Re: Bootloader and NVS

Postby chegewara » Thu Jun 14, 2018 7:11 pm

There is 2 parts in my thinking:
- part1: bootloader pin to reboot from factory, as you said it is already implemented in current bootloader (your words, i didnt test it yet),
- part 2(this part is only for factory): you have key in nvs, first thing you have to do when factory app starts is to test if its empty or not, right before you switch to ota partition set a value, if ota partition crash then factory partition if value is not empty set default values, if its empty just start. Now, at some point in ota app, when you decide it is stable, you have procedure that set empty value (or when wifi is connected and you obtain IP); im not saying its good design, but its how i would start; if you have spare gpio then in ota app you can use the same gpio that force factory app start to toggle nvs value between empty not empty;

User avatar
fly135
Posts: 396
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Bootloader and NVS

Postby fly135 » Thu Jun 14, 2018 7:26 pm

OK, I could just assume that if I boot into the factory partition that the NVS setting should be defaulted (except for below). So it boots into factory, then requires BLE setup to store wifi and other parameters.

Then the factory app can set an NVS parameter that if it boots again into factory that it doesn't wipe NVS. Then when it boots into OTA the upgrade clears that factory parameter for any future issues. So the NVS factory parameter persists until an OTA update is made.

Quite frankly I'm not sure a reset of the NVS is required. Just thinking that if somehow a bad parameter was unexpectedly bricking the unit right after boot, then clearing to defaults would be extra protection.

My original idea was to check the reset button first thing on reboot so I could fix a problem before it reached it in the code. But I like the idea of pushing a reset button on boot to go back to factory. Then I can check the button during normal operation and only default the NVS parameters and not revert to factory.

Thanks for working on ideas with me on this! :)

John A

ESP_Angus
Posts: 1225
Joined: Sun May 08, 2016 4:11 am

Re: Bootloader and NVS

Postby ESP_Angus » Thu Jun 14, 2018 11:45 pm

fly135 wrote:LI'd like to be able to not only force a boot from the factory partition, but also either erase the NVS or set a parameter in NVS so the app can make sure it goes back to default settings.

So my question is 1) does this GPIO factory boot thing work? and 2) can I access the NVS api from the bootloader.


1) Yes!

2) No, we try to keep the bootloader simple so it doesn't have the NVS component compiled into it.

However, you can provide a comma-delimited list of names of partitions to erase on factory reset. If you add "nvs" to this list of names, it will be erased on factory reset.

If you specifically only want to remove one key from NVS but keep the rest, then you can still do that with this same mechanism - create a 1 sector (4096 byte) partition called "factory_reset_flag", have the factory reset erase this partition. In your app startup code, read a byte from this partition on boot and if it's 0xFF (freshly erased) then delete the NVS key if it exists, then write a single byte 0x00 to the partition so it won't reset again.

(Alternatively, you can copy the entire bootloader component into your project and add whatever custom logic is useful. But the above can be accomplished just with config items and no custom code.)

Note that, in general, erasing the entire partition is probably a more reliable way to reset to a blank clean state than deleting a single key. However both can work.

User avatar
fly135
Posts: 396
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Bootloader and NVS

Postby fly135 » Thu Jun 14, 2018 11:53 pm

Thanks ESP_Angus!

I think I have enough info to provide some clear direction on the best way to go. Love that you guys put that GPIO check in the bootloader for exactly what I was looking to do. And the code to clear a partition as well. :)

John A

Who is online

Users browsing this forum: Bing [Bot] and 14 guests