Building Custom Flash Read/Write Protection Algorithms for General-Purpose MCUs: Online and Offline Programming Practices
When MCU vendors only ship internal Flash download algorithms without Option Bytes algorithms, programmers cannot enable or disable read-out protection (RDP) during mass production. This article explains how to close that gap by either creating a custom Option Bytes algorithm file or using the programmer's scripting mechanism (e.g., LUA) to perform protection operations directly over SWD, and how to unify the flow for both online and offline programming modes.
1. Background: Why Build Your Own Read/Write Protection Algorithm
During mass-production programming of embedded products, in addition to downloading firmware to internal Flash, you often need to enable read-out protection (RDP) on the chip to prevent the firmware from being illegally read out through the debug port. This relies on programming the chip’s Option Bytes.
The common industry situation is that most chip vendors only release download algorithm files for internal Flash (typically executable algorithm images in ELF/FLM format) and do not provide the corresponding Option Bytes algorithm files. As a result, when using a general-purpose programmer, firmware downloads normally, but enabling and disabling read/write protection is unavailable — you have to rely on vendor-specific tools or manual operations, which hurts production efficiency.
A pragmatic solution is for developers to create the Option Bytes algorithm file themselves and, together with the programmer’s scripting mechanism, encapsulate operations such as enabling and disabling protection. The following sections walk through a concrete practice (adding support for a new MCU series) to illustrate the complete implementation approach. The method applies to all similar scenarios.
2. Technical Principles
2.1 Flash Download Algorithms vs. Option Bytes Algorithms
A Flash download algorithm is essentially a small program that runs in the chip’s RAM. It contains functions for initialization, erase, program, and verify. The programmer loads it into RAM via a debug interface (such as SWD) and calls it. Its target is the regular Flash memory region.
Option Bytes are different. They form a configuration storage area independent of the main Flash, usually located in a specific system address space, and control critical parameters such as read protection level, write-protected sectors, watchdog configuration, and boot mode. Modifying Option Bytes typically requires:
- Access through specific addressing (reads/writes at specific offset addresses);
- Triggering a reload after writing (e.g., a system reset or a dedicated register trigger);
- Following an unlock sequence (write the unlock key first, then modify, then verify).
2.2 Why a Custom Algorithm File May Not Be Necessary
In practice, programming Option Bytes does not always require a dedicated FLM algorithm file. If the chip’s debug port can still access the system address space before protection is enabled, the programmer can directly write to and verify the Option Bytes region over SWD. In this case, describing the operation flow in a script (such as a LUA script) is enough to enable and disable protection — a much lighter-weight implementation. The trade-offs between the two approaches:
- Script-based direct write: low development effort and easy maintenance, suitable for chips with simple Option Bytes logic;
- Custom algorithm file: encapsulates complex unlock, erase, and program sequences inside the algorithm, offering stronger compatibility — suitable for chips with complex flows or special timing requirements.
3. Implementation Details
3.1 Scripted Configuration Structure
Taking the LUA scripting mechanism of a general-purpose programmer as an example, a typical device configuration script needs to describe the following:
-- Device interface type: "SWD", "SWIM", "SPI", "I2C", "UART"CHIP_TYPE = "SWD"-- Algorithm file paths for each memory typeAlgoFile_FLASH = "0:/Programmer/Device/xxx.FLM"AlgoFile_OPT = "" -- Option Bytes algorithm; leave empty when using script-based direct write-- Start address and size of the CPU internal FlashFLASH_ADDRESS = 0x08000000FLASH_SIZE = 512 * 1024 -- Can override the default value inside the FLM-- Start address of the CPU internal RAM (where the algorithm file is loaded and executed)RAM_ADDRESS = 0x20000000
Key points:
- FLASH_ADDRESS / FLASH_SIZE: define the address range of the target Flash. The size specified in the script can override the default inside the algorithm file, making it easier to handle models with different Flash capacities in the same series;
- RAM_ADDRESS: the algorithm file is loaded into RAM to run, so this address must match the chip’s actual SRAM mapping with sufficient space reserved;
- AlgoFile_OPT: fill in the path if you have built an Option Bytes algorithm file; otherwise, the script logic handles the protection operations directly.
3.2 Logic for Enabling and Disabling Protection
Read/write protection usually has multiple levels (e.g., fully open, read-protected, and a higher irreversible protection level). The script needs to implement two core operations:
Disabling protection: following the procedure in the vendor’s reference manual, write the unlock-level value to the Option Bytes region, then trigger a reload (usually a system reset). Note that after unprotection, the Flash contents may be entirely erased — this is by design as a security mechanism, and the production flow must account for it in advance.
Enabling protection: after the firmware has been downloaded and verified, set the protection level to the protected state as the final step of the production flow. This step must run only after firmware verification passes, to avoid a “lock first, program later” situation that leaves the chip inaccessible.
3.3 Unified Support for Online and Offline Programming
Modern production programmers generally support two working modes:
- Online mode: the programmer connects to host software, and scripts and firmware files are delivered in real time — suitable for R&D debugging and small-batch production;
- Offline mode: the programmer works standalone, with parameters and firmware pre-stored inside the device — suitable for high-volume line production.
The advantage of scripted configuration is that both modes share the same device description and protection logic. Once the script is saved to the device’s storage, offline execution follows the same scripted flow, completing the full closed loop of “download → verify → enable protection.”
4. Debugging and Verification Recommendations
- Verify the unprotection flow first: use a protected sample chip to confirm that the unprotection procedure correctly restores debug-port access;
- Verify the protected state: after enabling protection, try reading the Flash with a debugger and confirm the read returns invalid data or all zeros;
- Watch the reset timing: reloading Option Bytes after modification usually requires a reset, so leave sufficient reset-wait time in the script;
- Batch consistency: default Option Bytes values may differ across chip batches, so the script should explicitly write the complete configuration rather than modifying only individual bits;
- Avoid accidental locking: parameterize the protection level in the script to prevent operators from mistakenly selecting the irreversible highest protection level.
5. Conclusion
When a vendor does not provide an Option Bytes algorithm file, developers can fill the read/write protection gap in two ways: build a dedicated Option Bytes algorithm file, or use the programmer’s scripting mechanism to describe the protection operations directly. The latter has lower development cost and greater flexibility, and is sufficient for most application scenarios. By scripting the configuration and chaining it with the firmware download flow, you can achieve an automated “download, verify, lock” production closed loop in both online and offline modes, significantly improving line efficiency and firmware security. This method is broadly applicable to all MCUs that provide standard debug interfaces such as SWD.
