vaseboot/VasEBoot-core/commands/testload.c

173 lines
4.4 KiB
C

/* testload.c - load the same file in multiple ways */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2003,2005,2006,2007,2009,2010 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 <VasEBoot/dl.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/err.h>
#include <VasEBoot/env.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/file.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/term.h>
#include <VasEBoot/loader.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
/* Helper for VasEBoot_cmd_testload. */
static VasEBoot_err_t
read_progress (VasEBoot_disk_addr_t sector __attribute__ ((unused)),
unsigned offset __attribute__ ((unused)),
unsigned len,
char *buf __attribute__ ((unused)),
void *data __attribute__ ((unused)))
{
for (; len >= VAS_EBOOT_DISK_SECTOR_SIZE; len -= VAS_EBOOT_DISK_SECTOR_SIZE)
VasEBoot_xputs (".");
if (len)
VasEBoot_xputs (".");
VasEBoot_refresh ();
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_testload (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_file_t file;
char *buf;
VasEBoot_size_t size;
VasEBoot_off_t pos;
if (argc < 1)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
file = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_TESTLOAD);
if (! file)
return VasEBoot_errno;
size = VasEBoot_file_size (file) & ~(VAS_EBOOT_DISK_SECTOR_SIZE - 1);
if (size == 0)
{
VasEBoot_file_close (file);
return VAS_EBOOT_ERR_NONE;
}
buf = VasEBoot_malloc (size);
if (! buf)
goto fail;
VasEBoot_printf ("Reading %s sequentially", argv[0]);
file->read_hook = read_progress;
if (VasEBoot_file_read (file, buf, size) != (VasEBoot_ssize_t) size)
goto fail;
VasEBoot_printf (" Done.\n");
/* Read sequentially again. */
VasEBoot_printf ("Reading %s sequentially again", argv[0]);
VasEBoot_file_seek (file, 0);
for (pos = 0; pos < size;)
{
char sector[VAS_EBOOT_DISK_SECTOR_SIZE];
VasEBoot_size_t curlen = VAS_EBOOT_DISK_SECTOR_SIZE;
if (curlen > size - pos)
curlen = size - pos;
if (VasEBoot_file_read (file, sector, curlen)
!= (VasEBoot_ssize_t) curlen)
goto fail;
if (VasEBoot_memcmp (sector, buf + pos, curlen) != 0)
{
VasEBoot_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
goto fail;
}
pos += curlen;
}
VasEBoot_printf (" Done.\n");
/* Read backwards and compare. */
VasEBoot_printf ("Reading %s backwards", argv[0]);
pos = size;
while (pos > 0)
{
char sector[VAS_EBOOT_DISK_SECTOR_SIZE];
if (pos >= VAS_EBOOT_DISK_SECTOR_SIZE)
pos -= VAS_EBOOT_DISK_SECTOR_SIZE;
else
pos = 0;
VasEBoot_file_seek (file, pos);
if (VasEBoot_file_read (file, sector, VAS_EBOOT_DISK_SECTOR_SIZE)
!= VAS_EBOOT_DISK_SECTOR_SIZE)
goto fail;
if (VasEBoot_memcmp (sector, buf + pos, VAS_EBOOT_DISK_SECTOR_SIZE) != 0)
{
int i;
VasEBoot_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
for (i = 0; i < VAS_EBOOT_DISK_SECTOR_SIZE; i++)
{
VasEBoot_printf ("%02x ", buf[pos + i]);
if ((i & 15) == 15)
VasEBoot_printf ("\n");
}
if (i)
VasEBoot_refresh ();
goto fail;
}
}
VasEBoot_printf (" Done.\n");
return VAS_EBOOT_ERR_NONE;
fail:
VasEBoot_file_close (file);
VasEBoot_free (buf);
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_IO, "bad read");
return VasEBoot_errno;
}
static VasEBoot_command_t cmd;
VAS_EBOOT_MOD_INIT(testload)
{
cmd =
VasEBoot_register_command ("testload", VasEBoot_cmd_testload,
N_("FILE"),
N_("Load the same file in multiple ways."));
}
VAS_EBOOT_MOD_FINI(testload)
{
VasEBoot_unregister_command (cmd);
}