vaseboot/VasEBoot-core/disk/memdisk.c

124 lines
3.7 KiB
C

/* memdisk.c - Access embedded memory disk. */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2007,2008 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/disk.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/kernel.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/types.h>
#include <VasEBoot/safemath.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static char *memdisk_addr;
static VasEBoot_off_t memdisk_size = 0;
static int
VasEBoot_memdisk_iterate (VasEBoot_disk_dev_iterate_hook_t hook, void *hook_data,
VasEBoot_disk_pull_t pull)
{
if (pull != VAS_EBOOT_DISK_PULL_NONE)
return 0;
return hook ("memdisk", hook_data);
}
static VasEBoot_err_t
VasEBoot_memdisk_open (const char *name, VasEBoot_disk_t disk)
{
if (VasEBoot_strcmp (name, "memdisk"))
return VasEBoot_error (VAS_EBOOT_ERR_UNKNOWN_DEVICE, "not a memdisk");
disk->total_sectors = memdisk_size / VAS_EBOOT_DISK_SECTOR_SIZE;
disk->max_agglomerate = VAS_EBOOT_DISK_MAX_MAX_AGGLOMERATE;
disk->id = 0;
return VAS_EBOOT_ERR_NONE;
}
static void
VasEBoot_memdisk_close (VasEBoot_disk_t disk __attribute((unused)))
{
}
static VasEBoot_err_t
VasEBoot_memdisk_read (VasEBoot_disk_t disk __attribute((unused)), VasEBoot_disk_addr_t sector,
VasEBoot_size_t size, char *buf)
{
VasEBoot_memcpy (buf, memdisk_addr + (sector << VAS_EBOOT_DISK_SECTOR_BITS), size << VAS_EBOOT_DISK_SECTOR_BITS);
return 0;
}
static VasEBoot_err_t
VasEBoot_memdisk_write (VasEBoot_disk_t disk __attribute((unused)), VasEBoot_disk_addr_t sector,
VasEBoot_size_t size, const char *buf)
{
VasEBoot_memcpy (memdisk_addr + (sector << VAS_EBOOT_DISK_SECTOR_BITS), buf, size << VAS_EBOOT_DISK_SECTOR_BITS);
return 0;
}
static struct VasEBoot_disk_dev VasEBoot_memdisk_dev =
{
.name = "memdisk",
.id = VAS_EBOOT_DISK_DEVICE_MEMDISK_ID,
.disk_iterate = VasEBoot_memdisk_iterate,
.disk_open = VasEBoot_memdisk_open,
.disk_close = VasEBoot_memdisk_close,
.disk_read = VasEBoot_memdisk_read,
.disk_write = VasEBoot_memdisk_write,
.next = 0
};
VAS_EBOOT_MOD_INIT(memdisk)
{
struct VasEBoot_module_header *header;
FOR_MODULES (header)
if (header->type == OBJ_TYPE_MEMDISK)
{
char *memdisk_orig_addr;
memdisk_orig_addr = (char *) header + sizeof (struct VasEBoot_module_header);
VasEBoot_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr);
if (VasEBoot_sub (header->size, sizeof (struct VasEBoot_module_header), &memdisk_size))
{
VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, "underflow detected while obtaining memdisk size");
return;
}
memdisk_addr = VasEBoot_malloc (memdisk_size);
if (memdisk_addr == NULL)
return;
VasEBoot_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n");
VasEBoot_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size);
VasEBoot_disk_dev_register (&VasEBoot_memdisk_dev);
break;
}
}
VAS_EBOOT_MOD_FINI(memdisk)
{
if (! memdisk_size)
return;
VasEBoot_free (memdisk_addr);
VasEBoot_disk_dev_unregister (&VasEBoot_memdisk_dev);
}