* Initial working r_fs API with ext2 fs
- Support file reading and directory listing * Fix build
This commit is contained in:
parent
ca432e3f04
commit
b8b87f050e
|
@ -81,6 +81,7 @@ int main(int argc, char **argv) {
|
|||
int fullfile = 0;
|
||||
ut32 bsize = 0;
|
||||
ut64 seek = 0;
|
||||
char *cmdfile = NULL;
|
||||
|
||||
if (argc<2)
|
||||
return main_help (1);
|
||||
|
@ -98,7 +99,7 @@ int main(int argc, char **argv) {
|
|||
r_config_set (r.config, "file.project", optarg);
|
||||
break;
|
||||
case 'i':
|
||||
r_core_cmd_file (&r, optarg);
|
||||
cmdfile = optarg;
|
||||
break;
|
||||
case 'l':
|
||||
r_lib_open (r.lib, optarg);
|
||||
|
@ -262,6 +263,9 @@ int main(int argc, char **argv) {
|
|||
#endif
|
||||
} else eprintf ("Metadata loaded from 'file.project'\n");
|
||||
|
||||
if (cmdfile)
|
||||
r_core_cmd_file (&r, cmdfile);
|
||||
|
||||
if (r_io_is_listener (r.io))
|
||||
r_core_serve (&r, r.io->fd);
|
||||
else
|
||||
|
|
|
@ -7,7 +7,7 @@ PFX=${DESTDIR}${PREFIX}
|
|||
|
||||
# Libraries
|
||||
LIBLIST=util vm socket cons line lib io lang flags bin hash config syscall cmd
|
||||
LIBLIST+=reg asm anal print parse search diff bp sign th db crypto debug core fs
|
||||
LIBLIST+=reg asm anal print parse search diff bp sign th db crypto debug fs core
|
||||
|
||||
# TODO : generate single library linking against the rest
|
||||
#LIBSO=libr.so
|
||||
|
|
|
@ -924,7 +924,7 @@ static int cmd_yank_to(RCore *core, char *arg) {
|
|||
}
|
||||
|
||||
static int cmd_mount(void *data, const char *_input) {
|
||||
char *input, *oinput, *ptr;
|
||||
char *input, *oinput, *ptr, *ptr2;
|
||||
RList *list;
|
||||
RListIter *iter;
|
||||
RFSFile *file;
|
||||
|
@ -940,8 +940,16 @@ static int cmd_mount(void *data, const char *_input) {
|
|||
input++;
|
||||
ptr = strchr (input, ' ');
|
||||
if (ptr) {
|
||||
ut64 delta = 0;
|
||||
*ptr = 0;
|
||||
r_fs_mount (core->fs, input, ptr+1);
|
||||
ptr++;
|
||||
ptr2 = strchr (ptr, ' ');
|
||||
if (ptr2) {
|
||||
*ptr2 = 0;
|
||||
delta = r_num_math (core->num, ptr2+1);
|
||||
}
|
||||
//r_io_bind (core->io, &(core->fs->iob));
|
||||
r_fs_mount (core->fs, input, ptr, delta);
|
||||
} else eprintf ("Usage: m ext2 /mnt");
|
||||
break;
|
||||
case '-':
|
||||
|
@ -950,12 +958,12 @@ static int cmd_mount(void *data, const char *_input) {
|
|||
case '*':
|
||||
eprintf ("List commands in radare format\n");
|
||||
r_list_foreach (core->fs->roots, iter, root) {
|
||||
r_cons_printf ("m %s 0x%"PFMT64x" %s\n", root->fs->name, root->delta, root->path);
|
||||
r_cons_printf ("m %s 0x%"PFMT64x" %s\n", root->p->name, root->delta, root->path);
|
||||
}
|
||||
break;
|
||||
case '\0':
|
||||
r_list_foreach (core->fs->roots, iter, root) {
|
||||
r_cons_printf ("%s\t0x%"PFMT64x"\t%s\n", root->fs->name, root->delta, root->path);
|
||||
r_cons_printf ("%s\t0x%"PFMT64x"\t%s\n", root->p->name, root->delta, root->path);
|
||||
}
|
||||
break;
|
||||
case 'l': // list of plugins
|
||||
|
@ -994,7 +1002,7 @@ static int cmd_mount(void *data, const char *_input) {
|
|||
r_cons_printf (
|
||||
"Usage: m[-?*dgy] [...]\n"
|
||||
" m ; list all mountpoints in human readable format\n"
|
||||
" m /mnt ; list all mountpoints from a given path\n"
|
||||
" m ext2 /mnt 0 ; mount ext2 fs at /mnt with delta 0 on IO\n"
|
||||
" m* ; same as above, but in r2 commands\n"
|
||||
" ml ; list filesystem plugins\n"
|
||||
" m-/ ; umount given path (/)\n"
|
||||
|
|
|
@ -203,8 +203,6 @@ R_API int r_core_init(RCore *core) {
|
|||
core->print = r_print_new ();
|
||||
core->print->printf = (void *)r_cons_printf;
|
||||
core->lang = r_lang_new ();
|
||||
core->fs = r_fs_new ();
|
||||
r_io_bind (core->io, &(core->fs->iob));
|
||||
r_lang_define (core->lang, "RCore", "core", core);
|
||||
r_lang_set_user_ptr (core->lang, core);
|
||||
core->anal = r_anal_new ();
|
||||
|
@ -221,6 +219,8 @@ R_API int r_core_init(RCore *core) {
|
|||
core->sign = r_sign_new ();
|
||||
core->search = r_search_new (R_SEARCH_KEYWORD);
|
||||
r_io_undo_enable (core->io, 1, 0); // TODO: configurable via eval
|
||||
core->fs = r_fs_new ();
|
||||
r_io_bind (core->io, &(core->fs->iob));
|
||||
//r_cmd_macro_init (&core->macro);
|
||||
core->file = NULL;
|
||||
INIT_LIST_HEAD (&core->files);
|
||||
|
|
|
@ -2,9 +2,12 @@ NAME=r_fs
|
|||
DEPS=r_lib r_util r_io
|
||||
CFLAGS+=-DCORELIB -Iarch/include -Iarch
|
||||
|
||||
CFLAGS+=-Ip/grub/include
|
||||
LDFLAGS+=p/grub/libgrubfs.a
|
||||
|
||||
include ../config.mk
|
||||
|
||||
foo: pre ${LIBSO} ${LIBAR} plugins
|
||||
foo: pre plugins ${LIBSO} ${LIBAR}
|
||||
|
||||
include ${STATIC_FS_PLUGINS}
|
||||
STATIC_OBJS=$(subst ..,p/..,$(subst fs_,p/fs_,$(STATIC_OBJ)))
|
||||
|
@ -20,6 +23,10 @@ re:
|
|||
sudo ${MAKE} install
|
||||
cd t && ${MAKE}
|
||||
|
||||
test:
|
||||
#valgrind r2 -i test.rsc /tmp/test.fs.img
|
||||
gdb --args r2 -i test.rsc /tmp/test.fs.img
|
||||
|
||||
plugins:
|
||||
cd p && ${MAKE} all
|
||||
|
||||
|
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
#include <r_fs.h>
|
||||
|
||||
R_API RFSFile *r_fs_file_new (const char *path) {
|
||||
R_API RFSFile *r_fs_file_new (RFSRoot *root, const char *path) {
|
||||
RFSFile *file = R_NEW (RFSFile);
|
||||
memset (file, 0, sizeof (RFSFile));
|
||||
file->root = root;
|
||||
file->name = strdup (path);
|
||||
// TODO: concat path?
|
||||
return file;
|
||||
}
|
||||
|
||||
|
@ -24,6 +26,10 @@ R_API RFSRoot *r_fs_root_new (const char *path, ut64 delta) {
|
|||
}
|
||||
|
||||
R_API void r_fs_root_free (RFSRoot *root) {
|
||||
free (root->path);
|
||||
free (root);
|
||||
if (root) {
|
||||
if (root->p && root->p->umount)
|
||||
root->p->umount (root);
|
||||
free (root->path);
|
||||
free (root);
|
||||
}
|
||||
}
|
||||
|
|
57
libr/fs/fs.c
57
libr/fs/fs.c
|
@ -3,8 +3,7 @@
|
|||
#include <r_fs.h>
|
||||
#include "../config.h"
|
||||
|
||||
static RFSPlugin *fs_static_plugins[] =
|
||||
{ R_FS_STATIC_PLUGINS };
|
||||
static RFSPlugin *fs_static_plugins[] = { R_FS_STATIC_PLUGINS };
|
||||
|
||||
/* lifecycle */
|
||||
// TODO: needs much more love
|
||||
|
@ -59,18 +58,20 @@ R_API void r_fs_del (RFS *fs, RFSPlugin *p) {
|
|||
|
||||
/* mountpoint */
|
||||
|
||||
R_API RFSRoot *r_fs_mount (RFS* fs, const char *fstype, const char *path) {
|
||||
R_API RFSRoot *r_fs_mount (RFS* fs, const char *fstype, const char *path, ut64 delta) {
|
||||
RFSPlugin *p;
|
||||
RFSRoot * root;
|
||||
RFSRoot *root;
|
||||
if (path[0] != '/') {
|
||||
eprintf ("r_fs_mount: invalid mountpoint\n");
|
||||
return NULL;
|
||||
}
|
||||
//r_fs
|
||||
p = r_fs_plugin_get (fs, fstype);
|
||||
if (p != NULL) {
|
||||
root = r_fs_root_new (path, 0); // XXX hardcoded offset
|
||||
root->fs = p;
|
||||
root = r_fs_root_new (path, delta);
|
||||
root->p = p;
|
||||
//memcpy (&root->iob, &fs->iob, sizeof (root->iob));
|
||||
root->iob = fs->iob;
|
||||
p->mount (root);
|
||||
r_list_append (fs->roots, root);
|
||||
eprintf ("Mounted %s on %s at 0x%llx\n", fstype, path, 0LL);
|
||||
} else eprintf ("r_fs_mount: Invalid filesystem type\n");
|
||||
|
@ -107,21 +108,29 @@ R_API RFSRoot *r_fs_root (RFS *fs, const char *path) {
|
|||
|
||||
R_API RFSFile *r_fs_open (RFS* fs, const char *path) {
|
||||
RFSRoot *root = r_fs_root (fs, path);
|
||||
if (root)
|
||||
return root->fs->open (path);
|
||||
if (root && root->p && root->p->open)
|
||||
return root->p->open (root, path+strlen (root->path));
|
||||
else eprintf ("r_fs_open: null root->p->open\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// TODO: close or free?
|
||||
R_API void r_fs_close (RFS* fs, RFSFile *file) {
|
||||
if (fs && file)
|
||||
file->fs->close (file);
|
||||
if (fs && file && file->p && file->p->close)
|
||||
file->p->close (file);
|
||||
}
|
||||
|
||||
R_API int r_fs_read (RFS* fs, RFSFile *file, ut64 addr, int len) {
|
||||
if (len<1) {
|
||||
eprintf ("r_fs_read: too short read\n");
|
||||
} else
|
||||
if (fs && file) {
|
||||
free (file->data);
|
||||
file->data = malloc (file->size);
|
||||
file->fs->read (file, addr, len);
|
||||
file->data = malloc (len+1);
|
||||
if (file->p && file->p->read) {
|
||||
file->p->read (file, addr, len);
|
||||
return R_TRUE;
|
||||
} else eprintf ("r_fs_read: file->p->read is null\n");
|
||||
}
|
||||
return R_FALSE;
|
||||
}
|
||||
|
@ -129,9 +138,29 @@ R_API int r_fs_read (RFS* fs, RFSFile *file, ut64 addr, int len) {
|
|||
R_API RList *r_fs_dir(RFS* fs, const char *path) {
|
||||
if (fs) {
|
||||
RFSRoot *root = r_fs_root (fs, path);
|
||||
const char *dir = path + strlen (root->path);
|
||||
if (!*dir) dir = "/";
|
||||
if (root)
|
||||
return root->fs->dir (root, path);
|
||||
return root->p->dir (root, dir);
|
||||
eprintf ("r_fs_dir: error, path %s is not mounted\n", path);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
R_API RFSFile *r_fs_load(RFS* fs, const char *path) {
|
||||
RFSFile *file = NULL;
|
||||
RFSRoot *root = r_fs_root (fs, path);
|
||||
if (root && root->p) {
|
||||
if (root->p->open && root->p->read && root->p->close) {
|
||||
file = root->p->open (root, path);
|
||||
if (file) {
|
||||
root->p->read (file, 0, file->size); //file->data, file->size);
|
||||
} else eprintf ("r_fs_load: cannot open file\n");
|
||||
} else {
|
||||
if (root->p->load)
|
||||
return root->p->load (root, path);
|
||||
else eprintf ("r_fs_load: null root->p->load\n");
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
include ../../config.mk
|
||||
|
||||
CFLAGS+=-I../../include -Wall -shared -fPIC ${LDFLAGS_LIB} ${LDFLAGS_LINKPATH}..
|
||||
# XXX
|
||||
CFLAGS+=-DLIL_ENDIAN=1
|
||||
CFLAGS+=-L../../util -lr_util
|
||||
#CFLAGS+=-I../../include
|
||||
LDFLAGS+=${LINK}
|
||||
|
||||
foo: all
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
OBJ_EXT2=fs_ext2.o
|
||||
EXTRA=../p/grub/libgrubfs.a
|
||||
CFLAGS+=-Igrub/include
|
||||
|
||||
STATIC_OBJ+=${OBJ_EXT2}
|
||||
#STATIC_OBJ+=${EXTRA}
|
||||
TARGET_EXT2=fs_ext2.${EXT_SO}
|
||||
|
||||
ALL_TARGETS+=${TARGET_EXT2}
|
||||
|
||||
${TARGET_EXT2}: ${OBJ_EXT2}
|
||||
${CC} ${LDFLAGS} ${CFLAGS} -o ${TARGET_EXT2} ${OBJ_EXT2}
|
||||
${CC} ${LDFLAGS} ${CFLAGS} -o ${TARGET_EXT2} ${OBJ_EXT2} ${EXTRA}
|
||||
|
|
|
@ -1,20 +1,65 @@
|
|||
/* radare - LGPL - Copyright 2010 pancake<nopcode.org> */
|
||||
/* radare - LGPL - Copyright 2011 pancake<nopcode.org> */
|
||||
|
||||
#include <r_fs.h>
|
||||
#include "grubfs.h"
|
||||
|
||||
static RFSFile* ext2_open(const char *path) {
|
||||
return NULL;
|
||||
static RFSFile* ext2_open(RFSRoot *root, const char *path) {
|
||||
RFSFile *file = r_fs_file_new (root, path);
|
||||
GrubFS *gfs = grubfs_new (&grub_ext2_fs, &root->iob);
|
||||
file->ptr = gfs;
|
||||
file->p = root->p;
|
||||
if (gfs->file->fs->open (gfs->file, path)) {
|
||||
r_fs_file_free (file);
|
||||
grubfs_free (gfs);
|
||||
file = NULL;
|
||||
} else file->size = gfs->file->size;
|
||||
return file;
|
||||
}
|
||||
|
||||
static boolt ext2_read(RFSFile *fs, ut64 addr, int len) {
|
||||
static boolt ext2_read(RFSFile *file, ut64 addr, int len) {
|
||||
GrubFS *gfs = file->ptr;
|
||||
grubfs_bind_io (NULL, file->root->delta);
|
||||
gfs->file->fs->read (gfs->file, (char*)file->data, len);
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
static void ext2_close(RFSFile *fs) {
|
||||
static void ext2_close(RFSFile *file) {
|
||||
GrubFS *gfs = file->ptr;
|
||||
gfs->file->fs->close (gfs->file);
|
||||
}
|
||||
|
||||
static RList *list = NULL;
|
||||
|
||||
int dirhook (const char *filename, const struct grub_dirhook_info *info) {
|
||||
RFSFile *fsf = r_fs_file_new (NULL, filename);
|
||||
fsf->type = info->dir? 'd':'f';
|
||||
fsf->time = info->mtime;
|
||||
r_list_append (list, fsf);
|
||||
//info->mtimeset
|
||||
//info->case_insensitive
|
||||
printf ("DIRFILE: %c (%d) %s\n", info->dir?'d':'f',
|
||||
info->mtime, filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static RList *ext2_dir(RFSRoot *root, const char *path) {
|
||||
return NULL;
|
||||
GrubFS *gfs = root->ptr;
|
||||
list = r_list_new ();
|
||||
eprintf ("ext2_dir: %s\n", path);
|
||||
//gfs->file->device->data = &root->iob;
|
||||
grubfs_bind_io (&root->iob, root->delta);
|
||||
gfs->file->fs->dir (gfs->file->device, path, dirhook);
|
||||
grubfs_bind_io (NULL, root->delta);
|
||||
return list;
|
||||
}
|
||||
|
||||
static void ext2_mount(RFSRoot *root) {
|
||||
root->ptr = grubfs_new (&grub_ext2_fs, &root->iob);
|
||||
}
|
||||
|
||||
static void ext2_umount(RFSRoot *root) {
|
||||
grubfs_free (root->ptr);
|
||||
root->ptr = NULL;
|
||||
}
|
||||
|
||||
struct r_fs_plugin_t r_fs_plugin_ext2 = {
|
||||
|
@ -23,5 +68,7 @@ struct r_fs_plugin_t r_fs_plugin_ext2 = {
|
|||
.open = ext2_open,
|
||||
.read = ext2_read,
|
||||
.close = ext2_close,
|
||||
.dir = ext2_dir
|
||||
.dir = ext2_dir,
|
||||
.mount = ext2_mount,
|
||||
.umount = ext2_umount,
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
p/fs_ext2.o: p/fs_ext2.c ../include/r_fs.h ../include/r_types.h \
|
||||
../include/r_userconf.h ../include/r_types_base.h /usr/include/stdio.h \
|
||||
/usr/include/features.h /usr/include/sys/cdefs.h \
|
||||
fs_ext2.o: fs_ext2.c ../../include/r_fs.h ../../include/r_types.h \
|
||||
../../include/r_userconf.h ../../include/r_types_base.h \
|
||||
/usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h \
|
||||
/usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \
|
||||
/usr/include/gnu/stubs-32.h \
|
||||
/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/stddef.h \
|
||||
|
@ -21,7 +21,13 @@ p/fs_ext2.o: p/fs_ext2.c ../include/r_fs.h ../include/r_types.h \
|
|||
/usr/include/bits/posix1_lim.h /usr/include/bits/local_lim.h \
|
||||
/usr/include/linux/limits.h /usr/include/unistd.h \
|
||||
/usr/include/bits/posix_opt.h /usr/include/bits/environments.h \
|
||||
/usr/include/bits/confname.h /usr/include/getopt.h ../include/r_list.h \
|
||||
../include/r_flist.h ../include/r_io.h ../include/r_util.h \
|
||||
../include/btree.h ../include/r_types.h ../include/list.h \
|
||||
/usr/include/gmp.h
|
||||
/usr/include/bits/confname.h /usr/include/getopt.h \
|
||||
../../include/r_list.h ../../include/r_flist.h ../../include/r_io.h \
|
||||
../../include/r_util.h ../../include/btree.h ../../include/r_types.h \
|
||||
../../include/list.h /usr/include/gmp.h grub/include/grubfs.h \
|
||||
grub/include/grub/file.h grub/include/grub/types.h grub/include/config.h \
|
||||
grub/include/config-util.h grub/include/grub/cpu/types.h \
|
||||
grub/include/grub/err.h grub/include/grub/symbol.h \
|
||||
grub/include/grub/device.h grub/include/grub/fs.h \
|
||||
grub/include/grub/list.h grub/include/grub/misc.h \
|
||||
grub/include/grub/disk.h
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# TODO include ..config.mk
|
||||
CC?=gcc
|
||||
include ../../../config.mk
|
||||
#CC?=gcc
|
||||
KERNFILES=kern/file.c
|
||||
KERNFILES+=kern/term.c kern/device.c
|
||||
KERNFILES+=kern/err.c
|
||||
|
@ -8,16 +8,18 @@ KERNFILES+=kern/fs.c kern/misc.c
|
|||
KERNFILES+=kern/time.c
|
||||
KERNFILES+=kern/list.c kern/partition.c
|
||||
KERNFILES+=fs/fshelp.c fs/ext2.c
|
||||
KERNFILES+=main.c
|
||||
#KERNFILES+=main.c
|
||||
KERNFILES+=grubfs.c
|
||||
|
||||
KERNOBJS=$(subst .c,.o,${KERNFILES})
|
||||
CFLAGS=-Iinclude -g
|
||||
CFLAGS+=-I../../../include
|
||||
BIN=test${EXT_EXE}
|
||||
|
||||
all: ${KERNOBJS} ${BIN}
|
||||
all: ${KERNOBJS} main.o ${BIN}
|
||||
|
||||
${BIN}:
|
||||
${CC} -o ${BIN} ${CFLAGS} ${KERNOBJS}
|
||||
${CC} -o ${BIN} main.o ${CFLAGS} ${KERNOBJS}
|
||||
|
||||
lib: all libgrubfs.a
|
||||
|
||||
|
@ -27,5 +29,5 @@ libgrubfs.a:
|
|||
ar -q libgrubfs.a ${KERNOBJS}
|
||||
|
||||
clean:
|
||||
rm -f ${KERNOBJS} ${BIN}
|
||||
rm -f ${KERNOBJS} ${BIN} main.o
|
||||
rm -f libgrubfs.a
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/* radare - LGPL - Copyright 2011 pancake<nopcode.org> */
|
||||
|
||||
#include <r_io.h>
|
||||
#include <r_fs.h>
|
||||
#include "grubfs.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static RIOBind *bio = NULL;
|
||||
static ut64 delta = 0;
|
||||
|
||||
static void* empty (int sz) {
|
||||
void *p = malloc (sz);
|
||||
if (p) memset (p, '\0', sz);
|
||||
return p;
|
||||
}
|
||||
|
||||
static int read_foo (struct grub_disk *disk, grub_disk_addr_t sector, grub_size_t size, unsigned char *buf) {
|
||||
if (disk != NULL) {
|
||||
int ret;
|
||||
RIOBind *iob = disk->data;
|
||||
if (bio)
|
||||
iob = bio;
|
||||
//printf ("io %p\n", file->root->iob.io);
|
||||
ret = iob->read_at (iob->io, delta+(512*sector), buf, 512); // XXX: 512 is hardcoded?
|
||||
if (ret == -1)
|
||||
return 1;
|
||||
//printf ("DISK PTR = %p\n", disk->data);
|
||||
//printf ("\nBUF: %x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
|
||||
} else {
|
||||
FILE *fd = fopen ("/tmp/test.fs.img", "rb"); // XXX for debug only
|
||||
if (fd) {
|
||||
size = 512;
|
||||
fseek (fd, (512*sector), SEEK_SET);
|
||||
fread (buf, 1, size, fd);
|
||||
fclose (fd);
|
||||
} else return 1;
|
||||
}
|
||||
return 0; // 0 is ok
|
||||
}
|
||||
|
||||
GrubFS *grubfs_new (struct grub_fs *myfs, void *data) {
|
||||
struct grub_file *file;
|
||||
GrubFS *gfs = empty (sizeof (GrubFS));
|
||||
// hacky mallocs :D
|
||||
gfs->file = file = empty (sizeof (struct grub_file));
|
||||
file->device = empty (sizeof (struct grub_device)+1024);
|
||||
file->device->disk = empty (sizeof (struct grub_disk));
|
||||
file->device->disk->dev = file->device;
|
||||
file->device->disk->dev->read = read_foo; // grub_disk_dev
|
||||
file->device->disk->data = data;
|
||||
//file->device->disk->read_hook = read_foo; //read_hook;
|
||||
file->fs = myfs;
|
||||
return gfs;
|
||||
}
|
||||
|
||||
void grubfs_free (GrubFS *gf) {
|
||||
if (gf) {
|
||||
if (gf->file && gf->file->device)
|
||||
free (gf->file->device->disk);
|
||||
//free (gf->file->device);
|
||||
free (gf->file);
|
||||
free (gf);
|
||||
}
|
||||
}
|
||||
|
||||
void grubfs_bind_io (RIOBind *iob, ut64 _delta) {
|
||||
bio = iob;
|
||||
delta = _delta;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef _INCLUDE_GRUBFS_H_
|
||||
#define _INCLUDE_GRUBFS_H_
|
||||
|
||||
#include <r_io.h>
|
||||
#include <grub/file.h>
|
||||
#include <grub/disk.h>
|
||||
|
||||
typedef struct grubfs {
|
||||
struct grub_file *file;
|
||||
} GrubFS;
|
||||
|
||||
GrubFS *grubfs_new (struct grub_fs *myfs, void *data);
|
||||
void grubfs_free (GrubFS *gf);
|
||||
void grubfs_bind_io (RIOBind *iob, ut64 _delta);
|
||||
|
||||
extern struct grub_fs grub_ext2_fs;
|
||||
|
||||
#endif
|
|
@ -982,9 +982,6 @@ void
|
|||
grub_abort (void)
|
||||
{
|
||||
grub_printf ("\nAborted.");
|
||||
|
||||
|
||||
grub_exit ();
|
||||
}
|
||||
|
||||
#if ! defined (APPLE_CC) && !defined (GRUB_UTIL)
|
||||
|
|
|
@ -17,7 +17,7 @@ void read_foo (struct grub_disk *disk, grub_disk_addr_t sector, grub_size_t size
|
|||
//printf ("==> land: %p\n", buf);
|
||||
size=512;
|
||||
{
|
||||
FILE *fd = fopen ("test.fs.img", "rb");
|
||||
FILE *fd = fopen ("/tmp/test.fs.img", "rb");
|
||||
fseek (fd, (512*sector), SEEK_SET);
|
||||
fread (buf, 1, size, fd);
|
||||
//printf ("\nBUF: %x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
|
||||
|
@ -30,7 +30,7 @@ void read_hook (grub_disk_addr_t sector, unsigned long offset, unsigned long len
|
|||
//printf ("[hook]==> last %p\n", buf);
|
||||
{
|
||||
int size=length;
|
||||
FILE *fd = fopen("test.fs.img", "rb");
|
||||
FILE *fd = fopen("/tmp/test.fs.img", "rb");
|
||||
fseek (fd, (512*sector)+offset, SEEK_SET);
|
||||
fread (buf, 1, size, fd);
|
||||
//write (1, buf, size);
|
||||
|
@ -41,25 +41,25 @@ void read_hook (grub_disk_addr_t sector, unsigned long offset, unsigned long len
|
|||
|
||||
grub_file_t openimage(grub_fs_t fs, const char *str) {
|
||||
grub_file_t file = malloc (1024);
|
||||
file->device = malloc(1024);
|
||||
file->device = malloc (1024);
|
||||
memset (file->device, 0, 1024);
|
||||
file->device->disk = malloc(1024);
|
||||
file->device->disk->name = strdup ("disk0");
|
||||
file->device->disk = malloc (1024);
|
||||
memset (file->device->disk, 0, 1024);
|
||||
//file->device->disk->name = "disk0"; //strdup ("disk0");
|
||||
file->device->disk->dev = file->device;
|
||||
//file->device->disk->dev->read = read_hook; //file->device;
|
||||
file->device->disk->dev->read = read_foo; //file->device;
|
||||
file->device->disk->read_hook = read_hook; //read_hook;
|
||||
//file->device->read = read_hook;
|
||||
//file->device->disk->read_hook = read_hook; //read_hook;
|
||||
//file->device->read = read_hook;
|
||||
//file->read_hook = read_hook;
|
||||
//&device; // HACK to avoid segfault
|
||||
file->fs = fs;
|
||||
#if 0
|
||||
file->offset = 0;
|
||||
file->size = 12190208;
|
||||
file->data = malloc (file->size);
|
||||
#endif
|
||||
{
|
||||
FILE *fd = fopen("test.fs.img", "rb");
|
||||
FILE *fd = fopen("/tmp/test.fs.img", "rb");
|
||||
if (fd == NULL) {
|
||||
printf ("Cannot open fs image\n");
|
||||
return NULL;
|
||||
|
@ -67,7 +67,7 @@ grub_file_t openimage(grub_fs_t fs, const char *str) {
|
|||
fread (file->data, file->size, 1, fd);
|
||||
fclose (fd);
|
||||
}
|
||||
file->read_hook = read_hook;
|
||||
#endif
|
||||
return file;
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ int do_main() {
|
|||
struct grub_disk disk;
|
||||
|
||||
e2 = &grub_ext2_fs;
|
||||
file = openimage (e2, "test.fs.img");
|
||||
file = openimage (e2, "/tmp/test.fs.img");
|
||||
if (file == NULL) {
|
||||
printf ("oops\n");
|
||||
return 0;
|
||||
|
@ -112,8 +112,25 @@ int do_main() {
|
|||
return 1;
|
||||
}
|
||||
|
||||
main() {
|
||||
printf ("Hello grubfs!\n");
|
||||
#include "grubfs.h"
|
||||
int foo_main() {
|
||||
char buf[1024];
|
||||
GrubFS *gfs = grubfs_new (&grub_ext2_fs, NULL);
|
||||
gfs->file->fs->open (gfs->file, "/test");
|
||||
gfs->file->fs->read (gfs->file, buf, gfs->file->size);
|
||||
printf ("fs = %d\n", gfs->file->size);
|
||||
write (1, buf, gfs->file->size);
|
||||
gfs->file->fs->close (gfs->file);
|
||||
gfs->file->fs->dir (gfs->file->device, "/", dirhook);
|
||||
grubfs_free (gfs);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc>1) {
|
||||
printf ("grubfs api\n");
|
||||
return foo_main ();
|
||||
}
|
||||
printf ("grub internal api\n");
|
||||
if (do_main()) {
|
||||
printf ("\n** worked!\n");
|
||||
} else {
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
ml
|
||||
m ext2 /foo
|
||||
md /foo
|
||||
mg /foo/test
|
||||
m-/foo
|
||||
m
|
|
@ -25,25 +25,31 @@ typedef struct r_fs_file_t {
|
|||
void *ctx;
|
||||
char type;
|
||||
ut64 time;
|
||||
struct r_fs_plugin_t *fs;
|
||||
void *ptr;
|
||||
struct r_fs_plugin_t *p;
|
||||
struct r_fs_root_t *root;
|
||||
void *ptr; // pointer to internal
|
||||
} RFSFile;
|
||||
|
||||
typedef struct r_fs_root_t {
|
||||
char *path;
|
||||
ut64 delta;
|
||||
struct r_fs_plugin_t *fs;
|
||||
struct r_fs_plugin_t *p;
|
||||
void *ptr;
|
||||
RIOBind iob;
|
||||
} RFSRoot;
|
||||
|
||||
typedef struct r_fs_plugin_t {
|
||||
const char *name;
|
||||
const char *desc;
|
||||
RFSFile* (*open)(const char *path);
|
||||
RFSFile* (*load)(RFSRoot *root, const char *path);
|
||||
RFSFile* (*open)(RFSRoot *root, const char *path);
|
||||
boolt (*read)(RFSFile *fs, ut64 addr, int len);
|
||||
void (*close)(RFSFile *fs);
|
||||
RList *(*dir)(RFSRoot *root, const char *path);
|
||||
void (*init)();
|
||||
void (*fini)();
|
||||
void (*mount)(RFSRoot *root);
|
||||
void (*umount)(RFSRoot *root);
|
||||
} RFSPlugin;
|
||||
|
||||
#define R_FS_FILE_TYPE_DIRECTORY 'd'
|
||||
|
@ -55,16 +61,17 @@ R_API RFS *r_fs_new ();
|
|||
R_API void r_fs_free (RFS* fs);
|
||||
R_API void r_fs_add (RFS *fs, RFSPlugin *p);
|
||||
R_API void r_fs_del (RFS *fs, RFSPlugin *p);
|
||||
R_API RFSRoot *r_fs_mount (RFS* fs, const char *fstype, const char *path);
|
||||
R_API RFSRoot *r_fs_mount (RFS* fs, const char *fstype, const char *path, ut64 delta);
|
||||
R_API int r_fs_umount (RFS* fs, const char *path);
|
||||
R_API RFSRoot *r_fs_root (RFS *fs, const char *path);
|
||||
R_API RFSFile *r_fs_open (RFS* fs, const char *path);
|
||||
R_API void r_fs_close (RFS* fs, RFSFile *file);
|
||||
R_API int r_fs_read (RFS* fs, RFSFile *file, ut64 addr, int len);
|
||||
R_API RFSFile *r_fs_load(RFS* fs, const char *path);
|
||||
R_API RList *r_fs_dir(RFS* fs, const char *path);
|
||||
|
||||
/* file.c */
|
||||
R_API RFSFile *r_fs_file_new (const char *path);
|
||||
R_API RFSFile *r_fs_file_new (RFSRoot *root, const char *path);
|
||||
R_API void r_fs_file_free (RFSFile *file);
|
||||
R_API RFSRoot *r_fs_root_new (const char *path, ut64 delta);
|
||||
R_API void r_fs_root_free (RFSRoot *root);
|
||||
|
|
|
@ -130,6 +130,8 @@ R_API int r_io_set_fd(RIO *io, int fd) {
|
|||
|
||||
R_API int r_io_read(struct r_io_t *io, ut8 *buf, int len) {
|
||||
int ret;
|
||||
if (io==NULL)
|
||||
return -1;
|
||||
/* check section permissions */
|
||||
if (io->enforce_rwx && !(r_io_section_get_rwx (io, io->off) & R_IO_READ))
|
||||
return -1;
|
||||
|
@ -269,7 +271,8 @@ R_API int r_io_write_at(struct r_io_t *io, ut64 addr, const ut8 *buf, int len) {
|
|||
R_API ut64 r_io_seek(struct r_io_t *io, ut64 offset, int whence) {
|
||||
int posix_whence = SEEK_SET;
|
||||
ut64 ret = -1;
|
||||
|
||||
if (io == NULL)
|
||||
return offset; // XXX
|
||||
switch(whence) {
|
||||
case R_IO_SEEK_SET:
|
||||
posix_whence = SEEK_SET;
|
||||
|
|
Loading…
Reference in New Issue