U-Boot Stage2

來源:互聯網
上載者:User

start_armboot函數在lib_arm/board.c中定義,是U-Boot第二階段代碼的入口。U-Boot啟動第二階段流程如下:

 

 U-Boot第二階段執行流程

1)gd_t結構體

U-Boot使用了一個結構體gd_t來儲存全域資料區的資料,這個結構體在include/asm-arm/global_data.h中定義如下:

typedef struct global_data {

bd_t *bd;

unsigned long flags;

unsigned long baudrate;

unsigned long have_console; /* serial_init() was called */

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 */

void **jt; /* jump table */

} gd_t;

U-Boot使用了一個儲存在寄存器中的指標gd來記錄全域資料區的地址:

#define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("r8")

DECLARE_GLOBAL_DATA_PTR定義一個gd_t全域資料結構的指標,這個指標存放在指定的寄存器r8中。這個聲明也避免編譯器把r8分配給其它的變數。任何想要訪問全域資料區的代碼,只要代碼開頭加入“DECLARE_GLOBAL_DATA_PTR”一行代碼,然後就可以使用gd指標來訪問全域資料區了。

根據U-Boot記憶體使用量圖中可以計算gd的值:

gd = TEXT_BASE -CONFIG_SYS_MALLOC_LEN - sizeof(gd_t)

(2)bd_t結構體

bd_t在include/asm-arm.u/u-boot.h中定義如下:

typedef struct bd_info {

int bi_baudrate; /* 串口通訊傳輸速率 */

unsigned long bi_ip_addr; /* IP 位址*/

struct environment_s *bi_env; /* 環境變數開始地址 */

ulong bi_arch_number; /* 開發板的機器碼 */

ulong bi_boot_params; /* 核心參數的開始地址 */

struct /* RAM配置資訊 */

{

ulong start;

ulong size;

}bi_dram[CONFIG_NR_DRAM_BANKS];

} bd_t;

U-Boot啟動核心時要給核心傳遞參數,這時就要使用gd_t,bd_t結構體中的資訊來設定標記列表。

(3)init_sequence數組

U-Boot使用一個數組init_sequence來儲存對於大多數開發板都要執行的初始化函數的函數指標。init_sequence數組中有較多的編譯選項,去掉編譯選項後init_sequence數組如下所示:

typedef int (init_fnc_t) (void);

init_fnc_t *init_sequence[] = {

board_init, /*開發板相關的配置--board/samsung/mini2440/mini2440.c */

timer_init, /* 時鐘初始化-- cpu/arm920t/s3c24x0/timer.c */

env_init, /*初始化環境變數--common/env_flash.c 或common/env_nand.c*/

init_baudrate, /*初始化傳輸速率-- lib_arm/board.c */

serial_init, /* 串口初始化-- drivers/serial/serial_s3c24x0.c */

console_init_f, /* 控制通訊台初始化階段1-- common/console.c */

display_banner, /*列印U-Boot版本、編譯的時間-- gedit lib_arm/board.c */

dram_init, /*配置可用的RAM-- board/samsung/mini2440/mini2440.c */

display_dram_config, /* 顯示RAM大小-- lib_arm/board.c */

NULL,

};

其中的board_init函數在board/samsung/mini2440/mini2440.c中定義,該函數設定了MPLLCOM,UPLLCON,以及一些GPIO寄存器的值,還設定了U-Boot機器碼和核心啟動參數地址:

/* MINI2440開發板的機器碼*/

gd->bd->bi_arch_number = MACH_TYPE_MINI2440;

/* 核心啟動參數地址*/

gd->bd->bi_boot_params = 0x30000100;

其中的dram_init函數在board/samsung/mini2440/mini2440.c中定義如下:

int dram_init (void)

{

/* 由於mini2440隻有*/

gd->bd->bi_dram[0].start = PHYS_SDRAM_1;

gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;

return 0;

}

mini2440使用2片32MB的SDRAM組成了64MB的記憶體,接在儲存控制器的BANK6,地址空間是0x30000000~0x34000000。

在include/configs/mini2440.h中PHYS_SDRAM_1和PHYS_SDRAM_1_SIZE 分別被定義為0x30000000和0x04000000(64M)。

分析完上述的資料結構,下面來分析start_armboot函數:

void start_armboot (void)

{

init_fnc_t **init_fnc_ptr;

char *s;

… …

/* 計算全域資料結構的地址gd */

gd = (gd_t*)(_armboot_start - CONFIG_SYS_MALLOC_LEN - sizeof(gd_t));

… …

memset ((void*)gd, 0, sizeof (gd_t));

gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));

