#include <sys/param.h>
#include <lib/libsa/stand.h>
#include <hp300/stand/common/rawfs.h>
extern int debug;
#define RAWFS_BSIZE 0x2000
struct rawfs_file {
daddr_t fs_nextblk;
int fs_len;
char * fs_ptr;
char fs_buf[RAWFS_BSIZE];
};
static int rawfs_get_block(struct open_file *);
int
rawfs_open(const char *path, struct open_file *f)
{
struct rawfs_file *fs;
fs = alloc(sizeof(struct rawfs_file));
fs->fs_nextblk = 0;
fs->fs_len = 0;
fs->fs_ptr = fs->fs_buf;
#ifdef DEBUG_RAWFS
printf("rawfs_open: fs=0x%x\n", (u_long)fs);
#endif
f->f_fsdata = fs;
return 0;
}
int
rawfs_close(struct open_file *f)
{
struct rawfs_file *fs;
fs = (struct rawfs_file *) f->f_fsdata;
f->f_fsdata = (void *)0;
#ifdef DEBUG_RAWFS
printf("rawfs_close: fs=0x%x\n", (u_long)fs);
#endif
if (fs != (struct rawfs_file *)0)
dealloc(fs, sizeof(*fs));
return 0;
}
int
rawfs_read(struct open_file *f, void *start, u_int size, u_int *resid)
{
struct rawfs_file *fs = (struct rawfs_file *)f->f_fsdata;
char *addr = start;
int error = 0;
size_t csize;
#ifdef DEBUG_RAWFS
printf("rawfs_read: file=0x%x, start=0x%x, size=%d, resid=0x%x\n",
(u_long)f, (u_long)start, size, resid);
printf(" fs=0x%x\n", (u_long)fs);
#endif
while (size != 0) {
if (fs->fs_len == 0)
if ((error = rawfs_get_block(f)) != 0)
break;
if (fs->fs_len <= 0)
break;
csize = size;
if (csize > fs->fs_len)
csize = fs->fs_len;
memcpy(addr, fs->fs_ptr, csize);
fs->fs_ptr += csize;
fs->fs_len -= csize;
addr += csize;
size -= csize;
}
if (resid)
*resid = size;
return error;
}
int
rawfs_write(struct open_file *f, void *start, size_t size, size_t *resid)
{
#ifdef DEBUG_RAWFS
printf("rawfs_write: YOU'RE NOT SUPPOSED TO GET HERE!\n");
#endif
return EROFS;
}
off_t
rawfs_seek(struct open_file *f, off_t offset, int where)
{
#ifdef DEBUG_RAWFS
printf("rawfs_seek: YOU'RE NOT SUPPOSED TO GET HERE!\n");
#endif
return EFTYPE;
}
int
rawfs_stat(struct open_file *f, struct stat *sb)
{
#ifdef DEBUG_RAWFS
printf("rawfs_stat: I'll let you live only because of exec.c\n");
#endif
memset(sb, 0, sizeof(*sb));
return EFTYPE;
}
static int
rawfs_get_block(struct open_file *f)
{
struct rawfs_file *fs;
int error;
size_t len;
fs = (struct rawfs_file *)f->f_fsdata;
fs->fs_ptr = fs->fs_buf;
twiddle();
#ifdef DEBUG_RAWFS
printf("rawfs_get_block: calling strategy\n");
#endif
error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
fs->fs_nextblk, RAWFS_BSIZE, fs->fs_buf, &len);
#ifdef DEBUG_RAWFS
printf("rawfs_get_block: strategy returned %d\n", error);
#endif
if (!error) {
fs->fs_len = len;
fs->fs_nextblk += (RAWFS_BSIZE / DEV_BSIZE);
}
return error;
}