1. change some directory structure for the build script

2. add build script and document
   see DOC/BuildVentoyFromSource.txt for detail
This commit is contained in:
longpanda 2020-05-20 22:36:27 +08:00
parent 965417970b
commit 2aae096c2a
97 changed files with 6298 additions and 67 deletions

View file

@ -0,0 +1,295 @@
/*
* Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* Command line and initrd passed to iPXE at runtime
*
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/init.h>
#include <ipxe/image.h>
#include <ipxe/script.h>
#include <ipxe/umalloc.h>
#include <realmode.h>
#include <ventoy.h>
/** Command line physical address
*
* This can be set by the prefix.
*/
uint32_t __bss16 ( cmdline_phys );
#define cmdline_phys __use_data16 ( cmdline_phys )
/** initrd physical address
*
* This can be set by the prefix.
*/
uint32_t __bss16 ( initrd_phys );
#define initrd_phys __use_data16 ( initrd_phys )
/** initrd length
*
* This can be set by the prefix.
*/
uint32_t __bss16 ( initrd_len );
#define initrd_len __use_data16 ( initrd_len )
/** Internal copy of the command line */
static char *cmdline_copy;
/** Free command line image */
static void cmdline_image_free ( struct refcnt *refcnt ) {
struct image *image = container_of ( refcnt, struct image, refcnt );
DBGC ( image, "RUNTIME freeing command line\n" );
free ( cmdline_copy );
}
/** Embedded script representing the command line */
static struct image cmdline_image = {
.refcnt = REF_INIT ( cmdline_image_free ),
.name = "<CMDLINE>",
.type = &script_image_type,
};
/** Colour for debug messages */
#define colour &cmdline_image
/**
* Strip unwanted cruft from command line
*
* @v cmdline Command line
* @v cruft Initial substring of cruft to strip
*/
static void cmdline_strip ( char *cmdline, const char *cruft ) {
char *strip;
char *strip_end;
/* Find unwanted cruft, if present */
if ( ! ( strip = strstr ( cmdline, cruft ) ) )
return;
/* Strip unwanted cruft */
strip_end = strchr ( strip, ' ' );
if ( strip_end ) {
*strip_end = '\0';
DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
strcpy ( strip, ( strip_end + 1 ) );
} else {
DBGC ( colour, "RUNTIME stripping \"%s\"\n", strip );
*strip = '\0';
}
}
/**
* Initialise command line
*
* @ret rc Return status code
*/
static int cmdline_init ( void ) {
userptr_t cmdline_user;
userptr_t chainaddr;
char *pos1;
char *pos2;
int chainlen;
char *cmdline;
size_t len;
int rc;
/* Do nothing if no command line was specified */
if ( ! cmdline_phys ) {
DBGC ( colour, "RUNTIME found no command line\n" );
return 0;
}
cmdline_user = phys_to_user ( cmdline_phys );
len = ( strlen_user ( cmdline_user, 0 ) + 1 /* NUL */ );
pos1 = strstr((char *)cmdline_user, "mem:");
if (pos1)
{
pos2 = strstr(pos1, ":size:");
if (pos2)
{
*pos2 = 0;
chainaddr = phys_to_user(strtoul(pos1 + 4 + 2, NULL, 16)); // skip 0x prefix in hex number
chainlen = (int)strtoul(pos2 + 6, NULL, 10);
*pos2 = ':';
g_initrd_addr = (void *)umalloc(chainlen);
g_initrd_len = chainlen;
memcpy_user((userptr_t)g_initrd_addr, 0, chainaddr, 0, chainlen);
}
}
/* Allocate and copy command line */
cmdline_copy = malloc ( len );
if ( ! cmdline_copy ) {
DBGC ( colour, "RUNTIME could not allocate %zd bytes for "
"command line\n", len );
rc = -ENOMEM;
goto err_alloc_cmdline_copy;
}
g_cmdline_copy = cmdline_copy;
cmdline = cmdline_copy;
copy_from_user ( cmdline, cmdline_user, 0, len );
DBGC ( colour, "RUNTIME found command line \"%s\" at %08x\n",
cmdline, cmdline_phys );
/* Mark command line as consumed */
cmdline_phys = 0;
/* Strip unwanted cruft from the command line */
cmdline_strip ( cmdline, "BOOT_IMAGE=" );
cmdline_strip ( cmdline, "initrd=" );
while ( isspace ( *cmdline ) )
cmdline++;
DBGC ( colour, "RUNTIME using command line \"%s\"\n", cmdline );
/* Prepare and register image */
cmdline_image.data = virt_to_user ( cmdline );
cmdline_image.len = strlen ( cmdline );
if ( cmdline_image.len ) {
if ( ( rc = register_image ( &cmdline_image ) ) != 0 ) {
DBGC ( colour, "RUNTIME could not register command "
"line: %s\n", strerror ( rc ) );
goto err_register_image;
}
}
/* Drop our reference to the image */
image_put ( &cmdline_image );
return 0;
err_register_image:
image_put ( &cmdline_image );
err_alloc_cmdline_copy:
return rc;
}
/**
* Initialise initrd
*
* @ret rc Return status code
*/
static int initrd_init ( void ) {
struct image *image;
int rc;
/* Do nothing if no initrd was specified */
if ( ! initrd_phys ) {
DBGC ( colour, "RUNTIME found no initrd\n" );
return 0;
}
if ( ! initrd_len ) {
DBGC ( colour, "RUNTIME found empty initrd\n" );
return 0;
}
DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n",
initrd_phys, ( initrd_phys + initrd_len ) );
/* Allocate image */
image = alloc_image ( NULL );
if ( ! image ) {
DBGC ( colour, "RUNTIME could not allocate image for "
"initrd\n" );
rc = -ENOMEM;
goto err_alloc_image;
}
if ( ( rc = image_set_name ( image, "<INITRD>" ) ) != 0 ) {
DBGC ( colour, "RUNTIME could not set image name: %s\n",
strerror ( rc ) );
goto err_set_name;
}
/* Allocate and copy initrd content */
image->data = umalloc ( initrd_len );
if ( ! image->data ) {
DBGC ( colour, "RUNTIME could not allocate %d bytes for "
"initrd\n", initrd_len );
rc = -ENOMEM;
goto err_umalloc;
}
image->len = initrd_len;
memcpy_user ( image->data, 0, phys_to_user ( initrd_phys ), 0,
initrd_len );
/* Mark initrd as consumed */
initrd_phys = 0;
/* Register image */
if ( ( rc = register_image ( image ) ) != 0 ) {
DBGC ( colour, "RUNTIME could not register initrd: %s\n",
strerror ( rc ) );
goto err_register_image;
}
/* Drop our reference to the image */
image_put ( image );
return 0;
err_register_image:
err_umalloc:
err_set_name:
image_put ( image );
err_alloc_image:
return rc;
}
/**
* Initialise command line and initrd
*
*/
static void runtime_init ( void ) {
int rc;
/* Initialise command line */
if ( ( rc = cmdline_init() ) != 0 ) {
/* No way to report failure */
return;
}
/* Initialise initrd */
if ( ( rc = initrd_init() ) != 0 ) {
/* No way to report failure */
return;
}
}
/** Command line and initrd initialisation function */
struct startup_fn runtime_startup_fn __startup_fn ( STARTUP_NORMAL ) = {
.name = "runtime",
.startup = runtime_init,
};

View file

@ -0,0 +1,532 @@
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <byteswap.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/blockdev.h>
#include <ipxe/io.h>
#include <ipxe/acpi.h>
#include <ipxe/sanboot.h>
#include <ipxe/device.h>
#include <ipxe/pci.h>
#include <ipxe/eltorito.h>
#include <ipxe/timer.h>
#include <ipxe/umalloc.h>
#include <realmode.h>
#include <bios.h>
#include <biosint.h>
#include <bootsector.h>
#include <int13.h>
#include <ventoy.h>
int g_debug = 0;
char *g_cmdline_copy;
void *g_initrd_addr;
size_t g_initrd_len;
ventoy_chain_head *g_chain;
ventoy_img_chunk *g_chunk;
uint32_t g_img_chunk_num;
ventoy_img_chunk *g_cur_chunk;
uint32_t g_disk_sector_size;
ventoy_override_chunk *g_override_chunk;
uint32_t g_override_chunk_num;
ventoy_virt_chunk *g_virt_chunk;
uint32_t g_virt_chunk_num;
ventoy_sector_flag g_sector_flag[128];
static struct int13_disk_address __bss16 ( ventoy_address );
#define ventoy_address __use_data16 ( ventoy_address )
static uint64_t ventoy_remap_lba(uint64_t lba, uint32_t *count)
{
uint32_t i;
uint32_t max_sectors;
ventoy_img_chunk *cur;
if ((NULL == g_cur_chunk) || ((lba) < g_cur_chunk->img_start_sector) || ((lba) > g_cur_chunk->img_end_sector))
{
g_cur_chunk = NULL;
for (i = 0; i < g_img_chunk_num; i++)
{
cur = g_chunk + i;
if (lba >= cur->img_start_sector && lba <= cur->img_end_sector)
{
g_cur_chunk = cur;
break;
}
}
}
if (g_cur_chunk)
{
max_sectors = g_cur_chunk->img_end_sector - lba + 1;
if (*count > max_sectors)
{
*count = max_sectors;
}
if (512 == g_disk_sector_size)
{
return g_cur_chunk->disk_start_sector + ((lba - g_cur_chunk->img_start_sector) << 2);
}
return g_cur_chunk->disk_start_sector + (lba - g_cur_chunk->img_start_sector) * 2048 / g_disk_sector_size;
}
return lba;
}
static int ventoy_vdisk_read_real(uint64_t lba, unsigned int count, unsigned long buffer)
{
uint32_t i = 0;
uint32_t left = 0;
uint32_t readcount = 0;
uint32_t tmpcount = 0;
uint16_t status = 0;
uint64_t curlba = 0;
uint64_t maplba = 0;
uint64_t start = 0;
uint64_t end = 0;
uint64_t override_start = 0;
uint64_t override_end = 0;
unsigned long phyaddr;
unsigned long databuffer = buffer;
uint8_t *override_data;
curlba = lba;
left = count;
while (left > 0)
{
readcount = left;
maplba = ventoy_remap_lba(curlba, &readcount);
if (g_disk_sector_size == 512)
{
tmpcount = (readcount << 2);
}
else
{
tmpcount = (readcount * 2048) / g_disk_sector_size;
}
phyaddr = user_to_phys(buffer, 0);
while (tmpcount > 0)
{
/* Use INT 13, 42 to read the data from real disk */
ventoy_address.lba = maplba;
ventoy_address.buffer.segment = (uint16_t)(phyaddr >> 4);
ventoy_address.buffer.offset = (uint16_t)(phyaddr & 0x0F);
if (tmpcount >= 64) /* max sectors per transmit */
{
ventoy_address.count = 64;
tmpcount -= 64;
maplba += 64;
phyaddr += 32768;
}
else
{
ventoy_address.count = tmpcount;
tmpcount = 0;
}
__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
"sti\n\t"
"int $0x13\n\t"
"sti\n\t" /* BIOS bugs */
"jc 1f\n\t"
"xorw %%ax, %%ax\n\t"
"\n1:\n\t" )
: "=a" ( status )
: "a" ( 0x4200 ), "d" ( VENTOY_BIOS_FAKE_DRIVE ),
"S" ( __from_data16 ( &ventoy_address ) ) );
}
curlba += readcount;
left -= readcount;
buffer += (readcount * 2048);
}
start = lba * 2048;
if (start > g_chain->real_img_size_in_bytes)
{
goto end;
}
end = start + count * 2048;
for (i = 0; i < g_override_chunk_num; i++)
{
override_data = g_override_chunk[i].override_data;
override_start = g_override_chunk[i].img_offset;
override_end = override_start + g_override_chunk[i].override_size;
if (end <= override_start || start >= override_end)
{
continue;
}
if (start <= override_start)
{
if (end <= override_end)
{
memcpy((char *)databuffer + override_start - start, override_data, end - override_start);
}
else
{
memcpy((char *)databuffer + override_start - start, override_data, override_end - override_start);
}
}
else
{
if (end <= override_end)
{
memcpy((char *)databuffer, override_data + start - override_start, end - start);
}
else
{
memcpy((char *)databuffer, override_data + start - override_start, override_end - start);
}
}
}
end:
return 0;
}
int ventoy_vdisk_read(struct san_device *sandev, uint64_t lba, unsigned int count, unsigned long buffer)
{
uint32_t i, j;
uint64_t curlba;
uint64_t lastlba = 0;
uint32_t lbacount = 0;
unsigned long lastbuffer;
uint64_t readend;
ventoy_virt_chunk *node;
ventoy_sector_flag *cur_flag;
ventoy_sector_flag *sector_flag = g_sector_flag;
struct i386_all_regs *ix86;
if (INT13_EXTENDED_READ != sandev->int13_command)
{
DBGC(sandev, "invalid cmd %u\n", sandev->int13_command);
return 0;
}
ix86 = (struct i386_all_regs *)sandev->x86_regptr;
readend = (lba + count) * 2048;
if (readend < g_chain->real_img_size_in_bytes)
{
ventoy_vdisk_read_real(lba, count, buffer);
ix86->regs.dl = sandev->drive;
return 0;
}
if (count > sizeof(g_sector_flag))
{
sector_flag = (ventoy_sector_flag *)malloc(count * sizeof(ventoy_sector_flag));
}
for (curlba = lba, cur_flag = sector_flag, j = 0; j < count; j++, curlba++, cur_flag++)
{
cur_flag->flag = 0;
for (node = g_virt_chunk, i = 0; i < g_virt_chunk_num; i++, node++)
{
if (curlba >= node->mem_sector_start && curlba < node->mem_sector_end)
{
memcpy((void *)(buffer + j * 2048),
(char *)g_virt_chunk + node->mem_sector_offset + (curlba - node->mem_sector_start) * 2048,
2048);
cur_flag->flag = 1;
break;
}
else if (curlba >= node->remap_sector_start && curlba < node->remap_sector_end)
{
cur_flag->remap_lba = node->org_sector_start + curlba - node->remap_sector_start;
cur_flag->flag = 2;
break;
}
}
}
for (curlba = lba, cur_flag = sector_flag, j = 0; j < count; j++, curlba++, cur_flag++)
{
if (cur_flag->flag == 2)
{
if (lastlba == 0)
{
lastbuffer = buffer + j * 2048;
lastlba = cur_flag->remap_lba;
lbacount = 1;
}
else if (lastlba + lbacount == cur_flag->remap_lba)
{
lbacount++;
}
else
{
ventoy_vdisk_read_real(lastlba, lbacount, lastbuffer);
lastbuffer = buffer + j * 2048;
lastlba = cur_flag->remap_lba;
lbacount = 1;
}
}
}
if (lbacount > 0)
{
ventoy_vdisk_read_real(lastlba, lbacount, lastbuffer);
}
if (sector_flag != g_sector_flag)
{
free(sector_flag);
}
ix86->regs.dl = sandev->drive;
return 0;
}
static void ventoy_dump_img_chunk(ventoy_chain_head *chain)
{
uint32_t i;
ventoy_img_chunk *chunk;
chunk = (ventoy_img_chunk *)((char *)chain + chain->img_chunk_offset);
printf("##################### ventoy_dump_img_chunk #######################\n");
for (i = 0; i < chain->img_chunk_num; i++)
{
printf("%2u: [ %u - %u ] <==> [ %llu - %llu ]\n",
i, chunk[i].img_start_sector, chunk[i].img_end_sector,
chunk[i].disk_start_sector, chunk[i].disk_end_sector);
}
ventoy_debug_pause();
}
static void ventoy_dump_override_chunk(ventoy_chain_head *chain)
{
uint32_t i;
ventoy_override_chunk *chunk;
chunk = (ventoy_override_chunk *)((char *)chain + chain->override_chunk_offset);
printf("##################### ventoy_dump_override_chunk #######################\n");
for (i = 0; i < g_override_chunk_num; i++)
{
printf("%2u: [ %llu, %u ]\n", i, chunk[i].img_offset, chunk[i].override_size);
}
ventoy_debug_pause();
}
static void ventoy_dump_virt_chunk(ventoy_chain_head *chain)
{
uint32_t i;
ventoy_virt_chunk *node;
printf("##################### ventoy_dump_virt_chunk #######################\n");
printf("virt_chunk_offset=%u\n", chain->virt_chunk_offset);
printf("virt_chunk_num=%u\n", chain->virt_chunk_num);
node = (ventoy_virt_chunk *)((char *)chain + chain->virt_chunk_offset);
for (i = 0; i < chain->virt_chunk_num; i++, node++)
{
printf("%2u: mem:[ %u, %u, %u ] remap:[ %u, %u, %u ]\n", i,
node->mem_sector_start,
node->mem_sector_end,
node->mem_sector_offset,
node->remap_sector_start,
node->remap_sector_end,
node->org_sector_start);
}
ventoy_debug_pause();
}
static void ventoy_dump_chain(ventoy_chain_head *chain)
{
uint32_t i = 0;
uint8_t chksum = 0;
uint8_t *guid;
uint8_t *vtoy_reserve;
guid = chain->os_param.vtoy_disk_guid;
for (i = 0; i < sizeof(ventoy_os_param); i++)
{
chksum += *((uint8_t *)(&(chain->os_param)) + i);
}
vtoy_reserve = (uint8_t *)(chain->os_param.vtoy_reserved);
printf("##################### ventoy_dump_chain #######################\n");
printf("os_param will be save at %p\n", ventoy_get_runtime_addr());
printf("os_param->chksum=0x%x (%s)\n", chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
printf("os_param->vtoy_disk_guid=%02x%02x%02x%02x\n", guid[0], guid[1], guid[2], guid[3]);
printf("os_param->vtoy_disk_size=%llu\n", chain->os_param.vtoy_disk_size);
printf("os_param->vtoy_disk_part_id=%u\n", chain->os_param.vtoy_disk_part_id);
printf("os_param->vtoy_disk_part_type=%u\n", chain->os_param.vtoy_disk_part_type);
printf("os_param->vtoy_img_path=<%s>\n", chain->os_param.vtoy_img_path);
printf("os_param->vtoy_img_size=<%llu>\n", chain->os_param.vtoy_img_size);
printf("os_param->vtoy_reserve[0]=<%u>\n", vtoy_reserve[0]);
printf("os_param->vtoy_reserve[1]=<%u>\n", vtoy_reserve[1]);
printf("os_param->vtoy_img_location_addr=<0x%llx>\n", chain->os_param.vtoy_img_location_addr);
printf("os_param->vtoy_img_location_len=<%u>\n", chain->os_param.vtoy_img_location_len);
ventoy_debug_pause();
printf("chain->disk_drive=0x%x\n", chain->disk_drive);
printf("chain->drive_map=0x%x\n", chain->drive_map);
printf("chain->disk_sector_size=%u\n", chain->disk_sector_size);
printf("chain->real_img_size_in_bytes=%llu\n", chain->real_img_size_in_bytes);
printf("chain->virt_img_size_in_bytes=%llu\n", chain->virt_img_size_in_bytes);
printf("chain->boot_catalog=%u\n", chain->boot_catalog);
printf("chain->img_chunk_offset=%u\n", chain->img_chunk_offset);
printf("chain->img_chunk_num=%u\n", chain->img_chunk_num);
printf("chain->override_chunk_offset=%u\n", chain->override_chunk_offset);
printf("chain->override_chunk_num=%u\n", chain->override_chunk_num);
printf("chain->virt_chunk_offset=%u\n", chain->virt_chunk_offset);
printf("chain->virt_chunk_num=%u\n", chain->virt_chunk_num);
ventoy_debug_pause();
ventoy_dump_img_chunk(chain);
ventoy_dump_override_chunk(chain);
ventoy_dump_virt_chunk(chain);
}
static int ventoy_update_image_location(ventoy_os_param *param)
{
uint8_t chksum = 0;
unsigned int i;
unsigned int length;
userptr_t address = 0;
ventoy_image_location *location = NULL;
ventoy_image_disk_region *region = NULL;
ventoy_img_chunk *chunk = g_chunk;
length = sizeof(ventoy_image_location) + (g_img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
address = umalloc(length + 4096 * 2);
if (!address)
{
return 0;
}
if (address % 4096)
{
address += 4096 - (address % 4096);
}
param->chksum = 0;
param->vtoy_img_location_addr = user_to_phys(address, 0);
param->vtoy_img_location_len = length;
/* update check sum */
for (i = 0; i < sizeof(ventoy_os_param); i++)
{
chksum += *((uint8_t *)param + i);
}
param->chksum = (chksum == 0) ? 0 : (uint8_t)(0x100 - chksum);
location = (ventoy_image_location *)(unsigned long)(address);
if (NULL == location)
{
return 0;
}
memcpy(&location->guid, &param->guid, sizeof(ventoy_guid));
location->image_sector_size = 2048;
location->disk_sector_size = g_chain->disk_sector_size;
location->region_count = g_img_chunk_num;
region = location->regions;
for (i = 0; i < g_img_chunk_num; i++)
{
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
region->image_start_sector = chunk->img_start_sector;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
return 0;
}
int ventoy_boot_vdisk(void *data)
{
uint8_t chksum = 0;
unsigned int i;
unsigned int drive;
(void)data;
ventoy_address.bufsize = offsetof ( typeof ( ventoy_address ), buffer_phys );
if (strstr(g_cmdline_copy, "debug"))
{
g_debug = 1;
printf("### ventoy chain boot begin... ###\n");
ventoy_debug_pause();
}
g_chain = (ventoy_chain_head *)g_initrd_addr;
g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
g_img_chunk_num = g_chain->img_chunk_num;
g_disk_sector_size = g_chain->disk_sector_size;
g_cur_chunk = g_chunk;
g_override_chunk = (ventoy_override_chunk *)((char *)g_chain + g_chain->override_chunk_offset);
g_override_chunk_num = g_chain->override_chunk_num;
g_virt_chunk = (ventoy_virt_chunk *)((char *)g_chain + g_chain->virt_chunk_offset);
g_virt_chunk_num = g_chain->virt_chunk_num;
if (g_debug)
{
for (i = 0; i < sizeof(ventoy_os_param); i++)
{
chksum += *((uint8_t *)(&(g_chain->os_param)) + i);
}
printf("os param checksum: 0x%x %s\n", g_chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
}
ventoy_update_image_location(&(g_chain->os_param));
if (g_debug)
{
ventoy_dump_chain(g_chain);
}
drive = ventoy_int13_hook(g_chain);
if (g_debug)
{
printf("### ventoy chain boot before boot image ... ###\n");
ventoy_debug_pause();
}
ventoy_int13_boot(drive, &(g_chain->os_param), g_cmdline_copy);
if (g_debug)
{
printf("!!!!!!!!!! ventoy boot failed !!!!!!!!!!\n");
ventoy_debug_pause();
}
return 0;
}

View file

@ -0,0 +1,820 @@
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** @file
*
* Hyper-V driver
*
*/
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <byteswap.h>
#include <pic8259.h>
#include <ipxe/malloc.h>
#include <ipxe/device.h>
#include <ipxe/timer.h>
#include <ipxe/quiesce.h>
#include <ipxe/cpuid.h>
#include <ipxe/msr.h>
#include <ipxe/hyperv.h>
#include <ipxe/vmbus.h>
#include "hyperv.h"
/** Maximum time to wait for a message response
*
* This is a policy decision.
*/
#define HV_MESSAGE_MAX_WAIT_MS 1000
/** Hyper-V timer frequency (fixed 10Mhz) */
#define HV_TIMER_HZ 10000000
/** Hyper-V timer scale factor (used to avoid 64-bit division) */
#define HV_TIMER_SHIFT 18
/**
* Convert a Hyper-V status code to an iPXE status code
*
* @v status Hyper-V status code
* @ret rc iPXE status code (before negation)
*/
#define EHV( status ) EPLATFORM ( EINFO_EPLATFORM, (status) )
/**
* Allocate zeroed pages
*
* @v hv Hyper-V hypervisor
* @v ... Page addresses to fill in, terminated by NULL
* @ret rc Return status code
*/
__attribute__ (( sentinel )) int
hv_alloc_pages ( struct hv_hypervisor *hv, ... ) {
va_list args;
void **page;
int i;
/* Allocate and zero pages */
va_start ( args, hv );
for ( i = 0 ; ( ( page = va_arg ( args, void ** ) ) != NULL ); i++ ) {
*page = malloc_dma ( PAGE_SIZE, PAGE_SIZE );
if ( ! *page )
goto err_alloc;
memset ( *page, 0, PAGE_SIZE );
}
va_end ( args );
return 0;
err_alloc:
va_end ( args );
va_start ( args, hv );
for ( ; i >= 0 ; i-- ) {
page = va_arg ( args, void ** );
free_dma ( *page, PAGE_SIZE );
}
va_end ( args );
return -ENOMEM;
}
/**
* Free pages
*
* @v hv Hyper-V hypervisor
* @v ... Page addresses, terminated by NULL
*/
__attribute__ (( sentinel )) void
hv_free_pages ( struct hv_hypervisor *hv, ... ) {
va_list args;
void *page;
va_start ( args, hv );
while ( ( page = va_arg ( args, void * ) ) != NULL )
free_dma ( page, PAGE_SIZE );
va_end ( args );
}
/**
* Allocate message buffer
*
* @v hv Hyper-V hypervisor
* @ret rc Return status code
*/
static int hv_alloc_message ( struct hv_hypervisor *hv ) {
/* Allocate buffer. Must be aligned to at least 8 bytes and
* must not cross a page boundary, so align on its own size.
*/
hv->message = malloc_dma ( sizeof ( *hv->message ),
sizeof ( *hv->message ) );
if ( ! hv->message )
return -ENOMEM;
return 0;
}
/**
* Free message buffer
*
* @v hv Hyper-V hypervisor
*/
static void hv_free_message ( struct hv_hypervisor *hv ) {
/* Free buffer */
free_dma ( hv->message, sizeof ( *hv->message ) );
}
/**
* Check whether or not we are running in Hyper-V
*
* @ret rc Return status code
*/
static int hv_check_hv ( void ) {
struct x86_features features;
uint32_t interface_id;
uint32_t discard_ebx;
uint32_t discard_ecx;
uint32_t discard_edx;
/* Check for presence of a hypervisor (not necessarily Hyper-V) */
x86_features ( &features );
if ( ! ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_HYPERVISOR ) ) {
DBGC ( HV_INTERFACE_ID, "HV not running in a hypervisor\n" );
return -ENODEV;
}
/* Check that hypervisor is Hyper-V */
cpuid ( HV_CPUID_INTERFACE_ID, 0, &interface_id, &discard_ebx,
&discard_ecx, &discard_edx );
if ( interface_id != HV_INTERFACE_ID ) {
DBGC ( HV_INTERFACE_ID, "HV not running in Hyper-V (interface "
"ID %#08x)\n", interface_id );
return -ENODEV;
}
return 0;
}
/**
* Check required features
*
* @v hv Hyper-V hypervisor
* @ret rc Return status code
*/
static int hv_check_features ( struct hv_hypervisor *hv ) {
uint32_t available;
uint32_t permissions;
uint32_t discard_ecx;
uint32_t discard_edx;
/* Check that required features and privileges are available */
cpuid ( HV_CPUID_FEATURES, 0, &available, &permissions, &discard_ecx,
&discard_edx );
if ( ! ( available & HV_FEATURES_AVAIL_HYPERCALL_MSR ) ) {
DBGC ( hv, "HV %p has no hypercall MSRs (features %08x:%08x)\n",
hv, available, permissions );
return -ENODEV;
}
if ( ! ( available & HV_FEATURES_AVAIL_SYNIC_MSR ) ) {
DBGC ( hv, "HV %p has no SynIC MSRs (features %08x:%08x)\n",
hv, available, permissions );
return -ENODEV;
}
if ( ! ( permissions & HV_FEATURES_PERM_POST_MESSAGES ) ) {
DBGC ( hv, "HV %p cannot post messages (features %08x:%08x)\n",
hv, available, permissions );
return -EACCES;
}
if ( ! ( permissions & HV_FEATURES_PERM_SIGNAL_EVENTS ) ) {
DBGC ( hv, "HV %p cannot signal events (features %08x:%08x)",
hv, available, permissions );
return -EACCES;
}
return 0;
}
/**
* Check that Gen 2 UEFI firmware is not running
*
* @v hv Hyper-V hypervisor
* @ret rc Return status code
*
* We must not steal ownership from the Gen 2 UEFI firmware, since
* doing so will cause an immediate crash. Avoid this by checking for
* the guest OS identity known to be used by the Gen 2 UEFI firmware.
*/
static int hv_check_uefi ( struct hv_hypervisor *hv ) {
uint64_t guest_os_id;
/* Check for UEFI firmware's guest OS identity */
guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
if ( guest_os_id == HV_GUEST_OS_ID_UEFI ) {
DBGC ( hv, "HV %p is owned by UEFI firmware\n", hv );
return -ENOTSUP;
}
return 0;
}
/**
* Map hypercall page
*
* @v hv Hyper-V hypervisor
*/
static void hv_map_hypercall ( struct hv_hypervisor *hv ) {
union {
struct {
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
} __attribute__ (( packed ));
char text[ 13 /* "bbbbccccdddd" + NUL */ ];
} vendor_id;
uint32_t build;
uint32_t version;
uint32_t discard_eax;
uint32_t discard_ecx;
uint32_t discard_edx;
uint64_t guest_os_id;
uint64_t hypercall;
/* Report guest OS identity */
guest_os_id = rdmsr ( HV_X64_MSR_GUEST_OS_ID );
if ( guest_os_id != 0 ) {
DBGC ( hv, "HV %p guest OS ID MSR was %#08llx\n",
hv, guest_os_id );
}
guest_os_id = HV_GUEST_OS_ID_IPXE;
DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
/* Get hypervisor system identity (for debugging) */
cpuid ( HV_CPUID_VENDOR_ID, 0, &discard_eax, &vendor_id.ebx,
&vendor_id.ecx, &vendor_id.edx );
vendor_id.text[ sizeof ( vendor_id.text ) - 1 ] = '\0';
cpuid ( HV_CPUID_HYPERVISOR_ID, 0, &build, &version, &discard_ecx,
&discard_edx );
DBGC ( hv, "HV %p detected \"%s\" version %d.%d build %d\n", hv,
vendor_id.text, ( version >> 16 ), ( version & 0xffff ), build );
/* Map hypercall page */
hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
hypercall &= ( PAGE_SIZE - 1 );
hypercall |= ( virt_to_phys ( hv->hypercall ) | HV_HYPERCALL_ENABLE );
DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
}
/**
* Unmap hypercall page
*
* @v hv Hyper-V hypervisor
*/
static void hv_unmap_hypercall ( struct hv_hypervisor *hv ) {
uint64_t hypercall;
uint64_t guest_os_id;
/* Unmap the hypercall page */
hypercall = rdmsr ( HV_X64_MSR_HYPERCALL );
hypercall &= ( ( PAGE_SIZE - 1 ) & ~HV_HYPERCALL_ENABLE );
DBGC2 ( hv, "HV %p hypercall MSR is %#08llx\n", hv, hypercall );
wrmsr ( HV_X64_MSR_HYPERCALL, hypercall );
/* Reset the guest OS identity */
guest_os_id = 0;
DBGC2 ( hv, "HV %p guest OS ID MSR is %#08llx\n", hv, guest_os_id );
wrmsr ( HV_X64_MSR_GUEST_OS_ID, guest_os_id );
}
/**
* Map synthetic interrupt controller
*
* @v hv Hyper-V hypervisor
*/
static void hv_map_synic ( struct hv_hypervisor *hv ) {
uint64_t simp;
uint64_t siefp;
uint64_t scontrol;
/* Zero SynIC message and event pages */
memset ( hv->synic.message, 0, PAGE_SIZE );
memset ( hv->synic.event, 0, PAGE_SIZE );
/* Map SynIC message page */
simp = rdmsr ( HV_X64_MSR_SIMP );
simp &= ( PAGE_SIZE - 1 );
simp |= ( virt_to_phys ( hv->synic.message ) | HV_SIMP_ENABLE );
DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
wrmsr ( HV_X64_MSR_SIMP, simp );
/* Map SynIC event page */
siefp = rdmsr ( HV_X64_MSR_SIEFP );
siefp &= ( PAGE_SIZE - 1 );
siefp |= ( virt_to_phys ( hv->synic.event ) | HV_SIEFP_ENABLE );
DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
wrmsr ( HV_X64_MSR_SIEFP, siefp );
/* Enable SynIC */
scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
scontrol |= HV_SCONTROL_ENABLE;
DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
}
/**
* Unmap synthetic interrupt controller, leaving SCONTROL untouched
*
* @v hv Hyper-V hypervisor
*/
static void hv_unmap_synic_no_scontrol ( struct hv_hypervisor *hv ) {
uint64_t siefp;
uint64_t simp;
/* Unmap SynIC event page */
siefp = rdmsr ( HV_X64_MSR_SIEFP );
siefp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIEFP_ENABLE );
DBGC2 ( hv, "HV %p SIEFP MSR is %#08llx\n", hv, siefp );
wrmsr ( HV_X64_MSR_SIEFP, siefp );
/* Unmap SynIC message page */
simp = rdmsr ( HV_X64_MSR_SIMP );
simp &= ( ( PAGE_SIZE - 1 ) & ~HV_SIMP_ENABLE );
DBGC2 ( hv, "HV %p SIMP MSR is %#08llx\n", hv, simp );
wrmsr ( HV_X64_MSR_SIMP, simp );
}
/**
* Unmap synthetic interrupt controller
*
* @v hv Hyper-V hypervisor
*/
static void hv_unmap_synic ( struct hv_hypervisor *hv ) {
uint64_t scontrol;
/* Disable SynIC */
scontrol = rdmsr ( HV_X64_MSR_SCONTROL );
scontrol &= ~HV_SCONTROL_ENABLE;
DBGC2 ( hv, "HV %p SCONTROL MSR is %#08llx\n", hv, scontrol );
wrmsr ( HV_X64_MSR_SCONTROL, scontrol );
/* Unmap SynIC event and message pages */
hv_unmap_synic_no_scontrol ( hv );
}
/**
* Enable synthetic interrupt
*
* @v hv Hyper-V hypervisor
* @v sintx Synthetic interrupt number
*/
void hv_enable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
unsigned long msr = HV_X64_MSR_SINT ( sintx );
uint64_t sint;
/* Enable synthetic interrupt
*
* We have to enable the interrupt, otherwise messages will
* not be delivered (even though the documentation implies
* that polling for messages is possible). We enable AutoEOI
* and hook the interrupt to the obsolete IRQ13 (FPU
* exception) vector, which will be implemented as a no-op.
*/
sint = rdmsr ( msr );
sint &= ~( HV_SINT_MASKED | HV_SINT_VECTOR_MASK );
sint |= ( HV_SINT_AUTO_EOI |
HV_SINT_VECTOR ( IRQ_INT ( 13 /* See comment above */ ) ) );
DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
wrmsr ( msr, sint );
}
/**
* Disable synthetic interrupt
*
* @v hv Hyper-V hypervisor
* @v sintx Synthetic interrupt number
*/
void hv_disable_sint ( struct hv_hypervisor *hv, unsigned int sintx ) {
unsigned long msr = HV_X64_MSR_SINT ( sintx );
uint64_t sint;
/* Do nothing if interrupt is already disabled */
sint = rdmsr ( msr );
if ( sint & HV_SINT_MASKED )
return;
/* Disable synthetic interrupt */
sint &= ~HV_SINT_AUTO_EOI;
sint |= HV_SINT_MASKED;
DBGC2 ( hv, "HV %p SINT%d MSR is %#08llx\n", hv, sintx, sint );
wrmsr ( msr, sint );
}
/**
* Post message
*
* @v hv Hyper-V hypervisor
* @v id Connection ID
* @v type Message type
* @v data Message
* @v len Length of message
* @ret rc Return status code
*/
int hv_post_message ( struct hv_hypervisor *hv, unsigned int id,
unsigned int type, const void *data, size_t len ) {
struct hv_post_message *msg = &hv->message->posted;
int status;
int rc;
/* Sanity check */
assert ( len <= sizeof ( msg->data ) );
/* Construct message */
memset ( msg, 0, sizeof ( *msg ) );
msg->id = cpu_to_le32 ( id );
msg->type = cpu_to_le32 ( type );
msg->len = cpu_to_le32 ( len );
memcpy ( msg->data, data, len );
DBGC2 ( hv, "HV %p connection %d posting message type %#08x:\n",
hv, id, type );
DBGC2_HDA ( hv, 0, msg->data, len );
/* Post message */
if ( ( status = hv_call ( hv, HV_POST_MESSAGE, msg, NULL ) ) != 0 ) {
rc = -EHV ( status );
DBGC ( hv, "HV %p could not post message to %#08x: %s\n",
hv, id, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Wait for received message
*
* @v hv Hyper-V hypervisor
* @v sintx Synthetic interrupt number
* @ret rc Return status code
*/
int hv_wait_for_message ( struct hv_hypervisor *hv, unsigned int sintx ) {
struct hv_message *msg = &hv->message->received;
struct hv_message *src = &hv->synic.message[sintx];
unsigned int retries;
size_t len;
/* Wait for message to arrive */
for ( retries = 0 ; retries < HV_MESSAGE_MAX_WAIT_MS ; retries++ ) {
/* Check for message */
if ( src->type ) {
/* Copy message */
memset ( msg, 0, sizeof ( *msg ) );
len = src->len;
assert ( len <= sizeof ( *msg ) );
memcpy ( msg, src,
( offsetof ( typeof ( *msg ), data ) + len ) );
DBGC2 ( hv, "HV %p SINT%d received message type "
"%#08x:\n", hv, sintx,
le32_to_cpu ( msg->type ) );
DBGC2_HDA ( hv, 0, msg->data, len );
/* Consume message */
src->type = 0;
return 0;
}
/* Trigger message delivery */
wrmsr ( HV_X64_MSR_EOM, 0 );
/* Delay */
mdelay ( 1 );
}
DBGC ( hv, "HV %p SINT%d timed out waiting for message\n",
hv, sintx );
return -ETIMEDOUT;
}
/**
* Signal event
*
* @v hv Hyper-V hypervisor
* @v id Connection ID
* @v flag Flag number
* @ret rc Return status code
*/
int hv_signal_event ( struct hv_hypervisor *hv, unsigned int id,
unsigned int flag ) {
struct hv_signal_event *event = &hv->message->signalled;
int status;
int rc;
/* Construct event */
memset ( event, 0, sizeof ( *event ) );
event->id = cpu_to_le32 ( id );
event->flag = cpu_to_le16 ( flag );
/* Signal event */
if ( ( status = hv_call ( hv, HV_SIGNAL_EVENT, event, NULL ) ) != 0 ) {
rc = -EHV ( status );
DBGC ( hv, "HV %p could not signal event to %#08x: %s\n",
hv, id, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Probe root device
*
* @v rootdev Root device
* @ret rc Return status code
*/
static int hv_probe ( struct root_device *rootdev ) {
struct hv_hypervisor *hv;
int rc;
/* Check we are running in Hyper-V */
if ( ( rc = hv_check_hv() ) != 0 )
goto err_check_hv;
/* Allocate and initialise structure */
hv = zalloc ( sizeof ( *hv ) );
if ( ! hv ) {
rc = -ENOMEM;
goto err_alloc;
}
/* Check features */
if ( ( rc = hv_check_features ( hv ) ) != 0 )
goto err_check_features;
/* Check that Gen 2 UEFI firmware is not running */
if ( ( rc = hv_check_uefi ( hv ) ) != 0 )
goto err_check_uefi;
/* Allocate pages */
if ( ( rc = hv_alloc_pages ( hv, &hv->hypercall, &hv->synic.message,
&hv->synic.event, NULL ) ) != 0 )
goto err_alloc_pages;
/* Allocate message buffer */
if ( ( rc = hv_alloc_message ( hv ) ) != 0 )
goto err_alloc_message;
/* Map hypercall page */
hv_map_hypercall ( hv );
/* Map synthetic interrupt controller */
hv_map_synic ( hv );
/* Probe Hyper-V devices */
if ( ( rc = vmbus_probe ( hv, &rootdev->dev ) ) != 0 )
goto err_vmbus_probe;
rootdev_set_drvdata ( rootdev, hv );
return 0;
vmbus_remove ( hv, &rootdev->dev );
err_vmbus_probe:
hv_unmap_synic ( hv );
hv_unmap_hypercall ( hv );
hv_free_message ( hv );
err_alloc_message:
hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
NULL );
err_alloc_pages:
err_check_uefi:
err_check_features:
free ( hv );
err_alloc:
err_check_hv:
return rc;
}
/**
* Remove root device
*
* @v rootdev Root device
*/
static void hv_remove ( struct root_device *rootdev ) {
struct hv_hypervisor *hv = rootdev_get_drvdata ( rootdev );
vmbus_remove ( hv, &rootdev->dev );
hv_unmap_synic ( hv );
hv_unmap_hypercall ( hv );
hv_free_message ( hv );
hv_free_pages ( hv, hv->hypercall, hv->synic.message, hv->synic.event,
NULL );
free ( hv );
rootdev_set_drvdata ( rootdev, NULL );
}
/** Hyper-V root device driver */
static struct root_driver hv_root_driver = {
.probe = hv_probe,
.remove = hv_remove,
};
/** Hyper-V root device */
struct root_device hv_root_device __root_device = {
.dev = { .name = "Hyper-V" },
.driver = &hv_root_driver,
};
/**
* Quiesce system
*
*/
static void hv_quiesce ( void ) {
struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
unsigned int i;
/* Do nothing if we are not running in Hyper-V */
if ( ! hv )
return;
/* The "enlightened" portions of the Windows Server 2016 boot
* process will not cleanly take ownership of an active
* Hyper-V connection. Experimentation shows that the minimum
* requirement is that we disable the SynIC message page
* (i.e. zero the SIMP MSR).
*
* We cannot perform a full shutdown of the Hyper-V
* connection. Experimentation shows that if we disable the
* SynIC (i.e. zero the SCONTROL MSR) then Windows Server 2016
* will enter an indefinite wait loop.
*
* Attempt to create a safe handover environment by resetting
* all MSRs except for SCONTROL.
*
* Note that we do not shut down our VMBus devices, since we
* may need to unquiesce the system and continue operation.
*/
/* Disable all synthetic interrupts */
for ( i = 0 ; i <= HV_SINT_MAX ; i++ )
hv_disable_sint ( hv, i );
/* Unmap synthetic interrupt controller, leaving SCONTROL
* enabled (see above).
*/
hv_unmap_synic_no_scontrol ( hv );
/* Unmap hypercall page */
hv_unmap_hypercall ( hv );
DBGC ( hv, "HV %p quiesced\n", hv );
}
/**
* Unquiesce system
*
*/
static void hv_unquiesce ( void ) {
struct hv_hypervisor *hv = rootdev_get_drvdata ( &hv_root_device );
uint64_t simp;
int rc;
/* Do nothing if we are not running in Hyper-V */
if ( ! hv )
return;
/* Experimentation shows that the "enlightened" portions of
* Windows Server 2016 will break our Hyper-V connection at
* some point during a SAN boot. Surprisingly it does not
* change the guest OS ID MSR, but it does leave the SynIC
* message page disabled.
*
* Our own explicit quiescing procedure will also disable the
* SynIC message page. We can therefore use the SynIC message
* page enable bit as a heuristic to determine when we need to
* reestablish our Hyper-V connection.
*/
simp = rdmsr ( HV_X64_MSR_SIMP );
if ( simp & HV_SIMP_ENABLE )
return;
/* Remap hypercall page */
hv_map_hypercall ( hv );
/* Remap synthetic interrupt controller */
hv_map_synic ( hv );
/* Reset Hyper-V devices */
if ( ( rc = vmbus_reset ( hv, &hv_root_device.dev ) ) != 0 ) {
DBGC ( hv, "HV %p could not unquiesce: %s\n",
hv, strerror ( rc ) );
/* Nothing we can do */
return;
}
}
/** Hyper-V quiescer */
struct quiescer hv_quiescer __quiescer = {
.quiesce = hv_quiesce,
.unquiesce = hv_unquiesce,
};
/**
* Probe timer
*
* @ret rc Return status code
*/
static int hv_timer_probe ( void ) {
uint32_t available;
uint32_t discard_ebx;
uint32_t discard_ecx;
uint32_t discard_edx;
int rc;
/* Check we are running in Hyper-V */
if ( ( rc = hv_check_hv() ) != 0 )
return rc;
/* Check for available reference counter */
cpuid ( HV_CPUID_FEATURES, 0, &available, &discard_ebx, &discard_ecx,
&discard_edx );
if ( ! ( available & HV_FEATURES_AVAIL_TIME_REF_COUNT_MSR ) ) {
DBGC ( HV_INTERFACE_ID, "HV has no time reference counter\n" );
return -ENODEV;
}
return 0;
}
/**
* Get current system time in ticks
*
* @ret ticks Current time, in ticks
*/
static unsigned long hv_currticks ( void ) {
/* Calculate time using a combination of bit shifts and
* multiplication (to avoid a 64-bit division).
*/
return ( ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) >> HV_TIMER_SHIFT ) *
( TICKS_PER_SEC / ( HV_TIMER_HZ >> HV_TIMER_SHIFT ) ) );
}
/**
* Delay for a fixed number of microseconds
*
* @v usecs Number of microseconds for which to delay
*/
static void hv_udelay ( unsigned long usecs ) {
uint32_t start;
uint32_t elapsed;
uint32_t threshold;
/* Spin until specified number of 10MHz ticks have elapsed */
start = rdmsr ( HV_X64_MSR_TIME_REF_COUNT );
threshold = ( usecs * ( HV_TIMER_HZ / 1000000 ) );
do {
elapsed = ( rdmsr ( HV_X64_MSR_TIME_REF_COUNT ) - start );
} while ( elapsed < threshold );
}
/** Hyper-V timer */
struct timer hv_timer __timer ( TIMER_PREFERRED ) = {
.name = "Hyper-V",
.probe = hv_timer_probe,
.currticks = hv_currticks,
.udelay = hv_udelay,
};
/* Drag in objects via hv_root_device */
REQUIRING_SYMBOL ( hv_root_device );
/* Drag in netvsc driver */
//REQUIRE_OBJECT ( netvsc );

View file

@ -0,0 +1,503 @@
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <ipxe/malloc.h>
#include <ipxe/pci.h>
#include <ipxe/cpuid.h>
#include <ipxe/msr.h>
#include <ipxe/xen.h>
#include <ipxe/xenver.h>
#include <ipxe/xenmem.h>
#include <ipxe/xenstore.h>
#include <ipxe/xenbus.h>
#include <ipxe/xengrant.h>
#include "hvm.h"
/** @file
*
* Xen HVM driver
*
*/
/**
* Get CPUID base
*
* @v hvm HVM device
* @ret rc Return status code
*/
static int hvm_cpuid_base ( struct hvm_device *hvm ) {
struct {
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
} __attribute__ (( packed )) signature;
uint32_t base;
uint32_t version;
uint32_t discard_eax;
uint32_t discard_ebx;
uint32_t discard_ecx;
uint32_t discard_edx;
/* Scan for magic signature */
for ( base = HVM_CPUID_MIN ; base <= HVM_CPUID_MAX ;
base += HVM_CPUID_STEP ) {
cpuid ( base, 0, &discard_eax, &signature.ebx, &signature.ecx,
&signature.edx );
if ( memcmp ( &signature, HVM_CPUID_MAGIC,
sizeof ( signature ) ) == 0 ) {
hvm->cpuid_base = base;
cpuid ( ( base + HVM_CPUID_VERSION ), 0, &version,
&discard_ebx, &discard_ecx, &discard_edx );
DBGC2 ( hvm, "HVM using CPUID base %#08x (v%d.%d)\n",
base, ( version >> 16 ), ( version & 0xffff ) );
return 0;
}
}
DBGC ( hvm, "HVM could not find hypervisor\n" );
return -ENODEV;
}
/**
* Map hypercall page(s)
*
* @v hvm HVM device
* @ret rc Return status code
*/
static int hvm_map_hypercall ( struct hvm_device *hvm ) {
uint32_t pages;
uint32_t msr;
uint32_t discard_ecx;
uint32_t discard_edx;
physaddr_t hypercall_phys;
uint32_t version;
static xen_extraversion_t extraversion;
int xenrc;
int rc;
/* Get number of hypercall pages and MSR to use */
cpuid ( ( hvm->cpuid_base + HVM_CPUID_PAGES ), 0, &pages, &msr,
&discard_ecx, &discard_edx );
/* Allocate pages */
hvm->hypercall_len = ( pages * PAGE_SIZE );
hvm->xen.hypercall = malloc_dma ( hvm->hypercall_len, PAGE_SIZE );
if ( ! hvm->xen.hypercall ) {
DBGC ( hvm, "HVM could not allocate %d hypercall page(s)\n",
pages );
return -ENOMEM;
}
hypercall_phys = virt_to_phys ( hvm->xen.hypercall );
DBGC2 ( hvm, "HVM hypercall page(s) at [%#08lx,%#08lx) via MSR %#08x\n",
hypercall_phys, ( hypercall_phys + hvm->hypercall_len ), msr );
/* Write to MSR */
wrmsr ( msr, hypercall_phys );
/* Check that hypercall mechanism is working */
version = xenver_version ( &hvm->xen );
if ( ( xenrc = xenver_extraversion ( &hvm->xen, &extraversion ) ) != 0){
rc = -EXEN ( xenrc );
DBGC ( hvm, "HVM could not get extraversion: %s\n",
strerror ( rc ) );
return rc;
}
DBGC2 ( hvm, "HVM found Xen version %d.%d%s\n",
( version >> 16 ), ( version & 0xffff ) , extraversion );
return 0;
}
/**
* Unmap hypercall page(s)
*
* @v hvm HVM device
*/
static void hvm_unmap_hypercall ( struct hvm_device *hvm ) {
/* Free pages */
free_dma ( hvm->xen.hypercall, hvm->hypercall_len );
}
/**
* Allocate and map MMIO space
*
* @v hvm HVM device
* @v space Source mapping space
* @v len Length (must be a multiple of PAGE_SIZE)
* @ret mmio MMIO space address, or NULL on error
*/
static void * hvm_ioremap ( struct hvm_device *hvm, unsigned int space,
size_t len ) {
struct xen_add_to_physmap add;
struct xen_remove_from_physmap remove;
unsigned int pages = ( len / PAGE_SIZE );
physaddr_t mmio_phys;
unsigned int i;
void *mmio;
int xenrc;
int rc;
/* Sanity check */
assert ( ( len % PAGE_SIZE ) == 0 );
/* Check for available space */
if ( ( hvm->mmio_offset + len ) > hvm->mmio_len ) {
DBGC ( hvm, "HVM could not allocate %zd bytes of MMIO space "
"(%zd of %zd remaining)\n", len,
( hvm->mmio_len - hvm->mmio_offset ), hvm->mmio_len );
goto err_no_space;
}
/* Map this space */
mmio = ioremap ( ( hvm->mmio + hvm->mmio_offset ), len );
if ( ! mmio ) {
DBGC ( hvm, "HVM could not map MMIO space [%08lx,%08lx)\n",
( hvm->mmio + hvm->mmio_offset ),
( hvm->mmio + hvm->mmio_offset + len ) );
goto err_ioremap;
}
mmio_phys = virt_to_phys ( mmio );
/* Add to physical address space */
for ( i = 0 ; i < pages ; i++ ) {
add.domid = DOMID_SELF;
add.idx = i;
add.space = space;
add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
if ( ( xenrc = xenmem_add_to_physmap ( &hvm->xen, &add ) ) !=0){
rc = -EXEN ( xenrc );
DBGC ( hvm, "HVM could not add space %d idx %d at "
"[%08lx,%08lx): %s\n", space, i,
( mmio_phys + ( i * PAGE_SIZE ) ),
( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
strerror ( rc ) );
goto err_add_to_physmap;
}
}
/* Update offset */
hvm->mmio_offset += len;
return mmio;
i = pages;
err_add_to_physmap:
for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
remove.domid = DOMID_SELF;
add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
xenmem_remove_from_physmap ( &hvm->xen, &remove );
}
iounmap ( mmio );
err_ioremap:
err_no_space:
return NULL;
}
/**
* Unmap MMIO space
*
* @v hvm HVM device
* @v mmio MMIO space address
* @v len Length (must be a multiple of PAGE_SIZE)
*/
static void hvm_iounmap ( struct hvm_device *hvm, void *mmio, size_t len ) {
struct xen_remove_from_physmap remove;
physaddr_t mmio_phys = virt_to_phys ( mmio );
unsigned int pages = ( len / PAGE_SIZE );
unsigned int i;
int xenrc;
int rc;
/* Unmap this space */
iounmap ( mmio );
/* Remove from physical address space */
for ( i = 0 ; i < pages ; i++ ) {
remove.domid = DOMID_SELF;
remove.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
if ( ( xenrc = xenmem_remove_from_physmap ( &hvm->xen,
&remove ) ) != 0 ) {
rc = -EXEN ( xenrc );
DBGC ( hvm, "HVM could not remove space [%08lx,%08lx): "
"%s\n", ( mmio_phys + ( i * PAGE_SIZE ) ),
( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
strerror ( rc ) );
/* Nothing we can do about this */
}
}
}
/**
* Map shared info page
*
* @v hvm HVM device
* @ret rc Return status code
*/
static int hvm_map_shared_info ( struct hvm_device *hvm ) {
physaddr_t shared_info_phys;
int rc;
/* Map shared info page */
hvm->xen.shared = hvm_ioremap ( hvm, XENMAPSPACE_shared_info,
PAGE_SIZE );
if ( ! hvm->xen.shared ) {
rc = -ENOMEM;
goto err_alloc;
}
shared_info_phys = virt_to_phys ( hvm->xen.shared );
DBGC2 ( hvm, "HVM shared info page at [%#08lx,%#08lx)\n",
shared_info_phys, ( shared_info_phys + PAGE_SIZE ) );
/* Sanity check */
DBGC2 ( hvm, "HVM wallclock time is %d\n",
readl ( &hvm->xen.shared->wc_sec ) );
return 0;
hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
err_alloc:
return rc;
}
/**
* Unmap shared info page
*
* @v hvm HVM device
*/
static void hvm_unmap_shared_info ( struct hvm_device *hvm ) {
/* Unmap shared info page */
hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
}
/**
* Map grant table
*
* @v hvm HVM device
* @ret rc Return status code
*/
static int hvm_map_grant ( struct hvm_device *hvm ) {
physaddr_t grant_phys;
int rc;
/* Initialise grant table */
if ( ( rc = xengrant_init ( &hvm->xen ) ) != 0 ) {
DBGC ( hvm, "HVM could not initialise grant table: %s\n",
strerror ( rc ) );
return rc;
}
/* Map grant table */
hvm->xen.grant.table = hvm_ioremap ( hvm, XENMAPSPACE_grant_table,
hvm->xen.grant.len );
if ( ! hvm->xen.grant.table )
return -ENODEV;
grant_phys = virt_to_phys ( hvm->xen.grant.table );
DBGC2 ( hvm, "HVM mapped grant table at [%08lx,%08lx)\n",
grant_phys, ( grant_phys + hvm->xen.grant.len ) );
return 0;
}
/**
* Unmap grant table
*
* @v hvm HVM device
*/
static void hvm_unmap_grant ( struct hvm_device *hvm ) {
/* Unmap grant table */
hvm_iounmap ( hvm, hvm->xen.grant.table, hvm->xen.grant.len );
}
/**
* Map XenStore
*
* @v hvm HVM device
* @ret rc Return status code
*/
static int hvm_map_xenstore ( struct hvm_device *hvm ) {
uint64_t xenstore_evtchn;
uint64_t xenstore_pfn;
physaddr_t xenstore_phys;
char *name;
int xenrc;
int rc;
/* Get XenStore event channel */
if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_EVTCHN,
&xenstore_evtchn ) ) != 0 ) {
rc = -EXEN ( xenrc );
DBGC ( hvm, "HVM could not get XenStore event channel: %s\n",
strerror ( rc ) );
return rc;
}
hvm->xen.store.port = xenstore_evtchn;
/* Get XenStore PFN */
if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_PFN,
&xenstore_pfn ) ) != 0 ) {
rc = -EXEN ( xenrc );
DBGC ( hvm, "HVM could not get XenStore PFN: %s\n",
strerror ( rc ) );
return rc;
}
xenstore_phys = ( xenstore_pfn * PAGE_SIZE );
/* Map XenStore */
hvm->xen.store.intf = ioremap ( xenstore_phys, PAGE_SIZE );
if ( ! hvm->xen.store.intf ) {
DBGC ( hvm, "HVM could not map XenStore at [%08lx,%08lx)\n",
xenstore_phys, ( xenstore_phys + PAGE_SIZE ) );
return -ENODEV;
}
DBGC2 ( hvm, "HVM mapped XenStore at [%08lx,%08lx) with event port "
"%d\n", xenstore_phys, ( xenstore_phys + PAGE_SIZE ),
hvm->xen.store.port );
/* Check that XenStore is working */
if ( ( rc = xenstore_read ( &hvm->xen, &name, "name", NULL ) ) != 0 ) {
DBGC ( hvm, "HVM could not read domain name: %s\n",
strerror ( rc ) );
return rc;
}
DBGC2 ( hvm, "HVM running in domain \"%s\"\n", name );
free ( name );
return 0;
}
/**
* Unmap XenStore
*
* @v hvm HVM device
*/
static void hvm_unmap_xenstore ( struct hvm_device *hvm ) {
/* Unmap XenStore */
iounmap ( hvm->xen.store.intf );
}
/**
* Probe PCI device
*
* @v pci PCI device
* @ret rc Return status code
*/
static int hvm_probe ( struct pci_device *pci ) {
struct hvm_device *hvm;
int rc;
/* Allocate and initialise structure */
hvm = zalloc ( sizeof ( *hvm ) );
if ( ! hvm ) {
rc = -ENOMEM;
goto err_alloc;
}
hvm->mmio = pci_bar_start ( pci, HVM_MMIO_BAR );
hvm->mmio_len = pci_bar_size ( pci, HVM_MMIO_BAR );
DBGC2 ( hvm, "HVM has MMIO space [%08lx,%08lx)\n",
hvm->mmio, ( hvm->mmio + hvm->mmio_len ) );
/* Fix up PCI device */
adjust_pci_device ( pci );
/* Attach to hypervisor */
if ( ( rc = hvm_cpuid_base ( hvm ) ) != 0 )
goto err_cpuid_base;
if ( ( rc = hvm_map_hypercall ( hvm ) ) != 0 )
goto err_map_hypercall;
if ( ( rc = hvm_map_shared_info ( hvm ) ) != 0 )
goto err_map_shared_info;
if ( ( rc = hvm_map_grant ( hvm ) ) != 0 )
goto err_map_grant;
if ( ( rc = hvm_map_xenstore ( hvm ) ) != 0 )
goto err_map_xenstore;
/* Probe Xen devices */
if ( ( rc = xenbus_probe ( &hvm->xen, &pci->dev ) ) != 0 ) {
DBGC ( hvm, "HVM could not probe Xen bus: %s\n",
strerror ( rc ) );
goto err_xenbus_probe;
}
pci_set_drvdata ( pci, hvm );
return 0;
xenbus_remove ( &hvm->xen, &pci->dev );
err_xenbus_probe:
hvm_unmap_xenstore ( hvm );
err_map_xenstore:
hvm_unmap_grant ( hvm );
err_map_grant:
hvm_unmap_shared_info ( hvm );
err_map_shared_info:
hvm_unmap_hypercall ( hvm );
err_map_hypercall:
err_cpuid_base:
free ( hvm );
err_alloc:
return rc;
}
/**
* Remove PCI device
*
* @v pci PCI device
*/
static void hvm_remove ( struct pci_device *pci ) {
struct hvm_device *hvm = pci_get_drvdata ( pci );
xenbus_remove ( &hvm->xen, &pci->dev );
hvm_unmap_xenstore ( hvm );
hvm_unmap_grant ( hvm );
hvm_unmap_shared_info ( hvm );
hvm_unmap_hypercall ( hvm );
free ( hvm );
}
/** PCI device IDs */
static struct pci_device_id hvm_ids[] = {
PCI_ROM ( 0x5853, 0x0001, "hvm", "hvm", 0 ),
PCI_ROM ( 0x5853, 0x0002, "hvm2", "hvm2", 0 ),
};
/** PCI driver */
struct pci_driver hvm_driver __pci_driver = {
.ids = hvm_ids,
.id_count = ( sizeof ( hvm_ids ) / sizeof ( hvm_ids[0] ) ),
.probe = hvm_probe,
.remove = hvm_remove,
};
/* Drag in objects via hvm_driver */
REQUIRING_SYMBOL ( hvm_driver );
/* Drag in netfront driver */
//REQUIRE_OBJECT ( netfront );

