VxWorks里经常会看见有下面这2个组件,INCLUDE_MEMDRV和INCLUDE_RAMDRV,特别容易混淆,而且不明白它们具体是干什么用的,今天做了个研究,共享之。

1. INCLUDE_MEMDRV(memDrv)

Component INCLUDE_MEMDRV {
        NAME MEM disk driver
        SYNOPSIS allows a filesystem to be put on top of memory
        MODULES memDrv.o
        INIT_RTN memDrv ();
        HDR_FILES memDrv.h
}

 

#ifdef INCLUDE_MEMDRV

memDrv ();

#endif

typedef struct

{

DEV_HDR devHdr;

MEM_DRV_DIRENTRY dir;

off_t allowOffset;

} MEM_DEV;

风河注释:

The memDrv device allows the I/O system to access memory directly as a pseudo-I/O device. Memory location and size are specified when the device is created. The device provides a high-level means for reading and writing bytes in absolute memory locations through I/O calls. It is useful when data must be preserved between boots of VxWorks or when sharing data between CPUs. The memDrv driver is initialized automatically by the system with memDrv( ) when the INCLUDE_USR_MEMDRV component is included in VxWorks. The call for device creation must be made from the kernel:

STATUS memDevCreate (char * name, char * base, int length)

Memory for the device is an absolute memory location beginning at base. The length parameter indicates the size of the memory.

For additional information on the memory driver, see the memDrv( ), memDevCreate( ), and memDevCreateDir( ) entries in the VxWorks API reference.

我的理解是,把memDevCreate出来的这些存贮区域(可以是内存也可以是norflash上的一段地址空间)当成一个"文件"来用,一个假的IO设备,可以调用标准的IO操作和文件操作来访问这块区域。

memDrv的一个典型应用就是可以把VxWorks烧写到norflash的一片区域里,这片区域一定要通过memDevCreate创建,bootrom就可以直接加载memDevCreate里存的VxWorks,在无网络的情况调试非常方便。直接上代码:

比如从norFlash的0xFF800000(MMU 映射过)开始放VxWorks image,编译出来的VxWorks image的大小是0x1F6FA0

在config.h中加入:

#define INCLUDE_BOOT_FILESYSTEMS
#define INCLUDE_MEMDRV

在usrConfig.c中memDrv初始化之后加入:

memDevCreate("/mem/",0xFF800000, 0x1F6FA0);

注意:size一定要对,不能比实际的size小了。

重新编译bootrom并烧写到板子上,并且把vxWorks image烧写到0xFF800000开始处

