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
18 * You can also choose to distribute this program under the terms of
19 * the Unmodified Binary Distribution Licence (as given in the file
20 * COPYING.UBDL), provided that you have satisfied its requirements.
23 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30 #include <ipxe/init.h>
32 #include <ipxe/hidemem.h>
34 /** Set to true if you want to test a fake E820 map */
37 /** Alignment for hidden memory regions */
38 #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
41 * A hidden region of iPXE
43 * This represents a region that will be edited out of the system's
46 * This structure is accessed by assembly code, so must not be
49 struct hidden_region {
50 /** Physical start address */
52 /** Physical end address */
56 /** Hidden base memory */
57 extern struct hidden_region __data16 ( hidemem_base );
58 #define hidemem_base __use_data16 ( hidemem_base )
60 /** Hidden umalloc memory */
61 extern struct hidden_region __data16 ( hidemem_umalloc );
62 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
64 /** Hidden text memory */
65 extern struct hidden_region __data16 ( hidemem_textdata );
66 #define hidemem_textdata __use_data16 ( hidemem_textdata )
68 /** Assembly routine in e820mangler.S */
71 /** Vector for storing original INT 15 handler */
72 extern struct segoff __text16 ( int15_vector );
73 #define int15_vector __use_text16 ( int15_vector )
75 /* The linker defines these symbols for us */
76 extern char _textdata[];
77 extern char _etextdata[];
78 extern char _text16_memsz[];
79 #define _text16_memsz ( ( unsigned int ) _text16_memsz )
80 extern char _data16_memsz[];
81 #define _data16_memsz ( ( unsigned int ) _data16_memsz )
84 * Hide region of memory from system memory map
86 * @v region Hidden memory region
87 * @v start Start of region
88 * @v end End of region
90 static void hide_region ( struct hidden_region *region,
91 physaddr_t start, physaddr_t end ) {
93 /* Some operating systems get a nasty shock if a region of the
94 * E820 map seems to start on a non-page boundary. Make life
95 * safer by rounding out our edited region.
97 region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
98 region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
100 DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
104 * Hide used base memory
107 void hide_basemem ( void ) {
108 /* Hide from the top of free base memory to 640kB. Don't use
109 * hide_region(), because we don't want this rounded to the
110 * nearest page boundary.
112 hidemem_base.start = ( get_fbms() * 1024 );
116 * Hide umalloc() region
119 void hide_umalloc ( physaddr_t start, physaddr_t end ) {
120 assert ( end <= virt_to_phys ( _textdata ) );
121 hide_region ( &hidemem_umalloc, start, end );
125 * Hide .text and .data
128 void hide_textdata ( void ) {
129 hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
130 virt_to_phys ( _etextdata ) );
136 * Installs an INT 15 handler to edit Etherboot out of the memory map
137 * returned by the BIOS.
139 static void hide_etherboot ( void ) {
140 struct memory_map memmap;
141 unsigned int rm_ds_top;
142 unsigned int rm_cs_top;
145 /* Dump memory map before mangling */
146 DBG ( "Hiding iPXE from system memory map\n" );
147 get_memmap ( &memmap );
149 /* Hook in fake E820 map, if we're testing one */
151 DBG ( "Hooking in fake E820 map\n" );
153 get_memmap ( &memmap );
156 /* Initialise the hidden regions */
158 hide_umalloc ( virt_to_phys ( _textdata ), virt_to_phys ( _textdata ) );
161 /* Some really moronic BIOSes bring up the PXE stack via the
162 * UNDI loader entry point and then don't bother to unload it
163 * before overwriting the code and data segments. If this
164 * happens, we really don't want to leave INT 15 hooked,
165 * because that will cause any loaded OS to die horribly as
166 * soon as it attempts to fetch the system memory map.
168 * We use a heuristic to guess whether or not we are being
171 rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
172 rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
174 if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
175 DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
176 "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
177 DBG ( "Disabling INT 15 memory hiding\n" );
182 hook_bios_interrupt ( 0x15, ( unsigned int ) int15,
185 /* Dump memory map after mangling */
186 DBG ( "Hidden iPXE from system memory map\n" );
187 get_memmap ( &memmap );
193 * Uninstalls the INT 15 handler installed by hide_etherboot(), if
196 static void unhide_etherboot ( int flags __unused ) {
197 struct memory_map memmap;
200 /* If we have more than one hooked interrupt at this point, it
201 * means that some other vector is still hooked, in which case
202 * we can't safely unhook INT 15 because we need to keep our
203 * memory protected. (We expect there to be at least one
204 * hooked interrupt, because INT 15 itself is still hooked).
206 if ( hooked_bios_interrupts > 1 ) {
207 DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
208 hooked_bios_interrupts );
212 /* Try to unhook INT 15 */
213 if ( ( rc = unhook_bios_interrupt ( 0x15, ( unsigned int ) int15,
214 &int15_vector ) ) != 0 ) {
215 DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
216 /* Leave it hooked; there's nothing else we can do,
217 * and it should be intrinsically safe (though
222 /* Unhook fake E820 map, if used */
226 /* Dump memory map after unhiding */
227 DBG ( "Unhidden iPXE from system memory map\n" );
228 get_memmap ( &memmap );
231 /** Hide Etherboot startup function */
232 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
233 .startup = hide_etherboot,
234 .shutdown = unhide_etherboot,