About main() in bootloader

Jerry Feldman gerald.feldman at hp.com
Tue Nov 30 08:57:26 EST 2004


On Tuesday 30 November 2004 8:17 am, George Wong wrote:
> I am studying bootloaders in embedded system.
> Generally speaking, there are two stages in a bootloader.
> The first stage, which is usually written in assembler, does some
> necessary settings. The second stage, which is usually written in C,
> provides more complex functions,such as setting specific devices, loading
> kernel image. My questions occurs on the moment that bootloader jumps
> from stage 1 to the entrance of C of stage 2. The most comman method is
> to consider the address of main() function as the entrance of executable
> of code of stage2. It, however, may cause two problems: the one is that
> we cannot pass arguments by main() fuction and the other is that we
> cannot deal with the situation which main function return value. But the
> problem can be fixed by a skillful method that is using a "trampoline"
> which is usually a piece of assembler. Following is an example:
>
>
> .text
>
> .globl _trampoline
> _trampoline:
> 	bl	main
> 	/* if main ever returns we just call it again */
> 	b	_trampoline
>
>
> I want to know that why the most comman method can cause the problems? I
> don't think it is a question which is special for embedded system. It, I
> think, is about the structure of main fuction in memory. Could anybody
> give me some clues? And another question is that why a "trampoline" can
> fix the problems?
There is no difference between the main() function and any other function. 
It is the responsibility of the caller to pass the appropriate parameters 
to main(). In a hosted environment, there is a loader that calls the 
appropriate library functions, such as CRT0. You can set up the parameters 
via the trampoline. You need to know the calling standard for the system 
you are on. Some systems push parameters on the stack, others place 
parameters in registers. 
main() expects that argument 1 be an int containing the number of arguments 
followed by an array of pointers to char. You also must consider some other 
things in the C environment. You need to set up an array for the 
environment variables. (Ancient C used to provide this as the third 
parameter). Since the stage 2 is a special program running as a 
stand-alone, the environment is probably not necessary. 
-- 
Jerry Feldman <gerald.feldman at hp.com>
Partner Technology Access Center (contractor) (PTAC-MA)
Hewlett-Packard Co.
550 King Street LKG2a-X2
Littleton, Ma. 01460
(978)506-5243



More information about the Discuss mailing list