陳渝大俠翻譯的 uClinux2.6入門

原文出自:http://adam.kaist.ac.kr/~hschoe/Get...inuxARM2_6.html
作者:Hyok S.Choi < [email protected]  >
翻譯:renbagshoes < [email protected]  >
轉載請註明www.linuxsir.org ,謝謝!

一、使用uClinux/ARM 2.6的原因?
爲什麼你要使用uClinux?有很多的理由使uClinux做爲你的嵌入式操作系統。其中
大部分認爲uClinux有以下好處:

輕便

你可以編譯linux2.6的內核得到一個300k以下的內核鏡像,它包括終端和一些基本
的設備驅動以及只讀文件系統。並且flat格式的二進制zImage和應用程序要比linux
傳統使用的ELF二進制小30%--50%,由於使用了較少的標準的C和C++庫。
XIP(execution in place)

你能通過XIP來在一個較小的RAM時執行你的內核鏡像,甚至是應用程序。它能幫助
你快速的啓動,儘管flash的存取時間或是rom啓動鏡像足夠快以及有足夠的緩存大小
。(如果我們想找到它們最優化的平衡點,可以交換兩者之間這些變量。)

便宜

MMU(內存管理單元)核心大小大約佔典型的ARM SoC(System-on-Chip單片機)wafer
(晶體)的30%。在典型的網絡和嵌入式系統裏有一半以上的應用程序不需要MMU。甚
至如果只有一個CPU的許多RTOS(實時操作系統)在默認情況下也不使用MMU。
完全的Linux應用程序接口

除少數情況外,你可以在uClinux平臺上完全使用Linux應用程序接口。你能在uCl
ibc裏使用多線程以及使用標準的系統調用。在用戶盤裏的uClinux發佈包裏有大量
的應用程序被移植到了uClinux系統上。


Easiar to adapt

在uClinux性能裏有一個有趣的特性,是應用程序可以訪問整個系統,包括設備裏
的寄存器,with a bit of wrapper ,我們能製作固件代碼做爲無格式的應用程序運
行在 uClinux上!當然,你能加一些設備驅動和系統調用來調用以及使用許多打開的
Ips在混和的舊的固件代碼裏。如果你計劃移動你的固件代碼到linux,但你沒有熟
悉的linux系統程序,uClinux是一個很好的出發點

完全的linux2.6內核的特性

儘管uClinux很小,但它支持linux2.6內核約定的全部的特性,包括內核優先級特
性以及許多的文件系統,設備驅動。爲linux約定設備驅動端口是容易實現的。幾乎
所有的代碼不需要改變就可以編譯,除了從虛擬地址到物理的內存鏡像外。
注:上面的部分由於涉及的概念太多,翻譯的不大好,請大家指正,謝謝。

二、初次編譯uClinux/ARM2.6指南

如果你決定使用uClinux做爲開發環境,那麼下面的部分將會讓uClinux運行在你的
linux PC機上:

1、獲得uClinux發佈包

一個最容易得到uClinux的發佈包的方法是直接訪問uClinux.org站點:
http://www.uclinux.org/pub/uClinux/dist/
注意這裏發佈的內核版本可能不是最新的,但你能找到一個最新的補丁以及從ker
nel.org站點上找一個對應的linux內核版本來製作一個最新的uClinux內核。此時(
2004年05月25日)可從“
http://www.uclinux.org/pub/uClinux/dist”獲得最新的
發佈包的版本是uClinux-dist.20040408.tar.gz。如果你對編譯軟件不是很熟悉,那
麼根據這個指南你將會很容易的使用這個分佈包來製作你的uClinux內核或是升級它
到最新的版本。

2、獲得最新的內核及補丁

你能到這裏找到最新的uClinux/ARM內核補丁:
http://adam.kaist.ac.kr/~hschoe/
在下載的部分,你將會找到“linux-2.6.x-hscx.patch.gz”最新的鏈接,以及原
始的內核包:
   