memset (gd->bd, 0, sizeof (bd_t));

gd->flags |= GD_FLG_RELOC;

monitor_flash_len = _bss_start - _armboot_start;

/* 逐個調用init_sequence數組中的初始化函數 */

for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {

if ((*init_fnc_ptr)() != 0) {

hang ();

}

}

/* armboot_start 在cpu/arm920t/start.S 中被初始化為u-boot.lds串連指令碼中的_start */

mem_malloc_init (_armboot_start - CONFIG_SYS_MALLOC_LEN,

CONFIG_SYS_MALLOC_LEN);

/* NOR Flash初始化 */

#ifndef CONFIG_SYS_NO_FLASH

/* configure available FLASH banks */

display_flash_config (flash_init ());

#endif /* CONFIG_SYS_NO_FLASH */

… …

/* NAND Flash 初始化*/

#if defined(CONFIG_CMD_NAND)

puts ("NAND: ");

nand_init(); /* go init the NAND */

#endif

… …

/*配置環境變數,重新置放 */

env_relocate ();

… …

/* 從環境變數中擷取IP地址 */

gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");

stdio_init (); /* get the devices list going. */

jumptable_init ();

… …

console_init_r (); /* fully init console as a device */

… …

/* enable exceptions */

enable_interrupts ();

#ifdef CONFIG_USB_DEVICE

usb_init_slave();

#endif

/* Initialize from environment */

if ((s = getenv ("loadaddr")) != NULL) {

load_addr = simple_strtoul (s, NULL, 16);

}

#if defined(CONFIG_CMD_NET)

if ((s = getenv ("bootfile")) != NULL) {

copy_filename (BootFile, s, sizeof (BootFile));

}

#endif

… …

/* 網卡初始化 */

#if defined(CONFIG_CMD_NET)

#if defined(CONFIG_NET_MULTI)

puts ("Net: ");

#endif

eth_initialize(gd->bd);

… …

#endif

/* main_loop() can return to retry autoboot, if so just run it again. */

for (;;) {

main_loop ();

}

/* NOTREACHED - no way out of command loop except booting */

}

main_loop函數在common/main.c中定義。一般情況下,進入main_loop函數若干秒內沒有

1.1.3 U-Boot啟動Linux過程(在Arm-kernel
記憶體收集中有詳細解釋)

U-Boot使用標記列表(tagged list)的方式向Linux傳遞參數。標記的資料結構式是tag,在U-Boot原始碼目錄include/asm-arm/setup.h中定義如下:

struct tag_header {

u32 size; /* 表示tag資料結構的聯合u實質存放的資料的大小*/

u32 tag; /* 表示標記的類型 */

};

struct tag {

struct tag_header hdr;

union {

struct tag_core core;

struct tag_mem32 mem;

struct tag_videotext videotext;

struct tag_ramdisk ramdisk;

struct tag_initrd initrd;

struct tag_serialnr serialnr;

struct tag_revision revision;

struct tag_videolfb videolfb;

struct tag_cmdline cmdline;

/*

* Acorn specific

*/

struct tag_acorn acorn;

/*

* DC21285 specific

*/

struct tag_memclk memclk;

} u;

};

U-Boot使用命令bootm來啟動已經載入到記憶體中的核心。而bootm命令實際上調用的是do_bootm函數。對於Linux核心,do_bootm函數會調用do_bootm_linux函數來設定標記列表和啟動核心。do_bootm_linux函數在lib_arm/bootm.c 中定義如下:

59 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)

60 {

61 bd_t *bd = gd->bd;

62 char *s;

63 int machid = bd->bi_arch_number;

64 void (*theKernel)(int zero, int arch, uint params);

65

66 #ifdef CONFIG_CMDLINE_TAG

67 char *commandline = getenv ("bootargs"); /* U-Boot環境變數bootargs */

68 #endif

… …

73 theKernel = (void (*)(int, int, uint))images->ep;/* 擷取核心入口地址 */

… …

86 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \

87 defined (CONFIG_CMDLINE_TAG) || \

88 defined (CONFIG_INITRD_TAG) || \

89 defined (CONFIG_SERIAL_TAG) || \

90 defined (CONFIG_REVISION_TAG) || \

91 defined (CONFIG_LCD) || \

92 defined (CONFIG_VFD)

93 setup_start_tag (bd); /* 設定ATAG_CORE標誌 */

… …

100 #ifdef CONFIG_SETUP_MEMORY_TAGS

101 setup_memory_tags (bd); /* 設定記憶體標記 */

102 #endif

103 #ifdef CONFIG_CMDLINE_TAG

104 setup_commandline_tag (bd, commandline); /* 設定命令列標記 */

105 #endif

… …

113 setup_end_tag (bd); /* 設定ATAG_NONE標誌 */

114 #endif

115

116 /* we assume that the kernel is in place */

117 printf ("\nStarting kernel ...\n\n");

… …

126 cleanup_before_linux (); /* 啟動核心前對CPU作最後的設定 */

127

128 theKernel (0, machid, bd->bi_boot_params); /* 調用核心 */

129 /* does not return */

130

131 return 1;

132 }

