KernelRelocate() copies initialized global variables to RAM, and sets uninitialized global variables to zero.   KernelRelocate() is called from both the bootloader and the kernel.
If you are more familiar with writing applications that run within an Operating System (OS) then needing KernelRelocate() is different. When you run an application within an OS, initialization of global variables is automatically performed for the application by the OS. But, the bootloader and the kernel are the OS, so they must initialize global variables themselves.
The bootloader calls KernelRelocate() in BootloaderMain() if it uses BLCommon. Since KernelRelocate() isn’t called until BootloaderMain(), the code must not access the global variables until after BootloaderMain() calls kernel Relocate(). If the code reads a global variable prior to KernelRelocate() the data will be invalid, if the code writes to a global variable prior to KernelRelocate() the data will be lost when KernelRelocate() runs.
Taking that a step further, if the initialized global variable is a pointer initialized with an address the bootloader could fail to run successfully. That means that if you make the mistake of dereferencing an initialized global pointer prior to calling KernelRelocate() you will probably need to get out a JTAG tool to reprogram the bootloader.
The kernel calls KernelRelocate() prior to calling functions in the OAL, so it is generally safe to access global variables from OAL code.
One question that comes up is why does KernelRelocate() need to be called in the bootloader if the bootloader is running in RAM? KernelRelocate() must be called to initialize global variables because while the data is within the bootloader, the initialization data cannot be accessed by the bootloader directly for a few reasons; first the code doesn’t assume that the data is in RAM (in some cases it isn’t in RAM), second the code is looking for the data in RAM and third the code is compiled and linked using the same tools as general applications are build with.
Copyright © 2009 – Bruce Eitman
All Rights Reserved