[VxWorks Boot]: devs
drv name
0 /null
1 /tyCo/0
1 /tyCo/1
5 /mem/
3 /tffs0
3 /tffs1
7 host:
[VxWorks Boot]:d 0xff800000
0xff800000: 7f45 4c46 0102 0100  0000 0000 0000 0000  *.ELF............*
0xff800010: 0002 0014 0000 0001  0001 0000 0000 0034  *...............4*
0xff800020: 002c dab4 8000 0000  0034 0020 0004 0028  *.,.......4. ...(*
0xff800030: 001c 001b 0000 0001  0000 00c0 0001 0000  *................*
0xff800040: 0001 0000 001c ef74  001c ef74 0000 0007  *.......t...t....*
0xff800050: 0000 0001 0000 0001  001c f034 001d ef74  *...........4...t*
0xff800060: 001d ef74 0000 012c  0000 012c 0000 0006  *...t...,...,....*
0xff800070: 0000 0001 0000 0001  001c f160 001d f0a0  *...........`....*
[VxWorks Boot]: @

boot device          : fs
unit number          : 0
processor number     : 0
host name            : host
file name            : /mem/0
inet on ethernet (e) : 192.168.101.108:fffffe00
host inet (h)        : 192.168.101.63
gateway inet (g)     : 192.168.101.1
user (u)             : target
ftp password (pw)    : vxTarget
flags (f)            : 0x0
other (o)            : motetsec1

Loading /mem/0...1896308 + 300 + 568 + 335992
Starting at 0x10000...


Adding 7654 symbols for standalone.


VxWorks

Copyright 1984-2012  Wind River Systems, Inc.

CPU: Unsupported processor
Runtime Name: VxWorks
Runtime Version: 6.6
BSP version: 6.6/0
Created: Jul 26 2012, 10:06:01
ED&R Policy Mode: Deployed
WDB Comm Type: WDB_COMM_END
WDB: Ready.

->

2. INCLUDE_RAMDRV(ramDrv)

Component INCLUDE_RAMDRV {
        NAME RAM disk driver
        SYNOPSIS allows a filesystem to be put on top of RAM
        MODULES ramDrv.o
        INIT_RTN ramDrv ();
        HDR_FILES ramDrv.h
}

 
 
#ifdef INCLUDE_RAMDRV 
        ramDrv (); 
#endif 
 
typedef struct 
{ 
        BLK_DEV ram_blkdev; 
        off_t ram_blkOffset; 
        char *ram_addr; 
} RAM_DEV; 
 

风河注释:

This driver emulates a disk driver, but actually keeps all data in memory. Once the device has been created, it must be associated with a name and file system (dosFs, hrfs, or rawFs). This is accomplished in a two step process. The ramDevCreate() call returns a pointer to a block device structure (BLK_DEV). This structure contains fields that describe the physical properties of a disk device and specify the addresses of routines within the ramDrv driver. The BLK_DEV structure address should be passed to an XBD wrapper via xbdBlkDevCreate() along with the name of the device. XBDs are the new and preferred method for interfacing with file systems.

After the XBD wrapper is created, the file system framework will attempt to identify the type of file system instantiated on the device. If it can not be identified, then it is instantiated with rawFs. The desired file system (dosFs or hrfs) can be instantiated on the ram drive using either dosFsVolFormat(), dosfsDiskFormat(),hrfsFormat(), or hrfsDiskFormat(). The ram drive to be formatted is identified by the name of the device given in the XBD wrapper.

EXAMPLE
In the following example, a 208-Kbyte RAM disk is created with automatically allocated memory, 512-byte blocks, 32 blocks per track, and no block offset. The device is then initialized for use with dosFs and assigned the name "/ramDrv":
BLK_DEV *pBlkDev;
pBlkDev = ramDevCreate (NULL,  512,  32,  416,  0);
xbdBlkDevCreate (pBlkDev, "/ramDrv");
dosFsVolFormat ("/ramDrv:0", DOS_OPT_BLANK, NULL);

The names used in xbdBlkDevCreate() and dosFsVolFormat() are slightly different on purpose. The ":0" is appended to "/ramDrv" by xbdBlkDevCreate() and represents the whole (unpartitioned) disk.

If the RAM disk memory already contains a disk image created elsewhere, the first argument to ramDevCreate() should be the address in memory, and the formatting parameters -- , , , and -- must be identical to those used when the image was created.

For example:

pBlkDev = ramDevCreate (0xc0000, 512, 32, 416, 0);

In this case, the file system does not have to be explicitly created as the file system framework will probe the ram drive to determine the type of file system previously instantiated on it. The detected file system will be automatically re-instantiated on the device. This procedure is useful if a RAM disk is to be created at the same address used in a previous boot of VxWorks. The contents of the RAM disk will then be preserved.

BLK_DEV* ramDevCreate
(
        char *ramAddr,
        int bytesPerBlk,
        int blksPerTrack,
        int nBlocks,
        off_t blkOffset
)

不得不说,风河的注释写的太好了,什么意思一看就明白了,要注意的是ramDrv只能用于内存,flash不可以。

下面是我实现的用ramDrv+hrFs来加载VxWorks image的code:

1.config.h中加入

#  define INCLUDE_RAMDRV
#  define INCLUDE_DISK_UTIL
#  define INCLUDE_DEVICE_MANAGER
#  define INCLUDE_XBD
#  define INCLUDE_XBD_BLK_DEV
#  define INCLUDE_XBD_PART_LIB
#  define INCLUDE_FS_MONITOR
#  define INCLUDE_ERF
#  define INCLUDE_FS_EVENT_UTIL
#  define INCLUDE_BOOT_FILESYSTEMS

#  define INCLUDE_HRFS
#  define INCLUDE_HRFS_FORMAT
#  define INCLUDE_HRFS_CHKDSK
(如果不想用hrfs,可以看我之前的一篇怎样用dosfs)

2.用用户保留区的内存来申请ramDisk(用malloc的话复位后会清0)

#define USER_RESERVED_MEM 0x600000

3. usrConfig.c中在ramDrv()之后加入:

BLK_DEV * pBlkDev1;
pBlkDev1 = ramDevCreate((char *) sysMemTop(), 512, 6144, 6144, 0);
xbdBlkDevCreateSync(pBlkDev1, "/ram");

4. 编译bootrom烧写后加载vxworks image,格式化"/ram"并且把vxworks image拷贝到"/ram"里。

[VxWorks Boot]: devs
drv name
0 /null
1 /tyCo/0
1 /tyCo/1
3 /ram:0
5 /mem/
3 /tffs0
3 /tffs1
7 host:
[VxWorks Boot]: @

boot device          : fs
unit number          : 0
processor number     : 0
host name            : host
file name            : /ram:0/vxWorks
inet on ethernet (e) : 192.168.101.11:fffffe00
host inet (h)        : 192.168.101.4
gateway inet (g)     : 192.168.101.1
user (u)             : target
ftp password (pw)    : vxTarget
flags (f)            : 0x0
other (o)            : motetsec1

Loading /ram:0/vxWorks...1458740 + 188 + 596 + 212476
Starting at 0x100000...

Target Name: vxTarget


VxWorks

Copyright 1984-2011  Wind River Systems, Inc.

CPU: Freescale P1020E - Security Engine
Runtime Name: VxWorks
Runtime Version: 6.6
BSP version: 6.6/4
Created: Jul  5 2012, 11:53:01
ED&R Policy Mode: Deployed
WDB Comm Type: WDB_COMM_END
WDB: Agent Disabled.
小结:

ramDrv只能用于内存中并且要和FS一起用,它是个块设备,被模拟成一个块disk。

memDrv可以在内存或者Flash中使用,不需要FS参与,被模拟成一个IO或者文件设备。