mirror of
https://github.com/apache/nuttx.git
synced 2026-09-19 20:39:39 +08:00
Added mmap()/XIP test to ROMFS test
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@917 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -472,5 +472,6 @@
|
||||
ioctl command in the ROMFS filesystem. This is a requirement for eXecute
|
||||
In Place (XIP) support.
|
||||
* Add mmap() API with restricted capability (only for XIP support)
|
||||
* Extend ROMFS test at /examples/romfs to verify mmap() and XIP support.
|
||||
|
||||
|
||||
|
||||
@@ -1106,6 +1106,7 @@ nuttx-0.3.15 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
ioctl command in the ROMFS filesystem. This is a requirement for eXecute
|
||||
In Place (XIP) support.
|
||||
* Add mmap() API with restricted capability (only for XIP support)
|
||||
* Extend ROMFS test at /examples/romfs to verify mmap() and XIP support.
|
||||
|
||||
pascal-0.1.3 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+91
-12
@@ -59,10 +59,13 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
|
||||
@@ -105,6 +108,8 @@
|
||||
#define MKMOUNT_DEVNAME(m) "/dev/ram" STR_RAMDEVNO(m)
|
||||
#define MOUNT_DEVNAME MKMOUNT_DEVNAME(CONFIG_EXAMPLES_ROMFS_RAMDEVNO)
|
||||
|
||||
#define SCRATCHBUFFER_SIZE 1024
|
||||
|
||||
/* Test directory stuff */
|
||||
|
||||
#define WRITABLE_MODE (S_IWOTH|S_IWGRP|S_IWUSR)
|
||||
@@ -137,10 +142,10 @@ struct node_s
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const char g_afilecontent[] = "This is a file";
|
||||
static const char g_anotherfilecontent[] = "This is another file";
|
||||
static const char g_yafilecontent[] = "This is yet another file";
|
||||
static const char g_subdirfilecontent[] = "File in subdirectory";
|
||||
static const char g_afilecontent[] = "This is a file\n";
|
||||
static const char g_anotherfilecontent[] = "This is another file\n";
|
||||
static const char g_yafilecontent[] = "This is yet another file\n";
|
||||
static const char g_subdirfilecontent[] = "File in subdirectory\n";
|
||||
|
||||
#define g_hfilecontent g_subdirfilecontent
|
||||
|
||||
@@ -156,7 +161,7 @@ static struct node_s g_subdirfile;
|
||||
|
||||
static int g_nerrors = 0;
|
||||
|
||||
static char g_pathbuffer[1024];
|
||||
static char g_scratchbuffer[SCRATCHBUFFER_SIZE];
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
@@ -181,7 +186,7 @@ static void connectem(void)
|
||||
g_afile.found = FALSE;
|
||||
g_afile.name = "afile.txt";
|
||||
g_afile.mode = FILE_MODE;
|
||||
g_afile.size = strlen(g_afilecontent)+1;
|
||||
g_afile.size = strlen(g_afilecontent);
|
||||
g_afile.u.filecontent = g_afilecontent;
|
||||
|
||||
g_hfile.peer = NULL;
|
||||
@@ -189,7 +194,7 @@ static void connectem(void)
|
||||
g_hfile.found = FALSE;
|
||||
g_hfile.name = "hfile";
|
||||
g_hfile.mode = FILE_MODE;
|
||||
g_hfile.size = strlen(g_hfilecontent)+1;
|
||||
g_hfile.size = strlen(g_hfilecontent);
|
||||
g_hfile.u.filecontent = g_hfilecontent;
|
||||
|
||||
g_anotherfile.peer = &g_yafile;
|
||||
@@ -197,7 +202,7 @@ static void connectem(void)
|
||||
g_anotherfile.found = FALSE;
|
||||
g_anotherfile.name = "anotherfile.txt";
|
||||
g_anotherfile.mode = FILE_MODE;
|
||||
g_anotherfile.size = strlen(g_anotherfilecontent)+1;
|
||||
g_anotherfile.size = strlen(g_anotherfilecontent);
|
||||
g_anotherfile.u.filecontent = g_anotherfilecontent;
|
||||
|
||||
g_yafile.peer = &g_subdir;
|
||||
@@ -205,7 +210,7 @@ static void connectem(void)
|
||||
g_yafile.found = FALSE;
|
||||
g_yafile.name = "yafile.txt";
|
||||
g_yafile.mode = FILE_MODE;
|
||||
g_yafile.size = strlen(g_yafilecontent)+1;
|
||||
g_yafile.size = strlen(g_yafilecontent);
|
||||
g_yafile.u.filecontent = g_yafilecontent;
|
||||
|
||||
g_subdir.peer = NULL;
|
||||
@@ -221,7 +226,7 @@ static void connectem(void)
|
||||
g_subdirfile.found = FALSE;
|
||||
g_subdirfile.name = "subdirfile.txt";
|
||||
g_subdirfile.mode = FILE_MODE;
|
||||
g_subdirfile.size = strlen(g_subdirfilecontent)+1;
|
||||
g_subdirfile.size = strlen(g_subdirfilecontent);
|
||||
g_subdirfile.u.filecontent = g_subdirfilecontent;
|
||||
}
|
||||
|
||||
@@ -272,6 +277,79 @@ static void checkattributes(const char *path, mode_t mode, size_t size)
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: checkfile
|
||||
****************************************************************************/
|
||||
|
||||
static void checkfile(const char *path, struct node_s *node)
|
||||
{
|
||||
ssize_t nbytesread;
|
||||
char *filedata;
|
||||
int fd;
|
||||
|
||||
/* Open the file */
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
printf(" -- ERROR: Failed to open %s: %d\n", path, errno);
|
||||
g_nerrors++;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Read and verify the file contents */
|
||||
|
||||
nbytesread = read(fd, g_scratchbuffer, SCRATCHBUFFER_SIZE);
|
||||
if (nbytesread < 0)
|
||||
{
|
||||
printf(" -- ERROR: Failed to read from %s: %d\n", path, errno);
|
||||
g_nerrors++;
|
||||
}
|
||||
else if (nbytesread != node->size)
|
||||
{
|
||||
printf(" -- ERROR: Read %d bytes, expected %d\n", nbytesread, node->size);
|
||||
g_nerrors++;
|
||||
}
|
||||
else if (memcmp(g_scratchbuffer, node->u.filecontent, node->size) != 0)
|
||||
{
|
||||
g_scratchbuffer[nbytesread] = '\0';
|
||||
printf(" -- ERROR: File content read does not match expectation:\n");
|
||||
printf(" -- Read: [%s]\n", g_scratchbuffer);
|
||||
printf(" -- Expected: [%s]\n", node->u.filecontent);
|
||||
g_nerrors++;
|
||||
}
|
||||
|
||||
/* Memory map and verify the file contents */
|
||||
|
||||
filedata = (char*)mmap(NULL, node->size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
|
||||
if (!filedata || filedata == (char*)MAP_FAILED)
|
||||
{
|
||||
printf(" -- ERROR: mmap of %s failed: %d\n", path, errno);
|
||||
g_nerrors++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (memcmp(filedata, node->u.filecontent, node->size) != 0)
|
||||
{
|
||||
memcpy(g_scratchbuffer, filedata, node->size);
|
||||
g_scratchbuffer[node->size] = '\0';
|
||||
printf(" -- ERROR: Mapped file content read does not match expectation:\n");
|
||||
printf(" -- Memory: [%s]\n", filedata);
|
||||
printf(" -- Expected: [%s]\n", node->u.filecontent);
|
||||
g_nerrors++;
|
||||
}
|
||||
munmap(filedata, node->size);
|
||||
}
|
||||
|
||||
/* Close the file */
|
||||
|
||||
if (close(fd) != OK)
|
||||
{
|
||||
printf(" -- ERROR: Failed to close %s: %d\n", path, errno);
|
||||
g_nerrors++;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: readdirectories
|
||||
****************************************************************************/
|
||||
@@ -310,8 +388,8 @@ static void readdirectories(const char *path, struct node_s *entry)
|
||||
|
||||
/* Get the full path to the entry */
|
||||
|
||||
sprintf(g_pathbuffer, "%s/%s", path, direntry->d_name);
|
||||
fullpath = strdup(g_pathbuffer);
|
||||
sprintf(g_scratchbuffer, "%s/%s", path, direntry->d_name);
|
||||
fullpath = strdup(g_scratchbuffer);
|
||||
|
||||
if (DIRENT_ISDIRECTORY(direntry->d_type))
|
||||
{
|
||||
@@ -339,6 +417,7 @@ static void readdirectories(const char *path, struct node_s *entry)
|
||||
else
|
||||
{
|
||||
checkattributes(fullpath, node->mode, node->size);
|
||||
checkfile(fullpath, node);
|
||||
}
|
||||
}
|
||||
free(fullpath);
|
||||
|
||||
+6
-6
@@ -57,21 +57,21 @@
|
||||
* Description:
|
||||
* NuttX operates in a flat open address space. Therefore, it generally
|
||||
* does not require mmap() functionality. There is one one exception:
|
||||
* mmap is the API that is used to support direct access to random
|
||||
* mmap() is the API that is used to support direct access to random
|
||||
* access media under the following very restrictive conditions:
|
||||
*
|
||||
* 1. The filesystem supports the FIOC_MMAP ioctl command. Any file system
|
||||
* that maps files contiguously on the media should support this ioctl.
|
||||
* (vs. file system that scatter files over the media in non-contigous
|
||||
* (vs. file system that scatter files over the media in non-contiguous
|
||||
* sectors). As of this writing, ROMFS is the only file system that
|
||||
* meets this requirement.
|
||||
* 2. The underly block driver supports the BIOC_XIPBASE ioctl command.
|
||||
* 2. The underly block driver supports the BIOC_XIPBASE ioctl command
|
||||
* that maps the underlying media to a randomly accessible address. At
|
||||
* present, on the RAM/ROM disk driver does this.
|
||||
* present, only the RAM/ROM disk driver does this.
|
||||
*
|
||||
* Parameters:
|
||||
* start A hint at where to map the memory -- ignored. The address
|
||||
* of the underlying media is fixed and cannot be re-mapped with
|
||||
* of the underlying media is fixed and cannot be re-mapped withou
|
||||
* MMU support.
|
||||
* length The length of the mapping -- ignored. The entire underlying
|
||||
* media is always accessible.
|
||||
@@ -94,7 +94,7 @@
|
||||
* MAP_NORESERVE - Ignored
|
||||
* MAP_POPULATE - Ignored
|
||||
* MAP_NONBLOCK - Ignored
|
||||
* fd file descriptor of backing file -- required.
|
||||
* fd file descriptor of the backing file -- required.
|
||||
* offset The offset into the file to map
|
||||
*
|
||||
* Return:
|
||||
|
||||
+13
-3
@@ -211,13 +211,23 @@ static int romfs_open(FAR struct file *filep, const char *relpath,
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
|
||||
/* Initialize the file private data (only need to initialize non-zero elements) */
|
||||
/* Initialize the file private data (only need to initialize
|
||||
* non-zero elements)
|
||||
*/
|
||||
|
||||
rf->rf_open = TRUE;
|
||||
rf->rf_startoffset = romfs_datastart(rm, dirinfo.rd_dir.fr_curroffset);
|
||||
rf->rf_size = dirinfo.rd_size;
|
||||
|
||||
/* Confiure a buffering to support access to this file */
|
||||
/* Get the start of the file data */
|
||||
|
||||
ret = romfs_datastart(rm, dirinfo.rd_dir.fr_curroffset,
|
||||
&rf->rf_startoffset);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto errout_with_semaphore;
|
||||
}
|
||||
|
||||
/* Configure buffering to support access to this file */
|
||||
|
||||
ret = romfs_fileconfigure(rm, rf);
|
||||
if (ret < 0)
|
||||
|
||||
+10
-9
@@ -198,24 +198,25 @@ extern "C" {
|
||||
EXTERN void romfs_semtake(struct romfs_mountpt_s *rm);
|
||||
EXTERN void romfs_semgive(struct romfs_mountpt_s *rm);
|
||||
EXTERN int romfs_hwread(struct romfs_mountpt_s *rm, ubyte *buffer,
|
||||
uint32 sector, unsigned int nsectors);
|
||||
uint32 sector, unsigned int nsectors);
|
||||
EXTERN int romfs_devcacheread(struct romfs_mountpt_s *rm, uint32 sector);
|
||||
EXTERN int romfs_filecacheread(struct romfs_mountpt_s *rm,
|
||||
struct romfs_file_s *rf, uint32 sector);
|
||||
struct romfs_file_s *rf, uint32 sector);
|
||||
EXTERN int romfs_hwconfigure(struct romfs_mountpt_s *rm);
|
||||
EXTERN int romfs_fsconfigure(struct romfs_mountpt_s *rm);
|
||||
EXTERN int romfs_fileconfigure(struct romfs_mountpt_s *rm,
|
||||
struct romfs_file_s *rf);
|
||||
struct romfs_file_s *rf);
|
||||
EXTERN int romfs_checkmount(struct romfs_mountpt_s *rm);
|
||||
EXTERN int romfs_finddirentry(struct romfs_mountpt_s *rm,
|
||||
struct romfs_dirinfo_s *dirinfo,
|
||||
const char *path);
|
||||
struct romfs_dirinfo_s *dirinfo,
|
||||
const char *path);
|
||||
EXTERN int romfs_parsedirentry(struct romfs_mountpt_s *rm,
|
||||
uint32 offset, uint32 *poffset, uint32 *pnext,
|
||||
uint32 *pinfo, uint32 *psize);
|
||||
uint32 offset, uint32 *poffset, uint32 *pnext,
|
||||
uint32 *pinfo, uint32 *psize);
|
||||
EXTERN int romfs_parsefilename(struct romfs_mountpt_s *rm, uint32 offset,
|
||||
char *pname);
|
||||
EXTERN uint32 romfs_datastart(struct romfs_mountpt_s *rm, uint32 offset);
|
||||
char *pname);
|
||||
EXTERN int romfs_datastart(struct romfs_mountpt_s *rm, uint32 offset,
|
||||
uint32 *start);
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
|
||||
+50
-11
@@ -914,13 +914,45 @@ int romfs_parsefilename(struct romfs_mountpt_s *rm, uint32 offset, char *pname)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
uint32 romfs_datastart(struct romfs_mountpt_s *rm, uint32 offset)
|
||||
int romfs_datastart(struct romfs_mountpt_s *rm, uint32 offset, uint32 *start)
|
||||
{
|
||||
uint32 sector;
|
||||
uint32 next;
|
||||
uint32 info;
|
||||
uint16 ndx;
|
||||
int ret;
|
||||
|
||||
/* Loop until the header size of obtained. */
|
||||
/* Loop while we traverse any hardlinks */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Convert the offset into sector + index */
|
||||
|
||||
sector = SEC_NSECTORS(rm, offset);
|
||||
ndx = offset & SEC_NDXMASK(rm);
|
||||
|
||||
/* Read the sector into memory */
|
||||
|
||||
ret = romfs_devcacheread(rm, sector);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Check if this is a hard link */
|
||||
|
||||
next = romfs_devread32(rm, ndx + ROMFS_FHDR_NEXT);
|
||||
if ((next & RFNEXT_MODEMASK) != RFNEXT_HARDLINK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* Follow the hard-link */
|
||||
|
||||
offset = romfs_devread32(rm, ndx + ROMFS_FHDR_INFO);
|
||||
}
|
||||
|
||||
/* Loop until the header size is obtained. */
|
||||
|
||||
offset += ROMFS_FHDR_NAME;
|
||||
for (;;)
|
||||
@@ -930,26 +962,33 @@ uint32 romfs_datastart(struct romfs_mountpt_s *rm, uint32 offset)
|
||||
sector = SEC_NSECTORS(rm, offset);
|
||||
ndx = offset & SEC_NDXMASK(rm);
|
||||
|
||||
/* Get the offset to the next chunk */
|
||||
|
||||
offset += 16;
|
||||
DEBUGASSERT(offset < rm->rm_volsize);
|
||||
|
||||
/* Read the sector into memory */
|
||||
|
||||
ret = romfs_devcacheread(rm, sector);
|
||||
DEBUGASSERT(ret >= 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Get the offset to the next chunk */
|
||||
|
||||
offset += 16;
|
||||
if (offset >= rm->rm_volsize)
|
||||
{
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* Is the name terminated in this 16-byte block */
|
||||
|
||||
if (rm->rm_buffer[ndx + 15] == '\0')
|
||||
{
|
||||
/* Yes.. then the data starts after this chunk */
|
||||
/* Yes.. then the data starts at the next chunk */
|
||||
|
||||
return offset;
|
||||
*start = offset;
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
return ERROR; /* Won't get here */
|
||||
return -EINVAL; /* Won't get here */
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user