u-boot presentation 2013

28
Universal Boot loaders (U-boot) © Copyright 2013, wavedigitech.com. Latest update: June 15, 2013, http://www.wavedigitech.com/ Call us on 91-963283917 E-Mail : [email protected] E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Upload: wave-digitech

Post on 22-May-2015

5.410 views

Category:

Technology


3 download

DESCRIPTION

U-Boot, a boot loader for Embedded boards based on PowerPC, ARM, MIPS and several other processors, which can be installed in a boot ROM and used to initialize and test the hardware or to download and run application code.

TRANSCRIPT

Page 1: U-Boot presentation  2013

Universal Boot loaders (U-boot)

© Copyright 2013, wavedigitech.com.

Latest update: June 15, 2013,http://www.wavedigitech.com/

Call us on 91-963283917E-Mail : [email protected]

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 2: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 3: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 4: U-Boot presentation  2013

Boot Loader Stages

What is the responsibility of Boot-ROM, First, second stage boot loader ?

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 5: U-Boot presentation  2013

U-boot Porting

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 6: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 7: U-Boot presentation  2013

U-Boot Directory Structure

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 8: U-Boot presentation  2013

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

U-Boot Directory Structure

Page 9: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 10: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 11: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 12: U-Boot presentation  2013

U-boot : Linker script

OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")OUTPUT_ARCH(arm)ENTRY(_start)SECTIONS{ . = 0x00000000; . = ALIGN(4); .text : { __image_copy_start = .; CPUDIR/start.o (.text*) *(.text*) } /* Align */ .rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) } .data : {/* Align */ *(.data*) } /* Align */ .u_boot_list : { KEEP(*(SORT(.u_boot_list*))); } /* Align */ __image_copy_end = .;

arch/arm/cpu/u-boot.lds

.dynsym : { __dynsym_start = .; *(.dynsym) } _end = .; /* * Compiler-generated __bss_start and __bss_end, see arch/arm/lib/bss.c * __bss_base and __bss_limit are for linker only (overlay ordering) */ .bss_start __rel_dyn_start (OVERLAY) : { KEEP(*(.__bss_start)); __bss_base = .; } .bss __bss_base (OVERLAY) : { *(.bss*) . = ALIGN(4); __bss_limit = .; } .bss_end __bss_limit (OVERLAY) : { KEEP(*(.__bss_end)); }}

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 13: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 14: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 15: U-Boot presentation  2013

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 data8. 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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 16: U-Boot presentation  2013

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 toFinal 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/start.SReset()

Reset CPU and Hardware

cpu/armv7/start.S_start()

arm/lib/crt0.S_main()

Early Board Setup

arm/lib/crt0.S

board_init_f()

arm/lib/crt0.S

board_init_r()

cpu/armv7/shmobile/lowlevel_init.Slowlevel_init()

cpu/armv7/start.S

relocate_code()

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 17: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 18: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 19: U-Boot presentation  2013

Bootstrapping process

arm/lib/board.cboard_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/loganlte/loganlte.cboard_init()

board/rmi/loganlte/loganlte.c

enable_cache()

Setup late Board Data

Early arch/platform specific misc

initializationsInitialize and

Enable exceptions

Early arch specific & power

management specific

initialization

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 20: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 21: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 22: U-Boot presentation  2013

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:2001Remote debugging using bdi:20010xfffffffc in ?? ()(gdb) b cpu_init_fBreakpoint 1 at 0xfffd3310: file cpu_init.c, line 136.(gdb) cContinuing. Breakpoint 1, cpu_init_f () at cpu_init.c:136136 asm volatile(" bl 0f" ::: "lr");(gdb) s137 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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 23: U-Boot presentation  2013

Debugging after Relocationstruct 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-fileDiscard symbol table from `/home/dzu/denx/cvs-trees/u-boot/u-boot'? (y or n) yNo symbol file now.(gdb) add-symbol-file u-boot 0xfd0000add symbol table from file "u-boot" at .text_addr = 0xfd0000(y or n) yReading symbols from u-boot...done.(gdb) b board_init_rBreakpoint 2 at 0xfd99ac: file board.c, line 533.(gdb) cContinuing.

Breakpoint 2, board_init_r (id=0xfbb1f0, dest_addr=16495088) at board.c:533533 {(gdb)

board_init_r is the first C routine running in the newly relocated C friendly RAM environment.

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 24: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 25: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 26: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 27: U-Boot presentation  2013

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: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

Page 28: U-Boot presentation  2013

For more information visit http://www.denx.de/wiki/U-Boot

Any Question ?

E-mail: [email protected]; http://www.wavedigitech.com Phone : 91-9632839173

© Copyright 2013, wavedigitech.com.

Latest update: Janl 30, 2013,http://www.wavedigitech.com/

Call us on : +91-9632839173E-Mail : [email protected]

Thank You