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

/**
******************************************************************************
* @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)html

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

ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)

一路反查定義:this

#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。spa

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 地址處。

NewImage

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

相關文章
相關標籤/搜索