View file

@ -0,0 +1,235 @@
/* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <assert.h>
#include <realmode.h>
#include <biosint.h>
#include <basemem.h>
#include <fakee820.h>
#include <ipxe/init.h>
#include <ipxe/io.h>
#include <ipxe/hidemem.h>
/** Set to true if you want to test a fake E820 map */
#define FAKE_E820 0
/** Alignment for hidden memory regions */
#define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
/**
* A hidden region of iPXE
*
* This represents a region that will be edited out of the system's
* memory map.
*
* This structure is accessed by assembly code, so must not be
* changed.
*/
struct hidden_region {
/** Physical start address */
uint64_t start;
/** Physical end address */
uint64_t end;
};
/** Hidden base memory */
extern struct hidden_region __data16 ( hidemem_base );
#define hidemem_base __use_data16 ( hidemem_base )
/** Hidden umalloc memory */
extern struct hidden_region __data16 ( hidemem_umalloc );
#define hidemem_umalloc __use_data16 ( hidemem_umalloc )
/** Hidden text memory */
extern struct hidden_region __data16 ( hidemem_textdata );
#define hidemem_textdata __use_data16 ( hidemem_textdata )
/** Assembly routine in e820mangler.S */
extern void int15();
/** Vector for storing original INT 15 handler */
extern struct segoff __text16 ( int15_vector );
#define int15_vector __use_text16 ( int15_vector )
/* The linker defines these symbols for us */
extern char _textdata[];
extern char _etextdata[];
extern char _text16_memsz[];
#define _text16_memsz ( ( size_t ) _text16_memsz )
extern char _data16_memsz[];
#define _data16_memsz ( ( size_t ) _data16_memsz )
/**
* Hide region of memory from system memory map
*
* @v region Hidden memory region
* @v start Start of region
* @v end End of region
*/
static void hide_region ( struct hidden_region *region,
physaddr_t start, physaddr_t end ) {
/* Some operating systems get a nasty shock if a region of the
* E820 map seems to start on a non-page boundary. Make life
* safer by rounding out our edited region.
*/
region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
}
/**
* Hide used base memory
*
*/
void hide_basemem ( void ) {
/* Hide from the top of free base memory to 640kB. Don't use
* hide_region(), because we don't want this rounded to the
* nearest page boundary.
*/
hidemem_base.start = ( get_fbms() * 1024 );
}
/**
* Hide umalloc() region
*
*/
void hide_umalloc ( physaddr_t start, physaddr_t end ) {
assert ( end <= virt_to_phys ( _textdata ) );
hide_region ( &hidemem_umalloc, start, end );
}
/**
* Hide .text and .data
*
*/
void hide_textdata ( void ) {
hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
virt_to_phys ( _etextdata ) );
}
/**
* Hide Etherboot
*
* Installs an INT 15 handler to edit Etherboot out of the memory map
* returned by the BIOS.
*/
static void hide_etherboot ( void ) {
struct memory_map memmap;
unsigned int rm_ds_top;
unsigned int rm_cs_top;
unsigned int fbms;
/* Dump memory map before mangling */
DBG ( "Hiding iPXE from system memory map\n" );
get_memmap ( &memmap );
/* Hook in fake E820 map, if we're testing one */
if ( FAKE_E820 ) {
DBG ( "Hooking in fake E820 map\n" );
fake_e820();
get_memmap ( &memmap );
}
/* Initialise the hidden regions */
hide_basemem();
hide_umalloc ( virt_to_phys ( _textdata ), virt_to_phys ( _textdata ) );
hide_textdata();
/* Some really moronic BIOSes bring up the PXE stack via the
* UNDI loader entry point and then don't bother to unload it
* before overwriting the code and data segments. If this
* happens, we really don't want to leave INT 15 hooked,
* because that will cause any loaded OS to die horribly as
* soon as it attempts to fetch the system memory map.
*
* We use a heuristic to guess whether or not we are being
* loaded sensibly.
*/
rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
fbms = get_fbms();
if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
"DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
DBG ( "Disabling INT 15 memory hiding\n" );
return;
}
/* Hook INT 15 */
hook_bios_interrupt ( 0x15, ( intptr_t ) int15, &int15_vector );
/* Dump memory map after mangling */
DBG ( "Hidden iPXE from system memory map\n" );
get_memmap ( &memmap );
}
/**
* Unhide Etherboot
*
* Uninstalls the INT 15 handler installed by hide_etherboot(), if
* possible.
*/
static void unhide_etherboot ( int flags __unused ) {
struct memory_map memmap;
int rc;
/* If we have more than one hooked interrupt at this point, it
* means that some other vector is still hooked, in which case
* we can't safely unhook INT 15 because we need to keep our
* memory protected. (We expect there to be at least one
* hooked interrupt, because INT 15 itself is still hooked).
*/
if ( hooked_bios_interrupts > 1 ) {
DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
hooked_bios_interrupts );
return;
}
/* Try to unhook INT 15 */
if ( ( rc = unhook_bios_interrupt ( 0x15, ( intptr_t ) int15,
&int15_vector ) ) != 0 ) {
DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
/* Leave it hooked; there's nothing else we can do,
* and it should be intrinsically safe (though
* wasteful of RAM).
*/
}
/* Unhook fake E820 map, if used */
if ( FAKE_E820 )
unfake_e820();
/* Dump memory map after unhiding */
DBG ( "Unhidden iPXE from system memory map\n" );
get_memmap ( &memmap );
}
/** Hide Etherboot startup function */
struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
.name = "hidemem",
.startup = hide_etherboot,
.shutdown = unhide_etherboot,
};

