2014年11月4日 星期二

編譯 ARM Linux 核心


~/qemu_image/ -> 放置 ARM image 之目錄
~/qemu_image/kernel/ -> 放置 ARM Linux 核心編譯目錄
~/qemu_image/kernel/modules -> 放置 ARM Linux 核心模組目錄

一、先確定使用的 ARM Board

在這邊假設我們要使用 Beagle Board ,去官方找硬體 Datasheet ,可以知道其 CPU 為 OMAP 3530(Cortex A8) ,其它資訊可參考:
Beagle Board 官方網站 (http://beagleboard.org/)
OMAP 3530(Cortex A8) 德洲儀器官網 (http://www.ti.com/product/omap3530?247SEM)

二、查看 QEMU 支援

qemu-system-arm -cpu ?

我們看有沒有支援 (Cortex A8) ,指令輸出如下:
Available CPUs:
  arm1026
  arm1136
  arm1136-r2
  arm1176
  arm11mpcore
  arm926
  arm946
  cortex-a15
  cortex-a8
  cortex-a8-r2
  cortex-a9
  cortex-m3
  pxa250
  pxa255
  pxa260
  pxa261
  pxa262
  pxa270-a0
  pxa270-a1
  pxa270
  pxa270-b0
  pxa270-b1
  pxa270-c0
  pxa270-c5
  sa1100
  sa1110
  ti925t

三、下載 ARM Linux Kernel

twaren 載點(version 3.X): http://ftp.twaren.net/Unix/Kernel/linux/kernel/v3.x/

先假設我們選用 linux-3.4.100.tar.xz

cd ~/qemu_image/kernel/
wget http://ftp.twaren.net/Unix/Kernel/linux/kernel/v3.x/linux-3.4.100.tar.xz
tar xfva linux-3.4.100.tar.xz
cd linux-3.4.100/

四、基本參數設定(一般 User)

注意所在目錄位置: ~/qemu_image/kernel/linux-3.4.100/
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- omap2plus_defconfig

輸出:
#
# configuration written to .config
#

五、細部設定 Menuconfig

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig
我的設定如下:
General setup  ---> Kernel compression mode (...)  ---> XZ
System Type  ---> ARM system type (...)  ---> TI OMAP
System Type  ---> TI OMAP2/3/4 Specific Features  ---> [*] OMAP3 BEAGLE board
System Type  ---> TI OMAP2/3/4 Specific Features  ---> [*] DEVKIT8000 board
File systems  ---> <*> The Extended 4 (ext4) filesystem

########################################################
Debug 1:
當 make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig 
出現:
*** Unable to find the ncurses libraries or the
 *** required header files.
 *** 'make menuconfig' requires the ncurses libraries.
 ***
 *** Install ncurses (ncurses-devel) and try again.
 ***
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
make: *** [menuconfig] Error

解決 1
sudo apt-get install libncurses5-dev
########################################################

六、編譯核心 uImage

time make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- uImage -j {core number}

########################################################
Debug 2:
若執行編譯指令,出現:
/bin/sh: 1: arm-linux-gnueabi-gcc: not found
make[2]: *** [scripts/mod/empty.o] Error 127
make[1]: *** [scripts/mod] Error 2
make[1]: *** Waiting for unfinished jobs....

解決 2:
ls -l /usr/bin/arm-linux-gnueabi-*
查看所裝的 g++, gcc, cpp 之版本,例如我的如下:
arm-linux-gnueabi-gcc-4.4
arm-linux-gnueabi-g++-4.4
arm-linux-gnueabi-cpp-4.4

接下來做 sub-link
sudo ln -s /usr/bin/arm-linux-gnueabi-gcc-4.4 /usr/bin/arm-linux-gnueabi-gcc
sudo ln -s /usr/bin/arm-linux-gnueabi-g++-4.4 /usr/bin/arm-linux-gnueabi-g++
sudo ln -s /usr/bin/arm-linux-gnueabi-cpp-4.4 /usr/bin/arm-linux-gnueabi-cpp
########################################################
Debug 3:
若執行編譯指令,出現:
"mkimage" command not found - U-Boot images will not be built
make[1]: *** [arch/arm/boot/uImage] Error 1
make: *** [uImage] Error 2

解決 3:
sudo apt-get install u-boot-tools
########################################################

編譯成功後,會出現下面訊息:
Image Name:   Linux-3.4.100
Created:      Tue Nov  4 10:18:34 2014
Image Type:   ARM Linux Kernel Image (uncompressed)
Data Size:    2533656 Bytes = 2474.27 kB = 2.42 MB
Load Address: 80008000
Entry Point:  80008000
 Image arch/arm/boot/uImage is ready

可以查看 uImage 檔案屬性:
file ~/qemu_image/kernel/linux-3.4.100/arch/arm/boot/uImage

/home/lin/qemu_image/kernel/linux-3.4.100/arch/arm/boot/uImage: u-boot legacy uImage, Linux-3.4.100, Linux/ARM, OS Kernel Image (Not compressed), 2533656 bytes, Tue Nov  4 10:18:34 2014, Load Address: 0x80008000, Entry Point: 0x80008000, Header CRC: 0x3B853179, Data CRC: 0xB98ABE41

七、編譯核心 modules

0. 注意目錄位置

cd ~/qemu_image/kernel/linux-3.4.100

1. Compile Kernel Modules

time make -s ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules -j ${core number}

2. Install Kernel Modules

time make -s ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules_install INSTALL_MOD_PATH=~/qemu_image/kernel/modules/ -j ${core number}

3. 製成壓縮檔

cd ~/qemu_image/kernel/modules
tar cfva modules-3.4.100.tar.xz lib

八、總結

1. modules-3.4.100.tar.xz 可以用來取代原始 rootfs 中的 /lib/modules 舊版驅動。

2. 參考建立虛擬 SD Card,並且做連結掛載
losetup /dev/loop0 sdcard.img
losetup -o `expr 512 \ * 2048` /dev/loop1 /dev/loop0
mount /dev/loop1 /sd1

3. 將 uImage 移入 /sd1
cp ~/qemu_image/kernel/linux-3.4.100/arch/arm/boot/uImage /sd1

要在 /sd1 裡面看到下面五個檔案:
-rwxr-xr-x 1 root root   51100 11月 18 11:57 MLO
-rwxr-xr-x 1 root root 1852516 11月 18 11:57 u-boot
-rwxr-xr-x 1 root root  392152 11月 18 11:57 u-boot.bin
-rwxr-xr-x 1 root root  392216 11月 18 11:57 u-boot.img
-rwxr-xr-x 1 root root 2533720 11月 18 11:57 uImage

4. 測試開機(參考 u-boot 編譯 指令)

qemu-system-arm -M beagle -nographic -sd sdcard.img

########################################################
Debug
開虛擬機出現下面訊息:
qemu: hardware error: no boot device found
CPU #0:
R00=00000000 R01=00000000 R02=00000000 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=00000000 R14=00000000 R15=400140a4
PSR=400001d3 -Z-- A svc32
s00=00000000 s01=00000000 d00=0000000000000000
s02=00000000 s03=00000000 d01=0000000000000000
s04=00000000 s05=00000000 d02=0000000000000000
s06=00000000 s07=00000000 d03=0000000000000000
s08=00000000 s09=00000000 d04=0000000000000000
s10=00000000 s11=00000000 d05=0000000000000000
s12=00000000 s13=00000000 d06=0000000000000000
s14=00000000 s15=00000000 d07=0000000000000000
s16=00000000 s17=00000000 d08=0000000000000000
s18=00000000 s19=00000000 d09=0000000000000000
s20=00000000 s21=00000000 d10=0000000000000000
s22=00000000 s23=00000000 d11=0000000000000000
s24=00000000 s25=00000000 d12=0000000000000000
s26=00000000 s27=00000000 d13=0000000000000000
s28=00000000 s29=00000000 d14=0000000000000000
s30=00000000 s31=00000000 d15=0000000000000000
s32=00000000 s33=00000000 d16=0000000000000000
s34=00000000 s35=00000000 d17=0000000000000000
s36=00000000 s37=00000000 d18=0000000000000000
s38=00000000 s39=00000000 d19=0000000000000000
s40=00000000 s41=00000000 d20=0000000000000000
s42=00000000 s43=00000000 d21=0000000000000000
s44=00000000 s45=00000000 d22=0000000000000000
s46=00000000 s47=00000000 d23=0000000000000000
s48=00000000 s49=00000000 d24=0000000000000000
s50=00000000 s51=00000000 d25=0000000000000000
s52=00000000 s53=00000000 d26=0000000000000000
s54=00000000 s55=00000000 d27=0000000000000000
s56=00000000 s57=00000000 d28=0000000000000000
s58=00000000 s59=00000000 d29=0000000000000000
s60=00000000 s61=00000000 d30=0000000000000000
s62=00000000 s63=00000000 d31=0000000000000000
FPSCR: 00000000
已經終

解決:
fdisd sdcard.img
a
1
w

將開機選項加到 sdcard.img 第一分割區。
########################################################

九、 秘訣攻略

arm 的編譯指令有點長,我們可以利用 alias 來包裝成好記的指令。
vi ~/.bashrc
加入 alias makearm='make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-'
source ~/.bashrc

之後就可以用自訂的指令,
設定核心:
makearm omap2plus_defconfig

核心選單:
makearm menuconfig

編譯 uImage :
time makearm uImage -j 8

編譯 modules :
time makearm modules -s -j 8

編譯 modules_install:
time makearm modules_install INSTALL_MOD_PATH=../modules/ -s -j 8




Reference:

沒有留言:

張貼留言