2009-09-21 14:03:11 +00:00
|
|
|
/* hostdisk.c - emulate biosdisk */
|
2003-01-02 20:12:33 +00:00
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-09-26 13:53:05 +00:00
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
|
2003-01-02 20:12:33 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2003-01-02 20:12:33 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2003-01-02 20:12:33 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2003-01-02 20:12:33 +00:00
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2003-01-02 20:12:33 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/disk.h>
|
2004-12-04 Marco Gerards <metgerards@student.han.nl>
Modulize the partition map support and add support for the amiga
partition map.
* commands/ls.c: Include <grub/partition.h> instead of
<grub/machine/partition.h>.
* kern/disk.c: Likewise.
* kern/rescue.c: Likewise.
* loader/i386/pc/chainloader.c: Likewise.
* normal/cmdline.c: Likewise.
* kern/powerpc/ieee1275/init.c: Likewise.
(grub_machine_init): Call `grub_pc_partition_map_init',
`grub_amiga_partition_map_init' and
`grub_apple_partition_map_init'.
* conf/i386-pc.rmk (kernel_img_SOURCES): Remove
`disk/i386/pc/partition.c'. Add `kern/partition.c'.
(kernel_img_HEADERS): Remove `machine/partition.h'. Add
`partition.h' and `pc_partition.h'.
(grub_setup_SOURCES): Remove
`disk/i386/pc/partition.c'. Add `kern/partition.c',
`partmap/amiga.c', `partmap/apple.c' and `partmap/pc.c'.
(grub_emu_SOURCES): Likewise.
(pkgdata_MODULES): Add `amiga.mod', `apple.mod' and `pc.mod'.
(amiga_mod_SOURCES, amiga_mod_CFLAGS, apple_mod_SOURCES)
(apple_mod_CFLAGS, pc_mod_SOURCES, pc_mod_CFLAGS): New variables.
* conf/powerpc-ieee1275.rmk (grub_emu_SOURCES): Remove
`disk/powerpc/ieee1275/partition.c'. Add `kern/partition.c',
`partmap/amiga.c', `partmap/apple.c' and `partmap/pc.c'.
(grubof_SOURCES): Likewise.
* disk/i386/pc/partition.c: File removed.
* disk/powerpc/ieee1275/partition.c: Likewise.
* include/grub/powerpc/ieee1275/partition.h: Likewise.
* include/grub/i386/pc/partition.h: Likewise.
* kern/partition.c: New file.
* partmap/amiga.c: Likewise.
* partmap/apple.c: Likewise.
* partmap/pc.c: Likewise.
* include/grub/partition.h: Likewise..
* include/grub/pc_partition.h: Likewise.
* util/grub-emu.c: Include <grub/partition.h> instead of
<grub/machine/partition.h>.
(main): Call `grub_pc_partition_map_init',
`grub_amiga_partition_map_init' and
`grub_apple_partition_map_init' and deinitialize afterwards.
* util/i386/pc/biosdisk.c: Include `#include
<grub/partition.h>' and `include <grub/pc_partition.h>' instead of
`<grub/machine/partition.h>'.
* util/i386/pc/grub-setup.c: Likewise.
* util/i386/pc/biosdisk.c: Likewise.
(grub_util_biosdisk_get_grub_dev): Only access the PC specific
partition information in case of a PC partition.
* util/i386/pc/grub-setup.c: Include `#include
<grub/partition.h>' and `include <grub/pc_partition.h>' instead of
`<grub/machine/partition.h>'.
(setup): Only access the PC specific partition information in case
of a PC partition.
2004-12-04 18:45:46 +00:00
|
|
|
#include <grub/partition.h>
|
2009-08-23 Robert Millan <rmh.grub@aybabtu.com>
* partmap/pc.c: Rename to ...
* partmap/msdos.c: ... this. Update all users.
(grub_pc_partition_map): Rename to ...
(grub_msdos_partition_map): ... this. Update all users.
* parttool/pcpart.c: Rename to ...
* parttool/msdospart.c: ... this. Update all users.
* include/grub/pc_partition.h: Rename to ...
* include/grub/msdos_partition.h: ... this. Update all users.
(grub_pc_partition_bsd_entry): Rename to ...
(grub_msdos_partition_bsd_entry): ... this. Update all users.
(grub_pc_partition_disk_label): Rename to ...
(grub_msdos_partition_disk_label): ... this. Update all users.
(grub_pc_partition_entry): Rename to ...
(grub_msdos_partition_entry): ... this. Update all users.
(grub_pc_partition_mbr): Rename to ...
(grub_msdos_partition_mbr): ... this. Update all users.
(grub_pc_partition): Rename to ...
(grub_msdos_partition): ... this. Update all users.
(grub_pc_partition_is_empty): Rename to ...
(grub_msdos_partition_is_empty): ... this. Update all users.
(grub_pc_partition_is_extended): Rename to ...
(grub_msdos_partition_is_extended): ... this. Update all users.
(grub_pc_partition_is_bsd): Rename to ...
(grub_msdos_partition_is_bsd): ... this. Update all users.
* conf/common.rmk (amiga_mod_SOURCES, amiga_mod_CFLAGS)
(amiga_mod_LDFLAGS, apple_mod_SOURCES, apple_mod_CFLAGS)
(apple_mod_LDFLAGS, msdos_mod_SOURCES, msdos_mod_CFLAGS)
(msdos_mod_LDFLAGS, sun_mod_SOURCES, sun_mod_CFLAGS)
(sun_mod_LDFLAGS, acorn_mod_SOURCES, acorn_mod_CFLAGS)
(acorn_mod_LDFLAGS, gpt_mod_SOURCES, gpt_mod_CFLAGS)
(gpt_mod_LDFLAGS): Rename to ...
(part_amiga_mod_SOURCES, part_amiga_mod_CFLAGS, part_amiga_mod_LDFLAGS)
(part_apple_mod_SOURCES, part_apple_mod_CFLAGS, part_apple_mod_LDFLAGS)
(part_msdos_mod_SOURCES, part_msdos_mod_CFLAGS, part_msdos_mod_LDFLAGS)
(part_sun_mod_SOURCES, part_sun_mod_CFLAGS, part_sun_mod_LDFLAGS)
(part_acorn_mod_SOURCES, part_acorn_mod_CFLAGS, part_acorn_mod_LDFLAGS)
(part_gpt_mod_SOURCES, part_gpt_mod_CFLAGS)
(part_gpt_mod_LDFLAGS): ... this.
(pkglib_MODULES): Prefix partition modules with `part_'. Rename
`pcpart.mod' to `msdospart.mod'.
(pcpart_mod_SOURCES, pcpart_mod_CFLAGS, pcpart_mod_LDFLAGS): Rename
to ...
(msdospart_mod_SOURCES, msdospart_mod_CFLAGS)
(msdospart_mod_LDFLAGS): ... this.
2009-08-23 12:00:57 +00:00
|
|
|
#include <grub/msdos_partition.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/err.h>
|
2010-04-27 05:20:28 +00:00
|
|
|
#include <grub/emu/misc.h>
|
|
|
|
#include <grub/emu/hostdisk.h>
|
2011-01-05 11:41:12 +00:00
|
|
|
#include <grub/emu/getroot.h>
|
2008-09-08 14:00:29 +00:00
|
|
|
#include <grub/misc.h>
|
2009-11-23 20:30:56 +00:00
|
|
|
#include <grub/i18n.h>
|
2010-03-03 10:44:12 +00:00
|
|
|
#include <grub/list.h>
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/ioctl.h> /* ioctl */
|
2011-03-29 00:02:55 +00:00
|
|
|
# include <sys/mount.h>
|
2003-01-02 20:12:33 +00:00
|
|
|
# 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 */
|
|
|
|
# include <sys/ioctl.h> /* ioctl */
|
|
|
|
# ifndef HDIO_GETGEO
|
|
|
|
# define HDIO_GETGEO 0x0301 /* get device geometry */
|
|
|
|
/* If HDIO_GETGEO is not defined, it is unlikely that hd_geometry is
|
|
|
|
defined. */
|
|
|
|
struct hd_geometry
|
|
|
|
{
|
|
|
|
unsigned char heads;
|
|
|
|
unsigned char sectors;
|
|
|
|
unsigned short cylinders;
|
|
|
|
unsigned long start;
|
|
|
|
};
|
|
|
|
# endif /* ! HDIO_GETGEO */
|
2007-06-04 18:58:17 +00:00
|
|
|
# ifndef BLKGETSIZE64
|
|
|
|
# define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size */
|
|
|
|
# endif /* ! BLKGETSIZE64 */
|
2003-01-02 20:12:33 +00:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <cygwin/fs.h> /* BLKGETSIZE64 */
|
|
|
|
# include <cygwin/hdreg.h> /* HDIO_GETGEO */
|
|
|
|
#endif
|
|
|
|
|
2009-04-11 18:46:12 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-04-11 09:40:39 +00:00
|
|
|
# include <sys/disk.h> /* DIOCGMEDIASIZE */
|
2009-04-14 07:01:34 +00:00
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/sysctl.h>
|
2011-11-08 18:34:19 +00:00
|
|
|
# include <sys/mount.h>
|
|
|
|
#include <libgeom.h>
|
2009-04-11 09:40:39 +00:00
|
|
|
#endif
|
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#if defined (__sun__)
|
|
|
|
# include <sys/dkio.h>
|
|
|
|
#endif
|
|
|
|
|
2009-07-20 20:03:18 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
# include <sys/disk.h>
|
|
|
|
#endif
|
|
|
|
|
2010-01-25 17:47:51 +00:00
|
|
|
#ifdef HAVE_DEVICE_MAPPER
|
|
|
|
# include <libdevmapper.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
#if defined(__NetBSD__)
|
2010-09-20 11:12:33 +00:00
|
|
|
# define HAVE_DIOCGDINFO
|
2010-04-10 15:07:33 +00:00
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <sys/disklabel.h> /* struct disklabel */
|
2012-02-03 11:32:21 +00:00
|
|
|
# include <sys/disk.h> /* struct dkwedge_info */
|
2010-09-20 11:12:33 +00:00
|
|
|
#else /* !defined(__NetBSD__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) */
|
|
|
|
# undef HAVE_DIOCGDINFO
|
|
|
|
#endif /* defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
|
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
2010-04-10 15:07:33 +00:00
|
|
|
# ifdef HAVE_GETRAWPARTITION
|
|
|
|
# include <util.h> /* getrawpartition */
|
|
|
|
# endif /* HAVE_GETRAWPARTITION */
|
|
|
|
# include <sys/fdio.h>
|
|
|
|
# ifndef RAW_FLOPPY_MAJOR
|
|
|
|
# define RAW_FLOPPY_MAJOR 9
|
|
|
|
# endif /* ! RAW_FLOPPY_MAJOR */
|
|
|
|
#endif /* defined(__NetBSD__) */
|
|
|
|
|
2012-04-23 15:14:48 +00:00
|
|
|
static struct
|
2008-05-30 10:50:00 +00:00
|
|
|
{
|
|
|
|
char *drive;
|
|
|
|
char *device;
|
2011-12-23 17:19:16 +00:00
|
|
|
int device_map;
|
2008-05-30 10:50:00 +00:00
|
|
|
} map[256];
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2010-03-03 10:44:12 +00:00
|
|
|
struct grub_util_biosdisk_data
|
|
|
|
{
|
|
|
|
char *dev;
|
2010-03-03 20:03:50 +00:00
|
|
|
int access_mode;
|
2010-03-03 10:44:12 +00:00
|
|
|
int fd;
|
2011-04-10 14:08:58 +00:00
|
|
|
int is_disk;
|
2011-12-23 17:19:16 +00:00
|
|
|
int device_map;
|
2010-03-03 10:44:12 +00:00
|
|
|
};
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/* Check if we have devfs support. */
|
|
|
|
static int
|
|
|
|
have_devfs (void)
|
|
|
|
{
|
|
|
|
static int dev_devfsd_exists = -1;
|
|
|
|
|
|
|
|
if (dev_devfsd_exists < 0)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
dev_devfsd_exists = stat ("/dev/.devfsd", &st) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev_devfsd_exists;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2010-04-10 15:07:33 +00:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
/* 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. */
|
|
|
|
static void
|
|
|
|
configure_device_driver (int fd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
|
|
|
|
return;
|
|
|
|
if (major(st.st_rdev) == RAW_FLOPPY_MAJOR)
|
|
|
|
{
|
|
|
|
int floppy_opts;
|
|
|
|
|
|
|
|
if (ioctl (fd, FDIOCGETOPTS, &floppy_opts) == -1)
|
|
|
|
return;
|
|
|
|
floppy_opts |= FDOPT_NORETRY;
|
|
|
|
if (ioctl (fd, FDIOCSETOPTS, &floppy_opts) == -1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(__NetBSD__) */
|
|
|
|
|
2011-11-08 11:15:57 +00:00
|
|
|
static int
|
|
|
|
unescape_cmp (const char *a, const char *b_escaped)
|
|
|
|
{
|
|
|
|
while (*a || *b_escaped)
|
|
|
|
{
|
|
|
|
if (*b_escaped == '\\' && b_escaped[1] != 0)
|
|
|
|
b_escaped++;
|
|
|
|
if (*a < *b_escaped)
|
|
|
|
return -1;
|
|
|
|
if (*a > *b_escaped)
|
|
|
|
return +1;
|
|
|
|
a++;
|
|
|
|
b_escaped++;
|
|
|
|
}
|
|
|
|
if (*a)
|
|
|
|
return +1;
|
|
|
|
if (*b_escaped)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static int
|
2008-06-04 13:59:55 +00:00
|
|
|
find_grub_drive (const char *name)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2008-05-30 10:50:00 +00:00
|
|
|
unsigned int i;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
if (name)
|
|
|
|
{
|
2009-11-23 20:30:56 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (map); i++)
|
2011-11-08 11:15:57 +00:00
|
|
|
if (map[i].drive && unescape_cmp (map[i].drive, name) == 0)
|
2008-05-30 10:50:00 +00:00
|
|
|
return i;
|
|
|
|
}
|
2008-08-28 14:36:18 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-02-07 01:47:18 +00:00
|
|
|
find_free_slot (void)
|
2008-08-28 14:36:18 +00:00
|
|
|
{
|
2008-08-28 19:08:21 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2008-08-28 14:36:18 +00:00
|
|
|
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
|
|
|
if (! map[i].drive)
|
|
|
|
return i;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-04-21 22:09:07 +00:00
|
|
|
grub_util_biosdisk_iterate (int (*hook) (const char *name),
|
|
|
|
grub_disk_pull_t pull)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2011-08-16 21:12:20 +00:00
|
|
|
if (pull != GRUB_DISK_PULL_NONE)
|
|
|
|
return 0;
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
2008-05-30 10:50:00 +00:00
|
|
|
if (map[i].drive && hook (map[i].drive))
|
2003-01-02 20:12:33 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
#if !defined(__MINGW32__)
|
|
|
|
grub_uint64_t
|
2012-02-29 14:23:31 +00:00
|
|
|
grub_util_get_fd_size (int fd, const char *name, unsigned *log_secsize)
|
2011-04-25 12:52:07 +00:00
|
|
|
{
|
2012-04-22 16:44:19 +00:00
|
|
|
#if !defined (__GNU__)
|
2011-04-25 12:52:07 +00:00
|
|
|
# if defined(__NetBSD__)
|
|
|
|
struct disklabel label;
|
2011-11-08 11:38:30 +00:00
|
|
|
# elif defined (__sun__)
|
|
|
|
struct dk_minfo minfo;
|
2012-04-22 16:44:19 +00:00
|
|
|
# else
|
2011-04-25 12:52:07 +00:00
|
|
|
unsigned long long nr;
|
|
|
|
# endif
|
2012-04-22 16:44:19 +00:00
|
|
|
#endif
|
2011-04-25 12:52:07 +00:00
|
|
|
unsigned sector_size, log_sector_size;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat (fd, &st) < 0)
|
2012-03-03 11:59:28 +00:00
|
|
|
/* TRANSLATORS: "stat" comes from the name of POSIX function. */
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot stat `%s': %s"), name, strerror (errno));
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#if defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__) \
|
|
|
|
|| defined (__sun__)
|
|
|
|
|
|
|
|
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__) || defined (__sun__)
|
2011-04-25 12:52:07 +00:00
|
|
|
if (! S_ISCHR (st.st_mode))
|
|
|
|
# else
|
|
|
|
if (! S_ISBLK (st.st_mode))
|
|
|
|
# endif
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
if (ioctl (fd, DIOCGMEDIASIZE, &nr))
|
|
|
|
# elif defined(__APPLE__)
|
|
|
|
if (ioctl (fd, DKIOCGETBLOCKCOUNT, &nr))
|
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
configure_device_driver (fd);
|
|
|
|
if (ioctl (fd, DIOCGDINFO, &label) == -1)
|
2011-11-08 11:38:30 +00:00
|
|
|
# elif defined (__sun__)
|
|
|
|
if (!ioctl (fd, DKIOCGMEDIAINFO, &minfo))
|
2011-04-25 12:52:07 +00:00
|
|
|
# else
|
|
|
|
if (ioctl (fd, BLKGETSIZE64, &nr))
|
|
|
|
# endif
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
if (ioctl (fd, DIOCGSECTORSIZE, §or_size))
|
2011-09-17 21:01:48 +00:00
|
|
|
goto fail;
|
2012-02-29 14:17:59 +00:00
|
|
|
# elif defined(__APPLE__)
|
|
|
|
if (ioctl (fd, DKIOCGETBLOCKSIZE, §or_size))
|
|
|
|
goto fail;
|
2011-11-08 11:38:30 +00:00
|
|
|
# elif defined(__sun__)
|
|
|
|
sector_size = minfo.dki_lbsize;
|
2011-09-17 21:01:48 +00:00
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
sector_size = label.d_secsize;
|
2011-04-25 12:52:07 +00:00
|
|
|
# else
|
|
|
|
if (ioctl (fd, BLKSSZGET, §or_size))
|
|
|
|
goto fail;
|
2011-09-17 21:01:48 +00:00
|
|
|
# endif
|
2011-04-25 12:52:07 +00:00
|
|
|
if (sector_size & (sector_size - 1) || !sector_size)
|
|
|
|
goto fail;
|
|
|
|
for (log_sector_size = 0;
|
|
|
|
(1 << log_sector_size) < sector_size;
|
|
|
|
log_sector_size++);
|
|
|
|
|
|
|
|
if (log_secsize)
|
|
|
|
*log_secsize = log_sector_size;
|
|
|
|
|
|
|
|
# if defined (__APPLE__)
|
2012-02-29 14:23:31 +00:00
|
|
|
return nr << log_sector_size;
|
2011-04-25 12:52:07 +00:00
|
|
|
# elif defined(__NetBSD__)
|
2012-02-29 14:23:31 +00:00
|
|
|
return label.d_secperunit << log_sector_size;
|
2011-11-08 11:38:30 +00:00
|
|
|
# elif defined (__sun__)
|
2012-02-29 14:23:31 +00:00
|
|
|
return minfo.dki_capacity << log_sector_size;
|
2011-04-25 12:52:07 +00:00
|
|
|
# else
|
|
|
|
if (nr & ((1 << log_sector_size) - 1))
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_error ("%s", _("unaligned device size"));
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2012-02-29 14:23:31 +00:00
|
|
|
return nr;
|
2011-04-25 12:52:07 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
fail:
|
2011-11-08 11:38:30 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
/* In GNU/Hurd, stat() will return the right size. */
|
|
|
|
#elif !defined (__GNU__)
|
|
|
|
# warning "No special routine to get the size of a block device is implemented for your OS. This is not possibly fatal."
|
|
|
|
#endif
|
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
sector_size = 512;
|
|
|
|
log_sector_size = 9;
|
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
if (log_secsize)
|
|
|
|
*log_secsize = 9;
|
|
|
|
|
2012-02-29 14:23:31 +00:00
|
|
|
return st.st_size;
|
2011-04-25 12:52:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_util_biosdisk_open (const char *name, grub_disk_t disk)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
int drive;
|
|
|
|
struct stat st;
|
2010-03-03 10:44:12 +00:00
|
|
|
struct grub_util_biosdisk_data *data;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-06-04 13:59:55 +00:00
|
|
|
drive = find_grub_drive (name);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (drive < 0)
|
2010-02-07 15:13:59 +00:00
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"no mapping exists for `%s'", name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
disk->id = drive;
|
2010-03-03 10:44:12 +00:00
|
|
|
disk->data = data = xmalloc (sizeof (struct grub_util_biosdisk_data));
|
|
|
|
data->dev = NULL;
|
2010-03-03 20:03:50 +00:00
|
|
|
data->access_mode = 0;
|
2010-03-03 10:44:12 +00:00
|
|
|
data->fd = -1;
|
2011-04-10 14:08:58 +00:00
|
|
|
data->is_disk = 0;
|
2011-12-23 17:19:16 +00:00
|
|
|
data->device_map = map[drive].device_map;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
/* Get the size. */
|
2008-08-29 19:55:23 +00:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
{
|
|
|
|
grub_uint64_t size;
|
|
|
|
|
|
|
|
size = grub_util_get_disk_size (map[drive].device);
|
|
|
|
|
|
|
|
if (size % 512)
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_util_error (_("unaligned device size"));
|
2008-08-29 19:55:23 +00:00
|
|
|
|
|
|
|
disk->total_sectors = size >> 9;
|
|
|
|
|
|
|
|
grub_util_info ("the size of %s is %llu", name, disk->total_sectors);
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2011-04-25 12:52:07 +00:00
|
|
|
#else
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
fd = open (map[drive].device, O_RDONLY);
|
2008-02-11 15:47:35 +00:00
|
|
|
if (fd == -1)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"),
|
|
|
|
map[drive].device, strerror (errno));
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2012-02-29 14:23:31 +00:00
|
|
|
disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device,
|
|
|
|
&disk->log_sector_size);
|
|
|
|
disk->total_sectors >>= disk->log_sector_size;
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2010-04-10 15:07:33 +00:00
|
|
|
# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__)
|
2009-04-11 09:40:39 +00:00
|
|
|
if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
|
|
|
|
# else
|
2003-01-07 07:54:07 +00:00
|
|
|
if (fstat (fd, &st) < 0 || ! S_ISBLK (st.st_mode))
|
2009-04-11 09:40:39 +00:00
|
|
|
# endif
|
2011-04-25 12:52:07 +00:00
|
|
|
data->is_disk = 1;
|
2003-01-02 23:46:21 +00:00
|
|
|
|
|
|
|
close (fd);
|
2009-07-20 20:03:18 +00:00
|
|
|
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_info ("the size of %s is %" PRIuGRUB_UINT64_T,
|
|
|
|
name, disk->total_sectors);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 23:46:21 +00:00
|
|
|
}
|
2004-03-14 17:48:25 +00:00
|
|
|
#endif
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2011-01-08 15:33:49 +00:00
|
|
|
int
|
|
|
|
grub_util_device_is_mapped (const char *dev)
|
2010-01-25 17:47:51 +00:00
|
|
|
{
|
2011-01-08 15:33:49 +00:00
|
|
|
#ifdef HAVE_DEVICE_MAPPER
|
2010-01-25 17:47:51 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2011-01-08 15:33:49 +00:00
|
|
|
if (!grub_device_mapper_supported ())
|
|
|
|
return 0;
|
|
|
|
|
2010-01-25 17:47:51 +00:00
|
|
|
if (stat (dev, &st) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return dm_is_dm_major (major (st.st_rdev));
|
2011-01-08 15:33:49 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
2010-01-25 17:47:51 +00:00
|
|
|
#endif /* HAVE_DEVICE_MAPPER */
|
2011-01-08 15:33:49 +00:00
|
|
|
}
|
2010-01-25 17:47:51 +00:00
|
|
|
|
2011-11-08 18:34:19 +00:00
|
|
|
|
2011-03-26 11:59:02 +00:00
|
|
|
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
|
|
|
|
/* FIXME: geom actually gives us the whole container hierarchy.
|
|
|
|
It can be used more efficiently than this. */
|
2011-11-08 18:34:19 +00:00
|
|
|
void
|
|
|
|
grub_util_follow_gpart_up (const char *name, grub_disk_addr_t *off_out, char **name_out)
|
2011-03-26 11:59:02 +00:00
|
|
|
{
|
|
|
|
struct gmesh mesh;
|
|
|
|
struct gclass *class;
|
|
|
|
int error;
|
|
|
|
struct ggeom *geom;
|
|
|
|
|
|
|
|
grub_util_info ("following geom '%s'", name);
|
|
|
|
|
|
|
|
error = geom_gettree (&mesh);
|
|
|
|
if (error != 0)
|
2012-02-08 18:26:01 +00:00
|
|
|
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
|
|
|
Usually left untranslated.
|
|
|
|
*/
|
2012-04-21 14:49:56 +00:00
|
|
|
grub_util_error ("%s", _("couldn't open geom"));
|
2011-03-26 11:59:02 +00:00
|
|
|
|
|
|
|
LIST_FOREACH (class, &mesh.lg_class, lg_class)
|
|
|
|
if (strcasecmp (class->lg_name, "part") == 0)
|
|
|
|
break;
|
|
|
|
if (!class)
|
2012-02-08 18:26:01 +00:00
|
|
|
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
2012-03-03 11:59:28 +00:00
|
|
|
Usually left untranslated. "part" is the identifier of one of its
|
|
|
|
classes. */
|
2012-04-21 14:49:56 +00:00
|
|
|
grub_util_error ("%s", _("couldn't find geom `part' class"));
|
2011-03-26 11:59:02 +00:00
|
|
|
|
|
|
|
LIST_FOREACH (geom, &class->lg_geom, lg_geom)
|
|
|
|
{
|
|
|
|
struct gprovider *provider;
|
|
|
|
LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
|
|
|
|
if (strcmp (provider->lg_name, name) == 0)
|
|
|
|
{
|
|
|
|
char *name_tmp = xstrdup (geom->lg_name);
|
|
|
|
grub_disk_addr_t off = 0;
|
|
|
|
struct gconfig *config;
|
|
|
|
grub_util_info ("geom '%s' has parent '%s'", name, geom->lg_name);
|
|
|
|
|
2011-11-08 18:34:19 +00:00
|
|
|
grub_util_follow_gpart_up (name_tmp, &off, name_out);
|
2011-03-26 11:59:02 +00:00
|
|
|
free (name_tmp);
|
|
|
|
LIST_FOREACH (config, &provider->lg_config, lg_config)
|
|
|
|
if (strcasecmp (config->lg_name, "start") == 0)
|
|
|
|
off += strtoull (config->lg_val, 0, 10);
|
|
|
|
if (off_out)
|
|
|
|
*off_out = off;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grub_util_info ("geom '%s' has no parent", name);
|
|
|
|
if (name_out)
|
|
|
|
*name_out = xstrdup (name);
|
|
|
|
if (off_out)
|
|
|
|
*off_out = 0;
|
|
|
|
}
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_disk_addr_t
|
|
|
|
grub_hostdisk_find_partition_start (const char *dev)
|
2011-03-26 11:59:02 +00:00
|
|
|
{
|
|
|
|
grub_disk_addr_t out;
|
|
|
|
if (strncmp (dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
|
|
|
return 0;
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_util_follow_gpart_up (dev + sizeof ("/dev/") - 1, &out, NULL);
|
2011-03-26 11:59:02 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#elif defined(__linux__) || defined(__CYGWIN__) || defined(HAVE_DIOCGDINFO) || defined (__sun__)
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_disk_addr_t
|
|
|
|
grub_hostdisk_find_partition_start (const char *dev)
|
2010-01-25 17:47:51 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2011-11-08 11:38:30 +00:00
|
|
|
#ifdef __sun__
|
|
|
|
struct extpart_info pinfo;
|
|
|
|
# elif !defined(HAVE_DIOCGDINFO)
|
2010-01-25 17:47:51 +00:00
|
|
|
struct hd_geometry hdg;
|
2010-09-20 11:12:33 +00:00
|
|
|
# else /* defined(HAVE_DIOCGDINFO) */
|
2012-02-03 11:32:21 +00:00
|
|
|
# if defined(__NetBSD__)
|
|
|
|
struct dkwedge_info dkw;
|
|
|
|
# endif /* defined(__NetBSD__) */
|
2010-05-18 10:14:13 +00:00
|
|
|
struct disklabel label;
|
2010-05-26 21:49:52 +00:00
|
|
|
int p_index;
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* !defined(HAVE_DIOCGDINFO) */
|
2010-01-25 17:47:51 +00:00
|
|
|
|
2010-05-18 10:14:13 +00:00
|
|
|
# ifdef HAVE_DEVICE_MAPPER
|
2011-01-08 15:33:49 +00:00
|
|
|
if (grub_util_device_is_mapped (dev)) {
|
2010-01-25 17:47:51 +00:00
|
|
|
struct dm_task *task = NULL;
|
|
|
|
grub_uint64_t start, length;
|
|
|
|
char *target_type, *params, *space;
|
|
|
|
grub_disk_addr_t partition_start;
|
|
|
|
|
|
|
|
/* If any device-mapper operation fails, we fall back silently to
|
|
|
|
HDIO_GETGEO. */
|
|
|
|
task = dm_task_create (DM_DEVICE_TABLE);
|
|
|
|
if (! task)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm_task_create failed\n");
|
|
|
|
goto devmapper_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! dm_task_set_name (task, dev))
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm_task_set_name failed\n");
|
|
|
|
goto devmapper_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! dm_task_run (task))
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm_task_run failed\n");
|
|
|
|
goto devmapper_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
dm_get_next_target (task, NULL, &start, &length, &target_type, ¶ms);
|
|
|
|
if (! target_type)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "no dm target\n");
|
|
|
|
goto devmapper_fail;
|
|
|
|
}
|
|
|
|
if (strcmp (target_type, "linear") != 0)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "ignoring dm target %s (not linear)\n",
|
|
|
|
target_type);
|
|
|
|
goto devmapper_fail;
|
|
|
|
}
|
|
|
|
if (! params)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "no dm params\n");
|
|
|
|
goto devmapper_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The params string for a linear target looks like this:
|
|
|
|
DEVICE-NAME START-SECTOR
|
|
|
|
Parse this out. */
|
|
|
|
space = strchr (params, ' ');
|
|
|
|
if (! space)
|
|
|
|
goto devmapper_fail;
|
|
|
|
errno = 0;
|
|
|
|
partition_start = strtoull (space + 1, NULL, 10);
|
|
|
|
if (errno == 0)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm %s starts at %llu\n",
|
2010-05-18 10:14:13 +00:00
|
|
|
dev, (unsigned long long) partition_start);
|
2010-01-25 17:47:51 +00:00
|
|
|
dm_task_destroy (task);
|
|
|
|
return partition_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
devmapper_fail:
|
|
|
|
if (task)
|
|
|
|
dm_task_destroy (task);
|
|
|
|
}
|
2010-05-18 10:14:13 +00:00
|
|
|
# endif /* HAVE_DEVICE_MAPPER */
|
2010-01-25 17:47:51 +00:00
|
|
|
|
|
|
|
fd = open (dev, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
|
|
|
dev, strerror (errno));
|
2010-01-25 17:47:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#if defined(__sun__)
|
|
|
|
if (ioctl (fd, DKIOCEXTPARTINFO, &pinfo))
|
|
|
|
# elif !defined(HAVE_DIOCGDINFO)
|
2010-01-25 17:47:51 +00:00
|
|
|
if (ioctl (fd, HDIO_GETGEO, &hdg))
|
2010-09-20 11:12:33 +00:00
|
|
|
# else /* defined(HAVE_DIOCGDINFO) */
|
|
|
|
# if defined(__NetBSD__)
|
2010-05-18 10:14:13 +00:00
|
|
|
configure_device_driver (fd);
|
2012-02-03 11:32:21 +00:00
|
|
|
/* First handle the case of disk wedges. */
|
|
|
|
if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == 0)
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
return (grub_disk_addr_t) dkw.dkw_offset;
|
|
|
|
}
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* defined(__NetBSD__) */
|
2010-05-18 10:14:13 +00:00
|
|
|
if (ioctl (fd, DIOCGDINFO, &label) == -1)
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* !defined(HAVE_DIOCGDINFO) */
|
2010-01-25 17:47:51 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
2010-09-20 11:12:33 +00:00
|
|
|
# if !defined(HAVE_DIOCGDINFO)
|
2010-05-18 10:14:13 +00:00
|
|
|
"cannot get disk geometry of `%s'", dev);
|
2010-09-20 11:12:33 +00:00
|
|
|
# else /* defined(HAVE_DIOCGDINFO) */
|
2010-06-02 22:47:22 +00:00
|
|
|
"cannot get disk label of `%s'", dev);
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* !defined(HAVE_DIOCGDINFO) */
|
2010-01-25 17:47:51 +00:00
|
|
|
close (fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#ifdef __sun__
|
|
|
|
return pinfo.p_start;
|
|
|
|
# elif !defined(HAVE_DIOCGDINFO)
|
2010-01-25 17:47:51 +00:00
|
|
|
return hdg.start;
|
2010-09-20 11:12:33 +00:00
|
|
|
# else /* defined(HAVE_DIOCGDINFO) */
|
2011-03-29 13:00:23 +00:00
|
|
|
if (dev[0])
|
|
|
|
p_index = dev[strlen(dev) - 1] - 'a';
|
|
|
|
else
|
|
|
|
p_index = -1;
|
|
|
|
|
|
|
|
if (p_index >= label.d_npartitions || p_index < 0)
|
2010-05-18 10:14:13 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
|
|
|
"no disk label entry for `%s'", dev);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-26 21:49:52 +00:00
|
|
|
return (grub_disk_addr_t) label.d_partitions[p_index].p_offset;
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* !defined(HAVE_DIOCGDINFO) */
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
2010-09-20 11:12:33 +00:00
|
|
|
#endif /* __linux__ || __CYGWIN__ || HAVE_DIOCGDINFO */
|
2010-01-25 17:47:51 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef __linux__
|
2010-03-03 10:44:12 +00:00
|
|
|
/* Cache of partition start sectors for each disk. */
|
|
|
|
struct linux_partition_cache
|
|
|
|
{
|
|
|
|
struct linux_partition_cache *next;
|
2012-01-24 12:31:12 +00:00
|
|
|
struct linux_partition_cache **prev;
|
2010-03-03 10:44:12 +00:00
|
|
|
char *dev;
|
|
|
|
unsigned long start;
|
|
|
|
int partno;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct linux_partition_cache *linux_partition_cache_list;
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static int
|
2011-05-09 16:46:51 +00:00
|
|
|
linux_find_partition (char *dev, grub_disk_addr_t sector)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
size_t len = strlen (dev);
|
|
|
|
const char *format;
|
|
|
|
char *p;
|
|
|
|
int i;
|
2007-05-17 19:03:42 +00:00
|
|
|
char real_dev[PATH_MAX];
|
2010-03-03 10:44:12 +00:00
|
|
|
struct linux_partition_cache *cache;
|
2011-05-27 12:52:21 +00:00
|
|
|
int missing = 0;
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2007-05-17 19:03:42 +00:00
|
|
|
strcpy(real_dev, dev);
|
2008-05-30 21:55:31 +00:00
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
if (have_devfs () && strcmp (real_dev + len - 5, "/disc") == 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2003-01-02 23:46:21 +00:00
|
|
|
p = real_dev + len - 4;
|
2003-01-02 20:12:33 +00:00
|
|
|
format = "part%d";
|
|
|
|
}
|
2011-05-27 12:57:22 +00:00
|
|
|
else if (strncmp (real_dev, "/dev/disk/by-id/",
|
|
|
|
sizeof ("/dev/disk/by-id/") - 1) == 0)
|
|
|
|
{
|
|
|
|
p = real_dev + len;
|
|
|
|
format = "-part%d";
|
|
|
|
}
|
2008-05-30 21:55:31 +00:00
|
|
|
else if (real_dev[len - 1] >= '0' && real_dev[len - 1] <= '9')
|
2008-05-07 20:28:26 +00:00
|
|
|
{
|
2008-05-30 21:55:31 +00:00
|
|
|
p = real_dev + len;
|
2003-01-02 20:12:33 +00:00
|
|
|
format = "p%d";
|
|
|
|
}
|
2008-05-30 21:55:31 +00:00
|
|
|
else
|
2008-05-28 19:53:25 +00:00
|
|
|
{
|
2008-05-30 21:55:31 +00:00
|
|
|
p = real_dev + len;
|
|
|
|
format = "%d";
|
2008-05-28 19:53:25 +00:00
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2010-03-03 10:44:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
for (i = 1; i < 10000; i++)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2010-01-25 17:47:51 +00:00
|
|
|
grub_disk_addr_t start;
|
2008-05-30 21:55:31 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
sprintf (p, format, i);
|
2010-01-25 17:47:51 +00:00
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
fd = open (real_dev, O_RDONLY);
|
2008-02-11 15:47:35 +00:00
|
|
|
if (fd == -1)
|
2011-05-27 12:52:21 +00:00
|
|
|
{
|
|
|
|
if (missing++ < 10)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
missing = 0;
|
2003-01-02 20:12:33 +00:00
|
|
|
close (fd);
|
2008-05-30 21:55:31 +00:00
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
start = grub_hostdisk_find_partition_start (real_dev);
|
2010-01-25 17:47:51 +00:00
|
|
|
/* We don't care about errors here. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
if (start == sector)
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
2010-03-03 10:44:12 +00:00
|
|
|
struct linux_partition_cache *new_cache_item;
|
|
|
|
|
|
|
|
new_cache_item = xmalloc (sizeof *new_cache_item);
|
|
|
|
new_cache_item->dev = xstrdup (dev);
|
2010-05-18 10:14:13 +00:00
|
|
|
new_cache_item->start = start;
|
2010-03-03 10:44:12 +00:00
|
|
|
new_cache_item->partno = i;
|
|
|
|
grub_list_push (GRUB_AS_LIST_P (&linux_partition_cache_list),
|
|
|
|
GRUB_AS_LIST (new_cache_item));
|
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
strcpy (dev, real_dev);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2011-04-22 17:20:46 +00:00
|
|
|
#if defined(__linux__) && (!defined(__GLIBC__) || \
|
|
|
|
((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1))))
|
|
|
|
/* Maybe libc doesn't have large file support. */
|
|
|
|
grub_err_t
|
2011-04-24 01:12:06 +00:00
|
|
|
grub_util_fd_seek (int fd, const char *name, grub_uint64_t off)
|
2011-04-22 17:20:46 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2011-04-24 01:12:06 +00:00
|
|
|
offset = (loff_t) off;
|
2011-04-22 17:20:46 +00:00
|
|
|
if (_llseek (fd, offset >> 32, offset & 0xffffffff, &result, SEEK_SET))
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
|
|
|
name, strerror (errno));
|
2011-04-22 17:20:46 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
grub_err_t
|
2011-04-24 01:12:06 +00:00
|
|
|
grub_util_fd_seek (int fd, const char *name, grub_uint64_t off)
|
2011-04-22 17:20:46 +00:00
|
|
|
{
|
2011-04-24 01:12:06 +00:00
|
|
|
off_t offset = (off_t) off;
|
2011-04-22 17:20:46 +00:00
|
|
|
|
|
|
|
if (lseek (fd, offset, SEEK_SET) != offset)
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"),
|
|
|
|
name, strerror (errno));
|
2011-04-22 17:20:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
const char *
|
|
|
|
grub_hostdisk_os_dev_to_grub_drive (const char *os_disk, int add)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (map); i++)
|
|
|
|
if (! map[i].device)
|
|
|
|
break;
|
|
|
|
else if (strcmp (map[i].device, os_disk) == 0)
|
|
|
|
return map[i].drive;
|
|
|
|
|
|
|
|
if (!add)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (i == ARRAY_SIZE (map))
|
2012-02-26 16:28:05 +00:00
|
|
|
/* TRANSLATORS: it refers to the lack of free slots. */
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_error ("%s", _("device count exceeds limit"));
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
map[i].device = xstrdup (os_disk);
|
|
|
|
map[i].drive = xmalloc (sizeof ("hostdisk/") + strlen (os_disk));
|
|
|
|
strcpy (map[i].drive, "hostdisk/");
|
|
|
|
strcpy (map[i].drive + sizeof ("hostdisk/") - 1, os_disk);
|
|
|
|
map[i].device_map = 0;
|
|
|
|
|
|
|
|
return map[i].drive;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static int
|
2012-04-18 21:18:51 +00:00
|
|
|
open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
|
|
|
grub_disk_addr_t *max)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2010-03-03 10:44:12 +00:00
|
|
|
struct grub_util_biosdisk_data *data = disk->data;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
*max = ~0ULL;
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
#ifdef O_SYNC
|
|
|
|
flags |= O_SYNC;
|
|
|
|
#endif
|
|
|
|
#ifdef O_FSYNC
|
|
|
|
flags |= O_FSYNC;
|
|
|
|
#endif
|
2008-08-29 19:55:23 +00:00
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#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];
|
2010-02-06 17:43:37 +00:00
|
|
|
grub_disk_addr_t part_start = 0;
|
|
|
|
|
|
|
|
part_start = grub_partition_get_start (disk->partition);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
strcpy (dev, map[disk->id].device);
|
2012-04-18 21:18:51 +00:00
|
|
|
if (disk->partition
|
2010-02-02 23:49:35 +00:00
|
|
|
&& strncmp (map[disk->id].device, "/dev/", 5) == 0)
|
2012-04-18 21:18:51 +00:00
|
|
|
{
|
|
|
|
if (sector >= part_start)
|
|
|
|
is_partition = linux_find_partition (dev, part_start);
|
|
|
|
else
|
|
|
|
*max = part_start - sector;
|
|
|
|
}
|
|
|
|
|
|
|
|
reopen:
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-03-03 20:03:50 +00:00
|
|
|
if (data->dev && strcmp (data->dev, dev) == 0 &&
|
|
|
|
data->access_mode == (flags & O_ACCMODE))
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2010-03-03 10:44:12 +00:00
|
|
|
grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
|
|
|
|
fd = data->fd;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
2010-03-03 10:44:12 +00:00
|
|
|
else
|
|
|
|
{
|
2010-03-03 20:03:50 +00:00
|
|
|
free (data->dev);
|
|
|
|
if (data->fd != -1)
|
2011-04-08 09:49:38 +00:00
|
|
|
{
|
|
|
|
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
|
|
|
{
|
|
|
|
fsync (data->fd);
|
|
|
|
#ifdef __linux__
|
2011-04-10 14:08:58 +00:00
|
|
|
if (data->is_disk)
|
|
|
|
ioctl (data->fd, BLKFLSBUF, 0);
|
2011-04-08 09:49:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
close (data->fd);
|
2011-05-09 16:48:50 +00:00
|
|
|
data->fd = -1;
|
2011-04-08 09:49:38 +00:00
|
|
|
}
|
2010-03-03 20:03:50 +00:00
|
|
|
|
2010-03-03 10:44:12 +00:00
|
|
|
/* Open the partition. */
|
|
|
|
grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
|
|
|
|
fd = open (dev, flags);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
|
|
|
dev, strerror (errno));
|
2010-03-03 10:44:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2010-03-03 10:44:12 +00:00
|
|
|
data->dev = xstrdup (dev);
|
2010-03-03 20:03:50 +00:00
|
|
|
data->access_mode = (flags & O_ACCMODE);
|
2010-03-03 10:44:12 +00:00
|
|
|
data->fd = fd;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
2008-06-07 04:30:22 +00:00
|
|
|
|
|
|
|
if (is_partition)
|
2012-04-18 21:18:51 +00:00
|
|
|
{
|
|
|
|
*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;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
#else /* ! __linux__ */
|
2009-04-14 07:01:34 +00:00
|
|
|
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
int sysctl_flags, sysctl_oldflags;
|
2009-07-01 23:58:30 +00:00
|
|
|
size_t sysctl_size = sizeof (sysctl_flags);
|
2009-04-14 07:01:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-03-03 20:03:50 +00:00
|
|
|
if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 &&
|
|
|
|
data->access_mode == (flags & O_ACCMODE))
|
2010-03-03 10:44:12 +00:00
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev);
|
|
|
|
fd = data->fd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-03 20:03:50 +00:00
|
|
|
free (data->dev);
|
|
|
|
if (data->fd != -1)
|
2011-04-08 09:49:38 +00:00
|
|
|
{
|
|
|
|
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
|
|
|
{
|
|
|
|
fsync (data->fd);
|
|
|
|
#ifdef __linux__
|
2011-04-10 14:08:58 +00:00
|
|
|
if (data->is_disk)
|
|
|
|
ioctl (data->fd, BLKFLSBUF, 0);
|
2011-04-08 09:49:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
close (data->fd);
|
2011-05-09 16:48:50 +00:00
|
|
|
data->fd = -1;
|
2011-04-08 09:49:38 +00:00
|
|
|
}
|
2010-03-03 20:03:50 +00:00
|
|
|
|
2010-03-03 10:44:12 +00:00
|
|
|
fd = open (map[disk->id].device, flags);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
data->dev = xstrdup (map[disk->id].device);
|
2010-03-03 20:03:50 +00:00
|
|
|
data->access_mode = (flags & O_ACCMODE);
|
2010-03-03 10:44:12 +00:00
|
|
|
data->fd = fd;
|
|
|
|
}
|
|
|
|
}
|
2009-04-14 07:01:34 +00:00
|
|
|
|
|
|
|
#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 -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-20 20:03:18 +00:00
|
|
|
#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
|
|
|
|
|
2004-03-14 17:48:25 +00:00
|
|
|
if (fd < 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
|
|
|
map[disk->id].device, strerror (errno));
|
2003-01-02 20:12:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! __linux__ */
|
|
|
|
|
2010-04-10 15:07:33 +00:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
configure_device_driver (fd);
|
|
|
|
#endif /* defined(__NetBSD__) */
|
|
|
|
|
2011-04-24 01:12:06 +00:00
|
|
|
if (grub_util_fd_seek (fd, map[disk->id].device,
|
|
|
|
sector << disk->log_sector_size))
|
2011-04-22 17:20:46 +00:00
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
/* Read LEN bytes from FD in BUF. Return less than or equal to zero if an
|
|
|
|
error occurs, otherwise return LEN. */
|
2011-04-22 17:20:46 +00:00
|
|
|
ssize_t
|
|
|
|
grub_util_fd_read (int fd, char *buf, size_t len)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
ssize_t size = len;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t ret = read (fd, buf, len);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
len -= ret;
|
|
|
|
buf += ret;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write LEN bytes from BUF to FD. Return less than or equal to zero if an
|
|
|
|
error occurs, otherwise return LEN. */
|
2012-01-29 17:16:48 +00:00
|
|
|
ssize_t
|
|
|
|
grub_util_fd_write (int fd, const char *buf, size_t len)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
ssize_t size = len;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t ret = write (fd, buf, len);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
len -= ret;
|
|
|
|
buf += ret;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, char *buf)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
while (size)
|
2010-02-02 23:49:35 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
int fd;
|
|
|
|
grub_disk_addr_t max = ~0ULL;
|
|
|
|
fd = open_device (disk, sector, O_RDONLY, &max);
|
|
|
|
if (fd < 0)
|
|
|
|
return grub_errno;
|
2010-02-02 23:49:35 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
if (max > size)
|
|
|
|
max = size;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef __linux__
|
2012-04-18 21:18:51 +00:00
|
|
|
if (sector == 0 && max > 1)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
/* Work around a bug in Linux ez remapping. Linux remaps all
|
|
|
|
sectors that are read together with the MBR in one read. It
|
|
|
|
should only remap the MBR, so we split the read in two
|
|
|
|
parts. -jochen */
|
|
|
|
if (grub_util_fd_read (fd, buf, (1 << disk->log_sector_size))
|
|
|
|
!= (1 << disk->log_sector_size))
|
|
|
|
return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
|
|
|
|
map[disk->id].device, strerror (errno));
|
|
|
|
|
|
|
|
buf += (1 << disk->log_sector_size);
|
|
|
|
size--;
|
|
|
|
max--;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
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));
|
|
|
|
size -= max;
|
|
|
|
buf += (max << disk->log_sector_size);
|
|
|
|
sector += max;
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, const char *buf)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
while (size)
|
2010-02-02 23:49:35 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
int fd;
|
|
|
|
grub_disk_addr_t max = ~0ULL;
|
2012-04-19 18:35:06 +00:00
|
|
|
fd = open_device (disk, sector, O_WRONLY, &max);
|
2012-04-18 21:18:51 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return grub_errno;
|
2010-02-02 23:49:35 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
if (max > size)
|
|
|
|
max = size;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
if (sector == 0 && max > 1)
|
|
|
|
{
|
|
|
|
/* Work around a bug in Linux ez remapping. Linux remaps all
|
|
|
|
sectors that are write together with the MBR in one write. It
|
|
|
|
should only remap the MBR, so we split the write in two
|
|
|
|
parts. -jochen */
|
|
|
|
if (grub_util_fd_write (fd, buf, (1 << disk->log_sector_size))
|
|
|
|
!= (1 << disk->log_sector_size))
|
2012-04-18 21:48:52 +00:00
|
|
|
return grub_error (GRUB_ERR_WRITE_ERROR,
|
|
|
|
N_("cannot write to `%s': %s"),
|
2012-04-18 21:18:51 +00:00
|
|
|
map[disk->id].device, strerror (errno));
|
|
|
|
|
|
|
|
buf += (1 << disk->log_sector_size);
|
|
|
|
size--;
|
|
|
|
max--;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
if (grub_util_fd_write (fd, buf, max << disk->log_sector_size)
|
|
|
|
!= (ssize_t) (max << disk->log_sector_size))
|
2012-04-18 21:48:52 +00:00
|
|
|
return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to `%s': %s"),
|
2012-04-18 21:18:51 +00:00
|
|
|
map[disk->id].device, strerror (errno));
|
|
|
|
size -= max;
|
|
|
|
buf += (max << disk->log_sector_size);
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:39:14 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_util_biosdisk_flush (struct grub_disk *disk)
|
|
|
|
{
|
|
|
|
struct grub_util_biosdisk_data *data = disk->data;
|
|
|
|
|
|
|
|
if (disk->dev->id != GRUB_DISK_DEVICE_BIOSDISK_ID)
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
if (data->fd == -1)
|
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
grub_disk_addr_t max;
|
|
|
|
data->fd = open_device (disk, 0, O_RDONLY, &max);
|
2011-04-19 20:39:14 +00:00
|
|
|
if (data->fd < 0)
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
fsync (data->fd);
|
|
|
|
#ifdef __linux__
|
|
|
|
if (data->is_disk)
|
|
|
|
ioctl (data->fd, BLKFLSBUF, 0);
|
|
|
|
#endif
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-03-03 10:44:12 +00:00
|
|
|
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)
|
2011-04-08 09:49:38 +00:00
|
|
|
{
|
|
|
|
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
2011-04-19 20:39:14 +00:00
|
|
|
grub_util_biosdisk_flush (disk);
|
2011-04-08 09:49:38 +00:00
|
|
|
close (data->fd);
|
|
|
|
}
|
2010-03-03 10:44:12 +00:00
|
|
|
free (data);
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static struct grub_disk_dev grub_util_biosdisk_dev =
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-01-29 20:49:44 +00:00
|
|
|
.name = "hostdisk",
|
|
|
|
.id = GRUB_DISK_DEVICE_HOSTDISK_ID,
|
2004-04-04 13:46:03 +00:00
|
|
|
.iterate = grub_util_biosdisk_iterate,
|
|
|
|
.open = grub_util_biosdisk_open,
|
2010-03-03 10:44:12 +00:00
|
|
|
.close = grub_util_biosdisk_close,
|
2004-04-04 13:46:03 +00:00
|
|
|
.read = grub_util_biosdisk_read,
|
|
|
|
.write = grub_util_biosdisk_write,
|
2003-01-02 20:12:33 +00:00
|
|
|
.next = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_device_map (const char *dev_map)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buf[1024]; /* XXX */
|
|
|
|
int lineno = 0;
|
2008-04-07 15:00:27 +00:00
|
|
|
struct stat st;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-02-22 04:16:26 +00:00
|
|
|
auto void show_error (const char *msg)
|
|
|
|
__attribute__ ((noreturn));
|
|
|
|
void __attribute__ ((noreturn)) show_error (const char *msg)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("%s:%d: %s", dev_map, lineno, msg);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-12-23 17:19:16 +00:00
|
|
|
if (dev_map[0] == '\0')
|
|
|
|
{
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_info ("no device.map");
|
2011-12-23 17:19:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
fp = fopen (dev_map, "r");
|
|
|
|
if (! fp)
|
2009-11-23 20:30:56 +00:00
|
|
|
{
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno));
|
2009-11-23 20:30:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
while (fgets (buf, sizeof (buf), fp))
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
char *e;
|
2012-05-01 13:02:34 +00:00
|
|
|
char *drive_e, *drive_p;
|
2003-01-02 20:12:33 +00:00
|
|
|
int drive;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
lineno++;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
/* Skip leading spaces. */
|
2012-03-02 10:18:11 +00:00
|
|
|
while (*p && grub_isspace (*p))
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
/* If the first character is `#' or NUL, skip this line. */
|
|
|
|
if (*p == '\0' || *p == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p != '(')
|
2012-02-08 18:26:01 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
tmp = xasprintf (_("missing `%c' symbol"), '(');
|
|
|
|
show_error (tmp);
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
p++;
|
2008-05-30 10:50:00 +00:00
|
|
|
/* Find a free slot. */
|
2008-08-28 14:36:18 +00:00
|
|
|
drive = find_free_slot ();
|
2008-05-30 10:50:00 +00:00
|
|
|
if (drive < 0)
|
2012-02-26 16:28:05 +00:00
|
|
|
show_error (_("device count exceeds limit"));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
e = p;
|
2003-01-02 20:12:33 +00:00
|
|
|
p = strchr (p, ')');
|
|
|
|
if (! p)
|
2012-02-08 18:26:01 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
tmp = xasprintf (_("missing `%c' symbol"), ')');
|
|
|
|
show_error (tmp);
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-05-01 13:02:34 +00:00
|
|
|
map[drive].drive = 0;
|
|
|
|
if ((e[0] == 'f' || e[0] == 'h' || e[0] == 'c') && e[1] == 'd')
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
for (ptr = e + 2; ptr < p; ptr++)
|
|
|
|
if (!grub_isdigit (*ptr))
|
|
|
|
break;
|
|
|
|
if (ptr == p)
|
|
|
|
{
|
|
|
|
map[drive].drive = xmalloc (p - e + sizeof ('\0'));
|
|
|
|
strncpy (map[drive].drive, e, p - e + sizeof ('\0'));
|
|
|
|
map[drive].drive[p - e] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drive_e = e;
|
|
|
|
drive_p = p;
|
2011-12-23 17:19:16 +00:00
|
|
|
map[drive].device_map = 1;
|
2008-05-30 10:50:00 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
|
|
|
/* Skip leading spaces. */
|
2012-03-02 10:18:11 +00:00
|
|
|
while (*p && grub_isspace (*p))
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '\0')
|
2012-02-08 18:26:01 +00:00
|
|
|
show_error (_("filename expected"));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
/* NUL-terminate the filename. */
|
|
|
|
e = p;
|
2012-03-02 10:18:11 +00:00
|
|
|
while (*e && ! grub_isspace (*e))
|
2003-01-02 20:12:33 +00:00
|
|
|
e++;
|
|
|
|
*e = '\0';
|
|
|
|
|
2009-03-21 07:48:28 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
(void) st;
|
|
|
|
if (grub_util_get_disk_size (p) == -1LL)
|
|
|
|
#else
|
2008-04-07 15:00:27 +00:00
|
|
|
if (stat (p, &st) == -1)
|
2009-03-21 07:48:28 +00:00
|
|
|
#endif
|
2008-04-07 15:00:27 +00:00
|
|
|
{
|
2008-06-26 16:15:52 +00:00
|
|
|
free (map[drive].drive);
|
|
|
|
map[drive].drive = NULL;
|
2008-04-07 15:00:27 +00:00
|
|
|
grub_util_info ("Cannot stat `%s', skipping", p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/* On Linux, the devfs uses symbolic links horribly, and that
|
|
|
|
confuses the interface very much, so use realpath to expand
|
2010-09-17 10:00:37 +00:00
|
|
|
symbolic links. Leave /dev/mapper/ alone, though. */
|
|
|
|
if (strncmp (p, "/dev/mapper/", 12) != 0)
|
|
|
|
{
|
|
|
|
map[drive].device = xmalloc (PATH_MAX);
|
|
|
|
if (! realpath (p, map[drive].device))
|
2012-02-05 10:23:47 +00:00
|
|
|
grub_util_error (_("failed to get canonical path of %s"), p);
|
2010-09-17 10:00:37 +00:00
|
|
|
}
|
|
|
|
else
|
2003-01-02 20:12:33 +00:00
|
|
|
#endif
|
2010-09-17 10:00:37 +00:00
|
|
|
map[drive].device = xstrdup (p);
|
2012-05-01 13:02:34 +00:00
|
|
|
if (!map[drive].drive)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
map[drive].drive = xmalloc (sizeof ("hostdisk/") + strlen (p));
|
|
|
|
memcpy (map[drive].drive, "hostdisk/", sizeof ("hostdisk/") - 1);
|
|
|
|
strcpy (map[drive].drive + sizeof ("hostdisk/") - 1, p);
|
|
|
|
c = *drive_p;
|
|
|
|
*drive_p = 0;
|
|
|
|
grub_util_warn (_("the drive name %s in device.map is incorrect. "
|
|
|
|
"Using %s instead. "
|
|
|
|
"Please use the form [hfc]d[0-9]* "
|
|
|
|
"(E.g. `hd0' or `cd')"),
|
|
|
|
drive_e, map[drive].drive);
|
|
|
|
*drive_p = c;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_init (const char *dev_map)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
read_device_map (dev_map);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_dev_register (&grub_util_biosdisk_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_fini (void)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
2008-05-30 10:50:00 +00:00
|
|
|
{
|
|
|
|
if (map[i].drive)
|
|
|
|
free (map[i].drive);
|
|
|
|
if (map[i].device)
|
|
|
|
free (map[i].device);
|
|
|
|
map[i].drive = map[i].device = NULL;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_dev_unregister (&grub_util_biosdisk_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 17:19:16 +00:00
|
|
|
const char *
|
|
|
|
grub_util_biosdisk_get_compatibility_hint (grub_disk_t disk)
|
|
|
|
{
|
|
|
|
if (disk->dev != &grub_util_biosdisk_dev || map[disk->id].device_map)
|
|
|
|
return disk->name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-18 12:02:13 +00:00
|
|
|
const char *
|
|
|
|
grub_util_biosdisk_get_osdev (grub_disk_t disk)
|
|
|
|
{
|
2011-09-28 12:07:53 +00:00
|
|
|
if (disk->dev != &grub_util_biosdisk_dev)
|
|
|
|
return 0;
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
return map[disk->id].device;
|
2010-09-15 13:30:43 +00:00
|
|
|
}
|