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
|
|
|
# ifndef MAJOR
|
|
|
|
# ifndef MINORBITS
|
|
|
|
# define MINORBITS 8
|
|
|
|
# endif /* ! MINORBITS */
|
|
|
|
# define MAJOR(dev) ((unsigned) ((dev) >> MINORBITS))
|
|
|
|
# endif /* ! MAJOR */
|
|
|
|
# ifndef FLOPPY_MAJOR
|
|
|
|
# define FLOPPY_MAJOR 2
|
|
|
|
# endif /* ! FLOPPY_MAJOR */
|
|
|
|
# ifndef LOOP_MAJOR
|
|
|
|
# define LOOP_MAJOR 7
|
|
|
|
# endif /* ! LOOP_MAJOR */
|
|
|
|
#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 */
|
|
|
|
# define MAJOR(dev) ((unsigned) ((dev) >> 16))
|
|
|
|
# define FLOPPY_MAJOR 2
|
|
|
|
#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>
|
2011-03-26 11:49:34 +00:00
|
|
|
# define MAJOR(dev) major(dev)
|
|
|
|
# define FLOPPY_MAJOR 2
|
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 */
|
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>
|
2010-09-22 21:32:58 +00:00
|
|
|
# ifndef FLOPPY_MAJOR
|
|
|
|
# define FLOPPY_MAJOR 2
|
|
|
|
# endif /* ! FLOPPY_MAJOR */
|
2010-04-10 15:07:33 +00:00
|
|
|
# ifndef RAW_FLOPPY_MAJOR
|
|
|
|
# define RAW_FLOPPY_MAJOR 9
|
|
|
|
# endif /* ! RAW_FLOPPY_MAJOR */
|
|
|
|
#endif /* defined(__NetBSD__) */
|
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
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
|
|
|
|
grub_util_get_fd_sectors (int fd, unsigned *log_secsize)
|
|
|
|
{
|
|
|
|
# if defined(__NetBSD__)
|
|
|
|
struct disklabel label;
|
2011-11-08 11:38:30 +00:00
|
|
|
# elif defined (__sun__)
|
|
|
|
struct dk_minfo minfo;
|
|
|
|
#else
|
2011-04-25 12:52:07 +00:00
|
|
|
unsigned long long nr;
|
|
|
|
# endif
|
|
|
|
unsigned sector_size, log_sector_size;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat (fd, &st) < 0)
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_util_error (_("fstat failed"));
|
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;
|
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__)
|
|
|
|
return nr;
|
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
return label.d_secperunit;
|
2011-11-08 11:38:30 +00:00
|
|
|
# elif defined (__sun__)
|
|
|
|
return minfo.dki_capacity;
|
2011-04-25 12:52:07 +00:00
|
|
|
# else
|
|
|
|
if (nr & ((1 << log_sector_size) - 1))
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_util_error (_("unaligned device size"));
|
2011-04-25 12:52:07 +00:00
|
|
|
|
|
|
|
return (nr >> log_sector_size);
|
|
|
|
# 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;
|
|
|
|
|
|
|
|
return st.st_size >> 9;
|
|
|
|
}
|
|
|
|
#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)
|
2010-02-07 16:30:16 +00:00
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot open `%s' while attempting to get disk size", map[drive].device);
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
disk->total_sectors = grub_util_get_fd_sectors (fd, &disk->log_sector_size);
|
|
|
|
|
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
|
|
|
|
2007-06-04 18:58:17 +00:00
|
|
|
grub_util_info ("the size of %s is %llu", 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)
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_util_error (_("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)
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_util_error (_("couldn't open geom part"));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_disk_addr_t
|
|
|
|
find_partition_start (const char *dev)
|
|
|
|
{
|
|
|
|
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__)
|
2010-01-25 17:47:51 +00:00
|
|
|
static grub_disk_addr_t
|
|
|
|
find_partition_start (const char *dev)
|
|
|
|
{
|
|
|
|
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) */
|
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)
|
|
|
|
{
|
2010-05-20 00:05:43 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
2010-09-20 11:12:33 +00:00
|
|
|
# if !defined(HAVE_DIOCGDINFO)
|
2010-05-20 00:05:43 +00:00
|
|
|
"cannot open `%s' while attempting to get disk geometry", dev);
|
2010-09-20 11:12:33 +00:00
|
|
|
# else /* defined(HAVE_DIOCGDINFO) */
|
2010-06-02 22:47:22 +00:00
|
|
|
"cannot open `%s' while attempting to get disk label", dev);
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* !defined(HAVE_DIOCGDINFO) */
|
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);
|
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;
|
|
|
|
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
|
|
|
|
2010-01-25 17:47:51 +00:00
|
|
|
start = find_partition_start (real_dev);
|
|
|
|
/* 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))
|
|
|
|
{
|
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot seek `%s'", name);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot seek `%s'", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static int
|
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
|
|
|
open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags)
|
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
|
|
|
|
|
|
|
#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);
|
2010-02-06 17:47:17 +00:00
|
|
|
if (disk->partition && sector >= part_start
|
2010-02-02 23:49:35 +00:00
|
|
|
&& strncmp (map[disk->id].device, "/dev/", 5) == 0)
|
2010-02-06 17:43:37 +00:00
|
|
|
is_partition = linux_find_partition (dev, part_start);
|
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)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", dev);
|
|
|
|
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)
|
2010-02-06 17:43:37 +00:00
|
|
|
sector -= part_start;
|
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
|
|
|
{
|
2008-08-26 19:09:07 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s' in open_device()", map[disk->id].device);
|
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. */
|
|
|
|
static ssize_t
|
|
|
|
nwrite (int fd, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-02-02 23:49:35 +00:00
|
|
|
/* Split pre-partition and partition reads. */
|
|
|
|
if (disk->partition && sector < disk->partition->start
|
|
|
|
&& sector + size > disk->partition->start)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
err = grub_util_biosdisk_read (disk, sector,
|
|
|
|
disk->partition->start - sector,
|
|
|
|
buf);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return grub_util_biosdisk_read (disk, disk->partition->start,
|
|
|
|
size - (disk->partition->start - sector),
|
|
|
|
buf + ((disk->partition->start - sector)
|
|
|
|
<< GRUB_DISK_SECTOR_BITS));
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
fd = open_device (disk, sector, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
if (sector == 0 && size > 1)
|
|
|
|
{
|
2007-12-30 08:52:06 +00:00
|
|
|
/* Work around a bug in Linux ez remapping. Linux remaps all
|
2003-01-02 20:12:33 +00:00
|
|
|
sectors that are read together with the MBR in one read. It
|
2009-06-10 21:04:23 +00:00
|
|
|
should only remap the MBR, so we split the read in two
|
2003-01-02 20:12:33 +00:00
|
|
|
parts. -jochen */
|
2011-04-24 01:12:06 +00:00
|
|
|
if (grub_util_fd_read (fd, buf, (1 << disk->log_sector_size))
|
2011-03-29 00:02:55 +00:00
|
|
|
!= (1 << disk->log_sector_size))
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2008-05-30 10:50:00 +00:00
|
|
|
grub_error (GRUB_ERR_READ_ERROR, "cannot read `%s'", map[disk->id].device);
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-03-29 00:02:55 +00:00
|
|
|
buf += (1 << disk->log_sector_size);
|
2003-01-02 20:12:33 +00:00
|
|
|
size--;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-04-24 01:12:06 +00:00
|
|
|
if (grub_util_fd_read (fd, buf, size << disk->log_sector_size)
|
2011-03-29 00:02:55 +00:00
|
|
|
!= (ssize_t) (size << disk->log_sector_size))
|
2008-05-30 10:50:00 +00:00
|
|
|
grub_error (GRUB_ERR_READ_ERROR, "cannot read from `%s'", map[disk->id].device);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
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
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-02-02 23:49:35 +00:00
|
|
|
/* Split pre-partition and partition writes. */
|
|
|
|
if (disk->partition && sector < disk->partition->start
|
|
|
|
&& sector + size > disk->partition->start)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
err = grub_util_biosdisk_write (disk, sector,
|
|
|
|
disk->partition->start - sector,
|
|
|
|
buf);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return grub_util_biosdisk_write (disk, disk->partition->start,
|
|
|
|
size - (disk->partition->start - sector),
|
|
|
|
buf + ((disk->partition->start - sector)
|
|
|
|
<< GRUB_DISK_SECTOR_BITS));
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
fd = open_device (disk, sector, O_WRONLY);
|
|
|
|
if (fd < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-03-29 00:02:55 +00:00
|
|
|
if (nwrite (fd, buf, size << disk->log_sector_size)
|
|
|
|
!= (ssize_t) (size << disk->log_sector_size))
|
2008-05-30 10:50:00 +00:00
|
|
|
grub_error (GRUB_ERR_WRITE_ERROR, "cannot write to `%s'", map[disk->id].device);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
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)
|
|
|
|
{
|
|
|
|
data->fd = open_device (disk, 0, O_RDONLY);
|
|
|
|
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
|
|
|
{
|
|
|
|
.name = "biosdisk",
|
2004-08-21 13:54:22 +00:00
|
|
|
.id = GRUB_DISK_DEVICE_BIOSDISK_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
|
|
|
|
2008-04-07 15:00:27 +00:00
|
|
|
auto void show_error (const char *msg);
|
2003-01-02 20:12:33 +00:00
|
|
|
void show_error (const char *msg)
|
|
|
|
{
|
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')
|
|
|
|
{
|
|
|
|
grub_util_info (_("no device.map"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
fp = fopen (dev_map, "r");
|
|
|
|
if (! fp)
|
2009-11-23 20:30:56 +00:00
|
|
|
{
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_info (_("cannot open `%s'"), dev_map);
|
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;
|
|
|
|
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. */
|
|
|
|
while (*p && isspace (*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* If the first character is `#' or NUL, skip this line. */
|
|
|
|
if (*p == '\0' || *p == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p != '(')
|
2011-11-11 20:44:56 +00:00
|
|
|
show_error (_("No open parenthesis found"));
|
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)
|
2011-11-11 20:44:56 +00:00
|
|
|
show_error (_("Map table size exceeded"));
|
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)
|
2011-11-11 20:44:56 +00:00
|
|
|
show_error (_("No close parenthesis found"));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
map[drive].drive = xmalloc (p - e + sizeof ('\0'));
|
|
|
|
strncpy (map[drive].drive, e, p - e + sizeof ('\0'));
|
|
|
|
map[drive].drive[p - e] = '\0';
|
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. */
|
|
|
|
while (*p && isspace (*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '\0')
|
2011-11-11 20:44:56 +00:00
|
|
|
show_error (_("No filename found"));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
/* NUL-terminate the filename. */
|
|
|
|
e = p;
|
|
|
|
while (*e && ! isspace (*e))
|
|
|
|
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))
|
2011-11-11 20:44:56 +00:00
|
|
|
grub_util_error (_("cannot get the real 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);
|
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
|
|
|
}
|
|
|
|
|
2010-06-13 00:36:39 +00:00
|
|
|
/*
|
|
|
|
* Note: we do not use the new partition naming scheme as dos_part does not
|
2010-09-20 11:12:33 +00:00
|
|
|
* necessarily correspond to an msdos partition.
|
2010-06-13 00:36:39 +00:00
|
|
|
*/
|
2003-01-02 20:12:33 +00:00
|
|
|
static char *
|
|
|
|
make_device_name (int drive, int dos_part, int bsd_part)
|
|
|
|
{
|
2011-11-08 11:15:57 +00:00
|
|
|
char *ret, *ptr, *end;
|
|
|
|
const char *iptr;
|
|
|
|
|
|
|
|
ret = xmalloc (strlen (map[drive].drive) * 2
|
|
|
|
+ sizeof (",XXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
|
|
",XXXXXXXXXXXXXXXXXXXXXXXXXX"));
|
|
|
|
end = (ret + strlen (map[drive].drive) * 2
|
|
|
|
+ sizeof (",XXXXXXXXXXXXXXXXXXXXXXXXXX"
|
|
|
|
",XXXXXXXXXXXXXXXXXXXXXXXXXX"));
|
|
|
|
ptr = ret;
|
|
|
|
for (iptr = map[drive].drive; *iptr; iptr++)
|
|
|
|
{
|
|
|
|
if (*iptr == ',')
|
|
|
|
*ptr++ = '\\';
|
|
|
|
*ptr++ = *iptr;
|
|
|
|
}
|
|
|
|
*ptr = 0;
|
2009-04-13 23:01:54 +00:00
|
|
|
if (dos_part >= 0)
|
2011-11-08 11:15:57 +00:00
|
|
|
snprintf (ptr, end - ptr, ",%d", dos_part + 1);
|
|
|
|
ptr += strlen (ptr);
|
2009-04-13 23:01:54 +00:00
|
|
|
if (bsd_part >= 0)
|
2011-11-08 11:15:57 +00:00
|
|
|
snprintf (ptr, end - ptr, ",%d", bsd_part + 1);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-11-23 20:30:56 +00:00
|
|
|
return ret;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 15:51:50 +00:00
|
|
|
#ifdef HAVE_DEVICE_MAPPER
|
|
|
|
static int
|
|
|
|
grub_util_get_dm_node_linear_info (const char *dev,
|
|
|
|
int *maj, int *min)
|
|
|
|
{
|
|
|
|
struct dm_task *dmt;
|
|
|
|
void *next = NULL;
|
|
|
|
uint64_t length, start;
|
|
|
|
char *target, *params;
|
|
|
|
char *ptr;
|
|
|
|
int major, minor;
|
|
|
|
|
|
|
|
dmt = dm_task_create(DM_DEVICE_TABLE);
|
|
|
|
if (!dmt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!dm_task_set_name(dmt, dev))
|
|
|
|
return 0;
|
|
|
|
dm_task_no_open_count(dmt);
|
|
|
|
if (!dm_task_run(dmt))
|
|
|
|
return 0;
|
|
|
|
next = dm_get_next_target(dmt, next, &start, &length,
|
|
|
|
&target, ¶ms);
|
|
|
|
if (grub_strcmp (target, "linear") != 0)
|
|
|
|
return 0;
|
|
|
|
major = grub_strtoul (params, &ptr, 10);
|
|
|
|
if (grub_errno)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (*ptr != ':')
|
|
|
|
return 0;
|
|
|
|
ptr++;
|
|
|
|
minor = grub_strtoul (ptr, 0, 10);
|
|
|
|
if (grub_errno)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (maj)
|
|
|
|
*maj = major;
|
|
|
|
if (min)
|
|
|
|
*min = minor;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static char *
|
2010-01-25 17:47:51 +00:00
|
|
|
convert_system_partition_to_system_disk (const char *os_dev, struct stat *st)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
2008-05-16 21:39:52 +00:00
|
|
|
char *path = xmalloc (PATH_MAX);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! realpath (os_dev, path))
|
2009-11-23 20:30:56 +00:00
|
|
|
return NULL;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
if (strncmp ("/dev/", path, 5) == 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2008-05-16 21:39:52 +00:00
|
|
|
char *p = path + 5;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2008-01-05 12:02:03 +00:00
|
|
|
/* If this is an IDE disk. */
|
|
|
|
if (strncmp ("ide/", p, 4) == 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2008-01-05 12:02:03 +00:00
|
|
|
p = strstr (p, "part");
|
|
|
|
if (p)
|
|
|
|
strcpy (p, "disc");
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-01-05 12:02:03 +00:00
|
|
|
return path;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-01-05 12:02:03 +00:00
|
|
|
/* If this is a SCSI disk. */
|
|
|
|
if (strncmp ("scsi/", p, 5) == 0)
|
|
|
|
{
|
|
|
|
p = strstr (p, "part");
|
|
|
|
if (p)
|
|
|
|
strcpy (p, "disc");
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-01-05 12:02:03 +00:00
|
|
|
return path;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
/* If this is a DAC960 disk. */
|
|
|
|
if (strncmp ("rd/c", p, 4) == 0)
|
|
|
|
{
|
|
|
|
/* /dev/rd/c[0-9]+d[0-9]+(p[0-9]+)? */
|
|
|
|
p = strchr (p, 'p');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-07-29 14:11:50 +00:00
|
|
|
/* If this is a Mylex AcceleRAID Array. */
|
|
|
|
if (strncmp ("rs/c", p, 4) == 0)
|
|
|
|
{
|
|
|
|
/* /dev/rd/c[0-9]+d[0-9]+(p[0-9]+)? */
|
|
|
|
p = strchr (p, 'p');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2008-05-07 20:28:26 +00:00
|
|
|
/* If this is a CCISS disk. */
|
|
|
|
if (strncmp ("cciss/c", p, sizeof ("cciss/c") - 1) == 0)
|
|
|
|
{
|
|
|
|
/* /dev/cciss/c[0-9]+d[0-9]+(p[0-9]+)? */
|
|
|
|
p = strchr (p, 'p');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-06-04 11:20:36 +00:00
|
|
|
/* If this is a Compaq Intelligent Drive Array. */
|
|
|
|
if (strncmp ("ida/c", p, sizeof ("ida/c") - 1) == 0)
|
|
|
|
{
|
|
|
|
/* /dev/ida/c[0-9]+d[0-9]+(p[0-9]+)? */
|
|
|
|
p = strchr (p, 'p');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-06-16 20:45:21 +00:00
|
|
|
/* If this is an I2O disk. */
|
|
|
|
if (strncmp ("i2o/hd", p, sizeof ("i2o/hd") - 1) == 0)
|
|
|
|
{
|
|
|
|
/* /dev/i2o/hd[a-z]([0-9]+)? */
|
|
|
|
p[sizeof ("i2o/hda") - 1] = '\0';
|
|
|
|
return path;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-05-28 19:53:25 +00:00
|
|
|
/* If this is a MultiMediaCard (MMC). */
|
|
|
|
if (strncmp ("mmcblk", p, sizeof ("mmcblk") - 1) == 0)
|
|
|
|
{
|
|
|
|
/* /dev/mmcblk[0-9]+(p[0-9]+)? */
|
|
|
|
p = strchr (p, 'p');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-09-13 11:09:58 +00:00
|
|
|
if (strncmp ("md", p, 2) == 0
|
|
|
|
&& p[2] >= '0' && p[2] <= '9')
|
|
|
|
{
|
|
|
|
char *ptr = p + 2;
|
|
|
|
while (*ptr >= '0' && *ptr <= '9')
|
|
|
|
ptr++;
|
|
|
|
*ptr = 0;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2008-05-09 12:13:03 +00:00
|
|
|
/* If this is an IDE, SCSI or Virtio disk. */
|
2009-04-30 13:21:14 +00:00
|
|
|
if (strncmp ("vdisk", p, 5) == 0
|
|
|
|
&& p[5] >= 'a' && p[5] <= 'z')
|
|
|
|
{
|
|
|
|
/* /dev/vdisk[a-z][0-9]* */
|
|
|
|
p[6] = '\0';
|
|
|
|
return path;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
if ((strncmp ("hd", p, 2) == 0
|
2008-05-09 12:13:03 +00:00
|
|
|
|| strncmp ("vd", p, 2) == 0
|
2003-01-02 20:12:33 +00:00
|
|
|
|| strncmp ("sd", p, 2) == 0)
|
|
|
|
&& p[2] >= 'a' && p[2] <= 'z')
|
|
|
|
{
|
2010-11-06 23:10:49 +00:00
|
|
|
char *pp = p + 2;
|
|
|
|
while (*pp >= 'a' && *pp <= 'z')
|
|
|
|
pp++;
|
|
|
|
/* /dev/[hsv]d[a-z]+[0-9]* */
|
|
|
|
*pp = '\0';
|
2003-01-02 20:12:33 +00:00
|
|
|
return path;
|
|
|
|
}
|
2008-05-07 20:46:57 +00:00
|
|
|
|
|
|
|
/* If this is a Xen virtual block device. */
|
|
|
|
if ((strncmp ("xvd", p, 3) == 0) && p[3] >= 'a' && p[3] <= 'z')
|
|
|
|
{
|
2010-11-06 23:10:49 +00:00
|
|
|
char *pp = p + 3;
|
|
|
|
while (*pp >= 'a' && *pp <= 'z')
|
|
|
|
pp++;
|
|
|
|
/* /dev/xvd[a-z]+[0-9]* */
|
|
|
|
*pp = '\0';
|
2008-05-07 20:46:57 +00:00
|
|
|
return path;
|
|
|
|
}
|
2010-01-25 17:47:51 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DEVICE_MAPPER
|
2010-09-17 10:00:37 +00:00
|
|
|
/* If this is a DM-RAID device.
|
|
|
|
Compare os_dev rather than path here, since nodes under
|
|
|
|
/dev/mapper/ are often symlinks. */
|
|
|
|
if ((strncmp ("/dev/mapper/", os_dev, 12) == 0))
|
2010-01-25 17:47:51 +00:00
|
|
|
{
|
2010-09-17 10:43:46 +00:00
|
|
|
struct dm_tree *tree;
|
2010-01-25 17:47:51 +00:00
|
|
|
uint32_t maj, min;
|
2010-09-17 22:41:06 +00:00
|
|
|
struct dm_tree_node *node = NULL, *child;
|
2010-01-25 17:47:51 +00:00
|
|
|
void *handle;
|
2010-09-17 10:43:46 +00:00
|
|
|
const char *node_uuid, *mapper_name = NULL, *child_uuid, *child_name;
|
2010-01-25 17:47:51 +00:00
|
|
|
|
2010-09-17 10:43:46 +00:00
|
|
|
tree = dm_tree_create ();
|
2010-01-25 17:47:51 +00:00
|
|
|
if (! tree)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm_tree_create failed\n");
|
2010-09-17 10:43:46 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
maj = major (st->st_rdev);
|
|
|
|
min = minor (st->st_rdev);
|
|
|
|
if (! dm_tree_add_dev (tree, maj, min))
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm_tree_add_dev failed\n");
|
2010-09-17 10:43:46 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node = dm_tree_find_node (tree, maj, min);
|
|
|
|
if (! node)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "dm_tree_find_node failed\n");
|
2010-09-17 10:43:46 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
node_uuid = dm_tree_node_get_uuid (node);
|
|
|
|
if (! node_uuid)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "%s has no DM uuid\n", path);
|
2010-09-17 10:43:46 +00:00
|
|
|
node = NULL;
|
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
2010-11-08 15:51:50 +00:00
|
|
|
if (strncmp (node_uuid, "LVM-", 4) == 0)
|
2010-01-25 17:47:51 +00:00
|
|
|
{
|
2010-11-08 15:51:50 +00:00
|
|
|
grub_dprintf ("hostdisk", "%s is an LVM\n", path);
|
|
|
|
node = NULL;
|
|
|
|
goto devmapper_out;
|
|
|
|
}
|
2011-01-25 16:51:23 +00:00
|
|
|
if (strncmp (node_uuid, "mpath-", 6) == 0)
|
|
|
|
{
|
|
|
|
/* Multipath partitions have partN-mpath-* UUIDs, and are
|
|
|
|
linear mappings so are handled by
|
|
|
|
grub_util_get_dm_node_linear_info. Multipath disks are not
|
|
|
|
linear mappings and must be handled specially. */
|
|
|
|
grub_dprintf ("hostdisk", "%s is a multipath disk\n", path);
|
|
|
|
mapper_name = dm_tree_node_get_name (node);
|
|
|
|
goto devmapper_out;
|
|
|
|
}
|
2010-11-08 15:51:50 +00:00
|
|
|
if (strncmp (node_uuid, "DMRAID-", 7) != 0)
|
|
|
|
{
|
|
|
|
int major, minor;
|
|
|
|
const char *node_name;
|
2010-01-25 17:47:51 +00:00
|
|
|
grub_dprintf ("hostdisk", "%s is not DM-RAID\n", path);
|
2010-11-08 15:51:50 +00:00
|
|
|
|
|
|
|
if ((node_name = dm_tree_node_get_name (node))
|
|
|
|
&& grub_util_get_dm_node_linear_info (node_name,
|
|
|
|
&major, &minor))
|
|
|
|
{
|
|
|
|
if (tree)
|
|
|
|
dm_tree_free (tree);
|
|
|
|
free (path);
|
2011-10-14 17:20:44 +00:00
|
|
|
char *ret = grub_find_device ("/dev",
|
2011-05-21 05:03:55 +00:00
|
|
|
(major << 8) | minor);
|
2010-11-08 15:51:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-09-17 10:43:46 +00:00
|
|
|
node = NULL;
|
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handle = NULL;
|
|
|
|
/* Counter-intuitively, device-mapper refers to the disk-like
|
|
|
|
device containing a DM-RAID partition device as a "child" of
|
|
|
|
the partition device. */
|
|
|
|
child = dm_tree_next_child (&handle, node, 0);
|
|
|
|
if (! child)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "%s has no DM children\n", path);
|
2010-01-31 19:37:37 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
child_uuid = dm_tree_node_get_uuid (child);
|
|
|
|
if (! child_uuid)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "%s child has no DM uuid\n", path);
|
2010-01-31 19:37:37 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
else if (strncmp (child_uuid, "DMRAID-", 7) != 0)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "%s child is not DM-RAID\n", path);
|
2010-01-31 19:37:37 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
child_name = dm_tree_node_get_name (child);
|
|
|
|
if (! child_name)
|
|
|
|
{
|
|
|
|
grub_dprintf ("hostdisk", "%s child has no DM name\n", path);
|
2010-01-31 19:37:37 +00:00
|
|
|
goto devmapper_out;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
2010-01-31 19:37:37 +00:00
|
|
|
mapper_name = child_name;
|
2010-01-25 17:47:51 +00:00
|
|
|
|
2010-01-31 19:37:37 +00:00
|
|
|
devmapper_out:
|
2010-09-17 10:43:46 +00:00
|
|
|
if (! mapper_name && node)
|
2010-01-31 19:37:37 +00:00
|
|
|
{
|
|
|
|
/* This is a DM-RAID disk, not a partition. */
|
|
|
|
mapper_name = dm_tree_node_get_name (node);
|
|
|
|
if (! mapper_name)
|
2010-09-17 10:43:46 +00:00
|
|
|
grub_dprintf ("hostdisk", "%s has no DM name\n", path);
|
2010-01-31 19:37:37 +00:00
|
|
|
}
|
2010-09-17 10:43:46 +00:00
|
|
|
if (tree)
|
|
|
|
dm_tree_free (tree);
|
|
|
|
free (path);
|
|
|
|
if (mapper_name)
|
|
|
|
return xasprintf ("/dev/mapper/%s", mapper_name);
|
|
|
|
else
|
|
|
|
return NULL;
|
2010-01-25 17:47:51 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_DEVICE_MAPPER */
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#elif defined(__GNU__)
|
2008-05-16 21:39:52 +00:00
|
|
|
char *path = xstrdup (os_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (strncmp ("/dev/sd", path, 7) == 0 || strncmp ("/dev/hd", path, 7) == 0)
|
|
|
|
{
|
2008-05-16 21:39:52 +00:00
|
|
|
char *p = strchr (path + 7, 's');
|
2003-01-02 20:12:33 +00:00
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#elif defined(__CYGWIN__)
|
|
|
|
char *path = xstrdup (os_dev);
|
|
|
|
if (strncmp ("/dev/sd", path, 7) == 0 && 'a' <= path[7] && path[7] <= 'z')
|
|
|
|
path[8] = 0;
|
|
|
|
return path;
|
|
|
|
|
2011-03-26 11:59:02 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
char *out, *out2;
|
|
|
|
if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
|
|
|
return xstrdup (os_dev);
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_util_follow_gpart_up (os_dev + sizeof ("/dev/") - 1, NULL, &out);
|
2011-03-26 11:59:02 +00:00
|
|
|
|
|
|
|
out2 = xasprintf ("/dev/%s", out);
|
|
|
|
free (out);
|
|
|
|
|
|
|
|
return out2;
|
|
|
|
#elif defined(__APPLE__)
|
2009-04-11 09:40:39 +00:00
|
|
|
char *path = xstrdup (os_dev);
|
|
|
|
if (strncmp ("/dev/", path, 5) == 0)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
for (p = path + 5; *p; ++p)
|
|
|
|
if (grub_isdigit(*p))
|
|
|
|
{
|
2010-07-01 08:52:23 +00:00
|
|
|
p = strpbrk (p, "sp");
|
2009-04-11 09:40:39 +00:00
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
|
2010-04-10 15:07:33 +00:00
|
|
|
#elif defined(__NetBSD__)
|
2010-06-02 22:47:22 +00:00
|
|
|
/* NetBSD uses "/dev/r[a-z]+[0-9][a-z]". */
|
2010-04-10 15:07:33 +00:00
|
|
|
char *path = xstrdup (os_dev);
|
2010-06-02 22:47:22 +00:00
|
|
|
if (strncmp ("/dev/r", path, sizeof("/dev/r") - 1) == 0 &&
|
|
|
|
(path[sizeof("/dev/r") - 1] >= 'a' && path[sizeof("/dev/r") - 1] <= 'z') &&
|
|
|
|
strncmp ("fd", path + sizeof("/dev/r") - 1, sizeof("fd") - 1) != 0) /* not a floppy device name */
|
2010-04-10 15:07:33 +00:00
|
|
|
{
|
2010-06-02 22:47:22 +00:00
|
|
|
char *p;
|
|
|
|
for (p = path + sizeof("/dev/r"); *p >= 'a' && *p <= 'z'; p++);
|
|
|
|
if (grub_isdigit(*p))
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
if ((*p >= 'a' && *p <= 'z') && (*(p+1) == '\0'))
|
|
|
|
{
|
|
|
|
/* path matches the required regular expression and
|
|
|
|
p points to its last character. */
|
|
|
|
int rawpart = -1;
|
2010-04-10 15:07:33 +00:00
|
|
|
# ifdef HAVE_GETRAWPARTITION
|
2010-06-02 22:47:22 +00:00
|
|
|
rawpart = getrawpartition();
|
2010-04-10 15:07:33 +00:00
|
|
|
# endif /* HAVE_GETRAWPARTITION */
|
2010-06-02 22:47:22 +00:00
|
|
|
if (rawpart >= 0)
|
|
|
|
*p = 'a' + rawpart;
|
|
|
|
}
|
2010-04-10 15:07:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#elif defined (__sun__)
|
|
|
|
char *colon = grub_strrchr (os_dev, ':');
|
|
|
|
if (grub_memcmp (os_dev, "/devices", sizeof ("/devices") - 1) == 0
|
|
|
|
&& colon)
|
|
|
|
{
|
|
|
|
char *ret = xmalloc (colon - os_dev + sizeof (":q,raw"));
|
|
|
|
grub_memcpy (ret, os_dev, colon - os_dev);
|
|
|
|
grub_memcpy (ret + (colon - os_dev), ":q,raw", sizeof (":q,raw"));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return xstrdup (os_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
#else
|
2008-06-04 13:59:55 +00:00
|
|
|
# warning "The function `convert_system_partition_to_system_disk' might not work on your OS correctly."
|
2003-01-02 20:12:33 +00:00
|
|
|
return xstrdup (os_dev);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#if defined(__sun__)
|
|
|
|
static int
|
|
|
|
device_is_wholedisk (const char *os_dev)
|
|
|
|
{
|
|
|
|
if (grub_memcmp (os_dev, "/devices/", sizeof ("/devices/") - 1) != 0)
|
|
|
|
return 1;
|
|
|
|
if (grub_memcmp (os_dev + strlen (os_dev) - (sizeof (":q,raw") - 1),
|
|
|
|
":q,raw", (sizeof (":q,raw") - 1)) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-01 22:56:51 +00:00
|
|
|
#if defined(__linux__) || defined(__CYGWIN__)
|
2009-04-30 13:23:48 +00:00
|
|
|
static int
|
|
|
|
device_is_wholedisk (const char *os_dev)
|
|
|
|
{
|
|
|
|
int len = strlen (os_dev);
|
|
|
|
|
|
|
|
if (os_dev[len - 1] < '0' || os_dev[len - 1] > '9')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-07-01 22:56:51 +00:00
|
|
|
#endif
|
2009-04-30 13:23:48 +00:00
|
|
|
|
2010-04-10 15:07:33 +00:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
/* Try to determine whether a given device name corresponds to a whole disk.
|
|
|
|
This function should give in most cases a definite answer, but it may
|
|
|
|
actually give an approximate one in the following sense: if the return
|
|
|
|
value is 0 then the device name does not correspond to a whole disk. */
|
|
|
|
static int
|
|
|
|
device_is_wholedisk (const char *os_dev)
|
|
|
|
{
|
|
|
|
int len = strlen (os_dev);
|
|
|
|
int rawpart = -1;
|
|
|
|
|
|
|
|
# ifdef HAVE_GETRAWPARTITION
|
|
|
|
rawpart = getrawpartition();
|
|
|
|
# endif /* HAVE_GETRAWPARTITION */
|
|
|
|
if (rawpart < 0)
|
|
|
|
return 1;
|
|
|
|
return (os_dev[len - 1] == ('a' + rawpart));
|
|
|
|
}
|
|
|
|
#endif /* defined(__NetBSD__) */
|
|
|
|
|
2010-09-20 11:12:33 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
static int
|
|
|
|
device_is_wholedisk (const char *os_dev)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (p = os_dev + sizeof ("/dev/") - 1; *p; ++p)
|
|
|
|
if (grub_isdigit (*p))
|
|
|
|
{
|
|
|
|
if (strchr (p, 's'))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static int
|
2010-09-21 09:36:44 +00:00
|
|
|
find_system_device (const char *os_dev, struct stat *st, int convert, int add)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2009-11-23 20:30:56 +00:00
|
|
|
unsigned int i;
|
2003-01-02 20:12:33 +00:00
|
|
|
char *os_disk;
|
|
|
|
|
2010-09-21 09:36:44 +00:00
|
|
|
if (convert)
|
|
|
|
os_disk = convert_system_partition_to_system_disk (os_dev, st);
|
|
|
|
else
|
|
|
|
os_disk = xstrdup (os_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! os_disk)
|
|
|
|
return -1;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2009-11-23 20:30:56 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (map); i++)
|
|
|
|
if (! map[i].device)
|
|
|
|
break;
|
|
|
|
else if (strcmp (map[i].device, os_disk) == 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
free (os_disk);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-09-13 11:09:58 +00:00
|
|
|
if (!add)
|
2011-11-12 22:42:49 +00:00
|
|
|
{
|
|
|
|
free (os_disk);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-13 11:09:58 +00:00
|
|
|
|
2009-11-23 20:30:56 +00:00
|
|
|
if (i == ARRAY_SIZE (map))
|
|
|
|
grub_util_error (_("device count exceeds limit"));
|
|
|
|
|
|
|
|
map[i].device = os_disk;
|
2011-12-23 17:19:16 +00:00
|
|
|
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;
|
2009-11-23 20:30:56 +00:00
|
|
|
|
|
|
|
return i;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 11:09:58 +00:00
|
|
|
int
|
|
|
|
grub_util_biosdisk_is_present (const char *os_dev)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (os_dev, &st) < 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-09-21 09:36:44 +00:00
|
|
|
return find_system_device (os_dev, &st, 1, 0) != -1;
|
2010-09-13 11:09:58 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
char *
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_get_grub_dev (const char *os_dev)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int drive;
|
2011-11-12 22:46:47 +00:00
|
|
|
char *sys_disk;
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
grub_util_info ("Looking for %s", os_dev);
|
2003-01-02 23:46:21 +00:00
|
|
|
|
|
|
|
if (stat (os_dev, &st) < 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot stat `%s'", os_dev);
|
2011-03-30 11:13:25 +00:00
|
|
|
grub_util_info ("cannot stat `%s'", os_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-21 09:36:44 +00:00
|
|
|
drive = find_system_device (os_dev, &st, 1, 1);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (drive < 0)
|
|
|
|
{
|
2010-02-07 15:13:59 +00:00
|
|
|
grub_error (GRUB_ERR_UNKNOWN_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"no mapping exists for `%s'", os_dev);
|
2011-03-30 11:13:25 +00:00
|
|
|
grub_util_info ("no mapping exists for `%s'", os_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-06-08 13:04:02 +00:00
|
|
|
|
2011-11-12 22:46:47 +00:00
|
|
|
sys_disk = convert_system_partition_to_system_disk (os_dev, &st);
|
|
|
|
if (grub_strcmp (os_dev, sys_disk) == 0)
|
|
|
|
{
|
|
|
|
free (sys_disk);
|
|
|
|
return make_device_name (drive, -1, -1);
|
|
|
|
}
|
|
|
|
free (sys_disk);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__) || defined (__sun__)
|
2009-04-11 09:40:39 +00:00
|
|
|
if (! S_ISCHR (st.st_mode))
|
|
|
|
#else
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! S_ISBLK (st.st_mode))
|
2009-04-11 09:40:39 +00:00
|
|
|
#endif
|
2003-01-02 20:12:33 +00:00
|
|
|
return make_device_name (drive, -1, -1);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
#if defined(__linux__) || defined(__CYGWIN__) || defined(HAVE_DIOCGDINFO) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__sun__)
|
2011-03-26 11:59:02 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
/* Linux counts partitions uniformly, whether a BSD partition or a DOS
|
2004-04-04 13:46:03 +00:00
|
|
|
partition, so mapping them to GRUB devices is not trivial.
|
2003-01-02 20:12:33 +00:00
|
|
|
Here, get the start sector of a partition by HDIO_GETGEO, and
|
2008-05-16 21:39:52 +00:00
|
|
|
compare it with each partition GRUB recognizes.
|
|
|
|
|
|
|
|
Cygwin /dev/sdXN emulation uses Windows partition mapping. It
|
|
|
|
does not count the extended partition and missing primary
|
2010-04-10 15:07:33 +00:00
|
|
|
partitions. Use same method as on Linux here.
|
|
|
|
|
2010-09-20 11:12:33 +00:00
|
|
|
For NetBSD and FreeBSD, proceed as for Linux, except that the start
|
|
|
|
sector is obtained from the disk label. */
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2010-06-13 00:36:39 +00:00
|
|
|
char *name, *partname;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_t disk;
|
2010-01-25 17:47:51 +00:00
|
|
|
grub_disk_addr_t start;
|
2010-02-07 01:44:47 +00:00
|
|
|
auto int find_partition (grub_disk_t dsk,
|
2005-08-18 03:14:39 +00:00
|
|
|
const grub_partition_t partition);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-02-07 01:44:47 +00:00
|
|
|
int find_partition (grub_disk_t dsk __attribute__ ((unused)),
|
2005-08-18 03:14:39 +00:00
|
|
|
const grub_partition_t partition)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2010-02-06 17:43:37 +00:00
|
|
|
grub_disk_addr_t part_start = 0;
|
|
|
|
grub_util_info ("Partition %d starts from %lu",
|
|
|
|
partition->number, partition->start);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-02-06 17:43:37 +00:00
|
|
|
part_start = grub_partition_get_start (partition);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-05-18 10:14:13 +00:00
|
|
|
if (start == part_start)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2010-06-13 00:36:39 +00:00
|
|
|
partname = grub_partition_get_name (partition);
|
2003-01-02 20:12:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
name = make_device_name (drive, -1, -1);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2011-11-08 11:38:30 +00:00
|
|
|
# if !defined(HAVE_DIOCGDINFO) && !defined(__sun__)
|
2003-01-02 20:12:33 +00:00
|
|
|
if (MAJOR (st.st_rdev) == FLOPPY_MAJOR)
|
|
|
|
return name;
|
2010-09-20 11:12:33 +00:00
|
|
|
# else /* defined(HAVE_DIOCGDINFO) */
|
2010-04-10 15:07:33 +00:00
|
|
|
/* Since os_dev and convert_system_partition_to_system_disk (os_dev) are
|
2010-06-02 22:47:22 +00:00
|
|
|
* different, we know that os_dev cannot be a floppy device. */
|
2010-09-20 11:12:33 +00:00
|
|
|
# endif /* !defined(HAVE_DIOCGDINFO) */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-01-25 17:47:51 +00:00
|
|
|
start = find_partition_start (os_dev);
|
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
free (name);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-01-25 17:47:51 +00:00
|
|
|
grub_util_info ("%s starts from %lu", os_dev, start);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-01-25 17:47:51 +00:00
|
|
|
if (start == 0 && device_is_wholedisk (os_dev))
|
2003-01-02 20:12:33 +00:00
|
|
|
return name;
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("opening the device %s", name);
|
|
|
|
disk = grub_disk_open (name);
|
2003-01-02 20:12:33 +00:00
|
|
|
free (name);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! disk)
|
2010-09-21 09:36:44 +00:00
|
|
|
{
|
|
|
|
/* We already know that the partition exists. Given that we already
|
|
|
|
checked the device map above, we can only get
|
|
|
|
GRUB_ERR_UNKNOWN_DEVICE at this point if the disk does not exist.
|
|
|
|
This can happen on Xen, where disk images in the host can be
|
|
|
|
assigned to devices that have partition-like names in the guest
|
|
|
|
but are really more like disks. */
|
|
|
|
if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
|
|
|
|
{
|
|
|
|
grub_util_warn
|
|
|
|
("disk does not exist, so falling back to partition device %s",
|
|
|
|
os_dev);
|
|
|
|
|
|
|
|
drive = find_system_device (os_dev, &st, 0, 1);
|
|
|
|
if (drive < 0)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_UNKNOWN_DEVICE,
|
|
|
|
"no mapping exists for `%s'", os_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_device_name (drive, -1, -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-06-13 00:36:39 +00:00
|
|
|
partname = NULL;
|
2008-07-03 22:56:43 +00:00
|
|
|
grub_partition_iterate (disk, find_partition);
|
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_close (disk);
|
2003-01-02 23:46:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-06-13 00:36:39 +00:00
|
|
|
if (partname == NULL)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_close (disk);
|
2011-11-12 22:46:47 +00:00
|
|
|
grub_util_info ("cannot find the partition of `%s'", os_dev);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"cannot find the partition of `%s'", os_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-06-13 00:36:39 +00:00
|
|
|
name = grub_xasprintf ("%s,%s", disk->name, partname);
|
|
|
|
free (partname);
|
2011-12-23 18:25:24 +00:00
|
|
|
grub_disk_close (disk);
|
2010-06-13 00:36:39 +00:00
|
|
|
return name;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#elif defined(__GNU__)
|
|
|
|
/* GNU uses "/dev/[hs]d[0-9]+(s[0-9]+[a-z]?)?". */
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int dos_part = -1;
|
|
|
|
int bsd_part = -1;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
p = strrchr (os_dev, 's');
|
|
|
|
if (p)
|
|
|
|
{
|
2004-03-14 17:48:25 +00:00
|
|
|
long int n;
|
2003-01-02 20:12:33 +00:00
|
|
|
char *q;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
2004-03-14 17:48:25 +00:00
|
|
|
n = strtol (p, &q, 10);
|
2009-04-15 20:45:09 +00:00
|
|
|
if (p != q && n != GRUB_LONG_MIN && n != GRUB_LONG_MAX)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2009-11-02 19:14:00 +00:00
|
|
|
dos_part = (int) n - 1;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
if (*q >= 'a' && *q <= 'g')
|
|
|
|
bsd_part = *q - 'a';
|
|
|
|
}
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
return make_device_name (drive, dos_part, bsd_part);
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#else
|
2004-04-04 13:46:03 +00:00
|
|
|
# warning "The function `grub_util_biosdisk_get_grub_dev' might not work on your OS correctly."
|
2003-01-02 20:12:33 +00:00
|
|
|
return make_device_name (drive, -1, -1);
|
|
|
|
#endif
|
|
|
|
}
|
2010-04-18 12:02:13 +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)
|
|
|
|
{
|
|
|
|
return map[disk->id].device;
|
|
|
|
}
|
2010-09-15 13:30:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
grub_util_biosdisk_is_floppy (grub_disk_t disk)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int fd;
|
|
|
|
|
2011-09-28 12:07:53 +00:00
|
|
|
if (disk->dev != &grub_util_biosdisk_dev)
|
|
|
|
return 0;
|
|
|
|
|
2010-09-15 13:30:43 +00:00
|
|
|
fd = open (map[disk->id].device, O_RDONLY);
|
|
|
|
/* Shouldn't happen. */
|
|
|
|
if (fd == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Shouldn't happen either. */
|
|
|
|
if (fstat (fd, &st) < 0)
|
2011-06-23 20:02:05 +00:00
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
2010-09-15 13:30:43 +00:00
|
|
|
|
|
|
|
#if defined(__NetBSD__)
|
|
|
|
if (major(st.st_rdev) == RAW_FLOPPY_MAJOR)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
2010-09-26 13:53:05 +00:00
|
|
|
#if defined(FLOPPY_MAJOR)
|
2010-09-15 13:30:43 +00:00
|
|
|
if (major(st.st_rdev) == FLOPPY_MAJOR)
|
2010-09-26 13:53:05 +00:00
|
|
|
#else
|
|
|
|
/* Some kernels (e.g. kFreeBSD) don't have a static major number
|
|
|
|
for floppies, but they still use a "fd[0-9]" pathname. */
|
|
|
|
if (map[disk->id].device[5] == 'f'
|
|
|
|
&& map[disk->id].device[6] == 'd'
|
|
|
|
&& map[disk->id].device[7] >= '0'
|
|
|
|
&& map[disk->id].device[7] <= '9')
|
|
|
|
#endif
|
2010-09-15 13:30:43 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|