View file

@ -0,0 +1,53 @@
/*
* Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <errno.h>
#include <ipxe/sanboot.h>
static int null_san_hook ( unsigned int drive __unused,
struct uri **uris __unused,
unsigned int count __unused,
unsigned int flags __unused ) {
return -EOPNOTSUPP;
}
static void null_san_unhook ( unsigned int drive __unused ) {
/* Do nothing */
}
static int null_san_boot ( unsigned int drive __unused,
const char *filename __unused ) {
return -EOPNOTSUPP;
}
static int null_san_describe ( void ) {
return -EOPNOTSUPP;
}
PROVIDE_SANBOOT ( pcbios, san_hook, null_san_hook );
PROVIDE_SANBOOT ( pcbios, san_unhook, null_san_unhook );
PROVIDE_SANBOOT ( pcbios, san_boot, null_san_boot );
PROVIDE_SANBOOT ( pcbios, san_describe, null_san_describe );

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,48 @@
#ifndef __VENTOY_INT13_H__
#define __VENTOY_INT13_H__
#undef for_each_sandev
#define for_each_sandev( sandev ) sandev = g_sandev; if (sandev)
int ventoy_vdisk_read(struct san_device*sandev, uint64_t lba, unsigned int count, unsigned long buffer);
static inline int ventoy_sandev_write ( struct san_device *sandev, uint64_t lba, unsigned int count, unsigned long buffer )
{
(void)sandev;
(void)lba;
(void)count;
(void)buffer;
DBGC(sandev, "ventoy_sandev_write\n");
return 0;
}
static inline int ventoy_sandev_reset (void *sandev)
{
(void)sandev;
DBGC(sandev, "ventoy_sandev_reset\n");
return 0;
}
#define sandev_reset ventoy_sandev_reset
#define sandev_read ventoy_vdisk_read
#define sandev_write ventoy_sandev_write
#undef ECANCELED
#define ECANCELED 0x0b
#undef ENODEV
#define ENODEV 0x2c
#undef ENOTSUP
#define ENOTSUP 0x3c
#undef ENOMEM
#define ENOMEM 0x31
#undef EIO
#define EIO 0x1d
#undef ENOEXEC
#define ENOEXEC 0x2e
#undef ENOSPC
#define ENOSPC 0x34
#endif /* __VENTOY_INT13_H__ */

