diff --git a/ChangeLog b/ChangeLog index 20cfe740a..ec4e1d67f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2013-09-24 Vladimir Serbinenko + + * grub-core/kern/emu/hostdisk.c (grub_util_biosdisk_open): Use + grub_util_fd_strerror when using grub_util_fd_*. + (grub_util_fd_open_device): Likewise. + (grub_util_biosdisk_read): Likewise. + (grub_util_biosdisk_write): Likewise. + * grub-core/kern/emu/hostdisk_unix.c (grub_util_fd_open): New function. + (grub_util_fd_strerror): Likewise. + (grub_util_fd_sync): Likewise. + (grub_util_fd_close): Likewise. + * grub-core/kern/emu/hostdisk_windows.c (grub_util_fd_sync): Likewise. + (grub_util_fd_close): Likewise. + (grub_util_fd_strerror): Likewise. + * include/grub/emu/hostdisk.h (grub_util_fd_close): Make into real + function proto rather than macro. + (grub_util_fd_sync): Likewise. + (grub_util_fd_open): Likewise. + (grub_util_fd_strerror): New proto. + 2013-09-24 Vladimir Serbinenko * util/getroot.c (grub_util_biosdisk_is_present): Don't do stat on diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index 60e9344d1..c1bd25619 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -155,7 +155,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) if (!GRUB_UTIL_FD_IS_VALID(fd)) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"), - map[drive].device, strerror (errno)); + map[drive].device, grub_util_fd_strerror ()); disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device, &disk->log_sector_size); @@ -308,7 +308,7 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f if (!GRUB_UTIL_FD_IS_VALID(data->fd)) { grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"), - map[disk->id].device, strerror (errno)); + map[disk->id].device, grub_util_fd_strerror ()); return GRUB_UTIL_FD_INVALID; } @@ -353,7 +353,7 @@ grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector, if (grub_util_fd_read (fd, buf, max << disk->log_sector_size) != (ssize_t) (max << disk->log_sector_size)) return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"), - map[disk->id].device, strerror (errno)); + map[disk->id].device, grub_util_fd_strerror ()); size -= max; buf += (max << disk->log_sector_size); sector += max; @@ -388,7 +388,7 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector, if (grub_util_fd_write (fd, buf, max << disk->log_sector_size) != (ssize_t) (max << disk->log_sector_size)) return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to `%s': %s"), - map[disk->id].device, strerror (errno)); + map[disk->id].device, grub_util_fd_strerror ()); size -= max; buf += (max << disk->log_sector_size); } diff --git a/grub-core/kern/emu/hostdisk_unix.c b/grub-core/kern/emu/hostdisk_unix.c index fcadbe355..e02256e82 100644 --- a/grub-core/kern/emu/hostdisk_unix.c +++ b/grub-core/kern/emu/hostdisk_unix.c @@ -166,4 +166,28 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len) return size; } +grub_util_fd_t +grub_util_fd_open (const char *os_dev, int flags) +{ + return open (os_dev, flags); +} + +const char * +grub_util_fd_strerror (void) +{ + return strerror (errno); +} + +void +grub_util_fd_sync (grub_util_fd_t fd) +{ + fsync (fd); +} + +void +grub_util_fd_close (grub_util_fd_t fd) +{ + close (fd); +} + #endif diff --git a/grub-core/kern/emu/hostdisk_windows.c b/grub-core/kern/emu/hostdisk_windows.c index a197c2d08..39f944936 100644 --- a/grub-core/kern/emu/hostdisk_windows.c +++ b/grub-core/kern/emu/hostdisk_windows.c @@ -204,3 +204,38 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len) grub_util_info ("successful write"); return real_read; } + +void +grub_util_fd_sync (grub_util_fd_t fd) +{ + FlushFileBuffers (fd); +} + +void +grub_util_fd_close (grub_util_fd_t fd) +{ + CloseHandle (fd); +} + +const char * +grub_util_fd_strerror (void) +{ + DWORD err; + static TCHAR tbuf[1024]; + err = GetLastError (); + FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, err, + MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), + tbuf, ARRAY_SIZE (tbuf), NULL); + +#if SIZEOF_TCHAR == 1 + return (char *) tbuf; +#elif SIZEOF_TCHAR == 2 + static grub_uint8_t buf[ARRAY_SIZE (tbuf) * GRUB_MAX_UTF8_PER_UTF16 + 1]; + *grub_utf16_to_utf8 (buf, tbuf, ARRAY_SIZE (tbuf)) = '\0'; + return (char *) buf; +#else +#error "Unsupported TCHAR size" +#endif +} diff --git a/include/grub/emu/hostdisk.h b/include/grub/emu/hostdisk.h index ef27c6d7a..ab438667a 100644 --- a/include/grub/emu/hostdisk.h +++ b/include/grub/emu/hostdisk.h @@ -29,19 +29,21 @@ typedef HANDLE grub_util_fd_t; #define GRUB_UTIL_FD_INVALID INVALID_HANDLE_VALUE #define GRUB_UTIL_FD_IS_VALID(x) ((x) != GRUB_UTIL_FD_INVALID) -#define grub_util_fd_close(x) CloseHandle(x) -#define grub_util_fd_sync(x) FlushFileBuffers(x) -grub_util_fd_t -grub_util_fd_open (const char *os_dev, int flags); #else typedef int grub_util_fd_t; #define GRUB_UTIL_FD_INVALID -1 #define GRUB_UTIL_FD_IS_VALID(x) ((x) >= 0) -#define grub_util_fd_close(x) close(x) -#define grub_util_fd_sync(x) fsync(x) -#define grub_util_fd_open(x,y) open(x,y) #endif +grub_util_fd_t +grub_util_fd_open (const char *os_dev, int flags); +const char * +grub_util_fd_strerror (void); +void +grub_util_fd_sync (grub_util_fd_t fd); +void +grub_util_fd_close (grub_util_fd_t fd); + grub_util_fd_t grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags, grub_disk_addr_t *max);