From e44e3482bdb4d0ebde2d8b41830ac2cdb07948fb Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 28 Aug 2015 09:58:54 +0800 Subject: Add qemu 2.4.0 Change-Id: Ic99cbad4b61f8b127b7dc74d04576c0bcbaaf4f5 Signed-off-by: Yang Zhang --- .../roms/ipxe/src/arch/i386/interface/pcbios/apm.c | 108 ++ .../ipxe/src/arch/i386/interface/pcbios/bios_nap.c | 16 + .../src/arch/i386/interface/pcbios/bios_reboot.c | 48 + .../src/arch/i386/interface/pcbios/bios_smbios.c | 61 + .../src/arch/i386/interface/pcbios/bios_timer.c | 66 + .../ipxe/src/arch/i386/interface/pcbios/biosint.c | 92 + .../ipxe/src/arch/i386/interface/pcbios/int13.c | 1989 ++++++++++++++++++++ .../arch/i386/interface/pcbios/memtop_umalloc.c | 178 ++ .../ipxe/src/arch/i386/interface/pcbios/pcibios.c | 115 ++ .../src/arch/i386/interface/pcbios/rtc_entropy.c | 199 ++ .../ipxe/src/arch/i386/interface/pcbios/rtc_time.c | 138 ++ .../ipxe/src/arch/i386/interface/pcbios/vesafb.c | 538 ++++++ 12 files changed, 3548 insertions(+) create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/apm.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_nap.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_reboot.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_smbios.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_timer.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/biosint.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/int13.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/memtop_umalloc.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/pcibios.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_entropy.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_time.c create mode 100644 qemu/roms/ipxe/src/arch/i386/interface/pcbios/vesafb.c (limited to 'qemu/roms/ipxe/src/arch/i386/interface/pcbios') diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/apm.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/apm.c new file mode 100644 index 000000000..3b13e1cd0 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/apm.c @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2013 Marin Hannache . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** + * @file + * + * Advanced Power Management + * + */ + +#include +#include +#include + +/** + * Power off the computer using APM + * + * @ret rc Return status code + */ +static int apm_poweroff ( void ) { + uint16_t apm_version; + uint16_t apm_signature; + uint16_t apm_flags; + uint16_t carry; + + /* APM check */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t" + "adc %%edx,0\n\t" ) + : "=a" ( apm_version ), "=b" ( apm_signature ), + "=c" ( apm_flags ), "=d" ( carry ) + : "a" ( 0x5300 ), "b" ( 0x0000 ), + "d" ( 0x0000 ) ); + if ( carry ) { + DBG ( "APM not present\n" ); + return -ENOTSUP; + } + if ( apm_signature != 0x504d ) { /* signature 'PM' */ + DBG ( "APM not present\n" ); + return -ENOTSUP; + } + if ( apm_version < 0x0101 ) { /* Need version 1.1+ */ + DBG ( "APM 1.1+ not supported\n" ); + return -ENOTSUP; + } + if ( ( apm_flags & 0x8 ) == 0x8 ) { + DBG ( "APM power management disabled\n" ); + return -EPERM; + } + DBG2 ( "APM check completed\n" ); + + /* APM initialisation */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t" + "adc %%edx,0\n\t" ) + : "=d" ( carry ) + : "a" ( 0x5301 ), "b" ( 0x0000 ), + "d" ( 0x0000 ) ); + if ( carry ) { + DBG ( "APM initialisation failed\n" ); + return -EIO; + } + DBG2 ( "APM initialisation completed\n" ); + + /* Set APM driver version */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t" + "adc %%edx,0\n\t" ) + : "=d" ( carry ) + : "a" ( 0x530e ), "b" ( 0x0000 ), + "c" ( 0x0101 ), "d" ( 0x0000 ) ); + if ( carry ) { + DBG ( "APM setting driver version failed\n" ); + return -EIO; + } + DBG2 ( "APM driver version set\n" ); + + /* Setting power state to off */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t" + "adc %%edx,0\n\t" ) + : "=d" ( carry ) + : "a" ( 0x5307 ), "b" ( 0x0001 ), + "c" ( 0x0003 ), "d" ( 0x0000) ); + if ( carry ) { + DBG ( "APM setting power state failed\n" ); + return -ENOTTY; + } + + /* Should never happen */ + return -ECANCELED; +} + +PROVIDE_REBOOT ( pcbios, poweroff, apm_poweroff ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_nap.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_nap.c new file mode 100644 index 000000000..1e7de756b --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_nap.c @@ -0,0 +1,16 @@ +#include +#include + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** + * Save power by halting the CPU until the next interrupt + * + */ +static void bios_cpu_nap ( void ) { + __asm__ __volatile__ ( "sti\n\t" + "hlt\n\t" + "cli\n\t" ); +} + +PROVIDE_NAP ( pcbios, cpu_nap, bios_cpu_nap ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_reboot.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_reboot.c new file mode 100644 index 000000000..68546b2e5 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_reboot.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2010 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** @file + * + * Standard PC-BIOS reboot mechanism + * + */ + +#include +#include +#include + +/** + * Reboot system + * + * @v warm Perform a warm reboot + */ +static void bios_reboot ( int warm ) { + uint16_t flag; + + /* Configure BIOS for cold/warm reboot */ + flag = ( warm ? BDA_REBOOT_WARM : 0 ); + put_real ( flag, BDA_SEG, BDA_REBOOT ); + + /* Jump to system reset vector */ + __asm__ __volatile__ ( REAL_CODE ( "ljmp $0xf000, $0xfff0" ) : : ); +} + +PROVIDE_REBOOT ( pcbios, reboot, bios_reboot ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_smbios.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_smbios.c new file mode 100644 index 000000000..dd7897e29 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_smbios.c @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2007 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include +#include +#include +#include +#include + +/** @file + * + * System Management BIOS + * + */ + +/** + * Find SMBIOS + * + * @v smbios SMBIOS entry point descriptor structure to fill in + * @ret rc Return status code + */ +static int bios_find_smbios ( struct smbios *smbios ) { + struct smbios_entry entry; + int rc; + + /* Scan through BIOS segment to find SMBIOS entry point */ + if ( ( rc = find_smbios_entry ( real_to_user ( BIOS_SEG, 0 ), 0x10000, + &entry ) ) != 0 ) + return rc; + + /* Fill in entry point descriptor structure */ + smbios->address = phys_to_user ( entry.smbios_address ); + smbios->len = entry.smbios_len; + smbios->count = entry.smbios_count; + smbios->version = SMBIOS_VERSION ( entry.major, entry.minor ); + + return 0; +} + +PROVIDE_SMBIOS ( pcbios, find_smbios, bios_find_smbios ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_timer.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_timer.c new file mode 100644 index 000000000..65bbf9e01 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/bios_timer.c @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2008 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** @file + * + * BIOS timer + * + */ + +#include +#include +#include + +/** + * Get current system time in ticks + * + * @ret ticks Current time, in ticks + * + * Use direct memory access to BIOS variables, longword 0040:006C + * (ticks today) and byte 0040:0070 (midnight crossover flag) instead + * of calling timeofday BIOS interrupt. + */ +static unsigned long bios_currticks ( void ) { + static int days = 0; + uint32_t ticks; + uint8_t midnight; + + /* Re-enable interrupts so that the timer interrupt can occur */ + __asm__ __volatile__ ( "sti\n\t" + "nop\n\t" + "nop\n\t" + "cli\n\t" ); + + get_real ( ticks, BDA_SEG, 0x006c ); + get_real ( midnight, BDA_SEG, 0x0070 ); + + if ( midnight ) { + midnight = 0; + put_real ( midnight, BDA_SEG, 0x0070 ); + days += 0x1800b0; + } + + return ( days + ticks ); +} + +PROVIDE_TIMER_INLINE ( pcbios, udelay ); +PROVIDE_TIMER ( pcbios, currticks, bios_currticks ); +PROVIDE_TIMER_INLINE ( pcbios, ticks_per_sec ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/biosint.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/biosint.c new file mode 100644 index 000000000..a193defa3 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/biosint.c @@ -0,0 +1,92 @@ +#include +#include +#include + +/** + * @file BIOS interrupts + * + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** + * Hook INT vector + * + * @v interrupt INT number + * @v handler Offset within .text16 to interrupt handler + * @v chain_vector Vector for chaining to previous handler + * + * Hooks in an i386 INT handler. The handler itself must reside + * within the .text16 segment. @c chain_vector will be filled in with + * the address of the previously-installed handler for this interrupt; + * the handler should probably exit by ljmping via this vector. + */ +void hook_bios_interrupt ( unsigned int interrupt, unsigned int handler, + struct segoff *chain_vector ) { + struct segoff vector = { + .segment = rm_cs, + .offset = handler, + }; + + DBG ( "Hooking INT %#02x to %04x:%04x\n", + interrupt, rm_cs, handler ); + + if ( ( chain_vector->segment != 0 ) || + ( chain_vector->offset != 0 ) ) { + /* Already hooked; do nothing */ + DBG ( "...already hooked\n" ); + return; + } + + copy_from_real ( chain_vector, 0, ( interrupt * 4 ), + sizeof ( *chain_vector ) ); + DBG ( "...chaining to %04x:%04x\n", + chain_vector->segment, chain_vector->offset ); + if ( DBG_LOG ) { + char code[64]; + copy_from_real ( code, chain_vector->segment, + chain_vector->offset, sizeof ( code ) ); + DBG_HDA ( *chain_vector, code, sizeof ( code ) ); + } + + copy_to_real ( 0, ( interrupt * 4 ), &vector, sizeof ( vector ) ); + hooked_bios_interrupts++; +} + +/** + * Unhook INT vector + * + * @v interrupt INT number + * @v handler Offset within .text16 to interrupt handler + * @v chain_vector Vector containing address of previous handler + * + * Unhooks an i386 interrupt handler hooked by hook_i386_vector(). + * Note that this operation may fail, if some external code has hooked + * the vector since we hooked in our handler. If it fails, it means + * that it is not possible to unhook our handler, and we must leave it + * (and its chaining vector) resident in memory. + */ +int unhook_bios_interrupt ( unsigned int interrupt, unsigned int handler, + struct segoff *chain_vector ) { + struct segoff vector; + + DBG ( "Unhooking INT %#02x from %04x:%04x\n", + interrupt, rm_cs, handler ); + + copy_from_real ( &vector, 0, ( interrupt * 4 ), sizeof ( vector ) ); + if ( ( vector.segment != rm_cs ) || ( vector.offset != handler ) ) { + DBG ( "...cannot unhook; vector points to %04x:%04x\n", + vector.segment, vector.offset ); + return -EBUSY; + } + + DBG ( "...restoring to %04x:%04x\n", + chain_vector->segment, chain_vector->offset ); + copy_to_real ( 0, ( interrupt * 4 ), chain_vector, + sizeof ( *chain_vector ) ); + + chain_vector->segment = 0; + chain_vector->offset = 0; + hooked_bios_interrupts--; + return 0; +} diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/int13.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/int13.c new file mode 100644 index 000000000..1c7a8128f --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/int13.c @@ -0,0 +1,1989 @@ +/* + * Copyright (C) 2006 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** @file + * + * INT 13 emulation + * + * This module provides a mechanism for exporting block devices via + * the BIOS INT 13 disk interrupt interface. + * + */ + +/** + * Overall timeout for INT 13 commands (independent of underlying device + * + * Underlying devices should ideally never become totally stuck. + * However, if they do, then the INT 13 mechanism provides no means + * for the caller to cancel the operation, and the machine appears to + * hang. Use an overall timeout for all commands to avoid this + * problem and bounce timeout failures to the caller. + */ +#define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC ) + +/** An INT 13 emulated drive */ +struct int13_drive { + /** Reference count */ + struct refcnt refcnt; + /** List of all registered drives */ + struct list_head list; + + /** Block device URI */ + struct uri *uri; + /** Underlying block device interface */ + struct interface block; + + /** BIOS in-use drive number (0x00-0xff) */ + unsigned int drive; + /** BIOS natural drive number (0x00-0xff) + * + * This is the drive number that would have been assigned by + * 'naturally' appending the drive to the end of the BIOS + * drive list. + * + * If the emulated drive replaces a preexisting drive, this is + * the drive number that the preexisting drive gets remapped + * to. + */ + unsigned int natural_drive; + + /** Block device capacity */ + struct block_device_capacity capacity; + /** INT 13 emulated blocksize shift + * + * To allow for emulation of CD-ROM access, this represents + * the left-shift required to translate from INT 13 blocks to + * underlying blocks. + */ + unsigned int blksize_shift; + + /** Number of cylinders + * + * The cylinder number field in an INT 13 call is ten bits + * wide, giving a maximum of 1024 cylinders. Conventionally, + * when the 7.8GB limit of a CHS address is exceeded, it is + * the number of cylinders that is increased beyond the + * addressable limit. + */ + unsigned int cylinders; + /** Number of heads + * + * The head number field in an INT 13 call is eight bits wide, + * giving a maximum of 256 heads. However, apparently all + * versions of MS-DOS up to and including Win95 fail with 256 + * heads, so the maximum encountered in practice is 255. + */ + unsigned int heads; + /** Number of sectors per track + * + * The sector number field in an INT 13 call is six bits wide, + * giving a maximum of 63 sectors, since sector numbering + * (unlike head and cylinder numbering) starts at 1, not 0. + */ + unsigned int sectors_per_track; + + /** Drive is a CD-ROM */ + int is_cdrom; + /** Address of El Torito boot catalog (if any) */ + unsigned int boot_catalog; + + /** Underlying device status, if in error */ + int block_rc; + /** Status of last operation */ + int last_status; +}; + +/** Vector for chaining to other INT 13 handlers */ +static struct segoff __text16 ( int13_vector ); +#define int13_vector __use_text16 ( int13_vector ) + +/** Assembly wrapper */ +extern void int13_wrapper ( void ); + +/** Dummy floppy disk parameter table */ +static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = { + /* 512 bytes per sector */ + .bytes_per_sector = 0x02, + /* Highest sectors per track that we ever return */ + .sectors_per_track = 48, +}; +#define int13_fdd_params __use_data16 ( int13_fdd_params ) + +/** List of registered emulated drives */ +static LIST_HEAD ( int13s ); + +/** + * Equipment word + * + * This is a cached copy of the BIOS Data Area equipment word at + * 40:10. + */ +static uint16_t equipment_word; + +/** + * Number of BIOS floppy disk drives + * + * This is derived from the equipment word. It is held in .text16 to + * allow for easy access by the INT 13,08 wrapper. + */ +static uint8_t __text16 ( num_fdds ); +#define num_fdds __use_text16 ( num_fdds ) + +/** + * Number of BIOS hard disk drives + * + * This is a cached copy of the BIOS Data Area number of hard disk + * drives at 40:75. It is held in .text16 to allow for easy access by + * the INT 13,08 wrapper. + */ +static uint8_t __text16 ( num_drives ); +#define num_drives __use_text16 ( num_drives ) + +/** + * Calculate INT 13 drive sector size + * + * @v int13 Emulated drive + * @ret blksize Sector size + */ +static inline size_t int13_blksize ( struct int13_drive *int13 ) { + return ( int13->capacity.blksize << int13->blksize_shift ); +} + +/** + * Calculate INT 13 drive capacity + * + * @v int13 Emulated drive + * @ret blocks Number of blocks + */ +static inline uint64_t int13_capacity ( struct int13_drive *int13 ) { + return ( int13->capacity.blocks >> int13->blksize_shift ); +} + +/** + * Calculate INT 13 drive capacity (limited to 32 bits) + * + * @v int13 Emulated drive + * @ret blocks Number of blocks + */ +static inline uint32_t int13_capacity32 ( struct int13_drive *int13 ) { + uint64_t capacity = int13_capacity ( int13 ); + return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff ); +} + +/** + * Test if INT 13 drive is a floppy disk drive + * + * @v int13 Emulated drive + * @ret is_fdd Emulated drive is a floppy disk + */ +static inline int int13_is_fdd ( struct int13_drive *int13 ) { + return ( ! ( int13->drive & 0x80 ) ); +} + +/** An INT 13 command */ +struct int13_command { + /** Status */ + int rc; + /** INT 13 drive */ + struct int13_drive *int13; + /** Underlying block device interface */ + struct interface block; + /** Command timeout timer */ + struct retry_timer timer; +}; + +/** + * Record INT 13 drive capacity + * + * @v command INT 13 command + * @v capacity Block device capacity + */ +static void int13_command_capacity ( struct int13_command *command, + struct block_device_capacity *capacity ) { + memcpy ( &command->int13->capacity, capacity, + sizeof ( command->int13->capacity ) ); +} + +/** + * Close INT 13 command + * + * @v command INT 13 command + * @v rc Reason for close + */ +static void int13_command_close ( struct int13_command *command, int rc ) { + intf_restart ( &command->block, rc ); + stop_timer ( &command->timer ); + command->rc = rc; +} + +/** + * Handle INT 13 command timer expiry + * + * @v timer Timer + */ +static void int13_command_expired ( struct retry_timer *timer, + int over __unused ) { + struct int13_command *command = + container_of ( timer, struct int13_command, timer ); + + int13_command_close ( command, -ETIMEDOUT ); +} + +/** INT 13 command interface operations */ +static struct interface_operation int13_command_op[] = { + INTF_OP ( intf_close, struct int13_command *, int13_command_close ), + INTF_OP ( block_capacity, struct int13_command *, + int13_command_capacity ), +}; + +/** INT 13 command interface descriptor */ +static struct interface_descriptor int13_command_desc = + INTF_DESC ( struct int13_command, block, int13_command_op ); + +/** + * Open (or reopen) INT 13 emulated drive underlying block device + * + * @v int13 Emulated drive + * @ret rc Return status code + */ +static int int13_reopen_block ( struct int13_drive *int13 ) { + int rc; + + /* Close any existing block device */ + intf_restart ( &int13->block, -ECONNRESET ); + + /* Open block device */ + if ( ( rc = xfer_open_uri ( &int13->block, int13->uri ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x could not reopen block " + "device: %s\n", int13->drive, strerror ( rc ) ); + int13->block_rc = rc; + return rc; + } + + /* Clear block device error status */ + int13->block_rc = 0; + + return 0; +} + +/** + * Prepare to issue INT 13 command + * + * @v command INT 13 command + * @v int13 Emulated drive + * @ret rc Return status code + */ +static int int13_command_start ( struct int13_command *command, + struct int13_drive *int13 ) { + int rc; + + /* Sanity check */ + assert ( command->int13 == NULL ); + assert ( ! timer_running ( &command->timer ) ); + + /* Reopen block device if necessary */ + if ( ( int13->block_rc != 0 ) && + ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) ) + return rc; + + /* Initialise command */ + command->rc = -EINPROGRESS; + command->int13 = int13; + start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT ); + + /* Wait for block control interface to become ready */ + while ( ( command->rc == -EINPROGRESS ) && + ( xfer_window ( &int13->block ) == 0 ) ) { + step(); + } + + return ( ( command->rc == -EINPROGRESS ) ? + int13->block_rc : command->rc ); +} + +/** + * Wait for INT 13 command to complete + * + * @v command INT 13 command + * @ret rc Return status code + */ +static int int13_command_wait ( struct int13_command *command ) { + + /* Sanity check */ + assert ( timer_running ( &command->timer ) ); + + /* Wait for command to complete */ + while ( command->rc == -EINPROGRESS ) + step(); + + assert ( ! timer_running ( &command->timer ) ); + return command->rc; +} + +/** + * Terminate INT 13 command + * + * @v command INT 13 command + */ +static void int13_command_stop ( struct int13_command *command ) { + stop_timer ( &command->timer ); + command->int13 = NULL; +} + +/** The single active INT 13 command */ +static struct int13_command int13_command = { + .block = INTF_INIT ( int13_command_desc ), + .timer = TIMER_INIT ( int13_command_expired ), +}; + +/** + * Read from or write to INT 13 drive + * + * @v int13 Emulated drive + * @v lba Starting logical block address + * @v count Number of logical blocks + * @v buffer Data buffer + * @v block_rw Block read/write method + * @ret rc Return status code + */ +static int int13_rw ( struct int13_drive *int13, uint64_t lba, + unsigned int count, userptr_t buffer, + int ( * block_rw ) ( struct interface *control, + struct interface *data, + uint64_t lba, unsigned int count, + userptr_t buffer, size_t len ) ) { + struct int13_command *command = &int13_command; + unsigned int frag_count; + size_t frag_len; + int rc; + + /* Translate to underlying blocksize */ + lba <<= int13->blksize_shift; + count <<= int13->blksize_shift; + + while ( count ) { + + /* Determine fragment length */ + frag_count = count; + if ( frag_count > int13->capacity.max_count ) + frag_count = int13->capacity.max_count; + frag_len = ( int13->capacity.blksize * frag_count ); + + /* Issue command */ + if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) || + ( ( rc = block_rw ( &int13->block, &command->block, lba, + frag_count, buffer, + frag_len ) ) != 0 ) || + ( ( rc = int13_command_wait ( command ) ) != 0 ) ) { + int13_command_stop ( command ); + return rc; + } + int13_command_stop ( command ); + + /* Move to next fragment */ + lba += frag_count; + count -= frag_count; + buffer = userptr_add ( buffer, frag_len ); + } + + return 0; +} + +/** + * Read INT 13 drive capacity + * + * @v int13 Emulated drive + * @ret rc Return status code + */ +static int int13_read_capacity ( struct int13_drive *int13 ) { + struct int13_command *command = &int13_command; + int rc; + + /* Issue command */ + if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) || + ( ( rc = block_read_capacity ( &int13->block, + &command->block ) ) != 0 ) || + ( ( rc = int13_command_wait ( command ) ) != 0 ) ) { + int13_command_stop ( command ); + return rc; + } + + int13_command_stop ( command ); + return 0; +} + +/** + * Parse ISO9660 parameters + * + * @v int13 Emulated drive + * @v scratch Scratch area for single-sector reads + * @ret rc Return status code + * + * Reads and parses ISO9660 parameters, if present. + */ +static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) { + static const struct iso9660_primary_descriptor_fixed primary_check = { + .type = ISO9660_TYPE_PRIMARY, + .id = ISO9660_ID, + }; + struct iso9660_primary_descriptor *primary = scratch; + static const struct eltorito_descriptor_fixed boot_check = { + .type = ISO9660_TYPE_BOOT, + .id = ISO9660_ID, + .version = 1, + .system_id = "EL TORITO SPECIFICATION", + }; + struct eltorito_descriptor *boot = scratch; + unsigned int blksize; + unsigned int blksize_shift; + int rc; + + /* Calculate required blocksize shift */ + blksize = int13_blksize ( int13 ); + blksize_shift = 0; + while ( blksize < ISO9660_BLKSIZE ) { + blksize <<= 1; + blksize_shift++; + } + if ( blksize > ISO9660_BLKSIZE ) { + /* Do nothing if the blksize is invalid for CD-ROM access */ + return 0; + } + + /* Read primary volume descriptor */ + if ( ( rc = int13_rw ( int13, + ( ISO9660_PRIMARY_LBA << blksize_shift ), 1, + virt_to_user ( primary ), block_read ) ) != 0 ){ + DBGC ( int13, "INT13 drive %02x could not read ISO9660 " + "primary volume descriptor: %s\n", + int13->drive, strerror ( rc ) ); + return rc; + } + + /* Do nothing unless this is an ISO image */ + if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) != 0 ) + return 0; + DBGC ( int13, "INT13 drive %02x contains an ISO9660 filesystem; " + "treating as CD-ROM\n", int13->drive ); + int13->is_cdrom = 1; + + /* Read boot record volume descriptor */ + if ( ( rc = int13_rw ( int13, + ( ELTORITO_LBA << blksize_shift ), 1, + virt_to_user ( boot ), block_read ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x could not read El Torito boot " + "record volume descriptor: %s\n", + int13->drive, strerror ( rc ) ); + return rc; + } + + /* Check for an El Torito boot catalog */ + if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) { + int13->boot_catalog = boot->sector; + DBGC ( int13, "INT13 drive %02x has an El Torito boot catalog " + "at LBA %08x\n", int13->drive, int13->boot_catalog ); + } else { + DBGC ( int13, "INT13 drive %02x has no El Torito boot " + "catalog\n", int13->drive ); + } + + /* Configure drive for no-emulation CD-ROM access */ + int13->blksize_shift += blksize_shift; + + return 0; +} + +/** + * Guess INT 13 hard disk drive geometry + * + * @v int13 Emulated drive + * @v scratch Scratch area for single-sector reads + * @ret heads Guessed number of heads + * @ret sectors Guessed number of sectors per track + * @ret rc Return status code + * + * Guesses the drive geometry by inspecting the partition table. + */ +static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch, + unsigned int *heads, + unsigned int *sectors ) { + struct master_boot_record *mbr = scratch; + struct partition_table_entry *partition; + unsigned int i; + int rc; + + /* Default guess is xx/255/63 */ + *heads = 255; + *sectors = 63; + + /* Read partition table */ + if ( ( rc = int13_rw ( int13, 0, 1, virt_to_user ( mbr ), + block_read ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x could not read " + "partition table to guess geometry: %s\n", + int13->drive, strerror ( rc ) ); + return rc; + } + DBGC2 ( int13, "INT13 drive %02x has MBR:\n", int13->drive ); + DBGC2_HDA ( int13, 0, mbr, sizeof ( *mbr ) ); + DBGC ( int13, "INT13 drive %02x has signature %08x\n", + int13->drive, mbr->signature ); + + /* Scan through partition table and modify guesses for + * heads and sectors_per_track if we find any used + * partitions. + */ + for ( i = 0 ; i < 4 ; i++ ) { + partition = &mbr->partitions[i]; + if ( ! partition->type ) + continue; + *heads = ( PART_HEAD ( partition->chs_end ) + 1 ); + *sectors = PART_SECTOR ( partition->chs_end ); + DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based " + "on partition %d\n", + int13->drive, *heads, *sectors, ( i + 1 ) ); + } + + return 0; +} + +/** Recognised floppy disk geometries */ +static const struct int13_fdd_geometry int13_fdd_geometries[] = { + INT13_FDD_GEOMETRY ( 40, 1, 8 ), + INT13_FDD_GEOMETRY ( 40, 1, 9 ), + INT13_FDD_GEOMETRY ( 40, 2, 8 ), + INT13_FDD_GEOMETRY ( 40, 1, 9 ), + INT13_FDD_GEOMETRY ( 80, 2, 8 ), + INT13_FDD_GEOMETRY ( 80, 2, 9 ), + INT13_FDD_GEOMETRY ( 80, 2, 15 ), + INT13_FDD_GEOMETRY ( 80, 2, 18 ), + INT13_FDD_GEOMETRY ( 80, 2, 20 ), + INT13_FDD_GEOMETRY ( 80, 2, 21 ), + INT13_FDD_GEOMETRY ( 82, 2, 21 ), + INT13_FDD_GEOMETRY ( 83, 2, 21 ), + INT13_FDD_GEOMETRY ( 80, 2, 22 ), + INT13_FDD_GEOMETRY ( 80, 2, 23 ), + INT13_FDD_GEOMETRY ( 80, 2, 24 ), + INT13_FDD_GEOMETRY ( 80, 2, 36 ), + INT13_FDD_GEOMETRY ( 80, 2, 39 ), + INT13_FDD_GEOMETRY ( 80, 2, 40 ), + INT13_FDD_GEOMETRY ( 80, 2, 44 ), + INT13_FDD_GEOMETRY ( 80, 2, 48 ), +}; + +/** + * Guess INT 13 floppy disk drive geometry + * + * @v int13 Emulated drive + * @ret heads Guessed number of heads + * @ret sectors Guessed number of sectors per track + * @ret rc Return status code + * + * Guesses the drive geometry by inspecting the disk size. + */ +static int int13_guess_geometry_fdd ( struct int13_drive *int13, + unsigned int *heads, + unsigned int *sectors ) { + unsigned int blocks = int13_capacity ( int13 ); + const struct int13_fdd_geometry *geometry; + unsigned int cylinders; + unsigned int i; + + /* Look for a match against a known geometry */ + for ( i = 0 ; i < ( sizeof ( int13_fdd_geometries ) / + sizeof ( int13_fdd_geometries[0] ) ) ; i++ ) { + geometry = &int13_fdd_geometries[i]; + cylinders = INT13_FDD_CYLINDERS ( geometry ); + *heads = INT13_FDD_HEADS ( geometry ); + *sectors = INT13_FDD_SECTORS ( geometry ); + if ( ( cylinders * (*heads) * (*sectors) ) == blocks ) { + DBGC ( int13, "INT13 drive %02x guessing C/H/S " + "%d/%d/%d based on size %dK\n", int13->drive, + cylinders, *heads, *sectors, ( blocks / 2 ) ); + return 0; + } + } + + /* Otherwise, assume a partial disk image in the most common + * format (1440K, 80/2/18). + */ + *heads = 2; + *sectors = 18; + DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size " + "%dK\n", int13->drive, *heads, *sectors, ( blocks / 2 ) ); + return 0; +} + +/** + * Guess INT 13 drive geometry + * + * @v int13 Emulated drive + * @v scratch Scratch area for single-sector reads + * @ret rc Return status code + */ +static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) { + unsigned int guessed_heads; + unsigned int guessed_sectors; + unsigned int blocks; + unsigned int blocks_per_cyl; + int rc; + + /* Don't even try when the blksize is invalid for C/H/S access */ + if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) + return 0; + + /* Guess geometry according to drive type */ + if ( int13_is_fdd ( int13 ) ) { + if ( ( rc = int13_guess_geometry_fdd ( int13, &guessed_heads, + &guessed_sectors )) != 0) + return rc; + } else { + if ( ( rc = int13_guess_geometry_hdd ( int13, scratch, + &guessed_heads, + &guessed_sectors )) != 0) + return rc; + } + + /* Apply guesses if no geometry already specified */ + if ( ! int13->heads ) + int13->heads = guessed_heads; + if ( ! int13->sectors_per_track ) + int13->sectors_per_track = guessed_sectors; + if ( ! int13->cylinders ) { + /* Avoid attempting a 64-bit divide on a 32-bit system */ + blocks = int13_capacity32 ( int13 ); + blocks_per_cyl = ( int13->heads * int13->sectors_per_track ); + assert ( blocks_per_cyl != 0 ); + int13->cylinders = ( blocks / blocks_per_cyl ); + if ( int13->cylinders > 1024 ) + int13->cylinders = 1024; + } + + return 0; +} + +/** + * Update BIOS drive count + */ +static void int13_sync_num_drives ( void ) { + struct int13_drive *int13; + uint8_t *counter; + uint8_t max_drive; + uint8_t required; + + /* Get current drive counts */ + get_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD ); + get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES ); + num_fdds = ( ( equipment_word & 0x0001 ) ? + ( ( ( equipment_word >> 6 ) & 0x3 ) + 1 ) : 0 ); + + /* Ensure count is large enough to cover all of our emulated drives */ + list_for_each_entry ( int13, &int13s, list ) { + counter = ( int13_is_fdd ( int13 ) ? &num_fdds : &num_drives ); + max_drive = int13->drive; + if ( max_drive < int13->natural_drive ) + max_drive = int13->natural_drive; + required = ( ( max_drive & 0x7f ) + 1 ); + if ( *counter < required ) { + *counter = required; + DBGC ( int13, "INT13 drive %02x added to drive count: " + "%d HDDs, %d FDDs\n", + int13->drive, num_drives, num_fdds ); + } + } + + /* Update current drive count */ + equipment_word &= ~( ( 0x3 << 6 ) | 0x0001 ); + if ( num_fdds ) { + equipment_word |= ( 0x0001 | + ( ( ( num_fdds - 1 ) & 0x3 ) << 6 ) ); + } + put_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD ); + put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES ); +} + +/** + * Check number of drives + */ +static void int13_check_num_drives ( void ) { + uint16_t check_equipment_word; + uint8_t check_num_drives; + + get_real ( check_equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD ); + get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES ); + if ( ( check_equipment_word != equipment_word ) || + ( check_num_drives != num_drives ) ) { + int13_sync_num_drives(); + } +} + +/** + * INT 13, 00 - Reset disk system + * + * @v int13 Emulated drive + * @ret status Status code + */ +static int int13_reset ( struct int13_drive *int13, + struct i386_all_regs *ix86 __unused ) { + int rc; + + DBGC2 ( int13, "Reset drive\n" ); + + /* Reopen underlying block device */ + if ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) + return -INT13_STATUS_RESET_FAILED; + + /* Check that block device is functional */ + if ( ( rc = int13_read_capacity ( int13 ) ) != 0 ) + return -INT13_STATUS_RESET_FAILED; + + return 0; +} + +/** + * INT 13, 01 - Get status of last operation + * + * @v int13 Emulated drive + * @ret status Status code + */ +static int int13_get_last_status ( struct int13_drive *int13, + struct i386_all_regs *ix86 __unused ) { + DBGC2 ( int13, "Get status of last operation\n" ); + return int13->last_status; +} + +/** + * Read / write sectors + * + * @v int13 Emulated drive + * @v al Number of sectors to read or write (must be nonzero) + * @v ch Low bits of cylinder number + * @v cl (bits 7:6) High bits of cylinder number + * @v cl (bits 5:0) Sector number + * @v dh Head number + * @v es:bx Data buffer + * @v block_rw Block read/write method + * @ret status Status code + * @ret al Number of sectors read or written + */ +static int int13_rw_sectors ( struct int13_drive *int13, + struct i386_all_regs *ix86, + int ( * block_rw ) ( struct interface *control, + struct interface *data, + uint64_t lba, + unsigned int count, + userptr_t buffer, + size_t len ) ) { + unsigned int cylinder, head, sector; + unsigned long lba; + unsigned int count; + userptr_t buffer; + int rc; + + /* Validate blocksize */ + if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) { + DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) " + "for non-extended read/write\n", + int13->drive, int13_blksize ( int13 ) ); + return -INT13_STATUS_INVALID; + } + + /* Calculate parameters */ + cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch ); + head = ix86->regs.dh; + sector = ( ix86->regs.cl & 0x3f ); + if ( ( cylinder >= int13->cylinders ) || + ( head >= int13->heads ) || + ( sector < 1 ) || ( sector > int13->sectors_per_track ) ) { + DBGC ( int13, "C/H/S %d/%d/%d out of range for geometry " + "%d/%d/%d\n", cylinder, head, sector, int13->cylinders, + int13->heads, int13->sectors_per_track ); + return -INT13_STATUS_INVALID; + } + lba = ( ( ( ( cylinder * int13->heads ) + head ) + * int13->sectors_per_track ) + sector - 1 ); + count = ix86->regs.al; + buffer = real_to_user ( ix86->segs.es, ix86->regs.bx ); + + DBGC2 ( int13, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n", + cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx, + count ); + + /* Read from / write to block device */ + if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x I/O failed: %s\n", + int13->drive, strerror ( rc ) ); + return -INT13_STATUS_READ_ERROR; + } + + return 0; +} + +/** + * INT 13, 02 - Read sectors + * + * @v int13 Emulated drive + * @v al Number of sectors to read (must be nonzero) + * @v ch Low bits of cylinder number + * @v cl (bits 7:6) High bits of cylinder number + * @v cl (bits 5:0) Sector number + * @v dh Head number + * @v es:bx Data buffer + * @ret status Status code + * @ret al Number of sectors read + */ +static int int13_read_sectors ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + DBGC2 ( int13, "Read: " ); + return int13_rw_sectors ( int13, ix86, block_read ); +} + +/** + * INT 13, 03 - Write sectors + * + * @v int13 Emulated drive + * @v al Number of sectors to write (must be nonzero) + * @v ch Low bits of cylinder number + * @v cl (bits 7:6) High bits of cylinder number + * @v cl (bits 5:0) Sector number + * @v dh Head number + * @v es:bx Data buffer + * @ret status Status code + * @ret al Number of sectors written + */ +static int int13_write_sectors ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + DBGC2 ( int13, "Write: " ); + return int13_rw_sectors ( int13, ix86, block_write ); +} + +/** + * INT 13, 08 - Get drive parameters + * + * @v int13 Emulated drive + * @ret status Status code + * @ret ch Low bits of maximum cylinder number + * @ret cl (bits 7:6) High bits of maximum cylinder number + * @ret cl (bits 5:0) Maximum sector number + * @ret dh Maximum head number + * @ret dl Number of drives + */ +static int int13_get_parameters ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + unsigned int max_cylinder = int13->cylinders - 1; + unsigned int max_head = int13->heads - 1; + unsigned int max_sector = int13->sectors_per_track; /* sic */ + + DBGC2 ( int13, "Get drive parameters\n" ); + + /* Validate blocksize */ + if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) { + DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) " + "for non-extended parameters\n", + int13->drive, int13_blksize ( int13 ) ); + return -INT13_STATUS_INVALID; + } + + /* Common parameters */ + ix86->regs.ch = ( max_cylinder & 0xff ); + ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector ); + ix86->regs.dh = max_head; + ix86->regs.dl = ( int13_is_fdd ( int13 ) ? num_fdds : num_drives ); + + /* Floppy-specific parameters */ + if ( int13_is_fdd ( int13 ) ) { + ix86->regs.bl = INT13_FDD_TYPE_1M44; + ix86->segs.es = rm_ds; + ix86->regs.di = __from_data16 ( &int13_fdd_params ); + } + + return 0; +} + +/** + * INT 13, 15 - Get disk type + * + * @v int13 Emulated drive + * @ret ah Type code + * @ret cx:dx Sector count + * @ret status Status code / disk type + */ +static int int13_get_disk_type ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + uint32_t blocks; + + DBGC2 ( int13, "Get disk type\n" ); + + if ( int13_is_fdd ( int13 ) ) { + return INT13_DISK_TYPE_FDD; + } else { + blocks = int13_capacity32 ( int13 ); + ix86->regs.cx = ( blocks >> 16 ); + ix86->regs.dx = ( blocks & 0xffff ); + return INT13_DISK_TYPE_HDD; + } +} + +/** + * INT 13, 41 - Extensions installation check + * + * @v int13 Emulated drive + * @v bx 0x55aa + * @ret bx 0xaa55 + * @ret cx Extensions API support bitmap + * @ret status Status code / API version + */ +static int int13_extension_check ( struct int13_drive *int13 __unused, + struct i386_all_regs *ix86 ) { + if ( ix86->regs.bx == 0x55aa ) { + DBGC2 ( int13, "INT13 extensions installation check\n" ); + ix86->regs.bx = 0xaa55; + ix86->regs.cx = ( INT13_EXTENSION_LINEAR | + INT13_EXTENSION_EDD | + INT13_EXTENSION_64BIT ); + return INT13_EXTENSION_VER_3_0; + } else { + return -INT13_STATUS_INVALID; + } +} + +/** + * Extended read / write + * + * @v int13 Emulated drive + * @v ds:si Disk address packet + * @v block_rw Block read/write method + * @ret status Status code + */ +static int int13_extended_rw ( struct int13_drive *int13, + struct i386_all_regs *ix86, + int ( * block_rw ) ( struct interface *control, + struct interface *data, + uint64_t lba, + unsigned int count, + userptr_t buffer, + size_t len ) ) { + struct int13_disk_address addr; + uint8_t bufsize; + uint64_t lba; + unsigned long count; + userptr_t buffer; + int rc; + + /* Extended reads are not allowed on floppy drives. + * ELTORITO.SYS seems to assume that we are really a CD-ROM if + * we support extended reads for a floppy drive. + */ + if ( int13_is_fdd ( int13 ) ) + return -INT13_STATUS_INVALID; + + /* Get buffer size */ + get_real ( bufsize, ix86->segs.ds, + ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) ); + if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) { + DBGC2 ( int13, "\n", bufsize ); + return -INT13_STATUS_INVALID; + } + + /* Read parameters from disk address structure */ + memset ( &addr, 0, sizeof ( addr ) ); + copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize ); + lba = addr.lba; + DBGC2 ( int13, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) ); + if ( ( addr.count == 0xff ) || + ( ( addr.buffer.segment == 0xffff ) && + ( addr.buffer.offset == 0xffff ) ) ) { + buffer = phys_to_user ( addr.buffer_phys ); + DBGC2 ( int13, "%08llx", + ( ( unsigned long long ) addr.buffer_phys ) ); + } else { + buffer = real_to_user ( addr.buffer.segment, + addr.buffer.offset ); + DBGC2 ( int13, "%04x:%04x", addr.buffer.segment, + addr.buffer.offset ); + } + if ( addr.count <= 0x7f ) { + count = addr.count; + } else if ( addr.count == 0xff ) { + count = addr.long_count; + } else { + DBGC2 ( int13, " \n", addr.count ); + return -INT13_STATUS_INVALID; + } + DBGC2 ( int13, " (count %ld)\n", count ); + + /* Read from / write to block device */ + if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x extended I/O failed: %s\n", + int13->drive, strerror ( rc ) ); + /* Record that no blocks were transferred successfully */ + addr.count = 0; + put_real ( addr.count, ix86->segs.ds, + ( ix86->regs.si + + offsetof ( typeof ( addr ), count ) ) ); + return -INT13_STATUS_READ_ERROR; + } + + return 0; +} + +/** + * INT 13, 42 - Extended read + * + * @v int13 Emulated drive + * @v ds:si Disk address packet + * @ret status Status code + */ +static int int13_extended_read ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + DBGC2 ( int13, "Extended read: " ); + return int13_extended_rw ( int13, ix86, block_read ); +} + +/** + * INT 13, 43 - Extended write + * + * @v int13 Emulated drive + * @v ds:si Disk address packet + * @ret status Status code + */ +static int int13_extended_write ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + DBGC2 ( int13, "Extended write: " ); + return int13_extended_rw ( int13, ix86, block_write ); +} + +/** + * INT 13, 44 - Verify sectors + * + * @v int13 Emulated drive + * @v ds:si Disk address packet + * @ret status Status code + */ +static int int13_extended_verify ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + struct int13_disk_address addr; + uint64_t lba; + unsigned long count; + + /* Read parameters from disk address structure */ + if ( DBG_EXTRA ) { + copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, + sizeof ( addr )); + lba = addr.lba; + count = addr.count; + DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n", + ( ( unsigned long long ) lba ), count ); + } + + /* We have no mechanism for verifying sectors */ + return -INT13_STATUS_INVALID; +} + +/** + * INT 13, 44 - Extended seek + * + * @v int13 Emulated drive + * @v ds:si Disk address packet + * @ret status Status code + */ +static int int13_extended_seek ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + struct int13_disk_address addr; + uint64_t lba; + unsigned long count; + + /* Read parameters from disk address structure */ + if ( DBG_EXTRA ) { + copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, + sizeof ( addr )); + lba = addr.lba; + count = addr.count; + DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n", + ( ( unsigned long long ) lba ), count ); + } + + /* Ignore and return success */ + return 0; +} + +/** + * Build device path information + * + * @v int13 Emulated drive + * @v dpi Device path information + * @ret rc Return status code + */ +static int int13_device_path_info ( struct int13_drive *int13, + struct edd_device_path_information *dpi ) { + struct device *device; + struct device_description *desc; + unsigned int i; + uint8_t sum = 0; + int rc; + + /* Reopen block device if necessary */ + if ( ( int13->block_rc != 0 ) && + ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) ) + return rc; + + /* Get underlying hardware device */ + device = identify_device ( &int13->block ); + if ( ! device ) { + DBGC ( int13, "INT13 drive %02x cannot identify hardware " + "device\n", int13->drive ); + return -ENODEV; + } + + /* Fill in bus type and interface path */ + desc = &device->desc; + switch ( desc->bus_type ) { + case BUS_TYPE_PCI: + dpi->host_bus_type.type = EDD_BUS_TYPE_PCI; + dpi->interface_path.pci.bus = PCI_BUS ( desc->location ); + dpi->interface_path.pci.slot = PCI_SLOT ( desc->location ); + dpi->interface_path.pci.function = PCI_FUNC ( desc->location ); + dpi->interface_path.pci.channel = 0xff; /* unused */ + break; + default: + DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n", + int13->drive, desc->bus_type ); + return -ENOTSUP; + } + + /* Get EDD block device description */ + if ( ( rc = edd_describe ( &int13->block, &dpi->interface_type, + &dpi->device_path ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x cannot identify block device: " + "%s\n", int13->drive, strerror ( rc ) ); + return rc; + } + + /* Fill in common fields and fix checksum */ + dpi->key = EDD_DEVICE_PATH_INFO_KEY; + dpi->len = sizeof ( *dpi ); + for ( i = 0 ; i < sizeof ( *dpi ) ; i++ ) + sum += *( ( ( uint8_t * ) dpi ) + i ); + dpi->checksum -= sum; + + return 0; +} + +/** + * INT 13, 48 - Get extended parameters + * + * @v int13 Emulated drive + * @v ds:si Drive parameter table + * @ret status Status code + */ +static int int13_get_extended_parameters ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + struct int13_disk_parameters params; + struct segoff address; + size_t len = sizeof ( params ); + uint16_t bufsize; + int rc; + + /* Get buffer size */ + get_real ( bufsize, ix86->segs.ds, + ( ix86->regs.si + offsetof ( typeof ( params ), bufsize ))); + + DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n", + ix86->segs.ds, ix86->regs.si, bufsize ); + + /* Build drive parameters */ + memset ( ¶ms, 0, sizeof ( params ) ); + params.flags = INT13_FL_DMA_TRANSPARENT; + if ( ( int13->cylinders < 1024 ) && + ( int13_capacity ( int13 ) <= INT13_MAX_CHS_SECTORS ) ) { + params.flags |= INT13_FL_CHS_VALID; + } + params.cylinders = int13->cylinders; + params.heads = int13->heads; + params.sectors_per_track = int13->sectors_per_track; + params.sectors = int13_capacity ( int13 ); + params.sector_size = int13_blksize ( int13 ); + memset ( ¶ms.dpte, 0xff, sizeof ( params.dpte ) ); + if ( ( rc = int13_device_path_info ( int13, ¶ms.dpi ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x could not provide device " + "path information: %s\n", + int13->drive, strerror ( rc ) ); + len = offsetof ( typeof ( params ), dpi ); + } + + /* Calculate returned "buffer size" (which will be less than + * the length actually copied if device path information is + * present). + */ + if ( bufsize < offsetof ( typeof ( params ), dpte ) ) + return -INT13_STATUS_INVALID; + if ( bufsize < offsetof ( typeof ( params ), dpi ) ) { + params.bufsize = offsetof ( typeof ( params ), dpte ); + } else { + params.bufsize = offsetof ( typeof ( params ), dpi ); + } + + DBGC ( int13, "INT 13 drive %02x described using extended " + "parameters:\n", int13->drive ); + address.segment = ix86->segs.ds; + address.offset = ix86->regs.si; + DBGC_HDA ( int13, address, ¶ms, len ); + + /* Return drive parameters */ + if ( len > bufsize ) + len = bufsize; + copy_to_real ( ix86->segs.ds, ix86->regs.si, ¶ms, len ); + + return 0; +} + +/** + * INT 13, 4b - Get status or terminate CD-ROM emulation + * + * @v int13 Emulated drive + * @v ds:si Specification packet + * @ret status Status code + */ +static int int13_cdrom_status_terminate ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + struct int13_cdrom_specification specification; + + DBGC2 ( int13, "Get CD-ROM emulation status to %04x:%04x%s\n", + ix86->segs.ds, ix86->regs.si, + ( ix86->regs.al ? "" : " and terminate" ) ); + + /* Fail if we are not a CD-ROM */ + if ( ! int13->is_cdrom ) { + DBGC ( int13, "INT13 drive %02x is not a CD-ROM\n", + int13->drive ); + return -INT13_STATUS_INVALID; + } + + /* Build specification packet */ + memset ( &specification, 0, sizeof ( specification ) ); + specification.size = sizeof ( specification ); + specification.drive = int13->drive; + + /* Return specification packet */ + copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification, + sizeof ( specification ) ); + + return 0; +} + + +/** + * INT 13, 4d - Read CD-ROM boot catalog + * + * @v int13 Emulated drive + * @v ds:si Command packet + * @ret status Status code + */ +static int int13_cdrom_read_boot_catalog ( struct int13_drive *int13, + struct i386_all_regs *ix86 ) { + struct int13_cdrom_boot_catalog_command command; + int rc; + + /* Read parameters from command packet */ + copy_from_real ( &command, ix86->segs.ds, ix86->regs.si, + sizeof ( command ) ); + DBGC2 ( int13, "Read CD-ROM boot catalog to %08x\n", command.buffer ); + + /* Fail if we have no boot catalog */ + if ( ! int13->boot_catalog ) { + DBGC ( int13, "INT13 drive %02x has no boot catalog\n", + int13->drive ); + return -INT13_STATUS_INVALID; + } + + /* Read from boot catalog */ + if ( ( rc = int13_rw ( int13, ( int13->boot_catalog + command.start ), + command.count, phys_to_user ( command.buffer ), + block_read ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x could not read boot catalog: " + "%s\n", int13->drive, strerror ( rc ) ); + return -INT13_STATUS_READ_ERROR; + } + + return 0; +} + +/** + * INT 13 handler + * + */ +static __asmcall void int13 ( struct i386_all_regs *ix86 ) { + int command = ix86->regs.ah; + unsigned int bios_drive = ix86->regs.dl; + struct int13_drive *int13; + int status; + + /* Check BIOS hasn't killed off our drive */ + int13_check_num_drives(); + + list_for_each_entry ( int13, &int13s, list ) { + + if ( bios_drive != int13->drive ) { + /* Remap any accesses to this drive's natural number */ + if ( bios_drive == int13->natural_drive ) { + DBGC2 ( int13, "INT13,%02x (%02x) remapped to " + "(%02x)\n", ix86->regs.ah, + bios_drive, int13->drive ); + ix86->regs.dl = int13->drive; + return; + } else if ( ( ( bios_drive & 0x7f ) == 0x7f ) && + ( command == INT13_CDROM_STATUS_TERMINATE ) + && int13->is_cdrom ) { + /* Catch non-drive-specific CD-ROM calls */ + } else { + continue; + } + } + + DBGC2 ( int13, "INT13,%02x (%02x): ", + ix86->regs.ah, bios_drive ); + + switch ( command ) { + case INT13_RESET: + status = int13_reset ( int13, ix86 ); + break; + case INT13_GET_LAST_STATUS: + status = int13_get_last_status ( int13, ix86 ); + break; + case INT13_READ_SECTORS: + status = int13_read_sectors ( int13, ix86 ); + break; + case INT13_WRITE_SECTORS: + status = int13_write_sectors ( int13, ix86 ); + break; + case INT13_GET_PARAMETERS: + status = int13_get_parameters ( int13, ix86 ); + break; + case INT13_GET_DISK_TYPE: + status = int13_get_disk_type ( int13, ix86 ); + break; + case INT13_EXTENSION_CHECK: + status = int13_extension_check ( int13, ix86 ); + break; + case INT13_EXTENDED_READ: + status = int13_extended_read ( int13, ix86 ); + break; + case INT13_EXTENDED_WRITE: + status = int13_extended_write ( int13, ix86 ); + break; + case INT13_EXTENDED_VERIFY: + status = int13_extended_verify ( int13, ix86 ); + break; + case INT13_EXTENDED_SEEK: + status = int13_extended_seek ( int13, ix86 ); + break; + case INT13_GET_EXTENDED_PARAMETERS: + status = int13_get_extended_parameters ( int13, ix86 ); + break; + case INT13_CDROM_STATUS_TERMINATE: + status = int13_cdrom_status_terminate ( int13, ix86 ); + break; + case INT13_CDROM_READ_BOOT_CATALOG: + status = int13_cdrom_read_boot_catalog ( int13, ix86 ); + break; + default: + DBGC2 ( int13, "*** Unrecognised INT13 ***\n" ); + status = -INT13_STATUS_INVALID; + break; + } + + /* Store status for INT 13,01 */ + int13->last_status = status; + + /* Negative status indicates an error */ + if ( status < 0 ) { + status = -status; + DBGC ( int13, "INT13,%02x (%02x) failed with status " + "%02x\n", ix86->regs.ah, int13->drive, status ); + } else { + ix86->flags &= ~CF; + } + ix86->regs.ah = status; + + /* Set OF to indicate to wrapper not to chain this call */ + ix86->flags |= OF; + + return; + } +} + +/** + * Hook INT 13 handler + * + */ +static void int13_hook_vector ( void ) { + /* Assembly wrapper to call int13(). int13() sets OF if we + * should not chain to the previous handler. (The wrapper + * clears CF and OF before calling int13()). + */ + __asm__ __volatile__ ( + TEXT16_CODE ( "\nint13_wrapper:\n\t" + /* Preserve %ax and %dx for future reference */ + "pushw %%bp\n\t" + "movw %%sp, %%bp\n\t" + "pushw %%ax\n\t" + "pushw %%dx\n\t" + /* Clear OF, set CF, call int13() */ + "orb $0, %%al\n\t" + "stc\n\t" + "pushl %0\n\t" + "pushw %%cs\n\t" + "call prot_call\n\t" + /* Chain if OF not set */ + "jo 1f\n\t" + "pushfw\n\t" + "lcall *%%cs:int13_vector\n\t" + "\n1:\n\t" + /* Overwrite flags for iret */ + "pushfw\n\t" + "popw 6(%%bp)\n\t" + /* Fix up %dl: + * + * INT 13,15 : do nothing if hard disk + * INT 13,08 : load with number of drives + * all others: restore original value + */ + "cmpb $0x15, -1(%%bp)\n\t" + "jne 2f\n\t" + "testb $0x80, -4(%%bp)\n\t" + "jnz 3f\n\t" + "\n2:\n\t" + "movb -4(%%bp), %%dl\n\t" + "cmpb $0x08, -1(%%bp)\n\t" + "jne 3f\n\t" + "testb $0x80, %%dl\n\t" + "movb %%cs:num_drives, %%dl\n\t" + "jnz 3f\n\t" + "movb %%cs:num_fdds, %%dl\n\t" + /* Return */ + "\n3:\n\t" + "movw %%bp, %%sp\n\t" + "popw %%bp\n\t" + "iret\n\t" ) + : : "i" ( int13 ) ); + + hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper, + &int13_vector ); +} + +/** + * Unhook INT 13 handler + */ +static void int13_unhook_vector ( void ) { + unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper, + &int13_vector ); +} + +/** + * Check INT13 emulated drive flow control window + * + * @v int13 Emulated drive + */ +static size_t int13_block_window ( struct int13_drive *int13 __unused ) { + + /* We are never ready to receive data via this interface. + * This prevents objects that support both block and stream + * interfaces from attempting to send us stream data. + */ + return 0; +} + +/** + * Handle INT 13 emulated drive underlying block device closing + * + * @v int13 Emulated drive + * @v rc Reason for close + */ +static void int13_block_close ( struct int13_drive *int13, int rc ) { + + /* Any closing is an error from our point of view */ + if ( rc == 0 ) + rc = -ENOTCONN; + + DBGC ( int13, "INT13 drive %02x went away: %s\n", + int13->drive, strerror ( rc ) ); + + /* Record block device error code */ + int13->block_rc = rc; + + /* Shut down interfaces */ + intf_restart ( &int13->block, rc ); +} + +/** INT 13 drive interface operations */ +static struct interface_operation int13_block_op[] = { + INTF_OP ( xfer_window, struct int13_drive *, int13_block_window ), + INTF_OP ( intf_close, struct int13_drive *, int13_block_close ), +}; + +/** INT 13 drive interface descriptor */ +static struct interface_descriptor int13_block_desc = + INTF_DESC ( struct int13_drive, block, int13_block_op ); + +/** + * Free INT 13 emulated drive + * + * @v refcnt Reference count + */ +static void int13_free ( struct refcnt *refcnt ) { + struct int13_drive *int13 = + container_of ( refcnt, struct int13_drive, refcnt ); + + uri_put ( int13->uri ); + free ( int13 ); +} + +/** + * Hook INT 13 emulated drive + * + * @v uri URI + * @v drive Drive number + * @ret rc Return status code + * + * Registers the drive with the INT 13 emulation subsystem, and hooks + * the INT 13 interrupt vector (if not already hooked). + */ +static int int13_hook ( struct uri *uri, unsigned int drive ) { + struct int13_drive *int13; + unsigned int natural_drive; + void *scratch; + int rc; + + /* Calculate natural drive number */ + int13_sync_num_drives(); + natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds ); + + /* Check that drive number is not in use */ + list_for_each_entry ( int13, &int13s, list ) { + if ( int13->drive == drive ) { + rc = -EADDRINUSE; + goto err_in_use; + } + } + + /* Allocate and initialise structure */ + int13 = zalloc ( sizeof ( *int13 ) ); + if ( ! int13 ) { + rc = -ENOMEM; + goto err_zalloc; + } + ref_init ( &int13->refcnt, int13_free ); + intf_init ( &int13->block, &int13_block_desc, &int13->refcnt ); + int13->uri = uri_get ( uri ); + int13->drive = drive; + int13->natural_drive = natural_drive; + + /* Open block device interface */ + if ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) + goto err_reopen_block; + + /* Read device capacity */ + if ( ( rc = int13_read_capacity ( int13 ) ) != 0 ) + goto err_read_capacity; + + /* Allocate scratch area */ + scratch = malloc ( int13_blksize ( int13 ) ); + if ( ! scratch ) + goto err_alloc_scratch; + + /* Parse parameters, if present */ + if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 ) + goto err_parse_iso9660; + + /* Give drive a default geometry */ + if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 ) + goto err_guess_geometry; + + DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S " + "geometry %d/%d/%d\n", int13->drive, int13->natural_drive, + int13->cylinders, int13->heads, int13->sectors_per_track ); + + /* Hook INT 13 vector if not already hooked */ + if ( list_empty ( &int13s ) ) { + int13_hook_vector(); + devices_get(); + } + + /* Add to list of emulated drives */ + list_add ( &int13->list, &int13s ); + + /* Update BIOS drive count */ + int13_sync_num_drives(); + + free ( scratch ); + return 0; + + err_guess_geometry: + err_parse_iso9660: + free ( scratch ); + err_alloc_scratch: + err_read_capacity: + err_reopen_block: + intf_shutdown ( &int13->block, rc ); + ref_put ( &int13->refcnt ); + err_zalloc: + err_in_use: + return rc; +} + +/** + * Find INT 13 emulated drive by drive number + * + * @v drive Drive number + * @ret int13 Emulated drive, or NULL + */ +static struct int13_drive * int13_find ( unsigned int drive ) { + struct int13_drive *int13; + + list_for_each_entry ( int13, &int13s, list ) { + if ( int13->drive == drive ) + return int13; + } + return NULL; +} + +/** + * Unhook INT 13 emulated drive + * + * @v drive Drive number + * + * Unregisters the drive from the INT 13 emulation subsystem. If this + * is the last emulated drive, the INT 13 vector is unhooked (if + * possible). + */ +static void int13_unhook ( unsigned int drive ) { + struct int13_drive *int13; + + /* Find drive */ + int13 = int13_find ( drive ); + if ( ! int13 ) { + DBG ( "INT13 cannot find emulated drive %02x\n", drive ); + return; + } + + /* Shut down interfaces */ + intf_shutdown ( &int13->block, 0 ); + + /* Remove from list of emulated drives */ + list_del ( &int13->list ); + + /* Should adjust BIOS drive count, but it's difficult + * to do so reliably. + */ + + DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive ); + + /* Unhook INT 13 vector if no more drives */ + if ( list_empty ( &int13s ) ) { + devices_put(); + int13_unhook_vector(); + } + + /* Drop list's reference to drive */ + ref_put ( &int13->refcnt ); +} + +/** + * Load and verify master boot record from INT 13 drive + * + * @v drive Drive number + * @v address Boot code address to fill in + * @ret rc Return status code + */ +static int int13_load_mbr ( unsigned int drive, struct segoff *address ) { + uint8_t status; + int discard_b, discard_c, discard_d; + uint16_t magic; + + /* Use INT 13, 02 to read the MBR */ + address->segment = 0; + address->offset = 0x7c00; + __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t" + "pushl %%ebx\n\t" + "popw %%bx\n\t" + "popw %%es\n\t" + "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" + "popw %%es\n\t" ) + : "=a" ( status ), "=b" ( discard_b ), + "=c" ( discard_c ), "=d" ( discard_d ) + : "a" ( 0x0201 ), "b" ( *address ), + "c" ( 1 ), "d" ( drive ) ); + if ( status ) { + DBG ( "INT13 drive %02x could not read MBR (status %02x)\n", + drive, status ); + return -EIO; + } + + /* Check magic signature */ + get_real ( magic, address->segment, + ( address->offset + + offsetof ( struct master_boot_record, magic ) ) ); + if ( magic != INT13_MBR_MAGIC ) { + DBG ( "INT13 drive %02x does not contain a valid MBR\n", + drive ); + return -ENOEXEC; + } + + return 0; +} + +/** El Torito boot catalog command packet */ +static struct int13_cdrom_boot_catalog_command __data16 ( eltorito_cmd ) = { + .size = sizeof ( struct int13_cdrom_boot_catalog_command ), + .count = 1, + .buffer = 0x7c00, + .start = 0, +}; +#define eltorito_cmd __use_data16 ( eltorito_cmd ) + +/** El Torito disk address packet */ +static struct int13_disk_address __bss16 ( eltorito_address ); +#define eltorito_address __use_data16 ( eltorito_address ) + +/** + * Load and verify El Torito boot record from INT 13 drive + * + * @v drive Drive number + * @v address Boot code address to fill in + * @ret rc Return status code + */ +static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) { + struct { + struct eltorito_validation_entry valid; + struct eltorito_boot_entry boot; + } __attribute__ (( packed )) catalog; + uint8_t status; + + /* Use INT 13, 4d to read the boot catalog */ + __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" ( 0x4d00 ), "d" ( drive ), + "S" ( __from_data16 ( &eltorito_cmd ) ) ); + if ( status ) { + DBG ( "INT13 drive %02x could not read El Torito boot catalog " + "(status %02x)\n", drive, status ); + return -EIO; + } + copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0, + sizeof ( catalog ) ); + + /* Sanity checks */ + if ( catalog.valid.platform_id != ELTORITO_PLATFORM_X86 ) { + DBG ( "INT13 drive %02x El Torito specifies unknown platform " + "%02x\n", drive, catalog.valid.platform_id ); + return -ENOEXEC; + } + if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) { + DBG ( "INT13 drive %02x El Torito is not bootable\n", drive ); + return -ENOEXEC; + } + if ( catalog.boot.media_type != ELTORITO_NO_EMULATION ) { + DBG ( "INT13 drive %02x El Torito requires emulation " + "type %02x\n", drive, catalog.boot.media_type ); + return -ENOTSUP; + } + DBG ( "INT13 drive %02x El Torito boot image at LBA %08x (count %d)\n", + drive, catalog.boot.start, catalog.boot.length ); + address->segment = ( catalog.boot.load_segment ? + catalog.boot.load_segment : 0x7c0 ); + address->offset = 0; + DBG ( "INT13 drive %02x El Torito boot image loads at %04x:%04x\n", + drive, address->segment, address->offset ); + + /* Use INT 13, 42 to read the boot image */ + eltorito_address.bufsize = + offsetof ( typeof ( eltorito_address ), buffer_phys ); + eltorito_address.count = catalog.boot.length; + eltorito_address.buffer = *address; + eltorito_address.lba = catalog.boot.start; + __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" ( drive ), + "S" ( __from_data16 ( &eltorito_address ) ) ); + if ( status ) { + DBG ( "INT13 drive %02x could not read El Torito boot image " + "(status %02x)\n", drive, status ); + return -EIO; + } + + return 0; +} + +/** + * Attempt to boot from an INT 13 drive + * + * @v drive Drive number + * @ret rc Return status code + * + * This boots from the specified INT 13 drive by loading the Master + * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to + * capture an attempt by the MBR to boot the next device. (This is + * the closest thing to a return path from an MBR). + * + * Note that this function can never return success, by definition. + */ +static int int13_boot ( unsigned int drive ) { + struct memory_map memmap; + struct segoff address; + int rc; + + /* Look for a usable boot sector */ + if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) && + ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) ) + return rc; + + /* Dump out memory map prior to boot, if memmap debugging is + * enabled. Not required for program flow, but we have so + * many problems that turn out to be memory-map related that + * it's worth doing. + */ + get_memmap ( &memmap ); + + /* Jump to boot sector */ + if ( ( rc = call_bootsector ( address.segment, address.offset, + drive ) ) != 0 ) { + DBG ( "INT13 drive %02x boot returned: %s\n", + drive, strerror ( rc ) ); + return rc; + } + + return -ECANCELED; /* -EIMPOSSIBLE */ +} + +/** A boot firmware table generated by iPXE */ +union xbft_table { + /** ACPI header */ + struct acpi_description_header acpi; + /** Padding */ + char pad[768]; +}; + +/** The boot firmware table generated by iPXE */ +static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) )); +#define xbftab __use_data16 ( xbftab ) + +/** + * Describe INT 13 emulated drive for SAN-booted operating system + * + * @v drive Drive number + * @ret rc Return status code + */ +static int int13_describe ( unsigned int drive ) { + struct int13_drive *int13; + struct segoff xbft_address; + int rc; + + /* Find drive */ + int13 = int13_find ( drive ); + if ( ! int13 ) { + DBG ( "INT13 cannot find emulated drive %02x\n", drive ); + return -ENODEV; + } + + /* Reopen block device if necessary */ + if ( ( int13->block_rc != 0 ) && + ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) ) + return rc; + + /* Clear table */ + memset ( &xbftab, 0, sizeof ( xbftab ) ); + + /* Fill in common parameters */ + strncpy ( xbftab.acpi.oem_id, "FENSYS", + sizeof ( xbftab.acpi.oem_id ) ); + strncpy ( xbftab.acpi.oem_table_id, "iPXE", + sizeof ( xbftab.acpi.oem_table_id ) ); + + /* Fill in remaining parameters */ + if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi, + sizeof ( xbftab ) ) ) != 0 ) { + DBGC ( int13, "INT13 drive %02x could not create ACPI " + "description: %s\n", int13->drive, strerror ( rc ) ); + return rc; + } + + /* Fix up ACPI checksum */ + acpi_fix_checksum ( &xbftab.acpi ); + xbft_address.segment = rm_ds; + xbft_address.offset = __from_data16 ( &xbftab ); + DBGC ( int13, "INT13 drive %02x described using boot firmware " + "table:\n", int13->drive ); + DBGC_HDA ( int13, xbft_address, &xbftab, + le32_to_cpu ( xbftab.acpi.length ) ); + + return 0; +} + +PROVIDE_SANBOOT_INLINE ( pcbios, san_default_drive ); +PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook ); +PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook ); +PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot ); +PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/memtop_umalloc.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/memtop_umalloc.c new file mode 100644 index 000000000..c382e3c36 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/memtop_umalloc.c @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2007 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** + * @file + * + * External memory allocation + * + */ + +#include +#include +#include +#include +#include +#include +#include + +/** Alignment of external allocated memory */ +#define EM_ALIGN ( 4 * 1024 ) + +/** Equivalent of NOWHERE for user pointers */ +#define UNOWHERE ( ~UNULL ) + +/** An external memory block */ +struct external_memory { + /** Size of this memory block (excluding this header) */ + size_t size; + /** Block is currently in use */ + int used; +}; + +/** Top of heap */ +static userptr_t top = UNULL; + +/** Bottom of heap (current lowest allocated block) */ +static userptr_t bottom = UNULL; + +/** Remaining space on heap */ +static size_t heap_size; + +/** + * Initialise external heap + * + */ +static void init_eheap ( void ) { + userptr_t base; + + heap_size = largest_memblock ( &base ); + bottom = top = userptr_add ( base, heap_size ); + DBG ( "External heap grows downwards from %lx (size %zx)\n", + user_to_phys ( top, 0 ), heap_size ); +} + +/** + * Collect free blocks + * + */ +static void ecollect_free ( void ) { + struct external_memory extmem; + size_t len; + + /* Walk the free list and collect empty blocks */ + while ( bottom != top ) { + copy_from_user ( &extmem, bottom, -sizeof ( extmem ), + sizeof ( extmem ) ); + if ( extmem.used ) + break; + DBG ( "EXTMEM freeing [%lx,%lx)\n", user_to_phys ( bottom, 0 ), + user_to_phys ( bottom, extmem.size ) ); + len = ( extmem.size + sizeof ( extmem ) ); + bottom = userptr_add ( bottom, len ); + heap_size += len; + } +} + +/** + * Reallocate external memory + * + * @v old_ptr Memory previously allocated by umalloc(), or UNULL + * @v new_size Requested size + * @ret new_ptr Allocated memory, or UNULL + * + * Calling realloc() with a new size of zero is a valid way to free a + * memory block. + */ +static userptr_t memtop_urealloc ( userptr_t ptr, size_t new_size ) { + struct external_memory extmem; + userptr_t new = ptr; + size_t align; + + /* (Re)initialise external memory allocator if necessary */ + if ( bottom == top ) + init_eheap(); + + /* Get block properties into extmem */ + if ( ptr && ( ptr != UNOWHERE ) ) { + /* Determine old size */ + copy_from_user ( &extmem, ptr, -sizeof ( extmem ), + sizeof ( extmem ) ); + } else { + /* Create a zero-length block */ + if ( heap_size < sizeof ( extmem ) ) { + DBG ( "EXTMEM out of space\n" ); + return UNULL; + } + ptr = bottom = userptr_add ( bottom, -sizeof ( extmem ) ); + heap_size -= sizeof ( extmem ); + DBG ( "EXTMEM allocating [%lx,%lx)\n", + user_to_phys ( ptr, 0 ), user_to_phys ( ptr, 0 ) ); + extmem.size = 0; + } + extmem.used = ( new_size > 0 ); + + /* Expand/shrink block if possible */ + if ( ptr == bottom ) { + /* Update block */ + if ( new_size > ( heap_size - extmem.size ) ) { + DBG ( "EXTMEM out of space\n" ); + return UNULL; + } + new = userptr_add ( ptr, - ( new_size - extmem.size ) ); + align = ( user_to_phys ( new, 0 ) & ( EM_ALIGN - 1 ) ); + new_size += align; + new = userptr_add ( new, -align ); + DBG ( "EXTMEM expanding [%lx,%lx) to [%lx,%lx)\n", + user_to_phys ( ptr, 0 ), + user_to_phys ( ptr, extmem.size ), + user_to_phys ( new, 0 ), + user_to_phys ( new, new_size )); + memmove_user ( new, 0, ptr, 0, ( ( extmem.size < new_size ) ? + extmem.size : new_size ) ); + bottom = new; + heap_size -= ( new_size - extmem.size ); + extmem.size = new_size; + } else { + /* Cannot expand; can only pretend to shrink */ + if ( new_size > extmem.size ) { + /* Refuse to expand */ + DBG ( "EXTMEM cannot expand [%lx,%lx)\n", + user_to_phys ( ptr, 0 ), + user_to_phys ( ptr, extmem.size ) ); + return UNULL; + } + } + + /* Write back block properties */ + copy_to_user ( new, -sizeof ( extmem ), &extmem, + sizeof ( extmem ) ); + + /* Collect any free blocks and update hidden memory region */ + ecollect_free(); + hide_umalloc ( user_to_phys ( bottom, ( ( bottom == top ) ? + 0 : -sizeof ( extmem ) ) ), + user_to_phys ( top, 0 ) ); + + return ( new_size ? new : UNOWHERE ); +} + +PROVIDE_UMALLOC ( memtop, urealloc, memtop_urealloc ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/pcibios.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/pcibios.c new file mode 100644 index 000000000..61873039f --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/pcibios.c @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2006 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +#include +#include +#include + +/** @file + * + * PCI configuration space access via PCI BIOS + * + */ + +/** + * Determine number of PCI buses within system + * + * @ret num_bus Number of buses + */ +static int pcibios_num_bus ( void ) { + int discard_a, discard_D; + uint8_t max_bus; + + __asm__ __volatile__ ( REAL_CODE ( "stc\n\t" + "int $0x1a\n\t" + "jnc 1f\n\t" + "xorw %%cx, %%cx\n\t" + "\n1:\n\t" ) + : "=c" ( max_bus ), "=a" ( discard_a ), + "=D" ( discard_D ) + : "a" ( PCIBIOS_INSTALLATION_CHECK >> 16 ), + "D" ( 0 ) + : "ebx", "edx" ); + + return ( max_bus + 1 ); +} + +/** + * Read configuration space via PCI BIOS + * + * @v pci PCI device + * @v command PCI BIOS command + * @v value Value read + * @ret rc Return status code + */ +int pcibios_read ( struct pci_device *pci, uint32_t command, uint32_t *value ){ + int discard_b, discard_D; + int status; + + __asm__ __volatile__ ( REAL_CODE ( "stc\n\t" + "int $0x1a\n\t" + "jnc 1f\n\t" + "xorl %%eax, %%eax\n\t" + "decl %%eax\n\t" + "movl %%eax, %%ecx\n\t" + "\n1:\n\t" ) + : "=a" ( status ), "=b" ( discard_b ), + "=c" ( *value ), "=D" ( discard_D ) + : "a" ( command >> 16 ), "D" ( command ), + "b" ( pci->busdevfn ) + : "edx" ); + + return ( ( status >> 8 ) & 0xff ); +} + +/** + * Write configuration space via PCI BIOS + * + * @v pci PCI device + * @v command PCI BIOS command + * @v value Value to be written + * @ret rc Return status code + */ +int pcibios_write ( struct pci_device *pci, uint32_t command, uint32_t value ){ + int discard_b, discard_c, discard_D; + int status; + + __asm__ __volatile__ ( REAL_CODE ( "stc\n\t" + "int $0x1a\n\t" + "jnc 1f\n\t" + "movb $0xff, %%ah\n\t" + "\n1:\n\t" ) + : "=a" ( status ), "=b" ( discard_b ), + "=c" ( discard_c ), "=D" ( discard_D ) + : "a" ( command >> 16 ), "D" ( command ), + "b" ( pci->busdevfn ), "c" ( value ) + : "edx" ); + + return ( ( status >> 8 ) & 0xff ); +} + +PROVIDE_PCIAPI ( pcbios, pci_num_bus, pcibios_num_bus ); +PROVIDE_PCIAPI_INLINE ( pcbios, pci_read_config_byte ); +PROVIDE_PCIAPI_INLINE ( pcbios, pci_read_config_word ); +PROVIDE_PCIAPI_INLINE ( pcbios, pci_read_config_dword ); +PROVIDE_PCIAPI_INLINE ( pcbios, pci_write_config_byte ); +PROVIDE_PCIAPI_INLINE ( pcbios, pci_write_config_word ); +PROVIDE_PCIAPI_INLINE ( pcbios, pci_write_config_dword ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_entropy.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_entropy.c new file mode 100644 index 000000000..fad421c2a --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_entropy.c @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2012 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** @file + * + * RTC-based entropy source + * + */ + +#include +#include +#include +#include +#include +#include + +/** RTC "interrupt triggered" flag */ +static uint8_t __text16 ( rtc_flag ); +#define rtc_flag __use_text16 ( rtc_flag ) + +/** RTC interrupt handler */ +extern void rtc_isr ( void ); + +/** Previous RTC interrupt handler */ +static struct segoff rtc_old_handler; + +/** + * Hook RTC interrupt handler + * + */ +static void rtc_hook_isr ( void ) { + + /* RTC interrupt handler */ + __asm__ __volatile__ ( + TEXT16_CODE ( "\nrtc_isr:\n\t" + /* Preserve registers */ + "pushw %%ax\n\t" + /* Set "interrupt triggered" flag */ + "cs movb $0x01, %c0\n\t" + /* Read RTC status register C to + * acknowledge interrupt + */ + "movb %3, %%al\n\t" + "outb %%al, %1\n\t" + "inb %2\n\t" + /* Send EOI */ + "movb $0x20, %%al\n\t" + "outb %%al, $0xa0\n\t" + "outb %%al, $0x20\n\t" + /* Restore registers and return */ + "popw %%ax\n\t" + "iret\n\t" ) + : + : "p" ( __from_text16 ( &rtc_flag ) ), + "i" ( CMOS_ADDRESS ), "i" ( CMOS_DATA ), + "i" ( RTC_STATUS_C ) ); + + hook_bios_interrupt ( RTC_INT, ( unsigned int ) rtc_isr, + &rtc_old_handler ); +} + +/** + * Unhook RTC interrupt handler + * + */ +static void rtc_unhook_isr ( void ) { + int rc; + + rc = unhook_bios_interrupt ( RTC_INT, ( unsigned int ) rtc_isr, + &rtc_old_handler ); + assert ( rc == 0 ); /* Should always be able to unhook */ +} + +/** + * Enable RTC interrupts + * + */ +static void rtc_enable_int ( void ) { + uint8_t status_b; + + /* Set Periodic Interrupt Enable bit in status register B */ + outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS ); + status_b = inb ( CMOS_DATA ); + outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS ); + outb ( ( status_b | RTC_STATUS_B_PIE ), CMOS_DATA ); + + /* Re-enable NMI and reset to default address */ + outb ( CMOS_DEFAULT_ADDRESS, CMOS_ADDRESS ); + inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */ +} + +/** + * Disable RTC interrupts + * + */ +static void rtc_disable_int ( void ) { + uint8_t status_b; + + /* Clear Periodic Interrupt Enable bit in status register B */ + outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS ); + status_b = inb ( CMOS_DATA ); + outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS ); + outb ( ( status_b & ~RTC_STATUS_B_PIE ), CMOS_DATA ); + + /* Re-enable NMI and reset to default address */ + outb ( CMOS_DEFAULT_ADDRESS, CMOS_ADDRESS ); + inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */ +} + +/** + * Enable entropy gathering + * + * @ret rc Return status code + */ +static int rtc_entropy_enable ( void ) { + + rtc_hook_isr(); + enable_irq ( RTC_IRQ ); + rtc_enable_int(); + + return 0; +} + +/** + * Disable entropy gathering + * + */ +static void rtc_entropy_disable ( void ) { + + rtc_disable_int(); + disable_irq ( RTC_IRQ ); + rtc_unhook_isr(); +} + +/** + * Measure a single RTC tick + * + * @ret delta Length of RTC tick (in TSC units) + */ +uint8_t rtc_sample ( void ) { + uint32_t before; + uint32_t after; + uint32_t temp; + + __asm__ __volatile__ ( + REAL_CODE ( /* Enable interrupts */ + "sti\n\t" + /* Wait for RTC interrupt */ + "cs movb %b2, %c4\n\t" + "\n1:\n\t" + "cs xchgb %b2, %c4\n\t" /* Serialize */ + "testb %b2, %b2\n\t" + "jz 1b\n\t" + /* Read "before" TSC */ + "rdtsc\n\t" + /* Store "before" TSC on stack */ + "pushl %0\n\t" + /* Wait for another RTC interrupt */ + "xorb %b2, %b2\n\t" + "cs movb %b2, %c4\n\t" + "\n1:\n\t" + "cs xchgb %b2, %c4\n\t" /* Serialize */ + "testb %b2, %b2\n\t" + "jz 1b\n\t" + /* Read "after" TSC */ + "rdtsc\n\t" + /* Retrieve "before" TSC on stack */ + "popl %1\n\t" + /* Disable interrupts */ + "cli\n\t" + ) + : "=a" ( after ), "=d" ( before ), "=q" ( temp ) + : "2" ( 0 ), "p" ( __from_text16 ( &rtc_flag ) ) ); + + return ( after - before ); +} + +PROVIDE_ENTROPY_INLINE ( rtc, min_entropy_per_sample ); +PROVIDE_ENTROPY ( rtc, entropy_enable, rtc_entropy_enable ); +PROVIDE_ENTROPY ( rtc, entropy_disable, rtc_entropy_disable ); +PROVIDE_ENTROPY_INLINE ( rtc, get_noise ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_time.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_time.c new file mode 100644 index 000000000..67041d4ca --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/rtc_time.c @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2012 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** @file + * + * RTC-based time source + * + */ + +#include +#include +#include +#include + +/** + * Read RTC register + * + * @v address Register address + * @ret data Data + */ +static unsigned int rtc_readb ( int address ) { + outb ( address, CMOS_ADDRESS ); + return inb ( CMOS_DATA ); +} + +/** + * Check if RTC update is in progress + * + * @ret is_busy RTC update is in progress + */ +static int rtc_is_busy ( void ) { + return ( rtc_readb ( RTC_STATUS_A ) & RTC_STATUS_A_UPDATE_IN_PROGRESS ); +} + +/** + * Read RTC BCD register + * + * @v address Register address + * @ret value Value + */ +static unsigned int rtc_readb_bcd ( int address ) { + unsigned int bcd; + + bcd = rtc_readb ( address ); + return ( bcd - ( 6 * ( bcd >> 4 ) ) ); +} + +/** + * Read RTC time + * + * @ret time Time, in seconds + */ +static time_t rtc_read_time ( void ) { + unsigned int status_b; + int is_binary; + int is_24hour; + unsigned int ( * read_component ) ( int address ); + struct tm tm; + int is_pm; + unsigned int hour; + time_t time; + + /* Wait for any in-progress update to complete */ + while ( rtc_is_busy() ) {} + + /* Determine RTC mode */ + status_b = rtc_readb ( RTC_STATUS_B ); + is_binary = ( status_b & RTC_STATUS_B_BINARY ); + is_24hour = ( status_b & RTC_STATUS_B_24_HOUR ); + read_component = ( is_binary ? rtc_readb : rtc_readb_bcd ); + + /* Read time values */ + tm.tm_sec = read_component ( RTC_SEC ); + tm.tm_min = read_component ( RTC_MIN ); + hour = read_component ( RTC_HOUR ); + if ( ! is_24hour ) { + is_pm = ( hour >= 80 ); + hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) + + ( is_pm ? 12 : 0 ) ); + } + tm.tm_hour = hour; + tm.tm_mday = read_component ( RTC_MDAY ); + tm.tm_mon = ( read_component ( RTC_MON ) - 1 ); + tm.tm_year = ( read_component ( RTC_YEAR ) + + 100 /* Assume we are in the 21st century, since + * this code was written in 2012 */ ); + + DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d " + "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ), + tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, + ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) ); + + /* Convert to seconds since the Epoch */ + time = mktime ( &tm ); + + return time; +} + +/** + * Get current time in seconds + * + * @ret time Time, in seconds + */ +static time_t rtc_now ( void ) { + time_t time = 0; + time_t last_time; + + /* Read time until we get two matching values in a row, in + * case we end up reading a corrupted value in the middle of + * an update. + */ + do { + last_time = time; + time = rtc_read_time(); + } while ( time != last_time ); + + return time; +} + +PROVIDE_TIME ( rtc, time_now, rtc_now ); diff --git a/qemu/roms/ipxe/src/arch/i386/interface/pcbios/vesafb.c b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/vesafb.c new file mode 100644 index 000000000..2adc7b040 --- /dev/null +++ b/qemu/roms/ipxe/src/arch/i386/interface/pcbios/vesafb.c @@ -0,0 +1,538 @@ +/* + * Copyright (C) 2013 Michael Brown . + * + * 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. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** @file + * + * VESA frame buffer console + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Avoid dragging in BIOS console if not otherwise used */ +extern struct console_driver bios_console; +struct console_driver bios_console __attribute__ (( weak )); + +/* Disambiguate the various error causes */ +#define EIO_FAILED __einfo_error ( EINFO_EIO_FAILED ) +#define EINFO_EIO_FAILED \ + __einfo_uniqify ( EINFO_EIO, 0x01, \ + "Function call failed" ) +#define EIO_HARDWARE __einfo_error ( EINFO_EIO_HARDWARE ) +#define EINFO_EIO_HARDWARE \ + __einfo_uniqify ( EINFO_EIO, 0x02, \ + "Not supported in current configuration" ) +#define EIO_MODE __einfo_error ( EINFO_EIO_MODE ) +#define EINFO_EIO_MODE \ + __einfo_uniqify ( EINFO_EIO, 0x03, \ + "Invalid in current video mode" ) +#define EIO_VBE( code ) \ + EUNIQ ( EINFO_EIO, (code), EIO_FAILED, EIO_HARDWARE, EIO_MODE ) + +/* Set default console usage if applicable */ +#if ! ( defined ( CONSOLE_VESAFB ) && CONSOLE_EXPLICIT ( CONSOLE_VESAFB ) ) +#undef CONSOLE_VESAFB +#define CONSOLE_VESAFB ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG ) +#endif + +/** Font corresponding to selected character width and height */ +#define VESAFB_FONT VBE_FONT_8x16 + +/* Forward declaration */ +struct console_driver vesafb_console __console_driver; + +/** A VESA frame buffer */ +struct vesafb { + /** Frame buffer console */ + struct fbcon fbcon; + /** Physical start address */ + physaddr_t start; + /** Pixel geometry */ + struct fbcon_geometry pixel; + /** Margin */ + struct fbcon_margin margin; + /** Colour mapping */ + struct fbcon_colour_map map; + /** Font definition */ + struct fbcon_font font; + /** Saved VGA mode */ + uint8_t saved_mode; +}; + +/** The VESA frame buffer */ +static struct vesafb vesafb; + +/** Base memory buffer used for VBE calls */ +union vbe_buffer { + /** VBE controller information block */ + struct vbe_controller_info controller; + /** VBE mode information block */ + struct vbe_mode_info mode; +}; +static union vbe_buffer __bss16 ( vbe_buf ); +#define vbe_buf __use_data16 ( vbe_buf ) + +/** + * Convert VBE status code to iPXE status code + * + * @v status VBE status code + * @ret rc Return status code + */ +static int vesafb_rc ( unsigned int status ) { + unsigned int code; + + if ( ( status & 0xff ) != 0x4f ) + return -ENOTSUP; + code = ( ( status >> 8 ) & 0xff ); + return ( code ? -EIO_VBE ( code ) : 0 ); +} + +/** + * Get font definition + * + */ +static void vesafb_font ( void ) { + struct segoff font; + + /* Get font information + * + * Working around gcc bugs is icky here. The value we want is + * returned in %ebp, but there's no way to specify %ebp in an + * output constraint. We can't put %ebp in the clobber list, + * because this tends to cause random build failures on some + * gcc versions. We can't manually push/pop %ebp and return + * the value via a generic register output constraint, because + * gcc might choose to use %ebp to satisfy that constraint + * (and we have no way to prevent it from so doing). + * + * Work around this hideous mess by using %ecx and %edx as the + * output registers, since they get clobbered anyway. + */ + __asm__ __volatile__ ( REAL_CODE ( "pushw %%bp\n\t" /* gcc bug */ + "int $0x10\n\t" + "movw %%es, %%cx\n\t" + "movw %%bp, %%dx\n\t" + "popw %%bp\n\t" /* gcc bug */ ) + : "=c" ( font.segment ), + "=d" ( font.offset ) + : "a" ( VBE_GET_FONT ), + "b" ( VESAFB_FONT ) ); + DBGC ( &vbe_buf, "VESAFB has font %04x at %04x:%04x\n", + VESAFB_FONT, font.segment, font.offset ); + vesafb.font.start = real_to_user ( font.segment, font.offset ); +} + +/** + * Get VBE mode list + * + * @ret mode_numbers Mode number list (terminated with VBE_MODE_END) + * @ret rc Return status code + * + * The caller is responsible for eventually freeing the mode list. + */ +static int vesafb_mode_list ( uint16_t **mode_numbers ) { + struct vbe_controller_info *controller = &vbe_buf.controller; + userptr_t video_mode_ptr; + uint16_t mode_number; + uint16_t status; + size_t len; + int rc; + + /* Avoid returning uninitialised data on error */ + *mode_numbers = NULL; + + /* Get controller information block */ + controller->vbe_signature = 0; + __asm__ __volatile__ ( REAL_CODE ( "int $0x10" ) + : "=a" ( status ) + : "a" ( VBE_CONTROLLER_INFO ), + "D" ( __from_data16 ( controller ) ) + : "memory", "ebx", "edx" ); + if ( ( rc = vesafb_rc ( status ) ) != 0 ) { + DBGC ( &vbe_buf, "VESAFB could not get controller information: " + "[%04x] %s\n", status, strerror ( rc ) ); + return rc; + } + if ( controller->vbe_signature != VBE_CONTROLLER_SIGNATURE ) { + DBGC ( &vbe_buf, "VESAFB invalid controller signature " + "\"%c%c%c%c\"\n", ( controller->vbe_signature >> 0 ), + ( controller->vbe_signature >> 8 ), + ( controller->vbe_signature >> 16 ), + ( controller->vbe_signature >> 24 ) ); + DBGC_HDA ( &vbe_buf, 0, controller, sizeof ( *controller ) ); + return -EINVAL; + } + DBGC ( &vbe_buf, "VESAFB found VBE version %d.%d with mode list at " + "%04x:%04x\n", controller->vbe_major_version, + controller->vbe_minor_version, + controller->video_mode_ptr.segment, + controller->video_mode_ptr.offset ); + + /* Calculate length of mode list */ + video_mode_ptr = real_to_user ( controller->video_mode_ptr.segment, + controller->video_mode_ptr.offset ); + len = 0; + do { + copy_from_user ( &mode_number, video_mode_ptr, len, + sizeof ( mode_number ) ); + len += sizeof ( mode_number ); + } while ( mode_number != VBE_MODE_END ); + + /* Allocate and fill mode list */ + *mode_numbers = malloc ( len ); + if ( ! *mode_numbers ) + return -ENOMEM; + copy_from_user ( *mode_numbers, video_mode_ptr, 0, len ); + + return 0; +} + +/** + * Get video mode information + * + * @v mode_number Mode number + * @ret rc Return status code + */ +static int vesafb_mode_info ( unsigned int mode_number ) { + struct vbe_mode_info *mode = &vbe_buf.mode; + uint16_t status; + int rc; + + /* Get mode information */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x10" ) + : "=a" ( status ) + : "a" ( VBE_MODE_INFO ), + "c" ( mode_number ), + "D" ( __from_data16 ( mode ) ) + : "memory" ); + if ( ( rc = vesafb_rc ( status ) ) != 0 ) { + DBGC ( &vbe_buf, "VESAFB could not get mode %04x information: " + "[%04x] %s\n", mode_number, status, strerror ( rc ) ); + return rc; + } + DBGC ( &vbe_buf, "VESAFB mode %04x %dx%d %dbpp(%d:%d:%d:%d) model " + "%02x [x%d]%s%s%s%s%s\n", mode_number, mode->x_resolution, + mode->y_resolution, mode->bits_per_pixel, mode->rsvd_mask_size, + mode->red_mask_size, mode->green_mask_size, mode->blue_mask_size, + mode->memory_model, ( mode->number_of_image_pages + 1 ), + ( ( mode->mode_attributes & VBE_MODE_ATTR_SUPPORTED ) ? + "" : " [unsupported]" ), + ( ( mode->mode_attributes & VBE_MODE_ATTR_TTY ) ? + " [tty]" : "" ), + ( ( mode->mode_attributes & VBE_MODE_ATTR_GRAPHICS ) ? + "" : " [text]" ), + ( ( mode->mode_attributes & VBE_MODE_ATTR_LINEAR ) ? + "" : " [nonlinear]" ), + ( ( mode->mode_attributes & VBE_MODE_ATTR_TRIPLE_BUF ) ? + " [buf]" : "" ) ); + + return 0; +} + +/** + * Set video mode + * + * @v mode_number Mode number + * @ret rc Return status code + */ +static int vesafb_set_mode ( unsigned int mode_number ) { + struct vbe_mode_info *mode = &vbe_buf.mode; + uint16_t status; + int rc; + + /* Get mode information */ + if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 ) + return rc; + + /* Record mode parameters */ + vesafb.start = mode->phys_base_ptr; + vesafb.pixel.width = mode->x_resolution; + vesafb.pixel.height = mode->y_resolution; + vesafb.pixel.len = ( ( mode->bits_per_pixel + 7 ) / 8 ); + vesafb.pixel.stride = mode->bytes_per_scan_line; + DBGC ( &vbe_buf, "VESAFB mode %04x has frame buffer at %08x\n", + mode_number, mode->phys_base_ptr ); + + /* Initialise font colours */ + vesafb.map.red_scale = ( 8 - mode->red_mask_size ); + vesafb.map.green_scale = ( 8 - mode->green_mask_size ); + vesafb.map.blue_scale = ( 8 - mode->blue_mask_size ); + vesafb.map.red_lsb = mode->red_field_position; + vesafb.map.green_lsb = mode->green_field_position; + vesafb.map.blue_lsb = mode->blue_field_position; + + /* Select this mode */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x10" ) + : "=a" ( status ) + : "a" ( VBE_SET_MODE ), + "b" ( mode_number ) ); + if ( ( rc = vesafb_rc ( status ) ) != 0 ) { + DBGC ( &vbe_buf, "VESAFB could not set mode %04x: [%04x] %s\n", + mode_number, status, strerror ( rc ) ); + return rc; + } + + return 0; +} + +/** + * Select video mode + * + * @v mode_numbers Mode number list (terminated with VBE_MODE_END) + * @v min_width Minimum required width (in pixels) + * @v min_height Minimum required height (in pixels) + * @v min_bpp Minimum required colour depth (in bits per pixel) + * @ret mode_number Mode number, or negative error + */ +static int vesafb_select_mode ( const uint16_t *mode_numbers, + unsigned int min_width, unsigned int min_height, + unsigned int min_bpp ) { + struct vbe_mode_info *mode = &vbe_buf.mode; + int best_mode_number = -ENOENT; + unsigned int best_score = INT_MAX; + unsigned int score; + uint16_t mode_number; + int rc; + + /* Find the first suitable mode */ + while ( ( mode_number = *(mode_numbers++) ) != VBE_MODE_END ) { + + /* Force linear mode variant */ + mode_number |= VBE_MODE_LINEAR; + + /* Get mode information */ + if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 ) + continue; + + /* Skip unusable modes */ + if ( ( mode->mode_attributes & ( VBE_MODE_ATTR_SUPPORTED | + VBE_MODE_ATTR_GRAPHICS | + VBE_MODE_ATTR_LINEAR ) ) != + ( VBE_MODE_ATTR_SUPPORTED | VBE_MODE_ATTR_GRAPHICS | + VBE_MODE_ATTR_LINEAR ) ) { + continue; + } + if ( mode->memory_model != VBE_MODE_MODEL_DIRECT_COLOUR ) + continue; + + /* Skip modes not meeting the requirements */ + if ( ( mode->x_resolution < min_width ) || + ( mode->y_resolution < min_height ) || + ( mode->bits_per_pixel < min_bpp ) ) { + continue; + } + + /* Select this mode if it has the best (i.e. lowest) + * score. We choose the scoring system to favour + * modes close to the specified width and height; + * within modes of the same width and height we prefer + * a higher colour depth. + */ + score = ( ( mode->x_resolution * mode->y_resolution ) - + mode->bits_per_pixel ); + if ( score < best_score ) { + best_mode_number = mode_number; + best_score = score; + } + } + + if ( best_mode_number >= 0 ) { + DBGC ( &vbe_buf, "VESAFB selected mode %04x\n", + best_mode_number ); + } else { + DBGC ( &vbe_buf, "VESAFB found no suitable mode\n" ); + } + + return best_mode_number; +} + +/** + * Restore video mode + * + */ +static void vesafb_restore ( void ) { + uint32_t discard_a; + + /* Restore saved VGA mode */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x10" ) + : "=a" ( discard_a ) + : "a" ( VBE_SET_VGA_MODE | vesafb.saved_mode ) ); + DBGC ( &vbe_buf, "VESAFB restored VGA mode %#02x\n", + vesafb.saved_mode ); +} + +/** + * Initialise VESA frame buffer + * + * @v config Console configuration, or NULL to reset + * @ret rc Return status code + */ +static int vesafb_init ( struct console_configuration *config ) { + uint32_t discard_b; + uint16_t *mode_numbers; + unsigned int xgap; + unsigned int ygap; + unsigned int left; + unsigned int right; + unsigned int top; + unsigned int bottom; + int mode_number; + int rc; + + /* Record current VGA mode */ + __asm__ __volatile__ ( REAL_CODE ( "int $0x10" ) + : "=a" ( vesafb.saved_mode ), "=b" ( discard_b ) + : "a" ( VBE_GET_VGA_MODE ) ); + DBGC ( &vbe_buf, "VESAFB saved VGA mode %#02x\n", vesafb.saved_mode ); + + /* Get VESA mode list */ + if ( ( rc = vesafb_mode_list ( &mode_numbers ) ) != 0 ) + goto err_mode_list; + + /* Select mode */ + if ( ( mode_number = vesafb_select_mode ( mode_numbers, config->width, + config->height, + config->depth ) ) < 0 ) { + rc = mode_number; + goto err_select_mode; + } + + /* Set mode */ + if ( ( rc = vesafb_set_mode ( mode_number ) ) != 0 ) + goto err_set_mode; + + /* Calculate margin. If the actual screen size is larger than + * the requested screen size, then update the margins so that + * the margin remains relative to the requested screen size. + * (As an exception, if a zero margin was specified then treat + * this as meaning "expand to edge of actual screen".) + */ + xgap = ( vesafb.pixel.width - config->width ); + ygap = ( vesafb.pixel.height - config->height ); + left = ( xgap / 2 ); + right = ( xgap - left ); + top = ( ygap / 2 ); + bottom = ( ygap - top ); + vesafb.margin.left = ( config->left + ( config->left ? left : 0 ) ); + vesafb.margin.right = ( config->right + ( config->right ? right : 0 ) ); + vesafb.margin.top = ( config->top + ( config->top ? top : 0 ) ); + vesafb.margin.bottom = + ( config->bottom + ( config->bottom ? bottom : 0 ) ); + + /* Get font data */ + vesafb_font(); + + /* Initialise frame buffer console */ + if ( ( rc = fbcon_init ( &vesafb.fbcon, phys_to_user ( vesafb.start ), + &vesafb.pixel, &vesafb.margin, &vesafb.map, + &vesafb.font, config->pixbuf ) ) != 0 ) + goto err_fbcon_init; + + free ( mode_numbers ); + return 0; + + fbcon_fini ( &vesafb.fbcon ); + err_fbcon_init: + err_set_mode: + vesafb_restore(); + err_select_mode: + free ( mode_numbers ); + err_mode_list: + return rc; +} + +/** + * Finalise VESA frame buffer + * + */ +static void vesafb_fini ( void ) { + + /* Finalise frame buffer console */ + fbcon_fini ( &vesafb.fbcon ); + + /* Restore saved VGA mode */ + vesafb_restore(); +} + +/** + * Print a character to current cursor position + * + * @v character Character + */ +static void vesafb_putchar ( int character ) { + + fbcon_putchar ( &vesafb.fbcon, character ); +} + +/** + * Configure console + * + * @v config Console configuration, or NULL to reset + * @ret rc Return status code + */ +static int vesafb_configure ( struct console_configuration *config ) { + int rc; + + /* Reset console, if applicable */ + if ( ! vesafb_console.disabled ) { + vesafb_fini(); + bios_console.disabled &= ~CONSOLE_DISABLED_OUTPUT; + ansicol_reset_magic(); + } + vesafb_console.disabled = CONSOLE_DISABLED; + + /* Do nothing more unless we have a usable configuration */ + if ( ( config == NULL ) || + ( config->width == 0 ) || ( config->height == 0 ) ) { + return 0; + } + + /* Initialise VESA frame buffer */ + if ( ( rc = vesafb_init ( config ) ) != 0 ) + return rc; + + /* Mark console as enabled */ + vesafb_console.disabled = 0; + bios_console.disabled |= CONSOLE_DISABLED_OUTPUT; + + /* Set magic colour to transparent if we have a background picture */ + if ( config->pixbuf ) + ansicol_set_magic_transparent(); + + return 0; +} + +/** VESA frame buffer console driver */ +struct console_driver vesafb_console __console_driver = { + .usage = CONSOLE_VESAFB, + .putchar = vesafb_putchar, + .configure = vesafb_configure, + .disabled = CONSOLE_DISABLED, +}; -- cgit 1.2.3-korg