2003-01-02 20:12:33 +00:00
|
|
|
/* biosdisk.c - emulate biosdisk */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
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
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006 Free Software Foundation, Inc.
|
2003-01-02 20:12:33 +00:00
|
|
|
*
|
2004-04-04 13:46:03 +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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2004-04-04 13:46:03 +00:00
|
|
|
* along with GRUB; if not, write to the Free Software
|
2003-01-02 20:12:33 +00:00
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/machine/biosdisk.h>
|
|
|
|
#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>
|
|
|
|
#include <grub/pc_partition.h>
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/util/misc.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 */
|
|
|
|
# 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 */
|
2003-01-02 23:46:21 +00:00
|
|
|
# ifndef BLKGETSIZE
|
|
|
|
# define BLKGETSIZE _IO(0x12,96) /* return device size */
|
|
|
|
# endif /* ! BLKGETSIZE */
|
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__ */
|
|
|
|
|
|
|
|
static char *map[256];
|
|
|
|
|
|
|
|
#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__ */
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_drive (const char *name)
|
|
|
|
{
|
|
|
|
unsigned long drive;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ((name[0] != 'f' && name[0] != 'h') || name[1] != 'd')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
drive = strtoul (name + 2, &p, 10);
|
|
|
|
if (p == name + 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (name[0] == 'h')
|
|
|
|
drive += 0x80;
|
|
|
|
|
|
|
|
if (drive > sizeof (map) / sizeof (map[0]))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return (int) drive;
|
|
|
|
|
|
|
|
fail:
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a biosdisk");
|
2003-01-02 20:12:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
call_hook (int (*hook) (const char *name), int drive)
|
|
|
|
{
|
|
|
|
char name[10];
|
|
|
|
|
|
|
|
sprintf (name, (drive & 0x80) ? "hd%d" : "fd%d", drive & (~0x80));
|
|
|
|
return hook (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_biosdisk_iterate (int (*hook) (const char *name))
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
|
|
|
if (map[i] && call_hook (hook, i))
|
|
|
|
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;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
drive = get_drive (name);
|
|
|
|
if (drive < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (! map[drive])
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"no mapping exists for `%s'", name);
|
|
|
|
|
|
|
|
disk->has_partitions = (drive & 0x80);
|
|
|
|
disk->id = drive;
|
|
|
|
|
|
|
|
/* Get the size. */
|
2003-01-02 23:46:21 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
{
|
|
|
|
unsigned long nr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open (map[drive], O_RDONLY);
|
|
|
|
if (! fd)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", map[drive]);
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2003-01-07 07:54:07 +00:00
|
|
|
if (fstat (fd, &st) < 0 || ! S_ISBLK (st.st_mode))
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
if (ioctl (fd, BLKGETSIZE, &nr))
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
disk->total_sectors = nr;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("the size of %s is %lu", name, disk->total_sectors);
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 23:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
2004-03-14 17:48:25 +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
|
2003-01-02 23:46:21 +00:00
|
|
|
if (stat (map[drive], &st) < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_DEVICE, "cannot stat `%s'", map[drive]);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
disk->total_sectors = st.st_size >> GRUB_DISK_SECTOR_BITS;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("the size of %s is %lu", name, disk->total_sectors);
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
static int
|
|
|
|
linux_find_partition (char *dev, unsigned long sector)
|
|
|
|
{
|
|
|
|
size_t len = strlen (dev);
|
|
|
|
const char *format;
|
|
|
|
char *p;
|
|
|
|
int i;
|
2003-01-02 23:46:21 +00:00
|
|
|
char *real_dev;
|
|
|
|
|
|
|
|
real_dev = xstrdup (dev);
|
2003-01-02 20:12:33 +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";
|
|
|
|
}
|
2003-01-02 23:46:21 +00:00
|
|
|
else if ((strncmp (real_dev + 5, "hd", 2) == 0
|
|
|
|
|| strncmp (real_dev + 5, "sd", 2) == 0)
|
|
|
|
&& real_dev[7] >= 'a' && real_dev[7] <= 'z')
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2003-01-02 23:46:21 +00:00
|
|
|
p = real_dev + 8;
|
2003-01-02 20:12:33 +00:00
|
|
|
format = "%d";
|
|
|
|
}
|
2003-01-02 23:46:21 +00:00
|
|
|
else if (strncmp (real_dev + 5, "rd/c", 4) == 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2003-01-02 23:46:21 +00:00
|
|
|
p = strchr (real_dev + 9, 'd');
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! p)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
while (*p && isdigit (*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
format = "p%d";
|
|
|
|
}
|
|
|
|
else
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
|
|
|
free (real_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
for (i = 1; i < 10000; i++)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct hd_geometry hdg;
|
|
|
|
|
|
|
|
sprintf (p, format, i);
|
2003-01-02 23:46:21 +00:00
|
|
|
fd = open (real_dev, O_RDONLY);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (! fd)
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
|
|
|
free (real_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (ioctl (fd, HDIO_GETGEO, &hdg))
|
|
|
|
{
|
|
|
|
close (fd);
|
2003-01-02 23:46:21 +00:00
|
|
|
free (real_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
if (hdg.start == sector)
|
2003-01-02 23:46:21 +00:00
|
|
|
{
|
|
|
|
strcpy (dev, real_dev);
|
|
|
|
free (real_dev);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
free (real_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
#ifdef O_SYNC
|
|
|
|
flags |= O_SYNC;
|
|
|
|
#endif
|
|
|
|
#ifdef O_FSYNC
|
|
|
|
flags |= O_FSYNC;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
/* Linux has a bug that the disk cache for a whole disk is not consistent
|
|
|
|
with the one for a partition of the disk. */
|
|
|
|
{
|
|
|
|
int is_partition = 0;
|
|
|
|
char dev[PATH_MAX];
|
|
|
|
|
|
|
|
strcpy (dev, map[disk->id]);
|
|
|
|
if (disk->partition && strncmp (map[disk->id], "/dev/", 5) == 0)
|
|
|
|
is_partition = linux_find_partition (dev, disk->partition->start);
|
|
|
|
|
|
|
|
/* Open the partition. */
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("opening the device `%s'", dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
fd = open (dev, flags);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-01-02 23:46:21 +00:00
|
|
|
|
|
|
|
/* Make the buffer cache consistent with the physical disk. */
|
|
|
|
ioctl (fd, BLKFLSBUF, 0);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (is_partition)
|
|
|
|
sector -= disk->partition->start;
|
|
|
|
}
|
|
|
|
#else /* ! __linux__ */
|
|
|
|
fd = open (map[disk->id], flags);
|
2004-03-14 17:48:25 +00:00
|
|
|
if (fd < 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", map[disk->id]);
|
2003-01-02 20:12:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! __linux__ */
|
|
|
|
|
|
|
|
#if defined(__linux__) && (!defined(__GLIBC__) || \
|
|
|
|
((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1))))
|
|
|
|
/* Maybe libc doesn't have large file support. */
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
offset = (loff_t) sector << GRUB_DISK_SECTOR_BITS;
|
2003-01-02 20:12:33 +00:00
|
|
|
if (_llseek (fd, offset >> 32, offset & 0xffffffff, &result, SEEK_SET))
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot seek `%s'", map[disk->id]);
|
2003-01-02 20:12:33 +00:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
off_t offset = (off_t) sector << GRUB_DISK_SECTOR_BITS;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (lseek (fd, offset, SEEK_SET) != offset)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot seek `%s'", map[disk->id]);
|
2003-01-02 20:12:33 +00:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read LEN bytes from FD in BUF. Return less than or equal to zero if an
|
|
|
|
error occurs, otherwise return LEN. */
|
|
|
|
static ssize_t
|
|
|
|
nread (int fd, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t size = len;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t ret = read (fd, buf, len);
|
|
|
|
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= ret;
|
|
|
|
buf += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write LEN bytes from BUF to FD. Return less than or equal to zero if an
|
|
|
|
error occurs, otherwise return LEN. */
|
|
|
|
static ssize_t
|
|
|
|
nwrite (int fd, const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t size = len;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
ssize_t ret = write (fd, buf, len);
|
|
|
|
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= ret;
|
|
|
|
buf += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
fd = open_device (disk, sector, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
return grub_errno;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
if (sector == 0 && size > 1)
|
|
|
|
{
|
|
|
|
/* Work around a bug in linux's 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 */
|
2004-04-04 13:46:03 +00:00
|
|
|
if (nread (fd, buf, GRUB_DISK_SECTOR_SIZE) != GRUB_DISK_SECTOR_SIZE)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_READ_ERROR, "cannot read `%s'", map[disk->id]);
|
2003-01-02 20:12:33 +00:00
|
|
|
close (fd);
|
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
|
|
|
buf += GRUB_DISK_SECTOR_SIZE;
|
2003-01-02 20:12:33 +00:00
|
|
|
size--;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (nread (fd, buf, size << GRUB_DISK_SECTOR_BITS)
|
|
|
|
!= (ssize_t) (size << GRUB_DISK_SECTOR_BITS))
|
|
|
|
grub_error (GRUB_ERR_READ_ERROR, "cannot read from `%s'", map[disk->id]);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
close (fd);
|
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;
|
|
|
|
|
|
|
|
fd = open_device (disk, sector, O_WRONLY);
|
|
|
|
if (fd < 0)
|
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
|
|
|
if (nwrite (fd, buf, size << GRUB_DISK_SECTOR_BITS)
|
|
|
|
!= (ssize_t) (size << GRUB_DISK_SECTOR_BITS))
|
|
|
|
grub_error (GRUB_ERR_WRITE_ERROR, "cannot write to `%s'", map[disk->id]);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
close (fd);
|
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 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,
|
2003-01-02 20:12:33 +00:00
|
|
|
.close = 0,
|
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;
|
|
|
|
auto void show_error (const char *msg);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen (dev_map, "r");
|
|
|
|
if (! fp)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot open `%s'", dev_map);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
while (fgets (buf, sizeof (buf), fp))
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
char *e;
|
|
|
|
int drive;
|
|
|
|
|
|
|
|
lineno++;
|
|
|
|
|
|
|
|
/* 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 != '(')
|
|
|
|
show_error ("No open parenthesis found");
|
|
|
|
|
|
|
|
p++;
|
|
|
|
drive = get_drive (p);
|
|
|
|
if (drive < 0 || drive >= (int) (sizeof (map) / sizeof (map[0])))
|
|
|
|
show_error ("Bad device name");
|
|
|
|
|
|
|
|
p = strchr (p, ')');
|
|
|
|
if (! p)
|
|
|
|
show_error ("No close parenthesis found");
|
|
|
|
|
|
|
|
p++;
|
|
|
|
/* Skip leading spaces. */
|
|
|
|
while (*p && isspace (*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
show_error ("No filename found");
|
|
|
|
|
|
|
|
/* NUL-terminate the filename. */
|
|
|
|
e = p;
|
|
|
|
while (*e && ! isspace (*e))
|
|
|
|
e++;
|
|
|
|
*e = '\0';
|
|
|
|
|
|
|
|
/* Multiple entries for a given drive is not allowed. */
|
|
|
|
if (map[drive])
|
|
|
|
show_error ("Duplicated entry found");
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
/* On Linux, the devfs uses symbolic links horribly, and that
|
|
|
|
confuses the interface very much, so use realpath to expand
|
|
|
|
symbolic links. */
|
|
|
|
map[drive] = xmalloc (PATH_MAX);
|
|
|
|
if (! realpath (p, map[drive]))
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot get the real path of `%s'", p);
|
2003-01-02 20:12:33 +00:00
|
|
|
#else
|
|
|
|
map[drive] = xstrdup (p);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
|
|
|
free (map[i]);
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_dev_unregister (&grub_util_biosdisk_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
make_device_name (int drive, int dos_part, int bsd_part)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = xmalloc (30);
|
|
|
|
sprintf (p, (drive & 0x80) ? "hd%d" : "fd%d", drive & ~0x80);
|
|
|
|
|
|
|
|
if (dos_part >= 0)
|
2006-10-14 21:07:48 +00:00
|
|
|
sprintf (p + strlen (p), ",%d", dos_part + 1);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (bsd_part >= 0)
|
|
|
|
sprintf (p + strlen (p), ",%c", bsd_part + 'a');
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_os_disk (const char *os_dev)
|
|
|
|
{
|
|
|
|
char *path, *p;
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
path = xmalloc (PATH_MAX);
|
|
|
|
if (! realpath (os_dev, path))
|
|
|
|
return 0;
|
|
|
|
|
2003-01-02 23:46:21 +00:00
|
|
|
if (strncmp ("/dev/", path, 5) == 0)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
2003-01-02 23:46:21 +00:00
|
|
|
p = path + 5;
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (have_devfs ())
|
|
|
|
{
|
|
|
|
/* If this is an IDE disk. */
|
|
|
|
if (strncmp ("/dev/ide/", p, 9) == 0)
|
|
|
|
{
|
|
|
|
p = strstr (p, "part");
|
|
|
|
if (p)
|
|
|
|
strcpy (p, "disc");
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is a SCSI disk. */
|
|
|
|
if (strncmp ("/dev/scsi/", p, 10) == 0)
|
|
|
|
{
|
|
|
|
p = strstr (p, "part");
|
|
|
|
if (p)
|
|
|
|
strcpy (p, "disc");
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is an IDE disk or a SCSI disk. */
|
|
|
|
if ((strncmp ("hd", p, 2) == 0
|
|
|
|
|| strncmp ("sd", p, 2) == 0)
|
|
|
|
&& p[2] >= 'a' && p[2] <= 'z')
|
|
|
|
{
|
|
|
|
/* /dev/[hs]d[a-z][0-9]* */
|
|
|
|
p[3] = '\0';
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
#elif defined(__GNU__)
|
|
|
|
path = xstrdup (os_dev);
|
|
|
|
if (strncmp ("/dev/sd", path, 7) == 0 || strncmp ("/dev/hd", path, 7) == 0)
|
|
|
|
{
|
|
|
|
p = strchr (path, 's');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
|
|
|
|
#else
|
|
|
|
# warning "The function `get_os_disk' might not work on your OS correctly."
|
|
|
|
return xstrdup (os_dev);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_drive (const char *os_dev)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *os_disk;
|
|
|
|
|
|
|
|
os_disk = get_os_disk (os_dev);
|
|
|
|
if (! os_disk)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < (int) (sizeof (map) / sizeof (map[0])); i++)
|
|
|
|
if (map[i] && strcmp (map[i], os_disk) == 0)
|
|
|
|
{
|
|
|
|
free (os_disk);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (os_disk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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);
|
2003-01-02 20:12:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
drive = find_drive (os_dev);
|
|
|
|
if (drive < 0)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"no mapping exists for `%s'", os_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! S_ISBLK (st.st_mode))
|
|
|
|
return make_device_name (drive, -1, -1);
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
/* 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
|
2004-04-04 13:46:03 +00:00
|
|
|
compare it with each partition GRUB recognizes. */
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
|
|
|
char *name;
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_t disk;
|
2003-01-02 20:12:33 +00:00
|
|
|
int fd;
|
|
|
|
struct hd_geometry hdg;
|
|
|
|
int dos_part = -1;
|
|
|
|
int bsd_part = -1;
|
2005-08-18 03:14:39 +00:00
|
|
|
auto int find_partition (grub_disk_t disk,
|
|
|
|
const grub_partition_t partition);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
2005-08-18 03:14:39 +00:00
|
|
|
int find_partition (grub_disk_t disk __attribute__ ((unused)),
|
|
|
|
const grub_partition_t partition)
|
2003-01-02 20:12:33 +00:00
|
|
|
{
|
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
|
|
|
struct grub_pc_partition *pcdata = 0;
|
|
|
|
|
2005-08-07 17:12:52 +00:00
|
|
|
if (strcmp (partition->partmap->name, "pc_partition_map") == 0)
|
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
|
|
|
pcdata = partition->data;
|
|
|
|
|
|
|
|
if (pcdata)
|
|
|
|
{
|
|
|
|
if (pcdata->bsd_part < 0)
|
|
|
|
grub_util_info ("DOS partition %d starts from %lu",
|
|
|
|
pcdata->dos_part, partition->start);
|
|
|
|
else
|
|
|
|
grub_util_info ("BSD partition %d,%c starts from %lu",
|
|
|
|
pcdata->dos_part, pcdata->bsd_part + 'a',
|
|
|
|
partition->start);
|
|
|
|
}
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
if (hdg.start == partition->start)
|
|
|
|
{
|
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
|
|
|
if (pcdata)
|
|
|
|
{
|
|
|
|
dos_part = pcdata->dos_part;
|
|
|
|
bsd_part = pcdata->bsd_part;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dos_part = 0;
|
|
|
|
bsd_part = 0;
|
|
|
|
}
|
2003-01-02 20:12:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = make_device_name (drive, -1, -1);
|
|
|
|
|
|
|
|
if (MAJOR (st.st_rdev) == FLOPPY_MAJOR)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
fd = open (os_dev, O_RDONLY);
|
|
|
|
if (! fd)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", os_dev);
|
2003-01-02 20:12:33 +00:00
|
|
|
free (name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl (fd, HDIO_GETGEO, &hdg))
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"cannot get geometry of `%s'", os_dev);
|
|
|
|
close (fd);
|
|
|
|
free (name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
2003-01-02 23:46:21 +00:00
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("%s starts from %lu", os_dev, hdg.start);
|
2003-01-02 20:12:33 +00:00
|
|
|
|
|
|
|
if (hdg.start == 0)
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (! disk)
|
|
|
|
return 0;
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
if (grub_partition_iterate (disk, find_partition) != 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;
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:12:33 +00:00
|
|
|
if (dos_part < 0)
|
|
|
|
{
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_disk_close (disk);
|
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
2003-01-02 20:12:33 +00:00
|
|
|
"cannot find the partition of `%s'", os_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_device_name (drive, dos_part, bsd_part);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
p++;
|
2004-03-14 17:48:25 +00:00
|
|
|
n = strtol (p, &q, 10);
|
2003-01-02 20:12:33 +00:00
|
|
|
if (p != q && n != LONG_MIN && n != LONG_MAX)
|
|
|
|
{
|
|
|
|
dos_part = (int) n;
|
|
|
|
|
|
|
|
if (*q >= 'a' && *q <= 'g')
|
|
|
|
bsd_part = *q - 'a';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_device_name (drive, dos_part, bsd_part);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
}
|