Skip to content

Fix off-by-one errors truncating the last byte of FLASH and DATA - #589

Merged
gatk555 merged 1 commit into
buserror:masterfrom
chschu:bugfix/chunk-length-off-by-one
Sep 24, 2026
Merged

gatk555 merged 1 commit into
buserror:masterfrom
chschu:bugfix/chunk-length-off-by-one

Conversation

@chschu

@chschu chschu commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

When simulating the official optiboot_atmega328.hex bootloader using run_avr -m atmega328 optiboot_atmega328.hex, the following output is produced:

MCU type and frequency are not set when loading .hex file
read_ihex_chunks: optiboot_atmega328.hex, unsupported check type 03
Loaded 476 bytes of Flash data at 0x7e00
Loading 2 bytes of Flash data at 7ffe would overflow buffer (32767 bytes).
Loaded 1 bytes of Flash data at 0x7ffe

The very last byte of FLASH (Optiboot stores its own version there) is never loaded by simavr. The same is true for DATA.

Two of the length calculations in avr_load_firmware(...) are off by one: avr->flashend is the last valid flash address, not the flash size, and avr->ramend is the last valid RAM address, not the RAM size.

This Pull Request fixes the two off-by-one errors. With the change applied, the above command loads the last byte and produces the following output:

MCU type and frequency are not set when loading .hex file
read_ihex_chunks: optiboot_atmega328.hex, unsupported check type 03
Loaded 476 bytes of Flash data at 0x7e00
Loaded 2 bytes of Flash data at 0x7ffe

@gatk555
gatk555 merged commit e35d99a into buserror:master Sep 24, 2026
8 checks passed
@gatk555

gatk555 commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks. Not seen in examples/board_simduino, but that bootloader is very old!

@chschu
chschu deleted the bugfix/chunk-length-off-by-one branch September 24, 2026 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants