diff --git a/ChangeLog b/ChangeLog index b6d3cb372..2c43dceb6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2013-10-09 Vladimir Serbinenko + + 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 * util/grub-mkimage.c (generate_image): Use size_t instead of diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index a29eb5f3a..fb7d72edc 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -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)) { diff --git a/grub-core/osdep/apple/hostdisk.c b/grub-core/osdep/apple/hostdisk.c index ac653bb6a..c9df43e4f 100644 --- a/grub-core/osdep/apple/hostdisk.c +++ b/grub-core/osdep/apple/hostdisk.c @@ -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; } diff --git a/grub-core/osdep/aros/hostdisk.c b/grub-core/osdep/aros/hostdisk.c index b6a2de9ba..75fafe8d0 100644 --- a/grub-core/osdep/aros/hostdisk.c +++ b/grub-core/osdep/aros/hostdisk.c @@ -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))) { diff --git a/grub-core/osdep/basic/hostdisk.c b/grub-core/osdep/basic/hostdisk.c index 7ea079557..529bf29f3 100644 --- a/grub-core/osdep/basic/hostdisk.c +++ b/grub-core/osdep/basic/hostdisk.c @@ -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))) { diff --git a/grub-core/osdep/bsd/getroot.c b/grub-core/osdep/bsd/getroot.c index db2fb07d3..0cb0da109 100644 --- a/grub-core/osdep/bsd/getroot.c +++ b/grub-core/osdep/bsd/getroot.c @@ -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) { diff --git a/grub-core/osdep/bsd/hostdisk.c b/grub-core/osdep/bsd/hostdisk.c index a5dadf195..dc845b11f 100644 --- a/grub-core/osdep/bsd/hostdisk.c +++ b/grub-core/osdep/bsd/hostdisk.c @@ -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 diff --git a/grub-core/osdep/freebsd/hostdisk.c b/grub-core/osdep/freebsd/hostdisk.c index 86d876c44..bf864b0e4 100644 --- a/grub-core/osdep/freebsd/hostdisk.c +++ b/grub-core/osdep/freebsd/hostdisk.c @@ -48,11 +48,6 @@ # include # include -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; +} diff --git a/grub-core/osdep/hurd/hostdisk.c b/grub-core/osdep/hurd/hostdisk.c index 36ce09a0c..57560e1e2 100644 --- a/grub-core/osdep/hurd/hostdisk.c +++ b/grub-core/osdep/hurd/hostdisk.c @@ -47,11 +47,6 @@ #include #include -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) diff --git a/grub-core/osdep/sun/hostdisk.c b/grub-core/osdep/sun/hostdisk.c index fa691a0c9..dab96dcff 100644 --- a/grub-core/osdep/sun/hostdisk.c +++ b/grub-core/osdep/sun/hostdisk.c @@ -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))) { diff --git a/grub-core/osdep/unix/hostdisk.c b/grub-core/osdep/unix/hostdisk.c index 52572498b..bb0e49f9c 100644 --- a/grub-core/osdep/unix/hostdisk.c +++ b/grub-core/osdep/unix/hostdisk.c @@ -53,9 +53,6 @@ /* Maybe libc doesn't have large file support. */ # include /* _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) diff --git a/grub-core/osdep/windows/hostdisk.c b/grub-core/osdep/windows/hostdisk.c index 10ae6fd53..5f0fc5425 100644 --- a/grub-core/osdep/windows/hostdisk.c +++ b/grub-core/osdep/windows/hostdisk.c @@ -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))) { diff --git a/include/grub/emu/hostdisk.h b/include/grub/emu/hostdisk.h index 28038e6d2..3c621a843 100644 --- a/include/grub/emu/hostdisk.h +++ b/include/grub/emu/hostdisk.h @@ -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);