Received: from mailman by lists.gnu.org with archive (Exim 4.33) id 1BtrZe-0001or-IP for mharc-grub-devel@gnu.org; Sun, 08 Aug 2004 13:35:10 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1BtrZc-0001oV-HI for grub-devel@gnu.org; Sun, 08 Aug 2004 13:35:08 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1BtrZa-0001oF-Sr for grub-devel@gnu.org; Sun, 08 Aug 2004 13:35:07 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BtrZa-0001o5-Ig for grub-devel@gnu.org; Sun, 08 Aug 2004 13:35:06 -0400 Received: from [195.113.31.125] (helo=artax.karlin.mff.cuni.cz) by monty-python.gnu.org with esmtp (Exim 4.34) id 1BtrVJ-0002vQ-FA for grub-devel@gnu.org; Sun, 08 Aug 2004 13:30:42 -0400 Received: by artax.karlin.mff.cuni.cz (Postfix, from userid 17122) id 80C814D49; Sun, 8 Aug 2004 19:30:40 +0200 (CEST) Date: Sun, 8 Aug 2004 19:30:40 +0200 To: grub-devel@gnu.org Message-ID: <20040808173040.GA11531@artax.karlin.mff.cuni.cz> Mail-Followup-To: grub-devel@gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.6i From: ebik@artax.karlin.mff.cuni.cz (Tomas Ebenlendr) Subject: bugfix, hostfs X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 Aug 2004 17:35:09 -0000 I'm thinkig about module loading in grub-emu (now I'm exploring dl.c, ld and so on) and as side efect I wrote access to host filesystem for grub. This also discovered a bug in dl.c which occur when 'normal.mod' exists, but cannot be loaded. Here is the bugfix: ############################################################## Changelog: 2004-08-08 Tomas Ebenlendr * kern/dl.c (grub_dl_load_file): Fixed bug: When grub_dl_load_core fails, mod shouldn't be derefered. diff -rupN -x CVS grub2_x/kern/dl.c grub2_wodiff/kern/dl.c --- grub2_x/kern/dl.c 2004-06-05 00:20:18.000000000 +0200 +++ grub2_wodiff/kern/dl.c 2004-08-08 19:20:04.000000000 +0200 @@ -548,7 +548,7 @@ grub_dl_load_file (const char *filename) goto failed; mod = grub_dl_load_core (core, size); - mod->ref_count = 0; + if (mod) mod->ref_count = 0; failed: grub_file_close (file); ############################################################### And here is the hostfs patch. It adds device (host) to list of devices. Directory (host)/ is root directory of the host filesystem. ############################################################### file conf/i386-pc.mk should be generated before commiting, file conf/powerpc-ieee1275.rmk should be patched same way as i386-pc.rmk Changelog: 2004-08-08 Tomas Ebenlendr Added support for accessing files via host filesystem to grub-emu. * conf/i386-pc.rmk (grub_emu_SOURCES): Added util/hostdisk.c and fs/hostfs.c * fs/hostfs.c: New file. Connects grub fs interface and libc file interface. * include/grub/hostdisk.h: New file, prototypes for inicialization and destruction of device "host". * util/hostdisk.c: New file, implements device "host" (i.e., tells that device "host" is disk with filesystem hostfs). * util/grub-emu.c (main): Added inicialzation and destruction of hostfs and hostdisk. diff -rupN -x CVS grub2_x/conf/i386-pc.rmk grub2_wodiff/conf/i386-pc.rmk --- grub2_x/conf/i386-pc.rmk 2004-08-08 14:18:18.000000000 +0200 +++ grub2_wodiff/conf/i386-pc.rmk 2004-08-08 15:41:50.000000000 +0200 @@ -64,7 +64,8 @@ grub_emu_SOURCES = kern/main.c kern/devi kern/misc.c kern/loader.c kern/rescue.c kern/term.c \ disk/i386/pc/partition.c kern/env.c commands/ls.c \ commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c \ - util/i386/pc/biosdisk.c fs/fat.c fs/ext2.c fs/ufs.c fs/minix.c \ + util/i386/pc/biosdisk.c util/hostdisk.c \ + fs/fat.c fs/ext2.c fs/ufs.c fs/minix.c fs/hostfs.c \ normal/cmdline.c normal/command.c normal/main.c normal/menu.c normal/arg.c \ util/console.c util/grub-emu.c util/misc.c util/i386/pc/getroot.c grub_emu_LDFLAGS = -lncurses diff -rupN -x CVS grub2_x/fs/hostfs.c grub2_wodiff/fs/hostfs.c --- grub2_x/fs/hostfs.c 1970-01-01 01:00:00.000000000 +0100 +++ grub2_wodiff/fs/hostfs.c 2004-08-08 16:00:20.000000000 +0200 @@ -0,0 +1,170 @@ +/* hostfs.c - host filesystem for grub-emu */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2004 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 2 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, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifndef GRUB_UTIL +#error cannot live outside host fs +#endif + + +static grub_err_t +host_mount(grub_device_t device) +{ + if ((signed) device->disk->id != -2) { + grub_error(GRUB_ERR_BAD_FS, "not a host filesystem"); + return grub_errno; + } + return 0; +} + +static grub_err_t +errno2err(void) +{ + grub_err_t r; + switch (errno) + { + case EMFILE: + case ENFILE: + case ENOMEM: r = GRUB_ERR_OUT_OF_MEMORY; break; + case ENOTDIR: + case ENOENT: r = GRUB_ERR_FILE_NOT_FOUND; break; + case ELOOP: r = GRUB_ERR_SYMLINK_LOOP; break; + case EIO: r = GRUB_ERR_FILE_READ_ERROR; break; + default: r = GRUB_ERR_BAD_ARGUMENT; break; + } + grub_error(r, strerror(errno)); + return r; +} + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_host_open (struct grub_file *file, const char *name) +{ + struct stat statbuf; + + if (host_mount(file->device)) return grub_errno; + + file->data = (void *) open(name, O_RDONLY); + if (file->data == (void *) -1) + return errno2err(); + fstat((int) file->data, &statbuf); + file->size = statbuf.st_size; + file->offset = 0; + + return 0; +} + +static grub_err_t +grub_host_close (grub_file_t file) +{ + close ((int) file->data); + file->data = (void *) -1; + return 0; +} + +/* Read LEN bytes data from FILE into BUF. */ +static grub_ssize_t +grub_host_read (grub_file_t file, char *buf, grub_ssize_t len) +{ + int rv; + + rv = read((int) file->data, buf, len); + if (rv == -1) + return errno2err(); + return rv; +} + + +static grub_err_t +grub_host_dir (grub_device_t device, const char *path, + int (*hook) (const char *filename, int dir)) +{ + DIR * dir; + struct dirent * dent; + struct stat stent; + static char pathbuf[/*FIXME*/2048 + NAME_MAX]; + char * ename=pathbuf; + + if (host_mount(device)) return grub_errno; + grub_strncpy(pathbuf,path,/*FIXME*/2048 - 1); + pathbuf[/*FIXME*/2048 - 1]=0; + while (*ename) ename++;/* let ename point after 'path' */ + ename[NAME_MAX]=0; + + dir = opendir(pathbuf); + if (dir == 0) + return errno2err(); + + while ((dent = readdir (dir)) != 0) + { + grub_strncpy(ename, dent->d_name, NAME_MAX); + lstat(pathbuf,&stent); + hook (dent->d_name, (stent.st_mode & S_IFMT) == S_IFDIR); + } + return 0; +} + +static grub_err_t +grub_host_label (grub_device_t device, char **label) +{ + if (host_mount(device)) return grub_errno; + *label = 0; + return 0; +} + + +static struct grub_fs grub_host_fs = + { + .name = "host", + .dir = grub_host_dir, + .open = grub_host_open, + .read = grub_host_read, + .close = grub_host_close, + .label = grub_host_label, + .next = 0 + }; + +void +grub_hostfs_init (void) +{ + grub_fs_register (&grub_host_fs); +} + +void +grub_hostfs_fini (void) +{ + grub_fs_unregister (&grub_host_fs); +} + diff -rupN -x CVS grub2_x/include/grub/fs.h grub2_wodiff/include/grub/fs.h --- grub2_x/include/grub/fs.h 2004-08-08 14:18:18.000000000 +0200 +++ grub2_wodiff/include/grub/fs.h 2004-08-08 14:27:38.000000000 +0200 @@ -74,6 +74,8 @@ void grub_ufs_init (void); void grub_ufs_fini (void); void grub_minix_init (void); void grub_minix_fini (void); +void grub_hostfs_init (void); +void grub_hostfs_fini (void); #endif /* GRUB_UTIL */ #endif /* ! GRUB_FS_HEADER */ diff -rupN -x CVS grub2_x/include/grub/hostdisk.h grub2_wodiff/include/grub/hostdisk.h --- grub2_x/include/grub/hostdisk.h 1970-01-01 01:00:00.000000000 +0100 +++ grub2_wodiff/include/grub/hostdisk.h 2004-08-08 14:27:38.000000000 +0200 @@ -0,0 +1,26 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 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 2 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 GRUB; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GRUB_HOSTDISK_HEADER +#define GRUB_HOSTDISK_HEADER 1 + +void grub_util_hostdisk_init (void); +void grub_util_hostdisk_fini (void); + +#endif /* ! GRUB_HOSTDISK_HEADER */ diff -rupN -x CVS grub2_x/util/grub-emu.c grub2_wodiff/util/grub-emu.c --- grub2_x/util/grub-emu.c 2004-08-08 14:18:18.000000000 +0200 +++ grub2_wodiff/util/grub-emu.c 2004-08-08 14:29:16.000000000 +0200 @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2003 Free Software Foundation, Inc. + * Copyright (C) 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 @@ -35,6 +35,8 @@ #include #include +#include + #ifdef __NetBSD__ /* NetBSD uses /boot for its boot block. */ # define DEFAULT_DIRECTORY "/grub" @@ -154,7 +156,9 @@ main (int argc, char *argv[]) /* XXX: This is a bit unportable. */ grub_util_biosdisk_init (args.dev_map); + grub_util_hostdisk_init (); + grub_hostfs_init (); /* Initialize the default modules. */ grub_fat_init (); grub_ext2_init (); @@ -172,6 +176,7 @@ main (int argc, char *argv[]) /* Start GRUB! */ grub_main (); + grub_util_hostdisk_fini (); grub_util_biosdisk_fini (); grub_normal_fini (); grub_ufs_fini (); @@ -182,6 +187,7 @@ main (int argc, char *argv[]) grub_cmp_fini (); grub_cat_fini (); grub_terminal_fini (); + grub_hostfs_fini (); return 0; } diff -rupN -x CVS grub2_x/util/hostdisk.c grub2_wodiff/util/hostdisk.c --- grub2_x/util/hostdisk.c 1970-01-01 01:00:00.000000000 +0100 +++ grub2_wodiff/util/hostdisk.c 2004-08-08 15:59:33.000000000 +0200 @@ -0,0 +1,92 @@ +/* hostdisk.c - device for hostfs */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 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 2 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 GRUB; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +static int +grub_util_hostdisk_iterate (int (*hook) (const char *name)) +{ + if (hook("host")) return 1; + else return 0; +} + +/*FIXME*/ +static grub_err_t +grub_util_hostdisk_open (const char *name, grub_disk_t disk) +{ + if (grub_strcmp(name,"host") != 0) + return grub_error(GRUB_ERR_UNKNOWN_DEVICE, "not a host fake-disk"); + disk->has_partitions = 0; + disk->id = -2; + disk->total_sectors = 1234; + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_util_hostdisk_read (grub_disk_t disk __attribute__ ((unused)), + unsigned long sector __attribute__ ((unused)), + unsigned long size __attribute__ ((unused)), + char *buf __attribute__ ((unused))) +{ + /* BAD_FS is here for filesystem probe to work */ + grub_error (GRUB_ERR_BAD_FS, "cannot read from fake-device host"); + return grub_errno; +} + + +static grub_err_t +grub_util_hostdisk_write (grub_disk_t disk __attribute__ ((unused)), + unsigned long sector __attribute__ ((unused)), + unsigned long size __attribute__ ((unused)), + const char *buf __attribute__ ((unused))) +{ + grub_error (GRUB_ERR_BAD_FS, "cannot write to fake-device'host"); + return grub_errno; +} + + +static struct grub_disk_dev grub_util_hostdisk_dev = + { + .name = "hostdisk", + .iterate = grub_util_hostdisk_iterate, + .open = grub_util_hostdisk_open, + .close = 0, + .read = grub_util_hostdisk_read, + .write = grub_util_hostdisk_write, + .next = 0 + }; + + + +void +grub_util_hostdisk_init (void) +{ + grub_disk_dev_register (&grub_util_hostdisk_dev); +} + +void +grub_util_hostdisk_fini (void) +{ + grub_disk_dev_unregister (&grub_util_hostdisk_dev); +} ############################################################### -- Tomas 'ebi' Ebenlendr http://get.to/ebik PF 2004.60328080728