vaseboot/VasEBoot-core/commands/efi/loadbios.c

235 lines
6.4 KiB
C

/* loadbios.c - command to load a bios dump */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2009 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/dl.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/file.h>
#include <VasEBoot/efi/efi.h>
#include <VasEBoot/pci.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
static VasEBoot_efi_guid_t acpi_guid = VasEBoot_EFI_ACPI_TABLE_GUID;
static VasEBoot_efi_guid_t acpi2_guid = VasEBoot_EFI_ACPI_20_TABLE_GUID;
static VasEBoot_efi_guid_t smbios_guid = VasEBoot_EFI_SMBIOS_TABLE_GUID;
static VasEBoot_efi_guid_t smbios3_guid = VasEBoot_EFI_SMBIOS3_TABLE_GUID;
#define EBDA_SEG_ADDR 0x40e
#define LOW_MEM_ADDR 0x413
#define FAKE_EBDA_SEG 0x9fc0
#define BLANK_MEM 0xffffffff
#define VBIOS_ADDR 0xc0000
#define SBIOS_ADDR 0xf0000
static int
enable_rom_area (void)
{
VasEBoot_pci_address_t addr;
VasEBoot_uint32_t *rom_ptr;
VasEBoot_pci_device_t dev = { .bus = 0, .device = 0, .function = 0};
rom_ptr = (VasEBoot_uint32_t *) VBIOS_ADDR;
if (*rom_ptr != BLANK_MEM)
{
VasEBoot_puts_ (N_("ROM image is present."));
return 0;
}
/* FIXME: should be macroified. */
addr = VasEBoot_pci_make_address (dev, 144);
VasEBoot_pci_write_byte (addr++, 0x30);
VasEBoot_pci_write_byte (addr++, 0x33);
VasEBoot_pci_write_byte (addr++, 0x33);
VasEBoot_pci_write_byte (addr++, 0x33);
VasEBoot_pci_write_byte (addr++, 0x33);
VasEBoot_pci_write_byte (addr++, 0x33);
VasEBoot_pci_write_byte (addr++, 0x33);
VasEBoot_pci_write_byte (addr, 0);
*rom_ptr = 0;
if (*rom_ptr != 0)
{
VasEBoot_puts_ (N_("Can\'t enable ROM area."));
return 0;
}
return 1;
}
static void
lock_rom_area (void)
{
VasEBoot_pci_address_t addr;
VasEBoot_pci_device_t dev = { .bus = 0, .device = 0, .function = 0};
/* FIXME: should be macroified. */
addr = VasEBoot_pci_make_address (dev, 144);
VasEBoot_pci_write_byte (addr++, 0x10);
VasEBoot_pci_write_byte (addr++, 0x11);
VasEBoot_pci_write_byte (addr++, 0x11);
VasEBoot_pci_write_byte (addr++, 0x11);
VasEBoot_pci_write_byte (addr, 0x11);
}
static void
fake_bios_data (int use_rom)
{
unsigned i;
void *acpi, *smbios, *smbios3;
VasEBoot_uint16_t *ebda_seg_ptr, *low_mem_ptr;
ebda_seg_ptr = (VasEBoot_uint16_t *) EBDA_SEG_ADDR;
low_mem_ptr = (VasEBoot_uint16_t *) LOW_MEM_ADDR;
if ((*ebda_seg_ptr) || (*low_mem_ptr))
return;
acpi = 0;
smbios = 0;
smbios3 = 0;
for (i = 0; i < VasEBoot_efi_system_table->num_table_entries; i++)
{
VasEBoot_efi_packed_guid_t *guid =
&VasEBoot_efi_system_table->configuration_table[i].vendor_guid;
if (! VasEBoot_memcmp (guid, &acpi2_guid, sizeof (VasEBoot_efi_guid_t)))
{
acpi = VasEBoot_efi_system_table->configuration_table[i].vendor_table;
VasEBoot_dprintf ("efi", "ACPI2: %p\n", acpi);
}
else if (! VasEBoot_memcmp (guid, &acpi_guid, sizeof (VasEBoot_efi_guid_t)))
{
void *t;
t = VasEBoot_efi_system_table->configuration_table[i].vendor_table;
if (! acpi)
acpi = t;
VasEBoot_dprintf ("efi", "ACPI: %p\n", t);
}
else if (! VasEBoot_memcmp (guid, &smbios_guid, sizeof (VasEBoot_efi_guid_t)))
{
smbios = VasEBoot_efi_system_table->configuration_table[i].vendor_table;
VasEBoot_dprintf ("efi", "SMBIOS: %p\n", smbios);
}
else if (! VasEBoot_memcmp (guid, &smbios3_guid, sizeof (VasEBoot_efi_guid_t)))
{
smbios3 = VasEBoot_efi_system_table->configuration_table[i].vendor_table;
VasEBoot_dprintf ("efi", "SMBIOS3: %p\n", smbios3);
}
}
*ebda_seg_ptr = FAKE_EBDA_SEG;
*low_mem_ptr = (FAKE_EBDA_SEG >> 6);
*((VasEBoot_uint16_t *) (FAKE_EBDA_SEG << 4)) = 640 - *low_mem_ptr;
if (acpi)
VasEBoot_memcpy ((char *) ((FAKE_EBDA_SEG << 4) + 16), acpi, 1024 - 16);
if (use_rom)
{
if (smbios)
VasEBoot_memcpy ((char *) SBIOS_ADDR, (char *) smbios, 31);
if (smbios3)
VasEBoot_memcpy ((char *) SBIOS_ADDR + 32, (char *) smbios3, 24);
}
}
static VasEBoot_err_t
VasEBoot_cmd_fakebios (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char *argv[] __attribute__ ((unused)))
{
if (enable_rom_area ())
{
fake_bios_data (1);
lock_rom_area ();
}
else
fake_bios_data (0);
return 0;
}
static VasEBoot_err_t
VasEBoot_cmd_loadbios (VasEBoot_command_t cmd __attribute__ ((unused)),
int argc, char *argv[])
{
VasEBoot_file_t file;
int size;
if (argc == 0)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("filename expected"));
if (argc > 1)
{
file = VasEBoot_file_open (argv[1]);
if (! file)
return VasEBoot_errno;
if (file->size != 4)
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "invalid int10 dump size");
else
VasEBoot_file_read (file, (void *) 0x40, 4);
VasEBoot_file_close (file);
if (VasEBoot_errno)
return VasEBoot_errno;
}
file = VasEBoot_file_open (argv[0]);
if (! file)
return VasEBoot_errno;
size = file->size;
if ((size < 0x10000) || (size > 0x40000))
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "invalid bios dump size");
else if (enable_rom_area ())
{
VasEBoot_file_read (file, (void *) VBIOS_ADDR, size);
fake_bios_data (size <= 0x40000);
lock_rom_area ();
}
VasEBoot_file_close (file);
return VasEBoot_errno;
}
static VasEBoot_command_t cmd_fakebios, cmd_loadbios;
VasEBoot_MOD_INIT(loadbios)
{
cmd_fakebios = VasEBoot_register_command ("fakebios", VasEBoot_cmd_fakebios,
0, N_("Create BIOS-like structures for"
" backward compatibility with"
" existing OS."));
cmd_loadbios = VasEBoot_register_command ("loadbios", VasEBoot_cmd_loadbios,
N_("BIOS_DUMP [INT10_DUMP]"),
N_("Load BIOS dump."));
}
VasEBoot_MOD_FINI(loadbios)
{
VasEBoot_unregister_command (cmd_fakebios);
VasEBoot_unregister_command (cmd_loadbios);
}