About main() in bootloader

Michael ODonnell michael.odonnell at comcast.net
Fri Dec 3 21:57:01 EST 2004


> 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.


In most embedded code of recent vintage, the absolute minumum
is done in assembler, with transition to compiler-generated
code as early as possible.  The things that most commonly
prevent you from executing compiler-generated code are
memory constraints; either you don't have enough, or it's
not writable, or it's not initially available in a convenient
location, or the code was loaded from low-capacity storage,
etc, etc.  So the "necessary settings" are typically
involved with rigging your memory such that it's suitable
for execution of compiler-generated code.  Such setup can
involve anything from a trivial few lines to elaborate VM
gymnastics.  After that, though, most of the bit-bashing
you can accomplish in assembler is also possible in C,
and those ops that can't be expressed directly in C are
typically available via the sorely abused ASM statements.

Once you've transitioned into your C code it's very unusual
that you'll ever return to your assembler code except when
something very bad has happened, so I'd recommend against
the practice of just blindly leaping back into the C code.
Better to print a simple message and then just spin until
reset.  If your system could truly benefit from leaping back
in you should be able to detect that condition from within
the C code and arrange the restart there.  That trampoline
hack is not so much "skillful" as prayerful...

As for passing parameters back and forther between assembler
and compiler-generated code, that's a well-understood
practice and is easily accomplished - just check the docs
for whatever tools you're using.  It's frequently the case
that the caller passes parameters to the callee on the
stack while return values are passed via the registers.
 



More information about the gnhlug-discuss mailing list