把自己的应用程序加载到uClinux中运行

来源:互联网 发布:java webservice 框架 编辑:程序博客网 时间:2024/04/30 19:26
昨天终于顺利完成了在SkyEye上面仿真运行uClinux的工作,今天决心乘胜追击,把一个hello,world程序加载到uClinux中去运行。经过差不多一天的摸索,到晚饭时分终于成功!下面把开发过程总结一下。

    首先是应用程序的存放位置。在uClinux的安装目录(我的机器上是/home/user/uClinux-dist)下有个user目录,里面存放的就是系统的应用程序,一般规律是一个应用程序一个目录。

    在Debian Linux系统中打开一个终端,默认就在用户主目录下(我的机器就是/home/user),然后在uClinux的user目录下面建立了一个名为test的目录:

        cd uClinux/user/

        mkdir test

    然后在test目录下建立一个名为hello.c的C程序,源代码如下:

#include <stdio.h>
int main(void)
{
 printf(
"Hello,uClinux! ");
 
return(0);
}

    然后使用交叉编译器编译hello.c源程序,生成名为hello的可执行文件:

        arm-elf-gcc -Wall -elf2flt -o hello hello.c

    再将该可执行文件复制到uClinux映像文件目录(/home/user/uClinux/romfs/bin)中:

        cp ./hello ../../romfs/bin/

    然后在test目录下建立一个Makefile文件,内容如下(其实是从user下的其他目录中复制过来稍加修改得到的):

EXEC = hello
OBJS = hello.o

all: $(EXEC)

$(EXEC): $(OBJS)
        $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

$(OBJS): hello.c

romfs:
        $(ROMFSINST) /bin/$(EXEC)

clean:
        -rm -f $(EXEC) *.elf *.gdb *.o

    将来大家可以照着这个模板来修改其中的关联关系即可,其余的嘛,可以不动。

    接下来要对uClinux的一些配置文件进行修改,告诉uClinux系统添加了一个User Application。

  • 编辑uClinux目录下的文件user/Makefile,增加下面这行文字:

            dir_$(CONFIG_USER_TEST_HELLO)    += test

    在这里注意两个问题:一是增加的位置,打开文件就会发现这个Makefile里面有很多类似的语句,只要按照字母顺序,把上面这行放到合适的位置即可;二是注意上面这行文字的颜色变化,黑色部分其实跟其他行是一样的,红色部分则是我们在user中添加的那个用户程序目录,注意前面出现是全大写,后面出现是要与用户目录一致的,而蓝色部分则是用户程序编译之后生成的那个可执行文件的名字,要全大写。

  • 编辑uClinux目录下的文件config/Configure.help,在文件末尾追加下面这段文字:
CONFIG_USER_TEST_HELLO
  This is an example C program in uClinux OS, and it will print "Hello, uClinux!" on screen.

    注意:第一行要顶头写,第二行则要以2个空格开头,总长度小于70个字符。

  • 编辑uClinux目录下的文件config/config.in,在文件末尾追加下面这段文字:
#########################
mainmenu_option next_comment
comment 'Test'

bool  'hello'                     CONFIG_USER_TEST_HELLO
endmenu
#########################

    然后返回uClinux的根目录下,重新编译内核,不过这个过程就不用像第一遍编译内核那么复杂了,可以只编译连接最后需要生成的那个镜像文件即可:

        genromfs -v -V "ROMdisk" -f ./images/romfs.img -d ./romfs

        make image

        make

    前面两步操作会给出一个内存映像表,在里面可以发现有一个名为hello的应用程序。如果没有错误的话会很快完成,最后一步make就要花些时间了,去食堂吃饭打个来回貌似还是没问题的,呵呵~~

    等最后的make完成,那么新的内核即linux-2.4.x/linux文件已经生成了,使用SkyEye仿真运行新的内核:

        skyeye -e ./linux-2.4.x/linux

等uClinux启动完成出现“>”提示符时,输入hello并回车,系统将会显示什么呢?看看下面的记录吧,呵呵~~