http://adam.kaist.ac.kr/~hschoe/dow...5-hsc2.patch.gz
和 http://www.kernel.org/pub/linux/ker...x-2.6.5.tar.bz2

3、ARM-ELF工具鏈

從linux2.6內核代碼使用了一些更新的binutils新特性以來,你需要一個比uclin
ux.org站點上更新的ARM-ELF工具鏈來編譯內核。你能夠從上面的與下載內核和補丁
相同的download部分找到最新的工具鏈,就在當前的鏈接下:
http://adam.kaist.ac.kr/~hschoe/dow...ols-20040427.sh

4、硬件平臺或是一個ARMulator(ARM仿真)

當然你需要一個平臺來運行uClinux內核,請首先看一下注意事項裏有那些平臺被
支持。即使你不能找到適用於你的平臺,或是你沒有任何的硬件平臺,那你也能在你
的linux PC機上運行這個內核:使用ARMulator(ARM仿真)。ARMulator是一個以GDB調
試器爲基礎的仿 真,這個仿真完全支持Atmel AT91x和arm7tdmi EB01。ARMulator的
主頁在這:

http://www.uclinux.org/pub/uClinux/utilities/armulator/

你也能直接從這裏下載:

http://www.uclinux.org/pub/uClinux/...gdb-5.0.tar.bz2(原始gdb-5.0包)
http://www.uclinux.org/pub/uClinux/...021127.patch.gz(gdb-5.0所依賴的補
丁)

如果你想在ARMulator上運行內核的ramdisk(虛擬磁盤),那麼你就需要從這下載另
外一個軟件包:

http://adam.kaist.ac.kr/~hschoe/dow...040412.patch.gz

5、如果你是初次編譯這些發佈包的話,OK!從這我們將會一步一步的進行
(現在把上面所下載的軟件包都發在/root/incoming目錄裏)

(On this guide, the downloaded files are gathered on ~/incoming
and the ARMulator is used for platform to run on.)

6、安裝工具鏈

以root用戶來執行arm-elf-tools-20040427.sh,像這樣:

[root@hyoklinux /]#/bin/sh ~/incoming/arm-elf-tools-20040427.sh

注:~是你的root用戶目錄,當然也可以選擇別的目錄,但這取決於你的第5步所設
置的目錄。

你將看到被安裝文件的列表,這個工具鏈的可執行的二進制文件被安裝到了/usr/
local/bin目錄裏,所以你的環境變量的搜索路徑應包括這個目錄,通常使用的發行
版像redhat等已經將這個路徑包括在$PATH變量裏,可以用下面的命令查看一下:

[root@hyoklinux /]#echo $PATH

7、釋放uClinux發佈包


在一個有2GB空間的目錄裏來釋放uclinux發佈包
[root@hyoklinux /]# tar -zxvf ~/incoming/uClinux-dist-20040408.tar.gz
讓我們看看uClinux-dist目錄裏有什麼文件
[root@hyoklinux /]# cd uClinux-dist/
[root@hyoklinux uClinux-dist]# ls -al
total 104
drwxr-xr-x 15 1000 users 4096 Apr 8 09:27 .
drwxr-xr-x 28 root root 4096 Apr 27 20:32 ..
-rw-r--r-- 1 1000 users 18007 Apr 8 09:13 COPYING
drwxr-xr-x 3 1000 users 4096 Apr 8 09:13 documentation
-rw-r--r-- 1 1000 users 9305 Apr 8 09:13 Makefile
-rw-r--r-- 1 1000 users 4934 Apr 8 09:13 README
-rw-r--r-- 1 1000 users 1743 Apr 8 09:13 SOURCE
drwxr-xr-x 2 1000 users 4096 Apr 15 15:19 bin
drwxr-xr-x 3 1000 users 4096 Apr 8 09:27 config <-- 爲本地用戶提供的配置
文件以及其它的
drwxr-xr-x 11 1000 users 4096 Apr 8 09:27 freeswan <-- IPsec的實現
drwxr-xr-x 68 1000 users 4096 Apr 8 09:23 glibc <-- GNU C庫
drwxr-xr-x 18 1000 users 4096 Apr 8 09:28 lib <-- 許多移植到uClinux的庫
,包括uC-libc.
drwxr-xr-x 15 1000 users 4096 Apr 8 09:27 linux-2.0.x <-- uClinux 2.0.x
 kernel
