一.編譯環境搭建:
1.linux源碼下載:https://www.kernel.org/ (最新) https://mirrors.edge.kernel.org/pub/linux/kernel/ (歷史版本)
2.安裝交叉編譯工具鏈:
①手動下載配置工具鏈:
(1):解壓 arm-linux-gcc-3.4.1.tar.bz2
#tar -jxvf arm-linux-gcc-3.4.1.tar.bz2
解壓過程需要一段時間,解壓后的文件形成了 ../temp/usr/local/ 文件夾,進入該文件夾,將arm文件夾拷貝到/usr/local/下
# cd usr/local/
#cp -rv arm /usr/local/
現在交叉編譯程序集都在/usr/local/arm/3.4.1/bin下面了
(2):修改/etc/profile文件:
# vim /etc/profile
增加路徑設置,在末尾添加如下,保存/etc/profile文件:
export PATH=$PATH:/usr/local/arm/3.4.1/bin
(3):立即使新的環境變量生效,不用重啟電腦:
對應方法二:# source /etc/profile
(4): 檢查是否將路徑加入到PATH:
# echo $PATH
顯示的內容中有/usr/local/arm/bin,說明已經將交叉編譯器的路徑加入PATH。
(5).測試是否安裝成功
# arm-linux-gcc -v
②使用crosstoll-ng構建交叉編譯工具鏈:
(1)官網下載:http://crosstool-ng.org/
(2)編譯(確認/opt/cross/bin已經安裝):./configure --prefix=/opt/cross : 報錯的話缺what裝what
make
make install
(3)使用crosstool-ng構建交叉編譯工具鏈:
1. 在合適的地方新建一個crosstool-ng工作目錄。這個目錄將會存儲工具鏈的配置文件,自動下載的文件,構建過程中產生的一些中間文件,最后建立在普通可讀寫目錄:
mkdir -p /home/sheldon/workspace/linux/crosstool
2. 進入前面一步建立的目錄:
cd /home/sheldon/workspace/linux/crosstool
3. 執行 ct-ng menuconfig進入配置界面:(找不到執行檔就鏈接一下:sudo ln -s /opt/cross/bin/ct-ng /usr/bin/ct-ng)
ct-ng menuconfig
4. 選擇‘Paths and misc options’菜單,激活’ Try features marked as EXPERIMENTAL’選項,這一步很重要。
5. 在‘Paths and misc options’ 菜單下,修改‘Directory containing customsource components’值,設置為你想要最終存放工具鏈的目錄。
(${HOME}/src) Local tarballs directory # 指定制作編譯器所需要的源碼包的下載存放,可以修改成任意位置,請注意權限。現在指定到~/src下。
(/opt/${CT_TARGET}) Prefix directory # 制作好的編譯器所放置的目錄
6. 返回主菜單,選擇‘Target options’ , [*] Use EABI # 是否實用EABI方式,對某些指令采用異常的方式來處理(如除法指令),建議選中
7. ‘Target architecture ’選項選擇‘ arm’;‘Endianness ’選項選擇‘ Little endian’;‘Bitness’選項選擇‘32-bit’。Floating point: (software) ---> # 對於很多ARM架構CPU來說是不支持硬浮點運算的,所以選擇軟件方式有更好的兼容性
8. 返主菜單,選擇‘Operating system’(跳過‘Toolchain options’,這里使用缺省選項就夠了)
9. ‘Target OS’選擇‘linux’, ‘Linuxkernel version’選項中選擇你所使用的內核版本
10. 返回主菜單,選擇‘Binary utilities’
11. 選擇你需要的版本,筆者直接選擇最高版本(如果需要編譯Linaro版本,請先激活‘Show Linaro versions’選項)。
12. 返回主菜單,選擇‘C compiler’
13. 選擇你所使用的GCC版本(筆者選擇5.1.0,如果需要編譯Linaro版本,請先激活‘Show Linaro versions’選項),因為筆者需要使用GCC的一個新特性,所以激活‘Compile libsanitizer’選項,一般情況下,保持缺省值就夠了。
14. Toolchain options --->(tonghuix) Tuple's vendor string # 這里可以修改成你自己的個性化名稱,最后會生成形如arm-yourname-linux-gnueabi這樣的編譯器前綴
15. 其它選項用缺省值,退出並保存配置工具
16. 執行ct-ng build編譯工具鏈
ct-ng build
17. 使用交叉編譯器的時候一般是采用arm-tonghuix-linux-gnueabi這樣的命令的,但是很多標准Makefile需要實用標准的交叉編譯器的名稱,一般這個名稱是arm-linux-gcc這樣的。
那么我們在生成交叉編譯器的目錄下寫一個link.sh腳本,新建一些軟鏈接。
>link.sh
#!/bin/sh PREFIX=arm-tonghuix-linux-gnueabi- AFTFIX=arm-linux- ln -s ${PREFIX}gcc ${AFTFIX}gcc ln -s ${PREFIX}addr2line ${AFTFIX}addr2line ln -s ${PREFIX}gdbtui ${AFTFIX}gdbtui ln -s ${PREFIX}ar ${AFTFIX}ar ln -s ${PREFIX}as ${AFTFIX}as ln -s ${PREFIX}c++ ${AFTFIX}c++ ln -s ${PREFIX}c++filt ${AFTFIX}c++filt ln -s ${PREFIX}cpp ${AFTFIX}cpp ln -s ${PREFIX}g++ ${AFTFIX}g++ ln -s ${PREFIX}gccbug ${AFTFIX}gccbug ln -s ${PREFIX}gcj ${AFTFIX}gcj ln -s ${PREFIX}gcov ${AFTFIX}gcov ln -s ${PREFIX}gdb ${AFTFIX}gdb ln -s ${PREFIX}gfortran ${AFTFIX}gfortran ln -s ${PREFIX}gprof ${AFTFIX}gprof ln -s ${PREFIX}jcf-dump ${AFTFIX}jcf-dump ln -s ${PREFIX}ld ${AFTFIX}ld ln -s ${PREFIX}ldd ${AFTFIX}ldd ln -s ${PREFIX}nm ${AFTFIX}nm ln -s ${PREFIX}objcopy ${AFTFIX}objcopy ln -s ${PREFIX}objdump ${AFTFIX}objdump ln -s ${PREFIX}populate ${AFTFIX}populate ln -s ${PREFIX}ranlib ${AFTFIX}ranlib ln -s ${PREFIX}readelf ${AFTFIX}readelf ln -s ${PREFIX}run ${AFTFIX}run ln -s ${PREFIX}size ${AFTFIX}size ln -s ${PREFIX}strings ${AFTFIX}strings ln -s ${PREFIX}strip ${AFTFIX}strip
寫完這個link.sh文件以后執行一下:
sh link.sh
得到標准名稱的交叉編譯工具鏈,可以在makefile中使用類似arm-linux-gcc這樣的名稱了。
18. 把你的工具鏈目錄設置到$PATH 環境變量:
sudo vi /etc/environment 修改PATH的值
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/sheldon/x-tools/arm-unknown-linux-gnueabi/bin/"
. /etc/environment 使生效環境變量
二.編譯linux內核:
①清除一下之前的配置:make mrproper
②修改Makefile如下:
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
③使用默認配置: make s3c2410_defconfig
④再配置自己的需求:make menuconfig
進入【Device Drivers】--->
Memory Technology Device (MTD) support --->
<*>Enable UBI
進入 【File System】---> Miscellaneous filesustems ---> <*>UBIFS file system 進入 【Kernel Features】---> [*] Use the ARM EABI to compile the kernel [*] Allow old ABI binaries to run with this kernel (EXPERIMENTAL)
修改./arch/arm/mach-s3c24xx/common-smdk.c中的smdk_default_nand_part[]結構體數組
static struct mtd_partition smdk_default_nand_part[] = { [0] = { .name = "bootloader", .size = SZ_256K, .offset = 0, }, [1] = { .name = "params", .offset = MTDPART_OFS_APPEND, .size = SZ_128K, }, [2] = { .name = "kernel", .offset = MTDPART_OFS_APPEND, .size = SZ_8M, }, [3] = { .name = "rootfs", .offset = MTDPART_OFS_APPEND, .size = MTDPART_SIZ_FULL, } };
修改./arch/arm/mach-s3c24xx/mach-smdk2440.c中的smdk2440_map_io(void)函數
static void __init smdk2440_map_io(void) { s3c24xx_init_io(smdk2440_iodesc, ARRAY_SIZE(smdk2440_iodesc)); //s3c24xx_init_clocks(16934400); s3c24xx_init_clocks(12000000); //修改為JZ2440的晶振頻率:12M s3c24xx_init_uarts(smdk2440_uartcfgs, ARRAY_SIZE(smdk2440_uartcfgs)); }
最后make uImage,在/arch/arm/boot下生成uImage
三.修改U-boot支持新內核:
啟動jz2440,進入uboot
set bootargs console=ttySAC0,115200 root=/dev/mtdblock3 set bootcmd 'nand read 30000000 kernel;bootm 30000000'
set ipaddr 192.168.1.17 #根據自己修改 set serverip 192.168.1.123 #根據自己修改 save tftp 0x30000000 uImage nand erase.part kernel nand write.i 0x30000000 kernel $filesize
reset
如果系統啟動成功則會最后顯示類似end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-bl,則證明系統啟動成功,下面就是制作根文件系統了。
四.制作新根文件系統采用ubifs文件系統:
首先下載busybox,我這里用的版本是1.20.0
cd busybox1.20.0 make menconfig Busybox Settings --> Build options --> Cross Compiler prefix 填arm-linux- Build Options ---> [*]Build BusyBox as a static binary (no shared libs) make (sync 和 nsenter 有些問題,去掉沒編) make CONFIG_PREFIX=/home/sheldon/workspace/mount/busybox-1.28.1/out_ubifs install cp /home/sheldon/workspace/tool/opt/FriendlyARM/toolschain/4.4.3/lib/*.so.* /home/sheldon/workspace/mount/busybox-1.28.1/out_ubifs -d cp -r /home/sheldon/workspace/mount/busybox-1.28.1/examples/bootfloppy/etc /home/sheldon/workspace/mount/busybox-1.28.1/out_ubifs
vi etc/inittab
::sysinit:/etc/init.d/rcS
console::askfirst:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
::shutdown:/bin/umount -a -r
vi etc/fstab
proc /proc proc defaults 0 0 tmpfs /tmp tmpfs defaults 0 0 sysfs /sys sysfs defaults 0 0 tmpfs /dev tmpfs defaults 0 0
vi etc/init.d/rcS
#! /bin/sh mount -a mkdir /dev/pts mount -t devpts devpts /dev/pts echo /sbin/mdev > /proc/sys/kernel/hotplug mdev -s
cd /home/sheldon/workspace/mount/busybox-1.28.1/out_ubifs
mkdir dev cd dev sudo mknod null c 1 3 sudo mknod console c 5 1 cd .. mkdir proc mnt tmp sys root home cd ..
mkfs.ubifs -r out_ubifs -m 2048 -e 129024 -c 2044 -o fs_jz2440.img
#129024*2044=251MB PS: mkfs.ubifs源碼編譯指令:make WITHOUT_XATTR=1
vim ubinize.cfg
[ubifs] mode=ubi image=fs_jz2440.img vol_id=0 vol_size=10MiB # >= 編譯生成文件系統大小 vol_type=dynamic vol_name=rootfs vol_flags=autoresize
ubinize -o ubi.img -m 2048 -p 128KiB -s 512 ubinize.cfg 生成ubi.img
最后燒寫:
tftp 0x30000000 ubi.img nand erase.part rootfs nand write.i 0x30000000 rootfs $filesize
修改# bootargs=console=ttySAC0,115200 root=/dev/mtdblock3 init=/linuxrc rootfstype=yaffs2
set bootargs console=ttySAC0,115200 ubi.mtd=3 root=ubi0:rootfs rootfstype=ubifs
save reset
-end-