1,u-booot启动源码分析
1.1,u-boot启动三个2
第一条指令位置(参考u-boot.map) arch/arm/cpu/armv7/start.S 里的 _start: b reset
设置为SVC模式 msr cpsr,r0
关闭MMU Cache cpu_init_cp15
基本硬件设备初始化 board/samsung/fs4412/lowlevel_init.S 的 lowlevel_init
关中断 看门狗(时钟 串口,flash,内存等的初始化时间可能比较长,这样的话,看门狗会使开发板不停的重启,所以在初始化硬件之前,先关闭MMU Cache,关中断 看门狗)
,初始化时钟 串口,flash,内存
自搬移到内存 relocate_code
(把u-boot剩下的部分搬到内存中,然后运行)
设置栈, IRQ stack frame
(C代码运行之前,必须先设置栈)
===================================================
准备进入C部分 bl _main ( 参u-boot.map )
大部分硬件初始化 arch\arm\lib\board.c\board_init_f 里的init_sequence
搬移内核到内存运行 common/main.c main_loop -> getenv ("bootcmd")
bootdelay >= 0 && s && !abortboot (bootdelay))
下的 run_command (bootcmd)
uboot是在开发板上的EMMC里面,开发板上电后,BootLoader运行,设置为SVC模式,关闭MMU Cache,基本硬件初始化,自搬移到内存(uboot剩余程序进内存),设置栈,准备进入C语言部分,大部分硬件初始化,搬移内核到内存(通过网络—tftp服务,将uImage和设备树文件加载到内存),挂载根文件系统(通过网络—nfs服务,将rootfs加载到内存)
1.2, 启动步骤(重点)
{//-----------------------------启动步骤(重点)-----------------------------
1.3,U-boot 启动源码分析
{//----------------------------U-boot 启动源码分析---------------------------
{//?上电系统从哪里开始,启动代码放在什么位置
上电后,有的系统可通过硬件管脚的电平来选择从Nandflash还是从Norflash启动。这里是指定从Nandflash启动的
启动代码U-boot 是不能放在RAM中的,因掉电就消失了。
}
{//?boot第一件是为何是设置为SVC模式
为安全性,CPU本身提出了多种模式来实现安全性,和效率兼顾。
SVC模式就是CPU对资源的一种保护。在普通用户模式是不能访问到的。只有切换到SVC模式才能访问。
}
{//?并关闭中断,MMU,看门狗等
初始时为安全性,把系统单纯化, 排除掉别的干扰,仅仍u-boot实现单纯的搬移代码,
的功能。 所以关掉中断,避免中断打断带来的保存返回的问题。 关掉MMU,因为u-boot软件是硬件实地址访问。
根本没有用到内存地址映射。 看门狗如果没关,硬件上默认到时会复位的。 所以要关掉
}
{//?为何设备初始引导不能用C代码,要用汇编代码
特殊指令,如操作协处理器, MMC CASH ,切换SVC模式, 等C不能做到。
另C是需要先准备栈的.
}
{//?U-BOOT为何要搬移到RAM中运行
在RAM中速度更快,但因掉电就没了,所以要保存在Nandflash或Norflash中。
如果搬移,直接在Norflash上运行也是可以的。 Nandflash因是按块操作的,不太合适直接运行。
}
{//?U-boot如何传递参数到内核。 移植时要注意哪些
方式一 采用param_struct: (我们现在使用的是这种方式) 见common/cmd_boot.c go命令的优化
方式二 采用taglist:
}
{//—u-boot 的启动步骤(重点)
//阶段一(汇编)
设置为SVC模式,关闭中断,MMU,看门狗 //准备
基本硬件设备初始化 //初始化时钟,串口,flash,内存 见cpu/arm_cortexa8/start.S 的 cpu_init_crit
自搬移到内存 //copy_uboot_to_ram 或relocate
设置好栈 //stack_setup
跳转到第二阶段代码入口 //ldr pc, _start_armboot
//阶段二(C语言)
大部分硬件初始化 //lib_arm/board.c/start_armboot -> init_sequence
搬移内核到内存 //common/main.c main_loop -> getenv (“bootcmd”) bootdelay >= 0 && s && !abortboot (bootdelay)) 下的 run_command (bootcmd)
运行内核
}
1.3.1, 第一条指令位置
{//---u-boot.lds 或 u-boot.map 指明了第一条指令位置
arch/arm/cpu/armv7/start.o
}
1.3.2, u-boot的启动入口
{//---arch/arm/cpu/armv7/start.S u-boot的启动入口
_start: b reset
ldr pc, _undefined_instruction
ldr pc, _software_interrupt
ldr pc, _prefetch_abort
ldr pc, _data_abort
ldr pc, _not_used
ldr pc, _irq
ldr pc, _fiq
reset:
/*
* set the cpu to SVC32 mode
*/
mrs r0, cpsr
bic r0, r0, #0x1f
orr r0, r0, #0xd3
msr cpsr,r0
bl cpu_init_cp15 //关闭中断 cache MMU等
bl cpu_init_crit
bl _main //跳到C部分 位置 arch/arm/lib/board.c ( 在u-boot.map里搜索_main)
ENTRY(relocate_code) //自搬移 重定位 (运行时机见 u-boot.map 里执行位置)
adr r0, _start
cmp r0, r6
moveq r9, #0 /* no relocation. relocation offset(r9) = 0 */
beq relocate_done /* skip relocation */
mov r1, r6 /* r1 <- scratch for copy_loop */
ldr r3, _image_copy_end_ofs
add r2, r0, r3 /* r2 <- source end address */
copy_loop:
ldmia r0!, {r9-r10} /* copy from source address [r0] */
stmia r1!, {r9-r10} /* copy to target address [r1] */
cmp r0, r2 /* until source end address [r2] */
blo copy_loop
ENDPROC(relocate_code)
ENTRY(cpu_init_cp15)
/*
* Invalidate L1 I/D
*/
mov r0, #0 @ set up for MCR
mcr p15, 0, r0, c8, c7, 0 @ invalidate TLBs
mcr p15, 0, r0, c7, c5, 0 @ invalidate icache
/*
* disable MMU stuff and caches
*/
mrc p15, 0, r0, c1, c0, 0
bic r0, r0, #0x00002000 @ clear bits 13 (--V-)
bic r0, r0, #0x00000007 @ clear bits 2:0 (-CAM)
mcr p15, 0, r0, c1, c0, 0
mov pc, lr @ back to my caller
ENDPROC(cpu_init_cp15)
ENTRY(cpu_init_crit)
b lowlevel_init @ go setup pll,mux,memory //位置board/samsung/fs4412/libfs4412.o( 在u-boot.map里搜索lowlevel_init)
ENDPROC(cpu_init_crit)
}
1.3.3,基本硬件初始化
{//---board/samsung/fs4412/lowlevel_init.S 基本硬件初始化 (关闭看门狗中断 时钟初始化 串口初始化 nand初始化 dram初始化)
lowlevel_init: //有些初始化,已被三星剥离到 BLO或BL1 中
/* init system clock */
bl system_clock_init
/* Memory initialize */
bl mem_ctrl_asm_init
/* for UART */
bl uart_asm_init
mov pc, lr
}
1.3.4,大部分硬件初始化
{//---arch/arm/lib/board.c 大部分硬件初始化, 调用主循环main_loop(见common/main.c)
init_fnc_t *init_sequence[] = {
arch_cpu_init, /* basic arch cpu dependent setup */
mark_bootstage,
#if defined(CONFIG_BOARD_EARLY_INIT_F)
board_early_init_f,
#endif
timer_init, /* initialize timer */
env_init, /* initialize environment */
init_baudrate, /* initialze baudrate settings */
serial_init, /* serial communications setup */
console_init_f, /* stage 1 init of console */
display_banner, /* say that we are here */
dram_init, /* configure available RAM banks */
NULL,
};
void board_init_r(gd_t *id, ulong dest_addr)
{
board_init(); /* Setup chipselects */
/* main_loop() can return to retry autoboot, if so just run it again. */
for (;;) {
main_loop(); //调用主循环main_loop
}
}
void board_init(ulong bootflag)
{
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) { //大部分硬件初始化
if ((*init_fnc_ptr)() != 0) {
hang ();
}
}
}
}
1.3.5,BootLoader两种模式切换
{//5.common/main.c //主循环main_loop 实现 bootdelay延时时间到之前无输入,则加载bootcmd进入自启动模式;如果有输入则进入交互模式
void main_loop (void)
{
s = getenv ("bootdelay");
bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
s = getenv ("bootcmd"); //获取自启动命令如 bootcmd = tftp 20008000 zImage ; go zImage 表示从网络加载内核运行
if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
run_command (s, 0); //如果延时大于等于零,并且没有在延时过程中接收到按键,则运行bootcmd命令,引导内核。进入自启动模式
}
//否则进入交互模式
/*
* Main Loop for Monitor Command Processing
*/
for (;;) {
len = readline (CONFIG_SYS_PROMPT); //读取命令
rc = run_command (lastcommand, flag); //运行命令
}
}
}
}
}
3,u-boot 启动分析
{//u-boot 启动分析====
3.1,u-boot环境变量设置
{//-------------------------------启动信息----------------------------------
{//-------u-boot环境变量设置
FS4412 # pri
baudrate=115200
bootargs=root=/dev/nfs nfsroot=192.168.9.120:/nfs/rootfs rw console=ttySAC2,115200 init=/linuxrc ip=192.168.9.9
bootcmd=tftp 41000000 uImage;tftp 42000000 exynos4412-fs4412.dtb;bootm 41000000 - 42000000
bootdelay=3
ethact=dm9000
ethaddr=11:22:33:44:55:66
gatewayip=192.168.9.1
ipaddr=192.168.9.9
netmask=255.255.255.0
serverip=192.168.9.120
stderr=serial
stdin=serial
stdout=serial
Environment size: 425/16380 bytes
}
3.2,u-boot启动阶段
{//-------u-boot启动阶段
U-Boot 2013.01 (Aug 24 2014 - 12:01:19) for FS4412
CPU: Exynos4412@1000MHz
Board: FS4412
DRAM: 1 GiB
WARNING: Caches not enabled
MMC: MMC0: 3728 MB
In: serial
Out: serial
Err: serial
MMC read: dev # 0, block # 48, count 16 ...16 blocks read: OK
eMMC CLOSE Success.!!
Checking Boot Mode ... EMMC4.41
Net: dm9000
Hit any key to stop autoboot: 3 2 1 0
dm9000 i/o: 0x5000000, id: 0x90000a46
DM9000: running in 16 bit mode
MAC: 11:22:33:44:55:66
operating at 100M full duplex mode
Using dm9000 device
TFTP from server 192.168.9.120; our IP address is 192.168.9.9 //对应bootcmd的 tftp 0x41000000 uImage
Filename 'uImage'.
Load address: 0x41000000
Loading: *#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#######
69.3 KiB/s
done
Bytes transferred = 3028040 (2e3448 hex)
dm9000 i/o: 0x5000000, id: 0x90000a46
DM9000: running in 16 bit mode
MAC: 11:22:33:44:55:66
operating at 100M full duplex mode
Using dm9000 device
TFTP from server 192.168.9.120; our IP address is 192.168.9.9 //对应bootcmd的 tftp 42000000 exynos4412-fs4412.dtb
Filename 'exynos4412-fs4412.dtb'.
Load address: 0x42000000
Loading: *#######
393.6 KiB/s
done
Bytes transferred = 33876 (8454 hex)
Booting kernel from Legacy Image at 41000000 ...
Image Name: Linux-3.14.0
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 3027976 Bytes = 2.9 MiB
Load Address: 40008000
Entry Point: 40008000
Verifying Checksum ... OK
Flattened Device Tree blob at 42000000
Booting using the fdt blob at 0x42000000
Loading Kernel Image ... OK
Loading Device Tree to 4fff4000, end 4ffff453 ... OK
Starting kernel ... /*启动内核(对应bootmcd的 bootm 41000000 - 42000000)
如果到这里启动失败,可能原因
1.u-boot参数设置错误 如bootargs 中ttySAC2 设为了ttySAC1
2.uImage 有问题。 换一个OK的试试,或把这个uImage放在别的板上验证下
3.内核源码有问题, 加打印信息跟踪和打开内核调试信息开关, init\main.c start_kernel()
如machine type 不匹配
4.硬件相关,如u-boot初始CPU频率,分频不对
*/
}
3.3,linux内核启动阶段
{//-------linux内核启动阶段
Booting Linux on physical CPU 0xa00
//init\main.c start_kernel()
Linux version 3.14.0 (david@ubuntu) (gcc version 4.6.4 (crosstool-NG hg+default-2685dfa9de14 - tc0002) ) #23 SMP PREEMPT Fri Aug 15 11:30:16 CST 2014
CPU: ARMv7 Processor [413fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
Machine model: Insignal Origen evaluation board based on Exynos4412
Memory policy: Data cache writealloc
CPU EXYNOS4412 (id 0xe4412011)
Running under secure firmware.
PERCPU: Embedded 7 pages/cpu @eefb6000 s7424 r8192 d13056 u32768
Built 1 zonelists in Zone order, mobility grouping on. Total pages: 256528
Kernel command line: root=/dev/nfs nfsroot=192.168.9.120:/nfs/rootfs rw console=ttySAC2,115200 init=/linuxrc ip=192.168.9.9 //内核对u-boot传入的bootargs参数的解析
PID hash table entries: 4096 (order: 2, 16384 bytes)
Dentry cache hash table entries: 131072 (order: 7, 524288 bytes)
Inode-cache hash table entries: 65536 (order: 6, 262144 bytes)
Memory: 1016852K/1032192K available (3956K kernel code, 237K rwdata, 1320K rodata, 231K init, 276K bss, 15340K reserved, 270336K highmem)
Virtual kernel memory layout:
vector : 0xffff0000 - 0xffff1000 ( 4 kB)
fixmap : 0xfff00000 - 0xfffe0000 ( 896 kB)
vmalloc : 0xf0000000 - 0xff000000 ( 240 MB)
lowmem : 0xc0000000 - 0xef800000 ( 760 MB)
pkmap : 0xbfe00000 - 0xc0000000 ( 2 MB)
modules : 0xbf000000 - 0xbfe00000 ( 14 MB)
.text : 0xc0008000 - 0xc052f41c (5278 kB)
.init : 0xc0530000 - 0xc0569d00 ( 232 kB)
.data : 0xc056a000 - 0xc05a5540 ( 238 kB)
.bss : 0xc05a554c - 0xc05ea584 ( 277 kB)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
Preemptible hierarchical RCU implementation.
RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=4.
RCU: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
NR_IRQS:16 nr_irqs:16 16
Exynos4x12 clocks: sclk_apll = 500000000, sclk_mpll = 800000000
sclk_epll = 96000000, sclk_vpll = 350000000, arm_clk = 1000000000
sched_clock: 32 bits at 200 Hz, resolution 5000000ns, wraps every 10737418240000000ns
Console: colour dummy device 80x30
Calibrating delay loop... 1992.29 BogoMIPS (lpj=4980736)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 2048 (order: 1, 8192 bytes)
Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes)
CPU: Testing write buffer coherency: ok
missing device node for CPU 0
missing device node for CPU 1
missing device node for CPU 2
missing device node for CPU 3
CPU0: thread -1, cpu 0, socket 10, mpidr 80000a00
Setting up static identity map for 0x403c0e10 - 0x403c0e68
CPU1: Booted secondary processor
CPU1: thread -1, cpu 1, socket 10, mpidr 80000a01
CPU2: Booted secondary processor
CPU2: thread -1, cpu 2, socket 10, mpidr 80000a02
CPU3: Booted secondary processor
CPU3: thread -1, cpu 3, socket 10, mpidr 80000a03
Brought up 4 CPUs
SMP: Total of 4 processors activated.
CPU: All CPU(s) started in SVC mode.
devtmpfs: initialized
VFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 4
pinctrl core: initialized pinctrl subsystem
regulator-dummy: no parameters
NET: Registered protocol family 16
DMA: preallocated 256 KiB pool for atomic coherent allocations
S3C Power Management, Copyright 2004 Simtec Electronics
EXYNOS4x12 PMU Initialize
EXYNOS: Initializing architecture
bio: create slab at 0
VMEM_VDD_2.8V: 2800 mV
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
s3c-i2c 13860000.i2c: slave address 0x00
s3c-i2c 13860000.i2c: bus frequency set to 19 KHz
sec_pmic 0-0066: No interrupt specified, no interrupts
VDD_ALIVE: failed to apply 1100000uV constraint
s5m8767-pmic s5m8767-pmic: regulator init failed for 0
s3c-i2c 13860000.i2c: i2c-0: S3C I2C adapter
Switched to clocksource mct-frc
NET: Registered protocol family 2
TCP established hash table entries: 8192 (order: 3, 32768 bytes)
TCP bind hash table entries: 8192 (order: 5, 163840 bytes)
TCP: Hash tables configured (established 8192 bind 8192)
TCP: reno registered
UDP hash table entries: 512 (order: 2, 24576 bytes)
UDP-Lite hash table entries: 512 (order: 2, 24576 bytes)
NET: Registered protocol family 1
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
futex hash table entries: 1024 (order: 4, 65536 bytes)
bounce pool size: 64 pages
ROMFS MTD (C) 2007 Red Hat, Inc.
msgmni has been set to 1458
io scheduler noop registered
io scheduler deadline registered
io scheduler cfq registered (default)
dma-pl330 12680000.pdma: Loaded driver for PL330 DMAC-1315632
dma-pl330 12680000.pdma: DBUFF-32x4bytes Num_Chans-8 Num_Peri-32 Num_Events-32
dma-pl330 12690000.pdma: Loaded driver for PL330 DMAC-1315632
dma-pl330 12690000.pdma: DBUFF-32x4bytes Num_Chans-8 Num_Peri-32 Num_Events-32
dma-pl330 12850000.mdma: Loaded driver for PL330 DMAC-1315632
dma-pl330 12850000.mdma: DBUFF-64x8bytes Num_Chans-8 Num_Peri-1 Num_Events-32
Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled
13800000.serial: ttySAC0 at MMIO 0x13800000 (irq = 84, base_baud = 0) is a S3C6400/10
13810000.serial: ttySAC1 at MMIO 0x13810000 (irq = 85, base_baud = 0) is a S3C6400/10
13820000.serial: ttySAC2 at MMIO 0x13820000 (irq = 86, base_baud = 0) is a S3C6400/10
console [ttySAC2] enabled
13830000.serial: ttySAC3 at MMIO 0x13830000 (irq = 87, base_baud = 0) is a S3C6400/10
brd: module loaded
loop: module loaded
dm9000 5000000.ethernet: read wrong id 0x01010101
eth0: dm9000a at f0076000,f0078004 IRQ 167 MAC: 00:0a:2d:a6:55:a2 (platform data)
usbcore: registered new interface driver asix
usbcore: registered new interface driver ax88179_178a
usbcore: registered new interface driver cdc_ether
usbcore: registered new interface driver smsc75xx
usbcore: registered new interface driver smsc95xx
usbcore: registered new interface driver net1080
usbcore: registered new interface driver cdc_subset
usbcore: registered new interface driver zaurus
usbcore: registered new interface driver cdc_ncm
ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
ehci-exynos: EHCI EXYNOS driver
usbcore: registered new interface driver usb-storage
mousedev: PS/2 mouse device common for all mice
input: 100a0000.keypad as /devices/100a0000.keypad/input/input0
device-mapper: ioctl: 4.27.0-ioctl (2013-10-30) initialised: [email protected]
sdhci: Secure Digital Host Controller Interface driver
sdhci: Copyright(c) Pierre Ossman
s3c-sdhci 12530000.sdhci: clock source 2: mmc_busclk.2 (40000000 Hz)
mmc0: no vqmmc regulator found
mmc0: SDHCI controller on samsung-hsmmc [12530000.sdhci] using ADMA
Synopsys Designware Multimedia Card Interface Driver
dwmmc_exynos 12550000.mmc: no vmmc regulator found: -19
dwmmc_exynos 12550000.mmc: Using internal DMA controller.
dwmmc_exynos 12550000.mmc: Version ID is 240a
dwmmc_exynos 12550000.mmc: DW MMC controller at irq 109, 32 bit host data width, 128 deep fifo
dwmmc_exynos 12550000.mmc: 1 slots initialized
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
TCP: cubic registered
NET: Registered protocol family 17
NET: Registered protocol family 15
Registering SWP/SWPB emulation handler
VMEM_VDD_2.8V: disabling
regulator-dummy: disabling
drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
mmc1: BKOPS_EN bit is not set
dm9000 5000000.ethernet eth0: link down
mmc_host mmc1: Bus speed (slot 0) = 50000000Hz (slot req 52000000Hz, actual 50000000HZ div = 0)
mmc_host mmc1: Bus speed (slot 0) = 100000000Hz (slot req 52000000Hz, actual 50000000HZ div = 1)
mmc1: new high speed DDR MMC card at address 0001
mmcblk0: mmc1:0001 4YMD3R 3.64 GiB
mmcblk0boot0: mmc1:0001 4YMD3R partition 1 4.00 MiB
mmcblk0boot1: mmc1:0001 4YMD3R partition 2 4.00 MiB
mmcblk0rpmb: mmc1:0001 4YMD3R partition 3 512 KiB
mmcblk0: p1 p2 p3 p4
mmcblk0boot1: unknown partition table
mmcblk0boot0: unknown partition table
dm9000 5000000.ethernet eth0: link up, 100Mbps, full-duplex, lpa 0x45E1
IP-Config: Guessing netmask 255.255.255.0
IP-Config: Complete:
device=eth0, hwaddr=00:0a:2d:a6:55:a2, ipaddr=192.168.9.9, mask=255.255.255.0, gw=255.255.255.255
host=192.168.9.9, domain=, nis-domain=(none)
bootserver=255.255.255.255, rootserver=192.168.9.120, rootpath=
clk: Not disabling unused clocks
VFS: Mounted root (nfs filesystem) on device 0:10. /*挂载根文件系统(从 nfs filesystem知挂载的是NFS rootfs)
也可挂载jffs2 ramdisk cramfs等格式rootfs 等
如果到这里启动失败,可能原因
1. bootargs 参数设置用问题 ,如设置mtdblock2 写成了mtdblock3, 或路径,名称不对等
2. 设为nfs挂载,验证rootfs内容有没问题
3. 制作镜像过程有没问题
*/
devtmpfs: mounted
Freeing unused kernel memory: 228K (c0530000 - c0569000)
}
3.4, 根文件系统阶段(可运行应用程序)
{//-------根文件系统阶段(可运行应用程序)
[root@farsight ]# ls
a.out dev lib mnt root sys usr
bin etc linuxrc proc sbin tmp var
[root@farsight ]# ./a.out
hello
}
}
5,U-BOOT 移植方法