其中的setup_start_tag,setup_memory_tags,setup_end_tag函數在lib_arm/bootm.c中定義如下:

(1)setup_start_tag函數

static void setup_start_tag (bd_t *bd)

{

params = (struct tag *) bd->bi_boot_params; /* 核心的參數的開始地址 */

params->hdr.tag = ATAG_CORE;

params->hdr.size = tag_size (tag_core);

params->u.core.flags = 0;

params->u.core.pagesize = 0;

params->u.core.rootdev = 0;

params = tag_next (params);

}

標記列表必須以ATAG_CORE開始,setup_start_tag函數在核心的參數的開始地址設定了一個ATAG_CORE標記。

(2)setup_memory_tags函數

static void setup_memory_tags (bd_t *bd)

{

int i;

/*設定一個記憶體標記 */

for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {

params->hdr.tag = ATAG_MEM;

params->hdr.size = tag_size (tag_mem32);

params->u.mem.start = bd->bi_dram[i].start;

params->u.mem.size = bd->bi_dram[i].size;

params = tag_next (params);

}

}

setup_memory_tags函數設定了一個ATAG_MEM標記,該標記包含記憶體起始地址,記憶體大小這兩個參數。

(3)setup_end_tag函數

static void setup_end_tag (bd_t *bd)

{

params->hdr.tag = ATAG_NONE;

params->hdr.size = 0;

}

標記列表必須以標記ATAG_NONE結束,setup_end_tag函數設定了一個ATAG_NONE標記,表示標記列表的結束。

U-Boot設定好標記列表後就要調用核心了。但調用核心前,CPU必須滿足下面的條件:

(1) CPU寄存器的設定

Ø r0=0

Ø r1=機器碼

Ø r2=核心參數標記列表在RAM中的起始地址

(2) CPU工作模式

Ø 禁止IRQ與FIQ中斷

Ø CPU為SVC模式

(3) 使資料Cache與指令Cache失效

do_bootm_linux中調用的cleanup_before_linux函數完成了禁止中斷和使Cache失效的功能。cleanup_before_linux函數在cpu/arm920t/cpu.中定義:

int cleanup_before_linux (void)

{

/*

* this function is called just before we call linux

* it prepares the processor for linux

*

* we turn off caches etc ...

*/

disable_interrupts (); /* 禁止FIQ/IRQ中斷 */

/* turn off I/D-cache */

icache_disable(); /* 使指令Cache失效 */

dcache_disable(); /* 使資料Cache失效 */

/* flush I/D-cache */

cache_flush(); /* 重新整理Cache */

return 0;

}

由於U-Boot啟動以來就一直工作在SVC模式,因此CPU的工作模式就無需設定了。

do_bootm_linux中:

64 void (*theKernel)(int zero, int arch, uint params);

… …

73 theKernel = (void (*)(int, int, uint))images->ep;

… …

128 theKernel (0, machid, bd->bi_boot_params);

第73行代碼將核心的入口地址“images->ep”強制類型轉換為函數指標。根據ATPCS規則,函數的參數個數不超過4個時,使用r0~r3這4個寄存器來傳遞參數。因此第128行的函數調用則會將0放入r0,機器碼machid放入r1,核心參數地址bd->bi_boot_params放入r2,從而完成了寄存器的設定,最後轉到核心的入口地址。

到這裡,U-Boot的工作就結束了,系統跳轉到Linux核心代碼執行。

1.1.4 U-Boot添加命令的方法及U-Boot命令執行過程

下面以添加menu命令(啟動菜單)為例講解U-Boot添加命令的方法。

(1) 建立common/cmd_menu.c

習慣上通用命令原始碼放在common目錄下,與開發板專有命令原始碼則放在board/<board_dir>目錄下,並且習慣以“cmd_<命令名>.c”為檔案名稱。

(2) 定義“menu”命令

在cmd_menu.c中使用如下的代碼定義“menu”命令:

