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

608 lines
17 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 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/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/msdos_partition.h>
#include <VasEBoot/scsi.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/video.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/cpu/relocator.h>
#include <VasEBoot/extcmd.h>
#include <VasEBoot/verify.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static VasEBoot_dl_t my_mod;
static struct VasEBoot_relocator *rel;
static VasEBoot_uint32_t eip = 0xffffffff;
#define VAS_EBOOT_PLAN9_TARGET 0x100000
#define VAS_EBOOT_PLAN9_ALIGN 4096
#define VAS_EBOOT_PLAN9_CONFIG_ADDR 0x001200
#define VAS_EBOOT_PLAN9_CONFIG_PATH_SIZE 0x000040
#define VAS_EBOOT_PLAN9_CONFIG_MAGIC "ZORT 0\r\n"
static const struct VasEBoot_arg_option options[] =
{
{"map", 'm', VAS_EBOOT_ARG_OPTION_REPEATABLE,
/* TRANSLATORS: it's about guessing which VAS_EBOOT disk
is which Plan9 disk. If your language has no
word "mapping" you can use another word which
means that the VAS_EBOOTDEVICE and PLAN9DEVICE are
actually the same device, just named differently
in OS and VAS_EBOOT. */
N_("Override guessed mapping of Plan9 devices."),
N_("VAS_EBOOTDEVICE=PLAN9DEVICE"),
ARG_TYPE_STRING},
{0, 0, 0, 0, 0, 0}
};
struct VasEBoot_plan9_header
{
VasEBoot_uint32_t magic;
#define VAS_EBOOT_PLAN9_MAGIC 0x1eb
VasEBoot_uint32_t text_size;
VasEBoot_uint32_t data_size;
VasEBoot_uint32_t bss_size;
VasEBoot_uint32_t sectiona;
VasEBoot_uint32_t entry_addr;
VasEBoot_uint32_t zero;
VasEBoot_uint32_t sectionb;
};
static VasEBoot_err_t
VasEBoot_plan9_boot (void)
{
struct VasEBoot_relocator32_state state = {
.eax = 0,
.eip = eip,
.ebx = 0,
.ecx = 0,
.edx = 0,
.edi = 0,
.esp = 0,
.ebp = 0,
.esi = 0
};
VasEBoot_video_set_mode ("text", 0, 0);
return VasEBoot_relocator32_boot (rel, state, 0);
}
static VasEBoot_err_t
VasEBoot_plan9_unload (void)
{
VasEBoot_relocator_unload (rel);
rel = NULL;
VasEBoot_dl_unref (my_mod);
return VAS_EBOOT_ERR_NONE;
}
/* Context for VasEBoot_cmd_plan9. */
struct VasEBoot_cmd_plan9_ctx
{
VasEBoot_extcmd_context_t ctxt;
VasEBoot_file_t file;
char *pmap;
VasEBoot_size_t pmapalloc;
VasEBoot_size_t pmapptr;
int noslash;
int prefixescnt[5];
char *bootdisk, *bootpart;
};
static const char prefixes[5][10] = {
"dos", "plan9", "ntfs", "linux", "linuxswap"
};
#include <VasEBoot/err.h>
static inline VasEBoot_err_t
VasEBoot_extend_alloc (VasEBoot_size_t sz, VasEBoot_size_t *allocated, char **ptr)
{
void *n;
if (sz < *allocated)
return VAS_EBOOT_ERR_NONE;
*allocated = 2 * sz;
n = VasEBoot_realloc (*ptr, *allocated);
if (!n)
return VasEBoot_errno;
*ptr = n;
return VAS_EBOOT_ERR_NONE;
}
/* Helper for VasEBoot_cmd_plan9. */
static int
fill_partition (VasEBoot_disk_t disk, const VasEBoot_partition_t partition, void *data)
{
struct VasEBoot_cmd_plan9_ctx *fill_ctx = data;
int file_disk = 0;
int pstart, pend;
if (!fill_ctx->noslash)
{
if (VasEBoot_extend_alloc (fill_ctx->pmapptr + 1, &fill_ctx->pmapalloc,
&fill_ctx->pmap))
return 1;
fill_ctx->pmap[fill_ctx->pmapptr++] = '/';
}
fill_ctx->noslash = 0;
file_disk = fill_ctx->file->device->disk
&& disk->id == fill_ctx->file->device->disk->id
&& disk->dev->id == fill_ctx->file->device->disk->dev->id;
pstart = fill_ctx->pmapptr;
if (VasEBoot_strcmp (partition->partmap->name, "plan") == 0)
{
unsigned ptr = partition->index + sizeof ("part ") - 1;
VasEBoot_err_t err;
disk->partition = partition->parent;
do
{
if (VasEBoot_extend_alloc (fill_ctx->pmapptr + 1, &fill_ctx->pmapalloc,
&fill_ctx->pmap))
return 1;
err = VasEBoot_disk_read (disk, 1, ptr, 1,
fill_ctx->pmap + fill_ctx->pmapptr);
if (err)
{
disk->partition = 0;
return err;
}
ptr++;
fill_ctx->pmapptr++;
}
while (VasEBoot_isalpha (fill_ctx->pmap[fill_ctx->pmapptr - 1])
|| VasEBoot_isdigit (fill_ctx->pmap[fill_ctx->pmapptr - 1]));
fill_ctx->pmapptr--;
}
else
{
char name[50];
int c = 0;
if (VasEBoot_strcmp (partition->partmap->name, "msdos") == 0)
{
switch (partition->msdostype)
{
case VAS_EBOOT_PC_PARTITION_TYPE_PLAN9:
c = 1;
break;
case VAS_EBOOT_PC_PARTITION_TYPE_NTFS:
c = 2;
break;
case VAS_EBOOT_PC_PARTITION_TYPE_MINIX:
case VAS_EBOOT_PC_PARTITION_TYPE_LINUX_MINIX:
case VAS_EBOOT_PC_PARTITION_TYPE_EXT2FS:
c = 3;
break;
case VAS_EBOOT_PC_PARTITION_TYPE_LINUX_SWAP:
c = 4;
break;
}
}
if (fill_ctx->prefixescnt[c] == 0)
VasEBoot_strcpy (name, prefixes[c]);
else
VasEBoot_snprintf (name, sizeof (name), "%s.%d", prefixes[c],
fill_ctx->prefixescnt[c]);
fill_ctx->prefixescnt[c]++;
if (VasEBoot_extend_alloc (fill_ctx->pmapptr + VasEBoot_strlen (name) + 1,
&fill_ctx->pmapalloc, &fill_ctx->pmap))
return 1;
VasEBoot_strcpy (fill_ctx->pmap + fill_ctx->pmapptr, name);
fill_ctx->pmapptr += VasEBoot_strlen (name);
}
pend = fill_ctx->pmapptr;
if (VasEBoot_extend_alloc (fill_ctx->pmapptr + 2 + 25 + 5 + 25,
&fill_ctx->pmapalloc, &fill_ctx->pmap))
return 1;
fill_ctx->pmap[fill_ctx->pmapptr++] = ' ';
VasEBoot_snprintf (fill_ctx->pmap + fill_ctx->pmapptr, 25 + 5 + 25,
"%" PRIuVAS_EBOOT_UINT64_T " %" PRIuVAS_EBOOT_UINT64_T,
VasEBoot_partition_get_start (partition),
VasEBoot_partition_get_start (partition)
+ VasEBoot_partition_get_len (partition));
if (file_disk && VasEBoot_partition_get_start (partition)
== VasEBoot_partition_get_start (fill_ctx->file->device->disk->partition)
&& VasEBoot_partition_get_len (partition)
== VasEBoot_partition_get_len (fill_ctx->file->device->disk->partition))
{
VasEBoot_free (fill_ctx->bootpart);
fill_ctx->bootpart = VasEBoot_strndup (fill_ctx->pmap + pstart,
pend - pstart);
}
fill_ctx->pmapptr += VasEBoot_strlen (fill_ctx->pmap + fill_ctx->pmapptr);
return 0;
}
/* Helper for VasEBoot_cmd_plan9. */
static int
fill_disk (const char *name, void *data)
{
struct VasEBoot_cmd_plan9_ctx *fill_ctx = data;
VasEBoot_device_t dev;
char *plan9name = NULL;
unsigned i;
int file_disk = 0;
dev = VasEBoot_device_open (name);
if (!dev)
{
VasEBoot_print_error ();
return 0;
}
if (!dev->disk)
{
VasEBoot_device_close (dev);
return 0;
}
file_disk = fill_ctx->file->device->disk
&& dev->disk->id == fill_ctx->file->device->disk->id
&& dev->disk->dev->id == fill_ctx->file->device->disk->dev->id;
for (i = 0;
fill_ctx->ctxt->state[0].args && fill_ctx->ctxt->state[0].args[i]; i++)
if (VasEBoot_strncmp (name, fill_ctx->ctxt->state[0].args[i],
VasEBoot_strlen (name)) == 0
&& fill_ctx->ctxt->state[0].args[i][VasEBoot_strlen (name)] == '=')
break;
if (fill_ctx->ctxt->state[0].args && fill_ctx->ctxt->state[0].args[i])
plan9name = VasEBoot_strdup (fill_ctx->ctxt->state[0].args[i]
+ VasEBoot_strlen (name) + 1);
else
switch (dev->disk->dev->id)
{
case VAS_EBOOT_DISK_DEVICE_BIOSDISK_ID:
if (dev->disk->id & 0x80)
plan9name = VasEBoot_xasprintf ("sdB%u",
(unsigned) (dev->disk->id & 0x7f));
else
plan9name = VasEBoot_xasprintf ("fd%u",
(unsigned) (dev->disk->id & 0x7f));
break;
/* Shouldn't happen as Plan9 doesn't work on these platforms. */
case VAS_EBOOT_DISK_DEVICE_OFDISK_ID:
case VAS_EBOOT_DISK_DEVICE_EFIDISK_ID:
/* Plan9 doesn't see those. */
default:
/* Not sure how to handle those. */
case VAS_EBOOT_DISK_DEVICE_NAND_ID:
if (!file_disk)
{
VasEBoot_device_close (dev);
return 0;
}
/* if it's the disk the kernel is loaded from we need to name
it nevertheless. */
plan9name = VasEBoot_strdup ("sdZ0");
break;
case VAS_EBOOT_DISK_DEVICE_ATA_ID:
{
unsigned unit;
if (VasEBoot_strlen (dev->disk->name) < sizeof ("ata0") - 1)
unit = 0;
else
unit = VasEBoot_strtoul (dev->disk->name + sizeof ("ata0") - 1, 0, 0);
plan9name = VasEBoot_xasprintf ("sd%c%d", 'C' + unit / 2, unit % 2);
}
break;
case VAS_EBOOT_DISK_DEVICE_SCSI_ID:
if (((dev->disk->id >> VAS_EBOOT_SCSI_ID_SUBSYSTEM_SHIFT) & 0xff)
== VAS_EBOOT_SCSI_SUBSYSTEM_PATA)
{
unsigned unit;
if (VasEBoot_strlen (dev->disk->name) < sizeof ("ata0") - 1)
unit = 0;
else
unit = VasEBoot_strtoul (dev->disk->name + sizeof ("ata0") - 1,
0, 0);
plan9name = VasEBoot_xasprintf ("sd%c%d", 'C' + unit / 2, unit % 2);
break;
}
/* FIXME: how does Plan9 number controllers?
We probably need save the SCSI devices and sort them */
plan9name
= VasEBoot_xasprintf ("sd0%u", (unsigned)
((dev->disk->id >> VAS_EBOOT_SCSI_ID_BUS_SHIFT)
& 0xf));
break;
}
if (!plan9name)
{
VasEBoot_print_error ();
VasEBoot_device_close (dev);
return 0;
}
if (VasEBoot_extend_alloc (fill_ctx->pmapptr + VasEBoot_strlen (plan9name)
+ sizeof ("part="), &fill_ctx->pmapalloc,
&fill_ctx->pmap))
{
VasEBoot_free (plan9name);
VasEBoot_device_close (dev);
return 1;
}
VasEBoot_strcpy (fill_ctx->pmap + fill_ctx->pmapptr, plan9name);
fill_ctx->pmapptr += VasEBoot_strlen (plan9name);
if (!file_disk)
VasEBoot_free (plan9name);
else
{
VasEBoot_free (fill_ctx->bootdisk);
fill_ctx->bootdisk = plan9name;
}
VasEBoot_strcpy (fill_ctx->pmap + fill_ctx->pmapptr, "part=");
fill_ctx->pmapptr += sizeof ("part=") - 1;
fill_ctx->noslash = 1;
VasEBoot_memset (fill_ctx->prefixescnt, 0, sizeof (fill_ctx->prefixescnt));
if (VasEBoot_partition_iterate (dev->disk, fill_partition, fill_ctx))
{
VasEBoot_device_close (dev);
return 1;
}
if (VasEBoot_extend_alloc (fill_ctx->pmapptr + 1, &fill_ctx->pmapalloc,
&fill_ctx->pmap))
{
VasEBoot_device_close (dev);
return 1;
}
fill_ctx->pmap[fill_ctx->pmapptr++] = '\n';
VasEBoot_device_close (dev);
return 0;
}
static VasEBoot_err_t
VasEBoot_cmd_plan9 (VasEBoot_extcmd_context_t ctxt, int argc, char *argv[])
{
struct VasEBoot_cmd_plan9_ctx fill_ctx = {
.ctxt = ctxt,
.file = 0,
.pmap = NULL,
.pmapalloc = 256,
.pmapptr = 0,
.noslash = 1,
.bootdisk = NULL,
.bootpart = NULL
};
void *mem;
VasEBoot_size_t memsize, padsize;
struct VasEBoot_plan9_header hdr;
char *config, *configptr;
VasEBoot_size_t configsize;
char *bootpath = NULL;
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;
fill_ctx.file = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_PLAN9_KERNEL);
if (! fill_ctx.file)
goto fail;
fill_ctx.pmap = VasEBoot_malloc (fill_ctx.pmapalloc);
if (!fill_ctx.pmap)
goto fail;
if (VasEBoot_disk_dev_iterate (fill_disk, &fill_ctx))
goto fail;
if (VasEBoot_extend_alloc (fill_ctx.pmapptr + 1, &fill_ctx.pmapalloc,
&fill_ctx.pmap))
goto fail;
fill_ctx.pmap[fill_ctx.pmapptr] = 0;
{
char *file_name = VasEBoot_strchr (argv[0], ')');
if (file_name)
file_name++;
else
file_name = argv[0];
if (*file_name)
file_name++;
if (fill_ctx.bootpart)
bootpath = VasEBoot_xasprintf ("%s!%s!%s", fill_ctx.bootdisk,
fill_ctx.bootpart, file_name);
else
bootpath = VasEBoot_xasprintf ("%s!%s", fill_ctx.bootdisk, file_name);
VasEBoot_free (fill_ctx.bootdisk);
VasEBoot_free (fill_ctx.bootpart);
}
if (!bootpath)
goto fail;
if (VasEBoot_file_read (fill_ctx.file, &hdr,
sizeof (hdr)) != (VasEBoot_ssize_t) sizeof (hdr))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
if (VasEBoot_be_to_cpu32 (hdr.magic) != VAS_EBOOT_PLAN9_MAGIC
|| hdr.zero)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "unsupported Plan9");
goto fail;
}
memsize = ALIGN_UP (VasEBoot_be_to_cpu32 (hdr.text_size) + sizeof (hdr),
VAS_EBOOT_PLAN9_ALIGN);
memsize += ALIGN_UP (VasEBoot_be_to_cpu32 (hdr.data_size), VAS_EBOOT_PLAN9_ALIGN);
memsize += ALIGN_UP(VasEBoot_be_to_cpu32 (hdr.bss_size), VAS_EBOOT_PLAN9_ALIGN);
eip = VasEBoot_be_to_cpu32 (hdr.entry_addr) & 0xfffffff;
/* path */
configsize = VAS_EBOOT_PLAN9_CONFIG_PATH_SIZE;
/* magic */
configsize += sizeof (VAS_EBOOT_PLAN9_CONFIG_MAGIC) - 1;
{
int i;
for (i = 1; i < argc; i++)
configsize += VasEBoot_strlen (argv[i]) + 1;
}
configsize += (sizeof ("bootfile=") - 1) + VasEBoot_strlen (bootpath) + 1;
configsize += fill_ctx.pmapptr;
/* Terminating \0. */
configsize++;
{
VasEBoot_relocator_chunk_t ch;
VasEBoot_err_t err;
err = VasEBoot_relocator_alloc_chunk_addr (rel, &ch, VAS_EBOOT_PLAN9_CONFIG_ADDR,
configsize);
if (err)
goto fail;
config = get_virtual_current_address (ch);
}
VasEBoot_memset (config, 0, VAS_EBOOT_PLAN9_CONFIG_PATH_SIZE);
VasEBoot_strncpy (config, bootpath, VAS_EBOOT_PLAN9_CONFIG_PATH_SIZE - 1);
configptr = config + VAS_EBOOT_PLAN9_CONFIG_PATH_SIZE;
VasEBoot_memcpy (configptr, VAS_EBOOT_PLAN9_CONFIG_MAGIC,
sizeof (VAS_EBOOT_PLAN9_CONFIG_MAGIC) - 1);
configptr += sizeof (VAS_EBOOT_PLAN9_CONFIG_MAGIC) - 1;
configptr = VasEBoot_stpcpy (configptr, "bootfile=");
configptr = VasEBoot_stpcpy (configptr, bootpath);
*configptr++ = '\n';
char *cmdline = configptr;
{
int i;
for (i = 1; i < argc; i++)
{
configptr = VasEBoot_stpcpy (configptr, argv[i]);
*configptr++ = '\n';
}
}
{
VasEBoot_err_t err;
*configptr = '\0';
err = VasEBoot_verify_string (cmdline, VAS_EBOOT_VERIFY_KERNEL_CMDLINE);
if (err)
goto fail;
}
configptr = VasEBoot_stpcpy (configptr, fill_ctx.pmap);
{
VasEBoot_relocator_chunk_t ch;
VasEBoot_err_t err;
err = VasEBoot_relocator_alloc_chunk_addr (rel, &ch, VAS_EBOOT_PLAN9_TARGET,
memsize);
if (err)
goto fail;
mem = get_virtual_current_address (ch);
}
{
VasEBoot_uint8_t *ptr;
ptr = mem;
VasEBoot_memcpy (ptr, &hdr, sizeof (hdr));
ptr += sizeof (hdr);
if (VasEBoot_file_read (fill_ctx.file, ptr, VasEBoot_be_to_cpu32 (hdr.text_size))
!= (VasEBoot_ssize_t) VasEBoot_be_to_cpu32 (hdr.text_size))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
ptr += VasEBoot_be_to_cpu32 (hdr.text_size);
padsize = ALIGN_UP (VasEBoot_be_to_cpu32 (hdr.text_size) + sizeof (hdr),
VAS_EBOOT_PLAN9_ALIGN) - VasEBoot_be_to_cpu32 (hdr.text_size)
- sizeof (hdr);
VasEBoot_memset (ptr, 0, padsize);
ptr += padsize;
if (VasEBoot_file_read (fill_ctx.file, ptr, VasEBoot_be_to_cpu32 (hdr.data_size))
!= (VasEBoot_ssize_t) VasEBoot_be_to_cpu32 (hdr.data_size))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
ptr += VasEBoot_be_to_cpu32 (hdr.data_size);
padsize = ALIGN_UP (VasEBoot_be_to_cpu32 (hdr.data_size), VAS_EBOOT_PLAN9_ALIGN)
- VasEBoot_be_to_cpu32 (hdr.data_size);
VasEBoot_memset (ptr, 0, padsize);
ptr += padsize;
VasEBoot_memset (ptr, 0, ALIGN_UP(VasEBoot_be_to_cpu32 (hdr.bss_size),
VAS_EBOOT_PLAN9_ALIGN));
}
VasEBoot_loader_set (VasEBoot_plan9_boot, VasEBoot_plan9_unload, 1);
return VAS_EBOOT_ERR_NONE;
fail:
VasEBoot_free (fill_ctx.pmap);
if (fill_ctx.file)
VasEBoot_file_close (fill_ctx.file);
VasEBoot_plan9_unload ();
return VasEBoot_errno;
}
static VasEBoot_extcmd_t cmd;
VAS_EBOOT_MOD_INIT(plan9)
{
cmd = VasEBoot_register_extcmd ("plan9", VasEBoot_cmd_plan9,
VAS_EBOOT_COMMAND_OPTIONS_AT_START,
N_("KERNEL ARGS"), N_("Load Plan9 kernel."),
options);
my_mod = mod;
}
VAS_EBOOT_MOD_FINI(plan9)
{
VasEBoot_unregister_extcmd (cmd);
}