天天看點

從0開始運作主線Linux核心從0開始運作主線Linux核心[Mainline U-Boot & Mainline Kernel & Rootfs Howto]

從0開始運作主線Linux核心

本部落格以Xunlong Orangepi Zero為例,運作最新Linux 4.11.0-rc4核心。

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

Mainline U-Boot

# 克隆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

# 克隆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

# 克隆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

# 編譯
make

cd _install/

chmod  bin/busybox
           

接下來的步驟都基于這個_install檔案夾。

2\ etc/inittab

建立etc/inittab檔案:

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

3\ etc/fstab

建立etc/fstab檔案:

# device        mount-point     type    options dump    fsck    order
proc    /proc   proc    defaults               
tmpfs   /tmp    tmpfs   defaults               
           

4\ etc/init.d/rcS

建立etc/init.d/rcS檔案:

#!/bin/sh
mount -a
           

5\ 建立裝置節點

在dev目錄下建立基本的裝置節點:

mknod -m  console c  
mknod -m  null c  
mknod ttyS0 c  
           

Install

将sd卡分區,第一個分區放zImage和dtb檔案,第二個分區作為rootfs挂載:

#!/bin/sh

card=/dev/mmcblk0
cardroot=${card}${p}

# sd卡格式化
dd if=/dev/zero of=${card} bs=M count=

# sd卡分區
blockdev --rereadpt ${card}
cat <<EOT | sfdisk ${card}
M,M,c
,,L
EOT

# 格式化分區
mkfs.vfat ${card}${p}
mkfs.ext4 ${card}${p}

# 刷入U-Boot
dd if=u-boot-sunxi-with-spl.bin of=${card} bs= seek=

# 寫入zImage和dtb檔案
mount ${card}${p} /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指令行中執行:

=> setenv bootargs 'console=ttyS0,115200n8 earlyprintk root=/dev/mmcblk0p1 rootwait panic=10'
=> fatload mmc   zImage
reading zImage
 bytes read in  ms ( MiB/s)
=> fatload mmc   sun8i-h2-plus-orangepi-zero.dtb
reading sun8i-h2-plus-orangepi-zero.dtb
 bytes read in  ms ( KiB/s)
=> bootz  - 
           

Boot Success

成功進入Linux系統,重新挂載可讀寫檔案系統,挂載proc,sys,debug目錄:

/ # mount -o remount,rw /
[   ] 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 -rc4--g89970a0 (chenziping[email protected]) (gcc version   (Linaro GCC -) ) #1 SMP Thu Mar 30 18:55:45 CST 2017
/ # 
           
  • 我的個人首頁:http://www.techping.cn/
  • 我的個人站點部落格:http://www.techping.cn/blog/wordpress/
  • 我的CSDN部落格:http://blog.csdn.net/techping
  • 我的簡書:http://www.jianshu.com/users/b2a36e431d5e/timeline
  • 我的GitHub:https://github.com/techping

    歡迎互相follow~

繼續閱讀