Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-Stage Application Concept (IDFGH-8718) #10159

Closed
franz-ms-muc opened this issue Nov 11, 2022 · 13 comments
Closed

2-Stage Application Concept (IDFGH-8718) #10159

franz-ms-muc opened this issue Nov 11, 2022 · 13 comments
Assignees
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally Type: Feature Request Feature request for IDF

Comments

@franz-ms-muc
Copy link
Contributor

franz-ms-muc commented Nov 11, 2022

Is your feature request related to a problem?

not Really.

Describe the solution you'd like.

We want to extend the Application Bootup.

now it is like https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/startup.html.

  1. First stage bootloader
  2. Second stage bootloader
  3. Application startup executes

Our goal is to have another way to Flash the OTA0 and OTA1 Partitions, over a so-Called FBL (Flash Bootloader) which is able to get a new Application Binary over CAN-BUS (J1939, UDS). 

Therefore our intended Startup Flow would be:

  1. First stage bootloader
  2. Second stage bootloader
  3. FBL startup executes, and
    1. either flash OTA0 or OTA1 Partitions (initiated by a Magic CAN-Packet)
    2. or decide to go on with 4. 
  4. Application startup executes

i think @igrr is a guy who can answer this. 

Describe alternatives you've considered.

Alternative considered is to boot FBL, and then make a Reset, 

so the Second stage bootloader sees "we are in a warmstart" and start OTA1 or OTA2. But then we must extend the Bootloader, and it must know that FBL has already run, so decide if it is a warmstart or a coldstart.

Additional context.

No response

@franz-ms-muc franz-ms-muc added the Type: Feature Request Feature request for IDF label Nov 11, 2022
@espressif-bot espressif-bot added the Status: Opened Issue is new label Nov 11, 2022
@github-actions github-actions bot changed the title 2-Stage Application Concept 2-Stage Application Concept (IDFGH-8718) Nov 11, 2022
@MartinPatarinski
Copy link

Here is also a diagram of the startup flow , that is described above
image

@igrr
Copy link
Member

igrr commented Nov 11, 2022

I think something similar has been implemented in a number of projects already, as follows:

  • "Factory" partition is used as the "3rd stage" bootloader, which can download applications from various sources (Wi-Fi, SD card, etc)
  • "OTA" partitions are used to store the actual applications

This enables the following workflows:

  • Normal boot: 2nd stage bootloader loads the app from one of the OTA slots, the app starts running.
  • Upgrade:
    • OTA app receives an event that triggers the upgrade.
    • It calls esp_ota_set_boot_partition(factory_partition) and restarts.
    • The 2nd stage bootloader loads the factory app because it is now set as the boot partition.
    • The factory app downloads the update from one of the interfaces and flashes it into an OTA slot.
    • The factory app calls esp_ota_set_boot_partition(ota_partition) and restarts.
    • The 2nd stage bootloader loads the app from the selected OTA partition.
  • Fallback:
    • If an OTA app fails to boot after the upgrade, the 2nd stage bootloader can switch the default boot app to "factory", enabling recovery process. This can be done using CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE option.

For example, in one such project, the "factory" app would also present a menu on an LCD screen, letting the user to select the app ("game") to boot. The "factory" app also set up a webserver over Wi-Fi, letting users to update other apps ("games"). In your example, the "factory" app can get the update over CAN bus.

@franz-ms-muc
Copy link
Contributor Author

franz-ms-muc commented Nov 11, 2022 via email

@MartinPatarinski
Copy link

Yes, we have thought about putting the FBL as Factory, but it will not be possible to update the FBL after that.
Normally, we want also to add a so called "Bootloader updater" or "Bootkiller", which is downloaded in OTA partitions and updates the FBL itself.
If we choose to use factory we would not be able to use the OTA APIs to update the FBL, cause it is factory and not writable.
Maybe it is possible to write it directly via SPI Flash driver, but would the SSBL be able to read the updated FBL/Factory correctly. If there is a header check, this would make the SSBL consider the new image as invalid.

@igrr
Copy link
Member

igrr commented Nov 11, 2022

cause it is factory and not writable.

There's actually no restriction on writing to 'factory' partition. I think this scheme can be extended to make the factory partition updatable. You can add another partition (ota2) which would contain the dedicated 'factory partition updater app'. If the main app gets an event that signals the factory partition update, it sets the boot app to ota2 and resets. The rest of the logic is similar.

You may use a customized 2nd stage bootloader to implement custom boot priorities, if necessary — please refer to custom bootloader examples for that.

@MartinPatarinski
Copy link

We have tested that if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is enabled in the menuconfig, then it is possible to write the factory partition via the SPI flash driver. Also we have created custom boot priorities and this way our bootloader FBL is started every time after power on and checks for reprogramming request. If there is no such, it restarts and then SSBL starts the selected OTA partition that contains the main application.
image

@diplfranzhoepfinger
Copy link
Contributor

@igrr is this CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED neccessary ? 

du to my understanding it is protecting Partition Table and Bootloader, not the Factory App right ?

@igrr
Copy link
Member

igrr commented Nov 22, 2022

CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is only necessary to overwrite:

  • the bootloader
  • the partition table
  • the partition from which the application is currently running from.

So if you aren't trying to overwrite factory partition while running the app from the factory partition, this option should not be necessary.

@igrr
Copy link
Member

igrr commented Jan 9, 2023

@franz-ms-muc it seems that the question has been answered, can we close the issue now?

@franz-ms-muc
Copy link
Contributor Author

Close

@franz-ms-muc
Copy link
Contributor Author

@franz-ms-muc franz-ms-muc reopened this Feb 2, 2023
@igrr
Copy link
Member

igrr commented Feb 2, 2023

Have replied.

@franz-ms-muc
Copy link
Contributor Author

Outstanding good Support !

@espressif-bot espressif-bot added Resolution: Done Issue is done internally Status: Done Issue is done internally and removed Status: Opened Issue is new labels Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally Type: Feature Request Feature request for IDF
Projects
None yet
Development

No branches or pull requests

6 participants