Attack ATmega Firmware: The Word/Byte Trap in Intel HEX
A recovered AVR image is a file of bytes. The AVR’s program counter counts words. Between those two facts sits a conversion that every toolchain performs, most of them correctly, and a few of them in a way that produces an image which looks right and is displaced by a factor of two.
This is not an obscure edge case. It is the single most common format error in AVR work, and it is invisible until the disassembly starts at the wrong place or the vector table points into the middle of an instruction.
The two addressing schemes
| Context | Unit | Example |
|---|---|---|
| Program counter | Words, each 16 bits | A jump to word address 0x100 |
| Instruction encoding | Words | Two bytes per instruction, four for some |
| Byte-oriented access | Bytes | Load from program memory reads a byte |
| Intel HEX records | Bytes | A record’s address is a byte address |
| Debug symbols | Words, usually | Function addresses in a map file |
The second row is why the factor of two exists at all: an AVR instruction is 16 bits wide in the base encoding, so a program counter value multiplied by two gives the byte offset. A jump to word 0x100 lands at byte 0x200.
Where the conversion happens
The conversion is performed by whatever tool moves data between the device and the file. There are four such points, and each is a chance to get it wrong.
| Boundary | Tool | Failure mode |
|---|---|---|
| Device to programmer | Programming protocol | Raw read in words interpreted as bytes |
| Programmer to file | Format writer | Address field written in words |
| File to loader | Format parser | Address field read as words |
| Loader to device | Programming protocol | Double conversion, net no change |
The fourth row describes a failure that cancels itself and is therefore harmless. The second and third rows are the dangerous ones, because each produces a file or a load that is wrong on its own.
How the error presents
A factor-of-two error is easy to recognise once it is suspected, and almost impossible to see otherwise.
| Symptom | Explanation |
|---|---|
| Disassembly is full of invalid opcodes | Started at the wrong byte offset |
| Disassembly is valid but the program does not match behaviour | Reading the right bytes at the wrong interpretation |
| Vector table entries point into the middle of instructions | Vectors are word addresses, file is bytes |
| The first instruction is at 0x0000 but the reset jump lands oddly | A doubled or halved vector |
| File length is exactly half the device size | Word count written as byte count |
The fifth row is the giveaway in practice. A device with 32 kilobytes of flash produces a file of 32 kilobytes. A file of 16 kilobytes from that device is either half-empty or wrong.
The vector table as a detector
The interrupt vector table is the best available check, because its structure is fixed and its content is meaningful.
| Observation | Conclusion |
|---|---|
| First word is a jump or a jump-to-self | Consistent with a valid image |
| Vectors are evenly spaced by two bytes | The spacing matches word-addressed vectors in bytes |
| Vectors are evenly spaced by four bytes | Either a device with two-word vectors, or a doubled image |
| Vector targets point at instruction boundaries | Correct alignment |
| Vector targets point at odd offsets | Alignment error |
The second and third rows are the key comparison. On a device whose vectors are one word apart, the byte spacing in the file is two; if it reads four, the image has been doubled somewhere.
Why the trap is specific to this family
Other architectures do not have this problem, and the reason is instructive.
| Architecture | Program memory unit | Same trap |
|---|---|---|
| AVR | 16-bit words | Yes |
| PIC mid-range | 14-bit words, byte-addressed files | Yes, with a different factor |
| ARM Cortex-M | Bytes | No |
| 8051 | Bytes | No |
| Most DSPs | Words, with separate spaces | Yes, and worse |
The second row is worth noting because it produces the same class of error with a different ratio: a 14-bit instruction does not map cleanly onto bytes, and the packing convention is a property of the file format rather than of the architecture.
Recovering from a doubled image
If the error is suspected, the correction is mechanical and verifiable.
| Step | Check |
|---|---|
| Halve or double all addresses | Does the vector table align |
| Re-run the length check against the device size | Does the file fit |
| Disassemble from the corrected base | Are opcodes valid |
| Compare the vector targets against the entry point | Do they agree |
The third step is the confirmation. An AVR disassembly of a correct image contains a high proportion of recognisable instructions and no long runs of undefined opcodes. A wrong offset produces the opposite.
The other half of the image
Correcting the addressing does not make the image complete. Program memory is only one of the three storage areas on the device, and the other two are separate.
| Area | Part of the program image | Read separately |
|---|---|---|
| Flash | Yes | No, same read |
| EEPROM | No | Yes, different command |
| Fuse and lock bytes | No | Yes, different command |
The distinction is treated in attacking ATmega AVR programs, and it matters here because a corrected program image still leaves the device’s calibration and configuration unaccounted for.
Where the rest of the chain is
The two configuration blocks and what each one locks are covered in attacking ATmega embedded firmware, and the identity check that should accompany any image is in reading Atmel AVR firmware. Both are worth doing before the addressing question, because they establish what device the image came from.
Services at this boundary
Work covers readout with explicit addressing conventions, offset error detection and correction, vector table validation, disassembly sanity checking, and recording of the convention used in the deliverable. It does not cover recovering an image that was written with the wrong convention and then partially overwritten, because the ambiguity is no longer resolvable from the file.
The practical rule is that every AVR deliverable should state whether its addresses are words or bytes, because the file itself does not.