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/dhcppkt.h>
32 #include <gpxe/device.h>
33 #include <gpxe/netdevice.h>
34 #include <gpxe/isapnp.h>
35 #include <gpxe/init.h>
36 #include <gpxe/if_ether.h>
37 #include <basemem_packet.h>
41 /* Avoid dragging in isapnp.o unnecessarily */
42 uint16_t isapnp_read_port;
44 /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
45 enum pxe_cached_info_indices {
46 CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
47 CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
48 CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
52 /** A cached DHCP packet */
53 union pxe_cached_info {
54 struct dhcphdr dhcphdr;
55 /* This buffer must be *exactly* the size of a BOOTPLAYER_t
56 * structure, otherwise WinPE will die horribly. It takes the
57 * size of *our* buffer and feeds it in to us as the size of
58 * one of *its* buffers. If our buffer is larger than it
59 * expects, we therefore end up overwriting part of its data
60 * segment, since it tells us to do so. (D'oh!)
62 * Note that a BOOTPLAYER_t is not necessarily large enough to
63 * hold a DHCP packet; this is a flaw in the PXE spec.
66 } __attribute__ (( packed ));
68 /** A PXE DHCP packet creator */
69 struct pxe_dhcp_packet_creator {
70 /** Create DHCP packet
72 * @v netdev Network device
73 * @v data Buffer for DHCP packet
74 * @v max_len Size of DHCP packet buffer
75 * @ret rc Return status code
77 int ( * create ) ( struct net_device *netdev, void *data,
81 /** PXE DHCP packet creators */
82 static struct pxe_dhcp_packet_creator pxe_dhcp_packet_creators[] = {
83 [CACHED_INFO_DHCPDISCOVER] = { create_dhcpdiscover },
84 [CACHED_INFO_DHCPACK] = { create_dhcpack },
85 [CACHED_INFO_BINL] = { create_proxydhcpack },
88 /* The case in which the caller doesn't supply a buffer is really
89 * awkward to support given that we have multiple sources of options,
90 * and that we don't actually store the DHCP packets. (We may not
91 * even have performed DHCP; we may have obtained all configuration
92 * from non-volatile stored options or from the command line.)
94 * Some NBPs rely on the buffers we provide being persistent, so we
95 * can't just use the temporary packet buffer. 4.5kB of base memory
96 * always wasted just because some clients are too lazy to provide
97 * their own buffers...
99 static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
100 #define cached_info __use_data16 ( cached_info )
103 * Set PXE cached TFTP filename
105 * @v filename TFTP filename
107 * This is a bug-for-bug compatibility hack needed in order to work
108 * with Microsoft Remote Installation Services (RIS). The filename
109 * used in a call to PXENV_RESTART_TFTP or PXENV_TFTP_READ_FILE must
110 * be returned as the DHCP filename in subsequent calls to
111 * PXENV_GET_CACHED_INFO.
113 void pxe_set_cached_filename ( const unsigned char *filename ) {
114 memcpy ( cached_info[CACHED_INFO_DHCPACK].dhcphdr.file, filename,
115 sizeof ( cached_info[CACHED_INFO_DHCPACK].dhcphdr.file ) );
116 memcpy ( cached_info[CACHED_INFO_BINL].dhcphdr.file, filename,
117 sizeof ( cached_info[CACHED_INFO_BINL].dhcphdr.file ) );
121 * UNLOAD BASE CODE STACK
127 PXENV_EXIT_t pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
128 DBG ( "PXENV_UNLOAD_STACK" );
130 unload_stack->Status = PXENV_STATUS_SUCCESS;
131 return PXENV_EXIT_SUCCESS;
134 /* PXENV_GET_CACHED_INFO
138 PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO
140 struct pxe_dhcp_packet_creator *creator;
141 union pxe_cached_info *info;
147 DBG ( "PXENV_GET_CACHED_INFO %d", get_cached_info->PacketType );
149 DBG ( " to %04x:%04x+%x", get_cached_info->Buffer.segment,
150 get_cached_info->Buffer.offset, get_cached_info->BufferSize );
153 idx = ( get_cached_info->PacketType - 1 );
154 if ( idx >= NUM_CACHED_INFOS ) {
155 DBG ( " bad PacketType" );
158 info = &cached_info[idx];
160 /* Construct cached version of packet, if not already constructed. */
161 if ( ! info->dhcphdr.op ) {
162 /* Construct DHCP packet */
163 creator = &pxe_dhcp_packet_creators[idx];
164 if ( ( rc = creator->create ( pxe_netdev, info,
165 sizeof ( *info ) ) ) != 0 ) {
166 DBG ( " failed to build packet" );
171 len = get_cached_info->BufferSize;
173 /* Point client at our cached buffer.
175 * To add to the fun, Intel decided at some point in
176 * the evolution of the PXE specification to add the
177 * BufferLimit field, which we are meant to fill in
178 * with the length of our packet buffer, so that the
179 * caller can safely modify the boot server reply
180 * packet stored therein. However, this field was not
181 * present in earlier versions of the PXE spec, and
182 * there is at least one PXE NBP (Altiris) which
183 * allocates only exactly enough space for this
184 * earlier, shorter version of the structure. If we
185 * actually fill in the BufferLimit field, we
186 * therefore risk trashing random areas of the
187 * caller's memory. If we *don't* fill it in, then
188 * the caller is at liberty to assume that whatever
189 * random value happened to be in that location
190 * represents the length of the buffer we've just
193 * Since older PXE stacks won't fill this field in
194 * anyway, it's probably safe to assume that no
195 * callers actually rely on it, so we choose to not
198 get_cached_info->Buffer.segment = rm_ds;
199 get_cached_info->Buffer.offset =
200 ( unsigned ) ( __from_data16 ( info ) );
201 get_cached_info->BufferSize = sizeof ( *info );
202 DBG ( " returning %04x:%04x+%04x['%x']",
203 get_cached_info->Buffer.segment,
204 get_cached_info->Buffer.offset,
205 get_cached_info->BufferSize,
206 get_cached_info->BufferLimit );
208 /* Copy packet to client buffer */
209 if ( len > sizeof ( *info ) )
210 len = sizeof ( *info );
211 if ( len < sizeof ( *info ) )
212 DBG ( " buffer may be too short" );
213 buffer = real_to_user ( get_cached_info->Buffer.segment,
214 get_cached_info->Buffer.offset );
215 copy_to_user ( buffer, 0, info, len );
216 get_cached_info->BufferSize = len;
219 get_cached_info->Status = PXENV_STATUS_SUCCESS;
220 return PXENV_EXIT_SUCCESS;
223 get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
224 return PXENV_EXIT_FAILURE;
227 /* PXENV_RESTART_TFTP
231 PXENV_EXIT_t pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE
233 PXENV_EXIT_t tftp_exit;
235 DBG ( "PXENV_RESTART_TFTP " );
237 /* Intel bug-for-bug hack */
238 pxe_set_cached_filename ( restart_tftp->FileName );
240 /* Words cannot describe the complete mismatch between the PXE
241 * specification and any possible version of reality...
243 restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
244 restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
245 tftp_exit = pxenv_tftp_read_file ( restart_tftp );
246 if ( tftp_exit != PXENV_EXIT_SUCCESS )
249 /* Fire up the new NBP */
250 restart_tftp->Status = pxe_start_nbp();
252 /* Not sure what "SUCCESS" actually means, since we can only
253 * return if the new NBP failed to boot...
255 return PXENV_EXIT_SUCCESS;
262 PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
263 unsigned int bus_type;
264 unsigned int location;
265 struct net_device *netdev;
267 DBG ( "PXENV_START_UNDI %04x:%04x:%04x",
268 start_undi->AX, start_undi->BX, start_undi->DX );
270 /* Determine bus type and location. Use a heuristic to decide
271 * whether we are PCI or ISAPnP
273 if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
274 ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
275 ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
276 ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
277 bus_type = BUS_TYPE_ISAPNP;
278 location = start_undi->BX;
279 /* Record ISAPnP read port for use by isapnp.c */
280 isapnp_read_port = start_undi->DX;
282 bus_type = BUS_TYPE_PCI;
283 location = start_undi->AX;
286 /* Probe for devices, etc. */
289 /* Look for a matching net device */
290 netdev = find_netdev_by_location ( bus_type, location );
292 DBG ( " no net device found" );
293 start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
294 return PXENV_EXIT_FAILURE;
296 DBG ( " using netdev %s", netdev->name );
298 /* Save as PXE net device */
299 pxe_set_netdev ( netdev );
304 start_undi->Status = PXENV_STATUS_SUCCESS;
305 return PXENV_EXIT_SUCCESS;
312 PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
313 DBG ( "PXENV_STOP_UNDI" );
318 /* Clear PXE net device */
319 pxe_set_netdev ( NULL );
321 /* Prepare for unload */
324 stop_undi->Status = PXENV_STATUS_SUCCESS;
325 return PXENV_EXIT_SUCCESS;
330 * Status: won't implement (requires major structural changes)
332 PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
333 DBG ( "PXENV_START_BASE" );
335 start_base->Status = PXENV_STATUS_UNSUPPORTED;
336 return PXENV_EXIT_FAILURE;
343 PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
344 DBG ( "PXENV_STOP_BASE" );
346 /* The only time we will be called is when the NBP is trying
347 * to shut down the PXE stack. There's nothing we need to do
351 stop_base->Status = PXENV_STATUS_SUCCESS;
352 return PXENV_EXIT_SUCCESS;