U-boot Analysis

Source: Internet
Author: User
4. Bootloader: u-boot.2009.08 analysis and Transplantation
4.1: The makefile in the root directory of U-boot is analyzed, and the uboot compilation sequence is shown as follows. The first file for compilation execution is CPU/$ (CPU)/start. o, and because it is based on


For the ARM920T architecture, analyze the source file of CPU/ARM920T/start. S.
# U-boot objects... order is important (I. e. Start must be first)
Objs = CPU/$ (CPU)/start. o
Objs: =$ (addprefix $ (OBJ), $ (objs ))


Libs = lib_generic/libgeneric.
Libs + = lib_generic/lzma/liblzma.
Libs + = lib_generic/lzo/liblzo.
Libs + = $ (shell if [-F board/$ (vendor)/common/makefile]; Then ECHO \
"Board/$ (vendor)/common/Lib $ (vendor). A"; FI)
Libs + = CPU/$ (CPU)/Lib $ (CPU).
Libs + = lib _ $ (ARCH)/Lib $ (ARCH).
Libs + = FS/... (.)
Libs + = net/Libnet.
Libs + = Disk/libdisk.
Libs + = Drivers/... (.)
Libs + = Common/libcommon.
Libs + = libfdt/libfdt.
Libs + = API/libapi.
Libs + = post/libpost.
Libs: =$ (addprefix $ (OBJ), $ (libs ))


4.2: Analyze CPU/ARM920T/start. S source file: According to the ARM architecture, the execution sequence of the program is that the Development Board is powered on, that is, the execution starts from the zero address. What is stored in the zero address is a reset exception interrupt.


Analysis shows that the program runs from the power-on as follows: Set the processor mode, disable the watchdog, disable the interrupt, set the division factor ratio, system initialization (flush I/D cache, disable MMU


Memory SDRAM initialization), relocation code (copying uboot code from flash to SDRAM), initialization stack, clearing BSS segment, jump to Stage 2 C language code entry function start_armboot


Run.
(1). globl _ start
_ Start: B start_code

(2) start_code:
/* Set the CPU to svc32 mode */
/* Turn off the watchdog */
/* Mask all irqs by setting all bits in the intmr-default */
/* Setup fclk: hclk: pclk */
BL cpu_init_crit/* do Sys-critical inits only at reboot */
# Ifndef config_skip_relocate_uboot
/* Relocate U-boot from nor flash to Ram */
/* Set up the stack */
/* Clear BSS */
/* Jump to second stage */
Ldr pc, _ start_armboot
_ Start_armboot:. Word start_armboot


4.3: analyze the start_armboot function in/lib_arm/board. C:
GD = (gd_t *) (_ armboot_start-config_sys_malloc_len-sizeof (gd_t ));
Memset (void *) Gd, 0, sizeof (gd_t); and so on. initialize the gd_t struct pointer GD and initialize it.


Typedef int (init_fnc_t) (void );
Init_fnc_t * init_sequence [] = {
# If defined (config_arch_cpu_init)
Arch_cpu_init,/* Basic arch CPU dependent setup */
# Endif
Board_init,/* Basic board dependent setup */
# If defined (config_use_irq)
Interrupt_init,/* set up exceptions */
# Endif
Timer_init,/* initialize timer */
Env_init,/* initialize environment */
Init_baudrate,/* initialze baudrate settings */
Serial_init,/* serial communications setup */
Lele_init_f,/* Stage 1 init of console */
Display_banner,/* say that we are here */
# If defined (config_display_cpuinfo)
Print_cpuinfo,/* Display CPU Info (and speed )*/
# Endif
# If defined (config_display_boardinfo)
Checkboard,/* display board info */
# Endif
# If defined (config_hard_i2c) | defined (config_soft_i2c)
Init_func_i2c,
# Endif
Dram_init,/* configure available Ram banks */
# If defined (config_cmd_pci) | defined (config_pci)
Arm_pci_init,
# Endif
Display_dram_config,
Null,
};


For (init_fnc_ptr = init_sequence; * init_fnc_ptr; ++ init_fnc_ptr ){
If (* init_fnc_ptr )()! = 0 ){
Hang ();
}
}, Access the member functions in the init_sequence array in sequence through a for loop to further complete initialization and related settings.


Nand_init ();/* Go init the NAND */to initialize the NAND Flash.
Serial_initialize ();, initialize the serial port.


/* Main_loop () can return to retry autoboot, if so just run it again .*/
For (;;){
Main_loop ();
}


/* Notreached-No Way Out of command loop completion t booting */, execute the main_loop function in an infinite for loop.