$ skyeye -e ./linux-2.4.x/linux
big_endian is false.
arch: arm
cpu info: armv3, arm7tdmi, 41007700, fff8ff00, 0
mach info: name at91, mach_init addr 0x8062ea0
can't find device module: (name:lcd, type:(null))
uart_mod:0, desc_in:, desc_out:, converter:
SKYEYE: use arm7100 mmu ops
Loaded ROM   images/romfs.img
exec file "./linux-2.4.x/linux"'s format is elf32-little.
load section .init: addr = 0x01000000  size = 0x0000a000.
load section .text: addr = 0x0100a000  size = 0x000e4d00.
load section .data: addr = 0x010f0000  size = 0x0000a6d8.
not load section .bss: addr = 0x010fa6e0  size = 0x00022700 .
not load section .debug_abbrev: addr = 0x00000000  size = 0x0003ec44 .
not load section .debug_info: addr = 0x00000000  size = 0x019d8a71 .
not load section .debug_line: addr = 0x00000000  size = 0x00304781 .
not load section .debug_pubnames: addr = 0x00000000  size = 0x0000c669 .
not load section .debug_aranges: addr = 0x00000000  size = 0x000024d8 .
start addr is set to 0x01000000 by exec file.
Linux version 2.4.32-uc0 (user@yxhcs11) (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/)) #6 2007年 11月 16日 星期五 17:03:24 CST
Processor: Atmel AT91M40xxx revision 0
Architecture: EB01
On node 0 totalpages: 1024
zone(0): 0 pages.
zone(1): 1024 pages.
zone(2): 0 pages.
Kernel command line: root=/dev/rom0
Calibrating delay loop... 15.82 BogoMIPS
Memory: 4MB = 4MB total
Memory: 2900KB available (915K code, 184K data, 40K init)
Dentry cache hash table entries: 512 (order: 0, 4096 bytes)
Inode cache hash table entries: 512 (order: 0, 4096 bytes)
Mount cache hash table entries: 512 (order: 0, 4096 bytes)
Buffer cache hash table entries: 1024 (order: 0, 4096 bytes)
Page-cache hash table entries: 1024 (order: 0, 4096 bytes)
POSIX conformance testing by UNIFIX
Linux NET4.0 for Linux 2.4
Based upon Swansea University Computer Society NET3.039
Initializing RT netlink socket
Starting kswapd
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
Blkmem copyright 1998,1999 D. Jeff Dionne
Blkmem copyright 1998 Kenneth Albanowski
Blkmem 1 disk images:
0: 1400000-151EFFF [VIRTUAL 1400000-151EFFF] (RO) 
<ROMFS>
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
SkyEye NE2k Ethernet driver version 0.2 (2003-04-27)
sene2k dev name: eth0: 
<6>NET4: Linux TCP/IP 1.0 for NET4.0
IP Protocols: ICMP, UDP, TCP
IP: routing cache hash table of 512 buckets, 4Kbytes
TCP: Hash tables configured (established 512 bind 512)
NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.
VFS: Mounted root (romfs filesystem) readonly.
Shell invoked to run file: /etc/rc
Command: hostname GDB-ARMulator
Command: /bin/expand /etc/ramfs.img /dev/ram0
Command: mount -t proc proc /proc
mount: /etc/mtab: Read-only file system
Command: mount -t ext2 /dev/ram0 /var
mount: /etc/mtab: Read-only file system
Command: mkdir /var/tmp
Command: mkdir /var/log
Command: mkdir /var/run
Command: mkdir /var/lock
Command: mkdir /var/empty
Command: cat /etc/motd
Welcome to
          ____ _  _
         /  __| ||_|
    _   _| |  | | _ ____  _   _  _  _
   | | | | |  | || |  _ | | | | / /
   | |_| | |__| || | | | | |_| |/    
   |  _______|_||_|_| |_|____|_/_/
   | |
   |_|

GDB/ARMulator support by 
<davidm@snapgear.com>
For further information check:
http://www.uclinux.org/

Command: /bin/ifconfig eth0 up 10.0.0.2
Execution Finished, Exiting

Sash command shell (version 1.1.1)
/> hello
Hello,uClinux!
/>

    往后,只要我们继续扩充这个hello.c,或者继续在test增加其他的文件比如说.h或者.c文件都可以的,不过记得要修改Makefile啊。


 
原创粉丝点击