View file

@ -0,0 +1,31 @@
#!/bin/bash
build_bios() {
rm -f bin/ipxe.iso
make -e -k -j 8 bin/ipxe.iso BIOS_MODE=BIOS
if ! [ -e bin/ipxe.iso ]; then
echo "Failed"
exit 1
fi
mkdir -p ./mnt
mount bin/ipxe.iso ./mnt
rm -f ../../../INSTALL/ventoy/ipxe.krn
cp -a ./mnt/ipxe.krn ../../../INSTALL/ventoy/ipxe.krn
umount ./mnt > /dev/null 2>&1
umount ./mnt > /dev/null 2>&1
umount ./mnt > /dev/null 2>&1
rm -rf ./mnt
echo -e "\n===============SUCCESS===============\n"
}
build_bios

View file

@ -0,0 +1,24 @@
#ifndef CONFIG_SETTINGS_H
#define CONFIG_SETTINGS_H
/** @file
*
* Configuration settings sources
*
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
//#define PCI_SETTINGS /* PCI device settings */
//#define CPUID_SETTINGS /* CPUID settings */
//#define MEMMAP_SETTINGS /* Memory map settings */
//#define VMWARE_SETTINGS /* VMware GuestInfo settings */
//#define VRAM_SETTINGS /* Video RAM dump settings */
//#define ACPI_SETTINGS /* ACPI settings */
#include <config/named.h>
#include NAMED_CONFIG(settings.h)
#include <config/local/settings.h>
#include LOCAL_NAMED_CONFIG(settings.h)
#endif /* CONFIG_SETTINGS_H */

View file

