1 /* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of the
6 * License, or any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/init.h>
23 #include <gpxe/memmap.h>
24 #include <gpxe/hidemem.h>
26 /** Alignment for hidden memory regions */
27 #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
30 * A hidden region of gPXE
32 * This represents a region that will be edited out of the system's
35 * This structure is accessed by assembly code, so must not be
38 struct hidden_region {
39 /** Physical start address */
41 /** Physical end address */
45 /** Hidden base memory */
46 extern struct hidden_region __data16 ( hidemem_base );
47 #define hidemem_base __use_data16 ( hidemem_base )
49 /** Hidden umalloc memory */
50 extern struct hidden_region __data16 ( hidemem_umalloc );
51 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
53 /** Hidden text memory */
54 extern struct hidden_region __data16 ( hidemem_text );
55 #define hidemem_text __use_data16 ( hidemem_text )
57 /** Assembly routine in e820mangler.S */
60 /** Vector for storing original INT 15 handler */
61 extern struct segoff __text16 ( int15_vector );
62 #define int15_vector __use_text16 ( int15_vector )
64 /* The linker defines these symbols for us */
67 extern char _text16_size[];
68 #define _text16_size ( ( unsigned int ) _text16_size )
69 extern char _data16_size[];
70 #define _data16_size ( ( unsigned int ) _data16_size )
73 * Hide region of memory from system memory map
75 * @v region Hidden memory region
76 * @v start Start of region
77 * @v end End of region
79 static void hide_region ( struct hidden_region *region,
80 physaddr_t start, physaddr_t end ) {
82 /* Some operating systems get a nasty shock if a region of the
83 * E820 map seems to start on a non-page boundary. Make life
84 * safer by rounding out our edited region.
86 region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
87 region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
89 DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
93 * Hide used base memory
96 void hide_basemem ( void ) {
97 /* Hide from the top of free base memory to 640kB. Don't use
98 * hide_region(), because we don't want this rounded to the
99 * nearest page boundary.
101 hidemem_base.start = ( get_fbms() * 1024 );
105 * Hide umalloc() region
108 void hide_umalloc ( physaddr_t start, physaddr_t end ) {
109 assert ( end <= virt_to_phys ( _text ) );
110 hide_region ( &hidemem_umalloc, start, end );
114 * Hide .text and .data
117 void hide_text ( void ) {
118 hide_region ( &hidemem_text, virt_to_phys ( _text ),
119 virt_to_phys ( _end ) );
125 * Installs an INT 15 handler to edit Etherboot out of the memory map
126 * returned by the BIOS.
128 static void hide_etherboot ( void ) {
129 struct memory_map memmap;
130 unsigned int rm_ds_top;
131 unsigned int rm_cs_top;
134 /* Dump memory map before mangling */
135 DBG ( "Hiding gPXE from system memory map\n" );
136 get_memmap ( &memmap );
138 /* Initialise the hidden regions */
140 hide_umalloc ( virt_to_phys ( _text ), virt_to_phys ( _text ) );
143 /* Some really moronic BIOSes bring up the PXE stack via the
144 * UNDI loader entry point and then don't bother to unload it
145 * before overwriting the code and data segments. If this
146 * happens, we really don't want to leave INT 15 hooked,
147 * because that will cause any loaded OS to die horribly as
148 * soon as it attempts to fetch the system memory map.
150 * We use a heuristic to guess whether or not we are being
153 rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_size + 1024 - 1 ) >> 10 );
154 rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_size + 1024 - 1 ) >> 10 );
156 if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
157 DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
158 "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
159 DBG ( "Disabling INT 15 memory hiding\n" );
164 hook_bios_interrupt ( 0x15, ( unsigned int ) int15,
167 /* Dump memory map after mangling */
168 DBG ( "Hidden gPXE from system memory map\n" );
169 get_memmap ( &memmap );
175 * Uninstalls the INT 15 handler installed by hide_etherboot(), if
178 static void unhide_etherboot ( int flags __unused ) {
180 /* If we have more than one hooked interrupt at this point, it
181 * means that some other vector is still hooked, in which case
182 * we can't safely unhook INT 15 because we need to keep our
183 * memory protected. (We expect there to be at least one
184 * hooked interrupt, because INT 15 itself is still hooked).
186 if ( hooked_bios_interrupts > 1 ) {
187 DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
188 hooked_bios_interrupts );
192 /* Try to unhook INT 15. If it fails, then just leave it
193 * hooked; it takes care of protecting itself. :)
195 unhook_bios_interrupt ( 0x15, ( unsigned int ) int15,
199 /** Hide Etherboot startup function */
200 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
201 .startup = hide_etherboot,
202 .shutdown = unhide_etherboot,