7 /* PXE API interface for Etherboot.
9 * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * You can also choose to distribute this program under the terms of
27 * the Unmodified Binary Distribution Licence (as given in the file
28 * COPYING.UBDL), provided that you have satisfied its requirements.
31 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 #include <ipxe/uaccess.h>
38 #include <ipxe/dhcp.h>
39 #include <ipxe/fakedhcp.h>
40 #include <ipxe/device.h>
41 #include <ipxe/netdevice.h>
42 #include <ipxe/isapnp.h>
43 #include <ipxe/init.h>
44 #include <ipxe/if_ether.h>
45 #include <basemem_packet.h>
50 /* Avoid dragging in isapnp.o unnecessarily */
51 uint16_t isapnp_read_port;
53 /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
54 enum pxe_cached_info_indices {
55 CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
56 CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
57 CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
61 /** A cached DHCP packet */
62 union pxe_cached_info {
63 struct dhcphdr dhcphdr;
64 /* This buffer must be *exactly* the size of a BOOTPLAYER_t
65 * structure, otherwise WinPE will die horribly. It takes the
66 * size of *our* buffer and feeds it in to us as the size of
67 * one of *its* buffers. If our buffer is larger than it
68 * expects, we therefore end up overwriting part of its data
69 * segment, since it tells us to do so. (D'oh!)
71 * Note that a BOOTPLAYER_t is not necessarily large enough to
72 * hold a DHCP packet; this is a flaw in the PXE spec.
75 } __attribute__ (( packed ));
77 /** A PXE DHCP packet creator */
78 struct pxe_dhcp_packet_creator {
79 /** Create DHCP packet
81 * @v netdev Network device
82 * @v data Buffer for DHCP packet
83 * @v max_len Size of DHCP packet buffer
84 * @ret rc Return status code
86 int ( * create ) ( struct net_device *netdev, void *data,
90 /** PXE DHCP packet creators */
91 static struct pxe_dhcp_packet_creator pxe_dhcp_packet_creators[] = {
92 [CACHED_INFO_DHCPDISCOVER] = { create_fakedhcpdiscover },
93 [CACHED_INFO_DHCPACK] = { create_fakedhcpack },
94 [CACHED_INFO_BINL] = { create_fakepxebsack },
98 * Name PXENV_GET_CACHED_INFO packet type
100 * @v packet_type Packet type
101 * @ret name Name of packet type
103 static inline __attribute__ (( always_inline )) const char *
104 pxenv_get_cached_info_name ( int packet_type ) {
105 switch ( packet_type ) {
106 case PXENV_PACKET_TYPE_DHCP_DISCOVER:
107 return "DHCPDISCOVER";
108 case PXENV_PACKET_TYPE_DHCP_ACK:
110 case PXENV_PACKET_TYPE_CACHED_REPLY:
117 /* The case in which the caller doesn't supply a buffer is really
118 * awkward to support given that we have multiple sources of options,
119 * and that we don't actually store the DHCP packets. (We may not
120 * even have performed DHCP; we may have obtained all configuration
121 * from non-volatile stored options or from the command line.)
123 * Some NBPs rely on the buffers we provide being persistent, so we
124 * can't just use the temporary packet buffer. 4.5kB of base memory
125 * always wasted just because some clients are too lazy to provide
126 * their own buffers...
128 static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
129 #define cached_info __use_data16 ( cached_info )
132 * UNLOAD BASE CODE STACK
139 pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
140 DBGC ( &pxe_netdev, "PXENV_UNLOAD_STACK\n" );
142 unload_stack->Status = PXENV_STATUS_SUCCESS;
143 return PXENV_EXIT_SUCCESS;
146 /* PXENV_GET_CACHED_INFO
151 pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO *get_cached_info ) {
152 struct pxe_dhcp_packet_creator *creator;
153 union pxe_cached_info *info;
159 DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO %s to %04x:%04x+%x",
160 pxenv_get_cached_info_name ( get_cached_info->PacketType ),
161 get_cached_info->Buffer.segment,
162 get_cached_info->Buffer.offset, get_cached_info->BufferSize );
165 if ( ! pxe_netdev ) {
166 DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO called with no "
167 "network device\n" );
168 get_cached_info->Status = PXENV_STATUS_UNDI_INVALID_STATE;
169 return PXENV_EXIT_FAILURE;
173 idx = ( get_cached_info->PacketType - 1 );
174 if ( idx >= NUM_CACHED_INFOS ) {
175 DBGC ( &pxe_netdev, " bad PacketType %d\n",
176 get_cached_info->PacketType );
179 info = &cached_info[idx];
181 /* Construct DHCP packet */
182 creator = &pxe_dhcp_packet_creators[idx];
183 if ( ( rc = creator->create ( pxe_netdev, info,
184 sizeof ( *info ) ) ) != 0 ) {
185 DBGC ( &pxe_netdev, " failed to build packet: %s\n",
190 /* Copy packet (if applicable) */
191 len = get_cached_info->BufferSize;
193 /* Point client at our cached buffer.
195 * To add to the fun, Intel decided at some point in
196 * the evolution of the PXE specification to add the
197 * BufferLimit field, which we are meant to fill in
198 * with the length of our packet buffer, so that the
199 * caller can safely modify the boot server reply
200 * packet stored therein. However, this field was not
201 * present in earlier versions of the PXE spec, and
202 * there is at least one PXE NBP (Altiris) which
203 * allocates only exactly enough space for this
204 * earlier, shorter version of the structure. If we
205 * actually fill in the BufferLimit field, we
206 * therefore risk trashing random areas of the
207 * caller's memory. If we *don't* fill it in, then
208 * the caller is at liberty to assume that whatever
209 * random value happened to be in that location
210 * represents the length of the buffer we've just
213 * Since older PXE stacks won't fill this field in
214 * anyway, it's probably safe to assume that no
215 * callers actually rely on it, so we choose to not
218 get_cached_info->Buffer.segment = rm_ds;
219 get_cached_info->Buffer.offset = __from_data16 ( info );
220 get_cached_info->BufferSize = sizeof ( *info );
221 DBGC ( &pxe_netdev, " using %04x:%04x+%04x['%x']",
222 get_cached_info->Buffer.segment,
223 get_cached_info->Buffer.offset,
224 get_cached_info->BufferSize,
225 get_cached_info->BufferLimit );
227 /* Copy packet to client buffer */
228 if ( len > sizeof ( *info ) )
229 len = sizeof ( *info );
230 if ( len < sizeof ( *info ) )
231 DBGC ( &pxe_netdev, " buffer may be too short" );
232 buffer = real_to_user ( get_cached_info->Buffer.segment,
233 get_cached_info->Buffer.offset );
234 copy_to_user ( buffer, 0, info, len );
235 get_cached_info->BufferSize = len;
238 DBGC ( &pxe_netdev, "\n" );
239 get_cached_info->Status = PXENV_STATUS_SUCCESS;
240 return PXENV_EXIT_SUCCESS;
243 get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
244 return PXENV_EXIT_FAILURE;
247 /* PXENV_RESTART_TFTP
252 pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE *restart_tftp ) {
253 PXENV_EXIT_t tftp_exit;
255 DBGC ( &pxe_netdev, "PXENV_RESTART_TFTP\n" );
257 /* Words cannot describe the complete mismatch between the PXE
258 * specification and any possible version of reality...
260 restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
261 restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
262 tftp_exit = pxenv_tftp_read_file ( restart_tftp );
263 if ( tftp_exit != PXENV_EXIT_SUCCESS )
267 rmlongjmp ( pxe_restart_nbp, PXENV_RESTART_TFTP );
274 static PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
275 unsigned int bus_type;
276 unsigned int location;
277 struct net_device *netdev;
279 DBGC ( &pxe_netdev, "PXENV_START_UNDI %04x:%04x:%04x\n",
280 start_undi->AX, start_undi->BX, start_undi->DX );
282 /* Determine bus type and location. Use a heuristic to decide
283 * whether we are PCI or ISAPnP
285 if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
286 ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
287 ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
288 ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
289 bus_type = BUS_TYPE_ISAPNP;
290 location = start_undi->BX;
291 /* Record ISAPnP read port for use by isapnp.c */
292 isapnp_read_port = start_undi->DX;
294 bus_type = BUS_TYPE_PCI;
295 location = start_undi->AX;
298 /* Probe for devices, etc. */
301 /* Look for a matching net device */
302 netdev = find_netdev_by_location ( bus_type, location );
304 DBGC ( &pxe_netdev, "PXENV_START_UNDI could not find matching "
306 start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
307 return PXENV_EXIT_FAILURE;
309 DBGC ( &pxe_netdev, "PXENV_START_UNDI found net device %s\n",
313 pxe_activate ( netdev );
315 start_undi->Status = PXENV_STATUS_SUCCESS;
316 return PXENV_EXIT_SUCCESS;
323 static PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
324 DBGC ( &pxe_netdev, "PXENV_STOP_UNDI\n" );
329 /* Prepare for unload */
332 /* Check to see if we still have any hooked interrupts */
333 if ( hooked_bios_interrupts != 0 ) {
334 DBGC ( &pxe_netdev, "PXENV_STOP_UNDI failed: %d interrupts "
335 "still hooked\n", hooked_bios_interrupts );
336 stop_undi->Status = PXENV_STATUS_KEEP_UNDI;
337 return PXENV_EXIT_FAILURE;
340 stop_undi->Status = PXENV_STATUS_SUCCESS;
341 return PXENV_EXIT_SUCCESS;
346 * Status: won't implement (requires major structural changes)
348 static PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
349 DBGC ( &pxe_netdev, "PXENV_START_BASE\n" );
351 start_base->Status = PXENV_STATUS_UNSUPPORTED;
352 return PXENV_EXIT_FAILURE;
359 static PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
360 DBGC ( &pxe_netdev, "PXENV_STOP_BASE\n" );
362 /* The only time we will be called is when the NBP is trying
363 * to shut down the PXE stack. There's nothing we need to do
367 stop_base->Status = PXENV_STATUS_SUCCESS;
368 return PXENV_EXIT_SUCCESS;
371 /** PXE preboot API */
372 struct pxe_api_call pxe_preboot_api[] __pxe_api_call = {
373 PXE_API_CALL ( PXENV_UNLOAD_STACK, pxenv_unload_stack,
374 struct s_PXENV_UNLOAD_STACK ),
375 PXE_API_CALL ( PXENV_GET_CACHED_INFO, pxenv_get_cached_info,
376 struct s_PXENV_GET_CACHED_INFO ),
377 PXE_API_CALL ( PXENV_RESTART_TFTP, pxenv_restart_tftp,
378 struct s_PXENV_TFTP_READ_FILE ),
379 PXE_API_CALL ( PXENV_START_UNDI, pxenv_start_undi,
380 struct s_PXENV_START_UNDI ),
381 PXE_API_CALL ( PXENV_STOP_UNDI, pxenv_stop_undi,
382 struct s_PXENV_STOP_UNDI ),
383 PXE_API_CALL ( PXENV_START_BASE, pxenv_start_base,
384 struct s_PXENV_START_BASE ),
385 PXE_API_CALL ( PXENV_STOP_BASE, pxenv_stop_base,
386 struct s_PXENV_STOP_BASE ),