drwxr-xr-x 16 1000 users 4096 Apr 8 09:27 linux-2.4.x <-- uClinux 2.4.x
 kernel
drwxr-xr-x 18 1000 users 4096 Apr 8 09:27 linux-2.6.x <-- uClinux 2.6.x
 kernel
drwxr-xr-x 3 1000 users 4096 Apr 8 09:26 tools <-- 安裝romfs提供的程序以
及其它的
drwxr-xr-x 17 1000 users 4096 Apr 8 09:27 uClibc <-- uClibc可從uclibc.o
rg找到,與uC-libc不同.
drwxr-xr-x 174 1000 users 4096 Apr 8 09:27 user <-- the“userland”被移
植的應用程序
drwxr-xr-x 44 1000 users 4096 Apr 8 09:27 vendors <-- 每一個vendor/mode
ls的配置文件
注:vendor(計算機銷售商)  models(模型)

8、用這些文件製作出一個新的linux-2.6.x內核

在20040408發佈包目錄裏的linux-2.6.x是由內核“linux-2.6.2”和補丁“linux
-2.6.2-uc0”生成的。而uClinux/ARM2.6需要更新的內核版本(如linux-2.6.5 + l
inux-2.6.5-hsc2.patch)。
[root@hyoklinux uClinux-dist]# tar -jxvf ~/incoming/linux-2.6.5.tar.bz2

現在我們得到了整個的linux-2.6.5源代碼,以及所需的內核補丁。
[root@hyoklinux uClinux-dist]# gzip -dc ~/incoming/linux-2.6.5-hsc2.pat
ch.gz | patch -p0

運行上面的命令後,你將會看到一些補丁目錄裏包含有“armnommu”的字符串的代
碼。你可以在你的uClinux 2.6內核裏使用“linux-2.6.5”的目錄名,這不是必需的
。然而,我覺得使用“linux-2.6.x”這個名字會更方便些。
因此你現在不需要原來uClinux目錄裏的linux-2.6.x(即那個linux-2.6.2-uc0),
執行:
[root@hyoklinux uClinux-dist]# rm -rf linux-2.6.x/
然後我們將重命名更新補丁的內核目錄名爲“linux-2.6.x”,執行:
[root@hyoklinux uClinux-dist]# mv linux-2.6.5 linux-2.6.x

9、確定配置文件

當前的uClinux發佈包不包含有關於uClinux/ARM2.6完整的配置文件。我們需要簡
單的修改一下,ARMulator默認的配置文件在vendors/GDB/ARMulator目錄下面。我們
能像下面這樣來修改它:
[root@hyoklinux uClinux-dist]# cp linux-2.6.x/arch/armnommu/configs/GDB
_ARMulator_defconfig / vendors/GDB/ARMulator/config.linux-2.6.x

爲了rootfs能使用ramdisk,我們需要修改一下配置目錄裏的“rc”這個文件,這
樣就可以使用1號的ramdisk設備了。

(原文:Since we’ll use a ramdisk for the rootfs, we need to fix the “
rc” in the
configuration directory to use ramdisk device number 1)

像下面這樣編輯vendors/GDB/ARMulator/rc這個文件:
[root@hyoklinux uClinux-dist]# vim vendors/GDB/ARMulator/rc

hostname GDB-ARMulator
/bin/expand /etc/ramfs.img /dev/ram1
mount -t proc proc /proc
mount -t ext2 /dev/ram1 /var
mkdir /var/tmp
mkdir /var/log
mkdir /var/run
mkdir /var/lock
mkdir /var/empty
cat /etc/motd

