Linux 開機啓動詳解

一、開發環境

1、ubuntu-16.04 64bits,不能使用32bits的系統。
2、vmware12.01 —->mnt/hgfs
3、windows 64bits

二、嵌入式概述

這裏寫圖片描述

1、用戶空間—應用程序,內核空間—驅動程序
2、應用程序如何訪問驅動程序?(系統調用)
3、驅動程序給應用程序提供的接口
4、驅動程序訪問硬件

三、嵌入式平臺的啓動過程概述(GEC6818爲例)

說明:GEC6818是以cortex A53 爲核心架構,以三星公司生產的 S5P6818爲核心主板的一塊開發板
如圖:

這裏寫圖片描述

硬件上電–>運行CPU自帶的boot–>啓動u-boot—>加載linux內核
–>掛載rootfs–>調用rootfs中的腳本/etc/profile—>調用用戶的應用程序

四、u-boot

1、什麼是u-boot?

u-boot是嵌入式平臺硬件的啓動引導程序,完成硬件的初始化和操作系統的啓動。
u-boot—>通用的boot loader, u–universal

1)通用
(1)u-boot支持多種架構的CPU:ARM、x86、MIPS、PowerPC、…..
(2)u-boot支持多種操作系統的啓動引導:linux、WinCE、Vxworks、….。uC/OS-III–>簡單的操作系統,不需要使用u-boot,直接引導。

2)boot
硬件的初始化代碼,使用uboot可以完成硬件的初始化。uboot做硬件初始化的時候,分成2個階段:
stage1:CPU級的初始化(核心初始化)—>CPU的cache、時鐘頻率、MMU、DDR3內存、串口、電源管理、……
stage2:board級的初始化(外設的初始化)—>網卡、LCD、IIC、USB、…..
注意:
stage1一般使用ARM彙編語言編程;stage2使用C語言編程。

3)loader
加載linux系統的內核,並啓動內核。將linux內核從電子硬盤(flash存儲器,掉電不丟失)拷貝到DDR3內存(RAM,掉電數據丟失),並向linux內核傳遞啓動參數(bootargs—boot arguments),然後linux內核根據啓動參數完成啓動。

2、u-boot是免費的,源碼開放的。

uboot本身不是操作系統,是一個裸機程序。
1)u-boot的官網
http://www.denx.de/wiki/U-Boot/WebHome
ftp://ftp.denx.de/pub/u-boot/

3、GEC6818平臺的u-boot的版本

U-Boot 2014.07

五、u-boot的使用

u-boot除了有boot和loader的功能以外,它也提供了一些工具—命令。使用這些命令可以查看硬件信息,也可以下載程序、…..
uboot命令和linux的shell是不同的。

1、進入uboot命令行

Hit any key to stop autoboot: 0
X6818#
X6818#
X6818#

2、查看uboot支持的命令

X6818# help

3、bdinfo - 查看嵌入式硬件平臺的信息

X6818# bdinfo
arch_number = 0x000010EA —->u-boot針對每一個具體的硬件平臺(GEC6818)有一個ID。這個ID會傳給linux內核,linux內核收到該ID會與自身的ID做對比,如果二者一致,linux內核纔可以啓動。

boot_params = 0x40000100 —>uboot傳給linux內核的啓動參數存放的地址,uboot將該地址傳給內核,內核啓動過程中,去該地址獲取啓動參數。

DRAM bank = 0x00000000 —>DDR3內存
-> start = 0x40000000 —>內存的開始地址
-> size = 0x40000000 —>內存的大小 4*2**28=1*2**2 * 2**28=1*2**30=1GB

提示1:eMMC電子硬盤,8GB

提示2:linux內核在內存中運行的地址:

Booting kernel from Legacy Image at 48000000 …
Image Name: Linux-3.4.39-gec
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 5533816 Bytes = 5.3 MiB
Load Address: 40008000 —->linux內核的運行地址。
Entry Point: 40008000
Verifying Checksum … OK
Loading Kernel Image … OK

提示3:DDR3地址範圍:
0x40000000~0x80000000-1

eth0name = dwmac.c0060000
ethaddr = 00:e2:1c:ba:e8:60 —>網卡的MAC地址
current eth = dwmac.c0060000
ip_addr = 192.168.14.3 —>IP地址
baudrate = 115200 bps
TLB addr = 0x7FFF0000
relocaddr = 0x46000000
reloc off = 0x00000000
irq_sp = 0x7DF6BF00
sp start = 0x43BFFE68

4、fastboot

系統刷新命令,可以看到eMMC的分區

X6818# fastboot