@ -0,0 +1,142 @@
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <string.h>
#include <ipxe/list.h>
#include <ipxe/tables.h>
#include <ipxe/init.h>
#include <ipxe/interface.h>
#include <ipxe/device.h>
/**
* @file
*
* Device model
*
*/
/** Registered root devices */
static LIST_HEAD ( devices );
/** Device removal inhibition counter */
int device_keep_count = 0;
/**
* Probe a root device
*
* @v rootdev Root device
* @ret rc Return status code
*/
static int rootdev_probe ( struct root_device *rootdev ) {
int rc;
DBG ( "Adding %s root bus\n", rootdev->dev.name );
if ( ( rc = rootdev->driver->probe ( rootdev ) ) != 0 ) {
DBG ( "Failed to add %s root bus: %s\n",
rootdev->dev.name, strerror ( rc ) );
return rc;
}
return 0;
}
/**
* Remove a root device
*
* @v rootdev Root device
*/
static void rootdev_remove ( struct root_device *rootdev ) {
rootdev->driver->remove ( rootdev );
DBG ( "Removed %s root bus\n", rootdev->dev.name );
}
/**
* Probe all devices
*
* This initiates probing for all devices in the system. After this
* call, the device hierarchy will be populated, and all hardware
* should be ready to use.
*/
static void probe_devices ( void ) {
struct root_device *rootdev;
int rc;
for_each_table_entry ( rootdev, ROOT_DEVICES ) {
list_add ( &rootdev->dev.siblings, &devices );
INIT_LIST_HEAD ( &rootdev->dev.children );
if ( ( rc = rootdev_probe ( rootdev ) ) != 0 )
list_del ( &rootdev->dev.siblings );
}
}
/**
* Remove all devices
*
*/
static void remove_devices ( int booting __unused ) {
struct root_device *rootdev;
struct root_device *tmp;
if ( device_keep_count != 0 ) {
DBG ( "Refusing to remove devices on shutdown\n" );
return;
}
list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
rootdev_remove ( rootdev );
list_del ( &rootdev->dev.siblings );
}
}
//struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
struct startup_fn startup_devices = {
.name = "devices",
.startup = probe_devices,
.shutdown = remove_devices,
};
/**
* Identify a device behind an interface
*
* @v intf Interface
* @ret device Device, or NULL
*/
struct device * identify_device ( struct interface *intf ) {
struct interface *dest;
identify_device_TYPE ( void * ) *op =
intf_get_dest_op ( intf, identify_device, &dest );
void *object = intf_object ( dest );
void *device;
if ( op ) {
device = op ( object );
} else {
/* Default is to return NULL */
device = NULL;
}
intf_put ( dest );
return device;
}

View file

@ -0,0 +1,47 @@
/**************************************************************************
iPXE - Network Bootstrap Program
Literature dealing with the network protocols:
ARP - RFC826
RARP - RFC903
UDP - RFC768
BOOTP - RFC951, RFC2132 (vendor extensions)
DHCP - RFC2131, RFC2132 (options)
TFTP - RFC1350, RFC2347 (options), RFC2348 (blocksize), RFC2349 (tsize)
RPC - RFC1831, RFC1832 (XDR), RFC1833 (rpcbind/portmapper)
**************************************************************************/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stddef.h>
#include <stdio.h>
#include <ipxe/init.h>
#include <ipxe/version.h>
#include <usr/autoboot.h>
#include <ventoy.h>
/**
* Main entry point
*
* @ret rc Return status code
*/
__asmcall int main ( void ) {
int rc;
/* Perform one-time-only initialisation (e.g. heap) */
initialise();
/* Some devices take an unreasonably long time to initialise */
//printf ( "%s initialising devices...", product_short_name );
startup();
//printf ( "ok\n" );
/* Attempt to boot */
if ( ( rc = ventoy_boot_vdisk ( NULL ) ) != 0 )
goto err_ipxe;
err_ipxe:
shutdown_exit();
return rc;
}

View file

@ -0,0 +1,201 @@
/*
* Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/**
* @file
*
* SAN booting
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/timer.h>
#include <ipxe/process.h>
#include <ipxe/iso9660.h>
#include <ipxe/dhcp.h>
#include <ipxe/settings.h>
#include <ipxe/quiesce.h>
#include <ipxe/sanboot.h>
#include <ipxe/pci.h>
#include <ipxe/scsi.h>
#include <ipxe/ata.h>
#include <ipxe/acpi.h>
#include <ipxe/ibft.h>
unsigned long pci_bar_size ( struct pci_device *pci, unsigned int reg )
{
(void)pci;
(void)reg;
return 0;
}
unsigned long pci_bar_start ( struct pci_device *pci, unsigned int reg )
{
(void)pci;
(void)reg;
return 0;
}
void adjust_pci_device ( struct pci_device *pci )
{
(void)pci;
}
/*
* obj_netfront
* obj_netvsc
*
* ibft_model
*/
int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun )
{
(void)lun_string;
(void)lun;
return 0;
}
void scsi_parse_sense ( const void *data, size_t len,
struct scsi_sns_descriptor *sense )
{
(void)data;
(void)len;
(void)sense;
return;
}
void scsi_response ( struct interface *intf, struct scsi_rsp *response )
{
(void)intf;
(void)response;
}
int scsi_open ( struct interface *block, struct interface *scsi,
struct scsi_lun *lun )
{
(void)block;
(void)scsi;
(void)lun;
return 0;
}
int scsi_command ( struct interface *control, struct interface *data,
struct scsi_cmd *command )
{
(void)control;
(void)data;
(void)command;
return 0;
}
int ata_open ( struct interface *block, struct interface *ata,
unsigned int device, unsigned int max_count )
{
(void)block;
(void)ata;
(void)device;
(void)max_count;
return 0;
}
int ata_command ( struct interface *control, struct interface *data,
struct ata_cmd *command )
{
(void)control;
(void)data;
(void)command;
return 0;
}
#if 0
static uint32_t mCrcTable[256] =
{
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
uint32_t CalculateCrc32
(
const void *Buffer,
uint32_t Length,
uint32_t InitValue
)
{
uint32_t Crc = 0;
uint32_t Index = 0;
const uint8_t *Ptr = (const uint8_t *)Buffer;
Crc = InitValue ^ 0xffffffff;
for (Index = 0; Index < Length; Index++, Ptr++)
{
Crc = (Crc >> 8) ^ mCrcTable[(uint8_t) Crc ^ *Ptr];
}
return (Crc ^ 0xffffffff);
}
#endif

View file

@ -0,0 +1,501 @@
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <wchar.h>
#include <ipxe/vsprintf.h>
/** @file */
#define CHAR_LEN 0 /**< "hh" length modifier */
#define SHORT_LEN 1 /**< "h" length modifier */
#define INT_LEN 2 /**< no length modifier */
#define LONG_LEN 3 /**< "l" length modifier */
#define LONGLONG_LEN 4 /**< "ll" length modifier */
#define SIZE_T_LEN 5 /**< "z" length modifier */
static uint8_t type_sizes[] = {
[CHAR_LEN] = sizeof ( char ),
[SHORT_LEN] = sizeof ( short ),
[INT_LEN] = sizeof ( int ),
[LONG_LEN] = sizeof ( long ),
[LONGLONG_LEN] = sizeof ( long long ),
[SIZE_T_LEN] = sizeof ( size_t ),
};
/**
* Use lower-case for hexadecimal digits
*
* Note that this value is set to 0x20 since that makes for very
* efficient calculations. (Bitwise-ORing with @c LCASE converts to a
* lower-case character, for example.)
*/
#define LCASE 0x20
/**
* Use "alternate form"
*
* For hexadecimal numbers, this means to add a "0x" or "0X" prefix to
* the number.
*/
#define ALT_FORM 0x02
/**
* Use zero padding
*
* Note that this value is set to 0x10 since that allows the pad
* character to be calculated as @c 0x20|(flags&ZPAD)
*/
#define ZPAD 0x10
/**
* Format a hexadecimal number
*
* @v end End of buffer to contain number
* @v num Number to format
* @v width Minimum field width
* @v flags Format flags
* @ret ptr End of buffer
*
* Fills a buffer in reverse order with a formatted hexadecimal
* number. The number will be zero-padded to the specified width.
* Lower-case and "alternate form" (i.e. "0x" prefix) flags may be
* set.
*
* There must be enough space in the buffer to contain the largest
* number that this function can format.
*/
static char * format_hex ( char *end, unsigned long long num, int width,
int flags ) {
char *ptr = end;
int case_mod = ( flags & LCASE );
int pad = ( ( flags & ZPAD ) | ' ' );
/* Generate the number */
do {
*(--ptr) = "0123456789ABCDEF"[ num & 0xf ] | case_mod;
num >>= 4;
} while ( num );
/* Pad to width */
while ( ( end - ptr ) < width )
*(--ptr) = pad;
/* Add "0x" or "0X" if alternate form specified */
if ( flags & ALT_FORM ) {
*(--ptr) = 'X' | case_mod;
*(--ptr) = '0';
}
return ptr;
}
/**
* Format a decimal number
*
* @v end End of buffer to contain number
* @v num Number to format
* @v width Minimum field width
* @v flags Format flags
* @ret ptr End of buffer
*
* Fills a buffer in reverse order with a formatted decimal number.
* The number will be space-padded to the specified width.
*
* There must be enough space in the buffer to contain the largest
* number that this function can format.
*/
static char * format_decimal ( char *end, signed long num, int width,
int flags ) {
char *ptr = end;
int negative = 0;
int zpad = ( flags & ZPAD );
int pad = ( zpad | ' ' );
/* Generate the number */
if ( num < 0 ) {
negative = 1;
num = -num;
}
do {
*(--ptr) = '0' + ( num % 10 );
num /= 10;
} while ( num );
/* Add "-" if necessary */
if ( negative && ( ! zpad ) )
*(--ptr) = '-';
/* Pad to width */
while ( ( end - ptr ) < width )
*(--ptr) = pad;
/* Add "-" if necessary */
if ( negative && zpad )
*ptr = '-';
return ptr;
}
#define ZPAD 0x10
char *format_unsigned_decimal(char *end, unsigned long long num, int width, int flags)
{
char *ptr = end;
int zpad = ( flags & ZPAD );
int pad = ( zpad | ' ' );
do {
*(--ptr) = '0' + ( num % 10 );
num /= 10;
} while ( num );
/* Pad to width */
while ( ( end - ptr ) < width )
*(--ptr) = pad;
return ptr;
}
/**
* Print character via a printf context
*
* @v ctx Context
* @v c Character
*
* Call's the printf_context::handler() method and increments
* printf_context::len.
*/
static inline void cputchar ( struct printf_context *ctx, unsigned char c ) {
ctx->handler ( ctx, c );
++ctx->len;
}
/**
* Write a formatted string to a printf context
*
* @v ctx Context
* @v fmt Format string
* @v args Arguments corresponding to the format string
* @ret len Length of formatted string
*/
size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
int flags;
int width;
uint8_t *length;
char *ptr;
char tmp_buf[32]; /* 32 is enough for all numerical formats.
* Insane width fields could overflow this buffer. */
wchar_t *wptr;
/* Initialise context */
ctx->len = 0;
for ( ; *fmt ; fmt++ ) {
/* Pass through ordinary characters */
if ( *fmt != '%' ) {
cputchar ( ctx, *fmt );
continue;
}
fmt++;
/* Process flag characters */
flags = 0;
for ( ; ; fmt++ ) {
if ( *fmt == '#' ) {
flags |= ALT_FORM;
} else if ( *fmt == '0' ) {
flags |= ZPAD;
} else {
/* End of flag characters */
break;
}
}
/* Process field width */
width = 0;
for ( ; ; fmt++ ) {
if ( ( ( unsigned ) ( *fmt - '0' ) ) < 10 ) {
width = ( width * 10 ) + ( *fmt - '0' );
} else {
break;
}
}
/* We don't do floating point */
/* Process length modifier */
length = &type_sizes[INT_LEN];
for ( ; ; fmt++ ) {
if ( *fmt == 'h' ) {
length--;
} else if ( *fmt == 'l' ) {
length++;
} else if ( *fmt == 'z' ) {
length = &type_sizes[SIZE_T_LEN];
} else {
break;
}
}
/* Process conversion specifier */
ptr = tmp_buf + sizeof ( tmp_buf ) - 1;
*ptr = '\0';
wptr = NULL;
if ( *fmt == 'c' ) {
if ( length < &type_sizes[LONG_LEN] ) {
cputchar ( ctx, va_arg ( args, unsigned int ) );
} else {
wchar_t wc;
size_t len;
wc = va_arg ( args, wint_t );
len = wcrtomb ( tmp_buf, wc, NULL );
tmp_buf[len] = '\0';
ptr = tmp_buf;
}
} else if ( *fmt == 's' ) {
if ( length < &type_sizes[LONG_LEN] ) {
ptr = va_arg ( args, char * );
if ( ! ptr )
ptr = "<NULL>";
} else {
wptr = va_arg ( args, wchar_t * );
if ( ! wptr )
ptr = "<NULL>";
}
} else if ( *fmt == 'p' ) {
intptr_t ptrval;
ptrval = ( intptr_t ) va_arg ( args, void * );
ptr = format_hex ( ptr, ptrval, width,
( ALT_FORM | LCASE ) );
} else if ( ( *fmt & ~0x20 ) == 'X' ) {
unsigned long long hex;
flags |= ( *fmt & 0x20 ); /* LCASE */
if ( *length >= sizeof ( unsigned long long ) ) {
hex = va_arg ( args, unsigned long long );
} else if ( *length >= sizeof ( unsigned long ) ) {
hex = va_arg ( args, unsigned long );
} else {
hex = va_arg ( args, unsigned int );
}
ptr = format_hex ( ptr, hex, width, flags );
} else if ( ( *fmt == 'd' ) || ( *fmt == 'i' ) ){
signed long decimal;
if ( *length >= sizeof ( signed long ) ) {
decimal = va_arg ( args, signed long );
} else {
decimal = va_arg ( args, signed int );
}
ptr = format_decimal ( ptr, decimal, width, flags );
} else if ( ( *fmt == 'u' ) || ( *fmt == 'U' )){
unsigned long long decimal;
if ( *length >= sizeof ( unsigned long long ) ) {
decimal = va_arg ( args, unsigned long long );
} else if ( *length >= sizeof ( unsigned long ) ) {
decimal = va_arg ( args, unsigned long );
} else {
decimal = va_arg ( args, unsigned int );
}
ptr = format_unsigned_decimal( ptr, decimal, width, flags );
} else {
*(--ptr) = *fmt;
}
/* Write out conversion result */
if ( wptr == NULL ) {
for ( ; *ptr ; ptr++ ) {
cputchar ( ctx, *ptr );
}
} else {
for ( ; *wptr ; wptr++ ) {
size_t len = wcrtomb ( tmp_buf, *wptr, NULL );
for ( ptr = tmp_buf ; len-- ; ptr++ ) {
cputchar ( ctx, *ptr );
}
}
}
}
return ctx->len;
}
/** Context used by vsnprintf() and friends */
struct sputc_context {
struct printf_context ctx;
/** Buffer for formatted string (used by printf_sputc()) */
char *buf;
/** Buffer length (used by printf_sputc()) */
size_t max_len;
};
/**
* Write character to buffer
*
* @v ctx Context
* @v c Character
*/
static void printf_sputc ( struct printf_context *ctx, unsigned int c ) {
struct sputc_context * sctx =
container_of ( ctx, struct sputc_context, ctx );
if ( ctx->len < sctx->max_len )
sctx->buf[ctx->len] = c;
}
/**
* Write a formatted string to a buffer
*
* @v buf Buffer into which to write the string
* @v size Size of buffer
* @v fmt Format string
* @v args Arguments corresponding to the format string
* @ret len Length of formatted string
*
* If the buffer is too small to contain the string, the returned
* length is the length that would have been written had enough space
* been available.
*/
int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args ) {
struct sputc_context sctx;
size_t len;
size_t end;
/* Hand off to vcprintf */
sctx.ctx.handler = printf_sputc;
sctx.buf = buf;
sctx.max_len = size;
len = vcprintf ( &sctx.ctx, fmt, args );
/* Add trailing NUL */
if ( size ) {
end = size - 1;
if ( len < end )
end = len;
buf[end] = '\0';
}
return len;
}
/**
* Write a formatted string to a buffer
*
* @v buf Buffer into which to write the string
* @v size Size of buffer
* @v fmt Format string
* @v ... Arguments corresponding to the format string
* @ret len Length of formatted string
*/
int snprintf ( char *buf, size_t size, const char *fmt, ... ) {
va_list args;
int i;
va_start ( args, fmt );
i = vsnprintf ( buf, size, fmt, args );
va_end ( args );
return i;
}
/**
* Version of vsnprintf() that accepts a signed buffer size
*
* @v buf Buffer into which to write the string
* @v size Size of buffer
* @v fmt Format string
* @v args Arguments corresponding to the format string
* @ret len Length of formatted string
*/
int vssnprintf ( char *buf, ssize_t ssize, const char *fmt, va_list args ) {
/* Treat negative buffer size as zero buffer size */
if ( ssize < 0 )
ssize = 0;
/* Hand off to vsnprintf */
return vsnprintf ( buf, ssize, fmt, args );
}
/**
* Version of vsnprintf() that accepts a signed buffer size
*
* @v buf Buffer into which to write the string
* @v size Size of buffer
* @v fmt Format string
* @v ... Arguments corresponding to the format string
* @ret len Length of formatted string
*/
int ssnprintf ( char *buf, ssize_t ssize, const char *fmt, ... ) {
va_list args;
int len;
/* Hand off to vssnprintf */
va_start ( args, fmt );
len = vssnprintf ( buf, ssize, fmt, args );
va_end ( args );
return len;
}
/**
* Write character to console
*
* @v ctx Context
* @v c Character
*/
static void printf_putchar ( struct printf_context *ctx __unused,
unsigned int c ) {
putchar ( c );
}
/**
* Write a formatted string to the console
*
* @v fmt Format string
* @v args Arguments corresponding to the format string
* @ret len Length of formatted string
*/
int vprintf ( const char *fmt, va_list args ) {
struct printf_context ctx;
/* Hand off to vcprintf */
ctx.handler = printf_putchar;
return vcprintf ( &ctx, fmt, args );
}
/**
* Write a formatted string to the console.
*
* @v fmt Format string
* @v ... Arguments corresponding to the format string
* @ret len Length of formatted string
*/
int printf ( const char *fmt, ... ) {
va_list args;
int i;
va_start ( args, fmt );
i = vprintf ( fmt, args );
va_end ( args );
return i;
}

