Can program uploaded via serial port overwrite bootloader?

It's a custom ATMega328p board with a bootloader that I modified slightly (to flash LED on D9 during reboot). Flash with bootloader is 32650 bytes.
Normally I run it at 8 Mhz, but I changed lower fuse to run 16 Mhz. Since I didn't have it defined in my custom boards.txt I just switch Arduino IDE 1.6.5 to Uno.
It was working ok, until my sketch got really large (30,790 bytes). After that board just stopped working and I wasn't able to upload anything via FTDI adapter.
No problem flashing it with ISP programmer tho. This time I uploaded standard Uno bootloaer and everything works.
But I'd like to find what what happened. Is it possible for sketch to overrite part bootloader image via Serial (i.e. if board definitions are incorrect)?

Here's my board definition just in case:

##############################################################

LD8.name=ATmega328p @ 8Mhz LensDigital

LD8.upload.protocol=stk500
LD8.upload.maximum_size=32256
LD8.upload.speed=57600

LD8.bootloader.low_fuses=0xE2
LD8.bootloader.high_fuses=0xD2
LD8.bootloader.extended_fuses=0x06
LD8.bootloader.path=arduino:atmega
LD8.bootloader.file=ATmegaBOOT_168_atmega328_LD_8MHz.hex
LD8.bootloader.unlock_bits=0x3F
LD8.bootloader.lock_bits=0x0F

LD8.build.mcu=atmega328p
LD8.build.f_cpu=8000000L
LD8.build.core=arduino:arduino
LD8.build.variant=arduino:standard

##############################################################

AFAIK if the bootloader protection fuses BOOTSZx are not set correctly to protect the right amount of memory then you can overwrite part/all of the bootloader.

Once my flash code size was exactly the maximum, and everything was okay in the boards definition. Still my bootloader got corrupted. I never understood why it happened and it should not be possible, but it did.

I guess it's one of those ghost in the machine things :slight_smile: Thanks!

Bratan, you have high fuse set to 0xD2, so chip thinks that your bootloader have 2KB - is your bootloader really that huge?

majek:
Bratan, you have high fuse set to 0xD2, so chip thinks that your bootloader have 2KB - is your bootloader really that huge?

Hmm, that's interesting. How do I tell bootloader size? Bootloader hex file is 5.32KB

From hex file is hard to say how large is your bootloader. Which bootloader you modified?

majek:
From hex file is hard to say how large is your bootloader. Which bootloader you modified?

ATmegaBOOT_168_atmega328

Ok, so you have standard 2KB bootloader. It means that:

  1. you don't have fuses set to 'secure', so bootloader could destroy itself
  2. LD8.upload.maximum_size=32256 is wrong - it should be 30720 and this is maximum sketch size for this bootloader.

If you really need to put sketch larger than 30720B, you need smaller bootloader as Optiboot. Uno uses Optiboot, so that's why that way it works.