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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <gpxe/uaccess.h>
30 #include <gpxe/dhcp.h>
31 #include <gpxe/device.h>
32 #include <gpxe/netdevice.h>
33 #include <gpxe/isapnp.h>
34 #include <gpxe/init.h>
35 #include <gpxe/if_ether.h>
36 #include <basemem_packet.h>
40 /* Avoid dragging in isapnp.o unnecessarily */
41 uint16_t isapnp_read_port;
43 /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
44 enum pxe_cached_info_indices {
45 CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
46 CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
47 CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
51 /** A cached DHCP packet */
52 union pxe_cached_info {
53 struct dhcphdr dhcphdr;
54 /* This buffer must be *exactly* the size of a BOOTPLAYER_t
55 * structure, otherwise WinPE will die horribly. It takes the
56 * size of *our* buffer and feeds it in to us as the size of
57 * one of *its* buffers. If our buffer is larger than it
58 * expects, we therefore end up overwriting part of its data
59 * segment, since it tells us to do so. (D'oh!)
61 * Note that a BOOTPLAYER_t is not necessarily large enough to
62 * hold a DHCP packet; this is a flaw in the PXE spec.
65 } __attribute__ (( packed ));
67 /* The case in which the caller doesn't supply a buffer is really
68 * awkward to support given that we have multiple sources of options,
69 * and that we don't actually store the DHCP packets. (We may not
70 * even have performed DHCP; we may have obtained all configuration
71 * from non-volatile stored options or from the command line.)
73 * Some NBPs rely on the buffers we provide being persistent, so we
74 * can't just use the temporary packet buffer. 4.5kB of base memory
75 * always wasted just because some clients are too lazy to provide
76 * their own buffers...
78 static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
79 #define cached_info __use_data16 ( cached_info )
82 * Set PXE cached TFTP filename
84 * @v filename TFTP filename
86 * This is a bug-for-bug compatibility hack needed in order to work
87 * with Microsoft Remote Installation Services (RIS). The filename
88 * used in a call to PXENV_RESTART_TFTP or PXENV_TFTP_READ_FILE must
89 * be returned as the DHCP filename in subsequent calls to
90 * PXENV_GET_CACHED_INFO.
92 void pxe_set_cached_filename ( const unsigned char *filename ) {
93 memcpy ( cached_info[CACHED_INFO_DHCPACK].dhcphdr.file, filename,
94 sizeof ( cached_info[CACHED_INFO_DHCPACK].dhcphdr.file ) );
95 memcpy ( cached_info[CACHED_INFO_BINL].dhcphdr.file, filename,
96 sizeof ( cached_info[CACHED_INFO_BINL].dhcphdr.file ) );
100 * UNLOAD BASE CODE STACK
106 PXENV_EXIT_t pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
107 DBG ( "PXENV_UNLOAD_STACK" );
109 unload_stack->Status = PXENV_STATUS_SUCCESS;
110 return PXENV_EXIT_SUCCESS;
113 /* PXENV_GET_CACHED_INFO
117 PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO
119 struct dhcp_packet dhcppkt;
120 int ( * dhcp_packet_creator ) ( struct net_device *, int,
121 struct dhcp_option_block *, void *,
122 size_t, struct dhcp_packet * );
124 unsigned int msgtype;
129 DBG ( "PXENV_GET_CACHED_INFO %d", get_cached_info->PacketType );
131 DBG ( " to %04x:%04x+%x", get_cached_info->Buffer.segment,
132 get_cached_info->Buffer.offset, get_cached_info->BufferSize );
135 idx = ( get_cached_info->PacketType - 1 );
136 if ( idx >= ( sizeof ( cached_info ) / sizeof ( cached_info[0] ) ) ) {
137 DBG ( " bad PacketType" );
141 /* Construct cached version of packet, if not already constructed. */
142 if ( ! cached_info[idx].dhcphdr.op ) {
143 /* Construct DHCP packet */
144 if ( get_cached_info->PacketType ==
145 PXENV_PACKET_TYPE_DHCP_DISCOVER ) {
146 dhcp_packet_creator = create_dhcp_request;
147 msgtype = DHCPDISCOVER;
149 dhcp_packet_creator = create_dhcp_response;
152 if ( ( rc = dhcp_packet_creator ( pxe_netdev, msgtype,
153 NULL, &cached_info[idx],
154 sizeof ( cached_info[idx] ),
155 &dhcppkt ) ) != 0 ) {
156 DBG ( " failed to build packet" );
161 len = get_cached_info->BufferSize;
163 /* Point client at our cached buffer.
165 * To add to the fun, Intel decided at some point in
166 * the evolution of the PXE specification to add the
167 * BufferLimit field, which we are meant to fill in
168 * with the length of our packet buffer, so that the
169 * caller can safely modify the boot server reply
170 * packet stored therein. However, this field was not
171 * present in earlier versions of the PXE spec, and
172 * there is at least one PXE NBP (Altiris) which
173 * allocates only exactly enough space for this
174 * earlier, shorter version of the structure. If we
175 * actually fill in the BufferLimit field, we
176 * therefore risk trashing random areas of the
177 * caller's memory. If we *don't* fill it in, then
178 * the caller is at liberty to assume that whatever
179 * random value happened to be in that location
180 * represents the length of the buffer we've just
183 * Since older PXE stacks won't fill this field in
184 * anyway, it's probably safe to assume that no
185 * callers actually rely on it, so we choose to not
188 get_cached_info->Buffer.segment = rm_ds;
189 get_cached_info->Buffer.offset =
190 ( unsigned ) ( & __from_data16 ( cached_info[idx] ) );
191 get_cached_info->BufferSize = sizeof ( cached_info[idx] );
192 DBG ( " returning %04x:%04x+%04x['%x']",
193 get_cached_info->Buffer.segment,
194 get_cached_info->Buffer.offset,
195 get_cached_info->BufferSize,
196 get_cached_info->BufferLimit );
198 /* Copy packet to client buffer */
199 if ( len > sizeof ( cached_info[idx] ) )
200 len = sizeof ( cached_info[idx] );
201 if ( len < sizeof ( cached_info[idx] ) )
202 DBG ( " buffer may be too short" );
203 buffer = real_to_user ( get_cached_info->Buffer.segment,
204 get_cached_info->Buffer.offset );
205 copy_to_user ( buffer, 0, &cached_info[idx], len );
206 get_cached_info->BufferSize = len;
209 get_cached_info->Status = PXENV_STATUS_SUCCESS;
210 return PXENV_EXIT_SUCCESS;
213 get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
214 return PXENV_EXIT_FAILURE;
217 /* PXENV_RESTART_TFTP
221 PXENV_EXIT_t pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE
223 PXENV_EXIT_t tftp_exit;
225 DBG ( "PXENV_RESTART_TFTP " );
227 /* Intel bug-for-bug hack */
228 pxe_set_cached_filename ( restart_tftp->FileName );
230 /* Words cannot describe the complete mismatch between the PXE
231 * specification and any possible version of reality...
233 restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
234 restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
235 tftp_exit = pxenv_tftp_read_file ( restart_tftp );
236 if ( tftp_exit != PXENV_EXIT_SUCCESS )
239 /* Fire up the new NBP */
240 restart_tftp->Status = pxe_start_nbp();
242 /* Not sure what "SUCCESS" actually means, since we can only
243 * return if the new NBP failed to boot...
245 return PXENV_EXIT_SUCCESS;
252 PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
253 unsigned int bus_type;
254 unsigned int location;
255 struct net_device *netdev;
257 DBG ( "PXENV_START_UNDI %04x:%04x:%04x",
258 start_undi->AX, start_undi->BX, start_undi->DX );
260 /* Determine bus type and location. Use a heuristic to decide
261 * whether we are PCI or ISAPnP
263 if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
264 ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
265 ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
266 ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
267 bus_type = BUS_TYPE_ISAPNP;
268 location = start_undi->BX;
269 /* Record ISAPnP read port for use by isapnp.c */
270 isapnp_read_port = start_undi->DX;
272 bus_type = BUS_TYPE_PCI;
273 location = start_undi->AX;
276 /* Probe for devices, etc. */
279 /* Look for a matching net device */
280 netdev = find_netdev_by_location ( bus_type, location );
282 DBG ( " no net device found" );
283 start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
284 return PXENV_EXIT_FAILURE;
286 DBG ( " using netdev %s", netdev->name );
288 /* Save as PXE net device */
289 pxe_set_netdev ( netdev );
294 start_undi->Status = PXENV_STATUS_SUCCESS;
295 return PXENV_EXIT_SUCCESS;
302 PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
303 DBG ( "PXENV_STOP_UNDI" );
308 /* Clear PXE net device */
309 pxe_set_netdev ( NULL );
311 /* Prepare for unload */
314 stop_undi->Status = PXENV_STATUS_SUCCESS;
315 return PXENV_EXIT_SUCCESS;
320 * Status: won't implement (requires major structural changes)
322 PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
323 DBG ( "PXENV_START_BASE" );
325 start_base->Status = PXENV_STATUS_UNSUPPORTED;
326 return PXENV_EXIT_FAILURE;
333 PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
334 DBG ( "PXENV_STOP_BASE" );
336 /* The only time we will be called is when the NBP is trying
337 * to shut down the PXE stack. There's nothing we need to do
341 stop_base->Status = PXENV_STATUS_SUCCESS;
342 return PXENV_EXIT_SUCCESS;