[pxe] Notify BIOS via INT 1a,564e for each new network device
[ipxe.git] / src / arch / i386 / interface / pxe / pxe_call.c
1 /*
2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <ipxe/uaccess.h>
27 #include <ipxe/init.h>
28 #include <ipxe/profile.h>
29 #include <ipxe/netdevice.h>
30 #include <setjmp.h>
31 #include <registers.h>
32 #include <biosint.h>
33 #include <pxe.h>
34 #include <pxe_call.h>
35
36 /** @file
37 *
38 * PXE API entry point
39 */
40
41 /* Disambiguate the various error causes */
42 #define EINFO_EPXENBP \
43 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
44 "External PXE NBP error" )
45 #define EPXENBP( status ) EPLATFORM ( EINFO_EPXENBP, status )
46
47 /** Vector for chaining INT 1A */
48 extern struct segoff __text16 ( pxe_int_1a_vector );
49 #define pxe_int_1a_vector __use_text16 ( pxe_int_1a_vector )
50
51 /** INT 1A handler */
52 extern void pxe_int_1a ( void );
53
54 /** INT 1A hooked flag */
55 static int int_1a_hooked = 0;
56
57 /** PXENV_UNDI_TRANSMIT API call profiler */
58 static struct profiler pxe_api_tx_profiler __profiler =
59 { .name = "pxeapi.tx" };
60
61 /** PXENV_UNDI_ISR API call profiler */
62 static struct profiler pxe_api_isr_profiler __profiler =
63 { .name = "pxeapi.isr" };
64
65 /** PXE unknown API call profiler
66 *
67 * This profiler can be used to measure the overhead of a dummy PXE
68 * API call.
69 */
70 static struct profiler pxe_api_unknown_profiler __profiler =
71 { .name = "pxeapi.unknown" };
72
73 /** Miscellaneous PXE API call profiler */
74 static struct profiler pxe_api_misc_profiler __profiler =
75 { .name = "pxeapi.misc" };
76
77 /**
78 * Handle an unknown PXE API call
79 *
80 * @v pxenv_unknown Pointer to a struct s_PXENV_UNKNOWN
81 * @ret #PXENV_EXIT_FAILURE Always
82 * @err #PXENV_STATUS_UNSUPPORTED Always
83 */
84 static PXENV_EXIT_t pxenv_unknown ( struct s_PXENV_UNKNOWN *pxenv_unknown ) {
85 pxenv_unknown->Status = PXENV_STATUS_UNSUPPORTED;
86 return PXENV_EXIT_FAILURE;
87 }
88
89 /** Unknown PXE API call list */
90 struct pxe_api_call pxenv_unknown_api __pxe_api_call =
91 PXE_API_CALL ( PXENV_UNKNOWN, pxenv_unknown, struct s_PXENV_UNKNOWN );
92
93 /**
94 * Locate PXE API call
95 *
96 * @v opcode Opcode
97 * @ret call PXE API call, or NULL
98 */
99 static struct pxe_api_call * find_pxe_api_call ( uint16_t opcode ) {
100 struct pxe_api_call *call;
101
102 for_each_table_entry ( call, PXE_API_CALLS ) {
103 if ( call->opcode == opcode )
104 return call;
105 }
106 return NULL;
107 }
108
109 /**
110 * Determine applicable profiler (for debugging)
111 *
112 * @v opcode PXE opcode
113 * @ret profiler Profiler
114 */
115 static struct profiler * pxe_api_profiler ( unsigned int opcode ) {
116
117 /* Determine applicable profiler */
118 switch ( opcode ) {
119 case PXENV_UNDI_TRANSMIT:
120 return &pxe_api_tx_profiler;
121 case PXENV_UNDI_ISR:
122 return &pxe_api_isr_profiler;
123 case PXENV_UNKNOWN:
124 return &pxe_api_unknown_profiler;
125 default:
126 return &pxe_api_misc_profiler;
127 }
128 }
129
130 /**
131 * Dispatch PXE API call
132 *
133 * @v bx PXE opcode
134 * @v es:di Address of PXE parameter block
135 * @ret ax PXE exit code
136 */
137 __asmcall void pxe_api_call ( struct i386_all_regs *ix86 ) {
138 uint16_t opcode = ix86->regs.bx;
139 userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
140 struct profiler *profiler = pxe_api_profiler ( opcode );
141 struct pxe_api_call *call;
142 union u_PXENV_ANY params;
143 PXENV_EXIT_t ret;
144
145 /* Start profiling */
146 profile_start ( profiler );
147
148 /* Locate API call */
149 call = find_pxe_api_call ( opcode );
150 if ( ! call ) {
151 DBGC ( &pxe_netdev, "PXENV_UNKNOWN_%04x\n", opcode );
152 call = &pxenv_unknown_api;
153 }
154
155 /* Copy parameter block from caller */
156 copy_from_user ( &params, uparams, 0, call->params_len );
157
158 /* Set default status in case child routine fails to do so */
159 params.Status = PXENV_STATUS_FAILURE;
160
161 /* Hand off to relevant API routine */
162 ret = call->entry ( &params );
163
164 /* Copy modified parameter block back to caller and return */
165 copy_to_user ( uparams, 0, &params, call->params_len );
166 ix86->regs.ax = ret;
167
168 /* Stop profiling, if applicable */
169 profile_stop ( profiler );
170 }
171
172 /**
173 * Dispatch weak PXE API call with PXE stack available
174 *
175 * @v ix86 Registers for PXE call
176 * @ret present Zero (PXE stack present)
177 */
178 int pxe_api_call_weak ( struct i386_all_regs *ix86 ) {
179 pxe_api_call ( ix86 );
180 return 0;
181 }
182
183 /**
184 * Dispatch PXE loader call
185 *
186 * @v es:di Address of PXE parameter block
187 * @ret ax PXE exit code
188 */
189 __asmcall void pxe_loader_call ( struct i386_all_regs *ix86 ) {
190 userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
191 struct s_UNDI_LOADER params;
192 PXENV_EXIT_t ret;
193
194 /* Copy parameter block from caller */
195 copy_from_user ( &params, uparams, 0, sizeof ( params ) );
196
197 /* Fill in ROM segment address */
198 ppxe.UNDIROMID.segment = ix86->segs.ds;
199
200 /* Set default status in case child routine fails to do so */
201 params.Status = PXENV_STATUS_FAILURE;
202
203 /* Call UNDI loader */
204 ret = undi_loader ( &params );
205
206 /* Copy modified parameter block back to caller and return */
207 copy_to_user ( uparams, 0, &params, sizeof ( params ) );
208 ix86->regs.ax = ret;
209 }
210
211 /**
212 * Calculate byte checksum as used by PXE
213 *
214 * @v data Data
215 * @v size Length of data
216 * @ret sum Checksum
217 */
218 static uint8_t pxe_checksum ( void *data, size_t size ) {
219 uint8_t *bytes = data;
220 uint8_t sum = 0;
221
222 while ( size-- ) {
223 sum += *bytes++;
224 }
225 return sum;
226 }
227
228 /**
229 * Initialise !PXE and PXENV+ structures
230 *
231 */
232 static void pxe_init_structures ( void ) {
233 uint32_t rm_cs_phys = ( rm_cs << 4 );
234 uint32_t rm_ds_phys = ( rm_ds << 4 );
235
236 /* Fill in missing segment fields */
237 ppxe.EntryPointSP.segment = rm_cs;
238 ppxe.EntryPointESP.segment = rm_cs;
239 ppxe.Stack.segment_address = rm_ds;
240 ppxe.Stack.Physical_address = rm_ds_phys;
241 ppxe.UNDIData.segment_address = rm_ds;
242 ppxe.UNDIData.Physical_address = rm_ds_phys;
243 ppxe.UNDICode.segment_address = rm_cs;
244 ppxe.UNDICode.Physical_address = rm_cs_phys;
245 ppxe.UNDICodeWrite.segment_address = rm_cs;
246 ppxe.UNDICodeWrite.Physical_address = rm_cs_phys;
247 pxenv.RMEntry.segment = rm_cs;
248 pxenv.StackSeg = rm_ds;
249 pxenv.UNDIDataSeg = rm_ds;
250 pxenv.UNDICodeSeg = rm_cs;
251 pxenv.PXEPtr.segment = rm_cs;
252
253 /* Update checksums */
254 ppxe.StructCksum -= pxe_checksum ( &ppxe, sizeof ( ppxe ) );
255 pxenv.Checksum -= pxe_checksum ( &pxenv, sizeof ( pxenv ) );
256 }
257
258 /** PXE structure initialiser */
259 struct init_fn pxe_init_fn __init_fn ( INIT_NORMAL ) = {
260 .initialise = pxe_init_structures,
261 };
262
263 /**
264 * Activate PXE stack
265 *
266 * @v netdev Net device to use as PXE net device
267 */
268 void pxe_activate ( struct net_device *netdev ) {
269 uint32_t discard_a;
270 uint32_t discard_b;
271 uint32_t discard_d;
272
273 /* Ensure INT 1A is hooked */
274 if ( ! int_1a_hooked ) {
275 hook_bios_interrupt ( 0x1a, ( unsigned int ) pxe_int_1a,
276 &pxe_int_1a_vector );
277 devices_get();
278 int_1a_hooked = 1;
279 }
280
281 /* Set PXE network device */
282 pxe_set_netdev ( netdev );
283
284 /* Notify BIOS of installation */
285 __asm__ __volatile__ ( REAL_CODE ( "pushw %%cs\n\t"
286 "popw %%es\n\t"
287 "int $0x1a\n\t" )
288 : "=a" ( discard_a ), "=b" ( discard_b ),
289 "=d" ( discard_d )
290 : "0" ( 0x564e ),
291 "1" ( __from_text16 ( &pxenv ) ) );
292 }
293
294 /**
295 * Deactivate PXE stack
296 *
297 * @ret rc Return status code
298 */
299 int pxe_deactivate ( void ) {
300 int rc;
301
302 /* Clear PXE network device */
303 pxe_set_netdev ( NULL );
304
305 /* Ensure INT 1A is unhooked, if possible */
306 if ( int_1a_hooked ) {
307 if ( ( rc = unhook_bios_interrupt ( 0x1a,
308 (unsigned int) pxe_int_1a,
309 &pxe_int_1a_vector ))!= 0){
310 DBG ( "Could not unhook INT 1A: %s\n",
311 strerror ( rc ) );
312 return rc;
313 }
314 devices_put();
315 int_1a_hooked = 0;
316 }
317
318 return 0;
319 }
320
321 /** Jump buffer for PXENV_RESTART_TFTP */
322 rmjmp_buf pxe_restart_nbp;
323
324 /**
325 * Start PXE NBP at 0000:7c00
326 *
327 * @ret rc Return status code
328 */
329 int pxe_start_nbp ( void ) {
330 int jmp;
331 int discard_b, discard_c, discard_d, discard_D;
332 uint16_t status;
333
334 /* Allow restarting NBP via PXENV_RESTART_TFTP */
335 jmp = rmsetjmp ( pxe_restart_nbp );
336 if ( jmp )
337 DBG ( "Restarting NBP (%x)\n", jmp );
338
339 /* Far call to PXE NBP */
340 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
341 "movw %%cx, %%es\n\t"
342 "pushw %%es\n\t"
343 "pushw %%di\n\t"
344 "sti\n\t"
345 "lcall $0, $0x7c00\n\t"
346 "popl %%ebp\n\t" /* discard */
347 "popl %%ebp\n\t" /* gcc bug */ )
348 : "=a" ( status ), "=b" ( discard_b ),
349 "=c" ( discard_c ), "=d" ( discard_d ),
350 "=D" ( discard_D )
351 : "a" ( 0 ), "b" ( __from_text16 ( &pxenv ) ),
352 "c" ( rm_cs ),
353 "d" ( virt_to_phys ( &pxenv ) ),
354 "D" ( __from_text16 ( &ppxe ) )
355 : "esi", "memory" );
356 if ( status )
357 return -EPXENBP ( status );
358
359 return 0;
360 }
361
362 /**
363 * Notify BIOS of existence of network device
364 *
365 * @v netdev Network device
366 * @ret rc Return status code
367 */
368 static int pxe_notify ( struct net_device *netdev ) {
369
370 /* Do nothing if we already have a network device */
371 if ( pxe_netdev )
372 return 0;
373
374 /* Activate (and deactivate) PXE stack to notify BIOS */
375 pxe_activate ( netdev );
376 pxe_deactivate();
377
378 return 0;
379 }
380
381 /** PXE BIOS notification driver */
382 struct net_driver pxe_driver __net_driver = {
383 .name = "PXE",
384 .probe = pxe_notify,
385 };
386
387 REQUIRING_SYMBOL ( pxe_api_call );
388 REQUIRE_OBJECT ( pxe_preboot );
389 REQUIRE_OBJECT ( pxe_undi );
390 REQUIRE_OBJECT ( pxe_udp );
391 REQUIRE_OBJECT ( pxe_tftp );
392 REQUIRE_OBJECT ( pxe_file );