diff --git a/TODO b/TODO index eebd212865..e601166559 100644 --- a/TODO +++ b/TODO @@ -5,6 +5,10 @@ <{include libr/TODO}> +* Add support for aout binaries? +* eprintf should be modified to log into a file + - eprintf_open() -- start log to file + - eprintf_close() -- stop log to file /a ??? deprecated analyze code? srsly? * Search for wide strings /Z or so? diff --git a/doc/plugins b/doc/plugins index ab7144548e..110fce14ab 100644 --- a/doc/plugins +++ b/doc/plugins @@ -9,6 +9,8 @@ Libraries can be compiled: ./configure-plugins --enable-shared --enable-dynamic +LIBR_PLUGINS environment variable is honored as another search path for plugins + Plugins can be: - not compiled - compiled as shared diff --git a/libr/Makefile b/libr/Makefile index 1de62a2c61..9d8bf42193 100644 --- a/libr/Makefile +++ b/libr/Makefile @@ -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 +LIBLIST+=reg asm anal print parse search diff bp sign th db crypto debug core fs # TODO : generate single library linking against the rest #LIBSO=libr.so diff --git a/libr/core/Makefile b/libr/core/Makefile index 8655cc8d83..38392fd829 100644 --- a/libr/core/Makefile +++ b/libr/core/Makefile @@ -2,7 +2,7 @@ NAME=r_core DEPS=r_config r_cons r_line r_io r_cmd r_util r_print r_flags r_asm r_lib DEPS+=r_debug r_hash r_bin r_lang r_io r_anal r_parse r_print r_bp -DEPS+=r_reg r_search r_syscall r_sign r_diff r_vm r_socket +DEPS+=r_reg r_search r_syscall r_sign r_diff r_vm r_socket r_fs OBJ=core.o cmd.o file.o config.o visual.o io.o yank.o libs.o anal.o project.o gdiff.o asm.o rtr.o diff --git a/libr/core/cmd.c b/libr/core/cmd.c index 686b9bb0d7..f547406453 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2010 */ +/* radare - LGPL - Copyright 2009-2011 */ /* nibble<.ds@gmail.com> */ /* pancake */ @@ -886,6 +886,7 @@ static void r_core_cmd_bp(RCore *core, const char *input) { } /* TODO: this should be moved to the core->yank api */ +// TODO: arg must be const !!! use strdup here static int cmd_yank_to(RCore *core, char *arg) { ut64 src = core->offset; ut64 len = 0; @@ -915,13 +916,101 @@ static int cmd_yank_to(RCore *core, char *arg) { buf = (ut8*)malloc (len); r_core_read_at (core, src, buf, len); r_core_write_at (core, pos, buf, len); - free(buf); + free (buf); core->offset = src; r_core_block_read (core, 0); return 0; } +static int cmd_mount(void *data, const char *_input) { + char *input, *oinput, *ptr; + RList *list; + RListIter *iter; + RFSFile *file; + RFSRoot *root; + RFSPlugin *plug; + RCore *core = (RCore *)data; + input = oinput = strdup (_input); + + switch (input[0]) { + case ' ': + input++; + if (input[0]==' ') + input++; + ptr = strchr (input, ' '); + if (ptr) { + *ptr = 0; + r_fs_mount (core->fs, input, ptr+1); + } else eprintf ("Usage: m ext2 /mnt"); + break; + case '-': + r_fs_umount (core->fs, input+1); + break; + 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); + } + 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); + } + break; + case 'l': // list of plugins + r_list_foreach (core->fs->plugins, iter, plug) { + r_cons_printf ("%s\t%s\n", plug->name, plug->desc); + } + break; + case 'd': + input++; + if (input[0]==' ') + input++; + list = r_fs_dir (core->fs, input); + if (list) { + r_list_foreach (list, iter, file) { + r_cons_printf ("%c %s\n", file->type, file->name); + } + r_list_free (list); + } else eprintf ("Cannot open '%s' directory\n", input); + break; + case 'g': + input++; + if (input[0]==' ') + input++; + file = r_fs_open (core->fs, input); + if (file) { + // XXX: dump to file or just pipe? + r_fs_read (core->fs, file, 0, file->size); + write (1, file->data, file->size); + r_fs_close (core->fs, file); + } else eprintf ("Cannot open file\n"); + break; + case 'y': + eprintf ("TODO\n"); + break; + case '?': + 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* ; same as above, but in r2 commands\n" + " ml ; list filesystem plugins\n" + " m-/ ; umount given path (/)\n" + " m? ; display this help\n" + " my ; yank contents of file into clipboard\n" + " mg /foo ; get contents of file dumped to disk (XXX?)\n" + " md / ; list directory contents for path\n" + " m? ; show this help\n" + "TODO: support multiple mountpoints and RFile IO's (need io+core refactor)\n" + ); + break; + } + free (oinput); + return 0; +} + static int cmd_yank(void *data, const char *input) { RCore *core = (RCore *)data; switch (input[0]) { @@ -4155,6 +4244,7 @@ void r_core_cmd_init(RCore *core) { core->cmd->macro.cmd = r_core_cmd0; r_cmd_set_data (core->cmd, core); r_cmd_add (core->cmd, "x", "alias for px", &cmd_hexdump); + r_cmd_add (core->cmd, "mount", "mount filesystem", &cmd_mount); r_cmd_add (core->cmd, "analysis", "analysis", &cmd_anal); r_cmd_add (core->cmd, "flag", "get/set flags", &cmd_flag); r_cmd_add (core->cmd, "debug", "debugger operations", &cmd_debug); diff --git a/libr/core/core.c b/libr/core/core.c index ba66992270..c33055e0d2 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -203,6 +203,8 @@ 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 (); diff --git a/libr/fs/Makefile b/libr/fs/Makefile index fa5ee8b289..06f128e013 100644 --- a/libr/fs/Makefile +++ b/libr/fs/Makefile @@ -6,9 +6,9 @@ include ../config.mk foo: pre ${LIBSO} ${LIBAR} plugins -include ${STATIC_ASM_PLUGINS} +include ${STATIC_FS_PLUGINS} STATIC_OBJS=$(subst ..,p/..,$(subst fs_,p/fs_,$(STATIC_OBJ))) -OBJ=${STATIC_OBJS} fs.o code.o +OBJ=${STATIC_OBJS} fs.o file.o pre: @if [ ! -e libr_fs.${EXT_SO} ]; then if [ ! -e libr_fs.${EXT_AR} ]; then rm -f ${STATIC_OBJS} ; fi ; fi diff --git a/libr/fs/file.c b/libr/fs/file.c new file mode 100644 index 0000000000..13f8efb3ce --- /dev/null +++ b/libr/fs/file.c @@ -0,0 +1,29 @@ +/* radare - LGPL - Copyright 2011 pancake */ + +#include + +R_API RFSFile *r_fs_file_new (const char *path) { + RFSFile *file = R_NEW (RFSFile); + memset (file, 0, sizeof (RFSFile)); + file->name = strdup (path); + return file; +} + +R_API void r_fs_file_free (RFSFile *file) { + free (file->name); + free (file->data); + free (file); +} + +// TODO: Use RFSRoot and pass it in the stack instead of heap? problematic with bindings +R_API RFSRoot *r_fs_root_new (const char *path, ut64 delta) { + RFSRoot *root = R_NEW (RFSRoot); + root->path = strdup (path); + root->delta = delta; + return root; +} + +R_API void r_fs_root_free (RFSRoot *root) { + free (root->path); + free (root); +} diff --git a/libr/fs/fs.c b/libr/fs/fs.c index 61786f8612..23621ea1b0 100644 --- a/libr/fs/fs.c +++ b/libr/fs/fs.c @@ -1,22 +1,137 @@ -/* radare - LGPL - Copyright 2008-2010 pancake */ +/* radare - LGPL - Copyright 2011 pancake */ #include +#include "../config.h" +static RFSPlugin *fs_static_plugins[] = + { R_FS_STATIC_PLUGINS }; + +/* lifecycle */ // TODO: needs much more love R_API RFS *r_fs_new () { + int i; + RFSPlugin *static_plugin; RFS *fs = R_NEW (RFS); if (fs) { + fs->roots = r_list_new (); + fs->roots->free = (RListFree)r_fs_root_free; fs->plugins = r_list_new (); + // XXX fs->roots->free = r_fs_plugin_free; + for (i=0; fs_static_plugins[i]; i++) { + static_plugin = R_NEW (RFSPlugin); + memcpy (static_plugin, fs_static_plugins[i], sizeof (RFSPlugin)); + r_fs_add (fs, static_plugin); + } } return fs; } -R_API void r_fs_free (RFS* fs) { - free (fs); -} - -R_API RFSRoot *r_fs_mount (RFS* fs) { +R_API RFSPlugin *r_fs_plugin_get (RFS *fs, const char *name) { + RListIter *iter; + RFSPlugin *p; + r_list_foreach (fs->plugins, iter, p) { + if (!strcmp (p->name, name)) + return p; + } return NULL; } -R_API int r_fs_umount (RFS* fs, +R_API void r_fs_free (RFS* fs) { + r_list_free (fs->plugins); + r_list_free (fs->roots); + free (fs); +} + +/* plugins */ + +R_API void r_fs_add (RFS *fs, RFSPlugin *p) { + // find coliding plugin name + if (p) { + if (p->init) + p->init (); + } + r_list_append (fs->plugins, p); +} + +R_API void r_fs_del (RFS *fs, RFSPlugin *p) { + // TODO: implement +} + +/* mountpoint */ + +R_API RFSRoot *r_fs_mount (RFS* fs, const char *fstype, const char *path) { + RFSPlugin *p; + 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; + 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"); + return root; +} + +static inline int r_fs_match (const char *root, const char *path) { + return (!strncmp (path, root, strlen (path))); +} + +R_API int r_fs_umount (RFS* fs, const char *path) { + RFSRoot *root; + RListIter *iter; + r_list_foreach (fs->roots, iter, root) { + if (r_fs_match (path, root->path)) { + r_list_delete (fs->roots, iter); + return R_TRUE; + } + } + return R_FALSE; +} + +R_API RFSRoot *r_fs_root (RFS *fs, const char *path) { + RFSRoot *root; + RListIter *iter; + r_list_foreach (fs->roots, iter, root) { + if (r_fs_match (path, root->path)) + return root; + } + return NULL; +} + +/* filez */ + +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); + return NULL; +} + +R_API void r_fs_close (RFS* fs, RFSFile *file) { + if (fs && file) + file->fs->close (file); +} + +R_API int r_fs_read (RFS* fs, RFSFile *file, ut64 addr, int len) { + if (fs && file) { + free (file->data); + file->data = malloc (file->size); + file->fs->read (file, addr, len); + } + return R_FALSE; +} + +R_API RList *r_fs_dir(RFS* fs, const char *path) { + if (fs) { + RFSRoot *root = r_fs_root (fs, path); + if (root) + return root->fs->dir (root, path); + eprintf ("r_fs_dir: error, path %s is not mounted\n", path); + } + return NULL; +} diff --git a/libr/fs/p/Makefile b/libr/fs/p/Makefile new file mode 100644 index 0000000000..b5f1c4d6cc --- /dev/null +++ b/libr/fs/p/Makefile @@ -0,0 +1,29 @@ +include ../../config.mk + +CFLAGS+=-I../../include -Wall -shared -fPIC ${LDFLAGS_LIB} ${LDFLAGS_LINKPATH}.. +# XXX +CFLAGS+=-DLIL_ENDIAN=1 +CFLAGS+=-L../../util -lr_util +LDFLAGS+=${LINK} + +foo: all + +ALL_TARGETS= +# TODO: rename to enabled plugins +FSS=ext2.mk +include $(FSS) + +all: grublib ${ALL_TARGETS} + @true + +grublib: + cd grub && ${MAKE} lib + +clean: + -rm -f *.${EXT_SO} *.o ${STATIC_OBJ} + cd grub && ${MAKE} clean + +mrproper: clean + -rm -f *.d ../arch/*/*/*.d + +.PHONY: all clean foo mrproper diff --git a/libr/fs/p/ext2.mk b/libr/fs/p/ext2.mk new file mode 100644 index 0000000000..af3f059be4 --- /dev/null +++ b/libr/fs/p/ext2.mk @@ -0,0 +1,9 @@ +OBJ_EXT2=fs_ext2.o + +STATIC_OBJ+=${OBJ_EXT2} +TARGET_EXT2=fs_ext2.${EXT_SO} + +ALL_TARGETS+=${TARGET_EXT2} + +${TARGET_EXT2}: ${OBJ_EXT2} + ${CC} ${LDFLAGS} ${CFLAGS} -o ${TARGET_EXT2} ${OBJ_EXT2} diff --git a/libr/fs/p/fs_ext2.c b/libr/fs/p/fs_ext2.c new file mode 100644 index 0000000000..fe941234f5 --- /dev/null +++ b/libr/fs/p/fs_ext2.c @@ -0,0 +1,27 @@ +/* radare - LGPL - Copyright 2010 pancake */ + +#include + +static RFSFile* ext2_open(const char *path) { + return NULL; +} + +static boolt ext2_read(RFSFile *fs, ut64 addr, int len) { + return R_FALSE; +} + +static void ext2_close(RFSFile *fs) { +} + +static RList *ext2_dir(RFSRoot *root, const char *path) { + return NULL; +} + +struct r_fs_plugin_t r_fs_plugin_ext2 = { + .name = "ext2", + .desc = "ext2 filesystem", + .open = ext2_open, + .read = ext2_read, + .close = ext2_close, + .dir = ext2_dir +}; diff --git a/libr/fs/p/fs_ext2.d b/libr/fs/p/fs_ext2.d new file mode 100644 index 0000000000..c566ea2a12 --- /dev/null +++ b/libr/fs/p/fs_ext2.d @@ -0,0 +1,27 @@ +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 \ + /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 \ + /usr/include/bits/types.h /usr/include/bits/typesizes.h \ + /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \ + /usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/stdarg.h \ + /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h \ + /usr/include/string.h /usr/include/xlocale.h /usr/include/stdlib.h \ + /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h \ + /usr/include/endian.h /usr/include/bits/endian.h \ + /usr/include/bits/byteswap.h /usr/include/sys/types.h \ + /usr/include/time.h /usr/include/sys/select.h /usr/include/bits/select.h \ + /usr/include/bits/sigset.h /usr/include/bits/time.h \ + /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h \ + /usr/include/alloca.h /usr/include/sys/time.h /usr/include/sys/stat.h \ + /usr/include/bits/stat.h /usr/include/fcntl.h /usr/include/bits/fcntl.h \ + /usr/include/bits/uio.h /usr/include/dirent.h /usr/include/bits/dirent.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 diff --git a/libr/fs/p/grub/Makefile b/libr/fs/p/grub/Makefile new file mode 100644 index 0000000000..3519ae2a3b --- /dev/null +++ b/libr/fs/p/grub/Makefile @@ -0,0 +1,31 @@ +# TODO include ..config.mk +CC?=gcc +KERNFILES=kern/file.c +KERNFILES+=kern/term.c kern/device.c +KERNFILES+=kern/err.c +KERNFILES+=kern/env.c kern/disk.c +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 + +KERNOBJS=$(subst .c,.o,${KERNFILES}) +CFLAGS=-Iinclude -g +BIN=test${EXT_EXE} + +all: ${KERNOBJS} ${BIN} + +${BIN}: + ${CC} -o ${BIN} ${CFLAGS} ${KERNOBJS} + +lib: all libgrubfs.a + + +libgrubfs.a: + rm -f libgrubfs.a + ar -q libgrubfs.a ${KERNOBJS} + +clean: + rm -f ${KERNOBJS} ${BIN} + rm -f libgrubfs.a diff --git a/libr/fs/p/grub/TODO b/libr/fs/p/grub/TODO new file mode 100644 index 0000000000..38e9bbc93f --- /dev/null +++ b/libr/fs/p/grub/TODO @@ -0,0 +1,3 @@ +* Reduce the number of needed include files +* Add a sync method +* Use libtrum (from netbsd?) diff --git a/libr/fs/p/grub/fs/.ext2.c.swo b/libr/fs/p/grub/fs/.ext2.c.swo new file mode 100644 index 0000000000..d7c894e734 Binary files /dev/null and b/libr/fs/p/grub/fs/.ext2.c.swo differ diff --git a/libr/fs/p/grub/fs/affs.c b/libr/fs/p/grub/fs/affs.c new file mode 100644 index 0000000000..40be4b2f61 --- /dev/null +++ b/libr/fs/p/grub/fs/affs.c @@ -0,0 +1,553 @@ +/* affs.c - Amiga Fast FileSystem. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* The affs bootblock. */ +struct grub_affs_bblock +{ + grub_uint8_t type[3]; + grub_uint8_t flags; + grub_uint32_t checksum; + grub_uint32_t rootblock; +} __attribute__ ((packed)); + +/* Set if the filesystem is a AFFS filesystem. Otherwise this is an + OFS filesystem. */ +#define GRUB_AFFS_FLAG_FFS 1 + +/* The affs rootblock. */ +struct grub_affs_rblock +{ + grub_uint8_t type[4]; + grub_uint8_t unused1[8]; + grub_uint32_t htsize; + grub_uint32_t unused2; + grub_uint32_t checksum; + grub_uint32_t hashtable[1]; +} __attribute__ ((packed)); + +/* The second part of a file header block. */ +struct grub_affs_file +{ + grub_uint8_t unused1[12]; + grub_uint32_t size; + grub_uint8_t unused2[104]; + grub_uint8_t namelen; + grub_uint8_t name[30]; + grub_uint8_t unused3[33]; + grub_uint32_t next; + grub_uint32_t parent; + grub_uint32_t extension; + grub_int32_t type; +} __attribute__ ((packed)); + +/* The location of `struct grub_affs_file' relative to the end of a + file header block. */ +#define GRUB_AFFS_FILE_LOCATION 200 + +/* The offset in both the rootblock and the file header block for the + hashtable, symlink and block pointers (all synonyms). */ +#define GRUB_AFFS_HASHTABLE_OFFSET 24 +#define GRUB_AFFS_BLOCKPTR_OFFSET 24 +#define GRUB_AFFS_SYMLINK_OFFSET 24 + +#define GRUB_AFFS_SYMLINK_SIZE(blocksize) ((blocksize) - 225) + +#define GRUB_AFFS_FILETYPE_DIR -3 +#define GRUB_AFFS_FILETYPE_REG 2 +#define GRUB_AFFS_FILETYPE_SYMLINK 3 + + +struct grub_fshelp_node +{ + struct grub_affs_data *data; + int block; + int size; + int parent; +}; + +/* Information about a "mounted" affs filesystem. */ +struct grub_affs_data +{ + struct grub_affs_bblock bblock; + struct grub_fshelp_node diropen; + grub_disk_t disk; + + /* Blocksize in sectors. */ + int blocksize; + + /* The number of entries in the hashtable. */ + int htsize; +}; + +static grub_dl_t my_mod; + + +static grub_disk_addr_t +grub_affs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + int links; + grub_uint32_t pos; + int block = node->block; + struct grub_affs_file file; + struct grub_affs_data *data = node->data; + grub_uint32_t mod; + + /* Find the block that points to the fileblock we are looking up by + following the chain until the right table is reached. */ + for (links = grub_divmod64 (fileblock, data->htsize, &mod); links; links--) + { + grub_disk_read (data->disk, block + data->blocksize - 1, + data->blocksize * (GRUB_DISK_SECTOR_SIZE + - GRUB_AFFS_FILE_LOCATION), + sizeof (file), &file); + if (grub_errno) + return 0; + + block = grub_be_to_cpu32 (file.extension); + } + + /* Translate the fileblock to the block within the right table. */ + fileblock = mod; + grub_disk_read (data->disk, block, + GRUB_AFFS_BLOCKPTR_OFFSET + + (data->htsize - fileblock - 1) * sizeof (pos), + sizeof (pos), &pos); + if (grub_errno) + return 0; + + return grub_be_to_cpu32 (pos); +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_affs_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_affs_read_block, + node->size, 0); +} + + +static struct grub_affs_data * +grub_affs_mount (grub_disk_t disk) +{ + struct grub_affs_data *data; + grub_uint32_t *rootblock = 0; + struct grub_affs_rblock *rblock; + + int checksum = 0; + int checksumr = 0; + int blocksize = 0; + + data = grub_malloc (sizeof (struct grub_affs_data)); + if (!data) + return 0; + + /* Read the bootblock. */ + grub_disk_read (disk, 0, 0, sizeof (struct grub_affs_bblock), + &data->bblock); + if (grub_errno) + goto fail; + + /* Make sure this is an affs filesystem. */ + if (grub_strncmp ((char *) (data->bblock.type), "DOS", 3)) + { + grub_error (GRUB_ERR_BAD_FS, "not an AFFS filesystem"); + goto fail; + } + + /* Test if the filesystem is a OFS filesystem. */ + if (! (data->bblock.flags & GRUB_AFFS_FLAG_FFS)) + { + grub_error (GRUB_ERR_BAD_FS, "OFS not yet supported"); + goto fail; + } + + /* Read the bootblock. */ + grub_disk_read (disk, 0, 0, sizeof (struct grub_affs_bblock), + &data->bblock); + if (grub_errno) + goto fail; + + /* No sane person uses more than 8KB for a block. At least I hope + for that person because in that case this won't work. */ + rootblock = grub_malloc (GRUB_DISK_SECTOR_SIZE * 16); + if (!rootblock) + goto fail; + + rblock = (struct grub_affs_rblock *) rootblock; + + /* Read the rootblock. */ + grub_disk_read (disk, grub_be_to_cpu32 (data->bblock.rootblock), 0, + GRUB_DISK_SECTOR_SIZE * 16, rootblock); + if (grub_errno) + goto fail; + + /* The filesystem blocksize is not stored anywhere in the filesystem + itself. One way to determine it is reading blocks for the + rootblock until the checksum is correct. */ + checksumr = grub_be_to_cpu32 (rblock->checksum); + rblock->checksum = 0; + for (blocksize = 0; blocksize < 8; blocksize++) + { + grub_uint32_t *currblock = rootblock + GRUB_DISK_SECTOR_SIZE * blocksize; + unsigned int i; + + for (i = 0; i < GRUB_DISK_SECTOR_SIZE / sizeof (*currblock); i++) + checksum += grub_be_to_cpu32 (currblock[i]); + + if (checksumr == -checksum) + break; + } + if (-checksum != checksumr) + { + grub_error (GRUB_ERR_BAD_FS, "AFFS blocksize couldn't be determined"); + goto fail; + } + blocksize++; + + data->blocksize = blocksize; + data->disk = disk; + data->htsize = grub_be_to_cpu32 (rblock->htsize); + data->diropen.data = data; + data->diropen.block = grub_be_to_cpu32 (data->bblock.rootblock); + + grub_free (rootblock); + + return data; + + fail: + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not an AFFS filesystem"); + + grub_free (data); + grub_free (rootblock); + return 0; +} + + +static char * +grub_affs_read_symlink (grub_fshelp_node_t node) +{ + struct grub_affs_data *data = node->data; + char *symlink; + + symlink = grub_malloc (GRUB_AFFS_SYMLINK_SIZE (data->blocksize)); + if (!symlink) + return 0; + + grub_disk_read (data->disk, node->block, GRUB_AFFS_SYMLINK_OFFSET, + GRUB_AFFS_SYMLINK_SIZE (data->blocksize), symlink); + if (grub_errno) + { + grub_free (symlink); + return 0; + } + grub_dprintf ("affs", "Symlink: `%s'\n", symlink); + return symlink; +} + + +static int +grub_affs_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + int i; + struct grub_affs_file file; + struct grub_fshelp_node *node = 0; + struct grub_affs_data *data = dir->data; + grub_uint32_t *hashtable; + + auto int NESTED_FUNC_ATTR grub_affs_create_node (const char *name, int block, + int size, int type); + + int NESTED_FUNC_ATTR grub_affs_create_node (const char *name, int block, + int size, int type) + { + node = grub_malloc (sizeof (*node)); + if (!node) + { + grub_free (hashtable); + return 1; + } + + node->data = data; + node->size = size; + node->block = block; + node->parent = grub_be_to_cpu32 (file.parent); + + if (hook (name, type, node)) + { + grub_free (hashtable); + return 1; + } + return 0; + } + + hashtable = grub_malloc (data->htsize * sizeof (*hashtable)); + if (!hashtable) + return 1; + + grub_disk_read (data->disk, dir->block, GRUB_AFFS_HASHTABLE_OFFSET, + data->htsize * sizeof (*hashtable), (char *) hashtable); + if (grub_errno) + goto fail; + + /* Create the directory entries for `.' and `..'. */ + if (grub_affs_create_node (".", dir->block, dir->size, GRUB_FSHELP_DIR)) + return 1; + if (grub_affs_create_node ("..", dir->parent ? dir->parent : dir->block, + dir->size, GRUB_FSHELP_DIR)) + return 1; + + for (i = 0; i < data->htsize; i++) + { + enum grub_fshelp_filetype type; + grub_uint64_t next; + + if (!hashtable[i]) + continue; + + /* Every entry in the hashtable can be chained. Read the entire + chain. */ + next = grub_be_to_cpu32 (hashtable[i]); + + while (next) + { + grub_disk_read (data->disk, next + data->blocksize - 1, + data->blocksize * GRUB_DISK_SECTOR_SIZE + - GRUB_AFFS_FILE_LOCATION, + sizeof (file), (char *) &file); + if (grub_errno) + goto fail; + + file.name[file.namelen] = '\0'; + + if ((int) grub_be_to_cpu32 (file.type) == GRUB_AFFS_FILETYPE_DIR) + type = GRUB_FSHELP_REG; + else if (grub_be_to_cpu32 (file.type) == GRUB_AFFS_FILETYPE_REG) + type = GRUB_FSHELP_DIR; + else if (grub_be_to_cpu32 (file.type) == GRUB_AFFS_FILETYPE_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else + type = GRUB_FSHELP_UNKNOWN; + + if (grub_affs_create_node ((char *) (file.name), next, + grub_be_to_cpu32 (file.size), type)) + return 1; + + next = grub_be_to_cpu32 (file.next); + } + } + + grub_free (hashtable); + return 0; + + fail: + grub_free (node); + grub_free (hashtable); + return 0; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_affs_open (struct grub_file *file, const char *name) +{ + struct grub_affs_data *data; + struct grub_fshelp_node *fdiro = 0; + + grub_dl_ref (my_mod); + + data = grub_affs_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_affs_iterate_dir, + grub_affs_read_symlink, GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + file->size = fdiro->size; + data->diropen = *fdiro; + grub_free (fdiro); + + file->data = data; + file->offset = 0; + + return 0; + + fail: + if (data && fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_err_t +grub_affs_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + + +/* Read LEN bytes data from FILE into BUF. */ +static grub_ssize_t +grub_affs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_affs_data *data = + (struct grub_affs_data *) file->data; + + int size = grub_affs_read_file (&data->diropen, file->read_hook, + file->offset, len, buf); + + return size; +} + + +static grub_err_t +grub_affs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_affs_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_affs_mount (device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_affs_iterate_dir, + grub_affs_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_affs_iterate_dir (fdiro, iterate); + + fail: + if (data && fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_err_t +grub_affs_label (grub_device_t device, char **label) +{ + struct grub_affs_data *data; + struct grub_affs_file file; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_affs_mount (disk); + if (data) + { + /* The rootblock maps quite well on a file header block, it's + something we can use here. */ + grub_disk_read (data->disk, grub_be_to_cpu32 (data->bblock.rootblock), + data->blocksize * (GRUB_DISK_SECTOR_SIZE + - GRUB_AFFS_FILE_LOCATION), + sizeof (file), &file); + if (grub_errno) + return 0; + + *label = grub_strndup ((char *) (file.name), file.namelen); + } + else + *label = 0; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + +static struct grub_fs grub_affs_fs = + { + .name = "affs", + .dir = grub_affs_dir, + .open = grub_affs_open, + .read = grub_affs_read, + .close = grub_affs_close, + .label = grub_affs_label, +#ifdef GRUB_UTIL + .reserved_first_sector = 0, +#endif + .next = 0 + }; + +GRUB_MOD_INIT(affs) +{ + grub_fs_register (&grub_affs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(affs) +{ + grub_fs_unregister (&grub_affs_fs); +} diff --git a/libr/fs/p/grub/fs/afs.c b/libr/fs/p/grub/fs/afs.c new file mode 100644 index 0000000000..cd61f4db9b --- /dev/null +++ b/libr/fs/p/grub/fs/afs.c @@ -0,0 +1,718 @@ +/* afs.c - The native AtheOS file-system. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef MODE_BIGENDIAN +#define GRUB_AFS_FSNAME_SUFFIX "_be" +#else +#define GRUB_AFS_FSNAME_SUFFIX "" +#endif + +#ifdef MODE_BFS +#define GRUB_AFS_FSNAME "befs" GRUB_AFS_FSNAME_SUFFIX +#else +#define GRUB_AFS_FSNAME "afs" GRUB_AFS_FSNAME_SUFFIX +#endif + +#define GRUB_AFS_DIRECT_BLOCK_COUNT 12 +#define GRUB_AFS_BLOCKS_PER_DI_RUN 4 + +#ifdef MODE_BFS +#define GRUB_AFS_SBLOCK_SECTOR 1 +#define GRUB_AFS_SBLOCK_MAGIC1 0x42465331 /* BFS1. */ +#else +#define GRUB_AFS_SBLOCK_SECTOR 2 +#define GRUB_AFS_SBLOCK_MAGIC1 0x41465331 /* AFS1. */ +#endif + +#define GRUB_AFS_SBLOCK_MAGIC2 0xdd121031 +#define GRUB_AFS_SBLOCK_MAGIC3 0x15b6830e + +#define GRUB_AFS_INODE_MAGIC 0x64358428 + +#ifdef MODE_BFS +#define GRUB_AFS_BTREE_MAGIC 0x69f6c2e8 +#else +#define GRUB_AFS_BTREE_MAGIC 0x65768995 +#endif + +#define GRUB_AFS_BNODE_SIZE 1024 + +#define GRUB_AFS_S_IFMT 00170000 +#define GRUB_AFS_S_IFLNK 0120000 + +#define GRUB_AFS_S_IFREG 0100000 +#define GRUB_AFS_S_IFDIR 0040000 +#define GRUB_AFS_S_IFIFO 0010000 + +#define GRUB_AFS_NULL_VAL ((grub_afs_bvalue_t)-1) + +#ifdef MODE_BIGENDIAN +#define grub_afs_to_cpu16(x) grub_be_to_cpu16 (x) +#define grub_afs_to_cpu32(x) grub_be_to_cpu32 (x) +#define grub_afs_to_cpu64(x) grub_be_to_cpu64 (x) +#else +#define grub_afs_to_cpu16(x) grub_le_to_cpu16 (x) +#define grub_afs_to_cpu32(x) grub_le_to_cpu32 (x) +#define grub_afs_to_cpu64(x) grub_le_to_cpu64 (x) +#endif + +#ifdef MODE_BFS +#define B_KEY_INDEX_ALIGN 8 +#else +#define B_KEY_INDEX_ALIGN 4 +#endif + +#define B_KEY_INDEX_OFFSET(node) ((grub_uint16_t *) \ + ((char *) (node) \ + + ALIGN_UP (sizeof (struct grub_afs_bnode) \ + + node->key_size, \ + B_KEY_INDEX_ALIGN))) + +#define B_KEY_VALUE_OFFSET(node) ((grub_afs_bvalue_t *) \ + ((char *) B_KEY_INDEX_OFFSET (node) + \ + node->key_count * 2)) + +typedef grub_uint64_t grub_afs_off_t; +typedef grub_uint64_t grub_afs_bigtime; +typedef grub_uint64_t grub_afs_bvalue_t; + +struct grub_afs_blockrun +{ + grub_uint32_t group; + grub_uint16_t start; + grub_uint16_t len; +} __attribute__ ((packed)); + +struct grub_afs_datastream +{ + struct grub_afs_blockrun direct[GRUB_AFS_DIRECT_BLOCK_COUNT]; + grub_afs_off_t max_direct_range; + struct grub_afs_blockrun indirect; + grub_afs_off_t max_indirect_range; + struct grub_afs_blockrun double_indirect; + grub_afs_off_t max_double_indirect_range; + grub_afs_off_t size; +} __attribute__ ((packed)); + +struct grub_afs_bnode +{ + grub_afs_bvalue_t left; + grub_afs_bvalue_t right; + grub_afs_bvalue_t overflow; +#ifdef MODE_BFS + grub_uint16_t key_count; + grub_uint16_t key_size; +#else + grub_uint32_t key_count; + grub_uint32_t key_size; +#endif + char key_data[0]; +} __attribute__ ((packed)); + +#ifdef MODE_BFS +struct grub_afs_btree +{ + grub_uint32_t magic; + grub_uint32_t unused1; + grub_uint32_t tree_depth; + grub_uint32_t unused2; + grub_afs_bvalue_t root; + grub_uint32_t unused3[4]; +} __attribute__ ((packed)); +#else +struct grub_afs_btree +{ + grub_uint32_t magic; + grub_afs_bvalue_t root; + grub_uint32_t tree_depth; + grub_afs_bvalue_t last_node; + grub_afs_bvalue_t first_free; +} __attribute__ ((packed)); +#endif + +/* Beware that following structure describes AtheFS and if you write code + which uses currently unused fields check it with both AtheFS and BeFS. + */ +struct grub_afs_sblock +{ + char name[32]; + grub_uint32_t magic1; + grub_uint32_t byte_order; + grub_uint32_t block_size; + grub_uint32_t block_shift; + grub_afs_off_t num_blocks; + grub_afs_off_t used_blocks; + grub_uint32_t inode_size; + grub_uint32_t magic2; + grub_uint32_t block_per_group; /* Number of blocks per allocation + group. (Max 65536) */ + grub_uint32_t alloc_group_shift; /* Number of bits to shift a group + number to get a byte address. */ + grub_uint32_t alloc_group_count; + grub_uint32_t flags; + struct grub_afs_blockrun log_block; + grub_afs_off_t log_start; + grub_uint32_t valid_log_blocks; + grub_uint32_t log_size; + grub_uint32_t magic3; + struct grub_afs_blockrun root_dir; /* Root dir inode. */ + struct grub_afs_blockrun deleted_files; /* Directory containing files + scheduled for deletion. */ + struct grub_afs_blockrun index_dir; /* Directory of index files. */ + grub_uint32_t boot_loader_size; + grub_uint32_t pad[7]; +} __attribute__ ((packed)); + +struct grub_afs_inode +{ + grub_uint32_t magic1; + struct grub_afs_blockrun inode_num; + grub_uint32_t uid; + grub_uint32_t gid; + grub_uint32_t mode; + grub_uint32_t flags; +#ifndef MODE_BFS + grub_uint32_t link_count; +#endif + grub_afs_bigtime create_time; + grub_afs_bigtime modified_time; + struct grub_afs_blockrun parent; + struct grub_afs_blockrun attrib_dir; + grub_uint32_t index_type; /* Key data-key only used for index files. */ + grub_uint32_t inode_size; + grub_uint32_t unused; + struct grub_afs_datastream stream; + grub_uint32_t pad[4]; + grub_uint32_t small_data[1]; +} __attribute__ ((packed)); + +struct grub_fshelp_node +{ + struct grub_afs_data *data; + struct grub_afs_inode inode; +}; + +struct grub_afs_data +{ + grub_disk_t disk; + struct grub_afs_sblock sblock; + struct grub_afs_inode *inode; + struct grub_fshelp_node diropen; +}; + +static grub_dl_t my_mod; + +static grub_afs_off_t +grub_afs_run_to_num (struct grub_afs_sblock *sb, + struct grub_afs_blockrun *run) +{ + return ((grub_afs_off_t) grub_afs_to_cpu32 (run->group) + * sb->block_per_group + grub_afs_to_cpu16 (run->start)); +} + +static grub_err_t +grub_afs_read_inode (struct grub_afs_data *data, + grub_uint32_t ino, struct grub_afs_inode *inode) +{ + return grub_disk_read (data->disk, + ino * + (data->sblock.block_size >> GRUB_DISK_SECTOR_BITS), + 0, sizeof (struct grub_afs_inode), + inode); +} + +static grub_disk_addr_t +grub_afs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + struct grub_afs_sblock *sb = &node->data->sblock; + struct grub_afs_datastream *ds = &node->inode.stream; + + if (fileblock < grub_afs_to_cpu64 (ds->max_direct_range)) + { + int i; + + for (i = 0; i < GRUB_AFS_DIRECT_BLOCK_COUNT; i++) + { + if (fileblock < grub_afs_to_cpu16 (ds->direct[i].len)) + return grub_afs_run_to_num (sb, &ds->direct[i]) + fileblock; + fileblock -= grub_afs_to_cpu16 (ds->direct[i].len); + } + } + else if (fileblock < grub_afs_to_cpu64 (ds->max_indirect_range)) + { + int ptrs_per_blk = sb->block_size / sizeof (struct grub_afs_blockrun); + struct grub_afs_blockrun indir[ptrs_per_blk]; + grub_afs_off_t blk = grub_afs_run_to_num (sb, &ds->indirect); + int i; + + fileblock -= grub_afs_to_cpu64 (ds->max_direct_range); + for (i = 0; i < ds->indirect.len; i++, blk++) + { + int j; + + if (grub_disk_read (node->data->disk, + blk * (sb->block_size >> GRUB_DISK_SECTOR_BITS), + 0, sizeof (indir), + indir)) + return 0; + + for (j = 0; j < ptrs_per_blk; j++) + { + if (fileblock < grub_afs_to_cpu16 (indir[j].len)) + return grub_afs_run_to_num (sb, &indir[j]) + fileblock; + + fileblock -= grub_afs_to_cpu16 (indir[j].len); + } + } + } + else + { + int ptrs_per_blk = sb->block_size / sizeof (struct grub_afs_blockrun); + struct grub_afs_blockrun indir[ptrs_per_blk]; + + /* ([idblk][idptr]) ([dblk][dptr]) [blk] */ + int cur_pos = fileblock - grub_afs_to_cpu64 (ds->max_indirect_range); + + int dptr_size = GRUB_AFS_BLOCKS_PER_DI_RUN; + int dblk_size = dptr_size * ptrs_per_blk; + int idptr_size = dblk_size * GRUB_AFS_BLOCKS_PER_DI_RUN; + int idblk_size = idptr_size * ptrs_per_blk; + + int off = cur_pos % GRUB_AFS_BLOCKS_PER_DI_RUN; + int dptr = (cur_pos / dptr_size) % ptrs_per_blk; + int dblk = (cur_pos / dblk_size) % GRUB_AFS_BLOCKS_PER_DI_RUN; + int idptr = (cur_pos / idptr_size) % ptrs_per_blk; + int idblk = (cur_pos / idblk_size); + + if (grub_disk_read (node->data->disk, + (grub_afs_run_to_num (sb, &ds->double_indirect) + + idblk) * + (sb->block_size >> GRUB_DISK_SECTOR_BITS), + 0, sizeof (indir), + indir)) + return 0; + + if (grub_disk_read (node->data->disk, + (grub_afs_run_to_num (sb, &indir[idptr]) + dblk) * + (sb->block_size >> GRUB_DISK_SECTOR_BITS), + 0, sizeof (indir), + indir)) + return 0; + + return grub_afs_run_to_num (sb, &indir[dptr]) + off; + } + + return 0; +} + +static grub_ssize_t +grub_afs_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_afs_read_block, + grub_afs_to_cpu64 (node->inode.stream.size), + node->data->sblock.block_shift + - GRUB_DISK_SECTOR_BITS); +} + +static char * +grub_afs_read_symlink (grub_fshelp_node_t node) +{ + char *ret; + grub_afs_off_t size = grub_afs_to_cpu64 (node->inode.stream.size); + + if (size == 0) + { + size = sizeof (node->inode.stream); + ret = grub_zalloc (size + 1); + if (! ret) + return 0; + grub_memcpy (ret, (char *) &(node->inode.stream), + sizeof (node->inode.stream)); + return ret; + } + ret = grub_zalloc (size + 1); + if (! ret) + return 0; + grub_afs_read_file (node, 0, 0, size, ret); + return ret; +} + +static int +grub_afs_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + struct grub_afs_btree head; + char node_data [GRUB_AFS_BNODE_SIZE]; + struct grub_afs_bnode *node = (struct grub_afs_bnode *) node_data; + int i; + + if ((dir->inode.stream.size == 0) + || ((grub_afs_to_cpu32 (dir->inode.mode) & GRUB_AFS_S_IFMT) + != GRUB_AFS_S_IFDIR)) + return 0; + + grub_afs_read_file (dir, 0, 0, sizeof (head), (char *) &head); + if (grub_errno) + return 0; + + grub_afs_read_file (dir, 0, grub_afs_to_cpu64 (head.root), + GRUB_AFS_BNODE_SIZE, (char *) node); + if (grub_errno) + return 0; + + for (i = 0; i < (int) grub_afs_to_cpu32 (head.tree_depth) - 1; i++) + { + grub_afs_bvalue_t blk; + + blk = grub_afs_to_cpu64(B_KEY_VALUE_OFFSET (node) [0]); + grub_afs_read_file (dir, 0, blk, GRUB_AFS_BNODE_SIZE, (char *) node); + if (grub_errno) + return 0; + } + + if (node->key_count) + { + grub_uint32_t cur_key = 0; + + while (1) + { + int key_start, key_size; + grub_uint16_t *index; + + index = B_KEY_INDEX_OFFSET (node); + + key_start = (cur_key > 0) + ? grub_afs_to_cpu16 (index[cur_key - 1]) : 0; + key_size = grub_afs_to_cpu16 (index[cur_key]) - key_start; + if (key_size > 0) + { + char filename [key_size + 1]; + struct grub_fshelp_node *fdiro; + int mode, type; + + fdiro = grub_malloc (sizeof (struct grub_fshelp_node)); + if (! fdiro) + return 0; + + fdiro->data = dir->data; + if (grub_afs_read_inode (dir->data, + grub_afs_to_cpu64 + (B_KEY_VALUE_OFFSET (node) [cur_key]), + &fdiro->inode)) + return 0; + + grub_memcpy (filename, &node->key_data[key_start], key_size); + filename [key_size] = 0; + + mode = (grub_afs_to_cpu32 (fdiro->inode.mode) & GRUB_AFS_S_IFMT); + if (mode == GRUB_AFS_S_IFDIR) + type = GRUB_FSHELP_DIR; + else if (mode == GRUB_AFS_S_IFREG) + type = GRUB_FSHELP_REG; + else if (mode == GRUB_AFS_S_IFLNK) + type = GRUB_FSHELP_SYMLINK; + else + type = GRUB_FSHELP_UNKNOWN; + + if (hook (filename, type, fdiro)) + return 1; + } + + cur_key++; + if (cur_key >= grub_afs_to_cpu32 (node->key_count)) + { + if (node->right == GRUB_AFS_NULL_VAL) + break; + + grub_afs_read_file (dir, 0, grub_afs_to_cpu64 (node->right), + GRUB_AFS_BNODE_SIZE, (char *) node); + if (grub_errno) + return 0; + + cur_key = 0; + } + } + } + + return 0; +} + +static int +grub_afs_validate_sblock (struct grub_afs_sblock *sb) +{ + if (grub_afs_to_cpu32 (sb->magic1) == GRUB_AFS_SBLOCK_MAGIC1) + { + sb->magic2 = grub_afs_to_cpu32 (sb->magic2); + sb->magic3 = grub_afs_to_cpu32 (sb->magic3); + sb->block_shift = grub_afs_to_cpu32 (sb->block_shift); + sb->block_size = grub_afs_to_cpu32 (sb->block_size); + sb->used_blocks = grub_afs_to_cpu64 (sb->used_blocks); + sb->num_blocks = grub_afs_to_cpu64 (sb->num_blocks); + sb->inode_size = grub_afs_to_cpu32 (sb->inode_size); + sb->alloc_group_count = grub_afs_to_cpu32 (sb->alloc_group_count); + sb->alloc_group_shift = grub_afs_to_cpu32 (sb->alloc_group_shift); + sb->block_per_group = grub_afs_to_cpu32 (sb->block_per_group); + sb->alloc_group_count = grub_afs_to_cpu32 (sb->alloc_group_count); + sb->log_size = grub_afs_to_cpu32 (sb->log_size); + } + else + return 0; + + if ((sb->magic2 != GRUB_AFS_SBLOCK_MAGIC2) || + (sb->magic3 != GRUB_AFS_SBLOCK_MAGIC3)) + return 0; + +#ifdef MODE_BFS + sb->block_per_group = 1 << (sb->alloc_group_shift); +#endif + + if (((grub_uint32_t) (1 << sb->block_shift) != sb->block_size) + || (sb->used_blocks > sb->num_blocks ) + || (sb->inode_size != sb->block_size) + || (0 == sb->block_size) +#ifndef MODE_BFS + || ((grub_uint32_t) (1 << sb->alloc_group_shift) != + sb->block_per_group * sb->block_size) + || (sb->alloc_group_count * sb->block_per_group < sb->num_blocks) + || (grub_afs_to_cpu16 (sb->log_block.len) != sb->log_size) + || (grub_afs_to_cpu32 (sb->valid_log_blocks) > sb->log_size) +#endif + ) + return 0; + + return 1; +} + +static struct grub_afs_data * +grub_afs_mount (grub_disk_t disk) +{ + struct grub_afs_data *data = 0; + + data = grub_malloc (sizeof (struct grub_afs_data)); + if (!data) + return 0; + + /* Read the superblock. */ + if (grub_disk_read (disk, GRUB_AFS_SBLOCK_SECTOR, 0, + sizeof (struct grub_afs_sblock), &data->sblock)) + goto fail; + + if (! grub_afs_validate_sblock (&data->sblock)) + goto fail; + + data->diropen.data = data; + data->inode = &data->diropen.inode; + data->disk = disk; + + if (grub_afs_read_inode (data, + grub_afs_run_to_num (&data->sblock, + &data->sblock.root_dir), + data->inode)) + goto fail; + + return data; + +fail: + grub_error (GRUB_ERR_BAD_FS, "not an " GRUB_AFS_FSNAME " filesystem"); + + grub_free (data); + return 0; +} + +static grub_err_t +grub_afs_open (struct grub_file *file, const char *name) +{ + struct grub_afs_data *data; + struct grub_fshelp_node *fdiro = 0; + + grub_dl_ref (my_mod); + + data = grub_afs_mount (file->device->disk); + if (! data) + goto fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_afs_iterate_dir, + grub_afs_read_symlink, GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_afs_inode)); + grub_free (fdiro); + + file->size = grub_afs_to_cpu64 (data->inode->stream.size); + file->data = data; + file->offset = 0; + + return 0; + +fail: + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_ssize_t +grub_afs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_afs_data *data = (struct grub_afs_data *) file->data; + + return grub_afs_read_file (&data->diropen, file->read_hook, + file->offset, len, buf); +} + +static grub_err_t +grub_afs_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_afs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_afs_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + info.mtimeset = 1; +#ifdef MODE_BFS + info.mtime = grub_afs_to_cpu64 (node->inode.modified_time) >> 16; +#else + info.mtime = grub_divmod64 (grub_afs_to_cpu64 (node->inode.modified_time), + 1000000, 0); +#endif + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_afs_mount (device->disk); + if (! data) + goto fail; + + grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_afs_iterate_dir, + grub_afs_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_afs_iterate_dir (fdiro, iterate); + + if (fdiro != &data->diropen) + grub_free (fdiro); + + fail: + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_afs_label (grub_device_t device, char **label) +{ + struct grub_afs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_afs_mount (disk); + if (data) + *label = grub_strndup (data->sblock.name, sizeof (data->sblock.name)); + else + *label = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + +static struct grub_fs grub_afs_fs = { + .name = GRUB_AFS_FSNAME, + .dir = grub_afs_dir, + .open = grub_afs_open, + .read = grub_afs_read, + .close = grub_afs_close, + .label = grub_afs_label, + .next = 0 +}; + +#if defined (MODE_BIGENDIAN) && defined (MODE_BFS) +GRUB_MOD_INIT (befs_be) +#elif defined (MODE_BFS) +GRUB_MOD_INIT (befs) +#elif defined (MODE_BIGENDIAN) +GRUB_MOD_INIT (afs_be) +#else +GRUB_MOD_INIT (afs) +#endif +{ + grub_fs_register (&grub_afs_fs); + my_mod = mod; +} + +#if defined (MODE_BIGENDIAN) && defined (MODE_BFS) +GRUB_MOD_FINI (befs_be) +#elif defined (MODE_BFS) +GRUB_MOD_FINI (befs) +#elif defined (MODE_BIGENDIAN) +GRUB_MOD_FINI (afs_be) +#else +GRUB_MOD_FINI (afs) +#endif +{ + grub_fs_unregister (&grub_afs_fs); +} diff --git a/libr/fs/p/grub/fs/afs_be.c b/libr/fs/p/grub/fs/afs_be.c new file mode 100644 index 0000000000..1f1f48fbb9 --- /dev/null +++ b/libr/fs/p/grub/fs/afs_be.c @@ -0,0 +1,2 @@ +#define MODE_BIGENDIAN 1 +#include "afs.c" diff --git a/libr/fs/p/grub/fs/befs.c b/libr/fs/p/grub/fs/befs.c new file mode 100644 index 0000000000..c54d8e1cc9 --- /dev/null +++ b/libr/fs/p/grub/fs/befs.c @@ -0,0 +1,3 @@ +/* befs.c - The native BeOS/Haiku file-system. */ +#define MODE_BFS 1 +#include "afs.c" diff --git a/libr/fs/p/grub/fs/befs_be.c b/libr/fs/p/grub/fs/befs_be.c new file mode 100644 index 0000000000..f6e8179f44 --- /dev/null +++ b/libr/fs/p/grub/fs/befs_be.c @@ -0,0 +1,4 @@ +/* befs.c - The native BeOS/Haiku file-system. */ +#define MODE_BFS 1 +#define MODE_BIGENDIAN 1 +#include "afs.c" diff --git a/libr/fs/p/grub/fs/btrfs.c b/libr/fs/p/grub/fs/btrfs.c new file mode 100644 index 0000000000..a2ee485b4b --- /dev/null +++ b/libr/fs/p/grub/fs/btrfs.c @@ -0,0 +1,132 @@ +/* btrfs.c - B-tree file system. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define BTRFS_SIGNATURE "_BHRfS_M" + +struct btrfs_superblock +{ + grub_uint8_t dummy1[32]; + grub_uint16_t uuid[8]; + grub_uint8_t dummy2[16]; + grub_uint8_t signature[sizeof (BTRFS_SIGNATURE) - 1]; +} __attribute__ ((packed)); + +struct grub_btrfs_data +{ + struct btrfs_superblock sblock; +}; + +static struct grub_btrfs_data * +grub_btrfs_mount (grub_disk_t disk) +{ + struct grub_btrfs_data *data = grub_malloc (sizeof (*data)); + if (! data) + return NULL; + + if (grub_disk_read (disk, 128, 0, sizeof (data->sblock), + &data->sblock) != GRUB_ERR_NONE) + goto fail; + + if (grub_memcmp ((char *) data->sblock.signature, BTRFS_SIGNATURE, sizeof (BTRFS_SIGNATURE) - 1)) + { + grub_error (GRUB_ERR_BAD_FS, "not a Btrfs filesystem"); + goto fail; + } + + return data; + + fail: + grub_free (data); + return NULL; +} + +static grub_err_t +grub_btrfs_open (struct grub_file *file __attribute__ ((unused)), + const char *name __attribute__ ((unused))) +{ + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "only detection is supported for Btrfs"); +} + +static grub_err_t +grub_btrfs_dir (grub_device_t device, + const char *path __attribute__ ((unused)), + int (*hook) (const char *filename, + const struct grub_dirhook_info *info) + __attribute__ ((unused))) +{ + struct grub_btrfs_data *data = grub_btrfs_mount (device->disk); + if (grub_errno) + return grub_errno; + + grub_free (data); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_btrfs_uuid (grub_device_t device, char **uuid) +{ + struct grub_btrfs_data *data; + + *uuid = NULL; + + data = grub_btrfs_mount (device->disk); + if (! data) + return grub_errno; + + *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x", + grub_be_to_cpu16 (data->sblock.uuid[0]), + grub_be_to_cpu16 (data->sblock.uuid[1]), + grub_be_to_cpu16 (data->sblock.uuid[2]), + grub_be_to_cpu16 (data->sblock.uuid[3]), + grub_be_to_cpu16 (data->sblock.uuid[4]), + grub_be_to_cpu16 (data->sblock.uuid[5]), + grub_be_to_cpu16 (data->sblock.uuid[6]), + grub_be_to_cpu16 (data->sblock.uuid[7])); + + grub_free (data); + + return grub_errno; +} + +static struct grub_fs grub_btrfs_fs = + { + .name = "btrfs", + .dir = grub_btrfs_dir, + .open = grub_btrfs_open, + .uuid = grub_btrfs_uuid, + }; + +GRUB_MOD_INIT(btrfs) +{ + grub_fs_register (&grub_btrfs_fs); +} + +GRUB_MOD_FINI(btrfs) +{ + grub_fs_unregister (&grub_btrfs_fs); +} diff --git a/libr/fs/p/grub/fs/cpio.c b/libr/fs/p/grub/fs/cpio.c new file mode 100644 index 0000000000..2c92404c34 --- /dev/null +++ b/libr/fs/p/grub/fs/cpio.c @@ -0,0 +1,379 @@ +/* cpio.c - cpio and tar filesystem. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#ifndef MODE_USTAR +/* cpio support */ +#define MAGIC_BCPIO 070707 +struct head +{ + grub_uint16_t magic; + grub_uint16_t dev; + grub_uint16_t ino; + grub_uint16_t mode; + grub_uint16_t uid; + grub_uint16_t gid; + grub_uint16_t nlink; + grub_uint16_t rdev; + grub_uint16_t mtime_1; + grub_uint16_t mtime_2; + grub_uint16_t namesize; + grub_uint16_t filesize_1; + grub_uint16_t filesize_2; +} __attribute__ ((packed)); +#else +/* tar support */ +#define MAGIC_USTAR "ustar" +struct head +{ + char name[100]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char typeflag; + char linkname[100]; + char magic[6]; + char version[2]; + char uname[32]; + char gname[32]; + char devmajor[8]; + char devminor[8]; + char prefix[155]; +} __attribute__ ((packed)); +#endif + +struct grub_cpio_data +{ + grub_disk_t disk; + grub_uint32_t hofs; + grub_uint32_t dofs; + grub_uint32_t size; +}; + +static grub_dl_t my_mod; + +static grub_err_t +grub_cpio_find_file (struct grub_cpio_data *data, char **name, + grub_uint32_t * ofs) +{ +#ifndef MODE_USTAR + struct head hd; + + if (grub_disk_read + (data->disk, 0, data->hofs, sizeof (hd), &hd)) + return grub_errno; + + if (hd.magic != MAGIC_BCPIO) + return grub_error (GRUB_ERR_BAD_FS, "invalid cpio archive"); + + data->size = (((grub_uint32_t) hd.filesize_1) << 16) + hd.filesize_2; + + if (hd.namesize & 1) + hd.namesize++; + + if ((*name = grub_malloc (hd.namesize)) == NULL) + return grub_errno; + + if (grub_disk_read (data->disk, 0, data->hofs + sizeof (hd), + hd.namesize, *name)) + { + grub_free (*name); + return grub_errno; + } + + if (data->size == 0 && hd.mode == 0 && hd.namesize == 11 + 1 + && ! grub_memcmp(*name, "TRAILER!!!", 11)) + { + *ofs = 0; + return GRUB_ERR_NONE; + } + + data->dofs = data->hofs + sizeof (hd) + hd.namesize; + *ofs = data->dofs + data->size; + if (data->size & 1) + (*ofs)++; +#else + struct head hd; + + if (grub_disk_read + (data->disk, 0, data->hofs, sizeof (hd), &hd)) + return grub_errno; + + if (!hd.name[0]) + { + *ofs = 0; + return GRUB_ERR_NONE; + } + + if (grub_memcmp (hd.magic, MAGIC_USTAR, sizeof (MAGIC_USTAR) - 1)) + return grub_error (GRUB_ERR_BAD_FS, "invalid tar archive"); + + if ((*name = grub_strdup (hd.name)) == NULL) + return grub_errno; + + data->size = grub_strtoul (hd.size, NULL, 8); + data->dofs = data->hofs + GRUB_DISK_SECTOR_SIZE; + *ofs = data->dofs + ((data->size + GRUB_DISK_SECTOR_SIZE - 1) & + ~(GRUB_DISK_SECTOR_SIZE - 1)); +#endif + return GRUB_ERR_NONE; +} + +static struct grub_cpio_data * +grub_cpio_mount (grub_disk_t disk) +{ + struct head hd; + struct grub_cpio_data *data; + + if (grub_disk_read (disk, 0, 0, sizeof (hd), &hd)) + goto fail; + +#ifndef MODE_USTAR + if (hd.magic != MAGIC_BCPIO) +#else + if (grub_memcmp (hd.magic, MAGIC_USTAR, + sizeof (MAGIC_USTAR) - 1)) +#endif + goto fail; + + data = (struct grub_cpio_data *) grub_malloc (sizeof (*data)); + if (!data) + goto fail; + + data->disk = disk; + + return data; + +fail: + grub_error (GRUB_ERR_BAD_FS, "not a " +#ifdef MODE_USTAR + "tar" +#else + "cpio" +#endif + " filesystem"); + return 0; +} + +static grub_err_t +grub_cpio_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_cpio_data *data; + grub_uint32_t ofs; + char *prev, *name; + const char *np; + int len; + + grub_dl_ref (my_mod); + + prev = 0; + + data = grub_cpio_mount (device->disk); + if (!data) + goto fail; + + np = path + 1; + len = grub_strlen (path) - 1; + + data->hofs = 0; + while (1) + { + if (grub_cpio_find_file (data, &name, &ofs)) + goto fail; + + if (!ofs) + break; + + if (grub_memcmp (np, name, len) == 0) + { + char *p, *n; + + n = name + len; + if (*n == '/') + n++; + + p = grub_strchr (name + len, '/'); + if (p) + *p = 0; + + if ((!prev) || (grub_strcmp (prev, name) != 0)) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = (p != NULL); + + hook (name + len, &info); + if (prev) + grub_free (prev); + prev = name; + } + else + grub_free (name); + } + data->hofs = ofs; + } + +fail: + + if (prev) + grub_free (prev); + + if (data) + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_cpio_open (grub_file_t file, const char *name) +{ + struct grub_cpio_data *data; + grub_uint32_t ofs; + char *fn; + int i, j; + + grub_dl_ref (my_mod); + + data = grub_cpio_mount (file->device->disk); + if (!data) + goto fail; + + data->hofs = 0; + while (1) + { + if (grub_cpio_find_file (data, &fn, &ofs)) + goto fail; + + if (!ofs) + { + grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + break; + } + + /* Compare NAME and FN by hand in order to cope with duplicate + slashes. */ + i = 0; + j = 0; + while (name[i] == '/') + i++; + while (1) + { + if (name[i] != fn[j]) + goto no_match; + + if (name[i] == '\0') + break; + + while (name[i] == '/' && name[i+1] == '/') + i++; + + i++; + j++; + } + + if (name[i] != fn[j]) + goto no_match; + + file->data = data; + file->size = data->size; + grub_free (fn); + + return GRUB_ERR_NONE; + + no_match: + + grub_free (fn); + data->hofs = ofs; + } + +fail: + + if (data) + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_ssize_t +grub_cpio_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_cpio_data *data; + + data = file->data; + return (grub_disk_read (data->disk, 0, data->dofs + file->offset, + len, buf)) ? -1 : (grub_ssize_t) len; +} + +static grub_err_t +grub_cpio_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static struct grub_fs grub_cpio_fs = { +#ifdef MODE_USTAR + .name = "tarfs", +#else + .name = "cpiofs", +#endif + .dir = grub_cpio_dir, + .open = grub_cpio_open, + .read = grub_cpio_read, + .close = grub_cpio_close, +#ifdef GRUB_UTIL + .reserved_first_sector = 0, +#endif +}; + +#ifdef MODE_USTAR +GRUB_MOD_INIT (tar) +#else +GRUB_MOD_INIT (cpio) +#endif +{ + grub_fs_register (&grub_cpio_fs); + my_mod = mod; +} + +#ifdef MODE_USTAR +GRUB_MOD_FINI (tar) +#else +GRUB_MOD_FINI (cpio) +#endif +{ + grub_fs_unregister (&grub_cpio_fs); +} diff --git a/libr/fs/p/grub/fs/ext2.c b/libr/fs/p/grub/fs/ext2.c new file mode 100644 index 0000000000..3d78633535 --- /dev/null +++ b/libr/fs/p/grub/fs/ext2.c @@ -0,0 +1,990 @@ +/* ext2.c - Second Extended filesystem */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/* Magic value used to identify an ext2 filesystem. */ +#define EXT2_MAGIC 0xEF53 +/* Amount of indirect blocks in an inode. */ +#define INDIRECT_BLOCKS 12 +/* Maximum length of a pathname. */ +#define EXT2_PATH_MAX 4096 +/* Maximum nesting of symlinks, used to prevent a loop. */ +#define EXT2_MAX_SYMLINKCNT 8 + +/* The good old revision and the default inode size. */ +#define EXT2_GOOD_OLD_REVISION 0 +#define EXT2_GOOD_OLD_INODE_SIZE 128 + +/* Filetype used in directory entry. */ +#define FILETYPE_UNKNOWN 0 +#define FILETYPE_REG 1 +#define FILETYPE_DIRECTORY 2 +#define FILETYPE_SYMLINK 7 + +/* Filetype information as used in inodes. */ +#define FILETYPE_INO_MASK 0170000 +#define FILETYPE_INO_REG 0100000 +#define FILETYPE_INO_DIRECTORY 0040000 +#define FILETYPE_INO_SYMLINK 0120000 + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Log2 size of ext2 block in 512 blocks. */ +#define LOG2_EXT2_BLOCK_SIZE(data) \ + (grub_le_to_cpu32 (data->sblock.log2_block_size) + 1) + +/* Log2 size of ext2 block in bytes. */ +#define LOG2_BLOCK_SIZE(data) \ + (grub_le_to_cpu32 (data->sblock.log2_block_size) + 10) + +/* The size of an ext2 block in bytes. */ +#define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE (data)) + +/* The revision level. */ +#define EXT2_REVISION(data) grub_le_to_cpu32 (data->sblock.revision_level) + +/* The inode size. */ +#define EXT2_INODE_SIZE(data) \ + (EXT2_REVISION (data) == EXT2_GOOD_OLD_REVISION \ + ? EXT2_GOOD_OLD_INODE_SIZE \ + : grub_le_to_cpu16 (data->sblock.inode_size)) + +/* Superblock filesystem feature flags (RW compatible) + * A filesystem with any of these enabled can be read and written by a driver + * that does not understand them without causing metadata/data corruption. */ +#define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001 +#define EXT2_FEATURE_COMPAT_IMAGIC_INODES 0x0002 +#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004 +#define EXT2_FEATURE_COMPAT_EXT_ATTR 0x0008 +#define EXT2_FEATURE_COMPAT_RESIZE_INODE 0x0010 +#define EXT2_FEATURE_COMPAT_DIR_INDEX 0x0020 +/* Superblock filesystem feature flags (RO compatible) + * A filesystem with any of these enabled can be safely read by a driver that + * does not understand them, but should not be written to, usually because + * additional metadata is required. */ +#define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001 +#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE 0x0002 +#define EXT2_FEATURE_RO_COMPAT_BTREE_DIR 0x0004 +#define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010 +#define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020 +#define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040 +/* Superblock filesystem feature flags (back-incompatible) + * A filesystem with any of these enabled should not be attempted to be read + * by a driver that does not understand them, since they usually indicate + * metadata format changes that might confuse the reader. */ +#define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001 +#define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002 +#define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */ +#define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Volume is journal device */ +#define EXT2_FEATURE_INCOMPAT_META_BG 0x0010 +#define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* Extents used */ +#define EXT4_FEATURE_INCOMPAT_64BIT 0x0080 +#define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200 + +/* The set of back-incompatible features this driver DOES support. Add (OR) + * flags here as the related features are implemented into the driver. */ +#define EXT2_DRIVER_SUPPORTED_INCOMPAT ( EXT2_FEATURE_INCOMPAT_FILETYPE \ + | EXT4_FEATURE_INCOMPAT_EXTENTS \ + | EXT4_FEATURE_INCOMPAT_FLEX_BG ) +/* List of rationales for the ignored "incompatible" features: + * needs_recovery: Not really back-incompatible - was added as such to forbid + * ext2 drivers from mounting an ext3 volume with a dirty + * journal because they will ignore the journal, but the next + * ext3 driver to mount the volume will find the journal and + * replay it, potentially corrupting the metadata written by + * the ext2 drivers. Safe to ignore for this RO driver. */ +#define EXT2_DRIVER_IGNORED_INCOMPAT ( EXT3_FEATURE_INCOMPAT_RECOVER ) + + +#define EXT3_JOURNAL_MAGIC_NUMBER 0xc03b3998U + +#define EXT3_JOURNAL_DESCRIPTOR_BLOCK 1 +#define EXT3_JOURNAL_COMMIT_BLOCK 2 +#define EXT3_JOURNAL_SUPERBLOCK_V1 3 +#define EXT3_JOURNAL_SUPERBLOCK_V2 4 +#define EXT3_JOURNAL_REVOKE_BLOCK 5 + +#define EXT3_JOURNAL_FLAG_ESCAPE 1 +#define EXT3_JOURNAL_FLAG_SAME_UUID 2 +#define EXT3_JOURNAL_FLAG_DELETED 4 +#define EXT3_JOURNAL_FLAG_LAST_TAG 8 + +#define EXT4_EXTENTS_FLAG 0x80000 + +/* The ext2 superblock. */ +struct grub_ext2_sblock +{ + grub_uint32_t total_inodes; + grub_uint32_t total_blocks; + grub_uint32_t reserved_blocks; + grub_uint32_t free_blocks; + grub_uint32_t free_inodes; + grub_uint32_t first_data_block; + grub_uint32_t log2_block_size; + grub_uint32_t log2_fragment_size; + grub_uint32_t blocks_per_group; + grub_uint32_t fragments_per_group; + grub_uint32_t inodes_per_group; + grub_uint32_t mtime; + grub_uint32_t utime; + grub_uint16_t mnt_count; + grub_uint16_t max_mnt_count; + grub_uint16_t magic; + grub_uint16_t fs_state; + grub_uint16_t error_handling; + grub_uint16_t minor_revision_level; + grub_uint32_t lastcheck; + grub_uint32_t checkinterval; + grub_uint32_t creator_os; + grub_uint32_t revision_level; + grub_uint16_t uid_reserved; + grub_uint16_t gid_reserved; + grub_uint32_t first_inode; + grub_uint16_t inode_size; + grub_uint16_t block_group_number; + grub_uint32_t feature_compatibility; + grub_uint32_t feature_incompat; + grub_uint32_t feature_ro_compat; + grub_uint16_t uuid[8]; + char volume_name[16]; + char last_mounted_on[64]; + grub_uint32_t compression_info; + grub_uint8_t prealloc_blocks; + grub_uint8_t prealloc_dir_blocks; + grub_uint16_t reserved_gdt_blocks; + grub_uint8_t journal_uuid[16]; + grub_uint32_t journal_inum; + grub_uint32_t journal_dev; + grub_uint32_t last_orphan; + grub_uint32_t hash_seed[4]; + grub_uint8_t def_hash_version; + grub_uint8_t jnl_backup_type; + grub_uint16_t reserved_word_pad; + grub_uint32_t default_mount_opts; + grub_uint32_t first_meta_bg; + grub_uint32_t mkfs_time; + grub_uint32_t jnl_blocks[17]; +}; + +/* The ext2 blockgroup. */ +struct grub_ext2_block_group +{ + grub_uint32_t block_id; + grub_uint32_t inode_id; + grub_uint32_t inode_table_id; + grub_uint16_t free_blocks; + grub_uint16_t free_inodes; + grub_uint16_t used_dirs; + grub_uint16_t pad; + grub_uint32_t reserved[3]; +}; + +/* The ext2 inode. */ +struct grub_ext2_inode +{ + grub_uint16_t mode; + grub_uint16_t uid; + grub_uint32_t size; + grub_uint32_t atime; + grub_uint32_t ctime; + grub_uint32_t mtime; + grub_uint32_t dtime; + grub_uint16_t gid; + grub_uint16_t nlinks; + grub_uint32_t blockcnt; /* Blocks of 512 bytes!! */ + grub_uint32_t flags; + grub_uint32_t osd1; + union + { + struct datablocks + { + grub_uint32_t dir_blocks[INDIRECT_BLOCKS]; + grub_uint32_t indir_block; + grub_uint32_t double_indir_block; + grub_uint32_t triple_indir_block; + } blocks; + char symlink[60]; + }; + grub_uint32_t version; + grub_uint32_t acl; + grub_uint32_t size_high; + grub_uint32_t fragment_addr; + grub_uint32_t osd2[3]; +}; + +/* The header of an ext2 directory entry. */ +struct ext2_dirent +{ + grub_uint32_t inode; + grub_uint16_t direntlen; + grub_uint8_t namelen; + grub_uint8_t filetype; +}; + +struct grub_ext3_journal_header +{ + grub_uint32_t magic; + grub_uint32_t block_type; + grub_uint32_t sequence; +}; + +struct grub_ext3_journal_revoke_header +{ + struct grub_ext3_journal_header header; + grub_uint32_t count; + grub_uint32_t data[0]; +}; + +struct grub_ext3_journal_block_tag +{ + grub_uint32_t block; + grub_uint32_t flags; +}; + +struct grub_ext3_journal_sblock +{ + struct grub_ext3_journal_header header; + grub_uint32_t block_size; + grub_uint32_t maxlen; + grub_uint32_t first; + grub_uint32_t sequence; + grub_uint32_t start; +}; + +#define EXT4_EXT_MAGIC 0xf30a + +struct grub_ext4_extent_header +{ + grub_uint16_t magic; + grub_uint16_t entries; + grub_uint16_t max; + grub_uint16_t depth; + grub_uint32_t generation; +}; + +struct grub_ext4_extent +{ + grub_uint32_t block; + grub_uint16_t len; + grub_uint16_t start_hi; + grub_uint32_t start; +}; + +struct grub_ext4_extent_idx +{ + grub_uint32_t block; + grub_uint32_t leaf; + grub_uint16_t leaf_hi; + grub_uint16_t unused; +}; + +struct grub_fshelp_node +{ + struct grub_ext2_data *data; + struct grub_ext2_inode inode; + int ino; + int inode_read; +}; + +/* Information about a "mounted" ext2 filesystem. */ +struct grub_ext2_data +{ + struct grub_ext2_sblock sblock; + grub_disk_t disk; + struct grub_ext2_inode *inode; + struct grub_fshelp_node diropen; +}; + +static grub_dl_t my_mod; + + + +/* Read into BLKGRP the blockgroup descriptor of blockgroup GROUP of + the mounted filesystem DATA. */ +inline static grub_err_t +grub_ext2_blockgroup (struct grub_ext2_data *data, int group, + struct grub_ext2_block_group *blkgrp) +{ + return grub_disk_read (data->disk, + ((grub_le_to_cpu32 (data->sblock.first_data_block) + 1) + << LOG2_EXT2_BLOCK_SIZE (data)), + group * sizeof (struct grub_ext2_block_group), + sizeof (struct grub_ext2_block_group), blkgrp); +} + +static struct grub_ext4_extent_header * +grub_ext4_find_leaf (struct grub_ext2_data *data, char *buf, + struct grub_ext4_extent_header *ext_block, + grub_uint32_t fileblock) +{ + struct grub_ext4_extent_idx *index; + + while (1) + { + int i; + grub_disk_addr_t block; + + index = (struct grub_ext4_extent_idx *) (ext_block + 1); + + if (grub_le_to_cpu16(ext_block->magic) != EXT4_EXT_MAGIC) + return 0; + + if (ext_block->depth == 0) + return ext_block; + + for (i = 0; i < grub_le_to_cpu16 (ext_block->entries); i++) + { + if (fileblock < grub_le_to_cpu32(index[i].block)) + break; + } + + if (--i < 0) + return 0; + + block = grub_le_to_cpu16 (index[i].leaf_hi); + block = (block << 32) + grub_le_to_cpu32 (index[i].leaf); + if (grub_disk_read (data->disk, + block << LOG2_EXT2_BLOCK_SIZE (data), + 0, EXT2_BLOCK_SIZE(data), buf)) + return 0; + + ext_block = (struct grub_ext4_extent_header *) buf; + } +} + +static grub_disk_addr_t +grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + struct grub_ext2_data *data = node->data; + struct grub_ext2_inode *inode = &node->inode; + int blknr = -1; + unsigned int blksz = EXT2_BLOCK_SIZE (data); + int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data); + + if (grub_le_to_cpu32(inode->flags) & EXT4_EXTENTS_FLAG) + { + char buf[EXT2_BLOCK_SIZE(data)]; + struct grub_ext4_extent_header *leaf; + struct grub_ext4_extent *ext; + int i; + + leaf = grub_ext4_find_leaf (data, buf, + (struct grub_ext4_extent_header *) inode->blocks.dir_blocks, + fileblock); + if (! leaf) + { + grub_error (GRUB_ERR_BAD_FS, "invalid extent"); + return -1; + } + + ext = (struct grub_ext4_extent *) (leaf + 1); + for (i = 0; i < grub_le_to_cpu16 (leaf->entries); i++) + { + if (fileblock < grub_le_to_cpu32 (ext[i].block)) + break; + } + + if (--i >= 0) + { + fileblock -= grub_le_to_cpu32 (ext[i].block); + if (fileblock >= grub_le_to_cpu16 (ext[i].len)) + return 0; + else + { + grub_disk_addr_t start; + + start = grub_le_to_cpu16 (ext[i].start_hi); + start = (start << 32) + grub_le_to_cpu32 (ext[i].start); + + return fileblock + start; + } + } + else + { + grub_error (GRUB_ERR_BAD_FS, "something wrong with extent"); + return -1; + } + } + /* Direct blocks. */ + if (fileblock < INDIRECT_BLOCKS) + blknr = grub_le_to_cpu32 (inode->blocks.dir_blocks[fileblock]); + /* Indirect. */ + else if (fileblock < INDIRECT_BLOCKS + blksz / 4) + { + grub_uint32_t indir[blksz / 4]; + + if (grub_disk_read (data->disk, + ((grub_disk_addr_t) + grub_le_to_cpu32 (inode->blocks.indir_block)) + << log2_blksz, + 0, blksz, indir)) + return grub_errno; + + blknr = grub_le_to_cpu32 (indir[fileblock - INDIRECT_BLOCKS]); + } + /* Double indirect. */ + else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * (blksz / 4 + 1)) + { + unsigned int perblock = blksz / 4; + unsigned int rblock = fileblock - (INDIRECT_BLOCKS + + blksz / 4); + grub_uint32_t indir[blksz / 4]; + + if (grub_disk_read (data->disk, + ((grub_disk_addr_t) + grub_le_to_cpu32 (inode->blocks.double_indir_block)) + << log2_blksz, + 0, blksz, indir)) + return grub_errno; + + if (grub_disk_read (data->disk, + ((grub_disk_addr_t) + grub_le_to_cpu32 (indir[rblock / perblock])) + << log2_blksz, + 0, blksz, indir)) + return grub_errno; + + + blknr = grub_le_to_cpu32 (indir[rblock % perblock]); + } + /* triple indirect. */ + else if (fileblock < INDIRECT_BLOCKS + blksz / 4 * (blksz / 4 + 1) + + (blksz / 4) * (blksz / 4) * (blksz / 4 + 1)) + { + unsigned int perblock = blksz / 4; + unsigned int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 + * (blksz / 4 + 1)); + grub_uint32_t indir[blksz / 4]; + + if (grub_disk_read (data->disk, + ((grub_disk_addr_t) + grub_le_to_cpu32 (inode->blocks.triple_indir_block)) + << log2_blksz, + 0, blksz, indir)) + return grub_errno; + + if (grub_disk_read (data->disk, + ((grub_disk_addr_t) + grub_le_to_cpu32 (indir[(rblock / perblock) / perblock])) + << log2_blksz, + 0, blksz, indir)) + return grub_errno; + + if (grub_disk_read (data->disk, + ((grub_disk_addr_t) + grub_le_to_cpu32 (indir[(rblock / perblock) % perblock])) + << log2_blksz, + 0, blksz, indir)) + return grub_errno; + + blknr = grub_le_to_cpu32 (indir[rblock % perblock]); + } + else + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "ext2fs doesn't support quadruple indirect blocks"); + } + + return blknr; +} + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_ext2_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + grub_off_t pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_ext2_read_block, + grub_cpu_to_le32 (node->inode.size) + | (((grub_off_t) grub_cpu_to_le32 (node->inode.size_high)) << 32), + LOG2_EXT2_BLOCK_SIZE (node->data)); + +} + + +/* Read the inode INO for the file described by DATA into INODE. */ +static grub_err_t +grub_ext2_read_inode (struct grub_ext2_data *data, + int ino, struct grub_ext2_inode *inode) +{ + struct grub_ext2_block_group blkgrp; + struct grub_ext2_sblock *sblock = &data->sblock; + int inodes_per_block; + unsigned int blkno; + unsigned int blkoff; + + /* It is easier to calculate if the first inode is 0. */ + ino--; + + grub_ext2_blockgroup (data, + ino / grub_le_to_cpu32 (sblock->inodes_per_group), + &blkgrp); + if (grub_errno) + return grub_errno; + + inodes_per_block = EXT2_BLOCK_SIZE (data) / EXT2_INODE_SIZE (data); + blkno = (ino % grub_le_to_cpu32 (sblock->inodes_per_group)) + / inodes_per_block; + blkoff = (ino % grub_le_to_cpu32 (sblock->inodes_per_group)) + % inodes_per_block; + + /* Read the inode. */ + if (grub_disk_read (data->disk, + ((grub_le_to_cpu32 (blkgrp.inode_table_id) + blkno) + << LOG2_EXT2_BLOCK_SIZE (data)), + EXT2_INODE_SIZE (data) * blkoff, + sizeof (struct grub_ext2_inode), inode)) + return grub_errno; + + return 0; +} + +static struct grub_ext2_data * +grub_ext2_mount (grub_disk_t disk) +{ + struct grub_ext2_data *data; + + data = grub_malloc (sizeof (struct grub_ext2_data)); + if (!data) + return 0; + + /* Read the superblock. */ + grub_disk_read (disk, 1 * 2, 0, sizeof (struct grub_ext2_sblock), + &data->sblock); + if (grub_errno) + goto fail; + +//printf ("EXT2MAGIC %x %x\n", grub_le_to_cpu16 (data->sblock.magic), EXT2_MAGIC); + /* Make sure this is an ext2 filesystem. */ + if (grub_le_to_cpu16 (data->sblock.magic) != EXT2_MAGIC) + { + grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem"); + goto fail; + } + + /* Check the FS doesn't have feature bits enabled that we don't support. */ + if (grub_le_to_cpu32 (data->sblock.feature_incompat) + & ~(EXT2_DRIVER_SUPPORTED_INCOMPAT | EXT2_DRIVER_IGNORED_INCOMPAT)) + { + grub_error (GRUB_ERR_BAD_FS, "filesystem has unsupported incompatible features"); + goto fail; + } + + + data->disk = disk; + + data->diropen.data = data; + data->diropen.ino = 2; + data->diropen.inode_read = 1; + + data->inode = &data->diropen.inode; + + grub_ext2_read_inode (data, 2, data->inode); + if (grub_errno) + goto fail; + + return data; + + fail: + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not an ext2 filesystem"); + + grub_free (data); + return 0; +} + +static char * +grub_ext2_read_symlink (grub_fshelp_node_t node) +{ + char *symlink; + struct grub_fshelp_node *diro = node; + + if (! diro->inode_read) + { + grub_ext2_read_inode (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + symlink = grub_malloc (grub_le_to_cpu32 (diro->inode.size) + 1); + if (! symlink) + return 0; + + /* If the filesize of the symlink is bigger than + 60 the symlink is stored in a separate block, + otherwise it is stored in the inode. */ + if (grub_le_to_cpu32 (diro->inode.size) <= 60) + grub_strncpy (symlink, + diro->inode.symlink, + grub_le_to_cpu32 (diro->inode.size)); + else + { + grub_ext2_read_file (diro, 0, 0, + grub_le_to_cpu32 (diro->inode.size), + symlink); + if (grub_errno) + { + grub_free (symlink); + return 0; + } + } + + symlink[grub_le_to_cpu32 (diro->inode.size)] = '\0'; + return symlink; +} + +static int +grub_ext2_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + unsigned int fpos = 0; + struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir; + + if (! diro->inode_read) + { + grub_ext2_read_inode (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + /* Search the file. */ + while (fpos < grub_le_to_cpu32 (diro->inode.size)) + { + struct ext2_dirent dirent; + + grub_ext2_read_file (diro, 0, fpos, sizeof (struct ext2_dirent), + (char *) &dirent); + if (grub_errno) + return 0; + + if (dirent.direntlen == 0) + return 0; + + if (dirent.namelen != 0) + { + char filename[dirent.namelen + 1]; + struct grub_fshelp_node *fdiro; + enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN; + + grub_ext2_read_file (diro, 0, fpos + sizeof (struct ext2_dirent), + dirent.namelen, filename); + if (grub_errno) + return 0; + + fdiro = grub_malloc (sizeof (struct grub_fshelp_node)); + if (! fdiro) + return 0; + + fdiro->data = diro->data; + fdiro->ino = grub_le_to_cpu32 (dirent.inode); + + filename[dirent.namelen] = '\0'; + + if (dirent.filetype != FILETYPE_UNKNOWN) + { + fdiro->inode_read = 0; + + if (dirent.filetype == FILETYPE_DIRECTORY) + type = GRUB_FSHELP_DIR; + else if (dirent.filetype == FILETYPE_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else if (dirent.filetype == FILETYPE_REG) + type = GRUB_FSHELP_REG; + } + else + { + /* The filetype can not be read from the dirent, read + the inode to get more information. */ + grub_ext2_read_inode (diro->data, + grub_le_to_cpu32 (dirent.inode), + &fdiro->inode); + if (grub_errno) + { + grub_free (fdiro); + return 0; + } + + fdiro->inode_read = 1; + + if ((grub_le_to_cpu16 (fdiro->inode.mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY) + type = GRUB_FSHELP_DIR; + else if ((grub_le_to_cpu16 (fdiro->inode.mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else if ((grub_le_to_cpu16 (fdiro->inode.mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_REG) + type = GRUB_FSHELP_REG; + } + + if (hook (filename, type, fdiro)) + return 1; + } + + fpos += grub_le_to_cpu16 (dirent.direntlen); + } + + return 0; +} + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_ext2_open (struct grub_file *file, const char *name) +{ + struct grub_ext2_data *data; + struct grub_fshelp_node *fdiro = 0; + grub_err_t err; + + grub_dl_ref (my_mod); + + data = grub_ext2_mount (file->device->disk); + if (! data) + { + err = grub_errno; + goto fail; + } + + err = grub_fshelp_find_file (name, &data->diropen, &fdiro, + grub_ext2_iterate_dir, + grub_ext2_read_symlink, GRUB_FSHELP_REG); + if (err) + goto fail; + + if (! fdiro->inode_read) + { + err = grub_ext2_read_inode (data, fdiro->ino, &fdiro->inode); + if (err) + goto fail; + } + + grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_ext2_inode)); + grub_free (fdiro); + + file->size = grub_le_to_cpu32 (data->inode->size); + file->size |= ((grub_off_t) grub_le_to_cpu32 (data->inode->size_high)) << 32; + file->data = data; + file->offset = 0; + + return 0; + + fail: + if (fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return err; +} + +static grub_err_t +grub_ext2_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +/* Read LEN bytes data from FILE into BUF. */ +static grub_ssize_t +grub_ext2_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_ext2_data *data = (struct grub_ext2_data *) file->data; + + return grub_ext2_read_file (&data->diropen, file->read_hook, + file->offset, len, buf); +} + + +static grub_err_t +grub_ext2_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_ext2_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + if (! node->inode_read) + { + grub_ext2_read_inode (data, node->ino, &node->inode); + if (!grub_errno) + node->inode_read = 1; + grub_errno = GRUB_ERR_NONE; + } + if (node->inode_read) + { + info.mtimeset = 1; + info.mtime = grub_le_to_cpu32 (node->inode.mtime); + } + + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_ext2_mount (device->disk); + if (! data) + goto fail; + + grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_ext2_iterate_dir, + grub_ext2_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_ext2_iterate_dir (fdiro, iterate); + + fail: + if (fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_ext2_label (grub_device_t device, char **label) +{ + struct grub_ext2_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_ext2_mount (disk); + if (data) + *label = grub_strndup (data->sblock.volume_name, 14); + else + *label = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static grub_err_t +grub_ext2_uuid (grub_device_t device, char **uuid) +{ + struct grub_ext2_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_ext2_mount (disk); + if (data) + { + *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x", + grub_be_to_cpu16 (data->sblock.uuid[0]), + grub_be_to_cpu16 (data->sblock.uuid[1]), + grub_be_to_cpu16 (data->sblock.uuid[2]), + grub_be_to_cpu16 (data->sblock.uuid[3]), + grub_be_to_cpu16 (data->sblock.uuid[4]), + grub_be_to_cpu16 (data->sblock.uuid[5]), + grub_be_to_cpu16 (data->sblock.uuid[6]), + grub_be_to_cpu16 (data->sblock.uuid[7])); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +/* Get mtime. */ +static grub_err_t +grub_ext2_mtime (grub_device_t device, grub_int32_t *tm) +{ + struct grub_ext2_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_ext2_mount (disk); + if (!data) + *tm = 0; + else + *tm = grub_le_to_cpu32 (data->sblock.utime); + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; + +} + + + +struct grub_fs grub_ext2_fs = + { + .name = "ext2", + .dir = grub_ext2_dir, + .open = grub_ext2_open, + .read = grub_ext2_read, + .close = grub_ext2_close, + .label = grub_ext2_label, + .uuid = grub_ext2_uuid, + .mtime = grub_ext2_mtime, +#ifdef GRUB_UTIL + .reserved_first_sector = 1, +#endif + .next = 0 + }; + +GRUB_MOD_INIT(ext2) +{ + grub_fs_register (&grub_ext2_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(ext2) +{ + grub_fs_unregister (&grub_ext2_fs); +} diff --git a/libr/fs/p/grub/fs/fat.c b/libr/fs/p/grub/fs/fat.c new file mode 100644 index 0000000000..89050943cc --- /dev/null +++ b/libr/fs/p/grub/fs/fat.c @@ -0,0 +1,876 @@ +/* fat.c - FAT filesystem */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_FAT_DIR_ENTRY_SIZE 32 + +#define GRUB_FAT_ATTR_READ_ONLY 0x01 +#define GRUB_FAT_ATTR_HIDDEN 0x02 +#define GRUB_FAT_ATTR_SYSTEM 0x04 +#define GRUB_FAT_ATTR_VOLUME_ID 0x08 +#define GRUB_FAT_ATTR_DIRECTORY 0x10 +#define GRUB_FAT_ATTR_ARCHIVE 0x20 + +#define GRUB_FAT_MAXFILE 256 + +#define GRUB_FAT_ATTR_LONG_NAME (GRUB_FAT_ATTR_READ_ONLY \ + | GRUB_FAT_ATTR_HIDDEN \ + | GRUB_FAT_ATTR_SYSTEM \ + | GRUB_FAT_ATTR_VOLUME_ID) +#define GRUB_FAT_ATTR_VALID (GRUB_FAT_ATTR_READ_ONLY \ + | GRUB_FAT_ATTR_HIDDEN \ + | GRUB_FAT_ATTR_SYSTEM \ + | GRUB_FAT_ATTR_DIRECTORY \ + | GRUB_FAT_ATTR_ARCHIVE \ + | GRUB_FAT_ATTR_VOLUME_ID) + +struct grub_fat_bpb +{ + grub_uint8_t jmp_boot[3]; + grub_uint8_t oem_name[8]; + grub_uint16_t bytes_per_sector; + grub_uint8_t sectors_per_cluster; + grub_uint16_t num_reserved_sectors; + grub_uint8_t num_fats; + grub_uint16_t num_root_entries; + grub_uint16_t num_total_sectors_16; + grub_uint8_t media; + grub_uint16_t sectors_per_fat_16; + grub_uint16_t sectors_per_track; + grub_uint16_t num_heads; + grub_uint32_t num_hidden_sectors; + grub_uint32_t num_total_sectors_32; + union + { + struct + { + grub_uint8_t num_ph_drive; + grub_uint8_t reserved; + grub_uint8_t boot_sig; + grub_uint32_t num_serial; + grub_uint8_t label[11]; + grub_uint8_t fstype[8]; + } __attribute__ ((packed)) fat12_or_fat16; + struct + { + grub_uint32_t sectors_per_fat_32; + grub_uint16_t extended_flags; + grub_uint16_t fs_version; + grub_uint32_t root_cluster; + grub_uint16_t fs_info; + grub_uint16_t backup_boot_sector; + grub_uint8_t reserved[12]; + grub_uint8_t num_ph_drive; + grub_uint8_t reserved1; + grub_uint8_t boot_sig; + grub_uint32_t num_serial; + grub_uint8_t label[11]; + grub_uint8_t fstype[8]; + } __attribute__ ((packed)) fat32; + } __attribute__ ((packed)) version_specific; +} __attribute__ ((packed)); + +struct grub_fat_dir_entry +{ + grub_uint8_t name[11]; + grub_uint8_t attr; + grub_uint8_t nt_reserved; + grub_uint8_t c_time_tenth; + grub_uint16_t c_time; + grub_uint16_t c_date; + grub_uint16_t a_date; + grub_uint16_t first_cluster_high; + grub_uint16_t w_time; + grub_uint16_t w_date; + grub_uint16_t first_cluster_low; + grub_uint32_t file_size; +} __attribute__ ((packed)); + +struct grub_fat_long_name_entry +{ + grub_uint8_t id; + grub_uint16_t name1[5]; + grub_uint8_t attr; + grub_uint8_t reserved; + grub_uint8_t checksum; + grub_uint16_t name2[6]; + grub_uint16_t first_cluster; + grub_uint16_t name3[2]; +} __attribute__ ((packed)); + +struct grub_fat_data +{ + int logical_sector_bits; + grub_uint32_t num_sectors; + + grub_uint16_t fat_sector; + grub_uint32_t sectors_per_fat; + int fat_size; + + grub_uint32_t root_cluster; + grub_uint32_t root_sector; + grub_uint32_t num_root_sectors; + + int cluster_bits; + grub_uint32_t cluster_eof_mark; + grub_uint32_t cluster_sector; + grub_uint32_t num_clusters; + + grub_uint8_t attr; + grub_ssize_t file_size; + grub_uint32_t file_cluster; + grub_uint32_t cur_cluster_num; + grub_uint32_t cur_cluster; + + grub_uint32_t uuid; +}; + +static grub_dl_t my_mod; + +static int +fat_log2 (unsigned x) +{ + int i; + + if (x == 0) + return -1; + + for (i = 0; (x & 1) == 0; i++) + x >>= 1; + + if (x != 1) + return -1; + + return i; +} + +static struct grub_fat_data * +grub_fat_mount (grub_disk_t disk) +{ + struct grub_fat_bpb bpb; + struct grub_fat_data *data = 0; + grub_uint32_t first_fat, magic; + + if (! disk) + goto fail; + + data = (struct grub_fat_data *) grub_malloc (sizeof (*data)); + if (! data) + goto fail; + + /* Read the BPB. */ + if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb)) + goto fail; + + if (grub_strncmp((const char *) bpb.version_specific.fat12_or_fat16.fstype, "FAT12", 5) + && grub_strncmp((const char *) bpb.version_specific.fat12_or_fat16.fstype, "FAT16", 5) + && grub_strncmp((const char *) bpb.version_specific.fat32.fstype, "FAT32", 5)) + goto fail; + + /* Get the sizes of logical sectors and clusters. */ + data->logical_sector_bits = + fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector)); + if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS) + goto fail; + data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS; + + data->cluster_bits = fat_log2 (bpb.sectors_per_cluster); + if (data->cluster_bits < 0) + goto fail; + data->cluster_bits += data->logical_sector_bits; + + /* Get information about FATs. */ + data->fat_sector = (grub_le_to_cpu16 (bpb.num_reserved_sectors) + << data->logical_sector_bits); + if (data->fat_sector == 0) + goto fail; + + data->sectors_per_fat = ((bpb.sectors_per_fat_16 + ? grub_le_to_cpu16 (bpb.sectors_per_fat_16) + : grub_le_to_cpu32 (bpb.version_specific.fat32.sectors_per_fat_32)) + << data->logical_sector_bits); + if (data->sectors_per_fat == 0) + goto fail; + + /* Get the number of sectors in this volume. */ + data->num_sectors = ((bpb.num_total_sectors_16 + ? grub_le_to_cpu16 (bpb.num_total_sectors_16) + : grub_le_to_cpu32 (bpb.num_total_sectors_32)) + << data->logical_sector_bits); + if (data->num_sectors == 0) + goto fail; + + /* Get information about the root directory. */ + if (bpb.num_fats == 0) + goto fail; + + data->root_sector = data->fat_sector + bpb.num_fats * data->sectors_per_fat; + data->num_root_sectors + = ((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries) + * GRUB_FAT_DIR_ENTRY_SIZE + + grub_le_to_cpu16 (bpb.bytes_per_sector) - 1) + >> (data->logical_sector_bits + GRUB_DISK_SECTOR_BITS)) + << (data->logical_sector_bits)); + + data->cluster_sector = data->root_sector + data->num_root_sectors; + data->num_clusters = (((data->num_sectors - data->cluster_sector) + >> (data->cluster_bits + data->logical_sector_bits)) + + 2); + + if (data->num_clusters <= 2) + goto fail; + + if (! bpb.sectors_per_fat_16) + { + /* FAT32. */ + grub_uint16_t flags = grub_le_to_cpu16 (bpb.version_specific.fat32.extended_flags); + + data->root_cluster = grub_le_to_cpu32 (bpb.version_specific.fat32.root_cluster); + data->fat_size = 32; + data->cluster_eof_mark = 0x0ffffff8; + + if (flags & 0x80) + { + /* Get an active FAT. */ + unsigned active_fat = flags & 0xf; + + if (active_fat > bpb.num_fats) + goto fail; + + data->fat_sector += active_fat * data->sectors_per_fat; + } + + if (bpb.num_root_entries != 0 || bpb.version_specific.fat32.fs_version != 0) + goto fail; + } + else + { + /* FAT12 or FAT16. */ + data->root_cluster = ~0U; + + if (data->num_clusters <= 4085 + 2) + { + /* FAT12. */ + data->fat_size = 12; + data->cluster_eof_mark = 0x0ff8; + } + else + { + /* FAT16. */ + data->fat_size = 16; + data->cluster_eof_mark = 0xfff8; + } + } + + /* More sanity checks. */ + if (data->num_sectors <= data->fat_sector) + goto fail; + + if (grub_disk_read (disk, + data->fat_sector, + 0, + sizeof (first_fat), + &first_fat)) + goto fail; + + first_fat = grub_le_to_cpu32 (first_fat); + + if (data->fat_size == 32) + { + first_fat &= 0x0fffffff; + magic = 0x0fffff00; + } + else if (data->fat_size == 16) + { + first_fat &= 0x0000ffff; + magic = 0xff00; + } + else + { + first_fat &= 0x00000fff; + magic = 0x0f00; + } + + /* Serial number. */ + if (bpb.sectors_per_fat_16) + data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat12_or_fat16.num_serial); + else + data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat32.num_serial); + + /* Ignore the 3rd bit, because some BIOSes assigns 0xF0 to the media + descriptor, even if it is a so-called superfloppy (e.g. an USB key). + The check may be too strict for this kind of stupid BIOSes, as + they overwrite the media descriptor. */ + if ((first_fat | 0x8) != (magic | bpb.media | 0x8)) + goto fail; + + /* Start from the root directory. */ + data->file_cluster = data->root_cluster; + data->cur_cluster_num = ~0U; + data->attr = GRUB_FAT_ATTR_DIRECTORY; + return data; + + fail: + + grub_free (data); + grub_error (GRUB_ERR_BAD_FS, "not a FAT filesystem"); + return 0; +} + +static grub_ssize_t +grub_fat_read_data (grub_disk_t disk, struct grub_fat_data *data, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + grub_off_t offset, grub_size_t len, char *buf) +{ + grub_size_t size; + grub_uint32_t logical_cluster; + unsigned logical_cluster_bits; + grub_ssize_t ret = 0; + unsigned long sector; + + /* This is a special case. FAT12 and FAT16 doesn't have the root directory + in clusters. */ + if (data->file_cluster == ~0U) + { + size = (data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset; + if (size > len) + size = len; + + if (grub_disk_read (disk, data->root_sector, offset, size, buf)) + return -1; + + return size; + } + + /* Calculate the logical cluster number and offset. */ + logical_cluster_bits = (data->cluster_bits + + data->logical_sector_bits + + GRUB_DISK_SECTOR_BITS); + logical_cluster = offset >> logical_cluster_bits; + offset &= (1 << logical_cluster_bits) - 1; + + if (logical_cluster < data->cur_cluster_num) + { + data->cur_cluster_num = 0; + data->cur_cluster = data->file_cluster; + } + + while (len) + { + while (logical_cluster > data->cur_cluster_num) + { + /* Find next cluster. */ + grub_uint32_t next_cluster; + unsigned long fat_offset; + + switch (data->fat_size) + { + case 32: + fat_offset = data->cur_cluster << 2; + break; + case 16: + fat_offset = data->cur_cluster << 1; + break; + default: + /* case 12: */ + fat_offset = data->cur_cluster + (data->cur_cluster >> 1); + break; + } + + /* Read the FAT. */ + if (grub_disk_read (disk, data->fat_sector, fat_offset, + (data->fat_size + 7) >> 3, + (char *) &next_cluster)) + return -1; + + next_cluster = grub_le_to_cpu32 (next_cluster); + switch (data->fat_size) + { + case 16: + next_cluster &= 0xFFFF; + break; + case 12: + if (data->cur_cluster & 1) + next_cluster >>= 4; + + next_cluster &= 0x0FFF; + break; + } + + grub_dprintf ("fat", "fat_size=%d, next_cluster=%u\n", + data->fat_size, next_cluster); + + /* Check the end. */ + if (next_cluster >= data->cluster_eof_mark) + return ret; + + if (next_cluster < 2 || next_cluster >= data->num_clusters) + { + grub_error (GRUB_ERR_BAD_FS, "invalid cluster %u", + next_cluster); + return -1; + } + + data->cur_cluster = next_cluster; + data->cur_cluster_num++; + } + + /* Read the data here. */ + sector = (data->cluster_sector + + ((data->cur_cluster - 2) + << (data->cluster_bits + data->logical_sector_bits))); + size = (1 << logical_cluster_bits) - offset; + if (size > len) + size = len; + + disk->read_hook = read_hook; + grub_disk_read (disk, sector, offset, size, buf); + disk->read_hook = 0; + if (grub_errno) + return -1; + + len -= size; + buf += size; + ret += size; + logical_cluster++; + offset = 0; + } + + return ret; +} + +static grub_err_t +grub_fat_iterate_dir (grub_disk_t disk, struct grub_fat_data *data, + int (*hook) (const char *filename, + struct grub_fat_dir_entry *dir)) +{ + struct grub_fat_dir_entry dir; + char *filename, *filep = 0; + grub_uint16_t *unibuf; + int slot = -1, slots = -1; + int checksum = -1; + grub_ssize_t offset = -sizeof(dir); + + if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + + /* Allocate space enough to hold a long name. */ + filename = grub_malloc (0x40 * 13 * 4 + 1); + unibuf = (grub_uint16_t *) grub_malloc (0x40 * 13 * 2); + if (! filename || ! unibuf) + { + grub_free (filename); + grub_free (unibuf); + return 0; + } + + while (1) + { + unsigned i; + + /* Adjust the offset. */ + offset += sizeof (dir); + + /* Read a directory entry. */ + if ((grub_fat_read_data (disk, data, 0, + offset, sizeof (dir), (char *) &dir) + != sizeof (dir) || dir.name[0] == 0)) + break; + /* Handle long name entries. */ + if (dir.attr == GRUB_FAT_ATTR_LONG_NAME) + { + struct grub_fat_long_name_entry *long_name + = (struct grub_fat_long_name_entry *) &dir; + grub_uint8_t id = long_name->id; + + if (id & 0x40) + { + id &= 0x3f; + slots = slot = id; + checksum = long_name->checksum; + } + + if (id != slot || slot == 0 || checksum != long_name->checksum) + { + checksum = -1; + continue; + } + + slot--; + grub_memcpy (unibuf + slot * 13, long_name->name1, 5 * 2); + grub_memcpy (unibuf + slot * 13 + 5, long_name->name2, 6 * 2); + grub_memcpy (unibuf + slot * 13 + 11, long_name->name3, 2 * 2); + continue; + } + + /* Check if this entry is valid. */ + if (dir.name[0] == 0xe5 || (dir.attr & ~GRUB_FAT_ATTR_VALID)) + continue; + + /* This is a workaround for Japanese. */ + if (dir.name[0] == 0x05) + dir.name[0] = 0xe5; + + if (checksum != -1 && slot == 0) + { + grub_uint8_t sum; + + for (sum = 0, i = 0; i < sizeof (dir.name); i++) + sum = ((sum >> 1) | (sum << 7)) + dir.name[i]; + + if (sum == checksum) + { + int u; + + for (u = 0; u < slots * 13; u++) + unibuf[u] = grub_le_to_cpu16 (unibuf[u]); + + *grub_utf16_to_utf8 ((grub_uint8_t *) filename, unibuf, + slots * 13) = '\0'; + + if (hook (filename, &dir)) + break; + + checksum = -1; + continue; + } + + checksum = -1; + } + + /* Convert the 8.3 file name. */ + filep = filename; + if (dir.attr & GRUB_FAT_ATTR_VOLUME_ID) + { + for (i = 0; i < sizeof (dir.name) && dir.name[i] + && ! grub_isspace (dir.name[i]); i++) + *filep++ = dir.name[i]; + } + else + { + for (i = 0; i < 8 && dir.name[i] && ! grub_isspace (dir.name[i]); i++) + *filep++ = grub_tolower (dir.name[i]); + + *filep = '.'; + + for (i = 8; i < 11 && dir.name[i] && ! grub_isspace (dir.name[i]); i++) + *++filep = grub_tolower (dir.name[i]); + + if (*filep != '.') + filep++; + } + *filep = '\0'; + + if (hook (filename, &dir)) + break; + } + + grub_free (filename); + grub_free (unibuf); + + return grub_errno; +} + + +/* Find the underlying directory or file in PATH and return the + next path. If there is no next path or an error occurs, return NULL. + If HOOK is specified, call it with each file name. */ +static char * +grub_fat_find_dir (grub_disk_t disk, struct grub_fat_data *data, + const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + char *dirname, *dirp; + int call_hook; + int found = 0; + + auto int iter_hook (const char *filename, struct grub_fat_dir_entry *dir); + int iter_hook (const char *filename, struct grub_fat_dir_entry *dir) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + + info.dir = !! (dir->attr & GRUB_FAT_ATTR_DIRECTORY); + info.case_insensitive = 1; + + if (dir->attr & GRUB_FAT_ATTR_VOLUME_ID) + return 0; + if (*dirname == '\0' && call_hook) + return hook (filename, &info); + + if (grub_strcasecmp (dirname, filename) == 0) + { + found = 1; + data->attr = dir->attr; + data->file_size = grub_le_to_cpu32 (dir->file_size); + data->file_cluster = ((grub_le_to_cpu16 (dir->first_cluster_high) << 16) + | grub_le_to_cpu16 (dir->first_cluster_low)); + data->cur_cluster_num = ~0U; + + if (call_hook) + hook (filename, &info); + + return 1; + } + return 0; + } + + if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY)) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + return 0; + } + + /* Extract a directory name. */ + while (*path == '/') + path++; + + dirp = grub_strchr (path, '/'); + if (dirp) + { + unsigned len = dirp - path; + + dirname = grub_malloc (len + 1); + if (! dirname) + return 0; + + grub_memcpy (dirname, path, len); + dirname[len] = '\0'; + } + else + /* This is actually a file. */ + dirname = grub_strdup (path); + + call_hook = (! dirp && hook); + + grub_fat_iterate_dir (disk, data, iter_hook); + if (grub_errno == GRUB_ERR_NONE && ! found && !call_hook) + grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + + grub_free (dirname); + + return found ? dirp : 0; +} + +static grub_err_t +grub_fat_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_fat_data *data = 0; + grub_disk_t disk = device->disk; + grub_size_t len; + char *dirname = 0; + char *p; + + grub_dl_ref (my_mod); + + data = grub_fat_mount (disk); + if (! data) + goto fail; + + /* Make sure that DIRNAME terminates with '/'. */ + len = grub_strlen (path); + dirname = grub_malloc (len + 1 + 1); + if (! dirname) + goto fail; + grub_memcpy (dirname, path, len); + p = dirname + len; + if (path[len - 1] != '/') + *p++ = '/'; + *p = '\0'; + p = dirname; + + do + { + p = grub_fat_find_dir (disk, data, p, hook); + } + while (p && grub_errno == GRUB_ERR_NONE); + + fail: + + grub_free (dirname); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_fat_open (grub_file_t file, const char *name) +{ + struct grub_fat_data *data = 0; + char *p = (char *) name; + + grub_dl_ref (my_mod); + + data = grub_fat_mount (file->device->disk); + if (! data) + goto fail; + + do + { + p = grub_fat_find_dir (file->device->disk, data, p, 0); + if (grub_errno != GRUB_ERR_NONE) + goto fail; + } + while (p); + + if (data->attr & GRUB_FAT_ATTR_DIRECTORY) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file"); + goto fail; + } + + file->data = data; + file->size = data->file_size; + + return GRUB_ERR_NONE; + + fail: + + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_ssize_t +grub_fat_read (grub_file_t file, char *buf, grub_size_t len) +{ + return grub_fat_read_data (file->device->disk, file->data, file->read_hook, + file->offset, len, buf); +} + +static grub_err_t +grub_fat_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_fat_label (grub_device_t device, char **label) +{ + struct grub_fat_data *data; + grub_disk_t disk = device->disk; + + auto int iter_hook (const char *filename, struct grub_fat_dir_entry *dir); + int iter_hook (const char *filename, struct grub_fat_dir_entry *dir) + { + if (dir->attr == GRUB_FAT_ATTR_VOLUME_ID) + { + *label = grub_strdup (filename); + return 1; + } + return 0; + } + + grub_dl_ref (my_mod); + + data = grub_fat_mount (disk); + if (! data) + goto fail; + + if (! (data->attr & GRUB_FAT_ATTR_DIRECTORY)) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + return 0; + } + + *label = 0; + + grub_fat_iterate_dir (disk, data, iter_hook); + + fail: + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static grub_err_t +grub_fat_uuid (grub_device_t device, char **uuid) +{ + struct grub_fat_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_fat_mount (disk); + if (data) + { + *uuid = grub_xasprintf ("%04x-%04x", + (grub_uint16_t) (data->uuid >> 16), + (grub_uint16_t) data->uuid); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static struct grub_fs grub_fat_fs = + { + .name = "fat", + .dir = grub_fat_dir, + .open = grub_fat_open, + .read = grub_fat_read, + .close = grub_fat_close, + .label = grub_fat_label, + .uuid = grub_fat_uuid, +#ifdef GRUB_UTIL + .reserved_first_sector = 1, +#endif + .next = 0 + }; + +GRUB_MOD_INIT(fat) +{ + grub_fs_register (&grub_fat_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(fat) +{ + grub_fs_unregister (&grub_fat_fs); +} + diff --git a/libr/fs/p/grub/fs/fshelp.c b/libr/fs/p/grub/fs/fshelp.c new file mode 100644 index 0000000000..d0b1e493e7 --- /dev/null +++ b/libr/fs/p/grub/fs/fshelp.c @@ -0,0 +1,315 @@ +/* fshelp.c -- Filesystem helper functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include + + +/* Lookup the node PATH. The node ROOTNODE describes the root of the + directory tree. The node found is returned in FOUNDNODE, which is + either a ROOTNODE or a new malloc'ed node. ITERATE_DIR is used to + iterate over all directory entries in the current node. + READ_SYMLINK is used to read the symlink if a node is a symlink. + EXPECTTYPE is the type node that is expected by the called, an + error is generated if the node is not of the expected type. Make + sure you use the NESTED_FUNC_ATTR macro for HOOK, this is required + because GCC has a nasty bug when using regparm=3. */ +grub_err_t +grub_fshelp_find_file (const char *path, grub_fshelp_node_t rootnode, + grub_fshelp_node_t *foundnode, + int (*iterate_dir) (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR (*hook) + (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)), + char *(*read_symlink) (grub_fshelp_node_t node), + enum grub_fshelp_filetype expecttype) +{ + grub_err_t err; + enum grub_fshelp_filetype foundtype = GRUB_FSHELP_DIR; + int symlinknest = 0; + + auto grub_err_t NESTED_FUNC_ATTR find_file (const char *currpath, + grub_fshelp_node_t currroot, + grub_fshelp_node_t *currfound); + + grub_err_t NESTED_FUNC_ATTR find_file (const char *currpath, + grub_fshelp_node_t currroot, + grub_fshelp_node_t *currfound) + { + char fpath[grub_strlen (currpath) + 1]; + char *name = fpath; + char *next; + // unsigned int pos = 0; + enum grub_fshelp_filetype type = GRUB_FSHELP_DIR; + grub_fshelp_node_t currnode = currroot; + grub_fshelp_node_t oldnode = currroot; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + auto void free_node (grub_fshelp_node_t node); + + void free_node (grub_fshelp_node_t node) + { + if (node != rootnode && node != currroot) + grub_free (node); + } + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + if (filetype == GRUB_FSHELP_UNKNOWN || + (grub_strcmp (name, filename) && + (! (filetype & GRUB_FSHELP_CASE_INSENSITIVE) || + grub_strncasecmp (name, filename, GRUB_LONG_MAX)))) + { + grub_free (node); + return 0; + } + + /* The node is found, stop iterating over the nodes. */ + type = filetype & ~GRUB_FSHELP_CASE_INSENSITIVE; + oldnode = currnode; + currnode = node; + + return 1; + } + + grub_strncpy (fpath, currpath, grub_strlen (currpath) + 1); + + /* Remove all leading slashes. */ + while (*name == '/') + name++; + + if (! *name) + { + *currfound = currnode; + return 0; + } + + for (;;) + { + int found; + + /* Extract the actual part from the pathname. */ + next = grub_strchr (name, '/'); + if (next) + { + /* Remove all leading slashes. */ + while (*next == '/') + *(next++) = '\0'; + } + + /* At this point it is expected that the current node is a + directory, check if this is true. */ + if (type != GRUB_FSHELP_DIR) + { + free_node (currnode); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + } + + /* Iterate over the directory. */ + found = iterate_dir (currnode, iterate); + if (! found) + { + if (grub_errno) + return grub_errno; + + break; + } + + /* Read in the symlink and follow it. */ + if (type == GRUB_FSHELP_SYMLINK) + { + char *symlink; + + /* Test if the symlink does not loop. */ + if (++symlinknest == 8) + { + free_node (currnode); + free_node (oldnode); + return grub_error (GRUB_ERR_SYMLINK_LOOP, + "too deep nesting of symlinks"); + } + + symlink = read_symlink (currnode); + free_node (currnode); + + if (!symlink) + { + free_node (oldnode); + return grub_errno; + } + + /* The symlink is an absolute path, go back to the root inode. */ + if (symlink[0] == '/') + { + free_node (oldnode); + oldnode = rootnode; + } + + /* Lookup the node the symlink points to. */ + find_file (symlink, oldnode, &currnode); + type = foundtype; + grub_free (symlink); + + if (grub_errno) + { + free_node (oldnode); + return grub_errno; + } + } + + free_node (oldnode); + + /* Found the node! */ + if (! next || *next == '\0') + { + *currfound = currnode; + foundtype = type; + return 0; + } + + name = next; + } + + return grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + } + + if (!path || path[0] != '/') + { + grub_error (GRUB_ERR_BAD_FILENAME, "bad filename"); + return grub_errno; + } + + err = find_file (path, rootnode, foundnode); + if (err) + return err; + + /* Check if the node that was found was of the expected type. */ + if (expecttype == GRUB_FSHELP_REG && foundtype != expecttype) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file"); + else if (expecttype == GRUB_FSHELP_DIR && foundtype != expecttype) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + + return 0; +} + +/* Read LEN bytes from the file NODE on disk DISK into the buffer BUF, + beginning with the block POS. READ_HOOK should be set before + reading a block from the file. GET_BLOCK is used to translate file + blocks to disk blocks. The file is FILESIZE bytes big and the + blocks have a size of LOG2BLOCKSIZE (in log2). */ +grub_ssize_t +grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, + unsigned length), + grub_off_t pos, grub_size_t len, char *buf, + grub_disk_addr_t (*get_block) (grub_fshelp_node_t node, + grub_disk_addr_t block), + grub_off_t filesize, int log2blocksize) +{ + grub_disk_addr_t i, blockcnt; + int blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS); + + /* Adjust LEN so it we can't read past the end of the file. */ + if (pos + len > filesize) + len = filesize - pos; + + blockcnt = ((len + pos) + blocksize - 1) >> (log2blocksize + GRUB_DISK_SECTOR_BITS); + + for (i = pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS); i < blockcnt; i++) + { + grub_disk_addr_t blknr; + int blockoff = pos & (blocksize - 1); + int blockend = blocksize; + + int skipfirst = 0; + + blknr = get_block (node, i); + if (grub_errno) + return -1; + + blknr = blknr << log2blocksize; + + /* Last block. */ + if (i == blockcnt - 1) + { + blockend = (len + pos) & (blocksize - 1); + + /* The last portion is exactly blocksize. */ + if (! blockend) + blockend = blocksize; + } + + /* First block. */ + if (i == (pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS))) + { + skipfirst = blockoff; + blockend -= skipfirst; + } + + /* If the block number is 0 this block is not stored on disk but + is zero filled instead. */ + if (blknr) + { + disk->read_hook = read_hook; + + grub_disk_read (disk, blknr, skipfirst, + blockend, buf); + disk->read_hook = 0; + if (grub_errno) + return -1; + } + else + grub_memset (buf, 0, blockend); + + buf += blocksize - skipfirst; + } + + return len; +} + +unsigned int +grub_fshelp_log2blksize (unsigned int blksize, unsigned int *pow) +{ + int mod; + + *pow = 0; + while (blksize > 1) + { + mod = blksize - ((blksize >> 1) << 1); + blksize >>= 1; + + /* Check if it really is a power of two. */ + if (mod) + return grub_error (GRUB_ERR_BAD_NUMBER, + "the blocksize is not a power of two"); + (*pow)++; + } + + return GRUB_ERR_NONE; +} diff --git a/libr/fs/p/grub/fs/hfs.c b/libr/fs/p/grub/fs/hfs.c new file mode 100644 index 0000000000..cef8563269 --- /dev/null +++ b/libr/fs/p/grub/fs/hfs.c @@ -0,0 +1,1122 @@ +/* hfs.c - HFS. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/* HFS is documented at + http://developer.apple.com/documentation/mac/Files/Files-2.html */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_HFS_SBLOCK 2 +#define GRUB_HFS_EMBED_HFSPLUS_SIG 0x482B + +#define GRUB_HFS_BLKS (data->blksz >> 9) + +#define GRUB_HFS_NODE_LEAF 0xFF + +/* The two supported filesystems a record can have. */ +enum + { + GRUB_HFS_FILETYPE_DIR = 1, + GRUB_HFS_FILETYPE_FILE = 2 + }; + +/* Catalog node ID (CNID). */ +enum grub_hfs_cnid_type + { + GRUB_HFS_CNID_ROOT_PARENT = 1, + GRUB_HFS_CNID_ROOT = 2, + GRUB_HFS_CNID_EXT = 3, + GRUB_HFS_CNID_CAT = 4, + GRUB_HFS_CNID_BAD = 5 + }; + +/* A node descriptor. This is the header of every node. */ +struct grub_hfs_node +{ + grub_uint32_t next; + grub_uint32_t prev; + grub_uint8_t type; + grub_uint8_t level; + grub_uint16_t reccnt; + grub_uint16_t unused; +} __attribute__ ((packed)); + +/* The head of the B*-Tree. */ +struct grub_hfs_treeheader +{ + grub_uint16_t tree_depth; + /* The number of the first node. */ + grub_uint32_t root_node; + grub_uint32_t leaves; + grub_uint32_t first_leaf; + grub_uint32_t last_leaf; + grub_uint16_t node_size; + grub_uint16_t key_size; + grub_uint32_t nodes; + grub_uint32_t free_nodes; + grub_uint8_t unused[76]; +} __attribute__ ((packed)); + +/* The state of a mounted HFS filesystem. */ +struct grub_hfs_data +{ + struct grub_hfs_sblock sblock; + grub_disk_t disk; + grub_hfs_datarecord_t extents; + int fileid; + int size; + int ext_root; + int ext_size; + int cat_root; + int cat_size; + int blksz; + int log2_blksz; + int rootdir; +}; + +/* The key as used on disk in a catalog tree. This is used to lookup + file/directory nodes by parent directory ID and filename. */ +struct grub_hfs_catalog_key +{ + grub_uint8_t unused; + grub_uint32_t parent_dir; + + /* Filename length. */ + grub_uint8_t strlen; + + /* Filename. */ + grub_uint8_t str[31]; +} __attribute__ ((packed)); + +/* The key as used on disk in a extent overflow tree. Using this key + the extents can be looked up using a fileid and logical start block + as index. */ +struct grub_hfs_extent_key +{ + /* The kind of fork. This is used to store meta information like + icons, attributes, etc. We will only use the datafork, which is + 0. */ + grub_uint8_t forktype; + grub_uint32_t fileid; + grub_uint16_t first_block; +} __attribute__ ((packed)); + +/* A directory record. This is used to find out the directory ID. */ +struct grub_hfs_dirrec +{ + /* For a directory, type == 1. */ + grub_uint8_t type; + grub_uint8_t unused[5]; + grub_uint32_t dirid; +} __attribute__ ((packed)); + +/* Information about a file. */ +struct grub_hfs_filerec +{ + /* For a file, type == 2. */ + grub_uint8_t type; + grub_uint8_t unused[19]; + grub_uint32_t fileid; + grub_uint8_t unused2[2]; + grub_uint32_t size; + grub_uint8_t unused3[44]; + + /* The first 3 extents of the file. The other extents can be found + in the extent overflow file. */ + grub_hfs_datarecord_t extents; +} __attribute__ ((packed)); + +/* A record descriptor, both key and data, used to pass to call back + functions. */ +struct grub_hfs_record +{ + void *key; + int keylen; + void *data; + int datalen; +}; + +static grub_dl_t my_mod; + +static int grub_hfs_find_node (struct grub_hfs_data *, char *, + grub_uint32_t, int, char *, int); + +/* Find block BLOCK of the file FILE in the mounted UFS filesystem + DATA. The first 3 extents are described by DAT. If cache is set, + using caching to improve non-random reads. */ +static unsigned int +grub_hfs_block (struct grub_hfs_data *data, grub_hfs_datarecord_t dat, + int file, int block, int cache) +{ + grub_hfs_datarecord_t dr; + int pos = 0; + struct grub_hfs_extent_key key; + + int tree = 0; + static int cache_file = 0; + static int cache_pos = 0; + static grub_hfs_datarecord_t cache_dr; + + grub_memcpy (dr, dat, sizeof (dr)); + + key.forktype = 0; + key.fileid = grub_cpu_to_be32 (file); + + if (cache && cache_file == file && block > cache_pos) + { + pos = cache_pos; + key.first_block = grub_cpu_to_be16 (pos); + grub_memcpy (dr, cache_dr, sizeof (cache_dr)); + } + + for (;;) + { + int i; + + /* Try all 3 extents. */ + for (i = 0; i < 3; i++) + { + /* Check if the block is stored in this extent. */ + if (grub_be_to_cpu16 (dr[i].count) + pos > block) + { + int first = grub_be_to_cpu16 (dr[i].first_block); + + /* If the cache is enabled, store the current position + in the tree. */ + if (tree && cache) + { + cache_file = file; + cache_pos = pos; + grub_memcpy (cache_dr, dr, sizeof (cache_dr)); + } + + return (grub_be_to_cpu16 (data->sblock.first_block) + + (first + block - pos) * GRUB_HFS_BLKS); + } + + /* Try the next extent. */ + pos += grub_be_to_cpu16 (dr[i].count); + } + + /* Lookup the block in the extent overflow file. */ + key.first_block = grub_cpu_to_be16 (pos); + tree = 1; + grub_hfs_find_node (data, (char *) &key, data->ext_root, + 1, (char *) &dr, sizeof (dr)); + if (grub_errno) + return 0; + } +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_hfs_read_file (struct grub_hfs_data *data, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + int i; + int blockcnt; + + blockcnt = ((len + pos) + + data->blksz - 1) / data->blksz; + + for (i = pos / data->blksz; i < blockcnt; i++) + { + int blknr; + int blockoff = pos % data->blksz; + int blockend = data->blksz; + + int skipfirst = 0; + + blknr = grub_hfs_block (data, data->extents, data->fileid, i, 1); + if (grub_errno) + return -1; + + /* Last block. */ + if (i == blockcnt - 1) + { + blockend = (len + pos) % data->blksz; + + /* The last portion is exactly EXT2_BLOCK_SIZE (data). */ + if (! blockend) + blockend = data->blksz; + } + + /* First block. */ + if (i == pos / data->blksz) + { + skipfirst = blockoff; + blockend -= skipfirst; + } + + /* If the block number is 0 this block is not stored on disk but + is zero filled instead. */ + if (blknr) + { + data->disk->read_hook = read_hook; + grub_disk_read (data->disk, blknr, skipfirst, + blockend, buf); + data->disk->read_hook = 0; + if (grub_errno) + return -1; + } + + buf += data->blksz - skipfirst; + } + + return len; +} + + +/* Mount the filesystem on the disk DISK. */ +static struct grub_hfs_data * +grub_hfs_mount (grub_disk_t disk) +{ + struct grub_hfs_data *data; + struct grub_hfs_catalog_key key; + struct grub_hfs_dirrec dir; + int first_block; + + struct + { + struct grub_hfs_node node; + struct grub_hfs_treeheader head; + } treehead; + + data = grub_malloc (sizeof (struct grub_hfs_data)); + if (!data) + return 0; + + /* Read the superblock. */ + if (grub_disk_read (disk, GRUB_HFS_SBLOCK, 0, + sizeof (struct grub_hfs_sblock), &data->sblock)) + goto fail; + + /* Check if this is a HFS filesystem. */ + if (grub_be_to_cpu16 (data->sblock.magic) != GRUB_HFS_MAGIC) + { + grub_error (GRUB_ERR_BAD_FS, "not an HFS filesystem"); + goto fail; + } + + /* Check if this is an embedded HFS+ filesystem. */ + if (grub_be_to_cpu16 (data->sblock.embed_sig) == GRUB_HFS_EMBED_HFSPLUS_SIG) + { + grub_error (GRUB_ERR_BAD_FS, "embedded HFS+ filesystem"); + goto fail; + } + + data->blksz = grub_be_to_cpu32 (data->sblock.blksz); + data->disk = disk; + + /* Lookup the root node of the extent overflow tree. */ + first_block = ((grub_be_to_cpu16 (data->sblock.extent_recs[0].first_block) + * GRUB_HFS_BLKS) + + grub_be_to_cpu16 (data->sblock.first_block)); + + if (grub_disk_read (data->disk, first_block, 0, + sizeof (treehead), &treehead)) + goto fail; + data->ext_root = grub_be_to_cpu32 (treehead.head.root_node); + data->ext_size = grub_be_to_cpu16 (treehead.head.node_size); + + /* Lookup the root node of the catalog tree. */ + first_block = ((grub_be_to_cpu16 (data->sblock.catalog_recs[0].first_block) + * GRUB_HFS_BLKS) + + grub_be_to_cpu16 (data->sblock.first_block)); + if (grub_disk_read (data->disk, first_block, 0, + sizeof (treehead), &treehead)) + goto fail; + data->cat_root = grub_be_to_cpu32 (treehead.head.root_node); + data->cat_size = grub_be_to_cpu16 (treehead.head.node_size); + + /* Lookup the root directory node in the catalog tree using the + volume name. */ + key.parent_dir = grub_cpu_to_be32 (1); + key.strlen = data->sblock.volname[0]; + grub_strcpy ((char *) key.str, (char *) (data->sblock.volname + 1)); + + if (grub_hfs_find_node (data, (char *) &key, data->cat_root, + 0, (char *) &dir, sizeof (dir)) == 0) + { + grub_error (GRUB_ERR_BAD_FS, "cannot find the HFS root directory"); + goto fail; + } + + if (grub_errno) + goto fail; + + data->rootdir = grub_be_to_cpu32 (dir.dirid); + + return data; + fail: + grub_free (data); + + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not a HFS filesystem"); + + return 0; +} + +/* Compare the K1 and K2 catalog file keys using HFS character ordering. */ +static int +grub_hfs_cmp_catkeys (struct grub_hfs_catalog_key *k1, + struct grub_hfs_catalog_key *k2) +{ + /* Taken from hfsutils 3.2.6 and converted to a readable form */ + static const unsigned char hfs_charorder[256] = { + [0x00] = 0, + [0x01] = 1, + [0x02] = 2, + [0x03] = 3, + [0x04] = 4, + [0x05] = 5, + [0x06] = 6, + [0x07] = 7, + [0x08] = 8, + [0x09] = 9, + [0x0A] = 10, + [0x0B] = 11, + [0x0C] = 12, + [0x0D] = 13, + [0x0E] = 14, + [0x0F] = 15, + [0x10] = 16, + [0x11] = 17, + [0x12] = 18, + [0x13] = 19, + [0x14] = 20, + [0x15] = 21, + [0x16] = 22, + [0x17] = 23, + [0x18] = 24, + [0x19] = 25, + [0x1A] = 26, + [0x1B] = 27, + [0x1C] = 28, + [0x1D] = 29, + [0x1E] = 30, + [0x1F] = 31, + [' '] = 32, [0xCA] = 32, + ['!'] = 33, + ['"'] = 34, + [0xD2] = 35, + [0xD3] = 36, + [0xC7] = 37, + [0xC8] = 38, + ['#'] = 39, + ['$'] = 40, + ['%'] = 41, + ['&'] = 42, + ['\''] = 43, + [0xD4] = 44, + [0xD5] = 45, + ['('] = 46, + [')'] = 47, + ['*'] = 48, + ['+'] = 49, + [','] = 50, + ['-'] = 51, + ['.'] = 52, + ['/'] = 53, + ['0'] = 54, + ['1'] = 55, + ['2'] = 56, + ['3'] = 57, + ['4'] = 58, + ['5'] = 59, + ['6'] = 60, + ['7'] = 61, + ['8'] = 62, + ['9'] = 63, + [':'] = 64, + [';'] = 65, + ['<'] = 66, + ['='] = 67, + ['>'] = 68, + ['?'] = 69, + ['@'] = 70, + ['A'] = 71, ['a'] = 71, + [0x88] = 72, [0xCB] = 72, + [0x80] = 73, [0x8A] = 73, + [0x8B] = 74, [0xCC] = 74, + [0x81] = 75, [0x8C] = 75, + [0xAE] = 76, [0xBE] = 76, + ['`'] = 77, + [0x87] = 78, + [0x89] = 79, + [0xBB] = 80, + ['B'] = 81, ['b'] = 81, + ['C'] = 82, ['c'] = 82, + [0x82] = 83, [0x8D] = 83, + ['D'] = 84, ['d'] = 84, + ['E'] = 85, ['e'] = 85, + [0x83] = 86, [0x8E] = 86, + [0x8F] = 87, + [0x90] = 88, + [0x91] = 89, + ['F'] = 90, ['f'] = 90, + ['G'] = 91, ['g'] = 91, + ['H'] = 92, ['h'] = 92, + ['I'] = 93, ['i'] = 93, + [0x92] = 94, + [0x93] = 95, + [0x94] = 96, + [0x95] = 97, + ['J'] = 98, ['j'] = 98, + ['K'] = 99, ['k'] = 99, + ['L'] = 100, ['l'] = 100, + ['M'] = 101, ['m'] = 101, + ['N'] = 102, ['n'] = 102, + [0x84] = 103, [0x96] = 103, + ['O'] = 104, ['o'] = 104, + [0x85] = 105, [0x9A] = 105, + [0x9B] = 106, [0xCD] = 106, + [0xAF] = 107, [0xBF] = 107, + [0xCE] = 108, [0xCF] = 108, + [0x97] = 109, + [0x98] = 110, + [0x99] = 111, + [0xBC] = 112, + ['P'] = 113, ['p'] = 113, + ['Q'] = 114, ['q'] = 114, + ['R'] = 115, ['r'] = 115, + ['S'] = 116, ['s'] = 116, + [0xA7] = 117, + ['T'] = 118, ['t'] = 118, + ['U'] = 119, ['u'] = 119, + [0x86] = 120, [0x9F] = 120, + [0x9C] = 121, + [0x9D] = 122, + [0x9E] = 123, + ['V'] = 124, ['v'] = 124, + ['W'] = 125, ['w'] = 125, + ['X'] = 126, ['x'] = 126, + ['Y'] = 127, ['y'] = 127, + [0xD8] = 128, + ['Z'] = 129, ['z'] = 129, + ['['] = 130, + ['\\'] = 131, + [']'] = 132, + ['^'] = 133, + ['_'] = 134, + ['{'] = 135, + ['|'] = 136, + ['}'] = 137, + ['~'] = 138, + [0x7F] = 139, + [0xA0] = 140, + [0xA1] = 141, + [0xA2] = 142, + [0xA3] = 143, + [0xA4] = 144, + [0xA5] = 145, + [0xA6] = 146, + [0xA8] = 147, + [0xA9] = 148, + [0xAA] = 149, + [0xAB] = 150, + [0xAC] = 151, + [0xAD] = 152, + [0xB0] = 153, + [0xB1] = 154, + [0xB2] = 155, + [0xB3] = 156, + [0xB4] = 157, + [0xB5] = 158, + [0xB6] = 159, + [0xB7] = 160, + [0xB8] = 161, + [0xB9] = 162, + [0xBA] = 163, + [0xBD] = 164, + [0xC0] = 165, + [0xC1] = 166, + [0xC2] = 167, + [0xC3] = 168, + [0xC4] = 169, + [0xC5] = 170, + [0xC6] = 171, + [0xC9] = 172, + [0xD0] = 173, + [0xD1] = 174, + [0xD6] = 175, + [0xD7] = 176, + [0xD9] = 177, + [0xDA] = 178, + [0xDB] = 179, + [0xDC] = 180, + [0xDD] = 181, + [0xDE] = 182, + [0xDF] = 183, + [0xE0] = 184, + [0xE1] = 185, + [0xE2] = 186, + [0xE3] = 187, + [0xE4] = 188, + [0xE5] = 189, + [0xE6] = 190, + [0xE7] = 191, + [0xE8] = 192, + [0xE9] = 193, + [0xEA] = 194, + [0xEB] = 195, + [0xEC] = 196, + [0xED] = 197, + [0xEE] = 198, + [0xEF] = 199, + [0xF0] = 200, + [0xF1] = 201, + [0xF2] = 202, + [0xF3] = 203, + [0xF4] = 204, + [0xF5] = 205, + [0xF6] = 206, + [0xF7] = 207, + [0xF8] = 208, + [0xF9] = 209, + [0xFA] = 210, + [0xFB] = 211, + [0xFC] = 212, + [0xFD] = 213, + [0xFE] = 214, + [0xFF] = 215, + }; + int i; + int cmp; + int minlen = (k1->strlen < k2->strlen) ? k1->strlen : k2->strlen; + + cmp = (grub_be_to_cpu32 (k1->parent_dir) - grub_be_to_cpu32 (k2->parent_dir)); + if (cmp != 0) + return cmp; + + for (i = 0; i < minlen; i++) + { + cmp = (hfs_charorder[k1->str[i]] - hfs_charorder[k2->str[i]]); + if (cmp != 0) + return cmp; + } + + /* Shorter strings precede long ones. */ + return (k1->strlen - k2->strlen); +} + + +/* Compare the K1 and K2 extent overflow file keys. */ +static int +grub_hfs_cmp_extkeys (struct grub_hfs_extent_key *k1, + struct grub_hfs_extent_key *k2) +{ + int cmp = k1->forktype - k2->forktype; + if (cmp == 0) + cmp = grub_be_to_cpu32 (k1->fileid) - grub_be_to_cpu32 (k2->fileid); + if (cmp == 0) + cmp = (grub_be_to_cpu16 (k1->first_block) + - grub_be_to_cpu16 (k2->first_block)); + return cmp; +} + + +/* Iterate the records in the node with index IDX in the mounted HFS + filesystem DATA. This node holds data of the type TYPE (0 = + catalog node, 1 = extent overflow node). If this is set, continue + iterating to the next node. For every records, call NODE_HOOK. */ +static grub_err_t +grub_hfs_iterate_records (struct grub_hfs_data *data, int type, int idx, + int this, int (*node_hook) (struct grub_hfs_node *hnd, + struct grub_hfs_record *)) +{ + int nodesize = type == 0 ? data->cat_size : data->ext_size; + + union + { + struct grub_hfs_node node; + char rawnode[nodesize]; + grub_uint16_t offsets[nodesize / 2]; + } node; + + do + { + int i; + struct grub_hfs_extent *dat; + int blk; + + dat = (struct grub_hfs_extent *) (type == 0 + ? (&data->sblock.catalog_recs) + : (&data->sblock.extent_recs)); + + /* Read the node into memory. */ + blk = grub_hfs_block (data, dat, + (type == 0) ? GRUB_HFS_CNID_CAT : GRUB_HFS_CNID_EXT, + idx / (data->blksz / nodesize), 0); + blk += (idx % (data->blksz / nodesize)); + if (grub_errno) + return grub_errno; + + if (grub_disk_read (data->disk, blk, 0, + sizeof (node), &node)) + return grub_errno; + + /* Iterate over all records in this node. */ + for (i = 0; i < grub_be_to_cpu16 (node.node.reccnt); i++) + { + int pos = (nodesize >> 1) - 1 - i; + struct pointer + { + grub_uint8_t keylen; + grub_uint8_t key; + } __attribute__ ((packed)) *pnt; + pnt = (struct pointer *) (grub_be_to_cpu16 (node.offsets[pos]) + + node.rawnode); + + struct grub_hfs_record rec = + { + &pnt->key, + pnt->keylen, + &pnt->key + pnt->keylen +(pnt->keylen + 1) % 2, + nodesize - grub_be_to_cpu16 (node.offsets[pos]) + - pnt->keylen - 1 + }; + + if (node_hook (&node.node, &rec)) + return 0; + } + + idx = grub_be_to_cpu32 (node.node.next); + } while (idx && this); + + return 0; +} + + +/* Lookup a record in the mounted filesystem DATA using the key KEY. + The index of the node on top of the tree is IDX. The tree is of + the type TYPE (0 = catalog node, 1 = extent overflow node). Return + the data in DATAR with a maximum length of DATALEN. */ +static int +grub_hfs_find_node (struct grub_hfs_data *data, char *key, + grub_uint32_t idx, int type, char *datar, int datalen) +{ + int found = -1; + int isleaf = 0; + int done = 0; + + auto int node_found (struct grub_hfs_node *, struct grub_hfs_record *); + + int node_found (struct grub_hfs_node *hnd, struct grub_hfs_record *rec) + { + int cmp = 1; + + if (type == 0) + cmp = grub_hfs_cmp_catkeys (rec->key, (void *) key); + else + cmp = grub_hfs_cmp_extkeys (rec->key, (void *) key); + + /* If the key is smaller or equal to the current node, mark the + entry. In case of a non-leaf mode it will be used to lookup + the rest of the tree. */ + if (cmp <= 0) + { + grub_uint32_t *node = (grub_uint32_t *) rec->data; + found = grub_be_to_cpu32 (*node); + } + else /* The key can not be found in the tree. */ + return 1; + + /* Check if this node is a leaf node. */ + if (hnd->type == GRUB_HFS_NODE_LEAF) + { + isleaf = 1; + + /* Found it!!!! */ + if (cmp == 0) + { + done = 1; + + grub_memcpy (datar, rec->data, + rec->datalen < datalen ? rec->datalen : datalen); + return 1; + } + } + + return 0; + } + + do + { + found = -1; + + if (grub_hfs_iterate_records (data, type, idx, 0, node_found)) + return 0; + + if (found == -1) + return 0; + + idx = found; + } while (! isleaf); + + return done; +} + + +/* Iterate over the directory with the id DIR. The tree is searched + starting with the node ROOT_IDX. For every entry in this directory + call HOOK. */ +static grub_err_t +grub_hfs_iterate_dir (struct grub_hfs_data *data, grub_uint32_t root_idx, + unsigned int dir, int (*hook) (struct grub_hfs_record *)) +{ + int found = -1; + int isleaf = 0; + int next = 0; + + /* The lowest key possible with DIR as root directory. */ + struct grub_hfs_catalog_key key = {0, grub_cpu_to_be32 (dir), 0, ""}; + + auto int node_found (struct grub_hfs_node *, struct grub_hfs_record *); + auto int it_dir (struct grub_hfs_node * __attribute ((unused)), + struct grub_hfs_record *); + + + int node_found (struct grub_hfs_node *hnd, struct grub_hfs_record *rec) + { + struct grub_hfs_catalog_key *ckey = rec->key; + + if (grub_hfs_cmp_catkeys (rec->key, (void *) &key) <= 0) + found = grub_be_to_cpu32 (*(grub_uint32_t *) rec->data); + + if (hnd->type == 0xFF && ckey->strlen > 0) + { + isleaf = 1; + next = grub_be_to_cpu32 (hnd->next); + + /* An entry was found. */ + if (grub_be_to_cpu32 (ckey->parent_dir) == dir) + return hook (rec); + } + + return 0; + } + + int it_dir (struct grub_hfs_node *hnd __attribute ((unused)), + struct grub_hfs_record *rec) + { + struct grub_hfs_catalog_key *ckey = rec->key; + struct grub_hfs_catalog_key *origkey = &key; + + /* Stop when the entries do not match anymore. */ + if (grub_be_to_cpu32 (ckey->parent_dir) + != grub_be_to_cpu32 ((origkey)->parent_dir)) + return 1; + + return hook (rec); + } + + do + { + found = -1; + + if (grub_hfs_iterate_records (data, 0, root_idx, 0, node_found)) + return grub_errno; + + if (found == -1) + return 0; + + root_idx = found; + } while (! isleaf); + + /* If there was a matching record in this leaf node, continue the + iteration until the last record was found. */ + grub_hfs_iterate_records (data, 0, next, 1, it_dir); + return grub_errno; +} + + +/* Find a file or directory with the pathname PATH in the filesystem + DATA. Return the file record in RETDATA when it is non-zero. + Return the directory number in RETINODE when it is non-zero. */ +static grub_err_t +grub_hfs_find_dir (struct grub_hfs_data *data, const char *path, + struct grub_hfs_filerec *retdata, int *retinode) +{ + int inode = data->rootdir; + char *next; + char *origpath; + union { + struct grub_hfs_filerec frec; + struct grub_hfs_dirrec dir; + } fdrec; + + fdrec.frec.type = GRUB_HFS_FILETYPE_DIR; + + if (path[0] != '/') + { + grub_error (GRUB_ERR_BAD_FILENAME, "bad filename"); + return 0; + } + + origpath = grub_strdup (path); + if (!origpath) + return grub_errno; + + path = origpath; + while (*path == '/') + path++; + + while (path && grub_strlen (path)) + { + if (fdrec.frec.type != GRUB_HFS_FILETYPE_DIR) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + goto fail; + } + + /* Isolate a part of the path. */ + next = grub_strchr (path, '/'); + if (next) + { + while (*next == '/') + *(next++) = '\0'; + } + + struct grub_hfs_catalog_key key; + + key.parent_dir = grub_cpu_to_be32 (inode); + key.strlen = grub_strlen (path); + grub_strcpy ((char *) (key.str), path); + + /* Lookup this node. */ + if (! grub_hfs_find_node (data, (char *) &key, data->cat_root, + 0, (char *) &fdrec.frec, sizeof (fdrec.frec))) + { + grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + goto fail; + } + + if (grub_errno) + goto fail; + + inode = grub_be_to_cpu32 (fdrec.dir.dirid); + path = next; + } + + if (retdata) + grub_memcpy (retdata, &fdrec.frec, sizeof (fdrec.frec)); + + if (retinode) + *retinode = inode; + + fail: + grub_free (origpath); + return grub_errno; +} + + + +static grub_err_t +grub_hfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + int inode; + + auto int dir_hook (struct grub_hfs_record *rec); + + int dir_hook (struct grub_hfs_record *rec) + { + char fname[32] = { 0 }; + char *filetype = rec->data; + struct grub_hfs_catalog_key *ckey = rec->key; + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + + grub_strncpy (fname, (char *) (ckey->str), ckey->strlen); + + if (*filetype == GRUB_HFS_FILETYPE_DIR + || *filetype == GRUB_HFS_FILETYPE_FILE) + { + info.dir = (*filetype == GRUB_HFS_FILETYPE_DIR); + return hook (fname, &info); + } + return 0; + } + + struct grub_hfs_data *data; + struct grub_hfs_filerec frec; + + grub_dl_ref (my_mod); + + data = grub_hfs_mount (device->disk); + if (!data) + goto fail; + + /* First the directory ID for the directory. */ + if (grub_hfs_find_dir (data, path, &frec, &inode)) + goto fail; + + if (frec.type != GRUB_HFS_FILETYPE_DIR) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + goto fail; + } + + grub_hfs_iterate_dir (data, data->cat_root, inode, dir_hook); + + fail: + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_hfs_open (struct grub_file *file, const char *name) +{ + struct grub_hfs_data *data; + struct grub_hfs_filerec frec; + + grub_dl_ref (my_mod); + + data = grub_hfs_mount (file->device->disk); + + if (grub_hfs_find_dir (data, name, &frec, 0)) + { + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; + } + + if (frec.type != GRUB_HFS_FILETYPE_FILE) + { + grub_free (data); + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file"); + grub_dl_unref (my_mod); + return grub_errno; + } + + grub_memcpy (data->extents, frec.extents, sizeof (grub_hfs_datarecord_t)); + file->size = grub_be_to_cpu32 (frec.size); + data->size = grub_be_to_cpu32 (frec.size); + data->fileid = grub_be_to_cpu32 (frec.fileid); + file->offset = 0; + + file->data = data; + + return 0; +} + +static grub_ssize_t +grub_hfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_hfs_data *data = + (struct grub_hfs_data *) file->data; + + return grub_hfs_read_file (data, file->read_hook, file->offset, len, buf); +} + + +static grub_err_t +grub_hfs_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return 0; +} + + +static grub_err_t +grub_hfs_label (grub_device_t device, char **label) +{ + struct grub_hfs_data *data; + + data = grub_hfs_mount (device->disk); + + if (data) + *label = grub_strndup ((char *) (data->sblock.volname + 1), + *data->sblock.volname); + else + *label = 0; + + grub_free (data); + return grub_errno; +} + +static grub_err_t +grub_hfs_uuid (grub_device_t device, char **uuid) +{ + struct grub_hfs_data *data; + + grub_dl_ref (my_mod); + + data = grub_hfs_mount (device->disk); + if (data && data->sblock.num_serial != 0) + { + *uuid = grub_xasprintf ("%016llx", + (unsigned long long) + grub_be_to_cpu64 (data->sblock.num_serial)); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + + +static struct grub_fs grub_hfs_fs = + { + .name = "hfs", + .dir = grub_hfs_dir, + .open = grub_hfs_open, + .read = grub_hfs_read, + .close = grub_hfs_close, + .label = grub_hfs_label, + .uuid = grub_hfs_uuid, + .next = 0 + }; + +GRUB_MOD_INIT(hfs) +{ + grub_fs_register (&grub_hfs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(hfs) +{ + grub_fs_unregister (&grub_hfs_fs); +} diff --git a/libr/fs/p/grub/fs/hfsplus.c b/libr/fs/p/grub/fs/hfsplus.c new file mode 100644 index 0000000000..7d7115ce36 --- /dev/null +++ b/libr/fs/p/grub/fs/hfsplus.c @@ -0,0 +1,1048 @@ +/* hfsplus.c - HFS+ Filesystem. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/* HFS+ is documented at http://developer.apple.com/technotes/tn/tn1150.html */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_HFSPLUS_MAGIC 0x482B +#define GRUB_HFSPLUSX_MAGIC 0x4858 +#define GRUB_HFSPLUS_SBLOCK 2 + +/* A HFS+ extent. */ +struct grub_hfsplus_extent +{ + /* The first block of a file on disk. */ + grub_uint32_t start; + /* The amount of blocks described by this extent. */ + grub_uint32_t count; +} __attribute__ ((packed)); + +/* The descriptor of a fork. */ +struct grub_hfsplus_forkdata +{ + grub_uint64_t size; + grub_uint32_t clumpsize; + grub_uint32_t blocks; + struct grub_hfsplus_extent extents[8]; +} __attribute__ ((packed)); + +/* The HFS+ Volume Header. */ +struct grub_hfsplus_volheader +{ + grub_uint16_t magic; + grub_uint16_t version; + grub_uint32_t attributes; + grub_uint8_t unused1[12]; + grub_uint32_t utime; + grub_uint8_t unused2[16]; + grub_uint32_t blksize; + grub_uint8_t unused3[60]; + grub_uint64_t num_serial; + struct grub_hfsplus_forkdata allocations_file; + struct grub_hfsplus_forkdata extents_file; + struct grub_hfsplus_forkdata catalog_file; + struct grub_hfsplus_forkdata attrib_file; + struct grub_hfsplus_forkdata startup_file; +} __attribute__ ((packed)); + +/* The type of node. */ +enum grub_hfsplus_btnode_type + { + GRUB_HFSPLUS_BTNODE_TYPE_LEAF = -1, + GRUB_HFSPLUS_BTNODE_TYPE_INDEX = 0, + GRUB_HFSPLUS_BTNODE_TYPE_HEADER = 1, + GRUB_HFSPLUS_BTNODE_TYPE_MAP = 2, + }; + +struct grub_hfsplus_btnode +{ + grub_uint32_t next; + grub_uint32_t prev; + grub_int8_t type; + grub_uint8_t height; + grub_uint16_t count; + grub_uint16_t unused; +} __attribute__ ((packed)); + +/* The header of a HFS+ B+ Tree. */ +struct grub_hfsplus_btheader +{ + grub_uint16_t depth; + grub_uint32_t root; + grub_uint32_t leaf_records; + grub_uint32_t first_leaf_node; + grub_uint32_t last_leaf_node; + grub_uint16_t nodesize; + grub_uint16_t keysize; + grub_uint32_t total_nodes; + grub_uint32_t free_nodes; + grub_uint16_t reserved1; + grub_uint32_t clump_size; /* ignored */ + grub_uint8_t btree_type; + grub_uint8_t key_compare; + grub_uint32_t attributes; +} __attribute__ ((packed)); + +/* The on disk layout of a catalog key. */ +struct grub_hfsplus_catkey +{ + grub_uint16_t keylen; + grub_uint32_t parent; + grub_uint16_t namelen; + grub_uint16_t name[30]; +} __attribute__ ((packed)); + +/* The on disk layout of an extent overflow file key. */ +struct grub_hfsplus_extkey +{ + grub_uint16_t keylen; + grub_uint8_t type; + grub_uint8_t unused; + grub_uint32_t fileid; + grub_uint32_t start; +} __attribute__ ((packed)); + +struct grub_hfsplus_key +{ + union + { + struct grub_hfsplus_extkey extkey; + struct grub_hfsplus_catkey catkey; + grub_uint16_t keylen; + }; +} __attribute__ ((packed)); + +struct grub_hfsplus_catfile +{ + grub_uint16_t type; + grub_uint16_t flags; + grub_uint32_t reserved; + grub_uint32_t fileid; + grub_uint8_t unused1[4]; + grub_uint32_t mtime; + grub_uint8_t unused2[22]; + grub_uint16_t mode; + grub_uint8_t unused3[44]; + struct grub_hfsplus_forkdata data; + struct grub_hfsplus_forkdata resource; +} __attribute__ ((packed)); + +/* Filetype information as used in inodes. */ +#define GRUB_HFSPLUS_FILEMODE_MASK 0170000 +#define GRUB_HFSPLUS_FILEMODE_REG 0100000 +#define GRUB_HFSPLUS_FILEMODE_DIRECTORY 0040000 +#define GRUB_HFSPLUS_FILEMODE_SYMLINK 0120000 + +/* Some pre-defined file IDs. */ +#define GRUB_HFSPLUS_FILEID_ROOTDIR 2 +#define GRUB_HFSPLUS_FILEID_OVERFLOW 3 +#define GRUB_HFSPLUS_FILEID_CATALOG 4 + +enum grub_hfsplus_filetype + { + GRUB_HFSPLUS_FILETYPE_DIR = 1, + GRUB_HFSPLUS_FILETYPE_REG = 2, + GRUB_HFSPLUS_FILETYPE_DIR_THREAD = 3, + GRUB_HFSPLUS_FILETYPE_REG_THREAD = 4 + }; + +#define GRUB_HFSPLUSX_BINARYCOMPARE 0xBC +#define GRUB_HFSPLUSX_CASEFOLDING 0xCF + +/* Internal representation of a catalog key. */ +struct grub_hfsplus_catkey_internal +{ + grub_uint32_t parent; + char *name; +}; + +/* Internal representation of an extent overflow key. */ +struct grub_hfsplus_extkey_internal +{ + grub_uint32_t fileid; + grub_uint32_t start; +}; + +struct grub_hfsplus_key_internal +{ + union + { + struct grub_hfsplus_extkey_internal extkey; + struct grub_hfsplus_catkey_internal catkey; + }; +}; + + + +struct grub_fshelp_node +{ + struct grub_hfsplus_data *data; + struct grub_hfsplus_extent extents[8]; + grub_uint64_t size; + grub_uint32_t fileid; + grub_int32_t mtime; +}; + +struct grub_hfsplus_btree +{ + grub_uint32_t root; + int nodesize; + + /* Catalog file node. */ + struct grub_fshelp_node file; +}; + +/* Information about a "mounted" HFS+ filesystem. */ +struct grub_hfsplus_data +{ + struct grub_hfsplus_volheader volheader; + grub_disk_t disk; + + unsigned int log2blksize; + + struct grub_hfsplus_btree catalog_tree; + struct grub_hfsplus_btree extoverflow_tree; + + struct grub_fshelp_node dirroot; + struct grub_fshelp_node opened_file; + + /* This is the offset into the physical disk for an embedded HFS+ + filesystem (one inside a plain HFS wrapper). */ + int embedded_offset; + int case_sensitive; +}; + +static grub_dl_t my_mod; + + +/* Return the offset of the record with the index INDEX, in the node + NODE which is part of the B+ tree BTREE. */ +static inline unsigned int +grub_hfsplus_btree_recoffset (struct grub_hfsplus_btree *btree, + struct grub_hfsplus_btnode *node, int index) +{ + char *cnode = (char *) node; + grub_uint16_t *recptr; + recptr = (grub_uint16_t *) (&cnode[btree->nodesize + - index * sizeof (grub_uint16_t) - 2]); + return grub_be_to_cpu16 (*recptr); +} + +/* Return a pointer to the record with the index INDEX, in the node + NODE which is part of the B+ tree BTREE. */ +static inline struct grub_hfsplus_key * +grub_hfsplus_btree_recptr (struct grub_hfsplus_btree *btree, + struct grub_hfsplus_btnode *node, int index) +{ + char *cnode = (char *) node; + unsigned int offset; + offset = grub_hfsplus_btree_recoffset (btree, node, index); + return (struct grub_hfsplus_key *) &cnode[offset]; +} + + +/* Find the extent that points to FILEBLOCK. If it is not in one of + the 8 extents described by EXTENT, return -1. In that case set + FILEBLOCK to the next block. */ +static int +grub_hfsplus_find_block (struct grub_hfsplus_extent *extent, + int *fileblock) +{ + int i; + grub_size_t blksleft = *fileblock; + + /* First lookup the file in the given extents. */ + for (i = 0; i < 8; i++) + { + if (blksleft < grub_be_to_cpu32 (extent[i].count)) + return grub_be_to_cpu32 (extent[i].start) + blksleft; + blksleft -= grub_be_to_cpu32 (extent[i].count); + } + + *fileblock = blksleft; + return -1; +} + +static grub_err_t +grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree, + struct grub_hfsplus_key_internal *key, + int (*compare_keys) (struct grub_hfsplus_key *keya, + struct grub_hfsplus_key_internal *keyb), + struct grub_hfsplus_btnode **matchnode, int *keyoffset); + +static int grub_hfsplus_cmp_extkey (struct grub_hfsplus_key *keya, + struct grub_hfsplus_key_internal *keyb); + +/* Search for the block FILEBLOCK inside the file NODE. Return the + blocknumber of this block on disk. */ +static grub_disk_addr_t +grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + struct grub_hfsplus_btnode *nnode = 0; + int blksleft = fileblock; + struct grub_hfsplus_extent *extents = &node->extents[0]; + + while (1) + { + struct grub_hfsplus_extkey *key; + struct grub_hfsplus_extkey_internal extoverflow; + int blk; + int ptr; + + /* Try to find this block in the current set of extents. */ + blk = grub_hfsplus_find_block (extents, &blksleft); + + /* The previous iteration of this loop allocated memory. The + code above used this memory, it can be freed now. */ + grub_free (nnode); + nnode = 0; + + if (blk != -1) + return (blk + + (node->data->embedded_offset >> (node->data->log2blksize + - GRUB_DISK_SECTOR_BITS))); + + /* For the extent overflow file, extra extents can't be found in + the extent overflow file. If this happens, you found a + bug... */ + if (node->fileid == GRUB_HFSPLUS_FILEID_OVERFLOW) + { + grub_error (GRUB_ERR_READ_ERROR, + "extra extents found in an extend overflow file"); + break; + } + + /* Set up the key to look for in the extent overflow file. */ + extoverflow.fileid = node->fileid; + extoverflow.start = fileblock - blksleft; + + if (grub_hfsplus_btree_search (&node->data->extoverflow_tree, + (struct grub_hfsplus_key_internal *) &extoverflow, + grub_hfsplus_cmp_extkey, &nnode, &ptr)) + { + grub_error (GRUB_ERR_READ_ERROR, + "no block found for the file id 0x%x and the block offset 0x%x", + node->fileid, fileblock); + break; + } + + /* The extent overflow file has 8 extents right after the key. */ + key = (struct grub_hfsplus_extkey *) + grub_hfsplus_btree_recptr (&node->data->extoverflow_tree, nnode, ptr); + extents = (struct grub_hfsplus_extent *) (key + 1); + + /* The block wasn't found. Perhaps the next iteration will find + it. The last block we found is stored in BLKSLEFT now. */ + } + + grub_free (nnode); + + /* Too bad, you lose. */ + return -1; +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_hfsplus_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_hfsplus_read_block, + node->size, + node->data->log2blksize - GRUB_DISK_SECTOR_BITS); +} + +static struct grub_hfsplus_data * +grub_hfsplus_mount (grub_disk_t disk) +{ + struct grub_hfsplus_data *data; + struct grub_hfsplus_btheader header; + struct grub_hfsplus_btnode node; + grub_uint16_t magic; + union { + struct grub_hfs_sblock hfs; + struct grub_hfsplus_volheader hfsplus; + } volheader; + + data = grub_malloc (sizeof (*data)); + if (!data) + return 0; + + data->disk = disk; + + /* Read the bootblock. */ + grub_disk_read (disk, GRUB_HFSPLUS_SBLOCK, 0, sizeof (volheader), + &volheader); + if (grub_errno) + goto fail; + + data->embedded_offset = 0; + if (grub_be_to_cpu16 (volheader.hfs.magic) == GRUB_HFS_MAGIC) + { + int extent_start; + int ablk_size; + int ablk_start; + + /* See if there's an embedded HFS+ filesystem. */ + if (grub_be_to_cpu16 (volheader.hfs.embed_sig) != GRUB_HFSPLUS_MAGIC) + { + grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem"); + goto fail; + } + + /* Calculate the offset needed to translate HFS+ sector numbers. */ + extent_start = grub_be_to_cpu16 (volheader.hfs.embed_extent.first_block); + ablk_size = grub_be_to_cpu32 (volheader.hfs.blksz); + ablk_start = grub_be_to_cpu16 (volheader.hfs.first_block); + data->embedded_offset = (ablk_start + + extent_start + * (ablk_size >> GRUB_DISK_SECTOR_BITS)); + + grub_disk_read (disk, data->embedded_offset + GRUB_HFSPLUS_SBLOCK, 0, + sizeof (volheader), &volheader); + if (grub_errno) + goto fail; + } + + /* Make sure this is an HFS+ filesystem. XXX: Do we really support + HFX? */ + magic = grub_be_to_cpu16 (volheader.hfsplus.magic); + if ((magic != GRUB_HFSPLUS_MAGIC) && (magic != GRUB_HFSPLUSX_MAGIC)) + { + grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem"); + goto fail; + } + + grub_memcpy (&data->volheader, &volheader.hfsplus, + sizeof (volheader.hfsplus)); + + if (grub_fshelp_log2blksize (grub_be_to_cpu32 (data->volheader.blksize), + &data->log2blksize)) + goto fail; + + /* Make a new node for the catalog tree. */ + data->catalog_tree.file.data = data; + data->catalog_tree.file.fileid = GRUB_HFSPLUS_FILEID_CATALOG; + grub_memcpy (&data->catalog_tree.file.extents, + data->volheader.catalog_file.extents, + sizeof data->volheader.catalog_file.extents); + data->catalog_tree.file.size = + grub_be_to_cpu64 (data->volheader.catalog_file.size); + + /* Make a new node for the extent overflow file. */ + data->extoverflow_tree.file.data = data; + data->extoverflow_tree.file.fileid = GRUB_HFSPLUS_FILEID_OVERFLOW; + grub_memcpy (&data->extoverflow_tree.file.extents, + data->volheader.extents_file.extents, + sizeof data->volheader.catalog_file.extents); + + data->extoverflow_tree.file.size = + grub_be_to_cpu64 (data->volheader.extents_file.size); + + /* Read the essential information about the trees. */ + if (grub_hfsplus_read_file (&data->catalog_tree.file, 0, + sizeof (struct grub_hfsplus_btnode), + sizeof (header), (char *) &header) <= 0) + goto fail; + + data->catalog_tree.root = grub_be_to_cpu32 (header.root); + data->catalog_tree.nodesize = grub_be_to_cpu16 (header.nodesize); + data->case_sensitive = ((magic == GRUB_HFSPLUSX_MAGIC) && + (header.key_compare == GRUB_HFSPLUSX_BINARYCOMPARE)); + + if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0, + sizeof (struct grub_hfsplus_btnode), + sizeof (header), (char *) &header) <= 0) + goto fail; + + data->extoverflow_tree.root = grub_be_to_cpu32 (header.root); + + if (grub_hfsplus_read_file (&data->extoverflow_tree.file, 0, 0, + sizeof (node), (char *) &node) <= 0) + goto fail; + + data->extoverflow_tree.root = grub_be_to_cpu32 (header.root); + data->extoverflow_tree.nodesize = grub_be_to_cpu16 (header.nodesize); + + data->dirroot.data = data; + data->dirroot.fileid = GRUB_HFSPLUS_FILEID_ROOTDIR; + + return data; + + fail: + + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem"); + + grub_free (data); + return 0; +} + +/* Compare the on disk catalog key KEYA with the catalog key we are + looking for (KEYB). */ +static int +grub_hfsplus_cmp_catkey (struct grub_hfsplus_key *keya, + struct grub_hfsplus_key_internal *keyb) +{ + struct grub_hfsplus_catkey *catkey_a = &keya->catkey; + struct grub_hfsplus_catkey_internal *catkey_b = &keyb->catkey; + char *filename; + int i; + int diff; + + /* Safe unsigned comparison */ + grub_uint32_t aparent = grub_be_to_cpu32 (catkey_a->parent); + if (aparent > catkey_b->parent) + return 1; + if (aparent < catkey_b->parent) + return -1; + + /* Change the filename in keya so the endianness is correct. */ + for (i = 0; i < grub_be_to_cpu16 (catkey_a->namelen); i++) + catkey_a->name[i] = grub_be_to_cpu16 (catkey_a->name[i]); + + filename = grub_malloc (grub_be_to_cpu16 (catkey_a->namelen) + 1); + + if (! grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey_a->name, + grub_be_to_cpu16 (catkey_a->namelen))) + return -1; /* XXX: This error never occurs, but in case it happens + just skip this entry. */ + + diff = grub_strncmp (filename, catkey_b->name, + grub_be_to_cpu16 (catkey_a->namelen)); + + grub_free (filename); + + /* The endianness was changed to host format, change it back to + whatever it was. */ + for (i = 0; i < grub_be_to_cpu16 (catkey_a->namelen); i++) + catkey_a->name[i] = grub_cpu_to_be16 (catkey_a->name[i]); + return diff; +} + +/* Compare the on disk extent overflow key KEYA with the extent + overflow key we are looking for (KEYB). */ +static int +grub_hfsplus_cmp_extkey (struct grub_hfsplus_key *keya, + struct grub_hfsplus_key_internal *keyb) +{ + struct grub_hfsplus_extkey *extkey_a = &keya->extkey; + struct grub_hfsplus_extkey_internal *extkey_b = &keyb->extkey; + grub_uint32_t akey; + + /* Safe unsigned comparison */ + akey = grub_be_to_cpu32 (extkey_a->fileid); + if (akey > extkey_b->fileid) + return 1; + if (akey < extkey_b->fileid) + return -1; + + akey = grub_be_to_cpu32 (extkey_a->start); + if (akey > extkey_b->start) + return 1; + if (akey < extkey_b->start) + return -1; + return 0; +} + +static char * +grub_hfsplus_read_symlink (grub_fshelp_node_t node) +{ + char *symlink; + grub_ssize_t numread; + + symlink = grub_malloc (node->size + 1); + if (!symlink) + return 0; + + numread = grub_hfsplus_read_file (node, 0, 0, node->size, symlink); + if (numread != (grub_ssize_t) node->size) + { + grub_free (symlink); + return 0; + } + symlink[node->size] = '\0'; + + return symlink; +} + +static int +grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree, + struct grub_hfsplus_btnode *first_node, + int first_rec, + int (*hook) (void *record)) +{ + int rec; + + for (;;) + { + char *cnode = (char *) first_node; + + /* Iterate over all records in this node. */ + for (rec = first_rec; rec < grub_be_to_cpu16 (first_node->count); rec++) + { + if (hook (grub_hfsplus_btree_recptr (btree, first_node, rec))) + return 1; + } + + if (! first_node->next) + break; + + if (grub_hfsplus_read_file (&btree->file, 0, + (grub_be_to_cpu32 (first_node->next) + * btree->nodesize), + btree->nodesize, cnode) <= 0) + return 1; + + /* Don't skip any record in the next iteration. */ + first_rec = 0; + } + + return 0; +} + +/* Lookup the node described by KEY in the B+ Tree BTREE. Compare + keys using the function COMPARE_KEYS. When a match is found, + return the node in MATCHNODE and a pointer to the data in this node + in KEYOFFSET. MATCHNODE should be freed by the caller. */ +static grub_err_t +grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree, + struct grub_hfsplus_key_internal *key, + int (*compare_keys) (struct grub_hfsplus_key *keya, + struct grub_hfsplus_key_internal *keyb), + struct grub_hfsplus_btnode **matchnode, int *keyoffset) +{ + grub_uint64_t currnode; + char *node; + struct grub_hfsplus_btnode *nodedesc; + int rec; + + node = grub_malloc (btree->nodesize); + if (! node) + return grub_errno; + + currnode = btree->root; + while (1) + { + int match = 0; + + /* Read a node. */ + if (grub_hfsplus_read_file (&btree->file, 0, + (long)currnode * (long)btree->nodesize, + btree->nodesize, (char *) node) <= 0) + { + grub_free (node); + return grub_error (GRUB_ERR_BAD_FS, "couldn't read i-node"); + } + + nodedesc = (struct grub_hfsplus_btnode *) node; + + /* Find the record in this tree. */ + for (rec = 0; rec < grub_be_to_cpu16 (nodedesc->count); rec++) + { + struct grub_hfsplus_key *currkey; + currkey = grub_hfsplus_btree_recptr (btree, nodedesc, rec); + + /* The action that has to be taken depend on the type of + record. */ + if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_LEAF + && compare_keys (currkey, key) == 0) + { + /* An exact match was found! */ + + *matchnode = nodedesc; + *keyoffset = rec; + + return 0; + } + else if (nodedesc->type == GRUB_HFSPLUS_BTNODE_TYPE_INDEX) + { + grub_uint32_t *pointer; + + /* The place where the key could have been found didn't + contain the key. This means that the previous match + is the one that should be followed. */ + if (compare_keys (currkey, key) > 0) + break; + + /* Mark the last key which is lower or equal to the key + that we are looking for. The last match that is + found will be used to locate the child which can + contain the record. */ + pointer = (grub_uint32_t *) ((char *) currkey + + grub_be_to_cpu16 (currkey->keylen) + + 2); + currnode = grub_be_to_cpu32 (*pointer); + match = 1; + } + } + + /* No match is found, no record with this key exists in the + tree. */ + if (! match) + { + *matchnode = 0; + grub_free (node); + return 1; + } + } +} + +static int +grub_hfsplus_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + int ret = 0; + + auto int list_nodes (void *record); + int list_nodes (void *record) + { + struct grub_hfsplus_catkey *catkey; + char *filename; + int i; + struct grub_fshelp_node *node; + struct grub_hfsplus_catfile *fileinfo; + enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN; + + catkey = (struct grub_hfsplus_catkey *) record; + + fileinfo = + (struct grub_hfsplus_catfile *) ((char *) record + + grub_be_to_cpu16 (catkey->keylen) + + 2 + (grub_be_to_cpu16(catkey->keylen) + % 2)); + + /* Stop iterating when the last directory entry is found. */ + if (grub_be_to_cpu32 (catkey->parent) != dir->fileid) + return 1; + + /* Determine the type of the node that is found. */ + if (grub_be_to_cpu16 (fileinfo->type) == GRUB_HFSPLUS_FILETYPE_REG) + { + int mode = (grub_be_to_cpu16 (fileinfo->mode) + & GRUB_HFSPLUS_FILEMODE_MASK); + + if (mode == GRUB_HFSPLUS_FILEMODE_REG) + type = GRUB_FSHELP_REG; + else if (mode == GRUB_HFSPLUS_FILEMODE_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else + type = GRUB_FSHELP_UNKNOWN; + } + else if (grub_be_to_cpu16 (fileinfo->type) == GRUB_HFSPLUS_FILETYPE_DIR) + type = GRUB_FSHELP_DIR; + + if (type == GRUB_FSHELP_UNKNOWN) + return 0; + + /* Make sure the byte order of the UTF16 string is correct. */ + for (i = 0; i < grub_be_to_cpu16 (catkey->namelen); i++) + { + catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]); + + /* If the name is obviously invalid, skip this node. */ + if (catkey->name[i] == 0) + return 0; + } + + filename = grub_malloc (grub_be_to_cpu16 (catkey->namelen) + 1); + if (! filename) + return 0; + + if (! grub_utf16_to_utf8 ((grub_uint8_t *) filename, catkey->name, + grub_be_to_cpu16 (catkey->namelen))) + { + grub_free (filename); + return 0; + } + + filename[grub_be_to_cpu16 (catkey->namelen)] = '\0'; + + /* Restore the byte order to what it was previously. */ + for (i = 0; i < grub_be_to_cpu16 (catkey->namelen); i++) + catkey->name[i] = grub_be_to_cpu16 (catkey->name[i]); + + /* hfs+ is case insensitive. */ + if (! dir->data->case_sensitive) + type |= GRUB_FSHELP_CASE_INSENSITIVE; + + /* Only accept valid nodes. */ + if (grub_strlen (filename) == grub_be_to_cpu16 (catkey->namelen)) + { + /* A valid node is found; setup the node and call the + callback function. */ + node = grub_malloc (sizeof (*node)); + node->data = dir->data; + + grub_memcpy (node->extents, fileinfo->data.extents, + sizeof (node->extents)); + node->mtime = grub_be_to_cpu32 (fileinfo->mtime) - 2082844800; + node->size = grub_be_to_cpu64 (fileinfo->data.size); + node->fileid = grub_be_to_cpu32 (fileinfo->fileid); + + ret = hook (filename, type, node); + } + + grub_free (filename); + + return ret; + } + + struct grub_hfsplus_key_internal intern; + struct grub_hfsplus_btnode *node; + int ptr; + + /* Create a key that points to the first entry in the directory. */ + intern.catkey.parent = dir->fileid; + intern.catkey.name = ""; + + /* First lookup the first entry. */ + if (grub_hfsplus_btree_search (&dir->data->catalog_tree, &intern, + grub_hfsplus_cmp_catkey, &node, &ptr)) + return 0; + + /* Iterate over all entries in this directory. */ + grub_hfsplus_btree_iterate_node (&dir->data->catalog_tree, node, ptr, + list_nodes); + + grub_free (node); + + return ret; +} + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_hfsplus_open (struct grub_file *file, const char *name) +{ + struct grub_hfsplus_data *data; + struct grub_fshelp_node *fdiro = 0; + + grub_dl_ref (my_mod); + + data = grub_hfsplus_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->dirroot, &fdiro, + grub_hfsplus_iterate_dir, + grub_hfsplus_read_symlink, GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + file->size = fdiro->size; + data->opened_file = *fdiro; + grub_free (fdiro); + + file->data = data; + file->offset = 0; + + return 0; + + fail: + if (data && fdiro != &data->dirroot) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_err_t +grub_hfsplus_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + + +/* Read LEN bytes data from FILE into BUF. */ +static grub_ssize_t +grub_hfsplus_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_hfsplus_data *data = + (struct grub_hfsplus_data *) file->data; + + int size = grub_hfsplus_read_file (&data->opened_file, file->read_hook, + file->offset, len, buf); + + return size; +} + + +static grub_err_t +grub_hfsplus_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_hfsplus_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + info.mtimeset = 1; + info.mtime = node->mtime; + info.case_insensitive = !! (filetype & GRUB_FSHELP_CASE_INSENSITIVE); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_hfsplus_mount (device->disk); + if (!data) + goto fail; + + /* Find the directory that should be opened. */ + grub_fshelp_find_file (path, &data->dirroot, &fdiro, + grub_hfsplus_iterate_dir, + grub_hfsplus_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + /* Iterate over all entries in this directory. */ + grub_hfsplus_iterate_dir (fdiro, iterate); + + fail: + if (data && fdiro != &data->dirroot) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_err_t +grub_hfsplus_label (grub_device_t device __attribute__((unused)) + , char **label __attribute__((unused))) +{ + /* XXX: It's not documented how to read a label. */ + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "reading the label of a HFS+ " + "partition is not implemented"); +} + +/* Get mtime. */ +static grub_err_t +grub_hfsplus_mtime (grub_device_t device, grub_int32_t *tm) +{ + struct grub_hfsplus_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_hfsplus_mount (disk); + if (!data) + *tm = 0; + else + *tm = grub_be_to_cpu32 (data->volheader.utime) - 2082844800; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; + +} + +static grub_err_t +grub_hfsplus_uuid (grub_device_t device, char **uuid) +{ + struct grub_hfsplus_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_hfsplus_mount (disk); + if (data) + { + *uuid = grub_xasprintf ("%016llx", + (unsigned long long) + grub_be_to_cpu64 (data->volheader.num_serial)); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + + +static struct grub_fs grub_hfsplus_fs = + { + .name = "hfsplus", + .dir = grub_hfsplus_dir, + .open = grub_hfsplus_open, + .read = grub_hfsplus_read, + .close = grub_hfsplus_close, + .label = grub_hfsplus_label, + .mtime = grub_hfsplus_mtime, + .uuid = grub_hfsplus_uuid, +#ifdef GRUB_UTIL + .reserved_first_sector = 1, +#endif + .next = 0 + }; + +GRUB_MOD_INIT(hfsplus) +{ + grub_fs_register (&grub_hfsplus_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(hfsplus) +{ + grub_fs_unregister (&grub_hfsplus_fs); +} diff --git a/libr/fs/p/grub/fs/iso9660.c b/libr/fs/p/grub/fs/iso9660.c new file mode 100644 index 0000000000..6dc465f256 --- /dev/null +++ b/libr/fs/p/grub/fs/iso9660.c @@ -0,0 +1,897 @@ +/* iso9660.c - iso9660 implementation with extensions: + SUSP, Rock Ridge. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_ISO9660_FSTYPE_DIR 0040000 +#define GRUB_ISO9660_FSTYPE_REG 0100000 +#define GRUB_ISO9660_FSTYPE_SYMLINK 0120000 +#define GRUB_ISO9660_FSTYPE_MASK 0170000 + +#define GRUB_ISO9660_LOG2_BLKSZ 2 +#define GRUB_ISO9660_BLKSZ 2048 + +#define GRUB_ISO9660_RR_DOT 2 +#define GRUB_ISO9660_RR_DOTDOT 4 + +#define GRUB_ISO9660_VOLDESC_BOOT 0 +#define GRUB_ISO9660_VOLDESC_PRIMARY 1 +#define GRUB_ISO9660_VOLDESC_SUPP 2 +#define GRUB_ISO9660_VOLDESC_PART 3 +#define GRUB_ISO9660_VOLDESC_END 255 + +/* The head of a volume descriptor. */ +struct grub_iso9660_voldesc +{ + grub_uint8_t type; + grub_uint8_t magic[5]; + grub_uint8_t version; +} __attribute__ ((packed)); + +/* A directory entry. */ +struct grub_iso9660_dir +{ + grub_uint8_t len; + grub_uint8_t ext_sectors; + grub_uint32_t first_sector; + grub_uint32_t first_sector_be; + grub_uint32_t size; + grub_uint32_t size_be; + grub_uint8_t unused1[7]; + grub_uint8_t flags; + grub_uint8_t unused2[6]; + grub_uint8_t namelen; +} __attribute__ ((packed)); + +struct grub_iso9660_date +{ + grub_uint8_t year[4]; + grub_uint8_t month[2]; + grub_uint8_t day[2]; + grub_uint8_t hour[2]; + grub_uint8_t minute[2]; + grub_uint8_t second[2]; + grub_uint8_t hundredth[2]; + grub_uint8_t offset; +} __attribute__ ((packed)); + +/* The primary volume descriptor. Only little endian is used. */ +struct grub_iso9660_primary_voldesc +{ + struct grub_iso9660_voldesc voldesc; + grub_uint8_t unused1[33]; + grub_uint8_t volname[32]; + grub_uint8_t unused2[16]; + grub_uint8_t escape[32]; + grub_uint8_t unused3[12]; + grub_uint32_t path_table_size; + grub_uint8_t unused4[4]; + grub_uint32_t path_table; + grub_uint8_t unused5[12]; + struct grub_iso9660_dir rootdir; + grub_uint8_t unused6[624]; + struct grub_iso9660_date created; + struct grub_iso9660_date modified; +} __attribute__ ((packed)); + +/* A single entry in the path table. */ +struct grub_iso9660_path +{ + grub_uint8_t len; + grub_uint8_t sectors; + grub_uint32_t first_sector; + grub_uint16_t parentdir; + grub_uint8_t name[0]; +} __attribute__ ((packed)); + +/* An entry in the System Usage area of the directory entry. */ +struct grub_iso9660_susp_entry +{ + grub_uint8_t sig[2]; + grub_uint8_t len; + grub_uint8_t version; + grub_uint8_t data[0]; +} __attribute__ ((packed)); + +/* The CE entry. This is used to describe the next block where data + can be found. */ +struct grub_iso9660_susp_ce +{ + struct grub_iso9660_susp_entry entry; + grub_uint32_t blk; + grub_uint32_t blk_be; + grub_uint32_t off; + grub_uint32_t off_be; + grub_uint32_t len; + grub_uint32_t len_be; +} __attribute__ ((packed)); + +struct grub_iso9660_data +{ + struct grub_iso9660_primary_voldesc voldesc; + grub_disk_t disk; + unsigned int first_sector; + int rockridge; + int susp_skip; + int joliet; +}; + +struct grub_fshelp_node +{ + struct grub_iso9660_data *data; + unsigned int size; + unsigned int blk; + unsigned int dir_blk; + unsigned int dir_off; +}; + +static grub_dl_t my_mod; + + +/* Iterate over the susp entries, starting with block SUA_BLOCK on the + offset SUA_POS with a size of SUA_SIZE bytes. Hook is called for + every entry. */ +static grub_err_t +grub_iso9660_susp_iterate (struct grub_iso9660_data *data, + int sua_block, int sua_pos, int sua_size, + grub_err_t (*hook) + (struct grub_iso9660_susp_entry *entry)) +{ + char *sua; + struct grub_iso9660_susp_entry *entry; + + auto grub_err_t load_sua (void); + + /* Load a part of the System Usage Area. */ + grub_err_t load_sua (void) + { + sua = grub_malloc (sua_size); + if (!sua) + return grub_errno; + + if (grub_disk_read (data->disk, sua_block, sua_pos, + sua_size, sua)) + return grub_errno; + + entry = (struct grub_iso9660_susp_entry *) sua; + return 0; + } + + if (load_sua ()) + return grub_errno; + + for (; (char *) entry < (char *) sua + sua_size - 1; + entry = (struct grub_iso9660_susp_entry *) + ((char *) entry + entry->len)) + { + /* The last entry. */ + if (grub_strncmp ((char *) entry->sig, "ST", 2) == 0) + break; + + /* Additional entries are stored elsewhere. */ + if (grub_strncmp ((char *) entry->sig, "CE", 2) == 0) + { + struct grub_iso9660_susp_ce *ce; + + ce = (struct grub_iso9660_susp_ce *) entry; + sua_size = grub_le_to_cpu32 (ce->len); + sua_pos = grub_le_to_cpu32 (ce->off); + sua_block = grub_le_to_cpu32 (ce->blk) << GRUB_ISO9660_LOG2_BLKSZ; + + grub_free (sua); + if (load_sua ()) + return grub_errno; + } + + if (hook (entry)) + { + grub_free (sua); + return 0; + } + } + + grub_free (sua); + return 0; +} + +static char * +grub_iso9660_convert_string (grub_uint16_t *us, int len) +{ + char *p; + int i; + + p = grub_malloc (len * 4 + 1); + if (! p) + return p; + + for (i=0; isig, "ER", 2) == 0) + { + data->rockridge = 1; + return 1; + } + return 0; + } + + data = grub_zalloc (sizeof (struct grub_iso9660_data)); + if (! data) + return 0; + + data->disk = disk; + + block = 16; + do + { + int copy_voldesc = 0; + + /* Read the superblock. */ + if (grub_disk_read (disk, block << GRUB_ISO9660_LOG2_BLKSZ, 0, + sizeof (struct grub_iso9660_primary_voldesc), + (char *) &voldesc)) + { + grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem"); + goto fail; + } + + if (grub_strncmp ((char *) voldesc.voldesc.magic, "CD001", 5) != 0) + { + grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem"); + goto fail; + } + + if (voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_PRIMARY) + copy_voldesc = 1; + else if ((voldesc.voldesc.type == GRUB_ISO9660_VOLDESC_SUPP) && + (voldesc.escape[0] == 0x25) && (voldesc.escape[1] == 0x2f) && + ((voldesc.escape[2] == 0x40) || /* UCS-2 Level 1. */ + (voldesc.escape[2] == 0x43) || /* UCS-2 Level 2. */ + (voldesc.escape[2] == 0x45))) /* UCS-2 Level 3. */ + { + copy_voldesc = 1; + data->joliet = 1; + } + + if (copy_voldesc) + grub_memcpy((char *) &data->voldesc, (char *) &voldesc, + sizeof (struct grub_iso9660_primary_voldesc)); + + block++; + } while (voldesc.voldesc.type != GRUB_ISO9660_VOLDESC_END); + + /* Read the system use area and test it to see if SUSP is + supported. */ + if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector) + << GRUB_ISO9660_LOG2_BLKSZ), 0, + sizeof (rootdir), (char *) &rootdir)) + { + grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem"); + goto fail; + } + + sua_pos = (sizeof (rootdir) + rootdir.namelen + + (rootdir.namelen % 2) - 1); + sua_size = rootdir.len - sua_pos; + + sua = grub_malloc (sua_size); + if (! sua) + goto fail; + + if (grub_disk_read (disk, (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector) + << GRUB_ISO9660_LOG2_BLKSZ), sua_pos, + sua_size, sua)) + { + grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem"); + goto fail; + } + + entry = (struct grub_iso9660_susp_entry *) sua; + + /* Test if the SUSP protocol is used on this filesystem. */ + if (grub_strncmp ((char *) entry->sig, "SP", 2) == 0) + { + /* The 2nd data byte stored how many bytes are skipped every time + to get to the SUA (System Usage Area). */ + data->susp_skip = entry->data[2]; + entry = (struct grub_iso9660_susp_entry *) ((char *) entry + entry->len); + + /* Iterate over the entries in the SUA area to detect + extensions. */ + if (grub_iso9660_susp_iterate (data, + (grub_le_to_cpu32 (data->voldesc.rootdir.first_sector) + << GRUB_ISO9660_LOG2_BLKSZ), + sua_pos, sua_size, susp_iterate)) + goto fail; + } + + return data; + + fail: + grub_free (data); + return 0; +} + + +static char * +grub_iso9660_read_symlink (grub_fshelp_node_t node) +{ + struct grub_iso9660_dir dirent; + int sua_off; + int sua_size; + char *symlink = 0; + int addslash = 0; + + auto void add_part (const char *part, int len); + auto grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *); + + /* Extend the symlink. */ + void add_part (const char *part, int len) + { + int size = grub_strlen (symlink); + + symlink = grub_realloc (symlink, size + len + 1); + if (! symlink) + return; + + grub_strncat (symlink, part, len); + } + + /* Read in a symlink. */ + grub_err_t susp_iterate_sl (struct grub_iso9660_susp_entry *entry) + { + if (grub_strncmp ("SL", (char *) entry->sig, 2) == 0) + { + unsigned int pos = 1; + + /* The symlink is not stored as a POSIX symlink, translate it. */ + while (pos < grub_le_to_cpu32 (entry->len)) + { + if (addslash) + { + add_part ("/", 1); + addslash = 0; + } + + /* The current position is the `Component Flag'. */ + switch (entry->data[pos] & 30) + { + case 0: + { + /* The data on pos + 2 is the actual data, pos + 1 + is the length. Both are part of the `Component + Record'. */ + add_part ((char *) &entry->data[pos + 2], + entry->data[pos + 1]); + if ((entry->data[pos] & 1)) + addslash = 1; + + break; + } + + case 2: + add_part ("./", 2); + break; + + case 4: + add_part ("../", 3); + break; + + case 8: + add_part ("/", 1); + break; + } + /* In pos + 1 the length of the `Component Record' is + stored. */ + pos += entry->data[pos + 1] + 2; + } + + /* Check if `grub_realloc' failed. */ + if (grub_errno) + return grub_errno; + } + + return 0; + } + + if (grub_disk_read (node->data->disk, node->dir_blk, node->dir_off, + sizeof (dirent), (char *) &dirent)) + return 0; + + sua_off = (sizeof (dirent) + dirent.namelen + 1 - (dirent.namelen % 2) + + node->data->susp_skip); + sua_size = dirent.len - sua_off; + + symlink = grub_malloc (1); + if (!symlink) + return 0; + + *symlink = '\0'; + + if (grub_iso9660_susp_iterate (node->data, node->dir_blk, + node->dir_off + sua_off, + sua_size, susp_iterate_sl)) + { + grub_free (symlink); + return 0; + } + + return symlink; +} + + +static int +grub_iso9660_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + struct grub_iso9660_dir dirent; + unsigned int offset = 0; + char *filename; + int filename_alloc = 0; + enum grub_fshelp_filetype type; + + auto grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *); + + grub_err_t susp_iterate_dir (struct grub_iso9660_susp_entry *entry) + { + /* The filename in the rock ridge entry. */ + if (grub_strncmp ("NM", (char *) entry->sig, 2) == 0) + { + /* The flags are stored at the data position 0, here the + filename type is stored. */ + if (entry->data[0] & GRUB_ISO9660_RR_DOT) + filename = "."; + else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT) + filename = ".."; + else + { + int size = 1; + if (filename) + { + size += grub_strlen (filename); + grub_realloc (filename, + grub_strlen (filename) + + entry->len); + } + else + { + size = entry->len - 5; + filename = grub_zalloc (size + 1); + } + filename_alloc = 1; + grub_strncpy (filename, (char *) &entry->data[1], size); + filename[size] = '\0'; + } + } + /* The mode information (st_mode). */ + else if (grub_strncmp ((char *) entry->sig, "PX", 2) == 0) + { + /* At position 0 of the PX record the st_mode information is + stored (little-endian). */ + grub_uint32_t mode = ((entry->data[0] + (entry->data[1] << 8)) + & GRUB_ISO9660_FSTYPE_MASK); + + switch (mode) + { + case GRUB_ISO9660_FSTYPE_DIR: + type = GRUB_FSHELP_DIR; + break; + case GRUB_ISO9660_FSTYPE_REG: + type = GRUB_FSHELP_REG; + break; + case GRUB_ISO9660_FSTYPE_SYMLINK: + type = GRUB_FSHELP_SYMLINK; + break; + default: + type = GRUB_FSHELP_UNKNOWN; + } + } + + return 0; + } + + while (offset < dir->size) + { + if (grub_disk_read (dir->data->disk, + (dir->blk << GRUB_ISO9660_LOG2_BLKSZ) + + offset / GRUB_DISK_SECTOR_SIZE, + offset % GRUB_DISK_SECTOR_SIZE, + sizeof (dirent), (char *) &dirent)) + return 0; + + /* The end of the block, skip to the next one. */ + if (!dirent.len) + { + offset = (offset / GRUB_ISO9660_BLKSZ + 1) * GRUB_ISO9660_BLKSZ; + continue; + } + + { + char name[dirent.namelen + 1]; + int nameoffset = offset + sizeof (dirent); + struct grub_fshelp_node *node; + int sua_off = (sizeof (dirent) + dirent.namelen + 1 + - (dirent.namelen % 2)); + int sua_size = dirent.len - sua_off; + + sua_off += offset + dir->data->susp_skip; + + filename = 0; + filename_alloc = 0; + type = GRUB_FSHELP_UNKNOWN; + + if (dir->data->rockridge + && grub_iso9660_susp_iterate (dir->data, + (dir->blk << GRUB_ISO9660_LOG2_BLKSZ) + + (sua_off + / GRUB_DISK_SECTOR_SIZE), + sua_off % GRUB_DISK_SECTOR_SIZE, + sua_size, susp_iterate_dir)) + return 0; + + /* Read the name. */ + if (grub_disk_read (dir->data->disk, + (dir->blk << GRUB_ISO9660_LOG2_BLKSZ) + + nameoffset / GRUB_DISK_SECTOR_SIZE, + nameoffset % GRUB_DISK_SECTOR_SIZE, + dirent.namelen, (char *) name)) + return 0; + + node = grub_malloc (sizeof (struct grub_fshelp_node)); + if (!node) + return 0; + + /* Setup a new node. */ + node->data = dir->data; + node->size = grub_le_to_cpu32 (dirent.size); + node->blk = grub_le_to_cpu32 (dirent.first_sector); + node->dir_blk = ((dir->blk << GRUB_ISO9660_LOG2_BLKSZ) + + offset / GRUB_DISK_SECTOR_SIZE); + node->dir_off = offset % GRUB_DISK_SECTOR_SIZE; + + /* If the filetype was not stored using rockridge, use + whatever is stored in the iso9660 filesystem. */ + if (type == GRUB_FSHELP_UNKNOWN) + { + if ((dirent.flags & 3) == 2) + type = GRUB_FSHELP_DIR; + else + type = GRUB_FSHELP_REG; + } + + /* The filename was not stored in a rock ridge entry. Read it + from the iso9660 filesystem. */ + if (!filename) + { + name[dirent.namelen] = '\0'; + filename = grub_strrchr (name, ';'); + if (filename) + *filename = '\0'; + + if (dirent.namelen == 1 && name[0] == 0) + filename = "."; + else if (dirent.namelen == 1 && name[0] == 1) + filename = ".."; + else + filename = name; + } + + if (dir->data->joliet) + { + char *oldname, *semicolon; + + oldname = filename; + filename = grub_iso9660_convert_string + ((grub_uint16_t *) oldname, dirent.namelen >> 1); + + semicolon = grub_strrchr (filename, ';'); + if (semicolon) + *semicolon = '\0'; + + if (filename_alloc) + grub_free (oldname); + + filename_alloc = 1; + } + + if (hook (filename, type, node)) + { + if (filename_alloc) + grub_free (filename); + return 1; + } + if (filename_alloc) + grub_free (filename); + } + + offset += dirent.len; + } + + return 0; +} + + + +static grub_err_t +grub_iso9660_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_iso9660_data *data = 0; + struct grub_fshelp_node rootnode; + struct grub_fshelp_node *foundnode; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_iso9660_mount (device->disk); + if (! data) + goto fail; + + rootnode.data = data; + rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector); + rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size); + + /* Use the fshelp function to traverse the path. */ + if (grub_fshelp_find_file (path, &rootnode, + &foundnode, + grub_iso9660_iterate_dir, + grub_iso9660_read_symlink, + GRUB_FSHELP_DIR)) + goto fail; + + /* List the files in the directory. */ + grub_iso9660_iterate_dir (foundnode, iterate); + + if (foundnode != &rootnode) + grub_free (foundnode); + + fail: + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_iso9660_open (struct grub_file *file, const char *name) +{ + struct grub_iso9660_data *data; + struct grub_fshelp_node rootnode; + struct grub_fshelp_node *foundnode; + + grub_dl_ref (my_mod); + + data = grub_iso9660_mount (file->device->disk); + if (!data) + goto fail; + + rootnode.data = data; + rootnode.blk = grub_le_to_cpu32 (data->voldesc.rootdir.first_sector); + rootnode.size = grub_le_to_cpu32 (data->voldesc.rootdir.size); + + /* Use the fshelp function to traverse the path. */ + if (grub_fshelp_find_file (name, &rootnode, + &foundnode, + grub_iso9660_iterate_dir, + grub_iso9660_read_symlink, + GRUB_FSHELP_REG)) + goto fail; + + data->first_sector = foundnode->blk; + + file->data = data; + file->size = foundnode->size; + file->offset = 0; + + return 0; + + fail: + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + +static grub_ssize_t +grub_iso9660_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_iso9660_data *data = + (struct grub_iso9660_data *) file->data; + + /* XXX: The file is stored in as a single extent. */ + data->disk->read_hook = file->read_hook; + grub_disk_read (data->disk, + data->first_sector << GRUB_ISO9660_LOG2_BLKSZ, + file->offset, + len, buf); + data->disk->read_hook = NULL; + + if (grub_errno) + return -1; + + return len; +} + + +static grub_err_t +grub_iso9660_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + + +static grub_err_t +grub_iso9660_label (grub_device_t device, char **label) +{ + struct grub_iso9660_data *data; + data = grub_iso9660_mount (device->disk); + + if (data) + { + if (data->joliet) + *label = grub_iso9660_convert_string + ((grub_uint16_t *) &data->voldesc.volname, 16); + else + *label = grub_strndup ((char *) data->voldesc.volname, 32); + grub_free (data); + } + else + *label = 0; + + return grub_errno; +} + + +static grub_err_t +grub_iso9660_uuid (grub_device_t device, char **uuid) +{ + struct grub_iso9660_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_iso9660_mount (disk); + if (data) + { + if (! data->voldesc.modified.year[0] && ! data->voldesc.modified.year[1] + && ! data->voldesc.modified.year[2] && ! data->voldesc.modified.year[3] + && ! data->voldesc.modified.month[0] && ! data->voldesc.modified.month[1] + && ! data->voldesc.modified.day[0] && ! data->voldesc.modified.day[1] + && ! data->voldesc.modified.hour[0] && ! data->voldesc.modified.hour[1] + && ! data->voldesc.modified.minute[0] && ! data->voldesc.modified.minute[1] + && ! data->voldesc.modified.second[0] && ! data->voldesc.modified.second[1] + && ! data->voldesc.modified.hundredth[0] && ! data->voldesc.modified.hundredth[1]) + { + grub_error (GRUB_ERR_BAD_NUMBER, "no creation date in filesystem to generate UUID"); + *uuid = NULL; + } + else + { + *uuid = grub_xasprintf ("%c%c%c%c-%c%c-%c%c-%c%c-%c%c-%c%c-%c%c", + data->voldesc.modified.year[0], + data->voldesc.modified.year[1], + data->voldesc.modified.year[2], + data->voldesc.modified.year[3], + data->voldesc.modified.month[0], + data->voldesc.modified.month[1], + data->voldesc.modified.day[0], + data->voldesc.modified.day[1], + data->voldesc.modified.hour[0], + data->voldesc.modified.hour[1], + data->voldesc.modified.minute[0], + data->voldesc.modified.minute[1], + data->voldesc.modified.second[0], + data->voldesc.modified.second[1], + data->voldesc.modified.hundredth[0], + data->voldesc.modified.hundredth[1]); + } + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + + +static struct grub_fs grub_iso9660_fs = + { + .name = "iso9660", + .dir = grub_iso9660_dir, + .open = grub_iso9660_open, + .read = grub_iso9660_read, + .close = grub_iso9660_close, + .label = grub_iso9660_label, + .uuid = grub_iso9660_uuid, + .next = 0 + }; + +GRUB_MOD_INIT(iso9660) +{ + grub_fs_register (&grub_iso9660_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(iso9660) +{ + grub_fs_unregister (&grub_iso9660_fs); +} diff --git a/libr/fs/p/grub/fs/jfs.c b/libr/fs/p/grub/fs/jfs.c new file mode 100644 index 0000000000..76ef2e5400 --- /dev/null +++ b/libr/fs/p/grub/fs/jfs.c @@ -0,0 +1,899 @@ +/* jfs.c - JFS. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_JFS_MAX_SYMLNK_CNT 8 +#define GRUB_JFS_FILETYPE_MASK 0170000 +#define GRUB_JFS_FILETYPE_REG 0100000 +#define GRUB_JFS_FILETYPE_LNK 0120000 +#define GRUB_JFS_FILETYPE_DIR 0040000 + +#define GRUB_JFS_SBLOCK 64 +#define GRUB_JFS_AGGR_INODE 2 +#define GRUB_JFS_FS1_INODE_BLK 104 + +#define GRUB_JFS_TREE_LEAF 2 + +struct grub_jfs_sblock +{ + /* The magic for JFS. It should contain the string "JFS1". */ + grub_uint8_t magic[4]; + grub_uint32_t version; + grub_uint64_t ag_size; + + /* The size of a filesystem block in bytes. XXX: currently only + 4096 was tested. */ + grub_uint32_t blksz; + grub_uint16_t log2_blksz; + + grub_uint8_t unused[71]; + grub_uint8_t volname[11]; + grub_uint8_t unused2[32]; + grub_uint8_t uuid[16]; +}; + +struct grub_jfs_extent +{ + /* The length of the extent in filesystem blocks. */ + grub_uint16_t length; + grub_uint8_t length2; + + /* The physical offset of the first block on the disk. */ + grub_uint8_t blk1; + grub_uint32_t blk2; +} __attribute__ ((packed)); + +struct grub_jfs_iag +{ + grub_uint8_t unused[3072]; + struct grub_jfs_extent inodes[128]; +} __attribute__ ((packed)); + + +/* The head of the tree used to find extents. */ +struct grub_jfs_treehead +{ + grub_uint64_t next; + grub_uint64_t prev; + + grub_uint8_t flags; + grub_uint8_t unused; + + grub_uint16_t count; + grub_uint16_t max; + grub_uint8_t unused2[10]; +} __attribute__ ((packed)); + +/* A node in the extent tree. */ +struct grub_jfs_tree_extent +{ + grub_uint8_t flags; + grub_uint16_t unused; + + /* The offset is the key used to lookup an extent. */ + grub_uint8_t offset1; + grub_uint32_t offset2; + + struct grub_jfs_extent extent; +} __attribute__ ((packed)); + +/* The tree of directory entries. */ +struct grub_jfs_tree_dir +{ + /* Pointers to the previous and next tree headers of other nodes on + this level. */ + grub_uint64_t nextb; + grub_uint64_t prevb; + + grub_uint8_t flags; + + /* The amount of dirents in this node. */ + grub_uint8_t count; + grub_uint8_t freecnt; + grub_uint8_t freelist; + grub_uint8_t maxslot; + + /* The location of the sorted array of pointers to dirents. */ + grub_uint8_t sindex; + grub_uint8_t unused[10]; +} __attribute__ ((packed)); + +/* An internal node in the dirents tree. */ +struct grub_jfs_internal_dirent +{ + struct grub_jfs_extent ex; + grub_uint8_t next; + grub_uint8_t len; + grub_uint16_t namepart[11]; +} __attribute__ ((packed)); + +/* A leaf node in the dirents tree. */ +struct grub_jfs_leaf_dirent +{ + /* The inode for this dirent. */ + grub_uint32_t inode; + grub_uint8_t next; + + /* The size of the name. */ + grub_uint8_t len; + grub_uint16_t namepart[11]; + grub_uint32_t index; +} __attribute__ ((packed)); + +/* A leaf in the dirents tree. This one is used if the previously + dirent was not big enough to store the name. */ +struct grub_jfs_leaf_next_dirent +{ + grub_uint8_t next; + grub_uint8_t len; + grub_uint16_t namepart[15]; +} __attribute__ ((packed)); + +struct grub_jfs_inode +{ + grub_uint32_t stamp; + grub_uint32_t fileset; + grub_uint32_t inode; + grub_uint8_t unused[12]; + grub_uint64_t size; + grub_uint8_t unused2[20]; + grub_uint32_t mode; + grub_uint8_t unused3[72]; + grub_uint8_t unused4[96]; + + union + { + /* The tree describing the extents of the file. */ + struct __attribute__ ((packed)) + { + struct grub_jfs_treehead tree; + struct grub_jfs_tree_extent extents[16]; + } file; + union + { + /* The tree describing the dirents. */ + struct + { + grub_uint8_t unused[16]; + grub_uint8_t flags; + + /* Amount of dirents in this node. */ + grub_uint8_t count; + grub_uint8_t freecnt; + grub_uint8_t freelist; + grub_uint32_t idotdot; + grub_uint8_t sorted[8]; + } header; + struct grub_jfs_leaf_dirent dirents[8]; + } dir __attribute__ ((packed)); + /* Fast symlink. */ + struct + { + grub_uint8_t unused[32]; + grub_uint8_t path[128]; + } symlink; + } __attribute__ ((packed)); +} __attribute__ ((packed)); + +struct grub_jfs_data +{ + struct grub_jfs_sblock sblock; + grub_disk_t disk; + struct grub_jfs_inode fileset; + struct grub_jfs_inode currinode; + int pos; + int linknest; +} __attribute__ ((packed)); + +struct grub_jfs_diropen +{ + int index; + union + { + struct grub_jfs_tree_dir header; + struct grub_jfs_leaf_dirent dirent[0]; + struct grub_jfs_leaf_next_dirent next_dirent[0]; + char sorted[0]; + } *dirpage __attribute__ ((packed)); + struct grub_jfs_data *data; + struct grub_jfs_inode *inode; + int count; + char *sorted; + struct grub_jfs_leaf_dirent *leaf; + struct grub_jfs_leaf_next_dirent *next_leaf; + + /* The filename and inode of the last read dirent. */ + char name[255]; + grub_uint32_t ino; +} __attribute__ ((packed)); + + +static grub_dl_t my_mod; + +static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino); + +/* Get the block number for the block BLK in the node INODE in the + mounted filesystem DATA. */ +static int +grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode, + unsigned int blk) +{ + auto int getblk (struct grub_jfs_treehead *treehead, + struct grub_jfs_tree_extent *extents); + + int getblk (struct grub_jfs_treehead *treehead, + struct grub_jfs_tree_extent *extents) + { + int found = -1; + int i; + + for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2; i++) + { + if (treehead->flags & GRUB_JFS_TREE_LEAF) + { + /* Read the leafnode. */ + if (grub_le_to_cpu32 (extents[i].offset2) <= blk + && ((grub_le_to_cpu16 (extents[i].extent.length)) + + (extents[i].extent.length2 << 8) + + grub_le_to_cpu32 (extents[i].offset2)) > blk) + return (blk - grub_le_to_cpu32 (extents[i].offset2) + + grub_le_to_cpu32 (extents[i].extent.blk2)); + } + else + if (blk >= grub_le_to_cpu32 (extents[i].offset2)) + found = i; + } + + if (found != -1) + { + struct + { + struct grub_jfs_treehead treehead; + struct grub_jfs_tree_extent extents[254]; + } tree; + + if (grub_disk_read (data->disk, + grub_le_to_cpu32 (extents[found].extent.blk2) + << (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS), 0, + sizeof (tree), (char *) &tree)) + return -1; + + return getblk (&tree.treehead, &tree.extents[0]); + } + + return -1; + } + + return getblk (&inode->file.tree, &inode->file.extents[0]); +} + + +static grub_err_t +grub_jfs_read_inode (struct grub_jfs_data *data, int ino, + struct grub_jfs_inode *inode) +{ + struct grub_jfs_iag iag; + int iagnum = ino / 4096; + int inoext = (ino % 4096) / 32; + int inonum = (ino % 4096) % 32; + grub_uint32_t iagblk; + grub_uint32_t inoblk; + + iagblk = grub_jfs_blkno (data, &data->fileset, iagnum + 1); + if (grub_errno) + return grub_errno; + + /* Read in the IAG. */ + if (grub_disk_read (data->disk, + iagblk << (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS), 0, + sizeof (struct grub_jfs_iag), &iag)) + return grub_errno; + + inoblk = grub_le_to_cpu32 (iag.inodes[inoext].blk2); + inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS); + inoblk += inonum; + + if (grub_disk_read (data->disk, inoblk, 0, + sizeof (struct grub_jfs_inode), inode)) + return grub_errno; + + return 0; +} + + +static struct grub_jfs_data * +grub_jfs_mount (grub_disk_t disk) +{ + struct grub_jfs_data *data = 0; + + data = grub_malloc (sizeof (struct grub_jfs_data)); + if (!data) + return 0; + + /* Read the superblock. */ + if (grub_disk_read (disk, GRUB_JFS_SBLOCK, 0, + sizeof (struct grub_jfs_sblock), &data->sblock)) + goto fail; + + if (grub_strncmp ((char *) (data->sblock.magic), "JFS1", 4)) + { + grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem"); + goto fail; + } + + data->disk = disk; + data->pos = 0; + data->linknest = 0; + + /* Read the inode of the first fileset. */ + if (grub_disk_read (data->disk, GRUB_JFS_FS1_INODE_BLK, 0, + sizeof (struct grub_jfs_inode), &data->fileset)) + goto fail; + + return data; + + fail: + grub_free (data); + + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not a JFS filesystem"); + + return 0; +} + + +static struct grub_jfs_diropen * +grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode) +{ + struct grub_jfs_internal_dirent *de; + struct grub_jfs_diropen *diro; + int blk; + + de = (struct grub_jfs_internal_dirent *) inode->dir.dirents; + + if (!((grub_le_to_cpu32 (inode->mode) + & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR)) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + return 0; + } + + diro = grub_zalloc (sizeof (struct grub_jfs_diropen)); + if (!diro) + return 0; + + diro->data = data; + diro->inode = inode; + + /* Check if the entire tree is contained within the inode. */ + if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF) + { + diro->leaf = inode->dir.dirents; + diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de; + diro->sorted = (char *) (inode->dir.header.sorted); + diro->count = inode->dir.header.count; + + return diro; + } + + diro->dirpage = grub_malloc (grub_le_to_cpu32 (data->sblock.blksz)); + if (!diro->dirpage) + { + grub_free (diro); + return 0; + } + + blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2); + blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS); + + /* Read in the nodes until we are on the leaf node level. */ + do + { + int index; + if (grub_disk_read (data->disk, blk, 0, + grub_le_to_cpu32 (data->sblock.blksz), + diro->dirpage->sorted)) + { + grub_free (diro->dirpage); + grub_free (diro); + return 0; + } + + de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent; + index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32]; + blk = (grub_le_to_cpu32 (de[index].ex.blk2) + << (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS)); + } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF)); + + diro->leaf = diro->dirpage->dirent; + diro->next_leaf = diro->dirpage->next_dirent; + diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32]; + diro->count = diro->dirpage->header.count; + + return diro; +} + + +static void +grub_jfs_closedir (struct grub_jfs_diropen *diro) +{ + if (!diro) + return; + grub_free (diro->dirpage); + grub_free (diro); +} + + +/* Read in the next dirent from the directory described by DIRO. */ +static grub_err_t +grub_jfs_getent (struct grub_jfs_diropen *diro) +{ + int strpos = 0; + struct grub_jfs_leaf_dirent *leaf; + struct grub_jfs_leaf_next_dirent *next_leaf; + int len; + int nextent; + grub_uint16_t filename[255]; + + auto void addstr (grub_uint16_t *uname, int ulen); + + /* Add the unicode string to the utf16 filename buffer. */ + void addstr (grub_uint16_t *name, int ulen) + { + while (ulen--) + filename[strpos++] = *(name++); + } + + /* The last node, read in more. */ + if (diro->index == diro->count) + { + unsigned int next; + + /* If the inode contains the entry tree or if this was the last + node, there is nothing to read. */ + if ((diro->inode->file.tree.flags & GRUB_JFS_TREE_LEAF) + || !grub_le_to_cpu64 (diro->dirpage->header.nextb)) + return GRUB_ERR_OUT_OF_RANGE; + + next = grub_le_to_cpu64 (diro->dirpage->header.nextb); + next <<= (grub_le_to_cpu16 (diro->data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS); + + if (grub_disk_read (diro->data->disk, next, 0, + grub_le_to_cpu32 (diro->data->sblock.blksz), + diro->dirpage->sorted)) + return grub_errno; + + diro->leaf = diro->dirpage->dirent; + diro->next_leaf = diro->dirpage->next_dirent; + diro->sorted = &diro->dirpage->sorted[diro->dirpage->header.sindex * 32]; + diro->count = diro->dirpage->header.count; + diro->index = 0; + } + + leaf = &diro->leaf[(int) diro->sorted[diro->index]]; + next_leaf = &diro->next_leaf[diro->index]; + + len = leaf->len; + if (!len) + { + diro->index++; + return grub_jfs_getent (diro); + } + + addstr (leaf->namepart, len < 11 ? len : 11); + diro->ino = grub_le_to_cpu32 (leaf->inode); + len -= 11; + + /* Move down to the leaf level. */ + nextent = leaf->next; + if (leaf->next != 255) + do + { + next_leaf = &diro->next_leaf[nextent]; + addstr (next_leaf->namepart, len < 15 ? len : 15 ); + + len -= 15; + nextent = next_leaf->next; + } while (next_leaf->next != 255 && len > 0); + + diro->index++; + + /* Convert the temporary UTF16 filename to UTF8. */ + *grub_utf16_to_utf8 ((grub_uint8_t *) (diro->name), filename, strpos) = '\0'; + + return 0; +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_jfs_read_file (struct grub_jfs_data *data, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + int i; + int blockcnt; + + blockcnt = ((len + pos + grub_le_to_cpu32 (data->sblock.blksz) - 1) + / grub_le_to_cpu32 (data->sblock.blksz)); + + for (i = pos / grub_le_to_cpu32 (data->sblock.blksz); i < blockcnt; i++) + { + int blknr; + int blockoff = pos % grub_le_to_cpu32 (data->sblock.blksz); + int blockend = grub_le_to_cpu32 (data->sblock.blksz); + + int skipfirst = 0; + + blknr = grub_jfs_blkno (data, &data->currinode, i); + if (grub_errno) + return -1; + + /* Last block. */ + if (i == blockcnt - 1) + { + blockend = (len + pos) % grub_le_to_cpu32 (data->sblock.blksz); + + if (!blockend) + blockend = grub_le_to_cpu32 (data->sblock.blksz); + } + + /* First block. */ + if (i == (pos / (int) grub_le_to_cpu32 (data->sblock.blksz))) + { + skipfirst = blockoff; + blockend -= skipfirst; + } + + data->disk->read_hook = read_hook; + grub_disk_read (data->disk, + blknr << (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS), + skipfirst, blockend, buf); + + data->disk->read_hook = 0; + if (grub_errno) + return -1; + + buf += grub_le_to_cpu32 (data->sblock.blksz) - skipfirst; + } + + return len; +} + + +/* Find the file with the pathname PATH on the filesystem described by + DATA. */ +static grub_err_t +grub_jfs_find_file (struct grub_jfs_data *data, const char *path) +{ + char fpath[grub_strlen (path)]; + char *name = fpath; + char *next; + struct grub_jfs_diropen *diro; + + grub_strncpy (fpath, path, grub_strlen (path) + 1); + + if (grub_jfs_read_inode (data, GRUB_JFS_AGGR_INODE, &data->currinode)) + return grub_errno; + + /* Skip the first slashes. */ + while (*name == '/') + { + name++; + if (!*name) + return 0; + } + + /* Extract the actual part from the pathname. */ + next = grub_strchr (name, '/'); + if (next) + { + while (*next == '/') + { + next[0] = '\0'; + next++; + } + } + diro = grub_jfs_opendir (data, &data->currinode); + if (!diro) + return grub_errno; + + for (;;) + { + if (grub_strlen (name) == 0) + return GRUB_ERR_NONE; + + if (grub_jfs_getent (diro) == GRUB_ERR_OUT_OF_RANGE) + break; + + /* Check if the current direntry matches the current part of the + pathname. */ + if (!grub_strcmp (name, diro->name)) + { + int ino = diro->ino; + int dirino = grub_le_to_cpu32 (data->currinode.inode); + + grub_jfs_closedir (diro); + diro = 0; + + if (grub_jfs_read_inode (data, ino, &data->currinode)) + break; + + /* Check if this is a symlink. */ + if ((grub_le_to_cpu32 (data->currinode.mode) + & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_LNK) + { + grub_jfs_lookup_symlink (data, dirino); + if (grub_errno) + return grub_errno; + } + + if (!next) + return 0; + + name = next; + next = grub_strchr (name, '/'); + if (next) + { + next[0] = '\0'; + next++; + } + + /* Open this directory for reading dirents. */ + diro = grub_jfs_opendir (data, &data->currinode); + if (!diro) + return grub_errno; + + continue; + } + } + + grub_jfs_closedir (diro); + grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + return grub_errno; +} + + +static grub_err_t +grub_jfs_lookup_symlink (struct grub_jfs_data *data, int ino) +{ + int size = grub_le_to_cpu64 (data->currinode.size); + char symlink[size + 1]; + + if (++data->linknest > GRUB_JFS_MAX_SYMLNK_CNT) + return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks"); + + if (size <= 128) + grub_strncpy (symlink, (char *) (data->currinode.symlink.path), 128); + else if (grub_jfs_read_file (data, 0, 0, size, symlink) < 0) + return grub_errno; + + symlink[size] = '\0'; + + /* The symlink is an absolute path, go back to the root inode. */ + if (symlink[0] == '/') + ino = 2; + + /* Now load in the old inode. */ + if (grub_jfs_read_inode (data, ino, &data->currinode)) + return grub_errno; + + grub_jfs_find_file (data, symlink); + if (grub_errno) + grub_error (grub_errno, "cannot follow symlink `%s'", symlink); + + return grub_errno; +} + + +static grub_err_t +grub_jfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_jfs_data *data = 0; + struct grub_jfs_diropen *diro = 0; + + grub_dl_ref (my_mod); + + data = grub_jfs_mount (device->disk); + if (!data) + goto fail; + + if (grub_jfs_find_file (data, path)) + goto fail; + + diro = grub_jfs_opendir (data, &data->currinode); + if (!diro) + goto fail; + + /* Iterate over the dirents in the directory that was found. */ + while (grub_jfs_getent (diro) != GRUB_ERR_OUT_OF_RANGE) + { + struct grub_jfs_inode inode; + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + + if (grub_jfs_read_inode (data, diro->ino, &inode)) + goto fail; + + info.dir = (grub_le_to_cpu32 (inode.mode) + & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_DIR; + if (hook (diro->name, &info)) + goto fail; + } + + /* XXX: GRUB_ERR_OUT_OF_RANGE is used for the last dirent. */ + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_errno = 0; + + fail: + grub_jfs_closedir (diro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_jfs_open (struct grub_file *file, const char *name) +{ + struct grub_jfs_data *data; + + grub_dl_ref (my_mod); + + data = grub_jfs_mount (file->device->disk); + if (!data) + goto fail; + + grub_jfs_find_file (data, name); + if (grub_errno) + goto fail; + + /* It is only possible for open regular files. */ + if (! ((grub_le_to_cpu32 (data->currinode.mode) + & GRUB_JFS_FILETYPE_MASK) == GRUB_JFS_FILETYPE_REG)) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a regular file"); + goto fail; + } + + file->data = data; + file->size = grub_le_to_cpu64 (data->currinode.size); + + return 0; + + fail: + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + +static grub_ssize_t +grub_jfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_jfs_data *data = + (struct grub_jfs_data *) file->data; + + return grub_jfs_read_file (data, file->read_hook, file->offset, len, buf); +} + + +static grub_err_t +grub_jfs_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_jfs_uuid (grub_device_t device, char **uuid) +{ + struct grub_jfs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_jfs_mount (disk); + if (data) + { + *uuid = grub_xasprintf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" + "%02x%02x%02x%02x%02x%02x", + data->sblock.uuid[0], data->sblock.uuid[1], + data->sblock.uuid[2], data->sblock.uuid[3], + data->sblock.uuid[4], data->sblock.uuid[5], + data->sblock.uuid[6], data->sblock.uuid[7], + data->sblock.uuid[8], data->sblock.uuid[9], + data->sblock.uuid[10], data->sblock.uuid[11], + data->sblock.uuid[12], data->sblock.uuid[13], + data->sblock.uuid[14], data->sblock.uuid[15]); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static grub_err_t +grub_jfs_label (grub_device_t device, char **label) +{ + struct grub_jfs_data *data; + data = grub_jfs_mount (device->disk); + + if (data) + *label = grub_strndup ((char *) (data->sblock.volname), 11); + else + *label = 0; + + return grub_errno; +} + + +static struct grub_fs grub_jfs_fs = + { + .name = "jfs", + .dir = grub_jfs_dir, + .open = grub_jfs_open, + .read = grub_jfs_read, + .close = grub_jfs_close, + .label = grub_jfs_label, + .uuid = grub_jfs_uuid, + .next = 0 + }; + +GRUB_MOD_INIT(jfs) +{ + grub_fs_register (&grub_jfs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(jfs) +{ + grub_fs_unregister (&grub_jfs_fs); +} diff --git a/libr/fs/p/grub/fs/minix.c b/libr/fs/p/grub/fs/minix.c new file mode 100644 index 0000000000..679e1ec510 --- /dev/null +++ b/libr/fs/p/grub/fs/minix.c @@ -0,0 +1,588 @@ +/* minix.c - The minix filesystem, version 1 and 2. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef MODE_MINIX2 +#define GRUB_MINIX_MAGIC 0x2468 +#define GRUB_MINIX_MAGIC_30 0x2478 +#else +#define GRUB_MINIX_MAGIC 0x137F +#define GRUB_MINIX_MAGIC_30 0x138F +#endif +#define GRUB_MINIX_BSIZE 1024U +#define GRUB_MINIX_LOG2_BSIZE 1 +#define GRUB_MINIX_ROOT_INODE 1 +#define GRUB_MINIX_MAX_SYMLNK_CNT 8 +#define GRUB_MINIX_SBLOCK 2 + +#define GRUB_MINIX_IFDIR 0040000U +#define GRUB_MINIX_IFLNK 0120000U + +#ifdef MODE_MINIX2 +typedef grub_uint32_t grub_minix_uintn_t; +#define grub_minix_le_to_cpu_n grub_le_to_cpu32 +#else +typedef grub_uint16_t grub_minix_uintn_t; +#define grub_minix_le_to_cpu_n grub_le_to_cpu16 +#endif + +#define GRUB_MINIX_INODE_BLKSZ(data) sizeof (grub_minix_uintn_t) + +#define GRUB_MINIX_INODE_SIZE(data) (grub_minix_le_to_cpu_n (data->inode.size)) +#define GRUB_MINIX_INODE_MODE(data) (grub_le_to_cpu16 (data->inode.mode)) +#define GRUB_MINIX_INODE_DIR_ZONES(data,blk) (grub_minix_le_to_cpu_n \ + (data->inode.dir_zones[blk])) +#define GRUB_MINIX_INODE_INDIR_ZONE(data) (grub_minix_le_to_cpu_n \ + (data->inode.indir_zone)) +#define GRUB_MINIX_INODE_DINDIR_ZONE(data) (grub_minix_le_to_cpu_n \ + (data->inode.double_indir_zone)) + +#define GRUB_MINIX_LOG2_ZONESZ (GRUB_MINIX_LOG2_BSIZE \ + + grub_le_to_cpu16 (sblock->log2_zone_size)) +#define GRUB_MINIX_ZONESZ (GRUB_MINIX_BSIZE \ + << grub_le_to_cpu16 (sblock->log2_zone_size)) + +struct grub_minix_sblock +{ + grub_uint16_t inode_cnt; + grub_uint16_t zone_cnt; + grub_uint16_t inode_bmap_size; + grub_uint16_t zone_bmap_size; + grub_uint16_t first_data_zone; + grub_uint16_t log2_zone_size; + grub_uint32_t max_file_size; + grub_uint16_t magic; +}; + +#ifndef MODE_MINIX2 +struct grub_minix_inode +{ + grub_uint16_t mode; + grub_uint16_t uid; + grub_uint16_t size; + grub_uint32_t ctime; + grub_uint8_t gid; + grub_uint8_t nlinks; + grub_uint16_t dir_zones[7]; + grub_uint16_t indir_zone; + grub_uint16_t double_indir_zone; +}; + +#else + +struct grub_minix_inode +{ + grub_uint16_t mode; + grub_uint16_t nlinks; + grub_uint16_t uid; + grub_uint16_t gid; + grub_uint32_t size; + grub_uint32_t atime; + grub_uint32_t mtime; + grub_uint32_t ctime; + grub_uint32_t dir_zones[7]; + grub_uint32_t indir_zone; + grub_uint32_t double_indir_zone; + grub_uint32_t unused; + +}; + +#endif + +/* Information about a "mounted" minix filesystem. */ +struct grub_minix_data +{ + struct grub_minix_sblock sblock; + struct grub_minix_inode inode; + int ino; + int linknest; + grub_disk_t disk; + int filename_size; +}; + +static grub_dl_t my_mod; + +static grub_err_t grub_minix_find_file (struct grub_minix_data *data, + const char *path); + +static int +grub_minix_get_file_block (struct grub_minix_data *data, unsigned int blk) +{ + struct grub_minix_sblock *sblock = &data->sblock; + int indir; + + auto int grub_get_indir (int, int); + + /* Read the block pointer in ZONE, on the offset NUM. */ + int grub_get_indir (int zone, int num) + { + grub_minix_uintn_t indirn; + grub_disk_read (data->disk, + zone << GRUB_MINIX_LOG2_ZONESZ, + sizeof (grub_minix_uintn_t) * num, + sizeof (grub_minix_uintn_t), (char *) &indirn); + return grub_minix_le_to_cpu_n (indirn); + } + + /* Direct block. */ + if (blk < 7) + return GRUB_MINIX_INODE_DIR_ZONES (data, blk); + + /* Indirect block. */ + blk -= 7; + if (blk < GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data)) + { + indir = grub_get_indir (GRUB_MINIX_INODE_INDIR_ZONE (data), blk); + return indir; + } + + /* Double indirect block. */ + blk -= GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data); + if (blk < (GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data)) + * (GRUB_MINIX_ZONESZ / GRUB_MINIX_INODE_BLKSZ (data))) + { + indir = grub_get_indir (GRUB_MINIX_INODE_DINDIR_ZONE (data), + blk / GRUB_MINIX_ZONESZ); + + indir = grub_get_indir (indir, blk % GRUB_MINIX_ZONESZ); + + return indir; + } + + /* This should never happen. */ + grub_error (GRUB_ERR_OUT_OF_RANGE, "file bigger than maximum size"); + + return 0; +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_minix_read_file (struct grub_minix_data *data, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_disk_addr_t len, char *buf) +{ + struct grub_minix_sblock *sblock = &data->sblock; + int i; + int blockcnt; + + /* Adjust len so it we can't read past the end of the file. */ + if (len + pos > GRUB_MINIX_INODE_SIZE (data)) + len = GRUB_MINIX_INODE_SIZE (data) - pos; + + blockcnt = (len + pos + GRUB_MINIX_BSIZE - 1) / GRUB_MINIX_BSIZE; + + for (i = pos / GRUB_MINIX_BSIZE; i < blockcnt; i++) + { + int blknr; + int blockoff = pos % GRUB_MINIX_BSIZE; + int blockend = GRUB_MINIX_BSIZE; + + int skipfirst = 0; + + blknr = grub_minix_get_file_block (data, i); + if (grub_errno) + return -1; + + /* Last block. */ + if (i == blockcnt - 1) + { + blockend = (len + pos) % GRUB_MINIX_BSIZE; + + if (!blockend) + blockend = GRUB_MINIX_BSIZE; + } + + /* First block. */ + if (i == (pos / (int) GRUB_MINIX_BSIZE)) + { + skipfirst = blockoff; + blockend -= skipfirst; + } + + data->disk->read_hook = read_hook; + grub_disk_read (data->disk, blknr << GRUB_MINIX_LOG2_ZONESZ, + skipfirst, blockend, buf); + + data->disk->read_hook = 0; + if (grub_errno) + return -1; + + buf += GRUB_MINIX_BSIZE - skipfirst; + } + + return len; +} + + +/* Read inode INO from the mounted filesystem described by DATA. This + inode is used by default now. */ +static grub_err_t +grub_minix_read_inode (struct grub_minix_data *data, int ino) +{ + struct grub_minix_sblock *sblock = &data->sblock; + + /* Block in which the inode is stored. */ + int block; + data->ino = ino; + + /* The first inode in minix is inode 1. */ + ino--; + + block = ((2 + grub_le_to_cpu16 (sblock->inode_bmap_size) + + grub_le_to_cpu16 (sblock->zone_bmap_size)) + << GRUB_MINIX_LOG2_BSIZE); + + block += ino / (GRUB_DISK_SECTOR_SIZE / sizeof (struct grub_minix_inode)); + int offs = (ino % (GRUB_DISK_SECTOR_SIZE + / sizeof (struct grub_minix_inode)) + * sizeof (struct grub_minix_inode)); + + grub_disk_read (data->disk, block, offs, + sizeof (struct grub_minix_inode), &data->inode); + + return GRUB_ERR_NONE; +} + + +/* Lookup the symlink the current inode points to. INO is the inode + number of the directory the symlink is relative to. */ +static grub_err_t +grub_minix_lookup_symlink (struct grub_minix_data *data, int ino) +{ + char symlink[GRUB_MINIX_INODE_SIZE (data) + 1]; + + if (++data->linknest > GRUB_MINIX_MAX_SYMLNK_CNT) + return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks"); + + if (grub_minix_read_file (data, 0, 0, + GRUB_MINIX_INODE_SIZE (data), symlink) < 0) + return grub_errno; + + symlink[GRUB_MINIX_INODE_SIZE (data)] = '\0'; + + /* The symlink is an absolute path, go back to the root inode. */ + if (symlink[0] == '/') + ino = GRUB_MINIX_ROOT_INODE; + + /* Now load in the old inode. */ + if (grub_minix_read_inode (data, ino)) + return grub_errno; + + grub_minix_find_file (data, symlink); + if (grub_errno) + grub_error (grub_errno, "cannot follow symlink `%s'", symlink); + + return grub_errno; +} + + +/* Find the file with the pathname PATH on the filesystem described by + DATA. */ +static grub_err_t +grub_minix_find_file (struct grub_minix_data *data, const char *path) +{ + char fpath[grub_strlen (path) + 1]; + char *name = fpath; + char *next; + unsigned int pos = 0; + int dirino; + + grub_strcpy (fpath, path); + + /* Skip the first slash. */ + if (name[0] == '/') + { + name++; + if (!*name) + return 0; + } + + /* Extract the actual part from the pathname. */ + next = grub_strchr (name, '/'); + if (next) + { + next[0] = '\0'; + next++; + } + + do + { + grub_uint16_t ino; + char filename[data->filename_size + 1]; + + if (grub_strlen (name) == 0) + return GRUB_ERR_NONE; + + if (grub_minix_read_file (data, 0, pos, sizeof (ino), + (char *) &ino) < 0) + return grub_errno; + if (grub_minix_read_file (data, 0, pos + sizeof (ino), + data->filename_size, (char *) filename)< 0) + return grub_errno; + + filename[data->filename_size] = '\0'; + + /* Check if the current direntry matches the current part of the + pathname. */ + if (!grub_strcmp (name, filename)) + { + dirino = data->ino; + grub_minix_read_inode (data, grub_le_to_cpu16 (ino)); + + /* Follow the symlink. */ + if ((GRUB_MINIX_INODE_MODE (data) + & GRUB_MINIX_IFLNK) == GRUB_MINIX_IFLNK) + { + grub_minix_lookup_symlink (data, dirino); + if (grub_errno) + return grub_errno; + } + + if (!next) + return 0; + + pos = 0; + + name = next; + next = grub_strchr (name, '/'); + if (next) + { + next[0] = '\0'; + next++; + } + + if ((GRUB_MINIX_INODE_MODE (data) + & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + + continue; + } + + pos += sizeof (ino) + data->filename_size; + } while (pos < GRUB_MINIX_INODE_SIZE (data)); + + grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + return grub_errno; +} + + +/* Mount the filesystem on the disk DISK. */ +static struct grub_minix_data * +grub_minix_mount (grub_disk_t disk) +{ + struct grub_minix_data *data; + + data = grub_malloc (sizeof (struct grub_minix_data)); + if (!data) + return 0; + + /* Read the superblock. */ + grub_disk_read (disk, GRUB_MINIX_SBLOCK, 0, + sizeof (struct grub_minix_sblock),&data->sblock); + if (grub_errno) + goto fail; + + if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX_MAGIC) + data->filename_size = 14; + else if (grub_le_to_cpu16 (data->sblock.magic) == GRUB_MINIX_MAGIC_30) + data->filename_size = 30; + else + goto fail; + + data->disk = disk; + data->linknest = 0; + + return data; + + fail: + grub_free (data); +#ifdef MODE_MINIX2 + grub_error (GRUB_ERR_BAD_FS, "not a minix2 filesystem"); +#else + grub_error (GRUB_ERR_BAD_FS, "not a minix filesystem"); +#endif + return 0; +} + +static grub_err_t +grub_minix_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_minix_data *data = 0; + unsigned int pos = 0; + + data = grub_minix_mount (device->disk); + if (!data) + return grub_errno; + + grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE); + if (grub_errno) + goto fail; + + grub_minix_find_file (data, path); + if (grub_errno) + goto fail; + + if ((GRUB_MINIX_INODE_MODE (data) & GRUB_MINIX_IFDIR) != GRUB_MINIX_IFDIR) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + goto fail; + } + + while (pos < GRUB_MINIX_INODE_SIZE (data)) + { + grub_uint16_t ino; + char filename[data->filename_size + 1]; + int dirino = data->ino; + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + + + if (grub_minix_read_file (data, 0, pos, sizeof (ino), + (char *) &ino) < 0) + return grub_errno; + + if (grub_minix_read_file (data, 0, pos + sizeof (ino), + data->filename_size, + (char *) filename) < 0) + return grub_errno; + filename[data->filename_size] = '\0'; + + /* The filetype is not stored in the dirent. Read the inode to + find out the filetype. This *REALLY* sucks. */ + grub_minix_read_inode (data, grub_le_to_cpu16 (ino)); + info.dir = ((GRUB_MINIX_INODE_MODE (data) + & GRUB_MINIX_IFDIR) == GRUB_MINIX_IFDIR); + if (hook (filename, &info) ? 1 : 0) + break; + + /* Load the old inode back in. */ + grub_minix_read_inode (data, dirino); + + pos += sizeof (ino) + data->filename_size; + } + + fail: + grub_free (data); + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_minix_open (struct grub_file *file, const char *name) +{ + struct grub_minix_data *data; + data = grub_minix_mount (file->device->disk); + if (!data) + return grub_errno; + + /* Open the inode op the root directory. */ + grub_minix_read_inode (data, GRUB_MINIX_ROOT_INODE); + if (grub_errno) + { + grub_free (data); + return grub_errno; + } + + if (!name || name[0] != '/') + { + grub_error (GRUB_ERR_BAD_FILENAME, "bad filename"); + return grub_errno; + } + + /* Traverse the directory tree to the node that should be + opened. */ + grub_minix_find_file (data, name); + if (grub_errno) + { + grub_free (data); + return grub_errno; + } + + file->data = data; + file->size = GRUB_MINIX_INODE_SIZE (data); + + return GRUB_ERR_NONE; +} + + +static grub_ssize_t +grub_minix_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_minix_data *data = + (struct grub_minix_data *) file->data; + + return grub_minix_read_file (data, file->read_hook, file->offset, len, buf); +} + + +static grub_err_t +grub_minix_close (grub_file_t file) +{ + grub_free (file->data); + + return GRUB_ERR_NONE; +} + + + +static struct grub_fs grub_minix_fs = + { +#ifdef MODE_MINIX2 + .name = "minix2", +#else + .name = "minix", +#endif + .dir = grub_minix_dir, + .open = grub_minix_open, + .read = grub_minix_read, + .close = grub_minix_close, + .next = 0 + }; + +#ifdef MODE_MINIX2 +GRUB_MOD_INIT(minix2) +#else +GRUB_MOD_INIT(minix) +#endif +{ + grub_fs_register (&grub_minix_fs); + my_mod = mod; +} + +#ifdef MODE_MINIX2 +GRUB_MOD_FINI(minix2) +#else +GRUB_MOD_FINI(minix) +#endif +{ + grub_fs_unregister (&grub_minix_fs); +} diff --git a/libr/fs/p/grub/fs/minix2.c b/libr/fs/p/grub/fs/minix2.c new file mode 100644 index 0000000000..0fcd4b1399 --- /dev/null +++ b/libr/fs/p/grub/fs/minix2.c @@ -0,0 +1,2 @@ +#define MODE_MINIX2 1 +#include "minix.c" diff --git a/libr/fs/p/grub/fs/nilfs2.c b/libr/fs/p/grub/fs/nilfs2.c new file mode 100644 index 0000000000..e529775f42 --- /dev/null +++ b/libr/fs/p/grub/fs/nilfs2.c @@ -0,0 +1,1184 @@ +/* + * nilfs2.c - New Implementation of Log filesystem + * + * Written by Jiro SEKIBA + * + * Copyright (C) 2003,2004,2005,2007,2008,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + + +/* Filetype information as used in inodes. */ +#define FILETYPE_INO_MASK 0170000 +#define FILETYPE_INO_REG 0100000 +#define FILETYPE_INO_DIRECTORY 0040000 +#define FILETYPE_INO_SYMLINK 0120000 + +#include +#include +#include +#include +#include +#include +#include +#include + +#define NILFS_INODE_BMAP_SIZE 7 + +#define NILFS_SUPORT_REV 2 + +/* Magic value used to identify an nilfs2 filesystem. */ +#define NILFS2_SUPER_MAGIC 0x3434 +/* nilfs btree node flag. */ +#define NILFS_BTREE_NODE_ROOT 0x01 + +/* nilfs btree node level. */ +#define NILFS_BTREE_LEVEL_DATA 0 +#define NILFS_BTREE_LEVEL_NODE_MIN (NILFS_BTREE_LEVEL_DATA + 1) +#define NILFS_BTREE_LEVEL_MAX 14 + +/* nilfs 1st super block posission from beginning of the partition + in 512 block size */ +#define NILFS_1ST_SUPER_BLOCK 2 +/* nilfs 2nd super block posission from beginning of the partition + in 512 block size */ +#define NILFS_2ND_SUPER_BLOCK(devsize) (((devsize >> 3) - 1) << 3) + +struct grub_nilfs2_inode +{ + grub_uint64_t i_blocks; + grub_uint64_t i_size; + grub_uint64_t i_ctime; + grub_uint64_t i_mtime; + grub_uint32_t i_ctime_nsec; + grub_uint32_t i_mtime_nsec; + grub_uint32_t i_uid; + grub_uint32_t i_gid; + grub_uint16_t i_mode; + grub_uint16_t i_links_count; + grub_uint32_t i_flags; + grub_uint64_t i_bmap[NILFS_INODE_BMAP_SIZE]; +#define i_device_code i_bmap[0] + grub_uint64_t i_xattr; + grub_uint32_t i_generation; + grub_uint32_t i_pad; +}; + +struct grub_nilfs2_super_root +{ + grub_uint32_t sr_sum; + grub_uint16_t sr_bytes; + grub_uint16_t sr_flags; + grub_uint64_t sr_nongc_ctime; + struct grub_nilfs2_inode sr_dat; + struct grub_nilfs2_inode sr_cpfile; + struct grub_nilfs2_inode sr_sufile; +}; + +struct grub_nilfs2_super_block +{ + grub_uint32_t s_rev_level; + grub_uint16_t s_minor_rev_level; + grub_uint16_t s_magic; + grub_uint16_t s_bytes; + grub_uint16_t s_flags; + grub_uint32_t s_crc_seed; + grub_uint32_t s_sum; + grub_uint32_t s_log_block_size; + grub_uint64_t s_nsegments; + grub_uint64_t s_dev_size; + grub_uint64_t s_first_data_block; + grub_uint32_t s_blocks_per_segment; + grub_uint32_t s_r_segments_percentage; + grub_uint64_t s_last_cno; + grub_uint64_t s_last_pseg; + grub_uint64_t s_last_seq; + grub_uint64_t s_free_blocks_count; + grub_uint64_t s_ctime; + grub_uint64_t s_mtime; + grub_uint64_t s_wtime; + grub_uint16_t s_mnt_count; + grub_uint16_t s_max_mnt_count; + grub_uint16_t s_state; + grub_uint16_t s_errors; + grub_uint64_t s_lastcheck; + grub_uint32_t s_checkinterval; + grub_uint32_t s_creator_os; + grub_uint16_t s_def_resuid; + grub_uint16_t s_def_resgid; + grub_uint32_t s_first_ino; + grub_uint16_t s_inode_size; + grub_uint16_t s_dat_entry_size; + grub_uint16_t s_checkpoint_size; + grub_uint16_t s_segment_usage_size; + grub_uint8_t s_uuid[16]; + char s_volume_name[16]; + char s_last_mounted[64]; + grub_uint32_t s_c_interval; + grub_uint32_t s_c_block_max; + grub_uint32_t s_reserved[192]; +}; + +struct grub_nilfs2_dir_entry +{ + grub_uint64_t inode; + grub_uint16_t rec_len; + grub_uint8_t name_len; + grub_uint8_t file_type; +#if 0 /* followed by file name. */ + char name[NILFS_NAME_LEN]; + char pad; +#endif +} __attribute__ ((packed)); + +enum +{ + NILFS_FT_UNKNOWN, + NILFS_FT_REG_FILE, + NILFS_FT_DIR, + NILFS_FT_CHRDEV, + NILFS_FT_BLKDEV, + NILFS_FT_FIFO, + NILFS_FT_SOCK, + NILFS_FT_SYMLINK, + NILFS_FT_MAX +}; + +struct grub_nilfs2_finfo +{ + grub_uint64_t fi_ino; + grub_uint64_t fi_cno; + grub_uint32_t fi_nblocks; + grub_uint32_t fi_ndatablk; +}; + +struct grub_nilfs2_binfo_v +{ + grub_uint64_t bi_vblocknr; + grub_uint64_t bi_blkoff; +}; + +struct grub_nilfs2_binfo_dat +{ + grub_uint64_t bi_blkoff; + grub_uint8_t bi_level; + grub_uint8_t bi_pad[7]; +}; + +union grub_nilfs2_binfo +{ + struct grub_nilfs2_binfo_v bi_v; + struct grub_nilfs2_binfo_dat bi_dat; +}; + +struct grub_nilfs2_segment_summary +{ + grub_uint32_t ss_datasum; + grub_uint32_t ss_sumsum; + grub_uint32_t ss_magic; + grub_uint16_t ss_bytes; + grub_uint16_t ss_flags; + grub_uint64_t ss_seq; + grub_uint64_t ss_create; + grub_uint64_t ss_next; + grub_uint32_t ss_nblocks; + grub_uint32_t ss_nfinfo; + grub_uint32_t ss_sumbytes; + grub_uint32_t ss_pad; +}; + +struct grub_nilfs2_btree_node +{ + grub_uint8_t bn_flags; + grub_uint8_t bn_level; + grub_uint16_t bn_nchildren; + grub_uint32_t bn_pad; +}; + +struct grub_nilfs2_palloc_group_desc +{ + grub_uint32_t pg_nfrees; +}; + +struct grub_nilfs2_dat_entry +{ + grub_uint64_t de_blocknr; + grub_uint64_t de_start; + grub_uint64_t de_end; + grub_uint64_t de_rsv; +}; + +struct grub_nilfs2_snapshot_list +{ + grub_uint64_t ssl_next; + grub_uint64_t ssl_prev; +}; + +struct grub_nilfs2_cpfile_header +{ + grub_uint64_t ch_ncheckpoints; + grub_uint64_t ch_nsnapshots; + struct grub_nilfs2_snapshot_list ch_snapshot_list; +}; + +struct grub_nilfs2_checkpoint +{ + grub_uint32_t cp_flags; + grub_uint32_t cp_checkpoints_count; + struct grub_nilfs2_snapshot_list cp_snapshot_list; + grub_uint64_t cp_cno; + grub_uint64_t cp_create; + grub_uint64_t cp_nblk_inc; + grub_uint64_t cp_inodes_count; + grub_uint64_t cp_blocks_count; + struct grub_nilfs2_inode cp_ifile_inode; +}; + + +#define NILFS_BMAP_LARGE 0x1 +#define NILFS_BMAP_SIZE (NILFS_INODE_BMAP_SIZE * sizeof(grub_uint64_t)) + +/* nilfs extra padding for nonroot btree node. */ +#define NILFS_BTREE_NODE_EXTRA_PAD_SIZE (sizeof(grub_uint64_t)) +#define NILFS_BTREE_ROOT_SIZE NILFS_BMAP_SIZE +#define NILFS_BTREE_ROOT_NCHILDREN_MAX \ + ((NILFS_BTREE_ROOT_SIZE - sizeof(struct nilfs_btree_node)) / \ + (sizeof(grub_uint64_t) + sizeof(grub_uint64_t)) ) + + +struct grub_fshelp_node +{ + struct grub_nilfs2_data *data; + struct grub_nilfs2_inode inode; + grub_uint64_t ino; + int inode_read; +}; + +struct grub_nilfs2_data +{ + struct grub_nilfs2_super_block sblock; + struct grub_nilfs2_super_root sroot; + struct grub_nilfs2_inode ifile; + grub_disk_t disk; + struct grub_nilfs2_inode *inode; + struct grub_fshelp_node diropen; +}; + +/* Log2 size of nilfs2 block in 512 blocks. */ +#define LOG2_NILFS2_BLOCK_SIZE(data) \ + (grub_le_to_cpu32 (data->sblock.s_log_block_size) + 1) + +/* Log2 size of nilfs2 block in bytes. */ +#define LOG2_BLOCK_SIZE(data) \ + (grub_le_to_cpu32 (data->sblock.s_log_block_size) + 10) + +/* The size of an nilfs2 block in bytes. */ +#define NILFS2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE (data)) + +static grub_uint64_t +grub_nilfs2_dat_translate (struct grub_nilfs2_data *data, grub_uint64_t key); +static grub_dl_t my_mod; + + + +static inline unsigned long +grub_nilfs2_palloc_entries_per_group (struct grub_nilfs2_data *data) +{ + return 1UL << (LOG2_BLOCK_SIZE (data) + 3); +} + +static inline grub_uint64_t +grub_nilfs2_palloc_group (struct grub_nilfs2_data *data, + grub_uint64_t nr, grub_uint32_t * offset) +{ + return grub_divmod64 (nr, grub_nilfs2_palloc_entries_per_group (data), + offset); +} + +static inline grub_uint32_t +grub_nilfs2_palloc_groups_per_desc_block (struct grub_nilfs2_data *data) +{ + return NILFS2_BLOCK_SIZE (data) / + sizeof (struct grub_nilfs2_palloc_group_desc); +} + +static inline grub_uint32_t +grub_nilfs2_entries_per_block (struct grub_nilfs2_data *data, + unsigned long entry_size) +{ + return NILFS2_BLOCK_SIZE (data) / entry_size; +} + + +static inline grub_uint32_t +grub_nilfs2_blocks_per_group (struct grub_nilfs2_data *data, + unsigned long entry_size) +{ + return grub_div_roundup (grub_nilfs2_palloc_entries_per_group (data), + grub_nilfs2_entries_per_block (data, + entry_size)) + 1; +} + +static inline grub_uint32_t +grub_nilfs2_blocks_per_desc_block (struct grub_nilfs2_data *data, + unsigned long entry_size) +{ + return grub_nilfs2_palloc_groups_per_desc_block (data) * + grub_nilfs2_blocks_per_group (data, entry_size) + 1; +} + +static inline grub_uint32_t +grub_nilfs2_palloc_desc_block_offset (struct grub_nilfs2_data *data, + unsigned long group, + unsigned long entry_size) +{ + grub_uint32_t desc_block = + group / grub_nilfs2_palloc_groups_per_desc_block (data); + return desc_block * grub_nilfs2_blocks_per_desc_block (data, entry_size); +} + +static inline grub_uint32_t +grub_nilfs2_palloc_bitmap_block_offset (struct grub_nilfs2_data *data, + unsigned long group, + unsigned long entry_size) +{ + unsigned long desc_offset = group % + grub_nilfs2_palloc_groups_per_desc_block (data); + + return grub_nilfs2_palloc_desc_block_offset (data, group, entry_size) + 1 + + desc_offset * grub_nilfs2_blocks_per_group (data, entry_size); +} + +static inline grub_uint32_t +grub_nilfs2_palloc_entry_offset (struct grub_nilfs2_data *data, + grub_uint64_t nr, unsigned long entry_size) +{ + unsigned long group; + grub_uint32_t group_offset; + + group = grub_nilfs2_palloc_group (data, nr, &group_offset); + + return grub_nilfs2_palloc_bitmap_block_offset (data, group, + entry_size) + 1 + + group_offset / grub_nilfs2_entries_per_block (data, entry_size); + +} + +static inline struct grub_nilfs2_btree_node * +grub_nilfs2_btree_get_root (struct grub_nilfs2_inode *inode) +{ + return (struct grub_nilfs2_btree_node *) &inode->i_bmap[0]; +} + +static inline int +grub_nilfs2_btree_get_level (struct grub_nilfs2_btree_node *node) +{ + return node->bn_level; +} + +static inline grub_uint64_t * +grub_nilfs2_btree_node_dkeys (struct grub_nilfs2_btree_node *node) +{ + return (grub_uint64_t *) ((char *) (node + 1) + + ((node->bn_flags & NILFS_BTREE_NODE_ROOT) ? + 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE)); +} + +static inline grub_uint64_t +grub_nilfs2_btree_node_get_key (struct grub_nilfs2_btree_node *node, + int index) +{ + return grub_le_to_cpu64 (*(grub_nilfs2_btree_node_dkeys (node) + index)); +} + +static inline int +grub_nilfs2_btree_node_lookup (struct grub_nilfs2_btree_node *node, + grub_uint64_t key, int *indexp) +{ + grub_uint64_t nkey; + int index, low, high, s; + + low = 0; + high = grub_le_to_cpu16 (node->bn_nchildren) - 1; + index = 0; + s = 0; + while (low <= high) + { + index = (low + high) / 2; + nkey = grub_nilfs2_btree_node_get_key (node, index); + if (nkey == key) + { + *indexp = index; + return 1; + } + else if (nkey < key) + { + low = index + 1; + s = -1; + } + else + { + high = index - 1; + s = 1; + } + } + + if (node->bn_level > NILFS_BTREE_LEVEL_NODE_MIN) + { + if (s > 0 && index > 0) + index--; + } + else if (s < 0) + index++; + + *indexp = index; + return s == 0; +} + +static inline int +grub_nilfs2_btree_node_nchildren_max (struct grub_nilfs2_data *data, + struct grub_nilfs2_btree_node *node) +{ + int node_children_max = ((NILFS2_BLOCK_SIZE (data) - + sizeof (struct grub_nilfs2_btree_node) - + NILFS_BTREE_NODE_EXTRA_PAD_SIZE) / + (sizeof (grub_uint64_t) + sizeof (grub_uint64_t))); + + return (node->bn_flags & NILFS_BTREE_NODE_ROOT) ? 3 : node_children_max; +} + +static inline grub_uint64_t * +grub_nilfs2_btree_node_dptrs (struct grub_nilfs2_data *data, + struct grub_nilfs2_btree_node *node) +{ + return (grub_uint64_t *) (grub_nilfs2_btree_node_dkeys (node) + + grub_nilfs2_btree_node_nchildren_max (data, + node)); +} + +static inline grub_uint64_t +grub_nilfs2_btree_node_get_ptr (struct grub_nilfs2_data *data, + struct grub_nilfs2_btree_node *node, + int index) +{ + return + grub_le_to_cpu64 (*(grub_nilfs2_btree_node_dptrs (data, node) + index)); +} + +static inline int +grub_nilfs2_btree_get_nonroot_node (struct grub_nilfs2_data *data, + grub_uint64_t ptr, void *block) +{ + grub_disk_t disk = data->disk; + unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data)); + + return grub_disk_read (disk, ptr * nilfs2_block_count, 0, + NILFS2_BLOCK_SIZE (data), block); +} + +static grub_uint64_t +grub_nilfs2_btree_lookup (struct grub_nilfs2_data *data, + struct grub_nilfs2_inode *inode, + grub_uint64_t key, int need_translate) +{ + struct grub_nilfs2_btree_node *node; + unsigned char block[NILFS2_BLOCK_SIZE (data)]; + grub_uint64_t ptr; + int level, found, index; + + node = grub_nilfs2_btree_get_root (inode); + level = grub_nilfs2_btree_get_level (node); + + found = grub_nilfs2_btree_node_lookup (node, key, &index); + ptr = grub_nilfs2_btree_node_get_ptr (data, node, index); + if (need_translate) + ptr = grub_nilfs2_dat_translate (data, ptr); + + for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) + { + grub_nilfs2_btree_get_nonroot_node (data, ptr, block); + if (grub_errno) + { + return -1; + } + node = (struct grub_nilfs2_btree_node *) block; + + if (node->bn_level != level) + { + grub_error (GRUB_ERR_BAD_FS, "btree level mismatch\n"); + return -1; + } + + if (!found) + found = grub_nilfs2_btree_node_lookup (node, key, &index); + else + index = 0; + + if (index < grub_nilfs2_btree_node_nchildren_max (data, node)) + { + ptr = grub_nilfs2_btree_node_get_ptr (data, node, index); + if (need_translate) + ptr = grub_nilfs2_dat_translate (data, ptr); + } + else + { + grub_error (GRUB_ERR_BAD_FS, "btree corruption\n"); + return -1; + } + } + + if (!found) + return -1; + + return ptr; +} + +static inline grub_uint64_t +grub_nilfs2_direct_lookup (struct grub_nilfs2_inode *inode, grub_uint64_t key) +{ + return grub_le_to_cpu64 (inode->i_bmap[1 + key]); +} + +static inline grub_uint64_t +grub_nilfs2_bmap_lookup (struct grub_nilfs2_data *data, + struct grub_nilfs2_inode *inode, + grub_uint64_t key, int need_translate) +{ + struct grub_nilfs2_btree_node *root = grub_nilfs2_btree_get_root (inode); + if (root->bn_flags & NILFS_BMAP_LARGE) + return grub_nilfs2_btree_lookup (data, inode, key, need_translate); + else + { + grub_uint64_t ptr; + ptr = grub_nilfs2_direct_lookup (inode, key); + if (need_translate) + ptr = grub_nilfs2_dat_translate (data, ptr); + return ptr; + } +} + +static grub_uint64_t +grub_nilfs2_dat_translate (struct grub_nilfs2_data *data, grub_uint64_t key) +{ + struct grub_nilfs2_dat_entry entry; + grub_disk_t disk = data->disk; + grub_uint64_t pptr; + grub_uint32_t blockno, offset; + unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data)); + + blockno = grub_nilfs2_palloc_entry_offset (data, key, + sizeof (struct + grub_nilfs2_dat_entry)); + + grub_divmod64 (key * sizeof (struct grub_nilfs2_dat_entry), + NILFS2_BLOCK_SIZE (data), &offset); + + pptr = grub_nilfs2_bmap_lookup (data, &data->sroot.sr_dat, blockno, 0); + if (pptr == (grub_uint64_t) - 1) + { + grub_error (GRUB_ERR_BAD_FS, "btree lookup failure"); + return -1; + } + + grub_disk_read (disk, pptr * nilfs2_block_count, offset, + sizeof (struct grub_nilfs2_dat_entry), &entry); + + return grub_le_to_cpu64 (entry.de_blocknr); +} + + +static grub_disk_addr_t +grub_nilfs2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + struct grub_nilfs2_data *data = node->data; + struct grub_nilfs2_inode *inode = &node->inode; + grub_uint64_t pptr = -1; + + pptr = grub_nilfs2_bmap_lookup (data, inode, fileblock, 1); + if (pptr == (grub_uint64_t) - 1) + { + grub_error (GRUB_ERR_BAD_FS, "btree lookup failure"); + return -1; + } + + return pptr; +} + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_nilfs2_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t + sector, + unsigned offset, + unsigned length), + int pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_nilfs2_read_block, + grub_le_to_cpu64 (node->inode.i_size), + LOG2_NILFS2_BLOCK_SIZE (node->data)); + +} + +static grub_err_t +grub_nilfs2_read_checkpoint (struct grub_nilfs2_data *data, + grub_uint64_t cpno, + struct grub_nilfs2_checkpoint *cpp) +{ + grub_uint64_t blockno; + grub_uint32_t offset; + grub_uint64_t pptr; + grub_disk_t disk = data->disk; + unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data)); + + /* Assume sizeof(struct grub_nilfs2_cpfile_header) < + sizeof(struct grub_nilfs2_checkpoint). + */ + blockno = grub_divmod64 (cpno, NILFS2_BLOCK_SIZE (data) / + sizeof (struct grub_nilfs2_checkpoint), &offset); + + pptr = grub_nilfs2_bmap_lookup (data, &data->sroot.sr_cpfile, blockno, 1); + if (pptr == (grub_uint64_t) - 1) + { + return grub_error (GRUB_ERR_BAD_FS, "btree lookup failure"); + } + + return grub_disk_read (disk, pptr * nilfs2_block_count, + offset * sizeof (struct grub_nilfs2_checkpoint), + sizeof (struct grub_nilfs2_checkpoint), cpp); +} + +static inline grub_err_t +grub_nilfs2_read_last_checkpoint (struct grub_nilfs2_data *data, + struct grub_nilfs2_checkpoint *cpp) +{ + return grub_nilfs2_read_checkpoint (data, + grub_le_to_cpu64 (data-> + sblock.s_last_cno), + cpp); +} + +/* Read the inode INO for the file described by DATA into INODE. */ +static grub_err_t +grub_nilfs2_read_inode (struct grub_nilfs2_data *data, + grub_uint64_t ino, struct grub_nilfs2_inode *inodep) +{ + grub_uint64_t blockno; + unsigned int offset; + grub_uint64_t pptr; + grub_disk_t disk = data->disk; + unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data)); + + blockno = grub_nilfs2_palloc_entry_offset (data, ino, + sizeof (struct + grub_nilfs2_inode)); + + grub_divmod64 (sizeof (struct grub_nilfs2_inode) * ino, + NILFS2_BLOCK_SIZE (data), &offset); + pptr = grub_nilfs2_bmap_lookup (data, &data->ifile, blockno, 1); + if (pptr == (grub_uint64_t) - 1) + { + return grub_error (GRUB_ERR_BAD_FS, "btree lookup failure"); + } + + return grub_disk_read (disk, pptr * nilfs2_block_count, offset, + sizeof (struct grub_nilfs2_inode), inodep); +} + +static int +grub_nilfs2_valid_sb (struct grub_nilfs2_super_block *sbp) +{ + if (grub_le_to_cpu16 (sbp->s_magic) != NILFS2_SUPER_MAGIC) + return 0; + + if (grub_le_to_cpu32 (sbp->s_rev_level) != NILFS_SUPORT_REV) + return 0; + + return 1; +} + +static grub_err_t +grub_nilfs2_load_sb (struct grub_nilfs2_data *data) +{ + grub_disk_t disk = data->disk; + struct grub_nilfs2_super_block sb2; + grub_uint64_t partition_size; + int valid[2]; + int swp = 0; + grub_err_t err; + + /* Read first super block. */ + err = grub_disk_read (disk, NILFS_1ST_SUPER_BLOCK, 0, + sizeof (struct grub_nilfs2_super_block), &data->sblock); + if (err) + return err; + /* Make sure if 1st super block is valid. */ + valid[0] = grub_nilfs2_valid_sb (&data->sblock); + + partition_size = grub_disk_get_size (disk); + if (partition_size != GRUB_DISK_SIZE_UNKNOWN) + { + /* Read second super block. */ + err = grub_disk_read (disk, NILFS_2ND_SUPER_BLOCK (partition_size), 0, + sizeof (struct grub_nilfs2_super_block), &sb2); + if (err) + { + valid[1] = 0; + grub_errno = GRUB_ERR_NONE; + } + else + /* Make sure if 2nd super block is valid. */ + valid[1] = grub_nilfs2_valid_sb (&sb2); + } + else + /* 2nd super block may not exist, so it's invalid. */ + valid[1] = 0; + + if (!valid[0] && !valid[1]) + return grub_error (GRUB_ERR_BAD_FS, "not a nilfs2 filesystem"); + + swp = valid[1] && (!valid[0] || + grub_le_to_cpu64 (data->sblock.s_last_cno) < + grub_le_to_cpu64 (sb2.s_last_cno)); + + /* swap if first super block is invalid or older than second one. */ + if (swp) + grub_memcpy (&data->sblock, &sb2, + sizeof (struct grub_nilfs2_super_block)); + + return GRUB_ERR_NONE; +} + +static struct grub_nilfs2_data * +grub_nilfs2_mount (grub_disk_t disk) +{ + struct grub_nilfs2_data *data; + struct grub_nilfs2_segment_summary ss; + struct grub_nilfs2_checkpoint last_checkpoint; + grub_uint64_t last_pseg; + grub_uint32_t nblocks; + unsigned int nilfs2_block_count; + + data = grub_malloc (sizeof (struct grub_nilfs2_data)); + if (!data) + return 0; + + data->disk = disk; + + /* Read the superblock. */ + grub_nilfs2_load_sb (data); + if (grub_errno) + goto fail; + + nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data)); + + /* Read the last segment summary. */ + last_pseg = grub_le_to_cpu64 (data->sblock.s_last_pseg); + grub_disk_read (disk, last_pseg * nilfs2_block_count, 0, + sizeof (struct grub_nilfs2_segment_summary), &ss); + + if (grub_errno) + goto fail; + + /* Read the super root block. */ + nblocks = grub_le_to_cpu32 (ss.ss_nblocks); + grub_disk_read (disk, (last_pseg + (nblocks - 1)) * nilfs2_block_count, 0, + sizeof (struct grub_nilfs2_super_root), &data->sroot); + + if (grub_errno) + goto fail; + + grub_nilfs2_read_last_checkpoint (data, &last_checkpoint); + + if (grub_errno) + goto fail; + + grub_memcpy (&data->ifile, &last_checkpoint.cp_ifile_inode, + sizeof (struct grub_nilfs2_inode)); + + data->diropen.data = data; + data->diropen.ino = 2; + data->diropen.inode_read = 1; + data->inode = &data->diropen.inode; + + grub_nilfs2_read_inode (data, 2, data->inode); + + return data; + +fail: + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not a nilfs2 filesystem"); + + grub_free (data); + return 0; +} + +static char * +grub_nilfs2_read_symlink (grub_fshelp_node_t node) +{ + char *symlink; + struct grub_fshelp_node *diro = node; + + if (!diro->inode_read) + { + grub_nilfs2_read_inode (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + symlink = grub_malloc (grub_le_to_cpu64 (diro->inode.i_size) + 1); + if (!symlink) + return 0; + + grub_nilfs2_read_file (diro, 0, 0, + grub_le_to_cpu64 (diro->inode.i_size), symlink); + if (grub_errno) + { + grub_free (symlink); + return 0; + } + + symlink[grub_le_to_cpu64 (diro->inode.i_size)] = '\0'; + return symlink; +} + +static int +grub_nilfs2_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + unsigned int fpos = 0; + struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir; + + if (!diro->inode_read) + { + grub_nilfs2_read_inode (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + /* Iterate files. */ + while (fpos < grub_le_to_cpu64 (diro->inode.i_size)) + { + struct grub_nilfs2_dir_entry dirent; + + grub_nilfs2_read_file (diro, 0, fpos, + sizeof (struct grub_nilfs2_dir_entry), + (char *) &dirent); + if (grub_errno) + return 0; + + if (dirent.rec_len == 0) + return 0; + + if (dirent.name_len != 0) + { + char filename[dirent.name_len + 1]; + struct grub_fshelp_node *fdiro; + enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN; + + grub_nilfs2_read_file (diro, 0, + fpos + sizeof (struct grub_nilfs2_dir_entry), + dirent.name_len, filename); + if (grub_errno) + return 0; + + fdiro = grub_malloc (sizeof (struct grub_fshelp_node)); + if (!fdiro) + return 0; + + fdiro->data = diro->data; + fdiro->ino = grub_le_to_cpu64 (dirent.inode); + + filename[dirent.name_len] = '\0'; + + if (dirent.file_type != NILFS_FT_UNKNOWN) + { + fdiro->inode_read = 0; + + if (dirent.file_type == NILFS_FT_DIR) + type = GRUB_FSHELP_DIR; + else if (dirent.file_type == NILFS_FT_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else if (dirent.file_type == NILFS_FT_REG_FILE) + type = GRUB_FSHELP_REG; + } + else + { + /* The filetype can not be read from the dirent, read + the inode to get more information. */ + grub_nilfs2_read_inode (diro->data, + grub_le_to_cpu64 (dirent.inode), + &fdiro->inode); + if (grub_errno) + { + grub_free (fdiro); + return 0; + } + + fdiro->inode_read = 1; + + if ((grub_le_to_cpu16 (fdiro->inode.i_mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY) + type = GRUB_FSHELP_DIR; + else if ((grub_le_to_cpu16 (fdiro->inode.i_mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else if ((grub_le_to_cpu16 (fdiro->inode.i_mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_REG) + type = GRUB_FSHELP_REG; + } + + if (hook (filename, type, fdiro)) + return 1; + } + + fpos += grub_le_to_cpu16 (dirent.rec_len); + } + + return 0; +} + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_nilfs2_open (struct grub_file *file, const char *name) +{ + struct grub_nilfs2_data *data = NULL; + struct grub_fshelp_node *fdiro = 0; + + grub_dl_ref (my_mod); + + data = grub_nilfs2_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, + grub_nilfs2_iterate_dir, grub_nilfs2_read_symlink, + GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + if (!fdiro->inode_read) + { + grub_nilfs2_read_inode (data, fdiro->ino, &fdiro->inode); + if (grub_errno) + goto fail; + } + + grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_nilfs2_inode)); + grub_free (fdiro); + + file->size = grub_le_to_cpu64 (data->inode->i_size); + file->data = data; + file->offset = 0; + + return 0; + +fail: + if (fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_nilfs2_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +/* Read LEN bytes data from FILE into BUF. */ +static grub_ssize_t +grub_nilfs2_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_nilfs2_data *data = (struct grub_nilfs2_data *) file->data; + + return grub_nilfs2_read_file (&data->diropen, file->read_hook, + file->offset, len, buf); +} + +static grub_err_t +grub_nilfs2_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info * info)) +{ + struct grub_nilfs2_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + if (!node->inode_read) + { + grub_nilfs2_read_inode (data, node->ino, &node->inode); + if (!grub_errno) + node->inode_read = 1; + grub_errno = GRUB_ERR_NONE; + } + if (node->inode_read) + { + info.mtimeset = 1; + info.mtime = grub_le_to_cpu64 (node->inode.i_mtime); + } + + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_nilfs2_mount (device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (path, &data->diropen, &fdiro, + grub_nilfs2_iterate_dir, grub_nilfs2_read_symlink, + GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_nilfs2_iterate_dir (fdiro, iterate); + +fail: + if (fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_nilfs2_label (grub_device_t device, char **label) +{ + struct grub_nilfs2_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_nilfs2_mount (disk); + if (data) + *label = grub_strndup (data->sblock.s_volume_name, 14); + else + *label = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static grub_err_t +grub_nilfs2_uuid (grub_device_t device, char **uuid) +{ + struct grub_nilfs2_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_nilfs2_mount (disk); + if (data) + { + *uuid = + grub_xasprintf + ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%0x-%02x%02x%02x%02x%02x%02x", + data->sblock.s_uuid[0], data->sblock.s_uuid[1], + data->sblock.s_uuid[2], data->sblock.s_uuid[3], + data->sblock.s_uuid[4], data->sblock.s_uuid[5], + data->sblock.s_uuid[6], data->sblock.s_uuid[7], + data->sblock.s_uuid[8], data->sblock.s_uuid[9], + data->sblock.s_uuid[10], data->sblock.s_uuid[11], + data->sblock.s_uuid[12], data->sblock.s_uuid[13], + data->sblock.s_uuid[14], data->sblock.s_uuid[15]); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +/* Get mtime. */ +static grub_err_t +grub_nilfs2_mtime (grub_device_t device, grub_int32_t * tm) +{ + struct grub_nilfs2_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_nilfs2_mount (disk); + if (!data) + *tm = 0; + else + *tm = (grub_int32_t) grub_le_to_cpu64 (data->sblock.s_mtime); + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + + +static struct grub_fs grub_nilfs2_fs = { + .name = "nilfs2", + .dir = grub_nilfs2_dir, + .open = grub_nilfs2_open, + .read = grub_nilfs2_read, + .close = grub_nilfs2_close, + .label = grub_nilfs2_label, + .uuid = grub_nilfs2_uuid, + .mtime = grub_nilfs2_mtime, +#ifdef GRUB_UTIL + .reserved_first_sector = 1, +#endif + .next = 0 +}; + +GRUB_MOD_INIT (nilfs2) +{ + grub_fs_register (&grub_nilfs2_fs); + my_mod = mod; +} + +GRUB_MOD_FINI (nilfs2) +{ + grub_fs_unregister (&grub_nilfs2_fs); +} diff --git a/libr/fs/p/grub/fs/ntfs.c b/libr/fs/p/grub/fs/ntfs.c new file mode 100644 index 0000000000..414f6513d9 --- /dev/null +++ b/libr/fs/p/grub/fs/ntfs.c @@ -0,0 +1,1115 @@ +/* ntfs.c - NTFS filesystem */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static grub_dl_t my_mod; + +ntfscomp_func_t grub_ntfscomp_func; + +static grub_err_t +fixup (struct grub_ntfs_data *data, char *buf, int len, char *magic) +{ + int ss; + char *pu; + grub_uint16_t us; + + if (grub_memcmp (buf, magic, 4)) + return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic); + + ss = u16at (buf, 6) - 1; + if (ss * (int) data->blocksize != len * GRUB_DISK_SECTOR_SIZE) + return grub_error (GRUB_ERR_BAD_FS, "size not match", + ss * (int) data->blocksize, + len * GRUB_DISK_SECTOR_SIZE); + pu = buf + u16at (buf, 4); + us = u16at (pu, 0); + buf -= 2; + while (ss > 0) + { + buf += data->blocksize; + pu += 2; + if (u16at (buf, 0) != us) + return grub_error (GRUB_ERR_BAD_FS, "fixup signature not match"); + v16at (buf, 0) = v16at (pu, 0); + ss--; + } + + return 0; +} + +static grub_err_t read_mft (struct grub_ntfs_data *data, char *buf, + grub_uint32_t mftno); +static grub_err_t read_attr (struct grub_ntfs_attr *at, char *dest, + grub_disk_addr_t ofs, grub_size_t len, + int cached, + void + NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t + sector, + unsigned offset, + unsigned length)); + +static grub_err_t read_data (struct grub_ntfs_attr *at, char *pa, char *dest, + grub_disk_addr_t ofs, grub_size_t len, + int cached, + void + NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t + sector, + unsigned offset, + unsigned length)); + +static void +init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft) +{ + at->mft = mft; + at->flags = (mft == &mft->data->mmft) ? AF_MMFT : 0; + at->attr_nxt = mft->buf + u16at (mft->buf, 0x14); + at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL; +} + +static void +free_attr (struct grub_ntfs_attr *at) +{ + grub_free (at->emft_buf); + grub_free (at->edat_buf); + grub_free (at->sbuf); +} + +static char * +find_attr (struct grub_ntfs_attr *at, unsigned char attr) +{ + if (at->flags & AF_ALST) + { + retry: + while (at->attr_nxt < at->attr_end) + { + at->attr_cur = at->attr_nxt; + at->attr_nxt += u16at (at->attr_cur, 4); + if (((unsigned char) *at->attr_cur == attr) || (attr == 0)) + { + char *new_pos; + + if (at->flags & AF_MMFT) + { + if ((grub_disk_read + (at->mft->data->disk, v32at (at->attr_cur, 0x10), 0, + 512, at->emft_buf)) + || + (grub_disk_read + (at->mft->data->disk, v32at (at->attr_cur, 0x14), 0, + 512, at->emft_buf + 512))) + return NULL; + + if (fixup + (at->mft->data, at->emft_buf, at->mft->data->mft_size, + "FILE")) + return NULL; + } + else + { + if (read_mft (at->mft->data, at->emft_buf, + u32at (at->attr_cur, 0x10))) + return NULL; + } + + new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)]; + while ((unsigned char) *new_pos != 0xFF) + { + if (((unsigned char) *new_pos == + (unsigned char) *at->attr_cur) + && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18))) + { + return new_pos; + } + new_pos += u16at (new_pos, 4); + } + grub_error (GRUB_ERR_BAD_FS, + "can\'t find 0x%X in attribute list", + (unsigned char) *at->attr_cur); + return NULL; + } + } + return NULL; + } + at->attr_cur = at->attr_nxt; + while ((unsigned char) *at->attr_cur != 0xFF) + { + at->attr_nxt += u16at (at->attr_cur, 4); + if ((unsigned char) *at->attr_cur == AT_ATTRIBUTE_LIST) + at->attr_end = at->attr_cur; + if (((unsigned char) *at->attr_cur == attr) || (attr == 0)) + return at->attr_cur; + at->attr_cur = at->attr_nxt; + } + if (at->attr_end) + { + char *pa; + + at->emft_buf = grub_malloc (at->mft->data->mft_size << BLK_SHR); + if (at->emft_buf == NULL) + return NULL; + + pa = at->attr_end; + if (pa[8]) + { + int n; + + n = ((u32at (pa, 0x30) + GRUB_DISK_SECTOR_SIZE - 1) + & (~(GRUB_DISK_SECTOR_SIZE - 1))); + at->attr_cur = at->attr_end; + at->edat_buf = grub_malloc (n); + if (!at->edat_buf) + return NULL; + if (read_data (at, pa, at->edat_buf, 0, n, 0, 0)) + { + grub_error (GRUB_ERR_BAD_FS, + "fail to read non-resident attribute list"); + return NULL; + } + at->attr_nxt = at->edat_buf; + at->attr_end = at->edat_buf + u32at (pa, 0x30); + } + else + { + at->attr_nxt = at->attr_end + u16at (pa, 0x14); + at->attr_end = at->attr_end + u32at (pa, 4); + } + at->flags |= AF_ALST; + while (at->attr_nxt < at->attr_end) + { + if (((unsigned char) *at->attr_nxt == attr) || (attr == 0)) + break; + at->attr_nxt += u16at (at->attr_nxt, 4); + } + if (at->attr_nxt >= at->attr_end) + return NULL; + + if ((at->flags & AF_MMFT) && (attr == AT_DATA)) + { + at->flags |= AF_GPOS; + at->attr_cur = at->attr_nxt; + pa = at->attr_cur; + v32at (pa, 0x10) = at->mft->data->mft_start; + v32at (pa, 0x14) = at->mft->data->mft_start + 1; + pa = at->attr_nxt + u16at (pa, 4); + while (pa < at->attr_end) + { + if ((unsigned char) *pa != attr) + break; + if (read_attr + (at, pa + 0x10, + u32at (pa, 0x10) * (at->mft->data->mft_size << BLK_SHR), + at->mft->data->mft_size << BLK_SHR, 0, 0)) + return NULL; + pa += u16at (pa, 4); + } + at->attr_nxt = at->attr_cur; + at->flags &= ~AF_GPOS; + } + goto retry; + } + return NULL; +} + +static char * +locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft, + unsigned char attr) +{ + char *pa; + + init_attr (at, mft); + if ((pa = find_attr (at, attr)) == NULL) + return NULL; + if ((at->flags & AF_ALST) == 0) + { + while (1) + { + if ((pa = find_attr (at, attr)) == NULL) + break; + if (at->flags & AF_ALST) + return pa; + } + grub_errno = GRUB_ERR_NONE; + free_attr (at); + init_attr (at, mft); + pa = find_attr (at, attr); + } + return pa; +} + +static char * +read_run_data (char *run, int nn, grub_disk_addr_t * val, int sig) +{ + grub_disk_addr_t r, v; + + r = 0; + v = 1; + + while (nn--) + { + r += v * (*(unsigned char *) (run++)); + v <<= 8; + } + + if ((sig) && (r & (v >> 1))) + r -= v; + + *val = r; + return run; +} + +grub_err_t +grub_ntfs_read_run_list (struct grub_ntfs_rlst * ctx) +{ + int c1, c2; + grub_disk_addr_t val; + char *run; + + run = ctx->cur_run; +retry: + c1 = ((unsigned char) (*run) & 0xF); + c2 = ((unsigned char) (*run) >> 4); + if (!c1) + { + if ((ctx->attr) && (ctx->attr->flags & AF_ALST)) + { + void NESTED_FUNC_ATTR (*save_hook) (grub_disk_addr_t sector, + unsigned offset, + unsigned length); + + save_hook = ctx->comp.disk->read_hook; + ctx->comp.disk->read_hook = 0; + run = find_attr (ctx->attr, (unsigned char) *ctx->attr->attr_cur); + ctx->comp.disk->read_hook = save_hook; + if (run) + { + if (run[8] == 0) + return grub_error (GRUB_ERR_BAD_FS, + "$DATA should be non-resident"); + + run += u16at (run, 0x20); + ctx->curr_lcn = 0; + goto retry; + } + } + return grub_error (GRUB_ERR_BAD_FS, "run list overflown"); + } + run = read_run_data (run + 1, c1, &val, 0); /* length of current VCN */ + ctx->curr_vcn = ctx->next_vcn; + ctx->next_vcn += val; + run = read_run_data (run, c2, &val, 1); /* offset to previous LCN */ + ctx->curr_lcn += val; + if (val == 0) + ctx->flags |= RF_BLNK; + else + ctx->flags &= ~RF_BLNK; + ctx->cur_run = run; + return 0; +} + +static grub_disk_addr_t +grub_ntfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block) +{ + struct grub_ntfs_rlst *ctx; + + ctx = (struct grub_ntfs_rlst *) node; + if (block >= ctx->next_vcn) + { + if (grub_ntfs_read_run_list (ctx)) + return -1; + return ctx->curr_lcn; + } + else + return (ctx->flags & RF_BLNK) ? 0 : (block - + ctx->curr_vcn + ctx->curr_lcn); +} + +static grub_err_t +read_data (struct grub_ntfs_attr *at, char *pa, char *dest, + grub_disk_addr_t ofs, grub_size_t len, int cached, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, + unsigned length)) +{ + grub_disk_addr_t vcn; + struct grub_ntfs_rlst cc, *ctx; + + if (len == 0) + return 0; + + grub_memset (&cc, 0, sizeof (cc)); + ctx = &cc; + ctx->attr = at; + ctx->comp.spc = at->mft->data->spc; + ctx->comp.disk = at->mft->data->disk; + + if (pa[8] == 0) + { + if (ofs + len > u32at (pa, 0x10)) + return grub_error (GRUB_ERR_BAD_FS, "read out of range"); + grub_memcpy (dest, pa + u32at (pa, 0x14) + ofs, len); + return 0; + } + + if (u16at (pa, 0xC) & FLAG_COMPRESSED) + ctx->flags |= RF_COMP; + else + ctx->flags &= ~RF_COMP; + ctx->cur_run = pa + u16at (pa, 0x20); + + if (ctx->flags & RF_COMP) + { + if (!cached) + return grub_error (GRUB_ERR_BAD_FS, "attribute can\'t be compressed"); + + if (at->sbuf) + { + if ((ofs & (~(COM_LEN - 1))) == at->save_pos) + { + grub_disk_addr_t n; + + n = COM_LEN - (ofs - at->save_pos); + if (n > len) + n = len; + + grub_memcpy (dest, at->sbuf + ofs - at->save_pos, n); + if (n == len) + return 0; + + dest += n; + len -= n; + ofs += n; + } + } + else + { + at->sbuf = grub_malloc (COM_LEN); + if (at->sbuf == NULL) + return grub_errno; + at->save_pos = 1; + } + + vcn = ctx->target_vcn = (ofs >> COM_LOG_LEN) * (COM_SEC / ctx->comp.spc); + ctx->target_vcn &= ~0xF; + } + else + vcn = ctx->target_vcn = grub_divmod64 (ofs >> BLK_SHR, ctx->comp.spc, 0); + + ctx->next_vcn = u32at (pa, 0x10); + ctx->curr_lcn = 0; + while (ctx->next_vcn <= ctx->target_vcn) + { + if (grub_ntfs_read_run_list (ctx)) + return grub_errno; + } + + if (at->flags & AF_GPOS) + { + grub_disk_addr_t st0, st1; + grub_uint32_t m; + + grub_divmod64 (ofs >> BLK_SHR, ctx->comp.spc, &m); + + st0 = + (ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc + m; + st1 = st0 + 1; + if (st1 == + (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc) + { + if (grub_ntfs_read_run_list (ctx)) + return grub_errno; + st1 = ctx->curr_lcn * ctx->comp.spc; + } + v32at (dest, 0) = st0; + v32at (dest, 4) = st1; + return 0; + } + + if (!(ctx->flags & RF_COMP)) + { + unsigned int pow; + + if (!grub_fshelp_log2blksize (ctx->comp.spc, &pow)) + grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx, + read_hook, ofs, len, dest, + grub_ntfs_read_block, ofs + len, pow); + return grub_errno; + } + + return (grub_ntfscomp_func) ? grub_ntfscomp_func (at, dest, ofs, len, ctx, + vcn) : + grub_error (GRUB_ERR_BAD_FS, "ntfscomp module not loaded"); +} + +static grub_err_t +read_attr (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs, + grub_size_t len, int cached, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, + unsigned length)) +{ + char *save_cur; + unsigned char attr; + char *pp; + grub_err_t ret; + + save_cur = at->attr_cur; + at->attr_nxt = at->attr_cur; + attr = (unsigned char) *at->attr_nxt; + if (at->flags & AF_ALST) + { + char *pa; + grub_disk_addr_t vcn; + + vcn = grub_divmod64 (ofs, at->mft->data->spc << BLK_SHR, 0); + pa = at->attr_nxt + u16at (at->attr_nxt, 4); + while (pa < at->attr_end) + { + if ((unsigned char) *pa != attr) + break; + if (u32at (pa, 8) > vcn) + break; + at->attr_nxt = pa; + pa += u16at (pa, 4); + } + } + pp = find_attr (at, attr); + if (pp) + ret = read_data (at, pp, dest, ofs, len, cached, read_hook); + else + ret = + (grub_errno) ? grub_errno : grub_error (GRUB_ERR_BAD_FS, + "attribute not found"); + at->attr_cur = save_cur; + return ret; +} + +static grub_err_t +read_mft (struct grub_ntfs_data *data, char *buf, grub_uint32_t mftno) +{ + if (read_attr + (&data->mmft.attr, buf, mftno * ((grub_disk_addr_t) data->mft_size << BLK_SHR), + data->mft_size << BLK_SHR, 0, 0)) + return grub_error (GRUB_ERR_BAD_FS, "read MFT 0x%X fails", mftno); + return fixup (data, buf, data->mft_size, "FILE"); +} + +static grub_err_t +init_file (struct grub_ntfs_file *mft, grub_uint32_t mftno) +{ + unsigned short flag; + + mft->inode_read = 1; + + mft->buf = grub_malloc (mft->data->mft_size << BLK_SHR); + if (mft->buf == NULL) + return grub_errno; + + if (read_mft (mft->data, mft->buf, mftno)) + return grub_errno; + + flag = u16at (mft->buf, 0x16); + if ((flag & 1) == 0) + return grub_error (GRUB_ERR_BAD_FS, "MFT 0x%X is not in use", mftno); + + if ((flag & 2) == 0) + { + char *pa; + + pa = locate_attr (&mft->attr, mft, AT_DATA); + if (pa == NULL) + return grub_error (GRUB_ERR_BAD_FS, "no $DATA in MFT 0x%X", mftno); + + if (!pa[8]) + mft->size = u32at (pa, 0x10); + else + mft->size = u64at (pa, 0x30); + + if ((mft->attr.flags & AF_ALST) == 0) + mft->attr.attr_end = 0; /* Don't jump to attribute list */ + } + else + init_attr (&mft->attr, mft); + + return 0; +} + +static void +free_file (struct grub_ntfs_file *mft) +{ + free_attr (&mft->attr); + grub_free (mft->buf); +} + +static int +list_file (struct grub_ntfs_file *diro, char *pos, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + char *np; + int ns; + + while (1) + { + char *ustr, namespace; + + if (pos[0xC] & 2) /* end signature */ + break; + + np = pos + 0x50; + ns = (unsigned char) *(np++); + namespace = *(np++); + + /* + * Ignore files in DOS namespace, as they will reappear as Win32 + * names. + */ + if ((ns) && (namespace != 2)) + { + enum grub_fshelp_filetype type; + struct grub_ntfs_file *fdiro; + + if (u16at (pos, 4)) + { + grub_error (GRUB_ERR_BAD_FS, "64-bit MFT number"); + return 0; + } + + type = + (u32at (pos, 0x48) & ATTR_DIRECTORY) ? GRUB_FSHELP_DIR : + GRUB_FSHELP_REG; + + fdiro = grub_zalloc (sizeof (struct grub_ntfs_file)); + if (!fdiro) + return 0; + + fdiro->data = diro->data; + fdiro->ino = u32at (pos, 0); + + ustr = grub_malloc (ns * 4 + 1); + if (ustr == NULL) + return 0; + *grub_utf16_to_utf8 ((grub_uint8_t *) ustr, (grub_uint16_t *) np, + ns) = '\0'; + + if (namespace) + type |= GRUB_FSHELP_CASE_INSENSITIVE; + + if (hook (ustr, type, fdiro)) + { + grub_free (ustr); + return 1; + } + + grub_free (ustr); + } + pos += u16at (pos, 8); + } + return 0; +} + +static int +grub_ntfs_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + unsigned char *bitmap; + struct grub_ntfs_attr attr, *at; + char *cur_pos, *indx, *bmp; + int ret = 0; + grub_size_t bitmap_len; + struct grub_ntfs_file *mft; + + mft = (struct grub_ntfs_file *) dir; + + if (!mft->inode_read) + { + if (init_file (mft, mft->ino)) + return 0; + } + + indx = NULL; + bmp = NULL; + + at = &attr; + init_attr (at, mft); + while (1) + { + if ((cur_pos = find_attr (at, AT_INDEX_ROOT)) == NULL) + { + grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT"); + goto done; + } + + /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */ + if ((u32at (cur_pos, 8) != 0x180400) || + (u32at (cur_pos, 0x18) != 0x490024) || + (u32at (cur_pos, 0x1C) != 0x300033)) + continue; + cur_pos += u16at (cur_pos, 0x14); + if (*cur_pos != 0x30) /* Not filename index */ + continue; + break; + } + + cur_pos += 0x10; /* Skip index root */ + ret = list_file (mft, cur_pos + u16at (cur_pos, 0), hook); + if (ret) + goto done; + + bitmap = NULL; + bitmap_len = 0; + free_attr (at); + init_attr (at, mft); + while ((cur_pos = find_attr (at, AT_BITMAP)) != NULL) + { + int ofs; + + ofs = (unsigned char) cur_pos[0xA]; + /* Namelen=4, Name="$I30" */ + if ((cur_pos[9] == 4) && + (u32at (cur_pos, ofs) == 0x490024) && + (u32at (cur_pos, ofs + 4) == 0x300033)) + { + int is_resident = (cur_pos[8] == 0); + + bitmap_len = ((is_resident) ? u32at (cur_pos, 0x10) : + u32at (cur_pos, 0x28)); + + bmp = grub_malloc (bitmap_len); + if (bmp == NULL) + goto done; + + if (is_resident) + { + grub_memcpy (bmp, (char *) (cur_pos + u16at (cur_pos, 0x14)), + bitmap_len); + } + else + { + if (read_data (at, cur_pos, bmp, 0, bitmap_len, 0, 0)) + { + grub_error (GRUB_ERR_BAD_FS, + "fails to read non-resident $BITMAP"); + goto done; + } + bitmap_len = u32at (cur_pos, 0x30); + } + + bitmap = (unsigned char *) bmp; + break; + } + } + + free_attr (at); + cur_pos = locate_attr (at, mft, AT_INDEX_ALLOCATION); + while (cur_pos != NULL) + { + /* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */ + if ((u32at (cur_pos, 8) == 0x400401) && + (u32at (cur_pos, 0x40) == 0x490024) && + (u32at (cur_pos, 0x44) == 0x300033)) + break; + cur_pos = find_attr (at, AT_INDEX_ALLOCATION); + } + + if ((!cur_pos) && (bitmap)) + { + grub_error (GRUB_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION"); + goto done; + } + + if (bitmap) + { + grub_disk_addr_t v, i; + + indx = grub_malloc (mft->data->idx_size << BLK_SHR); + if (indx == NULL) + goto done; + + v = 1; + for (i = 0; i < (grub_disk_addr_t)bitmap_len * 8; i++) + { + if (*bitmap & v) + { + if ((read_attr + (at, indx, i * (mft->data->idx_size << BLK_SHR), + (mft->data->idx_size << BLK_SHR), 0, 0)) + || (fixup (mft->data, indx, mft->data->idx_size, "INDX"))) + goto done; + ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)], hook); + if (ret) + goto done; + } + v <<= 1; + if (v >= 0x100) + { + v = 1; + bitmap++; + } + } + } + +done: + free_attr (at); + grub_free (indx); + grub_free (bmp); + + return ret; +} + +static struct grub_ntfs_data * +grub_ntfs_mount (grub_disk_t disk) +{ + struct grub_ntfs_bpb bpb; + struct grub_ntfs_data *data = 0; + + if (!disk) + goto fail; + + data = (struct grub_ntfs_data *) grub_zalloc (sizeof (*data)); + if (!data) + goto fail; + + data->disk = disk; + + /* Read the BPB. */ + if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb)) + goto fail; + + if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4)) + goto fail; + + data->blocksize = grub_le_to_cpu16 (bpb.bytes_per_sector); + data->spc = bpb.sectors_per_cluster * (data->blocksize >> BLK_SHR); + + if (bpb.clusters_per_mft > 0) + data->mft_size = data->spc * bpb.clusters_per_mft; + else + data->mft_size = 1 << (-bpb.clusters_per_mft - BLK_SHR); + + if (bpb.clusters_per_index > 0) + data->idx_size = data->spc * bpb.clusters_per_index; + else + data->idx_size = 1 << (-bpb.clusters_per_index - BLK_SHR); + + data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) * data->spc; + + if ((data->mft_size > MAX_MFT) || (data->idx_size > MAX_IDX)) + goto fail; + + data->mmft.data = data; + data->cmft.data = data; + + data->mmft.buf = grub_malloc (data->mft_size << BLK_SHR); + if (!data->mmft.buf) + goto fail; + + if (grub_disk_read + (disk, data->mft_start, 0, data->mft_size << BLK_SHR, data->mmft.buf)) + goto fail; + + data->uuid = grub_le_to_cpu64 (bpb.num_serial); + + if (fixup (data, data->mmft.buf, data->mft_size, "FILE")) + goto fail; + + if (!locate_attr (&data->mmft.attr, &data->mmft, AT_DATA)) + goto fail; + + if (init_file (&data->cmft, FILE_ROOT)) + goto fail; + + return data; + +fail: + grub_error (GRUB_ERR_BAD_FS, "not an ntfs filesystem"); + + if (data) + { + free_file (&data->mmft); + free_file (&data->cmft); + grub_free (data); + } + return 0; +} + +static grub_err_t +grub_ntfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_ntfs_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_ntfs_mount (device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (path, &data->cmft, &fdiro, grub_ntfs_iterate_dir, + 0, GRUB_FSHELP_DIR); + + if (grub_errno) + goto fail; + + grub_ntfs_iterate_dir (fdiro, iterate); + +fail: + if ((fdiro) && (fdiro != &data->cmft)) + { + free_file (fdiro); + grub_free (fdiro); + } + if (data) + { + free_file (&data->mmft); + free_file (&data->cmft); + grub_free (data); + } + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_ntfs_open (grub_file_t file, const char *name) +{ + struct grub_ntfs_data *data = 0; + struct grub_fshelp_node *mft = 0; + + grub_dl_ref (my_mod); + + data = grub_ntfs_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->cmft, &mft, grub_ntfs_iterate_dir, + 0, GRUB_FSHELP_REG); + + if (grub_errno) + goto fail; + + if (mft != &data->cmft) + { + free_file (&data->cmft); + grub_memcpy (&data->cmft, mft, sizeof (*mft)); + grub_free (mft); + if (!data->cmft.inode_read) + { + if (init_file (&data->cmft, data->cmft.ino)) + goto fail; + } + } + + file->size = data->cmft.size; + file->data = data; + file->offset = 0; + + return 0; + +fail: + if (data) + { + free_file (&data->mmft); + free_file (&data->cmft); + grub_free (data); + } + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_ssize_t +grub_ntfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_ntfs_file *mft; + + mft = &((struct grub_ntfs_data *) file->data)->cmft; + if (file->read_hook) + mft->attr.save_pos = 1; + + read_attr (&mft->attr, buf, file->offset, len, 1, file->read_hook); + return (grub_errno) ? 0 : len; +} + +static grub_err_t +grub_ntfs_close (grub_file_t file) +{ + struct grub_ntfs_data *data; + + data = file->data; + + if (data) + { + free_file (&data->mmft); + free_file (&data->cmft); + grub_free (data); + } + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_ntfs_label (grub_device_t device, char **label) +{ + struct grub_ntfs_data *data = 0; + struct grub_fshelp_node *mft = 0; + char *pa; + + grub_dl_ref (my_mod); + + *label = 0; + + data = grub_ntfs_mount (device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file ("/$Volume", &data->cmft, &mft, grub_ntfs_iterate_dir, + 0, GRUB_FSHELP_REG); + + if (grub_errno) + goto fail; + + if (!mft->inode_read) + { + mft->buf = grub_malloc (mft->data->mft_size << BLK_SHR); + if (mft->buf == NULL) + goto fail; + + if (read_mft (mft->data, mft->buf, mft->ino)) + goto fail; + } + + init_attr (&mft->attr, mft); + pa = find_attr (&mft->attr, AT_VOLUME_NAME); + if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10))) + { + char *buf; + int len; + + len = u32at (pa, 0x10) / 2; + buf = grub_malloc (len * 4 + 1); + pa += u16at (pa, 0x14); + *grub_utf16_to_utf8 ((grub_uint8_t *) buf, (grub_uint16_t *) pa, len) = + '\0'; + *label = buf; + } + +fail: + if ((mft) && (mft != &data->cmft)) + { + free_file (mft); + grub_free (mft); + } + if (data) + { + free_file (&data->mmft); + free_file (&data->cmft); + grub_free (data); + } + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_ntfs_uuid (grub_device_t device, char **uuid) +{ + struct grub_ntfs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_ntfs_mount (disk); + if (data) + { + char *ptr; + *uuid = grub_xasprintf ("%016llx", (unsigned long long) data->uuid); + if (*uuid) + for (ptr = *uuid; *ptr; ptr++) + *ptr = grub_toupper (*ptr); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static struct grub_fs grub_ntfs_fs = + { + .name = "ntfs", + .dir = grub_ntfs_dir, + .open = grub_ntfs_open, + .read = grub_ntfs_read, + .close = grub_ntfs_close, + .label = grub_ntfs_label, + .uuid = grub_ntfs_uuid, +#ifdef GRUB_UTIL + .reserved_first_sector = 1, +#endif + .next = 0 +}; + +GRUB_MOD_INIT (ntfs) +{ + grub_fs_register (&grub_ntfs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI (ntfs) +{ + grub_fs_unregister (&grub_ntfs_fs); +} diff --git a/libr/fs/p/grub/fs/ntfscomp.c b/libr/fs/p/grub/fs/ntfscomp.c new file mode 100644 index 0000000000..c29979edc4 --- /dev/null +++ b/libr/fs/p/grub/fs/ntfscomp.c @@ -0,0 +1,374 @@ +/* ntfscomp.c - compression support for the NTFS filesystem */ +/* + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +static grub_err_t +decomp_nextvcn (struct grub_ntfs_comp *cc) +{ + if (cc->comp_head >= cc->comp_tail) + return grub_error (GRUB_ERR_BAD_FS, "compression block overflown"); + if (grub_disk_read + (cc->disk, + (cc->comp_table[cc->comp_head][1] - + (cc->comp_table[cc->comp_head][0] - cc->cbuf_vcn)) * cc->spc, 0, + cc->spc << BLK_SHR, cc->cbuf)) + return grub_errno; + cc->cbuf_vcn++; + if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head][0])) + cc->comp_head++; + cc->cbuf_ofs = 0; + return 0; +} + +static grub_err_t +decomp_getch (struct grub_ntfs_comp *cc, unsigned char *res) +{ + if (cc->cbuf_ofs >= (cc->spc << BLK_SHR)) + { + if (decomp_nextvcn (cc)) + return grub_errno; + } + *res = (unsigned char) cc->cbuf[cc->cbuf_ofs++]; + return 0; +} + +static grub_err_t +decomp_get16 (struct grub_ntfs_comp *cc, grub_uint16_t * res) +{ + unsigned char c1 = 0, c2 = 0; + + if ((decomp_getch (cc, &c1)) || (decomp_getch (cc, &c2))) + return grub_errno; + *res = ((grub_uint16_t) c2) * 256 + ((grub_uint16_t) c1); + return 0; +} + +/* Decompress a block (4096 bytes) */ +static grub_err_t +decomp_block (struct grub_ntfs_comp *cc, char *dest) +{ + grub_uint16_t flg, cnt; + + if (decomp_get16 (cc, &flg)) + return grub_errno; + cnt = (flg & 0xFFF) + 1; + + if (dest) + { + if (flg & 0x8000) + { + unsigned char tag; + grub_uint32_t bits, copied; + + bits = copied = tag = 0; + while (cnt > 0) + { + if (copied > COM_LEN) + return grub_error (GRUB_ERR_BAD_FS, + "compression block too large"); + + if (!bits) + { + if (decomp_getch (cc, &tag)) + return grub_errno; + + bits = 8; + cnt--; + if (cnt <= 0) + break; + } + if (tag & 1) + { + grub_uint32_t i, len, delta, code, lmask, dshift; + grub_uint16_t word; + + if (decomp_get16 (cc, &word)) + return grub_errno; + + code = word; + cnt -= 2; + + if (!copied) + { + grub_error (GRUB_ERR_BAD_FS, "nontext window empty"); + return 0; + } + + for (i = copied - 1, lmask = 0xFFF, dshift = 12; i >= 0x10; + i >>= 1) + { + lmask >>= 1; + dshift--; + } + + delta = code >> dshift; + len = (code & lmask) + 3; + + for (i = 0; i < len; i++) + { + dest[copied] = dest[copied - delta - 1]; + copied++; + } + } + else + { + unsigned char ch = 0; + + if (decomp_getch (cc, &ch)) + return grub_errno; + dest[copied++] = ch; + cnt--; + } + tag >>= 1; + bits--; + } + return 0; + } + else + { + if (cnt != COM_LEN) + return grub_error (GRUB_ERR_BAD_FS, + "invalid compression block size"); + } + } + + while (cnt > 0) + { + int n; + + n = (cc->spc << BLK_SHR) - cc->cbuf_ofs; + if (n > cnt) + n = cnt; + if ((dest) && (n)) + { + grub_memcpy (dest, &cc->cbuf[cc->cbuf_ofs], n); + dest += n; + } + cnt -= n; + cc->cbuf_ofs += n; + if ((cnt) && (decomp_nextvcn (cc))) + return grub_errno; + } + return 0; +} + +static grub_err_t +read_block (struct grub_ntfs_rlst *ctx, char *buf, int num) +{ + int cpb = COM_SEC / ctx->comp.spc; + + while (num) + { + int nn; + + if ((ctx->target_vcn & 0xF) == 0) + { + + if (ctx->comp.comp_head != ctx->comp.comp_tail) + return grub_error (GRUB_ERR_BAD_FS, "invalid compression block"); + ctx->comp.comp_head = ctx->comp.comp_tail = 0; + ctx->comp.cbuf_vcn = ctx->target_vcn; + ctx->comp.cbuf_ofs = (ctx->comp.spc << BLK_SHR); + if (ctx->target_vcn >= ctx->next_vcn) + { + if (grub_ntfs_read_run_list (ctx)) + return grub_errno; + } + while (ctx->target_vcn + 16 > ctx->next_vcn) + { + if (ctx->flags & RF_BLNK) + break; + ctx->comp.comp_table[ctx->comp.comp_tail][0] = ctx->next_vcn; + ctx->comp.comp_table[ctx->comp.comp_tail][1] = + ctx->curr_lcn + ctx->next_vcn - ctx->curr_vcn; + ctx->comp.comp_tail++; + if (grub_ntfs_read_run_list (ctx)) + return grub_errno; + } + } + + nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) / cpb; + if (nn > num) + nn = num; + num -= nn; + + if (ctx->flags & RF_BLNK) + { + ctx->target_vcn += nn * cpb; + if (ctx->comp.comp_tail == 0) + { + if (buf) + { + grub_memset (buf, 0, nn * COM_LEN); + buf += nn * COM_LEN; + } + } + else + { + while (nn) + { + if (decomp_block (&ctx->comp, buf)) + return grub_errno; + if (buf) + buf += COM_LEN; + nn--; + } + } + } + else + { + nn *= cpb; + while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn)) + { + int tt; + + tt = + ctx->comp.comp_table[ctx->comp.comp_head][0] - + ctx->target_vcn; + if (tt > nn) + tt = nn; + ctx->target_vcn += tt; + if (buf) + { + if (grub_disk_read + (ctx->comp.disk, + (ctx->comp.comp_table[ctx->comp.comp_head][1] - + (ctx->comp.comp_table[ctx->comp.comp_head][0] - + ctx->target_vcn)) * ctx->comp.spc, 0, + tt * (ctx->comp.spc << BLK_SHR), buf)) + return grub_errno; + buf += tt * (ctx->comp.spc << BLK_SHR); + } + nn -= tt; + if (ctx->target_vcn >= + ctx->comp.comp_table[ctx->comp.comp_head][0]) + ctx->comp.comp_head++; + } + if (nn) + { + if (buf) + { + if (grub_disk_read + (ctx->comp.disk, + (ctx->target_vcn - ctx->curr_vcn + + ctx->curr_lcn) * ctx->comp.spc, 0, + nn * (ctx->comp.spc << BLK_SHR), buf)) + return grub_errno; + buf += nn * (ctx->comp.spc << BLK_SHR); + } + ctx->target_vcn += nn; + } + } + } + return 0; +} + +static grub_err_t +ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_uint32_t ofs, + grub_uint32_t len, struct grub_ntfs_rlst *ctx, grub_uint32_t vcn) +{ + grub_err_t ret; + + ctx->comp.comp_head = ctx->comp.comp_tail = 0; + ctx->comp.cbuf = grub_malloc ((ctx->comp.spc) << BLK_SHR); + if (!ctx->comp.cbuf) + return 0; + + ret = 0; + + //ctx->comp.disk->read_hook = read_hook; + + if ((vcn > ctx->target_vcn) && + (read_block + (ctx, NULL, ((vcn - ctx->target_vcn) * ctx->comp.spc) / COM_SEC))) + { + ret = grub_errno; + goto quit; + } + + if (ofs % COM_LEN) + { + grub_uint32_t t, n, o; + + t = ctx->target_vcn * (ctx->comp.spc << BLK_SHR); + if (read_block (ctx, at->sbuf, 1)) + { + ret = grub_errno; + goto quit; + } + + at->save_pos = t; + + o = ofs % COM_LEN; + n = COM_LEN - o; + if (n > len) + n = len; + grub_memcpy (dest, &at->sbuf[o], n); + if (n == len) + goto quit; + dest += n; + len -= n; + } + + if (read_block (ctx, dest, len / COM_LEN)) + { + ret = grub_errno; + goto quit; + } + + dest += (len / COM_LEN) * COM_LEN; + len = len % COM_LEN; + if (len) + { + grub_uint32_t t; + + t = ctx->target_vcn * (ctx->comp.spc << BLK_SHR); + if (read_block (ctx, at->sbuf, 1)) + { + ret = grub_errno; + goto quit; + } + + at->save_pos = t; + + grub_memcpy (dest, at->sbuf, len); + } + +quit: + //ctx->comp.disk->read_hook = 0; + if (ctx->comp.cbuf) + grub_free (ctx->comp.cbuf); + return ret; +} + +GRUB_MOD_INIT (ntfscomp) +{ + grub_ntfscomp_func = ntfscomp; +} + +GRUB_MOD_FINI (ntfscomp) +{ + grub_ntfscomp_func = NULL; +} diff --git a/libr/fs/p/grub/fs/reiserfs.c b/libr/fs/p/grub/fs/reiserfs.c new file mode 100644 index 0000000000..e922795540 --- /dev/null +++ b/libr/fs/p/grub/fs/reiserfs.c @@ -0,0 +1,1382 @@ +/* reiserfs.c - ReiserFS versions up to 3.6 */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2004,2005,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/* + TODO: + implement journal handling (ram replay) + test tail packing & direct files + validate partition label position +*/ + +#if 0 +# define GRUB_REISERFS_DEBUG +# define GRUB_REISERFS_JOURNALING +# define GRUB_HEXDUMP +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#define MIN(a, b) \ + ({ typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + _a < _b ? _a : _b; }) + +#define MAX(a, b) \ + ({ typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + _a > _b ? _a : _b; }) + +#define REISERFS_SUPER_BLOCK_OFFSET 0x10000 +#define REISERFS_MAGIC_LEN 12 +#define REISERFS_MAGIC_STRING "ReIsEr" +#define REISERFS_MAGIC_DESC_BLOCK "ReIsErLB" +/* If the 3rd bit of an item state is set, then it's visible. */ +#define GRUB_REISERFS_VISIBLE_MASK ((grub_uint16_t) 0x04) +#define REISERFS_MAX_LABEL_LENGTH 16 +#define REISERFS_LABEL_OFFSET 0x64 + +#define S_IFLNK 0xA000 + +static grub_dl_t my_mod; + +#define assert(boolean) real_assert (boolean, GRUB_FILE, __LINE__) +static inline void +real_assert (int boolean, const char *file, const int line) +{ + if (! boolean) + grub_printf ("Assertion failed at %s:%d\n", file, line); +} + +enum grub_reiserfs_item_type + { + GRUB_REISERFS_STAT, + GRUB_REISERFS_DIRECTORY, + GRUB_REISERFS_DIRECT, + GRUB_REISERFS_INDIRECT, + /* Matches both _DIRECT and _INDIRECT when searching. */ + GRUB_REISERFS_ANY, + GRUB_REISERFS_UNKNOWN + }; + +struct grub_reiserfs_superblock +{ + grub_uint32_t block_count; + grub_uint32_t block_free_count; + grub_uint32_t root_block; + grub_uint32_t journal_block; + grub_uint32_t journal_device; + grub_uint32_t journal_original_size; + grub_uint32_t journal_max_transaction_size; + grub_uint32_t journal_block_count; + grub_uint32_t journal_max_batch; + grub_uint32_t journal_max_commit_age; + grub_uint32_t journal_max_transaction_age; + grub_uint16_t block_size; + grub_uint16_t oid_max_size; + grub_uint16_t oid_current_size; + grub_uint16_t state; + grub_uint8_t magic_string[REISERFS_MAGIC_LEN]; + grub_uint32_t function_hash_code; + grub_uint16_t tree_height; + grub_uint16_t bitmap_number; + grub_uint16_t version; + grub_uint16_t reserved; + grub_uint32_t inode_generation; + grub_uint8_t unused[4]; + grub_uint16_t uuid[8]; +} __attribute__ ((packed)); + +struct grub_reiserfs_journal_header +{ + grub_uint32_t last_flush_uid; + grub_uint32_t unflushed_offset; + grub_uint32_t mount_id; +} __attribute__ ((packed)); + +struct grub_reiserfs_description_block +{ + grub_uint32_t id; + grub_uint32_t len; + grub_uint32_t mount_id; + grub_uint32_t real_blocks[0]; +} __attribute__ ((packed)); + +struct grub_reiserfs_commit_block +{ + grub_uint32_t id; + grub_uint32_t len; + grub_uint32_t real_blocks[0]; +} __attribute__ ((packed)); + +struct grub_reiserfs_stat_item_v1 +{ + grub_uint16_t mode; + grub_uint16_t hardlink_count; + grub_uint16_t uid; + grub_uint16_t gid; + grub_uint32_t size; + grub_uint32_t atime; + grub_uint32_t mtime; + grub_uint32_t ctime; + grub_uint32_t rdev; + grub_uint32_t first_direct_byte; +} __attribute__ ((packed)); + +struct grub_reiserfs_stat_item_v2 +{ + grub_uint16_t mode; + grub_uint16_t reserved; + grub_uint32_t hardlink_count; + grub_uint64_t size; + grub_uint32_t uid; + grub_uint32_t gid; + grub_uint32_t atime; + grub_uint32_t mtime; + grub_uint32_t ctime; + grub_uint32_t blocks; + grub_uint32_t first_direct_byte; +} __attribute__ ((packed)); + +struct grub_reiserfs_key +{ + grub_uint32_t directory_id; + grub_uint32_t object_id; + union + { + struct + { + grub_uint32_t offset; + grub_uint32_t type; + } v1 __attribute__ ((packed)); + struct + { + grub_uint64_t offset_type; + } v2 __attribute__ ((packed)); + } u; +} __attribute__ ((packed)); + +struct grub_reiserfs_item_header +{ + struct grub_reiserfs_key key; + union + { + grub_uint16_t free_space; + grub_uint16_t entry_count; + } u __attribute__ ((packed)); + grub_uint16_t item_size; + grub_uint16_t item_location; + grub_uint16_t version; +} __attribute__ ((packed)); + +struct grub_reiserfs_block_header +{ + grub_uint16_t level; + grub_uint16_t item_count; + grub_uint16_t free_space; + grub_uint16_t reserved; + struct grub_reiserfs_key block_right_delimiting_key; +} __attribute__ ((packed)); + +struct grub_reiserfs_disk_child +{ + grub_uint32_t block_number; + grub_uint16_t size; + grub_uint16_t reserved; +} __attribute__ ((packed)); + +struct grub_reiserfs_directory_header +{ + grub_uint32_t offset; + grub_uint32_t directory_id; + grub_uint32_t object_id; + grub_uint16_t location; + grub_uint16_t state; +} __attribute__ ((packed)); + +struct grub_fshelp_node +{ + struct grub_reiserfs_data *data; + grub_uint32_t block_number; /* 0 if node is not found. */ + grub_uint16_t block_position; + grub_uint64_t next_offset; + enum grub_reiserfs_item_type type; /* To know how to read the header. */ + struct grub_reiserfs_item_header header; +}; + +/* Returned when opening a file. */ +struct grub_reiserfs_data +{ + struct grub_reiserfs_superblock superblock; + grub_disk_t disk; +}; + +/* Internal-only functions. Not to be used outside of this file. */ + +/* Return the type of given v2 key. */ +static enum grub_reiserfs_item_type +grub_reiserfs_get_key_v2_type (const struct grub_reiserfs_key *key) +{ + switch (grub_le_to_cpu64 (key->u.v2.offset_type) >> 60) + { + case 0: + return GRUB_REISERFS_STAT; + case 15: + return GRUB_REISERFS_ANY; + case 3: + return GRUB_REISERFS_DIRECTORY; + case 2: + return GRUB_REISERFS_DIRECT; + case 1: + return GRUB_REISERFS_INDIRECT; + } + return GRUB_REISERFS_UNKNOWN; +} + +/* Return the type of given v1 key. */ +static enum grub_reiserfs_item_type +grub_reiserfs_get_key_v1_type (const struct grub_reiserfs_key *key) +{ + switch (grub_le_to_cpu32 (key->u.v1.type)) + { + case 0: + return GRUB_REISERFS_STAT; + case 555: + return GRUB_REISERFS_ANY; + case 500: + return GRUB_REISERFS_DIRECTORY; + case 0x20000000: + case 0xFFFFFFFF: + return GRUB_REISERFS_DIRECT; + case 0x10000000: + case 0xFFFFFFFE: + return GRUB_REISERFS_INDIRECT; + } + return GRUB_REISERFS_UNKNOWN; +} + +/* Return 1 if the given key is version 1 key, 2 otherwise. */ +static int +grub_reiserfs_get_key_version (const struct grub_reiserfs_key *key) +{ + return grub_reiserfs_get_key_v1_type (key) == GRUB_REISERFS_UNKNOWN ? 2 : 1; +} + +#ifdef GRUB_HEXDUMP +static void +grub_hexdump (char *buffer, grub_size_t len) +{ + grub_size_t a; + for (a = 0; a < len; a++) + { + if (! (a & 0x0F)) + grub_printf ("\n%08x ", a); + grub_printf ("%02x ", + ((unsigned int) ((unsigned char *) buffer)[a]) & 0xFF); + } + grub_printf ("\n"); +} +#endif + +#ifdef GRUB_REISERFS_DEBUG +static grub_uint64_t +grub_reiserfs_get_key_offset (const struct grub_reiserfs_key *key); + +static enum grub_reiserfs_item_type +grub_reiserfs_get_key_type (const struct grub_reiserfs_key *key); + +static void +grub_reiserfs_print_key (const struct grub_reiserfs_key *key) +{ + unsigned int a; + char *reiserfs_type_strings[] = { + "stat ", + "directory", + "direct ", + "indirect ", + "any ", + "unknown " + }; + + for (a = 0; a < sizeof (struct grub_reiserfs_key); a++) + grub_printf ("%02x ", ((unsigned int) ((unsigned char *) key)[a]) & 0xFF); + grub_printf ("parent id = 0x%08x, self id = 0x%08x, type = %s, offset = ", + grub_le_to_cpu32 (key->directory_id), + grub_le_to_cpu32 (key->object_id), + reiserfs_type_strings [grub_reiserfs_get_key_type (key)]); + if (grub_reiserfs_get_key_version (key) == 1) + grub_printf("%08x", (unsigned int) grub_reiserfs_get_key_offset (key)); + else + grub_printf("0x%07x%08x", + (unsigned) (grub_reiserfs_get_key_offset (key) >> 32), + (unsigned) (grub_reiserfs_get_key_offset (key) & 0xFFFFFFFF)); + grub_printf ("\n"); +} +#endif + +/* Return the offset of given key. */ +static grub_uint64_t +grub_reiserfs_get_key_offset (const struct grub_reiserfs_key *key) +{ + if (grub_reiserfs_get_key_version (key) == 1) + return grub_le_to_cpu32 (key->u.v1.offset); + else + return grub_le_to_cpu64 (key->u.v2.offset_type) & (~0ULL >> 4); +} + +/* Set the offset of given key. */ +static void +grub_reiserfs_set_key_offset (struct grub_reiserfs_key *key, + grub_uint64_t value) +{ + if (grub_reiserfs_get_key_version (key) == 1) + key->u.v1.offset = grub_cpu_to_le32 (value); + else + key->u.v2.offset_type \ + = ((key->u.v2.offset_type & grub_cpu_to_le64 (15ULL << 60)) + | grub_cpu_to_le64 (value & (~0ULL >> 4))); +} + +/* Return the type of given key. */ +static enum grub_reiserfs_item_type +grub_reiserfs_get_key_type (const struct grub_reiserfs_key *key) +{ + if (grub_reiserfs_get_key_version (key) == 1) + return grub_reiserfs_get_key_v1_type (key); + else + return grub_reiserfs_get_key_v2_type (key); +} + +/* Set the type of given key, with given version number. */ +static void +grub_reiserfs_set_key_type (struct grub_reiserfs_key *key, + enum grub_reiserfs_item_type grub_type, + int version) +{ + grub_uint32_t type; + + switch (grub_type) + { + case GRUB_REISERFS_STAT: + type = 0; + break; + case GRUB_REISERFS_ANY: + type = (version == 1) ? 555 : 15; + break; + case GRUB_REISERFS_DIRECTORY: + type = (version == 1) ? 500 : 3; + break; + case GRUB_REISERFS_DIRECT: + type = (version == 1) ? 0xFFFFFFFF : 2; + break; + case GRUB_REISERFS_INDIRECT: + type = (version == 1) ? 0xFFFFFFFE : 1; + break; + default: + return; + } + + if (version == 1) + key->u.v1.type = grub_cpu_to_le32 (type); + else + key->u.v2.offset_type + = ((key->u.v2.offset_type & grub_cpu_to_le64 (~0ULL >> 4)) + | grub_cpu_to_le64 ((grub_uint64_t) type << 60)); + + assert (grub_reiserfs_get_key_type (key) == grub_type); +} + +/* -1 if key 1 if lower than key 2. + 0 if key 1 is equal to key 2. + 1 if key 1 is higher than key 2. */ +static int +grub_reiserfs_compare_keys (const struct grub_reiserfs_key *key1, + const struct grub_reiserfs_key *key2) +{ + grub_uint64_t offset1, offset2; + enum grub_reiserfs_item_type type1, type2; + grub_uint32_t id1, id2; + + if (! key1 || ! key2) + return -2; + + id1 = grub_le_to_cpu32 (key1->directory_id); + id2 = grub_le_to_cpu32 (key2->directory_id); + if (id1 < id2) + return -1; + if (id1 > id2) + return 1; + + id1 = grub_le_to_cpu32 (key1->object_id); + id2 = grub_le_to_cpu32 (key2->object_id); + if (id1 < id2) + return -1; + if (id1 > id2) + return 1; + + offset1 = grub_reiserfs_get_key_offset (key1); + offset2 = grub_reiserfs_get_key_offset (key2); + if (offset1 < offset2) + return -1; + if (offset1 > offset2) + return 1; + + type1 = grub_reiserfs_get_key_type (key1); + type2 = grub_reiserfs_get_key_type (key2); + if ((type1 == GRUB_REISERFS_ANY + && (type2 == GRUB_REISERFS_DIRECT + || type2 == GRUB_REISERFS_INDIRECT)) + || (type2 == GRUB_REISERFS_ANY + && (type1 == GRUB_REISERFS_DIRECT + || type1 == GRUB_REISERFS_INDIRECT))) + return 0; + if (type1 < type2) + return -1; + if (type1 > type2) + return 1; + + return 0; +} + +/* Find the item identified by KEY in mounted filesystem DATA, and fill ITEM + accordingly to what was found. */ +static grub_err_t +grub_reiserfs_get_item (struct grub_reiserfs_data *data, + const struct grub_reiserfs_key *key, + struct grub_fshelp_node *item) +{ + grub_uint32_t block_number; + struct grub_reiserfs_block_header *block_header = 0; + struct grub_reiserfs_key *block_key = 0; + grub_uint16_t block_size, item_count, current_level; + grub_uint16_t i; + grub_uint16_t previous_level = ~0; + struct grub_reiserfs_item_header *item_headers = 0; + + if (! data) + { + grub_error (GRUB_ERR_TEST_FAILURE, "data is NULL"); + goto fail; + } + + if (! key) + { + grub_error (GRUB_ERR_TEST_FAILURE, "key is NULL"); + goto fail; + } + + if (! item) + { + grub_error (GRUB_ERR_TEST_FAILURE, "item is NULL"); + goto fail; + } + + block_size = grub_le_to_cpu16 (data->superblock.block_size); + block_number = grub_le_to_cpu32 (data->superblock.root_block); +#ifdef GRUB_REISERFS_DEBUG + grub_printf("Searching for "); + grub_reiserfs_print_key (key); +#endif + block_header = grub_malloc (block_size); + if (! block_header) + goto fail; + + item->next_offset = 0; + do + { + grub_disk_read (data->disk, + block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + (((grub_off_t) block_number * block_size) + & (GRUB_DISK_SECTOR_SIZE - 1)), + block_size, block_header); + if (grub_errno) + goto fail; + current_level = grub_le_to_cpu16 (block_header->level); + grub_dprintf ("reiserfs_tree", " at level %d\n", current_level); + if (current_level >= previous_level) + { + grub_dprintf ("reiserfs_tree", "level loop detected, aborting\n"); + grub_error (GRUB_ERR_FILE_READ_ERROR, "level loop"); + goto fail; + } + previous_level = current_level; + item_count = grub_le_to_cpu16 (block_header->item_count); + grub_dprintf ("reiserfs_tree", " number of contained items : %d\n", + item_count); + if (current_level > 1) + { + /* Internal node. Navigate to the child that should contain + the searched key. */ + struct grub_reiserfs_key *keys + = (struct grub_reiserfs_key *) (block_header + 1); + struct grub_reiserfs_disk_child *children + = ((struct grub_reiserfs_disk_child *) + (keys + item_count)); + + for (i = 0; + i < item_count + && grub_reiserfs_compare_keys (key, &(keys[i])) >= 0; + i++) + { +#ifdef GRUB_REISERFS_DEBUG + grub_printf("i %03d/%03d ", i + 1, item_count + 1); + grub_reiserfs_print_key (&(keys[i])); +#endif + } + block_number = grub_le_to_cpu32 (children[i].block_number); + if ((i < item_count) && (key->directory_id == keys[i].directory_id) + && (key->object_id == keys[i].object_id)) + item->next_offset = grub_reiserfs_get_key_offset(&(keys[i])); +#ifdef GRUB_REISERFS_DEBUG + if (i == item_count + || grub_reiserfs_compare_keys (key, &(keys[i])) == 0) + grub_printf(">"); + else + grub_printf("<"); + if (i < item_count) + { + grub_printf (" %03d/%03d ", i + 1, item_count + 1); + grub_reiserfs_print_key (&(keys[i])); + if (i + 1 < item_count) + { + grub_printf ("+ %03d/%03d ", i + 2, item_count); + grub_reiserfs_print_key (&(keys[i + 1])); + } + } + else + grub_printf ("Accessing rightmost child at block %d.\n", + block_number); +#endif + } + else + { + /* Leaf node. Check that the key is actually present. */ + item_headers + = (struct grub_reiserfs_item_header *) (block_header + 1); + for (i = 0; + i < item_count + && (grub_reiserfs_compare_keys (key, &(item_headers[i].key)) + != 0); + i++) + { +#ifdef GRUB_REISERFS_DEBUG + if (key->directory_id == item_headers[i].key.directory_id && \ + key->object_id == item_headers[i].key.object_id) + grub_printf("C"); + else + grub_printf(" "); + grub_printf(" %03d/%03d ", i + 1, item_count); + grub_reiserfs_print_key (&(item_headers[i].key)); +#endif + } + if (i < item_count) + block_key = &(item_headers[i].key); + } + } + while (current_level > 1); + + item->data = data; + + if (i == item_count || grub_reiserfs_compare_keys (key, block_key)) + { + item->block_number = 0; + item->block_position = 0; + item->type = GRUB_REISERFS_UNKNOWN; +#ifdef GRUB_REISERFS_DEBUG + grub_printf("Not found.\n"); +#endif + } + else + { + item->block_number = block_number; + item->block_position = i; + item->type = grub_reiserfs_get_key_type (block_key); + grub_memcpy (&(item->header), &(item_headers[i]), + sizeof (struct grub_reiserfs_item_header)); +#ifdef GRUB_REISERFS_DEBUG + grub_printf ("F %03d/%03d ", i + 1, item_count); + grub_reiserfs_print_key (block_key); +#endif + } + + assert (grub_errno == GRUB_ERR_NONE); + grub_free (block_header); + return GRUB_ERR_NONE; + + fail: + assert (grub_errno != GRUB_ERR_NONE); + grub_free (block_header); + assert (grub_errno != GRUB_ERR_NONE); + return grub_errno; +} + +/* Return the path of the file which is pointed at by symlink NODE. */ +static char * +grub_reiserfs_read_symlink (grub_fshelp_node_t node) +{ + char *symlink_buffer = 0; + grub_uint16_t block_size; + grub_disk_addr_t block; + grub_off_t offset; + grub_size_t len; + struct grub_fshelp_node found; + struct grub_reiserfs_key key; + + grub_memcpy (&key, &(node->header.key), sizeof (key)); + grub_reiserfs_set_key_offset (&key, 1); + grub_reiserfs_set_key_type (&key, GRUB_REISERFS_DIRECT, + grub_reiserfs_get_key_version (&key)); + + if (grub_reiserfs_get_item (node->data, &key, &found) != GRUB_ERR_NONE) + goto fail; + + if (found.block_number == 0) + goto fail; + + block_size = grub_le_to_cpu16 (node->data->superblock.block_size); + len = grub_le_to_cpu16 (found.header.item_size); + block = found.block_number * (block_size >> GRUB_DISK_SECTOR_BITS); + offset = grub_le_to_cpu16 (found.header.item_location); + + symlink_buffer = grub_malloc (len + 1); + if (! symlink_buffer) + goto fail; + + grub_disk_read (node->data->disk, block, offset, len, symlink_buffer); + if (grub_errno) + goto fail; + + symlink_buffer[len] = 0; + return symlink_buffer; + + fail: + grub_free (symlink_buffer); + return 0; +} + +/* Fill the mounted filesystem structure and return it. */ +static struct grub_reiserfs_data * +grub_reiserfs_mount (grub_disk_t disk) +{ + struct grub_reiserfs_data *data = 0; + data = grub_malloc (sizeof (*data)); + if (! data) + goto fail; + grub_disk_read (disk, REISERFS_SUPER_BLOCK_OFFSET / GRUB_DISK_SECTOR_SIZE, + 0, sizeof (data->superblock), &(data->superblock)); + if (grub_errno) + goto fail; + if (grub_memcmp (data->superblock.magic_string, + REISERFS_MAGIC_STRING, sizeof (REISERFS_MAGIC_STRING) - 1)) + { + grub_error (GRUB_ERR_BAD_FS, "not a ReiserFS filesystem"); + goto fail; + } + data->disk = disk; + return data; + + fail: + /* Disk is too small to contain a ReiserFS. */ + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not a ReiserFS filesystem"); + + grub_free (data); + return 0; +} + +/* Call HOOK for each file in directory ITEM. */ +static int +grub_reiserfs_iterate_dir (grub_fshelp_node_t item, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + struct grub_reiserfs_data *data = item->data; + struct grub_reiserfs_block_header *block_header = 0; + grub_uint16_t block_size, block_position; + grub_uint32_t block_number; + grub_uint64_t next_offset = item->next_offset; + int ret = 0; + + if (item->type != GRUB_REISERFS_DIRECTORY) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, + "grub_reiserfs_iterate_dir called on a non-directory item"); + goto fail; + } + block_size = grub_le_to_cpu16 (data->superblock.block_size); + block_header = grub_malloc (block_size); + if (! block_header) + goto fail; + block_number = item->block_number; + block_position = item->block_position; + grub_dprintf ("reiserfs", "Iterating directory...\n"); + do + { + struct grub_reiserfs_directory_header *directory_headers; + struct grub_fshelp_node directory_item; + grub_uint16_t entry_count, entry_number; + struct grub_reiserfs_item_header *item_headers; + + grub_disk_read (data->disk, + block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + (((grub_off_t) block_number * block_size) + & (GRUB_DISK_SECTOR_SIZE - 1)), + block_size, (char *) block_header); + if (grub_errno) + goto fail; + +#if 0 + if (grub_le_to_cpu16 (block_header->level) != 1) + { + grub_error (GRUB_ERR_TEST_FAILURE, + "reiserfs: block %d is not a leaf block", + block_number); + goto fail; + } +#endif + + item_headers = (struct grub_reiserfs_item_header *) (block_header + 1); + directory_headers + = ((struct grub_reiserfs_directory_header *) + ((char *) block_header + + grub_le_to_cpu16 (item_headers[block_position].item_location))); + entry_count + = grub_le_to_cpu16 (item_headers[block_position].u.entry_count); + for (entry_number = 0; entry_number < entry_count; entry_number++) + { + struct grub_reiserfs_directory_header *directory_header + = &directory_headers[entry_number]; + grub_uint16_t entry_state + = grub_le_to_cpu16 (directory_header->state); + + if (entry_state & GRUB_REISERFS_VISIBLE_MASK) + { + grub_fshelp_node_t entry_item; + struct grub_reiserfs_key entry_key; + enum grub_reiserfs_item_type entry_type; + char *entry_name; + + entry_name = (((char *) directory_headers) + + grub_le_to_cpu16 (directory_header->location)); + entry_key.directory_id = directory_header->directory_id; + entry_key.object_id = directory_header->object_id; + entry_key.u.v2.offset_type = 0; + grub_reiserfs_set_key_type (&entry_key, GRUB_REISERFS_DIRECTORY, + 2); + grub_reiserfs_set_key_offset (&entry_key, 1); + + entry_item = grub_malloc (sizeof (*entry_item)); + if (! entry_item) + goto fail; + + if (grub_reiserfs_get_item (data, &entry_key, entry_item) + != GRUB_ERR_NONE) + { + grub_free (entry_item); + goto fail; + } + + if (entry_item->type == GRUB_REISERFS_DIRECTORY) + entry_type = GRUB_FSHELP_DIR; + else + { + grub_uint32_t entry_block_number; + /* Order is very important here. + First set the offset to 0 using current key version. + Then change the key type, which affects key version + detection. */ + grub_reiserfs_set_key_offset (&entry_key, 0); + grub_reiserfs_set_key_type (&entry_key, GRUB_REISERFS_STAT, + 2); + if (grub_reiserfs_get_item (data, &entry_key, entry_item) + != GRUB_ERR_NONE) + { + grub_free (entry_item); + goto fail; + } + + if (entry_item->block_number != 0) + { + grub_uint16_t entry_version; + entry_version + = grub_le_to_cpu16 (entry_item->header.version); + entry_block_number = entry_item->block_number; +#if 0 + grub_dprintf ("reiserfs", + "version %04x block %08x (%08x) position %08x\n", + entry_version, entry_block_number, + ((grub_disk_addr_t) entry_block_number * block_size) / GRUB_DISK_SECTOR_SIZE, + grub_le_to_cpu16 (entry_item->header.item_location)); +#endif + if (entry_version == 0) /* Version 1 stat item. */ + { + struct grub_reiserfs_stat_item_v1 entry_v1_stat; + grub_disk_read (data->disk, + entry_block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + grub_le_to_cpu16 (entry_item->header.item_location), + sizeof (entry_v1_stat), + (char *) &entry_v1_stat); + if (grub_errno) + goto fail; +#if 0 + grub_dprintf ("reiserfs", + "%04x %04x %04x %04x %08x %08x | %08x %08x %08x %08x\n", + grub_le_to_cpu16 (entry_v1_stat.mode), + grub_le_to_cpu16 (entry_v1_stat.hardlink_count), + grub_le_to_cpu16 (entry_v1_stat.uid), + grub_le_to_cpu16 (entry_v1_stat.gid), + grub_le_to_cpu32 (entry_v1_stat.size), + grub_le_to_cpu32 (entry_v1_stat.atime), + grub_le_to_cpu32 (entry_v1_stat.mtime), + grub_le_to_cpu32 (entry_v1_stat.ctime), + grub_le_to_cpu32 (entry_v1_stat.rdev), + grub_le_to_cpu32 (entry_v1_stat.first_direct_byte)); + grub_dprintf ("reiserfs", + "%04x %04x %04x %04x %08x %08x | %08x %08x %08x %08x\n", + entry_v1_stat.mode, + entry_v1_stat.hardlink_count, + entry_v1_stat.uid, + entry_v1_stat.gid, + entry_v1_stat.size, + entry_v1_stat.atime, + entry_v1_stat.mtime, + entry_v1_stat.ctime, + entry_v1_stat.rdev, + entry_v1_stat.first_direct_byte); +#endif + if ((grub_le_to_cpu16 (entry_v1_stat.mode) & S_IFLNK) + == S_IFLNK) + entry_type = GRUB_FSHELP_SYMLINK; + else + entry_type = GRUB_FSHELP_REG; + } + else + { + struct grub_reiserfs_stat_item_v2 entry_v2_stat; + grub_disk_read (data->disk, + entry_block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + grub_le_to_cpu16 (entry_item->header.item_location), + sizeof (entry_v2_stat), + (char *) &entry_v2_stat); + if (grub_errno) + goto fail; +#if 0 + grub_dprintf ("reiserfs", + "%04x %04x %08x %08x%08x | %08x %08x %08x %08x | %08x %08x %08x\n", + grub_le_to_cpu16 (entry_v2_stat.mode), + grub_le_to_cpu16 (entry_v2_stat.reserved), + grub_le_to_cpu32 (entry_v2_stat.hardlink_count), + (unsigned int) (grub_le_to_cpu64 (entry_v2_stat.size) >> 32), + (unsigned int) (grub_le_to_cpu64 (entry_v2_stat.size) && 0xFFFFFFFF), + grub_le_to_cpu32 (entry_v2_stat.uid), + grub_le_to_cpu32 (entry_v2_stat.gid), + grub_le_to_cpu32 (entry_v2_stat.atime), + grub_le_to_cpu32 (entry_v2_stat.mtime), + grub_le_to_cpu32 (entry_v2_stat.ctime), + grub_le_to_cpu32 (entry_v2_stat.blocks), + grub_le_to_cpu32 (entry_v2_stat.first_direct_byte)); + grub_dprintf ("reiserfs", + "%04x %04x %08x %08x%08x | %08x %08x %08x %08x | %08x %08x %08x\n", + entry_v2_stat.mode, + entry_v2_stat.reserved, + entry_v2_stat.hardlink_count, + (unsigned int) (entry_v2_stat.size >> 32), + (unsigned int) (entry_v2_stat.size && 0xFFFFFFFF), + entry_v2_stat.uid, + entry_v2_stat.gid, + entry_v2_stat.atime, + entry_v2_stat.mtime, + entry_v2_stat.ctime, + entry_v2_stat.blocks, + entry_v2_stat.first_direct_byte); +#endif + if ((grub_le_to_cpu16 (entry_v2_stat.mode) & S_IFLNK) + == S_IFLNK) + entry_type = GRUB_FSHELP_SYMLINK; + else + entry_type = GRUB_FSHELP_REG; + } + } + else + { + /* Pseudo file ".." never has stat block. */ + if (grub_strcmp (entry_name, "..")) + grub_dprintf ("reiserfs", + "Warning : %s has no stat block !\n", + entry_name); + grub_free (entry_item); + goto next; + } + } + if (hook (entry_name, entry_type, entry_item)) + { + grub_dprintf ("reiserfs", "Found : %s, type=%d\n", + entry_name, entry_type); + ret = 1; + goto found; + } + +next: + *entry_name = 0; /* Make sure next entry name (which is just + before this one in disk order) stops before + the current one. */ + } + } + + if (next_offset == 0) + break; + + grub_reiserfs_set_key_offset (&(item_headers[block_position].key), + next_offset); + if (grub_reiserfs_get_item (data, &(item_headers[block_position].key), + &directory_item) != GRUB_ERR_NONE) + goto fail; + block_number = directory_item.block_number; + block_position = directory_item.block_position; + next_offset = directory_item.next_offset; + } + while (block_number); + + found: + assert (grub_errno == GRUB_ERR_NONE); + grub_free (block_header); + return ret; + fail: + assert (grub_errno != GRUB_ERR_NONE); + grub_free (block_header); + return 0; +} + +/****************************************************************************/ +/* grub api functions */ +/****************************************************************************/ + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_reiserfs_open (struct grub_file *file, const char *name) +{ + struct grub_reiserfs_data *data = 0; + struct grub_fshelp_node root, *found = 0, info; + struct grub_reiserfs_key key; + grub_uint32_t block_number; + grub_uint16_t entry_version, block_size, entry_location; + + grub_dl_ref (my_mod); + data = grub_reiserfs_mount (file->device->disk); + if (! data) + goto fail; + block_size = grub_le_to_cpu16 (data->superblock.block_size); + key.directory_id = grub_cpu_to_le32 (1); + key.object_id = grub_cpu_to_le32 (2); + key.u.v2.offset_type = 0; + grub_reiserfs_set_key_type (&key, GRUB_REISERFS_DIRECTORY, 2); + grub_reiserfs_set_key_offset (&key, 1); + if (grub_reiserfs_get_item (data, &key, &root) != GRUB_ERR_NONE) + goto fail; + if (root.block_number == 0) + { + grub_error (GRUB_ERR_BAD_FS, "unable to find root item"); + goto fail; /* Should never happen since checked at mount. */ + } + grub_fshelp_find_file (name, &root, &found, + grub_reiserfs_iterate_dir, + grub_reiserfs_read_symlink, GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + key.directory_id = found->header.key.directory_id; + key.object_id = found->header.key.object_id; + grub_reiserfs_set_key_type (&key, GRUB_REISERFS_STAT, 2); + grub_reiserfs_set_key_offset (&key, 0); + if (grub_reiserfs_get_item (data, &key, &info) != GRUB_ERR_NONE) + goto fail; + if (info.block_number == 0) + { + grub_error (GRUB_ERR_BAD_FS, "unable to find searched item"); + goto fail; + } + entry_version = grub_le_to_cpu16 (info.header.version); + entry_location = grub_le_to_cpu16 (info.header.item_location); + block_number = info.block_number; + if (entry_version == 0) /* Version 1 stat item. */ + { + struct grub_reiserfs_stat_item_v1 entry_v1_stat; + grub_disk_read (data->disk, + block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + entry_location + + (((grub_off_t) block_number * block_size) + & (GRUB_DISK_SECTOR_SIZE - 1)), + sizeof (entry_v1_stat), &entry_v1_stat); + if (grub_errno) + goto fail; + file->size = (grub_off_t) grub_le_to_cpu64 (entry_v1_stat.size); + } + else + { + struct grub_reiserfs_stat_item_v2 entry_v2_stat; + grub_disk_read (data->disk, + block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + entry_location + + (((grub_off_t) block_number * block_size) + & (GRUB_DISK_SECTOR_SIZE - 1)), + sizeof (entry_v2_stat), &entry_v2_stat); + if (grub_errno) + goto fail; + file->size = (grub_off_t) grub_le_to_cpu64 (entry_v2_stat.size); + } + grub_dprintf ("reiserfs", "file size : %d (%08x%08x)\n", + (unsigned int) file->size, + (unsigned int) (file->size >> 32), (unsigned int) file->size); + file->offset = 0; + file->data = found; + return GRUB_ERR_NONE; + + fail: + assert (grub_errno != GRUB_ERR_NONE); + grub_free (found); + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; +} + +static grub_ssize_t +grub_reiserfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + unsigned int indirect_block, indirect_block_count; + struct grub_reiserfs_key key; + struct grub_fshelp_node *node = file->data; + struct grub_reiserfs_data *data = node->data; + struct grub_fshelp_node found; + grub_uint16_t block_size = grub_le_to_cpu16 (data->superblock.block_size); + grub_uint16_t item_size; + grub_uint32_t *indirect_block_ptr = 0; + grub_uint64_t current_key_offset = 1; + grub_off_t initial_position, current_position, final_position, length; + grub_disk_addr_t block; + grub_off_t offset; + + key.directory_id = node->header.key.directory_id; + key.object_id = node->header.key.object_id; + key.u.v2.offset_type = 0; + grub_reiserfs_set_key_type (&key, GRUB_REISERFS_ANY, 2); + initial_position = file->offset; + current_position = 0; + final_position = MIN (len + initial_position, file->size); + grub_dprintf ("reiserfs", + "Reading from %lld to %lld (%lld instead of requested %ld)\n", + (unsigned long long) initial_position, + (unsigned long long) final_position, + (unsigned long long) (final_position - initial_position), + (unsigned long) len); + while (current_position < final_position) + { + grub_reiserfs_set_key_offset (&key, current_key_offset); + + if (grub_reiserfs_get_item (data, &key, &found) != GRUB_ERR_NONE) + goto fail; + if (found.block_number == 0) + goto fail; + item_size = grub_le_to_cpu16 (found.header.item_size); + switch (found.type) + { + case GRUB_REISERFS_DIRECT: + block = found.block_number * (block_size >> GRUB_DISK_SECTOR_BITS); + grub_dprintf ("reiserfs_blocktype", "D: %u\n", (unsigned) block); + if (initial_position < current_position + item_size) + { + offset = MAX ((signed) (initial_position - current_position), 0); + length = (MIN (item_size, final_position - current_position) + - offset); + grub_dprintf ("reiserfs", + "Reading direct block %u from %u to %u...\n", + (unsigned) block, (unsigned) offset, + (unsigned) (offset + length)); + found.data->disk->read_hook = file->read_hook; + grub_disk_read (found.data->disk, + block, + offset + + grub_le_to_cpu16 (found.header.item_location), + length, buf); + found.data->disk->read_hook = 0; + if (grub_errno) + goto fail; + buf += length; + current_position += offset + length; + } + else + current_position += item_size; + break; + case GRUB_REISERFS_INDIRECT: + indirect_block_count = item_size / sizeof (*indirect_block_ptr); + indirect_block_ptr = grub_malloc (item_size); + if (! indirect_block_ptr) + goto fail; + grub_disk_read (found.data->disk, + found.block_number * (block_size >> GRUB_DISK_SECTOR_BITS), + grub_le_to_cpu16 (found.header.item_location), + item_size, indirect_block_ptr); + if (grub_errno) + goto fail; + found.data->disk->read_hook = file->read_hook; + for (indirect_block = 0; + indirect_block < indirect_block_count + && current_position < final_position; + indirect_block++) + { + block = grub_le_to_cpu32 (indirect_block_ptr[indirect_block]) * + (block_size >> GRUB_DISK_SECTOR_BITS); + grub_dprintf ("reiserfs_blocktype", "I: %u\n", (unsigned) block); + if (current_position + block_size >= initial_position) + { + offset = MAX ((signed) (initial_position - current_position), + 0); + length = (MIN (block_size, final_position - current_position) + - offset); + grub_dprintf ("reiserfs", + "Reading indirect block %u from %u to %u...\n", + (unsigned) block, (unsigned) offset, + (unsigned) (offset + length)); +#if 0 + grub_dprintf ("reiserfs", + "\nib=%04d/%04d, ip=%d, cp=%d, fp=%d, off=%d, l=%d, tl=%d\n", + indirect_block + 1, indirect_block_count, + initial_position, current_position, + final_position, offset, length, len); +#endif + grub_disk_read (found.data->disk, block, offset, length, buf); + if (grub_errno) + goto fail; + buf += length; + current_position += offset + length; + } + else + current_position += block_size; + } + found.data->disk->read_hook = 0; + grub_free (indirect_block_ptr); + indirect_block_ptr = 0; + break; + default: + goto fail; + } + current_key_offset = current_position + 1; + } + + grub_dprintf ("reiserfs", + "Have successfully read %lld bytes (%ld requested)\n", + (unsigned long long) (current_position - initial_position), + (unsigned long) len); + return current_position - initial_position; + +#if 0 + switch (found.type) + { + case GRUB_REISERFS_DIRECT: + read_length = MIN (len, item_size - file->offset); + grub_disk_read (found.data->disk, + (found.block_number * block_size) / GRUB_DISK_SECTOR_SIZE, + grub_le_to_cpu16 (found.header.item_location) + file->offset, + read_length, buf); + if (grub_errno) + goto fail; + break; + case GRUB_REISERFS_INDIRECT: + indirect_block_count = item_size / sizeof (*indirect_block_ptr); + indirect_block_ptr = grub_malloc (item_size); + if (!indirect_block_ptr) + goto fail; + grub_disk_read (found.data->disk, + (found.block_number * block_size) / GRUB_DISK_SECTOR_SIZE, + grub_le_to_cpu16 (found.header.item_location), + item_size, (char *) indirect_block_ptr); + if (grub_errno) + goto fail; + len = MIN (len, file->size - file->offset); + for (indirect_block = file->offset / block_size; + indirect_block < indirect_block_count && read_length < len; + indirect_block++) + { + read = MIN (block_size, len - read_length); + grub_disk_read (found.data->disk, + (grub_le_to_cpu32 (indirect_block_ptr[indirect_block]) * block_size) / GRUB_DISK_SECTOR_SIZE, + file->offset % block_size, read, + ((void *) buf) + read_length); + if (grub_errno) + goto fail; + read_length += read; + } + grub_free (indirect_block_ptr); + break; + default: + goto fail; + } + + return read_length; +#endif + + fail: + grub_free (indirect_block_ptr); + return 0; +} + +/* Close the file FILE. */ +static grub_err_t +grub_reiserfs_close (grub_file_t file) +{ + struct grub_fshelp_node *node = file->data; + struct grub_reiserfs_data *data = node->data; + + grub_free (data); + grub_free (node); + grub_dl_unref (my_mod); + return GRUB_ERR_NONE; +} + +/* Call HOOK with each file under DIR. */ +static grub_err_t +grub_reiserfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_reiserfs_data *data = 0; + struct grub_fshelp_node root, *found; + struct grub_reiserfs_key root_key; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + grub_dl_ref (my_mod); + data = grub_reiserfs_mount (device->disk); + if (! data) + goto fail; + root_key.directory_id = grub_cpu_to_le32 (1); + root_key.object_id = grub_cpu_to_le32 (2); + root_key.u.v2.offset_type = 0; + grub_reiserfs_set_key_type (&root_key, GRUB_REISERFS_DIRECTORY, 2); + grub_reiserfs_set_key_offset (&root_key, 1); + if (grub_reiserfs_get_item (data, &root_key, &root) != GRUB_ERR_NONE) + goto fail; + if (root.block_number == 0) + { + grub_error(GRUB_ERR_BAD_FS, "root not found"); + goto fail; + } + grub_fshelp_find_file (path, &root, &found, grub_reiserfs_iterate_dir, + grub_reiserfs_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + grub_reiserfs_iterate_dir (found, iterate); + grub_free (data); + grub_dl_unref (my_mod); + return GRUB_ERR_NONE; + + fail: + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; +} + +/* Return the label of the device DEVICE in LABEL. The label is + returned in a grub_malloc'ed buffer and should be freed by the + caller. */ +static grub_err_t +grub_reiserfs_label (grub_device_t device, char **label) +{ + *label = grub_malloc (REISERFS_MAX_LABEL_LENGTH); + if (*label) + { + grub_disk_read (device->disk, + REISERFS_SUPER_BLOCK_OFFSET / GRUB_DISK_SECTOR_SIZE, + REISERFS_LABEL_OFFSET, REISERFS_MAX_LABEL_LENGTH, + *label); + } + return grub_errno; +} + +static grub_err_t +grub_reiserfs_uuid (grub_device_t device, char **uuid) +{ + struct grub_reiserfs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_reiserfs_mount (disk); + if (data) + { + *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x", + grub_be_to_cpu16 (data->superblock.uuid[0]), + grub_be_to_cpu16 (data->superblock.uuid[1]), + grub_be_to_cpu16 (data->superblock.uuid[2]), + grub_be_to_cpu16 (data->superblock.uuid[3]), + grub_be_to_cpu16 (data->superblock.uuid[4]), + grub_be_to_cpu16 (data->superblock.uuid[5]), + grub_be_to_cpu16 (data->superblock.uuid[6]), + grub_be_to_cpu16 (data->superblock.uuid[7])); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static struct grub_fs grub_reiserfs_fs = + { + .name = "reiserfs", + .dir = grub_reiserfs_dir, + .open = grub_reiserfs_open, + .read = grub_reiserfs_read, + .close = grub_reiserfs_close, + .label = grub_reiserfs_label, + .uuid = grub_reiserfs_uuid, + .next = 0 + }; + +GRUB_MOD_INIT(reiserfs) +{ + grub_fs_register (&grub_reiserfs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(reiserfs) +{ + grub_fs_unregister (&grub_reiserfs_fs); +} diff --git a/libr/fs/p/grub/fs/sfs.c b/libr/fs/p/grub/fs/sfs.c new file mode 100644 index 0000000000..b49420de12 --- /dev/null +++ b/libr/fs/p/grub/fs/sfs.c @@ -0,0 +1,597 @@ +/* sfs.c - Amiga Smart FileSystem. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* The common header for a block. */ +struct grub_sfs_bheader +{ + grub_uint8_t magic[4]; + grub_uint32_t chksum; + grub_uint32_t ipointtomyself; +} __attribute__ ((packed)); + +/* The sfs rootblock. */ +struct grub_sfs_rblock +{ + struct grub_sfs_bheader header; + grub_uint32_t version; + grub_uint8_t unused1[36]; + grub_uint32_t blocksize; + grub_uint8_t unused2[40]; + grub_uint8_t unused3[8]; + grub_uint32_t rootobject; + grub_uint32_t btree; +} __attribute__ ((packed)); + +/* A SFS object container. */ +struct grub_sfs_obj +{ + grub_uint8_t unused1[4]; + grub_uint32_t nodeid; + grub_uint8_t unused2[4]; + union + { + struct + { + grub_uint32_t first_block; + grub_uint32_t size; + } file __attribute__ ((packed)); + struct + { + grub_uint32_t hashtable; + grub_uint32_t dir_objc; + } dir __attribute__ ((packed)); + } file_dir; + grub_uint8_t unused3[4]; + grub_uint8_t type; + grub_uint8_t filename[1]; + grub_uint8_t comment[1]; +} __attribute__ ((packed)); + +#define GRUB_SFS_TYPE_DELETED 32 +#define GRUB_SFS_TYPE_SYMLINK 64 +#define GRUB_SFS_TYPE_DIR 128 + +/* A SFS object container. */ +struct grub_sfs_objc +{ + struct grub_sfs_bheader header; + grub_uint32_t parent; + grub_uint32_t next; + grub_uint32_t prev; + /* The amount of objects depends on the blocksize. */ + struct grub_sfs_obj objects[1]; +} __attribute__ ((packed)); + +struct grub_sfs_btree_node +{ + grub_uint32_t key; + grub_uint32_t data; +} __attribute__ ((packed)); + +struct grub_sfs_btree_extent +{ + grub_uint32_t key; + grub_uint32_t next; + grub_uint32_t prev; + grub_uint16_t size; +} __attribute__ ((packed)); + +struct grub_sfs_btree +{ + struct grub_sfs_bheader header; + grub_uint16_t nodes; + grub_uint8_t leaf; + grub_uint8_t nodesize; + /* Normally this can be kind of node, but just extents are + supported. */ + struct grub_sfs_btree_node node[1]; +} __attribute__ ((packed)); + + + +struct grub_fshelp_node +{ + struct grub_sfs_data *data; + int block; + int size; +}; + +/* Information about a "mounted" sfs filesystem. */ +struct grub_sfs_data +{ + struct grub_sfs_rblock rblock; + struct grub_fshelp_node diropen; + grub_disk_t disk; + + /* Blocksize in sectors. */ + unsigned int blocksize; + + /* Label of the filesystem. */ + char *label; +}; + +static grub_dl_t my_mod; + + +/* Lookup the extent starting with BLOCK in the filesystem described + by DATA. Return the extent size in SIZE and the following extent + in NEXTEXT. */ +static grub_err_t +grub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block, + int *size, int *nextext) +{ + char *treeblock; + struct grub_sfs_btree *tree; + int i; + int next; + + treeblock = grub_malloc (data->blocksize); + if (!block) + return 0; + + next = grub_be_to_cpu32 (data->rblock.btree); + tree = (struct grub_sfs_btree *) treeblock; + + /* Handle this level in the btree. */ + do + { + grub_disk_read (data->disk, next, 0, data->blocksize, treeblock); + if (grub_errno) + { + grub_free (treeblock); + return grub_errno; + } + + for (i = grub_be_to_cpu16 (tree->nodes) - 1; i >= 0; i--) + { + +#define EXTNODE(tree, index) \ + ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \ + + (index) * (tree)->nodesize)) + + /* Follow the tree down to the leaf level. */ + if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) <= block) + && !tree->leaf) + { + next = grub_be_to_cpu32 (EXTNODE (tree, i)->data); + break; + } + + /* If the leaf level is reached, just find the correct extent. */ + if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf) + { + struct grub_sfs_btree_extent *extent; + extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i); + + /* We found a correct leaf. */ + *size = grub_be_to_cpu16 (extent->size); + *nextext = grub_be_to_cpu32 (extent->next); + + grub_free (treeblock); + return 0; + } + +#undef EXTNODE + + } + } while (!tree->leaf); + + grub_free (treeblock); + + return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found"); +} + +static grub_disk_addr_t +grub_sfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + int blk = node->block; + int size = 0; + int next = 0; + + while (blk) + { + grub_err_t err; + + /* In case of the first block we don't have to lookup the + extent, the minimum size is always 1. */ + if (fileblock == 0) + return blk; + + err = grub_sfs_read_extent (node->data, blk, &size, &next); + if (err) + return 0; + + if (fileblock < (unsigned int) size) + return fileblock + blk; + + fileblock -= size; + + blk = next; + } + + grub_error (GRUB_ERR_FILE_READ_ERROR, + "reading a SFS block outside the extent"); + + return 0; +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_sfs_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_sfs_read_block, + node->size, 0); +} + + +static struct grub_sfs_data * +grub_sfs_mount (grub_disk_t disk) +{ + struct grub_sfs_data *data; + struct grub_sfs_objc *rootobjc; + char *rootobjc_data = 0; + unsigned int blk; + + data = grub_malloc (sizeof (*data)); + if (!data) + return 0; + + /* Read the rootblock. */ + grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock), + &data->rblock); + if (grub_errno) + goto fail; + + /* Make sure this is a sfs filesystem. */ + if (grub_strncmp ((char *) (data->rblock.header.magic), "SFS", 4)) + { + grub_error (GRUB_ERR_BAD_FS, "not a SFS filesystem"); + goto fail; + } + + data->blocksize = grub_be_to_cpu32 (data->rblock.blocksize); + rootobjc_data = grub_malloc (data->blocksize); + if (! rootobjc_data) + goto fail; + + /* Read the root object container. */ + grub_disk_read (disk, grub_be_to_cpu32 (data->rblock.rootobject), 0, + data->blocksize, rootobjc_data); + if (grub_errno) + goto fail; + + rootobjc = (struct grub_sfs_objc *) rootobjc_data; + + blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc); + data->diropen.size = 0; + data->diropen.block = blk; + data->diropen.data = data; + data->disk = disk; + data->label = grub_strdup ((char *) (rootobjc->objects[0].filename)); + + return data; + + fail: + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not an SFS filesystem"); + + grub_free (data); + grub_free (rootobjc_data); + return 0; +} + + +static char * +grub_sfs_read_symlink (grub_fshelp_node_t node) +{ + struct grub_sfs_data *data = node->data; + char *symlink; + char *block; + + block = grub_malloc (data->blocksize); + if (!block) + return 0; + + grub_disk_read (data->disk, node->block, 0, data->blocksize, block); + if (grub_errno) + { + grub_free (block); + return 0; + } + + /* This is just a wild guess, but it always worked for me. How the + SLNK block looks like is not documented in the SFS docs. */ + symlink = grub_strdup (&block[24]); + grub_free (block); + if (!symlink) + return 0; + + return symlink; +} + +static int +grub_sfs_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + struct grub_fshelp_node *node = 0; + struct grub_sfs_data *data = dir->data; + char *objc_data; + struct grub_sfs_objc *objc; + unsigned int next = dir->block; + int pos; + + auto int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block, + int size, int type); + + int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name, int block, + int size, int type) + { + node = grub_malloc (sizeof (*node)); + if (!node) + return 1; + + node->data = data; + node->size = size; + node->block = block; + + return hook (name, type, node); + } + + objc_data = grub_malloc (data->blocksize); + if (!objc_data) + goto fail; + + /* The Object container can consist of multiple blocks, iterate over + every block. */ + while (next) + { + grub_disk_read (data->disk, next, 0, data->blocksize, objc_data); + if (grub_errno) + goto fail; + + objc = (struct grub_sfs_objc *) objc_data; + + pos = (char *) &objc->objects[0] - (char *) objc; + + /* Iterate over all entries in this block. */ + while (pos + sizeof (struct grub_sfs_obj) < data->blocksize) + { + struct grub_sfs_obj *obj; + obj = (struct grub_sfs_obj *) ((char *) objc + pos); + char *filename = (char *) (obj->filename); + int len; + enum grub_fshelp_filetype type; + unsigned int block; + + /* The filename and comment dynamically increase the size of + the object. */ + len = grub_strlen (filename); + len += grub_strlen (filename + len + 1); + + pos += sizeof (*obj) + len; + /* Round up to a multiple of two bytes. */ + pos = ((pos + 1) >> 1) << 1; + + if (grub_strlen (filename) == 0) + continue; + + /* First check if the file was not deleted. */ + if (obj->type & GRUB_SFS_TYPE_DELETED) + continue; + else if (obj->type & GRUB_SFS_TYPE_SYMLINK) + type = GRUB_FSHELP_SYMLINK; + else if (obj->type & GRUB_SFS_TYPE_DIR) + type = GRUB_FSHELP_DIR; + else + type = GRUB_FSHELP_REG; + + if (type == GRUB_FSHELP_DIR) + block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc); + else + block = grub_be_to_cpu32 (obj->file_dir.file.first_block); + + if (grub_sfs_create_node (filename, block, + grub_be_to_cpu32 (obj->file_dir.file.size), + type)) + { + grub_free (objc_data); + return 1; + } + } + + next = grub_be_to_cpu32 (objc->next); + } + + fail: + grub_free (objc_data); + return 0; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_sfs_open (struct grub_file *file, const char *name) +{ + struct grub_sfs_data *data; + struct grub_fshelp_node *fdiro = 0; + + grub_dl_ref (my_mod); + + data = grub_sfs_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir, + grub_sfs_read_symlink, GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + file->size = fdiro->size; + data->diropen = *fdiro; + grub_free (fdiro); + + file->data = data; + file->offset = 0; + + return 0; + + fail: + if (data && fdiro != &data->diropen) + grub_free (fdiro); + if (data) + grub_free (data->label); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_err_t +grub_sfs_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + + +/* Read LEN bytes data from FILE into BUF. */ +static grub_ssize_t +grub_sfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_sfs_data *data = (struct grub_sfs_data *) file->data; + + int size = grub_sfs_read_file (&data->diropen, file->read_hook, + file->offset, len, buf); + + return size; +} + + +static grub_err_t +grub_sfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_sfs_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_sfs_mount (device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir, + grub_sfs_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_sfs_iterate_dir (fdiro, iterate); + + fail: + if (data && fdiro != &data->diropen) + grub_free (fdiro); + if (data) + grub_free (data->label); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_err_t +grub_sfs_label (grub_device_t device, char **label) +{ + struct grub_sfs_data *data; + grub_disk_t disk = device->disk; + + data = grub_sfs_mount (disk); + if (data) + *label = data->label; + + grub_free (data); + + return grub_errno; +} + + +static struct grub_fs grub_sfs_fs = + { + .name = "sfs", + .dir = grub_sfs_dir, + .open = grub_sfs_open, + .read = grub_sfs_read, + .close = grub_sfs_close, + .label = grub_sfs_label, +#ifdef GRUB_UTIL + .reserved_first_sector = 0, +#endif + .next = 0 + }; + +GRUB_MOD_INIT(sfs) +{ + grub_fs_register (&grub_sfs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(sfs) +{ + grub_fs_unregister (&grub_sfs_fs); +} diff --git a/libr/fs/p/grub/fs/tar.c b/libr/fs/p/grub/fs/tar.c new file mode 100644 index 0000000000..6ab62bca79 --- /dev/null +++ b/libr/fs/p/grub/fs/tar.c @@ -0,0 +1,2 @@ +#define MODE_USTAR 1 +#include "cpio.c" diff --git a/libr/fs/p/grub/fs/udf.c b/libr/fs/p/grub/fs/udf.c new file mode 100644 index 0000000000..1672aab1be --- /dev/null +++ b/libr/fs/p/grub/fs/udf.c @@ -0,0 +1,1059 @@ +/* udf.c - Universal Disk Format filesystem. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_UDF_MAX_PDS 2 +#define GRUB_UDF_MAX_PMS 6 + +#define U16 grub_le_to_cpu16 +#define U32 grub_le_to_cpu32 +#define U64 grub_le_to_cpu64 + +#define GRUB_UDF_TAG_IDENT_PVD 0x0001 +#define GRUB_UDF_TAG_IDENT_AVDP 0x0002 +#define GRUB_UDF_TAG_IDENT_VDP 0x0003 +#define GRUB_UDF_TAG_IDENT_IUVD 0x0004 +#define GRUB_UDF_TAG_IDENT_PD 0x0005 +#define GRUB_UDF_TAG_IDENT_LVD 0x0006 +#define GRUB_UDF_TAG_IDENT_USD 0x0007 +#define GRUB_UDF_TAG_IDENT_TD 0x0008 +#define GRUB_UDF_TAG_IDENT_LVID 0x0009 + +#define GRUB_UDF_TAG_IDENT_FSD 0x0100 +#define GRUB_UDF_TAG_IDENT_FID 0x0101 +#define GRUB_UDF_TAG_IDENT_AED 0x0102 +#define GRUB_UDF_TAG_IDENT_IE 0x0103 +#define GRUB_UDF_TAG_IDENT_TE 0x0104 +#define GRUB_UDF_TAG_IDENT_FE 0x0105 +#define GRUB_UDF_TAG_IDENT_EAHD 0x0106 +#define GRUB_UDF_TAG_IDENT_USE 0x0107 +#define GRUB_UDF_TAG_IDENT_SBD 0x0108 +#define GRUB_UDF_TAG_IDENT_PIE 0x0109 +#define GRUB_UDF_TAG_IDENT_EFE 0x010A + +#define GRUB_UDF_ICBTAG_TYPE_UNDEF 0x00 +#define GRUB_UDF_ICBTAG_TYPE_USE 0x01 +#define GRUB_UDF_ICBTAG_TYPE_PIE 0x02 +#define GRUB_UDF_ICBTAG_TYPE_IE 0x03 +#define GRUB_UDF_ICBTAG_TYPE_DIRECTORY 0x04 +#define GRUB_UDF_ICBTAG_TYPE_REGULAR 0x05 +#define GRUB_UDF_ICBTAG_TYPE_BLOCK 0x06 +#define GRUB_UDF_ICBTAG_TYPE_CHAR 0x07 +#define GRUB_UDF_ICBTAG_TYPE_EA 0x08 +#define GRUB_UDF_ICBTAG_TYPE_FIFO 0x09 +#define GRUB_UDF_ICBTAG_TYPE_SOCKET 0x0A +#define GRUB_UDF_ICBTAG_TYPE_TE 0x0B +#define GRUB_UDF_ICBTAG_TYPE_SYMLINK 0x0C +#define GRUB_UDF_ICBTAG_TYPE_STREAMDIR 0x0D + +#define GRUB_UDF_ICBTAG_FLAG_AD_MASK 0x0007 +#define GRUB_UDF_ICBTAG_FLAG_AD_SHORT 0x0000 +#define GRUB_UDF_ICBTAG_FLAG_AD_LONG 0x0001 +#define GRUB_UDF_ICBTAG_FLAG_AD_EXT 0x0002 +#define GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB 0x0003 + +#define GRUB_UDF_EXT_NORMAL 0x00000000 +#define GRUB_UDF_EXT_NREC_ALLOC 0x40000000 +#define GRUB_UDF_EXT_NREC_NALLOC 0x80000000 +#define GRUB_UDF_EXT_MASK 0xC0000000 + +#define GRUB_UDF_FID_CHAR_HIDDEN 0x01 +#define GRUB_UDF_FID_CHAR_DIRECTORY 0x02 +#define GRUB_UDF_FID_CHAR_DELETED 0x04 +#define GRUB_UDF_FID_CHAR_PARENT 0x08 +#define GRUB_UDF_FID_CHAR_METADATA 0x10 + +#define GRUB_UDF_STD_IDENT_BEA01 "BEA01" +#define GRUB_UDF_STD_IDENT_BOOT2 "BOOT2" +#define GRUB_UDF_STD_IDENT_CD001 "CD001" +#define GRUB_UDF_STD_IDENT_CDW02 "CDW02" +#define GRUB_UDF_STD_IDENT_NSR02 "NSR02" +#define GRUB_UDF_STD_IDENT_NSR03 "NSR03" +#define GRUB_UDF_STD_IDENT_TEA01 "TEA01" + +#define GRUB_UDF_CHARSPEC_TYPE_CS0 0x00 +#define GRUB_UDF_CHARSPEC_TYPE_CS1 0x01 +#define GRUB_UDF_CHARSPEC_TYPE_CS2 0x02 +#define GRUB_UDF_CHARSPEC_TYPE_CS3 0x03 +#define GRUB_UDF_CHARSPEC_TYPE_CS4 0x04 +#define GRUB_UDF_CHARSPEC_TYPE_CS5 0x05 +#define GRUB_UDF_CHARSPEC_TYPE_CS6 0x06 +#define GRUB_UDF_CHARSPEC_TYPE_CS7 0x07 +#define GRUB_UDF_CHARSPEC_TYPE_CS8 0x08 + +#define GRUB_UDF_PARTMAP_TYPE_1 1 +#define GRUB_UDF_PARTMAP_TYPE_2 2 + +struct grub_udf_lb_addr +{ + grub_uint32_t block_num; + grub_uint16_t part_ref; +} __attribute__ ((packed)); + +struct grub_udf_short_ad +{ + grub_uint32_t length; + grub_uint32_t position; +} __attribute__ ((packed)); + +struct grub_udf_long_ad +{ + grub_uint32_t length; + struct grub_udf_lb_addr block; + grub_uint8_t imp_use[6]; +} __attribute__ ((packed)); + +struct grub_udf_extent_ad +{ + grub_uint32_t length; + grub_uint32_t start; +} __attribute__ ((packed)); + +struct grub_udf_charspec +{ + grub_uint8_t charset_type; + grub_uint8_t charset_info[63]; +} __attribute__ ((packed)); + +struct grub_udf_timestamp +{ + grub_uint16_t type_and_timezone; + grub_uint16_t year; + grub_uint8_t month; + grub_uint8_t day; + grub_uint8_t hour; + grub_uint8_t minute; + grub_uint8_t second; + grub_uint8_t centi_seconds; + grub_uint8_t hundreds_of_micro_seconds; + grub_uint8_t micro_seconds; +} __attribute__ ((packed)); + +struct grub_udf_regid +{ + grub_uint8_t flags; + grub_uint8_t ident[23]; + grub_uint8_t ident_suffix[8]; +} __attribute__ ((packed)); + +struct grub_udf_tag +{ + grub_uint16_t tag_ident; + grub_uint16_t desc_version; + grub_uint8_t tag_checksum; + grub_uint8_t reserved; + grub_uint16_t tag_serial_number; + grub_uint16_t desc_crc; + grub_uint16_t desc_crc_length; + grub_uint32_t tag_location; +} __attribute__ ((packed)); + +struct grub_udf_fileset +{ + struct grub_udf_tag tag; + struct grub_udf_timestamp datetime; + grub_uint16_t interchange_level; + grub_uint16_t max_interchange_level; + grub_uint32_t charset_list; + grub_uint32_t max_charset_list; + grub_uint32_t fileset_num; + grub_uint32_t fileset_desc_num; + struct grub_udf_charspec vol_charset; + grub_uint8_t vol_ident[128]; + struct grub_udf_charspec fileset_charset; + grub_uint8_t fileset_ident[32]; + grub_uint8_t copyright_file_ident[32]; + grub_uint8_t abstract_file_ident[32]; + struct grub_udf_long_ad root_icb; + struct grub_udf_regid domain_ident; + struct grub_udf_long_ad next_ext; + struct grub_udf_long_ad streamdir_icb; +} __attribute__ ((packed)); + +struct grub_udf_icbtag +{ + grub_uint32_t prior_recorded_num_direct_entries; + grub_uint16_t strategy_type; + grub_uint16_t strategy_parameter; + grub_uint16_t num_entries; + grub_uint8_t reserved; + grub_uint8_t file_type; + struct grub_udf_lb_addr parent_idb; + grub_uint16_t flags; +} __attribute__ ((packed)); + +struct grub_udf_file_ident +{ + struct grub_udf_tag tag; + grub_uint16_t version_num; + grub_uint8_t characteristics; + grub_uint8_t file_ident_length; + struct grub_udf_long_ad icb; + grub_uint16_t imp_use_length; +} __attribute__ ((packed)); + +struct grub_udf_file_entry +{ + struct grub_udf_tag tag; + struct grub_udf_icbtag icbtag; + grub_uint32_t uid; + grub_uint32_t gid; + grub_uint32_t permissions; + grub_uint16_t link_count; + grub_uint8_t record_format; + grub_uint8_t record_display_attr; + grub_uint32_t record_length; + grub_uint64_t file_size; + grub_uint64_t blocks_recorded; + struct grub_udf_timestamp access_time; + struct grub_udf_timestamp modification_time; + struct grub_udf_timestamp attr_time; + grub_uint32_t checkpoint; + struct grub_udf_long_ad extended_attr_idb; + struct grub_udf_regid imp_ident; + grub_uint64_t unique_id; + grub_uint32_t ext_attr_length; + grub_uint32_t alloc_descs_length; + grub_uint8_t ext_attr[1872]; +} __attribute__ ((packed)); + +struct grub_udf_extended_file_entry +{ + struct grub_udf_tag tag; + struct grub_udf_icbtag icbtag; + grub_uint32_t uid; + grub_uint32_t gid; + grub_uint32_t permissions; + grub_uint16_t link_count; + grub_uint8_t record_format; + grub_uint8_t record_display_attr; + grub_uint32_t record_length; + grub_uint64_t file_size; + grub_uint64_t object_size; + grub_uint64_t blocks_recorded; + struct grub_udf_timestamp access_time; + struct grub_udf_timestamp modification_time; + struct grub_udf_timestamp create_time; + struct grub_udf_timestamp attr_time; + grub_uint32_t checkpoint; + grub_uint32_t reserved; + struct grub_udf_long_ad extended_attr_icb; + struct grub_udf_long_ad streamdir_icb; + struct grub_udf_regid imp_ident; + grub_uint64_t unique_id; + grub_uint32_t ext_attr_length; + grub_uint32_t alloc_descs_length; + grub_uint8_t ext_attr[1832]; +} __attribute__ ((packed)); + +struct grub_udf_vrs +{ + grub_uint8_t type; + grub_uint8_t magic[5]; + grub_uint8_t version; +} __attribute__ ((packed)); + +struct grub_udf_avdp +{ + struct grub_udf_tag tag; + struct grub_udf_extent_ad vds; +} __attribute__ ((packed)); + +struct grub_udf_pd +{ + struct grub_udf_tag tag; + grub_uint32_t seq_num; + grub_uint16_t flags; + grub_uint16_t part_num; + struct grub_udf_regid contents; + grub_uint8_t contents_use[128]; + grub_uint32_t access_type; + grub_uint32_t start; + grub_uint32_t length; +} __attribute__ ((packed)); + +struct grub_udf_partmap +{ + grub_uint8_t type; + grub_uint8_t length; + union + { + struct + { + grub_uint16_t seq_num; + grub_uint16_t part_num; + } type1; + + struct + { + grub_uint8_t ident[62]; + } type2; + }; +}; + +struct grub_udf_lvd +{ + struct grub_udf_tag tag; + grub_uint32_t seq_num; + struct grub_udf_charspec charset; + grub_uint8_t ident[128]; + grub_uint32_t bsize; + struct grub_udf_regid domain_ident; + struct grub_udf_long_ad root_fileset; + grub_uint32_t map_table_length; + grub_uint32_t num_part_maps; + struct grub_udf_regid imp_ident; + grub_uint8_t imp_use[128]; + struct grub_udf_extent_ad integrity_seq_ext; + grub_uint8_t part_maps[1608]; +} __attribute__ ((packed)); + +struct grub_udf_aed +{ + struct grub_udf_tag tag; + grub_uint32_t prev_ae; + grub_uint32_t ae_len; +} __attribute__ ((packed)); + +struct grub_udf_data +{ + grub_disk_t disk; + struct grub_udf_lvd lvd; + struct grub_udf_pd pds[GRUB_UDF_MAX_PDS]; + struct grub_udf_partmap *pms[GRUB_UDF_MAX_PMS]; + struct grub_udf_long_ad root_icb; + int npd, npm, lbshift; +}; + +struct grub_fshelp_node +{ + struct grub_udf_data *data; + union + { + struct grub_udf_file_entry fe; + struct grub_udf_extended_file_entry efe; + }; + int part_ref; +}; + +static grub_dl_t my_mod; + +static grub_uint32_t +grub_udf_get_block (struct grub_udf_data *data, + grub_uint16_t part_ref, grub_uint32_t block) +{ + part_ref = U16 (part_ref); + + if (part_ref >= data->npm) + { + grub_error (GRUB_ERR_BAD_FS, "invalid part ref"); + return 0; + } + + return (U32 (data->pds[data->pms[part_ref]->type1.part_num].start) + + U32 (block)); +} + +static grub_err_t +grub_udf_read_icb (struct grub_udf_data *data, + struct grub_udf_long_ad *icb, + struct grub_fshelp_node *node) +{ + grub_uint32_t block; + + block = grub_udf_get_block (data, + icb->block.part_ref, + icb->block.block_num); + + if (grub_errno) + return grub_errno; + + if (grub_disk_read (data->disk, block << data->lbshift, 0, + sizeof (struct grub_udf_file_entry), + &node->fe)) + return grub_errno; + + if ((U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FE) && + (U16 (node->fe.tag.tag_ident) != GRUB_UDF_TAG_IDENT_EFE)) + return grub_error (GRUB_ERR_BAD_FS, "invalid fe/efe descriptor"); + + node->part_ref = icb->block.part_ref; + node->data = data; + return 0; +} + +static grub_disk_addr_t +grub_udf_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + char *buf = NULL; + char *ptr; + grub_ssize_t len; + grub_disk_addr_t filebytes; + + switch (U16 (node->fe.tag.tag_ident)) + { + case GRUB_UDF_TAG_IDENT_FE: + ptr = (char *) &node->fe.ext_attr[0] + U32 (node->fe.ext_attr_length); + len = U32 (node->fe.alloc_descs_length); + break; + + case GRUB_UDF_TAG_IDENT_EFE: + ptr = (char *) &node->efe.ext_attr[0] + U32 (node->efe.ext_attr_length); + len = U32 (node->efe.alloc_descs_length); + break; + + default: + grub_error (GRUB_ERR_BAD_FS, "invalid file entry"); + return 0; + } + + if ((U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK) + == GRUB_UDF_ICBTAG_FLAG_AD_SHORT) + { + struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr; + + filebytes = fileblock * U32 (node->data->lvd.bsize); + while (len >= (grub_ssize_t) sizeof (struct grub_udf_short_ad)) + { + grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff; + grub_uint32_t adtype = U32 (ad->length) >> 30; + if (adtype == 3) + { + struct grub_udf_aed *extension; + grub_disk_addr_t sec = grub_udf_get_block(node->data, + node->part_ref, + ad->position); + if (!buf) + { + buf = grub_malloc (U32 (node->data->lvd.bsize)); + if (!buf) + return 0; + } + if (grub_disk_read (node->data->disk, sec << node->data->lbshift, + 0, adlen, buf)) + goto fail; + + extension = (struct grub_udf_aed *) buf; + if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED) + { + grub_error (GRUB_ERR_BAD_FS, "invalid aed tag"); + goto fail; + } + + len = U32 (extension->ae_len); + ad = (struct grub_udf_short_ad *) + (buf + sizeof (struct grub_udf_aed)); + continue; + } + + if (filebytes < adlen) + { + grub_uint32_t ad_pos = ad->position; + grub_free (buf); + return ((U32 (ad_pos) & GRUB_UDF_EXT_MASK) ? 0 : + (grub_udf_get_block (node->data, node->part_ref, ad_pos) + + (filebytes >> (GRUB_DISK_SECTOR_BITS + + node->data->lbshift)))); + } + + filebytes -= adlen; + ad++; + len -= sizeof (struct grub_udf_short_ad); + } + } + else + { + struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr; + + filebytes = fileblock * U32 (node->data->lvd.bsize); + while (len >= (grub_ssize_t) sizeof (struct grub_udf_long_ad)) + { + grub_uint32_t adlen = U32 (ad->length) & 0x3fffffff; + grub_uint32_t adtype = U32 (ad->length) >> 30; + if (adtype == 3) + { + struct grub_udf_aed *extension; + grub_disk_addr_t sec = grub_udf_get_block(node->data, + ad->block.part_ref, + ad->block.block_num); + if (!buf) + { + buf = grub_malloc (U32 (node->data->lvd.bsize)); + if (!buf) + return 0; + } + if (grub_disk_read (node->data->disk, sec << node->data->lbshift, + 0, adlen, buf)) + goto fail; + + extension = (struct grub_udf_aed *) buf; + if (U16 (extension->tag.tag_ident) != GRUB_UDF_TAG_IDENT_AED) + { + grub_error (GRUB_ERR_BAD_FS, "invalid aed tag"); + goto fail; + } + + len = U32 (extension->ae_len); + ad = (struct grub_udf_long_ad *) + (buf + sizeof (struct grub_udf_aed)); + continue; + } + + if (filebytes < adlen) + { + grub_uint32_t ad_block_num = ad->block.block_num; + grub_uint32_t ad_part_ref = ad->block.part_ref; + grub_free (buf); + return ((U32 (ad_block_num) & GRUB_UDF_EXT_MASK) ? 0 : + (grub_udf_get_block (node->data, ad_part_ref, + ad_block_num) + + (filebytes >> (GRUB_DISK_SECTOR_BITS + + node->data->lbshift)))); + } + + filebytes -= adlen; + ad++; + len -= sizeof (struct grub_udf_long_ad); + } + } + +fail: + if (buf) + grub_free (buf); + + return 0; +} + +static grub_ssize_t +grub_udf_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR + (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + grub_off_t pos, grub_size_t len, char *buf) +{ + switch (U16 (node->fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK) + { + case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB: + { + char *ptr; + + ptr = ((U16 (node->fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ? + ((char *) &node->fe.ext_attr[0] + + U32 (node->fe.ext_attr_length)) : + ((char *) &node->efe.ext_attr[0] + + U32 (node->efe.ext_attr_length))); + + grub_memcpy (buf, ptr + pos, len); + + return len; + } + + case GRUB_UDF_ICBTAG_FLAG_AD_EXT: + grub_error (GRUB_ERR_BAD_FS, "invalid extent type"); + return 0; + } + + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_udf_read_block, + U64 (node->fe.file_size), + node->data->lbshift); +} + +static unsigned sblocklist[] = { 256, 512, 0 }; + +static struct grub_udf_data * +grub_udf_mount (grub_disk_t disk) +{ + struct grub_udf_data *data = 0; + struct grub_udf_fileset root_fs; + unsigned *sblklist; + grub_uint32_t block, vblock; + int i, lbshift; + + data = grub_malloc (sizeof (struct grub_udf_data)); + if (!data) + return 0; + + data->disk = disk; + + /* Search for Anchor Volume Descriptor Pointer (AVDP) + * and determine logical block size. */ + block = 0; + for (lbshift = 0; lbshift < 4; lbshift++) + { + for (sblklist = sblocklist; *sblklist; sblklist++) + { + struct grub_udf_avdp avdp; + + if (grub_disk_read (disk, *sblklist << lbshift, 0, + sizeof (struct grub_udf_avdp), &avdp)) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + + if (U16 (avdp.tag.tag_ident) == GRUB_UDF_TAG_IDENT_AVDP && + U32 (avdp.tag.tag_location) == *sblklist) + { + block = U32 (avdp.vds.start); + break; + } + } + + if (block) + break; + } + + if (!block) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + data->lbshift = lbshift; + + /* Search for Volume Recognition Sequence (VRS). */ + for (vblock = (32767 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1;; + vblock += (2047 >> (lbshift + GRUB_DISK_SECTOR_BITS)) + 1) + { + struct grub_udf_vrs vrs; + + if (grub_disk_read (disk, vblock << lbshift, 0, + sizeof (struct grub_udf_vrs), &vrs)) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + + if ((!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR03, 5)) || + (!grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_NSR02, 5))) + break; + + if ((grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BEA01, 5)) && + (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_BOOT2, 5)) && + (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CD001, 5)) && + (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_CDW02, 5)) && + (grub_memcmp (vrs.magic, GRUB_UDF_STD_IDENT_TEA01, 5))) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + } + + data->npd = data->npm = 0; + /* Locate Partition Descriptor (PD) and Logical Volume Descriptor (LVD). */ + while (1) + { + struct grub_udf_tag tag; + + if (grub_disk_read (disk, block << lbshift, 0, + sizeof (struct grub_udf_tag), &tag)) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + + tag.tag_ident = U16 (tag.tag_ident); + if (tag.tag_ident == GRUB_UDF_TAG_IDENT_PD) + { + if (data->npd >= GRUB_UDF_MAX_PDS) + { + grub_error (GRUB_ERR_BAD_FS, "too many PDs"); + goto fail; + } + + if (grub_disk_read (disk, block << lbshift, 0, + sizeof (struct grub_udf_pd), + &data->pds[data->npd])) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + + data->npd++; + } + else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_LVD) + { + int k; + + struct grub_udf_partmap *ppm; + + if (grub_disk_read (disk, block << lbshift, 0, + sizeof (struct grub_udf_lvd), + &data->lvd)) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + + if (data->npm + U32 (data->lvd.num_part_maps) > GRUB_UDF_MAX_PMS) + { + grub_error (GRUB_ERR_BAD_FS, "too many partition maps"); + goto fail; + } + + ppm = (struct grub_udf_partmap *) &data->lvd.part_maps; + for (k = U32 (data->lvd.num_part_maps); k > 0; k--) + { + if (ppm->type != GRUB_UDF_PARTMAP_TYPE_1) + { + grub_error (GRUB_ERR_BAD_FS, "partmap type not supported"); + goto fail; + } + + data->pms[data->npm++] = ppm; + ppm = (struct grub_udf_partmap *) ((char *) ppm + + U32 (ppm->length)); + } + } + else if (tag.tag_ident > GRUB_UDF_TAG_IDENT_TD) + { + grub_error (GRUB_ERR_BAD_FS, "invalid tag ident"); + goto fail; + } + else if (tag.tag_ident == GRUB_UDF_TAG_IDENT_TD) + break; + + block++; + } + + for (i = 0; i < data->npm; i++) + { + int j; + + for (j = 0; j < data->npd; j++) + if (data->pms[i]->type1.part_num == data->pds[j].part_num) + { + data->pms[i]->type1.part_num = j; + break; + } + + if (j == data->npd) + { + grub_error (GRUB_ERR_BAD_FS, "can\'t find PD"); + goto fail; + } + } + + block = grub_udf_get_block (data, + data->lvd.root_fileset.block.part_ref, + data->lvd.root_fileset.block.block_num); + + if (grub_errno) + goto fail; + + if (grub_disk_read (disk, block << lbshift, 0, + sizeof (struct grub_udf_fileset), &root_fs)) + { + grub_error (GRUB_ERR_BAD_FS, "not an UDF filesystem"); + goto fail; + } + + if (U16 (root_fs.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FSD) + { + grub_error (GRUB_ERR_BAD_FS, "invalid fileset descriptor"); + goto fail; + } + + data->root_icb = root_fs.root_icb; + + return data; + +fail: + grub_free (data); + return 0; +} + +static char * +read_string (grub_uint8_t *raw, grub_size_t sz) +{ + grub_uint16_t *utf16 = NULL; + char *ret; + grub_size_t utf16len = 0; + + if (raw[0] != 8 && raw[0] != 16) + return NULL; + + if (raw[0] == 8) + { + unsigned i; + utf16len = sz - 1; + utf16 = grub_malloc (utf16len * sizeof (utf16[0])); + if (!utf16) + return NULL; + for (i = 0; i < utf16len; i++) + utf16[i] = raw[i + 1]; + } + if (raw[0] == 16) + { + unsigned i; + utf16len = (sz - 1) / 2; + utf16 = grub_malloc (utf16len * sizeof (utf16[0])); + if (!utf16) + return NULL; + for (i = 0; i < utf16len; i++) + utf16[i] = (raw[2 * i + 1] << 8) | raw[2*i + 2]; + } + ret = grub_malloc (utf16len * 3 + 1); + if (ret) + *grub_utf16_to_utf8 ((grub_uint8_t *) ret, utf16, utf16len) = '\0'; + grub_free (utf16); + return ret; +} + +static int +grub_udf_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + grub_fshelp_node_t child; + struct grub_udf_file_ident dirent; + grub_off_t offset = 0; + + child = grub_malloc (sizeof (struct grub_fshelp_node)); + if (!child) + return 0; + + /* The current directory is not stored. */ + grub_memcpy ((char *) child, (char *) dir, + sizeof (struct grub_fshelp_node)); + + if (hook (".", GRUB_FSHELP_DIR, child)) + return 1; + + while (offset < U64 (dir->fe.file_size)) + { + if (grub_udf_read_file (dir, 0, offset, sizeof (dirent), + (char *) &dirent) != sizeof (dirent)) + return 0; + + if (U16 (dirent.tag.tag_ident) != GRUB_UDF_TAG_IDENT_FID) + { + grub_error (GRUB_ERR_BAD_FS, "invalid fid tag"); + return 0; + } + + offset += sizeof (dirent) + U16 (dirent.imp_use_length); + if (!(dirent.characteristics & GRUB_UDF_FID_CHAR_DELETED)) + { + child = grub_malloc (sizeof (struct grub_fshelp_node)); + if (!child) + return 0; + + if (grub_udf_read_icb (dir->data, &dirent.icb, child)) + return 0; + + if (dirent.characteristics & GRUB_UDF_FID_CHAR_PARENT) + { + /* This is the parent directory. */ + if (hook ("..", GRUB_FSHELP_DIR, child)) + return 1; + } + else + { + enum grub_fshelp_filetype type; + char *filename; + grub_uint8_t raw[dirent.file_ident_length]; + + type = ((dirent.characteristics & GRUB_UDF_FID_CHAR_DIRECTORY) ? + (GRUB_FSHELP_DIR) : (GRUB_FSHELP_REG)); + + if ((grub_udf_read_file (dir, 0, offset, + dirent.file_ident_length, + (char *) raw)) + != dirent.file_ident_length) + return 0; + + filename = read_string (raw, dirent.file_ident_length); + if (!filename) + grub_print_error (); + + if (filename && hook (filename, type, child)) + { + grub_free (filename); + return 1; + } + grub_free (filename); + } + } + + /* Align to dword boundary. */ + offset = (offset + dirent.file_ident_length + 3) & (~3); + } + + return 0; +} + +static grub_err_t +grub_udf_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_udf_data *data = 0; + struct grub_fshelp_node rootnode; + struct grub_fshelp_node *foundnode; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_udf_mount (device->disk); + if (!data) + goto fail; + + if (grub_udf_read_icb (data, &data->root_icb, &rootnode)) + goto fail; + + if (grub_fshelp_find_file (path, &rootnode, + &foundnode, + grub_udf_iterate_dir, 0, GRUB_FSHELP_DIR)) + goto fail; + + grub_udf_iterate_dir (foundnode, iterate); + + if (foundnode != &rootnode) + grub_free (foundnode); + +fail: + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_err_t +grub_udf_open (struct grub_file *file, const char *name) +{ + struct grub_udf_data *data; + struct grub_fshelp_node rootnode; + struct grub_fshelp_node *foundnode; + + grub_dl_ref (my_mod); + + data = grub_udf_mount (file->device->disk); + if (!data) + goto fail; + + if (grub_udf_read_icb (data, &data->root_icb, &rootnode)) + goto fail; + + if (grub_fshelp_find_file (name, &rootnode, + &foundnode, + grub_udf_iterate_dir, 0, GRUB_FSHELP_REG)) + goto fail; + + file->data = foundnode; + file->offset = 0; + file->size = U64 (foundnode->fe.file_size); + + return 0; + +fail: + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static grub_ssize_t +grub_udf_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data; + + return grub_udf_read_file (node, file->read_hook, file->offset, len, buf); +} + +static grub_err_t +grub_udf_close (grub_file_t file) +{ + if (file->data) + { + struct grub_fshelp_node *node = (struct grub_fshelp_node *) file->data; + + grub_free (node->data); + grub_free (node); + } + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_udf_label (grub_device_t device, char **label) +{ + struct grub_udf_data *data; + data = grub_udf_mount (device->disk); + + if (data) + { + *label = read_string (data->lvd.ident, sizeof (data->lvd.ident)); + grub_free (data); + } + else + *label = 0; + + return grub_errno; +} + +static struct grub_fs grub_udf_fs = { + .name = "udf", + .dir = grub_udf_dir, + .open = grub_udf_open, + .read = grub_udf_read, + .close = grub_udf_close, + .label = grub_udf_label, + .next = 0 +}; + +GRUB_MOD_INIT (udf) +{ + grub_fs_register (&grub_udf_fs); + my_mod = mod; +} + +GRUB_MOD_FINI (udf) +{ + grub_fs_unregister (&grub_udf_fs); +} diff --git a/libr/fs/p/grub/fs/ufs.c b/libr/fs/p/grub/fs/ufs.c new file mode 100644 index 0000000000..2b1021db65 --- /dev/null +++ b/libr/fs/p/grub/fs/ufs.c @@ -0,0 +1,810 @@ +/* ufs.c - Unix File System */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef MODE_UFS2 +#define GRUB_UFS_MAGIC 0x19540119 +#else +#define GRUB_UFS_MAGIC 0x11954 +#endif + +#define GRUB_UFS_INODE 2 +#define GRUB_UFS_FILETYPE_DIR 4 +#define GRUB_UFS_FILETYPE_LNK 10 +#define GRUB_UFS_MAX_SYMLNK_CNT 8 + +#define GRUB_UFS_DIRBLKS 12 +#define GRUB_UFS_INDIRBLKS 3 + +#define GRUB_UFS_ATTR_TYPE 0160000 +#define GRUB_UFS_ATTR_FILE 0100000 +#define GRUB_UFS_ATTR_DIR 0040000 +#define GRUB_UFS_ATTR_LNK 0120000 + +#define GRUB_UFS_VOLNAME_LEN 32 + +/* Calculate in which group the inode can be found. */ +#define UFS_BLKSZ(sblock) (grub_le_to_cpu32 (sblock->bsize)) + +#define INODE(data,field) data->inode. field +#ifdef MODE_UFS2 +#define INODE_ENDIAN(data,field,bits1,bits2) grub_le_to_cpu##bits2 (data->inode.field) +#else +#define INODE_ENDIAN(data,field,bits1,bits2) grub_le_to_cpu##bits1 (data->inode.field) +#endif + +#define INODE_SIZE(data) INODE_ENDIAN (data,size,32,64) +#define INODE_NBLOCKS(data) INODE_ENDIAN (data,nblocks,32,64) + +#define INODE_MODE(data) INODE_ENDIAN (data,mode,16,16) +#ifdef MODE_UFS2 +#define INODE_BLKSZ 8 +#else +#define INODE_BLKSZ 4 +#endif +#ifdef MODE_UFS2 +#define UFS_INODE_PER_BLOCK 2 +#else +#define UFS_INODE_PER_BLOCK 4 +#endif +#define INODE_DIRBLOCKS(data,blk) INODE_ENDIAN \ + (data,blocks.dir_blocks[blk],32,64) +#define INODE_INDIRBLOCKS(data,blk) INODE_ENDIAN \ + (data,blocks.indir_blocks[blk],32,64) + +/* The blocks on which the superblock can be found. */ +static int sblocklist[] = { 128, 16, 0, 512, -1 }; + +struct grub_ufs_sblock +{ + grub_uint8_t unused[16]; + /* The offset of the inodes in the cylinder group. */ + grub_uint32_t inoblk_offs; + + grub_uint8_t unused2[4]; + + /* The start of the cylinder group. */ + grub_uint32_t cylg_offset; + grub_uint32_t cylg_mask; + + grub_uint32_t mtime; + grub_uint8_t unused4[12]; + + /* The size of a block in bytes. */ + grub_int32_t bsize; + grub_uint8_t unused5[48]; + + /* The size of filesystem blocks to disk blocks. */ + grub_uint32_t log2_blksz; + grub_uint8_t unused6[40]; + grub_uint32_t uuidhi; + grub_uint32_t uuidlow; + grub_uint8_t unused7[32]; + + /* Inodes stored per cylinder group. */ + grub_uint32_t ino_per_group; + + /* The frags per cylinder group. */ + grub_uint32_t frags_per_group; + + grub_uint8_t unused8[488]; + + /* Volume name for UFS2. */ + grub_uint8_t volume_name[GRUB_UFS_VOLNAME_LEN]; + grub_uint8_t unused9[360]; + + grub_uint64_t mtime2; + grub_uint8_t unused10[292]; + + /* Magic value to check if this is really a UFS filesystem. */ + grub_uint32_t magic; +}; + +#ifdef MODE_UFS2 +/* UFS inode. */ +struct grub_ufs_inode +{ + grub_uint16_t mode; + grub_uint16_t nlinks; + grub_uint32_t uid; + grub_uint32_t gid; + grub_uint32_t blocksize; + grub_int64_t size; + grub_int64_t nblocks; + grub_uint64_t atime; + grub_uint64_t mtime; + grub_uint64_t ctime; + grub_uint64_t create_time; + grub_uint32_t atime_sec; + grub_uint32_t mtime_sec; + grub_uint32_t ctime_sec; + grub_uint32_t create_time_sec; + grub_uint32_t gen; + grub_uint32_t kernel_flags; + grub_uint32_t flags; + grub_uint32_t extsz; + grub_uint64_t ext[2]; + union + { + struct + { + grub_uint64_t dir_blocks[GRUB_UFS_DIRBLKS]; + grub_uint64_t indir_blocks[GRUB_UFS_INDIRBLKS]; + } blocks; + grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 8]; + }; + + grub_uint8_t unused[24]; +} __attribute__ ((packed)); +#else +/* UFS inode. */ +struct grub_ufs_inode +{ + grub_uint16_t mode; + grub_uint16_t nlinks; + grub_uint16_t uid; + grub_uint16_t gid; + grub_int64_t size; + grub_uint64_t atime; + grub_uint64_t mtime; + grub_uint64_t ctime; + union + { + struct + { + grub_uint32_t dir_blocks[GRUB_UFS_DIRBLKS]; + grub_uint32_t indir_blocks[GRUB_UFS_INDIRBLKS]; + } blocks; + grub_uint8_t symlink[(GRUB_UFS_DIRBLKS + GRUB_UFS_INDIRBLKS) * 4]; + }; + grub_uint32_t flags; + grub_uint32_t nblocks; + grub_uint32_t gen; + grub_uint32_t unused; + grub_uint8_t pad[12]; +} __attribute__ ((packed)); +#endif + +/* Directory entry. */ +struct grub_ufs_dirent +{ + grub_uint32_t ino; + grub_uint16_t direntlen; + union + { + grub_uint16_t namelen; + struct + { + grub_uint8_t filetype_bsd; + grub_uint8_t namelen_bsd; + }; + }; +} __attribute__ ((packed)); + +/* Information about a "mounted" ufs filesystem. */ +struct grub_ufs_data +{ + struct grub_ufs_sblock sblock; + grub_disk_t disk; + struct grub_ufs_inode inode; + int ino; + int linknest; +}; + +static grub_dl_t my_mod; + +/* Forward declaration. */ +static grub_err_t grub_ufs_find_file (struct grub_ufs_data *data, + const char *path); + + +static grub_disk_addr_t +grub_ufs_get_file_block (struct grub_ufs_data *data, unsigned int blk) +{ + struct grub_ufs_sblock *sblock = &data->sblock; + unsigned int indirsz; + int log2_blksz; + + /* Direct. */ + if (blk < GRUB_UFS_DIRBLKS) + return INODE_DIRBLOCKS (data, blk); + + log2_blksz = grub_le_to_cpu32 (data->sblock.log2_blksz); + + blk -= GRUB_UFS_DIRBLKS; + + indirsz = UFS_BLKSZ (sblock) / INODE_BLKSZ; + /* Single indirect block. */ + if (blk < indirsz) + { +#ifdef MODE_UFS2 + grub_uint64_t indir[UFS_BLKSZ (sblock) / sizeof (grub_uint64_t)]; +#else + grub_uint32_t indir[UFS_BLKSZ (sblock) / sizeof (grub_uint32_t)]; +#endif + grub_disk_read (data->disk, INODE_INDIRBLOCKS (data, 0) << log2_blksz, + 0, sizeof (indir), indir); + return indir[blk]; + } + blk -= indirsz; + + /* Double indirect block. */ + if (blk < indirsz * indirsz) + { +#ifdef MODE_UFS2 + grub_uint64_t indir[UFS_BLKSZ (sblock) / sizeof (grub_uint64_t)]; +#else + grub_uint32_t indir[UFS_BLKSZ (sblock) / sizeof (grub_uint32_t)]; +#endif + + grub_disk_read (data->disk, INODE_INDIRBLOCKS (data, 1) << log2_blksz, + 0, sizeof (indir), indir); + grub_disk_read (data->disk, + (indir [blk / indirsz]) + << log2_blksz, + 0, sizeof (indir), indir); + + return indir[blk % indirsz]; + } + + + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "ufs does not support triple indirect blocks"); + return 0; +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_ufs_read_file (struct grub_ufs_data *data, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + struct grub_ufs_sblock *sblock = &data->sblock; + int i; + int blockcnt; + + /* Adjust len so it we can't read past the end of the file. */ + if (len + pos > INODE_SIZE (data)) + len = INODE_SIZE (data) - pos; + + blockcnt = (len + pos + UFS_BLKSZ (sblock) - 1) / UFS_BLKSZ (sblock); + + for (i = pos / UFS_BLKSZ (sblock); i < blockcnt; i++) + { + int blknr; + int blockoff = pos % UFS_BLKSZ (sblock); + int blockend = UFS_BLKSZ (sblock); + + int skipfirst = 0; + + blknr = grub_ufs_get_file_block (data, i); + if (grub_errno) + return -1; + + /* Last block. */ + if (i == blockcnt - 1) + { + blockend = (len + pos) % UFS_BLKSZ (sblock); + + if (!blockend) + blockend = UFS_BLKSZ (sblock); + } + + /* First block. */ + if (i == (pos / (int) UFS_BLKSZ (sblock))) + { + skipfirst = blockoff; + blockend -= skipfirst; + } + + /* XXX: If the block number is 0 this block is not stored on + disk but is zero filled instead. */ + if (blknr) + { + data->disk->read_hook = read_hook; + grub_disk_read (data->disk, + blknr << grub_le_to_cpu32 (data->sblock.log2_blksz), + skipfirst, blockend, buf); + data->disk->read_hook = 0; + if (grub_errno) + return -1; + } + else + grub_memset (buf, UFS_BLKSZ (sblock) - skipfirst, 0); + + buf += UFS_BLKSZ (sblock) - skipfirst; + } + + return len; +} + +/* Read inode INO from the mounted filesystem described by DATA. This + inode is used by default now. */ +static grub_err_t +grub_ufs_read_inode (struct grub_ufs_data *data, int ino, char *inode) +{ + struct grub_ufs_sblock *sblock = &data->sblock; + + /* Determine the group the inode is in. */ + int group = ino / grub_le_to_cpu32 (sblock->ino_per_group); + + /* Determine the inode within the group. */ + int grpino = ino % grub_le_to_cpu32 (sblock->ino_per_group); + + /* The first block of the group. */ + int grpblk = group * (grub_le_to_cpu32 (sblock->frags_per_group)); + +#ifndef MODE_UFS2 + grpblk += grub_le_to_cpu32 (sblock->cylg_offset) + * (group & (~grub_le_to_cpu32 (sblock->cylg_mask))); +#endif + + if (!inode) + { + inode = (char *) &data->inode; + data->ino = ino; + } + + grub_disk_read (data->disk, + ((grub_le_to_cpu32 (sblock->inoblk_offs) + grpblk) + << grub_le_to_cpu32 (data->sblock.log2_blksz)) + + grpino / UFS_INODE_PER_BLOCK, + (grpino % UFS_INODE_PER_BLOCK) + * sizeof (struct grub_ufs_inode), + sizeof (struct grub_ufs_inode), + inode); + + return grub_errno; +} + + +/* Lookup the symlink the current inode points to. INO is the inode + number of the directory the symlink is relative to. */ +static grub_err_t +grub_ufs_lookup_symlink (struct grub_ufs_data *data, int ino) +{ + char symlink[INODE_SIZE (data)]; + + if (++data->linknest > GRUB_UFS_MAX_SYMLNK_CNT) + return grub_error (GRUB_ERR_SYMLINK_LOOP, "too deep nesting of symlinks"); + + if (INODE_NBLOCKS (data) == 0) + grub_strcpy (symlink, (char *) INODE (data, symlink)); + else + { + grub_disk_read (data->disk, + (INODE_DIRBLOCKS (data, 0) + << grub_le_to_cpu32 (data->sblock.log2_blksz)), + 0, INODE_SIZE (data), symlink); + symlink[INODE_SIZE (data)] = '\0'; + } + + /* The symlink is an absolute path, go back to the root inode. */ + if (symlink[0] == '/') + ino = GRUB_UFS_INODE; + + /* Now load in the old inode. */ + if (grub_ufs_read_inode (data, ino, 0)) + return grub_errno; + + grub_ufs_find_file (data, symlink); + if (grub_errno) + grub_error (grub_errno, "cannot follow symlink `%s'", symlink); + + return grub_errno; +} + + +/* Find the file with the pathname PATH on the filesystem described by + DATA. */ +static grub_err_t +grub_ufs_find_file (struct grub_ufs_data *data, const char *path) +{ + char fpath[grub_strlen (path) + 1]; + char *name = fpath; + char *next; + unsigned int pos = 0; + int dirino; + + grub_strcpy (fpath, path); + + /* Skip the first slash. */ + if (name[0] == '/') + { + name++; + if (!*name) + return 0; + } + + /* Extract the actual part from the pathname. */ + next = grub_strchr (name, '/'); + if (next) + { + next[0] = '\0'; + next++; + } + + do + { + struct grub_ufs_dirent dirent; + int namelen; + + if (grub_strlen (name) == 0) + return GRUB_ERR_NONE; + + if (grub_ufs_read_file (data, 0, pos, sizeof (dirent), + (char *) &dirent) < 0) + return grub_errno; + +#ifdef MODE_UFS2 + namelen = dirent.namelen_bsd; +#else + namelen = grub_le_to_cpu16 (dirent.namelen); +#endif + { + char filename[namelen + 1]; + + if (grub_ufs_read_file (data, 0, pos + sizeof (dirent), + namelen, filename) < 0) + return grub_errno; + + filename[namelen] = '\0'; + + if (!grub_strcmp (name, filename)) + { + dirino = data->ino; + grub_ufs_read_inode (data, grub_le_to_cpu32 (dirent.ino), 0); + + if ((INODE_MODE(data) & GRUB_UFS_ATTR_TYPE) + == GRUB_UFS_ATTR_LNK) + { + grub_ufs_lookup_symlink (data, dirino); + if (grub_errno) + return grub_errno; + } + + if (!next) + return 0; + + pos = 0; + + name = next; + next = grub_strchr (name, '/'); + if (next) + { + next[0] = '\0'; + next++; + } + + if ((INODE_MODE(data) & GRUB_UFS_ATTR_TYPE) != GRUB_UFS_ATTR_DIR) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + + continue; + } + } + + pos += grub_le_to_cpu16 (dirent.direntlen); + } while (pos < INODE_SIZE (data)); + + grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found"); + return grub_errno; +} + + +/* Mount the filesystem on the disk DISK. */ +static struct grub_ufs_data * +grub_ufs_mount (grub_disk_t disk) +{ + struct grub_ufs_data *data; + int *sblklist = sblocklist; + + data = grub_malloc (sizeof (struct grub_ufs_data)); + if (!data) + return 0; + + /* Find a UFS sblock. */ + while (*sblklist != -1) + { + grub_disk_read (disk, *sblklist, 0, sizeof (struct grub_ufs_sblock), + &data->sblock); + if (grub_errno) + goto fail; + + if (grub_le_to_cpu32 (data->sblock.magic) == GRUB_UFS_MAGIC) + { + data->disk = disk; + data->linknest = 0; + return data; + } + sblklist++; + } + + fail: + + if (grub_errno == GRUB_ERR_NONE || grub_errno == GRUB_ERR_OUT_OF_RANGE) + { +#ifdef MODE_UFS2 + grub_error (GRUB_ERR_BAD_FS, "not an ufs2 filesystem"); +#else + grub_error (GRUB_ERR_BAD_FS, "not an ufs1 filesystem"); +#endif + } + + grub_free (data); + + return 0; +} + + +static grub_err_t +grub_ufs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_ufs_data *data; + unsigned int pos = 0; + + data = grub_ufs_mount (device->disk); + if (!data) + return grub_errno; + + grub_ufs_read_inode (data, GRUB_UFS_INODE, 0); + if (grub_errno) + return grub_errno; + + if (!path || path[0] != '/') + { + grub_error (GRUB_ERR_BAD_FILENAME, "bad filename"); + return grub_errno; + } + + grub_ufs_find_file (data, path); + if (grub_errno) + goto fail; + + if ((INODE_MODE (data) & GRUB_UFS_ATTR_TYPE) != GRUB_UFS_ATTR_DIR) + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + goto fail; + } + + while (pos < INODE_SIZE (data)) + { + struct grub_ufs_dirent dirent; + int namelen; + + if (grub_ufs_read_file (data, 0, pos, sizeof (dirent), + (char *) &dirent) < 0) + break; + +#ifdef MODE_UFS2 + namelen = dirent.namelen_bsd; +#else + namelen = grub_le_to_cpu16 (dirent.namelen); +#endif + + { + char filename[namelen + 1]; + struct grub_dirhook_info info; + struct grub_ufs_inode inode; + + grub_memset (&info, 0, sizeof (info)); + + if (grub_ufs_read_file (data, 0, pos + sizeof (dirent), + namelen, filename) < 0) + break; + + filename[namelen] = '\0'; + grub_ufs_read_inode (data, dirent.ino, (char *) &inode); + + info.dir = ((grub_le_to_cpu16 (inode.mode) & GRUB_UFS_ATTR_TYPE) + == GRUB_UFS_ATTR_DIR); + info.mtime = grub_le_to_cpu64 (inode.mtime); + info.mtimeset = 1; + + if (hook (filename, &info)) + break; + } + + pos += grub_le_to_cpu16 (dirent.direntlen); + } + + fail: + grub_free (data); + + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_ufs_open (struct grub_file *file, const char *name) +{ + struct grub_ufs_data *data; + data = grub_ufs_mount (file->device->disk); + if (!data) + return grub_errno; + + grub_ufs_read_inode (data, 2, 0); + if (grub_errno) + { + grub_free (data); + return grub_errno; + } + + if (!name || name[0] != '/') + { + grub_error (GRUB_ERR_BAD_FILENAME, "bad filename"); + return grub_errno; + } + + grub_ufs_find_file (data, name); + if (grub_errno) + { + grub_free (data); + return grub_errno; + } + + file->data = data; + file->size = INODE_SIZE (data); + + return GRUB_ERR_NONE; +} + + +static grub_ssize_t +grub_ufs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_ufs_data *data = + (struct grub_ufs_data *) file->data; + + return grub_ufs_read_file (data, file->read_hook, file->offset, len, buf); +} + + +static grub_err_t +grub_ufs_close (grub_file_t file) +{ + grub_free (file->data); + + return GRUB_ERR_NONE; +} + + +#ifdef MODE_UFS2 +static grub_err_t +grub_ufs_label (grub_device_t device, char **label) +{ + struct grub_ufs_data *data = 0; + + grub_dl_ref (my_mod); + + *label = 0; + + data = grub_ufs_mount (device->disk); + if (data) + *label = grub_strdup ((char *) data->sblock.volume_name); + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} +#endif + +static grub_err_t +grub_ufs_uuid (grub_device_t device, char **uuid) +{ + struct grub_ufs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_ufs_mount (disk); + if (data && (data->sblock.uuidhi != 0 || data->sblock.uuidlow != 0)) + *uuid = grub_xasprintf ("%08x%08x", + (unsigned) grub_le_to_cpu32 (data->sblock.uuidhi), + (unsigned) grub_le_to_cpu32 (data->sblock.uuidlow)); + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + +/* Get mtime. */ +static grub_err_t +grub_ufs_mtime (grub_device_t device, grub_int32_t *tm) +{ + struct grub_ufs_data *data = 0; + + grub_dl_ref (my_mod); + + data = grub_ufs_mount (device->disk); + if (!data) + *tm = 0; + else +#ifdef MODE_UFS2 + *tm = grub_le_to_cpu64 (data->sblock.mtime2); +#else + *tm = grub_le_to_cpu32 (data->sblock.mtime); +#endif + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + + +static struct grub_fs grub_ufs_fs = + { +#ifdef MODE_UFS2 + .name = "ufs2", +#else + .name = "ufs1", +#endif + .dir = grub_ufs_dir, + .open = grub_ufs_open, + .read = grub_ufs_read, + .close = grub_ufs_close, +#ifdef MODE_UFS2 + .label = grub_ufs_label, +#endif + .uuid = grub_ufs_uuid, + .mtime = grub_ufs_mtime, + .next = 0 + }; + +#ifdef MODE_UFS2 +GRUB_MOD_INIT(ufs2) +#else +GRUB_MOD_INIT(ufs1) +#endif +{ + grub_fs_register (&grub_ufs_fs); + my_mod = mod; +} + +#ifdef MODE_UFS2 +GRUB_MOD_FINI(ufs2) +#else +GRUB_MOD_FINI(ufs1) +#endif +{ + grub_fs_unregister (&grub_ufs_fs); +} + diff --git a/libr/fs/p/grub/fs/ufs2.c b/libr/fs/p/grub/fs/ufs2.c new file mode 100644 index 0000000000..7f4eb95deb --- /dev/null +++ b/libr/fs/p/grub/fs/ufs2.c @@ -0,0 +1,3 @@ +/* ufs2.c - Unix File System 2 */ +#define MODE_UFS2 1 +#include "ufs.c" diff --git a/libr/fs/p/grub/fs/xfs.c b/libr/fs/p/grub/fs/xfs.c new file mode 100644 index 0000000000..3d773856e3 --- /dev/null +++ b/libr/fs/p/grub/fs/xfs.c @@ -0,0 +1,826 @@ +/* xfs.c - XFS. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define XFS_INODE_EXTENTS 9 + +#define XFS_INODE_FORMAT_INO 1 +#define XFS_INODE_FORMAT_EXT 2 +#define XFS_INODE_FORMAT_BTREE 3 + + +struct grub_xfs_sblock +{ + grub_uint8_t magic[4]; + grub_uint32_t bsize; + grub_uint8_t unused1[24]; + grub_uint16_t uuid[8]; + grub_uint8_t unused2[8]; + grub_uint64_t rootino; + grub_uint8_t unused3[20]; + grub_uint32_t agsize; + grub_uint8_t unused4[20]; + grub_uint8_t label[12]; + grub_uint8_t log2_bsize; + grub_uint8_t log2_sect; + grub_uint8_t log2_inode; + grub_uint8_t log2_inop; + grub_uint8_t log2_agblk; + grub_uint8_t unused6[67]; + grub_uint8_t log2_dirblk; +} __attribute__ ((packed)); + +struct grub_xfs_dir_header +{ + grub_uint8_t count; + grub_uint8_t smallino; + union + { + grub_uint32_t i4; + grub_uint64_t i8; + } parent __attribute__ ((packed)); +} __attribute__ ((packed)); + +struct grub_xfs_dir_entry +{ + grub_uint8_t len; + grub_uint16_t offset; + char name[1]; + /* Inode number follows, 32 bits. */ +} __attribute__ ((packed)); + +struct grub_xfs_dir2_entry +{ + grub_uint64_t inode; + grub_uint8_t len; +} __attribute__ ((packed)); + +typedef grub_uint32_t grub_xfs_extent[4]; + +struct grub_xfs_btree_node +{ + grub_uint8_t magic[4]; + grub_uint16_t level; + grub_uint16_t numrecs; + grub_uint64_t left; + grub_uint64_t right; + grub_uint64_t keys[1]; +} __attribute__ ((packed)); + +struct grub_xfs_btree_root +{ + grub_uint16_t level; + grub_uint16_t numrecs; + grub_uint64_t keys[1]; +} __attribute__ ((packed)); + +struct grub_xfs_inode +{ + grub_uint8_t magic[2]; + grub_uint16_t mode; + grub_uint8_t version; + grub_uint8_t format; + grub_uint8_t unused2[50]; + grub_uint64_t size; + grub_uint64_t nblocks; + grub_uint32_t extsize; + grub_uint32_t nextents; + grub_uint8_t unused3[20]; + union + { + char raw[156]; + struct dir + { + struct grub_xfs_dir_header dirhead; + struct grub_xfs_dir_entry direntry[1]; + } dir; + grub_xfs_extent extents[XFS_INODE_EXTENTS]; + struct grub_xfs_btree_root btree; + } data __attribute__ ((packed)); +} __attribute__ ((packed)); + +struct grub_xfs_dirblock_tail +{ + grub_uint32_t leaf_count; + grub_uint32_t leaf_stale; +} __attribute__ ((packed)); + +struct grub_fshelp_node +{ + struct grub_xfs_data *data; + grub_uint64_t ino; + int inode_read; + struct grub_xfs_inode inode; +}; + +struct grub_xfs_data +{ + struct grub_xfs_sblock sblock; + grub_disk_t disk; + int pos; + int bsize; + int agsize; + struct grub_fshelp_node diropen; +}; + +static grub_dl_t my_mod; + + + +/* Filetype information as used in inodes. */ +#define FILETYPE_INO_MASK 0170000 +#define FILETYPE_INO_REG 0100000 +#define FILETYPE_INO_DIRECTORY 0040000 +#define FILETYPE_INO_SYMLINK 0120000 + +#define GRUB_XFS_INO_AGBITS(data) \ + ((data)->sblock.log2_agblk + (data)->sblock.log2_inop) +#define GRUB_XFS_INO_INOINAG(data, ino) \ + (grub_be_to_cpu64 (ino) & ((1LL << GRUB_XFS_INO_AGBITS (data)) - 1)) +#define GRUB_XFS_INO_AG(data,ino) \ + (grub_be_to_cpu64 (ino) >> GRUB_XFS_INO_AGBITS (data)) + +#define GRUB_XFS_FSB_TO_BLOCK(data, fsb) \ + (((fsb) >> (data)->sblock.log2_agblk) * (data)->agsize \ + + ((fsb) & ((1LL << (data)->sblock.log2_agblk) - 1))) + +#define GRUB_XFS_EXTENT_OFFSET(exts,ex) \ + ((grub_be_to_cpu32 (exts[ex][0]) & ~(1 << 31)) << 23 \ + | grub_be_to_cpu32 (exts[ex][1]) >> 9) + +#define GRUB_XFS_EXTENT_BLOCK(exts,ex) \ + ((grub_uint64_t) (grub_be_to_cpu32 (exts[ex][1]) \ + & (0x1ff)) << 43 \ + | (grub_uint64_t) grub_be_to_cpu32 (exts[ex][2]) << 11 \ + | grub_be_to_cpu32 (exts[ex][3]) >> 21) + +#define GRUB_XFS_EXTENT_SIZE(exts,ex) \ + (grub_be_to_cpu32 (exts[ex][3]) & ((1 << 20) - 1)) + +#define GRUB_XFS_ROUND_TO_DIRENT(pos) ((((pos) + 8 - 1) / 8) * 8) +#define GRUB_XFS_NEXT_DIRENT(pos,len) \ + (pos) + GRUB_XFS_ROUND_TO_DIRENT (8 + 1 + len + 2) + +static inline grub_uint64_t +grub_xfs_inode_block (struct grub_xfs_data *data, + grub_uint64_t ino) +{ + long long int inoinag = GRUB_XFS_INO_INOINAG (data, ino); + long long ag = GRUB_XFS_INO_AG (data, ino); + long long block; + + block = (inoinag >> data->sblock.log2_inop) + ag * data->agsize; + block <<= (data->sblock.log2_bsize - GRUB_DISK_SECTOR_BITS); + return block; +} + + +static inline int +grub_xfs_inode_offset (struct grub_xfs_data *data, + grub_uint64_t ino) +{ + int inoag = GRUB_XFS_INO_INOINAG (data, ino); + return ((inoag & ((1 << data->sblock.log2_inop) - 1)) << + data->sblock.log2_inode); +} + + +static grub_err_t +grub_xfs_read_inode (struct grub_xfs_data *data, grub_uint64_t ino, + struct grub_xfs_inode *inode) +{ + grub_uint64_t block = grub_xfs_inode_block (data, ino); + int offset = grub_xfs_inode_offset (data, ino); + + /* Read the inode. */ + if (grub_disk_read (data->disk, block, offset, + 1 << data->sblock.log2_inode, inode)) + return grub_errno; + + if (grub_strncmp ((char *) inode->magic, "IN", 2)) + return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode"); + + return 0; +} + + +static grub_disk_addr_t +grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) +{ + struct grub_xfs_btree_node *leaf = 0; + int ex, nrec; + grub_xfs_extent *exts; + grub_uint64_t ret = 0; + + if (node->inode.format == XFS_INODE_FORMAT_BTREE) + { + grub_uint64_t *keys; + + leaf = grub_malloc (node->data->sblock.bsize); + if (leaf == 0) + return 0; + + nrec = grub_be_to_cpu16 (node->inode.data.btree.numrecs); + keys = &node->inode.data.btree.keys[0]; + do + { + int i; + + for (i = 0; i < nrec; i++) + { + if (fileblock < grub_be_to_cpu64 (keys[i])) + break; + } + + /* Sparse block. */ + if (i == 0) + { + grub_free (leaf); + return 0; + } + + if (grub_disk_read (node->data->disk, + grub_be_to_cpu64 (keys[i - 1 + nrec]) + << (node->data->sblock.log2_bsize + - GRUB_DISK_SECTOR_BITS), + 0, node->data->sblock.bsize, leaf)) + return 0; + + if (grub_strncmp ((char *) leaf->magic, "BMAP", 4)) + { + grub_free (leaf); + grub_error (GRUB_ERR_BAD_FS, "not a correct XFS BMAP node"); + return 0; + } + + nrec = grub_be_to_cpu16 (leaf->numrecs); + keys = &leaf->keys[0]; + } while (leaf->level); + exts = (grub_xfs_extent *) keys; + } + else if (node->inode.format == XFS_INODE_FORMAT_EXT) + { + nrec = grub_be_to_cpu32 (node->inode.nextents); + exts = &node->inode.data.extents[0]; + } + else + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "XFS does not support inode format %d yet", + node->inode.format); + return 0; + } + + /* Iterate over each extent to figure out which extent has + the block we are looking for. */ + for (ex = 0; ex < nrec; ex++) + { + grub_uint64_t start = GRUB_XFS_EXTENT_BLOCK (exts, ex); + grub_uint64_t offset = GRUB_XFS_EXTENT_OFFSET (exts, ex); + grub_uint64_t size = GRUB_XFS_EXTENT_SIZE (exts, ex); + + /* Sparse block. */ + if (fileblock < offset) + break; + else if (fileblock < offset + size) + { + ret = (fileblock - offset + start); + break; + } + } + + if (leaf) + grub_free (leaf); + + return GRUB_XFS_FSB_TO_BLOCK(node->data, ret); +} + + +/* Read LEN bytes from the file described by DATA starting with byte + POS. Return the amount of read bytes in READ. */ +static grub_ssize_t +grub_xfs_read_file (grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length), + int pos, grub_size_t len, char *buf) +{ + return grub_fshelp_read_file (node->data->disk, node, read_hook, + pos, len, buf, grub_xfs_read_block, + grub_be_to_cpu64 (node->inode.size), + node->data->sblock.log2_bsize + - GRUB_DISK_SECTOR_BITS); +} + + +static char * +grub_xfs_read_symlink (grub_fshelp_node_t node) +{ + int size = grub_be_to_cpu64 (node->inode.size); + + switch (node->inode.format) + { + case XFS_INODE_FORMAT_INO: + return grub_strndup (node->inode.data.raw, size); + + case XFS_INODE_FORMAT_EXT: + { + char *symlink; + grub_ssize_t numread; + + symlink = grub_malloc (size + 1); + if (!symlink) + return 0; + + numread = grub_xfs_read_file (node, 0, 0, size, symlink); + if (numread != size) + { + grub_free (symlink); + return 0; + } + symlink[size] = '\0'; + return symlink; + } + } + + return 0; +} + + +static enum grub_fshelp_filetype +grub_xfs_mode_to_filetype (grub_uint16_t mode) +{ + if ((grub_be_to_cpu16 (mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY) + return GRUB_FSHELP_DIR; + else if ((grub_be_to_cpu16 (mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK) + return GRUB_FSHELP_SYMLINK; + else if ((grub_be_to_cpu16 (mode) + & FILETYPE_INO_MASK) == FILETYPE_INO_REG) + return GRUB_FSHELP_REG; + return GRUB_FSHELP_UNKNOWN; +} + + +static int +grub_xfs_iterate_dir (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)) +{ + struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir; + auto int NESTED_FUNC_ATTR call_hook (grub_uint64_t ino, char *filename); + + int NESTED_FUNC_ATTR call_hook (grub_uint64_t ino, char *filename) + { + struct grub_fshelp_node *fdiro; + + fdiro = grub_malloc (sizeof (struct grub_fshelp_node) + - sizeof (struct grub_xfs_inode) + + (1 << diro->data->sblock.log2_inode)); + if (!fdiro) + return 0; + + /* The inode should be read, otherwise the filetype can + not be determined. */ + fdiro->ino = ino; + fdiro->inode_read = 1; + fdiro->data = diro->data; + grub_xfs_read_inode (diro->data, ino, &fdiro->inode); + + return hook (filename, + grub_xfs_mode_to_filetype (fdiro->inode.mode), + fdiro); + } + + switch (diro->inode.format) + { + case XFS_INODE_FORMAT_INO: + { + struct grub_xfs_dir_entry *de = &diro->inode.data.dir.direntry[0]; + int smallino = !diro->inode.data.dir.dirhead.smallino; + int i; + grub_uint64_t parent; + + /* If small inode numbers are used to pack the direntry, the + parent inode number is small too. */ + if (smallino) + { + parent = grub_be_to_cpu32 (diro->inode.data.dir.dirhead.parent.i4); + parent = grub_cpu_to_be64 (parent); + /* The header is a bit smaller than usual. */ + de = (struct grub_xfs_dir_entry *) ((char *) de - 4); + } + else + { + parent = diro->inode.data.dir.dirhead.parent.i8; + } + + /* Synthesize the direntries for `.' and `..'. */ + if (call_hook (diro->ino, ".")) + return 1; + + if (call_hook (parent, "..")) + return 1; + + for (i = 0; i < diro->inode.data.dir.dirhead.count; i++) + { + grub_uint64_t ino; + void *inopos = (((char *) de) + + sizeof (struct grub_xfs_dir_entry) + + de->len - 1); + char name[de->len + 1]; + + if (smallino) + { + ino = grub_be_to_cpu32 (*(grub_uint32_t *) inopos); + ino = grub_cpu_to_be64 (ino); + } + else + ino = *(grub_uint64_t *) inopos; + + grub_memcpy (name, de->name, de->len); + name[de->len] = '\0'; + if (call_hook (ino, name)) + return 1; + + de = ((struct grub_xfs_dir_entry *) + (((char *) de)+ sizeof (struct grub_xfs_dir_entry) + de->len + + ((smallino ? sizeof (grub_uint32_t) + : sizeof (grub_uint64_t))) - 1)); + } + break; + } + + case XFS_INODE_FORMAT_BTREE: + case XFS_INODE_FORMAT_EXT: + { + grub_ssize_t numread; + char *dirblock; + grub_uint64_t blk; + int dirblk_size, dirblk_log2; + + dirblk_log2 = (dir->data->sblock.log2_bsize + + dir->data->sblock.log2_dirblk); + dirblk_size = 1 << dirblk_log2; + + dirblock = grub_malloc (dirblk_size); + if (! dirblock) + return 0; + + /* Iterate over every block the directory has. */ + for (blk = 0; + blk < (grub_be_to_cpu64 (dir->inode.size) + >> dirblk_log2); + blk++) + { + /* The header is skipped, the first direntry is stored + from byte 16. */ + int pos = 16; + int entries; + int tail_start = (dirblk_size + - sizeof (struct grub_xfs_dirblock_tail)); + + struct grub_xfs_dirblock_tail *tail; + tail = (struct grub_xfs_dirblock_tail *) &dirblock[tail_start]; + + numread = grub_xfs_read_file (dir, 0, + blk << dirblk_log2, + dirblk_size, dirblock); + if (numread != dirblk_size) + return 0; + + entries = (grub_be_to_cpu32 (tail->leaf_count) + - grub_be_to_cpu32 (tail->leaf_stale)); + + /* Iterate over all entries within this block. */ + while (pos < (dirblk_size + - (int) sizeof (struct grub_xfs_dir2_entry))) + { + struct grub_xfs_dir2_entry *direntry; + grub_uint16_t *freetag; + char *filename; + + direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos]; + freetag = (grub_uint16_t *) direntry; + + if (*freetag == 0XFFFF) + { + grub_uint16_t *skip = (grub_uint16_t *) (freetag + 1); + + /* This entry is not used, go to the next one. */ + pos += grub_be_to_cpu16 (*skip); + + continue; + } + + filename = &dirblock[pos + sizeof (*direntry)]; + /* The byte after the filename is for the tag, which + is not used by GRUB. So it can be overwritten. */ + filename[direntry->len] = '\0'; + + if (call_hook (direntry->inode, filename)) + { + grub_free (dirblock); + return 1; + } + + /* Check if last direntry in this block is + reached. */ + entries--; + if (!entries) + break; + + /* Select the next directory entry. */ + pos = GRUB_XFS_NEXT_DIRENT (pos, direntry->len); + pos = GRUB_XFS_ROUND_TO_DIRENT (pos); + } + } + grub_free (dirblock); + break; + } + + default: + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "XFS does not support inode format %d yet", + diro->inode.format); + } + return 0; +} + + +static struct grub_xfs_data * +grub_xfs_mount (grub_disk_t disk) +{ + struct grub_xfs_data *data = 0; + + data = grub_zalloc (sizeof (struct grub_xfs_data)); + if (!data) + return 0; + + /* Read the superblock. */ + if (grub_disk_read (disk, 0, 0, + sizeof (struct grub_xfs_sblock), &data->sblock)) + goto fail; + + if (grub_strncmp ((char *) (data->sblock.magic), "XFSB", 4)) + { + grub_error (GRUB_ERR_BAD_FS, "not a XFS filesystem"); + goto fail; + } + + data = grub_realloc (data, + sizeof (struct grub_xfs_data) + - sizeof (struct grub_xfs_inode) + + (1 << data->sblock.log2_inode)); + + if (! data) + goto fail; + + data->diropen.data = data; + data->diropen.ino = data->sblock.rootino; + data->diropen.inode_read = 1; + data->bsize = grub_be_to_cpu32 (data->sblock.bsize); + data->agsize = grub_be_to_cpu32 (data->sblock.agsize); + + data->disk = disk; + data->pos = 0; + + grub_xfs_read_inode (data, data->diropen.ino, &data->diropen.inode); + + return data; + fail: + + if (grub_errno == GRUB_ERR_OUT_OF_RANGE) + grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem"); + + grub_free (data); + + return 0; +} + + +static grub_err_t +grub_xfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)) +{ + struct grub_xfs_data *data = 0; + struct grub_fshelp_node *fdiro = 0; + + auto int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node); + + int NESTED_FUNC_ATTR iterate (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node) + { + struct grub_dirhook_info info; + grub_memset (&info, 0, sizeof (info)); + info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR); + grub_free (node); + return hook (filename, &info); + } + + grub_dl_ref (my_mod); + + data = grub_xfs_mount (device->disk); + if (!data) + goto mount_fail; + + grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_xfs_iterate_dir, + grub_xfs_read_symlink, GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_xfs_iterate_dir (fdiro, iterate); + + fail: + if (fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + mount_fail: + + grub_dl_unref (my_mod); + + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_xfs_open (struct grub_file *file, const char *name) +{ + struct grub_xfs_data *data; + struct grub_fshelp_node *fdiro = 0; + + grub_dl_ref (my_mod); + + data = grub_xfs_mount (file->device->disk); + if (!data) + goto mount_fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_xfs_iterate_dir, + grub_xfs_read_symlink, GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + if (!fdiro->inode_read) + { + grub_xfs_read_inode (data, fdiro->ino, &fdiro->inode); + if (grub_errno) + goto fail; + } + + if (fdiro != &data->diropen) + grub_memcpy (&data->diropen, fdiro, + sizeof (struct grub_fshelp_node) + - sizeof (struct grub_xfs_inode) + + (1 << data->sblock.log2_inode)); + + file->size = grub_be_to_cpu64 (data->diropen.inode.size); + file->data = data; + file->offset = 0; + + return 0; + + fail: + if (fdiro != &data->diropen) + grub_free (fdiro); + grub_free (data); + + mount_fail: + grub_dl_unref (my_mod); + + return grub_errno; +} + + +static grub_ssize_t +grub_xfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_xfs_data *data = + (struct grub_xfs_data *) file->data; + + return grub_xfs_read_file (&data->diropen, file->read_hook, + file->offset, len, buf); +} + + +static grub_err_t +grub_xfs_close (grub_file_t file) +{ + grub_free (file->data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + + +static grub_err_t +grub_xfs_label (grub_device_t device, char **label) +{ + struct grub_xfs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_xfs_mount (disk); + if (data) + *label = grub_strndup ((char *) (data->sblock.label), 12); + else + *label = 0; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + +static grub_err_t +grub_xfs_uuid (grub_device_t device, char **uuid) +{ + struct grub_xfs_data *data; + grub_disk_t disk = device->disk; + + grub_dl_ref (my_mod); + + data = grub_xfs_mount (disk); + if (data) + { + *uuid = grub_xasprintf ("%04x%04x-%04x-%04x-%04x-%04x%04x%04x", + grub_be_to_cpu16 (data->sblock.uuid[0]), + grub_be_to_cpu16 (data->sblock.uuid[1]), + grub_be_to_cpu16 (data->sblock.uuid[2]), + grub_be_to_cpu16 (data->sblock.uuid[3]), + grub_be_to_cpu16 (data->sblock.uuid[4]), + grub_be_to_cpu16 (data->sblock.uuid[5]), + grub_be_to_cpu16 (data->sblock.uuid[6]), + grub_be_to_cpu16 (data->sblock.uuid[7])); + } + else + *uuid = NULL; + + grub_dl_unref (my_mod); + + grub_free (data); + + return grub_errno; +} + + + +static struct grub_fs grub_xfs_fs = + { + .name = "xfs", + .dir = grub_xfs_dir, + .open = grub_xfs_open, + .read = grub_xfs_read, + .close = grub_xfs_close, + .label = grub_xfs_label, + .uuid = grub_xfs_uuid, +#ifdef GRUB_UTIL + .reserved_first_sector = 0, +#endif + .next = 0 + }; + +GRUB_MOD_INIT(xfs) +{ + grub_fs_register (&grub_xfs_fs); + my_mod = mod; +} + +GRUB_MOD_FINI(xfs) +{ + grub_fs_unregister (&grub_xfs_fs); +} diff --git a/libr/fs/p/grub/fs/zfs/zfs.c b/libr/fs/p/grub/fs/zfs/zfs.c new file mode 100644 index 0000000000..8901af76fe --- /dev/null +++ b/libr/fs/p/grub/fs/zfs/zfs.c @@ -0,0 +1,2543 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009,2010 Free Software Foundation, Inc. + * Copyright 2010 Sun Microsystems, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * The zfs plug-in routines for GRUB are: + * + * zfs_mount() - locates a valid uberblock of the root pool and reads + * in its MOS at the memory address MOS. + * + * zfs_open() - locates a plain file object by following the MOS + * and places its dnode at the memory address DNODE. + * + * zfs_read() - read in the data blocks pointed by the DNODE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ZPOOL_PROP_BOOTFS "bootfs" + +#define MIN(a,b) (((a) < (b)) ? (a) : (b)) + +/* + * For nvlist manipulation. (from nvpair.h) + */ +#define NV_ENCODE_NATIVE 0 +#define NV_ENCODE_XDR 1 +#define NV_BIG_ENDIAN 0 +#define NV_LITTLE_ENDIAN 1 +#define DATA_TYPE_UINT64 8 +#define DATA_TYPE_STRING 9 +#define DATA_TYPE_NVLIST 19 +#define DATA_TYPE_NVLIST_ARRAY 20 + +#ifndef GRUB_UTIL +static grub_dl_t my_mod; +#endif + +#define P2PHASE(x, align) ((x) & ((align) - 1)) +#define DVA_OFFSET_TO_PHYS_SECTOR(offset) \ + ((offset + VDEV_LABEL_START_SIZE) >> SPA_MINBLOCKSHIFT) + +/* + * FAT ZAP data structures + */ +#define ZFS_CRC64_POLY 0xC96C5795D7870F42ULL /* ECMA-182, reflected form */ +#define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n)))) +#define CHAIN_END 0xffff /* end of the chunk chain */ + +/* + * The amount of space within the chunk available for the array is: + * chunk size - space for type (1) - space for next pointer (2) + */ +#define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3) + +#define ZAP_LEAF_HASH_SHIFT(bs) (bs - 5) +#define ZAP_LEAF_HASH_NUMENTRIES(bs) (1 << ZAP_LEAF_HASH_SHIFT(bs)) +#define LEAF_HASH(bs, h) \ + ((ZAP_LEAF_HASH_NUMENTRIES(bs)-1) & \ + ((h) >> (64 - ZAP_LEAF_HASH_SHIFT(bs)-l->l_hdr.lh_prefix_len))) + +/* + * The amount of space available for chunks is: + * block size shift - hash entry size (2) * number of hash + * entries - header space (2*chunksize) + */ +#define ZAP_LEAF_NUMCHUNKS(bs) \ + (((1<l_hash + ZAP_LEAF_HASH_NUMENTRIES(bs)))[idx] +#define ZAP_LEAF_ENTRY(l, bs, idx) (&ZAP_LEAF_CHUNK(l, bs, idx).l_entry) + + +/* + * Decompression Entry - lzjb + */ +#ifndef NBBY +#define NBBY 8 +#endif + +extern grub_err_t lzjb_decompress (void *, void *, grub_size_t, grub_size_t); + +typedef grub_err_t zfs_decomp_func_t (void *s_start, void *d_start, + grub_size_t s_len, grub_size_t d_len); +typedef struct decomp_entry +{ + char *name; + zfs_decomp_func_t *decomp_func; +} decomp_entry_t; + +typedef struct dnode_end +{ + dnode_phys_t dn; + grub_zfs_endian_t endian; +} dnode_end_t; + +struct grub_zfs_data +{ + /* cache for a file block of the currently zfs_open()-ed file */ + char *file_buf; + grub_uint64_t file_start; + grub_uint64_t file_end; + + /* cache for a dnode block */ + dnode_phys_t *dnode_buf; + dnode_phys_t *dnode_mdn; + grub_uint64_t dnode_start; + grub_uint64_t dnode_end; + grub_zfs_endian_t dnode_endian; + + uberblock_t current_uberblock; + grub_disk_t disk; + + dnode_end_t mos; + dnode_end_t mdn; + dnode_end_t dnode; + + grub_disk_addr_t vdev_phys_sector; +}; + +decomp_entry_t decomp_table[ZIO_COMPRESS_FUNCTIONS] = { + {"inherit", NULL}, /* ZIO_COMPRESS_INHERIT */ + {"on", lzjb_decompress}, /* ZIO_COMPRESS_ON */ + {"off", NULL}, /* ZIO_COMPRESS_OFF */ + {"lzjb", lzjb_decompress}, /* ZIO_COMPRESS_LZJB */ + {"empty", NULL}, /* ZIO_COMPRESS_EMPTY */ + {"gzip", NULL}, /* ZIO_COMPRESS_GZIP */ +}; + +static grub_err_t zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian, + void *buf, struct grub_zfs_data *data); + +/* + * Our own version of log2(). Same thing as highbit()-1. + */ +static int +zfs_log2 (grub_uint64_t num) +{ + int i = 0; + + while (num > 1) + { + i++; + num = num >> 1; + } + + return (i); +} + +/* Checksum Functions */ +static void +zio_checksum_off (const void *buf __attribute__ ((unused)), + grub_uint64_t size __attribute__ ((unused)), + grub_zfs_endian_t endian __attribute__ ((unused)), + zio_cksum_t * zcp) +{ + ZIO_SET_CHECKSUM (zcp, 0, 0, 0, 0); +} + +/* Checksum Table and Values */ +zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = { + {NULL, 0, 0, "inherit"}, + {NULL, 0, 0, "on"}, + {zio_checksum_off, 0, 0, "off"}, + {zio_checksum_SHA256, 1, 1, "label"}, + {zio_checksum_SHA256, 1, 1, "gang_header"}, + {NULL, 0, 0, "zilog"}, + {fletcher_2, 0, 0, "fletcher2"}, + {fletcher_4, 1, 0, "fletcher4"}, + {zio_checksum_SHA256, 1, 0, "SHA256"}, + {NULL, 0, 0, "zilog2"}, +}; + +/* + * zio_checksum_verify: Provides support for checksum verification. + * + * Fletcher2, Fletcher4, and SHA256 are supported. + * + */ +static grub_err_t +zio_checksum_verify (zio_cksum_t zc, grub_uint32_t checksum, + grub_zfs_endian_t endian, char *buf, int size) +{ + zio_eck_t *zec = (zio_eck_t *) (buf + size) - 1; + zio_checksum_info_t *ci = &zio_checksum_table[checksum]; + zio_cksum_t actual_cksum, expected_cksum; + + if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func == NULL) + { + grub_dprintf ("zfs", "unknown checksum function %d\n", checksum); + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "unknown checksum function %d", checksum); + } + + if (ci->ci_eck) + { + expected_cksum = zec->zec_cksum; + zec->zec_cksum = zc; + ci->ci_func (buf, size, endian, &actual_cksum); + zec->zec_cksum = expected_cksum; + zc = expected_cksum; + } + else + ci->ci_func (buf, size, endian, &actual_cksum); + + if ((actual_cksum.zc_word[0] != zc.zc_word[0]) + || (actual_cksum.zc_word[1] != zc.zc_word[1]) + || (actual_cksum.zc_word[2] != zc.zc_word[2]) + || (actual_cksum.zc_word[3] != zc.zc_word[3])) + { + grub_dprintf ("zfs", "checksum %d verification failed\n", checksum); + grub_dprintf ("zfs", "actual checksum %16llx %16llx %16llx %16llx\n", + (unsigned long long) actual_cksum.zc_word[0], + (unsigned long long) actual_cksum.zc_word[1], + (unsigned long long) actual_cksum.zc_word[2], + (unsigned long long) actual_cksum.zc_word[3]); + grub_dprintf ("zfs", "expected checksum %16llx %16llx %16llx %16llx\n", + (unsigned long long) zc.zc_word[0], + (unsigned long long) zc.zc_word[1], + (unsigned long long) zc.zc_word[2], + (unsigned long long) zc.zc_word[3]); + return grub_error (GRUB_ERR_BAD_FS, "checksum verification failed"); + } + + return GRUB_ERR_NONE; +} + +/* + * vdev_uberblock_compare takes two uberblock structures and returns an integer + * indicating the more recent of the two. + * Return Value = 1 if ub2 is more recent + * Return Value = -1 if ub1 is more recent + * The most recent uberblock is determined using its transaction number and + * timestamp. The uberblock with the highest transaction number is + * considered "newer". If the transaction numbers of the two blocks match, the + * timestamps are compared to determine the "newer" of the two. + */ +static int +vdev_uberblock_compare (uberblock_t * ub1, uberblock_t * ub2) +{ + grub_zfs_endian_t ub1_endian, ub2_endian; + if (grub_zfs_to_cpu64 (ub1->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC) + ub1_endian = LITTLE_ENDIAN; + else + ub1_endian = BIG_ENDIAN; + if (grub_zfs_to_cpu64 (ub2->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC) + ub2_endian = LITTLE_ENDIAN; + else + ub2_endian = BIG_ENDIAN; + + if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian) + < grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian)) + return (-1); + if (grub_zfs_to_cpu64 (ub1->ub_txg, ub1_endian) + > grub_zfs_to_cpu64 (ub2->ub_txg, ub2_endian)) + return (1); + + if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian) + < grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian)) + return (-1); + if (grub_zfs_to_cpu64 (ub1->ub_timestamp, ub1_endian) + > grub_zfs_to_cpu64 (ub2->ub_timestamp, ub2_endian)) + return (1); + + return (0); +} + +/* + * Three pieces of information are needed to verify an uberblock: the magic + * number, the version number, and the checksum. + * + * Currently Implemented: version number, magic number + * Need to Implement: checksum + * + */ +static grub_err_t +uberblock_verify (uberblock_phys_t * ub, int offset) +{ + uberblock_t *uber = &ub->ubp_uberblock; + grub_err_t err; + grub_zfs_endian_t endian = UNKNOWN_ENDIAN; + zio_cksum_t zc; + + if (grub_zfs_to_cpu64 (uber->ub_magic, LITTLE_ENDIAN) == UBERBLOCK_MAGIC + && grub_zfs_to_cpu64 (uber->ub_version, LITTLE_ENDIAN) > 0 + && grub_zfs_to_cpu64 (uber->ub_version, LITTLE_ENDIAN) <= SPA_VERSION) + endian = LITTLE_ENDIAN; + + if (grub_zfs_to_cpu64 (uber->ub_magic, BIG_ENDIAN) == UBERBLOCK_MAGIC + && grub_zfs_to_cpu64 (uber->ub_version, BIG_ENDIAN) > 0 + && grub_zfs_to_cpu64 (uber->ub_version, BIG_ENDIAN) <= SPA_VERSION) + endian = BIG_ENDIAN; + + if (endian == UNKNOWN_ENDIAN) + return grub_error (GRUB_ERR_BAD_FS, "invalid uberblock magic"); + + grub_memset (&zc, 0, sizeof (zc)); + + zc.zc_word[0] = grub_cpu_to_zfs64 (offset, endian); + err = zio_checksum_verify (zc, ZIO_CHECKSUM_LABEL, endian, + (char *) ub, UBERBLOCK_SIZE); + + return err; +} + +/* + * Find the best uberblock. + * Return: + * Success - Pointer to the best uberblock. + * Failure - NULL + */ +static uberblock_phys_t * +find_bestub (uberblock_phys_t * ub_array, grub_disk_addr_t sector) +{ + uberblock_phys_t *ubbest = NULL; + int i; + grub_disk_addr_t offset; + grub_err_t err = GRUB_ERR_NONE; + + for (i = 0; i < (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT); i++) + { + offset = (sector << SPA_MINBLOCKSHIFT) + VDEV_PHYS_SIZE + + (i << VDEV_UBERBLOCK_SHIFT); + + err = uberblock_verify (&ub_array[i], offset); + if (err) + { + grub_errno = GRUB_ERR_NONE; + continue; + } + if (ubbest == NULL + || vdev_uberblock_compare (&(ub_array[i].ubp_uberblock), + &(ubbest->ubp_uberblock)) > 0) + ubbest = &ub_array[i]; + } + if (!ubbest) + grub_errno = err; + + return (ubbest); +} + +static inline grub_size_t +get_psize (blkptr_t * bp, grub_zfs_endian_t endian) +{ + return ((((grub_zfs_to_cpu64 ((bp)->blk_prop, endian) >> 16) & 0xffff) + 1) + << SPA_MINBLOCKSHIFT); +} + +static grub_uint64_t +dva_get_offset (dva_t * dva, grub_zfs_endian_t endian) +{ + grub_dprintf ("zfs", "dva=%llx, %llx\n", + (unsigned long long) dva->dva_word[0], + (unsigned long long) dva->dva_word[1]); + return grub_zfs_to_cpu64 ((dva)->dva_word[1], + endian) << SPA_MINBLOCKSHIFT; +} + + +/* + * Read a block of data based on the gang block address dva, + * and put its data in buf. + * + */ +static grub_err_t +zio_read_gang (blkptr_t * bp, grub_zfs_endian_t endian, dva_t * dva, void *buf, + struct grub_zfs_data *data) +{ + zio_gbh_phys_t *zio_gb; + grub_uint64_t offset, sector; + unsigned i; + grub_err_t err; + zio_cksum_t zc; + + grub_memset (&zc, 0, sizeof (zc)); + + zio_gb = grub_malloc (SPA_GANGBLOCKSIZE); + if (!zio_gb) + return grub_errno; + grub_dprintf ("zfs", endian == LITTLE_ENDIAN ? "little-endian gang\n" + :"big-endian gang\n"); + offset = dva_get_offset (dva, endian); + sector = DVA_OFFSET_TO_PHYS_SECTOR (offset); + grub_dprintf ("zfs", "offset=%llx\n", (unsigned long long) offset); + + /* read in the gang block header */ + err = grub_disk_read (data->disk, sector, 0, SPA_GANGBLOCKSIZE, + (char *) zio_gb); + if (err) + { + grub_free (zio_gb); + return err; + } + + /* XXX */ + /* self checksuming the gang block header */ + ZIO_SET_CHECKSUM (&zc, DVA_GET_VDEV (dva), + dva_get_offset (dva, endian), bp->blk_birth, 0); + err = zio_checksum_verify (zc, ZIO_CHECKSUM_GANG_HEADER, endian, + (char *) zio_gb, SPA_GANGBLOCKSIZE); + if (err) + { + grub_free (zio_gb); + return err; + } + + endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1; + + for (i = 0; i < SPA_GBH_NBLKPTRS; i++) + { + if (zio_gb->zg_blkptr[i].blk_birth == 0) + continue; + + err = zio_read_data (&zio_gb->zg_blkptr[i], endian, buf, data); + if (err) + { + grub_free (zio_gb); + return err; + } + buf = (char *) buf + get_psize (&zio_gb->zg_blkptr[i], endian); + } + grub_free (zio_gb); + return GRUB_ERR_NONE; +} + +/* + * Read in a block of raw data to buf. + */ +static grub_err_t +zio_read_data (blkptr_t * bp, grub_zfs_endian_t endian, void *buf, + struct grub_zfs_data *data) +{ + int i, psize; + grub_err_t err = GRUB_ERR_NONE; + + psize = get_psize (bp, endian); + + /* pick a good dva from the block pointer */ + for (i = 0; i < SPA_DVAS_PER_BP; i++) + { + grub_uint64_t offset, sector; + + if (bp->blk_dva[i].dva_word[0] == 0 && bp->blk_dva[i].dva_word[1] == 0) + continue; + + if ((grub_zfs_to_cpu64 (bp->blk_dva[i].dva_word[1], endian)>>63) & 1) + err = zio_read_gang (bp, endian, &bp->blk_dva[i], buf, data); + else + { + /* read in a data block */ + offset = dva_get_offset (&bp->blk_dva[i], endian); + sector = DVA_OFFSET_TO_PHYS_SECTOR (offset); + err = grub_disk_read (data->disk, sector, 0, psize, buf); + } + if (!err) + return GRUB_ERR_NONE; + grub_errno = GRUB_ERR_NONE; + } + + if (!err) + err = grub_error (GRUB_ERR_BAD_FS, "couldn't find a valid DVA"); + grub_errno = err; + + return err; +} + +/* + * Read in a block of data, verify its checksum, decompress if needed, + * and put the uncompressed data in buf. + */ +static grub_err_t +zio_read (blkptr_t * bp, grub_zfs_endian_t endian, void **buf, + grub_size_t *size, struct grub_zfs_data *data) +{ + grub_size_t lsize, psize; + unsigned int comp; + char *compbuf = NULL; + grub_err_t err; + zio_cksum_t zc = bp->blk_cksum; + grub_uint32_t checksum; + + *buf = NULL; + + checksum = (grub_zfs_to_cpu64((bp)->blk_prop, endian) >> 40) & 0xff; + comp = (grub_zfs_to_cpu64((bp)->blk_prop, endian)>>32) & 0x7; + lsize = (BP_IS_HOLE(bp) ? 0 : + (((grub_zfs_to_cpu64 ((bp)->blk_prop, endian) & 0xffff) + 1) + << SPA_MINBLOCKSHIFT)); + psize = get_psize (bp, endian); + + if (size) + *size = lsize; + + if (comp >= ZIO_COMPRESS_FUNCTIONS) + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "compression algorithm %u not supported\n", (unsigned int) comp); + + if (comp != ZIO_COMPRESS_OFF && decomp_table[comp].decomp_func == NULL) + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "compression algorithm %s not supported\n", decomp_table[comp].name); + + if (comp != ZIO_COMPRESS_OFF) + { + compbuf = grub_malloc (psize); + if (! compbuf) + return grub_errno; + } + else + compbuf = *buf = grub_malloc (lsize); + + grub_dprintf ("zfs", "endian = %d\n", endian); + err = zio_read_data (bp, endian, compbuf, data); + if (err) + { + grub_free (compbuf); + *buf = NULL; + return err; + } + + err = zio_checksum_verify (zc, checksum, endian, compbuf, psize); + if (err) + { + grub_dprintf ("zfs", "incorrect checksum\n"); + grub_free (compbuf); + *buf = NULL; + return err; + } + + if (comp != ZIO_COMPRESS_OFF) + { + *buf = grub_malloc (lsize); + if (!*buf) + { + grub_free (compbuf); + return grub_errno; + } + + err = decomp_table[comp].decomp_func (compbuf, *buf, psize, lsize); + grub_free (compbuf); + if (err) + { + grub_free (*buf); + *buf = NULL; + return err; + } + } + + return GRUB_ERR_NONE; +} + +/* + * Get the block from a block id. + * push the block onto the stack. + * + */ +static grub_err_t +dmu_read (dnode_end_t * dn, grub_uint64_t blkid, void **buf, + grub_zfs_endian_t *endian_out, struct grub_zfs_data *data) +{ + int idx, level; + blkptr_t *bp_array = dn->dn.dn_blkptr; + int epbs = dn->dn.dn_indblkshift - SPA_BLKPTRSHIFT; + blkptr_t *bp, *tmpbuf = 0; + grub_zfs_endian_t endian; + grub_err_t err = GRUB_ERR_NONE; + + bp = grub_malloc (sizeof (blkptr_t)); + if (!bp) + return grub_errno; + + endian = dn->endian; + for (level = dn->dn.dn_nlevels - 1; level >= 0; level--) + { + grub_dprintf ("zfs", "endian = %d\n", endian); + idx = (blkid >> (epbs * level)) & ((1 << epbs) - 1); + *bp = bp_array[idx]; + if (bp_array != dn->dn.dn_blkptr) + { + grub_free (bp_array); + bp_array = 0; + } + + if (BP_IS_HOLE (bp)) + { + grub_size_t size = grub_zfs_to_cpu16 (dn->dn.dn_datablkszsec, + dn->endian) + << SPA_MINBLOCKSHIFT; + *buf = grub_malloc (size); + if (*buf) + { + err = grub_errno; + break; + } + grub_memset (*buf, 0, size); + endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1; + break; + } + if (level == 0) + { + grub_dprintf ("zfs", "endian = %d\n", endian); + err = zio_read (bp, endian, buf, 0, data); + endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1; + break; + } + grub_dprintf ("zfs", "endian = %d\n", endian); + err = zio_read (bp, endian, (void **) &tmpbuf, 0, data); + endian = (grub_zfs_to_cpu64 (bp->blk_prop, endian) >> 63) & 1; + if (err) + break; + bp_array = tmpbuf; + } + if (bp_array != dn->dn.dn_blkptr) + grub_free (bp_array); + if (endian_out) + *endian_out = endian; + + grub_free (bp); + return err; +} + +/* + * mzap_lookup: Looks up property described by "name" and returns the value + * in "value". + */ +static grub_err_t +mzap_lookup (mzap_phys_t * zapobj, grub_zfs_endian_t endian, + int objsize, char *name, grub_uint64_t * value) +{ + int i, chunks; + mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk; + + chunks = objsize / MZAP_ENT_LEN - 1; + for (i = 0; i < chunks; i++) + { + if (grub_strcmp (mzap_ent[i].mze_name, name) == 0) + { + *value = grub_zfs_to_cpu64 (mzap_ent[i].mze_value, endian); + return GRUB_ERR_NONE; + } + } + + return grub_error (GRUB_ERR_FILE_NOT_FOUND, "couldn't find %s", name); +} + +static int +mzap_iterate (mzap_phys_t * zapobj, grub_zfs_endian_t endian, int objsize, + int NESTED_FUNC_ATTR (*hook) (const char *name, + grub_uint64_t val)) +{ + int i, chunks; + mzap_ent_phys_t *mzap_ent = zapobj->mz_chunk; + + chunks = objsize / MZAP_ENT_LEN - 1; + for (i = 0; i < chunks; i++) + { + grub_dprintf ("zfs", "zap: name = %s, value = %llx, cd = %x\n", + mzap_ent[i].mze_name, (long long)mzap_ent[i].mze_value, + (int)mzap_ent[i].mze_cd); + if (hook (mzap_ent[i].mze_name, + grub_zfs_to_cpu64 (mzap_ent[i].mze_value, endian))) + return 1; + } + + return 0; +} + +static grub_uint64_t +zap_hash (grub_uint64_t salt, const char *name) +{ + static grub_uint64_t table[256]; + const grub_uint8_t *cp; + grub_uint8_t c; + grub_uint64_t crc = salt; + + if (table[128] == 0) + { + grub_uint64_t *ct; + int i, j; + for (i = 0; i < 256; i++) + { + for (ct = table + i, *ct = i, j = 8; j > 0; j--) + *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); + } + } + + for (cp = (const grub_uint8_t *) name; (c = *cp) != '\0'; cp++) + crc = (crc >> 8) ^ table[(crc ^ c) & 0xFF]; + + /* + * Only use 28 bits, since we need 4 bits in the cookie for the + * collision differentiator. We MUST use the high bits, since + * those are the onces that we first pay attention to when + * chosing the bucket. + */ + crc &= ~((1ULL << (64 - ZAP_HASHBITS)) - 1); + + return (crc); +} + +/* + * Only to be used on 8-bit arrays. + * array_len is actual len in bytes (not encoded le_value_length). + * buf is null-terminated. + */ +/* XXX */ +static int +zap_leaf_array_equal (zap_leaf_phys_t * l, grub_zfs_endian_t endian, + int blksft, int chunk, int array_len, const char *buf) +{ + int bseen = 0; + + while (bseen < array_len) + { + struct zap_leaf_array *la = &ZAP_LEAF_CHUNK (l, blksft, chunk).l_array; + int toread = MIN (array_len - bseen, ZAP_LEAF_ARRAY_BYTES); + + if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft)) + return (0); + + if (grub_memcmp (la->la_array, buf + bseen, toread) != 0) + break; + chunk = grub_zfs_to_cpu16 (la->la_next, endian); + bseen += toread; + } + return (bseen == array_len); +} + +/* XXX */ +static grub_err_t +zap_leaf_array_get (zap_leaf_phys_t * l, grub_zfs_endian_t endian, int blksft, + int chunk, int array_len, char *buf) +{ + int bseen = 0; + + while (bseen < array_len) + { + struct zap_leaf_array *la = &ZAP_LEAF_CHUNK (l, blksft, chunk).l_array; + int toread = MIN (array_len - bseen, ZAP_LEAF_ARRAY_BYTES); + + if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft)) + /* Don't use grub_error because this error is to be ignored. */ + return GRUB_ERR_BAD_FS; + + grub_memcpy (buf + bseen,la->la_array, toread); + chunk = grub_zfs_to_cpu16 (la->la_next, endian); + bseen += toread; + } + return GRUB_ERR_NONE; +} + + +/* + * Given a zap_leaf_phys_t, walk thru the zap leaf chunks to get the + * value for the property "name". + * + */ +/* XXX */ +static grub_err_t +zap_leaf_lookup (zap_leaf_phys_t * l, grub_zfs_endian_t endian, + int blksft, grub_uint64_t h, + const char *name, grub_uint64_t * value) +{ + grub_uint16_t chunk; + struct zap_leaf_entry *le; + + /* Verify if this is a valid leaf block */ + if (grub_zfs_to_cpu64 (l->l_hdr.lh_block_type, endian) != ZBT_LEAF) + return grub_error (GRUB_ERR_BAD_FS, "invalid leaf type"); + if (grub_zfs_to_cpu32 (l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) + return grub_error (GRUB_ERR_BAD_FS, "invalid leaf magic"); + + for (chunk = grub_zfs_to_cpu16 (l->l_hash[LEAF_HASH (blksft, h)], endian); + chunk != CHAIN_END; chunk = le->le_next) + { + + if (chunk >= ZAP_LEAF_NUMCHUNKS (blksft)) + return grub_error (GRUB_ERR_BAD_FS, "invalid chunk number"); + + le = ZAP_LEAF_ENTRY (l, blksft, chunk); + + /* Verify the chunk entry */ + if (le->le_type != ZAP_CHUNK_ENTRY) + return grub_error (GRUB_ERR_BAD_FS, "invalid chunk entry"); + + if (grub_zfs_to_cpu64 (le->le_hash,endian) != h) + continue; + + grub_dprintf ("zfs", "fzap: length %d\n", (int) le->le_name_length); + + if (zap_leaf_array_equal (l, endian, blksft, + grub_zfs_to_cpu16 (le->le_name_chunk,endian), + grub_zfs_to_cpu16 (le->le_name_length, endian), + name)) + { + struct zap_leaf_array *la; + grub_uint8_t *ip; + + if (le->le_int_size != 8 || le->le_value_length != 1) + return grub_error (GRUB_ERR_BAD_FS, "invalid leaf chunk entry"); + + /* get the uint64_t property value */ + la = &ZAP_LEAF_CHUNK (l, blksft, le->le_value_chunk).l_array; + ip = la->la_array; + + *value = grub_be_to_cpu64 (la->la_array64); + + return GRUB_ERR_NONE; + } + } + + return grub_error (GRUB_ERR_FILE_NOT_FOUND, "couldn't find %s", name); +} + + +/* Verify if this is a fat zap header block */ +static grub_err_t +zap_verify (zap_phys_t *zap) +{ + if (zap->zap_magic != (grub_uint64_t) ZAP_MAGIC) + return grub_error (GRUB_ERR_BAD_FS, "bad ZAP magic"); + + if (zap->zap_flags != 0) + return grub_error (GRUB_ERR_BAD_FS, "bad ZAP flags"); + + if (zap->zap_salt == 0) + return grub_error (GRUB_ERR_BAD_FS, "bad ZAP salt"); + + return GRUB_ERR_NONE; +} + +/* + * Fat ZAP lookup + * + */ +/* XXX */ +static grub_err_t +fzap_lookup (dnode_end_t * zap_dnode, zap_phys_t * zap, + char *name, grub_uint64_t * value, struct grub_zfs_data *data) +{ + zap_leaf_phys_t *l; + grub_uint64_t hash, idx, blkid; + int blksft = zfs_log2 (grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec, + zap_dnode->endian) << DNODE_SHIFT); + grub_err_t err; + grub_zfs_endian_t leafendian; + + err = zap_verify (zap); + if (err) + return err; + + hash = zap_hash (zap->zap_salt, name); + + /* get block id from index */ + if (zap->zap_ptrtbl.zt_numblks != 0) + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "external pointer tables not supported"); + idx = ZAP_HASH_IDX (hash, zap->zap_ptrtbl.zt_shift); + blkid = ((grub_uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))]; + + /* Get the leaf block */ + if ((1U << blksft) < sizeof (zap_leaf_phys_t)) + return grub_error (GRUB_ERR_BAD_FS, "ZAP leaf is too small"); + err = dmu_read (zap_dnode, blkid, (void **) &l, &leafendian, data); + if (err) + return err; + + err = zap_leaf_lookup (l, leafendian, blksft, hash, name, value); + grub_free (l); + return err; +} + +/* XXX */ +static int +fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap, + int NESTED_FUNC_ATTR (*hook) (const char *name, + grub_uint64_t val), + struct grub_zfs_data *data) +{ + zap_leaf_phys_t *l; + grub_uint64_t idx, blkid; + grub_uint16_t chunk; + int blksft = zfs_log2 (grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec, + zap_dnode->endian) << DNODE_SHIFT); + grub_err_t err; + grub_zfs_endian_t endian; + + if (zap_verify (zap)) + return 0; + + /* get block id from index */ + if (zap->zap_ptrtbl.zt_numblks != 0) + { + grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "external pointer tables not supported"); + return 0; + } + /* Get the leaf block */ + if ((1U << blksft) < sizeof (zap_leaf_phys_t)) + { + grub_error (GRUB_ERR_BAD_FS, "ZAP leaf is too small"); + return 0; + } + for (idx = 0; idx < zap->zap_ptrtbl.zt_numblks; idx++) + { + blkid = ((grub_uint64_t *) zap)[idx + (1 << (blksft - 3 - 1))]; + + err = dmu_read (zap_dnode, blkid, (void **) &l, &endian, data); + if (err) + { + grub_errno = GRUB_ERR_NONE; + continue; + } + + /* Verify if this is a valid leaf block */ + if (grub_zfs_to_cpu64 (l->l_hdr.lh_block_type, endian) != ZBT_LEAF) + { + grub_free (l); + continue; + } + if (grub_zfs_to_cpu32 (l->l_hdr.lh_magic, endian) != ZAP_LEAF_MAGIC) + { + grub_free (l); + continue; + } + + for (chunk = 0; chunk < ZAP_LEAF_NUMCHUNKS (blksft); chunk++) + { + char *buf; + struct zap_leaf_array *la; + struct zap_leaf_entry *le; + grub_uint64_t val; + le = ZAP_LEAF_ENTRY (l, blksft, chunk); + + /* Verify the chunk entry */ + if (le->le_type != ZAP_CHUNK_ENTRY) + continue; + + buf = grub_malloc (grub_zfs_to_cpu16 (le->le_name_length, endian) + + 1); + if (zap_leaf_array_get (l, endian, blksft, le->le_name_chunk, + le->le_name_length, buf)) + { + grub_free (buf); + continue; + } + buf[le->le_name_length] = 0; + + if (le->le_int_size != 8 + || grub_zfs_to_cpu16 (le->le_value_length, endian) != 1) + continue; + + /* get the uint64_t property value */ + la = &ZAP_LEAF_CHUNK (l, blksft, le->le_value_chunk).l_array; + val = grub_be_to_cpu64 (la->la_array64); + if (hook (buf, val)) + return 1; + grub_free (buf); + } + } + return 0; +} + + +/* + * Read in the data of a zap object and find the value for a matching + * property name. + * + */ +static grub_err_t +zap_lookup (dnode_end_t * zap_dnode, char *name, grub_uint64_t * val, + struct grub_zfs_data *data) +{ + grub_uint64_t block_type; + int size; + void *zapbuf; + grub_err_t err; + grub_zfs_endian_t endian; + + grub_dprintf ("zfs", "looking for '%s'\n", name); + + /* Read in the first block of the zap object data. */ + size = grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec, + zap_dnode->endian) << SPA_MINBLOCKSHIFT; + err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data); + if (err) + return err; + block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian); + + grub_dprintf ("zfs", "zap read\n"); + + if (block_type == ZBT_MICRO) + { + grub_dprintf ("zfs", "micro zap\n"); + err = (mzap_lookup (zapbuf, endian, size, name, val)); + grub_dprintf ("zfs", "returned %d\n", err); + grub_free (zapbuf); + return err; + } + else if (block_type == ZBT_HEADER) + { + grub_dprintf ("zfs", "fat zap\n"); + /* this is a fat zap */ + err = (fzap_lookup (zap_dnode, zapbuf, name, val, data)); + grub_dprintf ("zfs", "returned %d\n", err); + grub_free (zapbuf); + return err; + } + + return grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type"); +} + +static int +zap_iterate (dnode_end_t * zap_dnode, + int NESTED_FUNC_ATTR (*hook) (const char *name, grub_uint64_t val), + struct grub_zfs_data *data) +{ + grub_uint64_t block_type; + int size; + void *zapbuf; + grub_err_t err; + int ret; + grub_zfs_endian_t endian; + + /* Read in the first block of the zap object data. */ + size = grub_zfs_to_cpu16 (zap_dnode->dn.dn_datablkszsec, zap_dnode->endian) << SPA_MINBLOCKSHIFT; + err = dmu_read (zap_dnode, 0, &zapbuf, &endian, data); + if (err) + return 0; + block_type = grub_zfs_to_cpu64 (*((grub_uint64_t *) zapbuf), endian); + + grub_dprintf ("zfs", "zap read\n"); + + if (block_type == ZBT_MICRO) + { + grub_dprintf ("zfs", "micro zap\n"); + ret = mzap_iterate (zapbuf, endian, size, hook); + grub_free (zapbuf); + return ret; + } + else if (block_type == ZBT_HEADER) + { + grub_dprintf ("zfs", "fat zap\n"); + /* this is a fat zap */ + ret = fzap_iterate (zap_dnode, zapbuf, hook, data); + grub_free (zapbuf); + return ret; + } + grub_error (GRUB_ERR_BAD_FS, "unknown ZAP type"); + return 0; +} + + +/* + * Get the dnode of an object number from the metadnode of an object set. + * + * Input + * mdn - metadnode to get the object dnode + * objnum - object number for the object dnode + * buf - data buffer that holds the returning dnode + */ +static grub_err_t +dnode_get (dnode_end_t * mdn, grub_uint64_t objnum, grub_uint8_t type, + dnode_end_t * buf, struct grub_zfs_data *data) +{ + grub_uint64_t blkid, blksz; /* the block id this object dnode is in */ + int epbs; /* shift of number of dnodes in a block */ + int idx; /* index within a block */ + dnode_phys_t *dnbuf; + grub_err_t err; + grub_zfs_endian_t endian; + + blksz = grub_zfs_to_cpu16 (mdn->dn.dn_datablkszsec, + mdn->endian) << SPA_MINBLOCKSHIFT; + epbs = zfs_log2 (blksz) - DNODE_SHIFT; + blkid = objnum >> epbs; + idx = objnum & ((1 << epbs) - 1); + + if (data->dnode_buf != NULL && grub_memcmp (data->dnode_mdn, mdn, + sizeof (*mdn)) == 0 + && objnum >= data->dnode_start && objnum < data->dnode_end) + { + grub_memmove (&(buf->dn), &(data->dnode_buf)[idx], DNODE_SIZE); + buf->endian = data->dnode_endian; + if (type && buf->dn.dn_type != type) + return grub_error(GRUB_ERR_BAD_FS, "incorrect dnode type"); + return GRUB_ERR_NONE; + } + + grub_dprintf ("zfs", "endian = %d, blkid=%llx\n", mdn->endian, + (unsigned long long) blkid); + err = dmu_read (mdn, blkid, (void **) &dnbuf, &endian, data); + if (err) + return err; + grub_dprintf ("zfs", "alive\n"); + + grub_free (data->dnode_buf); + grub_free (data->dnode_mdn); + data->dnode_mdn = grub_malloc (sizeof (*mdn)); + if (! data->dnode_mdn) + { + grub_errno = GRUB_ERR_NONE; + data->dnode_buf = 0; + } + else + { + grub_memcpy (data->dnode_mdn, mdn, sizeof (*mdn)); + data->dnode_buf = dnbuf; + data->dnode_start = blkid << epbs; + data->dnode_end = (blkid + 1) << epbs; + data->dnode_endian = endian; + } + + grub_memmove (&(buf->dn), &dnbuf[idx], DNODE_SIZE); + buf->endian = endian; + if (type && buf->dn.dn_type != type) + return grub_error(GRUB_ERR_BAD_FS, "incorrect dnode type"); + + return GRUB_ERR_NONE; +} + +/* + * Get the file dnode for a given file name where mdn is the meta dnode + * for this ZFS object set. When found, place the file dnode in dn. + * The 'path' argument will be mangled. + * + */ +static grub_err_t +dnode_get_path (dnode_end_t * mdn, const char *path_in, dnode_end_t * dn, + struct grub_zfs_data *data) +{ + grub_uint64_t objnum, version; + char *cname, ch; + grub_err_t err = GRUB_ERR_NONE; + char *path, *path_buf; + struct dnode_chain + { + struct dnode_chain *next; + dnode_end_t dn; + }; + struct dnode_chain *dnode_path = 0, *dn_new, *root; + + dn_new = grub_malloc (sizeof (*dn_new)); + if (! dn_new) + return grub_errno; + dn_new->next = 0; + dnode_path = root = dn_new; + + err = dnode_get (mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE, + &(dnode_path->dn), data); + if (err) + { + grub_free (dn_new); + return err; + } + + err = zap_lookup (&(dnode_path->dn), ZPL_VERSION_STR, &version, data); + if (err) + { + grub_free (dn_new); + return err; + } + if (version > ZPL_VERSION) + { + grub_free (dn_new); + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "too new ZPL version"); + } + + err = zap_lookup (&(dnode_path->dn), ZFS_ROOT_OBJ, &objnum, data); + if (err) + { + grub_free (dn_new); + return err; + } + + err = dnode_get (mdn, objnum, 0, &(dnode_path->dn), data); + if (err) + { + grub_free (dn_new); + return err; + } + + path = path_buf = grub_strdup (path_in); + if (!path_buf) + { + grub_free (dn_new); + return grub_errno; + } + + while (1) + { + /* skip leading slashes */ + while (*path == '/') + path++; + if (!*path) + break; + /* get the next component name */ + cname = path; + while (*path && *path != '/') + path++; + /* Skip dot. */ + if (cname + 1 == path && cname[0] == '.') + continue; + /* Handle double dot. */ + if (cname + 2 == path && cname[0] == '.' && cname[1] == '.') + { + if (dn_new->next) + { + dn_new = dnode_path; + dnode_path = dn_new->next; + grub_free (dn_new); + } + else + { + err = grub_error (GRUB_ERR_FILE_NOT_FOUND, + "can't resolve .."); + break; + } + continue; + } + + ch = *path; + *path = 0; /* ensure null termination */ + + if (dnode_path->dn.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) + { + grub_free (path_buf); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + } + err = zap_lookup (&(dnode_path->dn), cname, &objnum, data); + if (err) + break; + + dn_new = grub_malloc (sizeof (*dn_new)); + if (! dn_new) + { + err = grub_errno; + break; + } + dn_new->next = dnode_path; + dnode_path = dn_new; + + objnum = ZFS_DIRENT_OBJ (objnum); + err = dnode_get (mdn, objnum, 0, &(dnode_path->dn), data); + if (err) + break; + + *path = ch; +#if 0 + if (((grub_zfs_to_cpu64(((znode_phys_t *) DN_BONUS (&dnode_path->dn.dn))->zp_mode, dnode_path->dn.endian) >> 12) & 0xf) == 0xa && ch) + { + char *oldpath = path, *oldpathbuf = path_buf; + path = path_buf + = grub_malloc (sizeof (dnode_path->dn.dn.dn_bonus) + - sizeof (znode_phys_t) + grub_strlen (oldpath) + 1); + if (!path_buf) + { + grub_free (oldpathbuf); + return grub_errno; + } + grub_memcpy (path, + (char *) DN_BONUS(&dnode_path->dn.dn) + sizeof (znode_phys_t), + sizeof (dnode_path->dn.dn.dn_bonus) - sizeof (znode_phys_t)); + path [sizeof (dnode_path->dn.dn.dn_bonus) - sizeof (znode_phys_t)] = 0; + grub_memcpy (path + grub_strlen (path), oldpath, + grub_strlen (oldpath) + 1); + + grub_free (oldpathbuf); + if (path[0] != '/') + { + dn_new = dnode_path; + dnode_path = dn_new->next; + grub_free (dn_new); + } + else while (dnode_path != root) + { + dn_new = dnode_path; + dnode_path = dn_new->next; + grub_free (dn_new); + } + } +#endif + } + + if (!err) + grub_memcpy (dn, &(dnode_path->dn), sizeof (*dn)); + + while (dnode_path) + { + dn_new = dnode_path->next; + grub_free (dnode_path); + dnode_path = dn_new; + } + grub_free (path_buf); + return err; +} + +#if 0 +/* + * Get the default 'bootfs' property value from the rootpool. + * + */ +static grub_err_t +get_default_bootfsobj (dnode_phys_t * mosmdn, grub_uint64_t * obj, + struct grub_zfs_data *data) +{ + grub_uint64_t objnum = 0; + dnode_phys_t *dn; + if (!dn) + return grub_errno; + + if ((grub_errno = dnode_get (mosmdn, DMU_POOL_DIRECTORY_OBJECT, + DMU_OT_OBJECT_DIRECTORY, dn, data))) + { + grub_free (dn); + return (grub_errno); + } + + /* + * find the object number for 'pool_props', and get the dnode + * of the 'pool_props'. + */ + if (zap_lookup (dn, DMU_POOL_PROPS, &objnum, data)) + { + grub_free (dn); + return (GRUB_ERR_BAD_FS); + } + if ((grub_errno = dnode_get (mosmdn, objnum, DMU_OT_POOL_PROPS, dn, data))) + { + grub_free (dn); + return (grub_errno); + } + if (zap_lookup (dn, ZPOOL_PROP_BOOTFS, &objnum, data)) + { + grub_free (dn); + return (GRUB_ERR_BAD_FS); + } + + if (!objnum) + { + grub_free (dn); + return (GRUB_ERR_BAD_FS); + } + + *obj = objnum; + return (0); +} +#endif +/* + * Given a MOS metadnode, get the metadnode of a given filesystem name (fsname), + * e.g. pool/rootfs, or a given object number (obj), e.g. the object number + * of pool/rootfs. + * + * If no fsname and no obj are given, return the DSL_DIR metadnode. + * If fsname is given, return its metadnode and its matching object number. + * If only obj is given, return the metadnode for this object number. + * + */ +static grub_err_t +get_filesystem_dnode (dnode_end_t * mosmdn, char *fsname, + dnode_end_t * mdn, struct grub_zfs_data *data) +{ + grub_uint64_t objnum; + grub_err_t err; + + grub_dprintf ("zfs", "endian = %d\n", mosmdn->endian); + + err = dnode_get (mosmdn, DMU_POOL_DIRECTORY_OBJECT, + DMU_OT_OBJECT_DIRECTORY, mdn, data); + if (err) + return err; + + grub_dprintf ("zfs", "alive\n"); + + err = zap_lookup (mdn, DMU_POOL_ROOT_DATASET, &objnum, data); + if (err) + return err; + + grub_dprintf ("zfs", "alive\n"); + + err = dnode_get (mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data); + if (err) + return err; + + grub_dprintf ("zfs", "alive\n"); + + while (*fsname) + { + grub_uint64_t childobj; + char *cname, ch; + + while (*fsname == '/') + fsname++; + + if (! *fsname || *fsname == '@') + break; + + cname = fsname; + while (*fsname && !grub_isspace (*fsname) && *fsname != '/') + fsname++; + ch = *fsname; + *fsname = 0; + + childobj = grub_zfs_to_cpu64 ((((dsl_dir_phys_t *) DN_BONUS (&mdn->dn)))->dd_child_dir_zapobj, mdn->endian); + err = dnode_get (mosmdn, childobj, + DMU_OT_DSL_DIR_CHILD_MAP, mdn, data); + if (err) + return err; + + err = zap_lookup (mdn, cname, &objnum, data); + if (err) + return err; + + err = dnode_get (mosmdn, objnum, DMU_OT_DSL_DIR, mdn, data); + if (err) + return err; + + *fsname = ch; + } + return GRUB_ERR_NONE; +} + +static grub_err_t +make_mdn (dnode_end_t * mdn, struct grub_zfs_data *data) +{ + objset_phys_t *osp; + blkptr_t *bp; + grub_size_t ospsize; + grub_err_t err; + + grub_dprintf ("zfs", "endian = %d\n", mdn->endian); + + bp = &(((dsl_dataset_phys_t *) DN_BONUS (&mdn->dn))->ds_bp); + err = zio_read (bp, mdn->endian, (void **) &osp, &ospsize, data); + if (err) + return err; + if (ospsize < OBJSET_PHYS_SIZE_V14) + { + grub_free (osp); + return grub_error (GRUB_ERR_BAD_FS, "too small osp"); + } + + mdn->endian = (grub_zfs_to_cpu64 (bp->blk_prop, mdn->endian)>>63) & 1; + grub_memmove ((char *) &(mdn->dn), (char *) &osp->os_meta_dnode, DNODE_SIZE); + grub_free (osp); + return GRUB_ERR_NONE; +} + +static grub_err_t +dnode_get_fullpath (const char *fullpath, dnode_end_t * mdn, + grub_uint64_t *mdnobj, dnode_end_t * dn, int *isfs, + struct grub_zfs_data *data) +{ + char *fsname, *snapname; + const char *ptr_at, *filename; + grub_uint64_t headobj; + grub_err_t err; + + ptr_at = grub_strchr (fullpath, '@'); + if (! ptr_at) + { + *isfs = 1; + filename = 0; + snapname = 0; + fsname = grub_strdup (fullpath); + } + else + { + const char *ptr_slash = grub_strchr (ptr_at, '/'); + + *isfs = 0; + fsname = grub_malloc (ptr_at - fullpath + 1); + if (!fsname) + return grub_errno; + grub_memcpy (fsname, fullpath, ptr_at - fullpath); + fsname[ptr_at - fullpath] = 0; + if (ptr_at[1] && ptr_at[1] != '/') + { + snapname = grub_malloc (ptr_slash - ptr_at); + if (!snapname) + { + grub_free (fsname); + return grub_errno; + } + grub_memcpy (snapname, ptr_at + 1, ptr_slash - ptr_at - 1); + snapname[ptr_slash - ptr_at - 1] = 0; + } + else + snapname = 0; + if (ptr_slash) + filename = ptr_slash; + else + filename = "/"; + grub_dprintf ("zfs", "fsname = '%s' snapname='%s' filename = '%s'\n", + fsname, snapname, filename); + } + grub_dprintf ("zfs", "alive\n"); + err = get_filesystem_dnode (&(data->mos), fsname, dn, data); + if (err) + { + grub_free (fsname); + grub_free (snapname); + return err; + } + + grub_dprintf ("zfs", "alive\n"); + + headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&dn->dn))->dd_head_dataset_obj, dn->endian); + + grub_dprintf ("zfs", "endian = %d\n", mdn->endian); + + err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data); + if (err) + { + grub_free (fsname); + grub_free (snapname); + return err; + } + grub_dprintf ("zfs", "endian = %d\n", mdn->endian); + + if (snapname) + { + grub_uint64_t snapobj; + + snapobj = grub_zfs_to_cpu64 (((dsl_dataset_phys_t *) DN_BONUS (&mdn->dn))->ds_snapnames_zapobj, mdn->endian); + + err = dnode_get (&(data->mos), snapobj, + DMU_OT_DSL_DS_SNAP_MAP, mdn, data); + if (!err) + err = zap_lookup (mdn, snapname, &headobj, data); + if (!err) + err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, mdn, data); + if (err) + { + grub_free (fsname); + grub_free (snapname); + return err; + } + } + + if (mdnobj) + *mdnobj = headobj; + + make_mdn (mdn, data); + + grub_dprintf ("zfs", "endian = %d\n", mdn->endian); + + if (*isfs) + { + grub_free (fsname); + grub_free (snapname); + return GRUB_ERR_NONE; + } + err = dnode_get_path (mdn, filename, dn, data); + grub_free (fsname); + grub_free (snapname); + return err; +} + +/* + * For a given XDR packed nvlist, verify the first 4 bytes and move on. + * + * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) : + * + * encoding method/host endian (4 bytes) + * nvl_version (4 bytes) + * nvl_nvflag (4 bytes) + * encoded nvpairs: + * encoded size of the nvpair (4 bytes) + * decoded size of the nvpair (4 bytes) + * name string size (4 bytes) + * name string data (sizeof(NV_ALIGN4(string)) + * data type (4 bytes) + * # of elements in the nvpair (4 bytes) + * data + * 2 zero's for the last nvpair + * (end of the entire list) (8 bytes) + * + */ + +static int +nvlist_find_value (char *nvlist, char *name, int valtype, char **val, + grub_size_t *size_out, grub_size_t *nelm_out) +{ + int name_len, type, encode_size; + char *nvpair, *nvp_name; + + /* Verify if the 1st and 2nd byte in the nvlist are valid. */ + /* NOTE: independently of what endianness header announces all + subsequent values are big-endian. */ + if (nvlist[0] != NV_ENCODE_XDR || (nvlist[1] != NV_LITTLE_ENDIAN + && nvlist[1] != NV_BIG_ENDIAN)) + { + grub_dprintf ("zfs", "incorrect nvlist header\n"); + grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist"); + return 0; + } + + /* skip the header, nvl_version, and nvl_nvflag */ + nvlist = nvlist + 4 * 3; + /* + * Loop thru the nvpair list + * The XDR representation of an integer is in big-endian byte order. + */ + while ((encode_size = grub_be_to_cpu32 (*(grub_uint32_t *) nvlist))) + { + int nelm; + + nvpair = nvlist + 4 * 2; /* skip the encode/decode size */ + + name_len = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair); + nvpair += 4; + + nvp_name = nvpair; + nvpair = nvpair + ((name_len + 3) & ~3); /* align */ + + type = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair); + nvpair += 4; + + nelm = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair); + if (nelm < 1) + return grub_error (GRUB_ERR_BAD_FS, "empty nvpair"); + + nvpair += 4; + + if ((grub_strncmp (nvp_name, name, name_len) == 0) && type == valtype) + { + *val = nvpair; + *size_out = encode_size; + if (nelm_out) + *nelm_out = nelm; + return 1; + } + + nvlist += encode_size; /* goto the next nvpair */ + } + return 0; +} + +int +grub_zfs_nvlist_lookup_uint64 (char *nvlist, char *name, grub_uint64_t * out) +{ + char *nvpair; + grub_size_t size; + int found; + + found = nvlist_find_value (nvlist, name, DATA_TYPE_UINT64, &nvpair, &size, 0); + if (!found) + return 0; + if (size < sizeof (grub_uint64_t)) + { + grub_error (GRUB_ERR_BAD_FS, "invalid uint64"); + return 0; + } + + *out = grub_be_to_cpu64 (*(grub_uint64_t *) nvpair); + return 1; +} + +char * +grub_zfs_nvlist_lookup_string (char *nvlist, char *name) +{ + char *nvpair; + char *ret; + grub_size_t slen; + grub_size_t size; + int found; + + found = nvlist_find_value (nvlist, name, DATA_TYPE_STRING, &nvpair, &size, 0); + if (!found) + return 0; + if (size < 4) + { + grub_error (GRUB_ERR_BAD_FS, "invalid string"); + return 0; + } + slen = grub_be_to_cpu32 (*(grub_uint32_t *) nvpair); + if (slen > size - 4) + slen = size - 4; + ret = grub_malloc (slen + 1); + if (!ret) + return 0; + grub_memcpy (ret, nvpair + 4, slen); + ret[slen] = 0; + return ret; +} + +char * +grub_zfs_nvlist_lookup_nvlist (char *nvlist, char *name) +{ + char *nvpair; + char *ret; + grub_size_t size; + int found; + + found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST, &nvpair, + &size, 0); + if (!found) + return 0; + ret = grub_zalloc (size + 3 * sizeof (grub_uint32_t)); + if (!ret) + return 0; + grub_memcpy (ret, nvlist, sizeof (grub_uint32_t)); + + grub_memcpy (ret + sizeof (grub_uint32_t), nvpair, size); + return ret; +} + +int +grub_zfs_nvlist_lookup_nvlist_array_get_nelm (char *nvlist, char *name) +{ + char *nvpair; + grub_size_t nelm, size; + int found; + + found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST, &nvpair, + &size, &nelm); + if (! found) + return -1; + return nelm; +} + +char * +grub_zfs_nvlist_lookup_nvlist_array (char *nvlist, char *name, + grub_size_t index) +{ + char *nvpair, *nvpairptr; + int found; + char *ret; + grub_size_t size; + unsigned i; + grub_size_t nelm; + + found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST, &nvpair, + &size, &nelm); + if (!found) + return 0; + if (index >= nelm) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, "trying to lookup past nvlist array"); + return 0; + } + + nvpairptr = nvpair; + + for (i = 0; i < index; i++) + { + grub_uint32_t encode_size; + + /* skip the header, nvl_version, and nvl_nvflag */ + nvpairptr = nvpairptr + 4 * 2; + + while (nvpairptr < nvpair + size + && (encode_size = grub_be_to_cpu32 (*(grub_uint32_t *) nvpairptr))) + nvlist += encode_size; /* goto the next nvpair */ + + nvlist = nvlist + 4 * 2; /* skip the ending 2 zeros - 8 bytes */ + } + + if (nvpairptr >= nvpair + size + || nvpairptr + grub_be_to_cpu32 (*(grub_uint32_t *) (nvpairptr + 4 * 2)) + >= nvpair + size) + { + grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist array"); + return 0; + } + + ret = grub_zalloc (grub_be_to_cpu32 (*(grub_uint32_t *) (nvpairptr + 4 * 2)) + + 3 * sizeof (grub_uint32_t)); + if (!ret) + return 0; + grub_memcpy (ret, nvlist, sizeof (grub_uint32_t)); + + grub_memcpy (ret + sizeof (grub_uint32_t), nvpairptr, size); + return ret; +} + +static grub_err_t +zfs_fetch_nvlist (struct grub_zfs_data * data, char **nvlist) +{ + grub_err_t err; + + *nvlist = grub_malloc (VDEV_PHYS_SIZE); + /* Read in the vdev name-value pair list (112K). */ + err = grub_disk_read (data->disk, data->vdev_phys_sector, 0, + VDEV_PHYS_SIZE, *nvlist); + if (err) + { + grub_free (*nvlist); + *nvlist = 0; + return err; + } + return GRUB_ERR_NONE; +} + +/* + * Check the disk label information and retrieve needed vdev name-value pairs. + * + */ +static grub_err_t +check_pool_label (struct grub_zfs_data *data) +{ + grub_uint64_t pool_state, txg = 0; + char *nvlist; +#if 0 + char *nv; +#endif + grub_uint64_t diskguid; + grub_uint64_t version; + int found; + grub_err_t err; + + err = zfs_fetch_nvlist (data, &nvlist); + if (err) + return err; + + grub_dprintf ("zfs", "check 2 passed\n"); + + found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_STATE, + &pool_state); + if (! found) + { + grub_free (nvlist); + if (! grub_errno) + grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_POOL_STATE " not found"); + return grub_errno; + } + grub_dprintf ("zfs", "check 3 passed\n"); + + if (pool_state == POOL_STATE_DESTROYED) + { + grub_free (nvlist); + return grub_error (GRUB_ERR_BAD_FS, "zpool is marked as destroyed"); + } + grub_dprintf ("zfs", "check 4 passed\n"); + + found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_TXG, &txg); + if (!found) + { + grub_free (nvlist); + if (! grub_errno) + grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_POOL_TXG " not found"); + return grub_errno; + } + grub_dprintf ("zfs", "check 6 passed\n"); + + /* not an active device */ + if (txg == 0) + { + grub_free (nvlist); + return grub_error (GRUB_ERR_BAD_FS, "zpool isn't active"); + } + grub_dprintf ("zfs", "check 7 passed\n"); + + found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_VERSION, + &version); + if (! found) + { + grub_free (nvlist); + if (! grub_errno) + grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_VERSION " not found"); + return grub_errno; + } + grub_dprintf ("zfs", "check 8 passed\n"); + + if (version > SPA_VERSION) + { + grub_free (nvlist); + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + "too new version %llu > %llu", + (unsigned long long) version, + (unsigned long long) SPA_VERSION); + } + grub_dprintf ("zfs", "check 9 passed\n"); +#if 0 + if (nvlist_lookup_value (nvlist, ZPOOL_CONFIG_VDEV_TREE, &nv, + DATA_TYPE_NVLIST, NULL)) + { + grub_free (vdev); + return (GRUB_ERR_BAD_FS); + } + grub_dprintf ("zfs", "check 10 passed\n"); +#endif + + found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_GUID, &diskguid); + if (! found) + { + grub_free (nvlist); + if (! grub_errno) + grub_error (GRUB_ERR_BAD_FS, ZPOOL_CONFIG_GUID " not found"); + return grub_errno; + } + grub_dprintf ("zfs", "check 11 passed\n"); + + grub_free (nvlist); + + return GRUB_ERR_NONE; +} + +static void +zfs_unmount (struct grub_zfs_data *data) +{ + grub_free (data->dnode_buf); + grub_free (data->dnode_mdn); + grub_free (data->file_buf); + grub_free (data); +} + +/* + * zfs_mount() locates a valid uberblock of the root pool and read in its MOS + * to the memory address MOS. + * + */ +static struct grub_zfs_data * +zfs_mount (grub_device_t dev) +{ + struct grub_zfs_data *data = 0; + int label = 0; + uberblock_phys_t *ub_array, *ubbest = NULL; + vdev_boot_header_t *bh; + objset_phys_t *osp = 0; + grub_size_t ospsize; + grub_err_t err; + int vdevnum; + + if (! dev->disk) + { + grub_error (GRUB_ERR_BAD_DEVICE, "not a disk"); + return 0; + } + + data = grub_malloc (sizeof (*data)); + if (!data) + return 0; + grub_memset (data, 0, sizeof (*data)); +#if 0 + /* if it's our first time here, zero the best uberblock out */ + if (data->best_drive == 0 && data->best_part == 0 && find_best_root) + grub_memset (¤t_uberblock, 0, sizeof (uberblock_t)); +#endif + + data->disk = dev->disk; + + ub_array = grub_malloc (VDEV_UBERBLOCK_RING); + if (!ub_array) + { + zfs_unmount (data); + return 0; + } + + bh = grub_malloc (VDEV_BOOT_HEADER_SIZE); + if (!bh) + { + zfs_unmount (data); + grub_free (ub_array); + return 0; + } + + vdevnum = VDEV_LABELS; + + /* Don't check back labels on CDROM. */ + if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN) + vdevnum = VDEV_LABELS / 2; + + for (label = 0; ubbest == NULL && label < vdevnum; label++) + { + grub_zfs_endian_t ub_endian = UNKNOWN_ENDIAN; + grub_dprintf ("zfs", "label %d\n", label); + + data->vdev_phys_sector + = label * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT) + + ((VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE) >> SPA_MINBLOCKSHIFT) + + (label < VDEV_LABELS / 2 ? 0 : grub_disk_get_size (dev->disk) + - VDEV_LABELS * (sizeof (vdev_label_t) >> SPA_MINBLOCKSHIFT)); + + /* Read in the uberblock ring (128K). */ + err = grub_disk_read (data->disk, data->vdev_phys_sector + + (VDEV_PHYS_SIZE >> SPA_MINBLOCKSHIFT), + 0, VDEV_UBERBLOCK_RING, (char *) ub_array); + if (err) + { + grub_errno = GRUB_ERR_NONE; + continue; + } + grub_dprintf ("zfs", "label ok %d\n", label); + + ubbest = find_bestub (ub_array, data->vdev_phys_sector); + if (!ubbest) + { + grub_dprintf ("zfs", "No uberblock found\n"); + grub_errno = GRUB_ERR_NONE; + continue; + } + ub_endian = (grub_zfs_to_cpu64 (ubbest->ubp_uberblock.ub_magic, + LITTLE_ENDIAN) == UBERBLOCK_MAGIC + ? LITTLE_ENDIAN : BIG_ENDIAN); + err = zio_read (&ubbest->ubp_uberblock.ub_rootbp, + ub_endian, + (void **) &osp, &ospsize, data); + if (err) + { + grub_dprintf ("zfs", "couldn't zio_read\n"); + grub_errno = GRUB_ERR_NONE; + continue; + } + + if (ospsize < OBJSET_PHYS_SIZE_V14) + { + grub_dprintf ("zfs", "osp too small\n"); + grub_free (osp); + continue; + } + grub_dprintf ("zfs", "ubbest %p\n", ubbest); + + err = check_pool_label (data); + if (err) + { + grub_errno = GRUB_ERR_NONE; + continue; + } +#if 0 + if (find_best_root && + vdev_uberblock_compare (&ubbest->ubp_uberblock, + &(current_uberblock)) <= 0) + continue; +#endif + /* Got the MOS. Save it at the memory addr MOS. */ + grub_memmove (&(data->mos.dn), &osp->os_meta_dnode, DNODE_SIZE); + data->mos.endian = (grub_zfs_to_cpu64 (ubbest->ubp_uberblock.ub_rootbp.blk_prop, ub_endian) >> 63) & 1; + grub_memmove (&(data->current_uberblock), + &ubbest->ubp_uberblock, sizeof (uberblock_t)); + grub_free (ub_array); + grub_free (bh); + grub_free (osp); + return data; + } + grub_error (GRUB_ERR_BAD_FS, "couldn't find a valid label"); + zfs_unmount (data); + grub_free (ub_array); + grub_free (bh); + grub_free (osp); + + return 0; +} + +grub_err_t +grub_zfs_fetch_nvlist (grub_device_t dev, char **nvlist) +{ + struct grub_zfs_data *zfs; + grub_err_t err; + + zfs = zfs_mount (dev); + if (!zfs) + return grub_errno; + err = zfs_fetch_nvlist (zfs, nvlist); + zfs_unmount (zfs); + return err; +} + +static grub_err_t +zfs_label (grub_device_t device, char **label) +{ + char *nvlist; + grub_err_t err; + struct grub_zfs_data *data; + + data = zfs_mount (device); + if (! data) + return grub_errno; + + err = zfs_fetch_nvlist (data, &nvlist); + if (err) + { + zfs_unmount (data); + return err; + } + + *label = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME); + grub_free (nvlist); + zfs_unmount (data); + return grub_errno; +} + +static grub_err_t +zfs_uuid (grub_device_t device, char **uuid) +{ + char *nvlist; + int found; + struct grub_zfs_data *data; + grub_uint64_t guid; + grub_err_t err; + + *uuid = 0; + + data = zfs_mount (device); + if (! data) + return grub_errno; + + err = zfs_fetch_nvlist (data, &nvlist); + if (err) + { + zfs_unmount (data); + return err; + } + + found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_GUID, &guid); + if (! found) + return grub_errno; + grub_free (nvlist); + *uuid = grub_xasprintf ("%016llx", (long long unsigned) guid); + zfs_unmount (data); + if (! *uuid) + return grub_errno; + return GRUB_ERR_NONE; +} + +/* + * zfs_open() locates a file in the rootpool by following the + * MOS and places the dnode of the file in the memory address DNODE. + */ +static grub_err_t +grub_zfs_open (struct grub_file *file, const char *fsfilename) +{ + struct grub_zfs_data *data; + grub_err_t err; + int isfs; + + data = zfs_mount (file->device); + if (! data) + return grub_errno; + + err = dnode_get_fullpath (fsfilename, &(data->mdn), 0, + &(data->dnode), &isfs, data); + if (err) + { + zfs_unmount (data); + return err; + } + + if (isfs) + { + zfs_unmount (data); + return grub_error (GRUB_ERR_FILE_NOT_FOUND, "Missing @ or / separator"); + } + + /* We found the dnode for this file. Verify if it is a plain file. */ + if (data->dnode.dn.dn_type != DMU_OT_PLAIN_FILE_CONTENTS) + { + zfs_unmount (data); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a file"); + } + + /* get the file size and set the file position to 0 */ + + /* + * For DMU_OT_SA we will need to locate the SIZE attribute + * attribute, which could be either in the bonus buffer + * or the "spill" block. + */ + if (data->dnode.dn.dn_bonustype == DMU_OT_SA) + { + sa_hdr_phys_t *sahdrp; + int hdrsize; + + if (data->dnode.dn.dn_bonuslen != 0) + { + sahdrp = (sa_hdr_phys_t *) DN_BONUS (&data->dnode.dn); + } + else if (data->dnode.dn.dn_flags & DNODE_FLAG_SPILL_BLKPTR) + { + blkptr_t *bp = &data->dnode.dn.dn_spill; + + err = zio_read (bp, data->dnode.endian, (void **) &sahdrp, NULL, data); + if (err) + return err; + } + else + { + return grub_error (GRUB_ERR_BAD_FS, "filesystem is corrupt"); + } + + hdrsize = SA_HDR_SIZE (sahdrp); + file->size = *(grub_uint64_t *) ((char *) sahdrp + hdrsize + SA_SIZE_OFFSET); + } + else + { + file->size = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&data->dnode.dn))->zp_size, data->dnode.endian); + } + + file->data = data; + file->offset = 0; + +#ifndef GRUB_UTIL + grub_dl_ref (my_mod); +#endif + + return GRUB_ERR_NONE; +} + +static grub_ssize_t +grub_zfs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_zfs_data *data = (struct grub_zfs_data *) file->data; + int blksz, movesize; + grub_size_t length; + grub_size_t read; + grub_err_t err; + + if (data->file_buf == NULL) + { + data->file_buf = grub_malloc (SPA_MAXBLOCKSIZE); + if (!data->file_buf) + return -1; + data->file_start = data->file_end = 0; + } + + /* + * If offset is in memory, move it into the buffer provided and return. + */ + if (file->offset >= data->file_start + && file->offset + len <= data->file_end) + { + grub_memmove (buf, data->file_buf + file->offset - data->file_start, + len); + return len; + } + + blksz = grub_zfs_to_cpu16 (data->dnode.dn.dn_datablkszsec, + data->dnode.endian) << SPA_MINBLOCKSHIFT; + + /* + * Entire Dnode is too big to fit into the space available. We + * will need to read it in chunks. This could be optimized to + * read in as large a chunk as there is space available, but for + * now, this only reads in one data block at a time. + */ + length = len; + read = 0; + while (length) + { + /* + * Find requested blkid and the offset within that block. + */ + grub_uint64_t blkid = grub_divmod64 (file->offset + read, blksz, 0); + grub_free (data->file_buf); + data->file_buf = 0; + + err = dmu_read (&(data->dnode), blkid, (void **) &(data->file_buf), + 0, data); + if (err) + return -1; + + data->file_start = blkid * blksz; + data->file_end = data->file_start + blksz; + + movesize = MIN (length, data->file_end - (int) file->offset - read); + + grub_memmove (buf, data->file_buf + file->offset + read + - data->file_start, movesize); + buf += movesize; + length -= movesize; + read += movesize; + } + + return len; +} + +static grub_err_t +grub_zfs_close (grub_file_t file) +{ + zfs_unmount ((struct grub_zfs_data *) file->data); + +#ifndef GRUB_UTIL + grub_dl_unref (my_mod); +#endif + + return GRUB_ERR_NONE; +} + +grub_err_t +grub_zfs_getmdnobj (grub_device_t dev, const char *fsfilename, + grub_uint64_t *mdnobj) +{ + struct grub_zfs_data *data; + grub_err_t err; + int isfs; + + data = zfs_mount (dev); + if (! data) + return grub_errno; + + err = dnode_get_fullpath (fsfilename, &(data->mdn), mdnobj, + &(data->dnode), &isfs, data); + zfs_unmount (data); + return err; +} + +static void +fill_fs_info (struct grub_dirhook_info *info, + dnode_end_t mdn, struct grub_zfs_data *data) +{ + grub_err_t err; + dnode_end_t dn; + grub_uint64_t objnum; + grub_uint64_t headobj; + + grub_memset (info, 0, sizeof (*info)); + + info->dir = 1; + + if (mdn.dn.dn_type == DMU_OT_DSL_DIR) + { + headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&mdn.dn))->dd_head_dataset_obj, mdn.endian); + + err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, &mdn, data); + if (err) + { + grub_dprintf ("zfs", "failed here\n"); + return; + } + } + make_mdn (&mdn, data); + err = dnode_get (&mdn, MASTER_NODE_OBJ, DMU_OT_MASTER_NODE, + &dn, data); + if (err) + { + grub_dprintf ("zfs", "failed here\n"); + return; + } + + err = zap_lookup (&dn, ZFS_ROOT_OBJ, &objnum, data); + if (err) + { + grub_dprintf ("zfs", "failed here\n"); + return; + } + + err = dnode_get (&mdn, objnum, 0, &dn, data); + if (err) + { + grub_dprintf ("zfs", "failed here\n"); + return; + } + + info->mtimeset = 1; + info->mtime = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&dn.dn))->zp_mtime[0], dn.endian); + return; +} + +static grub_err_t +grub_zfs_dir (grub_device_t device, const char *path, + int (*hook) (const char *, const struct grub_dirhook_info *)) +{ + struct grub_zfs_data *data; + grub_err_t err; + int isfs; + auto int NESTED_FUNC_ATTR iterate_zap (const char *name, grub_uint64_t val); + auto int NESTED_FUNC_ATTR iterate_zap_fs (const char *name, + grub_uint64_t val); + auto int NESTED_FUNC_ATTR iterate_zap_snap (const char *name, + grub_uint64_t val); + + int NESTED_FUNC_ATTR iterate_zap (const char *name, grub_uint64_t val) + { + struct grub_dirhook_info info; + dnode_end_t dn; + grub_memset (&info, 0, sizeof (info)); + + dnode_get (&(data->mdn), val, 0, &dn, data); + info.mtimeset = 1; + info.mtime = grub_zfs_to_cpu64 (((znode_phys_t *) DN_BONUS (&dn.dn))->zp_mtime[0], dn.endian); + info.dir = (dn.dn.dn_type == DMU_OT_DIRECTORY_CONTENTS); + grub_dprintf ("zfs", "type=%d, name=%s\n", + (int)dn.dn.dn_type, (char *)name); + return hook (name, &info); + } + + int NESTED_FUNC_ATTR iterate_zap_fs (const char *name, grub_uint64_t val) + { + struct grub_dirhook_info info; + dnode_end_t mdn; + err = dnode_get (&(data->mos), val, 0, &mdn, data); + if (err) + return 0; + if (mdn.dn.dn_type != DMU_OT_DSL_DIR) + return 0; + + fill_fs_info (&info, mdn, data); + return hook (name, &info); + } + int NESTED_FUNC_ATTR iterate_zap_snap (const char *name, grub_uint64_t val) + { + struct grub_dirhook_info info; + char *name2; + int ret; + dnode_end_t mdn; + + err = dnode_get (&(data->mos), val, 0, &mdn, data); + if (err) + return 0; + + if (mdn.dn.dn_type != DMU_OT_DSL_DATASET) + return 0; + + fill_fs_info (&info, mdn, data); + + name2 = grub_malloc (grub_strlen (name) + 2); + name2[0] = '@'; + grub_memcpy (name2 + 1, name, grub_strlen (name) + 1); + ret = hook (name2, &info); + grub_free (name2); + return ret; + } + + data = zfs_mount (device); + if (! data) + return grub_errno; + err = dnode_get_fullpath (path, &(data->mdn), 0, &(data->dnode), &isfs, data); + if (err) + { + zfs_unmount (data); + return err; + } + if (isfs) + { + grub_uint64_t childobj, headobj; + grub_uint64_t snapobj; + dnode_end_t dn; + struct grub_dirhook_info info; + + fill_fs_info (&info, data->dnode, data); + hook ("@", &info); + + childobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&data->dnode.dn))->dd_child_dir_zapobj, data->dnode.endian); + headobj = grub_zfs_to_cpu64 (((dsl_dir_phys_t *) DN_BONUS (&data->dnode.dn))->dd_head_dataset_obj, data->dnode.endian); + err = dnode_get (&(data->mos), childobj, + DMU_OT_DSL_DIR_CHILD_MAP, &dn, data); + if (err) + { + zfs_unmount (data); + return err; + } + + zap_iterate (&dn, iterate_zap_fs, data); + + err = dnode_get (&(data->mos), headobj, DMU_OT_DSL_DATASET, &dn, data); + if (err) + { + zfs_unmount (data); + return err; + } + + snapobj = grub_zfs_to_cpu64 (((dsl_dataset_phys_t *) DN_BONUS (&dn.dn))->ds_snapnames_zapobj, dn.endian); + + err = dnode_get (&(data->mos), snapobj, + DMU_OT_DSL_DS_SNAP_MAP, &dn, data); + if (err) + { + zfs_unmount (data); + return err; + } + + zap_iterate (&dn, iterate_zap_snap, data); + } + else + { + if (data->dnode.dn.dn_type != DMU_OT_DIRECTORY_CONTENTS) + { + zfs_unmount (data); + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "not a directory"); + } + zap_iterate (&(data->dnode), iterate_zap, data); + } + zfs_unmount (data); + return grub_errno; +} + +static struct grub_fs grub_zfs_fs = { + .name = "zfs", + .dir = grub_zfs_dir, + .open = grub_zfs_open, + .read = grub_zfs_read, + .close = grub_zfs_close, + .label = zfs_label, + .uuid = zfs_uuid, + .mtime = 0, + .next = 0 +}; + +GRUB_MOD_INIT (zfs) +{ + grub_fs_register (&grub_zfs_fs); +#ifndef GRUB_UTIL + my_mod = mod; +#endif +} + +GRUB_MOD_FINI (zfs) +{ + grub_fs_unregister (&grub_zfs_fs); +} diff --git a/libr/fs/p/grub/fs/zfs/zfs_fletcher.c b/libr/fs/p/grub/fs/zfs/zfs_fletcher.c new file mode 100644 index 0000000000..7d27b053dc --- /dev/null +++ b/libr/fs/p/grub/fs/zfs/zfs_fletcher.c @@ -0,0 +1,84 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc. + * Copyright 2007 Sun Microsystems, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void +fletcher_2(const void *buf, grub_uint64_t size, grub_zfs_endian_t endian, + zio_cksum_t *zcp) +{ + const grub_uint64_t *ip = buf; + const grub_uint64_t *ipend = ip + (size / sizeof (grub_uint64_t)); + grub_uint64_t a0, b0, a1, b1; + + for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) + { + a0 += grub_zfs_to_cpu64 (ip[0], endian); + a1 += grub_zfs_to_cpu64 (ip[1], endian); + b0 += a0; + b1 += a1; + } + + zcp->zc_word[0] = grub_cpu_to_zfs64 (a0, endian); + zcp->zc_word[1] = grub_cpu_to_zfs64 (a1, endian); + zcp->zc_word[2] = grub_cpu_to_zfs64 (b0, endian); + zcp->zc_word[3] = grub_cpu_to_zfs64 (b1, endian); +} + +void +fletcher_4 (const void *buf, grub_uint64_t size, grub_zfs_endian_t endian, + zio_cksum_t *zcp) +{ + const grub_uint32_t *ip = buf; + const grub_uint32_t *ipend = ip + (size / sizeof (grub_uint32_t)); + grub_uint64_t a, b, c, d; + + for (a = b = c = d = 0; ip < ipend; ip++) + { + a += grub_zfs_to_cpu32 (ip[0], endian);; + b += a; + c += b; + d += c; + } + + zcp->zc_word[0] = grub_cpu_to_zfs64 (a, endian); + zcp->zc_word[1] = grub_cpu_to_zfs64 (b, endian); + zcp->zc_word[2] = grub_cpu_to_zfs64 (c, endian); + zcp->zc_word[3] = grub_cpu_to_zfs64 (d, endian); +} + diff --git a/libr/fs/p/grub/fs/zfs/zfs_lzjb.c b/libr/fs/p/grub/fs/zfs/zfs_lzjb.c new file mode 100644 index 0000000000..62b5ea6a0e --- /dev/null +++ b/libr/fs/p/grub/fs/zfs/zfs_lzjb.c @@ -0,0 +1,93 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc. + * Copyright 2007 Sun Microsystems, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MATCH_BITS 6 +#define MATCH_MIN 3 +#define OFFSET_MASK ((1 << (16 - MATCH_BITS)) - 1) + +/* + * Decompression Entry - lzjb + */ +#ifndef NBBY +#define NBBY 8 +#endif + +grub_err_t +lzjb_decompress (void *s_start, void *d_start, grub_size_t s_len, + grub_size_t d_len); + +grub_err_t +lzjb_decompress (void *s_start, void *d_start, grub_size_t s_len, + grub_size_t d_len) +{ + grub_uint8_t *src = s_start; + grub_uint8_t *dst = d_start; + grub_uint8_t *d_end = (grub_uint8_t *) d_start + d_len; + grub_uint8_t *s_end = (grub_uint8_t *) s_start + s_len; + grub_uint8_t *cpy, copymap = 0; + int copymask = 1 << (NBBY - 1); + + while (dst < d_end && src < s_end) + { + if ((copymask <<= 1) == (1 << NBBY)) + { + copymask = 1; + copymap = *src++; + } + if (src >= s_end) + return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed"); + if (copymap & copymask) + { + int mlen = (src[0] >> (NBBY - MATCH_BITS)) + MATCH_MIN; + int offset = ((src[0] << NBBY) | src[1]) & OFFSET_MASK; + src += 2; + cpy = dst - offset; + if (src > s_end || cpy < (grub_uint8_t *) d_start) + return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed"); + while (--mlen >= 0 && dst < d_end) + *dst++ = *cpy++; + } + else + *dst++ = *src++; + } + if (dst < d_end) + return grub_error (GRUB_ERR_BAD_FS, "lzjb decompression failed"); + return GRUB_ERR_NONE; +} diff --git a/libr/fs/p/grub/fs/zfs/zfs_sha256.c b/libr/fs/p/grub/fs/zfs/zfs_sha256.c new file mode 100644 index 0000000000..ba510cf695 --- /dev/null +++ b/libr/fs/p/grub/fs/zfs/zfs_sha256.c @@ -0,0 +1,143 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc. + * Copyright 2007 Sun Microsystems, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * SHA-256 checksum, as specified in FIPS 180-2, available at: + * http://csrc.nist.gov/cryptval + * + * This is a very compact implementation of SHA-256. + * It is designed to be simple and portable, not to be fast. + */ + +/* + * The literal definitions according to FIPS180-2 would be: + * + * Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) + * Maj(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) + * + * We use logical equivalents which require one less op. + */ +#define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define Maj(x, y, z) (((x) & (y)) ^ ((z) & ((x) ^ (y)))) +#define Rot32(x, s) (((x) >> s) | ((x) << (32 - s))) +#define SIGMA0(x) (Rot32(x, 2) ^ Rot32(x, 13) ^ Rot32(x, 22)) +#define SIGMA1(x) (Rot32(x, 6) ^ Rot32(x, 11) ^ Rot32(x, 25)) +#define sigma0(x) (Rot32(x, 7) ^ Rot32(x, 18) ^ ((x) >> 3)) +#define sigma1(x) (Rot32(x, 17) ^ Rot32(x, 19) ^ ((x) >> 10)) + +static const grub_uint32_t SHA256_K[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + +static void +SHA256Transform(grub_uint32_t *H, const grub_uint8_t *cp) +{ + grub_uint32_t a, b, c, d, e, f, g, h, t, T1, T2, W[64]; + + for (t = 0; t < 16; t++, cp += 4) + W[t] = (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3]; + + for (t = 16; t < 64; t++) + W[t] = sigma1(W[t - 2]) + W[t - 7] + + sigma0(W[t - 15]) + W[t - 16]; + + a = H[0]; b = H[1]; c = H[2]; d = H[3]; + e = H[4]; f = H[5]; g = H[6]; h = H[7]; + + for (t = 0; t < 64; t++) { + T1 = h + SIGMA1(e) + Ch(e, f, g) + SHA256_K[t] + W[t]; + T2 = SIGMA0(a) + Maj(a, b, c); + h = g; g = f; f = e; e = d + T1; + d = c; c = b; b = a; a = T1 + T2; + } + + H[0] += a; H[1] += b; H[2] += c; H[3] += d; + H[4] += e; H[5] += f; H[6] += g; H[7] += h; +} + +void +zio_checksum_SHA256(const void *buf, grub_uint64_t size, + grub_zfs_endian_t endian, zio_cksum_t *zcp) +{ + grub_uint32_t H[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; + grub_uint8_t pad[128]; + unsigned padsize = size & 63; + unsigned i; + + for (i = 0; i < size - padsize; i += 64) + SHA256Transform(H, (grub_uint8_t *)buf + i); + + for (i = 0; i < padsize; i++) + pad[i] = ((grub_uint8_t *)buf)[i]; + + for (pad[padsize++] = 0x80; (padsize & 63) != 56; padsize++) + pad[padsize] = 0; + + for (i = 0; i < 8; i++) + pad[padsize++] = (size << 3) >> (56 - 8 * i); + + for (i = 0; i < padsize; i += 64) + SHA256Transform(H, pad + i); + + zcp->zc_word[0] = grub_cpu_to_zfs64 ((grub_uint64_t)H[0] << 32 | H[1], + endian); + zcp->zc_word[1] = grub_cpu_to_zfs64 ((grub_uint64_t)H[2] << 32 | H[3], + endian); + zcp->zc_word[2] = grub_cpu_to_zfs64 ((grub_uint64_t)H[4] << 32 | H[5], + endian); + zcp->zc_word[3] = grub_cpu_to_zfs64 ((grub_uint64_t)H[6] << 32 | H[7], + endian); +} diff --git a/libr/fs/p/grub/fs/zfs/zfsinfo.c b/libr/fs/p/grub/fs/zfs/zfsinfo.c new file mode 100644 index 0000000000..33065892a3 --- /dev/null +++ b/libr/fs/p/grub/fs/zfs/zfsinfo.c @@ -0,0 +1,412 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc. + * Copyright 2008 Sun Microsystems, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static inline void +print_tabs (int n) +{ + int i; + + for (i = 0; i < n; i++) + grub_printf (" "); +} + +static grub_err_t +print_state (char *nvlist, int tab) +{ + grub_uint64_t ival; + int isok = 1; + + print_tabs (tab); + grub_printf ("State: "); + + if (grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_REMOVED, &ival)) + { + grub_printf ("removed "); + isok = 0; + } + + if (grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_FAULTED, &ival)) + { + grub_printf ("faulted "); + isok = 0; + } + + if (grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_OFFLINE, &ival)) + { + grub_printf ("offline "); + isok = 0; + } + + if (grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_FAULTED, &ival)) + grub_printf ("degraded "); + + if (isok) + grub_printf ("online"); + grub_printf ("\n"); + + return GRUB_ERR_NONE; +} + +static grub_err_t +print_vdev_info (char *nvlist, int tab) +{ + char *type = 0; + + type = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_TYPE); + + if (!type) + { + print_tabs (tab); + grub_printf ("Incorrect VDEV: no type available\n"); + return grub_errno; + } + + if (grub_strcmp (type, VDEV_TYPE_DISK) == 0) + { + char *bootpath = 0; + char *path = 0; + char *devid = 0; + + print_tabs (tab); + grub_printf ("Leaf VDEV\n"); + + print_state (nvlist, tab); + + bootpath = + grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_PHYS_PATH); + print_tabs (tab); + if (!bootpath) + grub_printf ("Bootpath: unavailable\n"); + else + grub_printf ("Bootpath: %s\n", bootpath); + + path = grub_zfs_nvlist_lookup_string (nvlist, "path"); + print_tabs (tab); + if (!path) + grub_printf ("Path: unavailable\n"); + else + grub_printf ("Path: %s\n", path); + + devid = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_DEVID); + print_tabs (tab); + if (!devid) + grub_printf ("Devid: unavailable\n"); + else + grub_printf ("Devid: %s\n", devid); + grub_free (bootpath); + grub_free (devid); + grub_free (path); + return GRUB_ERR_NONE; + } + + if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0) + { + int nelm, i; + + nelm = grub_zfs_nvlist_lookup_nvlist_array_get_nelm + (nvlist, ZPOOL_CONFIG_CHILDREN); + + print_tabs (tab); + if (nelm <= 0) + { + grub_printf ("Incorrect mirror VDEV\n"); + return GRUB_ERR_NONE; + } + grub_printf ("Mirror VDEV with %d children\n", nelm); + print_state (nvlist, tab); + + for (i = 0; i < nelm; i++) + { + char *child; + + child = grub_zfs_nvlist_lookup_nvlist_array + (nvlist, ZPOOL_CONFIG_CHILDREN, i); + + print_tabs (tab); + if (!child) + { + grub_printf ("Mirror VDEV element %d isn't correct\n", i); + continue; + } + + grub_printf ("Mirror VDEV element %d:\n", i); + print_vdev_info (child, tab + 1); + + grub_free (child); + } + } + + print_tabs (tab); + grub_printf ("Unknown VDEV type: %s\n", type); + + return GRUB_ERR_NONE; +} + +static grub_err_t +get_bootpath (char *nvlist, char **bootpath, char **devid) +{ + char *type = 0; + + type = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_TYPE); + + if (!type) + return grub_errno; + + if (grub_strcmp (type, VDEV_TYPE_DISK) == 0) + { + *bootpath = grub_zfs_nvlist_lookup_string (nvlist, + ZPOOL_CONFIG_PHYS_PATH); + *devid = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_DEVID); + if (!*bootpath || !*devid) + { + grub_free (*bootpath); + grub_free (*devid); + *bootpath = 0; + *devid = 0; + } + return GRUB_ERR_NONE; + } + + if (grub_strcmp (type, VDEV_TYPE_MIRROR) == 0) + { + int nelm, i; + + nelm = grub_zfs_nvlist_lookup_nvlist_array_get_nelm + (nvlist, ZPOOL_CONFIG_CHILDREN); + + for (i = 0; i < nelm; i++) + { + char *child; + + child = grub_zfs_nvlist_lookup_nvlist_array (nvlist, + ZPOOL_CONFIG_CHILDREN, + i); + + get_bootpath (child, bootpath, devid); + + grub_free (child); + + if (*bootpath && *devid) + return GRUB_ERR_NONE; + } + } + + return GRUB_ERR_NONE; +} + +static char *poolstates[] = { + [POOL_STATE_ACTIVE] = "active", + [POOL_STATE_EXPORTED] = "exported", + [POOL_STATE_DESTROYED] = "destroyed", + [POOL_STATE_SPARE] = "reserved for hot spare", + [POOL_STATE_L2CACHE] = "level 2 ARC device", + [POOL_STATE_UNINITIALIZED] = "uninitialized", + [POOL_STATE_UNAVAIL] = "unavailable", + [POOL_STATE_POTENTIALLY_ACTIVE] = "potentially active" +}; + +static grub_err_t +grub_cmd_zfsinfo (grub_command_t cmd __attribute__ ((unused)), int argc, + char **args) +{ + grub_device_t dev; + char *devname; + grub_err_t err; + char *nvlist = 0; + char *nv = 0; + char *poolname; + grub_uint64_t guid; + grub_uint64_t pool_state; + int found; + + if (argc < 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required"); + + if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')') + { + devname = grub_strdup (args[0] + 1); + if (devname) + devname[grub_strlen (devname) - 1] = 0; + } + else + devname = grub_strdup (args[0]); + if (!devname) + return grub_errno; + + dev = grub_device_open (devname); + grub_free (devname); + if (!dev) + return grub_errno; + + err = grub_zfs_fetch_nvlist (dev, &nvlist); + + grub_device_close (dev); + + if (err) + return err; + + poolname = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME); + if (!poolname) + grub_printf ("Pool name: unavailable\n"); + else + grub_printf ("Pool name: %s\n", poolname); + + found = + grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_GUID, &guid); + if (!found) + grub_printf ("Pool GUID: unavailable\n"); + else + grub_printf ("Pool GUID: %016llx\n", (long long unsigned) guid); + + found = grub_zfs_nvlist_lookup_uint64 (nvlist, ZPOOL_CONFIG_POOL_STATE, + &pool_state); + if (!found) + grub_printf ("Unable to retrieve pool state\n"); + else if (pool_state >= ARRAY_SIZE (poolstates)) + grub_printf ("Unrecognized pool state\n"); + else + grub_printf ("Pool state: %s\n", poolstates[pool_state]); + + nv = grub_zfs_nvlist_lookup_nvlist (nvlist, ZPOOL_CONFIG_VDEV_TREE); + + if (!nv) + grub_printf ("No vdev tree available\n"); + else + print_vdev_info (nv, 1); + + grub_free (nv); + grub_free (nvlist); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_cmd_zfs_bootfs (grub_command_t cmd __attribute__ ((unused)), int argc, + char **args) +{ + grub_device_t dev; + char *devname; + grub_err_t err; + char *nvlist = 0; + char *nv = 0; + char *bootpath = 0, *devid = 0; + char *fsname; + char *bootfs; + char *poolname; + grub_uint64_t mdnobj; + + if (argc < 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "filesystem name required"); + + devname = grub_file_get_device_name (args[0]); + if (grub_errno) + return grub_errno; + + dev = grub_device_open (devname); + grub_free (devname); + if (!dev) + return grub_errno; + + err = grub_zfs_fetch_nvlist (dev, &nvlist); + + fsname = grub_strchr (args[0], ')'); + if (fsname) + fsname++; + else + fsname = args[0]; + + if (!err) + err = grub_zfs_getmdnobj (dev, fsname, &mdnobj); + + grub_device_close (dev); + + if (err) + return err; + + poolname = grub_zfs_nvlist_lookup_string (nvlist, ZPOOL_CONFIG_POOL_NAME); + if (!poolname) + { + if (!grub_errno) + grub_error (GRUB_ERR_BAD_FS, "No poolname found"); + return grub_errno; + } + + nv = grub_zfs_nvlist_lookup_nvlist (nvlist, ZPOOL_CONFIG_VDEV_TREE); + + if (nv) + get_bootpath (nv, &bootpath, &devid); + + grub_free (nv); + grub_free (nvlist); + + if (bootpath && devid) + { + bootfs = grub_xasprintf ("zfs-bootfs=%s/%llu bootpath=%s diskdevid=%s", + poolname, (unsigned long long) mdnobj, + bootpath, devid); + if (!bootfs) + return grub_errno; + } + else + { + bootfs = grub_xasprintf ("zfs-bootfs=%s/%llu", + poolname, (unsigned long long) mdnobj); + if (!bootfs) + return grub_errno; + } + if (argc >= 2) + grub_env_set (args[1], bootfs); + else + grub_printf ("%s\n", bootfs); + + grub_free (bootfs); + grub_free (poolname); + grub_free (bootpath); + grub_free (devid); + + return GRUB_ERR_NONE; +} + + +static grub_command_t cmd_info, cmd_bootfs; + +GRUB_MOD_INIT (zfsinfo) +{ + cmd_info = grub_register_command ("zfsinfo", grub_cmd_zfsinfo, + "zfsinfo DEVICE", + "Print ZFS info about DEVICE."); + cmd_bootfs = grub_register_command ("zfs-bootfs", grub_cmd_zfs_bootfs, + "zfs-bootfs FILESYSTEM [VARIABLE]", + "Print ZFS-BOOTFSOBJ or set it to VARIABLE"); +} + +GRUB_MOD_FINI (zfsinfo) +{ + grub_unregister_command (cmd_info); + grub_unregister_command (cmd_bootfs); +} diff --git a/libr/fs/p/grub/include/config-util.h b/libr/fs/p/grub/include/config-util.h new file mode 100644 index 0000000000..e50f6c577b --- /dev/null +++ b/libr/fs/p/grub/include/config-util.h @@ -0,0 +1,1245 @@ +/* config-util.h. Generated from config-util.h.in by configure. */ +/* config-util.h.in. Generated from configure.ac by autoheader. */ + +/* Define if the compiler is building for multiple architectures of Apple + platforms at once. */ +/* #undef AA_APPLE_UNIVERSAL_BUILD */ + +/* Define if building universal (internal helper macro) */ +/* #undef AC_APPLE_UNIVERSAL_BUILD */ + +/* Define to the number of bits in type 'ptrdiff_t'. */ +/* #undef BITSIZEOF_PTRDIFF_T */ + +/* Define to the number of bits in type 'sig_atomic_t'. */ +/* #undef BITSIZEOF_SIG_ATOMIC_T */ + +/* Define to the number of bits in type 'size_t'. */ +/* #undef BITSIZEOF_SIZE_T */ + +/* Define to the number of bits in type 'wchar_t'. */ +/* #undef BITSIZEOF_WCHAR_T */ + +/* Define to the number of bits in type 'wint_t'. */ +/* #undef BITSIZEOF_WINT_T */ + +/* Define to one of `_getb67', `GETB67', `getb67' for Cray-2 and Cray-YMP + systems. This function is required for `alloca.c' support on those systems. + */ +/* #undef CRAY_STACKSEG_END */ + +/* Define to 1 if using `alloca.c'. */ +/* #undef C_ALLOCA */ + +/* Define to 1 if // is a file system root distinct from /. */ +/* #undef DOUBLE_SLASH_IS_DISTINCT_ROOT */ + +/* Define to 1 if translation of program messages to the user's native + language is requested. */ +#define ENABLE_NLS 1 + +/* Define on systems for which file names may have a so-called `drive letter' + prefix, define this to compute the length of that prefix, including the + colon. */ +#define FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX 0 + +/* Define if the backslash character may also serve as a file name component + separator. */ +#define FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR 0 + +/* Define if a drive letter prefix denotes a relative path if it is not + followed by a file name component separator. */ +#define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0 + +/* Define to a C preprocessor expression that evaluates to 1 or 0, depending + whether the gnulib module malloc-gnu shall be considered present. */ +#define GNULIB_MALLOC_GNU 1 + +/* Define to 1 to add extern declaration of program_invocation_name to argp.h + */ +/* #undef GNULIB_PROGRAM_INVOCATION_NAME */ + +/* Define to 1 to add extern declaration of program_invocation_short_name to + argp.h */ +/* #undef GNULIB_PROGRAM_INVOCATION_SHORT_NAME */ + +/* Define to 1 when the gnulib module btowc should be tested. */ +#define GNULIB_TEST_BTOWC 1 + +/* Define to 1 when the gnulib module getdelim should be tested. */ +#define GNULIB_TEST_GETDELIM 1 + +/* Define to 1 when the gnulib module getline should be tested. */ +#define GNULIB_TEST_GETLINE 1 + +/* Define to 1 when the gnulib module getopt-gnu should be tested. */ +#define GNULIB_TEST_GETOPT_GNU 1 + +/* Define to 1 when the gnulib module malloc-posix should be tested. */ +#define GNULIB_TEST_MALLOC_POSIX 1 + +/* Define to 1 when the gnulib module mbrtowc should be tested. */ +#define GNULIB_TEST_MBRTOWC 1 + +/* Define to 1 when the gnulib module mbsinit should be tested. */ +#define GNULIB_TEST_MBSINIT 1 + +/* Define to 1 when the gnulib module mbsrtowcs should be tested. */ +#define GNULIB_TEST_MBSRTOWCS 1 + +/* Define to 1 when the gnulib module memchr should be tested. */ +#define GNULIB_TEST_MEMCHR 1 + +/* Define to 1 when the gnulib module mempcpy should be tested. */ +#define GNULIB_TEST_MEMPCPY 1 + +/* Define to 1 when the gnulib module nl_langinfo should be tested. */ +#define GNULIB_TEST_NL_LANGINFO 1 + +/* Define to 1 when the gnulib module rawmemchr should be tested. */ +#define GNULIB_TEST_RAWMEMCHR 1 + +/* Define to 1 when the gnulib module realloc-posix should be tested. */ +#define GNULIB_TEST_REALLOC_POSIX 1 + +/* Define to 1 when the gnulib module sleep should be tested. */ +#define GNULIB_TEST_SLEEP 1 + +/* Define to 1 when the gnulib module strchrnul should be tested. */ +#define GNULIB_TEST_STRCHRNUL 1 + +/* Define to 1 when the gnulib module strerror should be tested. */ +#define GNULIB_TEST_STRERROR 1 + +/* Define to 1 when the gnulib module strndup should be tested. */ +#define GNULIB_TEST_STRNDUP 1 + +/* Define to 1 when the gnulib module strnlen should be tested. */ +#define GNULIB_TEST_STRNLEN 1 + +/* Define to 1 when the gnulib module vsnprintf should be tested. */ +#define GNULIB_TEST_VSNPRINTF 1 + +/* Define to 1 when the gnulib module wcrtomb should be tested. */ +#define GNULIB_TEST_WCRTOMB 1 + +/* Default boot directory name" */ +#define GRUB_BOOT_DIR_NAME "boot" + +/* Default grub directory name */ +#define GRUB_DIR_NAME "grub" + +/* Define to 1 if you have 'alloca' after including , a header that + may be supplied by this distribution. */ +#define HAVE_ALLOCA 1 + +/* Define to 1 if you have and it should be used (not on Ultrix). + */ +#define HAVE_ALLOCA_H 1 + +/* Define to 1 if you have the `asprintf' function. */ +#define HAVE_ASPRINTF 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_BP_SYM_H */ + +/* Define to 1 if you have the `btowc' function. */ +#define HAVE_BTOWC 1 + +/* Define to 1 if you have the MacOS X function CFLocaleCopyCurrent in the + CoreFoundation framework. */ +/* #undef HAVE_CFLOCALECOPYCURRENT */ + +/* Define to 1 if you have the MacOS X function CFPreferencesCopyAppValue in + the CoreFoundation framework. */ +/* #undef HAVE_CFPREFERENCESCOPYAPPVALUE */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_CURSES_H */ + +/* Define if the GNU dcgettext() function is already present or preinstalled. + */ +#define HAVE_DCGETTEXT 1 + +/* Define to 1 if you have the declaration of `clearerr_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_CLEARERR_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `feof_unlocked', and to 0 if you + don't. */ +#define HAVE_DECL_FEOF_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `ferror_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FERROR_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `fflush_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FFLUSH_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `fgets_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FGETS_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `fputc_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FPUTC_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `fputs_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FPUTS_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `fread_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FREAD_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `fwrite_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_FWRITE_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `getchar_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_GETCHAR_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `getc_unlocked', and to 0 if you + don't. */ +#define HAVE_DECL_GETC_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `getdelim', and to 0 if you + don't. */ +#define HAVE_DECL_GETDELIM 1 + +/* Define to 1 if you have the declaration of `getenv', and to 0 if you don't. + */ +#define HAVE_DECL_GETENV 1 + +/* Define to 1 if you have the declaration of `getline', and to 0 if you + don't. */ +#define HAVE_DECL_GETLINE 1 + +/* Define to 1 if you have the declaration of `getopt_clip', and to 0 if you + don't. */ +#define HAVE_DECL_GETOPT_CLIP 0 + +/* Define to 1 if you have the declaration of `isblank', and to 0 if you + don't. */ +/* #undef HAVE_DECL_ISBLANK */ + +/* Define to 1 if you have the declaration of `iswblank', and to 0 if you + don't. */ +#define HAVE_DECL_ISWBLANK 0 + +/* Define to 1 if you have the declaration of `optreset', and to 0 if you + don't. */ +#define HAVE_DECL_OPTRESET 0 + +/* Define to 1 if you have the declaration of `program_invocation_name', and + to 0 if you don't. */ +#define HAVE_DECL_PROGRAM_INVOCATION_NAME 1 + +/* Define to 1 if you have the declaration of `program_invocation_short_name', + and to 0 if you don't. */ +#define HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME 1 + +/* Define to 1 if you have the declaration of `putchar_unlocked', and to 0 if + you don't. */ +#define HAVE_DECL_PUTCHAR_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `putc_unlocked', and to 0 if you + don't. */ +#define HAVE_DECL_PUTC_UNLOCKED 1 + +/* Define to 1 if you have the declaration of `sleep', and to 0 if you don't. + */ +#define HAVE_DECL_SLEEP 1 + +/* Define to 1 if you have the declaration of `strerror', and to 0 if you + don't. */ +/* #undef HAVE_DECL_STRERROR */ + +/* Define to 1 if you have the declaration of `strerror_r', and to 0 if you + don't. */ +#define HAVE_DECL_STRERROR_R 1 + +/* Define to 1 if you have the declaration of `strncasecmp', and to 0 if you + don't. */ +#define HAVE_DECL_STRNCASECMP 1 + +/* Define to 1 if you have the declaration of `strndup', and to 0 if you + don't. */ +#define HAVE_DECL_STRNDUP 1 + +/* Define to 1 if you have the declaration of `strnlen', and to 0 if you + don't. */ +#define HAVE_DECL_STRNLEN 1 + +/* Define to 1 if you have the declaration of `vsnprintf', and to 0 if you + don't. */ +#define HAVE_DECL_VSNPRINTF 1 + +/* Define to 1 if you have the declaration of `_snprintf', and to 0 if you + don't. */ +#define HAVE_DECL__SNPRINTF 0 + +/* Define to 1 if you have the devmapper library. */ +#define HAVE_DEVICE_MAPPER 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ERRNO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_FEATURES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_FLOAT_H 1 + +/* Define to 1 if you have the `flockfile' function. */ +#define HAVE_FLOCKFILE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_FT2BUILD_H 1 + +/* Define to 1 if you have the `funlockfile' function. */ +#define HAVE_FUNLOCKFILE 1 + +/* Define to 1 if you have the `getdelim' function. */ +#define HAVE_GETDELIM 1 + +/* Define to 1 if you have the `getextmntent' function. */ +/* #undef HAVE_GETEXTMNTENT */ + +/* Define to 1 if you have the header file. */ +#define HAVE_GETOPT_H 1 + +/* Define to 1 if you have the `getopt_long_only' function. */ +#define HAVE_GETOPT_LONG_ONLY 1 + +/* Define if getrawpartition() in -lutil can be used */ +/* #undef HAVE_GETRAWPARTITION */ + +/* Define if the GNU gettext() function is already present or preinstalled. */ +#define HAVE_GETTEXT 1 + +/* Define if you have the iconv() function and it works. */ +/* #undef HAVE_ICONV */ + +/* Define if you have the 'intmax_t' type in or . */ +#define HAVE_INTMAX_T 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define if exists, doesn't clash with , and + declares uintmax_t. */ +#define HAVE_INTTYPES_H_WITH_UINTMAX 1 + +/* Define to 1 if you have the `isblank' function. */ +#define HAVE_ISBLANK 1 + +/* Define to 1 if you have the `iswblank' function. */ +#define HAVE_ISWBLANK 1 + +/* Define to 1 if you have the `iswcntrl' function. */ +#define HAVE_ISWCNTRL 1 + +/* Define to 1 if you have the `iswctype' function. */ +#define HAVE_ISWCTYPE 1 + +/* Define if you have and nl_langinfo(CODESET). */ +#define HAVE_LANGINFO_CODESET 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LANGINFO_H 1 + +/* Define to 1 if you have the `devmapper' library (-ldevmapper). */ +#define HAVE_LIBDEVMAPPER 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LIBINTL_H */ + +/* Define to 1 if you have the LZMA library. */ +#define HAVE_LIBLZMA 1 + +/* Define to 1 if you have the NVPAIR library. */ +/* #undef HAVE_LIBNVPAIR */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LIBNVPAIR_H */ + +/* Define to 1 if you have the ZFS library. */ +/* #undef HAVE_LIBZFS */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LIBZFS_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINEWRAP_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_LOCALE_H 1 + +/* Define to 1 if the system has the type `long long int'. */ +#define HAVE_LONG_LONG_INT 1 + +/* Define to 1 if your system has a GNU libc compatible 'malloc' function, and + to 0 otherwise. */ +#define HAVE_MALLOC_GNU 1 + +/* Define if the 'malloc' function is POSIX compliant. */ +#define HAVE_MALLOC_POSIX 1 + +/* Define to 1 if mmap()'s MAP_ANONYMOUS flag is available after including + config.h and . */ +#define HAVE_MAP_ANONYMOUS 1 + +/* Define to 1 if you have the `mbrtowc' function. */ +#define HAVE_MBRTOWC 1 + +/* Define to 1 if you have the `mbsinit' function. */ +#define HAVE_MBSINIT 1 + +/* Define to 1 if you have the `mbsrtowcs' function. */ +#define HAVE_MBSRTOWCS 1 + +/* Define to 1 if declares mbstate_t. */ +#define HAVE_MBSTATE_T 1 + +/* Define to 1 if you have the `memalign' function. */ +#define HAVE_MEMALIGN 1 + +/* Define to 1 if you have the `memchr' function. */ +#define HAVE_MEMCHR 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `mempcpy' function. */ +#define HAVE_MEMPCPY 1 + +/* Define to 1 if you have the `mprotect' function. */ +#define HAVE_MPROTECT 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NCURSES_CURSES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NCURSES_H */ + +/* Define to 1 if you have the `nl_langinfo' function. */ +#define HAVE_NL_LANGINFO 1 + +/* Define if opendisk() in -lutil can be used */ +/* #undef HAVE_OPENDISK */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_PCI_PCI_H */ + +/* Define to 1 if you have the `posix_memalign' function. */ +#define HAVE_POSIX_MEMALIGN 1 + +/* Define if program_invocation_name is defined */ +#define HAVE_PROGRAM_INVOCATION_NAME 1 + +/* Define if program_invocation_short_name is defined */ +#define HAVE_PROGRAM_INVOCATION_SHORT_NAME 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_RANDOM_H */ + +/* Define to 1 if you have the `rawmemchr' function. */ +#define HAVE_RAWMEMCHR 1 + +/* Define to 1 if atoll is declared even after undefining macros. */ +#define HAVE_RAW_DECL_ATOLL 1 + +/* Define to 1 if btowc is declared even after undefining macros. */ +#define HAVE_RAW_DECL_BTOWC 1 + +/* Define to 1 if canonicalize_file_name is declared even after undefining + macros. */ +#define HAVE_RAW_DECL_CANONICALIZE_FILE_NAME 1 + +/* Define to 1 if chown is declared even after undefining macros. */ +#define HAVE_RAW_DECL_CHOWN 1 + +/* Define to 1 if dprintf is declared even after undefining macros. */ +#define HAVE_RAW_DECL_DPRINTF 1 + +/* Define to 1 if dup2 is declared even after undefining macros. */ +#define HAVE_RAW_DECL_DUP2 1 + +/* Define to 1 if dup3 is declared even after undefining macros. */ +#define HAVE_RAW_DECL_DUP3 1 + +/* Define to 1 if endusershell is declared even after undefining macros. */ +#define HAVE_RAW_DECL_ENDUSERSHELL 1 + +/* Define to 1 if environ is declared even after undefining macros. */ +#define HAVE_RAW_DECL_ENVIRON 1 + +/* Define to 1 if euidaccess is declared even after undefining macros. */ +#define HAVE_RAW_DECL_EUIDACCESS 1 + +/* Define to 1 if faccessat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FACCESSAT 1 + +/* Define to 1 if fchdir is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FCHDIR 1 + +/* Define to 1 if fchownat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FCHOWNAT 1 + +/* Define to 1 if fpurge is declared even after undefining macros. */ +/* #undef HAVE_RAW_DECL_FPURGE */ + +/* Define to 1 if fseeko is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FSEEKO 1 + +/* Define to 1 if fsync is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FSYNC 1 + +/* Define to 1 if ftello is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FTELLO 1 + +/* Define to 1 if ftruncate is declared even after undefining macros. */ +#define HAVE_RAW_DECL_FTRUNCATE 1 + +/* Define to 1 if getcwd is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETCWD 1 + +/* Define to 1 if getdelim is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETDELIM 1 + +/* Define to 1 if getdomainname is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETDOMAINNAME 1 + +/* Define to 1 if getdtablesize is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETDTABLESIZE 1 + +/* Define to 1 if getgroups is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETGROUPS 1 + +/* Define to 1 if gethostname is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETHOSTNAME 1 + +/* Define to 1 if getline is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETLINE 1 + +/* Define to 1 if getloadavg is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETLOADAVG 1 + +/* Define to 1 if getlogin is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETLOGIN 1 + +/* Define to 1 if getlogin_r is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETLOGIN_R 1 + +/* Define to 1 if getpagesize is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETPAGESIZE 1 + +/* Define to 1 if getsubopt is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETSUBOPT 1 + +/* Define to 1 if getusershell is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GETUSERSHELL 1 + +/* Define to 1 if grantpt is declared even after undefining macros. */ +#define HAVE_RAW_DECL_GRANTPT 1 + +/* Define to 1 if initstat_r is declared even after undefining macros. */ +/* #undef HAVE_RAW_DECL_INITSTAT_R */ + +/* Define to 1 if lchown is declared even after undefining macros. */ +#define HAVE_RAW_DECL_LCHOWN 1 + +/* Define to 1 if link is declared even after undefining macros. */ +#define HAVE_RAW_DECL_LINK 1 + +/* Define to 1 if linkat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_LINKAT 1 + +/* Define to 1 if lseek is declared even after undefining macros. */ +#define HAVE_RAW_DECL_LSEEK 1 + +/* Define to 1 if mbrlen is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MBRLEN 1 + +/* Define to 1 if mbrtowc is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MBRTOWC 1 + +/* Define to 1 if mbsinit is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MBSINIT 1 + +/* Define to 1 if mbsnrtowcs is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MBSNRTOWCS 1 + +/* Define to 1 if mbsrtowcs is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MBSRTOWCS 1 + +/* Define to 1 if memmem is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MEMMEM 1 + +/* Define to 1 if mempcpy is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MEMPCPY 1 + +/* Define to 1 if memrchr is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MEMRCHR 1 + +/* Define to 1 if mkdtemp is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MKDTEMP 1 + +/* Define to 1 if mkostemp is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MKOSTEMP 1 + +/* Define to 1 if mkostemps is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MKOSTEMPS 1 + +/* Define to 1 if mkstemp is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MKSTEMP 1 + +/* Define to 1 if mkstemps is declared even after undefining macros. */ +#define HAVE_RAW_DECL_MKSTEMPS 1 + +/* Define to 1 if nl_langinfo is declared even after undefining macros. */ +#define HAVE_RAW_DECL_NL_LANGINFO 1 + +/* Define to 1 if pipe2 is declared even after undefining macros. */ +#define HAVE_RAW_DECL_PIPE2 1 + +/* Define to 1 if popen is declared even after undefining macros. */ +#define HAVE_RAW_DECL_POPEN 1 + +/* Define to 1 if pread is declared even after undefining macros. */ +#define HAVE_RAW_DECL_PREAD 1 + +/* Define to 1 if ptsname is declared even after undefining macros. */ +#define HAVE_RAW_DECL_PTSNAME 1 + +/* Define to 1 if pwrite is declared even after undefining macros. */ +#define HAVE_RAW_DECL_PWRITE 1 + +/* Define to 1 if random_r is declared even after undefining macros. */ +#define HAVE_RAW_DECL_RANDOM_R 1 + +/* Define to 1 if rawmemchr is declared even after undefining macros. */ +#define HAVE_RAW_DECL_RAWMEMCHR 1 + +/* Define to 1 if readlink is declared even after undefining macros. */ +#define HAVE_RAW_DECL_READLINK 1 + +/* Define to 1 if readlinkat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_READLINKAT 1 + +/* Define to 1 if realpath is declared even after undefining macros. */ +#define HAVE_RAW_DECL_REALPATH 1 + +/* Define to 1 if renameat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_RENAMEAT 1 + +/* Define to 1 if rmdir is declared even after undefining macros. */ +#define HAVE_RAW_DECL_RMDIR 1 + +/* Define to 1 if rpmatch is declared even after undefining macros. */ +#define HAVE_RAW_DECL_RPMATCH 1 + +/* Define to 1 if setenv is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SETENV 1 + +/* Define to 1 if setstate_r is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SETSTATE_R 1 + +/* Define to 1 if setusershell is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SETUSERSHELL 1 + +/* Define to 1 if sleep is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SLEEP 1 + +/* Define to 1 if snprintf is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SNPRINTF 1 + +/* Define to 1 if srandom_r is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SRANDOM_R 1 + +/* Define to 1 if stpcpy is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STPCPY 1 + +/* Define to 1 if stpncpy is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STPNCPY 1 + +/* Define to 1 if strcasecmp is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRCASECMP 1 + +/* Define to 1 if strcasestr is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRCASESTR 1 + +/* Define to 1 if strchrnul is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRCHRNUL 1 + +/* Define to 1 if strdup is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRDUP 1 + +/* Define to 1 if strncasecmp is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRNCASECMP 1 + +/* Define to 1 if strncat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRNCAT 1 + +/* Define to 1 if strndup is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRNDUP 1 + +/* Define to 1 if strnlen is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRNLEN 1 + +/* Define to 1 if strpbrk is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRPBRK 1 + +/* Define to 1 if strsep is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRSEP 1 + +/* Define to 1 if strsignal is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRSIGNAL 1 + +/* Define to 1 if strtod is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRTOD 1 + +/* Define to 1 if strtok_r is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRTOK_R 1 + +/* Define to 1 if strtoll is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRTOLL 1 + +/* Define to 1 if strtoull is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRTOULL 1 + +/* Define to 1 if strverscmp is declared even after undefining macros. */ +#define HAVE_RAW_DECL_STRVERSCMP 1 + +/* Define to 1 if symlink is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SYMLINK 1 + +/* Define to 1 if symlinkat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_SYMLINKAT 1 + +/* Define to 1 if tmpfile is declared even after undefining macros. */ +#define HAVE_RAW_DECL_TMPFILE 1 + +/* Define to 1 if ttyname_r is declared even after undefining macros. */ +#define HAVE_RAW_DECL_TTYNAME_R 1 + +/* Define to 1 if unlink is declared even after undefining macros. */ +#define HAVE_RAW_DECL_UNLINK 1 + +/* Define to 1 if unlinkat is declared even after undefining macros. */ +#define HAVE_RAW_DECL_UNLINKAT 1 + +/* Define to 1 if unlockpt is declared even after undefining macros. */ +#define HAVE_RAW_DECL_UNLOCKPT 1 + +/* Define to 1 if unsetenv is declared even after undefining macros. */ +#define HAVE_RAW_DECL_UNSETENV 1 + +/* Define to 1 if usleep is declared even after undefining macros. */ +#define HAVE_RAW_DECL_USLEEP 1 + +/* Define to 1 if vdprintf is declared even after undefining macros. */ +#define HAVE_RAW_DECL_VDPRINTF 1 + +/* Define to 1 if vsnprintf is declared even after undefining macros. */ +#define HAVE_RAW_DECL_VSNPRINTF 1 + +/* Define to 1 if wcrtomb is declared even after undefining macros. */ +#define HAVE_RAW_DECL_WCRTOMB 1 + +/* Define to 1 if wcsnrtombs is declared even after undefining macros. */ +#define HAVE_RAW_DECL_WCSNRTOMBS 1 + +/* Define to 1 if wcsrtombs is declared even after undefining macros. */ +#define HAVE_RAW_DECL_WCSRTOMBS 1 + +/* Define to 1 if wctob is declared even after undefining macros. */ +#define HAVE_RAW_DECL_WCTOB 1 + +/* Define to 1 if wcwidth is declared even after undefining macros. */ +#define HAVE_RAW_DECL_WCWIDTH 1 + +/* Define to 1 if _Exit is declared even after undefining macros. */ +#define HAVE_RAW_DECL__EXIT 1 + +/* Define if the 'realloc' function is POSIX compliant. */ +#define HAVE_REALLOC_POSIX 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SDL_SDL_H */ + +/* Define to 1 if 'sig_atomic_t' is a signed integer type. */ +/* #undef HAVE_SIGNED_SIG_ATOMIC_T */ + +/* Define to 1 if 'wchar_t' is a signed integer type. */ +/* #undef HAVE_SIGNED_WCHAR_T */ + +/* Define to 1 if 'wint_t' is a signed integer type. */ +/* #undef HAVE_SIGNED_WINT_T */ + +/* Define to 1 if you have the `sleep' function. */ +#define HAVE_SLEEP 1 + +/* Define to 1 if you have the `snprintf' function. */ +#define HAVE_SNPRINTF 1 + +/* Define if the return value of the snprintf function is the number of of + bytes (excluding the terminating NUL) that would have been produced if the + buffer had been large enough. */ +#define HAVE_SNPRINTF_RETVAL_C99 1 + +/* Define to 1 if stdbool.h conforms to C99. */ +#define HAVE_STDBOOL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDDEF_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define if exists, doesn't clash with , and declares + uintmax_t. */ +#define HAVE_STDINT_H_WITH_UINTMAX 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDIO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the `strcasecmp' function. */ +#define HAVE_STRCASECMP 1 + +/* Define to 1 if you have the `strchrnul' function. */ +#define HAVE_STRCHRNUL 1 + +/* Define to 1 if you have the `strerror_r' function. */ +#define HAVE_STRERROR_R 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the `strncasecmp' function. */ +#define HAVE_STRNCASECMP 1 + +/* Define to 1 if you have the `strndup' function. */ +#define HAVE_STRNDUP 1 + +/* Define to 1 if you have the `strnlen' function. */ +#define HAVE_STRNLEN 1 + +/* Define to 1 if the system has the type `struct random_data'. */ +#define HAVE_STRUCT_RANDOM_DATA 1 + +/* Define to 1 if `f_fstypename' is a member of `struct statfs'. */ +/* #undef HAVE_STRUCT_STATFS_F_FSTYPENAME */ + +/* Define to 1 if `f_mntfromname' is a member of `struct statfs'. */ +/* #undef HAVE_STRUCT_STATFS_F_MNTFROMNAME */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYSEXITS_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_BITYPES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_INTTYPES_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_MKDEV_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_MMAN_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_MNTTAB_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_MOUNT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_PARAM_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_WAIT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if the system has the type `unsigned long long int'. */ +#define HAVE_UNSIGNED_LONG_LONG_INT 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_USB_H */ + +/* Define to 1 if you have the `vasnprintf' function. */ +/* #undef HAVE_VASNPRINTF */ + +/* Define to 1 if you have the `vasprintf' function. */ +#define HAVE_VASPRINTF 1 + +/* Define to 1 if you have the `vsnprintf' function. */ +#define HAVE_VSNPRINTF 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_WCHAR_H 1 + +/* Define if you have the 'wchar_t' type. */ +#define HAVE_WCHAR_T 1 + +/* Define to 1 if you have the `wcrtomb' function. */ +#define HAVE_WCRTOMB 1 + +/* Define to 1 if you have the `wcscoll' function. */ +#define HAVE_WCSCOLL 1 + +/* Define to 1 if you have the `wcslen' function. */ +#define HAVE_WCSLEN 1 + +/* Define to 1 if you have the `wcsnlen' function. */ +#define HAVE_WCSNLEN 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_WCTYPE_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_WINSOCK2_H */ + +/* Define if you have the 'wint_t' type. */ +#define HAVE_WINT_T 1 + +/* Define to 1 if you have the `wmemchr' function. */ +#define HAVE_WMEMCHR 1 + +/* Define to 1 if you have the `wmemcpy' function. */ +#define HAVE_WMEMCPY 1 + +/* Define to 1 if you have the `wmempcpy' function. */ +#define HAVE_WMEMPCPY 1 + +/* Define to 1 if O_NOATIME works. */ +#define HAVE_WORKING_O_NOATIME 1 + +/* Define to 1 if O_NOFOLLOW works. */ +#define HAVE_WORKING_O_NOFOLLOW 1 + +/* Define to 1 if the system has the type `_Bool'. */ +#define HAVE__BOOL 1 + +/* Define to 1 if you have the `_restgpr_14_x' function. */ +/* #undef HAVE__RESTGPR_14_X */ + +/* Define to 1 if you have the `__ashldi3' function. */ +/* #undef HAVE___ASHLDI3 */ + +/* Define to 1 if you have the `__ashrdi3' function. */ +/* #undef HAVE___ASHRDI3 */ + +/* Define to 1 if you have the `__bswapdi2' function. */ +/* #undef HAVE___BSWAPDI2 */ + +/* Define to 1 if you have the `__bswapsi2' function. */ +/* #undef HAVE___BSWAPSI2 */ + +/* Define to 1 if you have the `__lshrdi3' function. */ +/* #undef HAVE___LSHRDI3 */ + +/* Define to 1 if you have the `__trampoline_setup' function. */ +/* #undef HAVE___TRAMPOLINE_SETUP */ + +/* Define to 1 if you have the `__ucmpdi2' function. */ +/* #undef HAVE___UCMPDI2 */ + +#if FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR +# define ISSLASH(C) ((C) == '/' || (C) == '\\') +#else +# define ISSLASH(C) ((C) == '/') +#endif + +/* Define to a substitute value for mmap()'s MAP_ANONYMOUS flag. */ +/* #undef MAP_ANONYMOUS */ + +/* Define if the mbrtowc function has the NULL string argument bug. */ +/* #undef MBRTOWC_NULL_ARG_BUG */ + +/* Define if the mbrtowc function does not return 0 for a NUL character. */ +/* #undef MBRTOWC_NUL_RETVAL_BUG */ + +/* Define if the mbrtowc function returns a wrong return value. */ +/* #undef MBRTOWC_RETVAL_BUG */ + +/* Define to 1 if you enable memory manager debugging. */ +/* #undef MM_DEBUG */ + +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ +/* #undef NO_MINUS_C_MINUS_O */ + +/* Name of package */ +#define PACKAGE "grub" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "bug-grub@gnu.org" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "GRUB" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "GRUB 1.99~beta0" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "grub" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "1.99~beta0" + +/* Define to l, ll, u, ul, ull, etc., as suitable for constants of type + 'ptrdiff_t'. */ +/* #undef PTRDIFF_T_SUFFIX */ + +/* Define if nl_langinfo exists but is overridden by gnulib. */ +/* #undef REPLACE_NL_LANGINFO */ + +/* Define this to 1 if strerror is broken. */ +/* #undef REPLACE_STRERROR */ + +/* Define if vasnprintf exists but is overridden by gnulib. */ +/* #undef REPLACE_VASNPRINTF */ + +/* Define to l, ll, u, ul, ull, etc., as suitable for constants of type + 'sig_atomic_t'. */ +/* #undef SIG_ATOMIC_T_SUFFIX */ + +/* The size of `long', as computed by sizeof. */ +#define SIZEOF_LONG 4 + +/* The size of `void *', as computed by sizeof. */ +#define SIZEOF_VOID_P 4 + +/* Define as the maximum value of type 'size_t', if the system doesn't define + it. */ +#ifndef SIZE_MAX +/* # undef SIZE_MAX */ +#endif + +/* Define to l, ll, u, ul, ull, etc., as suitable for constants of type + 'size_t'. */ +/* #undef SIZE_T_SUFFIX */ + +/* If using the C implementation of alloca, define if you know the + direction of stack growth for your system; otherwise it will be + automatically deduced at runtime. + STACK_DIRECTION > 0 => grows toward higher addresses + STACK_DIRECTION < 0 => grows toward lower addresses + STACK_DIRECTION = 0 => direction of growth unknown */ +/* #undef STACK_DIRECTION */ + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Define to 1 if strerror_r returns char *. */ +#define STRERROR_R_CHAR_P 1 + +/* Define to the prefix of C symbols at the assembler and linker level, either + an underscore or empty. */ +#define USER_LABEL_PREFIX + +/* Version number of package */ +#define VERSION "1.99~beta0" + +/* Define to l, ll, u, ul, ull, etc., as suitable for constants of type + 'wchar_t'. */ +/* #undef WCHAR_T_SUFFIX */ + +/* Define to l, ll, u, ul, ull, etc., as suitable for constants of type + 'wint_t'. */ +/* #undef WINT_T_SUFFIX */ + +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#if defined AC_APPLE_UNIVERSAL_BUILD +# if defined __BIG_ENDIAN__ +# define WORDS_BIGENDIAN 1 +# endif +#else +# ifndef WORDS_BIGENDIAN +/* # undef WORDS_BIGENDIAN */ +# endif +#endif + +/* Define to 1 if `lex' declares `yytext' as a `char *' by default, not a + `char[]'. */ +#define YYTEXT_POINTER 1 + +/* Number of bits in a file offset, on hosts where this is settable. */ +#define _FILE_OFFSET_BITS 64 + +/* Define for large files, on AIX-style hosts. */ +/* #undef _LARGE_FILES */ + +/* Define to 1 if on MINIX. */ +/* #undef _MINIX */ + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +/* #undef _POSIX_1_SOURCE */ + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +/* #undef _POSIX_SOURCE */ + +/* Define if you want regoff_t to be at least as wide POSIX requires. */ +/* #undef _REGEX_LARGE_OFFSETS */ + +/* Define to 500 only on HP-UX. */ +/* #undef _XOPEN_SOURCE */ + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# define _ALL_SOURCE 1 +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# define _POSIX_PTHREAD_SEMANTICS 1 +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# define _TANDEM_SOURCE 1 +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# define __EXTENSIONS__ 1 +#endif + + +/* Define to rpl_ if the getopt replacement functions and variables should be + used. */ +#define __GETOPT_PREFIX rpl_ + +/* Define to a replacement function name for fnmatch(). */ +/* #undef fnmatch */ + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +/* #undef inline */ +#endif + +/* Define to long or long long if and don't define. */ +/* #undef intmax_t */ + +/* Work around a bug in Apple GCC 4.0.1 build 5465: In C99 mode, it supports + the ISO C 99 semantics of 'extern inline' (unlike the GNU C semantics of + earlier versions), but does not display it by setting __GNUC_STDC_INLINE__. + __APPLE__ && __MACH__ test for MacOS X. + __APPLE_CC__ tests for the Apple compiler and its version. + __STDC_VERSION__ tests for the C99 mode. */ +#if defined __APPLE__ && defined __MACH__ && __APPLE_CC__ >= 5465 && !defined __cplusplus && __STDC_VERSION__ >= 199901L && !defined __GNUC_STDC_INLINE__ +# define __GNUC_STDC_INLINE__ 1 +#endif + +/* Define to a type if does not define. */ +/* #undef mbstate_t */ + +/* Define as the type of the result of subtracting two pointers, if the system + doesn't define it. */ +/* #undef ptrdiff_t */ + +/* Define to rpl_re_comp if the replacement should be used. */ +/* #undef re_comp */ + +/* Define to rpl_re_compile_fastmap if the replacement should be used. */ +/* #undef re_compile_fastmap */ + +/* Define to rpl_re_compile_pattern if the replacement should be used. */ +/* #undef re_compile_pattern */ + +/* Define to rpl_re_exec if the replacement should be used. */ +/* #undef re_exec */ + +/* Define to rpl_re_match if the replacement should be used. */ +/* #undef re_match */ + +/* Define to rpl_re_match_2 if the replacement should be used. */ +/* #undef re_match_2 */ + +/* Define to rpl_re_search if the replacement should be used. */ +/* #undef re_search */ + +/* Define to rpl_re_search_2 if the replacement should be used. */ +/* #undef re_search_2 */ + +/* Define to rpl_re_set_registers if the replacement should be used. */ +/* #undef re_set_registers */ + +/* Define to rpl_re_set_syntax if the replacement should be used. */ +/* #undef re_set_syntax */ + +/* Define to rpl_re_syntax_options if the replacement should be used. */ +/* #undef re_syntax_options */ + +/* Define to rpl_regcomp if the replacement should be used. */ +/* #undef regcomp */ + +/* Define to rpl_regerror if the replacement should be used. */ +/* #undef regerror */ + +/* Define to rpl_regexec if the replacement should be used. */ +/* #undef regexec */ + +/* Define to rpl_regfree if the replacement should be used. */ +/* #undef regfree */ + +/* Define to the equivalent of the C99 'restrict' keyword, or to + nothing if this is not supported. Do not define if restrict is + supported directly. */ +#define restrict __restrict +/* Work around a bug in Sun C++: it does not support _Restrict or + __restrict__, even though the corresponding Sun C compiler ends up with + "#define restrict _Restrict" or "#define restrict __restrict__" in the + previous line. Perhaps some future version of Sun C++ will work with + restrict; if so, hopefully it defines __RESTRICT like Sun C does. */ +#if defined __SUNPRO_CC && !defined __RESTRICT +# define _Restrict +# define __restrict__ +#endif + +/* Define to `unsigned int' if does not define. */ +/* #undef size_t */ + +/* Define as a signed type of the same size as size_t. */ +/* #undef ssize_t */ + +/* Define as a marker that can be attached to declarations that might not + be used. This helps to reduce warnings, such as from + GCC -Wunused-parameter. */ +#if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) +# define _GL_UNUSED __attribute__ ((__unused__)) +#else +# define _GL_UNUSED +#endif +/* The name _UNUSED_PARAMETER_ is an earlier spelling, although the name + is a misnomer outside of parameter lists. */ +#define _UNUSED_PARAMETER_ _GL_UNUSED + diff --git a/libr/fs/p/grub/include/config.h b/libr/fs/p/grub/include/config.h new file mode 100644 index 0000000000..2d77f0151a --- /dev/null +++ b/libr/fs/p/grub/include/config.h @@ -0,0 +1,22 @@ +#ifndef _FOO_ +#define _FOO_ + +#include +#define GRUB_UTIL +#define SIZEOF_VOID_P 4 +#define SIZEOF_LONG 4 +#define GRUB_FILE "/tmp/" +#define NESTED_FUNC_ATTR +#undef WORDS_BIGENDIAN +//#define Elf_Sym void +#define grub_cpu_idle() // +#define grub_dl_ref(x) // +#define grub_dl_unref(x) // +#define grub_dl_unload_unneeded(x) // +#define grub_malloc(x) malloc(x) +#define grub_realloc(x,y) realloc(x,y) +#define grub_free(x) free(x) +#define grub_zalloc(x) calloc(1,x) + +#include +#endif diff --git a/libr/fs/p/grub/include/grub/.fs.h.swo b/libr/fs/p/grub/include/grub/.fs.h.swo new file mode 100644 index 0000000000..c034e5c33d Binary files /dev/null and b/libr/fs/p/grub/include/grub/.fs.h.swo differ diff --git a/libr/fs/p/grub/include/grub/aout.h b/libr/fs/p/grub/include/grub/aout.h new file mode 100644 index 0000000000..10d7dde61e --- /dev/null +++ b/libr/fs/p/grub/include/grub/aout.h @@ -0,0 +1,128 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)exec.h 8.1 (Berkeley) 6/11/93 + * $FreeBSD$ + */ + +#ifndef GRUB_AOUT_HEADER +#define GRUB_AOUT_HEADER 1 + +#include + +struct grub_aout32_header +{ + grub_uint32_t a_midmag; /* htonl(flags<<26 | mid<<16 | magic) */ + grub_uint32_t a_text; /* text segment size */ + grub_uint32_t a_data; /* initialized data size */ + grub_uint32_t a_bss; /* uninitialized data size */ + grub_uint32_t a_syms; /* symbol table size */ + grub_uint32_t a_entry; /* entry point */ + grub_uint32_t a_trsize; /* text relocation size */ + grub_uint32_t a_drsize; /* data relocation size */ +}; + +struct grub_aout64_header +{ + grub_uint32_t a_midmag; /* htonl(flags<<26 | mid<<16 | magic) */ + grub_uint64_t a_text; /* text segment size */ + grub_uint64_t a_data; /* initialized data size */ + grub_uint64_t a_bss; /* uninitialized data size */ + grub_uint64_t a_syms; /* symbol table size */ + grub_uint64_t a_entry; /* entry point */ + grub_uint64_t a_trsize; /* text relocation size */ + grub_uint64_t a_drsize; /* data relocation size */ +}; + +union grub_aout_header +{ + struct grub_aout32_header aout32; + struct grub_aout64_header aout64; +}; + +#define AOUT_TYPE_NONE 0 +#define AOUT_TYPE_AOUT32 1 +#define AOUT_TYPE_AOUT64 6 + +#define AOUT32_OMAGIC 0x107 /* 0407 old impure format */ +#define AOUT32_NMAGIC 0x108 /* 0410 read-only text */ +#define AOUT32_ZMAGIC 0x10b /* 0413 demand load format */ +#define AOUT32_QMAGIC 0xcc /* 0314 "compact" demand load format */ + +#define AOUT64_OMAGIC 0x1001 +#define AOUT64_ZMAGIC 0x1002 +#define AOUT64_NMAGIC 0x1003 + +#define AOUT_MID_ZERO 0 /* unknown - implementation dependent */ +#define AOUT_MID_SUN010 1 /* sun 68010/68020 binary */ +#define AOUT_MID_SUN020 2 /* sun 68020-only binary */ +#define AOUT_MID_I386 134 /* i386 BSD binary */ +#define AOUT_MID_SPARC 138 /* sparc */ +#define AOUT_MID_HP200 200 /* hp200 (68010) BSD binary */ +#define AOUT_MID_SUN 0x103 +#define AOUT_MID_HP300 300 /* hp300 (68020+68881) BSD binary */ +#define AOUT_MID_HPUX 0x20C /* hp200/300 HP-UX binary */ +#define AOUT_MID_HPUX800 0x20B /* hp800 HP-UX binary */ + +#define AOUT_FLAG_PIC 0x10 /* contains position independent code */ +#define AOUT_FLAG_DYNAMIC 0x20 /* contains run-time link-edit info */ +#define AOUT_FLAG_DPMASK 0x30 /* mask for the above */ + +#define AOUT_GETMAGIC(header) ((header).a_midmag & 0xffff) +#define AOUT_GETMID(header) ((header).a_midmag >> 16) & 0x03ff) +#define AOUT_GETFLAG(header) ((header).a_midmag >> 26) & 0x3f) + +#ifndef GRUB_UTIL + +int EXPORT_FUNC(grub_aout_get_type) (union grub_aout_header *header); + +grub_err_t EXPORT_FUNC(grub_aout_load) (grub_file_t file, int offset, + void *load_addr, int load_size, + grub_size_t bss_size); + +#endif + +#endif /* ! GRUB_AOUT_HEADER */ diff --git a/libr/fs/p/grub/include/grub/bsdlabel.h b/libr/fs/p/grub/include/grub/bsdlabel.h new file mode 100644 index 0000000000..636bd41a18 --- /dev/null +++ b/libr/fs/p/grub/include/grub/bsdlabel.h @@ -0,0 +1,91 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2004,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_BSDLABEL_PARTITION_HEADER +#define GRUB_BSDLABEL_PARTITION_HEADER 1 + +/* Constants for BSD disk label. */ +#define GRUB_PC_PARTITION_BSD_LABEL_SECTOR 1 +#define GRUB_PC_PARTITION_BSD_LABEL_MAGIC 0x82564557 + +/* BSD partition types. */ +#define GRUB_PC_PARTITION_BSD_TYPE_UNUSED 0 +#define GRUB_PC_PARTITION_BSD_TYPE_SWAP 1 +#define GRUB_PC_PARTITION_BSD_TYPE_V6 2 +#define GRUB_PC_PARTITION_BSD_TYPE_V7 3 +#define GRUB_PC_PARTITION_BSD_TYPE_SYSV 4 +#define GRUB_PC_PARTITION_BSD_TYPE_V71K 5 +#define GRUB_PC_PARTITION_BSD_TYPE_V8 6 +#define GRUB_PC_PARTITION_BSD_TYPE_BSDFFS 7 +#define GRUB_PC_PARTITION_BSD_TYPE_MSDOS 8 +#define GRUB_PC_PARTITION_BSD_TYPE_BSDLFS 9 +#define GRUB_PC_PARTITION_BSD_TYPE_OTHER 10 +#define GRUB_PC_PARTITION_BSD_TYPE_HPFS 11 +#define GRUB_PC_PARTITION_BSD_TYPE_ISO9660 12 +#define GRUB_PC_PARTITION_BSD_TYPE_BOOT 13 + +/* FreeBSD-specific types. */ +#define GRUB_PC_PARTITION_FREEBSD_TYPE_VINUM 14 +#define GRUB_PC_PARTITION_FREEBSD_TYPE_RAID 15 +#define GRUB_PC_PARTITION_FREEBSD_TYPE_JFS2 21 + +/* NetBSD-specific types. */ +#define GRUB_PC_PARTITION_NETBSD_TYPE_ADOS 14 +#define GRUB_PC_PARTITION_NETBSD_TYPE_HFS 15 +#define GRUB_PC_PARTITION_NETBSD_TYPE_FILECORE 16 +#define GRUB_PC_PARTITION_NETBSD_TYPE_EXT2FS 17 +#define GRUB_PC_PARTITION_NETBSD_TYPE_NTFS 18 +#define GRUB_PC_PARTITION_NETBSD_TYPE_RAID 19 +#define GRUB_PC_PARTITION_NETBSD_TYPE_CCD 20 +#define GRUB_PC_PARTITION_NETBSD_TYPE_JFS2 21 +#define GRUB_PC_PARTITION_NETBSD_TYPE_APPLEUFS 22 + +/* OpenBSD-specific types. */ +#define GRUB_PC_PARTITION_OPENBSD_TYPE_ADOS 14 +#define GRUB_PC_PARTITION_OPENBSD_TYPE_HFS 15 +#define GRUB_PC_PARTITION_OPENBSD_TYPE_FILECORE 16 +#define GRUB_PC_PARTITION_OPENBSD_TYPE_EXT2FS 17 +#define GRUB_PC_PARTITION_OPENBSD_TYPE_NTFS 18 +#define GRUB_PC_PARTITION_OPENBSD_TYPE_RAID 19 + +#define GRUB_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION 2 + +/* The BSD partition entry. */ +struct grub_partition_bsd_entry +{ + grub_uint32_t size; + grub_uint32_t offset; + grub_uint32_t fragment_size; + grub_uint8_t fs_type; + grub_uint8_t fs_fragments; + grub_uint16_t fs_cylinders; +} __attribute__ ((packed)); + +/* The BSD disk label. Only define members useful for GRUB. */ +struct grub_partition_bsd_disk_label +{ + grub_uint32_t magic; + grub_uint8_t padding[128]; + grub_uint32_t magic2; + grub_uint16_t checksum; + grub_uint16_t num_partitions; + grub_uint32_t boot_size; + grub_uint32_t superblock_size; +} __attribute__ ((packed)); + +#endif /* ! GRUB_PC_PARTITION_HEADER */ diff --git a/libr/fs/p/grub/include/grub/bufio.h b/libr/fs/p/grub/include/grub/bufio.h new file mode 100644 index 0000000000..acdd0c882c --- /dev/null +++ b/libr/fs/p/grub/include/grub/bufio.h @@ -0,0 +1,28 @@ +/* bufio.h - prototypes for bufio */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_BUFIO_H +#define GRUB_BUFIO_H 1 + +#include + +grub_file_t EXPORT_FUNC (grub_bufio_open) (grub_file_t io, int size); +grub_file_t EXPORT_FUNC (grub_buffile_open) (const char *name, int size); + +#endif /* ! GRUB_BUFIO_H */ diff --git a/libr/fs/p/grub/include/grub/charset.h b/libr/fs/p/grub/include/grub/charset.h new file mode 100644 index 0000000000..c8247f78a0 --- /dev/null +++ b/libr/fs/p/grub/include/grub/charset.h @@ -0,0 +1,134 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_CHARSET_HEADER +#define GRUB_CHARSET_HEADER 1 + +#include + +#define GRUB_UINT8_1_LEADINGBIT 0x80 +#define GRUB_UINT8_2_LEADINGBITS 0xc0 +#define GRUB_UINT8_3_LEADINGBITS 0xe0 +#define GRUB_UINT8_4_LEADINGBITS 0xf0 +#define GRUB_UINT8_5_LEADINGBITS 0xf8 +#define GRUB_UINT8_6_LEADINGBITS 0xfc +#define GRUB_UINT8_7_LEADINGBITS 0xfe + +#define GRUB_UINT8_1_TRAILINGBIT 0x01 +#define GRUB_UINT8_2_TRAILINGBITS 0x03 +#define GRUB_UINT8_3_TRAILINGBITS 0x07 +#define GRUB_UINT8_4_TRAILINGBITS 0x0f +#define GRUB_UINT8_5_TRAILINGBITS 0x1f +#define GRUB_UINT8_6_TRAILINGBITS 0x3f + +#define GRUB_UCS2_LIMIT 0x10000 +#define GRUB_UTF16_UPPER_SURROGATE(code) \ + (0xD800 + ((((code) - GRUB_UCS2_LIMIT) >> 12) & 0xfff)) +#define GRUB_UTF16_LOWER_SURROGATE(code) \ + (0xDC00 + (((code) - GRUB_UCS2_LIMIT) & 0xfff)) + +grub_ssize_t +grub_utf8_to_utf16 (grub_uint16_t *dest, grub_size_t destsize, + const grub_uint8_t *src, grub_size_t srcsize, + const grub_uint8_t **srcend); + +/* Convert UTF-16 to UTF-8. */ +static inline grub_uint8_t * +grub_utf16_to_utf8 (grub_uint8_t *dest, grub_uint16_t *src, + grub_size_t size) +{ + grub_uint32_t code_high = 0; + + while (size--) + { + grub_uint32_t code = *src++; + + if (code_high) + { + if (code >= 0xDC00 && code <= 0xDFFF) + { + /* Surrogate pair. */ + code = ((code_high - 0xD800) << 12) + (code - 0xDC00) + 0x10000; + + *dest++ = (code >> 18) | 0xF0; + *dest++ = ((code >> 12) & 0x3F) | 0x80; + *dest++ = ((code >> 6) & 0x3F) | 0x80; + *dest++ = (code & 0x3F) | 0x80; + } + else + { + /* Error... */ + *dest++ = '?'; + } + + code_high = 0; + } + else + { + if (code <= 0x007F) + *dest++ = code; + else if (code <= 0x07FF) + { + *dest++ = (code >> 6) | 0xC0; + *dest++ = (code & 0x3F) | 0x80; + } + else if (code >= 0xD800 && code <= 0xDBFF) + { + code_high = code; + continue; + } + else if (code >= 0xDC00 && code <= 0xDFFF) + { + /* Error... */ + *dest++ = '?'; + } + else if (code < 0x10000) + { + *dest++ = (code >> 12) | 0xE0; + *dest++ = ((code >> 6) & 0x3F) | 0x80; + *dest++ = (code & 0x3F) | 0x80; + } + else + { + *dest++ = (code >> 18) | 0xF0; + *dest++ = ((code >> 12) & 0x3F) | 0x80; + *dest++ = ((code >> 6) & 0x3F) | 0x80; + *dest++ = (code & 0x3F) | 0x80; + } + } + } + + return dest; +} + +/* Convert UCS-4 to UTF-8. */ +char *grub_ucs4_to_utf8_alloc (grub_uint32_t *src, grub_size_t size); + +int +grub_is_valid_utf8 (const grub_uint8_t *src, grub_size_t srcsize); + +int grub_utf8_to_ucs4_alloc (const char *msg, grub_uint32_t **unicode_msg, + grub_uint32_t **last_position); +void +grub_ucs4_to_utf8 (grub_uint32_t *src, grub_size_t size, + grub_uint8_t *dest, grub_size_t destsize); +grub_size_t grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize, + const grub_uint8_t *src, grub_size_t srcsize, + const grub_uint8_t **srcend); + +#endif diff --git a/libr/fs/p/grub/include/grub/command.h b/libr/fs/p/grub/include/grub/command.h new file mode 100644 index 0000000000..19622752e2 --- /dev/null +++ b/libr/fs/p/grub/include/grub/command.h @@ -0,0 +1,123 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_COMMAND_HEADER +#define GRUB_COMMAND_HEADER 1 + +#include +#include +#include + +typedef enum grub_command_flags + { + /* This is an extended command. */ + GRUB_COMMAND_FLAG_EXTCMD = 0x10, + /* This is an dynamic command. */ + GRUB_COMMAND_FLAG_DYNCMD = 0x20, + /* This command accepts block arguments. */ + GRUB_COMMAND_FLAG_BLOCKS = 0x40, + /* This command accepts unknown arguments as direct parameters. */ + GRUB_COMMAND_ACCEPT_DASH = 0x80, + /* This command accepts only options preceding direct arguments. */ + GRUB_COMMAND_OPTIONS_AT_START = 0x100, + /* Can be executed in an entries extractor. */ + GRUB_COMMAND_FLAG_EXTRACTOR = 0x200 + } grub_command_flags_t; + +struct grub_command; + +typedef grub_err_t (*grub_command_func_t) (struct grub_command *cmd, + int argc, char **argv); + +/* The command description. */ +struct grub_command +{ + /* The next element. */ + struct grub_command *next; + + /* The name. */ + const char *name; + + /* The priority. */ + int prio; + + /* The callback function. */ + grub_command_func_t func; + + /* The flags. */ + grub_command_flags_t flags; + + /* The summary of the command usage. */ + const char *summary; + + /* The description of the command. */ + const char *description; + + /* Arbitrary data. */ + void *data; +}; +typedef struct grub_command *grub_command_t; + +extern grub_command_t EXPORT_VAR(grub_command_list); + +grub_command_t +EXPORT_FUNC(grub_register_command_prio) (const char *name, + grub_command_func_t func, + const char *summary, + const char *description, + int prio); +void EXPORT_FUNC(grub_unregister_command) (grub_command_t cmd); + +static inline grub_command_t +grub_register_command (const char *name, + grub_command_func_t func, + const char *summary, + const char *description) +{ + return grub_register_command_prio (name, func, summary, description, 0); +} + +static inline grub_command_t +grub_register_command_p1 (const char *name, + grub_command_func_t func, + const char *summary, + const char *description) +{ + return grub_register_command_prio (name, func, summary, description, 1); +} + +static inline grub_command_t +grub_command_find (const char *name) +{ + return grub_named_list_find (GRUB_AS_NAMED_LIST (grub_command_list), name); +} + +static inline grub_err_t +grub_command_execute (const char *name, int argc, char **argv) +{ + grub_command_t cmd; + + cmd = grub_command_find (name); + return (cmd) ? cmd->func (cmd, argc, argv) : GRUB_ERR_FILE_NOT_FOUND; +} + +#define FOR_COMMANDS(var) FOR_LIST_ELEMENTS((var), grub_command_list) + +void grub_register_core_commands (void); + +#endif /* ! GRUB_COMMAND_HEADER */ diff --git a/libr/fs/p/grub/include/grub/cpu/i386 b/libr/fs/p/grub/include/grub/cpu/i386 new file mode 120000 index 0000000000..d51455ffed --- /dev/null +++ b/libr/fs/p/grub/include/grub/cpu/i386 @@ -0,0 +1 @@ +../../include/grub/i386 \ No newline at end of file diff --git a/libr/fs/p/grub/include/grub/cpu/time.h b/libr/fs/p/grub/include/grub/cpu/time.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libr/fs/p/grub/include/grub/cpu/types.h b/libr/fs/p/grub/include/grub/cpu/types.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libr/fs/p/grub/include/grub/cs5536.h b/libr/fs/p/grub/include/grub/cs5536.h new file mode 100644 index 0000000000..cd17e11fcb --- /dev/null +++ b/libr/fs/p/grub/include/grub/cs5536.h @@ -0,0 +1,190 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_CS5536_HEADER +#define GRUB_CS5536_HEADER 1 + +#ifndef ASM_FILE +#include +#include +#include +#endif + +#define GRUB_CS5536_PCIID 0x208f1022 +#define GRUB_CS5536_MSR_MAILBOX_ADDR 0xf4 +#define GRUB_CS5536_MSR_MAILBOX_DATA0 0xf8 +#define GRUB_CS5536_MSR_MAILBOX_DATA1 0xfc +#define GRUB_CS5536_MSR_IRQ_MAP_BAR 0x80000008 +#define GRUB_CS5536_MSR_SMB_BAR 0x8000000b + +#define GRUB_CS5536_SMBUS_REGS_SIZE 8 +#define GRUB_CS5536_GPIO_REGS_SIZE 256 +#define GRUB_CS5536_MFGPT_REGS_SIZE 64 +#define GRUB_CS5536_IRQ_MAP_REGS_SIZE 32 +#define GRUB_CS5536_PM_REGS_SIZE 128 +#define GRUB_CS5536_ACPI_REGS_SIZE 32 + +#define GRUB_CS5536_USB_OPTION_REGS_SIZE 0x1c +#define GRUB_CS5536_USB_OPTION_REG_UOCMUX 1 +#define GRUB_CS5536_USB_OPTION_REG_UOCMUX_PMUX_MASK 0x03 +#define GRUB_CS5536_USB_OPTION_REG_UOCMUX_PMUX_HC 0x02 + +#define GRUB_CS5536_DESTINATION_GLIU 0 +#define GRUB_CS5536_DESTINATION_GLPCI_SB 1 +#define GRUB_CS5536_DESTINATION_USB 2 +#define GRUB_CS5536_DESTINATION_IDE 3 +#define GRUB_CS5536_DESTINATION_DD 4 +#define GRUB_CS5536_DESTINATION_ACC 5 +#define GRUB_CS5536_DESTINATION_GLCP 7 + +#define GRUB_CS5536_P2D_DEST_SHIFT 61 +#define GRUB_CS5536_P2D_LOG_ALIGN 12 +#define GRUB_CS5536_P2D_ALIGN (1 << GRUB_CS5536_P2D_LOG_ALIGN) +#define GRUB_CS5536_P2D_BASE_SHIFT 20 +#define GRUB_CS5536_P2D_MASK_SHIFT 0 + +#define GRUB_CS5536_MSR_GL_IOD_START 0x000100e0 +#define GRUB_CS5536_IOD_DEST_SHIFT 61 +#define GRUB_CS5536_IOD_BASE_SHIFT 20 +#define GRUB_CS5536_IOD_MASK_SHIFT 0 +#define GRUB_CS5536_IOD_ADDR_MASK 0xfffff + +#define GRUB_CS5536_MSR_GPIO_BAR 0x8000000c +#define GRUB_CS5536_MSR_MFGPT_BAR 0x8000000d +#define GRUB_CS5536_MSR_ACPI_BAR 0x8000000e +#define GRUB_CS5536_MSR_PM_BAR 0x8000000f +#define GRUB_CS5536_MSR_DIVIL_LEG_IO 0x80000014 +#define GRUB_CS5536_MSR_DIVIL_LEG_IO_RTC_ENABLE0 0x00000001 +#define GRUB_CS5536_MSR_DIVIL_LEG_IO_RTC_ENABLE1 0x00000002 +#define GRUB_CS5536_MSR_DIVIL_LEG_IO_MODE_X86 0x10000000 +#define GRUB_CS5536_MSR_DIVIL_LEG_IO_F_REMAP 0x04000000 +#define GRUB_CS5536_MSR_DIVIL_IRQ_MAPPER_PRIMARY_MASK 0x80000024 +#define GRUB_CS5536_MSR_DIVIL_IRQ_MAPPER_LPC_MASK 0x80000025 +#define GRUB_CS5536_DIVIL_LPC_INTERRUPTS 0x1002 +#define GRUB_CS5536_MSR_DIVIL_LPC_SERIAL_IRQ_CONTROL 0x8000004e +#define GRUB_CS5536_MSR_DIVIL_LPC_SERIAL_IRQ_CONTROL_ENABLE 0x80 + +#define GRUB_CS5536_MSR_USB_OHCI_BASE 0x40000008 +#define GRUB_CS5536_MSR_USB_EHCI_BASE 0x40000009 +#define GRUB_CS5536_MSR_USB_CONTROLLER_BASE 0x4000000a +#define GRUB_CS5536_MSR_USB_OPTION_CONTROLLER_BASE 0x4000000b +#define GRUB_CS5536_MSR_USB_BASE_ADDR_MASK 0x00ffffff00ULL +#define GRUB_CS5536_MSR_USB_BASE_BUS_MASTER 0x0400000000ULL +#define GRUB_CS5536_MSR_USB_BASE_MEMORY_ENABLE 0x0200000000ULL +#define GRUB_CS5536_MSR_USB_BASE_PME_ENABLED 0x0800000000ULL +#define GRUB_CS5536_MSR_USB_BASE_PME_STATUS 0x1000000000ULL +#define GRUB_CS5536_MSR_USB_EHCI_BASE_FLDJ_SHIFT 40 + +#define GRUB_CS5536_MSR_IDE_IO_BAR 0x60000008 +#define GRUB_CS5536_MSR_IDE_IO_BAR_UNITS 1 +#define GRUB_CS5536_MSR_IDE_IO_BAR_ADDR_MASK 0xfffffff0 +#define GRUB_CS5536_MSR_IDE_CFG 0x60000010 +#define GRUB_CS5536_MSR_IDE_CFG_CHANNEL_ENABLE 2 +#define GRUB_CS5536_MSR_IDE_TIMING 0x60000012 +#define GRUB_CS5536_MSR_IDE_TIMING_PIO0 0x98 +#define GRUB_CS5536_MSR_IDE_TIMING_DRIVE0_SHIFT 24 +#define GRUB_CS5536_MSR_IDE_TIMING_DRIVE1_SHIFT 16 +#define GRUB_CS5536_MSR_IDE_CAS_TIMING 0x60000013 +#define GRUB_CS5536_MSR_IDE_CAS_TIMING_CMD_PIO0 0x99 +#define GRUB_CS5536_MSR_IDE_CAS_TIMING_CMD_SHIFT 24 +#define GRUB_CS5536_MSR_IDE_CAS_TIMING_DRIVE0_SHIFT 6 +#define GRUB_CS5536_MSR_IDE_CAS_TIMING_DRIVE1_SHIFT 4 +#define GRUB_CS5536_MSR_IDE_CAS_TIMING_PIO0 2 + +#define GRUB_CS5536_MSR_GL_PCI_CTRL 0x00000010 +#define GRUB_CS5536_MSR_GL_PCI_CTRL_MEMORY_ENABLE 1 +#define GRUB_CS5536_MSR_GL_PCI_CTRL_IO_ENABLE 2 +#define GRUB_CS5536_MSR_GL_PCI_CTRL_LATENCY_SHIFT 35 +#define GRUB_CS5536_MSR_GL_PCI_CTRL_OUT_THR_SHIFT 60 +#define GRUB_CS5536_MSR_GL_PCI_CTRL_IN_THR_SHIFT 56 + +#define GRUB_CS5536_MSR_GL_REGIONS_START 0x00000020 +#define GRUB_CS5536_MSR_GL_REGIONS_NUM 16 +#define GRUB_CS5536_MSR_GL_REGION_ENABLE 1 +#define GRUB_CS5536_MSR_GL_REGION_IO 0x100000000ULL +#define GRUB_CS5536_MSR_GL_REGION_BASE_MASK 0xfffff000ULL +#define GRUB_CS5536_MSR_GL_REGION_IO_BASE_SHIFT 12 +#define GRUB_CS5536_MSR_GL_REGION_TOP_MASK 0xfffff00000000000ULL +#define GRUB_CS5536_MSR_GL_REGION_IO_TOP_SHIFT 44 + +#define GRUB_CS5536_MSR_GL_P2D_START 0x00010020 + +#define GRUB_CS5536_SMB_REG_DATA 0x0 +#define GRUB_CS5536_SMB_REG_STATUS 0x1 +#define GRUB_CS5536_SMB_REG_STATUS_SDAST (1 << 6) +#define GRUB_CS5536_SMB_REG_STATUS_BER (1 << 5) +#define GRUB_CS5536_SMB_REG_STATUS_NACK (1 << 4) +#define GRUB_CS5536_SMB_REG_CTRL1 0x3 +#define GRUB_CS5536_SMB_REG_CTRL1_START 0x01 +#define GRUB_CS5536_SMB_REG_CTRL1_STOP 0x02 +#define GRUB_CS5536_SMB_REG_CTRL1_ACK 0x10 +#define GRUB_CS5536_SMB_REG_ADDR 0x4 +#define GRUB_CS5536_SMB_REG_ADDR_MASTER 0x0 +#define GRUB_CS5536_SMB_REG_CTRL2 0x5 +#define GRUB_CS5536_SMB_REG_CTRL2_ENABLE 0x1 +#define GRUB_CS5536_SMB_REG_CTRL3 0x6 + +#ifdef ASM_FILE +#define GRUB_ULL(x) x +#else +#define GRUB_ULL(x) x ## ULL +#endif + +#define GRUB_CS5536_LBAR_ADDR_MASK GRUB_ULL (0x000000000000fff8) +#define GRUB_CS5536_LBAR_ENABLE GRUB_ULL (0x0000000100000000) +#define GRUB_CS5536_LBAR_MASK_MASK GRUB_ULL (0x0000f00000000000) +#define GRUB_CS5536_LBAR_TURN_ON (GRUB_CS5536_LBAR_ENABLE | GRUB_CS5536_LBAR_MASK_MASK) + +/* PMON-compatible LBARs. */ +#define GRUB_CS5536_LBAR_GPIO 0xb000 +#define GRUB_CS5536_LBAR_ACC 0xb200 +#define GRUB_CS5536_LBAR_PM 0xb280 +#define GRUB_CS5536_LBAR_MFGPT 0xb300 +#define GRUB_CS5536_LBAR_ACPI 0xb340 +#define GRUB_CS5536_LBAR_IRQ_MAP 0xb360 +#define GRUB_CS5536_LBAR_IDE 0xb380 +#define GRUB_CS5536_LBAR_SMBUS 0xb390 + +#define GRUB_GPIO_SMBUS_PINS ((1 << 14) | (1 << 15)) +#define GRUB_GPIO_REG_OUT_EN 0x4 +#define GRUB_GPIO_REG_OUT_AUX1 0x10 +#define GRUB_GPIO_REG_IN_EN 0x20 +#define GRUB_GPIO_REG_IN_AUX1 0x34 + +#ifndef ASM_FILE +int EXPORT_FUNC (grub_cs5536_find) (grub_pci_device_t *devp); + +grub_uint64_t EXPORT_FUNC (grub_cs5536_read_msr) (grub_pci_device_t dev, + grub_uint32_t addr); +void EXPORT_FUNC (grub_cs5536_write_msr) (grub_pci_device_t dev, + grub_uint32_t addr, + grub_uint64_t val); +grub_err_t grub_cs5536_read_spd_byte (grub_port_t smbbase, grub_uint8_t dev, + grub_uint8_t addr, grub_uint8_t *res); +grub_err_t EXPORT_FUNC (grub_cs5536_read_spd) (grub_port_t smbbase, + grub_uint8_t dev, + struct grub_smbus_spd *res); +grub_err_t grub_cs5536_smbus_wait (grub_port_t smbbase); +grub_err_t EXPORT_FUNC (grub_cs5536_init_smbus) (grub_pci_device_t dev, + grub_uint16_t divisor, + grub_port_t *smbbase); + +void grub_cs5536_init_geode (grub_pci_device_t dev); +#endif + +#endif diff --git a/libr/fs/p/grub/include/grub/datetime.h b/libr/fs/p/grub/include/grub/datetime.h new file mode 100644 index 0000000000..e721e89af4 --- /dev/null +++ b/libr/fs/p/grub/include/grub/datetime.h @@ -0,0 +1,55 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef KERNEL_DATETIME_HEADER +#define KERNEL_DATETIME_HEADER 1 + +#include +#include + +struct grub_datetime +{ + grub_uint16_t year; + grub_uint8_t month; + grub_uint8_t day; + grub_uint8_t hour; + grub_uint8_t minute; + grub_uint8_t second; +}; + +/* Return date and time. */ +#ifdef GRUB_MACHINE_EMU +grub_err_t EXPORT_FUNC(grub_get_datetime) (struct grub_datetime *datetime); + +/* Set date and time. */ +grub_err_t EXPORT_FUNC(grub_set_datetime) (struct grub_datetime *datetime); +#else +grub_err_t grub_get_datetime (struct grub_datetime *datetime); + +/* Set date and time. */ +grub_err_t grub_set_datetime (struct grub_datetime *datetime); +#endif + +int grub_get_weekday (struct grub_datetime *datetime); +char *grub_get_weekday_name (struct grub_datetime *datetime); + +void grub_unixtime2datetime (grub_int32_t nix, + struct grub_datetime *datetime); + + +#endif /* ! KERNEL_DATETIME_HEADER */ diff --git a/libr/fs/p/grub/include/grub/decompressor.h b/libr/fs/p/grub/include/grub/decompressor.h new file mode 100644 index 0000000000..a6eefb01b1 --- /dev/null +++ b/libr/fs/p/grub/include/grub/decompressor.h @@ -0,0 +1,34 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_DECOMPRESSOR_HEADER +#define GRUB_DECOMPRESSOR_HEADER 1 + +void +grub_decompress_core (void *src, void *dst, unsigned long srcsize, + unsigned long dstsize); + +void +find_scratch (void *src, void *dst, unsigned long srcsize, + unsigned long dstsize); + +#define GRUB_DECOMPRESSOR_DICT_SIZE (1 << 16) + +extern void *grub_decompressor_scratch; + +#endif diff --git a/libr/fs/p/grub/include/grub/device.h b/libr/fs/p/grub/include/grub/device.h new file mode 100644 index 0000000000..f0e8a8ca8a --- /dev/null +++ b/libr/fs/p/grub/include/grub/device.h @@ -0,0 +1,41 @@ +/* device.h - device manager */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_DEVICE_HEADER +#define GRUB_DEVICE_HEADER 1 + +#include +#include + +struct grub_disk; +struct grub_net; +struct grub_fs; + +struct grub_device +{ + struct grub_disk *disk; + struct grub_net *net; +}; +typedef struct grub_device *grub_device_t; + +grub_device_t EXPORT_FUNC(grub_device_open) (const char *name); +grub_err_t EXPORT_FUNC(grub_device_close) (grub_device_t device); +int EXPORT_FUNC(grub_device_iterate) (int (*hook) (const char *name)); + +#endif /* ! GRUB_DEVICE_HEADER */ diff --git a/libr/fs/p/grub/include/grub/disk.h b/libr/fs/p/grub/include/grub/disk.h new file mode 100644 index 0000000000..66db1149a4 --- /dev/null +++ b/libr/fs/p/grub/include/grub/disk.h @@ -0,0 +1,189 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_DISK_HEADER +#define GRUB_DISK_HEADER 1 + +#include +#include +#include +#include + +/* These are used to set a device id. When you add a new disk device, + you must define a new id for it here. */ +enum grub_disk_dev_id + { + GRUB_DISK_DEVICE_BIOSDISK_ID, + GRUB_DISK_DEVICE_OFDISK_ID, + GRUB_DISK_DEVICE_LOOPBACK_ID, + GRUB_DISK_DEVICE_EFIDISK_ID, + GRUB_DISK_DEVICE_RAID_ID, + GRUB_DISK_DEVICE_LVM_ID, + GRUB_DISK_DEVICE_HOST_ID, + GRUB_DISK_DEVICE_ATA_ID, + GRUB_DISK_DEVICE_MEMDISK_ID, + GRUB_DISK_DEVICE_NAND_ID, + GRUB_DISK_DEVICE_UUID_ID, + GRUB_DISK_DEVICE_PXE_ID, + GRUB_DISK_DEVICE_SCSI_ID, + GRUB_DISK_DEVICE_FILE_ID, + GRUB_DISK_DEVICE_LUKS_ID + }; + +struct grub_disk; +#ifdef GRUB_UTIL +struct grub_disk_memberlist; +#endif + +/* Disk device. */ +struct grub_disk_dev +{ + /* The device name. */ + const char *name; + + /* The device id used by the cache manager. */ + enum grub_disk_dev_id id; + + /* Call HOOK with each device name, until HOOK returns non-zero. */ + int (*iterate) (int (*hook) (const char *name)); + + /* Open the device named NAME, and set up DISK. */ + grub_err_t (*open) (const char *name, struct grub_disk *disk); + + /* Close the disk DISK. */ + void (*close) (struct grub_disk *disk); + + /* Read SIZE sectors from the sector SECTOR of the disk DISK into BUF. */ + grub_err_t (*read) (struct grub_disk *disk, grub_disk_addr_t sector, + grub_size_t size, char *buf); + + /* Write SIZE sectors from BUF into the sector SECTOR of the disk DISK. */ + grub_err_t (*write) (struct grub_disk *disk, grub_disk_addr_t sector, + grub_size_t size, const char *buf); + +#ifdef GRUB_UTIL + struct grub_disk_memberlist *(*memberlist) (struct grub_disk *disk); + const char * (*raidname) (struct grub_disk *disk); +#endif + + /* The next disk device. */ + struct grub_disk_dev *next; +}; +typedef struct grub_disk_dev *grub_disk_dev_t; + +struct grub_partition; + +/* Disk. */ +struct grub_disk +{ + /* The disk name. */ + const char *name; + + /* The underlying disk device. */ + grub_disk_dev_t dev; + + /* The total number of sectors. */ + grub_uint64_t total_sectors; + + /* The id used by the disk cache manager. */ + unsigned long id; + + /* The partition information. This is machine-specific. */ + struct grub_partition *partition; + + /* Called when a sector was read. OFFSET is between 0 and + the sector size minus 1, and LENGTH is between 0 and the sector size. */ + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length); + + /* Device-specific data. */ + void *data; +}; +typedef struct grub_disk *grub_disk_t; + +#ifdef GRUB_UTIL +struct grub_disk_memberlist +{ + grub_disk_t disk; + struct grub_disk_memberlist *next; +}; +typedef struct grub_disk_memberlist *grub_disk_memberlist_t; +#endif + +/* The sector size. */ +#define GRUB_DISK_SECTOR_SIZE 0x200 +#define GRUB_DISK_SECTOR_BITS 9 + +/* The maximum number of disk caches. */ +#define GRUB_DISK_CACHE_NUM 1021 + +/* The size of a disk cache in sector units. */ +#define GRUB_DISK_CACHE_SIZE 8 +#define GRUB_DISK_CACHE_BITS 3 + +/* Return value of grub_disk_get_size() in case disk size is unknown. */ +#define GRUB_DISK_SIZE_UNKNOWN 0xffffffffffffffffULL + +/* This is called from the memory manager. */ +void grub_disk_cache_invalidate_all (void); + +void EXPORT_FUNC(grub_disk_dev_register) (grub_disk_dev_t dev); +void EXPORT_FUNC(grub_disk_dev_unregister) (grub_disk_dev_t dev); +int EXPORT_FUNC(grub_disk_dev_iterate) (int (*hook) (const char *name)); + +grub_disk_t EXPORT_FUNC(grub_disk_open) (const char *name); +void EXPORT_FUNC(grub_disk_close) (grub_disk_t disk); +grub_err_t EXPORT_FUNC(grub_disk_read) (grub_disk_t disk, + grub_disk_addr_t sector, + grub_off_t offset, + grub_size_t size, + void *buf); +grub_err_t EXPORT_FUNC(grub_disk_write) (grub_disk_t disk, + grub_disk_addr_t sector, + grub_off_t offset, + grub_size_t size, + const void *buf); + +grub_uint64_t EXPORT_FUNC(grub_disk_get_size) (grub_disk_t disk); + +extern void (* EXPORT_VAR(grub_disk_firmware_fini)) (void); +extern int EXPORT_VAR(grub_disk_firmware_is_tainted); + +/* ATA pass through parameters and function. */ +struct grub_disk_ata_pass_through_parms +{ + grub_uint8_t taskfile[8]; + void * buffer; + int size; +}; + +extern grub_err_t (* EXPORT_VAR(grub_disk_ata_pass_through)) (grub_disk_t, + struct grub_disk_ata_pass_through_parms *); + +#if defined (GRUB_UTIL) || defined (GRUB_MACHINE_EMU) +void grub_lvm_init (void); +void grub_mdraid09_init (void); +void grub_mdraid1x_init (void); +void grub_raid_init (void); +void grub_lvm_fini (void); +void grub_mdraid09_fini (void); +void grub_mdraid1x_fini (void); +void grub_raid_fini (void); +#endif + +#endif /* ! GRUB_DISK_HEADER */ diff --git a/libr/fs/p/grub/include/grub/dl.h b/libr/fs/p/grub/include/grub/dl.h new file mode 100644 index 0000000000..6c5161b6cc --- /dev/null +++ b/libr/fs/p/grub/include/grub/dl.h @@ -0,0 +1,123 @@ +/* dl.h - types and prototypes for loadable module support */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2004,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_DL_H +#define GRUB_DL_H 1 + +#include +#include +#include +//#include + + +/* + * Macros GRUB_MOD_INIT and GRUB_MOD_FINI are also used by build rules + * to collect module names, so we define them only when they are not + * defined already. + */ + +#ifndef GRUB_MOD_INIT +#define GRUB_MOD_INIT(name) \ +static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \ +void grub_##name##_init (void); \ +void \ +grub_##name##_init (void) { grub_mod_init (0); } \ +static void \ +grub_mod_init (grub_dl_t mod __attribute__ ((unused))) +#endif + +#ifndef GRUB_MOD_FINI +#define GRUB_MOD_FINI(name) \ +static void grub_mod_fini (void) __attribute__ ((used)); \ +void grub_##name##_fini (void); \ +void \ +grub_##name##_fini (void) { grub_mod_fini (); } \ +static void \ +grub_mod_fini (void) +#endif + +#ifdef APPLE_CC +#define GRUB_MOD_NAME(name) \ +static char grub_modname[] __attribute__ ((section ("_modname, _modname"), used)) = #name; + +#define GRUB_MOD_DEP(name) \ +__asm__ (".section _moddeps, _moddeps\n.asciz \"" #name "\"\n") +#else +#define GRUB_MOD_NAME(name) \ +__asm__ (".section .modname\n.asciz \"" #name "\"\n") + +#define GRUB_MOD_DEP(name) \ +__asm__ (".section .moddeps\n.asciz \"" #name "\"\n") +#endif + +struct grub_dl_segment +{ + struct grub_dl_segment *next; + void *addr; + grub_size_t size; + unsigned section; +}; +typedef struct grub_dl_segment *grub_dl_segment_t; + +struct grub_dl; + +struct grub_dl_dep +{ + struct grub_dl_dep *next; + struct grub_dl *mod; +}; +typedef struct grub_dl_dep *grub_dl_dep_t; + +struct grub_dl +{ + char *name; + int ref_count; + grub_dl_dep_t dep; + grub_dl_segment_t segment; + void *symtab;//Elf_Sym *symtab; + void (*init) (struct grub_dl *mod); + void (*fini) (void); + struct grub_dl *next; +}; +typedef struct grub_dl *grub_dl_t; + +grub_dl_t EXPORT_FUNC(grub_dl_load_file) (const char *filename); +grub_dl_t EXPORT_FUNC(grub_dl_load) (const char *name); +grub_dl_t grub_dl_load_core (void *addr, grub_size_t size); +int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod); +//void grub_dl_unload_unneeded (void); +//int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod); +//int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod); +extern grub_dl_t EXPORT_VAR(grub_dl_head); + +#define FOR_DL_MODULES(var) FOR_LIST_ELEMENTS ((var), (grub_dl_head)) + +grub_dl_t EXPORT_FUNC(grub_dl_get) (const char *name); +grub_err_t grub_dl_register_symbol (const char *name, void *addr, + grub_dl_t mod); + +grub_err_t grub_arch_dl_check_header (void *ehdr); +grub_err_t grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr); + +#if defined (_mips) +#define GRUB_LINKER_HAVE_INIT 1 +void grub_arch_dl_init_linker (void); +#endif + +#endif /* ! GRUB_DL_H */ diff --git a/libr/fs/p/grub/include/grub/emu/console.h b/libr/fs/p/grub/include/grub/emu/console.h new file mode 100644 index 0000000000..1e55682826 --- /dev/null +++ b/libr/fs/p/grub/include/grub/emu/console.h @@ -0,0 +1,28 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_CONSOLE_UTIL_HEADER +#define GRUB_CONSOLE_UTIL_HEADER 1 + +/* Initialize the console system. */ +void grub_console_init (void); + +/* Finish the console system. */ +void grub_console_fini (void); + +#endif /* ! GRUB_CONSOLE_UTIL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/emu/getroot.h b/libr/fs/p/grub/include/grub/emu/getroot.h new file mode 100644 index 0000000000..581ea80568 --- /dev/null +++ b/libr/fs/p/grub/include/grub/emu/getroot.h @@ -0,0 +1,38 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003, 2007, 2008, 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_UTIL_GETROOT_HEADER +#define GRUB_UTIL_GETROOT_HEADER 1 + +#include + +enum grub_dev_abstraction_types { + GRUB_DEV_ABSTRACTION_NONE, + GRUB_DEV_ABSTRACTION_LVM, + GRUB_DEV_ABSTRACTION_RAID, +}; + +char *grub_find_device (const char *dir, dev_t dev); +char *grub_guess_root_device (const char *dir); +int grub_util_get_dev_abstraction (const char *os_dev); +char *grub_util_get_grub_dev (const char *os_dev); +char *grub_make_system_path_relative_to_its_root (const char *path); +const char *grub_util_check_block_device (const char *blk_dev); +const char *grub_util_check_char_device (const char *blk_dev); + +#endif /* ! GRUB_UTIL_GETROOT_HEADER */ diff --git a/libr/fs/p/grub/include/grub/emu/hostdisk.h b/libr/fs/p/grub/include/grub/emu/hostdisk.h new file mode 100644 index 0000000000..d8cc02e145 --- /dev/null +++ b/libr/fs/p/grub/include/grub/emu/hostdisk.h @@ -0,0 +1,32 @@ +/* biosdisk.h - emulate biosdisk */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_BIOSDISK_MACHINE_UTIL_HEADER +#define GRUB_BIOSDISK_MACHINE_UTIL_HEADER 1 + +#include + +void grub_util_biosdisk_init (const char *dev_map); +void grub_util_biosdisk_fini (void); +char *grub_util_biosdisk_get_grub_dev (const char *os_dev); +const char *grub_util_biosdisk_get_osdev (grub_disk_t disk); +int grub_util_biosdisk_is_present (const char *name); +int grub_util_biosdisk_is_floppy (grub_disk_t disk); + +#endif /* ! GRUB_BIOSDISK_MACHINE_UTIL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/emu/misc.h b/libr/fs/p/grub/include/grub/emu/misc.h new file mode 100644 index 0000000000..ef0d18300b --- /dev/null +++ b/libr/fs/p/grub/include/grub/emu/misc.h @@ -0,0 +1,81 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_EMU_MISC_H +#define GRUB_EMU_MISC_H 1 + +#include +#include + +#include +#include + +#ifdef __CYGWIN__ +# include +# include +# include +# define DEV_CYGDRIVE_MAJOR 98 +#endif + +#ifdef __NetBSD__ +/* NetBSD uses /boot for its boot block. */ +# define DEFAULT_DIRECTORY "/"GRUB_DIR_NAME +#else +# define DEFAULT_DIRECTORY "/"GRUB_BOOT_DIR_NAME"/"GRUB_DIR_NAME +#endif + +#define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map" + +extern int verbosity; +extern const char *program_name; + +void grub_emu_init (void); +void grub_init_all (void); +void grub_fini_all (void); +void grub_emu_post_init (void); + +void grub_find_zpool_from_dir (const char *dir, + char **poolname, char **poolfs); + +char *grub_make_system_path_relative_to_its_root (const char *path) + __attribute__ ((warn_unused_result)); + +void * EXPORT_FUNC(xmalloc) (grub_size_t size) __attribute__ ((warn_unused_result)); +void * EXPORT_FUNC(xrealloc) (void *ptr, grub_size_t size) __attribute__ ((warn_unused_result)); +char * EXPORT_FUNC(xstrdup) (const char *str) __attribute__ ((warn_unused_result)); +char * EXPORT_FUNC(xasprintf) (const char *fmt, ...) __attribute__ ((warn_unused_result)); + +void EXPORT_FUNC(grub_util_warn) (const char *fmt, ...); +void EXPORT_FUNC(grub_util_info) (const char *fmt, ...); +void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((noreturn)); + +#ifndef HAVE_VASPRINTF +int EXPORT_FUNC(vasprintf) (char **buf, const char *fmt, va_list ap); +#endif + +#ifndef HAVE_ASPRINTF +int EXPORT_FUNC(asprintf) (char **buf, const char *fmt, ...); +#endif + +extern char * canonicalize_file_name (const char *path); + +#ifdef HAVE_DEVICE_MAPPER +int grub_device_mapper_supported (void); +#endif + +#endif /* GRUB_EMU_MISC_H */ diff --git a/libr/fs/p/grub/include/grub/env.h b/libr/fs/p/grub/include/grub/env.h new file mode 100644 index 0000000000..6d1f0de6e8 --- /dev/null +++ b/libr/fs/p/grub/include/grub/env.h @@ -0,0 +1,69 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_ENV_HEADER +#define GRUB_ENV_HEADER 1 + +#include +#include +#include +#include + +struct grub_env_var; + +typedef char *(*grub_env_read_hook_t) (struct grub_env_var *var, + const char *val); +typedef char *(*grub_env_write_hook_t) (struct grub_env_var *var, + const char *val); + +struct grub_env_var +{ + char *name; + char *value; + grub_env_read_hook_t read_hook; + grub_env_write_hook_t write_hook; + struct grub_env_var *next; + struct grub_env_var **prevp; + int global; +}; + +grub_err_t EXPORT_FUNC(grub_env_set) (const char *name, const char *val); +char *EXPORT_FUNC(grub_env_get) (const char *name); +void EXPORT_FUNC(grub_env_unset) (const char *name); +void EXPORT_FUNC(grub_env_iterate) (int (*func) (struct grub_env_var *var)); +struct grub_env_var *EXPORT_FUNC(grub_env_find) (const char *name); +grub_err_t EXPORT_FUNC(grub_register_variable_hook) (const char *name, + grub_env_read_hook_t read_hook, + grub_env_write_hook_t write_hook); + +grub_err_t grub_env_context_open (void); +grub_err_t grub_env_context_close (void); +grub_err_t grub_env_export (const char *name); + +void grub_env_unset_menu (void); +grub_menu_t grub_env_get_menu (void); +void grub_env_set_menu (grub_menu_t nmenu); + +grub_err_t +grub_env_extractor_open (int source); + +grub_err_t +grub_env_extractor_close (int source); + + +#endif /* ! GRUB_ENV_HEADER */ diff --git a/libr/fs/p/grub/include/grub/env_private.h b/libr/fs/p/grub/include/grub/env_private.h new file mode 100644 index 0000000000..bb001533fb --- /dev/null +++ b/libr/fs/p/grub/include/grub/env_private.h @@ -0,0 +1,46 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2005,2006,2007,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_ENV_PRIVATE_HEADER +#define GRUB_ENV_PRIVATE_HEADER 1 + +#include + +/* The size of the hash table. */ +#define HASHSZ 13 + +/* A hashtable for quick lookup of variables. */ +struct grub_env_context +{ + /* A hash table for variables. */ + struct grub_env_var *vars[HASHSZ]; + + /* One level deeper on the stack. */ + struct grub_env_context *prev; +}; + +/* This is used for sorting only. */ +struct grub_env_sorted_var +{ + struct grub_env_var *var; + struct grub_env_sorted_var *next; +}; + +extern struct grub_env_context *EXPORT_VAR(grub_current_context); + +#endif /* ! GRUB_ENV_PRIVATE_HEADER */ diff --git a/libr/fs/p/grub/include/grub/err.h b/libr/fs/p/grub/include/grub/err.h new file mode 100644 index 0000000000..22334038d1 --- /dev/null +++ b/libr/fs/p/grub/include/grub/err.h @@ -0,0 +1,74 @@ +/* err.h - error numbers and prototypes */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_ERR_HEADER +#define GRUB_ERR_HEADER 1 + +#include + +typedef enum + { + GRUB_ERR_NONE = 0, + GRUB_ERR_TEST_FAILURE, + GRUB_ERR_BAD_MODULE, + GRUB_ERR_OUT_OF_MEMORY, + GRUB_ERR_BAD_FILE_TYPE, + GRUB_ERR_FILE_NOT_FOUND, + GRUB_ERR_FILE_READ_ERROR, + GRUB_ERR_BAD_FILENAME, + GRUB_ERR_UNKNOWN_FS, + GRUB_ERR_BAD_FS, + GRUB_ERR_BAD_NUMBER, + GRUB_ERR_OUT_OF_RANGE, + GRUB_ERR_UNKNOWN_DEVICE, + GRUB_ERR_BAD_DEVICE, + GRUB_ERR_READ_ERROR, + GRUB_ERR_WRITE_ERROR, + GRUB_ERR_UNKNOWN_COMMAND, + GRUB_ERR_INVALID_COMMAND, + GRUB_ERR_BAD_ARGUMENT, + GRUB_ERR_BAD_PART_TABLE, + GRUB_ERR_UNKNOWN_OS, + GRUB_ERR_BAD_OS, + GRUB_ERR_NO_KERNEL, + GRUB_ERR_BAD_FONT, + GRUB_ERR_NOT_IMPLEMENTED_YET, + GRUB_ERR_SYMLINK_LOOP, + GRUB_ERR_BAD_COMPRESSED_DATA, + GRUB_ERR_MENU, + GRUB_ERR_TIMEOUT, + GRUB_ERR_IO, + GRUB_ERR_ACCESS_DENIED, + GRUB_ERR_EXTRACTOR + } +grub_err_t; + +extern grub_err_t EXPORT_VAR(grub_errno); +extern char EXPORT_VAR(grub_errmsg)[]; + +grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...); +void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__ ((noreturn)); +void EXPORT_FUNC(grub_error_push) (void); +int EXPORT_FUNC(grub_error_pop) (void); +void EXPORT_FUNC(grub_print_error) (void); +extern int EXPORT_VAR(grub_err_printed_errors); +int grub_err_printf (const char *fmt, ...) + __attribute__ ((format (printf, 1, 2))); + +#endif /* ! GRUB_ERR_HEADER */ diff --git a/libr/fs/p/grub/include/grub/extcmd.h b/libr/fs/p/grub/include/grub/extcmd.h new file mode 100644 index 0000000000..19fe592669 --- /dev/null +++ b/libr/fs/p/grub/include/grub/extcmd.h @@ -0,0 +1,79 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_EXTCMD_HEADER +#define GRUB_EXTCMD_HEADER 1 + +#include +#include +#include + +struct grub_extcmd; +struct grub_extcmd_context; + +typedef grub_err_t (*grub_extcmd_func_t) (struct grub_extcmd_context *ctxt, + int argc, char **args); + +/* The argcmd description. */ +struct grub_extcmd +{ + grub_command_t cmd; + + grub_extcmd_func_t func; + + /* The argument parser optionlist. */ + const struct grub_arg_option *options; + + void *data; +}; +typedef struct grub_extcmd *grub_extcmd_t; + +/* Command context for each instance of execution. */ +struct grub_extcmd_context +{ + struct grub_extcmd *extcmd; + + struct grub_arg_list *state; + + /* Script parameter, if any. */ + struct grub_script *script; +}; +typedef struct grub_extcmd_context *grub_extcmd_context_t; + +grub_extcmd_t EXPORT_FUNC(grub_register_extcmd) (const char *name, + grub_extcmd_func_t func, + grub_command_flags_t flags, + const char *summary, + const char *description, + const struct grub_arg_option *parser); + +grub_extcmd_t EXPORT_FUNC(grub_register_extcmd_prio) (const char *name, + grub_extcmd_func_t func, + grub_command_flags_t flags, + const char *summary, + const char *description, + const struct grub_arg_option *parser, + int prio); + +void EXPORT_FUNC(grub_unregister_extcmd) (grub_extcmd_t cmd); + +grub_err_t EXPORT_FUNC(grub_extcmd_dispatcher) (struct grub_command *cmd, + int argc, char **args, + struct grub_script *script); + +#endif /* ! GRUB_EXTCMD_HEADER */ diff --git a/libr/fs/p/grub/include/grub/file.h b/libr/fs/p/grub/include/grub/file.h new file mode 100644 index 0000000000..72cd454688 --- /dev/null +++ b/libr/fs/p/grub/include/grub/file.h @@ -0,0 +1,129 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_FILE_HEADER +#define GRUB_FILE_HEADER 1 + +#include +#include +#include +#include + +/* File description. */ +struct grub_file +{ + /* The underlying device. */ + grub_device_t device; + + /* The underlying filesystem. */ + grub_fs_t fs; + + /* The current offset. */ + grub_off_t offset; + + /* The file size. */ + grub_off_t size; + + /* If file is not easly seekable. Should be set by underlying layer. */ + int not_easly_seekable; + + /* Filesystem-specific data. */ + void *data; + + /* This is called when a sector is read. Used only for a disk device. */ + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, unsigned length); +}; +typedef struct grub_file *grub_file_t; + +/* Filters with lower ID are executed first. */ +typedef enum grub_file_filter_id + { + GRUB_FILE_FILTER_GZIO, + GRUB_FILE_FILTER_XZIO, + GRUB_FILE_FILTER_MAX, + GRUB_FILE_FILTER_COMPRESSION_FIRST = GRUB_FILE_FILTER_GZIO, + GRUB_FILE_FILTER_COMPRESSION_LAST = GRUB_FILE_FILTER_XZIO, + } grub_file_filter_id_t; + +typedef grub_file_t (*grub_file_filter_t) (grub_file_t in); + +extern grub_file_filter_t EXPORT_VAR(grub_file_filters_all)[GRUB_FILE_FILTER_MAX]; +extern grub_file_filter_t EXPORT_VAR(grub_file_filters_enabled)[GRUB_FILE_FILTER_MAX]; + +static inline void +grub_file_filter_register (grub_file_filter_id_t id, grub_file_filter_t filter) +{ + grub_file_filters_all[id] = filter; + grub_file_filters_enabled[id] = filter; +}; + +static inline void +grub_file_filter_unregister (grub_file_filter_id_t id) +{ + grub_file_filters_all[id] = 0; + grub_file_filters_enabled[id] = 0; +}; + +static inline void +grub_file_filter_disable (grub_file_filter_id_t id) +{ + grub_file_filters_enabled[id] = 0; +}; + +static inline void +grub_file_filter_disable_compression (void) +{ + grub_file_filter_id_t id; + + for (id = GRUB_FILE_FILTER_COMPRESSION_FIRST; + id <= GRUB_FILE_FILTER_COMPRESSION_LAST; id++) + grub_file_filters_enabled[id] = 0; +}; + +/* Get a device name from NAME. */ +char *EXPORT_FUNC(grub_file_get_device_name) (const char *name); + +grub_file_t EXPORT_FUNC(grub_file_open) (const char *name); +grub_ssize_t EXPORT_FUNC(grub_file_read) (grub_file_t file, void *buf, + grub_size_t len); +grub_off_t EXPORT_FUNC(grub_file_seek) (grub_file_t file, grub_off_t offset); +grub_err_t EXPORT_FUNC(grub_file_close) (grub_file_t file); + +/* Return value of grub_file_size() in case file size is unknown. */ +#define GRUB_FILE_SIZE_UNKNOWN 0xffffffffffffffffULL + +static inline grub_off_t +grub_file_size (const grub_file_t file) +{ + return file->size; +} + +static inline grub_off_t +grub_file_tell (const grub_file_t file) +{ + return file->offset; +} + +static inline int +grub_file_seekable (const grub_file_t file) +{ + return !file->not_easly_seekable; +} + +#endif /* ! GRUB_FILE_HEADER */ diff --git a/libr/fs/p/grub/include/grub/fs.h b/libr/fs/p/grub/include/grub/fs.h new file mode 100644 index 0000000000..994eb80809 --- /dev/null +++ b/libr/fs/p/grub/include/grub/fs.h @@ -0,0 +1,112 @@ +/* fs.h - filesystem manager */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2004,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_FS_HEADER +#define GRUB_FS_HEADER 1 + +#include +#include +#include + +#include + +/* Forward declaration is required, because of mutual reference. */ +struct grub_file; + +struct grub_dirhook_info +{ + int dir:1; + int mtimeset:1; + int case_insensitive:1; + grub_int32_t mtime; +}; + +/* Filesystem descriptor. */ +struct grub_fs +{ + /* The next filesystem. */ + struct grub_fs *next; + + /* My name. */ + const char *name; + + /* Call HOOK with each file under DIR. */ + grub_err_t (*dir) (grub_device_t device, const char *path, + int (*hook) (const char *filename, + const struct grub_dirhook_info *info)); + + /* Open a file named NAME and initialize FILE. */ + grub_err_t (*open) (struct grub_file *file, const char *name); + + /* Read LEN bytes data from FILE into BUF. */ + grub_ssize_t (*read) (struct grub_file *file, char *buf, grub_size_t len); + + /* Close the file FILE. */ + grub_err_t (*close) (struct grub_file *file); + + /* Return the label of the device DEVICE in LABEL. The label is + returned in a grub_malloc'ed buffer and should be freed by the + caller. */ + grub_err_t (*label) (grub_device_t device, char **label); + + /* Return the uuid of the device DEVICE in UUID. The uuid is + returned in a grub_malloc'ed buffer and should be freed by the + caller. */ + grub_err_t (*uuid) (grub_device_t device, char **uuid); + + /* Get writing time of filesystem. */ + grub_err_t (*mtime) (grub_device_t device, grub_int32_t *timebuf); + +#ifdef GRUB_UTIL + /* Whether this filesystem reserves first sector for DOS-style boot. */ + int reserved_first_sector; +#endif +}; +typedef struct grub_fs *grub_fs_t; + +/* This is special, because block lists are not files in usual sense. */ +extern struct grub_fs grub_fs_blocklist; + +/* This hook is used to automatically load filesystem modules. + If this hook loads a module, return non-zero. Otherwise return zero. + The newly loaded filesystem is assumed to be inserted into the head of + the linked list GRUB_FS_LIST through the function grub_fs_register. */ +typedef int (*grub_fs_autoload_hook_t) (void); +extern grub_fs_autoload_hook_t EXPORT_VAR(grub_fs_autoload_hook); +extern grub_fs_t EXPORT_VAR (grub_fs_list); + +#ifndef GRUB_LST_GENERATOR +static inline void +grub_fs_register (grub_fs_t fs) +{ + grub_list_push (GRUB_AS_LIST_P (&grub_fs_list), GRUB_AS_LIST (fs)); +} +#endif + +static inline void +grub_fs_unregister (grub_fs_t fs) +{ + grub_list_remove (GRUB_AS_LIST_P (&grub_fs_list), GRUB_AS_LIST (fs)); +} + +#define FOR_FILESYSTEMS(var) FOR_LIST_ELEMENTS((var), (grub_fs_list)) + +grub_fs_t EXPORT_FUNC(grub_fs_probe) (grub_device_t device); + +#endif /* ! GRUB_FS_HEADER */ diff --git a/libr/fs/p/grub/include/grub/fshelp.h b/libr/fs/p/grub/include/grub/fshelp.h new file mode 100644 index 0000000000..42d8da5f24 --- /dev/null +++ b/libr/fs/p/grub/include/grub/fshelp.h @@ -0,0 +1,82 @@ +/* fshelp.h -- Filesystem helper functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_FSHELP_HEADER +#define GRUB_FSHELP_HEADER 1 + +#include +#include +#include + +typedef struct grub_fshelp_node *grub_fshelp_node_t; + +#define GRUB_FSHELP_CASE_INSENSITIVE 0x100 +#define GRUB_FSHELP_TYPE_MASK 0xff +#define GRUB_FSHELP_FLAGS_MASK 0x100 + +enum grub_fshelp_filetype + { + GRUB_FSHELP_UNKNOWN, + GRUB_FSHELP_REG, + GRUB_FSHELP_DIR, + GRUB_FSHELP_SYMLINK + }; + +/* Lookup the node PATH. The node ROOTNODE describes the root of the + directory tree. The node found is returned in FOUNDNODE, which is + either a ROOTNODE or a new malloc'ed node. ITERATE_DIR is used to + iterate over all directory entries in the current node. + READ_SYMLINK is used to read the symlink if a node is a symlink. + EXPECTTYPE is the type node that is expected by the called, an + error is generated if the node is not of the expected type. Make + sure you use the NESTED_FUNC_ATTR macro for HOOK, this is required + because GCC has a nasty bug when using regparm=3. */ +grub_err_t +EXPORT_FUNC(grub_fshelp_find_file) (const char *path, + grub_fshelp_node_t rootnode, + grub_fshelp_node_t *foundnode, + int (*iterate_dir) (grub_fshelp_node_t dir, + int NESTED_FUNC_ATTR + (*hook) (const char *filename, + enum grub_fshelp_filetype filetype, + grub_fshelp_node_t node)), + char *(*read_symlink) (grub_fshelp_node_t node), + enum grub_fshelp_filetype expect); + + +/* Read LEN bytes from the file NODE on disk DISK into the buffer BUF, + beginning with the block POS. READ_HOOK should be set before + reading a block from the file. GET_BLOCK is used to translate file + blocks to disk blocks. The file is FILESIZE bytes big and the + blocks have a size of LOG2BLOCKSIZE (in log2). */ +grub_ssize_t +EXPORT_FUNC(grub_fshelp_read_file) (grub_disk_t disk, grub_fshelp_node_t node, + void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector, + unsigned offset, + unsigned length), + grub_off_t pos, grub_size_t len, char *buf, + grub_disk_addr_t (*get_block) (grub_fshelp_node_t node, + grub_disk_addr_t block), + grub_off_t filesize, int log2blocksize); + +unsigned int +EXPORT_FUNC(grub_fshelp_log2blksize) (unsigned int blksize, + unsigned int *pow); + +#endif /* ! GRUB_FSHELP_HEADER */ diff --git a/libr/fs/p/grub/include/grub/gpt_partition.h b/libr/fs/p/grub/include/grub/gpt_partition.h new file mode 100644 index 0000000000..5f8ddead73 --- /dev/null +++ b/libr/fs/p/grub/include/grub/gpt_partition.h @@ -0,0 +1,71 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2006,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_GPT_PARTITION_HEADER +#define GRUB_GPT_PARTITION_HEADER 1 + +#include + +struct grub_gpt_part_type +{ + grub_uint32_t data1; + grub_uint16_t data2; + grub_uint16_t data3; + grub_uint8_t data4[8]; +} __attribute__ ((aligned(8))); +typedef struct grub_gpt_part_type grub_gpt_part_type_t; + +#define GRUB_GPT_PARTITION_TYPE_EMPTY \ + { 0x0, 0x0, 0x0, \ + { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } \ + } + +#define GRUB_GPT_PARTITION_TYPE_BIOS_BOOT \ + { grub_cpu_to_le32_compile_time (0x21686148), grub_cpu_to_le16_compile_time (0x6449), grub_cpu_to_le16_compile_time (0x6e6f), \ + { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } \ + } + +struct grub_gpt_header +{ + grub_uint8_t magic[8]; + grub_uint32_t version; + grub_uint32_t headersize; + grub_uint32_t crc32; + grub_uint32_t unused1; + grub_uint64_t primary; + grub_uint64_t backup; + grub_uint64_t start; + grub_uint64_t end; + grub_uint8_t guid[16]; + grub_uint64_t partitions; + grub_uint32_t maxpart; + grub_uint32_t partentry_size; + grub_uint32_t partentry_crc32; +} __attribute__ ((packed)); + +struct grub_gpt_partentry +{ + grub_gpt_part_type_t type; + grub_uint8_t guid[16]; + grub_uint64_t start; + grub_uint64_t end; + grub_uint64_t attrib; + char name[72]; +} __attribute__ ((packed)); + +#endif /* ! GRUB_GPT_PARTITION_HEADER */ diff --git a/libr/fs/p/grub/include/grub/hfs.h b/libr/fs/p/grub/include/grub/hfs.h new file mode 100644 index 0000000000..d93b9a2c9d --- /dev/null +++ b/libr/fs/p/grub/include/grub/hfs.h @@ -0,0 +1,61 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2005,2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_HFS_HEADER +#define GRUB_HFS_HEADER 1 + +#include + +#define GRUB_HFS_MAGIC 0x4244 + +/* A single extent. A file consists of one or more extents. */ +struct grub_hfs_extent +{ + /* The first physical block. */ + grub_uint16_t first_block; + grub_uint16_t count; +}; + +/* HFS stores extents in groups of 3. */ +typedef struct grub_hfs_extent grub_hfs_datarecord_t[3]; + +/* The HFS superblock (The official name is `Master Directory + Block'). */ +struct grub_hfs_sblock +{ + grub_uint16_t magic; + grub_uint8_t unused[18]; + grub_uint32_t blksz; + grub_uint8_t unused2[4]; + grub_uint16_t first_block; + grub_uint8_t unused4[6]; + + /* A pascal style string that holds the volumename. */ + grub_uint8_t volname[28]; + + grub_uint8_t unused5[52]; + grub_uint64_t num_serial; + grub_uint16_t embed_sig; + struct grub_hfs_extent embed_extent; + grub_uint8_t unused6[4]; + grub_hfs_datarecord_t extent_recs; + grub_uint32_t catalog_size; + grub_hfs_datarecord_t catalog_recs; +} __attribute__ ((packed)); + +#endif /* ! GRUB_HFS_HEADER */ diff --git a/libr/fs/p/grub/include/grub/i18n.h b/libr/fs/p/grub/include/grub/i18n.h new file mode 100644 index 0000000000..a91d73346e --- /dev/null +++ b/libr/fs/p/grub/include/grub/i18n.h @@ -0,0 +1,68 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_I18N_H +#define GRUB_I18N_H 1 + +#include +#include + +/* NLS can be disabled through the configure --disable-nls option. */ +#if (defined(ENABLE_NLS) && ENABLE_NLS) || !defined (GRUB_UTIL) + +extern const char *(*EXPORT_VAR(grub_gettext)) (const char *s); + +# ifdef GRUB_UTIL + +# include +# include + +# endif /* GRUB_UTIL */ + +#else /* ! (defined(ENABLE_NLS) && ENABLE_NLS) */ + +/* Disabled NLS. + The casts to 'const char *' serve the purpose of producing warnings + for invalid uses of the value returned from these functions. + On pre-ANSI systems without 'const', the config.h file is supposed to + contain "#define const". */ +static inline const char * __attribute__ ((always_inline)) +gettext (const char *str) +{ + return str; +} + +#endif /* (defined(ENABLE_NLS) && ENABLE_NLS) */ + +#ifdef GRUB_UTIL +static inline const char * __attribute__ ((always_inline)) +_ (const char *str) +{ + return gettext(str); +} +#else +static inline const char * __attribute__ ((always_inline)) +_ (const char *str) +{ + return grub_gettext(str); +} +#endif /* GRUB_UTIL */ + +#define N_(str) str + +#endif /* GRUB_I18N_H */ diff --git a/libr/fs/p/grub/include/grub/list.h b/libr/fs/p/grub/include/grub/list.h new file mode 100644 index 0000000000..75353010c9 --- /dev/null +++ b/libr/fs/p/grub/include/grub/list.h @@ -0,0 +1,117 @@ +/* list.h - header for grub list */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LIST_HEADER +#define GRUB_LIST_HEADER 1 + +#include +#include +#include + +struct grub_list +{ + struct grub_list *next; +}; +typedef struct grub_list *grub_list_t; + +void EXPORT_FUNC(grub_list_push) (grub_list_t *head, grub_list_t item); +void EXPORT_FUNC(grub_list_remove) (grub_list_t *head, grub_list_t item); + +#define FOR_LIST_ELEMENTS(var, list) for ((var) = (list); (var); (var) = (var)->next) + +static inline void * +grub_bad_type_cast_real (int line, const char *file) + ATTRIBUTE_ERROR ("bad type cast between incompatible grub types"); + +static inline void * +grub_bad_type_cast_real (int line, const char *file) +{ + grub_fatal ("error:%s:%u: bad type cast between incompatible grub types", + file, line); + return 0; +} + +#define grub_bad_type_cast() grub_bad_type_cast_real(__LINE__, GRUB_FILE) + +#define GRUB_FIELD_MATCH(ptr, type, field) \ + ((char *) &(ptr)->field == (char *) &((type) (ptr))->field) + +#define GRUB_AS_LIST(ptr) \ + (GRUB_FIELD_MATCH (ptr, grub_list_t, next) ? \ + (grub_list_t) ptr : grub_bad_type_cast ()) + +#define GRUB_AS_LIST_P(pptr) \ + (GRUB_FIELD_MATCH (*pptr, grub_list_t, next) ? \ + (grub_list_t *) (void *) pptr : grub_bad_type_cast ()) + +struct grub_named_list +{ + struct grub_named_list *next; + char *name; +}; +typedef struct grub_named_list *grub_named_list_t; + +void * EXPORT_FUNC(grub_named_list_find) (grub_named_list_t head, + const char *name); + +#define GRUB_AS_NAMED_LIST(ptr) \ + ((GRUB_FIELD_MATCH (ptr, grub_named_list_t, next) && \ + GRUB_FIELD_MATCH (ptr, grub_named_list_t, name))? \ + (grub_named_list_t) ptr : grub_bad_type_cast ()) + +#define GRUB_AS_NAMED_LIST_P(pptr) \ + ((GRUB_FIELD_MATCH (*pptr, grub_named_list_t, next) && \ + GRUB_FIELD_MATCH (*pptr, grub_named_list_t, name))? \ + (grub_named_list_t *) (void *) pptr : grub_bad_type_cast ()) + +#define GRUB_PRIO_LIST_PRIO_MASK 0xff +#define GRUB_PRIO_LIST_FLAG_ACTIVE 0x100 + +struct grub_prio_list +{ + struct grub_prio_list *next; + char *name; + int prio; +}; +typedef struct grub_prio_list *grub_prio_list_t; + +void EXPORT_FUNC(grub_prio_list_insert) (grub_prio_list_t *head, + grub_prio_list_t item); + +static inline void +grub_prio_list_remove (grub_prio_list_t *head, grub_prio_list_t item) +{ + if ((item->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) && (item->next)) + item->next->prio |= GRUB_PRIO_LIST_FLAG_ACTIVE; + grub_list_remove (GRUB_AS_LIST_P (head), GRUB_AS_LIST (item)); +} + +#define GRUB_AS_PRIO_LIST(ptr) \ + ((GRUB_FIELD_MATCH (ptr, grub_prio_list_t, next) && \ + GRUB_FIELD_MATCH (ptr, grub_prio_list_t, name) && \ + GRUB_FIELD_MATCH (ptr, grub_prio_list_t, prio))? \ + (grub_prio_list_t) ptr : grub_bad_type_cast ()) + +#define GRUB_AS_PRIO_LIST_P(pptr) \ + ((GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, next) && \ + GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, name) && \ + GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, prio))? \ + (grub_prio_list_t *) (void *) pptr : grub_bad_type_cast ()) + +#endif /* ! GRUB_LIST_HEADER */ diff --git a/libr/fs/p/grub/include/grub/loader.h b/libr/fs/p/grub/include/grub/loader.h new file mode 100644 index 0000000000..c71e8dd10a --- /dev/null +++ b/libr/fs/p/grub/include/grub/loader.h @@ -0,0 +1,66 @@ +/* loader.h - OS loaders */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2004,2006,2007,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LOADER_HEADER +#define GRUB_LOADER_HEADER 1 + +#include +#include +#include +#include + +/* Check if a loader is loaded. */ +int EXPORT_FUNC (grub_loader_is_loaded) (void); + +/* Set loader functions. NORETURN must be set to true, if BOOT won't return + to the original state. */ +void EXPORT_FUNC (grub_loader_set) (grub_err_t (*boot) (void), + grub_err_t (*unload) (void), + int noreturn); + +/* Unset current loader, if any. */ +void EXPORT_FUNC (grub_loader_unset) (void); + +/* Call the boot hook in current loader. This may or may not return, + depending on the setting by grub_loader_set. */ +grub_err_t grub_loader_boot (void); + +/* The space between numbers is intentional for the simplicity of adding new + values even if external modules use them. */ +typedef enum { + /* A preboot hook which can use everything and turns nothing off. */ + GRUB_LOADER_PREBOOT_HOOK_PRIO_NORMAL = 400, + /* A preboot hook which can't use disks and may stop disks. */ + GRUB_LOADER_PREBOOT_HOOK_PRIO_DISK = 300, + /* A preboot hook which can't use disks or console and may stop console. */ + GRUB_LOADER_PREBOOT_HOOK_PRIO_CONSOLE = 200, + /* A preboot hook which can't use disks or console, can't modify memory map + and may stop memory services or finalize memory map. */ + GRUB_LOADER_PREBOOT_HOOK_PRIO_MEMORY = 100, +} grub_loader_preboot_hook_prio_t; + +/* Register a preboot hook. */ +void *EXPORT_FUNC(grub_loader_register_preboot_hook) (grub_err_t (*preboot_func) (int noret), + grub_err_t (*preboot_rest_func) (void), + grub_loader_preboot_hook_prio_t prio); + +/* Unregister given preboot hook. */ +void grub_loader_unregister_preboot_hook (void *hnd); + +#endif /* ! GRUB_LOADER_HEADER */ diff --git a/libr/fs/p/grub/include/grub/lvm.h b/libr/fs/p/grub/include/grub/lvm.h new file mode 100644 index 0000000000..a4bf3b2884 --- /dev/null +++ b/libr/fs/p/grub/include/grub/lvm.h @@ -0,0 +1,129 @@ +/* lvm.h - On disk structures for LVM. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LVM_H +#define GRUB_LVM_H 1 + +#include + +/* Length of ID string, excluding terminating zero. */ +#define GRUB_LVM_ID_STRLEN 38 + +struct grub_lvm_vg { + char id[GRUB_LVM_ID_STRLEN+1]; + char *name; + int extent_size; + struct grub_lvm_pv *pvs; + struct grub_lvm_lv *lvs; + struct grub_lvm_vg *next; +}; + +struct grub_lvm_pv { + char id[GRUB_LVM_ID_STRLEN+1]; + char *name; + grub_disk_t disk; + int start; /* Sector number where the data area starts. */ + struct grub_lvm_pv *next; +}; + +struct grub_lvm_lv { + char *name; + unsigned int number; + unsigned int segment_count; + grub_uint64_t size; + struct grub_lvm_segment *segments; /* Pointer to segment_count segments. */ + struct grub_lvm_vg *vg; + struct grub_lvm_lv *next; +}; + +struct grub_lvm_segment { + unsigned int start_extent; + unsigned int extent_count; + unsigned int stripe_count; + unsigned int stripe_size; + struct grub_lvm_stripe *stripes; /* Pointer to stripe_count stripes. */ +}; + +struct grub_lvm_stripe { + int start; + struct grub_lvm_pv *pv; +}; + +#define GRUB_LVM_LABEL_SIZE GRUB_DISK_SECTOR_SIZE +#define GRUB_LVM_LABEL_SCAN_SECTORS 4L + +#define GRUB_LVM_LABEL_ID "LABELONE" +#define GRUB_LVM_LVM2_LABEL "LVM2 001" + +#define GRUB_LVM_ID_LEN 32 + +/* On disk - 32 bytes */ +struct grub_lvm_label_header { + grub_int8_t id[8]; /* LABELONE */ + grub_uint64_t sector_xl; /* Sector number of this label */ + grub_uint32_t crc_xl; /* From next field to end of sector */ + grub_uint32_t offset_xl; /* Offset from start of struct to contents */ + grub_int8_t type[8]; /* LVM2 001 */ +} __attribute__ ((packed)); + +/* On disk */ +struct grub_lvm_disk_locn { + grub_uint64_t offset; /* Offset in bytes to start sector */ + grub_uint64_t size; /* Bytes */ +} __attribute__ ((packed)); + +/* Fields with the suffix _xl should be xlate'd wherever they appear */ +/* On disk */ +struct grub_lvm_pv_header { + grub_int8_t pv_uuid[GRUB_LVM_ID_LEN]; + + /* This size can be overridden if PV belongs to a VG */ + grub_uint64_t device_size_xl; /* Bytes */ + + /* NULL-terminated list of data areas followed by */ + /* NULL-terminated list of metadata area headers */ + struct grub_lvm_disk_locn disk_areas_xl[0]; /* Two lists */ +} __attribute__ ((packed)); + +#define GRUB_LVM_FMTT_MAGIC "\040\114\126\115\062\040\170\133\065\101\045\162\060\116\052\076" +#define GRUB_LVM_FMTT_VERSION 1 +#define GRUB_LVM_MDA_HEADER_SIZE 512 + +/* On disk */ +struct grub_lvm_raw_locn { + grub_uint64_t offset; /* Offset in bytes to start sector */ + grub_uint64_t size; /* Bytes */ + grub_uint32_t checksum; + grub_uint32_t filler; +} __attribute__ ((packed)); + +/* On disk */ +/* Structure size limited to one sector */ +struct grub_lvm_mda_header { + grub_uint32_t checksum_xl; /* Checksum of rest of mda_header */ + grub_int8_t magic[16]; /* To aid scans for metadata */ + grub_uint32_t version; + grub_uint64_t start; /* Absolute start byte of mda_header */ + grub_uint64_t size; /* Size of metadata area */ + + struct grub_lvm_raw_locn raw_locns[0]; /* NULL-terminated list */ +} __attribute__ ((packed)); + + +#endif /* ! GRUB_LVM_H */ diff --git a/libr/fs/p/grub/include/grub/memory.h b/libr/fs/p/grub/include/grub/memory.h new file mode 100644 index 0000000000..d983865652 --- /dev/null +++ b/libr/fs/p/grub/include/grub/memory.h @@ -0,0 +1,73 @@ +/* memory.h - describe the memory map */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_MEMORY_HEADER +#define GRUB_MEMORY_HEADER 1 + +#include +#include + +typedef enum grub_memory_type + { + GRUB_MEMORY_AVAILABLE = 1, + GRUB_MEMORY_RESERVED = 2, + GRUB_MEMORY_ACPI = 3, + GRUB_MEMORY_NVS = 4, + GRUB_MEMORY_BADRAM = 5, + GRUB_MEMORY_CODE = 20, + /* This one is special: it's used internally but is never reported + by firmware. */ + GRUB_MEMORY_HOLE = 21 + } grub_memory_type_t; + +typedef int NESTED_FUNC_ATTR (*grub_memory_hook_t) (grub_uint64_t, + grub_uint64_t, + grub_memory_type_t); + +grub_err_t grub_mmap_iterate (grub_memory_hook_t hook); + +#if !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_EFI) +grub_err_t EXPORT_FUNC(grub_machine_mmap_iterate) (grub_memory_hook_t hook); +#else +grub_err_t grub_machine_mmap_iterate (grub_memory_hook_t hook); +#endif + +int grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type); +grub_err_t grub_mmap_unregister (int handle); + +void *grub_mmap_malign_and_register (grub_uint64_t align, grub_uint64_t size, + int *handle, int type, int flags); + +void grub_mmap_free_and_unregister (int handle); + +#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE + +struct grub_mmap_region +{ + struct grub_mmap_region *next; + grub_uint64_t start; + grub_uint64_t end; + grub_memory_type_t type; + int handle; +}; + +extern struct grub_mmap_region *grub_mmap_overlays; +#endif + +#endif /* ! GRUB_MEMORY_HEADER */ diff --git a/libr/fs/p/grub/include/grub/menu.h b/libr/fs/p/grub/include/grub/menu.h new file mode 100644 index 0000000000..5ff356beb4 --- /dev/null +++ b/libr/fs/p/grub/include/grub/menu.h @@ -0,0 +1,108 @@ +/* menu.h - Menu model function prototypes and data structures. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_MENU_HEADER +#define GRUB_MENU_HEADER 1 + +struct grub_menu_entry_class +{ + char *name; + struct grub_menu_entry_class *next; +}; + +/* The menu entry. */ +struct grub_menu_entry +{ + /* The title name. */ + const char *title; + + /* If set means not everybody is allowed to boot this entry. */ + int restricted; + + /* Allowed users. */ + const char *users; + + /* The classes associated with the menu entry: + used to choose an icon or other style attributes. + This is a dummy head node for the linked list, so for an entry E, + E.classes->next is the first class if it is not NULL. */ + struct grub_menu_entry_class *classes; + + /* The sourcecode of the menu entry, used by the editor. */ + const char *sourcecode; + + /* Parameters to be passed to menu definition. */ + int argc; + char **args; + + int hotkey; + + int submenu; + + /* The next element. */ + struct grub_menu_entry *next; +}; +typedef struct grub_menu_entry *grub_menu_entry_t; + +/* The menu. */ +struct grub_menu +{ + /* The size of a menu. */ + int size; + + /* The list of menu entries. */ + grub_menu_entry_t entry_list; +}; +typedef struct grub_menu *grub_menu_t; + +/* Callback structure menu viewers can use to provide user feedback when + default entries are executed, possibly including fallback entries. */ +typedef struct grub_menu_execute_callback +{ + /* Called immediately before ENTRY is booted. */ + void (*notify_booting) (grub_menu_entry_t entry, void *userdata); + + /* Called when executing one entry has failed, and another entry, ENTRY, will + be executed as a fallback. The implementation of this function should + delay for a period of at least 2 seconds before returning in order to + allow the user time to read the information before it can be lost by + executing ENTRY. */ + void (*notify_fallback) (grub_menu_entry_t entry, void *userdata); + + /* Called when an entry has failed to execute and there is no remaining + fallback entry to attempt. */ + void (*notify_failure) (void *userdata); +} +*grub_menu_execute_callback_t; + +grub_menu_entry_t grub_menu_get_entry (grub_menu_t menu, int no); +int grub_menu_get_timeout (void); +void grub_menu_set_timeout (int timeout); +void grub_menu_execute_entry (grub_menu_entry_t entry); +void grub_menu_execute_with_fallback (grub_menu_t menu, + grub_menu_entry_t entry, + grub_menu_execute_callback_t callback, + void *callback_data); +void grub_menu_entry_run (grub_menu_entry_t entry); +int grub_menu_get_default_entry_index (grub_menu_t menu); + +void grub_menu_init (void); +void grub_menu_fini (void); + +#endif /* GRUB_MENU_HEADER */ diff --git a/libr/fs/p/grub/include/grub/menu_viewer.h b/libr/fs/p/grub/include/grub/menu_viewer.h new file mode 100644 index 0000000000..c6513c4e80 --- /dev/null +++ b/libr/fs/p/grub/include/grub/menu_viewer.h @@ -0,0 +1,48 @@ +/* menu_viewer.h - Interface to menu viewer implementations. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_MENU_VIEWER_HEADER +#define GRUB_MENU_VIEWER_HEADER 1 + +#include +#include +#include +#include +#include + +struct grub_menu_viewer +{ + struct grub_menu_viewer *next; + void *data; + void (*set_chosen_entry) (int entry, void *data); + void (*print_timeout) (int timeout, void *data); + void (*clear_timeout) (void *data); + void (*fini) (void *fini); +}; + +void grub_menu_register_viewer (struct grub_menu_viewer *viewer); + +grub_err_t +grub_menu_try_text (struct grub_term_output *term, + int entry, grub_menu_t menu, int nested); + +extern grub_err_t (*grub_gfxmenu_try_hook) (int entry, grub_menu_t menu, + int nested); + +#endif /* GRUB_MENU_VIEWER_HEADER */ diff --git a/libr/fs/p/grub/include/grub/misc.h b/libr/fs/p/grub/include/grub/misc.h new file mode 100644 index 0000000000..6fcaa148ba --- /dev/null +++ b/libr/fs/p/grub/include/grub/misc.h @@ -0,0 +1,356 @@ +/* misc.h - prototypes for misc functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_MISC_HEADER +#define GRUB_MISC_HEADER 1 + +#include +#include +#include +#include + +/* GCC version checking borrowed from glibc. */ +#if defined(__GNUC__) && defined(__GNUC_MINOR__) +# define GNUC_PREREQ(maj,min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +#else +# define GNUC_PREREQ(maj,min) 0 +#endif + +/* Does this compiler support compile-time error attributes? */ +#if GNUC_PREREQ(4,3) +# define ATTRIBUTE_ERROR(msg) \ + __attribute__ ((__error__ (msg))) +#else +# define ATTRIBUTE_ERROR(msg) +#endif + +#define ALIGN_UP(addr, align) \ + ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1)) +#define ALIGN_DOWN(addr, align) \ + ((addr) & ~((typeof (addr)) align - 1)) +#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0])) +#define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; } + +#define grub_dprintf(condition, fmt, args...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, fmt, ## args) +/* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */ +#define grub_memcpy(d,s,n) grub_memmove ((d), (s), (n)) + +void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n); +char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src); +char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c); +char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src); + +static inline char * +grub_strcat (char *dest, const char *src) +{ + char *p = dest; + + while (*p) + p++; + + while ((*p = *src) != '\0') + { + p++; + src++; + } + + return dest; +} + +static inline char * +grub_strncat (char *dest, const char *src, int c) +{ + char *p = dest; + + while (*p) + p++; + + while ((*p = *src) != '\0' && c--) + { + p++; + src++; + } + + *p = '\0'; + + return dest; +} + +/* Prototypes for aliases. */ +#ifndef GRUB_UTIL +int EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n); +void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n); +void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n); +void *EXPORT_FUNC(memset) (void *s, int c, grub_size_t n); +#endif + +int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n); +int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2); +int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n); + +char *EXPORT_FUNC(grub_strchr) (const char *s, int c); +char *EXPORT_FUNC(grub_strrchr) (const char *s, int c); +int EXPORT_FUNC(grub_strword) (const char *s, const char *w); +char *EXPORT_FUNC(grub_strstr) (const char *haystack, const char *needle); +int EXPORT_FUNC(grub_isspace) (int c); +int EXPORT_FUNC(grub_isprint) (int c); + +static inline int +grub_iscntrl (int c) +{ + return (c >= 0x00 && c <= 0x1F) || c == 0x7F; +} + +static inline int +grub_isalpha (int c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +static inline int +grub_isgraph (int c) +{ + return (c >= '!' && c <= '~'); +} + +static inline int +grub_isdigit (int c) +{ + return (c >= '0' && c <= '9'); +} + +static inline int +grub_isalnum (int c) +{ + return grub_isalpha (c) || grub_isdigit (c); +} + +static inline int +grub_tolower (int c) +{ + if (c >= 'A' && c <= 'Z') + return c - 'A' + 'a'; + + return c; +} + +static inline int +grub_toupper (int c) +{ + if (c >= 'a' && c <= 'z') + return c - 'a' + 'A'; + + return c; +} + +static inline int +grub_strcasecmp (const char *s1, const char *s2) +{ + while (*s1 && *s2) + { + if (grub_tolower (*s1) != grub_tolower (*s2)) + break; + + s1++; + s2++; + } + + return (int) grub_tolower (*s1) - (int) grub_tolower (*s2); +} + +static inline int +grub_strncasecmp (const char *s1, const char *s2, grub_size_t n) +{ + if (n == 0) + return 0; + + while (*s1 && *s2 && --n) + { + if (grub_tolower (*s1) != grub_tolower (*s2)) + break; + + s1++; + s2++; + } + + return (int) grub_tolower (*s1) - (int) grub_tolower (*s2); +} + +/* Replace all `ch' characters of `input' with `with' and copy the + result into `output'; return EOS address of `output'. */ +static inline char * +grub_strchrsub (char *output, const char *input, char ch, const char *with) +{ + grub_size_t grub_strlen (const char *s); + while (*input) + { + if (*input == ch) + { + grub_strcpy (output, with); + output += grub_strlen (with); + input++; + continue; + } + *output++ = *input++; + } + *output = '\0'; + return output; +} + +unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base); +unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base); + +static inline long +grub_strtol (const char *str, char **end, int base) +{ + int negative = 0; + unsigned long magnitude; + + while (*str && grub_isspace (*str)) + str++; + + if (*str == '-') + { + negative = 1; + str++; + } + + magnitude = grub_strtoull (str, end, base); + if (negative) + { + if (magnitude > (unsigned long) GRUB_LONG_MAX + 1) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, "negative overflow"); + return GRUB_LONG_MIN; + } + return -((long) magnitude); + } + else + { + if (magnitude > GRUB_LONG_MAX) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, "positive overflow"); + return GRUB_LONG_MAX; + } + return (long) magnitude; + } +} + +char *EXPORT_FUNC(grub_strdup) (const char *s) __attribute__ ((warn_unused_result)); +char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) __attribute__ ((warn_unused_result)); +void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n); +grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) __attribute__ ((warn_unused_result)); +int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); +int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); + +extern void (*EXPORT_VAR (grub_xputs)) (const char *str); + +static inline int +grub_puts (const char *s) +{ + const char nl[2] = "\n"; + grub_xputs (s); + grub_xputs (nl); + + return 1; /* Cannot fail. */ +} + +int EXPORT_FUNC(grub_puts_) (const char *s); +void EXPORT_FUNC(grub_real_dprintf) (const char *file, + const int line, + const char *condition, + const char *fmt, ...) __attribute__ ((format (printf, 4, 5))); +int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args); +int EXPORT_FUNC(grub_snprintf) (char *str, grub_size_t n, const char *fmt, ...) + __attribute__ ((format (printf, 3, 4))); +int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt, + va_list args); +char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...) + __attribute__ ((format (printf, 1, 2))) __attribute__ ((warn_unused_result)); +char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) __attribute__ ((warn_unused_result)); +void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn)); +void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn)); +grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n, + grub_uint32_t d, grub_uint32_t *r); + +#if NEED_ENABLE_EXECUTE_STACK && !defined(GRUB_UTIL) +void EXPORT_FUNC(__enable_execute_stack) (void *addr); +#endif + +#if NEED_REGISTER_FRAME_INFO && !defined(GRUB_UTIL) +void EXPORT_FUNC (__register_frame_info) (void); +void EXPORT_FUNC (__deregister_frame_info) (void); +#endif + +/* Inline functions. */ + +static inline unsigned int +grub_abs (int x) +{ + if (x < 0) + return (unsigned int) (-x); + else + return (unsigned int) x; +} + +static inline long +grub_min (long x, long y) +{ + if (x < y) + return x; + else + return y; +} + +static inline long +grub_max (long x, long y) +{ + if (x > y) + return x; + else + return y; +} + +/* Rounded-up division */ +static inline unsigned int +grub_div_roundup (unsigned int x, unsigned int y) +{ + return (x + y - 1) / y; +} + +/* Reboot the machine. */ +void EXPORT_FUNC (grub_reboot) (void) __attribute__ ((noreturn)); + +#ifdef GRUB_MACHINE_PCBIOS +/* Halt the system, using APM if possible. If NO_APM is true, don't + * use APM even if it is available. */ +void grub_halt (int no_apm) __attribute__ ((noreturn)); +#else +void grub_halt (void) __attribute__ ((noreturn)); +#endif + +#ifdef GRUB_MACHINE_EMU +/* Flag to control module autoloading in normal mode. */ +extern int EXPORT_VAR(grub_no_autoload); +#else +#define grub_no_autoload 0 +#endif + +#endif /* ! GRUB_MISC_HEADER */ diff --git a/libr/fs/p/grub/include/grub/mm.h b/libr/fs/p/grub/include/grub/mm.h new file mode 100644 index 0000000000..7188361b6d --- /dev/null +++ b/libr/fs/p/grub/include/grub/mm.h @@ -0,0 +1,77 @@ +/* mm.h - prototypes and declarations for memory manager */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_MM_H +#define GRUB_MM_H 1 + +#include +#include +#include + +#ifndef NULL +# define NULL ((void *) 0) +#endif + +#if 0 +void grub_mm_init_region (void *addr, grub_size_t size); +void *EXPORT_FUNC(grub_malloc) (grub_size_t size); +void *EXPORT_FUNC(grub_zalloc) (grub_size_t size); +void EXPORT_FUNC(grub_free) (void *ptr); +void *EXPORT_FUNC(grub_realloc) (void *ptr, grub_size_t size); +void *EXPORT_FUNC(grub_memalign) (grub_size_t align, grub_size_t size); +#endif + +void grub_mm_check_real (char *file, int line); +#define GRUB_MM_CHECK grub_mm_check_real (__FILE__, __LINE__); + +/* For debugging. */ +#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) +/* Set this variable to 1 when you want to trace all memory function calls. */ +extern int EXPORT_VAR(grub_mm_debug); + +void grub_mm_dump_free (void); +void grub_mm_dump (unsigned lineno); + +#define grub_malloc(size) \ + grub_debug_malloc (GRUB_FILE, __LINE__, size) + +#define grub_zalloc(size) \ + grub_debug_zalloc (GRUB_FILE, __LINE__, size) + +#define grub_realloc(ptr,size) \ + grub_debug_realloc (GRUB_FILE, __LINE__, ptr, size) + +#define grub_memalign(align,size) \ + grub_debug_memalign (GRUB_FILE, __LINE__, align, size) + +#define grub_free(ptr) \ + grub_debug_free (GRUB_FILE, __LINE__, ptr) + +void *EXPORT_FUNC(grub_debug_malloc) (const char *file, int line, + grub_size_t size); +void *EXPORT_FUNC(grub_debug_zalloc) (const char *file, int line, + grub_size_t size); +void EXPORT_FUNC(grub_debug_free) (const char *file, int line, void *ptr); +void *EXPORT_FUNC(grub_debug_realloc) (const char *file, int line, void *ptr, + grub_size_t size); +void *EXPORT_FUNC(grub_debug_memalign) (const char *file, int line, + grub_size_t align, grub_size_t size); +#endif /* MM_DEBUG && ! GRUB_UTIL */ + +#endif /* ! GRUB_MM_H */ diff --git a/libr/fs/p/grub/include/grub/mm_private.h b/libr/fs/p/grub/include/grub/mm_private.h new file mode 100644 index 0000000000..c2c4cb1511 --- /dev/null +++ b/libr/fs/p/grub/include/grub/mm_private.h @@ -0,0 +1,64 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_MM_PRIVATE_H +#define GRUB_MM_PRIVATE_H 1 + +#include + +/* Magic words. */ +#define GRUB_MM_FREE_MAGIC 0x2d3c2808 +#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4 + +typedef struct grub_mm_header +{ + struct grub_mm_header *next; + grub_size_t size; + grub_size_t magic; +#if GRUB_CPU_SIZEOF_VOID_P == 4 + char padding[4]; +#elif GRUB_CPU_SIZEOF_VOID_P == 8 + char padding[8]; +#else +# error "unknown word size" +#endif +} +*grub_mm_header_t; + +#if GRUB_CPU_SIZEOF_VOID_P == 4 +# define GRUB_MM_ALIGN_LOG2 4 +#elif GRUB_CPU_SIZEOF_VOID_P == 8 +# define GRUB_MM_ALIGN_LOG2 5 +#endif + +#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2) + +typedef struct grub_mm_region +{ + struct grub_mm_header *first; + struct grub_mm_region *next; + grub_size_t pre_size; + grub_size_t size; +} +*grub_mm_region_t; + +#ifndef GRUB_MACHINE_EMU +extern grub_mm_region_t EXPORT_VAR (grub_mm_base); +#endif + +#endif diff --git a/libr/fs/p/grub/include/grub/msdos_partition.h b/libr/fs/p/grub/include/grub/msdos_partition.h new file mode 100644 index 0000000000..a6e3fda495 --- /dev/null +++ b/libr/fs/p/grub/include/grub/msdos_partition.h @@ -0,0 +1,124 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2004,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PC_PARTITION_HEADER +#define GRUB_PC_PARTITION_HEADER 1 + +#include +#include +#include +#include +#include + +/* The signature. */ +#define GRUB_PC_PARTITION_SIGNATURE 0xaa55 + +/* This is not a flag actually, but used as if it were a flag. */ +#define GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG 0x10 + +/* DOS partition types. */ +#define GRUB_PC_PARTITION_TYPE_NONE 0 +#define GRUB_PC_PARTITION_TYPE_FAT12 1 +#define GRUB_PC_PARTITION_TYPE_FAT16_LT32M 4 +#define GRUB_PC_PARTITION_TYPE_EXTENDED 5 +#define GRUB_PC_PARTITION_TYPE_FAT16_GT32M 6 +#define GRUB_PC_PARTITION_TYPE_NTFS 7 +#define GRUB_PC_PARTITION_TYPE_FAT32 0xb +#define GRUB_PC_PARTITION_TYPE_FAT32_LBA 0xc +#define GRUB_PC_PARTITION_TYPE_FAT16_LBA 0xe +#define GRUB_PC_PARTITION_TYPE_WIN95_EXTENDED 0xf +#define GRUB_PC_PARTITION_TYPE_EZD 0x55 +#define GRUB_PC_PARTITION_TYPE_MINIX 0x80 +#define GRUB_PC_PARTITION_TYPE_LINUX_MINIX 0x81 +#define GRUB_PC_PARTITION_TYPE_EXT2FS 0x83 +#define GRUB_PC_PARTITION_TYPE_LINUX_EXTENDED 0x85 +#define GRUB_PC_PARTITION_TYPE_VSTAFS 0x9e +#define GRUB_PC_PARTITION_TYPE_FREEBSD 0xa5 +#define GRUB_PC_PARTITION_TYPE_OPENBSD 0xa6 +#define GRUB_PC_PARTITION_TYPE_NETBSD 0xa9 +#define GRUB_PC_PARTITION_TYPE_HFS 0xaf +#define GRUB_PC_PARTITION_TYPE_GPT_DISK 0xee +#define GRUB_PC_PARTITION_TYPE_LINUX_RAID 0xfd + +/* The partition entry. */ +struct grub_msdos_partition_entry +{ + /* If active, 0x80, otherwise, 0x00. */ + grub_uint8_t flag; + + /* The head of the start. */ + grub_uint8_t start_head; + + /* (S | ((C >> 2) & 0xC0)) where S is the sector of the start and C + is the cylinder of the start. Note that S is counted from one. */ + grub_uint8_t start_sector; + + /* (C & 0xFF) where C is the cylinder of the start. */ + grub_uint8_t start_cylinder; + + /* The partition type. */ + grub_uint8_t type; + + /* The end versions of start_head, start_sector and start_cylinder, + respectively. */ + grub_uint8_t end_head; + grub_uint8_t end_sector; + grub_uint8_t end_cylinder; + + /* The start sector. Note that this is counted from zero. */ + grub_uint32_t start; + + /* The length in sector units. */ + grub_uint32_t length; +} __attribute__ ((packed)); + +/* The structure of MBR. */ +struct grub_msdos_partition_mbr +{ + /* The code area (actually, including BPB). */ + grub_uint8_t code[446]; + + /* Four partition entries. */ + struct grub_msdos_partition_entry entries[4]; + + /* The signature 0xaa55. */ + grub_uint16_t signature; +} __attribute__ ((packed)); + + + +static inline int +grub_msdos_partition_is_empty (int type) +{ + return (type == GRUB_PC_PARTITION_TYPE_NONE); +} + +static inline int +grub_msdos_partition_is_extended (int type) +{ + return (type == GRUB_PC_PARTITION_TYPE_EXTENDED + || type == GRUB_PC_PARTITION_TYPE_WIN95_EXTENDED + || type == GRUB_PC_PARTITION_TYPE_LINUX_EXTENDED); +} + +grub_err_t +grub_partition_msdos_iterate (grub_disk_t disk, + int (*hook) (grub_disk_t disk, + const grub_partition_t partition)); + +#endif /* ! GRUB_PC_PARTITION_HEADER */ diff --git a/libr/fs/p/grub/include/grub/net.h b/libr/fs/p/grub/include/grub/net.h new file mode 100644 index 0000000000..c6d71d5b62 --- /dev/null +++ b/libr/fs/p/grub/include/grub/net.h @@ -0,0 +1,72 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_NET_HEADER +#define GRUB_NET_HEADER 1 + +#include +#include +#include + +struct grub_net; + +struct grub_net_dev +{ + /* The device name. */ + const char *name; + + /* FIXME: Just a template. */ + int (*probe) (struct grub_net *net, const void *addr); + void (*reset) (struct grub_net *net); + int (*poll) (struct grub_net *net); + void (*transmit) (struct grub_net *net, const void *destip, + unsigned srcsock, unsigned destsock, const void *packet); + void (*disable) (struct grub_net *net); + + /* The next net device. */ + struct grub_net_dev *next; +}; +typedef struct grub_net_dev *grub_net_dev_t; + +struct grub_fs; + +struct grub_net +{ + /* The net name. */ + const char *name; + + /* The underlying disk device. */ + grub_net_dev_t dev; + + /* The binding filesystem. */ + struct grub_fs *fs; + + /* FIXME: More data would be required, such as an IP address, a mask, + a gateway, etc. */ + + /* Device-specific data. */ + void *data; +}; +typedef struct grub_net *grub_net_t; + +/* FIXME: How to abstract networks? More consideration is necessary. */ + +/* Note: Networks are very different from disks, because networks must + be initialized before used, and the status is persistent. */ + +#endif /* ! GRUB_NET_HEADER */ diff --git a/libr/fs/p/grub/include/grub/normal.h b/libr/fs/p/grub/include/grub/normal.h new file mode 100644 index 0000000000..1875677974 --- /dev/null +++ b/libr/fs/p/grub/include/grub/normal.h @@ -0,0 +1,133 @@ +/* normal.h - prototypes for the normal mode */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_NORMAL_HEADER +#define GRUB_NORMAL_HEADER 1 + +#include +#include +#include +#include +#include +#include +#include + +/* The maximum size of a command-line. */ +#define GRUB_MAX_CMDLINE 1600 + +/* The standard left and right margin for some messages. */ +#define STANDARD_MARGIN 6 + +/* The type of a completion item. */ +enum grub_completion_type + { + GRUB_COMPLETION_TYPE_COMMAND, + GRUB_COMPLETION_TYPE_DEVICE, + GRUB_COMPLETION_TYPE_PARTITION, + GRUB_COMPLETION_TYPE_FILE, + GRUB_COMPLETION_TYPE_ARGUMENT + }; +typedef enum grub_completion_type grub_completion_type_t; + +extern struct grub_menu_viewer grub_normal_text_menu_viewer; +extern int grub_normal_exit_level; + +/* Defined in `main.c'. */ +void grub_enter_normal_mode (const char *config); +void grub_normal_execute (const char *config, int nested, int batch); +void grub_menu_init_page (int nested, int edit, + struct grub_term_output *term); +void grub_normal_init_page (struct grub_term_output *term); +char *grub_file_getline (grub_file_t file); +void grub_cmdline_run (int nested); + +/* Defined in `cmdline.c'. */ +char *grub_cmdline_get (const char *prompt); +grub_err_t grub_set_history (int newsize); + +/* Defined in `completion.c'. */ +char *grub_normal_do_completion (char *buf, int *restore, + void (*hook) (const char *item, grub_completion_type_t type, int count)); + +/* Defined in `misc.c'. */ +grub_err_t grub_normal_print_device_info (const char *name); + +/* Defined in `color.c'. */ +char *grub_env_write_color_normal (struct grub_env_var *var, const char *val); +char *grub_env_write_color_highlight (struct grub_env_var *var, const char *val); +int grub_parse_color_name_pair (grub_uint8_t *ret, const char *name); + +/* Defined in `menu_text.c'. */ +void grub_wait_after_message (void); +void +grub_print_ucs4 (const grub_uint32_t * str, + const grub_uint32_t * last_position, + int margin_left, int margin_right, + struct grub_term_output *term); +grub_ssize_t grub_getstringwidth (grub_uint32_t * str, + const grub_uint32_t * last_position, + struct grub_term_output *term); +void grub_print_message_indented (const char *msg, int margin_left, + int margin_right, + struct grub_term_output *term); +void +grub_menu_text_register_instances (int entry, grub_menu_t menu, int nested); +grub_err_t +grub_show_menu (grub_menu_t menu, int nested); + +/* Defined in `handler.c'. */ +void read_handler_list (void); +void free_handler_list (void); + +/* Defined in `dyncmd.c'. */ +void read_command_list (const char *prefix); + +/* Defined in `autofs.c'. */ +void read_fs_list (const char *prefix); + +void grub_context_init (void); +void grub_context_fini (void); + +void read_crypto_list (const char *prefix); + +void read_terminal_list (const char *prefix); + +void grub_set_more (int onoff); + +void grub_normal_reset_more (void); + +void grub_xputs_normal (const char *str); + +extern int grub_extractor_level; + +grub_err_t +grub_normal_add_menu_entry (int argc, const char **args, char **classes, + const char *users, const char *hotkey, + const char *prefix, const char *sourcecode, + int submenu); + +grub_err_t +grub_normal_set_password (const char *user, const char *password); + +void grub_normal_free_menu (grub_menu_t menu); + +void grub_normal_auth_init (void); +void grub_normal_auth_fini (void); + +#endif /* ! GRUB_NORMAL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/ntfs.h b/libr/fs/p/grub/include/grub/ntfs.h new file mode 100644 index 0000000000..31b99398be --- /dev/null +++ b/libr/fs/p/grub/include/grub/ntfs.h @@ -0,0 +1,183 @@ +/* ntfs.h - header for the NTFS filesystem */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2007,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_NTFS_H +#define GRUB_NTFS_H 1 + +#define FILE_MFT 0 +#define FILE_MFTMIRR 1 +#define FILE_LOGFILE 2 +#define FILE_VOLUME 3 +#define FILE_ATTRDEF 4 +#define FILE_ROOT 5 +#define FILE_BITMAP 6 +#define FILE_BOOT 7 +#define FILE_BADCLUS 8 +#define FILE_QUOTA 9 +#define FILE_UPCASE 10 + +#define AT_STANDARD_INFORMATION 0x10 +#define AT_ATTRIBUTE_LIST 0x20 +#define AT_FILENAME 0x30 +#define AT_OBJECT_ID 0x40 +#define AT_SECURITY_DESCRIPTOR 0x50 +#define AT_VOLUME_NAME 0x60 +#define AT_VOLUME_INFORMATION 0x70 +#define AT_DATA 0x80 +#define AT_INDEX_ROOT 0x90 +#define AT_INDEX_ALLOCATION 0xA0 +#define AT_BITMAP 0xB0 +#define AT_SYMLINK 0xC0 +#define AT_EA_INFORMATION 0xD0 +#define AT_EA 0xE0 + +#define ATTR_READ_ONLY 0x1 +#define ATTR_HIDDEN 0x2 +#define ATTR_SYSTEM 0x4 +#define ATTR_ARCHIVE 0x20 +#define ATTR_DEVICE 0x40 +#define ATTR_NORMAL 0x80 +#define ATTR_TEMPORARY 0x100 +#define ATTR_SPARSE 0x200 +#define ATTR_REPARSE 0x400 +#define ATTR_COMPRESSED 0x800 +#define ATTR_OFFLINE 0x1000 +#define ATTR_NOT_INDEXED 0x2000 +#define ATTR_ENCRYPTED 0x4000 +#define ATTR_DIRECTORY 0x10000000 +#define ATTR_INDEX_VIEW 0x20000000 + +#define FLAG_COMPRESSED 1 +#define FLAG_ENCRYPTED 0x4000 +#define FLAG_SPARSE 0x8000 + +#define BLK_SHR GRUB_DISK_SECTOR_BITS + +#define MAX_MFT (1024 >> BLK_SHR) +#define MAX_IDX (16384 >> BLK_SHR) + +#define COM_LEN 4096 +#define COM_LOG_LEN 12 +#define COM_SEC (COM_LEN >> BLK_SHR) + +#define AF_ALST 1 +#define AF_MMFT 2 +#define AF_GPOS 4 + +#define RF_COMP 1 +#define RF_CBLK 2 +#define RF_BLNK 4 + +#define valueat(buf,ofs,type) *((type*)(((char*)buf)+ofs)) + +#define u16at(buf,ofs) grub_le_to_cpu16(valueat(buf,ofs,grub_uint16_t)) +#define u32at(buf,ofs) grub_le_to_cpu32(valueat(buf,ofs,grub_uint32_t)) +#define u64at(buf,ofs) grub_le_to_cpu64(valueat(buf,ofs,grub_uint64_t)) + +#define v16at(buf,ofs) valueat(buf,ofs,grub_uint16_t) +#define v32at(buf,ofs) valueat(buf,ofs,grub_uint32_t) +#define v64at(buf,ofs) valueat(buf,ofs,grub_uint64_t) + +struct grub_ntfs_bpb +{ + grub_uint8_t jmp_boot[3]; + grub_uint8_t oem_name[8]; + grub_uint16_t bytes_per_sector; + grub_uint8_t sectors_per_cluster; + grub_uint8_t reserved_1[7]; + grub_uint8_t media; + grub_uint16_t reserved_2; + grub_uint16_t sectors_per_track; + grub_uint16_t num_heads; + grub_uint32_t num_hidden_sectors; + grub_uint32_t reserved_3[2]; + grub_uint64_t num_total_sectors; + grub_uint64_t mft_lcn; + grub_uint64_t mft_mirr_lcn; + grub_int8_t clusters_per_mft; + grub_int8_t reserved_4[3]; + grub_int8_t clusters_per_index; + grub_int8_t reserved_5[3]; + grub_uint64_t num_serial; + grub_uint32_t checksum; +} __attribute__ ((packed)); + +#define grub_ntfs_file grub_fshelp_node + +struct grub_ntfs_attr +{ + int flags; + char *emft_buf, *edat_buf; + char *attr_cur, *attr_nxt, *attr_end; + grub_uint32_t save_pos; + char *sbuf; + struct grub_ntfs_file *mft; +}; + +struct grub_fshelp_node +{ + struct grub_ntfs_data *data; + char *buf; + grub_uint64_t size; + grub_uint32_t ino; + int inode_read; + struct grub_ntfs_attr attr; +}; + +struct grub_ntfs_data +{ + struct grub_ntfs_file cmft; + struct grub_ntfs_file mmft; + grub_disk_t disk; + grub_uint32_t mft_size; + grub_uint32_t idx_size; + grub_uint32_t spc; + grub_uint32_t blocksize; + grub_uint32_t mft_start; + grub_uint64_t uuid; +}; + +struct grub_ntfs_comp +{ + grub_disk_t disk; + int comp_head, comp_tail; + grub_uint32_t comp_table[16][2]; + grub_uint32_t cbuf_ofs, cbuf_vcn, spc; + char *cbuf; +}; + +struct grub_ntfs_rlst +{ + int flags; + grub_disk_addr_t target_vcn, curr_vcn, next_vcn, curr_lcn; + char *cur_run; + struct grub_ntfs_attr *attr; + struct grub_ntfs_comp comp; +}; + +typedef grub_err_t (*ntfscomp_func_t) (struct grub_ntfs_attr * at, char *dest, + grub_uint32_t ofs, grub_uint32_t len, + struct grub_ntfs_rlst * ctx, + grub_uint32_t vcn); + +extern ntfscomp_func_t EXPORT_VAR (grub_ntfscomp_func); + +grub_err_t EXPORT_FUNC(grub_ntfs_read_run_list) (struct grub_ntfs_rlst *ctx); + +#endif /* ! GRUB_NTFS_H */ diff --git a/libr/fs/p/grub/include/grub/offsets.h b/libr/fs/p/grub/include/grub/offsets.h new file mode 100644 index 0000000000..817372b691 --- /dev/null +++ b/libr/fs/p/grub/include/grub/offsets.h @@ -0,0 +1,182 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef OFFSETS_HEADER +#define OFFSETS_HEADER 1 + +/* The offset of GRUB_TOTAL_MODULE_SIZE. */ +#define GRUB_KERNEL_I386_PC_TOTAL_MODULE_SIZE 0x8 + +/* The offset of GRUB_KERNEL_IMAGE_SIZE. */ +#define GRUB_KERNEL_I386_PC_KERNEL_IMAGE_SIZE 0xc + +/* The offset of GRUB_COMPRESSED_SIZE. */ +#define GRUB_KERNEL_I386_PC_COMPRESSED_SIZE 0x10 + +/* The offset of GRUB_INSTALL_DOS_PART. */ +#define GRUB_KERNEL_I386_PC_INSTALL_DOS_PART 0x14 + +/* The offset of GRUB_INSTALL_BSD_PART. */ +#define GRUB_KERNEL_I386_PC_INSTALL_BSD_PART 0x18 + +/* Offset of reed_solomon_redundancy. */ +#define GRUB_KERNEL_I386_PC_REED_SOLOMON_REDUNDANCY 0x1c + +/* The size of the first region which won't be compressed. */ +#define GRUB_KERNEL_I386_PC_RAW_SIZE 0xca4 + +#define GRUB_KERNEL_I386_PC_NO_REED_SOLOMON_PART 0x70c + +/* The offset of GRUB_PREFIX. */ +#define GRUB_KERNEL_I386_PC_PREFIX GRUB_KERNEL_I386_PC_RAW_SIZE + +/* End of the data section. */ +#define GRUB_KERNEL_I386_PC_PREFIX_END (GRUB_KERNEL_I386_PC_PREFIX + 0x40) + +/* The segment where the kernel is loaded. */ +#define GRUB_BOOT_I386_PC_KERNEL_SEG 0x800 + +#define GRUB_KERNEL_I386_PC_LINK_ADDR 0x8200 + +/* The upper memory area (starting at 640 kiB). */ +#define GRUB_MEMORY_I386_PC_UPPER 0xa0000 +#define GRUB_MEMORY_I386_QEMU_UPPER GRUB_MEMORY_I386_PC_UPPER + +/* The offset of GRUB_CORE_ENTRY_ADDR. */ +#define GRUB_BOOT_I386_QEMU_CORE_ENTRY_ADDR 0x4 + +/* The offset of GRUB_CORE_ENTRY_ADDR. */ +#define GRUB_KERNEL_I386_QEMU_CORE_ENTRY_ADDR 0x8 + +/* The offset of GRUB_KERNEL_IMAGE_SIZE. */ +#define GRUB_KERNEL_I386_QEMU_KERNEL_IMAGE_SIZE 0xc + +/* The offset of GRUB_PREFIX. */ +#define GRUB_KERNEL_I386_QEMU_PREFIX 0x10 + +/* End of the data section. */ +#define GRUB_KERNEL_I386_QEMU_PREFIX_END 0x50 + +#define GRUB_KERNEL_I386_QEMU_LINK_ADDR 0x8200 + +/* The offset of GRUB_TOTAL_MODULE_SIZE. */ +#define GRUB_KERNEL_SPARC64_IEEE1275_TOTAL_MODULE_SIZE 0x8 + +/* The offset of GRUB_KERNEL_IMAGE_SIZE. */ +#define GRUB_KERNEL_SPARC64_IEEE1275_KERNEL_IMAGE_SIZE 0xc + +/* The offset of GRUB_COMPRESSED_SIZE. */ +#define GRUB_KERNEL_SPARC64_IEEE1275_COMPRESSED_SIZE 0x10 + +/* The offset of GRUB_PREFIX. */ +#define GRUB_KERNEL_SPARC64_IEEE1275_PREFIX 0x14 + +/* End of the data section. */ +#define GRUB_KERNEL_SPARC64_IEEE1275_PREFIX_END 0x114 + +#define GRUB_BOOT_SPARC64_IEEE1275_LIST_SIZE 12 + +#define GRUB_BOOT_SPARC64_IEEE1275_IMAGE_ADDRESS 0x4400 +#define GRUB_KERNEL_SPARC64_IEEE1275_RAW_SIZE 0 +#define GRUB_KERNEL_SPARC64_IEEE1275_LINK_ADDR 0x4400 + +#define GRUB_KERNEL_POWERPC_IEEE1275_PREFIX 0x4 +#define GRUB_KERNEL_POWERPC_IEEE1275_PREFIX_END 0x44 +#define GRUB_KERNEL_POWERPC_IEEE1275_LINK_ALIGN 4 +#define GRUB_KERNEL_POWERPC_IEEE1275_LINK_ADDR 0x200000 + +#define GRUB_KERNEL_MIPS_YEELOONG_LINK_ADDR 0x80200000 + +#define GRUB_KERNEL_MIPS_YEELOONG_LINK_ALIGN 32 + +#define GRUB_KERNEL_MIPS_YEELOONG_COMPRESSED_SIZE 0x8 +#define GRUB_KERNEL_MIPS_YEELOONG_UNCOMPRESSED_SIZE 0xc + +#define GRUB_KERNEL_MIPS_YEELOONG_TOTAL_MODULE_SIZE 0x08 +#define GRUB_KERNEL_MIPS_YEELOONG_PREFIX 0x0c +#define GRUB_KERNEL_MIPS_YEELOONG_PREFIX_END 0x54 + +/* The offset of GRUB_PREFIX. */ +#define GRUB_KERNEL_I386_EFI_PREFIX 0x8 + +/* End of the data section. */ +#define GRUB_KERNEL_I386_EFI_PREFIX_END 0x50 + +/* The offset of GRUB_PREFIX. */ +#define GRUB_KERNEL_X86_64_EFI_PREFIX 0x8 + +/* End of the data section. */ +#define GRUB_KERNEL_X86_64_EFI_PREFIX_END 0x50 + +#define GRUB_KERNEL_I386_COREBOOT_PREFIX 0x2 +#define GRUB_KERNEL_I386_COREBOOT_PREFIX_END 0x42 +#define GRUB_KERNEL_I386_COREBOOT_LINK_ADDR 0x8200 + +#define GRUB_KERNEL_I386_MULTIBOOT_PREFIX GRUB_KERNEL_I386_COREBOOT_PREFIX +#define GRUB_KERNEL_I386_MULTIBOOT_PREFIX_END GRUB_KERNEL_I386_COREBOOT_PREFIX_END + +#define GRUB_KERNEL_I386_IEEE1275_PREFIX 0x2 +#define GRUB_KERNEL_I386_IEEE1275_PREFIX_END 0x42 +#define GRUB_KERNEL_I386_IEEE1275_LINK_ADDR 0x10000 + +#define GRUB_KERNEL_I386_IEEE1275_MOD_ALIGN 0x1000 +#define GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN 0x1 +#define GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN + +/* Non-zero value is only needed for PowerMacs. */ +#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0 +#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP 0x0 + +#define GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN 0x1000 + +#define GRUB_KERNEL_MIPS_YEELOONG_MOD_ALIGN 0x1 + +/* Minimal gap between _end and the start of the modules. It's a hack + for PowerMac to prevent "CLAIM failed" error. The real fix is to + rewrite grub-mkimage to generate valid ELF files. */ +#define GRUB_KERNEL_POWERPC_IEEE1275_MOD_GAP 0x8000 + +#ifdef GRUB_MACHINE +#define GRUB_OFFSETS_CONCAT_(a,b,c) a ## b ## c +#define GRUB_OFFSETS_CONCAT(a,b,c) GRUB_OFFSETS_CONCAT_(a,b,c) +#define GRUB_KERNEL_MACHINE_MOD_ALIGN GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _MOD_ALIGN) +#define GRUB_KERNEL_MACHINE_MOD_GAP GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _MOD_GAP) +#define GRUB_KERNEL_MACHINE_TOTAL_MODULE_SIZE GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _TOTAL_MODULE_SIZE) +#define GRUB_KERNEL_MACHINE_KERNEL_IMAGE_SIZE GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _KERNEL_IMAGE_SIZE) +#define GRUB_KERNEL_MACHINE_COMPRESSED_SIZE GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _COMPRESSED_SIZE) +#define GRUB_KERNEL_MACHINE_UNCOMPRESSED_SIZE GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _UNCOMPRESSED_SIZE) + +#define GRUB_KERNEL_MACHINE_PREFIX GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _PREFIX) +#define GRUB_KERNEL_MACHINE_PREFIX_END GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _PREFIX_END) +#define GRUB_BOOT_MACHINE_KERNEL_SEG GRUB_OFFSETS_CONCAT (GRUB_BOOT_, GRUB_MACHINE, _KERNEL_SEG) +#define GRUB_MEMORY_MACHINE_UPPER GRUB_OFFSETS_CONCAT (GRUB_MEMORY_, GRUB_MACHINE, _UPPER) +#define GRUB_KERNEL_MACHINE_RAW_SIZE GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _RAW_SIZE) +#define GRUB_KERNEL_MACHINE_INSTALL_BSD_PART GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _INSTALL_BSD_PART) +#define GRUB_KERNEL_MACHINE_INSTALL_DOS_PART GRUB_OFFSETS_CONCAT (GRUB_KERNEL_, GRUB_MACHINE, _INSTALL_DOS_PART) +#endif + +#ifndef ASM_FILE +struct grub_pc_bios_boot_blocklist +{ + grub_uint64_t start; + grub_uint16_t len; + grub_uint16_t segment; +} __attribute__ ((packed)); +#endif + +#endif diff --git a/libr/fs/p/grub/include/grub/parser.h b/libr/fs/p/grub/include/grub/parser.h new file mode 100644 index 0000000000..de4da05ad1 --- /dev/null +++ b/libr/fs/p/grub/include/grub/parser.h @@ -0,0 +1,91 @@ +/* parser.h - prototypes for the command line parser. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PARSER_HEADER +#define GRUB_PARSER_HEADER 1 + +#include +#include +#include + +/* All the states for the command line. */ +typedef enum + { + GRUB_PARSER_STATE_TEXT = 1, + GRUB_PARSER_STATE_ESC, + GRUB_PARSER_STATE_QUOTE, + GRUB_PARSER_STATE_DQUOTE, + GRUB_PARSER_STATE_VAR, + GRUB_PARSER_STATE_VARNAME, + GRUB_PARSER_STATE_VARNAME2, + GRUB_PARSER_STATE_QVAR, + GRUB_PARSER_STATE_QVARNAME, + GRUB_PARSER_STATE_QVARNAME2 + } grub_parser_state_t; + +/* A single state transition. */ +struct grub_parser_state_transition +{ + /* The state that is looked up. */ + grub_parser_state_t from_state; + + /* The next state, determined by FROM_STATE and INPUT. */ + grub_parser_state_t to_state; + + /* The input that will determine the next state from FROM_STATE. */ + char input; + + /* If set to 1, the input is valid and should be used. */ + int keep_value; +}; + +/* Determines the state following STATE, determined by C. */ +grub_parser_state_t +EXPORT_FUNC (grub_parser_cmdline_state) (grub_parser_state_t state, + char c, char *result); + +grub_err_t +EXPORT_FUNC (grub_parser_split_cmdline) (const char *cmdline, + grub_reader_getline_t getline, + int *argc, char ***argv); + +struct grub_parser +{ + /* The next parser. */ + struct grub_parser *next; + + /* The parser name. */ + const char *name; + + /* Initialize the parser. */ + grub_err_t (*init) (void); + + /* Clean up the parser. */ + grub_err_t (*fini) (void); + + grub_err_t (*parse_line) (char *line, grub_reader_getline_t getline); +}; +typedef struct grub_parser *grub_parser_t; + +grub_err_t grub_parser_execute (char *source); + +grub_err_t +grub_rescue_parse_line (char *line, grub_reader_getline_t getline); + +#endif /* ! GRUB_PARSER_HEADER */ diff --git a/libr/fs/p/grub/include/grub/partition.h b/libr/fs/p/grub/include/grub/partition.h new file mode 100644 index 0000000000..e7e00ef7fa --- /dev/null +++ b/libr/fs/p/grub/include/grub/partition.h @@ -0,0 +1,134 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2004,2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PART_HEADER +#define GRUB_PART_HEADER 1 + +#include +#include + +struct grub_disk; + +typedef struct grub_partition *grub_partition_t; + +#ifdef GRUB_UTIL +typedef enum +{ + GRUB_EMBED_PCBIOS +} grub_embed_type_t; +#endif + +/* Partition map type. */ +struct grub_partition_map +{ + /* The next partition map type. */ + struct grub_partition_map *next; + + /* The name of the partition map type. */ + const char *name; + + /* Call HOOK with each partition, until HOOK returns non-zero. */ + grub_err_t (*iterate) (struct grub_disk *disk, + int (*hook) (struct grub_disk *disk, + const grub_partition_t partition)); +#ifdef GRUB_UTIL + /* Determine sectors available for embedding. */ + grub_err_t (*embed) (struct grub_disk *disk, unsigned int *nsectors, + grub_embed_type_t embed_type, + grub_disk_addr_t **sectors); +#endif +}; +typedef struct grub_partition_map *grub_partition_map_t; + +/* Partition description. */ +struct grub_partition +{ + /* The partition number. */ + int number; + + /* The start sector (relative to parent). */ + grub_disk_addr_t start; + + /* The length in sector units. */ + grub_uint64_t len; + + /* The offset of the partition table. */ + grub_disk_addr_t offset; + + /* The index of this partition in the partition table. */ + int index; + + /* Parent partition (physically contains this partition). */ + struct grub_partition *parent; + + /* The type partition map. */ + grub_partition_map_t partmap; + + /* The type of partition whne it's on MSDOS. + Used for embedding detection. */ + grub_uint8_t msdostype; +}; + +grub_partition_t EXPORT_FUNC(grub_partition_probe) (struct grub_disk *disk, + const char *str); +int EXPORT_FUNC(grub_partition_iterate) (struct grub_disk *disk, + int (*hook) (struct grub_disk *disk, + const grub_partition_t partition)); +char *EXPORT_FUNC(grub_partition_get_name) (const grub_partition_t partition); + + +extern grub_partition_map_t EXPORT_VAR(grub_partition_map_list); + +#ifndef GRUB_LST_GENERATOR +static inline void +grub_partition_map_register (grub_partition_map_t partmap) +{ + grub_list_push (GRUB_AS_LIST_P (&grub_partition_map_list), + GRUB_AS_LIST (partmap)); +} +#endif + +static inline void +grub_partition_map_unregister (grub_partition_map_t partmap) +{ + grub_list_remove (GRUB_AS_LIST_P (&grub_partition_map_list), + GRUB_AS_LIST (partmap)); +} + +#define FOR_PARTITION_MAPS(var) FOR_LIST_ELEMENTS((var), (grub_partition_map_list)) + + +static inline grub_disk_addr_t +grub_partition_get_start (const grub_partition_t p) +{ + grub_partition_t part; + grub_uint64_t part_start = 0; + + for (part = p; part; part = part->parent) + part_start += part->start; + + return part_start; +} + +static inline grub_uint64_t +grub_partition_get_len (const grub_partition_t p) +{ + return p->len; +} + +#endif /* ! GRUB_PART_HEADER */ diff --git a/libr/fs/p/grub/include/grub/parttool.h b/libr/fs/p/grub/include/grub/parttool.h new file mode 100644 index 0000000000..8291e11614 --- /dev/null +++ b/libr/fs/p/grub/include/grub/parttool.h @@ -0,0 +1,58 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PARTTOOL_HEADER +#define GRUB_PARTTOOL_HEADER 1 + +struct grub_parttool_argdesc +{ + char *name; + char *desc; + enum {GRUB_PARTTOOL_ARG_END, GRUB_PARTTOOL_ARG_BOOL, GRUB_PARTTOOL_ARG_VAL} + type; +}; + +struct grub_parttool_args +{ + int set; + union + { + int bool; + char *str; + }; +}; + +typedef grub_err_t (*grub_parttool_function_t) (const grub_device_t dev, + const struct grub_parttool_args *args); + +struct grub_parttool +{ + struct grub_parttool *next; + char *name; + int handle; + int nargs; + struct grub_parttool_argdesc *args; + grub_parttool_function_t func; +}; + +int grub_parttool_register(const char *part_name, + const grub_parttool_function_t func, + const struct grub_parttool_argdesc *args); +void grub_parttool_unregister (int handle); + +#endif /* ! GRUB_PARTTOOL_HEADER*/ diff --git a/libr/fs/p/grub/include/grub/pci.h b/libr/fs/p/grub/include/grub/pci.h new file mode 100644 index 0000000000..f34e3d907f --- /dev/null +++ b/libr/fs/p/grub/include/grub/pci.h @@ -0,0 +1,137 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PCI_H +#define GRUB_PCI_H 1 + +#ifndef ASM_FILE +#include +#include +#endif + +#define GRUB_PCI_ADDR_SPACE_MASK 0x01 +#define GRUB_PCI_ADDR_SPACE_MEMORY 0x00 +#define GRUB_PCI_ADDR_SPACE_IO 0x01 + +#define GRUB_PCI_ADDR_MEM_TYPE_MASK 0x06 +#define GRUB_PCI_ADDR_MEM_TYPE_32 0x00 /* 32 bit address */ +#define GRUB_PCI_ADDR_MEM_TYPE_1M 0x02 /* Below 1M [obsolete] */ +#define GRUB_PCI_ADDR_MEM_TYPE_64 0x04 /* 64 bit address */ +#define GRUB_PCI_ADDR_MEM_PREFETCH 0x08 /* prefetchable */ + +#define GRUB_PCI_ADDR_MEM_MASK ~0xf +#define GRUB_PCI_ADDR_IO_MASK ~0x03 + +#define GRUB_PCI_REG_PCI_ID 0x00 +#define GRUB_PCI_REG_VENDOR 0x00 +#define GRUB_PCI_REG_DEVICE 0x02 +#define GRUB_PCI_REG_COMMAND 0x04 +#define GRUB_PCI_REG_STATUS 0x06 +#define GRUB_PCI_REG_REVISION 0x08 +#define GRUB_PCI_REG_CLASS 0x08 +#define GRUB_PCI_REG_CACHELINE 0x0c +#define GRUB_PCI_REG_LAT_TIMER 0x0d +#define GRUB_PCI_REG_HEADER_TYPE 0x0e +#define GRUB_PCI_REG_BIST 0x0f +#define GRUB_PCI_REG_ADDRESSES 0x10 + +/* Beware that 64-bit address takes 2 registers. */ +#define GRUB_PCI_REG_ADDRESS_REG0 0x10 +#define GRUB_PCI_REG_ADDRESS_REG1 0x14 +#define GRUB_PCI_REG_ADDRESS_REG2 0x18 +#define GRUB_PCI_REG_ADDRESS_REG3 0x1c +#define GRUB_PCI_REG_ADDRESS_REG4 0x20 +#define GRUB_PCI_REG_ADDRESS_REG5 0x24 + +#define GRUB_PCI_REG_CIS_POINTER 0x28 +#define GRUB_PCI_REG_SUBVENDOR 0x2c +#define GRUB_PCI_REG_SUBSYSTEM 0x2e +#define GRUB_PCI_REG_ROM_ADDRESS 0x30 +#define GRUB_PCI_REG_CAP_POINTER 0x34 +#define GRUB_PCI_REG_IRQ_LINE 0x3c +#define GRUB_PCI_REG_IRQ_PIN 0x3d +#define GRUB_PCI_REG_MIN_GNT 0x3e +#define GRUB_PCI_REG_MAX_LAT 0x3f + +#define GRUB_PCI_COMMAND_IO_ENABLED 0x0001 +#define GRUB_PCI_COMMAND_MEM_ENABLED 0x0002 +#define GRUB_PCI_COMMAND_BUS_MASTER 0x0004 +#define GRUB_PCI_COMMAND_PARITY_ERROR 0x0040 +#define GRUB_PCI_COMMAND_SERR_ENABLE 0x0100 + +#define GRUB_PCI_STATUS_CAPABILITIES 0x0010 +#define GRUB_PCI_STATUS_66MHZ_CAPABLE 0x0020 +#define GRUB_PCI_STATUS_FAST_B2B_CAPABLE 0x0080 + +#define GRUB_PCI_STATUS_DEVSEL_TIMING_SHIFT 9 +#define GRUB_PCI_STATUS_DEVSEL_TIMING_MASK 0x0600 +#define GRUB_PCI_CLASS_SUBCLASS_VGA 0x0300 + +#ifndef ASM_FILE +typedef grub_uint32_t grub_pci_id_t; + +#ifdef GRUB_MACHINE_EMU +#include +#else +typedef grub_uint32_t grub_pci_address_t; +struct grub_pci_device +{ + int bus; + int device; + int function; +}; +typedef struct grub_pci_device grub_pci_device_t; +static inline int +grub_pci_get_bus (grub_pci_device_t dev) +{ + return dev.bus; +} + +static inline int +grub_pci_get_device (grub_pci_device_t dev) +{ + return dev.device; +} + +static inline int +grub_pci_get_function (grub_pci_device_t dev) +{ + return dev.function; +} +#include +#endif + +typedef int NESTED_FUNC_ATTR (*grub_pci_iteratefunc_t) + (grub_pci_device_t dev, grub_pci_id_t pciid); + +grub_pci_address_t EXPORT_FUNC(grub_pci_make_address) (grub_pci_device_t dev, + int reg); + +void EXPORT_FUNC(grub_pci_iterate) (grub_pci_iteratefunc_t hook); + +struct grub_pci_dma_chunk; + +struct grub_pci_dma_chunk *EXPORT_FUNC(grub_memalign_dma32) (grub_size_t align, + grub_size_t size); +void EXPORT_FUNC(grub_dma_free) (struct grub_pci_dma_chunk *ch); +volatile void *EXPORT_FUNC(grub_dma_get_virt) (struct grub_pci_dma_chunk *ch); +grub_uint32_t EXPORT_FUNC(grub_dma_get_phys) (struct grub_pci_dma_chunk *ch); + +#endif + +#endif /* GRUB_PCI_H */ diff --git a/libr/fs/p/grub/include/grub/pciutils.h b/libr/fs/p/grub/include/grub/pciutils.h new file mode 100644 index 0000000000..36d47e5c86 --- /dev/null +++ b/libr/fs/p/grub/include/grub/pciutils.h @@ -0,0 +1,103 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_PCIUTILS_H +#define GRUB_PCIUTILS_H 1 + +#include + +typedef struct pci_device *grub_pci_device_t; + +static inline int +grub_pci_get_bus (grub_pci_device_t dev) +{ + return dev->bus; +} + +static inline int +grub_pci_get_device (grub_pci_device_t dev) +{ + return dev->dev; +} + +static inline int +grub_pci_get_function (grub_pci_device_t dev) +{ + return dev->func; +} + +struct grub_pci_address +{ + grub_pci_device_t dev; + int pos; +}; + +typedef struct grub_pci_address grub_pci_address_t; + +static inline grub_uint32_t +grub_pci_read (grub_pci_address_t addr) +{ + grub_uint32_t ret; + pci_device_cfg_read_u32 (addr.dev, &ret, addr.pos); + return ret; +} + +static inline grub_uint16_t +grub_pci_read_word (grub_pci_address_t addr) +{ + grub_uint16_t ret; + pci_device_cfg_read_u16 (addr.dev, &ret, addr.pos); + return ret; +} + +static inline grub_uint8_t +grub_pci_read_byte (grub_pci_address_t addr) +{ + grub_uint8_t ret; + pci_device_cfg_read_u8 (addr.dev, &ret, addr.pos); + return ret; +} + +static inline void +grub_pci_write (grub_pci_address_t addr, grub_uint32_t data) +{ + pci_device_cfg_write_u32 (addr.dev, data, addr.pos); +} + +static inline void +grub_pci_write_word (grub_pci_address_t addr, grub_uint16_t data) +{ + pci_device_cfg_write_u16 (addr.dev, data, addr.pos); +} + +static inline void +grub_pci_write_byte (grub_pci_address_t addr, grub_uint8_t data) +{ + pci_device_cfg_write_u8 (addr.dev, data, addr.pos); +} + +void * +grub_pci_device_map_range (grub_pci_device_t dev, grub_addr_t base, + grub_size_t size); + +void +grub_pci_device_unmap_range (grub_pci_device_t dev, void *mem, + grub_size_t size); + + +#endif /* GRUB_PCIUTILS_H */ diff --git a/libr/fs/p/grub/include/grub/raid.h b/libr/fs/p/grub/include/grub/raid.h new file mode 100644 index 0000000000..d5853639d5 --- /dev/null +++ b/libr/fs/p/grub/include/grub/raid.h @@ -0,0 +1,96 @@ +/* raid.h - On disk structures for RAID. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006,2007,2008,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_RAID_H +#define GRUB_RAID_H 1 + +#include + +#define GRUB_RAID_LAYOUT_LEFT_ASYMMETRIC 0 +#define GRUB_RAID_LAYOUT_RIGHT_ASYMMETRIC 1 +#define GRUB_RAID_LAYOUT_LEFT_SYMMETRIC 2 +#define GRUB_RAID_LAYOUT_RIGHT_SYMMETRIC 3 + +#define GRUB_RAID_LAYOUT_RIGHT_MASK 1 +#define GRUB_RAID_LAYOUT_SYMMETRIC_MASK 2 + +struct grub_raid_member +{ + grub_disk_t device; /* Array of total_devs devices. */ + grub_disk_addr_t start_sector; + /* Start of each device, in 512 byte sectors. */ +}; + +struct grub_raid_array +{ + int number; /* The device number, taken from md_minor so we + are consistent with the device name in + Linux. */ + int level; /* RAID levels, only 0, 1 or 5 at the moment. */ + int layout; /* Layout for RAID 5/6. */ + unsigned int total_devs; /* Total number of devices in the array. */ + grub_size_t chunk_size; /* The size of a chunk, in 512 byte sectors. */ + grub_uint64_t disk_size; /* Size of an individual disk, in 512 byte + sectors. */ + unsigned int index; /* Index of current device. */ + int uuid_len; /* The length of uuid. */ + char *uuid; /* The UUID of the device. */ + + /* The following field is setup by the caller. */ + char *name; /* That will be "md". */ + unsigned int nr_devs; /* The number of devices we've found so far. */ + unsigned int allocated_devs; + struct grub_raid_member *members; + struct grub_raid_array *next; + +#ifdef GRUB_UTIL + struct grub_raid *driver; +#endif +}; + +struct grub_raid +{ + const char *name; + + grub_err_t (*detect) (grub_disk_t disk, struct grub_raid_array *array, + grub_disk_addr_t *start_sector); + + struct grub_raid *next; +}; +typedef struct grub_raid *grub_raid_t; + +void grub_raid_register (grub_raid_t raid); +void grub_raid_unregister (grub_raid_t raid); + +void grub_raid_block_xor (char *buf1, const char *buf2, int size); + +typedef grub_err_t (*grub_raid5_recover_func_t) (struct grub_raid_array *array, + int disknr, char *buf, + grub_disk_addr_t sector, + int size); + +typedef grub_err_t (*grub_raid6_recover_func_t) (struct grub_raid_array *array, + int disknr, int p, char *buf, + grub_disk_addr_t sector, + int size); + +extern grub_raid5_recover_func_t grub_raid5_recover_func; +extern grub_raid6_recover_func_t grub_raid6_recover_func; + +#endif /* ! GRUB_RAID_H */ diff --git a/libr/fs/p/grub/include/grub/reader.h b/libr/fs/p/grub/include/grub/reader.h new file mode 100644 index 0000000000..fd72a32526 --- /dev/null +++ b/libr/fs/p/grub/include/grub/reader.h @@ -0,0 +1,29 @@ +/* reader.h - prototypes for command line reader. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_READER_HEADER +#define GRUB_READER_HEADER 1 + +#include + +typedef grub_err_t (*grub_reader_getline_t) (char **, int); + +void grub_rescue_run (void); + +#endif /* ! GRUB_READER_HEADER */ diff --git a/libr/fs/p/grub/include/grub/reed_solomon.h b/libr/fs/p/grub/include/grub/reed_solomon.h new file mode 100644 index 0000000000..596dff2462 --- /dev/null +++ b/libr/fs/p/grub/include/grub/reed_solomon.h @@ -0,0 +1,30 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_REED_SOLOMON_HEADER +#define GRUB_REED_SOLOMON_HEADER 1 + +void +grub_reed_solomon_add_redundancy (void *buffer, grub_size_t data_size, + grub_size_t redundancy); + +void +grub_reed_solomon_recover (void *buffer, grub_size_t data_size, + grub_size_t redundancy); + +#endif diff --git a/libr/fs/p/grub/include/grub/relocator.h b/libr/fs/p/grub/include/grub/relocator.h new file mode 100644 index 0000000000..653a00eba7 --- /dev/null +++ b/libr/fs/p/grub/include/grub/relocator.h @@ -0,0 +1,58 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_RELOCATOR_HEADER +#define GRUB_RELOCATOR_HEADER 1 + +#include +#include +#include +#include + +struct grub_relocator; +struct grub_relocator_chunk; +typedef const struct grub_relocator_chunk *grub_relocator_chunk_t; + +struct grub_relocator *grub_relocator_new (void); + +grub_err_t +grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, + grub_relocator_chunk_t *out, + grub_phys_addr_t target, grub_size_t size); + +void * +get_virtual_current_address (grub_relocator_chunk_t in); +grub_phys_addr_t +get_physical_target_address (grub_relocator_chunk_t in); + +grub_err_t +grub_relocator_alloc_chunk_align (struct grub_relocator *rel, + grub_relocator_chunk_t *out, + grub_phys_addr_t min_addr, + grub_phys_addr_t max_addr, + grub_size_t size, grub_size_t align, + int preference); + +#define GRUB_RELOCATOR_PREFERENCE_NONE 0 +#define GRUB_RELOCATOR_PREFERENCE_LOW 1 +#define GRUB_RELOCATOR_PREFERENCE_HIGH 2 + +void +grub_relocator_unload (struct grub_relocator *rel); + +#endif diff --git a/libr/fs/p/grub/include/grub/relocator_private.h b/libr/fs/p/grub/include/grub/relocator_private.h new file mode 100644 index 0000000000..1c563cb64e --- /dev/null +++ b/libr/fs/p/grub/include/grub/relocator_private.h @@ -0,0 +1,113 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_RELOCATOR_PRIVATE_HEADER +#define GRUB_RELOCATOR_PRIVATE_HEADER 1 + +#include +#include +#include + +extern grub_size_t grub_relocator_align; +extern grub_size_t grub_relocator_forward_size; +extern grub_size_t grub_relocator_backward_size; +extern grub_size_t grub_relocator_jumper_size; + +void +grub_cpu_relocator_init (void); +grub_err_t +grub_relocator_prepare_relocs (struct grub_relocator *rel, + grub_addr_t addr, + void **relstart, grub_size_t *relsize); +void grub_cpu_relocator_forward (void *rels, void *src, void *tgt, + grub_size_t size); +void grub_cpu_relocator_backward (void *rels, void *src, void *tgt, + grub_size_t size); +void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr); + +/* Remark: GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG = 1 or 2 + aren't supported. */ +#ifdef GRUB_MACHINE_IEEE1275 +#define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 1 +#define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG 0 +#elif defined (GRUB_MACHINE_EFI) +#define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 1 +#define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG 12 +#else +#define GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS 0 +#endif + +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS && GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG != 0 +#define GRUB_RELOCATOR_HAVE_LEFTOVERS 1 +#else +#define GRUB_RELOCATOR_HAVE_LEFTOVERS 0 +#endif + +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +#define GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT (1 << GRUB_RELOCATOR_FIRMWARE_REQUESTS_QUANT_LOG) +#endif + +struct grub_relocator_mmap_event +{ + enum { + IN_REG_START = 0, + IN_REG_END = 1, + REG_BEG_START = 2, + REG_BEG_END = REG_BEG_START | 1, +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + REG_FIRMWARE_START = 4, + REG_FIRMWARE_END = REG_FIRMWARE_START | 1, + /* To track the regions already in heap. */ + FIRMWARE_BLOCK_START = 6, + FIRMWARE_BLOCK_END = FIRMWARE_BLOCK_START | 1, +#endif +#if GRUB_RELOCATOR_HAVE_LEFTOVERS + REG_LEFTOVER_START = 8, + REG_LEFTOVER_END = REG_LEFTOVER_START | 1, +#endif + COLLISION_START = 10, + COLLISION_END = COLLISION_START | 1 + } type; + grub_phys_addr_t pos; + union + { + struct + { + grub_mm_region_t reg; + grub_mm_header_t hancestor; + grub_mm_region_t *regancestor; + grub_mm_header_t head; + }; +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS + struct grub_relocator_fw_leftover *leftover; +#endif + }; +}; + +/* Return 0 on failure, 1 on success. The failure here + can be very time-expensive, so please make sure fill events is accurate. */ +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +int grub_relocator_firmware_alloc_region (grub_phys_addr_t start, + grub_size_t size); +unsigned grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events); +unsigned grub_relocator_firmware_get_max_events (void); +void grub_relocator_firmware_free_region (grub_phys_addr_t start, + grub_size_t size); +#endif + +#endif diff --git a/libr/fs/p/grub/include/grub/search.h b/libr/fs/p/grub/include/grub/search.h new file mode 100644 index 0000000000..d80347df34 --- /dev/null +++ b/libr/fs/p/grub/include/grub/search.h @@ -0,0 +1,29 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_SEARCH_HEADER +#define GRUB_SEARCH_HEADER 1 + +void grub_search_fs_file (const char *key, const char *var, int no_floppy, + char **hints, unsigned nhints); +void grub_search_fs_uuid (const char *key, const char *var, int no_floppy, + char **hints, unsigned nhints); +void grub_search_label (const char *key, const char *var, int no_floppy, + char **hints, unsigned nhints); + +#endif diff --git a/libr/fs/p/grub/include/grub/serial.h b/libr/fs/p/grub/include/grub/serial.h new file mode 100644 index 0000000000..9540bee64e --- /dev/null +++ b/libr/fs/p/grub/include/grub/serial.h @@ -0,0 +1,123 @@ +/* serial.h - serial device interface */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_SERIAL_HEADER +#define GRUB_SERIAL_HEADER 1 + +#include +#include +#include +#include +#include + +struct grub_serial_port; +struct grub_serial_config; + +struct grub_serial_driver +{ + grub_err_t (*configure) (struct grub_serial_port *port, + struct grub_serial_config *config); + int (*fetch) (struct grub_serial_port *port); + void (*put) (struct grub_serial_port *port, const int c); + void (*fini) (struct grub_serial_port *port); +}; + +/* The type of parity. */ +typedef enum + { + GRUB_SERIAL_PARITY_NONE, + GRUB_SERIAL_PARITY_ODD, + GRUB_SERIAL_PARITY_EVEN, + } grub_serial_parity_t; + +typedef enum + { + GRUB_SERIAL_STOP_BITS_1, + GRUB_SERIAL_STOP_BITS_2, + } grub_serial_stop_bits_t; + +struct grub_serial_config +{ + unsigned speed; + int word_len; + grub_serial_parity_t parity; + grub_serial_stop_bits_t stop_bits; +}; + +struct grub_serial_port +{ + struct grub_serial_port *next; + char *name; + struct grub_serial_driver *driver; + struct grub_serial_config config; + int configured; + /* This should be void *data but since serial is useful as an early console + when malloc isn't available it's a union. + */ + union + { + struct + { + grub_port_t port; + int broken; + }; + struct + { + grub_usb_device_t usbdev; + int configno; + int interfno; + char buf[64]; + int bufstart, bufend; + struct grub_usb_desc_endp *in_endp; + struct grub_usb_desc_endp *out_endp; + }; + }; + grub_term_output_t term_out; + grub_term_input_t term_in; +}; + +grub_err_t EXPORT_FUNC(grub_serial_register) (struct grub_serial_port *port); + +void EXPORT_FUNC(grub_serial_unregister) (struct grub_serial_port *port); + + /* Set default settings. */ +static inline grub_err_t +grub_serial_config_defaults (struct grub_serial_port *port) +{ + struct grub_serial_config config = + { +#ifdef GRUB_MACHINE_MIPS_YEELOONG + .speed = 115200, +#else + .speed = 9600, +#endif + .word_len = 8, + .parity = GRUB_SERIAL_PARITY_NONE, + .stop_bits = GRUB_SERIAL_STOP_BITS_1 + }; + + return port->driver->configure (port, &config); +} + +void grub_ns8250_init (void); +char *grub_serial_ns8250_add_port (grub_port_t port); +extern struct grub_serial_driver grub_ns8250_driver; +void EXPORT_FUNC(grub_serial_unregister_driver) (struct grub_serial_driver *driver); + +#endif diff --git a/libr/fs/p/grub/include/grub/setjmp.h b/libr/fs/p/grub/include/grub/setjmp.h new file mode 100644 index 0000000000..70147a7d4f --- /dev/null +++ b/libr/fs/p/grub/include/grub/setjmp.h @@ -0,0 +1,33 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_SETJMP_HEADER +#define GRUB_SETJMP_HEADER 1 + +#if defined(GRUB_UTIL) && !defined(GRUBOF) +#include +typedef jmp_buf grub_jmp_buf; +#define grub_setjmp setjmp +#define grub_longjmp longjmp +#else +/* This must define grub_jmp_buf, and declare grub_setjmp and + grub_longjmp. */ +# include +#endif + +#endif /* ! GRUB_SETJMP_HEADER */ diff --git a/libr/fs/p/grub/include/grub/symbol.h b/libr/fs/p/grub/include/grub/symbol.h new file mode 100644 index 0000000000..c6adb7ee3c --- /dev/null +++ b/libr/fs/p/grub/include/grub/symbol.h @@ -0,0 +1,52 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_SYMBOL_HEADER +#define GRUB_SYMBOL_HEADER 1 + +#include + +/* Apple assembler requires local labels to start with a capital L */ +#define LOCAL(sym) L_ ## sym + +/* Add an underscore to a C symbol in assembler code if needed. */ +#if HAVE_ASM_USCORE +# define EXT_C(sym) _ ## sym +#else +# define EXT_C(sym) sym +#endif + +#if defined (APPLE_CC) +#define FUNCTION(x) .globl EXT_C(x) ; EXT_C(x): +#define VARIABLE(x) .globl EXT_C(x) ; EXT_C(x): +#elif ! defined (__CYGWIN__) && ! defined (__MINGW32__) +#define FUNCTION(x) .globl EXT_C(x) ; .type EXT_C(x), "function" ; EXT_C(x): +#define VARIABLE(x) .globl EXT_C(x) ; .type EXT_C(x), "object" ; EXT_C(x): +#else +/* .type not supported for non-ELF targets. XXX: Check this in configure? */ +#define FUNCTION(x) .globl EXT_C(x) ; .def EXT_C(x); .scl 2; .type 32; .endef; EXT_C(x): +#define VARIABLE(x) .globl EXT_C(x) ; .def EXT_C(x); .scl 2; .type 0; .endef; EXT_C(x): +#endif + +/* Mark an exported symbol. */ +#ifndef GRUB_SYMBOL_GENERATOR +# define EXPORT_FUNC(x) x +# define EXPORT_VAR(x) x +#endif /* ! GRUB_SYMBOL_GENERATOR */ + +#endif /* ! GRUB_SYMBOL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/term.h b/libr/fs/p/grub/include/grub/term.h new file mode 100644 index 0000000000..dbcb2f52cc --- /dev/null +++ b/libr/fs/p/grub/include/grub/term.h @@ -0,0 +1,497 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2005,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_TERM_HEADER +#define GRUB_TERM_HEADER 1 + +#define GRUB_TERM_NO_KEY 0 + +/* Internal codes used by GRUB to represent terminal input. */ +/* Only for keys otherwise not having shifted modification. */ +#define GRUB_TERM_SHIFT 0x01000000 +#define GRUB_TERM_CTRL 0x02000000 +#define GRUB_TERM_ALT 0x04000000 + +/* Keys without associated character. */ +#define GRUB_TERM_EXTENDED 0x00800000 +#define GRUB_TERM_KEY_MASK 0x00ffffff + +#define GRUB_TERM_KEY_LEFT (GRUB_TERM_EXTENDED | 0x4b) +#define GRUB_TERM_KEY_RIGHT (GRUB_TERM_EXTENDED | 0x4d) +#define GRUB_TERM_KEY_UP (GRUB_TERM_EXTENDED | 0x48) +#define GRUB_TERM_KEY_DOWN (GRUB_TERM_EXTENDED | 0x50) +#define GRUB_TERM_KEY_HOME (GRUB_TERM_EXTENDED | 0x47) +#define GRUB_TERM_KEY_END (GRUB_TERM_EXTENDED | 0x4f) +#define GRUB_TERM_KEY_DC (GRUB_TERM_EXTENDED | 0x53) +#define GRUB_TERM_KEY_PPAGE (GRUB_TERM_EXTENDED | 0x49) +#define GRUB_TERM_KEY_NPAGE (GRUB_TERM_EXTENDED | 0x51) +#define GRUB_TERM_KEY_F1 (GRUB_TERM_EXTENDED | 0x3b) +#define GRUB_TERM_KEY_F2 (GRUB_TERM_EXTENDED | 0x3c) +#define GRUB_TERM_KEY_F3 (GRUB_TERM_EXTENDED | 0x3d) +#define GRUB_TERM_KEY_F4 (GRUB_TERM_EXTENDED | 0x3e) +#define GRUB_TERM_KEY_F5 (GRUB_TERM_EXTENDED | 0x3f) +#define GRUB_TERM_KEY_F6 (GRUB_TERM_EXTENDED | 0x40) +#define GRUB_TERM_KEY_F7 (GRUB_TERM_EXTENDED | 0x41) +#define GRUB_TERM_KEY_F8 (GRUB_TERM_EXTENDED | 0x42) +#define GRUB_TERM_KEY_F9 (GRUB_TERM_EXTENDED | 0x43) +#define GRUB_TERM_KEY_F10 (GRUB_TERM_EXTENDED | 0x44) +#define GRUB_TERM_KEY_F11 (GRUB_TERM_EXTENDED | 0x57) +#define GRUB_TERM_KEY_F12 (GRUB_TERM_EXTENDED | 0x58) +#define GRUB_TERM_KEY_INSERT (GRUB_TERM_EXTENDED | 0x52) +#define GRUB_TERM_KEY_CENTER (GRUB_TERM_EXTENDED | 0x4c) + +#define GRUB_TERM_ESC '\e' +#define GRUB_TERM_TAB '\t' +#define GRUB_TERM_BACKSPACE '\b' + +#ifndef ASM_FILE + +#include +#include +#include +#include +#include + +/* These are used to represent the various color states we use. */ +typedef enum + { + /* The color used to display all text that does not use the + user defined colors below. */ + GRUB_TERM_COLOR_STANDARD, + /* The user defined colors for normal text. */ + GRUB_TERM_COLOR_NORMAL, + /* The user defined colors for highlighted text. */ + GRUB_TERM_COLOR_HIGHLIGHT + } +grub_term_color_state; + +/* Flags for representing the capabilities of a terminal. */ +/* Some notes about the flags: + - These flags are used by higher-level functions but not terminals + themselves. + - If a terminal is dumb, you may assume that only putchar, getkey and + checkkey are called. + - Some fancy features (setcolorstate, setcolor and setcursor) can be set + to NULL. */ + +/* Set when input characters shouldn't be echoed back. */ +#define GRUB_TERM_NO_ECHO (1 << 0) +/* Set when the editing feature should be disabled. */ +#define GRUB_TERM_NO_EDIT (1 << 1) +/* Set when the terminal cannot do fancy things. */ +#define GRUB_TERM_DUMB (1 << 2) +/* Which encoding does terminal expect stream to be. */ +#define GRUB_TERM_CODE_TYPE_SHIFT 3 +#define GRUB_TERM_CODE_TYPE_MASK (7 << GRUB_TERM_CODE_TYPE_SHIFT) +/* Only ASCII characters accepted. */ +#define GRUB_TERM_CODE_TYPE_ASCII (0 << GRUB_TERM_CODE_TYPE_SHIFT) +/* Expects CP-437 characters (ASCII + pseudographics). */ +#define GRUB_TERM_CODE_TYPE_CP437 (1 << GRUB_TERM_CODE_TYPE_SHIFT) +/* UTF-8 stream in logical order. Usually used for terminals + which just forward the stream to another computer. */ +#define GRUB_TERM_CODE_TYPE_UTF8_LOGICAL (2 << GRUB_TERM_CODE_TYPE_SHIFT) +/* UTF-8 in visual order. Like UTF-8 logical but for buggy endpoints. */ +#define GRUB_TERM_CODE_TYPE_UTF8_VISUAL (3 << GRUB_TERM_CODE_TYPE_SHIFT) +/* Glyph description in visual order. */ +#define GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS (4 << GRUB_TERM_CODE_TYPE_SHIFT) + + +/* Bitmasks for modifier keys returned by grub_getkeystatus. */ +#define GRUB_TERM_STATUS_RSHIFT (1 << 0) +#define GRUB_TERM_STATUS_LSHIFT (1 << 1) +#define GRUB_TERM_STATUS_RCTRL (1 << 2) +#define GRUB_TERM_STATUS_RALT (1 << 3) +#define GRUB_TERM_STATUS_SCROLL (1 << 4) +#define GRUB_TERM_STATUS_NUM (1 << 5) +#define GRUB_TERM_STATUS_CAPS (1 << 6) +#define GRUB_TERM_STATUS_LCTRL (1 << 8) +#define GRUB_TERM_STATUS_LALT (1 << 9) + +/* Menu-related geometrical constants. */ + +/* The number of lines of "GRUB version..." at the top. */ +#define GRUB_TERM_INFO_HEIGHT 1 + +/* The number of columns/lines between messages/borders/etc. */ +#define GRUB_TERM_MARGIN 1 + +/* The number of columns of scroll information. */ +#define GRUB_TERM_SCROLL_WIDTH 1 + +/* The Y position of the top border. */ +#define GRUB_TERM_TOP_BORDER_Y (GRUB_TERM_MARGIN + GRUB_TERM_INFO_HEIGHT \ + + GRUB_TERM_MARGIN) + +/* The X position of the left border. */ +#define GRUB_TERM_LEFT_BORDER_X GRUB_TERM_MARGIN + +/* The number of lines of messages at the bottom. */ +#define GRUB_TERM_MESSAGE_HEIGHT 8 + +/* The Y position of the first entry. */ +#define GRUB_TERM_FIRST_ENTRY_Y (GRUB_TERM_TOP_BORDER_Y + 1) + +struct grub_term_input +{ + /* The next terminal. */ + struct grub_term_input *next; + + /* The terminal name. */ + const char *name; + + /* Initialize the terminal. */ + grub_err_t (*init) (struct grub_term_input *term); + + /* Clean up the terminal. */ + grub_err_t (*fini) (struct grub_term_input *term); + + /* Get a character if any input character is available. Otherwise return -1 */ + int (*getkey) (struct grub_term_input *term); + + /* Get keyboard modifier status. */ + int (*getkeystatus) (struct grub_term_input *term); + + void *data; +}; +typedef struct grub_term_input *grub_term_input_t; + +struct grub_term_output +{ + /* The next terminal. */ + struct grub_term_output *next; + + /* The terminal name. */ + const char *name; + + /* Initialize the terminal. */ + grub_err_t (*init) (struct grub_term_output *term); + + /* Clean up the terminal. */ + grub_err_t (*fini) (struct grub_term_output *term); + + /* Put a character. C is encoded in Unicode. */ + void (*putchar) (struct grub_term_output *term, + const struct grub_unicode_glyph *c); + + /* Get the number of columns occupied by a given character C. C is + encoded in Unicode. */ + grub_ssize_t (*getcharwidth) (struct grub_term_output *term, + const struct grub_unicode_glyph *c); + + /* Get the screen size. The return value is ((Width << 8) | Height). */ + grub_uint16_t (*getwh) (struct grub_term_output *term); + + /* Get the cursor position. The return value is ((X << 8) | Y). */ + grub_uint16_t (*getxy) (struct grub_term_output *term); + + /* Go to the position (X, Y). */ + void (*gotoxy) (struct grub_term_output *term, + grub_uint8_t x, grub_uint8_t y); + + /* Clear the screen. */ + void (*cls) (struct grub_term_output *term); + + /* Set the current color to be used */ + void (*setcolorstate) (struct grub_term_output *term, + grub_term_color_state state); + + /* Turn on/off the cursor. */ + void (*setcursor) (struct grub_term_output *term, int on); + + /* Update the screen. */ + void (*refresh) (struct grub_term_output *term); + + /* The feature flags defined above. */ + grub_uint32_t flags; + + /* Current color state. */ + grub_uint8_t normal_color; + grub_uint8_t highlight_color; + + void *data; +}; +typedef struct grub_term_output *grub_term_output_t; + +#define GRUB_TERM_DEFAULT_NORMAL_COLOR 0x07 +#define GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR 0x70 +#define GRUB_TERM_DEFAULT_STANDARD_COLOR 0x07 + +extern struct grub_term_output *EXPORT_VAR(grub_term_outputs_disabled); +extern struct grub_term_input *EXPORT_VAR(grub_term_inputs_disabled); +extern struct grub_term_output *EXPORT_VAR(grub_term_outputs); +extern struct grub_term_input *EXPORT_VAR(grub_term_inputs); + +static inline void +grub_term_register_input (const char *name __attribute__ ((unused)), + grub_term_input_t term) +{ + if (grub_term_inputs) + grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs_disabled), + GRUB_AS_LIST (term)); + else + { + /* If this is the first terminal, enable automatically. */ + if (! term->init || term->init (term) == GRUB_ERR_NONE) + grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term)); + } +} + +static inline void +grub_term_register_input_active (const char *name __attribute__ ((unused)), + grub_term_input_t term) +{ + if (! term->init || term->init (term) == GRUB_ERR_NONE) + grub_list_push (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term)); +} + +static inline void +grub_term_register_output (const char *name __attribute__ ((unused)), + grub_term_output_t term) +{ + if (grub_term_outputs) + grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs_disabled), + GRUB_AS_LIST (term)); + else + { + /* If this is the first terminal, enable automatically. */ + if (! term->init || term->init (term) == GRUB_ERR_NONE) + grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs), + GRUB_AS_LIST (term)); + } +} + +static inline void +grub_term_register_output_active (const char *name __attribute__ ((unused)), + grub_term_output_t term) +{ + if (! term->init || term->init (term) == GRUB_ERR_NONE) + grub_list_push (GRUB_AS_LIST_P (&grub_term_outputs), + GRUB_AS_LIST (term)); +} + +static inline void +grub_term_unregister_input (grub_term_input_t term) +{ + grub_list_remove (GRUB_AS_LIST_P (&grub_term_inputs), GRUB_AS_LIST (term)); + grub_list_remove (GRUB_AS_LIST_P (&grub_term_inputs_disabled), + GRUB_AS_LIST (term)); +} + +static inline void +grub_term_unregister_output (grub_term_output_t term) +{ + grub_list_remove (GRUB_AS_LIST_P (&grub_term_outputs), GRUB_AS_LIST (term)); + grub_list_remove (GRUB_AS_LIST_P (&(grub_term_outputs_disabled)), + GRUB_AS_LIST (term)); +} + +#define FOR_ACTIVE_TERM_INPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_inputs)) +#define FOR_DISABLED_TERM_INPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_inputs_disabled)) +#define FOR_ACTIVE_TERM_OUTPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_outputs)) +#define FOR_DISABLED_TERM_OUTPUTS(var) FOR_LIST_ELEMENTS((var), (grub_term_outputs_disabled)) + +void grub_putcode (grub_uint32_t code, struct grub_term_output *term); +int EXPORT_FUNC(grub_getkey) (void); +int EXPORT_FUNC(grub_checkkey) (void); +void grub_cls (void); +void EXPORT_FUNC(grub_refresh) (void); +void grub_puts_terminal (const char *str, struct grub_term_output *term); +grub_uint16_t *grub_term_save_pos (void); +void grub_term_restore_pos (grub_uint16_t *pos); + +static inline unsigned grub_term_width (struct grub_term_output *term) +{ + return ((term->getwh(term)&0xFF00)>>8); +} + +static inline unsigned grub_term_height (struct grub_term_output *term) +{ + return (term->getwh(term)&0xFF); +} + +/* The width of the border. */ +static inline unsigned +grub_term_border_width (struct grub_term_output *term) +{ + return grub_term_width (term) - GRUB_TERM_MARGIN * 3 - GRUB_TERM_SCROLL_WIDTH; +} + +/* The max column number of an entry. The last "-1" is for a + continuation marker. */ +static inline int +grub_term_entry_width (struct grub_term_output *term) +{ + return grub_term_border_width (term) - 2 - GRUB_TERM_MARGIN * 2 - 1; +} + +/* The height of the border. */ + +static inline unsigned +grub_term_border_height (struct grub_term_output *term) +{ + return grub_term_height (term) - GRUB_TERM_TOP_BORDER_Y + - GRUB_TERM_MESSAGE_HEIGHT; +} + +/* The number of entries shown at a time. */ +static inline int +grub_term_num_entries (struct grub_term_output *term) +{ + return grub_term_border_height (term) - 2; +} + +static inline int +grub_term_cursor_x (struct grub_term_output *term) +{ + return (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term) + - GRUB_TERM_MARGIN - 1); +} + +static inline grub_uint16_t +grub_term_getxy (struct grub_term_output *term) +{ + return term->getxy (term); +} + +static inline void +grub_term_refresh (struct grub_term_output *term) +{ + if (term->refresh) + term->refresh (term); +} + +static inline void +grub_term_gotoxy (struct grub_term_output *term, grub_uint8_t x, grub_uint8_t y) +{ + term->gotoxy (term, x, y); +} + +static inline void +grub_term_setcolorstate (struct grub_term_output *term, + grub_term_color_state state) +{ + if (term->setcolorstate) + term->setcolorstate (term, state); +} + +static inline void +grub_setcolorstate (grub_term_color_state state) +{ + struct grub_term_output *term; + + FOR_ACTIVE_TERM_OUTPUTS(term) + grub_term_setcolorstate (term, state); +} + +/* Set the normal color and the highlight color. The format of each + color is VGA's. */ +static inline void +grub_term_setcolor (struct grub_term_output *term, + grub_uint8_t normal_color, grub_uint8_t highlight_color) +{ + term->normal_color = normal_color; + term->highlight_color = highlight_color; +} + +/* Turn on/off the cursor. */ +static inline void +grub_term_setcursor (struct grub_term_output *term, int on) +{ + if (term->setcursor) + term->setcursor (term, on); +} + +static inline void +grub_term_cls (struct grub_term_output *term) +{ + if (term->cls) + (term->cls) (term); + else + { + grub_putcode ('\n', term); + grub_term_refresh (term); + } +} + +#ifdef HAVE_UNIFONT_WIDTHSPEC + +grub_ssize_t +grub_unicode_estimate_width (const struct grub_unicode_glyph *c); + +#else + +static inline grub_ssize_t +grub_unicode_estimate_width (const struct grub_unicode_glyph *c __attribute__ ((unused))) +{ + if (grub_unicode_get_comb_type (c->base)) + return 0; + return 1; +} + +#endif + +static inline grub_ssize_t +grub_term_getcharwidth (struct grub_term_output *term, + const struct grub_unicode_glyph *c) +{ + if (term->getcharwidth) + return term->getcharwidth (term, c); + else if (((term->flags & GRUB_TERM_CODE_TYPE_MASK) + == GRUB_TERM_CODE_TYPE_UTF8_LOGICAL) + || ((term->flags & GRUB_TERM_CODE_TYPE_MASK) + == GRUB_TERM_CODE_TYPE_UTF8_VISUAL) + || ((term->flags & GRUB_TERM_CODE_TYPE_MASK) + == GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS)) + return grub_unicode_estimate_width (c); + else + return 1; +} + +static inline void +grub_term_getcolor (struct grub_term_output *term, + grub_uint8_t *normal_color, grub_uint8_t *highlight_color) +{ + *normal_color = term->normal_color; + *highlight_color = term->highlight_color; +} + +struct grub_term_autoload +{ + struct grub_term_autoload *next; + char *name; + char *modname; +}; + +extern struct grub_term_autoload *grub_term_input_autoload; +extern struct grub_term_autoload *grub_term_output_autoload; + +static inline void +grub_print_spaces (struct grub_term_output *term, int number_spaces) +{ + while (--number_spaces >= 0) + grub_putcode (' ', term); +} + +extern void (*EXPORT_VAR (grub_term_poll_usb)) (void); + +#define GRUB_TERM_REPEAT_PRE_INTERVAL 400 +#define GRUB_TERM_REPEAT_INTERVAL 50 + +#endif /* ! ASM_FILE */ + +#endif /* ! GRUB_TERM_HEADER */ diff --git a/libr/fs/p/grub/include/grub/terminfo.h b/libr/fs/p/grub/include/grub/terminfo.h new file mode 100644 index 0000000000..5a552b3277 --- /dev/null +++ b/libr/fs/p/grub/include/grub/terminfo.h @@ -0,0 +1,81 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2005,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_TERMINFO_HEADER +#define GRUB_TERMINFO_HEADER 1 + +#include +#include +#include + +char *EXPORT_FUNC(grub_terminfo_get_current) (struct grub_term_output *term); +grub_err_t EXPORT_FUNC(grub_terminfo_set_current) (struct grub_term_output *term, + const char *); + +#define GRUB_TERMINFO_READKEY_MAX_LEN 4 +struct grub_terminfo_input_state +{ + int input_buf[GRUB_TERMINFO_READKEY_MAX_LEN]; + int npending; + int (*readkey) (struct grub_term_input *term); +}; + +struct grub_terminfo_output_state +{ + struct grub_term_output *next; + + char *name; + + char *gotoxy; + char *cls; + char *reverse_video_on; + char *reverse_video_off; + char *cursor_on; + char *cursor_off; + char *setcolor; + + unsigned int width, height; + + unsigned int xpos, ypos; + + void (*put) (struct grub_term_output *term, const int c); +}; + +grub_err_t EXPORT_FUNC(grub_terminfo_output_init) (struct grub_term_output *term); +void EXPORT_FUNC(grub_terminfo_gotoxy) (grub_term_output_t term, + grub_uint8_t x, grub_uint8_t y); +void EXPORT_FUNC(grub_terminfo_cls) (grub_term_output_t term); +grub_uint16_t EXPORT_FUNC (grub_terminfo_getxy) (struct grub_term_output *term); +void EXPORT_FUNC (grub_terminfo_setcursor) (struct grub_term_output *term, + const int on); +void EXPORT_FUNC (grub_terminfo_setcolorstate) (struct grub_term_output *term, + const grub_term_color_state state); + + +grub_err_t EXPORT_FUNC (grub_terminfo_input_init) (struct grub_term_input *term); +int EXPORT_FUNC (grub_terminfo_getkey) (struct grub_term_input *term); +void EXPORT_FUNC (grub_terminfo_putchar) (struct grub_term_output *term, + const struct grub_unicode_glyph *c); +grub_uint16_t EXPORT_FUNC (grub_terminfo_getwh) (struct grub_term_output *term); + + +grub_err_t EXPORT_FUNC (grub_terminfo_output_register) (struct grub_term_output *term, + const char *type); +grub_err_t EXPORT_FUNC (grub_terminfo_output_unregister) (struct grub_term_output *term); + +#endif /* ! GRUB_TERMINFO_HEADER */ diff --git a/libr/fs/p/grub/include/grub/test.h b/libr/fs/p/grub/include/grub/test.h new file mode 100644 index 0000000000..336d3b672b --- /dev/null +++ b/libr/fs/p/grub/include/grub/test.h @@ -0,0 +1,85 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_TEST_HEADER +#define GRUB_TEST_HEADER + +#include +#include +#include +#include +#include + +struct grub_test +{ + /* The next test. */ + struct grub_test *next; + + /* The test name. */ + char *name; + + /* The test main function. */ + void (*main) (void); +}; +typedef struct grub_test *grub_test_t; + +extern grub_test_t grub_test_list; + +void grub_test_register (const char *name, void (*test) (void)); +void grub_test_unregister (const char *name); + +/* Execute a test and print results. */ +int grub_test_run (grub_test_t test); + +/* Test `cond' for nonzero; log failure otherwise. */ +void grub_test_nonzero (int cond, const char *file, + const char *func, grub_uint32_t line, + const char *fmt, ...) + __attribute__ ((format (printf, 5, 6))); + +/* Macro to fill in location details and an optional error message. */ +#define grub_test_assert(cond, ...) \ + grub_test_nonzero(cond, GRUB_FILE, __FUNCTION__, __LINE__, \ + ## __VA_ARGS__, \ + "assert failed: %s", #cond) + +/* Macro to define a unit test. */ +#define GRUB_UNIT_TEST(name, funp) \ + void grub_unit_test_init (void) \ + { \ + grub_test_register (name, funp); \ + } \ + \ + void grub_unit_test_fini (void) \ + { \ + grub_test_unregister (name); \ + } + +/* Macro to define a functional test. */ +#define GRUB_FUNCTIONAL_TEST(name, funp) \ + GRUB_MOD_INIT(name) \ + { \ + grub_test_register (#name, funp); \ + } \ + \ + GRUB_MOD_FINI(name) \ + { \ + grub_test_unregister (#name); \ + } + +#endif /* ! GRUB_TEST_HEADER */ diff --git a/libr/fs/p/grub/include/grub/time.h b/libr/fs/p/grub/include/grub/time.h new file mode 100644 index 0000000000..ae2617edb1 --- /dev/null +++ b/libr/fs/p/grub/include/grub/time.h @@ -0,0 +1,47 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2007, 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef KERNEL_TIME_HEADER +#define KERNEL_TIME_HEADER 1 + +#include +#include +#include + +#if defined (GRUB_MACHINE_EMU) || defined (GRUB_UTIL) +#define GRUB_TICKS_PER_SECOND 100000 +/* Return the real time in ticks. */ +grub_uint32_t EXPORT_FUNC (grub_get_rtc) (void); +#else +#include +#endif + +void EXPORT_FUNC(grub_millisleep) (grub_uint32_t ms); +grub_uint64_t EXPORT_FUNC(grub_get_time_ms) (void); + +grub_uint64_t grub_rtc_get_time_ms (void); + +static __inline void +grub_sleep (grub_uint32_t s) +{ + grub_millisleep (1000 * s); +} + +void grub_install_get_time_ms (grub_uint64_t (*get_time_ms_func) (void)); + +#endif /* ! KERNEL_TIME_HEADER */ diff --git a/libr/fs/p/grub/include/grub/tparm.h b/libr/fs/p/grub/include/grub/tparm.h new file mode 100644 index 0000000000..642a22f906 --- /dev/null +++ b/libr/fs/p/grub/include/grub/tparm.h @@ -0,0 +1,26 @@ +/* tparm.h - parameter formatting of terminfo */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_TPARM_HEADER +#define GRUB_TPARM_HEADER 1 + +/* Function prototypes. */ +char *grub_terminfo_tparm (const char *string, ...); + +#endif /* ! GRUB_TPARM_HEADER */ diff --git a/libr/fs/p/grub/include/grub/trig.h b/libr/fs/p/grub/include/grub/trig.h new file mode 100644 index 0000000000..2512a5f070 --- /dev/null +++ b/libr/fs/p/grub/include/grub/trig.h @@ -0,0 +1,44 @@ +/* trig.h - Trigonometric function support. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_TRIG_HEADER +#define GRUB_TRIG_HEADER 1 + +#define GRUB_TRIG_ANGLE_MAX 256 +#define GRUB_TRIG_ANGLE_MASK 255 +#define GRUB_TRIG_FRACTION_SCALE 16384 + +extern short grub_trig_sintab[]; +extern short grub_trig_costab[]; + +static __inline int +grub_sin (int x) +{ + x &= GRUB_TRIG_ANGLE_MASK; + return grub_trig_sintab[x]; +} + +static __inline int +grub_cos (int x) +{ + x &= GRUB_TRIG_ANGLE_MASK; + return grub_trig_costab[x]; +} + +#endif /* ! GRUB_TRIG_HEADER */ diff --git a/libr/fs/p/grub/include/grub/types.h b/libr/fs/p/grub/include/grub/types.h new file mode 100644 index 0000000000..8632eacd0e --- /dev/null +++ b/libr/fs/p/grub/include/grub/types.h @@ -0,0 +1,218 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_TYPES_HEADER +#define GRUB_TYPES_HEADER 1 + +#include +#include + +#ifdef GRUB_UTIL +# define GRUB_CPU_SIZEOF_VOID_P SIZEOF_VOID_P +# define GRUB_CPU_SIZEOF_LONG SIZEOF_LONG +# ifdef WORDS_BIGENDIAN +# define GRUB_CPU_WORDS_BIGENDIAN 1 +# else +# undef GRUB_CPU_WORDS_BIGENDIAN +# endif +#else /* ! GRUB_UTIL */ +# define GRUB_CPU_SIZEOF_VOID_P GRUB_TARGET_SIZEOF_VOID_P +# define GRUB_CPU_SIZEOF_LONG GRUB_TARGET_SIZEOF_LONG +# ifdef GRUB_TARGET_WORDS_BIGENDIAN +# define GRUB_CPU_WORDS_BIGENDIAN 1 +# else +# undef GRUB_CPU_WORDS_BIGENDIAN +# endif +#endif /* ! GRUB_UTIL */ + +#if GRUB_CPU_SIZEOF_VOID_P != GRUB_CPU_SIZEOF_LONG +# error "This architecture is not supported because sizeof(void *) != sizeof(long)" +#endif + +#if GRUB_CPU_SIZEOF_VOID_P != 4 && GRUB_CPU_SIZEOF_VOID_P != 8 +# error "This architecture is not supported because sizeof(void *) != 4 and sizeof(void *) != 8" +#endif + +#ifndef GRUB_TARGET_WORDSIZE +# if GRUB_TARGET_SIZEOF_VOID_P == 4 +# define GRUB_TARGET_WORDSIZE 32 +# elif GRUB_TARGET_SIZEOF_VOID_P == 8 +# define GRUB_TARGET_WORDSIZE 64 +# endif +#endif + +/* Define various wide integers. */ +typedef signed char grub_int8_t; +typedef short grub_int16_t; +typedef int grub_int32_t; +#if GRUB_CPU_SIZEOF_LONG == 8 +typedef long grub_int64_t; +#else +typedef long long grub_int64_t; +#endif + +typedef unsigned char grub_uint8_t; +typedef unsigned short grub_uint16_t; +typedef unsigned grub_uint32_t; +# define PRIxGRUB_UINT32_T "x" +# define PRIuGRUB_UINT32_T "u" +#if GRUB_CPU_SIZEOF_LONG == 8 +typedef unsigned long grub_uint64_t; +# define PRIxGRUB_UINT64_T "lx" +# define PRIuGRUB_UINT64_T "lu" +#else +typedef unsigned long long grub_uint64_t; +# define PRIxGRUB_UINT64_T "llx" +# define PRIuGRUB_UINT64_T "llu" +#endif + +/* Misc types. */ +#if GRUB_TARGET_SIZEOF_VOID_P == 8 +typedef grub_uint64_t grub_target_addr_t; +typedef grub_uint64_t grub_target_size_t; +typedef grub_int64_t grub_target_ssize_t; +#else +typedef grub_uint32_t grub_target_addr_t; +typedef grub_uint32_t grub_target_size_t; +typedef grub_int32_t grub_target_ssize_t; +#endif + +#if GRUB_CPU_SIZEOF_VOID_P == 8 +typedef grub_uint64_t grub_addr_t; +typedef grub_uint64_t grub_size_t; +typedef grub_int64_t grub_ssize_t; + +# if GRUB_CPU_SIZEOF_LONG == 8 +# define PRIxGRUB_SIZE "lx" +# define PRIuGRUB_SIZE "lu" +# else +# define PRIxGRUB_SIZE "llx" +# define PRIuGRUB_SIZE "llu" +# endif +#else +typedef grub_uint32_t grub_addr_t; +typedef grub_uint32_t grub_size_t; +typedef grub_int32_t grub_ssize_t; + +# define PRIxGRUB_SIZE "x" +# define PRIuGRUB_SIZE "u" +#endif + +#if GRUB_CPU_SIZEOF_LONG == 8 +# define GRUB_ULONG_MAX 18446744073709551615UL +# define GRUB_LONG_MAX 9223372036854775807L +# define GRUB_LONG_MIN (-9223372036854775807L - 1) +#else +# define GRUB_ULONG_MAX 4294967295UL +# define GRUB_LONG_MAX 2147483647L +# define GRUB_LONG_MIN (-2147483647L - 1) +#endif + +#if GRUB_CPU_SIZEOF_VOID_P == 4 +#define UINT_TO_PTR(x) ((void*)(grub_uint32_t)(x)) +#define PTR_TO_UINT64(x) ((grub_uint64_t)(grub_uint32_t)(x)) +#define PTR_TO_UINT32(x) ((grub_uint32_t)(x)) +#else +#define UINT_TO_PTR(x) ((void*)(grub_uint64_t)(x)) +#define PTR_TO_UINT64(x) ((grub_uint64_t)(x)) +#define PTR_TO_UINT32(x) ((grub_uint32_t)(grub_uint64_t)(x)) +#endif + +/* The type for representing a file offset. */ +typedef grub_uint64_t grub_off_t; + +/* The type for representing a disk block address. */ +typedef grub_uint64_t grub_disk_addr_t; + +/* Byte-orders. */ +#define grub_swap_bytes16(x) \ +({ \ + grub_uint16_t _x = (x); \ + (grub_uint16_t) ((_x << 8) | (_x >> 8)); \ +}) + +#define grub_swap_bytes16_compile_time(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8)) +#define grub_swap_bytes32_compile_time(x) ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) & 0xff000000UL) >> 24)) + +#if defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3) +static inline grub_uint32_t grub_swap_bytes32(grub_uint32_t x) +{ + return __builtin_bswap32(x); +} + +static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t x) +{ + return __builtin_bswap64(x); +} +#else /* not gcc 4.3 or newer */ +#define grub_swap_bytes32(x) \ +({ \ + grub_uint32_t _x = (x); \ + (grub_uint32_t) ((_x << 24) \ + | ((_x & (grub_uint32_t) 0xFF00UL) << 8) \ + | ((_x & (grub_uint32_t) 0xFF0000UL) >> 8) \ + | (_x >> 24)); \ +}) + +#define grub_swap_bytes64(x) \ +({ \ + grub_uint64_t _x = (x); \ + (grub_uint64_t) ((_x << 56) \ + | ((_x & (grub_uint64_t) 0xFF00ULL) << 40) \ + | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24) \ + | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8) \ + | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8) \ + | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24) \ + | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40) \ + | (_x >> 56)); \ +}) +#endif /* not gcc 4.3 or newer */ + +#ifdef GRUB_CPU_WORDS_BIGENDIAN +# define grub_cpu_to_le16(x) grub_swap_bytes16(x) +# define grub_cpu_to_le32(x) grub_swap_bytes32(x) +# define grub_cpu_to_le64(x) grub_swap_bytes64(x) +# define grub_le_to_cpu16(x) grub_swap_bytes16(x) +# define grub_le_to_cpu32(x) grub_swap_bytes32(x) +# define grub_le_to_cpu64(x) grub_swap_bytes64(x) +# define grub_cpu_to_be16(x) ((grub_uint16_t) (x)) +# define grub_cpu_to_be32(x) ((grub_uint32_t) (x)) +# define grub_cpu_to_be64(x) ((grub_uint64_t) (x)) +# define grub_be_to_cpu16(x) ((grub_uint16_t) (x)) +# define grub_be_to_cpu32(x) ((grub_uint32_t) (x)) +# define grub_be_to_cpu64(x) ((grub_uint64_t) (x)) +# define grub_cpu_to_le32_compile_time(x) grub_swap_bytes32_compile_time(x) +# define grub_cpu_to_le16_compile_time(x) grub_swap_bytes16_compile_time(x) +#else /* ! WORDS_BIGENDIAN */ +# define grub_cpu_to_le16(x) ((grub_uint16_t) (x)) +# define grub_cpu_to_le32(x) ((grub_uint32_t) (x)) +# define grub_cpu_to_le64(x) ((grub_uint64_t) (x)) +# define grub_le_to_cpu16(x) ((grub_uint16_t) (x)) +# define grub_le_to_cpu32(x) ((grub_uint32_t) (x)) +# define grub_le_to_cpu64(x) ((grub_uint64_t) (x)) +# define grub_cpu_to_be16(x) grub_swap_bytes16(x) +# define grub_cpu_to_be32(x) grub_swap_bytes32(x) +# define grub_cpu_to_be64(x) grub_swap_bytes64(x) +# define grub_be_to_cpu16(x) grub_swap_bytes16(x) +# define grub_be_to_cpu32(x) grub_swap_bytes32(x) +# define grub_be_to_cpu64(x) grub_swap_bytes64(x) +# define grub_cpu_to_le16_compile_time(x) ((grub_uint16_t) (x)) +# define grub_cpu_to_le32_compile_time(x) ((grub_uint32_t) (x)) +#endif /* ! WORDS_BIGENDIAN */ + +#endif /* ! GRUB_TYPES_HEADER */ diff --git a/libr/fs/p/grub/include/grub/unicode.h b/libr/fs/p/grub/include/grub/unicode.h new file mode 100644 index 0000000000..64769258a9 --- /dev/null +++ b/libr/fs/p/grub/include/grub/unicode.h @@ -0,0 +1,272 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_BIDI_HEADER +#define GRUB_BIDI_HEADER 1 + +#include +#include +#include + +struct grub_unicode_bidi_pair +{ + grub_uint32_t key; + grub_uint32_t replace; +}; + +struct grub_unicode_compact_range +{ + grub_uint32_t start:21; + grub_uint32_t end:21; + grub_uint8_t bidi_type:5; + grub_uint8_t comb_type; + grub_uint8_t bidi_mirror:1; + grub_uint8_t join_type:3; +} __attribute__ ((packed)); + +/* Old-style Arabic shaping. Used for "visual UTF-8" and + in grub-mkfont to find variant glyphs in absence of GPOS tables. */ +struct grub_unicode_arabic_shape +{ + grub_uint32_t code; + grub_uint32_t isolated; + grub_uint32_t right_linked; + grub_uint32_t both_linked; + grub_uint32_t left_linked; +}; + +extern struct grub_unicode_arabic_shape grub_unicode_arabic_shapes[]; + +enum grub_bidi_type + { + GRUB_BIDI_TYPE_L = 0, + GRUB_BIDI_TYPE_LRE, + GRUB_BIDI_TYPE_LRO, + GRUB_BIDI_TYPE_R, + GRUB_BIDI_TYPE_AL, + GRUB_BIDI_TYPE_RLE, + GRUB_BIDI_TYPE_RLO, + GRUB_BIDI_TYPE_PDF, + GRUB_BIDI_TYPE_EN, + GRUB_BIDI_TYPE_ES, + GRUB_BIDI_TYPE_ET, + GRUB_BIDI_TYPE_AN, + GRUB_BIDI_TYPE_CS, + GRUB_BIDI_TYPE_NSM, + GRUB_BIDI_TYPE_BN, + GRUB_BIDI_TYPE_B, + GRUB_BIDI_TYPE_S, + GRUB_BIDI_TYPE_WS, + GRUB_BIDI_TYPE_ON + }; + +enum grub_join_type + { + GRUB_JOIN_TYPE_NONJOINING = 0, + GRUB_JOIN_TYPE_LEFT = 1, + GRUB_JOIN_TYPE_RIGHT = 2, + GRUB_JOIN_TYPE_DUAL = 3, + GRUB_JOIN_TYPE_CAUSING = 4, + GRUB_JOIN_TYPE_TRANSPARENT = 5 + }; + +enum grub_comb_type + { + GRUB_UNICODE_COMB_NONE = 0, + GRUB_UNICODE_COMB_OVERLAY = 1, + GRUB_UNICODE_COMB_HEBREW_SHEVA = 10, + GRUB_UNICODE_COMB_HEBREW_HATAF_SEGOL = 11, + GRUB_UNICODE_COMB_HEBREW_HATAF_PATAH = 12, + GRUB_UNICODE_COMB_HEBREW_HATAF_QAMATS = 13, + GRUB_UNICODE_COMB_HEBREW_HIRIQ = 14, + GRUB_UNICODE_COMB_HEBREW_TSERE = 15, + GRUB_UNICODE_COMB_HEBREW_SEGOL = 16, + GRUB_UNICODE_COMB_HEBREW_PATAH = 17, + GRUB_UNICODE_COMB_HEBREW_QAMATS = 18, + GRUB_UNICODE_COMB_HEBREW_HOLAM = 19, + GRUB_UNICODE_COMB_HEBREW_QUBUTS = 20, + GRUB_UNICODE_COMB_HEBREW_DAGESH = 21, + GRUB_UNICODE_COMB_HEBREW_METEG = 22, + GRUB_UNICODE_COMB_HEBREW_RAFE = 23, + GRUB_UNICODE_COMB_HEBREW_SHIN_DOT = 24, + GRUB_UNICODE_COMB_HEBREW_SIN_DOT = 25, + GRUB_UNICODE_COMB_HEBREW_VARIKA = 26, + GRUB_UNICODE_COMB_ARABIC_FATHATAN = 27, + GRUB_UNICODE_COMB_ARABIC_DAMMATAN = 28, + GRUB_UNICODE_COMB_ARABIC_KASRATAN = 29, + GRUB_UNICODE_COMB_ARABIC_FATHAH = 30, + GRUB_UNICODE_COMB_ARABIC_DAMMAH = 31, + GRUB_UNICODE_COMB_ARABIC_KASRA = 32, + GRUB_UNICODE_COMB_ARABIC_SHADDA = 33, + GRUB_UNICODE_COMB_ARABIC_SUKUN = 34, + GRUB_UNICODE_COMB_ARABIC_SUPERSCRIPT_ALIF = 35, + GRUB_UNICODE_COMB_SYRIAC_SUPERSCRIPT_ALAPH = 36, + GRUB_UNICODE_STACK_ATTACHED_BELOW = 202, + GRUB_UNICODE_STACK_ATTACHED_ABOVE = 214, + GRUB_UNICODE_COMB_ATTACHED_ABOVE_RIGHT = 216, + GRUB_UNICODE_STACK_BELOW = 220, + GRUB_UNICODE_COMB_BELOW_RIGHT = 222, + GRUB_UNICODE_COMB_ABOVE_LEFT = 228, + GRUB_UNICODE_STACK_ABOVE = 230, + GRUB_UNICODE_COMB_ABOVE_RIGHT = 232, + GRUB_UNICODE_COMB_YPOGEGRAMMENI = 240, + /* If combining nature is indicated only by class and + not "combining type". */ + GRUB_UNICODE_COMB_ME = 253, + GRUB_UNICODE_COMB_MC = 254, + GRUB_UNICODE_COMB_MN = 255, + }; + +/* This structure describes a glyph as opposed to character. */ +struct grub_unicode_glyph +{ + grub_uint32_t base; + grub_uint16_t variant:9; + grub_uint8_t attributes:5; + grub_size_t ncomb; + struct grub_unicode_combining { + grub_uint32_t code; + enum grub_comb_type type; + } *combining; + /* Hint by unicode subsystem how wide this character usually is. + Real width is determined by font. Set only in UTF-8 stream. */ + int estimated_width; +}; + +#define GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR 0x1 +#define GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT 1 +#define GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED 0x2 +#define GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED \ + (GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED \ + << GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT) +/* Set iff the corresponding joining flags come from ZWJ or ZWNJ. */ +#define GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT 0x8 +#define GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT \ + (GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT \ + << GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT) +#define GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN \ + (GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED \ + | GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED \ + | GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT \ + | GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT) + +enum + { + GRUB_UNICODE_COMBINING_GRAPHEME_JOINER = 0x034f, + GRUB_UNICODE_HEBREW_WAW = 0x05d5, + GRUB_UNICODE_ARABIC_START = 0x0600, + GRUB_UNICODE_ARABIC_END = 0x0700, + GRUB_UNICODE_THAANA_ABAFILI = 0x07a6, + GRUB_UNICODE_THAANA_AABAAFILI = 0x07a7, + GRUB_UNICODE_THAANA_IBIFILI = 0x07a8, + GRUB_UNICODE_THAANA_EEBEEFILI = 0x07a9, + GRUB_UNICODE_THAANA_UBUFILI = 0x07aa, + GRUB_UNICODE_THAANA_OOBOOFILI = 0x07ab, + GRUB_UNICODE_THAANA_EBEFILI = 0x07ac, + GRUB_UNICODE_THAANA_EYBEYFILI = 0x07ad, + GRUB_UNICODE_THAANA_OBOFILI = 0x07ae, + GRUB_UNICODE_THAANA_OABOAFILI = 0x07af, + GRUB_UNICODE_THAANA_SUKUN = 0x07b0, + GRUB_UNICODE_ZWNJ = 0x200c, + GRUB_UNICODE_ZWJ = 0x200d, + GRUB_UNICODE_LEFTARROW = 0x2190, + GRUB_UNICODE_UPARROW = 0x2191, + GRUB_UNICODE_RIGHTARROW = 0x2192, + GRUB_UNICODE_DOWNARROW = 0x2193, + GRUB_UNICODE_LIGHT_HLINE = 0x2500, + GRUB_UNICODE_HLINE = 0x2501, + GRUB_UNICODE_LIGHT_VLINE = 0x2502, + GRUB_UNICODE_VLINE = 0x2503, + GRUB_UNICODE_LIGHT_CORNER_UL = 0x250c, + GRUB_UNICODE_CORNER_UL = 0x250f, + GRUB_UNICODE_LIGHT_CORNER_UR = 0x2510, + GRUB_UNICODE_CORNER_UR = 0x2513, + GRUB_UNICODE_LIGHT_CORNER_LL = 0x2514, + GRUB_UNICODE_CORNER_LL = 0x2517, + GRUB_UNICODE_LIGHT_CORNER_LR = 0x2518, + GRUB_UNICODE_CORNER_LR = 0x251b, + GRUB_UNICODE_BLACK_UP_TRIANGLE = 0x25b2, + GRUB_UNICODE_BLACK_RIGHT_TRIANGLE = 0x25ba, + GRUB_UNICODE_BLACK_DOWN_TRIANGLE = 0x25bc, + GRUB_UNICODE_BLACK_LEFT_TRIANGLE = 0x25c4, + GRUB_UNICODE_VARIATION_SELECTOR_1 = 0xfe00, + GRUB_UNICODE_VARIATION_SELECTOR_16 = 0xfe0f, + GRUB_UNICODE_VARIATION_SELECTOR_17 = 0xe0100, + GRUB_UNICODE_VARIATION_SELECTOR_256 = 0xe01ef + }; + +extern struct grub_unicode_compact_range grub_unicode_compact[]; +extern struct grub_unicode_bidi_pair grub_unicode_bidi_pairs[]; + +#define GRUB_UNICODE_MAX_CACHED_CHAR 0x20000 +/* Unicode mandates an arbitrary limit. */ +#define GRUB_BIDI_MAX_EXPLICIT_LEVEL 61 + +grub_ssize_t +grub_bidi_logical_to_visual (const grub_uint32_t *logical, + grub_size_t logical_len, + struct grub_unicode_glyph **visual_out, + grub_ssize_t (*getcharwidth) (const struct grub_unicode_glyph *visual), + grub_size_t max_width, + grub_size_t start_width); + +enum grub_comb_type +grub_unicode_get_comb_type (grub_uint32_t c); +grub_size_t +grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen, + struct grub_unicode_glyph *out); + +static inline struct grub_unicode_glyph * +grub_unicode_glyph_dup (const struct grub_unicode_glyph *in) +{ + struct grub_unicode_glyph *out = grub_malloc (sizeof (*out)); + if (!out) + return NULL; + grub_memcpy (out, in, sizeof (*in)); + if (in->combining) + { + out->combining = grub_malloc (in->ncomb * sizeof (*in)); + if (!out->combining) + { + grub_free (out); + return NULL; + } + grub_memcpy (out->combining, in->combining, in->ncomb * sizeof (*in)); + } + return out; +} + +static inline struct grub_unicode_glyph * +grub_unicode_glyph_from_code (grub_uint32_t code) +{ + struct grub_unicode_glyph *ret; + ret = grub_zalloc (sizeof (*ret)); + if (!ret) + return NULL; + + ret->base = code; + + return ret; +} + +grub_uint32_t +grub_unicode_mirror_code (grub_uint32_t in); +grub_uint32_t +grub_unicode_shape_code (grub_uint32_t in, grub_uint8_t attr); + +#endif diff --git a/libr/fs/p/grub/include/grub/util/deviceiter.h b/libr/fs/p/grub/include/grub/util/deviceiter.h new file mode 100644 index 0000000000..a8af03cfe7 --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/deviceiter.h @@ -0,0 +1,11 @@ +#ifndef GRUB_DEVICEITER_MACHINE_UTIL_HEADER +#define GRUB_DEVICEITER_MACHINE_UTIL_HEADER 1 + +#include + +void grub_util_iterate_devices (int NESTED_FUNC_ATTR (*hook) (const char *, int), + int floppy_disks); +void grub_util_emit_devicemap_entry (FILE *fp, char *name, int is_floppy, + int *num_fd, int *num_hd); + +#endif /* ! GRUB_DEVICEITER_MACHINE_UTIL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/util/libnvpair.h b/libr/fs/p/grub/include/grub/util/libnvpair.h new file mode 100644 index 0000000000..26f7e9d0f3 --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/libnvpair.h @@ -0,0 +1,39 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LIBNVPAIR_UTIL_HEADER +#define GRUB_LIBNVPAIR_UTIL_HEADER 1 + +#include + +#ifdef HAVE_LIBNVPAIR_H +#include +#else /* ! HAVE_LIBNVPAIR_H */ + +#include /* FILE */ + +typedef void nvlist_t; + +int nvlist_lookup_string (nvlist_t *, const char *, char **); +int nvlist_lookup_nvlist (nvlist_t *, const char *, nvlist_t **); +int nvlist_lookup_nvlist_array (nvlist_t *, const char *, nvlist_t ***, unsigned int *); +void nvlist_print (FILE *, nvlist_t *); + +#endif /* ! HAVE_LIBNVPAIR_H */ + +#endif diff --git a/libr/fs/p/grub/include/grub/util/libzfs.h b/libr/fs/p/grub/include/grub/util/libzfs.h new file mode 100644 index 0000000000..a02caa3358 --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/libzfs.h @@ -0,0 +1,47 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LIBZFS_UTIL_HEADER +#define GRUB_LIBZFS_UTIL_HEADER 1 + +#include + +#ifdef HAVE_LIBZFS_H +#include +#else /* ! HAVE_LIBZFS_H */ + +#include + +typedef void libzfs_handle_t; +typedef void zpool_handle_t; + +extern libzfs_handle_t *libzfs_init (void); +extern void libzfs_fini (libzfs_handle_t *); + +extern zpool_handle_t *zpool_open (libzfs_handle_t *, const char *); +extern void zpool_close (zpool_handle_t *); + +extern int zpool_get_physpath (zpool_handle_t *, const char *); + +extern nvlist_t *zpool_get_config (zpool_handle_t *, nvlist_t **); + +#endif /* ! HAVE_LIBZFS_H */ + +libzfs_handle_t *grub_get_libzfs_handle (void); + +#endif diff --git a/libr/fs/p/grub/include/grub/util/lvm.h b/libr/fs/p/grub/include/grub/util/lvm.h new file mode 100644 index 0000000000..7a4c76c6bb --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/lvm.h @@ -0,0 +1,27 @@ +/* lvm.h - LVM support for GRUB utils. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LVM_UTIL_HEADER +#define GRUB_LVM_UTIL_HEADER 1 + +#ifdef __linux__ +int grub_util_lvm_isvolume (char *name); +#endif + +#endif /* ! GRUB_RAID_UTIL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/util/misc.h b/libr/fs/p/grub/include/grub/util/misc.h new file mode 100644 index 0000000000..48dfbb8686 --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/misc.h @@ -0,0 +1,63 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_UTIL_MISC_HEADER +#define GRUB_UTIL_MISC_HEADER 1 + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +char *grub_util_get_path (const char *dir, const char *file); +size_t grub_util_get_fp_size (FILE *fp); +size_t grub_util_get_image_size (const char *path); +void grub_util_read_at (void *img, size_t len, off_t offset, FILE *fp); +char *grub_util_read_image (const char *path); +void grub_util_load_image (const char *path, char *buf); +void grub_util_write_image (const char *img, size_t size, FILE *out); +void grub_util_write_image_at (const void *img, size_t size, off_t offset, + FILE *out); + +#ifdef __MINGW32__ + +#define fseeko fseeko64 +#define ftello ftello64 + +void sync (void); +int fsync (int fno); +void sleep(int s); + +grub_int64_t grub_util_get_disk_size (char *name); + +#endif + + +char *make_system_path_relative_to_its_root (const char *path); + +char *canonicalize_file_name (const char *path); + +void grub_util_init_nls (void); + +#endif /* ! GRUB_UTIL_MISC_HEADER */ diff --git a/libr/fs/p/grub/include/grub/util/ofpath.h b/libr/fs/p/grub/include/grub/util/ofpath.h new file mode 100644 index 0000000000..b43c523cb2 --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/ofpath.h @@ -0,0 +1,6 @@ +#ifndef GRUB_OFPATH_MACHINE_UTIL_HEADER +#define GRUB_OFPATH_MACHINE_UTIL_HEADER 1 + +char *grub_util_devname_to_ofpath (const char *devname); + +#endif /* ! GRUB_OFPATH_MACHINE_UTIL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/util/raid.h b/libr/fs/p/grub/include/grub/util/raid.h new file mode 100644 index 0000000000..67020bb865 --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/raid.h @@ -0,0 +1,27 @@ +/* raid.h - RAID support for GRUB utils. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_RAID_UTIL_HEADER +#define GRUB_RAID_UTIL_HEADER 1 + +#ifdef __linux__ +char** grub_util_raid_getmembers (char *name); +#endif + +#endif /* ! GRUB_RAID_UTIL_HEADER */ diff --git a/libr/fs/p/grub/include/grub/util/resolve.h b/libr/fs/p/grub/include/grub/util/resolve.h new file mode 100644 index 0000000000..f42df32f9c --- /dev/null +++ b/libr/fs/p/grub/include/grub/util/resolve.h @@ -0,0 +1,35 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_UTIL_RESOLVE_HEADER +#define GRUB_UTIL_RESOLVE_HEADER 1 + +struct grub_util_path_list +{ + const char *name; + struct grub_util_path_list *next; +}; + +/* Resolve the dependencies of the modules MODULES using the information + in the file DEP_LIST_FILE. The directory PREFIX is used to find files. */ +struct grub_util_path_list * +grub_util_resolve_dependencies (const char *prefix, + const char *dep_list_file, + char *modules[]); + +#endif /* ! GRUB_UTIL_RESOLVE_HEADER */ diff --git a/libr/fs/p/grub/include/grub/zfs/dmu.h b/libr/fs/p/grub/include/grub/zfs/dmu.h new file mode 100644 index 0000000000..bee317e8a4 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/dmu.h @@ -0,0 +1,119 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_DMU_H +#define _SYS_DMU_H + +/* + * This file describes the interface that the DMU provides for its + * consumers. + * + * The DMU also interacts with the SPA. That interface is described in + * dmu_spa.h. + */ +typedef enum dmu_object_type { + DMU_OT_NONE, + /* general: */ + DMU_OT_OBJECT_DIRECTORY, /* ZAP */ + DMU_OT_OBJECT_ARRAY, /* UINT64 */ + DMU_OT_PACKED_NVLIST, /* UINT8 (XDR by nvlist_pack/unpack) */ + DMU_OT_PACKED_NVLIST_SIZE, /* UINT64 */ + DMU_OT_BPLIST, /* UINT64 */ + DMU_OT_BPLIST_HDR, /* UINT64 */ + /* spa: */ + DMU_OT_SPACE_MAP_HEADER, /* UINT64 */ + DMU_OT_SPACE_MAP, /* UINT64 */ + /* zil: */ + DMU_OT_INTENT_LOG, /* UINT64 */ + /* dmu: */ + DMU_OT_DNODE, /* DNODE */ + DMU_OT_OBJSET, /* OBJSET */ + /* dsl: */ + DMU_OT_DSL_DIR, /* UINT64 */ + DMU_OT_DSL_DIR_CHILD_MAP, /* ZAP */ + DMU_OT_DSL_DS_SNAP_MAP, /* ZAP */ + DMU_OT_DSL_PROPS, /* ZAP */ + DMU_OT_DSL_DATASET, /* UINT64 */ + /* zpl: */ + DMU_OT_ZNODE, /* ZNODE */ + DMU_OT_OLDACL, /* OLD ACL */ + DMU_OT_PLAIN_FILE_CONTENTS, /* UINT8 */ + DMU_OT_DIRECTORY_CONTENTS, /* ZAP */ + DMU_OT_MASTER_NODE, /* ZAP */ + DMU_OT_UNLINKED_SET, /* ZAP */ + /* zvol: */ + DMU_OT_ZVOL, /* UINT8 */ + DMU_OT_ZVOL_PROP, /* ZAP */ + /* other; for testing only! */ + DMU_OT_PLAIN_OTHER, /* UINT8 */ + DMU_OT_UINT64_OTHER, /* UINT64 */ + DMU_OT_ZAP_OTHER, /* ZAP */ + /* new object types: */ + DMU_OT_ERROR_LOG, /* ZAP */ + DMU_OT_SPA_HISTORY, /* UINT8 */ + DMU_OT_SPA_HISTORY_OFFSETS, /* spa_his_phys_t */ + DMU_OT_POOL_PROPS, /* ZAP */ + DMU_OT_DSL_PERMS, /* ZAP */ + DMU_OT_ACL, /* ACL */ + DMU_OT_SYSACL, /* SYSACL */ + DMU_OT_FUID, /* FUID table (Packed NVLIST UINT8) */ + DMU_OT_FUID_SIZE, /* FUID table size UINT64 */ + DMU_OT_NEXT_CLONES, /* ZAP */ + DMU_OT_SCRUB_QUEUE, /* ZAP */ + DMU_OT_USERGROUP_USED, /* ZAP */ + DMU_OT_USERGROUP_QUOTA, /* ZAP */ + DMU_OT_USERREFS, /* ZAP */ + DMU_OT_DDT_ZAP, /* ZAP */ + DMU_OT_DDT_STATS, /* ZAP */ + DMU_OT_SA, /* System attr */ + DMU_OT_SA_MASTER_NODE, /* ZAP */ + DMU_OT_SA_ATTR_REGISTRATION, /* ZAP */ + DMU_OT_SA_ATTR_LAYOUTS, /* ZAP */ + DMU_OT_NUMTYPES +} dmu_object_type_t; + +typedef enum dmu_objset_type { + DMU_OST_NONE, + DMU_OST_META, + DMU_OST_ZFS, + DMU_OST_ZVOL, + DMU_OST_OTHER, /* For testing only! */ + DMU_OST_ANY, /* Be careful! */ + DMU_OST_NUMTYPES +} dmu_objset_type_t; + +/* + * The names of zap entries in the DIRECTORY_OBJECT of the MOS. + */ +#define DMU_POOL_DIRECTORY_OBJECT 1 +#define DMU_POOL_CONFIG "config" +#define DMU_POOL_ROOT_DATASET "root_dataset" +#define DMU_POOL_SYNC_BPLIST "sync_bplist" +#define DMU_POOL_ERRLOG_SCRUB "errlog_scrub" +#define DMU_POOL_ERRLOG_LAST "errlog_last" +#define DMU_POOL_SPARES "spares" +#define DMU_POOL_DEFLATE "deflate" +#define DMU_POOL_HISTORY "history" +#define DMU_POOL_PROPS "pool_props" +#define DMU_POOL_L2CACHE "l2cache" + +#endif /* _SYS_DMU_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/dmu_objset.h b/libr/fs/p/grub/include/grub/zfs/dmu_objset.h new file mode 100644 index 0000000000..57d21db5c3 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/dmu_objset.h @@ -0,0 +1,43 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * Copyright (C) 2010 Robert Millan + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_DMU_OBJSET_H +#define _SYS_DMU_OBJSET_H + +#include + +#define OBJSET_PHYS_SIZE 2048 +#define OBJSET_PHYS_SIZE_V14 1024 + +typedef struct objset_phys { + dnode_phys_t os_meta_dnode; + zil_header_t os_zil_header; + grub_uint64_t os_type; + grub_uint64_t os_flags; + char os_pad[OBJSET_PHYS_SIZE - sizeof (dnode_phys_t)*3 - + sizeof (zil_header_t) - sizeof (grub_uint64_t)*2]; + dnode_phys_t os_userused_dnode; + dnode_phys_t os_groupused_dnode; +} objset_phys_t; + +#endif /* _SYS_DMU_OBJSET_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/dnode.h b/libr/fs/p/grub/include/grub/zfs/dnode.h new file mode 100644 index 0000000000..279c5451e8 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/dnode.h @@ -0,0 +1,80 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_DNODE_H +#define _SYS_DNODE_H + +#include + +/* + * Fixed constants. + */ +#define DNODE_SHIFT 9 /* 512 bytes */ +#define DN_MIN_INDBLKSHIFT 10 /* 1k */ +#define DN_MAX_INDBLKSHIFT 14 /* 16k */ +#define DNODE_BLOCK_SHIFT 14 /* 16k */ +#define DNODE_CORE_SIZE 64 /* 64 bytes for dnode sans blkptrs */ +#define DN_MAX_OBJECT_SHIFT 48 /* 256 trillion (zfs_fid_t limit) */ +#define DN_MAX_OFFSET_SHIFT 64 /* 2^64 bytes in a dnode */ + +/* + * Derived constants. + */ +#define DNODE_SIZE (1 << DNODE_SHIFT) +#define DN_MAX_NBLKPTR ((DNODE_SIZE - DNODE_CORE_SIZE) >> SPA_BLKPTRSHIFT) +#define DN_MAX_BONUSLEN (DNODE_SIZE - DNODE_CORE_SIZE - (1 << SPA_BLKPTRSHIFT)) +#define DN_MAX_OBJECT (1ULL << DN_MAX_OBJECT_SHIFT) + +#define DNODES_PER_BLOCK_SHIFT (DNODE_BLOCK_SHIFT - DNODE_SHIFT) +#define DNODES_PER_BLOCK (1ULL << DNODES_PER_BLOCK_SHIFT) +#define DNODES_PER_LEVEL_SHIFT (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT) + +#define DNODE_FLAG_SPILL_BLKPTR (1<<2) + +#define DN_BONUS(dnp) ((void*)((dnp)->dn_bonus + \ + (((dnp)->dn_nblkptr - 1) * sizeof (blkptr_t)))) + +typedef struct dnode_phys { + grub_uint8_t dn_type; /* dmu_object_type_t */ + grub_uint8_t dn_indblkshift; /* ln2(indirect block size) */ + grub_uint8_t dn_nlevels; /* 1=dn_blkptr->data blocks */ + grub_uint8_t dn_nblkptr; /* length of dn_blkptr */ + grub_uint8_t dn_bonustype; /* type of data in bonus buffer */ + grub_uint8_t dn_checksum; /* ZIO_CHECKSUM type */ + grub_uint8_t dn_compress; /* ZIO_COMPRESS type */ + grub_uint8_t dn_flags; /* DNODE_FLAG_* */ + grub_uint16_t dn_datablkszsec; /* data block size in 512b sectors */ + grub_uint16_t dn_bonuslen; /* length of dn_bonus */ + grub_uint8_t dn_pad2[4]; + + /* accounting is protected by dn_dirty_mtx */ + grub_uint64_t dn_maxblkid; /* largest allocated block ID */ + grub_uint64_t dn_used; /* bytes (or sectors) of disk space */ + + grub_uint64_t dn_pad3[4]; + + blkptr_t dn_blkptr[1]; + grub_uint8_t dn_bonus[DN_MAX_BONUSLEN - sizeof (blkptr_t)]; + blkptr_t dn_spill; +} dnode_phys_t; + +#endif /* _SYS_DNODE_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/dsl_dataset.h b/libr/fs/p/grub/include/grub/zfs/dsl_dataset.h new file mode 100644 index 0000000000..c17bf801e2 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/dsl_dataset.h @@ -0,0 +1,52 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_DSL_DATASET_H +#define _SYS_DSL_DATASET_H + +typedef struct dsl_dataset_phys { + grub_uint64_t ds_dir_obj; + grub_uint64_t ds_prev_snap_obj; + grub_uint64_t ds_prev_snap_txg; + grub_uint64_t ds_next_snap_obj; + grub_uint64_t ds_snapnames_zapobj; /* zap obj of snaps; ==0 for snaps */ + grub_uint64_t ds_num_children; /* clone/snap children; ==0 for head */ + grub_uint64_t ds_creation_time; /* seconds since 1970 */ + grub_uint64_t ds_creation_txg; + grub_uint64_t ds_deadlist_obj; + grub_uint64_t ds_used_bytes; + grub_uint64_t ds_compressed_bytes; + grub_uint64_t ds_uncompressed_bytes; + grub_uint64_t ds_unique_bytes; /* only relevant to snapshots */ + /* + * The ds_fsid_guid is a 56-bit ID that can change to avoid + * collisions. The ds_guid is a 64-bit ID that will never + * change, so there is a small probability that it will collide. + */ + grub_uint64_t ds_fsid_guid; + grub_uint64_t ds_guid; + grub_uint64_t ds_flags; + blkptr_t ds_bp; + grub_uint64_t ds_pad[8]; /* pad out to 320 bytes for good measure */ +} dsl_dataset_phys_t; + +#endif /* _SYS_DSL_DATASET_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/dsl_dir.h b/libr/fs/p/grub/include/grub/zfs/dsl_dir.h new file mode 100644 index 0000000000..41d77c790c --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/dsl_dir.h @@ -0,0 +1,48 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_DSL_DIR_H +#define _SYS_DSL_DIR_H + +typedef struct dsl_dir_phys { + grub_uint64_t dd_creation_time; /* not actually used */ + grub_uint64_t dd_head_dataset_obj; + grub_uint64_t dd_parent_obj; + grub_uint64_t dd_clone_parent_obj; + grub_uint64_t dd_child_dir_zapobj; + /* + * how much space our children are accounting for; for leaf + * datasets, == physical space used by fs + snaps + */ + grub_uint64_t dd_used_bytes; + grub_uint64_t dd_compressed_bytes; + grub_uint64_t dd_uncompressed_bytes; + /* Administrative quota setting */ + grub_uint64_t dd_quota; + /* Administrative reservation setting */ + grub_uint64_t dd_reserved; + grub_uint64_t dd_props_zapobj; + grub_uint64_t dd_deleg_zapobj; /* dataset permissions */ + grub_uint64_t dd_pad[20]; /* pad out to 256 bytes for good measure */ +} dsl_dir_phys_t; + +#endif /* _SYS_DSL_DIR_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/sa_impl.h b/libr/fs/p/grub/include/grub/zfs/sa_impl.h new file mode 100644 index 0000000000..a2b728d379 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/sa_impl.h @@ -0,0 +1,34 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ +#ifndef _SYS_SA_IMPL_H +#define _SYS_SA_IMPL_H + +typedef struct sa_hdr_phys { + grub_uint32_t sa_magic; + grub_uint16_t sa_layout_info; + grub_uint16_t sa_lengths[1]; +} sa_hdr_phys_t; + +#define SA_HDR_SIZE(hdr) BF32_GET_SB(hdr->sa_layout_info, 10, 16, 3, 0) +#define SA_SIZE_OFFSET 0x8 + +#endif /* _SYS_SA_IMPL_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/spa.h b/libr/fs/p/grub/include/grub/zfs/spa.h new file mode 100644 index 0000000000..22ee03b15d --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/spa.h @@ -0,0 +1,310 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc. + * Copyright 2010 Sun Microsystems, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_ZFS_SPA_HEADER +#define GRUB_ZFS_SPA_HEADER 1 + +typedef enum grub_zfs_endian + { + UNKNOWN_ENDIAN = -2, + LITTLE_ENDIAN = -1, + BIG_ENDIAN = 0 + } grub_zfs_endian_t; + +#define grub_zfs_to_cpu16(x,a) (((a) == BIG_ENDIAN) ? grub_be_to_cpu16(x) \ + : grub_le_to_cpu16(x)) +#define grub_cpu_to_zfs16(x,a) (((a) == BIG_ENDIAN) ? grub_cpu_to_be16(x) \ + : grub_cpu_to_le16(x)) + +#define grub_zfs_to_cpu32(x,a) (((a) == BIG_ENDIAN) ? grub_be_to_cpu32(x) \ + : grub_le_to_cpu32(x)) +#define grub_cpu_to_zfs32(x,a) (((a) == BIG_ENDIAN) ? grub_cpu_to_be32(x) \ + : grub_cpu_to_le32(x)) + +#define grub_zfs_to_cpu64(x,a) (((a) == BIG_ENDIAN) ? grub_be_to_cpu64(x) \ + : grub_le_to_cpu64(x)) +#define grub_cpu_to_zfs64(x,a) (((a) == BIG_ENDIAN) ? grub_cpu_to_be64(x) \ + : grub_cpu_to_le64(x)) + +/* + * General-purpose 32-bit and 64-bit bitfield encodings. + */ +#define BF32_DECODE(x, low, len) P2PHASE((x) >> (low), 1U << (len)) +#define BF64_DECODE(x, low, len) P2PHASE((x) >> (low), 1ULL << (len)) +#define BF32_ENCODE(x, low, len) (P2PHASE((x), 1U << (len)) << (low)) +#define BF64_ENCODE(x, low, len) (P2PHASE((x), 1ULL << (len)) << (low)) + +#define BF32_GET(x, low, len) BF32_DECODE(x, low, len) +#define BF64_GET(x, low, len) BF64_DECODE(x, low, len) + +#define BF32_SET(x, low, len, val) \ + ((x) ^= BF32_ENCODE((x >> low) ^ (val), low, len)) +#define BF64_SET(x, low, len, val) \ + ((x) ^= BF64_ENCODE((x >> low) ^ (val), low, len)) + +#define BF32_GET_SB(x, low, len, shift, bias) \ + ((BF32_GET(x, low, len) + (bias)) << (shift)) +#define BF64_GET_SB(x, low, len, shift, bias) \ + ((BF64_GET(x, low, len) + (bias)) << (shift)) + +#define BF32_SET_SB(x, low, len, shift, bias, val) \ + BF32_SET(x, low, len, ((val) >> (shift)) - (bias)) +#define BF64_SET_SB(x, low, len, shift, bias, val) \ + BF64_SET(x, low, len, ((val) >> (shift)) - (bias)) + +/* + * We currently support nine block sizes, from 512 bytes to 128K. + * We could go higher, but the benefits are near-zero and the cost + * of COWing a giant block to modify one byte would become excessive. + */ +#define SPA_MINBLOCKSHIFT 9 +#define SPA_MAXBLOCKSHIFT 17 +#define SPA_MINBLOCKSIZE (1ULL << SPA_MINBLOCKSHIFT) +#define SPA_MAXBLOCKSIZE (1ULL << SPA_MAXBLOCKSHIFT) + +#define SPA_BLOCKSIZES (SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1) + +/* + * Size of block to hold the configuration data (a packed nvlist) + */ +#define SPA_CONFIG_BLOCKSIZE (1 << 14) + +/* + * The DVA size encodings for LSIZE and PSIZE support blocks up to 32MB. + * The ASIZE encoding should be at least 64 times larger (6 more bits) + * to support up to 4-way RAID-Z mirror mode with worst-case gang block + * overhead, three DVAs per bp, plus one more bit in case we do anything + * else that expands the ASIZE. + */ +#define SPA_LSIZEBITS 16 /* LSIZE up to 32M (2^16 * 512) */ +#define SPA_PSIZEBITS 16 /* PSIZE up to 32M (2^16 * 512) */ +#define SPA_ASIZEBITS 24 /* ASIZE up to 64 times larger */ + +/* + * All SPA data is represented by 128-bit data virtual addresses (DVAs). + * The members of the dva_t should be considered opaque outside the SPA. + */ +typedef struct dva { + grub_uint64_t dva_word[2]; +} dva_t; + +/* + * Each block has a 256-bit checksum -- strong enough for cryptographic hashes. + */ +typedef struct zio_cksum { + grub_uint64_t zc_word[4]; +} zio_cksum_t; + +/* + * Each block is described by its DVAs, time of birth, checksum, etc. + * The word-by-word, bit-by-bit layout of the blkptr is as follows: + * + * 64 56 48 40 32 24 16 8 0 + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 0 | vdev1 | GRID | ASIZE | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 1 |G| offset1 | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 2 | vdev2 | GRID | ASIZE | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 3 |G| offset2 | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 4 | vdev3 | GRID | ASIZE | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 5 |G| offset3 | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 6 |BDX|lvl| type | cksum | comp | PSIZE | LSIZE | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 7 | padding | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 8 | padding | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * 9 | physical birth txg | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * a | logical birth txg | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * b | fill count | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * c | checksum[0] | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * d | checksum[1] | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * e | checksum[2] | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * f | checksum[3] | + * +-------+-------+-------+-------+-------+-------+-------+-------+ + * + * Legend: + * + * vdev virtual device ID + * offset offset into virtual device + * LSIZE logical size + * PSIZE physical size (after compression) + * ASIZE allocated size (including RAID-Z parity and gang block headers) + * GRID RAID-Z layout information (reserved for future use) + * cksum checksum function + * comp compression function + * G gang block indicator + * B byteorder (endianness) + * D dedup + * X unused + * lvl level of indirection + * type DMU object type + * phys birth txg of block allocation; zero if same as logical birth txg + * log. birth transaction group in which the block was logically born + * fill count number of non-zero blocks under this bp + * checksum[4] 256-bit checksum of the data this bp describes + */ +#define SPA_BLKPTRSHIFT 7 /* blkptr_t is 128 bytes */ +#define SPA_DVAS_PER_BP 3 /* Number of DVAs in a bp */ + +typedef struct blkptr { + dva_t blk_dva[SPA_DVAS_PER_BP]; /* Data Virtual Addresses */ + grub_uint64_t blk_prop; /* size, compression, type, etc */ + grub_uint64_t blk_pad[2]; /* Extra space for the future */ + grub_uint64_t blk_phys_birth; /* txg when block was allocated */ + grub_uint64_t blk_birth; /* transaction group at birth */ + grub_uint64_t blk_fill; /* fill count */ + zio_cksum_t blk_cksum; /* 256-bit checksum */ +} blkptr_t; + +/* + * Macros to get and set fields in a bp or DVA. + */ +#define DVA_GET_ASIZE(dva) \ + BF64_GET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0) +#define DVA_SET_ASIZE(dva, x) \ + BF64_SET_SB((dva)->dva_word[0], 0, 24, SPA_MINBLOCKSHIFT, 0, x) + +#define DVA_GET_GRID(dva) BF64_GET((dva)->dva_word[0], 24, 8) +#define DVA_SET_GRID(dva, x) BF64_SET((dva)->dva_word[0], 24, 8, x) + +#define DVA_GET_VDEV(dva) BF64_GET((dva)->dva_word[0], 32, 32) +#define DVA_SET_VDEV(dva, x) BF64_SET((dva)->dva_word[0], 32, 32, x) + +#define DVA_GET_GANG(dva) BF64_GET((dva)->dva_word[1], 63, 1) +#define DVA_SET_GANG(dva, x) BF64_SET((dva)->dva_word[1], 63, 1, x) + +#define BP_GET_LSIZE(bp) \ + BF64_GET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1) +#define BP_SET_LSIZE(bp, x) \ + BF64_SET_SB((bp)->blk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x) + +#define BP_GET_COMPRESS(bp) BF64_GET((bp)->blk_prop, 32, 8) +#define BP_SET_COMPRESS(bp, x) BF64_SET((bp)->blk_prop, 32, 8, x) + +#define BP_GET_CHECKSUM(bp) BF64_GET((bp)->blk_prop, 40, 8) +#define BP_SET_CHECKSUM(bp, x) BF64_SET((bp)->blk_prop, 40, 8, x) + +#define BP_GET_TYPE(bp) BF64_GET((bp)->blk_prop, 48, 8) +#define BP_SET_TYPE(bp, x) BF64_SET((bp)->blk_prop, 48, 8, x) + +#define BP_GET_LEVEL(bp) BF64_GET((bp)->blk_prop, 56, 5) +#define BP_SET_LEVEL(bp, x) BF64_SET((bp)->blk_prop, 56, 5, x) + +#define BP_GET_PROP_BIT_61(bp) BF64_GET((bp)->blk_prop, 61, 1) +#define BP_SET_PROP_BIT_61(bp, x) BF64_SET((bp)->blk_prop, 61, 1, x) + +#define BP_GET_DEDUP(bp) BF64_GET((bp)->blk_prop, 62, 1) +#define BP_SET_DEDUP(bp, x) BF64_SET((bp)->blk_prop, 62, 1, x) + +#define BP_GET_BYTEORDER(bp) (0 - BF64_GET((bp)->blk_prop, 63, 1)) +#define BP_SET_BYTEORDER(bp, x) BF64_SET((bp)->blk_prop, 63, 1, x) + +#define BP_PHYSICAL_BIRTH(bp) \ + ((bp)->blk_phys_birth ? (bp)->blk_phys_birth : (bp)->blk_birth) + +#define BP_SET_BIRTH(bp, logical, physical) \ +{ \ + (bp)->blk_birth = (logical); \ + (bp)->blk_phys_birth = ((logical) == (physical) ? 0 : (physical)); \ +} + +#define BP_GET_ASIZE(bp) \ + (DVA_GET_ASIZE(&(bp)->blk_dva[0]) + DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ + DVA_GET_ASIZE(&(bp)->blk_dva[2])) + +#define BP_GET_UCSIZE(bp) \ + ((BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata) ? \ + BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp)); + +#define BP_GET_NDVAS(bp) \ + (!!DVA_GET_ASIZE(&(bp)->blk_dva[0]) + \ + !!DVA_GET_ASIZE(&(bp)->blk_dva[1]) + \ + !!DVA_GET_ASIZE(&(bp)->blk_dva[2])) + +#define BP_COUNT_GANG(bp) \ + (DVA_GET_GANG(&(bp)->blk_dva[0]) + \ + DVA_GET_GANG(&(bp)->blk_dva[1]) + \ + DVA_GET_GANG(&(bp)->blk_dva[2])) + +#define DVA_EQUAL(dva1, dva2) \ + ((dva1)->dva_word[1] == (dva2)->dva_word[1] && \ + (dva1)->dva_word[0] == (dva2)->dva_word[0]) + +#define BP_EQUAL(bp1, bp2) \ + (BP_PHYSICAL_BIRTH(bp1) == BP_PHYSICAL_BIRTH(bp2) && \ + DVA_EQUAL(&(bp1)->blk_dva[0], &(bp2)->blk_dva[0]) && \ + DVA_EQUAL(&(bp1)->blk_dva[1], &(bp2)->blk_dva[1]) && \ + DVA_EQUAL(&(bp1)->blk_dva[2], &(bp2)->blk_dva[2])) + +#define ZIO_CHECKSUM_EQUAL(zc1, zc2) \ + (0 == (((zc1).zc_word[0] - (zc2).zc_word[0]) | \ + ((zc1).zc_word[1] - (zc2).zc_word[1]) | \ + ((zc1).zc_word[2] - (zc2).zc_word[2]) | \ + ((zc1).zc_word[3] - (zc2).zc_word[3]))) + +#define DVA_IS_VALID(dva) (DVA_GET_ASIZE(dva) != 0) + +#define ZIO_SET_CHECKSUM(zcp, w0, w1, w2, w3) \ +{ \ + (zcp)->zc_word[0] = w0; \ + (zcp)->zc_word[1] = w1; \ + (zcp)->zc_word[2] = w2; \ + (zcp)->zc_word[3] = w3; \ +} + +#define BP_IDENTITY(bp) (&(bp)->blk_dva[0]) +#define BP_IS_GANG(bp) DVA_GET_GANG(BP_IDENTITY(bp)) +#define BP_IS_HOLE(bp) ((bp)->blk_birth == 0) + +/* BP_IS_RAIDZ(bp) assumes no block compression */ +#define BP_IS_RAIDZ(bp) (DVA_GET_ASIZE(&(bp)->blk_dva[0]) > \ + BP_GET_PSIZE(bp)) + +#define BP_ZERO(bp) \ +{ \ + (bp)->blk_dva[0].dva_word[0] = 0; \ + (bp)->blk_dva[0].dva_word[1] = 0; \ + (bp)->blk_dva[1].dva_word[0] = 0; \ + (bp)->blk_dva[1].dva_word[1] = 0; \ + (bp)->blk_dva[2].dva_word[0] = 0; \ + (bp)->blk_dva[2].dva_word[1] = 0; \ + (bp)->blk_prop = 0; \ + (bp)->blk_pad[0] = 0; \ + (bp)->blk_pad[1] = 0; \ + (bp)->blk_phys_birth = 0; \ + (bp)->blk_birth = 0; \ + (bp)->blk_fill = 0; \ + ZIO_SET_CHECKSUM(&(bp)->blk_cksum, 0, 0, 0, 0); \ +} + +#define BP_SPRINTF_LEN 320 + +#endif /* ! GRUB_ZFS_SPA_HEADER */ diff --git a/libr/fs/p/grub/include/grub/zfs/uberblock_impl.h b/libr/fs/p/grub/include/grub/zfs/uberblock_impl.h new file mode 100644 index 0000000000..1bf7f2b071 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/uberblock_impl.h @@ -0,0 +1,60 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_UBERBLOCK_IMPL_H +#define _SYS_UBERBLOCK_IMPL_H + +/* + * The uberblock version is incremented whenever an incompatible on-disk + * format change is made to the SPA, DMU, or ZAP. + * + * Note: the first two fields should never be moved. When a storage pool + * is opened, the uberblock must be read off the disk before the version + * can be checked. If the ub_version field is moved, we may not detect + * version mismatch. If the ub_magic field is moved, applications that + * expect the magic number in the first word won't work. + */ +#define UBERBLOCK_MAGIC 0x00bab10c /* oo-ba-bloc! */ +#define UBERBLOCK_SHIFT 10 /* up to 1K */ + +typedef struct uberblock { + grub_uint64_t ub_magic; /* UBERBLOCK_MAGIC */ + grub_uint64_t ub_version; /* ZFS_VERSION */ + grub_uint64_t ub_txg; /* txg of last sync */ + grub_uint64_t ub_guid_sum; /* sum of all vdev guids */ + grub_uint64_t ub_timestamp; /* UTC time of last sync */ + blkptr_t ub_rootbp; /* MOS objset_phys_t */ +} uberblock_t; + +#define UBERBLOCK_SIZE (1ULL << UBERBLOCK_SHIFT) +#define VDEV_UBERBLOCK_SHIFT UBERBLOCK_SHIFT + +/* XXX Uberblock_phys_t is no longer in the kernel zfs */ +typedef struct uberblock_phys { + uberblock_t ubp_uberblock; + char ubp_pad[UBERBLOCK_SIZE - sizeof (uberblock_t) - + sizeof (zio_eck_t)]; + zio_eck_t ubp_zec; +} uberblock_phys_t; + + +#endif /* _SYS_UBERBLOCK_IMPL_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/vdev_impl.h b/libr/fs/p/grub/include/grub/zfs/vdev_impl.h new file mode 100644 index 0000000000..9b5f0a7a86 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/vdev_impl.h @@ -0,0 +1,69 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_VDEV_IMPL_H +#define _SYS_VDEV_IMPL_H + +#define VDEV_SKIP_SIZE (8 << 10) +#define VDEV_BOOT_HEADER_SIZE (8 << 10) +#define VDEV_PHYS_SIZE (112 << 10) +#define VDEV_UBERBLOCK_RING (128 << 10) + +/* ZFS boot block */ +#define VDEV_BOOT_MAGIC 0x2f5b007b10cULL +#define VDEV_BOOT_VERSION 1 /* version number */ + +typedef struct vdev_boot_header { + grub_uint64_t vb_magic; /* VDEV_BOOT_MAGIC */ + grub_uint64_t vb_version; /* VDEV_BOOT_VERSION */ + grub_uint64_t vb_offset; /* start offset (bytes) */ + grub_uint64_t vb_size; /* size (bytes) */ + char vb_pad[VDEV_BOOT_HEADER_SIZE - 4 * sizeof (grub_uint64_t)]; +} vdev_boot_header_t; + +typedef struct vdev_phys { + char vp_nvlist[VDEV_PHYS_SIZE - sizeof (zio_eck_t)]; + zio_eck_t vp_zbt; +} vdev_phys_t; + +typedef struct vdev_label { + char vl_pad[VDEV_SKIP_SIZE]; /* 8K */ + vdev_boot_header_t vl_boot_header; /* 8K */ + vdev_phys_t vl_vdev_phys; /* 112K */ + char vl_uberblock[VDEV_UBERBLOCK_RING]; /* 128K */ +} vdev_label_t; /* 256K total */ + +/* + * Size and offset of embedded boot loader region on each label. + * The total size of the first two labels plus the boot area is 4MB. + */ +#define VDEV_BOOT_OFFSET (2 * sizeof (vdev_label_t)) +#define VDEV_BOOT_SIZE (7ULL << 19) /* 3.5M */ + +/* + * Size of label regions at the start and end of each leaf device. + */ +#define VDEV_LABEL_START_SIZE (2 * sizeof (vdev_label_t) + VDEV_BOOT_SIZE) +#define VDEV_LABEL_END_SIZE (2 * sizeof (vdev_label_t)) +#define VDEV_LABELS 4 + +#endif /* _SYS_VDEV_IMPL_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/zap_impl.h b/libr/fs/p/grub/include/grub/zfs/zap_impl.h new file mode 100644 index 0000000000..e42727ab64 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zap_impl.h @@ -0,0 +1,111 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_ZAP_IMPL_H +#define _SYS_ZAP_IMPL_H + +#define ZAP_MAGIC 0x2F52AB2ABULL + +#define ZAP_HASHBITS 28 +#define MZAP_ENT_LEN 64 +#define MZAP_NAME_LEN (MZAP_ENT_LEN - 8 - 4 - 2) +#define MZAP_MAX_BLKSHIFT SPA_MAXBLOCKSHIFT +#define MZAP_MAX_BLKSZ (1 << MZAP_MAX_BLKSHIFT) + +typedef struct mzap_ent_phys { + grub_uint64_t mze_value; + grub_uint32_t mze_cd; + grub_uint16_t mze_pad; /* in case we want to chain them someday */ + char mze_name[MZAP_NAME_LEN]; +} mzap_ent_phys_t; + +typedef struct mzap_phys { + grub_uint64_t mz_block_type; /* ZBT_MICRO */ + grub_uint64_t mz_salt; + grub_uint64_t mz_pad[6]; + mzap_ent_phys_t mz_chunk[1]; + /* actually variable size depending on block size */ +} mzap_phys_t; + +/* + * The (fat) zap is stored in one object. It is an array of + * 1<= 6] [zap_leaf_t] [ptrtbl] ... + * + */ + +#define ZBT_LEAF ((1ULL << 63) + 0) +#define ZBT_HEADER ((1ULL << 63) + 1) +#define ZBT_MICRO ((1ULL << 63) + 3) +/* any other values are ptrtbl blocks */ + +/* + * the embedded pointer table takes up half a block: + * block size / entry size (2^3) / 2 + */ +#define ZAP_EMBEDDED_PTRTBL_SHIFT(zap) (FZAP_BLOCK_SHIFT(zap) - 3 - 1) + +/* + * The embedded pointer table starts half-way through the block. Since + * the pointer table itself is half the block, it starts at (64-bit) + * word number (1<zap_f.zap_phys) \ + [(idx) + (1<. + */ +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_ZAP_LEAF_H +#define _SYS_ZAP_LEAF_H + +#define ZAP_LEAF_MAGIC 0x2AB1EAF + +/* chunk size = 24 bytes */ +#define ZAP_LEAF_CHUNKSIZE 24 + +/* + * The amount of space within the chunk available for the array is: + * chunk size - space for type (1) - space for next pointer (2) + */ +#define ZAP_LEAF_ARRAY_BYTES (ZAP_LEAF_CHUNKSIZE - 3) + +typedef enum zap_chunk_type { + ZAP_CHUNK_FREE = 253, + ZAP_CHUNK_ENTRY = 252, + ZAP_CHUNK_ARRAY = 251, + ZAP_CHUNK_TYPE_MAX = 250 +} zap_chunk_type_t; + +/* + * TAKE NOTE: + * If zap_leaf_phys_t is modified, zap_leaf_byteswap() must be modified. + */ +typedef struct zap_leaf_phys { + struct zap_leaf_header { + grub_uint64_t lh_block_type; /* ZBT_LEAF */ + grub_uint64_t lh_pad1; + grub_uint64_t lh_prefix; /* hash prefix of this leaf */ + grub_uint32_t lh_magic; /* ZAP_LEAF_MAGIC */ + grub_uint16_t lh_nfree; /* number free chunks */ + grub_uint16_t lh_nentries; /* number of entries */ + grub_uint16_t lh_prefix_len; /* num bits used to id this */ + +/* above is accessable to zap, below is zap_leaf private */ + + grub_uint16_t lh_freelist; /* chunk head of free list */ + grub_uint8_t lh_pad2[12]; + } l_hdr; /* 2 24-byte chunks */ + + /* + * The header is followed by a hash table with + * ZAP_LEAF_HASH_NUMENTRIES(zap) entries. The hash table is + * followed by an array of ZAP_LEAF_NUMCHUNKS(zap) + * zap_leaf_chunk structures. These structures are accessed + * with the ZAP_LEAF_CHUNK() macro. + */ + + grub_uint16_t l_hash[1]; +} zap_leaf_phys_t; + +typedef union zap_leaf_chunk { + struct zap_leaf_entry { + grub_uint8_t le_type; /* always ZAP_CHUNK_ENTRY */ + grub_uint8_t le_int_size; /* size of ints */ + grub_uint16_t le_next; /* next entry in hash chain */ + grub_uint16_t le_name_chunk; /* first chunk of the name */ + grub_uint16_t le_name_length; /* bytes in name, incl null */ + grub_uint16_t le_value_chunk; /* first chunk of the value */ + grub_uint16_t le_value_length; /* value length in ints */ + grub_uint32_t le_cd; /* collision differentiator */ + grub_uint64_t le_hash; /* hash value of the name */ + } l_entry; + struct zap_leaf_array { + grub_uint8_t la_type; /* always ZAP_CHUNK_ARRAY */ + union + { + grub_uint8_t la_array[ZAP_LEAF_ARRAY_BYTES]; + grub_uint64_t la_array64; + }; + grub_uint16_t la_next; /* next blk or CHAIN_END */ + } l_array; + struct zap_leaf_free { + grub_uint8_t lf_type; /* always ZAP_CHUNK_FREE */ + grub_uint8_t lf_pad[ZAP_LEAF_ARRAY_BYTES]; + grub_uint16_t lf_next; /* next in free list, or CHAIN_END */ + } l_free; +} zap_leaf_chunk_t; + +#endif /* _SYS_ZAP_LEAF_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/zfs.h b/libr/fs/p/grub/include/grub/zfs/zfs.h new file mode 100644 index 0000000000..0576925732 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zfs.h @@ -0,0 +1,123 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2009 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + /* + * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#ifndef GRUB_ZFS_HEADER +#define GRUB_ZFS_HEADER 1 + +#include +#include + +/* + * On-disk version number. + */ +#define SPA_VERSION 28ULL + +/* + * The following are configuration names used in the nvlist describing a pool's + * configuration. + */ +#define ZPOOL_CONFIG_VERSION "version" +#define ZPOOL_CONFIG_POOL_NAME "name" +#define ZPOOL_CONFIG_POOL_STATE "state" +#define ZPOOL_CONFIG_POOL_TXG "txg" +#define ZPOOL_CONFIG_POOL_GUID "pool_guid" +#define ZPOOL_CONFIG_CREATE_TXG "create_txg" +#define ZPOOL_CONFIG_TOP_GUID "top_guid" +#define ZPOOL_CONFIG_VDEV_TREE "vdev_tree" +#define ZPOOL_CONFIG_TYPE "type" +#define ZPOOL_CONFIG_CHILDREN "children" +#define ZPOOL_CONFIG_ID "id" +#define ZPOOL_CONFIG_GUID "guid" +#define ZPOOL_CONFIG_PATH "path" +#define ZPOOL_CONFIG_DEVID "devid" +#define ZPOOL_CONFIG_METASLAB_ARRAY "metaslab_array" +#define ZPOOL_CONFIG_METASLAB_SHIFT "metaslab_shift" +#define ZPOOL_CONFIG_ASHIFT "ashift" +#define ZPOOL_CONFIG_ASIZE "asize" +#define ZPOOL_CONFIG_DTL "DTL" +#define ZPOOL_CONFIG_STATS "stats" +#define ZPOOL_CONFIG_WHOLE_DISK "whole_disk" +#define ZPOOL_CONFIG_ERRCOUNT "error_count" +#define ZPOOL_CONFIG_NOT_PRESENT "not_present" +#define ZPOOL_CONFIG_SPARES "spares" +#define ZPOOL_CONFIG_IS_SPARE "is_spare" +#define ZPOOL_CONFIG_NPARITY "nparity" +#define ZPOOL_CONFIG_PHYS_PATH "phys_path" +#define ZPOOL_CONFIG_L2CACHE "l2cache" +#define ZPOOL_CONFIG_HOLE_ARRAY "hole_array" +#define ZPOOL_CONFIG_VDEV_CHILDREN "vdev_children" +#define ZPOOL_CONFIG_IS_HOLE "is_hole" +#define ZPOOL_CONFIG_DDT_HISTOGRAM "ddt_histogram" +#define ZPOOL_CONFIG_DDT_OBJ_STATS "ddt_object_stats" +#define ZPOOL_CONFIG_DDT_STATS "ddt_stats" +/* + * The persistent vdev state is stored as separate values rather than a single + * 'vdev_state' entry. This is because a device can be in multiple states, such + * as offline and degraded. + */ +#define ZPOOL_CONFIG_OFFLINE "offline" +#define ZPOOL_CONFIG_FAULTED "faulted" +#define ZPOOL_CONFIG_DEGRADED "degraded" +#define ZPOOL_CONFIG_REMOVED "removed" + +#define VDEV_TYPE_ROOT "root" +#define VDEV_TYPE_MIRROR "mirror" +#define VDEV_TYPE_REPLACING "replacing" +#define VDEV_TYPE_RAIDZ "raidz" +#define VDEV_TYPE_DISK "disk" +#define VDEV_TYPE_FILE "file" +#define VDEV_TYPE_MISSING "missing" +#define VDEV_TYPE_HOLE "hole" +#define VDEV_TYPE_SPARE "spare" +#define VDEV_TYPE_L2CACHE "l2cache" + +/* + * pool state. The following states are written to disk as part of the normal + * SPA lifecycle: ACTIVE, EXPORTED, DESTROYED, SPARE, L2CACHE. The remaining + * states are software abstractions used at various levels to communicate pool + * state. + */ +typedef enum pool_state { + POOL_STATE_ACTIVE = 0, /* In active use */ + POOL_STATE_EXPORTED, /* Explicitly exported */ + POOL_STATE_DESTROYED, /* Explicitly destroyed */ + POOL_STATE_SPARE, /* Reserved for hot spare use */ + POOL_STATE_L2CACHE, /* Level 2 ARC device */ + POOL_STATE_UNINITIALIZED, /* Internal spa_t state */ + POOL_STATE_UNAVAIL, /* Internal libzfs state */ + POOL_STATE_POTENTIALLY_ACTIVE /* Internal libzfs state */ +} pool_state_t; + +struct grub_zfs_data; + +grub_err_t grub_zfs_fetch_nvlist (grub_device_t dev, char **nvlist); +grub_err_t grub_zfs_getmdnobj (grub_device_t dev, const char *fsfilename, + grub_uint64_t *mdnobj); + +char *grub_zfs_nvlist_lookup_string (char *nvlist, char *name); +char *grub_zfs_nvlist_lookup_nvlist (char *nvlist, char *name); +int grub_zfs_nvlist_lookup_uint64 (char *nvlist, char *name, + grub_uint64_t *out); +char *grub_zfs_nvlist_lookup_nvlist_array (char *nvlist, char *name, + grub_size_t index); +int grub_zfs_nvlist_lookup_nvlist_array_get_nelm (char *nvlist, char *name); + +#endif /* ! GRUB_ZFS_HEADER */ diff --git a/libr/fs/p/grub/include/grub/zfs/zfs_acl.h b/libr/fs/p/grub/include/grub/zfs/zfs_acl.h new file mode 100644 index 0000000000..2777382797 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zfs_acl.h @@ -0,0 +1,59 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_FS_ZFS_ACL_H +#define _SYS_FS_ZFS_ACL_H + +#ifndef _UID_T +#define _UID_T +typedef unsigned int uid_t; /* UID type */ +#endif /* _UID_T */ + +typedef struct zfs_oldace { + grub_uint32_t z_fuid; /* "who" */ + grub_uint32_t z_access_mask; /* access mask */ + grub_uint16_t z_flags; /* flags, i.e inheritance */ + grub_uint16_t z_type; /* type of entry allow/deny */ +} zfs_oldace_t; + +#define ACE_SLOT_CNT 6 + +typedef struct zfs_znode_acl_v0 { + grub_uint64_t z_acl_extern_obj; /* ext acl pieces */ + grub_uint32_t z_acl_count; /* Number of ACEs */ + grub_uint16_t z_acl_version; /* acl version */ + grub_uint16_t z_acl_pad; /* pad */ + zfs_oldace_t z_ace_data[ACE_SLOT_CNT]; /* 6 standard ACEs */ +} zfs_znode_acl_v0_t; + +#define ZFS_ACE_SPACE (sizeof (zfs_oldace_t) * ACE_SLOT_CNT) + +typedef struct zfs_znode_acl { + grub_uint64_t z_acl_extern_obj; /* ext acl pieces */ + grub_uint32_t z_acl_size; /* Number of bytes in ACL */ + grub_uint16_t z_acl_version; /* acl version */ + grub_uint16_t z_acl_count; /* ace count */ + grub_uint8_t z_ace_data[ZFS_ACE_SPACE]; /* space for embedded ACEs */ +} zfs_znode_acl_t; + + +#endif /* _SYS_FS_ZFS_ACL_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/zfs_znode.h b/libr/fs/p/grub/include/grub/zfs/zfs_znode.h new file mode 100644 index 0000000000..efd6d10a6f --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zfs_znode.h @@ -0,0 +1,70 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_FS_ZFS_ZNODE_H +#define _SYS_FS_ZFS_ZNODE_H + +#include + +#define MASTER_NODE_OBJ 1 +#define ZFS_ROOT_OBJ "ROOT" +#define ZPL_VERSION_STR "VERSION" +#define ZFS_SA_ATTRS "SA_ATTRS" + +#define ZPL_VERSION 5ULL + +#define ZFS_DIRENT_OBJ(de) BF64_GET(de, 0, 48) + +/* + * This is the persistent portion of the znode. It is stored + * in the "bonus buffer" of the file. Short symbolic links + * are also stored in the bonus buffer. + */ +typedef struct znode_phys { + grub_uint64_t zp_atime[2]; /* 0 - last file access time */ + grub_uint64_t zp_mtime[2]; /* 16 - last file modification time */ + grub_uint64_t zp_ctime[2]; /* 32 - last file change time */ + grub_uint64_t zp_crtime[2]; /* 48 - creation time */ + grub_uint64_t zp_gen; /* 64 - generation (txg of creation) */ + grub_uint64_t zp_mode; /* 72 - file mode bits */ + grub_uint64_t zp_size; /* 80 - size of file */ + grub_uint64_t zp_parent; /* 88 - directory parent (`..') */ + grub_uint64_t zp_links; /* 96 - number of links to file */ + grub_uint64_t zp_xattr; /* 104 - DMU object for xattrs */ + grub_uint64_t zp_rdev; /* 112 - dev_t for VBLK & VCHR files */ + grub_uint64_t zp_flags; /* 120 - persistent flags */ + grub_uint64_t zp_uid; /* 128 - file owner */ + grub_uint64_t zp_gid; /* 136 - owning group */ + grub_uint64_t zp_pad[4]; /* 144 - future */ + zfs_znode_acl_t zp_acl; /* 176 - 263 ACL */ + /* + * Data may pad out any remaining bytes in the znode buffer, eg: + * + * |<---------------------- dnode_phys (512) ------------------------>| + * |<-- dnode (192) --->|<----------- "bonus" buffer (320) ---------->| + * |<---- znode (264) ---->|<---- data (56) ---->| + * + * At present, we only use this space to store symbolic links. + */ +} znode_phys_t; + +#endif /* _SYS_FS_ZFS_ZNODE_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/zil.h b/libr/fs/p/grub/include/grub/zfs/zil.h new file mode 100644 index 0000000000..45d16f4027 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zil.h @@ -0,0 +1,56 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_ZIL_H +#define _SYS_ZIL_H + +/* + * Intent log format: + * + * Each objset has its own intent log. The log header (zil_header_t) + * for objset N's intent log is kept in the Nth object of the SPA's + * intent_log objset. The log header points to a chain of log blocks, + * each of which contains log records (i.e., transactions) followed by + * a log block trailer (zil_trailer_t). The format of a log record + * depends on the record (or transaction) type, but all records begin + * with a common structure that defines the type, length, and txg. + */ + +/* + * Intent log header - this on disk structure holds fields to manage + * the log. All fields are 64 bit to easily handle cross architectures. + */ +typedef struct zil_header { + grub_uint64_t zh_claim_txg; /* txg in which log blocks were claimed */ + grub_uint64_t zh_replay_seq; /* highest replayed sequence number */ + blkptr_t zh_log; /* log chain */ + grub_uint64_t zh_claim_seq; /* highest claimed sequence number */ + grub_uint64_t zh_flags; /* header flags */ + grub_uint64_t zh_pad[4]; +} zil_header_t; + +/* + * zh_flags bit settings + */ +#define ZIL_REPLAY_NEEDED 0x1 /* replay needed - internal only */ + +#endif /* _SYS_ZIL_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/zio.h b/libr/fs/p/grub/include/grub/zfs/zio.h new file mode 100644 index 0000000000..797d4f9b3a --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zio.h @@ -0,0 +1,84 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _ZIO_H +#define _ZIO_H + +#include + +#define ZEC_MAGIC 0x210da7ab10c7a11ULL /* zio data bloc tail */ + +typedef struct zio_eck { + grub_uint64_t zec_magic; /* for validation, endianness */ + zio_cksum_t zec_cksum; /* 256-bit checksum */ +} zio_eck_t; + +/* + * Gang block headers are self-checksumming and contain an array + * of block pointers. + */ +#define SPA_GANGBLOCKSIZE SPA_MINBLOCKSIZE +#define SPA_GBH_NBLKPTRS ((SPA_GANGBLOCKSIZE - \ + sizeof (zio_eck_t)) / sizeof (blkptr_t)) +#define SPA_GBH_FILLER ((SPA_GANGBLOCKSIZE - \ + sizeof (zio_eck_t) - \ + (SPA_GBH_NBLKPTRS * sizeof (blkptr_t))) /\ + sizeof (grub_uint64_t)) + +#define ZIO_GET_IOSIZE(zio) \ + (BP_IS_GANG((zio)->io_bp) ? \ + SPA_GANGBLOCKSIZE : BP_GET_PSIZE((zio)->io_bp)) + +typedef struct zio_gbh { + blkptr_t zg_blkptr[SPA_GBH_NBLKPTRS]; + grub_uint64_t zg_filler[SPA_GBH_FILLER]; + zio_eck_t zg_tail; +} zio_gbh_phys_t; + +enum zio_checksum { + ZIO_CHECKSUM_INHERIT = 0, + ZIO_CHECKSUM_ON, + ZIO_CHECKSUM_OFF, + ZIO_CHECKSUM_LABEL, + ZIO_CHECKSUM_GANG_HEADER, + ZIO_CHECKSUM_ZILOG, + ZIO_CHECKSUM_FLETCHER_2, + ZIO_CHECKSUM_FLETCHER_4, + ZIO_CHECKSUM_SHA256, + ZIO_CHECKSUM_ZILOG2, + ZIO_CHECKSUM_FUNCTIONS +}; + +#define ZIO_CHECKSUM_ON_VALUE ZIO_CHECKSUM_FLETCHER_2 +#define ZIO_CHECKSUM_DEFAULT ZIO_CHECKSUM_ON + +enum zio_compress { + ZIO_COMPRESS_INHERIT = 0, + ZIO_COMPRESS_ON, + ZIO_COMPRESS_OFF, + ZIO_COMPRESS_LZJB, + ZIO_COMPRESS_EMPTY, + ZIO_COMPRESS_GZIP, + ZIO_COMPRESS_FUNCTIONS +}; + +#endif /* _ZIO_H */ diff --git a/libr/fs/p/grub/include/grub/zfs/zio_checksum.h b/libr/fs/p/grub/include/grub/zfs/zio_checksum.h new file mode 100644 index 0000000000..0ef5a3ec75 --- /dev/null +++ b/libr/fs/p/grub/include/grub/zfs/zio_checksum.h @@ -0,0 +1,49 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. + * + * GRUB is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +/* + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _SYS_ZIO_CHECKSUM_H +#define _SYS_ZIO_CHECKSUM_H + +/* + * Signature for checksum functions. + */ +typedef void zio_checksum_t(const void *data, grub_uint64_t size, + grub_zfs_endian_t endian, zio_cksum_t *zcp); + +/* + * Information about each checksum function. + */ +typedef struct zio_checksum_info { + zio_checksum_t *ci_func; /* checksum function for each byteorder */ + int ci_correctable; /* number of correctable bits */ + int ci_eck; /* uses zio embedded checksum? */ + char *ci_name; /* descriptive name */ +} zio_checksum_info_t; + +extern void zio_checksum_SHA256 (const void *, grub_uint64_t, + grub_zfs_endian_t endian, zio_cksum_t *); +extern void fletcher_2 (const void *, grub_uint64_t, grub_zfs_endian_t endian, + zio_cksum_t *); +extern void fletcher_4 (const void *, grub_uint64_t, grub_zfs_endian_t endian, + zio_cksum_t *); + +#endif /* _SYS_ZIO_CHECKSUM_H */ diff --git a/libr/fs/p/grub/kern/device.c b/libr/fs/p/grub/kern/device.c new file mode 100644 index 0000000000..3db14f50e0 --- /dev/null +++ b/libr/fs/p/grub/kern/device.c @@ -0,0 +1,169 @@ +/* device.c - device manager */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +grub_device_t +grub_device_open (const char *name) +{ + grub_disk_t disk = 0; + grub_device_t dev = 0; + + if (! name) + { + name = grub_env_get ("root"); + if (*name == '\0') + { + grub_error (GRUB_ERR_BAD_DEVICE, "no device is set"); + goto fail; + } + } + + dev = grub_malloc (sizeof (*dev)); + if (! dev) + goto fail; + + /* Try to open a disk. */ + disk = grub_disk_open (name); + if (! disk) + goto fail; + + dev->disk = disk; + dev->net = 0; /* FIXME */ + + return dev; + + fail: + if (disk) + grub_disk_close (disk); + + grub_free (dev); + + return 0; +} + +grub_err_t +grub_device_close (grub_device_t device) +{ + if (device->disk) + grub_disk_close (device->disk); + + grub_free (device); + + return grub_errno; +} + +int +grub_device_iterate (int (*hook) (const char *name)) +{ + auto int iterate_disk (const char *disk_name); + auto int iterate_partition (grub_disk_t disk, + const grub_partition_t partition); + + struct part_ent + { + struct part_ent *next; + char *name; + } *ents; + + int iterate_disk (const char *disk_name) + { + grub_device_t dev; + + if (hook (disk_name)) + return 1; + + dev = grub_device_open (disk_name); + if (! dev) + { + grub_errno = GRUB_ERR_NONE; + return 0; + } + + if (dev->disk) + { + struct part_ent *p; + int ret = 0; + + ents = NULL; + (void) grub_partition_iterate (dev->disk, iterate_partition); + grub_device_close (dev); + + grub_errno = GRUB_ERR_NONE; + + p = ents; + while (p != NULL) + { + struct part_ent *next = p->next; + + if (!ret) + ret = hook (p->name); + grub_free (p->name); + grub_free (p); + p = next; + } + + return ret; + } + + grub_device_close (dev); + return 0; + } + + int iterate_partition (grub_disk_t disk, const grub_partition_t partition) + { + struct part_ent *p; + char *part_name; + + p = grub_malloc (sizeof (*p)); + if (!p) + { + return 1; + } + + part_name = grub_partition_get_name (partition); + if (!part_name) + { + grub_free (p); + return 1; + } + p->name = grub_xasprintf ("%s,%s", disk->name, part_name); + grub_free (part_name); + if (!p->name) + { + grub_free (p); + return 1; + } + + p->next = ents; + ents = p; + + return 0; + } + + /* Only disk devices are supported at the moment. */ + return grub_disk_dev_iterate (iterate_disk); +} diff --git a/libr/fs/p/grub/kern/disk.c b/libr/fs/p/grub/kern/disk.c new file mode 100644 index 0000000000..eb68bffff5 --- /dev/null +++ b/libr/fs/p/grub/kern/disk.c @@ -0,0 +1,610 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GRUB_CACHE_TIMEOUT 2 + +/* The last time the disk was used. */ +static grub_uint64_t grub_last_time = 0; + + +/* Disk cache. */ +struct grub_disk_cache +{ + enum grub_disk_dev_id dev_id; + unsigned long disk_id; + grub_disk_addr_t sector; + char *data; + int lock; +}; + +static struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM]; + +void (*grub_disk_firmware_fini) (void); +int grub_disk_firmware_is_tainted; + +grub_err_t (* grub_disk_ata_pass_through) (grub_disk_t, + struct grub_disk_ata_pass_through_parms *); + + +#if 0 +static unsigned long grub_disk_cache_hits; +static unsigned long grub_disk_cache_misses; + +void +grub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses) +{ + *hits = grub_disk_cache_hits; + *misses = grub_disk_cache_misses; +} +#endif + +static unsigned +grub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id, + grub_disk_addr_t sector) +{ + return ((dev_id * 524287UL + disk_id * 2606459UL + + ((unsigned) (sector >> GRUB_DISK_CACHE_BITS))) + % GRUB_DISK_CACHE_NUM); +} + +static void +grub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id, + grub_disk_addr_t sector) +{ + unsigned index; + struct grub_disk_cache *cache; + + sector &= ~(GRUB_DISK_CACHE_SIZE - 1); + index = grub_disk_cache_get_index (dev_id, disk_id, sector); + cache = grub_disk_cache_table + index; + + if (cache->dev_id == dev_id && cache->disk_id == disk_id + && cache->sector == sector && cache->data) + { + cache->lock = 1; + grub_free (cache->data); + cache->data = 0; + cache->lock = 0; + } +} + +void +grub_disk_cache_invalidate_all (void) +{ + unsigned i; + + for (i = 0; i < GRUB_DISK_CACHE_NUM; i++) + { + struct grub_disk_cache *cache = grub_disk_cache_table + i; + + if (cache->data && ! cache->lock) + { + grub_free (cache->data); + cache->data = 0; + } + } +} + +static char * +grub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id, + grub_disk_addr_t sector) +{ + struct grub_disk_cache *cache; + unsigned index; + + index = grub_disk_cache_get_index (dev_id, disk_id, sector); + cache = grub_disk_cache_table + index; + + if (cache->dev_id == dev_id && cache->disk_id == disk_id + && cache->sector == sector) + { + cache->lock = 1; +#if 0 + grub_disk_cache_hits++; +#endif + return cache->data; + } + +#if 0 + grub_disk_cache_misses++; +#endif + + return 0; +} + +static void +grub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id, + grub_disk_addr_t sector) +{ + struct grub_disk_cache *cache; + unsigned index; + + index = grub_disk_cache_get_index (dev_id, disk_id, sector); + cache = grub_disk_cache_table + index; + + if (cache->dev_id == dev_id && cache->disk_id == disk_id + && cache->sector == sector) + cache->lock = 0; +} + +static grub_err_t +grub_disk_cache_store (unsigned long dev_id, unsigned long disk_id, + grub_disk_addr_t sector, const char *data) +{ + unsigned index; + struct grub_disk_cache *cache; + + index = grub_disk_cache_get_index (dev_id, disk_id, sector); + cache = grub_disk_cache_table + index; + + cache->lock = 1; + grub_free (cache->data); + cache->data = 0; + cache->lock = 0; + + cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS); + if (! cache->data) + return grub_errno; + + grub_memcpy (cache->data, data, + GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS); + cache->dev_id = dev_id; + cache->disk_id = disk_id; + cache->sector = sector; + + return GRUB_ERR_NONE; +} + + + +static grub_disk_dev_t grub_disk_dev_list; + +void +grub_disk_dev_register (grub_disk_dev_t dev) +{ + dev->next = grub_disk_dev_list; + grub_disk_dev_list = dev; +} + +void +grub_disk_dev_unregister (grub_disk_dev_t dev) +{ + grub_disk_dev_t *p, q; + + for (p = &grub_disk_dev_list, q = *p; q; p = &(q->next), q = q->next) + if (q == dev) + { + *p = q->next; + break; + } +} + +int +grub_disk_dev_iterate (int (*hook) (const char *name)) +{ + grub_disk_dev_t p; + + for (p = grub_disk_dev_list; p; p = p->next) + if (p->iterate && (p->iterate) (hook)) + return 1; + + return 0; +} + +/* Return the location of the first ',', if any, which is not + escaped by a '\'. */ +static const char * +find_part_sep (const char *name) +{ + const char *p = name; + char c; + + while ((c = *p++) != '\0') + { + if (c == '\\' && *p == ',') + p++; + else if (c == ',') + return p - 1; + } + return NULL; +} + +grub_disk_t +grub_disk_open (const char *name) +{ + const char *p; + grub_disk_t disk; + grub_disk_dev_t dev; + char *raw = (char *) name; + grub_uint64_t current_time; + + grub_dprintf ("disk", "Opening `%s'...\n", name); + + disk = (grub_disk_t) grub_zalloc (sizeof (*disk)); + if (! disk) + return 0; + + p = find_part_sep (name); + if (p) + { + grub_size_t len = p - name; + + raw = grub_malloc (len + 1); + if (! raw) + goto fail; + + grub_memcpy (raw, name, len); + raw[len] = '\0'; + disk->name = grub_strdup (raw); + } + else + disk->name = grub_strdup (name); + if (! disk->name) + goto fail; + + + for (dev = grub_disk_dev_list; dev; dev = dev->next) + { + if ((dev->open) (raw, disk) == GRUB_ERR_NONE) + break; + else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE) + grub_errno = GRUB_ERR_NONE; + else + goto fail; + } + + if (! dev) + { + grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk"); + goto fail; + } + + disk->dev = dev; + + if (p) + { + disk->partition = grub_partition_probe (disk, p + 1); + if (! disk->partition) + { + grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such partition"); + goto fail; + } + } + + /* The cache will be invalidated about 2 seconds after a device was + closed. */ + current_time = grub_get_time_ms (); + + if (current_time > (grub_last_time + + GRUB_CACHE_TIMEOUT * 1000)) + grub_disk_cache_invalidate_all (); + + grub_last_time = current_time; + + fail: + + if (raw && raw != name) + grub_free (raw); + + if (grub_errno != GRUB_ERR_NONE) + { + grub_error_push (); + grub_dprintf ("disk", "Opening `%s' failed.\n", name); + grub_error_pop (); + + grub_disk_close (disk); + return 0; + } + + return disk; +} + +void +grub_disk_close (grub_disk_t disk) +{ + grub_partition_t part; + grub_dprintf ("disk", "Closing `%s'.\n", disk->name); + + if (disk->dev && disk->dev->close) + (disk->dev->close) (disk); + + /* Reset the timer. */ + grub_last_time = grub_get_time_ms (); + + while (disk->partition) + { + part = disk->partition->parent; + grub_free (disk->partition); + disk->partition = part; + } + grub_free ((void *) disk->name); + grub_free (disk); +} + +/* This function performs three tasks: + - Make sectors disk relative from partition relative. + - Normalize offset to be less than the sector size. + - Verify that the range is inside the partition. */ +static grub_err_t +grub_disk_adjust_range (grub_disk_t disk, grub_disk_addr_t *sector, + grub_off_t *offset, grub_size_t size) +{ + grub_partition_t part; + *sector += *offset >> GRUB_DISK_SECTOR_BITS; + *offset &= GRUB_DISK_SECTOR_SIZE - 1; + +// XXX HACK +return GRUB_ERR_NONE; + for (part = disk->partition; part; part = part->parent) + { + grub_disk_addr_t start; + grub_uint64_t len; + + start = part->start; + len = part->len; + + if (*sector >= len + || len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1) + >> GRUB_DISK_SECTOR_BITS)) + return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of partition"); + + *sector += start; + } + + if (disk->total_sectors <= *sector + || ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1) + >> GRUB_DISK_SECTOR_BITS) > disk->total_sectors - *sector) + return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk"); + + return GRUB_ERR_NONE; +} + +/* Read data from the disk. */ +grub_err_t +grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + grub_off_t offset, grub_size_t size, void *buf) +{ + char *tmp_buf; + unsigned real_offset; + + /* First of all, check if the region is within the disk. */ + if (grub_disk_adjust_range (disk, §or, &offset, size) != GRUB_ERR_NONE) + { + grub_error_push (); + printf ("disk: Read out of range: sector 0x%llx (%s).\n", + (unsigned long long) sector, grub_errmsg); + grub_error_pop (); + return grub_errno; + } + + real_offset = offset; + + /* Allocate a temporary buffer. */ + tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS); + if (! tmp_buf) { + return grub_errno; + } + + /* Until SIZE is zero... */ + while (size) + { + char *data; + grub_disk_addr_t start_sector; + grub_size_t len; + grub_size_t pos; + + /* For reading bulk data. */ + start_sector = sector & ~(GRUB_DISK_CACHE_SIZE - 1); + pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS; + len = ((GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS) + - pos - real_offset); + if (len > size) + len = size; + + /* Fetch the cache. */ + data = grub_disk_cache_fetch (disk->dev->id, disk->id, start_sector); + if (data) + { + /* Just copy it! */ + grub_memcpy (buf, data + pos + real_offset, len); + grub_disk_cache_unlock (disk->dev->id, disk->id, start_sector); + } + else + { + /* Otherwise read data from the disk actually. */ + if (start_sector + GRUB_DISK_CACHE_SIZE > disk->total_sectors + || (disk->dev->read) (disk, start_sector, + GRUB_DISK_CACHE_SIZE, tmp_buf) + != GRUB_ERR_NONE) + { + /* Uggh... Failed. Instead, just read necessary data. */ + unsigned num; + char *p; + + grub_errno = GRUB_ERR_NONE; + + num = ((size + real_offset + GRUB_DISK_SECTOR_SIZE - 1) + >> GRUB_DISK_SECTOR_BITS); + + p = grub_realloc (tmp_buf, num << GRUB_DISK_SECTOR_BITS); + if (!p) { + goto finish; + } + + tmp_buf = p; + + if ((disk->dev->read) (disk, sector, num, tmp_buf)) + { + grub_error_push (); + printf ("disk: %s read failed\n", disk->name); + grub_error_pop (); + goto finish; + } + + grub_memcpy (buf, tmp_buf + real_offset, size); + + /* Call the read hook, if any. */ + if (disk->read_hook) + while (size) + { + grub_size_t to_read = (size > GRUB_DISK_SECTOR_SIZE) ? GRUB_DISK_SECTOR_SIZE : size; + (disk->read_hook) (sector, real_offset, + to_read); + if (grub_errno != GRUB_ERR_NONE) + goto finish; + + sector++; + size -= to_read - real_offset; + real_offset = 0; + } + + /* This must be the end. */ + goto finish; + } + + /* Copy it and store it in the disk cache. */ + grub_memcpy (buf, tmp_buf + pos + real_offset, len); + grub_disk_cache_store (disk->dev->id, disk->id, + start_sector, tmp_buf); + } + + /* Call the read hook, if any. */ + if (disk->read_hook) + { + grub_disk_addr_t s = sector; + grub_size_t l = len; + + while (l) + { + (disk->read_hook) (s, real_offset, + ((l > GRUB_DISK_SECTOR_SIZE) + ? GRUB_DISK_SECTOR_SIZE + : l)); + + if (l < GRUB_DISK_SECTOR_SIZE - real_offset) + break; + + s++; + l -= GRUB_DISK_SECTOR_SIZE - real_offset; + real_offset = 0; + } + } + + sector = start_sector + GRUB_DISK_CACHE_SIZE; + buf = (char *) buf + len; + size -= len; + real_offset = 0; + } + + finish: + + grub_free (tmp_buf); +//printf("ERRONO=%d\n", grub_errno); + + return grub_errno; +} + +grub_err_t +grub_disk_write (grub_disk_t disk, grub_disk_addr_t sector, + grub_off_t offset, grub_size_t size, const void *buf) +{ + unsigned real_offset; + + grub_dprintf ("disk", "Writing `%s'...\n", disk->name); + + if (grub_disk_adjust_range (disk, §or, &offset, size) != GRUB_ERR_NONE) + return -1; + + real_offset = offset; + + while (size) + { + if (real_offset != 0 || (size < GRUB_DISK_SECTOR_SIZE && size != 0)) + { + char tmp_buf[GRUB_DISK_SECTOR_SIZE]; + grub_size_t len; + grub_partition_t part; + + part = disk->partition; + disk->partition = 0; + if (grub_disk_read (disk, sector, 0, GRUB_DISK_SECTOR_SIZE, tmp_buf) + != GRUB_ERR_NONE) + { + disk->partition = part; + goto finish; + } + disk->partition = part; + + len = GRUB_DISK_SECTOR_SIZE - real_offset; + if (len > size) + len = size; + + grub_memcpy (tmp_buf + real_offset, buf, len); + + grub_disk_cache_invalidate (disk->dev->id, disk->id, sector); + + if ((disk->dev->write) (disk, sector, 1, tmp_buf) != GRUB_ERR_NONE) + goto finish; + + sector++; + buf = (char *) buf + len; + size -= len; + real_offset = 0; + } + else + { + grub_size_t len; + grub_size_t n; + + len = size & ~(GRUB_DISK_SECTOR_SIZE - 1); + n = size >> GRUB_DISK_SECTOR_BITS; + + if ((disk->dev->write) (disk, sector, n, buf) != GRUB_ERR_NONE) + goto finish; + + while (n--) + grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++); + + buf = (char *) buf + len; + size -= len; + } + } + + finish: + + return grub_errno; +} + +grub_uint64_t +grub_disk_get_size (grub_disk_t disk) +{ + if (disk->partition) + return grub_partition_get_len (disk->partition); + else + return disk->total_sectors; +} diff --git a/libr/fs/p/grub/kern/dl.c b/libr/fs/p/grub/kern/dl.c new file mode 100644 index 0000000000..02d785b9b9 --- /dev/null +++ b/libr/fs/p/grub/kern/dl.c @@ -0,0 +1,684 @@ +/* dl.c - loadable module support */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/* Force native word size */ +#define GRUB_TARGET_WORDSIZE (8 * GRUB_CPU_SIZEOF_VOID_P) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Platforms where modules are in a readonly area of memory. */ +#if defined(GRUB_MACHINE_QEMU) +#define GRUB_MODULES_MACHINE_READONLY +#endif + + + +grub_dl_t grub_dl_head = 0; + +static grub_err_t +grub_dl_add (grub_dl_t mod) +{ + if (grub_dl_get (mod->name)) + return grub_error (GRUB_ERR_BAD_MODULE, + "`%s' is already loaded", mod->name); + + mod->next = grub_dl_head; + grub_dl_head = mod; + + return GRUB_ERR_NONE; +} + +static void +grub_dl_remove (grub_dl_t mod) +{ + grub_dl_t *p, q; + + for (p = &grub_dl_head, q = *p; q; p = &q->next, q = *p) + if (q == mod) + { + *p = q->next; + return; + } +} + +grub_dl_t +grub_dl_get (const char *name) +{ + grub_dl_t l; + + for (l = grub_dl_head; l; l = l->next) + if (grub_strcmp (name, l->name) == 0) + return l; + + return 0; +} + + + +struct grub_symbol +{ + struct grub_symbol *next; + const char *name; + void *addr; + grub_dl_t mod; /* The module to which this symbol belongs. */ +}; +typedef struct grub_symbol *grub_symbol_t; + +/* The size of the symbol table. */ +#define GRUB_SYMTAB_SIZE 509 + +/* The symbol table (using an open-hash). */ +static struct grub_symbol *grub_symtab[GRUB_SYMTAB_SIZE]; + +/* Simple hash function. */ +static unsigned +grub_symbol_hash (const char *s) +{ + unsigned key = 0; + + while (*s) + key = key * 65599 + *s++; + + return (key + (key >> 5)) % GRUB_SYMTAB_SIZE; +} + +/* Resolve the symbol name NAME and return the address. + Return NULL, if not found. */ +static void * +grub_dl_resolve_symbol (const char *name) +{ + grub_symbol_t sym; + + for (sym = grub_symtab[grub_symbol_hash (name)]; sym; sym = sym->next) + if (grub_strcmp (sym->name, name) == 0) + return sym->addr; + + return 0; +} + +/* Register a symbol with the name NAME and the address ADDR. */ +grub_err_t +grub_dl_register_symbol (const char *name, void *addr, grub_dl_t mod) +{ + grub_symbol_t sym; + unsigned k; + + sym = (grub_symbol_t) grub_malloc (sizeof (*sym)); + if (! sym) + return grub_errno; + + if (mod) + { + sym->name = grub_strdup (name); + if (! sym->name) + { + grub_free (sym); + return grub_errno; + } + } + else + sym->name = name; + + sym->addr = addr; + sym->mod = mod; + + k = grub_symbol_hash (name); + sym->next = grub_symtab[k]; + grub_symtab[k] = sym; + + return GRUB_ERR_NONE; +} + +/* Unregister all the symbols defined in the module MOD. */ +static void +grub_dl_unregister_symbols (grub_dl_t mod) +{ + unsigned i; + + if (! mod) + grub_fatal ("core symbols cannot be unregistered"); + + for (i = 0; i < GRUB_SYMTAB_SIZE; i++) + { + grub_symbol_t sym, *p, q; + + for (p = &grub_symtab[i], sym = *p; sym; sym = q) + { + q = sym->next; + if (sym->mod == mod) + { + *p = q; + grub_free ((void *) sym->name); + grub_free (sym); + } + else + p = &sym->next; + } + } +} + +/* Return the address of a section whose index is N. */ +static void * +grub_dl_get_section_addr (grub_dl_t mod, unsigned n) +{ + grub_dl_segment_t seg; + + for (seg = mod->segment; seg; seg = seg->next) + if (seg->section == n) + return seg->addr; + + return 0; +} + +/* Check if EHDR is a valid ELF header. */ +static grub_err_t +grub_dl_check_header (void *ehdr, grub_size_t size) +{ + Elf_Ehdr *e = ehdr; + + /* Check the header size. */ + if (size < sizeof (Elf_Ehdr)) + return grub_error (GRUB_ERR_BAD_OS, "ELF header smaller than expected"); + + /* Check the magic numbers. */ + if (grub_arch_dl_check_header (ehdr) + || e->e_ident[EI_MAG0] != ELFMAG0 + || e->e_ident[EI_MAG1] != ELFMAG1 + || e->e_ident[EI_MAG2] != ELFMAG2 + || e->e_ident[EI_MAG3] != ELFMAG3 + || e->e_ident[EI_VERSION] != EV_CURRENT + || e->e_version != EV_CURRENT) + return grub_error (GRUB_ERR_BAD_OS, "invalid arch independent ELF magic"); + + return GRUB_ERR_NONE; +} + +/* Load all segments from memory specified by E. */ +static grub_err_t +grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e) +{ + unsigned i; + Elf_Shdr *s; + + for (i = 0, s = (Elf_Shdr *)((char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (Elf_Shdr *)((char *) s + e->e_shentsize)) + { + if (s->sh_flags & SHF_ALLOC) + { + grub_dl_segment_t seg; + + seg = (grub_dl_segment_t) grub_malloc (sizeof (*seg)); + if (! seg) + return grub_errno; + + if (s->sh_size) + { + void *addr; + + addr = grub_memalign (s->sh_addralign, s->sh_size); + if (! addr) + { + grub_free (seg); + return grub_errno; + } + + switch (s->sh_type) + { + case SHT_PROGBITS: + grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size); + break; + case SHT_NOBITS: + grub_memset (addr, 0, s->sh_size); + break; + } + + seg->addr = addr; + } + else + seg->addr = 0; + + seg->size = s->sh_size; + seg->section = i; + seg->next = mod->segment; + mod->segment = seg; + } + } + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e) +{ + unsigned i; + Elf_Shdr *s; + Elf_Sym *sym; + const char *str; + Elf_Word size, entsize; + + for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize)) + if (s->sh_type == SHT_SYMTAB) + break; + + if (i == e->e_shnum) + return grub_error (GRUB_ERR_BAD_MODULE, "no symbol table"); + +#ifdef GRUB_MODULES_MACHINE_READONLY + mod->symtab = grub_malloc (s->sh_size); + memcpy (mod->symtab, (char *) e + s->sh_offset, s->sh_size); +#else + mod->symtab = (Elf_Sym *) ((char *) e + s->sh_offset); +#endif + sym = mod->symtab; + size = s->sh_size; + entsize = s->sh_entsize; + + s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link); + str = (char *) e + s->sh_offset; + + for (i = 0; + i < size / entsize; + i++, sym = (Elf_Sym *) ((char *) sym + entsize)) + { + unsigned char type = ELF_ST_TYPE (sym->st_info); + unsigned char bind = ELF_ST_BIND (sym->st_info); + const char *name = str + sym->st_name; + + switch (type) + { + case STT_NOTYPE: + case STT_OBJECT: + /* Resolve a global symbol. */ + if (sym->st_name != 0 && sym->st_shndx == 0) + { + sym->st_value = (Elf_Addr) grub_dl_resolve_symbol (name); + if (! sym->st_value) + return grub_error (GRUB_ERR_BAD_MODULE, + "symbol not found: `%s'", name); + } + else + { + sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod, + sym->st_shndx); + if (bind != STB_LOCAL) + if (grub_dl_register_symbol (name, (void *) sym->st_value, mod)) + return grub_errno; + } + break; + + case STT_FUNC: + sym->st_value += (Elf_Addr) grub_dl_get_section_addr (mod, + sym->st_shndx); + if (bind != STB_LOCAL) + if (grub_dl_register_symbol (name, (void *) sym->st_value, mod)) + return grub_errno; + + if (grub_strcmp (name, "grub_mod_init") == 0) + mod->init = (void (*) (grub_dl_t)) sym->st_value; + else if (grub_strcmp (name, "grub_mod_fini") == 0) + mod->fini = (void (*) (void)) sym->st_value; + break; + + case STT_SECTION: + sym->st_value = (Elf_Addr) grub_dl_get_section_addr (mod, + sym->st_shndx); + break; + + case STT_FILE: + sym->st_value = 0; + break; + + default: + return grub_error (GRUB_ERR_BAD_MODULE, + "unknown symbol type `%d'", (int) type); + } + } + + return GRUB_ERR_NONE; +} + +static void +grub_dl_call_init (grub_dl_t mod) +{ + if (mod->init) + (mod->init) (mod); +} + +static grub_err_t +grub_dl_resolve_name (grub_dl_t mod, Elf_Ehdr *e) +{ + Elf_Shdr *s; + const char *str; + unsigned i; + + s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize); + str = (char *) e + s->sh_offset; + + for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize)) + if (grub_strcmp (str + s->sh_name, ".modname") == 0) + { + mod->name = grub_strdup ((char *) e + s->sh_offset); + if (! mod->name) + return grub_errno; + break; + } + + if (i == e->e_shnum) + return grub_error (GRUB_ERR_BAD_MODULE, "no module name found"); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e) +{ + Elf_Shdr *s; + const char *str; + unsigned i; + + s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shstrndx * e->e_shentsize); + str = (char *) e + s->sh_offset; + + for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff); + i < e->e_shnum; + i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize)) + if (grub_strcmp (str + s->sh_name, ".moddeps") == 0) + { + const char *name = (char *) e + s->sh_offset; + const char *max = name + s->sh_size; + + while ((name < max) && (*name)) + { + grub_dl_t m; + grub_dl_dep_t dep; + + m = grub_dl_load (name); + if (! m) + return grub_errno; + + grub_dl_ref (m); + + dep = (grub_dl_dep_t) grub_malloc (sizeof (*dep)); + if (! dep) + return grub_errno; + + dep->mod = m; + dep->next = mod->dep; + mod->dep = dep; + + name += grub_strlen (name) + 1; + } + } + + return GRUB_ERR_NONE; +} + +int +grub_dl_ref (grub_dl_t mod) +{ + grub_dl_dep_t dep; + + if (!mod) + return 0; + + for (dep = mod->dep; dep; dep = dep->next) + grub_dl_ref (dep->mod); + + return ++mod->ref_count; +} + +int +grub_dl_unref (grub_dl_t mod) +{ + grub_dl_dep_t dep; + + if (!mod) + return 0; + + for (dep = mod->dep; dep; dep = dep->next) + grub_dl_unref (dep->mod); + + return --mod->ref_count; +} + +static void +grub_dl_flush_cache (grub_dl_t mod) +{ + grub_dl_segment_t seg; + + for (seg = mod->segment; seg; seg = seg->next) { + if (seg->size) { + grub_dprintf ("modules", "flushing 0x%lx bytes at %p\n", + (unsigned long) seg->size, seg->addr); + grub_arch_sync_caches (seg->addr, seg->size); + } + } +} + +/* Load a module from core memory. */ +grub_dl_t +grub_dl_load_core (void *addr, grub_size_t size) +{ + Elf_Ehdr *e; + grub_dl_t mod; + + grub_dprintf ("modules", "module at %p, size 0x%lx\n", addr, + (unsigned long) size); + e = addr; + if (grub_dl_check_header (e, size)) + return 0; + + if (e->e_type != ET_REL) + { + grub_error (GRUB_ERR_BAD_MODULE, "invalid ELF file type"); + return 0; + } + + /* Make sure that every section is within the core. */ + if (size < e->e_shoff + e->e_shentsize * e->e_shnum) + { + grub_error (GRUB_ERR_BAD_OS, "ELF sections outside core"); + return 0; + } + + mod = (grub_dl_t) grub_zalloc (sizeof (*mod)); + if (! mod) + return 0; + + mod->ref_count = 1; + + grub_dprintf ("modules", "relocating to %p\n", mod); + if (grub_dl_resolve_name (mod, e) + || grub_dl_resolve_dependencies (mod, e) + || grub_dl_load_segments (mod, e) + || grub_dl_resolve_symbols (mod, e) + || grub_arch_dl_relocate_symbols (mod, e)) + { + mod->fini = 0; + grub_dl_unload (mod); + return 0; + } + + grub_dl_flush_cache (mod); + + grub_dprintf ("modules", "module name: %s\n", mod->name); + grub_dprintf ("modules", "init function: %p\n", mod->init); + grub_dl_call_init (mod); + + if (grub_dl_add (mod)) + { + grub_dl_unload (mod); + return 0; + } + + return mod; +} + +/* Load a module from the file FILENAME. */ +grub_dl_t +grub_dl_load_file (const char *filename) +{ + grub_file_t file = NULL; + grub_ssize_t size; + void *core = 0; + grub_dl_t mod = 0; + + file = grub_file_open (filename); + if (! file) + return 0; + + size = grub_file_size (file); + core = grub_malloc (size); + if (! core) + { + grub_file_close (file); + return 0; + } + + if (grub_file_read (file, core, size) != (int) size) + { + grub_file_close (file); + grub_free (core); + return 0; + } + + /* We must close this before we try to process dependencies. + Some disk backends do not handle gracefully multiple concurrent + opens of the same device. */ + grub_file_close (file); + + mod = grub_dl_load_core (core, size); + if (! mod) + { + grub_free (core); + return 0; + } + + mod->ref_count = 0; + return mod; +} + +/* Load a module using a symbolic name. */ +grub_dl_t +grub_dl_load (const char *name) +{ + char *filename; + grub_dl_t mod; + char *grub_dl_dir = grub_env_get ("prefix"); + + mod = grub_dl_get (name); + if (mod) + return mod; + + if (! grub_dl_dir) { + grub_error (GRUB_ERR_FILE_NOT_FOUND, "\"prefix\" is not set"); + return 0; + } + + filename = grub_xasprintf ("%s/%s.mod", grub_dl_dir, name); + if (! filename) + return 0; + + mod = grub_dl_load_file (filename); + grub_free (filename); + + if (! mod) + return 0; + + if (grub_strcmp (mod->name, name) != 0) + grub_error (GRUB_ERR_BAD_MODULE, "mismatched names"); + + return mod; +} + +/* Unload the module MOD. */ +int +grub_dl_unload (grub_dl_t mod) +{ + grub_dl_dep_t dep, depn; + grub_dl_segment_t seg, segn; + + if (mod->ref_count > 0) + return 0; + + if (mod->fini) + (mod->fini) (); + + grub_dl_remove (mod); + grub_dl_unregister_symbols (mod); + + for (dep = mod->dep; dep; dep = depn) + { + depn = dep->next; + + if (! grub_dl_unref (dep->mod)) + grub_dl_unload (dep->mod); + + grub_free (dep); + } + + for (seg = mod->segment; seg; seg = segn) + { + segn = seg->next; + grub_free (seg->addr); + grub_free (seg); + } + + grub_free (mod->name); +#ifdef GRUB_MODULES_MACHINE_READONLY + grub_free (mod->symtab); +#endif + grub_free (mod); + return 1; +} + +/* Unload unneeded modules. */ +void +grub_dl_unload_unneeded (void) +{ + /* Because grub_dl_remove modifies the list of modules, this + implementation is tricky. */ + grub_dl_t p = grub_dl_head; + + while (p) + { + if (grub_dl_unload (p)) + { + p = grub_dl_head; + continue; + } + + p = p->next; + } +} diff --git a/libr/fs/p/grub/kern/env.c b/libr/fs/p/grub/kern/env.c new file mode 100644 index 0000000000..84b3a001dd --- /dev/null +++ b/libr/fs/p/grub/kern/env.c @@ -0,0 +1,242 @@ +/* env.c - Environment variables */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2005,2006,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +/* The initial context. */ +static struct grub_env_context initial_context; + +/* The current context. */ +struct grub_env_context *grub_current_context = &initial_context; + +/* Return the hash representation of the string S. */ +static unsigned int +grub_env_hashval (const char *s) +{ + unsigned int i = 0; + + /* XXX: This can be done much more efficiently. */ + while (*s) + i += 5 * *(s++); + + return i % HASHSZ; +} + +struct grub_env_var * +grub_env_find (const char *name) +{ + struct grub_env_var *var; + int idx = grub_env_hashval (name); + + /* Look for the variable in the current context. */ + for (var = grub_current_context->vars[idx]; var; var = var->next) + if (grub_strcmp (var->name, name) == 0) + return var; + + return 0; +} + +static void +grub_env_insert (struct grub_env_context *context, + struct grub_env_var *var) +{ + int idx = grub_env_hashval (var->name); + + /* Insert the variable into the hashtable. */ + var->prevp = &context->vars[idx]; + var->next = context->vars[idx]; + if (var->next) + var->next->prevp = &(var->next); + context->vars[idx] = var; +} + +static void +grub_env_remove (struct grub_env_var *var) +{ + /* Remove the entry from the variable table. */ + *var->prevp = var->next; + if (var->next) + var->next->prevp = var->prevp; +} + +grub_err_t +grub_env_set (const char *name, const char *val) +{ + struct grub_env_var *var; + + /* If the variable does already exist, just update the variable. */ + var = grub_env_find (name); + if (var) + { + char *old = var->value; + + if (var->write_hook) + var->value = var->write_hook (var, val); + else + var->value = grub_strdup (val); + + if (! var->value) + { + var->value = old; + return grub_errno; + } + + grub_free (old); + return GRUB_ERR_NONE; + } + + /* The variable does not exist, so create a new one. */ + var = grub_zalloc (sizeof (*var)); + if (! var) + return grub_errno; + + /* This is not necessary. But leave this for readability. */ + var->global = 0; + + var->name = grub_strdup (name); + if (! var->name) + goto fail; + + var->value = grub_strdup (val); + if (! var->value) + goto fail; + + grub_env_insert (grub_current_context, var); + + return GRUB_ERR_NONE; + + fail: + grub_free (var->name); + grub_free (var->value); + grub_free (var); + + return grub_errno; +} + +char * +grub_env_get (const char *name) +{ + struct grub_env_var *var; + + var = grub_env_find (name); + if (! var) + return 0; + + if (var->read_hook) + return var->read_hook (var, var->value); + + return var->value; +} + +void +grub_env_unset (const char *name) +{ + struct grub_env_var *var; + + var = grub_env_find (name); + if (! var) + return; + + if (var->read_hook || var->write_hook) + { + grub_env_set (name, ""); + return; + } + + grub_env_remove (var); + + grub_free (var->name); + grub_free (var->value); + grub_free (var); +} + +void +grub_env_iterate (int (*func) (struct grub_env_var *var)) +{ + struct grub_env_sorted_var *sorted_list = 0; + struct grub_env_sorted_var *sorted_var; + int i; + + /* Add variables associated with this context into a sorted list. */ + for (i = 0; i < HASHSZ; i++) + { + struct grub_env_var *var; + + for (var = grub_current_context->vars[i]; var; var = var->next) + { + struct grub_env_sorted_var *p, **q; + + sorted_var = grub_malloc (sizeof (*sorted_var)); + if (! sorted_var) + goto fail; + + sorted_var->var = var; + + for (q = &sorted_list, p = *q; p; q = &((*q)->next), p = *q) + { + if (grub_strcmp (p->var->name, var->name) > 0) + break; + } + + sorted_var->next = *q; + *q = sorted_var; + } + } + + /* Iterate FUNC on the sorted list. */ + for (sorted_var = sorted_list; sorted_var; sorted_var = sorted_var->next) + if (func (sorted_var->var)) + break; + + fail: + + /* Free the sorted list. */ + for (sorted_var = sorted_list; sorted_var; ) + { + struct grub_env_sorted_var *tmp = sorted_var->next; + + grub_free (sorted_var); + sorted_var = tmp; + } +} + +grub_err_t +grub_register_variable_hook (const char *name, + grub_env_read_hook_t read_hook, + grub_env_write_hook_t write_hook) +{ + struct grub_env_var *var = grub_env_find (name); + + if (! var) + { + if (grub_env_set (name, "") != GRUB_ERR_NONE) + return grub_errno; + + var = grub_env_find (name); + /* XXX Insert an assertion? */ + } + + var->read_hook = read_hook; + var->write_hook = write_hook; + + return GRUB_ERR_NONE; +} diff --git a/libr/fs/p/grub/kern/err.c b/libr/fs/p/grub/kern/err.c new file mode 100644 index 0000000000..9bd850b263 --- /dev/null +++ b/libr/fs/p/grub/kern/err.c @@ -0,0 +1,140 @@ +/* err.c - error handling routines */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include + +#define GRUB_MAX_ERRMSG 256 +#define GRUB_ERROR_STACK_SIZE 10 + +grub_err_t grub_errno; +char grub_errmsg[GRUB_MAX_ERRMSG]; +int grub_err_printed_errors; + +static struct +{ + grub_err_t errno; + char errmsg[GRUB_MAX_ERRMSG]; +} grub_error_stack_items[GRUB_ERROR_STACK_SIZE]; + +static int grub_error_stack_pos; +static int grub_error_stack_assert; + +grub_err_t +grub_error (grub_err_t n, const char *fmt, ...) +{ + va_list ap; + + grub_errno = n; + + va_start (ap, fmt); + grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap); + va_end (ap); + + return n; +} + +void +grub_fatal (const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + grub_vprintf (_(fmt), ap); + va_end (ap); + + grub_abort (); +} + +void +grub_error_push (void) +{ + /* Only add items to stack, if there is enough room. */ + if (grub_error_stack_pos < GRUB_ERROR_STACK_SIZE) + { + /* Copy active error message to stack. */ + grub_error_stack_items[grub_error_stack_pos].errno = grub_errno; + grub_memcpy (grub_error_stack_items[grub_error_stack_pos].errmsg, + grub_errmsg, + sizeof (grub_errmsg)); + + /* Advance to next error stack position. */ + grub_error_stack_pos++; + } + else + { + /* There is no room for new error message. Discard new error message + and mark error stack assertion flag. */ + grub_error_stack_assert = 1; + } + + /* Allow further operation of other components by resetting + active errno to GRUB_ERR_NONE. */ + grub_errno = GRUB_ERR_NONE; +} + +int +grub_error_pop (void) +{ + if (grub_error_stack_pos > 0) + { + /* Pop error message from error stack to current active error. */ + grub_error_stack_pos--; + + grub_errno = grub_error_stack_items[grub_error_stack_pos].errno; + grub_memcpy (grub_errmsg, + grub_error_stack_items[grub_error_stack_pos].errmsg, + sizeof (grub_errmsg)); + + return 1; + } + else + { + /* There is no more items on error stack, reset to no error state. */ + grub_errno = GRUB_ERR_NONE; + + return 0; + } +} + +void +grub_print_error (void) +{ + /* Print error messages in reverse order. First print active error message + and then empty error stack. */ + do + { + if (grub_errno != GRUB_ERR_NONE) + { + fprintf (stderr, "error: %s.\n", grub_errmsg); + grub_err_printed_errors++; + } + } + while (grub_error_pop ()); + + /* If there was an assert while using error stack, report about it. */ + if (grub_error_stack_assert) + { + fprintf (stderr, "assert: error stack overflow detected!\n"); + grub_error_stack_assert = 0; + } +} diff --git a/libr/fs/p/grub/kern/file.c b/libr/fs/p/grub/kern/file.c new file mode 100644 index 0000000000..c93fbf7371 --- /dev/null +++ b/libr/fs/p/grub/kern/file.c @@ -0,0 +1,186 @@ +/* file.c - file I/O functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2006,2007,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +grub_file_filter_t grub_file_filters_all[GRUB_FILE_FILTER_MAX]; +grub_file_filter_t grub_file_filters_enabled[GRUB_FILE_FILTER_MAX]; + +/* Get the device part of the filename NAME. It is enclosed by parentheses. */ +char * +grub_file_get_device_name (const char *name) +{ + if (name[0] == '(') + { + char *p = grub_strchr (name, ')'); + char *ret; + + if (! p) + { + grub_error (GRUB_ERR_BAD_FILENAME, "missing `)'"); + return 0; + } + + ret = (char *) grub_malloc (p - name); + if (! ret) + return 0; + + grub_memcpy (ret, name + 1, p - name - 1); + ret[p - name - 1] = '\0'; + return ret; + } + + return 0; +} + +grub_file_t +grub_file_open (const char *name) +{ + grub_device_t device = 0; + grub_file_t file = 0, last_file = 0; + char *device_name; + char *file_name; + grub_file_filter_id_t filter; + + device_name = grub_file_get_device_name (name); + if (grub_errno) + goto fail; + + /* Get the file part of NAME. */ + file_name = grub_strchr (name, ')'); + if (file_name) + file_name++; + else + file_name = (char *) name; + + device = grub_device_open (device_name); + grub_free (device_name); + if (! device) + goto fail; + + file = (grub_file_t) grub_zalloc (sizeof (*file)); + if (! file) + goto fail; + + file->device = device; + + if (device->disk && file_name[0] != '/') + /* This is a block list. */ + file->fs = &grub_fs_blocklist; + else + { + file->fs = grub_fs_probe (device); + if (! file->fs) + goto fail; + } + + if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE) + goto fail; + + for (filter = 0; file && filter < ARRAY_SIZE (grub_file_filters_enabled); + filter++) + if (grub_file_filters_enabled[filter]) + { + last_file = file; + file = grub_file_filters_enabled[filter] (file); + } + if (!file) + grub_file_close (last_file); + + grub_memcpy (grub_file_filters_enabled, grub_file_filters_all, + sizeof (grub_file_filters_enabled)); + + return file; + + fail: + if (device) + grub_device_close (device); + + /* if (net) grub_net_close (net); */ + + grub_free (file); + + grub_memcpy (grub_file_filters_enabled, grub_file_filters_all, + sizeof (grub_file_filters_enabled)); + + return 0; +} + +grub_ssize_t +grub_file_read (grub_file_t file, void *buf, grub_size_t len) +{ + grub_ssize_t res; + + if (file->offset > file->size) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, + "attempt to read past the end of file"); + return -1; + } + + if (len == 0 || len > file->size - file->offset) + len = file->size - file->offset; + + /* Prevent an overflow. */ + if ((grub_ssize_t) len < 0) + len >>= 1; + + if (len == 0) + return 0; + + res = (file->fs->read) (file, buf, len); + if (res > 0) + file->offset += res; + + return res; +} + +grub_err_t +grub_file_close (grub_file_t file) +{ + if (file->fs->close) + (file->fs->close) (file); + + if (file->device) + grub_device_close (file->device); + grub_free (file); + return grub_errno; +} + +grub_off_t +grub_file_seek (grub_file_t file, grub_off_t offset) +{ + grub_off_t old; + + if (offset > file->size) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, + "attempt to seek outside of the file"); + return -1; + } + + old = file->offset; + file->offset = offset; + return old; +} diff --git a/libr/fs/p/grub/kern/fs.c b/libr/fs/p/grub/kern/fs.c new file mode 100644 index 0000000000..cf800f4cc4 --- /dev/null +++ b/libr/fs/p/grub/kern/fs.c @@ -0,0 +1,231 @@ +/* fs.c - filesystem manager */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +grub_fs_t grub_fs_list = 0; + +grub_fs_autoload_hook_t grub_fs_autoload_hook = 0; + +grub_fs_t +grub_fs_probe (grub_device_t device) +{ + grub_fs_t p; + auto int dummy_func (const char *filename, + const struct grub_dirhook_info *info); + + int dummy_func (const char *filename __attribute__ ((unused)), + const struct grub_dirhook_info *info __attribute__ ((unused))) + { + return 1; + } + + if (device->disk) + { + /* Make it sure not to have an infinite recursive calls. */ + static int count = 0; + + for (p = grub_fs_list; p; p = p->next) + { + grub_dprintf ("fs", "Detecting %s...\n", p->name); + (p->dir) (device, "/", dummy_func); + if (grub_errno == GRUB_ERR_NONE) + return p; + + grub_error_push (); + grub_dprintf ("fs", "%s detection failed.\n", p->name); + grub_error_pop (); + + if (grub_errno != GRUB_ERR_BAD_FS) + return 0; + + grub_errno = GRUB_ERR_NONE; + } + + /* Let's load modules automatically. */ + if (grub_fs_autoload_hook && count == 0) + { + count++; + + while (grub_fs_autoload_hook ()) + { + p = grub_fs_list; + + (p->dir) (device, "/", dummy_func); + if (grub_errno == GRUB_ERR_NONE) + { + count--; + return p; + } + + if (grub_errno != GRUB_ERR_BAD_FS) + { + count--; + return 0; + } + + grub_errno = GRUB_ERR_NONE; + } + + count--; + } + } + else if (device->net->fs) + return device->net->fs; + + grub_error (GRUB_ERR_UNKNOWN_FS, "unknown filesystem"); + return 0; +} + + + +/* Block list support routines. */ + +struct grub_fs_block +{ + grub_disk_addr_t offset; + unsigned long length; +}; + +static grub_err_t +grub_fs_blocklist_open (grub_file_t file, const char *name) +{ + char *p = (char *) name; + unsigned num = 0; + unsigned i; + grub_disk_t disk = file->device->disk; + struct grub_fs_block *blocks; + + /* First, count the number of blocks. */ + do + { + num++; + p = grub_strchr (p, ','); + if (p) + p++; + } + while (p); + + /* Allocate a block list. */ + blocks = grub_zalloc (sizeof (struct grub_fs_block) * (num + 1)); + if (! blocks) + return 0; + + file->size = 0; + p = (char *) name; + for (i = 0; i < num; i++) + { + if (*p != '+') + { + blocks[i].offset = grub_strtoull (p, &p, 0); + if (grub_errno != GRUB_ERR_NONE || *p != '+') + { + grub_error (GRUB_ERR_BAD_FILENAME, + "invalid file name `%s'", name); + goto fail; + } + } + + p++; + blocks[i].length = grub_strtoul (p, &p, 0); + if (grub_errno != GRUB_ERR_NONE + || blocks[i].length == 0 + || (*p && *p != ',' && ! grub_isspace (*p))) + { + grub_error (GRUB_ERR_BAD_FILENAME, + "invalid file name `%s'", name); + goto fail; + } + + if (disk->total_sectors < blocks[i].offset + blocks[i].length) + { + grub_error (GRUB_ERR_BAD_FILENAME, "beyond the total sectors"); + goto fail; + } + + file->size += (blocks[i].length << GRUB_DISK_SECTOR_BITS); + p++; + } + + file->data = blocks; + + return GRUB_ERR_NONE; + + fail: + grub_free (blocks); + return grub_errno; +} + +static grub_ssize_t +grub_fs_blocklist_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_fs_block *p; + grub_disk_addr_t sector; + grub_off_t offset; + grub_ssize_t ret = 0; + + if (len > file->size - file->offset) + len = file->size - file->offset; + + sector = (file->offset >> GRUB_DISK_SECTOR_BITS); + offset = (file->offset & (GRUB_DISK_SECTOR_SIZE - 1)); + for (p = file->data; p->length && len > 0; p++) + { + if (sector < p->length) + { + grub_size_t size; + + size = len; + if (((size + offset + GRUB_DISK_SECTOR_SIZE - 1) + >> GRUB_DISK_SECTOR_BITS) > p->length - sector) + size = ((p->length - sector) << GRUB_DISK_SECTOR_BITS) - offset; + + if (grub_disk_read (file->device->disk, p->offset + sector, offset, + size, buf) != GRUB_ERR_NONE) + return -1; + + ret += size; + len -= size; + sector -= ((size + offset) >> GRUB_DISK_SECTOR_BITS); + offset = ((size + offset) & (GRUB_DISK_SECTOR_SIZE - 1)); + } + else + sector -= p->length; + } + + return ret; +} + +struct grub_fs grub_fs_blocklist = + { + .name = "blocklist", + .dir = 0, + .open = grub_fs_blocklist_open, + .read = grub_fs_blocklist_read, + .close = 0, + .next = 0 + }; diff --git a/libr/fs/p/grub/kern/list.c b/libr/fs/p/grub/kern/list.c new file mode 100644 index 0000000000..33c3341666 --- /dev/null +++ b/libr/fs/p/grub/kern/list.c @@ -0,0 +1,87 @@ +/* list.c - grub list function */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include + +void +grub_list_push (grub_list_t *head, grub_list_t item) +{ + item->next = *head; + *head = item; +} + +void +grub_list_remove (grub_list_t *head, grub_list_t item) +{ + grub_list_t *p, q; + + for (p = head, q = *p; q; p = &(q->next), q = q->next) + if (q == item) + { + *p = q->next; + break; + } +} + +void * +grub_named_list_find (grub_named_list_t head, const char *name) +{ + grub_named_list_t item; + + FOR_LIST_ELEMENTS (item, head) + if (grub_strcmp (item->name, name) == 0) + return item; + + return NULL; +} + +void +grub_prio_list_insert (grub_prio_list_t *head, grub_prio_list_t nitem) +{ + int inactive = 0; + + grub_prio_list_t *p, q; + + for (p = head, q = *p; q; p = &(q->next), q = q->next) + { + int r; + + r = grub_strcmp (nitem->name, q->name); + if (r < 0) + break; + if (r > 0) + continue; + + if (nitem->prio >= (q->prio & GRUB_PRIO_LIST_PRIO_MASK)) + { + q->prio &= ~GRUB_PRIO_LIST_FLAG_ACTIVE; + break; + } + + inactive = 1; + } + + *p = nitem; + nitem->next = q; + + if (! inactive) + nitem->prio |= GRUB_PRIO_LIST_FLAG_ACTIVE; +} diff --git a/libr/fs/p/grub/kern/misc.c b/libr/fs/p/grub/kern/misc.c new file mode 100644 index 0000000000..b1715ba35c --- /dev/null +++ b/libr/fs/p/grub/kern/misc.c @@ -0,0 +1,1012 @@ +/* misc.c - definitions of misc functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +static int +grub_vsnprintf_real (char *str, grub_size_t n, const char *fmt, va_list args); + +static int +grub_iswordseparator (int c) +{ + return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&'); +} + +/* grub_gettext_dummy is not translating anything. */ +static const char * +grub_gettext_dummy (const char *s) +{ + return s; +} + +const char* (*grub_gettext) (const char *s) = grub_gettext_dummy; + +void * +grub_memmove (void *dest, const void *src, grub_size_t n) +{ + char *d = (char *) dest; + const char *s = (const char *) src; + + if (d < s) + while (n--) + *d++ = *s++; + else + { + d += n; + s += n; + + while (n--) + *--d = *--s; + } + + return dest; +} + +#ifndef APPLE_CC +void *memmove (void *dest, const void *src, grub_size_t n) + __attribute__ ((alias ("grub_memmove"))); +/* GCC emits references to memcpy() for struct copies etc. */ +void *memcpy (void *dest, const void *src, grub_size_t n) + __attribute__ ((alias ("grub_memmove"))); +#else +void *memcpy (void *dest, const void *src, grub_size_t n) +{ + return grub_memmove (dest, src, n); +} +void *memmove (void *dest, const void *src, grub_size_t n) +{ + return grub_memmove (dest, src, n); +} +#endif + +char * +grub_strcpy (char *dest, const char *src) +{ + char *p = dest; + + while ((*p++ = *src++) != '\0') + ; + + return dest; +} + +char * +grub_strncpy (char *dest, const char *src, int c) +{ + char *p = dest; + + while ((*p++ = *src++) != '\0' && --c) + ; + + return dest; +} + +char * +grub_stpcpy (char *dest, const char *src) +{ + char *d = dest; + const char *s = src; + + do + *d++ = *s; + while (*s++ != '\0'); + + return d - 1; +} + +int +grub_printf (const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start (ap, fmt); + ret = grub_vprintf (fmt, ap); + va_end (ap); + + return ret; +} + +int +grub_printf_ (const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start (ap, fmt); + ret = grub_vprintf (_(fmt), ap); + va_end (ap); + + return ret; +} + +int +grub_puts_ (const char *s) +{ + return grub_puts (_(s)); +} + +#if defined (APPLE_CC) && ! defined (GRUB_UTIL) +int +grub_err_printf (const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start (ap, fmt); + ret = grub_vprintf (fmt, ap); + va_end (ap); + + return ret; +} +#endif + +#if ! defined (APPLE_CC) && ! defined (GRUB_UTIL) +int grub_err_printf (const char *fmt, ...) +__attribute__ ((alias("grub_printf"))); +#endif + +void +grub_real_dprintf (const char *file, const int line, const char *condition, + const char *fmt, ...) +{ + va_list args; + const char *debug = grub_env_get ("debug"); + + if (! debug) + return; + + if (grub_strword (debug, "all") || grub_strword (debug, condition)) + { + grub_printf ("%s:%d: ", file, line); + va_start (args, fmt); + grub_vprintf (fmt, args); + va_end (args); + grub_refresh (); + } +} + +#define PREALLOC_SIZE 255 + +int +grub_vprintf (const char *fmt, va_list args) +{ + grub_size_t s; + static char buf[PREALLOC_SIZE + 1]; + char *curbuf = buf; + va_list ap2; + va_copy (ap2, args); + + s = grub_vsnprintf_real (buf, PREALLOC_SIZE, fmt, args); + if (s > PREALLOC_SIZE) + { + curbuf = grub_malloc (s + 1); + if (!curbuf) + { + grub_errno = GRUB_ERR_NONE; + buf[PREALLOC_SIZE - 3] = '.'; + buf[PREALLOC_SIZE - 2] = '.'; + buf[PREALLOC_SIZE - 1] = '.'; + buf[PREALLOC_SIZE] = 0; + } + else + s = grub_vsnprintf_real (curbuf, s, fmt, ap2); + } + + grub_xputs (curbuf); + + if (curbuf != buf) + grub_free (curbuf); + + return s; +} + +int +grub_memcmp (const void *s1, const void *s2, grub_size_t n) +{ + const char *t1 = s1; + const char *t2 = s2; + + while (n--) + { + if (*t1 != *t2) + return (int) *t1 - (int) *t2; + + t1++; + t2++; + } + + return 0; +} +#ifndef APPLE_CC +int memcmp (const void *s1, const void *s2, grub_size_t n) + __attribute__ ((alias ("grub_memcmp"))); +#else +int memcmp (const void *s1, const void *s2, grub_size_t n) +{ + return grub_memcmp (s1, s2, n); +} +#endif + +int +grub_strcmp (const char *s1, const char *s2) +{ + while (*s1 && *s2) + { + if (*s1 != *s2) + break; + + s1++; + s2++; + } + + return (int) *s1 - (int) *s2; +} + +int +grub_strncmp (const char *s1, const char *s2, grub_size_t n) +{ + if (n == 0) + return 0; + + while (*s1 && *s2 && --n) + { + if (*s1 != *s2) + break; + + s1++; + s2++; + } + + return (int) *s1 - (int) *s2; +} + +char * +grub_strchr (const char *s, int c) +{ + do + { + if (*s == c) + return (char *) s; + } + while (*s++); + + return 0; +} + +char * +grub_strrchr (const char *s, int c) +{ + char *p = NULL; + + do + { + if (*s == c) + p = (char *) s; + } + while (*s++); + + return p; +} + +/* Copied from gnulib. + Written by Bruno Haible , 2005. */ +char * +grub_strstr (const char *haystack, const char *needle) +{ + /* Be careful not to look at the entire extent of haystack or needle + until needed. This is useful because of these two cases: + - haystack may be very long, and a match of needle found early, + - needle may be very long, and not even a short initial segment of + needle may be found in haystack. */ + if (*needle != '\0') + { + /* Speed up the following searches of needle by caching its first + character. */ + char b = *needle++; + + for (;; haystack++) + { + if (*haystack == '\0') + /* No match. */ + return NULL; + if (*haystack == b) + /* The first character matches. */ + { + const char *rhaystack = haystack + 1; + const char *rneedle = needle; + + for (;; rhaystack++, rneedle++) + { + if (*rneedle == '\0') + /* Found a match. */ + return (char *) haystack; + if (*rhaystack == '\0') + /* No match. */ + return NULL; + if (*rhaystack != *rneedle) + /* Nothing in this round. */ + break; + } + } + } + } + else + return (char *) haystack; +} + +int +grub_strword (const char *haystack, const char *needle) +{ + const char *n_pos = needle; + + while (grub_iswordseparator (*haystack)) + haystack++; + + while (*haystack) + { + /* Crawl both the needle and the haystack word we're on. */ + while(*haystack && !grub_iswordseparator (*haystack) + && *haystack == *n_pos) + { + haystack++; + n_pos++; + } + + /* If we reached the end of both words at the same time, the word + is found. If not, eat everything in the haystack that isn't the + next word (or the end of string) and "reset" the needle. */ + if ( (!*haystack || grub_iswordseparator (*haystack)) + && (!*n_pos || grub_iswordseparator (*n_pos))) + return 1; + else + { + n_pos = needle; + while (*haystack && !grub_iswordseparator (*haystack)) + haystack++; + while (grub_iswordseparator (*haystack)) + haystack++; + } + } + + return 0; +} + +int +grub_isspace (int c) +{ + return (c == '\n' || c == '\r' || c == ' ' || c == '\t'); +} + +int +grub_isprint (int c) +{ + return (c >= ' ' && c <= '~'); +} + + +unsigned long +grub_strtoul (const char *str, char **end, int base) +{ + unsigned long long num; + + num = grub_strtoull (str, end, base); + if (num > ~0UL) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected"); + return ~0UL; + } + + return (unsigned long) num; +} + +unsigned long long +grub_strtoull (const char *str, char **end, int base) +{ + unsigned long long num = 0; + int found = 0; + + /* Skip white spaces. */ + while (*str && grub_isspace (*str)) + str++; + + /* Guess the base, if not specified. The prefix `0x' means 16, and + the prefix `0' means 8. */ + if (str[0] == '0') + { + if (str[1] == 'x') + { + if (base == 0 || base == 16) + { + base = 16; + str += 2; + } + } + else if (base == 0 && str[1] >= '0' && str[1] <= '7') + base = 8; + } + + if (base == 0) + base = 10; + + while (*str) + { + unsigned long digit; + + digit = grub_tolower (*str) - '0'; + if (digit > 9) + { + digit += '0' - 'a' + 10; + if (digit >= (unsigned long) base) + break; + } + + found = 1; + + /* NUM * BASE + DIGIT > ~0ULL */ + if (num > grub_divmod64 (~0ULL - digit, base, 0)) + { + grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow is detected"); + return ~0ULL; + } + + num = num * base + digit; + str++; + } + + if (! found) + { + grub_error (GRUB_ERR_BAD_NUMBER, "unrecognized number"); + return 0; + } + + if (end) + *end = (char *) str; + + return num; +} + +char * +grub_strdup (const char *s) +{ + grub_size_t len; + char *p; + + len = grub_strlen (s) + 1; + p = (char *) grub_malloc (len); + if (! p) + return 0; + + return grub_memcpy (p, s, len); +} + +char * +grub_strndup (const char *s, grub_size_t n) +{ + grub_size_t len; + char *p; + + len = grub_strlen (s); + if (len > n) + len = n; + p = (char *) grub_malloc (len + 1); + if (! p) + return 0; + + grub_memcpy (p, s, len); + p[len] = '\0'; + return p; +} + +void * +grub_memset (void *s, int c, grub_size_t len) +{ + void *p = s; + grub_uint8_t pattern8 = c; + + if (len >= 3 * sizeof (unsigned long)) + { + unsigned long patternl = 0; + grub_size_t i; + + for (i = 0; i < sizeof (unsigned long); i++) + patternl |= ((unsigned long) pattern8) << (8 * i); + + while (len > 0 && (((grub_addr_t) p) & (sizeof (unsigned long) - 1))) + { + *(grub_uint8_t *) p = pattern8; + p = (grub_uint8_t *) p + 1; + len--; + } + while (len >= sizeof (unsigned long)) + { + *(unsigned long *) p = patternl; + p = (unsigned long *) p + 1; + len -= sizeof (unsigned long); + } + } + + while (len > 0) + { + *(grub_uint8_t *) p = pattern8; + p = (grub_uint8_t *) p + 1; + len--; + } + + return s; +} +#ifndef APPLE_CC +void *memset (void *s, int c, grub_size_t n) + __attribute__ ((alias ("grub_memset"))); +#else +void *memset (void *s, int c, grub_size_t n) +{ + return grub_memset (s, c, n); +} +#endif + +grub_size_t +grub_strlen (const char *s) +{ + const char *p = s; + + while (*p) + p++; + + return p - s; +} + +static inline void +grub_reverse (char *str) +{ + char *p = str + grub_strlen (str) - 1; + + while (str < p) + { + char tmp; + + tmp = *str; + *str = *p; + *p = tmp; + str++; + p--; + } +} + +/* Divide N by D, return the quotient, and store the remainder in *R. */ +grub_uint64_t +grub_divmod64 (grub_uint64_t n, grub_uint32_t d, grub_uint32_t *r) +{ + /* This algorithm is typically implemented by hardware. The idea + is to get the highest bit in N, 64 times, by keeping + upper(N * 2^i) = upper((Q * 10 + M) * 2^i), where upper + represents the high 64 bits in 128-bits space. */ + unsigned bits = 64; + unsigned long long q = 0; + unsigned m = 0; + + /* Skip the slow computation if 32-bit arithmetic is possible. */ + if (n < 0xffffffff) + { + if (r) + *r = ((grub_uint32_t) n) % d; + + return ((grub_uint32_t) n) / d; + } + + while (bits--) + { + m <<= 1; + + if (n & (1ULL << 63)) + m |= 1; + + q <<= 1; + n <<= 1; + + if (m >= d) + { + q |= 1; + m -= d; + } + } + + if (r) + *r = m; + + return q; +} + +/* Convert a long long value to a string. This function avoids 64-bit + modular arithmetic or divisions. */ +static char * +grub_lltoa (char *str, int c, unsigned long long n) +{ + unsigned base = (c == 'x') ? 16 : 10; + char *p; + + if ((long long) n < 0 && c == 'd') + { + n = (unsigned long long) (-((long long) n)); + *str++ = '-'; + } + + p = str; + + if (base == 16) + do + { + unsigned d = (unsigned) (n & 0xf); + *p++ = (d > 9) ? d + 'a' - 10 : d + '0'; + } + while (n >>= 4); + else + /* BASE == 10 */ + do + { + unsigned m; + + n = grub_divmod64 (n, 10, &m); + *p++ = m + '0'; + } + while (n); + + *p = 0; + + grub_reverse (str); + return p; +} + +static int +grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list args) +{ + char c; + grub_size_t count = 0; + auto void write_char (unsigned char ch); + auto void write_str (const char *s); + auto void write_fill (const char ch, int n); + + void write_char (unsigned char ch) + { + if (count < max_len) + *str++ = ch; + + count++; + } + + void write_str (const char *s) + { + while (*s) + write_char (*s++); + } + + void write_fill (const char ch, int n) + { + int i; + for (i = 0; i < n; i++) + write_char (ch); + } + + while ((c = *fmt++) != 0) + { + if (c != '%') + write_char (c); + else + { + char tmp[32]; + char *p; + unsigned int format1 = 0; + unsigned int format2 = ~ 0U; + char zerofill = ' '; + int rightfill = 0; + int n; + int longfmt = 0; + int longlongfmt = 0; + int unsig = 0; + + if (*fmt && *fmt =='-') + { + rightfill = 1; + fmt++; + } + + p = (char *) fmt; + /* Read formatting parameters. */ + while (*p && grub_isdigit (*p)) + p++; + + if (p > fmt) + { + char s[p - fmt + 1]; + grub_strncpy (s, fmt, p - fmt); + s[p - fmt] = 0; + if (s[0] == '0') + zerofill = '0'; + format1 = grub_strtoul (s, 0, 10); + fmt = p; + } + + if (*p && *p == '.') + { + p++; + fmt++; + while (*p && grub_isdigit (*p)) + p++; + + if (p > fmt) + { + char fstr[p - fmt + 1]; + grub_strncpy (fstr, fmt, p - fmt); + fstr[p - fmt] = 0; + format2 = grub_strtoul (fstr, 0, 10); + fmt = p; + } + } + + c = *fmt++; + if (c == 'l') + { + longfmt = 1; + c = *fmt++; + if (c == 'l') + { + longlongfmt = 1; + c = *fmt++; + } + } + + switch (c) + { + case 'p': + write_str ("0x"); + c = 'x'; + longlongfmt |= (sizeof (void *) == sizeof (long long)); + /* Fall through. */ + case 'x': + case 'u': + unsig = 1; + /* Fall through. */ + case 'd': + if (longlongfmt) + { + long long ll; + + ll = va_arg (args, long long); + grub_lltoa (tmp, c, ll); + } + else if (longfmt && unsig) + { + unsigned long l = va_arg (args, unsigned long); + grub_lltoa (tmp, c, l); + } + else if (longfmt) + { + long l = va_arg (args, long); + grub_lltoa (tmp, c, l); + } + else if (unsig) + { + unsigned u = va_arg (args, unsigned); + grub_lltoa (tmp, c, u); + } + else + { + n = va_arg (args, int); + grub_lltoa (tmp, c, n); + } + if (! rightfill && grub_strlen (tmp) < format1) + write_fill (zerofill, format1 - grub_strlen (tmp)); + write_str (tmp); + if (rightfill && grub_strlen (tmp) < format1) + write_fill (zerofill, format1 - grub_strlen (tmp)); + break; + + case 'c': + n = va_arg (args, int); + write_char (n & 0xff); + break; + + case 'C': + { + grub_uint32_t code = va_arg (args, grub_uint32_t); + int shift; + unsigned mask; + + if (code <= 0x7f) + { + shift = 0; + mask = 0; + } + else if (code <= 0x7ff) + { + shift = 6; + mask = 0xc0; + } + else if (code <= 0xffff) + { + shift = 12; + mask = 0xe0; + } + else if (code <= 0x1fffff) + { + shift = 18; + mask = 0xf0; + } + else if (code <= 0x3ffffff) + { + shift = 24; + mask = 0xf8; + } + else if (code <= 0x7fffffff) + { + shift = 30; + mask = 0xfc; + } + else + { + code = '?'; + shift = 0; + mask = 0; + } + + write_char (mask | (code >> shift)); + + for (shift -= 6; shift >= 0; shift -= 6) + write_char (0x80 | (0x3f & (code >> shift))); + } + break; + + case 's': + p = va_arg (args, char *); + if (p) + { + grub_size_t len = 0; + while (len < format2 && p[len]) + len++; + + if (!rightfill && len < format1) + write_fill (zerofill, format1 - len); + + grub_size_t i; + for (i = 0; i < len; i++) + write_char (*p++); + + if (rightfill && len < format1) + write_fill (zerofill, format1 - len); + } + else + write_str ("(null)"); + + break; + + default: + write_char (c); + break; + } + } + } + + *str = '\0'; + + return count; +} + +int +grub_vsnprintf (char *str, grub_size_t n, const char *fmt, va_list ap) +{ + grub_size_t ret; + + if (!n) + return 0; + + n--; + + ret = grub_vsnprintf_real (str, n, fmt, ap); + + return ret < n ? ret : n; +} + +int +grub_snprintf (char *str, grub_size_t n, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start (ap, fmt); + ret = grub_vsnprintf (str, n, fmt, ap); + va_end (ap); + + return ret; +} + +char * +grub_xvasprintf (const char *fmt, va_list ap) +{ + grub_size_t s, as = PREALLOC_SIZE; + char *ret; + + while (1) + { + va_list ap2; + va_copy (ap2, ap); + ret = grub_malloc (as + 1); + if (!ret) + return NULL; + + s = grub_vsnprintf_real (ret, as, fmt, ap2); + if (s <= as) + return ret; + + grub_free (ret); + as = s; + } +} + +char * +grub_xasprintf (const char *fmt, ...) +{ + va_list ap; + char *ret; + + va_start (ap, fmt); + ret = grub_xvasprintf (fmt, ap); + va_end (ap); + + return ret; +} + +/* Abort GRUB. This function does not return. */ +void +grub_abort (void) +{ + grub_printf ("\nAborted."); + + + grub_exit (); +} + +#if ! defined (APPLE_CC) && !defined (GRUB_UTIL) +/* GCC emits references to abort(). */ +void abort (void) __attribute__ ((alias ("grub_abort"))); +#endif + +#if NEED_ENABLE_EXECUTE_STACK && !defined(GRUB_UTIL) && !defined(GRUB_MACHINE_EMU) +/* Some gcc versions generate a call to this function + in trampolines for nested functions. */ +void __enable_execute_stack (void *addr __attribute__ ((unused))) +{ +} +#endif + +#if NEED_REGISTER_FRAME_INFO && !defined(GRUB_UTIL) +void __register_frame_info (void) +{ +} + +void __deregister_frame_info (void) +{ +} +#endif + diff --git a/libr/fs/p/grub/kern/mm.c b/libr/fs/p/grub/kern/mm.c new file mode 100644 index 0000000000..8d9b5db78a --- /dev/null +++ b/libr/fs/p/grub/kern/mm.c @@ -0,0 +1,570 @@ +/* mm.c - functions for memory manager */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +/* + The design of this memory manager. + + This is a simple implementation of malloc with a few extensions. These are + the extensions: + + - memalign is implemented efficiently. + + - multiple regions may be used as free space. They may not be + contiguous. + + Regions are managed by a singly linked list, and the meta information is + stored in the beginning of each region. Space after the meta information + is used to allocate memory. + + The memory space is used as cells instead of bytes for simplicity. This + is important for some CPUs which may not access multiple bytes at a time + when the first byte is not aligned at a certain boundary (typically, + 4-byte or 8-byte). The size of each cell is equal to the size of struct + grub_mm_header, so the header of each allocated/free block fits into one + cell precisely. One cell is 16 bytes on 32-bit platforms and 32 bytes + on 64-bit platforms. + + There are two types of blocks: allocated blocks and free blocks. + + In allocated blocks, the header of each block has only its size. Note that + this size is based on cells but not on bytes. The header is located right + before the returned pointer, that is, the header resides at the previous + cell. + + Free blocks constitutes a ring, using a singly linked list. The first free + block is pointed to by the meta information of a region. The allocator + attempts to pick up the second block instead of the first one. This is + a typical optimization against defragmentation, and makes the + implementation a bit easier. + + For safety, both allocated blocks and free ones are marked by magic + numbers. Whenever anything unexpected is detected, GRUB aborts the + operation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef MM_DEBUG +# undef grub_malloc +# undef grub_zalloc +# undef grub_realloc +# undef grub_free +# undef grub_memalign +#endif + + + +grub_mm_region_t grub_mm_base; + +/* Get a header from the pointer PTR, and set *P and *R to a pointer + to the header and a pointer to its region, respectively. PTR must + be allocated. */ +static void +get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r) +{ + if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1)) + grub_fatal ("unaligned pointer %p", ptr); + + for (*r = grub_mm_base; *r; *r = (*r)->next) + if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1) + && (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size) + break; + + if (! *r) + grub_fatal ("out of range pointer %p", ptr); + + *p = (grub_mm_header_t) ptr - 1; + if ((*p)->magic != GRUB_MM_ALLOC_MAGIC) + grub_fatal ("alloc magic is broken at %p", *p); +} + +/* Initialize a region starting from ADDR and whose size is SIZE, + to use it as free space. */ +void +grub_mm_init_region (void *addr, grub_size_t size) +{ + grub_mm_header_t h; + grub_mm_region_t r, *p, q; + +#if 0 + grub_printf ("Using memory for heap: start=%p, end=%p\n", addr, addr + (unsigned int) size); +#endif + + /* Allocate a region from the head. */ + r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN); + size -= (char *) r - (char *) addr + sizeof (*r); + + /* If this region is too small, ignore it. */ + if (size < GRUB_MM_ALIGN) + return; + + h = (grub_mm_header_t) (r + 1); + h->next = h; + h->magic = GRUB_MM_FREE_MAGIC; + h->size = (size >> GRUB_MM_ALIGN_LOG2); + + r->first = h; + r->pre_size = (grub_addr_t) r - (grub_addr_t) addr; + r->size = (h->size << GRUB_MM_ALIGN_LOG2); + + /* Find where to insert this region. Put a smaller one before bigger ones, + to prevent fragmentation. */ + for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p) + if (q->size > r->size) + break; + + *p = r; + r->next = q; +} + +/* Allocate the number of units N with the alignment ALIGN from the ring + buffer starting from *FIRST. ALIGN must be a power of two. Both N and + ALIGN are in units of GRUB_MM_ALIGN. Return a non-NULL if successful, + otherwise return NULL. */ +static void * +grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) +{ + grub_mm_header_t p, q; + + /* When everything is allocated side effect is that *first will have alloc + magic marked, meaning that there is no room in this region. */ + if ((*first)->magic == GRUB_MM_ALLOC_MAGIC) + return 0; + + /* Try to search free slot for allocation in this memory region. */ + for (q = *first, p = q->next; ; q = p, p = p->next) + { + grub_off_t extra; + + extra = ((grub_addr_t) (p + 1) >> GRUB_MM_ALIGN_LOG2) % align; + if (extra) + extra = align - extra; + + if (! p) + grub_fatal ("null in the ring"); + + if (p->magic != GRUB_MM_FREE_MAGIC) + grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic); + + if (p->size >= n + extra) + { + extra += (p->size - extra - n) & (~(align - 1)); + if (extra == 0 && p->size == n) + { + /* There is no special alignment requirement and memory block + is complete match. + + 1. Just mark memory block as allocated and remove it from + free list. + + Result: + +---------------+ previous block's next + | alloc, size=n | | + +---------------+ v + */ + q->next = p->next; + } + else if (align == 1 || p->size == n + extra) + { + /* There might be alignment requirement, when taking it into + account memory block fits in. + + 1. Allocate new area at end of memory block. + 2. Reduce size of available blocks from original node. + 3. Mark new area as allocated and "remove" it from free + list. + + Result: + +---------------+ + | free, size-=n | next --+ + +---------------+ | + | alloc, size=n | | + +---------------+ v + */ + + p->size -= n; + p += p->size; + } + else if (extra == 0) + { + grub_mm_header_t r; + + r = p + extra + n; + r->magic = GRUB_MM_FREE_MAGIC; + r->size = p->size - extra - n; + r->next = p->next; + q->next = r; + + if (q == p) + { + q = r; + r->next = r; + } + } + else + { + /* There is alignment requirement and there is room in memory + block. Split memory block to three pieces. + + 1. Create new memory block right after section being + allocated. Mark it as free. + 2. Add new memory block to free chain. + 3. Mark current memory block having only extra blocks. + 4. Advance to aligned block and mark that as allocated and + "remove" it from free list. + + Result: + +------------------------------+ + | free, size=extra | next --+ + +------------------------------+ | + | alloc, size=n | | + +------------------------------+ | + | free, size=orig.size-extra-n | <------+, next --+ + +------------------------------+ v + */ + grub_mm_header_t r; + + r = p + extra + n; + r->magic = GRUB_MM_FREE_MAGIC; + r->size = p->size - extra - n; + r->next = p; + + p->size = extra; + q->next = r; + p += extra; + } + + p->magic = GRUB_MM_ALLOC_MAGIC; + p->size = n; + + /* Mark find as a start marker for next allocation to fasten it. + This will have side effect of fragmenting memory as small + pieces before this will be un-used. */ + *first = q; + + return p + 1; + } + + /* Search was completed without result. */ + if (p == *first) + break; + } + + return 0; +} + +/* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */ +void * +grub_memalign (grub_size_t align, grub_size_t size) +{ + grub_mm_region_t r; + grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; + int count = 0; + + if (!grub_mm_base) + goto fail; + + align = (align >> GRUB_MM_ALIGN_LOG2); + if (align == 0) + align = 1; + + again: + + for (r = grub_mm_base; r; r = r->next) + { + void *p; + + p = grub_real_malloc (&(r->first), n, align); + if (p) + return p; + } + + /* If failed, increase free memory somehow. */ + switch (count) + { + case 0: + /* Invalidate disk caches. */ + grub_disk_cache_invalidate_all (); + count++; + goto again; + + case 1: + /* Unload unneeded modules. */ + grub_dl_unload_unneeded (); + count++; + goto again; + + default: + break; + } + + fail: + grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory"); + return 0; +} + +/* Allocate SIZE bytes and return the pointer. */ +void * +grub_malloc (grub_size_t size) +{ + return grub_memalign (0, size); +} + +/* Allocate SIZE bytes, clear them and return the pointer. */ +void * +grub_zalloc (grub_size_t size) +{ + void *ret; + + ret = grub_memalign (0, size); + if (ret) + grub_memset (ret, 0, size); + + return ret; +} + +/* Deallocate the pointer PTR. */ +void +grub_free (void *ptr) +{ + grub_mm_header_t p; + grub_mm_region_t r; + + if (! ptr) + return; + + get_header_from_pointer (ptr, &p, &r); + + if (r->first->magic == GRUB_MM_ALLOC_MAGIC) + { + p->magic = GRUB_MM_FREE_MAGIC; + r->first = p->next = p; + } + else + { + grub_mm_header_t q; + +#if 0 + q = r->first; + do + { + grub_printf ("%s:%d: q=%p, q->size=0x%x, q->magic=0x%x\n", + GRUB_FILE, __LINE__, q, q->size, q->magic); + q = q->next; + } + while (q != r->first); +#endif + + for (q = r->first; q >= p || q->next <= p; q = q->next) + { + if (q->magic != GRUB_MM_FREE_MAGIC) + grub_fatal ("free magic is broken at %p: 0x%x", q, q->magic); + + if (q >= q->next && (q < p || q->next > p)) + break; + } + + p->magic = GRUB_MM_FREE_MAGIC; + p->next = q->next; + q->next = p; + + if (p + p->size == p->next) + { + if (p->next == q) + q = p; + + p->next->magic = 0; + p->size += p->next->size; + p->next = p->next->next; + } + + if (q + q->size == p) + { + p->magic = 0; + q->size += p->size; + q->next = p->next; + } + + r->first = q; + } +} + +/* Reallocate SIZE bytes and return the pointer. The contents will be + the same as that of PTR. */ +void * +grub_realloc (void *ptr, grub_size_t size) +{ + grub_mm_header_t p; + grub_mm_region_t r; + void *q; + grub_size_t n; + + if (! ptr) + return grub_malloc (size); + + if (! size) + { + grub_free (ptr); + return 0; + } + + /* FIXME: Not optimal. */ + n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; + get_header_from_pointer (ptr, &p, &r); + + if (p->size >= n) + return ptr; + + q = grub_malloc (size); + if (! q) + return q; + + grub_memcpy (q, ptr, size); + grub_free (ptr); + return q; +} + +#ifdef MM_DEBUG +int grub_mm_debug = 0; + +void +grub_mm_dump_free (void) +{ + grub_mm_region_t r; + + for (r = grub_mm_base; r; r = r->next) + { + grub_mm_header_t p; + + /* Follow the free list. */ + p = r->first; + do + { + if (p->magic != GRUB_MM_FREE_MAGIC) + grub_fatal ("free magic is broken at %p: 0x%x", p, p->magic); + + grub_printf ("F:%p:%u:%p\n", + p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next); + p = p->next; + } + while (p != r->first); + } + + grub_printf ("\n"); +} + +void +grub_mm_dump (unsigned lineno) +{ + grub_mm_region_t r; + + grub_printf ("called at line %u\n", lineno); + for (r = grub_mm_base; r; r = r->next) + { + grub_mm_header_t p; + + for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1), + GRUB_MM_ALIGN); + (grub_addr_t) p < (grub_addr_t) (r+1) + r->size; + p++) + { + switch (p->magic) + { + case GRUB_MM_FREE_MAGIC: + grub_printf ("F:%p:%u:%p\n", + p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2, p->next); + break; + case GRUB_MM_ALLOC_MAGIC: + grub_printf ("A:%p:%u\n", p, (unsigned int) p->size << GRUB_MM_ALIGN_LOG2); + break; + } + } + } + + grub_printf ("\n"); +} + +void * +grub_debug_malloc (const char *file, int line, grub_size_t size) +{ + void *ptr; + + if (grub_mm_debug) + grub_printf ("%s:%d: malloc (0x%zx) = ", file, line, size); + ptr = grub_malloc (size); + if (grub_mm_debug) + grub_printf ("%p\n", ptr); + return ptr; +} + +void * +grub_debug_zalloc (const char *file, int line, grub_size_t size) +{ + void *ptr; + + if (grub_mm_debug) + grub_printf ("%s:%d: zalloc (0x%zx) = ", file, line, size); + ptr = grub_zalloc (size); + if (grub_mm_debug) + grub_printf ("%p\n", ptr); + return ptr; +} + +void +grub_debug_free (const char *file, int line, void *ptr) +{ + if (grub_mm_debug) + grub_printf ("%s:%d: free (%p)\n", file, line, ptr); + grub_free (ptr); +} + +void * +grub_debug_realloc (const char *file, int line, void *ptr, grub_size_t size) +{ + if (grub_mm_debug) + grub_printf ("%s:%d: realloc (%p, 0x%zx) = ", file, line, ptr, size); + ptr = grub_realloc (ptr, size); + if (grub_mm_debug) + grub_printf ("%p\n", ptr); + return ptr; +} + +void * +grub_debug_memalign (const char *file, int line, grub_size_t align, + grub_size_t size) +{ + void *ptr; + + if (grub_mm_debug) + grub_printf ("%s:%d: memalign (0x%zx, 0x%zx) = ", + file, line, align, size); + ptr = grub_memalign (align, size); + if (grub_mm_debug) + grub_printf ("%p\n", ptr); + return ptr; +} + +#endif /* MM_DEBUG */ diff --git a/libr/fs/p/grub/kern/partition.c b/libr/fs/p/grub/kern/partition.c new file mode 100644 index 0000000000..e6b2831d25 --- /dev/null +++ b/libr/fs/p/grub/kern/partition.c @@ -0,0 +1,249 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004,2007 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +#ifdef GRUB_UTIL +#include +#endif + +grub_partition_map_t grub_partition_map_list; + +/* + * Checks that disk->partition contains part. This function assumes that the + * start of part is relative to the start of disk->partition. Returns 1 if + * disk->partition is null. + */ +static int +grub_partition_check_containment (const grub_disk_t disk, + const grub_partition_t part) +{ + if (disk->partition == NULL) + return 1; + + if (part->start + part->len > disk->partition->len) + { + char *partname; + + partname = grub_partition_get_name (disk->partition); + grub_dprintf ("partition", "sub-partition %s%d of (%s,%s) ends after parent.\n", + part->partmap->name, part->number + 1, disk->name, partname); + grub_free (partname); + + return 0; + } + + return 1; +} + +static grub_partition_t +grub_partition_map_probe (const grub_partition_map_t partmap, + grub_disk_t disk, int partnum) +{ + grub_partition_t p = 0; + + auto int find_func (grub_disk_t d, const grub_partition_t partition); + + int find_func (grub_disk_t dsk, + const grub_partition_t partition) + { + if (partnum != partition->number) + return 0; + + if (!(grub_partition_check_containment (dsk, partition))) + return 0; + + p = (grub_partition_t) grub_malloc (sizeof (*p)); + if (! p) + return 1; + + grub_memcpy (p, partition, sizeof (*p)); + return 1; + } + + partmap->iterate (disk, find_func); + if (grub_errno) + goto fail; + + return p; + + fail: + grub_free (p); + return 0; +} + +grub_partition_t +grub_partition_probe (struct grub_disk *disk, const char *str) +{ + grub_partition_t part = 0; + grub_partition_t curpart = 0; + grub_partition_t tail; + const char *ptr; + + part = tail = disk->partition; + + for (ptr = str; *ptr;) + { + grub_partition_map_t partmap; + int num; + const char *partname, *partname_end; + + partname = ptr; + while (*ptr && grub_isalpha (*ptr)) + ptr++; + partname_end = ptr; + num = grub_strtoul (ptr, (char **) &ptr, 0) - 1; + + curpart = 0; + /* Use the first partition map type found. */ + FOR_PARTITION_MAPS(partmap) + { + if (partname_end != partname && + (grub_strncmp (partmap->name, partname, partname_end - partname) + != 0 || partmap->name[partname_end - partname] != 0)) + continue; + + disk->partition = part; + curpart = grub_partition_map_probe (partmap, disk, num); + disk->partition = tail; + if (curpart) + break; + + if (grub_errno == GRUB_ERR_BAD_PART_TABLE) + { + /* Continue to next partition map type. */ + grub_errno = GRUB_ERR_NONE; + continue; + } + + break; + } + + if (! curpart) + { + while (part) + { + curpart = part->parent; + grub_free (part); + part = curpart; + } + return 0; + } + curpart->parent = part; + part = curpart; + if (! ptr || *ptr != ',') + break; + ptr++; + } + + return part; +} + +int +grub_partition_iterate (struct grub_disk *disk, + int (*hook) (grub_disk_t disk, + const grub_partition_t partition)) +{ + int ret = 0; + + auto int part_iterate (grub_disk_t dsk, const grub_partition_t p); + + int part_iterate (grub_disk_t dsk, + const grub_partition_t partition) + { + struct grub_partition p = *partition; + + if (!(grub_partition_check_containment (dsk, partition))) + return 0; + + p.parent = dsk->partition; + dsk->partition = 0; + if (hook (dsk, &p)) + { + ret = 1; + return 1; + } + if (p.start != 0) + { + const struct grub_partition_map *partmap; + dsk->partition = &p; + FOR_PARTITION_MAPS(partmap) + { + grub_err_t err; + err = partmap->iterate (dsk, part_iterate); + if (err) + grub_errno = GRUB_ERR_NONE; + if (ret) + break; + } + } + dsk->partition = p.parent; + return ret; + } + + { + const struct grub_partition_map *partmap; + FOR_PARTITION_MAPS(partmap) + { + grub_err_t err; + err = partmap->iterate (disk, part_iterate); + if (err) + grub_errno = GRUB_ERR_NONE; + if (ret) + break; + } + } + + return ret; +} + +char * +grub_partition_get_name (const grub_partition_t partition) +{ + char *out = 0; + int curlen = 0; + grub_partition_t part; + for (part = partition; part; part = part->parent) + { + /* Even on 64-bit machines this buffer is enough to hold + longest number. */ + char buf[grub_strlen (part->partmap->name) + 25]; + int strl; + grub_snprintf (buf, sizeof (buf), "%s%d", part->partmap->name, + part->number + 1); + strl = grub_strlen (buf); + if (curlen) + { + out = grub_realloc (out, curlen + strl + 2); + grub_memcpy (out + strl + 1, out, curlen); + out[curlen + 1 + strl] = 0; + grub_memcpy (out, buf, strl); + out[strl] = ','; + curlen = curlen + 1 + strl; + } + else + { + curlen = strl; + out = grub_strdup (buf); + } + } + return out; +} diff --git a/libr/fs/p/grub/kern/term.c b/libr/fs/p/grub/kern/term.c new file mode 100644 index 0000000000..d607161fc3 --- /dev/null +++ b/libr/fs/p/grub/kern/term.c @@ -0,0 +1,130 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +struct grub_term_output *grub_term_outputs_disabled; +struct grub_term_input *grub_term_inputs_disabled; +struct grub_term_output *grub_term_outputs; +struct grub_term_input *grub_term_inputs; + +void (*grub_term_poll_usb) (void) = NULL; + +/* Put a Unicode character. */ +static void +grub_putcode_dumb (grub_uint32_t code, + struct grub_term_output *term) +{ + struct grub_unicode_glyph c = + { + .base = code, + .variant = 0, + .attributes = 0, + .ncomb = 0, + .combining = 0, + .estimated_width = 1 + }; + + if (code == '\t' && term->getxy) + { + int n; + + n = 8 - ((term->getxy (term) >> 8) & 7); + while (n--) + grub_putcode_dumb (' ', term); + + return; + } + + (term->putchar) (term, &c); + if (code == '\n') + grub_putcode_dumb ('\r', term); +} + +static void +grub_xputs_dumb (const char *str) +{ + for (; *str; str++) + { + grub_term_output_t term; + grub_uint32_t code = *str; + if (code > 0x7f) + code = '?'; + + FOR_ACTIVE_TERM_OUTPUTS(term) + grub_putcode_dumb (code, term); + } +} + +void (*grub_xputs) (const char *str) = grub_xputs_dumb; + +static int pending_key = GRUB_TERM_NO_KEY; + +int +grub_checkkey (void) +{ + grub_term_input_t term; + + if (pending_key != GRUB_TERM_NO_KEY) + return pending_key; + + if (grub_term_poll_usb) + grub_term_poll_usb (); + + FOR_ACTIVE_TERM_INPUTS(term) + { + pending_key = term->getkey (term); + if (pending_key != GRUB_TERM_NO_KEY) + return pending_key; + } + + return -1; +} + +int +grub_getkey (void) +{ + int ret; + + grub_refresh (); + + grub_checkkey (); + while (pending_key == GRUB_TERM_NO_KEY) + { + //grub_cpu_idle (); + grub_checkkey (); + } + ret = pending_key; + pending_key = GRUB_TERM_NO_KEY; + return ret; +} + + +void +grub_refresh (void) +{ + struct grub_term_output *term; + + FOR_ACTIVE_TERM_OUTPUTS(term) + grub_term_refresh (term); +} diff --git a/libr/fs/p/grub/kern/time.c b/libr/fs/p/grub/kern/time.c new file mode 100644 index 0000000000..6521ec66fe --- /dev/null +++ b/libr/fs/p/grub/kern/time.c @@ -0,0 +1,37 @@ +/* time.c - kernel time functions */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include + +typedef grub_uint64_t (*get_time_ms_func_t) (void); + +/* Function pointer to the implementation in use. */ +static get_time_ms_func_t get_time_ms_func; + +grub_uint64_t +grub_get_time_ms (void) +{ + return get_time_ms_func (); +} + +void +grub_install_get_time_ms (get_time_ms_func_t func) +{ + get_time_ms_func = func; +} diff --git a/libr/fs/p/grub/main.c b/libr/fs/p/grub/main.c new file mode 100644 index 0000000000..c16f71bcfd --- /dev/null +++ b/libr/fs/p/grub/main.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include + +extern struct grub_fs grub_ext2_fs; + +void grub_exit () { + printf ("GTFO!\n"); + exit (0); +} + +void read_foo (struct grub_disk *disk, grub_disk_addr_t sector, grub_size_t size, unsigned char *buf) { + //printf ("==> DISK %x\n", disk); + //printf ("==> OFFSET %x\n", offset); + //printf ("[foo]==> Reading hook %x %x\n", sector, size); + //printf ("==> land: %p\n", buf); + size=512; + { + FILE *fd = fopen ("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]); + fclose (fd); + } +} + +void read_hook (grub_disk_addr_t sector, unsigned long offset, unsigned long length, unsigned char *buf) { + //printf ("[hook]==> Reading hook sector=%x offset=%x %x\n", sector, offset, length); + //printf ("[hook]==> last %p\n", buf); + { + int size=length; + FILE *fd = fopen("test.fs.img", "rb"); + fseek (fd, (512*sector)+offset, SEEK_SET); + fread (buf, 1, size, fd); + //write (1, buf, size); + //printf ("BUF: %x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); + fclose (fd); + } +} + +grub_file_t openimage(grub_fs_t fs, const char *str) { + grub_file_t file = 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->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->read = 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"); + if (fd == NULL) { + printf ("Cannot open fs image\n"); + return NULL; + } + fread (file->data, file->size, 1, fd); + fclose (fd); + } + file->read_hook = read_hook; + return file; +} + +int dirhook (const char *filename, const struct grub_dirhook_info *info) { + //info->mtimeset + //info->case_insensitive + printf ("DIRFILE: %c (%d) %s\n", info->dir?'d':'f', + info->mtime, filename); + return 0; +} + +int do_main() { + struct grub_file *file; + struct grub_fs *e2; + grub_err_t err; + struct grub_disk disk; + + e2 = &grub_ext2_fs; + file = openimage (e2, "test.fs.img"); + if (file == NULL) { + printf ("oops\n"); + return 0; + } + + err = e2->open (file, "/test"); + if (err == 0) { + char buf[1024]; + err = e2->read (file, buf, file->size); +//file->read_hook (2, 0, 0); + write (1, buf, file->size); + e2->close (file); + + // Root directory list + err = e2->dir (file->device, "/", dirhook); + if (err != 0) + grub_print_error (); + } else { + grub_print_error (); + printf ("error is : %d\n", err); + return 0; + } + return 1; +} + +main() { + printf ("Hello grubfs!\n"); + if (do_main()) { + printf ("\n** worked!\n"); + } else { + printf ("\n** failed!\n"); + } + return 0; +} diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 5eecb86cb1..bd5d560b0a 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -3,6 +3,7 @@ #include "r_types.h" #include "r_io.h" +#include "r_fs.h" #include "r_lib.h" #include "r_lang.h" #include "r_asm.h" @@ -96,6 +97,7 @@ typedef struct r_core_t { RSearch *search; RSign *sign; RVm *vm; + RFS *fs; char *cmdqueue; int rtr_n; RCoreRtrHost rtr_host[RTR_MAX_HOSTS]; diff --git a/libr/include/r_fs.h b/libr/include/r_fs.h index e2b1f73064..f47f6d6b28 100644 --- a/libr/include/r_fs.h +++ b/libr/include/r_fs.h @@ -2,21 +2,75 @@ #define _LIB_R_FS_H_ #include +#include #include +struct r_fs_plugin_t; +struct r_fs_root_t; +struct r_fs_t; + typedef struct r_fs_t { RIOBind iob; - RList *plugins; - RList *mounts; + RList /**/ *plugins; + RList /**/ *roots; + void *ptr; } RFS; +typedef struct r_fs_file_t { + char *name; + char *path; + ut64 off; + ut32 size; + ut8 *data; + void *ctx; + char type; + ut64 time; + struct r_fs_plugin_t *fs; + void *ptr; +} RFSFile; + +typedef struct r_fs_root_t { + char *path; + ut64 delta; + struct r_fs_plugin_t *fs; + void *ptr; +} RFSRoot; + typedef struct r_fs_plugin_t { const char *name; + const char *desc; + RFSFile* (*open)(const char *path); + boolt (*read)(RFSFile *fs, ut64 addr, int len); + void (*close)(RFSFile *fs); + RList *(*dir)(RFSRoot *root, const char *path); + void (*init)(); } RFSPlugin; -typedef struct r_fs_mount_t { - const char *path; - RFSPlugin *plugin; -} RFSRoot; +#define R_FS_FILE_TYPE_DIRECTORY 'd' +#define R_FS_FILE_TYPE_REGULAR 'r' + +#ifdef R_API + +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 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 RList *r_fs_dir(RFS* fs, const char *path); + +/* file.c */ +R_API RFSFile *r_fs_file_new (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); + +/* plugins */ +extern RFSPlugin r_fs_plugin_ext2; +#endif #endif diff --git a/plugins.def.cfg b/plugins.def.cfg index 0c2723429e..51beebfc53 100644 --- a/plugins.def.cfg +++ b/plugins.def.cfg @@ -32,6 +32,7 @@ bp.ppc cmd.dummy crypto.aes debug.native +fs.ext2 io.debug io.rap io.gdb