iPXE
fakee820.c
Go to the documentation of this file.
00001 /* Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00002  *
00003  * This program is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU General Public License as
00005  * published by the Free Software Foundation; either version 2 of the
00006  * License, or any later version.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00016  * 02110-1301, USA.
00017  *
00018  * You can also choose to distribute this program under the terms of
00019  * the Unmodified Binary Distribution Licence (as given in the file
00020  * COPYING.UBDL), provided that you have satisfied its requirements.
00021  */
00022 
00023 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00024 
00025 #include <realmode.h>
00026 #include <biosint.h>
00027 
00028 /** Assembly routine in inline asm */
00029 extern void int15_fakee820();
00030 
00031 /** Original INT 15 handler */
00032 static struct segoff __text16 ( real_int15_vector );
00033 #define real_int15_vector __use_text16 ( real_int15_vector )
00034 
00035 /** An INT 15,e820 memory map entry */
00036 struct e820_entry {
00037         /** Start of region */
00038         uint64_t start;
00039         /** Length of region */
00040         uint64_t len;
00041         /** Type of region */
00042         uint32_t type;
00043 } __attribute__ (( packed ));
00044 
00045 #define E820_TYPE_RAM           1 /**< Normal memory */
00046 #define E820_TYPE_RSVD          2 /**< Reserved and unavailable */
00047 #define E820_TYPE_ACPI          3 /**< ACPI reclaim memory */
00048 #define E820_TYPE_NVS           4 /**< ACPI NVS memory */
00049 
00050 /** Fake e820 map */
00051 static struct e820_entry __text16_array ( e820map, [] ) __used = {
00052         { 0x00000000ULL, ( 0x000a0000ULL - 0x00000000ULL ), E820_TYPE_RAM },
00053         { 0x00100000ULL, ( 0xcfb50000ULL - 0x00100000ULL ), E820_TYPE_RAM },
00054         { 0xcfb50000ULL, ( 0xcfb64000ULL - 0xcfb50000ULL ), E820_TYPE_RSVD },
00055         { 0xcfb64000ULL, ( 0xcfb66000ULL - 0xcfb64000ULL ), E820_TYPE_RSVD },
00056         { 0xcfb66000ULL, ( 0xcfb85c00ULL - 0xcfb66000ULL ), E820_TYPE_ACPI },
00057         { 0xcfb85c00ULL, ( 0xd0000000ULL - 0xcfb85c00ULL ), E820_TYPE_RSVD },
00058         { 0xe0000000ULL, ( 0xf0000000ULL - 0xe0000000ULL ), E820_TYPE_RSVD },
00059         { 0xfe000000ULL, (0x100000000ULL - 0xfe000000ULL ), E820_TYPE_RSVD },
00060         {0x100000000ULL, (0x230000000ULL -0x100000000ULL ), E820_TYPE_RAM },
00061 };
00062 #define e820map __use_text16 ( e820map )
00063 
00064 void fake_e820 ( void ) {
00065         __asm__ __volatile__ (
00066                 TEXT16_CODE ( "\nint15_fakee820:\n\t"
00067                               "pushfw\n\t"
00068                               "cmpl $0xe820, %%eax\n\t"
00069                               "jne 99f\n\t"
00070                               "cmpl $0x534d4150, %%edx\n\t"
00071                               "jne 99f\n\t"
00072                               "pushaw\n\t"
00073                               "movw %%sp, %%bp\n\t"
00074                               "andb $~0x01, 22(%%bp)\n\t" /* Clear return CF */
00075                               "leaw e820map(%%bx), %%si\n\t"
00076                               "cs rep movsb\n\t"
00077                               "popaw\n\t"
00078                               "movl %%edx, %%eax\n\t"
00079                               "addl $20, %%ebx\n\t"
00080                               "cmpl %0, %%ebx\n\t"
00081                               "jne 1f\n\t"
00082                               "xorl %%ebx,%%ebx\n\t"
00083                               "\n1:\n\t"
00084                               "popfw\n\t"
00085                               "iret\n\t"
00086                               "\n99:\n\t"
00087                               "popfw\n\t"
00088                               "ljmp *%%cs:real_int15_vector\n\t" )
00089                 : : "i" ( sizeof ( e820map ) ) );
00090 
00091         hook_bios_interrupt ( 0x15, ( intptr_t ) int15_fakee820,
00092                               &real_int15_vector );
00093 }
00094 
00095 void unfake_e820 ( void ) {
00096         unhook_bios_interrupt ( 0x15, ( intptr_t ) int15_fakee820,
00097                                 &real_int15_vector );
00098 }