vaseboot/VasEBoot-core/osdep/generic/blocklist.c

157 lines
4.5 KiB
C

/* VasEBoot-setup.c - make VAS_EBOOT usable */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
*
* VAS_EBOOT 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.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/file.h>
#include <VasEBoot/partition.h>
#include <VasEBoot/util/misc.h>
#include <VasEBoot/util/install.h>
#include <VasEBoot/emu/hostdisk.h>
#include <string.h>
#define MAX_TRIES 5
struct wrapper_hook_data
{
void (*callback) (VasEBoot_disk_addr_t sector,
unsigned int offset,
unsigned int length,
void *data);
void *callback_data;
};
static VasEBoot_err_t
callback_wrapper (VasEBoot_disk_addr_t sector,
unsigned int offset, unsigned int length,
char *buf, void *data)
{
struct wrapper_hook_data *wrap = data;
wrap->callback (sector, offset, length, wrap->callback_data);
return VAS_EBOOT_ERR_NONE;
}
void
VasEBoot_install_get_blocklist (VasEBoot_device_t root_dev,
const char *core_path, const char *core_img,
size_t core_size,
void (*callback) (VasEBoot_disk_addr_t sector,
unsigned offset,
unsigned length,
void *data),
void *hook_data)
{
int i;
char *tmp_img;
char *core_path_dev;
struct wrapper_hook_data wrap_hook_data = {
.callback = callback,
.callback_data = hook_data
};
core_path_dev = VasEBoot_make_system_path_relative_to_its_root (core_path);
/* Make sure that VAS_EBOOT reads the identical image as the OS. */
tmp_img = xmalloc (core_size);
for (i = 0; i < MAX_TRIES; i++)
{
VasEBoot_file_t file;
VasEBoot_util_info ((i == 0) ? _("attempting to read the core image `%s' from VAS_EBOOT")
: _("attempting to read the core image `%s' from VAS_EBOOT again"),
core_path_dev);
VasEBoot_disk_cache_invalidate_all ();
file = VasEBoot_file_open (core_path_dev, VAS_EBOOT_FILE_TYPE_NONE | VAS_EBOOT_FILE_TYPE_NO_DECOMPRESS);
if (file)
{
if (VasEBoot_file_size (file) != core_size)
VasEBoot_util_info ("succeeded in opening the core image but the size is different (%d != %d)",
(int) VasEBoot_file_size (file), (int) core_size);
else if (VasEBoot_file_read (file, tmp_img, core_size)
!= (VasEBoot_ssize_t) core_size)
VasEBoot_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
VasEBoot_util_info ("succeeded in opening the core image but the data is different");
}
else
{
VasEBoot_file_close (file);
break;
}
VasEBoot_file_close (file);
}
else
VasEBoot_util_info ("couldn't open the core image");
if (VasEBoot_errno)
VasEBoot_util_info ("error message = %s", VasEBoot_errmsg);
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
VasEBoot_util_biosdisk_flush (root_dev->disk);
sleep (1);
}
if (i == MAX_TRIES)
VasEBoot_util_error (_("cannot read `%s' correctly"), core_path_dev);
VasEBoot_file_t file;
/* Now read the core image to determine where the sectors are. */
file = VasEBoot_file_open (core_path_dev, VAS_EBOOT_FILE_TYPE_NONE | VAS_EBOOT_FILE_TYPE_NO_DECOMPRESS);
if (! file)
VasEBoot_util_error ("%s", VasEBoot_errmsg);
file->read_hook = callback_wrapper;
file->read_hook_data = &wrap_hook_data;
if (VasEBoot_file_read (file, tmp_img, core_size) != (VasEBoot_ssize_t) core_size)
VasEBoot_util_error ("%s", _("failed to read the sectors of the core image"));
VasEBoot_file_close (file);
free (tmp_img);
free (core_path_dev);
}