vaseboot/VasEBoot-core/loader/i386/pc/pxechainloader.c

169 lines
4.3 KiB
C

/* chainloader.c - boot another boot loader */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2002,2004,2007,2009,2010,2012 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/loader.h>
#include <VasEBoot/file.h>
#include <VasEBoot/err.h>
#include <VasEBoot/device.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/types.h>
#include <VasEBoot/partition.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/command.h>
#include <VasEBoot/machine/biosnum.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/video.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/cpu/relocator.h>
#include <VasEBoot/machine/pxe.h>
#include <VasEBoot/net.h>
static VasEBoot_dl_t my_mod;
static struct VasEBoot_relocator *rel;
static VasEBoot_uint32_t edx = 0xffffffff;
static char boot_file[128];
static char server_name[64];
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static VasEBoot_err_t
VasEBoot_pxechain_boot (void)
{
struct VasEBoot_relocator16_state state = {
.cs = 0,
.ip = 0x7c00,
.ds = 0,
.es = 0,
.fs = 0,
.gs = 0,
.ss = 0,
.sp = 0x7c00,
.edx = edx
};
struct VasEBoot_net_bootp_packet *bp;
bp = VasEBoot_pxe_get_cached (VAS_EBOOT_PXENV_PACKET_TYPE_DHCP_ACK);
VasEBoot_video_set_mode ("text", 0, 0);
if (bp && boot_file[0])
VasEBoot_memcpy (bp->boot_file, boot_file, sizeof (bp->boot_file));
if (bp && server_name[0])
VasEBoot_memcpy (bp->server_name, server_name, sizeof (bp->server_name));
return VasEBoot_relocator16_boot (rel, state);
}
static VasEBoot_err_t
VasEBoot_pxechain_unload (void)
{
VasEBoot_relocator_unload (rel);
rel = NULL;
VasEBoot_dl_unref (my_mod);
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_cmd_pxechain (VasEBoot_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_file_t file = 0;
VasEBoot_err_t err;
void *image;
VasEBoot_size_t imagesize;
char *fname;
if (argc == 0)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
VasEBoot_dl_ref (my_mod);
rel = VasEBoot_relocator_new ();
if (!rel)
goto fail;
file = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_PXECHAINLOADER);
if (! file)
goto fail;
if (file->device->net && file->device->net->name)
fname = file->device->net->name;
else
{
fname = argv[0];
if (fname[0] == '(')
{
fname = VasEBoot_strchr (fname, ')');
if (fname)
fname++;
else
fname = argv[0];
}
}
VasEBoot_memset (boot_file, 0, sizeof (boot_file));
VasEBoot_strncpy (boot_file, fname, sizeof (boot_file));
VasEBoot_memset (server_name, 0, sizeof (server_name));
if (file->device->net && file->device->net->server)
VasEBoot_strncpy (server_name, file->device->net->server, sizeof (server_name));
edx = VasEBoot_get_root_biosnumber ();
imagesize = VasEBoot_file_size (file);
{
VasEBoot_relocator_chunk_t ch;
err = VasEBoot_relocator_alloc_chunk_addr (rel, &ch, 0x7c00, imagesize);
if (err)
goto fail;
image = get_virtual_current_address (ch);
}
if (VasEBoot_file_read (file, image, imagesize) != (VasEBoot_ssize_t) imagesize)
goto fail;
VasEBoot_loader_set (VasEBoot_pxechain_boot, VasEBoot_pxechain_unload,
VAS_EBOOT_LOADER_FLAG_NORETURN | VAS_EBOOT_LOADER_FLAG_PXE_NOT_UNLOAD);
return VAS_EBOOT_ERR_NONE;
fail:
if (file)
VasEBoot_file_close (file);
VasEBoot_pxechain_unload ();
return VasEBoot_errno;
}
static VasEBoot_command_t cmd;
VAS_EBOOT_MOD_INIT(pxechainloader)
{
cmd = VasEBoot_register_command ("pxechainloader", VasEBoot_cmd_pxechain,
0, N_("Load a PXE image."));
my_mod = mod;
}
VAS_EBOOT_MOD_FINI(pxechainloader)
{
VasEBoot_unregister_command (cmd);
}