iPXE
undiload.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <pxe.h>
00030 #include <realmode.h>
00031 #include <bios.h>
00032 #include <pnpbios.h>
00033 #include <basemem.h>
00034 #include <ipxe/pci.h>
00035 #include <undi.h>
00036 #include <undirom.h>
00037 #include <undiload.h>
00038 
00039 /** @file
00040  *
00041  * UNDI load/unload
00042  *
00043  */
00044 
00045 /* Disambiguate the various error causes */
00046 #define EINFO_EUNDILOAD                                                 \
00047         __einfo_uniqify ( EINFO_EPLATFORM, 0x01,                        \
00048                           "UNDI loader error" )
00049 #define EUNDILOAD( status ) EPLATFORM ( EINFO_EUNDILOAD, status )
00050 
00051 /** Parameter block for calling UNDI loader */
00052 static struct s_UNDI_LOADER __bss16 ( undi_loader );
00053 #define undi_loader __use_data16 ( undi_loader )
00054 
00055 /** UNDI loader entry point */
00056 static SEGOFF16_t __bss16 ( undi_loader_entry );
00057 #define undi_loader_entry __use_data16 ( undi_loader_entry )
00058 
00059 /**
00060  * Call UNDI loader to create a pixie
00061  *
00062  * @v undi              UNDI device
00063  * @v undirom           UNDI ROM
00064  * @ret rc              Return status code
00065  */
00066 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
00067         struct s_PXE ppxe;
00068         unsigned int fbms_seg;
00069         uint16_t exit;
00070         int rc;
00071 
00072         /* Only one UNDI instance may be loaded at any given time */
00073         if ( undi_loader_entry.segment ) {
00074                 DBG ( "UNDI %p cannot load multiple instances\n", undi );
00075                 rc = -EBUSY;
00076                 goto err_multiple;
00077         }
00078 
00079         /* Set up START_UNDI parameters */
00080         memset ( &undi_loader, 0, sizeof ( undi_loader ) );
00081         undi_loader.AX = undi->pci_busdevfn;
00082         undi_loader.BX = undi->isapnp_csn;
00083         undi_loader.DX = undi->isapnp_read_port;
00084         undi_loader.ES = BIOS_SEG;
00085         undi_loader.DI = find_pnp_bios();
00086 
00087         /* Allocate base memory for PXE stack */
00088         undi->restore_fbms = get_fbms();
00089         fbms_seg = ( undi->restore_fbms << 6 );
00090         fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
00091         undi_loader.UNDI_CS = fbms_seg;
00092         fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
00093         undi_loader.UNDI_DS = fbms_seg;
00094         undi->fbms = ( fbms_seg >> 6 );
00095         set_fbms ( undi->fbms );
00096         DBGC ( undi, "UNDI %p allocated [%d,%d) kB of base memory\n",
00097                undi, undi->fbms, undi->restore_fbms );
00098 
00099         /* Debug info */
00100         DBGC ( undi, "UNDI %p loading ROM %p to CS %04x:%04zx DS %04x:%04zx "
00101                "for ", undi, undirom, undi_loader.UNDI_CS, undirom->code_size,
00102                undi_loader.UNDI_DS, undirom->data_size );
00103         if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
00104                 unsigned int bus = ( undi->pci_busdevfn >> 8 );
00105                 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
00106                 DBGC ( undi, "PCI %02x:%02x.%x\n",
00107                        bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
00108         }
00109         if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
00110                 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
00111                        undi->isapnp_read_port, undi->isapnp_csn );
00112         }
00113 
00114         /* Call loader */
00115         undi_loader_entry = undirom->loader_entry;
00116         __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
00117                                            "pushw %%ds\n\t"
00118                                            "pushw %%ax\n\t"
00119                                            "lcall *undi_loader_entry\n\t"
00120                                            "popl %%ebp\n\t" /* discard */
00121                                            "popl %%ebp\n\t" /* gcc bug */ )
00122                                : "=a" ( exit )
00123                                : "a" ( __from_data16 ( &undi_loader ) )
00124                                : "ebx", "ecx", "edx", "esi", "edi" );
00125         if ( exit != PXENV_EXIT_SUCCESS ) {
00126                 rc = -EUNDILOAD ( undi_loader.Status );
00127                 DBGC ( undi, "UNDI %p loader failed: %s\n",
00128                        undi, strerror ( rc ) );
00129                 goto err_loader;
00130         }
00131 
00132         /* Populate PXE device structure */
00133         undi->pxenv = undi_loader.PXENVptr;
00134         undi->ppxe = undi_loader.PXEptr;
00135         copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
00136                          sizeof ( ppxe ) );
00137         undi->entry = ppxe.EntryPointSP;
00138         DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
00139                "entry %04x:%04x\n", undi, undi->pxenv.segment,
00140                undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
00141                undi->entry.segment, undi->entry.offset );
00142 
00143         return 0;
00144 
00145  err_loader:
00146         set_fbms ( undi->restore_fbms );
00147         memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
00148  err_multiple:
00149         return rc;
00150 }
00151 
00152 /**
00153  * Unload a pixie
00154  *
00155  * @v undi              UNDI device
00156  * @ret rc              Return status code
00157  *
00158  * Erases the PXENV+ and !PXE signatures, and frees the used base
00159  * memory (if possible).
00160  */
00161 int undi_unload ( struct undi_device *undi ) {
00162         static uint32_t dead = 0xdeaddead;
00163 
00164         DBGC ( undi, "UNDI %p unloading\n", undi );
00165 
00166         /* Clear entry point */
00167         memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
00168 
00169         /* Erase signatures */
00170         if ( undi->pxenv.segment )
00171                 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
00172         if ( undi->ppxe.segment )
00173                 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
00174 
00175         /* Free base memory, if possible */
00176         if ( undi->fbms == get_fbms() ) {
00177                 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
00178                        undi, undi->fbms, undi->restore_fbms );
00179                 set_fbms ( undi->restore_fbms );
00180                 return 0;
00181         } else {
00182                 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
00183                        undi, undi->fbms, undi->restore_fbms );
00184                 return -EBUSY;
00185         }
00186 }