u-boot启动流程分析之第二阶段

u-boot-1.1.6\lib_arm\board.c

----------------------------------
.globl _start
_start: b       reset

.globl _armboot_start
_armboot_start:
    .word _start
-------------------------------
_armboot_start是u-boot的起始地址

gd_t结构体和bd_t结构体

typedef struct  global_data {
    bd_t        *bd;
    unsigned long   flags;
    unsigned long   baudrate;
    unsigned long   have_console;   /* serial_init() was called */
    unsigned long   reloc_off;  /* Relocation Offset */
    unsigned long   env_addr;   /* Address  of Environment struct */
    unsigned long   env_valid;  /* Checksum of Environment valid? */
    unsigned long   fb_base;    /* base address of frame buffer */
#ifdef CONFIG_VFD
    unsigned char   vfd_type;   /* display type */
#endif
#if 0
    unsigned long   cpu_clk;    /* CPU clock in Hz!     */
    unsigned long   bus_clk;
    unsigned long   ram_size;   /* RAM size */
    unsigned long   reset_status;   /* reset status register at boot */
#endif
    void        **jt;       /* jump table */
} gd_t;

typedef struct bd_info {
    int         bi_baudrate;    /* serial console baudrate */
    unsigned long   bi_ip_addr; /* IP Address */
    unsigned char   bi_enetaddr[6]; /* Ethernet adress */
    struct environment_s           *bi_env;
    ulong           bi_arch_number; /* unique id for this board */
    ulong           bi_boot_params; /* where this board expects params */
    struct              /* RAM configuration */
    {
    ulong start;
    ulong size;
    }           bi_dram[CONFIG_NR_DRAM_BANKS];
#ifdef CONFIG_HAS_ETH1
    /* second onboard ethernet port */
    unsigned char   bi_enet1addr[6];
#endif
} bd_t;

CFG_MALLOC_LEN 长度是128K,u-boot是512K,

    gd = (gd_t*)(_armboot_start - CFG_MALLOC_LEN - sizeof(gd_t));
  //执行初始化列表函数
    for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
        if ((*init_fnc_ptr)() != 0) {
            hang ();
        }
    }
100ask24x0.c (board\100ask24x0)
    gd->bd->bi_arch_number = MACH_TYPE_S3C2440;
    gd->bd->bi_boot_params = 0x30000100;
bootcmd=nand read.jffs2 0x30007FC0 kernel; 
void main_loop (void)
{
  s = getenv ("bootcmd");
  bootm 0x30007FC0

  printf("Booting Linux ...\n");            
  run_command (s, 0);
}

查找命令的时候,
__u_boot_cmd_start和__u_boot_cmd_end中间的cmd段是怎么初始化的

--------------------------------------------------
链接脚本里面
    __u_boot_cmd_start = .;
    .u_boot_cmd : { *(.u_boot_cmd) }
    __u_boot_cmd_end = .; 
----------------------------------------------
cmd_tbl_t *find_cmd (const char *cmd)
{
    for (cmdtp = &__u_boot_cmd_start;
         cmdtp != &__u_boot_cmd_end;
         cmdtp++) {
        if (strncmp (cmd, cmdtp->name, len) == 0) {
            if (len == strlen (cmdtp->name))
                return cmdtp;   /* full match */

            cmdtp_temp = cmdtp; /* abbreviated command ? */
            n_found++;
        }
    }

}

.u_boot_cmd这个段是哪来的

#define Struct_Section  __attribute__ ((unused,section (".u_boot_cmd")))

bootm命令的定义

U_BOOT_CMD(
    bootm,  CFG_MAXARGS,    1,  do_bootm,
    "bootm   - boot application image from memory\n",
    "[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"
#ifdef CONFIG_OF_FLAT_TREE
    "\tWhen booting a Linux kernel which requires a flat device-tree\n"
    "\ta third argument is required which is the address of the of the\n"
    "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
    "\tuse a '-' for the second argument. If you do not pass a third\n"
    "\ta bd_info struct will be passed instead\n"
#endif
);

U_BOOT_CMD的定义

#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \
cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}

进行宏替换展开

  1. 字符串化(Stringification)
    在宏体中,如果宏参数前加个#,那么在宏体扩展的时候,宏参数会被扩展成字符串的形式。如:
#define WARN_IF(EXP) \
     do { if (EXP) \
             fprintf (stderr, "Warning: " #EXP "\n"); } \
     while (0)

WARN_IF (x == 0);会被扩展成:

do { if (x == 0)
    fprintf (stderr, "Warning: " "x == 0" "\n"); }
while (0);

bootm命令把kernel拷到加载地址运行,并告诉内核一些参数,然后在跳到kernel运行地址
1.设置启动参数

setup_start_tag (bd);

  char *commandline = getenv ("bootargs");  
setup_commandline_tag (bd, commandline);
  

2.跳到入口地址

    void (*theKernel)(int zero, int arch, uint params);
    theKernel = (void (*)(int, int, uint))ntohl(hdr->ih_ep);
    theKernel (0, bd->bi_arch_number, bd->bi_boot_params);

nand read.jffs2 0x30007FC0 kernel; 使用.jffs2是因为不需要页,块对齐

nand read[.jffs2]     - addr off|partition size
nand write[.jffs2]    - addr off|partiton size - read/write `size' bytes starting
    at offset `off' to/from memory address `addr'

flash上没有分区表

u-boot中如何添加自己的命令###

1.头文件参考别的cmd_xxx.c
2.定义自己的U_BOOT_CMD宏
3.实现U_BOOT_CMD宏中cmd函数
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("%s\n", argv[i]);        
    }
}

U_BOOT_CMD(
    hello,  CFG_MAXARGS,    1,  do_hello,
    "hello\n",
    "fsdfdsfs example!\n"
);

---0x34000000 sdram最大地址---
  存放的是u-boot
---_armboot_start u-boot起始地址---
---CFG_MALLOC_LEN---大小192K

你可能感兴趣的:(u-boot启动流程分析之第二阶段)