2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
22 #include <ipxe/uaccess.h>
23 #include <ipxe/init.h>
24 #include <ipxe/profile.h>
26 #include <registers.h>
36 /* Disambiguate the various error causes */
37 #define EINFO_EPXENBP \
38 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
39 "External PXE NBP error" )
40 #define EPXENBP( status ) EPLATFORM ( EINFO_EPXENBP, status )
42 /** Vector for chaining INT 1A */
43 extern struct segoff __text16 ( pxe_int_1a_vector );
44 #define pxe_int_1a_vector __use_text16 ( pxe_int_1a_vector )
47 extern void pxe_int_1a ( void );
49 /** INT 1A hooked flag */
50 static int int_1a_hooked = 0;
52 /** PXENV_UNDI_TRANSMIT API call profiler */
53 static struct profiler pxe_api_tx_profiler __profiler =
54 { .name = "pxeapi.tx" };
56 /** PXENV_UNDI_ISR API call profiler */
57 static struct profiler pxe_api_isr_profiler __profiler =
58 { .name = "pxeapi.isr" };
60 /** PXE unknown API call profiler
62 * This profiler can be used to measure the overhead of a dummy PXE
65 static struct profiler pxe_api_unknown_profiler __profiler =
66 { .name = "pxeapi.unknown" };
68 /** Miscellaneous PXE API call profiler */
69 static struct profiler pxe_api_misc_profiler __profiler =
70 { .name = "pxeapi.misc" };
73 * Handle an unknown PXE API call
75 * @v pxenv_unknown Pointer to a struct s_PXENV_UNKNOWN
76 * @ret #PXENV_EXIT_FAILURE Always
77 * @err #PXENV_STATUS_UNSUPPORTED Always
79 static PXENV_EXIT_t pxenv_unknown ( struct s_PXENV_UNKNOWN *pxenv_unknown ) {
80 pxenv_unknown->Status = PXENV_STATUS_UNSUPPORTED;
81 return PXENV_EXIT_FAILURE;
84 /** Unknown PXE API call list */
85 struct pxe_api_call pxenv_unknown_api __pxe_api_call =
86 PXE_API_CALL ( PXENV_UNKNOWN, pxenv_unknown, struct s_PXENV_UNKNOWN );
92 * @ret call PXE API call, or NULL
94 static struct pxe_api_call * find_pxe_api_call ( uint16_t opcode ) {
95 struct pxe_api_call *call;
97 for_each_table_entry ( call, PXE_API_CALLS ) {
98 if ( call->opcode == opcode )
105 * Determine applicable profiler (for debugging)
107 * @v opcode PXE opcode
108 * @ret profiler Profiler
110 static struct profiler * pxe_api_profiler ( unsigned int opcode ) {
112 /* Determine applicable profiler */
114 case PXENV_UNDI_TRANSMIT:
115 return &pxe_api_tx_profiler;
117 return &pxe_api_isr_profiler;
119 return &pxe_api_unknown_profiler;
121 return &pxe_api_misc_profiler;
126 * Dispatch PXE API call
129 * @v es:di Address of PXE parameter block
130 * @ret ax PXE exit code
132 __asmcall void pxe_api_call ( struct i386_all_regs *ix86 ) {
133 uint16_t opcode = ix86->regs.bx;
134 userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
135 struct profiler *profiler = pxe_api_profiler ( opcode );
136 struct pxe_api_call *call;
137 union u_PXENV_ANY params;
140 /* Start profiling */
141 profile_start ( profiler );
143 /* Locate API call */
144 call = find_pxe_api_call ( opcode );
146 DBGC ( &pxe_netdev, "PXENV_UNKNOWN_%04x\n", opcode );
147 call = &pxenv_unknown_api;
150 /* Copy parameter block from caller */
151 copy_from_user ( ¶ms, uparams, 0, call->params_len );
153 /* Set default status in case child routine fails to do so */
154 params.Status = PXENV_STATUS_FAILURE;
156 /* Hand off to relevant API routine */
157 ret = call->entry ( ¶ms );
159 /* Copy modified parameter block back to caller and return */
160 copy_to_user ( uparams, 0, ¶ms, call->params_len );
163 /* Stop profiling, if applicable */
164 profile_stop ( profiler );
168 * Dispatch weak PXE API call with PXE stack available
170 * @v ix86 Registers for PXE call
171 * @ret present Zero (PXE stack present)
173 int pxe_api_call_weak ( struct i386_all_regs *ix86 ) {
174 pxe_api_call ( ix86 );
179 * Dispatch PXE loader call
181 * @v es:di Address of PXE parameter block
182 * @ret ax PXE exit code
184 __asmcall void pxe_loader_call ( struct i386_all_regs *ix86 ) {
185 userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
186 struct s_UNDI_LOADER params;
189 /* Copy parameter block from caller */
190 copy_from_user ( ¶ms, uparams, 0, sizeof ( params ) );
192 /* Fill in ROM segment address */
193 ppxe.UNDIROMID.segment = ix86->segs.ds;
195 /* Set default status in case child routine fails to do so */
196 params.Status = PXENV_STATUS_FAILURE;
198 /* Call UNDI loader */
199 ret = undi_loader ( ¶ms );
201 /* Copy modified parameter block back to caller and return */
202 copy_to_user ( uparams, 0, ¶ms, sizeof ( params ) );
207 * Calculate byte checksum as used by PXE
210 * @v size Length of data
213 static uint8_t pxe_checksum ( void *data, size_t size ) {
214 uint8_t *bytes = data;
224 * Initialise !PXE and PXENV+ structures
227 static void pxe_init_structures ( void ) {
228 uint32_t rm_cs_phys = ( rm_cs << 4 );
229 uint32_t rm_ds_phys = ( rm_ds << 4 );
231 /* Fill in missing segment fields */
232 ppxe.EntryPointSP.segment = rm_cs;
233 ppxe.EntryPointESP.segment = rm_cs;
234 ppxe.Stack.segment_address = rm_ds;
235 ppxe.Stack.Physical_address = rm_ds_phys;
236 ppxe.UNDIData.segment_address = rm_ds;
237 ppxe.UNDIData.Physical_address = rm_ds_phys;
238 ppxe.UNDICode.segment_address = rm_cs;
239 ppxe.UNDICode.Physical_address = rm_cs_phys;
240 ppxe.UNDICodeWrite.segment_address = rm_cs;
241 ppxe.UNDICodeWrite.Physical_address = rm_cs_phys;
242 pxenv.RMEntry.segment = rm_cs;
243 pxenv.StackSeg = rm_ds;
244 pxenv.UNDIDataSeg = rm_ds;
245 pxenv.UNDICodeSeg = rm_cs;
246 pxenv.PXEPtr.segment = rm_cs;
248 /* Update checksums */
249 ppxe.StructCksum -= pxe_checksum ( &ppxe, sizeof ( ppxe ) );
250 pxenv.Checksum -= pxe_checksum ( &pxenv, sizeof ( pxenv ) );
253 /** PXE structure initialiser */
254 struct init_fn pxe_init_fn __init_fn ( INIT_NORMAL ) = {
255 .initialise = pxe_init_structures,
261 * @v netdev Net device to use as PXE net device
263 void pxe_activate ( struct net_device *netdev ) {
265 /* Ensure INT 1A is hooked */
266 if ( ! int_1a_hooked ) {
267 hook_bios_interrupt ( 0x1a, ( unsigned int ) pxe_int_1a,
268 &pxe_int_1a_vector );
273 /* Set PXE network device */
274 pxe_set_netdev ( netdev );
278 * Deactivate PXE stack
280 * @ret rc Return status code
282 int pxe_deactivate ( void ) {
285 /* Clear PXE network device */
286 pxe_set_netdev ( NULL );
288 /* Ensure INT 1A is unhooked, if possible */
289 if ( int_1a_hooked ) {
290 if ( ( rc = unhook_bios_interrupt ( 0x1a,
291 (unsigned int) pxe_int_1a,
292 &pxe_int_1a_vector ))!= 0){
293 DBG ( "Could not unhook INT 1A: %s\n",
304 /** Jump buffer for PXENV_RESTART_TFTP */
305 rmjmp_buf pxe_restart_nbp;
308 * Start PXE NBP at 0000:7c00
310 * @ret rc Return status code
312 int pxe_start_nbp ( void ) {
314 int discard_b, discard_c, discard_d, discard_D;
317 /* Allow restarting NBP via PXENV_RESTART_TFTP */
318 jmp = rmsetjmp ( pxe_restart_nbp );
320 DBG ( "Restarting NBP (%x)\n", jmp );
322 /* Far call to PXE NBP */
323 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
324 "movw %%cx, %%es\n\t"
328 "lcall $0, $0x7c00\n\t"
329 "popl %%ebp\n\t" /* discard */
330 "popl %%ebp\n\t" /* gcc bug */ )
331 : "=a" ( status ), "=b" ( discard_b ),
332 "=c" ( discard_c ), "=d" ( discard_d ),
334 : "a" ( 0 ), "b" ( __from_text16 ( &pxenv ) ),
336 "d" ( virt_to_phys ( &pxenv ) ),
337 "D" ( __from_text16 ( &ppxe ) )
340 return -EPXENBP ( status );
345 REQUIRE_OBJECT ( pxe_preboot );
346 REQUIRE_OBJECT ( pxe_undi );
347 REQUIRE_OBJECT ( pxe_udp );
348 REQUIRE_OBJECT ( pxe_tftp );
349 REQUIRE_OBJECT ( pxe_file );