_BOOT_CMD(

menu, 3, 0, do_menu,

"menu - display a menu, to select the items to do something\n",

" - display a menu, to select the items to do something"

);

其中U_BOOT_CMD命令格式如下:

U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)

各個參數的意義如下:

name:命令名,非字串,但在U_BOOT_CMD中用“#”符號轉化為字串

maxargs:命令的最大參數個數

rep:是否自動重複(按Enter鍵是否會重複執行)

cmd:該命令對應的響應函數

usage:簡短的使用說明(字串)

help:較詳細的使用說明(字串)

在記憶體中儲存命令的help欄位會佔用一定的記憶體,通過配置U-Boot可以選擇是否儲存help欄位。若在include/configs/mini2440.h中定義了CONFIG_SYS_LONGHELP宏,則在U-Boot中使用help命令查看某個命令的協助資訊時將顯示usage和help欄位的內容,否則就只顯示usage欄位的內容。

U_BOOT_CMD宏在include/command.h中定義:

#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}

“##”與“#”都是先行編譯操作符,“##”有字串串連的功能,“#”表示後面緊接著的是一個字串。

其中的cmd_tbl_t在include/command.h中定義如下:

struct cmd_tbl_s {

char *name; /* 命令名*/

int maxargs; /* 最大參數個數*/

int repeatable; /* 是否自動重複*/

int (*cmd)(struct cmd_tbl_s *, int, int, char *[]); /* 響應函數*/

char *usage; /* 簡短的協助資訊*/

#ifdef CONFIG_SYS_LONGHELP

char *help; /* 較詳細的協助資訊*/

#endif

#ifdef CONFIG_AUTO_COMPLETE

/* 自動補全參數*/

int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);

#endif

};

typedef struct cmd_tbl_s cmd_tbl_t;

一個cmd_tbl_t結構體變數包含了調用一條命令的所需要的資訊。

其中Struct_Section在include/command.h中定義如下:

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

凡是帶有__attribute__ ((unused,section (".u_boot_cmd"))屬性聲明的變數都將被存放在".u_boot_cmd"段中,並且即使該變數沒有在代碼中顯式的使用編譯器也不產生警告資訊。

在U-Boot串連指令碼u-boot.lds中定義了".u_boot_cmd"段:

. = .;

__u_boot_cmd_start = .; /*將 __u_boot_cmd_start指定為當前地址 */

.u_boot_cmd : { *(.u_boot_cmd) }

__u_boot_cmd_end = .; /* 將__u_boot_cmd_end指定為當前地址 */

這表明帶有“.u_boot_cmd”聲明的函數或變數將儲存在“u_boot_cmd”段。這樣只要將U-Boot所有命令對應的cmd_tbl_t變數加上“.u_boot_cmd”聲明,編譯器就會自動將其放在“u_boot_cmd”段,尋找cmd_tbl_t變數時只要在__u_boot_cmd_start與__u_boot_cmd_end之間尋找就可以了。

因此“menu”命令的定義經過宏展開後如下:

cmd_tbl_t __u_boot_cmd_menu __attribute__ ((unused,section (".u_boot_cmd"))) = {menu, 3, 0, do_menu, "menu - display a menu, to select the items to do something\n", " - display a menu, to select the items to do something"}

實質上就是用U_BOOT_CMD宏定義的資訊構造了一個cmd_tbl_t類型的結構體。編譯器將該結構體放在“u_boot_cmd”段,執行命令時就可以在“u_boot_cmd”段尋找到對應的cmd_tbl_t類型結構體。

(3) 實現命令的函數

在cmd_menu.c中添加“menu”命令的響應函數的實現。具體的實現代碼略:

int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])

{

/* 實現代碼略 */

}

(4) 將common/cmd_menu.c編譯進u-boot.bin

在common/Makefile中加入如下代碼:

COBJS-$(CONFIG_BOOT_MENU) += cmd_menu.o

在include/configs/mini2440.h加入如代碼:

#define CONFIG_BOOT_MENU 1

重新編譯下載U-Boot就可以使用menu命令了

(5)menu命令執行的過程

在U-Boot中輸入“menu”命令執行時,U-Boot接收輸入的字串“menu”,傳遞給run_command函數。run_command函數調用common/command.c中實現的find_cmd函數在__u_boot_cmd_start與__u_boot_cmd_end間尋找命令,並返回menu命令的cmd_tbl_t結構。然後run_command函數使用返回的cmd_tbl_t結構中的函數指標調用menu命令的響應函數do_menu,從而完成了命令的執行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.