merge mainline into nestpart
This commit is contained in:
commit
c9ea504d25
170 changed files with 4270 additions and 3366 deletions
128
util/hostdisk.c
128
util/hostdisk.c
|
|
@ -26,6 +26,7 @@
|
|||
#include <grub/util/hostdisk.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/list.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
|
@ -103,6 +104,13 @@ struct
|
|||
char *device;
|
||||
} map[256];
|
||||
|
||||
struct grub_util_biosdisk_data
|
||||
{
|
||||
char *dev;
|
||||
int access_mode;
|
||||
int fd;
|
||||
};
|
||||
|
||||
#ifdef __linux__
|
||||
/* Check if we have devfs support. */
|
||||
static int
|
||||
|
|
@ -137,7 +145,7 @@ find_grub_drive (const char *name)
|
|||
}
|
||||
|
||||
static int
|
||||
find_free_slot ()
|
||||
find_free_slot (void)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
|
|
@ -165,14 +173,19 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
|||
{
|
||||
int drive;
|
||||
struct stat st;
|
||||
struct grub_util_biosdisk_data *data;
|
||||
|
||||
drive = find_grub_drive (name);
|
||||
if (drive < 0)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE,
|
||||
return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
|
||||
"no mapping exists for `%s'", name);
|
||||
|
||||
disk->has_partitions = 1;
|
||||
disk->id = drive;
|
||||
disk->data = data = xmalloc (sizeof (struct grub_util_biosdisk_data));
|
||||
data->dev = NULL;
|
||||
data->access_mode = 0;
|
||||
data->fd = -1;
|
||||
|
||||
/* Get the size. */
|
||||
#if defined(__MINGW32__)
|
||||
|
|
@ -198,7 +211,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
|||
|
||||
fd = open (map[drive].device, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s' while attempting to get disk size", map[drive].device);
|
||||
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot open `%s' while attempting to get disk size", map[drive].device);
|
||||
|
||||
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
|
||||
if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
|
||||
|
|
@ -244,7 +257,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
|||
# warning "No special routine to get the size of a block device is implemented for your OS. This is not possibly fatal."
|
||||
#endif
|
||||
if (stat (map[drive].device, &st) < 0)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot stat `%s'", map[drive].device);
|
||||
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot stat `%s'", map[drive].device);
|
||||
|
||||
disk->total_sectors = st.st_size >> GRUB_DISK_SECTOR_BITS;
|
||||
|
||||
|
|
@ -254,6 +267,17 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
|||
}
|
||||
|
||||
#ifdef __linux__
|
||||
/* Cache of partition start sectors for each disk. */
|
||||
struct linux_partition_cache
|
||||
{
|
||||
struct linux_partition_cache *next;
|
||||
char *dev;
|
||||
unsigned long start;
|
||||
int partno;
|
||||
};
|
||||
|
||||
struct linux_partition_cache *linux_partition_cache_list;
|
||||
|
||||
static int
|
||||
linux_find_partition (char *dev, unsigned long sector)
|
||||
{
|
||||
|
|
@ -262,6 +286,7 @@ linux_find_partition (char *dev, unsigned long sector)
|
|||
char *p;
|
||||
int i;
|
||||
char real_dev[PATH_MAX];
|
||||
struct linux_partition_cache *cache;
|
||||
|
||||
strcpy(real_dev, dev);
|
||||
|
||||
|
|
@ -281,6 +306,16 @@ linux_find_partition (char *dev, unsigned long sector)
|
|||
format = "%d";
|
||||
}
|
||||
|
||||
for (cache = linux_partition_cache_list; cache; cache = cache->next)
|
||||
{
|
||||
if (strcmp (cache->dev, dev) == 0 && cache->start == sector)
|
||||
{
|
||||
sprintf (p, format, cache->partno);
|
||||
strcpy (dev, real_dev);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 1; i < 10000; i++)
|
||||
{
|
||||
int fd;
|
||||
|
|
@ -301,6 +336,15 @@ linux_find_partition (char *dev, unsigned long sector)
|
|||
|
||||
if (hdg.start == sector)
|
||||
{
|
||||
struct linux_partition_cache *new_cache_item;
|
||||
|
||||
new_cache_item = xmalloc (sizeof *new_cache_item);
|
||||
new_cache_item->dev = xstrdup (dev);
|
||||
new_cache_item->start = hdg.start;
|
||||
new_cache_item->partno = i;
|
||||
grub_list_push (GRUB_AS_LIST_P (&linux_partition_cache_list),
|
||||
GRUB_AS_LIST (new_cache_item));
|
||||
|
||||
strcpy (dev, real_dev);
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -314,6 +358,7 @@ static int
|
|||
open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags)
|
||||
{
|
||||
int fd;
|
||||
struct grub_util_biosdisk_data *data = disk->data;
|
||||
|
||||
#ifdef O_LARGEFILE
|
||||
flags |= O_LARGEFILE;
|
||||
|
|
@ -343,18 +388,35 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags)
|
|||
&& strncmp (map[disk->id].device, "/dev/", 5) == 0)
|
||||
is_partition = linux_find_partition (dev, part_start);
|
||||
|
||||
/* Open the partition. */
|
||||
grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
|
||||
fd = open (dev, flags);
|
||||
if (fd < 0)
|
||||
if (data->dev && strcmp (data->dev, dev) == 0 &&
|
||||
data->access_mode == (flags & O_ACCMODE))
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", dev);
|
||||
return -1;
|
||||
grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
|
||||
fd = data->fd;
|
||||
}
|
||||
else
|
||||
{
|
||||
free (data->dev);
|
||||
if (data->fd != -1)
|
||||
close (data->fd);
|
||||
|
||||
/* Flush the buffer cache to the physical disk.
|
||||
XXX: This also empties the buffer cache. */
|
||||
ioctl (fd, BLKFLSBUF, 0);
|
||||
/* 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, "cannot open `%s'", dev);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Flush the buffer cache to the physical disk.
|
||||
XXX: This also empties the buffer cache. */
|
||||
ioctl (fd, BLKFLSBUF, 0);
|
||||
|
||||
data->dev = xstrdup (dev);
|
||||
data->access_mode = (flags & O_ACCMODE);
|
||||
data->fd = fd;
|
||||
}
|
||||
|
||||
if (is_partition)
|
||||
sector -= part_start;
|
||||
|
|
@ -378,7 +440,26 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags)
|
|||
}
|
||||
#endif
|
||||
|
||||
fd = open (map[disk->id].device, flags);
|
||||
if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 &&
|
||||
data->access_mode == (flags & O_ACCMODE))
|
||||
{
|
||||
grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev);
|
||||
fd = data->fd;
|
||||
}
|
||||
else
|
||||
{
|
||||
free (data->dev);
|
||||
if (data->fd != -1)
|
||||
close (data->fd);
|
||||
|
||||
fd = open (map[disk->id].device, flags);
|
||||
if (fd >= 0)
|
||||
{
|
||||
data->dev = xstrdup (map[disk->id].device);
|
||||
data->access_mode = (flags & O_ACCMODE);
|
||||
data->fd = fd;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
if (! (sysctl_oldflags & 0x10)
|
||||
|
|
@ -538,7 +619,6 @@ grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
|
|||
!= (ssize_t) (size << GRUB_DISK_SECTOR_BITS))
|
||||
grub_error (GRUB_ERR_READ_ERROR, "cannot read from `%s'", map[disk->id].device);
|
||||
|
||||
close (fd);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
|
|
@ -573,17 +653,27 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
|
|||
!= (ssize_t) (size << GRUB_DISK_SECTOR_BITS))
|
||||
grub_error (GRUB_ERR_WRITE_ERROR, "cannot write to `%s'", map[disk->id].device);
|
||||
|
||||
close (fd);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
static void
|
||||
grub_util_biosdisk_close (struct grub_disk *disk)
|
||||
{
|
||||
struct grub_util_biosdisk_data *data = disk->data;
|
||||
|
||||
free (data->dev);
|
||||
if (data->fd != -1)
|
||||
close (data->fd);
|
||||
free (data);
|
||||
}
|
||||
|
||||
static struct grub_disk_dev grub_util_biosdisk_dev =
|
||||
{
|
||||
.name = "biosdisk",
|
||||
.id = GRUB_DISK_DEVICE_BIOSDISK_ID,
|
||||
.iterate = grub_util_biosdisk_iterate,
|
||||
.open = grub_util_biosdisk_open,
|
||||
.close = 0,
|
||||
.close = grub_util_biosdisk_close,
|
||||
.read = grub_util_biosdisk_read,
|
||||
.write = grub_util_biosdisk_write,
|
||||
.next = 0
|
||||
|
|
@ -984,10 +1074,10 @@ grub_util_biosdisk_get_grub_dev (const char *os_dev)
|
|||
struct hd_geometry hdg;
|
||||
int dos_part = -1;
|
||||
int bsd_part = -1;
|
||||
auto int find_partition (grub_disk_t disk,
|
||||
auto int find_partition (grub_disk_t dsk,
|
||||
const grub_partition_t partition);
|
||||
|
||||
int find_partition (grub_disk_t disk __attribute__ ((unused)),
|
||||
int find_partition (grub_disk_t dsk __attribute__ ((unused)),
|
||||
const grub_partition_t partition)
|
||||
{
|
||||
grub_disk_addr_t part_start = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue