stk500v2 bootloader (Arduino MEGA, etc..) question

Hi all,

Looking at the source for the stk500v2 bootloader (used for the MEGA2560 and others) I seem to find code that's duplicated.

There is a function named "__jumpMain" which consists of the following code:

void __jumpMain(void)
{
//  July 17, 2010   <MLS> Added stack pointer initialzation
//  the first line did not do the job on the ATmega128

    asm volatile ( ".set __stack, %0" :: "i" (RAMEND) );

//  set stack pointer to top of RAM

    asm volatile ( "ldi 16, %0" :: "i" (RAMEND >> 8) );
    asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_HI_ADDR) );

    asm volatile ( "ldi 16, %0" :: "i" (RAMEND & 0x0ff) );
    asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_LO_ADDR) );

    asm volatile ( "clr __zero_reg__" );                                    // GCC depends on register r1 set to 0
    asm volatile ( "out %0, __zero_reg__" :: "I" (_SFR_IO_ADDR(SREG)) );    // set SREG to 0
    asm volatile ( "jmp main");                                             // jump to main()
}

But then within main itself, there is this code:

    // some chips dont set the stack properly
    asm volatile ( ".set __stack, %0" :: "i" (RAMEND) );
    asm volatile ( "ldi 16, %0" :: "i" (RAMEND >> 8) );
    asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_HI_ADDR) );
    asm volatile ( "ldi 16, %0" :: "i" (RAMEND & 0x0ff) );
    asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_LO_ADDR) );

Now, since __jumpMain already does the stack initialization, why is it again done in main()?

I know it's not much code, but being able to remove a few bytes of (seemingly) redundant code would make room for other features without the need to balloon past 2048 bytes.

Thanks!

We should look (at) a branch for serial bootloader discussions...
I’m currently looking at developing an SD(hex) to Optiboot uploader package, but all the examples i’ve found only support the first 64k of FLASH.

I’m primarily working with 1284P, but of course, the 1284 hex has (02) extended address segments, so the uploader also needs to support that (Optiboot already does)

It's worse than that, because sp is initialized to eom on reset anyway.