Split out blocklist retrieving from setup.c to
grub-core/osdep/blocklist.c and add windows implementation since generic version doesn't work on NTFS on Windows due to aggressive unflushable cache.
This commit is contained in:
parent
c06c75201c
commit
5a869940de
13 changed files with 602 additions and 288 deletions
|
@ -26,7 +26,11 @@
|
|||
#include <grub/err.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/charset.h>
|
||||
#ifndef MODE_EXFAT
|
||||
#include <grub/fat.h>
|
||||
#else
|
||||
#include <grub/exfat.h>
|
||||
#endif
|
||||
#include <grub/i18n.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
@ -60,27 +64,6 @@ enum
|
|||
};
|
||||
|
||||
#ifdef MODE_EXFAT
|
||||
struct grub_exfat_bpb
|
||||
{
|
||||
grub_uint8_t jmp_boot[3];
|
||||
grub_uint8_t oem_name[8];
|
||||
grub_uint8_t mbz[53];
|
||||
grub_uint64_t num_hidden_sectors;
|
||||
grub_uint64_t num_total_sectors;
|
||||
grub_uint32_t num_reserved_sectors;
|
||||
grub_uint32_t sectors_per_fat;
|
||||
grub_uint32_t cluster_offset;
|
||||
grub_uint32_t cluster_count;
|
||||
grub_uint32_t root_cluster;
|
||||
grub_uint32_t num_serial;
|
||||
grub_uint16_t fs_revision;
|
||||
grub_uint16_t volume_flags;
|
||||
grub_uint8_t bytes_per_sector_shift;
|
||||
grub_uint8_t sectors_per_cluster_shift;
|
||||
grub_uint8_t num_fats;
|
||||
grub_uint8_t num_ph_drive;
|
||||
grub_uint8_t reserved[8];
|
||||
} __attribute__ ((packed));
|
||||
typedef struct grub_exfat_bpb grub_current_fat_bpb_t;
|
||||
#else
|
||||
typedef struct grub_fat_bpb grub_current_fat_bpb_t;
|
||||
|
@ -1208,6 +1191,29 @@ grub_fat_uuid (grub_device_t device, char **uuid)
|
|||
return grub_errno;
|
||||
}
|
||||
|
||||
#ifdef GRUB_UTIL
|
||||
#ifndef MODE_EXFAT
|
||||
grub_disk_addr_t
|
||||
grub_fat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
|
||||
#else
|
||||
grub_disk_addr_t
|
||||
grub_exfat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
|
||||
#endif
|
||||
{
|
||||
grub_disk_addr_t ret;
|
||||
struct grub_fat_data *data;
|
||||
data = grub_fat_mount (disk);
|
||||
if (!data)
|
||||
return 0;
|
||||
ret = data->cluster_sector;
|
||||
|
||||
*sec_per_lcn = 1ULL << data->cluster_bits;
|
||||
|
||||
grub_free (data);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct grub_fs grub_fat_fs =
|
||||
{
|
||||
#ifdef MODE_EXFAT
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include <grub/fshelp.h>
|
||||
#include <grub/charset.h>
|
||||
#include <grub/datetime.h>
|
||||
#include <grub/udf.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
|
@ -800,6 +801,24 @@ fail:
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef GRUB_UTIL
|
||||
grub_disk_addr_t
|
||||
grub_udf_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
|
||||
{
|
||||
grub_disk_addr_t ret;
|
||||
static struct grub_udf_data *data;
|
||||
|
||||
data = grub_udf_mount (disk);
|
||||
if (!data)
|
||||
return 0;
|
||||
|
||||
ret = U32 (data->pds[data->pms[0]->type1.part_num].start);
|
||||
*sec_per_lcn = 1ULL << data->lbshift;
|
||||
grub_free (data);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
static char *
|
||||
read_string (const grub_uint8_t *raw, grub_size_t sz, char *outbuf)
|
||||
{
|
||||
|
|
7
grub-core/osdep/blocklist.c
Normal file
7
grub-core/osdep/blocklist.c
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifdef __linux__
|
||||
#include "linux/blocklist.c"
|
||||
#elif defined (__MINGW32__) || defined (__CYGWIN__)
|
||||
#include "windows/blocklist.c"
|
||||
#else
|
||||
#include "generic/blocklist.c"
|
||||
#endif
|
132
grub-core/osdep/generic/blocklist.c
Normal file
132
grub-core/osdep/generic/blocklist.c
Normal file
|
@ -0,0 +1,132 @@
|
|||
/* grub-setup.c - make GRUB usable */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/file.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/install.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
|
||||
#define MAX_TRIES 5
|
||||
|
||||
void
|
||||
grub_install_get_blocklist (grub_device_t root_dev,
|
||||
const char *core_path, const char *core_img,
|
||||
size_t core_size,
|
||||
void (*callback) (grub_disk_addr_t sector,
|
||||
unsigned offset,
|
||||
unsigned length,
|
||||
void *data),
|
||||
void *hook_data)
|
||||
{
|
||||
int i;
|
||||
char *tmp_img;
|
||||
char *core_path_dev;
|
||||
|
||||
core_path_dev = grub_make_system_path_relative_to_its_root (core_path);
|
||||
|
||||
/* Make sure that GRUB reads the identical image as the OS. */
|
||||
tmp_img = xmalloc (core_size);
|
||||
|
||||
for (i = 0; i < MAX_TRIES; i++)
|
||||
{
|
||||
grub_file_t file;
|
||||
|
||||
grub_util_info ((i == 0) ? _("attempting to read the core image `%s' from GRUB")
|
||||
: _("attempting to read the core image `%s' from GRUB again"),
|
||||
core_path_dev);
|
||||
|
||||
grub_disk_cache_invalidate_all ();
|
||||
|
||||
grub_file_filter_disable_compression ();
|
||||
file = grub_file_open (core_path_dev);
|
||||
if (file)
|
||||
{
|
||||
if (grub_file_size (file) != core_size)
|
||||
grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
|
||||
(int) grub_file_size (file), (int) core_size);
|
||||
else if (grub_file_read (file, tmp_img, core_size)
|
||||
!= (grub_ssize_t) core_size)
|
||||
grub_util_info ("succeeded in opening the core image but cannot read %d bytes",
|
||||
(int) core_size);
|
||||
else if (memcmp (core_img, tmp_img, core_size) != 0)
|
||||
{
|
||||
#if 0
|
||||
FILE *dump;
|
||||
FILE *dump2;
|
||||
|
||||
dump = fopen ("dump.img", "wb");
|
||||
if (dump)
|
||||
{
|
||||
fwrite (tmp_img, 1, core_size, dump);
|
||||
fclose (dump);
|
||||
}
|
||||
|
||||
dump2 = fopen ("dump2.img", "wb");
|
||||
if (dump2)
|
||||
{
|
||||
fwrite (core_img, 1, core_size, dump2);
|
||||
fclose (dump2);
|
||||
}
|
||||
|
||||
#endif
|
||||
grub_util_info ("succeeded in opening the core image but the data is different");
|
||||
}
|
||||
else
|
||||
{
|
||||
grub_file_close (file);
|
||||
break;
|
||||
}
|
||||
|
||||
grub_file_close (file);
|
||||
}
|
||||
else
|
||||
grub_util_info ("couldn't open the core image");
|
||||
|
||||
if (grub_errno)
|
||||
grub_util_info ("error message = %s", grub_errmsg);
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
grub_util_biosdisk_flush (root_dev->disk);
|
||||
sleep (1);
|
||||
}
|
||||
|
||||
if (i == MAX_TRIES)
|
||||
grub_util_error (_("cannot read `%s' correctly"), core_path_dev);
|
||||
|
||||
grub_file_t file;
|
||||
/* Now read the core image to determine where the sectors are. */
|
||||
grub_file_filter_disable_compression ();
|
||||
file = grub_file_open (core_path_dev);
|
||||
if (! file)
|
||||
grub_util_error ("%s", grub_errmsg);
|
||||
|
||||
file->read_hook = callback;
|
||||
file->read_hook_data = hook_data;
|
||||
if (grub_file_read (file, tmp_img, core_size) != (grub_ssize_t) core_size)
|
||||
grub_util_error ("%s", _("failed to read the sectors of the core image"));
|
||||
|
||||
grub_file_close (file);
|
||||
free (tmp_img);
|
||||
|
||||
free (core_path_dev);
|
||||
}
|
144
grub-core/osdep/linux/blocklist.c
Normal file
144
grub-core/osdep/linux/blocklist.c
Normal file
|
@ -0,0 +1,144 @@
|
|||
/* grub-setup.c - make GRUB usable */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <assert.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/fiemap.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/install.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
void
|
||||
grub_install_get_blocklist (grub_device_t root_dev,
|
||||
const char *core_path,
|
||||
const char *core_img __attribute__ ((unused)),
|
||||
size_t core_size,
|
||||
void (*callback) (grub_disk_addr_t sector,
|
||||
unsigned offset,
|
||||
unsigned length,
|
||||
void *data),
|
||||
void *hook_data)
|
||||
{
|
||||
grub_partition_t container = root_dev->disk->partition;
|
||||
grub_uint64_t container_start = grub_partition_get_start (container);
|
||||
struct fiemap fie1;
|
||||
int fd;
|
||||
|
||||
/* Write the first two sectors of the core image onto the disk. */
|
||||
grub_util_info ("opening the core image `%s'", core_path);
|
||||
fd = open (core_path, O_RDONLY);
|
||||
if (fd < 0)
|
||||
grub_util_error (_("cannot open `%s': %s"), core_path,
|
||||
strerror (errno));
|
||||
|
||||
grub_memset (&fie1, 0, sizeof (fie1));
|
||||
fie1.fm_length = core_size;
|
||||
fie1.fm_flags = FIEMAP_FLAG_SYNC;
|
||||
|
||||
if (ioctl (fd, FS_IOC_FIEMAP, &fie1) < 0)
|
||||
{
|
||||
int nblocks, i, j;
|
||||
int bsize;
|
||||
int mul;
|
||||
|
||||
grub_util_info ("FIEMAP failed. Reverting to FIBMAP");
|
||||
|
||||
if (ioctl (fd, FIGETBSZ, &bsize) < 0)
|
||||
grub_util_error (_("can't retrieve blocklists: %s"),
|
||||
strerror (errno));
|
||||
if (bsize & (GRUB_DISK_SECTOR_SIZE - 1))
|
||||
grub_util_error ("%s", _("blocksize is not divisible by 512"));
|
||||
mul = bsize >> GRUB_DISK_SECTOR_BITS;
|
||||
nblocks = (core_size + bsize - 1) / bsize;
|
||||
if (mul == 0 || nblocks == 0)
|
||||
grub_util_error ("%s", _("can't retrieve blocklists"));
|
||||
for (i = 0; i < nblocks; i++)
|
||||
{
|
||||
unsigned blk = i;
|
||||
if (ioctl (fd, FIBMAP, &blk) < 0)
|
||||
grub_util_error (_("can't retrieve blocklists: %s"),
|
||||
strerror (errno));
|
||||
|
||||
for (j = 0; j < mul; j++)
|
||||
{
|
||||
int rest = core_size - ((i * mul + j) << GRUB_DISK_SECTOR_BITS);
|
||||
if (rest <= 0)
|
||||
break;
|
||||
if (rest > GRUB_DISK_SECTOR_SIZE)
|
||||
rest = GRUB_DISK_SECTOR_SIZE;
|
||||
callback (((grub_uint64_t) blk) * mul + j
|
||||
+ container_start,
|
||||
0, rest, hook_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
struct fiemap *fie2;
|
||||
int i, j;
|
||||
fie2 = xmalloc (sizeof (*fie2)
|
||||
+ fie1.fm_mapped_extents
|
||||
* sizeof (fie1.fm_extents[1]));
|
||||
memset (fie2, 0, sizeof (*fie2)
|
||||
+ fie1.fm_mapped_extents * sizeof (fie2->fm_extents[1]));
|
||||
fie2->fm_length = core_size;
|
||||
fie2->fm_flags = FIEMAP_FLAG_SYNC;
|
||||
fie2->fm_extent_count = fie1.fm_mapped_extents;
|
||||
if (ioctl (fd, FS_IOC_FIEMAP, fie2) < 0)
|
||||
grub_util_error (_("can't retrieve blocklists: %s"),
|
||||
strerror (errno));
|
||||
for (i = 0; i < fie2->fm_mapped_extents; i++)
|
||||
{
|
||||
for (j = 0;
|
||||
j < ((fie2->fm_extents[i].fe_length
|
||||
+ GRUB_DISK_SECTOR_SIZE - 1)
|
||||
>> GRUB_DISK_SECTOR_BITS);
|
||||
j++)
|
||||
{
|
||||
size_t len = (fie2->fm_extents[i].fe_length
|
||||
- j * GRUB_DISK_SECTOR_SIZE);
|
||||
if (len > GRUB_DISK_SECTOR_SIZE)
|
||||
len = GRUB_DISK_SECTOR_SIZE;
|
||||
callback ((fie2->fm_extents[i].fe_physical
|
||||
>> GRUB_DISK_SECTOR_BITS)
|
||||
+ j + container_start,
|
||||
fie2->fm_extents[i].fe_physical
|
||||
& (GRUB_DISK_SECTOR_SIZE - 1), len, hook_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
close (fd);
|
||||
}
|
119
grub-core/osdep/windows/blocklist.c
Normal file
119
grub-core/osdep/windows/blocklist.c
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GRUB 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
|
||||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <grub/disk.h>
|
||||
#include <grub/partition.h>
|
||||
#include <grub/fs.h>
|
||||
#include <grub/ntfs.h>
|
||||
#include <grub/fat.h>
|
||||
#include <grub/exfat.h>
|
||||
#include <grub/udf.h>
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/install.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/emu/hostfile.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
void
|
||||
grub_install_get_blocklist (grub_device_t root_dev,
|
||||
const char *core_path,
|
||||
const char *core_img __attribute__ ((unused)),
|
||||
size_t core_size,
|
||||
void (*callback) (grub_disk_addr_t sector,
|
||||
unsigned offset,
|
||||
unsigned length,
|
||||
void *data),
|
||||
void *hook_data)
|
||||
{
|
||||
grub_disk_addr_t first_lcn = 0;
|
||||
HANDLE filehd;
|
||||
DWORD rets;
|
||||
RETRIEVAL_POINTERS_BUFFER *extbuf;
|
||||
size_t extbuf_size;
|
||||
DWORD i, j, k;
|
||||
grub_uint64_t sec_per_lcn;
|
||||
grub_uint64_t curvcn = 0;
|
||||
STARTING_VCN_INPUT_BUFFER start_vcn;
|
||||
grub_fs_t fs;
|
||||
grub_err_t err;
|
||||
|
||||
fs = grub_fs_probe (root_dev);
|
||||
if (!fs)
|
||||
grub_util_error ("%s", grub_errmsg);
|
||||
|
||||
/* This is ugly but windows doesn't give all needed data. Or does anyone
|
||||
have a pointer how to retrieve it?
|
||||
*/
|
||||
if (grub_strcmp (fs->name, "ntfs") == 0)
|
||||
{
|
||||
struct grub_ntfs_bpb bpb;
|
||||
err = grub_disk_read (root_dev->disk, 0, 0, sizeof (bpb), &bpb);
|
||||
if (err)
|
||||
grub_util_error ("%s", grub_errmsg);
|
||||
sec_per_lcn = ((grub_uint32_t) bpb.sectors_per_cluster
|
||||
* (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
|
||||
>> 9;
|
||||
first_lcn = 0;
|
||||
}
|
||||
else if (grub_strcmp (fs->name, "exfat") == 0)
|
||||
first_lcn = grub_exfat_get_cluster_sector (root_dev->disk, &sec_per_lcn);
|
||||
else if (grub_strcmp (fs->name, "fat") == 0)
|
||||
first_lcn = grub_fat_get_cluster_sector (root_dev->disk, &sec_per_lcn);
|
||||
else if (grub_strcmp (fs->name, "udf") == 0)
|
||||
first_lcn = grub_udf_get_cluster_sector (root_dev->disk, &sec_per_lcn);
|
||||
else
|
||||
grub_util_error ("unsupported fs for blocklist under windows: %s",
|
||||
fs->name);
|
||||
|
||||
grub_util_info ("sec_per_lcn = %lld, first_lcn=%lld", sec_per_lcn, first_lcn);
|
||||
|
||||
first_lcn += grub_partition_get_start (root_dev->disk->partition);
|
||||
|
||||
start_vcn.StartingVcn.QuadPart = 0;
|
||||
|
||||
filehd = grub_util_fd_open (core_path, GRUB_UTIL_FD_O_RDONLY);
|
||||
if (!GRUB_UTIL_FD_IS_VALID (filehd))
|
||||
grub_util_error (_("cannot open `%s': %s"), core_path,
|
||||
grub_util_fd_strerror ());
|
||||
|
||||
extbuf_size = sizeof (*extbuf) + sizeof (extbuf->Extents[0])
|
||||
* ((core_size + 511) / 512);
|
||||
extbuf = xmalloc (extbuf_size);
|
||||
|
||||
if (!DeviceIoControl(filehd, FSCTL_GET_RETRIEVAL_POINTERS,
|
||||
&start_vcn, sizeof (start_vcn),
|
||||
extbuf, extbuf_size, &rets, NULL))
|
||||
grub_util_error ("FSCTL_GET_RETRIEVAL_POINTERS fails: %s",
|
||||
grub_util_fd_strerror ());
|
||||
|
||||
CloseHandle (filehd);
|
||||
|
||||
for (i = 0; i < extbuf->ExtentCount; i++)
|
||||
{
|
||||
for (j = 0; j < extbuf->Extents[i].NextVcn.QuadPart - curvcn; j++)
|
||||
for (k = 0; k < sec_per_lcn; k++)
|
||||
callback ((extbuf->Extents[i].Lcn.QuadPart + j)
|
||||
* sec_per_lcn + first_lcn
|
||||
+ k, 0, 512, hook_data);
|
||||
}
|
||||
free (extbuf);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue