Move OS-specific driver configuration to grub_util_fd_open. This

moves OS-dependent parts from kern/emu/hostdisk.c to
	grub-core/osdep/*/hostdisk.c.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-09 07:04:25 +02:00
parent f7676b7016
commit caca1c70cf
13 changed files with 101 additions and 93 deletions

View file

@ -1,3 +1,9 @@
2013-10-09 Vladimir Serbinenko <phcoder@gmail.com>
Move OS-specific driver configuration to grub_util_fd_open. This
moves OS-dependent parts from kern/emu/hostdisk.c to
grub-core/osdep/*/hostdisk.c.
2013-10-09 Vladimir Serbinenko <phcoder@gmail.com>
* util/grub-mkimage.c (generate_image): Use size_t instead of

View file

@ -232,36 +232,12 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f
*max = ~0ULL;
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
#ifdef O_SYNC
flags |= O_SYNC;
#endif
#ifdef O_FSYNC
flags |= O_FSYNC;
#endif
#ifdef O_BINARY
flags |= O_BINARY;
#endif
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
int sysctl_flags, sysctl_oldflags;
size_t sysctl_size = sizeof (sysctl_flags);
if (sysctlbyname ("kern.geom.debugflags", &sysctl_oldflags, &sysctl_size, NULL, 0))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot get current flags of sysctl kern.geom.debugflags");
return GRUB_UTIL_FD_INVALID;
}
sysctl_flags = sysctl_oldflags | 0x10;
if (! (sysctl_oldflags & 0x10)
&& sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_flags, sysctl_size))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags of sysctl kern.geom.debugflags");
return GRUB_UTIL_FD_INVALID;
}
#endif
if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 &&
data->access_mode == (flags & O_ACCMODE))
@ -290,21 +266,6 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f
}
}
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
if (! (sysctl_oldflags & 0x10)
&& sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_oldflags, sysctl_size))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags back to the old value for sysctl kern.geom.debugflags");
return GRUB_UTIL_FD_INVALID;
}
#endif
#if defined(__APPLE__)
/* If we can't have exclusive access, try shared access */
if (fd < 0)
fd = open(map[disk->id].device, flags | O_SHLOCK);
#endif
if (!GRUB_UTIL_FD_IS_VALID(data->fd))
{
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
@ -312,8 +273,6 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f
return GRUB_UTIL_FD_INVALID;
}
grub_hostdisk_configure_device_driver (fd);
if (grub_util_fd_seek (fd, map[disk->id].device,
sector << disk->log_sector_size))
{

View file

@ -68,7 +68,23 @@ grub_util_get_fd_size (grub_util_fd_t fd, const char *name, unsigned *log_secsiz
return nr << log_sector_size;
}
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags)
{
grub_util_fd_t ret;
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
#ifdef O_BINARY
flags |= O_BINARY;
#endif
ret = open (os_dev, flags);
/* If we can't have exclusive access, try shared access */
if (ret < 0)
ret = open (os_dev, flags | O_SHLOCK);
return ret;
}

View file

@ -169,6 +169,13 @@ grub_util_fd_open (const char *dev, int flg)
IPTR unit = 0;
ULONG flags = 0;
#ifdef O_LARGEFILE
flg |= O_LARGEFILE;
#endif
#ifdef O_BINARY
flg |= O_BINARY;
#endif
ret->off = 0;
if (dev[0] == '\0')
@ -479,12 +486,6 @@ grub_util_fd_sync (grub_util_fd_t fd)
}
}
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
{
}
void
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
{

View file

@ -52,11 +52,6 @@ grub_util_get_fd_size_os (grub_util_fd_t fd __attribute__ ((unused)),
return -1;
}
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
{
}
void
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
{

View file

@ -86,7 +86,6 @@ grub_util_part_to_disk (const char *os_dev, struct stat *st,
strerror (errno));
return xstrdup (os_dev);
}
/* We don't call configure_device_driver since this isn't a floppy device name. */
if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == -1)
{
grub_error (GRUB_ERR_BAD_DEVICE,
@ -156,7 +155,7 @@ grub_util_find_partition_start_os (const char *dev)
struct disklabel label;
int p_index;
fd = open (dev, O_RDONLY);
fd = grub_util_fd_open (dev, O_RDONLY);
if (fd == -1)
{
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
@ -165,7 +164,6 @@ grub_util_find_partition_start_os (const char *dev)
}
# if defined(__NetBSD__)
configure_device_driver (fd);
/* First handle the case of disk wedges. */
if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == 0)
{

View file

@ -60,8 +60,8 @@
after successfully opening the device. For now, it simply prevents the
floppy driver from retrying operations on failure, as otherwise the
driver takes a while to abort when there is no floppy in the drive. */
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd)
static void
configure_device_driver (grub_util_fd_t fd)
{
struct stat st;
@ -78,11 +78,24 @@ grub_hostdisk_configure_device_driver (grub_util_fd_t fd)
return;
}
}
#else
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags)
{
grub_util_fd_t ret;
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
#ifdef O_BINARY
flags |= O_BINARY;
#endif
ret = open (os_dev, flags);
if (ret >= 0)
configure_device_driver (fd);
return ret;
}
#endif
grub_int64_t

