Posts Tagged ‘break microcontroller embedded code’
ATmega164 Boot Section: SPM and the Bits That Guard It
An AVR that can update its own firmware must be able to write to its own flash. The instruction that does it is SPM, store program memory, and its existence changes the shape of the protection problem: there are now two containers — the application section and the boot section — and the boot section is the one that holds the update logic.
Most discussions of AVR protection treat the lock bits as a single wall. On a part with a bootloader there are two walls, and they are built from different bit fields with different rules. Which wall is up determines what a read can see, and it is common to find one up and the other down.
Two sections, two jobs
The flash on this family is divided into a read-while-write section and a no-read-while-write section. The boot section sits at the top of the address space and is intended to hold code that must not be overwritten by the code it is updating.
The boot section has three properties that matter:
- It can be entered automatically on reset if the boot reset fuse is programmed.
- Its size is selected by a fuse pair, in fixed steps.
- It is the only place from which
SPMmay write, on parts where the SPM instruction is restricted to the boot section.
That last point is the crux. The application code cannot rewrite flash by itself; it has to hand control to the boot section, which performs the write and returns. The boot section is therefore privileged code, and the hardware gives it its own protection bits.
The boot lock bits are not the lock bits
| Field | Guards | Typical effect when set |
|---|---|---|
| General lock bits | Whole flash, both sections | Blocks programming, then verification |
| Boot lock bits, mode 1 | Application writing into boot section | Application cannot modify the bootloader |
| Boot lock bits, mode 2 | Boot section writing into itself | Bootloader cannot self-modify |
| Boot lock bits, mode 3 | Both of the above | Bootloader is read-only to software |
Two facts follow, and both are useful on the bench.
First, a part can have its general lock bits clear — flash readable — and still have boot lock mode 2 set, which blocks the boot section from rewriting itself. That combination is common in products that want a field-updatable application but an immutable bootloader.
Second, the boot lock bits do not block external reads. They govern what software running on the chip may write. External read permission comes from the general lock bits alone.
Why a bootloader is a second door
If the boot section is present and its lock bits allow it, the device can program itself. That means a device in the field with a working bootloader accepts new code over whatever transport the bootloader implements — UART, USB, CAN, radio.
For anyone examining a product, that is a completely different situation from a device with no bootloader. There is an existing, vendor-supported path to change the contents. The question stops being “how do I get in” and becomes “what does the bootloader accept, and what does it check”. Signature verification in the bootloader turns an open door into a locked one, and the presence or absence of a verification step is often the single most important thing to determine about the product.
The erase question again
SPM can write flash, and it can also perform a page erase. It cannot erase the whole chip, and it cannot change the lock bits. That asymmetry is deliberate: a bootloader must be able to update a page, and must not be able to remove its own protection.
So on a part whose protection is the general lock bits in mode 2 or 3, the bootloader is not an escape route. It can rewrite the application section; it cannot unlock anything. The only operation that clears the lock bits remains the external chip erase, which returns the flash to blank.
That is worth stating clearly because the intuition runs the other way — “there’s a bootloader, so there’s a way in”. There is a way to write, not a way to read.
Reading the boot section specifically
If the general lock bits permit verification, both sections read normally, and the boot section is just the top slice of the address space. Its contents are frequently the most informative part of the image, because bootloaders tend to contain readable strings: protocol names, version numbers, error messages, sometimes vendor URLs.
If the general lock bits do not permit verification, neither section reads. The distinction between the two containers only becomes decisive when the general bits are open and the boot bits are not — in which case the application reads, the boot section reads, but the on-chip software cannot touch the bootloader.
What a fuse read tells you before any of this
Before assuming anything about sections, read the fuses. Three of them set the shape of the problem:
- Boot reset fuse: whether reset vectors into the boot section at all.
- Boot size fuse pair: how large the boot section is, in fixed steps.
- Boot lock bits: which of the three modes applies.
A part with the boot reset fuse unprogrammed still has a boot section defined, but never enters it. A part with the boot size fuse at its smallest setting has a boot section that can hold a jump table and very little else. Both are useful facts and both come from a read that does not touch the flash.
Related notes
- ATmega644 and read-while-write behaviour — what the other half of the split does under load.
- ATmega324PA and brown-out reads — why a marginal read looks like corruption.
- ATmega644PA and RSTDISBL — the fuse that removes ISP entry entirely.
FAQ
Can a bootloader remove the lock bits? No. SPM cannot modify the lock bits. Only an external chip erase resets them, and it erases the flash as well.
Does a locked part still boot through its bootloader? Yes. Lock bits are enforced by the programming interface, not by the CPU. The device runs exactly as designed.
Are the boot lock bits readable? Yes, the lock byte includes them and can be read back through the programming interface.
If the boot section is locked, is the application section still readable? That depends on the general lock bits, not the boot bits. The two fields are independent.
Conclusion
On an ATmega164 with a bootloader, protection is two containers with two bit fields. The boot lock bits govern what on-chip software may write; the general lock bits govern what an external programmer may read. Confusing the two leads to wrong conclusions in both directions — assuming a bootloader means access, and assuming a locked boot section means a locked device.