Fastboot Partitions:
mmc.2: ubootpak, img : 0x200, 0x78000
mmc.2: 2ndboot, img : 0x200, 0x4000
mmc.2: bootloader, img : 0x8000, 0x70000
mmc.2: boot, fs : 0x100000, 0x4000000
mmc.2: system, fs : 0x4100000, 0x2f200000
mmc.2: cache, fs : 0x33300000, 0x1ac00000
mmc.2: misc, fs : 0x4e000000, 0x800000
mmc.2: recovery, fs : 0x4e900000, 0x1600000
mmc.2: userdata, fs : 0x50000000, 0x0
Support fstype : 2nd boot factory raw fat ext4 emmc nand ubi ubifs
Reserved part : partmap mem env cmd
DONE: Logo bmp 311 by 300 (3bpp), len=280854
DRAW: 0x47000000 -> 0x46000000
Load USB Driver: android
Core usb device tie configuration done
OTG cable Connected!

5、printenv

查看uboot的環境變量
X6818# printenv
baudrate=115200 —->串口控制檯的波特率。console=/dev/ttySAC0

bootargs=lcd=at070tn92 tp=gslx680-linux root=/dev/mmcblk0p2 rw rootfstype=ext4

bootcmd=ext4load mmc 2:1 0x48000000 uImage;bootm 0x48000000

bootdelay=2 —>uboot自動系統的倒計時
bootfile=uImage —>linux內核映像文件的格式
ethaddr=00:e2:1c:ba:e8:60 —>網卡的MAC地址
ethprime=RTL8211
—->網絡信息
gatewayip=192.168.14.1
ipaddr=192.168.14.3
netmask=255.255.254.0
serverip=192.168.15.216
Environment size: 865/32764 bytes

1)啓動命令
—->告訴uboot,自動啓動後,去哪裏加載linux內核,並運行內核。
bootcmd=ext4load mmc 2:1 0x48000000 uImage;bootm 0x48000000
ext4load mmc 2:1 —->去電子硬盤eMMC的第一個分區去加載linux內核
0x48000000 —->內核要加載到DDR3內存的0x48000000地址上
uImage —>linux內核映像文件的格式是uImage

bootm 0x48000000 —>運行拷貝到0x48000000地址上的linux內核映像。
如果uboot可以啓動,但是不能啓動linux內核,可以查看uboot的bootcmd是否正確。

2)啓動參數
bootargs=lcd=at070tn92 tp=gslx680-linux root=/dev/mmcblk0p2 rw rootfstype=ext4
—–>uboot將啓動參數存放到DDR3內存的0x40000100地址上,linux內核從該地址或這些啓動參數,並根據啓動參數進行內核的啓動和rootfs的掛載。
lcd=at070tn92 —>GEC6818平臺使用LCD的型號,內核根據這個型號,來調用對應LCD的驅動。
tp=gslx680-linux —>GEC6818平臺使用觸摸屏的型號,內核根據這個型號,來調用對應觸摸屏的驅動。
root=/dev/mmcblk0p2 —->根文件系統是存放在eMMC的第2個分區
rw rootfstype=ext4 —>rw–rootfs是可讀寫的,rootfstype=ext4—rootfs的類型是ext4.

—->如果linux內核可以啓動,但是不能掛載rootfs,我們可以查看啓動參數是否指定了rootfs的路徑。

6、setenv

—–>設置環境變量
X6818# setenv bootdelay 5
X6818# setenv ipaddr 192.168.7.5
X6818# setenv serverip 192.168.7.3
X6818# setenv gatewayip 192.168.7.1
X6818# saveenv
Saving Environment to MMC…
Writing to MMC(2)… done

六、linux內核

1、linux內核的作用

1)進程的管理和進程通信
2)內存管理—虛擬內存
3)文件系統
[root@GEC6818 /]#cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev cgroup
nodev tmpfs
nodev devtmpfs
nodev binfmt_misc
nodev debugfs
nodev sockfs
nodev pipefs
nodev anon_inodefs
nodev rpc_pipefs
nodev devpts
ext2
ext3
ext4 —–> rootfs的文件系統類型
nodev ramfs
vfat —->U盤、SD卡的文件系統類型
msdos
iso9660
nodev nfs ——>網絡文件系統
nodev nfs4
ntfs
nodev autofs
fuseblk
nodev fuse
nodev fusectl

注意:
想要linux內核支持一種新的文件系統—->通過配置linux內核,來選擇新的文件系統。