View file

@ -0,0 +1,122 @@
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#if 0
#include <errno.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_snp.h>
#include "snpnet.h"
#include "nii.h"
/** @file
*
* SNP driver
*
*/
/**
* Check to see if driver supports a device
*
* @v device EFI device handle
* @ret rc Return status code
*/
static int snp_supported ( EFI_HANDLE device ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_STATUS efirc;
/* Check that this is not a device we are providing ourselves */
if ( find_snpdev ( device ) != NULL ) {
DBGCP ( device, "SNP %s is provided by this binary\n",
efi_handle_name ( device ) );
return -ENOTTY;
}
/* Test for presence of simple network protocol */
if ( ( efirc = bs->OpenProtocol ( device,
&efi_simple_network_protocol_guid,
NULL, efi_image_handle, device,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL))!=0){
DBGCP ( device, "SNP %s is not an SNP device\n",
efi_handle_name ( device ) );
return -EEFI ( efirc );
}
DBGC ( device, "SNP %s is an SNP device\n",
efi_handle_name ( device ) );
return 0;
}
/**
* Check to see if driver supports a device
*
* @v device EFI device handle
* @ret rc Return status code
*/
static int nii_supported ( EFI_HANDLE device ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_STATUS efirc;
/* Check that this is not a device we are providing ourselves */
if ( find_snpdev ( device ) != NULL ) {
DBGCP ( device, "NII %s is provided by this binary\n",
efi_handle_name ( device ) );
return -ENOTTY;
}
/* Test for presence of NII protocol */
if ( ( efirc = bs->OpenProtocol ( device,
&efi_nii31_protocol_guid,
NULL, efi_image_handle, device,
EFI_OPEN_PROTOCOL_TEST_PROTOCOL))!=0){
DBGCP ( device, "NII %s is not an NII device\n",
efi_handle_name ( device ) );
return -EEFI ( efirc );
}
DBGC ( device, "NII %s is an NII device\n",
efi_handle_name ( device ) );
return 0;
}
/** EFI SNP driver */
struct efi_driver snp_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
.name = "SNP",
.supported = snp_supported,
.start = snpnet_start,
.stop = snpnet_stop,
};
/** EFI NII driver */
struct efi_driver nii_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
.name = "NII",
.supported = nii_supported,
.start = nii_start,
.stop = nii_stop,
};
#endif

View file

