pxe_preboot.c

Go to the documentation of this file.
00001 /** @file
00002  *
00003  * PXE Preboot API
00004  *
00005  */
00006 
00007 /* PXE API interface for Etherboot.
00008  *
00009  * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License as
00013  * published by the Free Software Foundation; either version 2 of the
00014  * License, or any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  */
00025 
00026 FILE_LICENCE ( GPL2_OR_LATER );
00027 
00028 #include <stdint.h>
00029 #include <string.h>
00030 #include <stdlib.h>
00031 #include <gpxe/uaccess.h>
00032 #include <gpxe/dhcp.h>
00033 #include <gpxe/fakedhcp.h>
00034 #include <gpxe/device.h>
00035 #include <gpxe/netdevice.h>
00036 #include <gpxe/isapnp.h>
00037 #include <gpxe/init.h>
00038 #include <gpxe/if_ether.h>
00039 #include <basemem_packet.h>
00040 #include <biosint.h>
00041 #include "pxe.h"
00042 #include "pxe_call.h"
00043 
00044 /* Avoid dragging in isapnp.o unnecessarily */
00045 uint16_t isapnp_read_port;
00046 
00047 /** Zero-based versions of PXENV_GET_CACHED_INFO::PacketType */
00048 enum pxe_cached_info_indices {
00049         CACHED_INFO_DHCPDISCOVER = ( PXENV_PACKET_TYPE_DHCP_DISCOVER - 1 ),
00050         CACHED_INFO_DHCPACK = ( PXENV_PACKET_TYPE_DHCP_ACK - 1 ),
00051         CACHED_INFO_BINL = ( PXENV_PACKET_TYPE_CACHED_REPLY - 1 ),
00052         NUM_CACHED_INFOS
00053 };
00054 
00055 /** A cached DHCP packet */
00056 union pxe_cached_info {
00057         struct dhcphdr dhcphdr;
00058         /* This buffer must be *exactly* the size of a BOOTPLAYER_t
00059          * structure, otherwise WinPE will die horribly.  It takes the
00060          * size of *our* buffer and feeds it in to us as the size of
00061          * one of *its* buffers.  If our buffer is larger than it
00062          * expects, we therefore end up overwriting part of its data
00063          * segment, since it tells us to do so.  (D'oh!)
00064          *
00065          * Note that a BOOTPLAYER_t is not necessarily large enough to
00066          * hold a DHCP packet; this is a flaw in the PXE spec.
00067          */
00068         BOOTPLAYER_t packet;
00069 } __attribute__ (( packed ));
00070 
00071 /** A PXE DHCP packet creator */
00072 struct pxe_dhcp_packet_creator {
00073         /** Create DHCP packet
00074          *
00075          * @v netdev            Network device
00076          * @v data              Buffer for DHCP packet
00077          * @v max_len           Size of DHCP packet buffer
00078          * @ret rc              Return status code
00079          */
00080         int ( * create ) ( struct net_device *netdev, void *data,
00081                            size_t max_len );
00082 };
00083 
00084 /** PXE DHCP packet creators */
00085 static struct pxe_dhcp_packet_creator pxe_dhcp_packet_creators[] = {
00086         [CACHED_INFO_DHCPDISCOVER] = { create_fakedhcpdiscover },
00087         [CACHED_INFO_DHCPACK] = { create_fakedhcpack },
00088         [CACHED_INFO_BINL] = { create_fakepxebsack },
00089 };
00090 
00091 /* The case in which the caller doesn't supply a buffer is really
00092  * awkward to support given that we have multiple sources of options,
00093  * and that we don't actually store the DHCP packets.  (We may not
00094  * even have performed DHCP; we may have obtained all configuration
00095  * from non-volatile stored options or from the command line.)
00096  *
00097  * Some NBPs rely on the buffers we provide being persistent, so we
00098  * can't just use the temporary packet buffer.  4.5kB of base memory
00099  * always wasted just because some clients are too lazy to provide
00100  * their own buffers...
00101  */
00102 static union pxe_cached_info __bss16_array ( cached_info, [NUM_CACHED_INFOS] );
00103 #define cached_info __use_data16 ( cached_info )
00104 
00105 /**
00106  * UNLOAD BASE CODE STACK
00107  *
00108  * @v None                              -
00109  * @ret ...
00110  *
00111  */
00112 PXENV_EXIT_t pxenv_unload_stack ( struct s_PXENV_UNLOAD_STACK *unload_stack ) {
00113         DBG ( "PXENV_UNLOAD_STACK" );
00114 
00115         unload_stack->Status = PXENV_STATUS_SUCCESS;
00116         return PXENV_EXIT_SUCCESS;
00117 }
00118 
00119 /* PXENV_GET_CACHED_INFO
00120  *
00121  * Status: working
00122  */
00123 PXENV_EXIT_t pxenv_get_cached_info ( struct s_PXENV_GET_CACHED_INFO
00124                                      *get_cached_info ) {
00125         struct pxe_dhcp_packet_creator *creator;
00126         union pxe_cached_info *info;
00127         unsigned int idx;
00128         size_t len;
00129         userptr_t buffer;
00130         int rc;
00131 
00132         DBG ( "PXENV_GET_CACHED_INFO %d", get_cached_info->PacketType );
00133 
00134         DBG ( " to %04x:%04x+%x", get_cached_info->Buffer.segment,
00135               get_cached_info->Buffer.offset, get_cached_info->BufferSize );
00136 
00137         /* Sanity check */
00138         idx = ( get_cached_info->PacketType - 1 );
00139         if ( idx >= NUM_CACHED_INFOS ) {
00140                 DBG ( " bad PacketType" );
00141                 goto err;
00142         }
00143         info = &cached_info[idx];
00144 
00145         /* Construct cached version of packet, if not already constructed. */
00146         if ( ! info->dhcphdr.op ) {
00147                 /* Construct DHCP packet */
00148                 creator = &pxe_dhcp_packet_creators[idx];
00149                 if ( ( rc = creator->create ( pxe_netdev, info,
00150                                               sizeof ( *info ) ) ) != 0 ) {
00151                         DBG ( " failed to build packet" );
00152                         goto err;
00153                 }
00154         }
00155 
00156         len = get_cached_info->BufferSize;
00157         if ( len == 0 ) {
00158                 /* Point client at our cached buffer.
00159                  *
00160                  * To add to the fun, Intel decided at some point in
00161                  * the evolution of the PXE specification to add the
00162                  * BufferLimit field, which we are meant to fill in
00163                  * with the length of our packet buffer, so that the
00164                  * caller can safely modify the boot server reply
00165                  * packet stored therein.  However, this field was not
00166                  * present in earlier versions of the PXE spec, and
00167                  * there is at least one PXE NBP (Altiris) which
00168                  * allocates only exactly enough space for this
00169                  * earlier, shorter version of the structure.  If we
00170                  * actually fill in the BufferLimit field, we
00171                  * therefore risk trashing random areas of the
00172                  * caller's memory.  If we *don't* fill it in, then
00173                  * the caller is at liberty to assume that whatever
00174                  * random value happened to be in that location
00175                  * represents the length of the buffer we've just
00176                  * passed back to it.
00177                  *
00178                  * Since older PXE stacks won't fill this field in
00179                  * anyway, it's probably safe to assume that no
00180                  * callers actually rely on it, so we choose to not
00181                  * fill it in.
00182                  */
00183                 get_cached_info->Buffer.segment = rm_ds;
00184                 get_cached_info->Buffer.offset = __from_data16 ( info );
00185                 get_cached_info->BufferSize = sizeof ( *info );
00186                 DBG ( " returning %04x:%04x+%04x['%x']",
00187                       get_cached_info->Buffer.segment,
00188                       get_cached_info->Buffer.offset,
00189                       get_cached_info->BufferSize,
00190                       get_cached_info->BufferLimit );
00191         } else {
00192                 /* Copy packet to client buffer */
00193                 if ( len > sizeof ( *info ) )
00194                         len = sizeof ( *info );
00195                 if ( len < sizeof ( *info ) )
00196                         DBG ( " buffer may be too short" );
00197                 buffer = real_to_user ( get_cached_info->Buffer.segment,
00198                                         get_cached_info->Buffer.offset );
00199                 copy_to_user ( buffer, 0, info, len );
00200                 get_cached_info->BufferSize = len;
00201         }
00202 
00203         get_cached_info->Status = PXENV_STATUS_SUCCESS;
00204         return PXENV_EXIT_SUCCESS;
00205 
00206  err:
00207         get_cached_info->Status = PXENV_STATUS_OUT_OF_RESOURCES;
00208         return PXENV_EXIT_FAILURE;
00209 }
00210 
00211 /* PXENV_RESTART_TFTP
00212  *
00213  * Status: working
00214  */
00215 PXENV_EXIT_t pxenv_restart_tftp ( struct s_PXENV_TFTP_READ_FILE
00216                                   *restart_tftp ) {
00217         PXENV_EXIT_t tftp_exit;
00218 
00219         DBG ( "PXENV_RESTART_TFTP " );
00220 
00221         /* Words cannot describe the complete mismatch between the PXE
00222          * specification and any possible version of reality...
00223          */
00224         restart_tftp->Buffer = PXE_LOAD_PHYS; /* Fixed by spec, apparently */
00225         restart_tftp->BufferSize = ( 0xa0000 - PXE_LOAD_PHYS ); /* Near enough */
00226         tftp_exit = pxenv_tftp_read_file ( restart_tftp );
00227         if ( tftp_exit != PXENV_EXIT_SUCCESS )
00228                 return tftp_exit;
00229 
00230         /* Fire up the new NBP */
00231         restart_tftp->Status = pxe_start_nbp();
00232 
00233         /* Not sure what "SUCCESS" actually means, since we can only
00234          * return if the new NBP failed to boot...
00235          */
00236         return PXENV_EXIT_SUCCESS;
00237 }
00238 
00239 /* PXENV_START_UNDI
00240  *
00241  * Status: working
00242  */
00243 PXENV_EXIT_t pxenv_start_undi ( struct s_PXENV_START_UNDI *start_undi ) {
00244         unsigned int bus_type;
00245         unsigned int location;
00246         struct net_device *netdev;
00247 
00248         DBG ( "PXENV_START_UNDI %04x:%04x:%04x",
00249               start_undi->AX, start_undi->BX, start_undi->DX );
00250 
00251         /* Determine bus type and location.  Use a heuristic to decide
00252          * whether we are PCI or ISAPnP
00253          */
00254         if ( ( start_undi->DX >= ISAPNP_READ_PORT_MIN ) &&
00255              ( start_undi->DX <= ISAPNP_READ_PORT_MAX ) &&
00256              ( start_undi->BX >= ISAPNP_CSN_MIN ) &&
00257              ( start_undi->BX <= ISAPNP_CSN_MAX ) ) {
00258                 bus_type = BUS_TYPE_ISAPNP;
00259                 location = start_undi->BX;
00260                 /* Record ISAPnP read port for use by isapnp.c */
00261                 isapnp_read_port = start_undi->DX;
00262         } else {
00263                 bus_type = BUS_TYPE_PCI;
00264                 location = start_undi->AX;
00265         }
00266 
00267         /* Probe for devices, etc. */
00268         startup();
00269 
00270         /* Look for a matching net device */
00271         netdev = find_netdev_by_location ( bus_type, location );
00272         if ( ! netdev ) {
00273                 DBG ( " no net device found" );
00274                 start_undi->Status = PXENV_STATUS_UNDI_CANNOT_INITIALIZE_NIC;
00275                 return PXENV_EXIT_FAILURE;
00276         }
00277         DBG ( " using netdev %s", netdev->name );
00278 
00279         /* Activate PXE */
00280         pxe_activate ( netdev );
00281 
00282         start_undi->Status = PXENV_STATUS_SUCCESS;
00283         return PXENV_EXIT_SUCCESS;
00284 }
00285 
00286 /* PXENV_STOP_UNDI
00287  *
00288  * Status: working
00289  */
00290 PXENV_EXIT_t pxenv_stop_undi ( struct s_PXENV_STOP_UNDI *stop_undi ) {
00291         DBG ( "PXENV_STOP_UNDI" );
00292 
00293         /* Deactivate PXE */
00294         pxe_deactivate();
00295 
00296         /* Prepare for unload */
00297         shutdown ( SHUTDOWN_BOOT );
00298 
00299         /* Check to see if we still have any hooked interrupts */
00300         if ( hooked_bios_interrupts != 0 ) {
00301                 DBG ( "PXENV_STOP_UNDI failed: %d interrupts still hooked\n",
00302                       hooked_bios_interrupts );
00303                 stop_undi->Status = PXENV_STATUS_KEEP_UNDI;
00304                 return PXENV_EXIT_FAILURE;
00305         }
00306 
00307         stop_undi->Status = PXENV_STATUS_SUCCESS;
00308         return PXENV_EXIT_SUCCESS;
00309 }
00310 
00311 /* PXENV_START_BASE
00312  *
00313  * Status: won't implement (requires major structural changes)
00314  */
00315 PXENV_EXIT_t pxenv_start_base ( struct s_PXENV_START_BASE *start_base ) {
00316         DBG ( "PXENV_START_BASE" );
00317 
00318         start_base->Status = PXENV_STATUS_UNSUPPORTED;
00319         return PXENV_EXIT_FAILURE;
00320 }
00321 
00322 /* PXENV_STOP_BASE
00323  *
00324  * Status: working
00325  */
00326 PXENV_EXIT_t pxenv_stop_base ( struct s_PXENV_STOP_BASE *stop_base ) {
00327         DBG ( "PXENV_STOP_BASE" );
00328 
00329         /* The only time we will be called is when the NBP is trying
00330          * to shut down the PXE stack.  There's nothing we need to do
00331          * in this call.
00332          */
00333 
00334         stop_base->Status = PXENV_STATUS_SUCCESS;
00335         return PXENV_EXIT_SUCCESS;
00336 }

Generated on Tue Apr 6 20:00:50 2010 for gPXE by  doxygen 1.5.7.1