]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IPXE/ipxe-3fe683e/src/arch/x86/interface/pcbios/hidemem.c
initial commit
[Ventoy.git] / IPXE / ipxe-3fe683e / src / arch / x86 / interface / pcbios / hidemem.c
1 /* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301, USA.
17 *
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.
21 */
22
23 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
24
25 #include <assert.h>
26 #include <realmode.h>
27 #include <biosint.h>
28 #include <basemem.h>
29 #include <fakee820.h>
30 #include <ipxe/init.h>
31 #include <ipxe/io.h>
32 #include <ipxe/hidemem.h>
33
34 /** Set to true if you want to test a fake E820 map */
35 #define FAKE_E820 0
36
37 /** Alignment for hidden memory regions */
38 #define ALIGN_HIDDEN 4096 /* 4kB page alignment should be enough */
39
40 /**
41 * A hidden region of iPXE
42 *
43 * This represents a region that will be edited out of the system's
44 * memory map.
45 *
46 * This structure is accessed by assembly code, so must not be
47 * changed.
48 */
49 struct hidden_region {
50 /** Physical start address */
51 uint64_t start;
52 /** Physical end address */
53 uint64_t end;
54 };
55
56 /** Hidden base memory */
57 extern struct hidden_region __data16 ( hidemem_base );
58 #define hidemem_base __use_data16 ( hidemem_base )
59
60 /** Hidden umalloc memory */
61 extern struct hidden_region __data16 ( hidemem_umalloc );
62 #define hidemem_umalloc __use_data16 ( hidemem_umalloc )
63
64 /** Hidden text memory */
65 extern struct hidden_region __data16 ( hidemem_textdata );
66 #define hidemem_textdata __use_data16 ( hidemem_textdata )
67
68 /** Assembly routine in e820mangler.S */
69 extern void int15();
70
71 /** Vector for storing original INT 15 handler */
72 extern struct segoff __text16 ( int15_vector );
73 #define int15_vector __use_text16 ( int15_vector )
74
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 ( ( size_t ) _text16_memsz )
80 extern char _data16_memsz[];
81 #define _data16_memsz ( ( size_t ) _data16_memsz )
82
83 /**
84 * Hide region of memory from system memory map
85 *
86 * @v region Hidden memory region
87 * @v start Start of region
88 * @v end End of region
89 */
90 static void hide_region ( struct hidden_region *region,
91 physaddr_t start, physaddr_t end ) {
92
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.
96 */
97 region->start = ( start & ~( ALIGN_HIDDEN - 1 ) );
98 region->end = ( ( end + ALIGN_HIDDEN - 1 ) & ~( ALIGN_HIDDEN - 1 ) );
99
100 DBG ( "Hiding region [%llx,%llx)\n", region->start, region->end );
101 }
102
103 /**
104 * Hide used base memory
105 *
106 */
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.
111 */
112 hidemem_base.start = ( get_fbms() * 1024 );
113 }
114
115 /**
116 * Hide umalloc() region
117 *
118 */
119 void hide_umalloc ( physaddr_t start, physaddr_t end ) {
120 assert ( end <= virt_to_phys ( _textdata ) );
121 hide_region ( &hidemem_umalloc, start, end );
122 }
123
124 /**
125 * Hide .text and .data
126 *
127 */
128 void hide_textdata ( void ) {
129 /* Deleted by longpanda */
130 #if 0
131 hide_region ( &hidemem_textdata, virt_to_phys ( _textdata ),
132 virt_to_phys ( _etextdata ) );
133 #endif
134 }
135
136 /**
137 * Hide Etherboot
138 *
139 * Installs an INT 15 handler to edit Etherboot out of the memory map
140 * returned by the BIOS.
141 */
142 static void hide_etherboot ( void ) {
143 struct memory_map memmap;
144 unsigned int rm_ds_top;
145 unsigned int rm_cs_top;
146 unsigned int fbms;
147
148 /* Dump memory map before mangling */
149 DBG ( "Hiding iPXE from system memory map\n" );
150 get_memmap ( &memmap );
151
152 /* Hook in fake E820 map, if we're testing one */
153 if ( FAKE_E820 ) {
154 DBG ( "Hooking in fake E820 map\n" );
155 fake_e820();
156 get_memmap ( &memmap );
157 }
158
159 /* Initialise the hidden regions */
160 hide_basemem();
161 hide_umalloc ( virt_to_phys ( _textdata ), virt_to_phys ( _textdata ) );
162 hide_textdata();
163
164 /* Some really moronic BIOSes bring up the PXE stack via the
165 * UNDI loader entry point and then don't bother to unload it
166 * before overwriting the code and data segments. If this
167 * happens, we really don't want to leave INT 15 hooked,
168 * because that will cause any loaded OS to die horribly as
169 * soon as it attempts to fetch the system memory map.
170 *
171 * We use a heuristic to guess whether or not we are being
172 * loaded sensibly.
173 */
174 rm_cs_top = ( ( ( rm_cs << 4 ) + _text16_memsz + 1024 - 1 ) >> 10 );
175 rm_ds_top = ( ( ( rm_ds << 4 ) + _data16_memsz + 1024 - 1 ) >> 10 );
176 fbms = get_fbms();
177 if ( ( rm_cs_top < fbms ) && ( rm_ds_top < fbms ) ) {
178 DBG ( "Detected potentially unsafe UNDI load at CS=%04x "
179 "DS=%04x FBMS=%dkB\n", rm_cs, rm_ds, fbms );
180 DBG ( "Disabling INT 15 memory hiding\n" );
181 return;
182 }
183
184 /* Hook INT 15 */
185 hook_bios_interrupt ( 0x15, ( intptr_t ) int15, &int15_vector );
186
187 /* Dump memory map after mangling */
188 DBG ( "Hidden iPXE from system memory map\n" );
189 get_memmap ( &memmap );
190 }
191
192 /**
193 * Unhide Etherboot
194 *
195 * Uninstalls the INT 15 handler installed by hide_etherboot(), if
196 * possible.
197 */
198 static void unhide_etherboot ( int flags __unused ) {
199 struct memory_map memmap;
200 int rc;
201
202 /* If we have more than one hooked interrupt at this point, it
203 * means that some other vector is still hooked, in which case
204 * we can't safely unhook INT 15 because we need to keep our
205 * memory protected. (We expect there to be at least one
206 * hooked interrupt, because INT 15 itself is still hooked).
207 */
208 if ( hooked_bios_interrupts > 1 ) {
209 DBG ( "Cannot unhide: %d interrupt vectors still hooked\n",
210 hooked_bios_interrupts );
211 return;
212 }
213
214 /* Try to unhook INT 15 */
215 if ( ( rc = unhook_bios_interrupt ( 0x15, ( intptr_t ) int15,
216 &int15_vector ) ) != 0 ) {
217 DBG ( "Cannot unhook INT15: %s\n", strerror ( rc ) );
218 /* Leave it hooked; there's nothing else we can do,
219 * and it should be intrinsically safe (though
220 * wasteful of RAM).
221 */
222 }
223
224 /* Unhook fake E820 map, if used */
225 if ( FAKE_E820 )
226 unfake_e820();
227
228 /* Dump memory map after unhiding */
229 DBG ( "Unhidden iPXE from system memory map\n" );
230 get_memmap ( &memmap );
231 }
232
233 /** Hide Etherboot startup function */
234 struct startup_fn hide_etherboot_startup_fn __startup_fn ( STARTUP_EARLY ) = {
235 .name = "hidemem",
236 .startup = hide_etherboot,
237 .shutdown = unhide_etherboot,
238 };