iPXE
pxe_call.c
Go to the documentation of this file.
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 <rmsetjmp.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 /** Real-mode code segment size */
58 extern char _text16_memsz[];
59 #define _text16_memsz ( ( size_t ) _text16_memsz )
60 
61 /** Real-mode data segment size */
62 extern char _data16_memsz[];
63 #define _data16_memsz ( ( size_t ) _data16_memsz )
64 
65 /** PXENV_UNDI_TRANSMIT API call profiler */
66 static struct profiler pxe_api_tx_profiler __profiler =
67  { .name = "pxeapi.tx" };
68 
69 /** PXENV_UNDI_ISR API call profiler */
70 static struct profiler pxe_api_isr_profiler __profiler =
71  { .name = "pxeapi.isr" };
72 
73 /** PXE unknown API call profiler
74  *
75  * This profiler can be used to measure the overhead of a dummy PXE
76  * API call.
77  */
78 static struct profiler pxe_api_unknown_profiler __profiler =
79  { .name = "pxeapi.unknown" };
80 
81 /** Miscellaneous PXE API call profiler */
82 static struct profiler pxe_api_misc_profiler __profiler =
83  { .name = "pxeapi.misc" };
84 
85 /**
86  * Handle an unknown PXE API call
87  *
88  * @v pxenv_unknown Pointer to a struct s_PXENV_UNKNOWN
89  * @ret #PXENV_EXIT_FAILURE Always
90  * @err #PXENV_STATUS_UNSUPPORTED Always
91  */
94  return PXENV_EXIT_FAILURE;
95 }
96 
97 /** Unknown PXE API call list */
98 struct pxe_api_call pxenv_unknown_api __pxe_api_call =
100 
101 /**
102  * Locate PXE API call
103  *
104  * @v opcode Opcode
105  * @ret call PXE API call, or NULL
106  */
108  struct pxe_api_call *call;
109 
111  if ( call->opcode == opcode )
112  return call;
113  }
114  return NULL;
115 }
116 
117 /**
118  * Determine applicable profiler (for debugging)
119  *
120  * @v opcode PXE opcode
121  * @ret profiler Profiler
122  */
123 static struct profiler * pxe_api_profiler ( unsigned int opcode ) {
124 
125  /* Determine applicable profiler */
126  switch ( opcode ) {
127  case PXENV_UNDI_TRANSMIT:
128  return &pxe_api_tx_profiler;
129  case PXENV_UNDI_ISR:
130  return &pxe_api_isr_profiler;
131  case PXENV_UNKNOWN:
132  return &pxe_api_unknown_profiler;
133  default:
134  return &pxe_api_misc_profiler;
135  }
136 }
137 
138 /**
139  * Dispatch PXE API call
140  *
141  * @v bx PXE opcode
142  * @v es:di Address of PXE parameter block
143  * @ret ax PXE exit code
144  */
145 __asmcall void pxe_api_call ( struct i386_all_regs *ix86 ) {
146  uint16_t opcode = ix86->regs.bx;
147  userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
149  struct pxe_api_call *call;
150  union u_PXENV_ANY params;
151  PXENV_EXIT_t ret;
152 
153  /* Start profiling */
155 
156  /* Locate API call */
157  call = find_pxe_api_call ( opcode );
158  if ( ! call ) {
159  DBGC ( &pxe_netdev, "PXENV_UNKNOWN_%04x\n", opcode );
160  call = &pxenv_unknown_api;
161  }
162 
163  /* Copy parameter block from caller */
164  copy_from_user ( &params, uparams, 0, call->params_len );
165 
166  /* Set default status in case child routine fails to do so */
167  params.Status = PXENV_STATUS_FAILURE;
168 
169  /* Hand off to relevant API routine */
170  ret = call->entry ( &params );
171 
172  /* Copy modified parameter block back to caller and return */
173  copy_to_user ( uparams, 0, &params, call->params_len );
174  ix86->regs.ax = ret;
175 
176  /* Stop profiling, if applicable */
178 }
179 
180 /**
181  * Dispatch weak PXE API call with PXE stack available
182  *
183  * @v ix86 Registers for PXE call
184  * @ret present Zero (PXE stack present)
185  */
186 int pxe_api_call_weak ( struct i386_all_regs *ix86 ) {
187  pxe_api_call ( ix86 );
188  return 0;
189 }
190 
191 /**
192  * Dispatch PXE loader call
193  *
194  * @v es:di Address of PXE parameter block
195  * @ret ax PXE exit code
196  */
197 __asmcall void pxe_loader_call ( struct i386_all_regs *ix86 ) {
198  userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
199  struct s_UNDI_LOADER params;
200  PXENV_EXIT_t ret;
201 
202  /* Copy parameter block from caller */
203  copy_from_user ( &params, uparams, 0, sizeof ( params ) );
204 
205  /* Fill in ROM segment address */
206  ppxe.UNDIROMID.segment = ix86->segs.ds;
207 
208  /* Set default status in case child routine fails to do so */
209  params.Status = PXENV_STATUS_FAILURE;
210 
211  /* Call UNDI loader */
212  ret = undi_loader ( &params );
213 
214  /* Copy modified parameter block back to caller and return */
215  copy_to_user ( uparams, 0, &params, sizeof ( params ) );
216  ix86->regs.ax = ret;
217 }
218 
219 /**
220  * Calculate byte checksum as used by PXE
221  *
222  * @v data Data
223  * @v size Length of data
224  * @ret sum Checksum
225  */
226 static uint8_t pxe_checksum ( void *data, size_t size ) {
227  uint8_t *bytes = data;
228  uint8_t sum = 0;
229 
230  while ( size-- ) {
231  sum += *bytes++;
232  }
233  return sum;
234 }
235 
236 /**
237  * Initialise !PXE and PXENV+ structures
238  *
239  */
240 static void pxe_init_structures ( void ) {
241  uint32_t rm_cs_phys = ( rm_cs << 4 );
242  uint32_t rm_ds_phys = ( rm_ds << 4 );
243 
244  /* Fill in missing segment fields */
245  ppxe.EntryPointSP.segment = rm_cs;
246  ppxe.EntryPointESP.segment = rm_cs;
247  ppxe.Stack.segment_address = rm_ds;
248  ppxe.Stack.Physical_address = rm_ds_phys;
249  ppxe.UNDIData.segment_address = rm_ds;
250  ppxe.UNDIData.Physical_address = rm_ds_phys;
251  ppxe.UNDICode.segment_address = rm_cs;
252  ppxe.UNDICode.Physical_address = rm_cs_phys;
253  ppxe.UNDICodeWrite.segment_address = rm_cs;
254  ppxe.UNDICodeWrite.Physical_address = rm_cs_phys;
255  pxenv.RMEntry.segment = rm_cs;
256  pxenv.StackSeg = rm_ds;
257  pxenv.UNDIDataSeg = rm_ds;
258  pxenv.UNDICodeSeg = rm_cs;
259  pxenv.PXEPtr.segment = rm_cs;
260 
261  /* Update checksums */
262  ppxe.StructCksum -= pxe_checksum ( &ppxe, sizeof ( ppxe ) );
263  pxenv.Checksum -= pxe_checksum ( &pxenv, sizeof ( pxenv ) );
264 }
265 
266 /** PXE structure initialiser */
267 struct init_fn pxe_init_fn __init_fn ( INIT_NORMAL ) = {
269 };
270 
271 /**
272  * Activate PXE stack
273  *
274  * @v netdev Net device to use as PXE net device
275  */
276 void pxe_activate ( struct net_device *netdev ) {
277  uint32_t discard_a;
278  uint32_t discard_b;
279  uint32_t discard_d;
280 
281  /* Ensure INT 1A is hooked */
282  if ( ! int_1a_hooked ) {
285  devices_get();
286  int_1a_hooked = 1;
287  }
288 
289  /* Set PXE network device */
291 
292  /* Notify BIOS of installation */
293  __asm__ __volatile__ ( REAL_CODE ( "pushw %%cs\n\t"
294  "popw %%es\n\t"
295  "int $0x1a\n\t" )
296  : "=a" ( discard_a ), "=b" ( discard_b ),
297  "=d" ( discard_d )
298  : "0" ( 0x564e ),
299  "1" ( __from_text16 ( &pxenv ) ) );
300 }
301 
302 /**
303  * Deactivate PXE stack
304  *
305  * @ret rc Return status code
306  */
307 int pxe_deactivate ( void ) {
308  int rc;
309 
310  /* Clear PXE network device */
311  pxe_set_netdev ( NULL );
312 
313  /* Ensure INT 1A is unhooked, if possible */
314  if ( int_1a_hooked ) {
315  if ( ( rc = unhook_bios_interrupt ( 0x1a,
316  ( intptr_t ) pxe_int_1a,
317  &pxe_int_1a_vector ))!= 0){
318  DBGC ( &pxe_netdev, "PXE could not unhook INT 1A: %s\n",
319  strerror ( rc ) );
320  return rc;
321  }
322  devices_put();
323  int_1a_hooked = 0;
324  }
325 
326  return 0;
327 }
328 
329 /** Jump buffer for PXENV_RESTART_TFTP */
331 
332 /**
333  * Start PXE NBP at 0000:7c00
334  *
335  * @ret rc Return status code
336  */
337 int pxe_start_nbp ( void ) {
338  int jmp;
339  int discard_b, discard_c, discard_d, discard_D;
341 
342  DBGC ( &pxe_netdev, "PXE NBP starting with netdev %s, code %04x:%04zx, "
343  "data %04x:%04zx\n", ( pxe_netdev ? pxe_netdev->name : "<none>"),
345 
346  /* Allow restarting NBP via PXENV_RESTART_TFTP */
348  if ( jmp )
349  DBGC ( &pxe_netdev, "PXE NBP restarting (%x)\n", jmp );
350 
351  /* Far call to PXE NBP */
352  __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
353  "movw %%cx, %%es\n\t"
354  "pushw %%es\n\t"
355  "pushw %%di\n\t"
356  "sti\n\t"
357  "lcall $0, $0x7c00\n\t"
358  "popl %%ebp\n\t" /* discard */
359  "popl %%ebp\n\t" /* gcc bug */ )
360  : "=a" ( status ), "=b" ( discard_b ),
361  "=c" ( discard_c ), "=d" ( discard_d ),
362  "=D" ( discard_D )
363  : "a" ( 0 ), "b" ( __from_text16 ( &pxenv ) ),
364  "c" ( rm_cs ),
365  "d" ( virt_to_phys ( &pxenv ) ),
366  "D" ( __from_text16 ( &ppxe ) )
367  : "esi", "memory" );
368  if ( status )
369  return -EPXENBP ( status );
370 
371  return 0;
372 }
373 
374 /**
375  * Notify BIOS of existence of network device
376  *
377  * @v netdev Network device
378  * @v priv Private data
379  * @ret rc Return status code
380  */
381 static int pxe_notify ( struct net_device *netdev, void *priv __unused ) {
382 
383  /* Do nothing if we already have a network device */
384  if ( pxe_netdev )
385  return 0;
386 
387  /* Activate (and deactivate) PXE stack to notify BIOS */
388  pxe_activate ( netdev );
389  pxe_deactivate();
390 
391  return 0;
392 }
393 
394 /** PXE BIOS notification driver */
395 struct net_driver pxe_driver __net_driver = {
396  .name = "PXE",
397  .probe = pxe_notify,
398 };
399 
401 REQUIRE_OBJECT ( pxe_preboot );
402 REQUIRE_OBJECT ( pxe_undi );
405 REQUIRE_OBJECT ( pxe_file );
void pxe_activate(struct net_device *netdev)
Activate PXE stack.
Definition: pxe_call.c:276
static PXENV_EXIT_t pxenv_unknown(struct s_PXENV_UNKNOWN *pxenv_unknown)
Handle an unknown PXE API call.
Definition: pxe_call.c:92
struct init_fn pxe_init_fn __init_fn(INIT_NORMAL)
PXE structure initialiser.
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
uint8_t jmp
"jmp" instruction
Definition: librm.h:256
struct i386_seg_regs segs
Definition: registers.h:175
static void devices_put(void)
Allow devices to be removed on shutdown.
Definition: device.h:169
#define PXENV_EXIT_FAILURE
An error occurred.
Definition: pxe_types.h:46
void(* initialise)(void)
Definition: init.h:15
PXENV_STATUS_t Status
Definition: pxe.h:29
uint8_t opcode
Opcode.
Definition: ena.h:16
uint16_t es
Definition: registers.h:142
#define ppxe
Definition: pxe_call.h:28
#define __from_text16(pointer)
Definition: libkir.h:23
void pxe_int_1a(void)
INT 1A handler.
struct net_driver pxe_driver __net_driver
PXE BIOS notification driver.
Definition: pxe_call.c:395
static struct profiler pxe_api_tx_profiler __profiler
PXENV_UNDI_TRANSMIT API call profiler.
Definition: pxe_call.c:66
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
#define DBGC(...)
Definition: compiler.h:505
i386 registers.
PXE API entry point.
REQUIRING_SYMBOL(pxe_api_call)
A data structure for storing profiling information.
Definition: profile.h:26
#define rm_ds
Definition: libkir.h:39
unsigned long intptr_t
Definition: stdint.h:21
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
A full register dump.
Definition: registers.h:174
struct segoff __text16(pxe_int_1a_vector)
Vector for chaining INT 1A.
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
Access to external ("user") memory.
A network upper-layer driver.
Definition: netdevice.h:473
#define PXENV_UNKNOWN
PXE API invalid function code.
Definition: pxe.h:13
#define _data16_memsz
Definition: pxe_call.c:63
#define PXE_API_CALL(_opcode, _entry, _params_type)
Define a PXE API call.
Definition: pxe.h:108
void hook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Hook INT vector.
Definition: biosint.c:24
uint8_t status
Status.
Definition: ena.h:16
int unhook_bios_interrupt(unsigned int interrupt, unsigned int handler, struct segoff *chain_vector)
Unhook INT vector.
Definition: biosint.c:69
static struct profiler * pxe_api_profiler(unsigned int opcode)
Determine applicable profiler (for debugging)
Definition: pxe_call.c:123
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:30
const char * name
Name.
Definition: netdevice.h:475
UINT16_t PXENV_EXIT_t
A PXE exit code.
Definition: pxe_types.h:44
const char * name
Name.
Definition: profile.h:28
#define __asmcall
Declare a function with standard calling conventions.
Definition: compiler.h:15
An initialisation function.
Definition: init.h:14
static int int_1a_hooked
INT 1A hooked flag.
Definition: pxe_call.c:55
int pxe_deactivate(void)
Deactivate PXE stack.
Definition: pxe_call.c:307
struct i386_regs regs
Definition: registers.h:176
static void devices_get(void)
Prevent devices from being removed on shutdown.
Definition: device.h:161
#define PXE_API_CALLS
PXE API call table.
Definition: pxe.h:95
#define pxe_int_1a_vector
Definition: pxe_call.c:49
static struct net_device * netdev
Definition: gdbudp.c:52
void pxe_set_netdev(struct net_device *netdev)
Set network device as current PXE network device.
Definition: pxe_undi.c:69
static struct pxe_tftp_connection pxe_tftp
The PXE TFTP connection.
Definition: pxe_tftp.c:158
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
Profiling.
uint16_t bx
Definition: registers.h:84
#define PXENV_UNDI_TRANSMIT
PXE API function code for pxenv_undi_transmit()
Definition: pxe_api.h:1009
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define _text16_memsz
Definition: pxe_call.c:59
struct net_device * pxe_netdev
Definition: pxe_undi.c:59
int pxe_start_nbp(void)
Start PXE NBP at 0000:7c00.
Definition: pxe_call.c:337
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
A network device.
Definition: netdevice.h:352
uint16_t ds
Definition: registers.h:141
unsigned char uint8_t
Definition: stdint.h:10
#define PXENV_STATUS_UNSUPPORTED
Definition: pxe_error.h:22
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
void * discard_D
Definition: bigint.h:31
uint16_t opcode
Opcode.
Definition: pxe.h:91
unsigned int uint32_t
Definition: stdint.h:12
REQUIRE_OBJECT(pxe_preboot)
__asmcall void pxe_loader_call(struct i386_all_regs *ix86)
Dispatch PXE loader call.
Definition: pxe_call.c:197
#define undi_loader
Definition: undiload.c:53
Network device management.
static uint8_t pxe_checksum(void *data, size_t size)
Calculate byte checksum as used by PXE.
Definition: pxe_call.c:226
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
uint16_t params_len
Length of parameters.
Definition: pxe.h:89
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
static void pxe_init_structures(void)
Initialise !PXE and PXENV+ structures.
Definition: pxe_call.c:240
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
int pxe_api_call_weak(struct i386_all_regs *ix86)
Dispatch weak PXE API call with PXE stack available.
Definition: pxe_call.c:186
A PXE API call.
Definition: pxe.h:81
uint16_t di
Definition: registers.h:64
static struct tlan_private * priv
Definition: tlan.c:224
uint16_t ax
Definition: registers.h:108
Parameter block for undi_loader()
Definition: pxe_api.h:1729
#define PXENV_UNDI_ISR
PXE API function code for pxenv_undi_isr()
Definition: pxe_api.h:1450
static struct pxe_api_call * find_pxe_api_call(uint16_t opcode)
Locate PXE API call.
Definition: pxe_call.c:107
#define rmsetjmp(_env)
Definition: rmsetjmp.h:17
PXENV_STATUS_t Status
PXE status code.
Definition: pxe_api.h:1731
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint8_t size
Entry size (in 32-bit words)
Definition: ena.h:16
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define rm_cs
Definition: libkir.h:38
static struct pxe_udp_connection pxe_udp
The PXE UDP connection.
Definition: pxe_udp.c:126
static __always_inline userptr_t real_to_user(unsigned int segment, unsigned int offset)
Convert segment:offset address to user buffer.
Definition: realmode.h:75
long discard_c
Definition: bigint.h:32
PXENV_EXIT_t(* entry)(union u_PXENV_ANY *params)
Entry point.
Definition: pxe.h:87
Parameter block for pxenv_unknown()
Definition: pxe.h:16
struct pxe_api_call pxenv_unknown_api __pxe_api_call
Unknown PXE API call list.
Definition: pxe_call.c:98
#define pxenv
Definition: pxe_call.h:32
#define EPXENBP(status)
Definition: pxe_call.c:45
static int pxe_notify(struct net_device *netdev, void *priv __unused)
Notify BIOS of existence of network device.
Definition: pxe_call.c:381
uint8_t bytes[64]
Definition: ib_mad.h:16
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
rmjmp_buf pxe_restart_nbp
Jump buffer for PXENV_RESTART_TFTP.
Definition: pxe_call.c:330
A real-mode-extended jump buffer.
Definition: rmsetjmp.h:10
#define PXENV_STATUS_FAILURE
Definition: pxe_error.h:20
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33
__asmcall void pxe_api_call(struct i386_all_regs *ix86)
Dispatch PXE API call.
Definition: pxe_call.c:145