10、配置內核和安裝用戶應用程序

[root@hyoklinux uClinux-dist]# make menuconfig
首先在“Main Menu”找到“Vendor/Product”所在行,選擇“GDB/ARMulator”,
像這樣:
--- Select the Vendor you wish to target
(GDB) Vendor
--- Select the Product you wish to target
(ARMulator) GDB Products
你可以按’esc’鍵或是“Exit”按鈕返回主菜單。

在主菜單選擇“Kernel/Library/Defaults選項”。
選擇內核版本爲“linux-2.6.x”,以及選擇使用的庫爲“uClibc”。
切換到下面的菜單:
(linux-2.6.x) Kernel Version
(uClibc) Libc Version
Star Default all settings (lose changes) (NEW)
Star Customize Kernel Settings (NEW)
Star Customize Vendor/User Settings (NEW)
Star Update Default Vendor Settings (NEW)
你可以按’esc’鍵或是“Exit”按鈕返回主菜單。

第一個“default all settings”選項是vendors目錄裏默認的配置文件。第二個
“Customize kernel settings”選項能讓我們編輯內核配置。第三個“Customize
Vendor/User settings”選項是爲了生成romfs.img所需要配置應用程序和庫。最後
一“Update Default Vendor settings”項,你對內核的改變以及你所配置的應用程
序將保存在vendors/product的目錄裏。
你可以按’esc’鍵或是“Exit”按鈕返回主菜單,然後保存並退出!

11、配置內核

如果你確認了上面的配置過程,那麼你可以得到一個配置文件。然而我們需要改變
初始的ramdis的大小。因爲romfs.img文件的大小在默認情況下大約是771KB。在“一
般設置(General setup)”菜單裏,有“Default kernel command string”這一項,
這樣改變它:

root=/dev/ram0 initrd=0x01300000,800K keepinitrd
你可以按’esc’鍵或是“Exit”按鈕返回主菜單,然後保存並退出!

程序/庫的配置

在這個菜單上,我們不需要修改任何的設置,看一下配置按“exit”保存並退出!


現在我們運行下面的這步來進行整個的編譯過程:

[root@hyoklinux uClinux-dist]# make
注:如果是編譯linux-2.4.x或是以下的版本的內核,你在“make”之前還需要“
make dep”這一步,但是在編譯linux-2.6.x的內核時就不需要了。
這一步將編譯整個內核和uclibc(uClinux的系統庫),用戶程序,以及製作romfs.
img文件。

下面是結果,你可以像下面這樣查看image目錄裏的文件:

[root@hyoklinux uClinux-dist]# ls -al images
total 3036
drwxr-xr-x 2 root root 4096 Apr 27 22:13 .
drwxr-xr-x 17 1000 users 4096 Apr 27 22:13 ..
-rw-r--r-- 1 root root 1540272 Apr 27 22:13 image.bin
-rwxr-xr-x 1 root root 45912 Apr 27 22:13 linux.data
-rwxr-xr-x 1 root root 704856 Apr 27 22:13 linux.text
-rw-r--r-- 1 root root 789504 Apr 27 22:13 romfs.img

[root@hyoklinux uClinux-dist]# ls -al linux-2.6.x/linux
-rwxr-xr-x 2 root root 15499433 Apr 27 22:11 linux-2.6.x/linux

如果你得到了相同的文件,那麼你就有了整個內核和rootfs鏡像。

12、讓我們來使用ARMulator!

安裝ARMulator,我們需要另外一個目錄來編譯GDB/ARMulator。然而這時我們將在
uClinux-dist的臨時使用的目錄裏安裝它。
[root@hyoklinux uClinux-dist]# tar -jxvf ~/incoming/gdb-5.0.tar.bz2
打上補丁:
[root@hyoklinux uClinux-dist]# gzip -dc ~/incoming/gdb-5.0-uclinux-armu
lator-20021127.patch.gz | patch -p0
執行另外的一步:
[root@hyoklinux uClinux-dist]# gzip -dc ~/incoming/gdb-5.0-uclinux-armu
lator-ramdisk-20040412.patch.gz | patch -p0