4.4: analyze the main_loop function in/common/Main. C: process the uboot command.
/*
* Main Loop for monitor command processing
*/
For (;;){
# Ifdef config_boot_retry_time
If (rc> = 0 ){
/* Saw enough of a valid command
* Restart the timeout.
*/
Reset_timeout ();
}
# Endif
Len = Readline (config_sys_prompt );


Flag = 0;/* assume no special flags for now */
If (LEN> 0)
Strcpy (lastcommand, console_buffer );
Else if (LEN = 0)
Flag | = pai_flag_repeat;
# Ifdef config_boot_retry_time
Else if (LEN =-2 ){
/*-2 means timed out, retry autoboot
*/
Puts ("\ ntimed out waiting for command \ n ");
# Ifdef config_reset_to_retry
/* Reinit board to run initialization code again */
Do_reset (null, 0, 0, null );
# Else
Return;/* retry autoboot */
# Endif
}
# Endif


If (LEN =-1)
Puts ("<interrupt> \ n ");
Else
Rc = run_command (lastcommand, flag );


If (RC <= 0 ){
/* Invalid command or not repeatable, forget it */
Lastcommand [0] = 0;
}
}

Parse the command parameters according to the input command format, execute the command (execute the run_command function), and find the run_command function in/commmon/Main. c Based on the cmd_tbl_s command table structure.


To the implementation function corresponding to the input command.
/*
* Monitor command table
*/


Struct rj_tbl_s {
Char * Name;/* command name */
Int maxargs;/* Maximum number of arguments */
Int repeatable;/* autorepeat allowed? */
/* Implementation function */
INT (* cmd) (struct cmd_tbl_s *, Int, Int, char * []);
Char * usage;/* usage message (short )*/
# Ifdef config_sys_longhelp
Char * help;/* help message (long )*/
# Endif
# Ifdef config_auto_complete
/* Do Auto completion on the arguments */
INT (* Complete) (INT argc, char * argv [], char last_char, int maxv, char * cmdv []);
# Endif
};
Typedef struct pai_tbl_scmd_tbl_t;
__Tbl_t * cmdtp;
/* OK-call function to do the command */
If (distinct TP-> cmd) (distinct TP, flag, argc, argv )! = 0 ){
Rc =-1;
} // At This Point, different implementation functions are executed based on different uboot commands.
4.5: The uboot command to start the Linux operating system is bootm, And the uboot bootm command implementation file is pai_bootm.c, so we will analyze the/common/cmd_bootm.c file: command implementation


The current format is as follows: the command is bootm, And the implementation function is the do_bootm function.
# Define struct_section _ attribute _ (unused, section (". u_boot_cmd ")))
# Ifdef config_sys_longhelp
# Define u_boot_cmd (name, maxargs, rep, CMD, usage, help )\
Pai_tbl_t _ u_boot_cmd _ # name struct_section = {# name, maxargs, rep, CMD, usage, help}
# Else/* No long help info */
# Define u_boot_cmd (name, maxargs, rep, CMD, usage, help )\
Pai_tbl_t _ u_boot_cmd _ # name struct_section = {# name, maxargs, rep, CMD, usage}
# Endif/* config_sys_longhelp */


U_boot_cmd (
Bootm, config_sys_maxargs, 1, do_bootm,
"Boot application image from memory ",
"[ADDR [arg...] \ n-boot application image stored in memory \ n"
"\ Tpassing arguments 'arg... '; when booting a Linux kernel, \ n"
"\ T 'arg 'can be the address of an initrd image \ n"
"\ Tbdt-OS specific bd_t processing \ n"
"\ Tshortline-OS specific command line processing/Setup \ n"
"\ Tprep-OS specific prep before relocation or go \ n"
"\ TGO-start OS"
);


4.6: analyze the do_bootm function in/common/cmd_bootm.c:
Static bootm_headers_t images;/* pointers to OS/initrd/FDT images */
Int do_bootm (pai_tbl_t * cmdtp, int flag, int argc, char * argv []);
Disable_interrupts ();
Usb_stop ();
Icache_disable ();
Dcache_disable ();
Ret = bootm_load_ OS (images. OS, & load_end, 1); // load the specific operating system
Images. OS. OS = ih_ OS _linux // if Linux is loaded, the following
Boot_ OS _fn * boot_fn;
Boot_fn = boot_ OS [images. OS. OS]; // The boot_fn Pointer Points to a specific type function in the boot_ OS array.
Boot_fn (0, argc, argv, & images); // call the do_bootm_linux Function
Boot_ OS _fn * boot_ OS [] = {
# Ifdef config_bootm_linux
[Ih_ OS _linux] = do_bootm_linux,
# Endif
......
}; // We can see that if the Linux system is loaded, the do_bootm_linux function is called. The do_bootm_linuxm file is in the/lib_arm/bootm. c file.


4.7: analyze the do_bootm_linux function in/lib_arm/bootm. C:
Void (* Thekernel) (INT zero, Int Arch, uint Params );
Thekernel = (void (*) (INT, Int, uint) images-> EP; // images-> EP (entry point)
Setup_start_tag (BD );
......
Setup_end_tag (BD );
Thekernel (0, machid, BD-> bi_boot_params); // At This Point, jump to the Linux kernel and start the execution.
/* Does not return */
Return 1;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.