@ -0,0 +1,255 @@
#ifndef _IPXE_SANBOOT_H
#define _IPXE_SANBOOT_H
/** @file
*
* iPXE sanboot API
*
* The sanboot API provides methods for hooking, unhooking,
* describing, and booting from SAN devices.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/api.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>
#include <ipxe/uri.h>
#include <ipxe/retry.h>
#include <ipxe/process.h>
#include <ipxe/blockdev.h>
#include <ipxe/acpi.h>
#include <config/sanboot.h>
/** A SAN path */
struct san_path {
/** Containing SAN device */
struct san_device *sandev;
/** Path index */
unsigned int index;
/** SAN device URI */
struct uri *uri;
/** List of open/closed paths */
struct list_head list;
/** Underlying block device interface */
struct interface block;
/** Process */
struct process process;
/** Path status */
int path_rc;
/** ACPI descriptor (if applicable) */
struct acpi_descriptor *desc;
};
/** A SAN device */
struct san_device {
/** Reference count */
struct refcnt refcnt;
/** List of SAN devices */
struct list_head list;
/** Drive number */
unsigned int drive;
/** Flags */
unsigned int flags;
/** Command interface */
struct interface command;
/** Command timeout timer */
struct retry_timer timer;
/** Command status */
int command_rc;
/** Raw block device capacity */
struct block_device_capacity capacity;
/** Block size shift
*
* To allow for emulation of CD-ROM access, this represents
* the left-shift required to translate from exposed logical
* I/O blocks to underlying blocks.
*/
unsigned int blksize_shift;
/** Drive is a CD-ROM */
int is_cdrom;
/** Driver private data */
void *priv;
/** Number of paths */
unsigned int paths;
/** Current active path */
struct san_path *active;
/** List of opened SAN paths */
struct list_head opened;
/** List of closed SAN paths */
struct list_head closed;
/** SAN paths */
struct san_path path[0];
unsigned int exdrive;
int int13_command;
void *x86_regptr;
uint8_t boot_catalog_sector[2048];
};
/** SAN device flags */
enum san_device_flags {
/** Device should not be included in description tables */
SAN_NO_DESCRIBE = 0x0001,
};
/**
* Calculate static inline sanboot API function name
*
* @v _prefix Subsystem prefix
* @v _api_func API function
* @ret _subsys_func Subsystem API function
*/
#define SANBOOT_INLINE( _subsys, _api_func ) \
SINGLE_API_INLINE ( SANBOOT_PREFIX_ ## _subsys, _api_func )
/**
* Provide a sanboot API implementation
*
* @v _prefix Subsystem prefix
* @v _api_func API function
* @v _func Implementing function
*/
#define PROVIDE_SANBOOT( _subsys, _api_func, _func ) \
PROVIDE_SINGLE_API ( SANBOOT_PREFIX_ ## _subsys, _api_func, _func )
/**
* Provide a static inline sanboot API implementation
*
* @v _prefix Subsystem prefix
* @v _api_func API function
*/
#define PROVIDE_SANBOOT_INLINE( _subsys, _api_func ) \
PROVIDE_SINGLE_API_INLINE ( SANBOOT_PREFIX_ ## _subsys, _api_func )
/* Include all architecture-independent sanboot API headers */
#include <ipxe/null_sanboot.h>
#include <ipxe/dummy_sanboot.h>
#include <ipxe/efi/efi_block.h>
/* Include all architecture-dependent sanboot API headers */
#include <bits/sanboot.h>
/**
* Hook SAN device
*
* @v drive Drive number
* @v uris List of URIs
* @v count Number of URIs
* @v flags Flags
* @ret drive Drive number, or negative error
*/
int san_hook ( unsigned int drive, struct uri **uris, unsigned int count,
unsigned int flags );
/**
* Unhook SAN device
*
* @v drive Drive number
*/
void san_unhook ( unsigned int drive );
/**
* Attempt to boot from a SAN device
*
* @v drive Drive number
* @v filename Filename (or NULL to use default)
* @ret rc Return status code
*/
int san_boot ( unsigned int drive, const char *filename );
/**
* Describe SAN devices for SAN-booted operating system
*
* @ret rc Return status code
*/
int san_describe ( void );
extern struct list_head san_devices;
/** Iterate over all SAN devices */
#define for_each_sandev( sandev ) \
list_for_each_entry ( (sandev), &san_devices, list )
/** There exist some SAN devices
*
* @ret existence Existence of SAN devices
*/
static inline int have_sandevs ( void ) {
return ( ! list_empty ( &san_devices ) );
}
/**
* Get reference to SAN device
*
* @v sandev SAN device
* @ret sandev SAN device
*/
static inline __attribute__ (( always_inline )) struct san_device *
sandev_get ( struct san_device *sandev ) {
ref_get ( &sandev->refcnt );
return sandev;
}
/**
* Drop reference to SAN device
*
* @v sandev SAN device
*/
static inline __attribute__ (( always_inline )) void
sandev_put ( struct san_device *sandev ) {
ref_put ( &sandev->refcnt );
}
/**
* Calculate SAN device block size
*
* @v sandev SAN device
* @ret blksize Sector size
*/
static inline size_t sandev_blksize ( struct san_device *sandev ) {
return ( sandev->capacity.blksize << sandev->blksize_shift );
}
/**
* Calculate SAN device capacity
*
* @v sandev SAN device
* @ret blocks Number of blocks
*/
static inline uint64_t sandev_capacity ( struct san_device *sandev ) {
return ( sandev->capacity.blocks >> sandev->blksize_shift );
}
/**
* Check if SAN device needs to be reopened
*
* @v sandev SAN device
* @ret needs_reopen SAN device needs to be reopened
*/
static inline int sandev_needs_reopen ( struct san_device *sandev ) {
return ( sandev->active == NULL );
}
extern struct san_device * sandev_find ( unsigned int drive );
extern int sandev_reopen ( struct san_device *sandev );
extern int sandev_reset ( struct san_device *sandev );
extern int sandev_read ( struct san_device *sandev, uint64_t lba,
unsigned int count, userptr_t buffer );
extern int sandev_write ( struct san_device *sandev, uint64_t lba,
unsigned int count, userptr_t buffer );
extern struct san_device * alloc_sandev ( struct uri **uris, unsigned int count,
size_t priv_size );
extern int register_sandev ( struct san_device *sandev, unsigned int drive,
unsigned int flags );
extern void unregister_sandev ( struct san_device *sandev );
extern unsigned int san_default_drive ( void );
#endif /* _IPXE_SANBOOT_H */

View file

@ -0,0 +1,227 @@
#ifndef __VENTOY_VDISK_H__
#define __VENTOY_VDISK_H__
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#define grub_uint64_t uint64_t
#define grub_uint32_t uint32_t
#define grub_uint16_t uint16_t
#define grub_uint8_t uint8_t
#define COMPILE_ASSERT(expr) extern char __compile_assert[(expr) ? 1 : -1]
#define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}
#pragma pack(1)
typedef struct ventoy_guid
{
grub_uint32_t data1;
grub_uint16_t data2;
grub_uint16_t data3;
grub_uint8_t data4[8];
}ventoy_guid;
typedef struct ventoy_image_disk_region
{
grub_uint32_t image_sector_count; /* image sectors contained in this region */
grub_uint32_t image_start_sector; /* image sector start */
grub_uint64_t disk_start_sector; /* disk sector start */
}ventoy_image_disk_region;
typedef struct ventoy_image_location
{
ventoy_guid guid;
/* image sector size, currently this value is always 2048 */
grub_uint32_t image_sector_size;
/* disk sector size, normally the value is 512 */
grub_uint32_t disk_sector_size;
grub_uint32_t region_count;
/*
* disk region data
* If the image file has more than one fragments in disk,
* there will be more than one region data here.
*
*/
ventoy_image_disk_region regions[1];
/* ventoy_image_disk_region regions[2~region_count-1] */
}ventoy_image_location;
typedef struct ventoy_os_param
{
ventoy_guid guid; // VENTOY_GUID
grub_uint8_t chksum; // checksum
grub_uint8_t vtoy_disk_guid[16];
grub_uint64_t vtoy_disk_size; // disk size in bytes
grub_uint16_t vtoy_disk_part_id; // begin with 1
grub_uint16_t vtoy_disk_part_type; // 0:exfat 1:ntfs other: reserved
char vtoy_img_path[384]; // It seems to be enough, utf-8 format
grub_uint64_t vtoy_img_size; // image file size in bytes
/*
* Ventoy will write a copy of ventoy_image_location data into runtime memory
* this is the physically address and length of that memory.
* Address 0 means no such data exist.
* Address will be aligned by 4KB.
*
*/
grub_uint64_t vtoy_img_location_addr;
grub_uint32_t vtoy_img_location_len;
grub_uint64_t vtoy_reserved[4]; // Internal use by ventoy
grub_uint8_t reserved[31];
}ventoy_os_param;
#pragma pack()
// compile assert to check that size of ventoy_os_param must be 512
COMPILE_ASSERT(sizeof(ventoy_os_param) == 512);
#pragma pack(4)
typedef struct ventoy_chain_head
{
ventoy_os_param os_param;
grub_uint32_t disk_drive;
grub_uint32_t drive_map;
grub_uint32_t disk_sector_size;
grub_uint64_t real_img_size_in_bytes;
grub_uint64_t virt_img_size_in_bytes;
grub_uint32_t boot_catalog;
grub_uint8_t boot_catalog_sector[2048];
grub_uint32_t img_chunk_offset;
grub_uint32_t img_chunk_num;
grub_uint32_t override_chunk_offset;
grub_uint32_t override_chunk_num;
grub_uint32_t virt_chunk_offset;
grub_uint32_t virt_chunk_num;
}ventoy_chain_head;
typedef struct ventoy_img_chunk
{
grub_uint32_t img_start_sector; //2KB
grub_uint32_t img_end_sector;
grub_uint64_t disk_start_sector; // in disk_sector_size
grub_uint64_t disk_end_sector;
}ventoy_img_chunk;
typedef struct ventoy_override_chunk
{
grub_uint64_t img_offset;
grub_uint32_t override_size;
grub_uint8_t override_data[512];
}ventoy_override_chunk;
typedef struct ventoy_virt_chunk
{
grub_uint32_t mem_sector_start;
grub_uint32_t mem_sector_end;
grub_uint32_t mem_sector_offset;
grub_uint32_t remap_sector_start;
grub_uint32_t remap_sector_end;
grub_uint32_t org_sector_start;
}ventoy_virt_chunk;
#pragma pack()
#define ventoy_debug_pause() \
{\
printf("\nPress Ctrl+C to continue......");\
sleep(3600);\
printf("\n");\
}
typedef struct ventoy_sector_flag
{
uint8_t flag; // 0:init 1:mem 2:remap
uint64_t remap_lba;
}ventoy_sector_flag;
#define VENTOY_BIOS_FAKE_DRIVE 0xFE
extern int g_debug;
extern char *g_cmdline_copy;
extern void *g_initrd_addr;
extern size_t g_initrd_len;
extern uint32_t g_disk_sector_size;
unsigned int ventoy_int13_hook (ventoy_chain_head *chain);
int ventoy_int13_boot ( unsigned int drive, void *imginfo, const char *cmdline);
void * ventoy_get_runtime_addr(void);
int ventoy_boot_vdisk(void *data);
uint32_t CalculateCrc32
(
const void *Buffer,
uint32_t Length,
uint32_t InitValue
);
struct smbios3_entry {
uint8_t signature[5];
/** Checksum */
uint8_t checksum;
/** Length */
uint8_t len;
/** Major version */
uint8_t major;
/** Minor version */
uint8_t minor;
uint8_t docrev;
uint8_t revision;
uint8_t reserved;
uint32_t maxsize;
uint64_t address;
} __attribute__ (( packed ));
typedef struct isolinux_boot_info
{
uint32_t isolinux0;
uint32_t isolinux1;
uint32_t PvdLocation;
uint32_t BootFileLocation;
uint32_t BootFileLen;
uint32_t BootFileChecksum;
uint8_t Reserved[40];
}isolinux_boot_info;
//#undef DBGLVL
//#define DBGLVL 7
#endif /* __VENTOY_VDISK_H__ */

View file

@ -0,0 +1,467 @@
/*
* Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program 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 2 of the
* License, or any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#if 0
#include <stdlib.h>
#include <errno.h>
#include <ipxe/pci.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_pci.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/Protocol/PciIo.h>
#include <ipxe/efi/Protocol/PciRootBridgeIo.h>
/** @file
*
* iPXE PCI I/O API for EFI
*
*/
/* Disambiguate the various error causes */
#define EINFO_EEFI_PCI \
__einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
"Could not open PCI I/O protocol" )
#define EINFO_EEFI_PCI_NOT_PCI \
__einfo_platformify ( EINFO_EEFI_PCI, EFI_UNSUPPORTED, \
"Not a PCI device" )
#define EEFI_PCI_NOT_PCI __einfo_error ( EINFO_EEFI_PCI_NOT_PCI )
#define EINFO_EEFI_PCI_IN_USE \
__einfo_platformify ( EINFO_EEFI_PCI, EFI_ACCESS_DENIED, \
"PCI device already has a driver" )
#define EEFI_PCI_IN_USE __einfo_error ( EINFO_EEFI_PCI_IN_USE )
#define EEFI_PCI( efirc ) \
EPLATFORM ( EINFO_EEFI_PCI, efirc, \
EEFI_PCI_NOT_PCI, EEFI_PCI_IN_USE )
/******************************************************************************
*
* iPXE PCI API
*
******************************************************************************
*/
/**
* Locate EFI PCI root bridge I/O protocol
*
* @v pci PCI device
* @ret handle EFI PCI root bridge handle
* @ret root EFI PCI root bridge I/O protocol, or NULL if not found
* @ret rc Return status code
*/
static int efipci_root ( struct pci_device *pci, EFI_HANDLE *handle,
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL **root ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_HANDLE *handles;
UINTN num_handles;
union {
void *interface;
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
} u;
EFI_STATUS efirc;
UINTN i;
int rc;
/* Enumerate all handles */
if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol,
&efi_pci_root_bridge_io_protocol_guid,
NULL, &num_handles, &handles ) ) != 0 ) {
rc = -EEFI ( efirc );
DBGC ( pci, "EFIPCI " PCI_FMT " cannot locate root bridges: "
"%s\n", PCI_ARGS ( pci ), strerror ( rc ) );
goto err_locate;
}
/* Look for matching root bridge I/O protocol */
for ( i = 0 ; i < num_handles ; i++ ) {
*handle = handles[i];
if ( ( efirc = bs->OpenProtocol ( *handle,
&efi_pci_root_bridge_io_protocol_guid,
&u.interface, efi_image_handle, *handle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
rc = -EEFI ( efirc );
DBGC ( pci, "EFIPCI " PCI_FMT " cannot open %s: %s\n",
PCI_ARGS ( pci ), efi_handle_name ( *handle ),
strerror ( rc ) );
continue;
}
if ( u.root->SegmentNumber == PCI_SEG ( pci->busdevfn ) ) {
*root = u.root;
bs->FreePool ( handles );
return 0;
}
bs->CloseProtocol ( *handle,
&efi_pci_root_bridge_io_protocol_guid,
efi_image_handle, *handle );
}
DBGC ( pci, "EFIPCI " PCI_FMT " found no root bridge\n",
PCI_ARGS ( pci ) );
rc = -ENOENT;
bs->FreePool ( handles );
err_locate:
return rc;
}
/**
* Calculate EFI PCI configuration space address
*
* @v pci PCI device
* @v location Encoded offset and width
* @ret address EFI PCI address
*/
static unsigned long efipci_address ( struct pci_device *pci,
unsigned long location ) {
return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
PCI_SLOT ( pci->busdevfn ),
PCI_FUNC ( pci->busdevfn ),
EFIPCI_OFFSET ( location ) );
}
/**
* Read from PCI configuration space
*
* @v pci PCI device
* @v location Encoded offset and width
* @ret value Value
* @ret rc Return status code
*/
int efipci_read ( struct pci_device *pci, unsigned long location,
void *value ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
EFI_HANDLE handle;
EFI_STATUS efirc;
int rc;
/* Identify root bridge */
if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 )
goto err_root;
/* Read from configuration space */
if ( ( efirc = root->Pci.Read ( root, EFIPCI_WIDTH ( location ),
efipci_address ( pci, location ), 1,
value ) ) != 0 ) {
rc = -EEFI ( efirc );
DBGC ( pci, "EFIPCI " PCI_FMT " config read from offset %02lx "
"failed: %s\n", PCI_ARGS ( pci ),
EFIPCI_OFFSET ( location ), strerror ( rc ) );
goto err_read;
}
err_read:
bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid,
efi_image_handle, handle );
err_root:
return rc;
}
/**
* Write to PCI configuration space
*
* @v pci PCI device
* @v location Encoded offset and width
* @v value Value
* @ret rc Return status code
*/
int efipci_write ( struct pci_device *pci, unsigned long location,
unsigned long value ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
EFI_HANDLE handle;
EFI_STATUS efirc;
int rc;
/* Identify root bridge */
if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 )
goto err_root;
/* Read from configuration space */
if ( ( efirc = root->Pci.Write ( root, EFIPCI_WIDTH ( location ),
efipci_address ( pci, location ), 1,
&value ) ) != 0 ) {
rc = -EEFI ( efirc );
DBGC ( pci, "EFIPCI " PCI_FMT " config write to offset %02lx "
"failed: %s\n", PCI_ARGS ( pci ),
EFIPCI_OFFSET ( location ), strerror ( rc ) );
goto err_write;
}
err_write:
bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid,
efi_image_handle, handle );
err_root:
return rc;
}
PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
/******************************************************************************
*
* EFI PCI device instantiation
*
******************************************************************************
*/
/**
* Open EFI PCI device
*
* @v device EFI device handle
* @v attributes Protocol opening attributes
* @v pci PCI device to fill in
* @ret rc Return status code
*/
int efipci_open ( EFI_HANDLE device, UINT32 attributes,
struct pci_device *pci ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
union {
EFI_PCI_IO_PROTOCOL *pci_io;
void *interface;
} pci_io;
UINTN pci_segment, pci_bus, pci_dev, pci_fn;
unsigned int busdevfn;
EFI_STATUS efirc;
int rc;
/* See if device is a PCI device */
if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
&pci_io.interface, efi_image_handle,
device, attributes ) ) != 0 ) {
rc = -EEFI_PCI ( efirc );
DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n",
efi_handle_name ( device ), strerror ( rc ) );
goto err_open_protocol;
}
/* Get PCI bus:dev.fn address */
if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
&pci_bus, &pci_dev,
&pci_fn ) ) != 0 ) {
rc = -EEFI ( efirc );
DBGC ( device, "EFIPCI %s could not get PCI location: %s\n",
efi_handle_name ( device ), strerror ( rc ) );
goto err_get_location;
}
busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn );
pci_init ( pci, busdevfn );
DBGCP ( device, "EFIPCI " PCI_FMT " is %s\n",
PCI_ARGS ( pci ), efi_handle_name ( device ) );
/* Try to enable I/O cycles, memory cycles, and bus mastering.
* Some platforms will 'helpfully' report errors if these bits
* can't be enabled (for example, if the card doesn't actually
* support I/O cycles). Work around any such platforms by
* enabling bits individually and simply ignoring any errors.
*/
pci_io.pci_io->Attributes ( pci_io.pci_io,
EfiPciIoAttributeOperationEnable,
EFI_PCI_IO_ATTRIBUTE_IO, NULL );
pci_io.pci_io->Attributes ( pci_io.pci_io,
EfiPciIoAttributeOperationEnable,
EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
pci_io.pci_io->Attributes ( pci_io.pci_io,
EfiPciIoAttributeOperationEnable,
EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
/* Populate PCI device */
if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
DBGC ( device, "EFIPCI " PCI_FMT " cannot read PCI "
"configuration: %s\n",
PCI_ARGS ( pci ), strerror ( rc ) );
goto err_pci_read_config;
}
return 0;
err_pci_read_config:
err_get_location:
bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
efi_image_handle, device );
err_open_protocol:
return rc;
}
/**
* Close EFI PCI device
*
* @v device EFI device handle
*/
void efipci_close ( EFI_HANDLE device ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
efi_image_handle, device );
}
/**
* Get EFI PCI device information
*
* @v device EFI device handle
* @v pci PCI device to fill in
* @ret rc Return status code
*/
int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
int rc;
/* Open PCI device, if possible */
if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
pci ) ) != 0 )
return rc;
/* Close PCI device */
efipci_close ( device );
return 0;
}
/******************************************************************************
*
* EFI PCI driver
*
******************************************************************************
*/
/**
* Check to see if driver supports a device
*
* @v device EFI device handle
* @ret rc Return status code
*/
static int efipci_supported ( EFI_HANDLE device ) {
struct pci_device pci;
int rc;
/* Get PCI device information */
if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
return rc;
/* Look for a driver */
if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) {
DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) "
"has no driver\n", PCI_ARGS ( &pci ), pci.vendor,
pci.device, pci.class );
return rc;
}
DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) has driver "
"\"%s\"\n", PCI_ARGS ( &pci ), pci.vendor, pci.device,
pci.class, pci.id->name );
return 0;
}
/**
* Attach driver to device
*
* @v efidev EFI device
* @ret rc Return status code
*/
static int efipci_start ( struct efi_device *efidev ) {
EFI_HANDLE device = efidev->device;
struct pci_device *pci;
int rc;
/* Allocate PCI device */
pci = zalloc ( sizeof ( *pci ) );
if ( ! pci ) {
rc = -ENOMEM;
goto err_alloc;
}
/* Open PCI device */
if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
EFI_OPEN_PROTOCOL_EXCLUSIVE ),
pci ) ) != 0 ) {
DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
efi_handle_name ( device ), strerror ( rc ) );
DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
goto err_open;
}
/* Find driver */
if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
DBGC ( device, "EFIPCI " PCI_FMT " has no driver\n",
PCI_ARGS ( pci ) );
goto err_find_driver;
}
/* Mark PCI device as a child of the EFI device */
pci->dev.parent = &efidev->dev;
list_add ( &pci->dev.siblings, &efidev->dev.children );
/* Probe driver */
if ( ( rc = pci_probe ( pci ) ) != 0 ) {
DBGC ( device, "EFIPCI " PCI_FMT " could not probe driver "
"\"%s\": %s\n", PCI_ARGS ( pci ), pci->id->name,
strerror ( rc ) );
goto err_probe;
}
DBGC ( device, "EFIPCI " PCI_FMT " using driver \"%s\"\n",
PCI_ARGS ( pci ), pci->id->name );
efidev_set_drvdata ( efidev, pci );
return 0;
pci_remove ( pci );
err_probe:
list_del ( &pci->dev.siblings );
err_find_driver:
efipci_close ( device );
err_open:
free ( pci );
err_alloc:
return rc;
}
/**
* Detach driver from device
*
* @v efidev EFI device
*/
static void efipci_stop ( struct efi_device *efidev ) {
struct pci_device *pci = efidev_get_drvdata ( efidev );
EFI_HANDLE device = efidev->device;
pci_remove ( pci );
list_del ( &pci->dev.siblings );
efipci_close ( device );
free ( pci );
}
/** EFI PCI driver */
struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
.name = "PCI",
.supported = efipci_supported,
.start = efipci_start,
.stop = efipci_stop,
};
#endif

File diff suppressed because it is too large Load diff