好,現在所要編譯的代碼已經準備好了,讓我們執行它:

[root@hyoklinux uClinux-dist]# cd gdb-5.0/
[root@hyoklinux gdb-5.0]# ./configure --target=arm-elf
[root@hyoklinux gdb-5.0]# make
[root@hyoklinux gdb-5.0]# make install
現在可以運行ARMulator了!

在運行ARMulator之前,我們需要一個能鏈接到鏡像文件“romfs.img”的標誌。

[root@hyoklinux gdb-5.0]# cd ..
[root@hyoklinux uClinux-dist]# ln -s images/romfs.img boot.rom

以及製作一個初始化GDB的腳本:
[root@hyoklinux uClinux-dist]# cat > .gdbinit
target sim
load
^D

通過下面的命令,就可以運行我們的內核了。
[root@hyoklinux uClinux-dist]# arm-elf-gdb linux-2.6.x/linux
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and yo
u are
welcome to change it and/or distribute copies of it under certain condi
tions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for detai
ls.
This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-elf".
..
Loaded ROM boot.rom to 0x01300000
Loaded ROM boot.rom to 0x01400000
Loaded ROM boot.rom to 0x04000000
Loaded ROM boot.rom to 0x01300000
Loaded ROM boot.rom to 0x01400000
Loaded ROM boot.rom to 0x04000000
Connected to the simulator.
Loading section .init, size 0xb000 vma 0x1000000
Loading section .text, size 0xa0b98 vma 0x100b000
Loading section __ex_table, size 0x5b8 vma 0x10abba0
Loading section .data, size 0x8ef8 vma 0x10ae000
Start address 0x1000000
Transfer rate: 5931584 bits in <1 sec.

(gdb) run

