Fork me on GitHub

从0开始运行主线Linux内核

从0开始运行主线Linux内核

本博客以Xunlong Orangepi Zero为例,运行最新Linux 4.11.0-rc4内核。

[Mainline U-Boot & Mainline Kernel & Rootfs Howto]

Mainline U-Boot

1
2
3
4
5
6
7
8
# 克隆u-boot仓库
git clone git://git.denx.de/u-boot.git
# 编译配置
make orangepi_zero_defconfig
# 交叉编译
make V=s -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

编译完成在源码根目录出现目标文件:

  • u-boot-sunxi-with-spl.bin

Mainline Kernel

1
2
3
4
5
6
7
8
# 克隆kernel仓库
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
# 编译配置
make ARCH=arm sunxi_defconfig
# 交叉编译
make V=s -j8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

编译完成在源码目录出现目标文件:

  • zImage (arch/arm/boot/zImage)
  • sun8i-h2-plus-orangepi-zero.dtb (arch/arm/boot/dts/sun8i-h2-plus-orangepi-zero.dtb)

Rootfs

1 busybox

1
2
3
4
5
# 克隆busybox仓库
git clone git://git.busybox.net/busybox
# 编译配置
make menuconfig

[*] Build busybox as a static binary

(arm-linux-gnueabihf-) Cross Compiler prefix

(./_install) Busybox Installation prefix

1
2
3
4
5
6
# 编译
make
cd _install/
chmod 4755 bin/busybox

接下来的步骤都基于这个_install文件夹。

2 etc/inittab

创建etc/inittab文件:

1
2
3
4
5
# /etc/inittab
::sysinit:/etc/init.d/rcS
ttyS0::sysinit:/bin/ash
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r

3 etc/fstab

创建etc/fstab文件:

1
2
3
# device mount-point type options dump fsck order
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0

4 etc/init.d/rcS

创建etc/init.d/rcS文件:

1
2
#!/bin/sh
mount -a

5 创建设备节点

在dev目录下创建基本的设备节点:

1
2
3
mknod -m 0666 console c 5 1
mknod -m 0666 null c 1 3
mknod ttyS0 c 4 64

Install

将sd卡分区,第一个分区放zImage和dtb文件,第二个分区作为rootfs挂载:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/sh
card=/dev/mmcblk0
cardroot=${card}${p}2
# sd卡格式化
dd if=/dev/zero of=${card} bs=1M count=1
# sd卡分区
blockdev --rereadpt ${card}
cat <<EOT | sfdisk ${card}
1M,16M,c
,,L
EOT
# 格式化分区
mkfs.vfat ${card}${p}1
mkfs.ext4 ${card}${p}2
# 刷入U-Boot
dd if=u-boot-sunxi-with-spl.bin of=${card} bs=1024 seek=8
# 写入zImage和dtb文件
mount ${card}${p}1 /mnt/
cp zImage /mnt/
cp sun8i-h2-plus-orangepi-zero.dtb /mnt/
umount /mnt/
# 写入rootfs
mount ${cardroot} /mnt/
cp -r /myrootfs /mnt/
umount /mnt

Boot

在U-Boot命令行中执行:

1
2
3
4
5
6
7
8
=> setenv bootargs 'console=ttyS0,115200n8 earlyprintk root=/dev/mmcblk0p1 rootwait panic=10'
=> fatload mmc 0 0x46000000 zImage
reading zImage
3657840 bytes read in 226 ms (15.4 MiB/s)
=> fatload mmc 0 0x49000000 sun8i-h2-plus-orangepi-zero.dtb
reading sun8i-h2-plus-orangepi-zero.dtb
14025 bytes read in 30 ms (456.1 KiB/s)
=> bootz 0x46000000 - 0x49000000

Boot Success

成功进入Linux系统,重新挂载可读写文件系统,挂载proc,sys,debug目录:

1
2
3
4
5
6
7
8
9
/ # mount -o remount,rw /
[ 58.970790] EXT4-fs (mmcblk0p2): re-mounted. Opts: data=ordered
/ # mount -t proc proc /proc
/ # mount -t sysfs sysfs /sys
/ # mount -t debugfs debugfs /sys/kernel/debug
/ # cat /proc/version
Linux version 4.11.0-rc4-00064-g89970a0 (chenziping@techping-pc) (gcc version 6.2.1 20161016 (Linaro GCC 6.2-2016.11) ) #1 SMP Thu Mar 30 18:55:45 CST 2017
/ #