U-Boot is an open source boot loader that initializes hardware and loads operating systems. It supports many CPUs and boards. The boot process involves a pre-relocation phase where U-Boot initializes hardware and copies itself to RAM, and a post-relocation phase where it finishes hardware initialization and loads the kernel or operating system. Debugging can be done before and after relocation by setting breakpoints and examining memory.
2. Boot loader
• What is boot loader ?
– A boot loader is the first program
which executes (before the main
program i.e. kernel ) whenever a
system is initialized.
– A boot loader is a computer program
that loads the main program (i.e.
operating system, kernel ) for the
board after completion of the self-
tests.
• Why boot loader is needed ?
– Gives a development environment
– Saves cost of flash writers
– Give flexibility to load any operating
system
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
3. Types of Boot Loaders
• What are different types of boot loaders ?
– Boot-ROM ( or Pre-Boot Loader)
• Small code which loads First stage boot loader
– First Stage Boot Loader
• Small Piece of code that initialize the NAND/MMC & DRAM
controller.
– Second Stage Boot Loader
• Primary function of the second-stage boot loader is to Loading the
kernel into RAM or jumping directly to the start of the kernel.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
4. Boot Loader Stages
What is the responsibility of Boot-ROM, First, second stage boot loader ?
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
6. About U-Boot
• U-Boot (Universal Boot-loader) is an open source, multi platform boot-loader.
• U-Boot supports interactive commands, environment variables, command
scripting and booting from external media.
• U-Boot supports a large variety of popular CPUs and CPU families used
today, and a much larger collection of reference boards based on these
processors
• Generally, U-Boot resides in the beginning area of the flash. The exact sector or
block is defined by the processor.
• U-Boot initialize the CPU and several peripherals located on board, create
some critical data structures which will be used by kernel
• U-Boot is under GPLv2 License
• Refer below
– Wikipedia: http://en.wikipedia.org/wiki/Das_U-Boot
– Project: http://www.denx.de/wiki/U-Boot/WebHome
– Git: http://git.denx.de/u-boot.git
– FTP: http://ftp.denx.de/pub/u-boot/
– Mailing list: http://lists.denx.de/mailman/listinfo/u-boot
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
9. Before adding new board
• Look for an already supported board and SOC which
is as close as possible to yours.
• Identify the On-Chip and On-board peripherals
• Define physical memory map of board.
• Study of ARM Application Binary Interface (EABI)
• Study ARM assembly instruction
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
10. U-Boot: Porting to a LoganLTE board
• Choose an existing board which is most similar to the new board.
Say armadillo-800eva board
• Copy the ‘include/configs/armadillo-800eva.h’to
‘include/config/loganlte.h’.
• Create directory in ‘board/vendor/loganlte/’
• Update boards.cfg to add new entry for loganlte
• Create directory for SOC specific code ‘arch/arm/armv7/shmobile/’.
• Add corresponding header files in ‘arch/arm/include/asm/arch-shmobile’
• Modify the value of TEXT_BASE in ‘board/loganlte /config.mk’.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
11. U-boot: Build procedure
• export ARCH=arm
• export PATH=~/arm_eabi/bin/:$PATH
• export CROSS_COMPILE=arm-eabi-
• make distclean
• make <board>_config
• make
Output :
• u-boot is an ELF file of the code
• u-boot.bin is the binary image. (with ELF headers striped out) used
for download and debugging
• u-boot.srec is the S-Record image.
• u-boot.map has the global addresses for symbols, etc.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
13. U-boot : Linker script location
• If explicitly specified LDSCRIPT in board/cpu directory.
• If CONFIG_SYS_LDSCRIPT, use that or fail if absent.
• Otherwise, it search for 'u-boot.lds' sequentially in below location
– board/<board_name>/u-boot.lds
– arch/$(ARCH)/cpu/<SOC>/u-boot.lds
– arch/$(ARCH)/cpu/u-boot.lds
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
14. Global Data
typedef struct global_data {
bd_t *bd;
unsigned int baudrate;
unsigned long cpu_clk; /* CPU clock in Hz! */
unsigned long bus_clk;
unsigned long pci_clk;
unsigned long mem_clk;
#ifdef CONFIG_BOARD_TYPES
unsigned long board_type;
#endif
unsigned long have_console; /* serial_init() was called */
unsigned long env_addr; /* Address of Environment struct */
unsigned long env_valid; /* Checksum of Environment valid? */
/* TODO: is this the same as relocaddr, or something else? */
unsigned long dest_addr; /* Post-relocation address of U-Boot */
unsigned long dest_addr_sp;
unsigned long ram_top; /* Top address of RAM used by U-Boot */
unsigned long relocaddr; /* Start address of U-Boot in RAM */
phys_size_t ram_size; /* RAM size */
unsigned long mon_len; /* monitor len */
unsigned long irq_sp; /* irq stack pointer */
unsigned long start_addr_sp; /* start_addr_stackpointer */
unsigned long reloc_off; /* relocation offset */
struct global_data * new_gd; /* relocated global data */
const void * fdt_blob; /* Our device tree, NULL if none */
void * new_fdt; /* Relocated FDT */
unsigned long fdt_size; /* Space reserved for relocated FDT */
char env_buf[32]; /* buffer for getenv() before reloc. */
struct arch_global_data arch; /* architecture-specific data */
} gd_t;
include/asm-generic/global_data.h (DECLARE_GLOBAL_DATA_PTR)
.
typedef struct bd_info {
unsigned long bi_memstart; /* start of DRAM memory */
phys_size_t bi_memsize; /* size of DRAM memory in bytes */
unsigned long bi_flashstart; /* start of FLASH memory */
unsigned long bi_flashsize; /* size of FLASH memory */
unsigned long bi_flashoffset; /* reserved area for startup monitor */
unsigned long bi_sramstart; /* start of SRAM memory */
unsigned long bi_sramsize; /* size of SRAM memory */
#ifdef CONFIG_ARM
unsigned long bi_arm_freq; /* arm frequency */
unsigned long bi_ddr_freq; /* ddr frequency */
#endif
unsigned long bi_bootflags; /* boot / reboot flag (Unused) */
unsigned long bi_ip_addr; /* IP Address */
unsigned char bi_enetaddr[6]; /* OLD: see README.enetaddr */
unsigned short bi_ethspeed; /* Ethernet speed in Mbps */
unsigned long bi_intfreq; /* Internal Freq, in MHz */
unsigned long bi_busfreq; /* Bus Freq, in MHz */
unsigned int bi_baudrate; /* Console Baudrate */
unsigned int bi_procfreq; /* CPU (Internal) Freq, in Hz */
unsigned char bi_pci_enetaddr[6]; /* PCI Ethernet MAC address */
#endif
unsigned char bi_enetXaddr[6]; /* OLD: see README.enetaddr */
ulong bi_arch_number; /* unique id for this board */
ulong bi_boot_params; /* where this board expects params */
#ifdef CONFIG_NR_DRAM_BANKS
struct { /* RAM configuration */
ulong start;
ulong size;
} bi_dram[CONFIG_NR_DRAM_BANKS];
#endif /* CONFIG_NR_DRAM_BANKS */
} bd_t;
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
15. General bootstrapping Process
1. Disable all interrupts.
2. Copy any initialized data from ROM to RAM.
3. Zero the uninitialized data area.
4. Allocate space for and initialize the stack.
5. Initialize the processor’s stack pointer.
6. Create and initialize the heap.
7. Execute the initializers for all global data
8. Enable interrupts.
9. Call main loop.
U-Boot relocates itself from ROM to RAM. Divided the boot-strapping
process into two phases:
Pre-Relocation U-Boot Bootstrapping
Post-Relocation U-Boot Bootstrapping
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
16. Setup SP for
Early Board Setup
Environment (ASM->C)
Calculate Addresses
(SP, Dest, GD) for
Relocation
General Relocation
Copy Binary to RAM
Fix Relocation
Fix GOT
Clear BSS
Setup SP for
Common Board Setup
Setup GD and JUMP to
Final Board Setup
Low Level Initialization
Lowlevel_init
cpu_init_cp15
Disable MMU
Invalidate and disable
Instruction & data
Cache
Pre-Relocation bootstrapping
Disable IRQ &FIQ. Switch
to supervisor mode
cpu/armv7/star
t.S
Reset()
Reset CPU and Hardware
cpu/armv7/star
t.S
_start()
arm/lib/crt0.S
_main()
Early Board Setup
arm/lib/crt
0.S
board_init_
f()
arm/lib/crt
0.S
board_init_
r()
cpu/armv7/shmobile/lowl
evel_init.S
lowlevel_init()
cpu/armv7/start.S
relocate_code()
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
17. Pre-relocation bootstrapping
_start() /* Entry point function */
call reset() /* Reset CPU configurations */
save_boot_params() /* Save boot parameters */
Disable the FIQ & IRQ
Switch to supervisor mode
cpu_init_cp15() /* Initialize the Coprocessor 15 if
!CONFIG_SKIP_LOWLEVEL_INIT */
Invalidate I cache
Invalidate D Cache
Disable I cache if CONFIG_SYS_ICACHE_OFF
Disable MMU
cpu_init_crit() /* Call arch low level initialization if
!CONFIG_SKIP_LOWLEVEL_INIT */
lowlevel_init()
Call _main() /* setup initial stack & global data.
And jump to C routine for board initialization */
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
18. Pre-relocation bootstrapping
arch_cpu_init() // if CONFIG_ARCH_CPU_INIT
mark_bootstage() // Set boot stage to pre-relocation boot
board_early_init_f() // if CONFIG_BOARD_EARLY_INIT_F
timer_init() // Initialize timer
board_postclk_init() // if CONFIG_BOARD_POSTCLK_INIT
env_init() // initialize mmc/sdcard environment
init_baudrate() // set baudrate to CONFIG_BAUDRATE
serial_init() // Initialize serial driver
console_init_f() // Configure console from console.c
display_banner() // Print U-Boot banner
print_cpuinfo() // Print CPU information
checkboard() //print board info
init_func_i2c() // Initialize I2C if CONFIG_I2C
dram_init() // configure available RAM banks
board_init_f() calling sequence for arm
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
19. Bootstrapping process
arm/lib/board.c
board_init_r()
Setup Memory for
malloc()
Setup Board Data
Setup Board
FLASH
Setup Environment
env_relocate();
Jump into Main
loop
Setup Board Specific
Devices Load
Default
Environment
s
board/rmi/loganlt
e/loganlte.c
board_init()
board/rmi/loganlte/l
oganlte.c
enable_cache()
Setup late Board
Data
Early
arch/platform
specific misc
initializations
Initialize and
Enable exceptions
Early arch
specific & power
management
specific
initialization
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
20. U-boot : Pre-relocation bootstrapping
mark_bootstage() // Set boot stage to post-relocation boot
enable_caches() // Enable cache
board_init() // Setup board specific configuration (GPIO)
mem_malloc_init() // Configure malloc area
arch_early_init_r() // if CONFIG_ARCH_EARLY_INIT_R
power_init_board() // PMIC initializations
Flash/Nand/MMC initialization
env_relocate() // Relocate environment or set NULL
checkboard() //print board info
arch_misc_init() // miscellaneous arch dependent init
misc_init_r() // miscellaneous platform dependent init
interrupt_init() // Initialize exceptions if CONFIG_IRQ
enable_interrupts() // Enable Exceptions
board_late_init() // Board late init
eth_initialize() // Setup ethernet
main_loop() // Jump to main loop & waiting for commands from console.
board_init_r() calling sequence for arm
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
21. Debugging
• Add DEBUG to configuration file to inspect stack and relocation address.
– include/configs/loganlte.h:
• #define DEBUG
• When U-Boot starts it is running from ROM space. Running from flash would
make it nearly impossible to read from flash while executing code.
• With GCC it is possible to use optimization in combination with the debugging
option -g. Many other compilers do not allow this.
• U-boot image can also used to generate the disassembly.
– $(CROSS_COMPILE)objdump -D u-boot > u-boot.dis
• Later U-Boot relocates itself to RAM. We therefore have two phases:
– Debugging of U-Boot Before Relocation
– Debugging of U-Boot After Relocation
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
22. Debugging before Relocation
Debugging U-Boot in this phase with the BDI2000 is quite easy
bash[0]$ ${CROSS_COMPILE}gdb u-boot
(gdb) target remote bdi:2001
Remote debugging using bdi:2001
0xfffffffc in ?? ()
(gdb) b cpu_init_f
Breakpoint 1 at 0xfffd3310: file cpu_init.c, line 136.
(gdb) c
Continuing.
Breakpoint 1, cpu_init_f () at cpu_init.c:136
136 asm volatile(" bl 0f" ::: "lr");
(gdb) s
137 asm volatile("0: mflr 3" :::
"r3");
(gdb)
138 asm volatile(" addi 4, 0, 14" ::: "r4");
(gdb)
cpu_init_f is the first C function called from the code in start.C.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
23. Debugging after Relocation
struct global_data (register r8)‘relocaddr’ contains start address of u-boot in RAM
Relocation address = <MAXMEM> - CFG_MONITOR_LEN
= 16 MB – 192KB
= 0x1000000 - 0x30000 = 0xFD0000
(gdb) symbol-file
Discard symbol table from `/home/dzu/denx/cvs-trees/u-boot/u-boot'? (y or n) y
No symbol file now.
(gdb) add-symbol-file u-boot 0xfd0000
add symbol table from file "u-boot" at
.text_addr = 0xfd0000
(y or n) y
Reading symbols from u-boot...done.
(gdb) b board_init_r
Breakpoint 2 at 0xfd99ac: file board.c, line 533.
(gdb) c
Continuing.
Breakpoint 2, board_init_r (id=0xfbb1f0, dest_addr=16495088) at board.c:533
533 {
(gdb)
board_init_r is the first C routine running in the newly relocated C friendly
RAM environment.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
24. The 10 Golden Rules of U-Boot
design
1. Keep it Small
2. Keep it Fast
3. Keep it Simple
4. Keep it Portable
5. Keep it Configurable
6. Keep it Debug-able
7. Keep it Usable
8. Keep it Maintainable
9. Keep it Beautiful
10.Keep it Open
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
25. At the end…
• how to write optimized u-boot code ?
Use inline functions whenever possible ? it increases code size but saves u the
overhead of stack push & pop operations
Code all critical / frequently used code in assembly
Store frequently used variables in register
Use global variables it saves you push/pop stack operation during function
call.
Don't use floating point calculations / variables.
Enable GCC Optimization.
-O1 = With this option the resulting executables should be smaller and
faster than with -O0
-O2 = This option is generally the best choice for deployment of a program
-O3 = This option may increase the speed of the resulting executable, but
can also increase its size
-Os = his option selects optimizations which reduce the size of an
executable.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
26. At the end…
• Why U-boot relocate itself into RAM ?
– Memory access is faster from RAM than from ROM, this will matter
particularly if target has no instruction cache.
– Executing from RAM allows flash reprogramming; also (more minor) it
allows software breakpoints with "trap" instructions.
– Variable cannot be modified when it is stored in ROM.
• Boot loader must copy itself to RAM to update the variables correctly.
• Function calls relies on stack operation which is also should be stored
in RAM.
• Can U-Boot be configured such that it can be started in RAM?
– Yes, By enabling the CONFIG_SYS_RAMBOOT.
– But this feature is not tested with all the boards, refer
doc/README.mpc85xx.
– For u-boot builds switch CONFIG_SYS_TEXT_BASE between flash and ram
builds.
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173
27. At the end…
• Can U-Boot be configured such that it can be started in RAM?
– Yes, By enabling the CONFIG_SYS_RAMBOOT.
– But this feature is not tested with all the boards, refer
doc/README.mpc85xx.
– For u-boot builds switch CONFIG_SYS_TEXT_BASE between flash and ram
builds if MMU is disabled.
• Why Linux hangs after uncompressing the kernel ?
Uncompressing Kernel Image ... OK
– Bad definition of the bd_info structure .
– Bad clock information
• setenv clocks_in_mhz 1; saveenv
• For more: Read U-Boot FAQ at http://www.denx.de/wiki/view/DULG/FaqUBoot
E-mail: info@wavedigitech.com; http://www.wavedigitech.com Phone : 91-9632839173