Starting program: /uClinux-dist/linux-2.6.x/linux
Linux version 2.6.5-hsc1 (root@hyoklinux) (gcc version 2.95.3 20010315
(release)(ColdFire patches - 20010318 from 
http://fiddes.net/coldfire/)(
uClinux XIP and shared lib patches from 
http://www.snapgear.com/))
#9 Mon May 3 11:00:29 KST 2004
CPU: Atmel-AT91M40xxx [14000040] revision 0 (ARMvundefined/unknown)
Machine: ATMEL EB01
On node 0 totalpages: 1024
DMA zone: 0 pages, LIFO batch:1
Normal zone: 1024 pages, LIFO batch:1
HighMem zone: 0 pages, LIFO batch:1
Built 1 zonelists
Kernel command line: root=/dev/ram0 initrd=0x01300000,800K keepinitrd
PID hash table entries: 32 (order 5: 256 bytes)
Memory: 4MB = 4MB total
Memory: 2448KB available (642K code, 92K data, 44K init)
Calibrating delay loop... 15.87 BogoMIPS
Dentry cache hash table entries: 1024 (order: 0, 4096 bytes)
Inode-cache hash table entries: 1024 (order: 0, 4096 bytes)
Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
checking if image is initramfs...it isn’t (ungzip failed); looks like
an initrd
POSIX conformance testing by UNIFIX
Linux NoNET1.0 for Linux 2.6
Atmel USART driver version 0.99
ttyS0 at 0xfffd0000 (irq = 2) is a builtin Atmel APB USART
ttyS1 at 0xfffcc000 (irq = 3) is a builtin Atmel APB USART
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
loop: loaded (max 8 devices)
RAMDISK: romfs filesystem found at block 0
RAMDISK: Loading 771 blocks [1 disk] into ram disk... done.
VFS: Mounted root (romfs filesystem) readonly.
Freeing init memory: 44K
Built 1 zonelists
Kernel command line: root=/dev/ram0 initrd=0x01300000,800K keepinitrd
PID hash table entries: 32 (order 5: 256 bytes)
Memory: 4MB = 4MB total
Memory: 2448KB available (642K code, 92K data, 44K init)
Calibrating delay loop... 15.87 BogoMIPS
Dentry cache hash table entries: 1024 (order: 0, 4096 bytes)
Inode-cache hash table entries: 1024 (order: 0, 4096 bytes)
Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
checking if image is initramfs...it isn’t (ungzip failed); looks like
an initrd
POSIX conformance testing by UNIFIX
Linux NoNET1.0 for Linux 2.6
Atmel USART driver version 0.99
ttyS0 at 0xfffd0000 (irq = 2) is a builtin Atmel APB USART
ttyS1 at 0xfffcc000 (irq = 3) is a builtin Atmel APB USART
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
loop: loaded (max 8 devices)
RAMDISK: romfs filesystem found at block 0
RAMDISK: Loading 771 blocks [1 disk] into ram disk... done.
VFS: Mounted root (romfs filesystem) readonly.
Freeing init memory: 44K
GDB/ARMulator support by <[email protected]>
For further information check:
http://www.uclinux.org/


Execution Finished, Exiting


Sash command shell (version 1.1.1)
/> ps
PID PORT STAT SIZE SHARED %CPU COMMAND
1 S 163K 0K 48.0 /sbin/init
2 S 0K 0K 0.0 ksoftirqd/0
3 S 0K 0K 1.0 events/0
4 S 0K 0K 0.0 kblockd/0
5 S 0K 0K 0.0 pdflush
6 S 0K 0K 0.0 pdflush
7 S 0K 0K 0.0 kswapd0
8 S 0K 0K 0.0 aio/0
11 S0 R 165K 0K 0.0 /bin/sh
/> cd bin
/bin> ls -al
drwxr-xr-x 1 0 0 32 Jan 1 00:00 .
drwxr-xr-x 1 0 0 32 Jan 1 00:00 ..
-rwxr-xr-x 1 0 0 105980 Jan 1 00:00 boa
-rwxr-xr-x 1 0 0 91104 Jan 1 00:00 dhcpcd
-rwxr-xr-x 1 0 0 29624 Jan 1 00:00 expand
-rwxr-xr-x 1 0 0 40504 Jan 1 00:00 gdbserver
-rwxr-xr-x 1 0 0 73748 Jan 1 00:00 ifconfig
-rwxr-xr-x 1 0 0 41144 Jan 1 00:00 inetd
-rwxr-xr-x 1 0 0 31176 Jan 1 00:00 init
-rwxr-xr-x 1 0 0 56380 Jan 1 00:00 login
-rwxr-xr-x 1 0 0 64656 Jan 1 00:00 ping
-rwxr-xr-x 1 0 0 34280 Jan 1 00:00 reboot
-rwxr-xr-x 1 0 0 73860 Jan 1 00:00 route
-rwxr-xr-x 1 0 0 79852 Jan 1 00:00 sh
-rwxr-xr-x 1 0 0 51216 Jan 1 00:00 telnetd
/bin> cat /proc/meminfo
MemTotal: 2516 kB
MemFree: 560 kB
Buffers: 900 kB
Cached: 188 kB
SwapCached: 0 kB
Active: 984 kB
Inactive: 44 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 2516 kB
LowFree: 560 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
Mapped: 0 kB
Slab: 752 kB
Committed_AS: 0 kB
PageTables: 0 kB
VmallocTotal: 4194303 kB
VmallocUsed: 0 kB
VmallocChunk: 4194303 kB
/bin>

不錯的文章,值得讀一下,翻譯時來來回回讀了好幾遍,每次都有不同的收穫,感
覺不錯

由於時間倉促,未能測試,請大家和我關注這個貼子,使它能更準確、更完善!
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章