Use Winapi on both cygwin and mingw32 to share more code between both.
This commit is contained in:
parent
d68d01573b
commit
7e518ca84f
30 changed files with 1984 additions and 1670 deletions
|
@ -46,11 +46,6 @@
|
|||
#ifdef __linux__
|
||||
# include <sys/ioctl.h> /* ioctl */
|
||||
# include <sys/mount.h>
|
||||
# if !defined(__GLIBC__) || \
|
||||
((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1)))
|
||||
/* 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 */
|
||||
|
@ -68,15 +63,6 @@ static struct
|
|||
int device_map;
|
||||
} map[256];
|
||||
|
||||
struct grub_util_biosdisk_data
|
||||
{
|
||||
char *dev;
|
||||
int access_mode;
|
||||
int fd;
|
||||
int is_disk;
|
||||
int device_map;
|
||||
};
|
||||
|
||||
static int
|
||||
unescape_cmp (const char *a, const char *b_escaped)
|
||||
{
|
||||
|
@ -141,81 +127,42 @@ 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, const char *name,
|
||||
unsigned *log_secsize)
|
||||
{
|
||||
return grub_util_get_fd_size_os (fd, name, log_secsize);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
grub_uint64_t
|
||||
grub_util_get_fd_size (int fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
struct stat st;
|
||||
grub_int64_t ret = -1;
|
||||
|
||||
if (fstat (fd, &st) < 0)
|
||||
/* TRANSLATORS: "stat" comes from the name of POSIX function. */
|
||||
grub_util_error (_("cannot stat `%s': %s"), name, strerror (errno));
|
||||
#if GRUB_DISK_DEVS_ARE_CHAR
|
||||
if (S_ISCHR (st.st_mode))
|
||||
#else
|
||||
if (S_ISBLK (st.st_mode))
|
||||
#endif
|
||||
ret = grub_util_get_fd_size_os (fd, name, log_secsize);
|
||||
if (ret != -1LL)
|
||||
return ret;
|
||||
|
||||
if (log_secsize)
|
||||
*log_secsize = 9;
|
||||
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static grub_err_t
|
||||
grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
||||
{
|
||||
int drive;
|
||||
struct stat st;
|
||||
struct grub_util_biosdisk_data *data;
|
||||
struct grub_util_hostdisk_data *data;
|
||||
|
||||
drive = find_grub_drive (name);
|
||||
grub_util_info ("drive = %d", drive);
|
||||
if (drive < 0)
|
||||
return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
|
||||
"no mapping exists for `%s'", name);
|
||||
|
||||
disk->id = drive;
|
||||
disk->data = data = xmalloc (sizeof (struct grub_util_biosdisk_data));
|
||||
disk->data = data = xmalloc (sizeof (struct grub_util_hostdisk_data));
|
||||
data->dev = NULL;
|
||||
data->access_mode = 0;
|
||||
data->fd = -1;
|
||||
data->fd = GRUB_UTIL_FD_INVALID;
|
||||
data->is_disk = 0;
|
||||
data->device_map = map[drive].device_map;
|
||||
|
||||
/* Get the size. */
|
||||
{
|
||||
int fd;
|
||||
grub_util_fd_t fd;
|
||||
|
||||
#if defined(__MINGW32__)
|
||||
fd = -1;
|
||||
#else
|
||||
fd = open (map[drive].device, O_RDONLY);
|
||||
if (fd == -1)
|
||||
fd = grub_util_fd_open (map[drive].device, O_RDONLY);
|
||||
|
||||
if (!GRUB_UTIL_FD_IS_VALID(fd))
|
||||
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(__MINGW32__) && !defined(__CYGWIN__)
|
||||
|
||||
# if GRUB_DISK_DEVS_ARE_CHAR
|
||||
if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
|
||||
|
@ -223,10 +170,10 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
|||
if (fstat (fd, &st) < 0 || ! S_ISBLK (st.st_mode))
|
||||
# endif
|
||||
data->is_disk = 1;
|
||||
|
||||
close (fd);
|
||||
#endif
|
||||
|
||||
grub_util_fd_close (fd);
|
||||
|
||||
grub_util_info ("the size of %s is %" PRIuGRUB_UINT64_T,
|
||||
name, disk->total_sectors);
|
||||
|
||||
|
@ -234,37 +181,6 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
|||
}
|
||||
}
|
||||
|
||||
#if defined(__linux__) && (!defined(__GLIBC__) || \
|
||||
((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1))))
|
||||
/* Maybe libc doesn't have large file support. */
|
||||
grub_err_t
|
||||
grub_util_fd_seek (int fd, const char *name, grub_uint64_t off)
|
||||
{
|
||||
loff_t offset, result;
|
||||
static int _llseek (uint filedes, ulong hi, ulong lo,
|
||||
loff_t *res, uint wh);
|
||||
_syscall5 (int, _llseek, uint, filedes, ulong, hi, ulong, lo,
|
||||
loff_t *, res, uint, wh);
|
||||
|
||||
offset = (loff_t) off;
|
||||
if (_llseek (fd, offset >> 32, offset & 0xffffffff, &result, SEEK_SET))
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
||||
name, strerror (errno));
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
#else
|
||||
grub_err_t
|
||||
grub_util_fd_seek (int fd, const char *name, grub_uint64_t off)
|
||||
{
|
||||
off_t offset = (off_t) off;
|
||||
|
||||
if (lseek (fd, offset, SEEK_SET) != offset)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
||||
name, strerror (errno));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
const char *
|
||||
grub_hostdisk_os_dev_to_grub_drive (const char *os_disk, int add)
|
||||
{
|
||||
|
@ -305,12 +221,13 @@ grub_hostdisk_os_dev_to_grub_drive (const char *os_disk, int add)
|
|||
return map[i].drive;
|
||||
}
|
||||
|
||||
static int
|
||||
open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
||||
grub_disk_addr_t *max)
|
||||
#ifndef __linux__
|
||||
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)
|
||||
{
|
||||
int fd;
|
||||
struct grub_util_biosdisk_data *data = disk->data;
|
||||
grub_util_fd_t fd;
|
||||
struct grub_util_hostdisk_data *data = disk->data;
|
||||
|
||||
*max = ~0ULL;
|
||||
|
||||
|
@ -327,87 +244,6 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
|||
flags |= O_BINARY;
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
/* Linux has a bug that the disk cache for a whole disk is not consistent
|
||||
with the one for a partition of the disk. */
|
||||
{
|
||||
int is_partition = 0;
|
||||
char dev[PATH_MAX];
|
||||
grub_disk_addr_t part_start = 0;
|
||||
|
||||
part_start = grub_partition_get_start (disk->partition);
|
||||
|
||||
strcpy (dev, map[disk->id].device);
|
||||
if (disk->partition
|
||||
&& strncmp (map[disk->id].device, "/dev/", 5) == 0)
|
||||
{
|
||||
if (sector >= part_start)
|
||||
is_partition = grub_hostdisk_linux_find_partition (dev, part_start);
|
||||
else
|
||||
*max = part_start - sector;
|
||||
}
|
||||
|
||||
reopen:
|
||||
|
||||
if (data->dev && strcmp (data->dev, dev) == 0 &&
|
||||
data->access_mode == (flags & O_ACCMODE))
|
||||
{
|
||||
grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
|
||||
fd = data->fd;
|
||||
}
|
||||
else
|
||||
{
|
||||
free (data->dev);
|
||||
data->dev = 0;
|
||||
if (data->fd != -1)
|
||||
{
|
||||
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
||||
{
|
||||
fsync (data->fd);
|
||||
if (data->is_disk)
|
||||
ioctl (data->fd, BLKFLSBUF, 0);
|
||||
}
|
||||
|
||||
close (data->fd);
|
||||
data->fd = -1;
|
||||
}
|
||||
|
||||
/* Open the partition. */
|
||||
grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
|
||||
fd = open (dev, flags);
|
||||
if (fd < 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
||||
dev, strerror (errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
data->dev = xstrdup (dev);
|
||||
data->access_mode = (flags & O_ACCMODE);
|
||||
data->fd = fd;
|
||||
|
||||
if (data->is_disk)
|
||||
ioctl (data->fd, BLKFLSBUF, 0);
|
||||
}
|
||||
|
||||
if (is_partition)
|
||||
{
|
||||
*max = grub_util_get_fd_size (fd, dev, 0);
|
||||
*max >>= disk->log_sector_size;
|
||||
if (sector - part_start >= *max)
|
||||
{
|
||||
*max = disk->partition->len - (sector - part_start);
|
||||
if (*max == 0)
|
||||
*max = ~0ULL;
|
||||
is_partition = 0;
|
||||
strcpy (dev, map[disk->id].device);
|
||||
goto reopen;
|
||||
}
|
||||
sector -= part_start;
|
||||
*max -= sector;
|
||||
}
|
||||
}
|
||||
#else /* ! __linux__ */
|
||||
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
int sysctl_flags, sysctl_oldflags;
|
||||
size_t sysctl_size = sizeof (sysctl_flags);
|
||||
|
@ -415,14 +251,14 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int 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 -1;
|
||||
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 -1;
|
||||
return GRUB_UTIL_FD_INVALID;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -436,18 +272,16 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
|||
{
|
||||
free (data->dev);
|
||||
data->dev = 0;
|
||||
if (data->fd != -1)
|
||||
if (GRUB_UTIL_FD_IS_VALID(data->fd))
|
||||
{
|
||||
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
||||
{
|
||||
fsync (data->fd);
|
||||
}
|
||||
close (data->fd);
|
||||
data->fd = -1;
|
||||
grub_util_fd_sync (data->fd);
|
||||
grub_util_fd_close (data->fd);
|
||||
data->fd = GRUB_UTIL_FD_INVALID;
|
||||
}
|
||||
|
||||
fd = open (map[disk->id].device, flags);
|
||||
if (fd >= 0)
|
||||
fd = grub_util_fd_open (map[disk->id].device, flags);
|
||||
if (GRUB_UTIL_FD_IS_VALID(fd))
|
||||
{
|
||||
data->dev = xstrdup (map[disk->id].device);
|
||||
data->access_mode = (flags & O_ACCMODE);
|
||||
|
@ -460,7 +294,7 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
|||
&& 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 -1;
|
||||
return GRUB_UTIL_FD_INVALID;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -470,77 +304,26 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
|||
fd = open(map[disk->id].device, flags | O_SHLOCK);
|
||||
#endif
|
||||
|
||||
if (fd < 0)
|
||||
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));
|
||||
return -1;
|
||||
return GRUB_UTIL_FD_INVALID;
|
||||
}
|
||||
#endif /* ! __linux__ */
|
||||
|
||||
grub_hostdisk_configure_device_driver (fd);
|
||||
|
||||
if (grub_util_fd_seek (fd, map[disk->id].device,
|
||||
sector << disk->log_sector_size))
|
||||
{
|
||||
close (fd);
|
||||
return -1;
|
||||
grub_util_fd_close (fd);
|
||||
return GRUB_UTIL_FD_INVALID;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Read LEN bytes from FD in BUF. Return less than or equal to zero if an
|
||||
error occurs, otherwise return LEN. */
|
||||
ssize_t
|
||||
grub_util_fd_read (int fd, char *buf, size_t len)
|
||||
{
|
||||
ssize_t size = len;
|
||||
|
||||
while (len)
|
||||
{
|
||||
ssize_t ret = read (fd, buf, len);
|
||||
|
||||
if (ret <= 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
len -= ret;
|
||||
buf += ret;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/* Write LEN bytes from BUF to FD. Return less than or equal to zero if an
|
||||
error occurs, otherwise return LEN. */
|
||||
ssize_t
|
||||
grub_util_fd_write (int fd, const char *buf, size_t len)
|
||||
{
|
||||
ssize_t size = len;
|
||||
|
||||
while (len)
|
||||
{
|
||||
ssize_t ret = write (fd, buf, len);
|
||||
|
||||
if (ret <= 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
len -= ret;
|
||||
buf += ret;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
|
||||
|
@ -548,10 +331,10 @@ grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
|
|||
{
|
||||
while (size)
|
||||
{
|
||||
int fd;
|
||||
grub_util_fd_t fd;
|
||||
grub_disk_addr_t max = ~0ULL;
|
||||
fd = open_device (disk, sector, O_RDONLY, &max);
|
||||
if (fd < 0)
|
||||
fd = grub_util_fd_open_device (disk, sector, O_RDONLY, &max);
|
||||
if (!GRUB_UTIL_FD_IS_VALID (fd))
|
||||
return grub_errno;
|
||||
|
||||
#ifdef __linux__
|
||||
|
@ -583,10 +366,10 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
|
|||
{
|
||||
while (size)
|
||||
{
|
||||
int fd;
|
||||
grub_util_fd_t fd;
|
||||
grub_disk_addr_t max = ~0ULL;
|
||||
fd = open_device (disk, sector, O_WRONLY, &max);
|
||||
if (fd < 0)
|
||||
fd = grub_util_fd_open_device (disk, sector, O_WRONLY, &max);
|
||||
if (!GRUB_UTIL_FD_IS_VALID (fd))
|
||||
return grub_errno;
|
||||
|
||||
#ifdef __linux__
|
||||
|
@ -614,18 +397,18 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
|
|||
grub_err_t
|
||||
grub_util_biosdisk_flush (struct grub_disk *disk)
|
||||
{
|
||||
struct grub_util_biosdisk_data *data = disk->data;
|
||||
struct grub_util_hostdisk_data *data = disk->data;
|
||||
|
||||
if (disk->dev->id != GRUB_DISK_DEVICE_BIOSDISK_ID)
|
||||
return GRUB_ERR_NONE;
|
||||
if (data->fd == -1)
|
||||
if (!GRUB_UTIL_FD_IS_VALID (data->fd))
|
||||
{
|
||||
grub_disk_addr_t max;
|
||||
data->fd = open_device (disk, 0, O_RDONLY, &max);
|
||||
if (data->fd < 0)
|
||||
data->fd = grub_util_fd_open_device (disk, 0, O_RDONLY, &max);
|
||||
if (!GRUB_UTIL_FD_IS_VALID (data->fd))
|
||||
return grub_errno;
|
||||
}
|
||||
fsync (data->fd);
|
||||
grub_util_fd_sync (data->fd);
|
||||
#ifdef __linux__
|
||||
if (data->is_disk)
|
||||
ioctl (data->fd, BLKFLSBUF, 0);
|
||||
|
@ -636,14 +419,14 @@ grub_util_biosdisk_flush (struct grub_disk *disk)
|
|||
static void
|
||||
grub_util_biosdisk_close (struct grub_disk *disk)
|
||||
{
|
||||
struct grub_util_biosdisk_data *data = disk->data;
|
||||
struct grub_util_hostdisk_data *data = disk->data;
|
||||
|
||||
free (data->dev);
|
||||
if (data->fd != -1)
|
||||
if (!GRUB_UTIL_FD_IS_VALID (data->fd))
|
||||
{
|
||||
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
||||
grub_util_biosdisk_flush (disk);
|
||||
close (data->fd);
|
||||
grub_util_fd_close (data->fd);
|
||||
}
|
||||
free (data);
|
||||
}
|
||||
|
@ -660,13 +443,31 @@ static struct grub_disk_dev grub_util_biosdisk_dev =
|
|||
.next = 0
|
||||
};
|
||||
|
||||
static int
|
||||
grub_util_check_file_presence (const char *p)
|
||||
{
|
||||
#if defined (__MINGW32__) || defined(__CYGWIN__)
|
||||
HANDLE h;
|
||||
h = grub_util_fd_open (p, O_RDONLY);
|
||||
if (!GRUB_UTIL_FD_IS_VALID(h))
|
||||
return 0;
|
||||
CloseHandle (h);
|
||||
return 1;
|
||||
#else
|
||||
struct stat st;
|
||||
|
||||
if (stat (p, &st) == -1)
|
||||
return 0;
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
read_device_map (const char *dev_map)
|
||||
{
|
||||
FILE *fp;
|
||||
char buf[1024]; /* XXX */
|
||||
int lineno = 0;
|
||||
struct stat st;
|
||||
|
||||
if (dev_map[0] == '\0')
|
||||
{
|
||||
|
@ -770,12 +571,7 @@ read_device_map (const char *dev_map)
|
|||
e++;
|
||||
*e = '\0';
|
||||
|
||||
#ifdef __MINGW32__
|
||||
(void) st;
|
||||
if (grub_util_get_fd_size (-1, p, NULL) == -1LL)
|
||||
#else
|
||||
if (stat (p, &st) == -1)
|
||||
#endif
|
||||
if (!grub_util_check_file_presence (p))
|
||||
{
|
||||
free (map[drive].drive);
|
||||
map[drive].drive = NULL;
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
# include <sys/disk.h>
|
||||
|
||||
grub_uint64_t
|
||||
grub_util_get_fd_size (int fd, const char *name, unsigned *log_secsize)
|
||||
grub_util_get_fd_size (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
unsigned long long nr;
|
||||
unsigned sector_size, log_sector_size;
|
||||
|
@ -69,6 +69,6 @@ grub_util_get_fd_size (int fd, const char *name, unsigned *log_secsize)
|
|||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
#include <limits.h>
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd __attribute__ ((unused)),
|
||||
grub_util_get_fd_size_os (grub_util_fd_t fd __attribute__ ((unused)),
|
||||
const char *name __attribute__ ((unused)),
|
||||
unsigned *log_secsize __attribute__ ((unused)))
|
||||
{
|
||||
|
@ -53,7 +53,7 @@ grub_util_get_fd_size_os (int fd __attribute__ ((unused)),
|
|||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
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 (int fd)
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
|
@ -80,13 +80,13 @@ grub_hostdisk_configure_device_driver (int fd)
|
|||
}
|
||||
#else
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
||||
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
struct disklabel label;
|
||||
unsigned sector_size, log_sector_size;
|
||||
|
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/msdos_partition.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/list.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
# include <sys/ioctl.h>
|
||||
# include <cygwin/fs.h> /* BLKGETSIZE64 */
|
||||
# include <cygwin/hdreg.h> /* HDIO_GETGEO */
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
unsigned long long nr;
|
||||
unsigned sector_size, log_sector_size;
|
||||
|
||||
if (ioctl (fd, BLKGETSIZE64, &nr))
|
||||
return -1;
|
||||
|
||||
if (ioctl (fd, BLKSSZGET, §or_size))
|
||||
return -1;
|
||||
|
||||
if (sector_size & (sector_size - 1) || !sector_size)
|
||||
return -1;
|
||||
for (log_sector_size = 0;
|
||||
(1 << log_sector_size) < sector_size;
|
||||
log_sector_size++);
|
||||
|
||||
if (log_secsize)
|
||||
*log_secsize = log_sector_size;
|
||||
|
||||
if (nr & ((1 << log_sector_size) - 1))
|
||||
grub_util_error ("%s", _("unaligned device size"));
|
||||
|
||||
return nr;
|
||||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
|
@ -49,12 +49,12 @@
|
|||
# include <libgeom.h>
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
||||
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
unsigned long long nr;
|
||||
unsigned sector_size, log_sector_size;
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
#include <sys/mman.h>
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ grub_util_hurd_get_disk_info (const char *dev, grub_uint32_t *secsize, grub_disk
|
|||
}
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
||||
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
grub_uint32_t sector_size;
|
||||
grub_disk_addr_t size;
|
||||
|
|
|
@ -67,7 +67,7 @@ struct hd_geometry
|
|||
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
||||
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
unsigned long long nr;
|
||||
unsigned sector_size, log_sector_size;
|
||||
|
@ -96,7 +96,7 @@ grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
|||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev)
|
||||
{
|
||||
int fd;
|
||||
grub_util_fd_t fd;
|
||||
struct hd_geometry hdg;
|
||||
|
||||
fd = open (dev, O_RDONLY);
|
||||
|
@ -195,7 +195,7 @@ grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
|
|||
|
||||
for (i = 1; i < 10000; i++)
|
||||
{
|
||||
int fd;
|
||||
grub_util_fd_t fd;
|
||||
grub_disk_addr_t start;
|
||||
|
||||
sprintf (p, format, i);
|
||||
|
@ -239,7 +239,7 @@ grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
|
|||
void
|
||||
grub_hostdisk_flush_initial_buffer (const char *os_dev)
|
||||
{
|
||||
int fd;
|
||||
grub_util_fd_t fd;
|
||||
struct stat st;
|
||||
|
||||
fd = open (os_dev, O_RDONLY);
|
||||
|
@ -249,7 +249,114 @@ grub_hostdisk_flush_initial_buffer (const char *os_dev)
|
|||
close (fd);
|
||||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
int
|
||||
grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
||||
grub_disk_addr_t *max)
|
||||
{
|
||||
grub_util_fd_t fd;
|
||||
struct grub_util_hostdisk_data *data = disk->data;
|
||||
|
||||
*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
|
||||
|
||||
/* Linux has a bug that the disk cache for a whole disk is not consistent
|
||||
with the one for a partition of the disk. */
|
||||
{
|
||||
int is_partition = 0;
|
||||
char dev[PATH_MAX];
|
||||
grub_disk_addr_t part_start = 0;
|
||||
|
||||
part_start = grub_partition_get_start (disk->partition);
|
||||
|
||||
strcpy (dev, grub_util_biosdisk_get_osdev (disk));
|
||||
if (disk->partition
|
||||
&& strncmp (dev, "/dev/", 5) == 0)
|
||||
{
|
||||
if (sector >= part_start)
|
||||
is_partition = grub_hostdisk_linux_find_partition (dev, part_start);
|
||||
else
|
||||
*max = part_start - sector;
|
||||
}
|
||||
|
||||
reopen:
|
||||
|
||||
if (data->dev && strcmp (data->dev, dev) == 0 &&
|
||||
data->access_mode == (flags & O_ACCMODE))
|
||||
{
|
||||
grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
|
||||
fd = data->fd;
|
||||
}
|
||||
else
|
||||
{
|
||||
free (data->dev);
|
||||
data->dev = 0;
|
||||
if (data->fd != -1)
|
||||
{
|
||||
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
||||
{
|
||||
fsync (data->fd);
|
||||
if (data->is_disk)
|
||||
ioctl (data->fd, BLKFLSBUF, 0);
|
||||
}
|
||||
|
||||
close (data->fd);
|
||||
data->fd = -1;
|
||||
}
|
||||
|
||||
/* Open the partition. */
|
||||
grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
|
||||
fd = open (dev, flags);
|
||||
if (fd < 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
||||
dev, strerror (errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
data->dev = xstrdup (dev);
|
||||
data->access_mode = (flags & O_ACCMODE);
|
||||
data->fd = fd;
|
||||
|
||||
if (data->is_disk)
|
||||
ioctl (data->fd, BLKFLSBUF, 0);
|
||||
}
|
||||
|
||||
if (is_partition)
|
||||
{
|
||||
*max = grub_util_get_fd_size (fd, dev, 0);
|
||||
*max >>= disk->log_sector_size;
|
||||
if (sector - part_start >= *max)
|
||||
{
|
||||
*max = disk->partition->len - (sector - part_start);
|
||||
if (*max == 0)
|
||||
*max = ~0ULL;
|
||||
is_partition = 0;
|
||||
strcpy (dev, grub_util_biosdisk_get_osdev (disk));
|
||||
goto reopen;
|
||||
}
|
||||
sector -= part_start;
|
||||
*max -= sector;
|
||||
}
|
||||
}
|
||||
|
||||
if (grub_util_fd_seek (fd, grub_util_biosdisk_get_osdev (disk),
|
||||
sector << disk->log_sector_size))
|
||||
{
|
||||
close (fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
|
|
@ -1,120 +0,0 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/msdos_partition.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/list.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
#include "dirname.h"
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (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;
|
||||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_flush_initial_buffer (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
}
|
|
@ -10,10 +10,8 @@
|
|||
#include "hostdisk_sun.c"
|
||||
#elif defined(__GNU__)
|
||||
#include "hostdisk_hurd.c"
|
||||
#elif defined(__CYGWIN__)
|
||||
#include "hostdisk_cygwin.c"
|
||||
#elif defined(__MINGW32__)
|
||||
#include "hostdisk_mingw.c"
|
||||
#elif defined(__CYGWIN__) || defined(__MINGW32__)
|
||||
#include "hostdisk_windows.c"
|
||||
#else
|
||||
# warning "No hostdisk OS-specific functions is available for your system. Device detection may not work properly."
|
||||
#include "hostdisk_basic.c"
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
# include <sys/dkio.h>
|
||||
|
||||
grub_int64_t
|
||||
grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
||||
grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
struct dk_minfo minfo;
|
||||
unsigned sector_size, log_sector_size;
|
||||
|
@ -68,7 +68,7 @@ grub_util_get_fd_size_os (int fd, const char *name, unsigned *log_secsize)
|
|||
}
|
||||
|
||||
void
|
||||
grub_hostdisk_configure_device_driver (int fd __attribute__ ((unused)))
|
||||
grub_hostdisk_configure_device_driver (grub_util_fd_t fd __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
169
grub-core/kern/emu/hostdisk_unix.c
Normal file
169
grub-core/kern/emu/hostdisk_unix.c
Normal file
|
@ -0,0 +1,169 @@
|
|||
/* hostdisk.c - emulate biosdisk */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/msdos_partition.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/list.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#if !defined (__CYGWIN__) && !defined (__MINGW32__)
|
||||
|
||||
#ifdef __linux__
|
||||
# include <sys/ioctl.h> /* ioctl */
|
||||
# include <sys/mount.h>
|
||||
# if !defined(__GLIBC__) || \
|
||||
((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1)))
|
||||
/* 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
|
||||
grub_util_get_fd_size (grub_util_fd_t fd, const char *name, unsigned *log_secsize)
|
||||
{
|
||||
struct stat st;
|
||||
grub_int64_t ret = -1;
|
||||
|
||||
if (fstat (fd, &st) < 0)
|
||||
/* TRANSLATORS: "stat" comes from the name of POSIX function. */
|
||||
grub_util_error (_("cannot stat `%s': %s"), name, strerror (errno));
|
||||
#if GRUB_DISK_DEVS_ARE_CHAR
|
||||
if (S_ISCHR (st.st_mode))
|
||||
#else
|
||||
if (S_ISBLK (st.st_mode))
|
||||
#endif
|
||||
ret = grub_util_get_fd_size_os (fd, name, log_secsize);
|
||||
if (ret != -1LL)
|
||||
return ret;
|
||||
|
||||
if (log_secsize)
|
||||
*log_secsize = 9;
|
||||
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
#if defined(__linux__) && (!defined(__GLIBC__) || \
|
||||
((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1))))
|
||||
/* Maybe libc doesn't have large file support. */
|
||||
grub_err_t
|
||||
grub_util_fd_seek (grub_util_fd_t fd, const char *name, grub_uint64_t off)
|
||||
{
|
||||
loff_t offset, result;
|
||||
static int _llseek (uint filedes, ulong hi, ulong lo,
|
||||
loff_t *res, uint wh);
|
||||
_syscall5 (int, _llseek, uint, filedes, ulong, hi, ulong, lo,
|
||||
loff_t *, res, uint, wh);
|
||||
|
||||
offset = (loff_t) off;
|
||||
if (_llseek (fd, offset >> 32, offset & 0xffffffff, &result, SEEK_SET))
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
||||
name, strerror (errno));
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
#else
|
||||
grub_err_t
|
||||
grub_util_fd_seek (grub_util_fd_t fd, const char *name, grub_uint64_t off)
|
||||
{
|
||||
off_t offset = (off_t) off;
|
||||
|
||||
if (lseek (fd, offset, SEEK_SET) != offset)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
||||
name, strerror (errno));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* Read LEN bytes from FD in BUF. Return less than or equal to zero if an
|
||||
error occurs, otherwise return LEN. */
|
||||
ssize_t
|
||||
grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
|
||||
{
|
||||
ssize_t size = len;
|
||||
|
||||
while (len)
|
||||
{
|
||||
ssize_t ret = read (fd, buf, len);
|
||||
|
||||
if (ret <= 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
len -= ret;
|
||||
buf += ret;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/* Write LEN bytes from BUF to FD. Return less than or equal to zero if an
|
||||
error occurs, otherwise return LEN. */
|
||||
ssize_t
|
||||
grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
|
||||
{
|
||||
ssize_t size = len;
|
||||
|
||||
while (len)
|
||||
{
|
||||
ssize_t ret = write (fd, buf, len);
|
||||
|
||||
if (ret <= 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
len -= ret;
|
||||
buf += ret;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
#endif
|
197
grub-core/kern/emu/hostdisk_windows.c
Normal file
197
grub-core/kern/emu/hostdisk_windows.c
Normal file
|
@ -0,0 +1,197 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/msdos_partition.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/list.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <grub/util/windows.h>
|
||||
#include <grub/charset.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
LPTSTR
|
||||
grub_util_get_windows_path (const char *path)
|
||||
{
|
||||
LPTSTR winpath;
|
||||
winpath = xmalloc (sizeof (winpath[0]) * PATH_MAX);
|
||||
if (cygwin_conv_path (sizeof (winpath[0]) == 1 ? CCP_POSIX_TO_WIN_A
|
||||
: CCP_POSIX_TO_WIN_W, path, winpath,
|
||||
sizeof (winpath[0]) * PATH_MAX))
|
||||
grub_util_error ("%s", _("cygwin_conv_path() failed"));
|
||||
return winpath;
|
||||
}
|
||||
#else
|
||||
LPTSTR
|
||||
grub_util_get_windows_path (const char *path)
|
||||
{
|
||||
#if SIZEOF_TCHAR == 1
|
||||
return xstrdup (path);
|
||||
#elif SIZEOF_TCHAR == 2
|
||||
size_t ssz = strlen (path);
|
||||
size_t tsz = 2 * (GRUB_MAX_UTF16_PER_UTF8 * ssz + 1);
|
||||
LPTSTR ret = xmalloc (tsz);
|
||||
tsz = grub_utf8_to_utf16 (ret, tsz, (const grub_uint8_t *) path, ssz, NULL);
|
||||
ret[tsz] = 0;
|
||||
return ret;
|
||||
#else
|
||||
#error SIZEOF_TCHAR
|
||||
#error "Unsupported TCHAR size"
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
grub_uint64_t
|
||||
grub_util_get_fd_size (grub_util_fd_t hd, const char *name_in,
|
||||
unsigned *log_secsize)
|
||||
{
|
||||
grub_int64_t size = -1LL;
|
||||
int log_sector_size = 9;
|
||||
LPTSTR name = grub_util_get_windows_path (name_in);
|
||||
|
||||
if (log_secsize)
|
||||
*log_secsize = log_sector_size;
|
||||
|
||||
if (((name[0] == '/') || (name[0] == '\\')) &&
|
||||
((name[1] == '/') || (name[1] == '\\')) &&
|
||||
((name[2] == '.') || (name[2] == '?')) &&
|
||||
((name[3] == '/') || (name[3] == '\\')))
|
||||
{
|
||||
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);
|
||||
|
||||
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)))
|
||||
{
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_util_fd_seek (grub_util_fd_t fd, const char *name, grub_uint64_t off)
|
||||
{
|
||||
LARGE_INTEGER offset;
|
||||
offset.QuadPart = off;
|
||||
|
||||
if (!SetFilePointerEx (fd, offset, NULL, FILE_BEGIN))
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
||||
name, strerror (errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
grub_util_fd_t
|
||||
grub_util_fd_open (const char *os_dev, int flags)
|
||||
{
|
||||
DWORD flg = 0;
|
||||
LPTSTR dev = grub_util_get_windows_path (os_dev);
|
||||
grub_util_fd_t ret;
|
||||
|
||||
if (flags & O_WRONLY)
|
||||
flg |= GENERIC_WRITE;
|
||||
if (flags & O_RDONLY)
|
||||
flg |= GENERIC_READ;
|
||||
flg = GENERIC_READ;
|
||||
ret = CreateFile (dev, flg, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
0, OPEN_EXISTING, 0, 0);
|
||||
free (dev);
|
||||
grub_util_info ("handle = %p", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
|
||||
{
|
||||
DWORD real_read;
|
||||
if (!ReadFile(fd, buf, len, &real_read, NULL))
|
||||
{
|
||||
grub_util_info ("read err %x", (int) GetLastError ());
|
||||
return -1;
|
||||
}
|
||||
grub_util_info ("successful read");
|
||||
return real_read;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
|
||||
{
|
||||
DWORD real_read;
|
||||
if (!WriteFile(fd, buf, len, &real_read, NULL))
|
||||
{
|
||||
grub_util_info ("write err %x", (int) GetLastError ());
|
||||
return -1;
|
||||
}
|
||||
|
||||
grub_util_info ("successful write");
|
||||
return real_read;
|
||||
}
|
|
@ -132,7 +132,13 @@ grub_hostfs_open (struct grub_file *file, const char *name)
|
|||
|
||||
file->data = data;
|
||||
|
||||
#if defined (__CYGWIN__) || defined (__MINGW32__)
|
||||
fseek (f, 0, SEEK_END);
|
||||
file->size = ftello (f);
|
||||
fseek (f, 0, SEEK_SET);
|
||||
#else
|
||||
file->size = grub_util_get_fd_size (fileno (f), name, NULL);
|
||||
#endif
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
|
|
@ -234,7 +234,7 @@ main (int argc, char *argv[])
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __MINGW32__
|
||||
#if defined (__MINGW32__) || defined (__CYGWIN__)
|
||||
|
||||
void
|
||||
grub_millisleep (grub_uint32_t ms)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue