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
27 FILE_LICENCE ( GPL2_OR_LATER );
33 #include <ipxe/uaccess.h>
34 #include <ipxe/dhcp.h>
35 #include <ipxe/fakedhcp.h>
36 #include <ipxe/device.h>
37 #include <ipxe/netdevice.h>
38 #include <ipxe/isapnp.h>
39 #include <ipxe/init.h>
40 #include <ipxe/if_ether.h>
41 #include <basemem_packet.h>
46 /* Avoid dragging in isapnp.o unnecessarily */
47 uint16_t isapnp_read_port;
49 /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
50 enum pxe_cached_info_indices {
51 CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
52 CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
53 CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
57 /** A cached DHCP packet */
58 union pxe_cached_info {
59 struct dhcphdr dhcphdr;
60 /* This buffer must be *exactly* the size of a BOOTPLAYER_t
61 * structure, otherwise WinPE will die horribly. It takes the
62 * size of *our* buffer and feeds it in to us as the size of
63 * one of *its* buffers. If our buffer is larger than it
64 * expects, we therefore end up overwriting part of its data
65 * segment, since it tells us to do so. (D'oh!)
67 * Note that a BOOTPLAYER_t is not necessarily large enough to
68 * hold a DHCP packet; this is a flaw in the PXE spec.
71 } __attribute__ (( packed ));
73 /** A PXE DHCP packet creator */
74 struct pxe_dhcp_packet_creator {
75 /** Create DHCP packet
77 * @v netdev Network device
78 * @v data Buffer for DHCP packet
79 * @v max_len Size of DHCP packet buffer
80 * @ret rc Return status code
82 int ( * create ) ( struct net_device *netdev, void *data,
86 /** PXE DHCP packet creators */
87 static struct pxe_dhcp_packet_creator pxe_dhcp_packet_creators[] = {
88 [CACHED_INFO_DHCPDISCOVER] = { create_fakedhcpdiscover },
89 [CACHED_INFO_DHCPACK] = { create_fakedhcpack },
90 [CACHED_INFO_BINL] = { create_fakepxebsack },
94 * Name PXENV_GET_CACHED_INFO packet type
96 * @v packet_type Packet type
97 * @ret name Name of packet type
99 static inline __attribute__ (( always_inline )) const char *
100 pxenv_get_cached_info_name ( int packet_type ) {
101 switch ( packet_type ) {
102 case PXENV_PACKET_TYPE_DHCP_DISCOVER:
103 return "DHCPDISCOVER";
104 case PXENV_PACKET_TYPE_DHCP_ACK:
106 case PXENV_PACKET_TYPE_CACHED_REPLY:
113 /* The case in which the caller doesn't supply a buffer is really
114 * awkward to support given that we have multiple sources of options,
115 * and that we don't actually store the DHCP packets. (We may not
116 * even have performed DHCP; we may have obtained all configuration
117 * from non-volatile stored options or from the command line.)
119 * Some NBPs rely on the buffers we provide being persistent, so we
120 * can't just use the temporary packet buffer. 4.5kB of base memory
121 * always wasted just because some clients are too lazy to provide
122 * their own buffers...
124 static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
125 #define cached_info __use_data16 ( cached_info )
128 * UNLOAD BASE CODE STACK
135 pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
136 DBGC ( &pxe_netdev, "PXENV_UNLOAD_STACK\n" );
138 unload_stack->Status = PXENV_STATUS_SUCCESS;
139 return PXENV_EXIT_SUCCESS;
142 /* PXENV_GET_CACHED_INFO
147 pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO *get_cached_info ) {
148 struct pxe_dhcp_packet_creator *creator;
149 union pxe_cached_info *info;
155 DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO %s to %04x:%04x+%x",
156 pxenv_get_cached_info_name ( get_cached_info->PacketType ),
157 get_cached_info->Buffer.segment,
158 get_cached_info->Buffer.offset, get_cached_info->BufferSize );
161 if ( ! pxe_netdev ) {
162 DBGC ( &pxe_netdev, "PXENV_GET_CACHED_INFO called with no "
163 "network device\n" );
164 get_cached_info->Status = PXENV_STATUS_UNDI_INVALID_STATE;
165 return PXENV_EXIT_FAILURE;
169 idx = ( get_cached_info->PacketType - 1 );
170 if ( idx >= NUM_CACHED_INFOS ) {
171 DBGC ( &pxe_netdev, " bad PacketType %d\n",
172 get_cached_info->PacketType );
175 info = &cached_info[idx];
177 /* Construct cached version of packet, if not already constructed. */
178 if ( ! info->dhcphdr.op ) {
179 /* Construct DHCP packet */
180 creator = &pxe_dhcp_packet_creators[idx];
181 if ( ( rc = creator->create ( pxe_netdev, info,
182 sizeof ( *info ) ) ) != 0 ) {
183 DBGC ( &pxe_netdev, " failed to build packet: %s\n",
189 len = get_cached_info->BufferSize;
191 /* Point client at our cached buffer.
193 * To add to the fun, Intel decided at some point in
194 * the evolution of the PXE specification to add the
195 * BufferLimit field, which we are meant to fill in
196 * with the length of our packet buffer, so that the
197 * caller can safely modify the boot server reply
198 * packet stored therein. However, this field was not
199 * present in earlier versions of the PXE spec, and
200 * there is at least one PXE NBP (Altiris) which
201 * allocates only exactly enough space for this
202 * earlier, shorter version of the structure. If we
203 * actually fill in the BufferLimit field, we
204 * therefore risk trashing random areas of the
205 * caller's memory. If we *don't* fill it in, then
206 * the caller is at liberty to assume that whatever
207 * random value happened to be in that location
208 * represents the length of the buffer we've just
211 * Since older PXE stacks won't fill this field in
212 * anyway, it's probably safe to assume that no
213 * callers actually rely on it, so we choose to not
216 get_cached_info->Buffer.segment = rm_ds;
217 get_cached_info->Buffer.offset = __from_data16 ( info );
218 get_cached_info->BufferSize = sizeof ( *info );
219 DBGC ( &pxe_netdev, " using %04x:%04x+%04x['%x']",
220 get_cached_info->Buffer.segment,
221 get_cached_info->Buffer.offset,
222 get_cached_info->BufferSize,
223 get_cached_info->BufferLimit );
225 /* Copy packet to client buffer */
226 if ( len > sizeof ( *info ) )
227 len = sizeof ( *info );
228 if ( len < sizeof ( *info ) )
229 DBGC ( &pxe_netdev, " buffer may be too short" );
230 buffer = real_to_user ( get_cached_info->Buffer.segment,
231 get_cached_info->Buffer.offset );
232 copy_to_user ( buffer, 0, info, len );
233 get_cached_info->BufferSize = len;
236 DBGC ( &pxe_netdev, "\n" );
237 get_cached_info->Status = PXENV_STATUS_SUCCESS;
238 return PXENV_EXIT_SUCCESS;
241 get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
242 return PXENV_EXIT_FAILURE;
245 /* PXENV_RESTART_TFTP
250 pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE *restart_tftp ) {
251 PXENV_EXIT_t tftp_exit;
253 DBGC ( &pxe_netdev, "PXENV_RESTART_TFTP\n" );
255 /* Words cannot describe the complete mismatch between the PXE
256 * specification and any possible version of reality...
258 restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
259 restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
260 tftp_exit = pxenv_tftp_read_file ( restart_tftp );
261 if ( tftp_exit != PXENV_EXIT_SUCCESS )
265 rmlongjmp ( pxe_restart_nbp, PXENV_RESTART_TFTP );
272 static PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
273 unsigned int bus_type;
274 unsigned int location;
275 struct net_device *netdev;
277 DBGC ( &pxe_netdev, "PXENV_START_UNDI %04x:%04x:%04x\n",
278 start_undi->AX, start_undi->BX, start_undi->DX );
280 /* Determine bus type and location. Use a heuristic to decide
281 * whether we are PCI or ISAPnP
283 if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
284 ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
285 ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
286 ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
287 bus_type = BUS_TYPE_ISAPNP;
288 location = start_undi->BX;
289 /* Record ISAPnP read port for use by isapnp.c */
290 isapnp_read_port = start_undi->DX;
292 bus_type = BUS_TYPE_PCI;
293 location = start_undi->AX;
296 /* Probe for devices, etc. */
299 /* Look for a matching net device */
300 netdev = find_netdev_by_location ( bus_type, location );
302 DBGC ( &pxe_netdev, "PXENV_START_UNDI could not find matching "
304 start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
305 return PXENV_EXIT_FAILURE;
307 DBGC ( &pxe_netdev, "PXENV_START_UNDI found net device %s\n",
311 pxe_activate ( netdev );
313 start_undi->Status = PXENV_STATUS_SUCCESS;
314 return PXENV_EXIT_SUCCESS;
321 static PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
322 DBGC ( &pxe_netdev, "PXENV_STOP_UNDI\n" );
327 /* Prepare for unload */
330 /* Check to see if we still have any hooked interrupts */
331 if ( hooked_bios_interrupts != 0 ) {
332 DBGC ( &pxe_netdev, "PXENV_STOP_UNDI failed: %d interrupts "
333 "still hooked\n", hooked_bios_interrupts );
334 stop_undi->Status = PXENV_STATUS_KEEP_UNDI;
335 return PXENV_EXIT_FAILURE;
338 stop_undi->Status = PXENV_STATUS_SUCCESS;
339 return PXENV_EXIT_SUCCESS;
344 * Status: won't implement (requires major structural changes)
346 static PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
347 DBGC ( &pxe_netdev, "PXENV_START_BASE\n" );
349 start_base->Status = PXENV_STATUS_UNSUPPORTED;
350 return PXENV_EXIT_FAILURE;
357 static PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
358 DBGC ( &pxe_netdev, "PXENV_STOP_BASE\n" );
360 /* The only time we will be called is when the NBP is trying
361 * to shut down the PXE stack. There's nothing we need to do
365 stop_base->Status = PXENV_STATUS_SUCCESS;
366 return PXENV_EXIT_SUCCESS;
369 /** PXE preboot API */
370 struct pxe_api_call pxe_preboot_api[] __pxe_api_call = {
371 PXE_API_CALL ( PXENV_UNLOAD_STACK, pxenv_unload_stack,
372 struct s_PXENV_UNLOAD_STACK ),
373 PXE_API_CALL ( PXENV_GET_CACHED_INFO, pxenv_get_cached_info,
374 struct s_PXENV_GET_CACHED_INFO ),
375 PXE_API_CALL ( PXENV_RESTART_TFTP, pxenv_restart_tftp,
376 struct s_PXENV_TFTP_READ_FILE ),
377 PXE_API_CALL ( PXENV_START_UNDI, pxenv_start_undi,
378 struct s_PXENV_START_UNDI ),
379 PXE_API_CALL ( PXENV_STOP_UNDI, pxenv_stop_undi,
380 struct s_PXENV_STOP_UNDI ),
381 PXE_API_CALL ( PXENV_START_BASE, pxenv_start_base,
382 struct s_PXENV_START_BASE ),
383 PXE_API_CALL ( PXENV_STOP_BASE, pxenv_stop_base,
384 struct s_PXENV_STOP_BASE ),