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
|
|
|
*/
|
|
|
|
|
2013-04-11 19:12:46 +00:00
|
|
|
#include <config-util.h>
|
|
|
|
|
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
|
|
|
# ifndef BLKFLSBUF
|
|
|
|
# define BLKFLSBUF _IO (0x12,97) /* flush buffer cache */
|
|
|
|
# endif /* ! BLKFLSBUF */
|
|
|
|
# include <sys/ioctl.h> /* ioctl */
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2009-04-11 18:46:12 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2009-04-14 07:01:34 +00:00
|
|
|
# include <sys/sysctl.h>
|
2010-01-25 17:47:51 +00:00
|
|
|
#endif
|
|
|
|
|
2012-04-23 15:14:48 +00:00
|
|
|
static struct
|
2008-05-30 10:50:00 +00:00
|
|
|
{
|
|
|
|
char *drive;
|
|
|
|
char *device;
|
2011-12-23 17:19:16 +00:00
|
|
|
int device_map;
|
2008-05-30 10:50:00 +00:00
|
|
|
} map[256];
|
2003-01-02 20:12:33 +00:00
|
|
|
|
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
|
2013-01-20 15:52:15 +00:00
|
|
|
grub_util_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
|
2011-04-21 22:09:07 +00:00
|
|
|
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++)
|
2013-01-20 15:52:15 +00:00
|
|
|
if (map[i].drive && hook (map[i].drive, hook_data))
|
2003-01-02 20:12:33 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2013-09-23 09:21:09 +00:00
|
|
|
struct grub_util_hostdisk_data *data;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-06-04 13:59:55 +00:00
|
|
|
drive = find_grub_drive (name);
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_info ("drive = %d", drive);
|
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;
|
2013-09-23 09:21:09 +00:00
|
|
|
disk->data = data = xmalloc (sizeof (struct grub_util_hostdisk_data));
|
2010-03-03 10:44:12 +00:00
|
|
|
data->dev = NULL;
|
2010-03-03 20:03:50 +00:00
|
|
|
data->access_mode = 0;
|
2013-09-23 09:21:09 +00:00
|
|
|
data->fd = GRUB_UTIL_FD_INVALID;
|
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. */
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_t fd;
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
fd = grub_util_fd_open (map[drive].device, O_RDONLY);
|
|
|
|
|
|
|
|
if (!GRUB_UTIL_FD_IS_VALID(fd))
|
2012-02-08 18:26:01 +00:00
|
|
|
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"),
|
2013-09-24 17:08:34 +00:00
|
|
|
map[drive].device, grub_util_fd_strerror ());
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2012-02-29 14:23:31 +00:00
|
|
|
disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device,
|
|
|
|
&disk->log_sector_size);
|
|
|
|
disk->total_sectors >>= disk->log_sector_size;
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2013-09-24 17:17:24 +00:00
|
|
|
#if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
|
2013-09-24 16:57:19 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
2013-09-22 05:36:17 +00:00
|
|
|
# if GRUB_DISK_DEVS_ARE_CHAR
|
2013-09-24 16:57:19 +00:00
|
|
|
if (fstat (fd, &st) < 0 || ! S_ISCHR (st.st_mode))
|
2009-04-11 09:40:39 +00:00
|
|
|
# else
|
2013-09-24 16:57:19 +00:00
|
|
|
if (fstat (fd, &st) < 0 || ! S_ISBLK (st.st_mode))
|
2009-04-11 09:40:39 +00:00
|
|
|
# endif
|
2013-09-24 16:57:19 +00:00
|
|
|
data->is_disk = 1;
|
|
|
|
}
|
2013-08-22 14:50:12 +00:00
|
|
|
#endif
|
2009-07-20 20:03:18 +00:00
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_close (fd);
|
|
|
|
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_info ("the size of %s is %" PRIuGRUB_UINT64_T,
|
|
|
|
name, disk->total_sectors);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 23:46:21 +00:00
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
const char *
|
|
|
|
grub_hostdisk_os_dev_to_grub_drive (const char *os_disk, int add)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2013-09-19 06:48:54 +00:00
|
|
|
char *canon;
|
|
|
|
|
|
|
|
canon = canonicalize_file_name (os_disk);
|
|
|
|
if (!canon)
|
|
|
|
canon = xstrdup (os_disk);
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (map); i++)
|
|
|
|
if (! map[i].device)
|
|
|
|
break;
|
2013-09-19 06:48:54 +00:00
|
|
|
else if (strcmp (map[i].device, canon) == 0)
|
|
|
|
{
|
|
|
|
free (canon);
|
|
|
|
return map[i].drive;
|
|
|
|
}
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
if (!add)
|
2013-09-19 06:48:54 +00:00
|
|
|
{
|
|
|
|
free (canon);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
if (i == ARRAY_SIZE (map))
|
2012-02-26 16:28:05 +00:00
|
|
|
/* TRANSLATORS: it refers to the lack of free slots. */
|
2012-02-10 13:01:53 +00:00
|
|
|
grub_util_error ("%s", _("device count exceeds limit"));
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-09-19 06:48:54 +00:00
|
|
|
map[i].device = canon;
|
2012-01-29 13:28:01 +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;
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
grub_hostdisk_flush_initial_buffer (os_disk);
|
2012-05-11 19:03:47 +00:00
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
return map[i].drive;
|
|
|
|
}
|
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
grub_util_fd_t
|
|
|
|
grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
|
|
|
|
grub_disk_addr_t *max)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_t fd;
|
|
|
|
struct grub_util_hostdisk_data *data = disk->data;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
*max = ~0ULL;
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef O_SYNC
|
|
|
|
flags |= O_SYNC;
|
|
|
|
#endif
|
|
|
|
#ifdef O_FSYNC
|
|
|
|
flags |= O_FSYNC;
|
|
|
|
#endif
|
2009-04-14 07:01:34 +00:00
|
|
|
|
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);
|
2012-05-27 11:19:09 +00:00
|
|
|
data->dev = 0;
|
2013-09-23 09:21:09 +00:00
|
|
|
if (GRUB_UTIL_FD_IS_VALID(data->fd))
|
2011-04-08 09:49:38 +00:00
|
|
|
{
|
|
|
|
if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY)
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_sync (data->fd);
|
|
|
|
grub_util_fd_close (data->fd);
|
|
|
|
data->fd = GRUB_UTIL_FD_INVALID;
|
2011-04-08 09:49:38 +00:00
|
|
|
}
|
2010-03-03 20:03:50 +00:00
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
fd = grub_util_fd_open (map[disk->id].device, flags);
|
|
|
|
if (GRUB_UTIL_FD_IS_VALID(fd))
|
2010-03-03 10:44:12 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
if (!GRUB_UTIL_FD_IS_VALID(data->fd))
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
2013-09-24 17:08:34 +00:00
|
|
|
map[disk->id].device, grub_util_fd_strerror ());
|
2013-09-23 09:21:09 +00:00
|
|
|
return GRUB_UTIL_FD_INVALID;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_close (fd);
|
|
|
|
return GRUB_UTIL_FD_INVALID;
|
2011-04-22 17:20:46 +00:00
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2013-09-23 09:21:09 +00:00
|
|
|
#endif
|
2009-06-10 21:04:23 +00:00
|
|
|
|
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_read (grub_disk_t disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, char *buf)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
while (size)
|
2010-02-02 23:49:35 +00:00
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_t fd;
|
2012-04-18 21:18:51 +00:00
|
|
|
grub_disk_addr_t max = ~0ULL;
|
2013-09-23 09:21:09 +00:00
|
|
|
fd = grub_util_fd_open_device (disk, sector, O_RDONLY, &max);
|
|
|
|
if (!GRUB_UTIL_FD_IS_VALID (fd))
|
2012-04-18 21:18:51 +00:00
|
|
|
return grub_errno;
|
2010-02-02 23:49:35 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
#ifdef __linux__
|
2012-06-02 11:48:44 +00:00
|
|
|
if (sector == 0)
|
|
|
|
/* Work around a bug in Linux ez remapping. Linux remaps all
|
|
|
|
sectors that are read together with the MBR in one read. It
|
|
|
|
should only remap the MBR, so we split the read in two
|
|
|
|
parts. -jochen */
|
|
|
|
max = 1;
|
2003-01-02 20:12:33 +00:00
|
|
|
#endif /* __linux__ */
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2012-06-02 11:48:44 +00:00
|
|
|
if (max > size)
|
|
|
|
max = size;
|
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
if (grub_util_fd_read (fd, buf, max << disk->log_sector_size)
|
|
|
|
!= (ssize_t) (max << disk->log_sector_size))
|
|
|
|
return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
|
2013-09-24 17:08:34 +00:00
|
|
|
map[disk->id].device, grub_util_fd_strerror ());
|
2012-04-18 21:18:51 +00:00
|
|
|
size -= max;
|
|
|
|
buf += (max << disk->log_sector_size);
|
|
|
|
sector += max;
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static grub_err_t
|
2006-06-04 Yoshinori K. Okuji <okuji@enbug.org>
Clean up the code to support 64-bit addressing in disks and
files. This change is not enough for filesystems yet.
* util/i386/pc/grub-setup.c (struct boot_blocklist): Change the
type of "start" to grub_uint64_t.
(setup): Change the types of KERNEL_SECTOR and FIRST_SECTOR to
grub_disk_addr_t * and grub_disk_addr_t. Fix the format string in
save_first_sector and save_blocklists. Use grub_le_to_cpu64 to
convert addresses.
* util/i386/pc/biosdisk.c (open_device): Change the type of SECTOR
to grub_disk_addr_t.
* partmap/gpt.c (gpt_partition_map_iterate): Fix the format
string.
* partmap/pc.c (pc_partition_map_iterate): Likewise.
* partmap/amiga.c (amiga_partition_map_iterate): Cast RDSK.MAGIC
to char *.
* normal/script.c (grub_script_parse): Remove unused MEMFREE.
* normal/parser.y (YYLTYPE_IS_TRIVIAL): New macro.
* normal/lexer.c (grub_script_yyerror): Specify unused to LEX.
* loader/i386/pc/multiboot.c (grub_multiboot_load_elf64): Cast -1
to grub_off_t, to detect an error from grub_file_seek.
(grub_multiboot_load_elf32): Likewise.
* kern/misc.c (grub_strtoul): Use grub_strtoull. Return the
maximum unsigned long value when an overflow is detected.
(grub_strtoull): New function.
(grub_divmod64): Likewise.
(grub_lltoa): use grub_divmod64.
* kern/fs.c (struct grub_fs_block): Change the type of "offset" to
grub_disk_addr_t.
(grub_fs_blocklist_open): Increase P if P is not NULL to advance
the pointer to next character. Use grub_strtoull instead of
grub_strtoul.
(grub_fs_blocklist_read): Change the types of SECTOR, OFFSET and
SIZE to grub_disk_addr_t, grub_off_t and grub_size_t,
respectively.
* kern/file.c (grub_file_read): Prevent an oveflow of LEN, as the
return value is signed.
(grub_file_seek): Change the type of OLD to grub_off_t. Do not
test if OFFSET is less than zero, as OFFSET is unsigned now.
* kern/disk.c (struct grub_disk_cache): Change the type of
"sector" to grub_disk_addr_t.
(grub_disk_cache_get_index): Change the type of SECTOR to
grub_disk_addr_t. Calculate the hash with SECTOR casted to
unsigned after shifting.
(grub_disk_cache_invalidate): Change the type of SECTOR to
grub_disk_addr_t.
(grub_disk_cache_unlock): Likewise.
(grub_disk_cache_store): Likewise.
(grub_disk_check_range): Change the types of SECTOR, OFFSET, SIZE,
START and LEN to grub_disk_addr_t *, grub_off_t *, grub_size_t,
grub_disk_addr_t and grub_uint64_t, respectively.
(grub_disk_read): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of START_SECTOR, LEN and
POS to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
(grub_disk_write): Use an unsigned variable REAL_OFFSET for the
body, as the value of OFFSET is tweaked by
grub_disk_check_range. Change the types of LEN and N to
grub_size_t.
* io/gzio.c (struct grub_gzio): Change the types of "data_offset"
and "saved_offset" to grub_off_t.
(test_header): Cast BUF to char *.
(get_byte): Cast GZIO->DATA_OFFSET to grub_off_t. Cast GZIO->INBUF
to char *.
(grub_gzio_read): Change the types of OFFSET and SIZE to
grub_off_t and grub_size_t, respectively.
* include/grub/i386/pc/boot.h (GRUB_BOOT_MACHINE_FORCE_LBA):
Removed.
(GRUB_BOOT_MACHINE_BOOT_DRIVE): Changed to 0x4c.
(GRUB_BOOT_MACHINE_KERNEL_ADDRESS): Changed to 0x40.
(GRUB_BOOT_MACHINE_KERNEL_SEGMENT): Changed to 0x42.
(GRUB_BOOT_MACHINE_DRIVE_CHECK): Changed to 0x4e.
(GRUB_BOOT_MACHINE_LIST_SIZE): Increased to 12.
* include/grub/types.h (grub_off_t): Unconditionally set to
grub_uint64_t.
(grub_disk_addr_t): Changed to grub_uint64_t.
* include/grub/partition.h (struct grub_partition): Change the
types of "start", "len" and "offset" to grub_disk_addr_t,
grub_uint64_t and grub_disk_addr_t, respectively.
(grub_partition_get_start): Return grub_disk_addr_t.
(grub_partition_get_len): Return grub_uint64_t.
* include/grub/misc.h (grub_strtoull): New prototype.
(grub_divmod64): Likewise.
* include/grub/fshelp.h (grub_fshelp_read_file): Change the types
of SECTOR, LEN and FILESIZE to grub_disk_addr_t, grub_size_t and
grub_off_t, respectively.
All callers and references changed.
* include/grub/fs.h (struct grub_fs): Change the type of LEN to
grub_size_t in "read".
All callers and references changed.
* include/grub/file.h (struct grub_file): Change the types of
"offset" and "size" to grub_off_t and grub_off_t,
respectively. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_file_read): Change the type of LEN to grub_size_t.
(grub_file_seek): Return grub_off_t. Change the type of OFFSET to
grub_off_t.
(grub_file_size): Return grub_off_t.
(grub_file_tell): Likewise.
All callers and references changed.
* include/grub/disk.h (struct grub_disk_dev): Change the types of
SECTOR and SIZE to grub_disk_addr_t and grub_size_t in "read" and
"write".
(struct grub_disk): Change the type of "total_sectors" to
grub_uint64_t. Change the type of SECTOR to grub_disk_addr_t in
"read_hook".
(grub_disk_read): Change the types of SECTOR, OFFSET and SIZE to
grub_disk_addr_t, grub_off_t and grub_size_t, respectively.
(grub_disk_write): Likewise.
All callers and references changed.
* fs/iso9660.c (grub_iso9660_susp_iterate): Cast parameters to
char * for grub_strncmp to silence gcc.
(grub_iso9660_mount): Likewise.
(grub_iso9660_mount): Likewise.
(grub_iso9660_read_symlink): Likewise. Also, remove the nonsense
return statement.
(grub_iso9660_iterate_dir): Likewise.
(grub_iso9660_label): Cast DATA->VOLDESC.VOLNAME to char *.
* fs/hfs.c (grub_hfs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/hfsplus.c (grub_hfsplus_read_file): Likewise.
* fs/jfs.c (grub_jfs_read_file): Likewise.
* fs/minix.c (grub_jfs_read_file): Likewise.
* fs/sfs.c (grub_jfs_read_file): Likewise.
* fs/ufs.c (grub_jfs_read_file): Likewise.
* fs/xfs.c (grub_jfs_read_file): Likewise.
* fs/fat.c (grub_fat_read_data): Change the types of SECTOR, LEN
and SIZE to grub_disk_addr_t, grub_size_t and grub_size_t,
respectively.
* fs/ext2.c (grub_ext2_read_block): When an error happens, set
BLKNR to -1 instead of returning GRUB_ERRNO.
(grub_ext2_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* fs/affs.c (grub_affs_read_file): Change the types of SECTOR and
LEN to grub_disk_addr_t and grub_size_t, respectively.
* font/manager.c (grub_font_get_glyph): Cast BITMAP to char * for
grub_file_read.
* disk/ieee1275/ofdisk.c (grub_ofdisk_read): Fix the format
string. Do not cast SECTOR explicitly.
* disk/i386/pc/biosdisk.c (grub_biosdisk_open): Change the type of
TOTAL_SECTORS to grub_uint64_t. Do not mask DRP->TOTAL_SECTORS.
(grub_biosdisk_rw): Change the types of SECTOR and SIZE to
grub_disk_addr_t and grub_size_t, respectively. If the sector is
over 2TB and LBA mode is not supported, raise an error.
(get_safe_sectors): New function.
(grub_biosdisk_read): Use get_safe_sectors.
(grub_biosdisk_write): Likewise.
* disk/efi/efidisk.c (grub_efidisk_read): Fix the format string.
(grub_efidisk_write): Likewise.
* disk/loopback.c (delete_loopback): Cosmetic changes.
(grub_cmd_loopback): Likewise. Also, test NEWDEV->FILENAME
correctly.
(grub_loopback_open): Likewise.
(grub_loopback_read): Likewise. Also, change the type of POS to
grub_off_t, and fix the usage of grub_memset.
* commands/i386/pc/play.c: Include grub/machine/time.h.
* commands/ls.c (grub_ls_list_files): Use "llu" instead of "d" to
print FILE->SIZE.
* commands/configfile.c: Include grub/env.h.
* commands/cmp.c (grub_cmd_cmp): Do not use ERR, but use
GRUB_ERRNO directly instead. Change the type of POS to
grub_off_t. Follow the coding standard.
* commands/blocklist.c: Include grub/partition.h.
(grub_cmd_blocklist): Return an error if the underlying device is
not a disk. Take the starting sector of a partition into account,
if a partition is used.
* boot/i386/pc/diskboot.S (bootloop): Adapted to the new offset of
a length field.
(lba_mode): Support 64-bit addresses.
(chs_mode): Likewise.
(copy_buffer): Adapted to the new offsets of a length field and a
segment field.
(blocklist_default_start): Allocate 64-bit space.
* boot/i386/pc/boot.S (force_lba): Removed.
(boot_drive): Moved to under KERNEL_SECTOR.
(kernel_sector): Moved to under KENREL_SEGMENT. Allocate 64-bit
space.
(real_start): Set %si earlier. Remove code for FORCE_LBA, since it
is useless.
(lba_mode): Refactored to support a 64-bit address. More size
optimization.
(setup_sectors): Likewise.
2006-06-04 15:56:55 +00:00
|
|
|
grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
|
|
|
|
grub_size_t size, const char *buf)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
while (size)
|
2010-02-02 23:49:35 +00:00
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_t fd;
|
2012-04-18 21:18:51 +00:00
|
|
|
grub_disk_addr_t max = ~0ULL;
|
2013-09-23 09:21:09 +00:00
|
|
|
fd = grub_util_fd_open_device (disk, sector, O_WRONLY, &max);
|
|
|
|
if (!GRUB_UTIL_FD_IS_VALID (fd))
|
2012-04-18 21:18:51 +00:00
|
|
|
return grub_errno;
|
2010-02-02 23:49:35 +00:00
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
#ifdef __linux__
|
2012-06-02 11:48:44 +00:00
|
|
|
if (sector == 0)
|
|
|
|
/* Work around a bug in Linux ez remapping. Linux remaps all
|
|
|
|
sectors that are write together with the MBR in one write. It
|
|
|
|
should only remap the MBR, so we split the write in two
|
|
|
|
parts. -jochen */
|
|
|
|
max = 1;
|
2012-04-18 21:18:51 +00:00
|
|
|
#endif /* __linux__ */
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-06-02 11:48:44 +00:00
|
|
|
if (max > size)
|
|
|
|
max = size;
|
|
|
|
|
2012-04-18 21:18:51 +00:00
|
|
|
if (grub_util_fd_write (fd, buf, max << disk->log_sector_size)
|
|
|
|
!= (ssize_t) (max << disk->log_sector_size))
|
2012-04-18 21:48:52 +00:00
|
|
|
return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to `%s': %s"),
|
2013-09-24 17:08:34 +00:00
|
|
|
map[disk->id].device, grub_util_fd_strerror ());
|
2012-04-18 21:18:51 +00:00
|
|
|
size -= max;
|
|
|
|
buf += (max << disk->log_sector_size);
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:39:14 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_util_biosdisk_flush (struct grub_disk *disk)
|
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
struct grub_util_hostdisk_data *data = disk->data;
|
2011-04-19 20:39:14 +00:00
|
|
|
|
|
|
|
if (disk->dev->id != GRUB_DISK_DEVICE_BIOSDISK_ID)
|
|
|
|
return GRUB_ERR_NONE;
|
2013-09-23 09:21:09 +00:00
|
|
|
if (!GRUB_UTIL_FD_IS_VALID (data->fd))
|
2011-04-19 20:39:14 +00:00
|
|
|
{
|
2012-04-18 21:18:51 +00:00
|
|
|
grub_disk_addr_t max;
|
2013-09-23 09:21:09 +00:00
|
|
|
data->fd = grub_util_fd_open_device (disk, 0, O_RDONLY, &max);
|
|
|
|
if (!GRUB_UTIL_FD_IS_VALID (data->fd))
|
2011-04-19 20:39:14 +00:00
|
|
|
return grub_errno;
|
|
|
|
}
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_sync (data->fd);
|
2011-04-19 20:39:14 +00:00
|
|
|
#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)
|
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
struct grub_util_hostdisk_data *data = disk->data;
|
2010-03-03 10:44:12 +00:00
|
|
|
|
|
|
|
free (data->dev);
|
2013-09-24 16:36:51 +00:00
|
|
|
if (GRUB_UTIL_FD_IS_VALID (data->fd))
|
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);
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_fd_close (data->fd);
|
2011-04-08 09:49:38 +00:00
|
|
|
}
|
2010-03-03 10:44:12 +00:00
|
|
|
free (data);
|
|
|
|
}
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
static struct grub_disk_dev grub_util_biosdisk_dev =
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2012-01-29 20:49:44 +00:00
|
|
|
.name = "hostdisk",
|
|
|
|
.id = GRUB_DISK_DEVICE_HOSTDISK_ID,
|
2004-04-04 13:46:03 +00:00
|
|
|
.iterate = grub_util_biosdisk_iterate,
|
|
|
|
.open = grub_util_biosdisk_open,
|
2010-03-03 10:44:12 +00:00
|
|
|
.close = grub_util_biosdisk_close,
|
2004-04-04 13:46:03 +00:00
|
|
|
.read = grub_util_biosdisk_read,
|
|
|
|
.write = grub_util_biosdisk_write,
|
2003-01-02 20:12:33 +00:00
|
|
|
.next = 0
|
|
|
|
};
|
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
static int
|
|
|
|
grub_util_check_file_presence (const char *p)
|
|
|
|
{
|
|
|
|
#if defined (__MINGW32__) || defined(__CYGWIN__)
|
|
|
|
HANDLE h;
|
|
|
|
h = grub_util_fd_open (p, O_RDONLY);
|
|
|
|
if (!GRUB_UTIL_FD_IS_VALID(h))
|
|
|
|
return 0;
|
|
|
|
CloseHandle (h);
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (p, &st) == -1)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
static void
|
|
|
|
read_device_map (const char *dev_map)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buf[1024]; /* XXX */
|
|
|
|
int lineno = 0;
|
|
|
|
|
2011-12-23 17:19:16 +00:00
|
|
|
if (dev_map[0] == '\0')
|
|
|
|
{
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_info ("no device.map");
|
2011-12-23 17:19:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-13 18:36:28 +00:00
|
|
|
fp = grub_util_fopen (dev_map, "r");
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! fp)
|
2009-11-23 20:30:56 +00:00
|
|
|
{
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno));
|
2009-11-23 20:30:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
while (fgets (buf, sizeof (buf), fp))
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
char *e;
|
2012-05-01 13:02:34 +00:00
|
|
|
char *drive_e, *drive_p;
|
2003-01-02 20:12:33 +00:00
|
|
|
int drive;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
lineno++;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
/* Skip leading spaces. */
|
2012-03-02 10:18:11 +00:00
|
|
|
while (*p && grub_isspace (*p))
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
/* If the first character is `#' or NUL, skip this line. */
|
|
|
|
if (*p == '\0' || *p == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p != '(')
|
2012-02-08 18:26:01 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
tmp = xasprintf (_("missing `%c' symbol"), '(');
|
2013-03-01 12:46:24 +00:00
|
|
|
grub_util_error ("%s:%d: %s", dev_map, lineno, tmp);
|
2012-02-08 18:26:01 +00:00
|
|
|
}
|
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)
|
2013-03-01 12:46:24 +00:00
|
|
|
grub_util_error ("%s:%d: %s", dev_map, lineno, _("device count exceeds limit"));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2008-05-30 10:50:00 +00:00
|
|
|
e = p;
|
2003-01-02 20:12:33 +00:00
|
|
|
p = strchr (p, ')');
|
|
|
|
if (! p)
|
2012-02-08 18:26:01 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
tmp = xasprintf (_("missing `%c' symbol"), ')');
|
2013-03-01 12:46:24 +00:00
|
|
|
grub_util_error ("%s:%d: %s", dev_map, lineno, tmp);
|
2012-02-08 18:26:01 +00:00
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2012-05-01 13:02:34 +00:00
|
|
|
map[drive].drive = 0;
|
|
|
|
if ((e[0] == 'f' || e[0] == 'h' || e[0] == 'c') && e[1] == 'd')
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
for (ptr = e + 2; ptr < p; ptr++)
|
|
|
|
if (!grub_isdigit (*ptr))
|
|
|
|
break;
|
|
|
|
if (ptr == p)
|
|
|
|
{
|
|
|
|
map[drive].drive = xmalloc (p - e + sizeof ('\0'));
|
|
|
|
strncpy (map[drive].drive, e, p - e + sizeof ('\0'));
|
|
|
|
map[drive].drive[p - e] = '\0';
|
|
|
|
}
|
2012-05-03 22:07:55 +00:00
|
|
|
if (*ptr == ',')
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
|
2012-05-12 12:15:45 +00:00
|
|
|
/* TRANSLATORS: Only one entry is ignored. However the suggestion
|
|
|
|
is to correct/delete the whole file.
|
|
|
|
device.map is a file indicating which
|
2012-05-03 22:07:55 +00:00
|
|
|
devices are available at boot time. Fedora populated it with
|
|
|
|
entries like (hd0,1) /dev/sda1 which would mean that every
|
|
|
|
partition is a separate disk for BIOS. Such entries were
|
|
|
|
inactive in GRUB due to its bug which is now gone. Without
|
|
|
|
this additional check these entries would be harmful now.
|
|
|
|
*/
|
|
|
|
grub_util_warn (_("the device.map entry `%s' is invalid. "
|
|
|
|
"Ignoring it. Please correct or "
|
|
|
|
"delete your device.map"), e);
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-01 13:02:34 +00:00
|
|
|
}
|
|
|
|
drive_e = e;
|
|
|
|
drive_p = p;
|
2011-12-23 17:19:16 +00:00
|
|
|
map[drive].device_map = 1;
|
2008-05-30 10:50:00 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
|
|
|
/* Skip leading spaces. */
|
2012-03-02 10:18:11 +00:00
|
|
|
while (*p && grub_isspace (*p))
|
2003-01-02 20:12:33 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '\0')
|
2013-03-01 12:46:24 +00:00
|
|
|
grub_util_error ("%s:%d: %s", dev_map, lineno, _("filename expected"));
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
/* NUL-terminate the filename. */
|
|
|
|
e = p;
|
2012-03-02 10:18:11 +00:00
|
|
|
while (*e && ! grub_isspace (*e))
|
2003-01-02 20:12:33 +00:00
|
|
|
e++;
|
|
|
|
*e = '\0';
|
|
|
|
|
2013-09-23 09:21:09 +00:00
|
|
|
if (!grub_util_check_file_presence (p))
|
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
|
|
|
/* On Linux, the devfs uses symbolic links horribly, and that
|
|
|
|
confuses the interface very much, so use realpath to expand
|
2013-09-19 06:48:54 +00:00
|
|
|
symbolic links. */
|
|
|
|
map[drive].device = canonicalize_file_name (p);
|
|
|
|
if (! map[drive].device)
|
|
|
|
map[drive].device = xstrdup (p);
|
|
|
|
|
2012-05-01 13:02:34 +00:00
|
|
|
if (!map[drive].drive)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
map[drive].drive = xmalloc (sizeof ("hostdisk/") + strlen (p));
|
|
|
|
memcpy (map[drive].drive, "hostdisk/", sizeof ("hostdisk/") - 1);
|
|
|
|
strcpy (map[drive].drive + sizeof ("hostdisk/") - 1, p);
|
|
|
|
c = *drive_p;
|
|
|
|
*drive_p = 0;
|
2012-05-03 21:43:59 +00:00
|
|
|
/* TRANSLATORS: device.map is a filename. Not to be translated.
|
|
|
|
device.map specifies disk correspondance overrides. Previously
|
|
|
|
one could create any kind of device name with this. Due to
|
|
|
|
some problems we decided to limit it to just a handful
|
|
|
|
possibilities. */
|
|
|
|
grub_util_warn (_("the drive name `%s' in device.map is incorrect. "
|
2012-05-01 13:02:34 +00:00
|
|
|
"Using %s instead. "
|
|
|
|
"Please use the form [hfc]d[0-9]* "
|
|
|
|
"(E.g. `hd0' or `cd')"),
|
|
|
|
drive_e, map[drive].drive);
|
|
|
|
*drive_p = c;
|
|
|
|
}
|
2012-05-11 19:03:47 +00:00
|
|
|
|
2013-09-19 06:48:54 +00:00
|
|
|
grub_util_info ("adding `%s' -> `%s' from device.map", map[drive].drive,
|
|
|
|
map[drive].device);
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
grub_hostdisk_flush_initial_buffer (map[drive].device);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_init (const char *dev_map)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
read_device_map (dev_map);
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_dev_register (&grub_util_biosdisk_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_fini (void)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
2008-05-30 10:50:00 +00:00
|
|
|
{
|
|
|
|
if (map[i].drive)
|
|
|
|
free (map[i].drive);
|
|
|
|
if (map[i].device)
|
|
|
|
free (map[i].device);
|
|
|
|
map[i].drive = map[i].device = NULL;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_dev_unregister (&grub_util_biosdisk_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 17:19:16 +00:00
|
|
|
const char *
|
|
|
|
grub_util_biosdisk_get_compatibility_hint (grub_disk_t disk)
|
|
|
|
{
|
|
|
|
if (disk->dev != &grub_util_biosdisk_dev || map[disk->id].device_map)
|
|
|
|
return disk->name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-18 12:02:13 +00:00
|
|
|
const char *
|
|
|
|
grub_util_biosdisk_get_osdev (grub_disk_t disk)
|
|
|
|
{
|
2011-09-28 12:07:53 +00:00
|
|
|
if (disk->dev != &grub_util_biosdisk_dev)
|
|
|
|
return 0;
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
return map[disk->id].device;
|
2010-09-15 13:30:43 +00:00
|
|
|
}
|