vaseboot/VasEBoot-core/disk/memdisk.c

117 lines
3.4 KiB
C

/* memdisk.c - Access embedded memory disk. */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2007,2008 Free Software Foundation, Inc.
*
* VasEBoot 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.
*
* VasEBoot 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 VasEBoot. 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>
VasEBoot_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 != VasEBoot_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 (VasEBoot_ERR_UNKNOWN_DEVICE, "not a memdisk");
disk->total_sectors = memdisk_size / VasEBoot_DISK_SECTOR_SIZE;
disk->max_agglomerate = VasEBoot_DISK_MAX_MAX_AGGLOMERATE;
disk->id = 0;
return VasEBoot_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 << VasEBoot_DISK_SECTOR_BITS), size << VasEBoot_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 << VasEBoot_DISK_SECTOR_BITS), buf, size << VasEBoot_DISK_SECTOR_BITS);
return 0;
}
static struct VasEBoot_disk_dev VasEBoot_memdisk_dev =
{
.name = "memdisk",
.id = VasEBoot_DISK_DEVICE_MEMDISK_ID,
.iterate = VasEBoot_memdisk_iterate,
.open = VasEBoot_memdisk_open,
.close = VasEBoot_memdisk_close,
.read = VasEBoot_memdisk_read,
.write = VasEBoot_memdisk_write,
.next = 0
};
VasEBoot_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);
memdisk_size = header->size - sizeof (struct VasEBoot_module_header);
memdisk_addr = VasEBoot_malloc (memdisk_size);
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;
}
}
VasEBoot_MOD_FINI(memdisk)
{
if (! memdisk_size)
return;
VasEBoot_free (memdisk_addr);
VasEBoot_disk_dev_unregister (&VasEBoot_memdisk_dev);
}