4)設備管理—-linux設備驅動
5)網絡協議—TCP/IP
網絡七層:
應用層、表示層、會話層—–>linux用戶空間,屬於linux應用程序:ftp、telnet、http、….
傳輸層、網絡層 —–>linux內核中標準網絡協議(TCP/IP)
數據鏈路層—–網卡驅動
物理層—網卡

2、GEC6818內核啓動的過程中,主要做了哪些工作?

1)uboot加載linux內核,得到內核的信息,並啓動內核。

Booting kernel from Legacy Image at 48000000 …

Image Name: Linux-3.4.39-gec —->3.4.39, -gec—->編譯內核時,加入的本地版本(local version)
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 5533816 Bytes = 5.3 MiB
Load Address: 40008000
Entry Point: 40008000
Verifying Checksum … OK
Loading Kernel Image … OK

Starting kernel …

2)linux內核的基本信息
[ 0.000000] Booting Linux on physical CPU 0
[ 0.000000] Linux version 3.4.39-gec (gec@ubuntu) (gcc version 4.8 (GCC) ) #3 SMP PREEMPT Thu Jan 25 14:47:54 CST 2018
[ 0.000000] GEC6818 : done board initialize …
[ 0.000000] PERCPU: Embedded 9 pages/cpu @c1519000 s12544 r8192 d16128 u36864
[ 0.000000] Kernel command line: console=ttySAC0,115200n8 androidboot.hardware=GEC6818 androidboot.console=ttySAC0 androidboot.serialno=0123456789abcdef initrd=0x49000000,0x1000000 lcd=at070tn92 tp=gslx680-linux root=/dev/mmcblk0p2 rw rootfstype=ext4

3)linux內核內存的劃分:
[ 0.000000] Memory: 1024MB = 1024MB total
[ 0.000000] Memory: 810812k/810812k available, 237764k reserved, 272384K highmem
[ 0.000000] Virtual kernel memory layout:
[ 0.000000] vector : 0xffff0000 - 0xffff1000 ( 4 kB) —>中斷向量表
[ 0.000000] fixmap : 0xfff00000 - 0xfffe0000 ( 896 kB)
[ 0.000000] vmalloc : 0xef800000 - 0xfee00000 ( 246 MB)
[ 0.000000] lowmem : 0xc0000000 - 0xef600000 ( 758 MB)
[ 0.000000] pkmap : 0xbfe00000 - 0xc0000000 ( 2 MB)
[ 0.000000] modules : 0xbf000000 - 0xbfe00000 ( 14 MB)
[ 0.000000] .text : 0xc0008000 - 0xc0a53154 (10541 kB)
[ 0.000000] .init : 0xc0a54000 - 0xc0a91100 ( 245 kB)
[ 0.000000] .data : 0xc0a92000 - 0xc0b2c148 ( 617 kB)
[ 0.000000] .bss : 0xc0b2c16c - 0xc0d0be88 (1920 kB)
[ 0.000000] SLUB: Genslabs=11, HWalign=64, Order=0-3, MinObjects=0, CPUs=8, Nodes=1

4)8核處理器的測試
[ 0.008000] CPU: Testing write buffer coherency: ok
[ 0.008000] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
[ 0.008000] hw perfevents: no hardware support available
[ 0.008000] Setting up static identity map for 0x407343c8 - 0x40734420
[ 0.024000] CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
[ 0.037000] CPU2: thread -1, cpu 2, socket 0, mpidr 80000002
[ 0.050000] CPU3: thread -1, cpu 3, socket 0, mpidr 80000003
[ 0.063000] CPU4: thread -1, cpu 0, socket 4, mpidr 80040400
[ 0.076000] CPU5: thread -1, cpu 1, socket 4, mpidr 80040401
[ 0.088000] CPU6: thread -1, cpu 2, socket 4, mpidr 80040402
[ 0.101000] CPU7: thread -1, cpu 3, socket 4, mpidr 80040403
[ 0.101000] Brought up 8 CPUs
[ 0.101000] SMP: Total of 8 processors activated (12750.84 BogoMIPS).

5)設備驅動的初始化
[ 0.223000] axp22_ldo1: 3000 mV
[ 0.309000] Advanced Linux Sound Architecture Driver Version 1.0.25.
[ 0.309000] Bluetooth: Core ver 2.16
[ 0.353000] Display LCD register operation
[ 0.354000] Display LVDS register operation
[ 0.354000] Display MiPi register operation

