311 lines
8.2 KiB
C
311 lines
8.2 KiB
C
/* chainloader.c - boot another boot loader */
|
|
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 2002,2004,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/loader.h>
|
|
#include <VasEBoot/machine/chainloader.h>
|
|
#include <VasEBoot/machine/biosdisk.h>
|
|
#include <VasEBoot/machine/memory.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/memory.h>
|
|
#include <VasEBoot/dl.h>
|
|
#include <VasEBoot/command.h>
|
|
#include <VasEBoot/msdos_partition.h>
|
|
#include <VasEBoot/machine/biosnum.h>
|
|
#include <VasEBoot/cpu/floppy.h>
|
|
#include <VasEBoot/i18n.h>
|
|
#include <VasEBoot/video.h>
|
|
#include <VasEBoot/mm.h>
|
|
#include <VasEBoot/fat.h>
|
|
#include <VasEBoot/ntfs.h>
|
|
#include <VasEBoot/i386/relocator.h>
|
|
|
|
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
|
|
|
static VasEBoot_dl_t my_mod;
|
|
static int boot_drive;
|
|
static VasEBoot_addr_t boot_part_addr;
|
|
static struct VasEBoot_relocator *rel;
|
|
|
|
typedef enum
|
|
{
|
|
VAS_EBOOT_CHAINLOADER_FORCE = 0x1,
|
|
VAS_EBOOT_CHAINLOADER_BPB = 0x2,
|
|
} VasEBoot_chainloader_flags_t;
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_chainloader_boot (void)
|
|
{
|
|
struct VasEBoot_relocator16_state state = {
|
|
.edx = boot_drive,
|
|
.esi = boot_part_addr,
|
|
.ds = 0,
|
|
.es = 0,
|
|
.fs = 0,
|
|
.gs = 0,
|
|
.ss = 0,
|
|
.cs = 0,
|
|
.sp = VAS_EBOOT_MEMORY_MACHINE_BOOT_LOADER_ADDR,
|
|
.ip = VAS_EBOOT_MEMORY_MACHINE_BOOT_LOADER_ADDR,
|
|
.a20 = 0
|
|
};
|
|
VasEBoot_video_set_mode ("text", 0, 0);
|
|
|
|
return VasEBoot_relocator16_boot (rel, state);
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_chainloader_unload (void)
|
|
{
|
|
VasEBoot_relocator_unload (rel);
|
|
rel = NULL;
|
|
VasEBoot_dl_unref (my_mod);
|
|
return VAS_EBOOT_ERR_NONE;
|
|
}
|
|
|
|
void
|
|
VasEBoot_chainloader_patch_bpb (void *bs, VasEBoot_device_t dev, VasEBoot_uint8_t dl)
|
|
{
|
|
VasEBoot_uint32_t part_start = 0, heads = 0, sectors = 0;
|
|
if (dev && dev->disk)
|
|
{
|
|
part_start = VasEBoot_partition_get_start (dev->disk->partition);
|
|
if (dev->disk->data)
|
|
{
|
|
heads = ((struct VasEBoot_biosdisk_data *)(dev->disk->data))->heads;
|
|
sectors = ((struct VasEBoot_biosdisk_data *)(dev->disk->data))->sectors;
|
|
}
|
|
}
|
|
if (VasEBoot_memcmp ((char *) &((struct VasEBoot_ntfs_bpb *) bs)->oem_name,
|
|
"NTFS", 4) == 0)
|
|
{
|
|
struct VasEBoot_ntfs_bpb *bpb = (struct VasEBoot_ntfs_bpb *) bs;
|
|
bpb->num_hidden_sectors = VasEBoot_cpu_to_le32 (part_start);
|
|
bpb->bios_drive = dl;
|
|
return;
|
|
}
|
|
|
|
do
|
|
{
|
|
struct VasEBoot_fat_bpb *bpb = (struct VasEBoot_fat_bpb *) bs;
|
|
if (VasEBoot_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT12", 5)
|
|
&& VasEBoot_strncmp((const char *) bpb->version_specific.fat12_or_fat16.fstype, "FAT16", 5)
|
|
&& VasEBoot_strncmp((const char *) bpb->version_specific.fat32.fstype, "FAT32", 5))
|
|
break;
|
|
|
|
if (VasEBoot_le_to_cpu16 (bpb->bytes_per_sector) < 512
|
|
|| (VasEBoot_le_to_cpu16 (bpb->bytes_per_sector)
|
|
& (VasEBoot_le_to_cpu16 (bpb->bytes_per_sector) - 1)))
|
|
break;
|
|
|
|
if (bpb->sectors_per_cluster == 0
|
|
|| (bpb->sectors_per_cluster & (bpb->sectors_per_cluster - 1)))
|
|
break;
|
|
|
|
if (bpb->num_reserved_sectors == 0)
|
|
break;
|
|
if (bpb->num_total_sectors_16 == 0 && bpb->num_total_sectors_32 == 0)
|
|
break;
|
|
|
|
if (bpb->num_fats == 0)
|
|
break;
|
|
|
|
if (bpb->sectors_per_fat_16)
|
|
{
|
|
bpb->num_hidden_sectors = VasEBoot_cpu_to_le32 (part_start);
|
|
bpb->version_specific.fat12_or_fat16.num_ph_drive = dl;
|
|
if (sectors)
|
|
bpb->sectors_per_track = VasEBoot_cpu_to_le16 (sectors);
|
|
if (heads)
|
|
bpb->num_heads = VasEBoot_cpu_to_le16 (heads);
|
|
return;
|
|
}
|
|
if (bpb->version_specific.fat32.sectors_per_fat_32)
|
|
{
|
|
bpb->num_hidden_sectors = VasEBoot_cpu_to_le32 (part_start);
|
|
bpb->version_specific.fat32.num_ph_drive = dl;
|
|
if (sectors)
|
|
bpb->sectors_per_track = VasEBoot_cpu_to_le16 (sectors);
|
|
if (heads)
|
|
bpb->num_heads = VasEBoot_cpu_to_le16 (heads);
|
|
return;
|
|
}
|
|
break;
|
|
}
|
|
while (0);
|
|
}
|
|
|
|
static void
|
|
VasEBoot_chainloader_cmd (const char *filename, VasEBoot_chainloader_flags_t flags)
|
|
{
|
|
VasEBoot_file_t file = 0;
|
|
VasEBoot_uint16_t signature;
|
|
VasEBoot_device_t dev;
|
|
int drive = -1;
|
|
VasEBoot_addr_t part_addr = 0;
|
|
VasEBoot_uint8_t *bs, *ptable;
|
|
|
|
rel = VasEBoot_relocator_new ();
|
|
if (!rel)
|
|
goto fail;
|
|
|
|
VasEBoot_dl_ref (my_mod);
|
|
|
|
file = VasEBoot_file_open (filename, VAS_EBOOT_FILE_TYPE_PCCHAINLOADER
|
|
| VAS_EBOOT_FILE_TYPE_NO_DECOMPRESS);
|
|
if (! file)
|
|
goto fail;
|
|
|
|
{
|
|
VasEBoot_relocator_chunk_t ch;
|
|
VasEBoot_err_t err;
|
|
|
|
err = VasEBoot_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,
|
|
VAS_EBOOT_DISK_SECTOR_SIZE);
|
|
if (err)
|
|
goto fail;
|
|
bs = get_virtual_current_address (ch);
|
|
err = VasEBoot_relocator_alloc_chunk_addr (rel, &ch,
|
|
VAS_EBOOT_MEMORY_MACHINE_PART_TABLE_ADDR,
|
|
64);
|
|
if (err)
|
|
goto fail;
|
|
ptable = get_virtual_current_address (ch);
|
|
}
|
|
|
|
/* Read the first block. */
|
|
if (VasEBoot_file_read (file, bs, VAS_EBOOT_DISK_SECTOR_SIZE)
|
|
!= VAS_EBOOT_DISK_SECTOR_SIZE)
|
|
{
|
|
if (!VasEBoot_errno)
|
|
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
|
|
filename);
|
|
|
|
goto fail;
|
|
}
|
|
|
|
/* Check the signature. */
|
|
signature = *((VasEBoot_uint16_t *) (bs + VAS_EBOOT_DISK_SECTOR_SIZE - 2));
|
|
if (signature != VasEBoot_le_to_cpu16 (0xaa55)
|
|
&& ! (flags & VAS_EBOOT_CHAINLOADER_FORCE))
|
|
{
|
|
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "invalid signature");
|
|
goto fail;
|
|
}
|
|
|
|
VasEBoot_file_close (file);
|
|
|
|
/* Obtain the partition table from the root device. */
|
|
drive = VasEBoot_get_root_biosnumber ();
|
|
dev = VasEBoot_device_open (0);
|
|
if (dev && dev->disk && dev->disk->partition)
|
|
{
|
|
VasEBoot_disk_t disk = dev->disk;
|
|
|
|
if (disk)
|
|
{
|
|
VasEBoot_partition_t p = disk->partition;
|
|
|
|
if (p && VasEBoot_strcmp (p->partmap->name, "msdos") == 0)
|
|
{
|
|
disk->partition = p->parent;
|
|
VasEBoot_disk_read (disk, p->offset, 446, 64, ptable);
|
|
part_addr = (VAS_EBOOT_MEMORY_MACHINE_PART_TABLE_ADDR
|
|
+ (p->index << 4));
|
|
disk->partition = p;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (flags & VAS_EBOOT_CHAINLOADER_BPB)
|
|
VasEBoot_chainloader_patch_bpb ((void *) 0x7C00, dev, drive);
|
|
|
|
if (dev)
|
|
VasEBoot_device_close (dev);
|
|
|
|
/* Ignore errors. Perhaps it's not fatal. */
|
|
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
|
|
|
|
boot_drive = drive;
|
|
boot_part_addr = part_addr;
|
|
|
|
VasEBoot_loader_set (VasEBoot_chainloader_boot, VasEBoot_chainloader_unload, 1);
|
|
return;
|
|
|
|
fail:
|
|
|
|
if (file)
|
|
VasEBoot_file_close (file);
|
|
|
|
VasEBoot_dl_unref (my_mod);
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_cmd_chainloader (VasEBoot_command_t cmd __attribute__ ((unused)),
|
|
int argc, char *argv[])
|
|
{
|
|
VasEBoot_chainloader_flags_t flags = 0;
|
|
|
|
while (argc > 0)
|
|
{
|
|
if (VasEBoot_strcmp (argv[0], "--force") == 0)
|
|
{
|
|
flags |= VAS_EBOOT_CHAINLOADER_FORCE;
|
|
argc--;
|
|
argv++;
|
|
continue;
|
|
}
|
|
if (VasEBoot_strcmp (argv[0], "--bpb") == 0)
|
|
{
|
|
flags |= VAS_EBOOT_CHAINLOADER_BPB;
|
|
argc--;
|
|
argv++;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (argc == 0)
|
|
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
|
|
|
|
VasEBoot_chainloader_cmd (argv[0], flags);
|
|
|
|
return VasEBoot_errno;
|
|
}
|
|
|
|
static VasEBoot_command_t cmd;
|
|
|
|
VAS_EBOOT_MOD_INIT(chainloader)
|
|
{
|
|
cmd = VasEBoot_register_command ("chainloader", VasEBoot_cmd_chainloader,
|
|
N_("[--force|--bpb] FILE"),
|
|
N_("Load another boot loader."));
|
|
my_mod = mod;
|
|
}
|
|
|
|
VAS_EBOOT_MOD_FINI(chainloader)
|
|
{
|
|
VasEBoot_unregister_command (cmd);
|
|
}
|