Posts Tagged ‘crack mcu atmega8a flash’

PostHeaderIcon ATmega8A Lock Bits: What Each of the Three Modes Blocks

People talk about “the lock bit” on an AVR as if it were a single switch. On the ATmega8A it is not. There are two lock bits, and they encode four states. Each state blocks a different operation, and the difference between them is the difference between a chip you can still program and a chip you cannot even verify.

This matters because the words “protected” and “unreadable” are used interchangeably in conversation and are not the same condition. A part sitting in mode 1 refuses new code but will happily run the code it already has, and in some setups will still answer a signature query. A part in mode 2 or 3 refuses verification as well. Same silicon, same fuse values except two bits, completely different bench behaviour.

The four states

The classic megaAVR encoding uses two lock bits. On most family members they are labelled LB1 and LB0; on the earliest parts they are LB2 and LB1. The state table is the same either way.

Lock state Programming of flash Verification of flash Programming of EEPROM Verification of EEPROM
Unlocked (both bits 1) allowed allowed allowed allowed
Mode 1 blocked allowed blocked allowed
Mode 2 blocked blocked blocked blocked
Mode 3 blocked blocked blocked blocked

Two things in that table are worth pausing on.

First, mode 1 blocks writing but leaves verification open. That means a mode 1 part is, in the literal sense, readable. The protection prevents you from changing the program; it does not prevent you from confirming what the program is. The usual way to reach a mode 1 state is a production line that wants to stop field reflashing without hiding anything.

Second, mode 2 and mode 3 differ in intent rather than in the two operations listed here. Mode 3 extends the block to the data memory as well, and on parts with a boot section the boot lock bits interact with it. For the practical question of “can I read the flash”, modes 2 and 3 behave the same way: no.

Why the chip still runs

Lock bits are not a runtime mechanism. They are enforced by the programming interface, not by the CPU. Once the part is out of reset and executing, the core fetches instructions from flash exactly as it always did. Nothing in the pipeline checks a lock bit before every fetch, and there is no bus matrix gate that hides an instruction from the prefetch unit.

That is why a locked ATmega8A still drives its UART, still toggles its pins, still behaves like a working product. The protection is a property of the door, not of the room.

It is also why behavioural observation remains useful on a locked part. If the interface will not tell you what the firmware is, the pins will tell you what it does. Those are different questions, and the second one is often the one that matters commercially.

Verification is its own gate

The programming interface exposes separate command groups: chip erase, program flash, program EEPROM, program lock bits, read signature, read flash, read EEPROM, read fuse and lock bits.

Lock mode 2 removes the read-flash command from the set of commands the part will accept. It does not remove the read-signature command, and it does not remove the read-fuse command. That asymmetry is deliberate — a production tester needs to confirm it is talking to the right device even when the device is protected.

So the first useful measurement on an unknown part is not “can I read the flash”. It is “which command groups still answer”. A part that answers signature and fuse reads but returns nothing for flash reads is telling you precisely which lock state it is in.

The boot section has a second set of bits

On parts with a boot loader, the boot section is guarded independently. The boot lock bits restrict whether the application section can write into the boot section, whether the boot section can write into itself, and whether the boot section can be read by the application through SPM.

This is where a lot of confusion originates. A part can be fully unlocked in the general sense and still refuse a boot-section read, because the boot lock bits are a separate field with separate rules. Conversely, a part with the general lock bits set may still have a readable boot section if the boot lock bits were left open.

If the product in front of you has a field-update mechanism, the boot section is a real and separate container. It is worth asking which of the two containers is actually locked before assuming the whole device is closed.

Chip erase clears the locks and the code together

Every AVR in this family supports chip erase. Chip erase does three things at once: it erases the flash, it erases the EEPROM unless the EESAVE fuse says otherwise, and it returns the lock bits to the unlocked state.

This is the single most misunderstood point in the whole subject. Chip erase is the only documented way to remove lock mode 2 or 3, and it destroys the contents in the same operation. There is no sequence of documented commands that moves a part from mode 2 back to unlocked while preserving the flash. The datasheet does not describe one because the hardware does not implement one.

So when someone says a part was “unlocked”, the first question to ask is what happened to the code. Either the answer is “it was erased”, or the statement is describing something other than the documented lock-bit state machine.

What a read attempt actually returns

A refused read does not necessarily return an error. On many programmers it returns a buffer full of a repeating pattern, because the interface shifts out whatever the internal bus drives when no valid data is presented. Seeing FF everywhere or 00 everywhere is usually a sign that the command was rejected and the programmer filled the gap, not that the chip contains a blank or a solid block.

That distinction has practical value. A blank part and a rejected read look similar in a hex dump and completely different at the command level. Checking the actual command response — not the buffer — separates them.

Related notes

FAQ

Does high-voltage programming bypass the lock bits? No. High-voltage parallel programming changes how the part is entered, not what the lock bits permit. It can always perform a chip erase, and a chip erase clears the locks along with the contents. It does not grant a read of protected flash.

Can the lock bits be read back? Yes. The lock byte is readable through the programming interface, which is how a programmer reports the current state. Reading the lock bits tells you the state; it does not tell you the contents.

Is mode 1 worth attacking? Mode 1 does not block verification, so there is nothing to attack — the data is available through the normal read commands. Effort spent on a mode 1 part is a procedural problem, not a technical one.

Does the EEPROM survive a chip erase? Only if the EESAVE fuse is programmed to preserve it. With EESAVE left at its default, chip erase takes the EEPROM too.

Conclusion

The ATmega8A’s protection is a four-state machine with three meaningful behaviours, not a single lock. Knowing which state a part is in changes the entire approach: mode 1 is a permissions question, mode 2 and 3 are an erasure question, and the boot section is a third container with its own rules. The one operation that resolves every locked state also removes the thing you wanted to read, which is the constraint the whole problem reduces to.