View file

@ -48,11 +48,6 @@
# include <sys/mount.h>
# include <libgeom.h>
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
{
}
grub_int64_t
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
{
@ -83,3 +78,45 @@ void
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
{
}
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags)
{
grub_util_fd_t ret;
int sysctl_flags, sysctl_oldflags;
size_t sysctl_size = sizeof (sysctl_flags);
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
#ifdef O_BINARY
flags |= O_BINARY;
#endif
if (sysctlbyname ("kern.geom.debugflags", &sysctl_oldflags, &sysctl_size, NULL, 0))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot get current flags of sysctl kern.geom.debugflags");
return GRUB_UTIL_FD_INVALID;
}
sysctl_flags = sysctl_oldflags | 0x10;
if (! (sysctl_oldflags & 0x10)
&& sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_flags, sysctl_size))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags of sysctl kern.geom.debugflags");
return GRUB_UTIL_FD_INVALID;
}
ret = open (os_dev, flags);
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
if (! (sysctl_oldflags & 0x10)
&& sysctlbyname ("kern.geom.debugflags", NULL , 0, &sysctl_oldflags, sysctl_size))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot set flags back to the old value for sysctl kern.geom.debugflags");
close (ret);
return GRUB_UTIL_FD_INVALID;
}
#endif
return ret;
}

View file

@ -47,11 +47,6 @@
#include <hurd/fs.h>
#include <sys/mman.h>
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
{
}
int
grub_util_hurd_get_disk_info (const char *dev, grub_uint32_t *secsize, grub_disk_addr_t *offset,
grub_disk_addr_t *size, char **parent)

View file

@ -67,11 +67,6 @@ grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_sec
return minfo.dki_capacity << log_sector_size;
}
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
{
}
void
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
{

View file

@ -53,9 +53,6 @@
/* Maybe libc doesn't have large file support. */
# include <linux/unistd.h> /* _llseek */
# endif /* (GLIBC < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR < 1)) */
# ifndef BLKFLSBUF
# define BLKFLSBUF _IO (0x12,97) /* flush buffer cache */
# endif /* ! BLKFLSBUF */
#endif /* __linux__ */
grub_uint64_t
@ -166,11 +163,20 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
return size;
}
#if !defined (__NetBSD__) && !defined (__APPLE__) && !defined (__FreeBSD__) && !defined(__FreeBSD_kernel__)
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags)
{
#ifdef O_LARGEFILE
flags |= O_LARGEFILE;
#endif
#ifdef O_BINARY
flags |= O_BINARY;
#endif
return open (os_dev, flags);
}
#endif
const char *
grub_util_fd_strerror (void)

View file

@ -137,11 +137,6 @@ grub_util_get_fd_size (grub_util_fd_t hd, const char *name_in,
return size;
}
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
{
}
void
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
{

View file

@ -126,14 +126,6 @@ grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_sec
/* REturns partition offset in 512B blocks. */
grub_disk_addr_t
grub_hostdisk_find_partition_start_os (const char *dev);
/* Adjust device driver parameters. This function should be called just
after successfully opening the device. For now, it simply prevents the
floppy driver from retrying operations on failure, as otherwise the
driver takes a while to abort when there is no floppy in the drive.
For now it's non-nop only on NetBSD.
*/
void
grub_hostdisk_configure_device_driver (grub_util_fd_t fd);
void
grub_hostdisk_flush_initial_buffer (const char *os_dev);