Skip to main content
Associate
September 15, 2025
Question

How to store a 1-bit bootloader flag without wasting 1 Flash page

  • September 15, 2025
  • 5 replies
  • 1199 views

Hello,

I am working on custom bootloader + application setup on a STM32G070CBT6

Currently, I need to store bootloader flag that indicate whether the MCU should jump to the bootloader code or to the application code.

  • Since the movement i am reserving 1 flash page(2KB) ,just to store single flag bit.
  • This feels like a waste of memory, since I only need 1 bit of storage.

My questions are:

     1. Can I use Option Bytes memory area to store the custom flag? 

  • Are Option bytes  writable at runtime for application/bootloader use, or are they limited to ST system configurations (e.g., RDP, WRP, BOR levels)?

      2. If Option Bytes memory area are not suitable ,is there another small memory area(apart from flash)

          that is recommended for storing such a small boot flag?

I need only one bit for this flag, but dedicating a full 2KB flash feel inefficient.

5 replies

Andrew Neil
Super User
September 15, 2025

Does this flag need to be non-volatile ?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
bhagatAuthor
Associate
September 15, 2025

Yes, the bootloader flag must be non-volatile, because I need it to persist across a reset.

Andrew Neil
Super User
September 15, 2025

@bhagat wrote:

Yes, the bootloader flag must be non-volatile, because I need it to persist across a reset.


That's a different thing!

Non-volatile means persisting across power loss/removal.

RAM is volatile (loses content with power loss), but it does persist across a reset.

All you need to do is to ensure that your flag does not get cleared by your startup code.

Check your compiler documentation for how to do that.

For GCC, you use a noinit section ...

https://community.st.com/t5/stm32cubemx-mcus/feature-request-quot-no-init-quot-linker-section/m-p/696858

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
Technical Moderator
September 15, 2025

Hello @bhagat and welcome to the community,

  • Since the movement i am reserving 1 flash page(2KB) ,just to store single flag bit.

Unfortunately, you need to do that when storing that flag in the flash.

Why putting the flag in the flash while you can put it in the RAM?

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question."
bhagatAuthor
Associate
September 15, 2025

I’m using the flag for my custom bootloader. Since I need the bootloader to check this flag after a reset, it must be stored in a non-volatile location. If I keep it in RAM, the value will be lost once the MCU resets, so the bootloader wouldn’t see it. That’s why I’m not using RAM for this flag.

MM..1
Chief III
September 15, 2025

Sorry boss, but flags (or any variable) in RAM can persist reset (right config) and not persist power down. Then this is primary condition.

AScha.3
Super User
September 15, 2025

Hi,

>Are Option bytes  writable at runtime

yes ; you could set bootloader/start or not.

+

If you have Vbat/RTC , could use user backup registers, 20 B afair.

"If you feel a post has answered your question, please click ""Accept as Solution""."
LCE
Principal II
September 15, 2025

I guess the OP wants to keep that info / bit even after power-down, so no RAM.

What about putting that info into the application flash when the bootloader writes it?
You're probably moving the vector table anyway, so move it a little further, and put the info you need into flash before the new VT address. Some linker file & __attribute__((section(".BootInfo"))) shenanigans required... :D

Andrew Neil
Super User
September 15, 2025

@LCE wrote:

I guess the OP wants to keep that info / bit even after power-down, so no RAM.


@bhagat only said it needs to survive reset:

https://community.st.com/t5/stm32-mcus-products/how-to-store-a-1-bit-bootloader-flag-without-wasting-1-flash/m-p/838949/highlight/true#M285703

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
bhagatAuthor
Associate
September 15, 2025

Sorry in my case, I need the bootloader flag to survive not only a reset but also a full power cycle. Since RAM contents are lost when power is removed, I cannot rely on RAM for this purpose.

TDK
Super User
September 15, 2025

You can use option bytes if you find one that won't cause other issues. For example, you can use NBOOT0 freely if NBOOT_SEL = 0. There are no free bits.

Note that writing option bytes also uses a page of flash internally and that interrupting the process due to a power loss can cause them to be corrupted.

"If you feel a post has answered your question, please click ""Accept as Solution""."