9
0
Fork 0

reactivate cramfs

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2009-06-11 15:34:21 +02:00
parent 02ba8a0542
commit b42f481fa4
1 changed files with 91 additions and 80 deletions

View File

@ -43,18 +43,24 @@
struct cramfs_priv { struct cramfs_priv {
struct cramfs_super super; struct cramfs_super super;
int curr_block; int curr_base;
struct cramfs_inode *inode;
char buf[4096]; char buf[4096];
int curr_block_len; size_t curr_block_len;
struct cdev *cdev;
}; };
static int cramfs_read_super (struct device_d *dev, struct cramfs_priv *priv) struct cramfs_inode_info {
struct cramfs_inode inode;
unsigned long *block_ptrs;
};
static int cramfs_read_super(struct cramfs_priv *priv)
{ {
unsigned long root_offset; unsigned long root_offset;
struct cramfs_super *super = &priv->super; struct cramfs_super *super = &priv->super;
struct cdev *cdev = priv->cdev;
if (dev_read(dev, super, sizeof (struct cramfs_super), 0, 0) < sizeof (struct cramfs_super)) { if (cdev_read(cdev, super, sizeof (struct cramfs_super), 0, 0) < sizeof (struct cramfs_super)) {
printf("read superblock failed\n"); printf("read superblock failed\n");
return -EINVAL; return -EINVAL;
} }
@ -62,7 +68,7 @@ static int cramfs_read_super (struct device_d *dev, struct cramfs_priv *priv)
/* Do sanity checks on the superblock */ /* Do sanity checks on the superblock */
if (super->magic != CRAMFS_32 (CRAMFS_MAGIC)) { if (super->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
/* check at 512 byte offset */ /* check at 512 byte offset */
if (dev_read(dev, super, sizeof (struct cramfs_super), 512, 0) < sizeof (struct cramfs_super)) { if (cdev_read(cdev, super, sizeof (struct cramfs_super), 512, 0) < sizeof (struct cramfs_super)) {
printf("read superblock failed\n"); printf("read superblock failed\n");
return -EINVAL; return -EINVAL;
} }
@ -100,37 +106,37 @@ static int cramfs_read_super (struct device_d *dev, struct cramfs_priv *priv)
return 0; return 0;
} }
static struct cramfs_inode *cramfs_get_inode(struct device_d *dev, unsigned long offset) static struct cramfs_inode_info *cramfs_get_inode(struct cramfs_priv *priv, unsigned long offset)
{ {
struct cramfs_inode *inode = xmalloc(sizeof(struct cramfs_inode)); struct cramfs_inode_info *inodei = xmalloc(sizeof(*inodei));
if (dev_read(dev, inode, sizeof(struct cramfs_inode), offset, 0) < 0) { if (cdev_read(priv->cdev, &inodei->inode, sizeof(struct cramfs_inode), offset, 0) < 0) {
free(inode); free(inodei);
return NULL; return NULL;
} }
return inode; return inodei;
} }
static struct cramfs_inode *cramfs_resolve (struct device_d *dev, unsigned long offset, static struct cramfs_inode_info *cramfs_resolve (struct cramfs_priv *priv, unsigned long offset,
unsigned long size, int raw, unsigned long size, int raw,
char *filename) char *filename)
{ {
unsigned long inodeoffset = 0, nextoffset; unsigned long inodeoffset = 0, nextoffset;
struct cramfs_inode *inode = NULL, *ret; struct cramfs_inode_info *inodei = NULL, *ret;
char *name = xmalloc(256); char *name = xmalloc(256);
while (inodeoffset < size) { while (inodeoffset < size) {
int namelen; int namelen;
inode = cramfs_get_inode(dev, offset + inodeoffset); inodei = cramfs_get_inode(priv, offset + inodeoffset);
/* /*
* Namelengths on disk are shifted by two * Namelengths on disk are shifted by two
* and the name padded out to 4-byte boundaries * and the name padded out to 4-byte boundaries
* with zeroes. * with zeroes.
*/ */
namelen = CRAMFS_GET_NAMELEN (inode) << 2; namelen = CRAMFS_GET_NAMELEN (&inodei->inode) << 2;
dev_read(dev, name, namelen, offset + inodeoffset + sizeof (struct cramfs_inode), 0); cdev_read(priv->cdev, name, namelen, offset + inodeoffset + sizeof (struct cramfs_inode), 0);
nextoffset = nextoffset =
inodeoffset + sizeof (struct cramfs_inode) + namelen; inodeoffset + sizeof (struct cramfs_inode) + namelen;
@ -141,24 +147,24 @@ static struct cramfs_inode *cramfs_resolve (struct device_d *dev, unsigned long
if (raw && (p == NULL || *p == '\0')) if (raw && (p == NULL || *p == '\0'))
goto out1; goto out1;
if (S_ISDIR (CRAMFS_16 (inode->mode))) { if (S_ISDIR (CRAMFS_16 (inodei->inode.mode))) {
ret = cramfs_resolve (dev, ret = cramfs_resolve(priv,
CRAMFS_GET_OFFSET(inode) << 2, CRAMFS_GET_OFFSET(&inodei->inode) << 2,
CRAMFS_24 (inode->size), CRAMFS_24 (inodei->inode.size),
raw, p); raw, p);
goto out; goto out;
} else if (S_ISREG (CRAMFS_16 (inode->mode))) { } else if (S_ISREG (CRAMFS_16 (inodei->inode.mode))) {
goto out1; goto out1;
} else { } else {
printf ("%*.*s: unsupported file type (%x)\n", printf ("%*.*s: unsupported file type (%x)\n",
namelen, namelen, name, namelen, namelen, name,
CRAMFS_16 (inode->mode)); CRAMFS_16 (inodei->inode.mode));
ret = NULL; ret = NULL;
goto out; goto out;
} }
} }
free(inode); free(inodei);
inodeoffset = nextoffset; inodeoffset = nextoffset;
} }
@ -166,19 +172,19 @@ static struct cramfs_inode *cramfs_resolve (struct device_d *dev, unsigned long
return NULL; return NULL;
out1: out1:
ret = cramfs_get_inode(dev, offset + inodeoffset); ret = cramfs_get_inode(priv, offset + inodeoffset);
out: out:
free(inode); free(inodei);
free(name); free(name);
return ret; return ret;
} }
static int cramfs_fill_dirent (struct device_d *dev, unsigned long offset, struct dirent *d) static int cramfs_fill_dirent (struct cramfs_priv *priv, unsigned long offset, struct dirent *d)
{ {
struct cramfs_inode *inode = cramfs_get_inode(dev, offset); struct cramfs_inode_info *inodei = cramfs_get_inode(priv, offset);
int namelen; int namelen;
if (!inode) if (!inodei)
return -EINVAL; return -EINVAL;
memset(d->d_name, 0, 256); memset(d->d_name, 0, 256);
@ -189,9 +195,9 @@ static int cramfs_fill_dirent (struct device_d *dev, unsigned long offset, struc
* with zeroes. * with zeroes.
*/ */
namelen = CRAMFS_GET_NAMELEN (inode) << 2; namelen = CRAMFS_GET_NAMELEN (&inodei->inode) << 2;
dev_read(dev, d->d_name, namelen, offset + sizeof(struct cramfs_inode), 0); cdev_read(priv->cdev, d->d_name, namelen, offset + sizeof(struct cramfs_inode), 0);
free(inode); free(inodei);
return namelen; return namelen;
} }
@ -203,10 +209,8 @@ struct cramfs_dir {
static DIR* cramfs_opendir(struct device_d *_dev, const char *filename) static DIR* cramfs_opendir(struct device_d *_dev, const char *filename)
{ {
char *f;
struct cramfs_priv *priv = _dev->priv; struct cramfs_priv *priv = _dev->priv;
struct fs_device_d *fsdev = _dev->type_data; char *f;
struct device_d *dev = fsdev->parent;
struct cramfs_dir *dir = xzalloc(sizeof(struct cramfs_dir)); struct cramfs_dir *dir = xzalloc(sizeof(struct cramfs_dir));
dir->dir.priv = dir; dir->dir.priv = dir;
@ -216,28 +220,28 @@ static DIR* cramfs_opendir(struct device_d *_dev, const char *filename)
dir->offset = CRAMFS_GET_OFFSET (&(priv->super.root)) << 2; dir->offset = CRAMFS_GET_OFFSET (&(priv->super.root)) << 2;
dir->size = CRAMFS_24 (priv->super.root.size); dir->size = CRAMFS_24 (priv->super.root.size);
} else { } else {
struct cramfs_inode *inode; struct cramfs_inode_info *inodei;
f = strdup(filename); f = strdup(filename);
/* Resolve the path */ /* Resolve the path */
inode = cramfs_resolve (dev, inodei = cramfs_resolve(priv,
CRAMFS_GET_OFFSET (&(priv->super.root)) << CRAMFS_GET_OFFSET (&(priv->super.root)) <<
2, CRAMFS_24 (priv->super.root.size), 1, 2, CRAMFS_24 (priv->super.root.size), 1,
strtok (f, "/")); strtok (f, "/"));
free(f); free(f);
if (!inode) if (!inodei)
goto err_free; goto err_free;
/* Resolving was successful. Examine the inode */ /* Resolving was successful. Examine the inode */
if (!S_ISDIR (CRAMFS_16 (inode->mode))) { if (!S_ISDIR (CRAMFS_16 (inodei->inode.mode))) {
/* It's not a directory */ /* It's not a directory */
free(inode); free(inodei);
goto err_free; goto err_free;
} }
dir->offset = CRAMFS_GET_OFFSET (inode) << 2; dir->offset = CRAMFS_GET_OFFSET (&inodei->inode) << 2;
dir->size = CRAMFS_24 (inode->size); dir->size = CRAMFS_24 (inodei->inode.size);
free(inode); free(inodei);
} }
return &dir->dir; return &dir->dir;
@ -249,14 +253,13 @@ err_free:
static struct dirent* cramfs_readdir(struct device_d *_dev, DIR *_dir) static struct dirent* cramfs_readdir(struct device_d *_dev, DIR *_dir)
{ {
struct fs_device_d *fsdev = _dev->type_data; struct cramfs_priv *priv = _dev->priv;
struct device_d *dev = fsdev->parent;
struct cramfs_dir *dir = _dir->priv; struct cramfs_dir *dir = _dir->priv;
unsigned long nextoffset; unsigned long nextoffset;
/* List the given directory */ /* List the given directory */
if (dir->inodeoffset < dir->size) { if (dir->inodeoffset < dir->size) {
nextoffset = cramfs_fill_dirent (dev, dir->offset + dir->inodeoffset, &_dir->d); nextoffset = cramfs_fill_dirent (priv, dir->offset + dir->inodeoffset, &_dir->d);
dir->inodeoffset += sizeof (struct cramfs_inode) + nextoffset; dir->inodeoffset += sizeof (struct cramfs_inode) + nextoffset;
return &_dir->d; return &_dir->d;
@ -274,45 +277,48 @@ static int cramfs_closedir(struct device_d *dev, DIR *_dir)
static int cramfs_open(struct device_d *_dev, FILE *file, const char *filename) static int cramfs_open(struct device_d *_dev, FILE *file, const char *filename)
{ {
struct cramfs_priv *priv = _dev->priv; struct cramfs_priv *priv = _dev->priv;
struct cramfs_inode *inode; struct cramfs_inode_info *inodei;
struct fs_device_d *fsdev = _dev->type_data;
struct device_d *dev = fsdev->parent;
char *f; char *f;
f = strdup(filename); f = strdup(filename);
inode = cramfs_resolve (dev, inodei = cramfs_resolve (priv,
CRAMFS_GET_OFFSET (&(priv->super.root)) << 2, CRAMFS_GET_OFFSET (&(priv->super.root)) << 2,
CRAMFS_24 (priv->super.root.size), 0, CRAMFS_24 (priv->super.root.size), 0,
strtok (f, "/")); strtok (f, "/"));
free(f); free(f);
if (!inode) if (!inodei)
return -ENOENT; return -ENOENT;
file->inode = inode; file->inode = inodei;
file->size = inode->size; file->size = inodei->inode.size;
inodei->block_ptrs = xzalloc(4096);
cdev_read(priv->cdev, inodei->block_ptrs, 4096, CRAMFS_GET_OFFSET(&inodei->inode) << 2, 0);
return 0; return 0;
} }
static int cramfs_close(struct device_d *dev, FILE *file) static int cramfs_close(struct device_d *dev, FILE *file)
{ {
free(file->inode); struct cramfs_inode_info *inodei = file->inode;
free(inodei->block_ptrs);
free(inodei);
return 0; return 0;
} }
static int cramfs_read(struct device_d *_dev, FILE *f, void *buf, size_t size) static int cramfs_read(struct device_d *_dev, FILE *f, void *buf, size_t size)
{ {
struct cramfs_priv *priv = _dev->priv; struct cramfs_priv *priv = _dev->priv;
struct fs_device_d *fsdev = _dev->type_data; struct cramfs_inode_info *inodei = f->inode;
struct device_d *dev = fsdev->parent; struct cramfs_inode *inode = &inodei->inode;
struct cramfs_inode *inode = (struct cramfs_inode *)f->inode;
unsigned int blocknr; unsigned int blocknr;
int outsize = 0; int outsize = 0;
unsigned long *block_ptrs = (unsigned long *) unsigned long *block_ptrs = inodei->block_ptrs;
(dev->map_base + (CRAMFS_GET_OFFSET (inode) << 2));
int ofs = f->pos % 4096; int ofs = f->pos % 4096;
static char cramfs_read_buf[4096];
if (f->pos + size > inode->size) if (f->pos + size > inode->size)
size = inode->size - f->pos; size = inode->size - f->pos;
@ -322,21 +328,21 @@ static int cramfs_read(struct device_d *_dev, FILE *f, void *buf, size_t size)
int copy; int copy;
blocknr = (f->pos + outsize) >> 12; blocknr = (f->pos + outsize) >> 12;
if (blocknr != priv->curr_block || priv->inode != inode) {
if (blocknr)
base = CRAMFS_32 (block_ptrs[blocknr - 1]);
else
base = (CRAMFS_GET_OFFSET(inode) + (((CRAMFS_24 (inode->size)) + 4095) >> 12)) << 2;
priv->curr_block_len = cramfs_uncompress_block (priv->buf, if (blocknr)
(void *)(dev->map_base + base), 4096); base = CRAMFS_32 (block_ptrs[blocknr - 1]);
else
base = (CRAMFS_GET_OFFSET(inode) + (((CRAMFS_24 (inode->size)) + 4095) >> 12)) << 2;
// printf("READ blocknr: %d len %d\n",blocknr,priv->curr_block_len ); if (priv->curr_base < 0 || priv->curr_base != base) {
cdev_read(priv->cdev, cramfs_read_buf, 4096, base, 0);
priv->curr_block_len = cramfs_uncompress_block(priv->buf,
cramfs_read_buf, 4096);
if (priv->curr_block_len <= 0) if (priv->curr_block_len <= 0)
break; break;
priv->curr_block = blocknr; priv->curr_base = base;
priv->inode = inode;
} }
copy = min(priv->curr_block_len, size); copy = min(priv->curr_block_len, size);
@ -361,26 +367,26 @@ static off_t cramfs_lseek(struct device_d *dev, FILE *f, off_t pos)
static int cramfs_stat(struct device_d *_dev, const char *filename, struct stat *stat) static int cramfs_stat(struct device_d *_dev, const char *filename, struct stat *stat)
{ {
struct cramfs_priv *priv = _dev->priv; struct cramfs_priv *priv = _dev->priv;
struct fs_device_d *fsdev = _dev->type_data; struct cramfs_inode_info *inodei;
struct device_d *dev = fsdev->parent;
struct cramfs_inode *inode; struct cramfs_inode *inode;
char *f; char *f;
f = strdup(filename); f = strdup(filename);
inode = cramfs_resolve (dev, inodei = cramfs_resolve (priv,
CRAMFS_GET_OFFSET (&(priv->super.root)) << 2, CRAMFS_GET_OFFSET (&(priv->super.root)) << 2,
CRAMFS_24 (priv->super.root.size), 1, CRAMFS_24 (priv->super.root.size), 1,
strtok (f, "/")); strtok (f, "/"));
free(f); free(f);
if (!inode) if (!inodei)
return -ENOENT; return -ENOENT;
inode = &inodei->inode;
stat->st_mode = CRAMFS_16 (inode->mode); stat->st_mode = CRAMFS_16 (inode->mode);
stat->st_size = CRAMFS_24 (inode->size); stat->st_size = CRAMFS_24 (inode->size);
free(inode); free(inodei);
return 0; return 0;
} }
@ -422,26 +428,31 @@ static int cramfs_probe(struct device_d *dev)
priv = xmalloc(sizeof(struct cramfs_priv)); priv = xmalloc(sizeof(struct cramfs_priv));
dev->priv = priv; dev->priv = priv;
if (cramfs_read_super (fsdev->parent, priv)) { if (strncmp(fsdev->backingstore, "/dev/", 5))
printf("no valid cramfs found on %s\n",fsdev->parent->id); return -ENODEV;
priv->cdev = cdev_by_name(fsdev->backingstore);
if (!priv->cdev)
return -ENODEV;
if (cramfs_read_super(priv)) {
dev_info(dev, "no valid cramfs found\n");
free(priv); free(priv);
return -EINVAL; return -EINVAL;
} }
priv->curr_block = -1; priv->curr_base = -1;
cramfs_uncompress_init (); cramfs_uncompress_init ();
return 0; return 0;
} }
static int cramfs_remove(struct device_d *dev) static void cramfs_remove(struct device_d *dev)
{ {
struct cramfs_priv *priv = dev->priv; struct cramfs_priv *priv = dev->priv;
cramfs_uncompress_exit(); cramfs_uncompress_exit();
free(priv); free(priv);
return 0;
} }
static struct fs_driver_d cramfs_driver = { static struct fs_driver_d cramfs_driver = {