uboot 启动流程【2】

arch/arm/lib/crt0_64.S

crt0是C-runtime Startup Code的简称,意思就是运行C代码之前的准备工作

/*
18 * This file handles the target-independent stages of the U-Boot
19 * start-up where a C runtime environment is needed. Its entry point
20 * is _main and is branched into from the target's start.S file.
21 *
22 * _main execution sequence is:
23 *
24 * 1. Set up initial environment for calling board_init_f().
25 *    This environment only provides a stack and a place to store
26 *    the GD ('global data') structure, both located in some readily
27 *    available RAM (SRAM, locked cache...). In this context, VARIABLE
28 *    global data, initialized or not (BSS), are UNAVAILABLE; only
29 *    CONSTANT initialized data are available. GD should be zeroed
30 *    before board_init_f() is called.
31 *
32 * 2. Call board_init_f(). This function prepares the hardware for
33 *    execution from system RAM (DRAM, DDR...) As system RAM may not
34 *    be available yet, , board_init_f() must use the current GD to
35 *    store any data which must be passed on to later stages. These
36 *    data include the relocation destination, the future stack, and
37 *    the future GD location.
38 *
39 * 3. Set up intermediate environment where the stack and GD are the
40 *    ones allocated by board_init_f() in system RAM, but BSS and
41 *    initialized non-const data are still not available.
42 *
43 * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
44 *    relocates U-Boot from its current location into the relocation
45 *    destination computed by board_init_f().
46 *
47 * 4b.For SPL, board_init_f() just returns (to crt0). There is no
48 *    code relocation in SPL.
49 *
50 * 5. Set up final environment for calling board_init_r(). This
51 *    environment has BSS (initialized to 0), initialized non-const
52 *    data (initialized to their intended value), and stack in system
53 *    RAM (for SPL moving the stack and GD into RAM is optional - see
54 *    CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
55 *
56 * TODO: For SPL, implement stack relocation on AArch64.
57 *
58 * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
59 *    at this point regarding memory, so call c_runtime_cpu_setup.
60 *
61 * 7. Branch to board_init_r().
62 *
63 * For more information see 'Board Initialisation Flow in README.
64 */
65
66ENTRY(_main)
67
68/*
69 * Set up initial C runtime environment and call board_init_f(0).
70 */
71#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
72	ldr	x0, =(CONFIG_TPL_STACK)
73#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
74	ldr	x0, =(CONFIG_SPL_STACK)
75#elif defined(CONFIG_INIT_SP_RELATIVE)
76#if CONFIG_POSITION_INDEPENDENT
77	adrp	x0, __bss_start     /* x0 <- Runtime &__bss_start */
78	add	x0, x0, #:lo12:__bss_start
79#else
80	adr	x0, __bss_start
81#endif
82	add	x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET
83#else
84	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
85#endif
86	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
87	mov	x0, sp
88	bl	board_init_f_alloc_reserve
89	mov	sp, x0
90	/* set up gd here, outside any C code */
91	mov	x18, x0
92	bl	board_init_f_init_reserve
93
94#if defined(CONFIG_DEBUG_UART)
95	bl	debug_uart_init
96#endif
97
98	mov	x0, #0
99	bl	board_init_f
100
101#if !defined(CONFIG_SPL_BUILD)
102/*
103 * Set up intermediate environment (new sp and gd) and call
104 * relocate_code(addr_moni). Trick here is that we'll return
105 * 'here' but relocated.
106 */
107	ldr	x0, [x18, #GD_START_ADDR_SP]	/* x0 <- gd->start_addr_sp */
108	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
109	ldr	x18, [x18, #GD_NEW_GD]		/* x18 <- gd->new_gd */
110
111	adr	lr, relocation_return
112#if CONFIG_POSITION_INDEPENDENT
113	/* Add in link-vs-runtime offset */
114	adrp	x0, _start		/* x0 <- Runtime value of _start */
115	add	x0, x0, #:lo12:_start
116	ldr	x9, _TEXT_BASE		/* x9 <- Linked value of _start */
117	sub	x9, x9, x0		/* x9 <- Run-vs-link offset */
118	add	lr, lr, x9
119#endif
120	/* Add in link-vs-relocation offset */
121	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
122	add	lr, lr, x9	/* new return address after relocation */
123	ldr	x0, [x18, #GD_RELOCADDR]	/* x0 <- gd->relocaddr */
124	b	relocate_code
125
126relocation_return:
127
128/*
129 * Set up final (full) environment
130 */
131	bl	c_runtime_cpu_setup		/* still call old routine */
132#endif /* !CONFIG_SPL_BUILD */
133#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(FRAMEWORK)
134#if defined(CONFIG_SPL_BUILD)
135	bl	spl_relocate_stack_gd           /* may return NULL */
136	/* set up gd here, outside any C code, if new stack is returned */
137	cmp	x0, #0
138	csel	x18, x0, x18, ne
139	/*
140	 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
141	 * around the constraint that conditional moves can not
142	 * have 'sp' as an operand
143	 */
144	mov	x1, sp
145	cmp	x0, #0
146	csel	x0, x0, x1, ne
147	mov	sp, x0
148#endif
149
150/*
151 * Clear BSS section
152 */
153	ldr	x0, =__bss_start		/* this is auto-relocated! */
154	ldr	x1, =__bss_end			/* this is auto-relocated! */
155clear_loop:
156	str	xzr, [x0], #8
157	cmp	x0, x1
158	b.lo	clear_loop
159
160	/* call board_init_r(gd_t *id, ulong dest_addr) */
161	mov	x0, x18				/* gd_t */
162	ldr	x1, [x18, #GD_RELOCADDR]	/* dest_addr */
163	b	board_init_r			/* PC relative jump */
164
165	/* NOTREACHED - board_init_r() does not return */
166#endif
167
168ENDPROC(_main)
169

功能可总结为(大部分翻译自crt0_64.S中的注释):

1)设置C代码的运行环境,为调用board_init_f接口做准备。包括:

a)设置堆栈(C代码的函数调用,堆栈是必须的)。如果当前的编译是SPL(由CONFIG_SPL_BUILD定义),可单独定义堆栈基址(CONFIG_SPL_STACK),否则,通过CONFIG_SYS_INIT_SP_ADDR定义堆栈基址。

b)调用board_init_f_alloc_reserve接口,从堆栈开始的地方,为u-boot中大名鼎鼎的GD ('global data') 数据结构,分配空间。

c)调用board_init_f_init_reserve
接口,对GD进行初始化。

2)调用board_init_f函数,完成一些前期的初始化工作,例如:

a)点亮一个Debug用的LED灯,表示u-boot已经活了。

b)初始化DRAM、DDR等system范围的RAM等。

c)计算后续代码需要使用的一些参数,包括relocation destination、future stack、future GD location等。

注5:关于u-boot的relocation操作,后续会有专门的文章介绍。

3)如果当前是SPL(由CONFIG_SPL_BUILD控制),则_main函数结束,直接返回。如果是正常的u-boot,则继续执行后续的动作。

4)根据board_init_f指定的参数,执行u-boot的relocation操作。

5)清除BBS段。

6)调用board_init_r函数,执行后续的初始化操作(已经不再本文的讨论范围了,具体请参考后续的分析文章)。

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打个工而已

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值