歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> u-boot ctr0.S詳解 包含_main函數

u-boot ctr0.S詳解 包含_main函數

日期:2017/3/1 9:17:38   编辑:Linux編程
/** ****************************************************************************** * @author Maoxiao Hu * @version V1.0.0 * @date Jan-2015 ****************************************************************************** * < COPYRIGHT 2015 ISE of SHANDONG UNIVERSITY > ****************************************************************************** **/ based on u-boot-2014.10。源代碼以紅色標示,其它均為添加的注釋。

------------腦補區---------------

本文匯編涉及的指令:ldr , bic , mov , sub , cmp , strlo , addlo , blo , str , bl 。 不是很清楚的請到我的另外一篇博客《ARM匯編指令總結》中集中腦補。

----------------------------------

分析一下 crt0.S 文件開頭的一段。crt0.S 位於u-boot-2014.10/arch/arm/lib/crt0.S。 正如 crt0.S 文件開頭所注釋的一樣: “ 1. Set up initial environment for calling board_init_f().This environment only provides a stack and a place to store the GD (‘global data’) structure, both located in some readily available RAM (SRAM, locked cache...). In this context, VARIABLE global data, initialized or not (BSS), are UNAVAILABLE; only CONSTANT initialized data are available. ” 最開始先為調用 board_init_f() 設定初始化條件。這個初始化條件就是:設定棧首地址,將棧往下移 GD_SIZE 個大小,然後將剛才經過的這段 SRAM 從頭清空(寫0)。話不多說看代碼:

ENTRY(_main)

/*
* Set up initial C runtime environment and call board_init_f(0).
*/

ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)

一路反查定義:

#define CONFIG_SYS_INIT_SP_ADDR (CONFIG_SYS_LOAD_ADDR - GENERATED_GBL_DATA_SIZE) #define CONFIG_SYS_LOAD_ADDR (CONFIG_SYS_SDRAM_BASE + 0x4800000) #define GENERATED_GBL_DATA_SIZE 0xC0 #define CONFIG_SYS_INIT_SP_ADDR (0x40000000 + 0x4800000 - 0xC0) #define CONFIG_SYS_INIT_SP_ADDR 0x447FFF40

最終SP值為0x447FFF40。

bic sp, sp, #7 /* 8-byte alignment for ABI compliance */

sp低三位清0。

mov r2, sp

保存sp到r2中,現在r2中的值也是0x447FFF40。

sub sp, sp, #GD_SIZE /* allocate one GD above SP */

GD_SIZE為184(0xB8),將sp指針向下移184個字節,現在sp為0x447ffe88。

bic sp, sp, #7 /* 8-byte alignment for ABI compliance */

sp低三位再次清0。

mov r9, sp /* GD is above SP */

保存sp到r9中,現在r9中的值也是0x447ffe88。

mov r1, sp

再次保存sp到r1中,現在r1中的值也是0x447ffe88。

mov r0, #0

清r0。

clr_gd:

cmp r1, r2 /* while not at end of GD */

現在 r1=0x447ffe88 < r2=0x447FFF40不相等是必然的。

strlo r0, [r1] /* clear 32-bit GD word */


addlo r1, r1, #4 /* move to next */


blo clr_gd

blo小於則跳轉。


#if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SPL_BUILD)
sub sp, sp, #CONFIG_SYS_MALLOC_F_LEN

CONFIG_SYS_MALLOC_F_LEN=(1<<10)即0x400,現在sp為0x447ffa88。


str sp, [r9, #GD_MALLOC_BASE]

GD_MALLOC_BASE = 136 (0x88),把sp的值存到r9+ 0x88 = 0x447fff10 地址處。

#endif
/* mov r0, #0 not needed due to above code */
bl board_init_f

U-Boot源代碼下載地址 http://www.linuxidc.com/Linux/2011-07/38897.htm

Copyright © Linux教程網 All Rights Reserved