Following on from the ideas in the Optiloader sketch, I made my own version which will handle the Mega2560. This is because it programs a byte at a time, rather than trying to read the entire bootloader into RAM (where it would not fit).
Details, wiring and photos here:
Example of programming a Mega:
Sketch output:
Atmega chip programmer.
Written by Nick Gammon.
Entered programming mode OK.
Signature = 0x1E 0x98 0x01
Processor = ATmega2560
Flash memory size = 262144 bytes.
LFuse = 0xFF
HFuse = 0xD8
EFuse = 0xFD
Lock byte = 0xCF
Bootloader address = 0x3E000
Bootloader length = 8192 bytes.
Type 'G' to program the chip with the bootloader ...
Erasing chip ...
Writing bootloader ...
Committing page starting at 0x3E000
Committing page starting at 0x3E100
...
Committing page starting at 0x3FE00
Committing page starting at 0x3FF00
Written.
Verifying ...
No errors found.
Writing fuses ...
LFuse = 0xFF
HFuse = 0xD8
EFuse = 0xFD
Lock byte = 0xCF
Done.
Type 'C' when ready to continue with another chip ...
Time to program: 3 seconds (one second to program a Uno).
There are instructions on my web site for how to process other .hex files so you can install bootloaders for other chips. At present it handles the ATmega168PA, ATmega328P and ATmega2560.
The sketch size is:
Binary sketch size: 17082 bytes (of a 32256 byte maximum)
Since the Optiboot loaders only take 512 bytes, there is room for a few more of them in the sketch for different processor types.