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., 51 Franklin Street, Fifth Floor, Boston, MA
19 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/init.h>
28 #include <ipxe/hidemem.h>
30 /** Set to true if you want to test a fake E820 map */
33 /** Alignment for hidden memory regions */
34 #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
37 * A hidden region of iPXE
39 * This represents a region that will be edited out of the system's
42 * This structure is accessed by assembly code, so must not be
45 struct hidden_region {
46 /** Physical start address */
48 /** Physical end address */
52 /** Hidden base memory */
53 extern struct hidden_region __data16 ( hidemem_base );
54 #define hidemem_base __use_data16 ( hidemem_base )
56 /** Hidden umalloc memory */
57 extern struct hidden_region __data16 ( hidemem_umalloc );
58 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
60 /** Hidden text memory */
61 extern struct hidden_region __data16 ( hidemem_textdata );
62 #define hidemem_textdata __use_data16 ( hidemem_textdata )
64 /** Assembly routine in e820mangler.S */
67 /** Vector for storing original INT 15 handler */
68 extern struct segoff __text16 ( int15_vector );
69 #define int15_vector __use_text16 ( int15_vector )
71 /* The linker defines these symbols for us */
72 extern char _textdata[];
73 extern char _etextdata[];
74 extern char _text16_memsz[];
75 #define _text16_memsz ( ( unsigned int ) _text16_memsz )
76 extern char _data16_memsz[];
77 #define _data16_memsz ( ( unsigned int ) _data16_memsz )
80 * Hide region of memory from system memory map
82 * @v region Hidden memory region
83 * @v start Start of region
84 * @v end End of region
86 static void hide_region ( struct hidden_region *region,
87 physaddr_t start, physaddr_t end ) {
89 /* Some operating systems get a nasty shock if a region of the
90 * E820 map seems to start on a non-page boundary. Make life
91 * safer by rounding out our edited region.
93 region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
94 region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
96 DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
100 * Hide used base memory
103 void hide_basemem ( void ) {
104 /* Hide from the top of free base memory to 640kB. Don't use
105 * hide_region(), because we don't want this rounded to the
106 * nearest page boundary.
108 hidemem_base.start = ( get_fbms() * 1024 );
112 * Hide umalloc() region
115 void hide_umalloc ( physaddr_t start, physaddr_t end ) {
116 assert ( end <= virt_to_phys ( _textdata ) );
117 hide_region ( &hidemem_umalloc, start, end );
121 * Hide .text and .data
124 void hide_textdata ( void ) {
125 hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
126 virt_to_phys ( _etextdata ) );
132 * Installs an INT 15 handler to edit Etherboot out of the memory map
133 * returned by the BIOS.
135 static void hide_etherboot ( void ) {
136 struct memory_map memmap;
137 unsigned int rm_ds_top;
138 unsigned int rm_cs_top;
141 /* Dump memory map before mangling */
142 DBG ( "Hiding iPXE from system memory map\n" );
143 get_memmap ( &memmap );
145 /* Hook in fake E820 map, if we're testing one */
147 DBG ( "Hooking in fake E820 map\n" );
149 get_memmap ( &memmap );
152 /* Initialise the hidden regions */
154 hide_umalloc ( virt_to_phys ( _textdata ), virt_to_phys ( _textdata ) );
157 /* Some really moronic BIOSes bring up the PXE stack via the
158 * UNDI loader entry point and then don't bother to unload it
159 * before overwriting the code and data segments. If this
160 * happens, we really don't want to leave INT 15 hooked,
161 * because that will cause any loaded OS to die horribly as
162 * soon as it attempts to fetch the system memory map.
164 * We use a heuristic to guess whether or not we are being
167 rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
168 rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
170 if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
171 DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
172 "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
173 DBG ( "Disabling INT 15 memory hiding\n" );
178 hook_bios_interrupt ( 0x15, ( unsigned int ) int15,
181 /* Dump memory map after mangling */
182 DBG ( "Hidden iPXE from system memory map\n" );
183 get_memmap ( &memmap );
189 * Uninstalls the INT 15 handler installed by hide_etherboot(), if
192 static void unhide_etherboot ( int flags __unused ) {
193 struct memory_map memmap;
196 /* If we have more than one hooked interrupt at this point, it
197 * means that some other vector is still hooked, in which case
198 * we can't safely unhook INT 15 because we need to keep our
199 * memory protected. (We expect there to be at least one
200 * hooked interrupt, because INT 15 itself is still hooked).
202 if ( hooked_bios_interrupts > 1 ) {
203 DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
204 hooked_bios_interrupts );
208 /* Try to unhook INT 15 */
209 if ( ( rc = unhook_bios_interrupt ( 0x15, ( unsigned int ) int15,
210 &int15_vector ) ) != 0 ) {
211 DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
212 /* Leave it hooked; there's nothing else we can do,
213 * and it should be intrinsically safe (though
218 /* Unhook fake E820 map, if used */
222 /* Dump memory map after unhiding */
223 DBG ( "Unhidden iPXE from system memory map\n" );
224 get_memmap ( &memmap );
227 /** Hide Etherboot startup function */
228 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
229 .startup = hide_etherboot,
230 .shutdown = unhide_etherboot,