[ 0.381000] nxp-uart.0: ttySAC0 at MMIO 0xc00a1000 (irq = 39) is a S3C6400/10
[ 1.431000] console [ttySAC0] enabled
[ 1.435000] nxp-uart.1: ttySAC1 at MMIO 0xc00a0000 (irq = 38) is a S3C6400/10
[ 1.442000] nxp-uart.2: ttySAC2 at MMIO 0xc00a2000 (irq = 40) is a S3C6400/10
[ 1.449000] nxp-uart.3: ttySAC3 at MMIO 0xc00a3000 (irq = 41) is a S3C6400/10
[ 1.456000] nxp-uart nxp-uart.0: DMA channel TX dma1chan2
[ 1.462000] nxp-uart nxp-uart.0: DMA channel RX dma1chan3
[ 1.467000] nxp-uart nxp-uart.1: DMA channel TX dma1chan0
[ 1.472000] nxp-uart nxp-uart.1: DMA channel RX dma1chan1
[ 1.478000] nxp-uart nxp-uart.2: DMA channel TX dma1chan4
[ 1.483000] nxp-uart nxp-uart.2: DMA channel RX dma1chan5

[ 3.037000] input: ft5x0x as /devices/virtual/input/input0
[ 3.046000] i2c-nxp.1: ack was not received
[ 3.046000] i2c->nxp.1: incomplete xfer (-6)
[ 3.050000] ft5x0x 1-0038: chip not found
[ 3.074000] ft5x0x 1-0038: probe ft5x0x TouchScreen failed, -6
[ 3.075000] nxp-rtc nxp-rtc.0: rtc core: registered nxp-rtc as rtc0
[ 3.078000] i2c /dev entries driver

[ 3.283000] nxp_wdt: NXP Watchdog Timer, (c) 2014 SLsiAP

6)網絡初始化
[ 0.331000] NET: Registered protocol family 2
[ 0.331000] IP route cache hash table entries: 32768 (order: 5, 131072 bytes)
[ 0.332000] TCP established hash table entries: 131072 (order: 8, 1048576 bytes)
[ 0.334000] TCP bind hash table entries: 65536 (order: 8, 1572864 bytes)
[ 0.336000] TCP: Hash tables configured (established 131072 bind 65536)
[ 0.336000] TCP: reno registered
[ 0.336000] UDP hash table entries: 512 (order: 3, 32768 bytes)
[ 0.337000] UDP-Lite hash table entries: 512 (order: 3, 32768 bytes)
[ 0.337000] NET: Registered protocol family 1
[ 0.338000] RPC: Registered named UNIX socket transport module.
[ 0.338000] RPC: Registered udp transport module.
[ 0.338000] RPC: Registered tcp transport module.
[ 0.338000] RPC: Registered tcp NFSv4.1 backchannel transport module.

7)文件系統
[ 0.365000] NFS: Registering the id_resolver key type
[ 0.366000] NTFS driver 2.1.30 [Flags: R/W].
[ 0.366000] fuse init (API version 7.18)
[ 0.367000] msgmni has been set to 1083

8)掛載rootfs
[ 3.911000] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[ 3.913000] VFS: Mounted root (ext4 filesystem) on device 179:2. —->掛載rootfs
[ 3.920000] devtmpfs: mounted —->成功掛載
[ 3.922000] Freeing init memory: 244K
[ 3.926000] Write protecting the kernel text section c0008000 - c0a1d000
[ 3.933000] rodata_test: attempting to write to read-only section:
[ 3.939000] write to read-only section trapped, success
[ 4.079000] EXT4-fs (mmcblk0p2): re-mounted. Opts: data=ordered
-/bin/sh: fconfig: not found
[ 4.436000] eth0: device MAC address 7e:82:f1:98:01:8f
[ 4.443000] stmmac_open: failed PTP initialisation

9)初始化環境變量(執行/etc/profile),進入linux的命令行,可以執行應用程序
[root@GEC6818 /]#
[root@GEC6818 /]#
[root@GEC6818 /]#

講解:VFS: Mounted root (ext4 filesystem) on device 179:2.—通過虛擬文件系統掛載rootfs。rootfs在179:2設備上
[root@GEC6818 /]#ls /dev/mmc* -l
brw-rw—- 1 root root 179, 0 Jan 1 1970 /dev/mmcblk0
brw-rw—- 1 root root 179, 8 Jan 1 1970 /dev/mmcblk0boot0
brw-rw—- 1 root root 179, 16 Jan 1 1970 /dev/mmcblk0boot1
brw-rw—- 1 root root 179, 1 Jan 1 1970 /dev/mmcblk0p1
brw-rw—- 1 root root 179, 2 Jan 1 1970 /dev/mmcblk0p2 —->179主設備號,2—次設備號,硬盤的第二個分區。

3、linux源碼

源碼開發,免費使用。
https://www.kernel.org/
https://www.kernel.org/pub/linux/kernel/v3.x/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章