From 984cfd8a793da4a20d4a84d9923f83ae320da7b1 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Thu, 22 Aug 2013 16:50:12 +0200 Subject: [PATCH] * grub-core/kern/emu/hostdisk.c (grub_util_get_fd_size): Adapt for mingw32 as well based on grub_util_get_disk_size. * util/misc.c (grub_util_get_disk_size): Removed. all users switched to grub_util_get_fd_size. (sync): Removed. (fsync): Moved to ... * grub-core/kern/emu/misc.c (fsync): ... here. --- ChangeLog | 10 ++++ grub-core/kern/emu/hostdisk.c | 99 ++++++++++++++++++++++++++++------- grub-core/kern/emu/hostfs.c | 4 -- grub-core/kern/emu/misc.c | 17 +++--- include/grub/emu/hostdisk.h | 2 - include/grub/emu/misc.h | 6 +++ include/grub/util/misc.h | 5 -- util/misc.c | 57 -------------------- 8 files changed, 106 insertions(+), 94 deletions(-) diff --git a/ChangeLog b/ChangeLog index 77a4e423b..f74307eb5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2013-08-22 Vladimir Serbinenko + + * grub-core/kern/emu/hostdisk.c (grub_util_get_fd_size): Adapt for + mingw32 as well based on grub_util_get_disk_size. + * util/misc.c (grub_util_get_disk_size): Removed. all users switched to + grub_util_get_fd_size. + (sync): Removed. + (fsync): Moved to ... + * grub-core/kern/emu/misc.c (fsync): ... here. + 2013-08-22 Vladimir Serbinenko * include/grub/mm.h (grub_extend_alloc): Remove. diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index 525ae9f21..81d37262b 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -43,6 +43,12 @@ #include #include +#ifdef __MINGW32__ +#include +#include +#include "dirname.h" +#endif + #ifdef __linux__ # include /* ioctl */ # include @@ -245,6 +251,73 @@ grub_util_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data, return 0; } + +#ifdef __MINGW32__ + +grub_uint64_t +grub_util_get_fd_size (int fd __attribute__ ((unused)), const char *name_in, + unsigned *log_secsize) +{ + HANDLE hd; + grub_int64_t size = -1LL; + int log_sector_size = 9; + char *name = xstrdup (name_in); + + if (log_secsize) + *log_secsize = log_sector_size; + + strip_trailing_slashes(name); + hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, + 0, OPEN_EXISTING, 0, 0); + + if (hd == INVALID_HANDLE_VALUE) + { + free (name); + return size; + } + + if (((name[0] == '/') || (name[0] == '\\')) && + ((name[1] == '/') || (name[1] == '\\')) && + (name[2] == '.') && + ((name[3] == '/') || (name[3] == '\\')) && + (! strncasecmp (name + 4, "PHYSICALDRIVE", 13))) + { + DWORD nr; + DISK_GEOMETRY g; + + if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY, + 0, 0, &g, sizeof (g), &nr, 0)) + goto fail; + + size = g.Cylinders.QuadPart; + size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector; + + for (log_sector_size = 0; + (1 << log_sector_size) < g.BytesPerSector; + log_sector_size++); + } + else + { + ULARGE_INTEGER s; + + s.LowPart = GetFileSize (hd, &s.HighPart); + size = s.QuadPart; + } + +fail: + + if (log_secsize) + *log_secsize = log_sector_size; + + free (name); + + CloseHandle (hd); + + return size; +} + +#endif + #if !defined(__MINGW32__) grub_uint64_t grub_util_get_fd_size (int fd, const char *name, unsigned *log_secsize) @@ -366,34 +439,24 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) data->device_map = map[drive].device_map; /* Get the size. */ -#if defined(__MINGW32__) - { - grub_uint64_t size; - - size = grub_util_get_disk_size (map[drive].device); - - if (size % 512) - grub_util_error (_("unaligned device size")); - - disk->total_sectors = size >> 9; - - grub_util_info ("the size of %s is %llu", name, disk->total_sectors); - - return GRUB_ERR_NONE; - } -#else { int fd; +#if defined(__MINGW32__) + fd = -1; +#else fd = open (map[drive].device, O_RDONLY); if (fd == -1) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"), map[drive].device, strerror (errno)); +#endif disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device, &disk->log_sector_size); disk->total_sectors >>= disk->log_sector_size; +#if !defined(__MINGW32__) + # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__) if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode)) # else @@ -402,13 +465,13 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) data->is_disk = 1; close (fd); +#endif grub_util_info ("the size of %s is %" PRIuGRUB_UINT64_T, name, disk->total_sectors); return GRUB_ERR_NONE; } -#endif } #ifdef HAVE_DEVICE_MAPPER @@ -1359,7 +1422,7 @@ read_device_map (const char *dev_map) #ifdef __MINGW32__ (void) st; - if (grub_util_get_disk_size (p) == -1LL) + if (grub_util_get_fd_size (-1, p, NULL) == -1LL) #else if (stat (p, &st) == -1) #endif diff --git a/grub-core/kern/emu/hostfs.c b/grub-core/kern/emu/hostfs.c index 0bb323281..727f7bece 100644 --- a/grub-core/kern/emu/hostfs.c +++ b/grub-core/kern/emu/hostfs.c @@ -132,11 +132,7 @@ grub_hostfs_open (struct grub_file *file, const char *name) file->data = data; -#ifdef __MINGW32__ - file->size = grub_util_get_disk_size (name); -#else file->size = grub_util_get_fd_size (fileno (f), name, NULL); -#endif return GRUB_ERR_NONE; } diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c index c1668bad5..1b2dbf4fe 100644 --- a/grub-core/kern/emu/misc.c +++ b/grub-core/kern/emu/misc.c @@ -42,10 +42,6 @@ #include #include -#ifdef HAVE_DEVICE_MAPPER -# include -#endif - #ifdef HAVE_SYS_PARAM_H # include #endif @@ -59,10 +55,6 @@ # include #endif -#ifdef HAVE_SYS_MKDEV_H -# include /* makedev */ -#endif - int verbosity; void @@ -189,6 +181,15 @@ grub_get_rtc (void) * GRUB_TICKS_PER_SECOND / 1000000)); } +#ifdef __MINGW32__ + +int fsync (int fno __attribute__ ((unused))) +{ + return 0; +} + +#endif + char * canonicalize_file_name (const char *path) { diff --git a/include/grub/emu/hostdisk.h b/include/grub/emu/hostdisk.h index 058973b61..a4e7d1434 100644 --- a/include/grub/emu/hostdisk.h +++ b/include/grub/emu/hostdisk.h @@ -57,10 +57,8 @@ grub_hostdisk_find_partition_start (const char *dev); const char * grub_hostdisk_os_dev_to_grub_drive (const char *os_dev, int add); -#if !defined(__MINGW32__) grub_uint64_t grub_util_get_fd_size (int fd, const char *name, unsigned *log_secsize); -#endif char * grub_util_get_os_disk (const char *os_dev); diff --git a/include/grub/emu/misc.h b/include/grub/emu/misc.h index 92669f09a..a31bc0f01 100644 --- a/include/grub/emu/misc.h +++ b/include/grub/emu/misc.h @@ -66,6 +66,12 @@ void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((format extern char * canonicalize_file_name (const char *path); +#ifdef __MINGW32__ + +int fsync (int fno); + +#endif + #ifdef HAVE_DEVICE_MAPPER int grub_device_mapper_supported (void); #endif diff --git a/include/grub/util/misc.h b/include/grub/util/misc.h index 073493eed..598e71243 100644 --- a/include/grub/util/misc.h +++ b/include/grub/util/misc.h @@ -44,11 +44,6 @@ void grub_util_write_image_at (const void *img, size_t size, off_t offset, #define fseeko fseeko64 #define ftello ftello64 -void sync (void); -int fsync (int fno); - -grub_int64_t grub_util_get_disk_size (char *name); - #endif diff --git a/util/misc.c b/util/misc.c index 93fa0b4bd..74080c4a2 100644 --- a/util/misc.c +++ b/util/misc.c @@ -281,63 +281,6 @@ grub_millisleep (grub_uint32_t ms) #endif -#ifdef __MINGW32__ - -void sync (void) -{ -} - -int fsync (int fno __attribute__ ((unused))) -{ - return 0; -} - -grub_int64_t -grub_util_get_disk_size (char *name) -{ - HANDLE hd; - grub_int64_t size = -1LL; - - strip_trailing_slashes(name); - hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, - 0, OPEN_EXISTING, 0, 0); - - if (hd == INVALID_HANDLE_VALUE) - return size; - - if (((name[0] == '/') || (name[0] == '\\')) && - ((name[1] == '/') || (name[1] == '\\')) && - (name[2] == '.') && - ((name[3] == '/') || (name[3] == '\\')) && - (! strncasecmp (name + 4, "PHYSICALDRIVE", 13))) - { - DWORD nr; - DISK_GEOMETRY g; - - if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY, - 0, 0, &g, sizeof (g), &nr, 0)) - goto fail; - - size = g.Cylinders.QuadPart; - size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector; - } - else - { - LARGE_INTEGER s; - - s.LowPart = GetFileSize (hd, &s.HighPart); - size = s.QuadPart; - } - -fail: - - CloseHandle (hd); - - return size; -} - -#endif /* __MINGW32__ */ - #ifdef GRUB_UTIL void grub_util_init_nls (void)