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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <pxe.h>
00025 #include <realmode.h>
00026 #include <bios.h>
00027 #include <pnpbios.h>
00028 #include <basemem.h>
00029 #include <gpxe/pci.h>
00030 #include <undi.h>
00031 #include <undirom.h>
00032 #include <undiload.h>
00033 
00034 /** @file
00035  *
00036  * UNDI load/unload
00037  *
00038  */
00039 
00040 /** Parameter block for calling UNDI loader */
00041 static struct s_UNDI_LOADER __bss16 ( undi_loader );
00042 #define undi_loader __use_data16 ( undi_loader )
00043 
00044 /** UNDI loader entry point */
00045 static SEGOFF16_t __bss16 ( undi_loader_entry );
00046 #define undi_loader_entry __use_data16 ( undi_loader_entry )
00047 
00048 /**
00049  * Call UNDI loader to create a pixie
00050  *
00051  * @v undi              UNDI device
00052  * @v undirom           UNDI ROM
00053  * @ret rc              Return status code
00054  */
00055 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
00056         struct s_PXE ppxe;
00057         unsigned int fbms_seg;
00058         uint16_t exit;
00059         int rc;
00060 
00061         /* Only one UNDI instance may be loaded at any given time */
00062         if ( undi_loader_entry.segment ) {
00063                 DBG ( "UNDI %p cannot load multiple instances\n", undi );
00064                 return -EBUSY;
00065         }
00066 
00067         /* Set up START_UNDI parameters */
00068         memset ( &undi_loader, 0, sizeof ( undi_loader ) );
00069         undi_loader.AX = undi->pci_busdevfn;
00070         undi_loader.BX = undi->isapnp_csn;
00071         undi_loader.DX = undi->isapnp_read_port;
00072         undi_loader.ES = BIOS_SEG;
00073         undi_loader.DI = find_pnp_bios();
00074 
00075         /* Allocate base memory for PXE stack */
00076         undi->restore_fbms = get_fbms();
00077         fbms_seg = ( undi->restore_fbms << 6 );
00078         fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
00079         undi_loader.UNDI_CS = fbms_seg;
00080         fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
00081         undi_loader.UNDI_DS = fbms_seg;
00082 
00083         /* Debug info */
00084         DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
00085                undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
00086         if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
00087                 unsigned int bus = ( undi->pci_busdevfn >> 8 );
00088                 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
00089                 DBGC ( undi, "PCI %02x:%02x.%x\n",
00090                        bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
00091         }
00092         if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
00093                 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
00094                        undi->isapnp_read_port, undi->isapnp_csn );
00095         }
00096 
00097         /* Call loader */
00098         undi_loader_entry = undirom->loader_entry;
00099         __asm__ __volatile__ ( REAL_CODE ( "pushw %%ds\n\t"
00100                                            "pushw %%ax\n\t"
00101                                            "lcall *undi_loader_entry\n\t"
00102                                            "addw $4, %%sp\n\t" )
00103                                : "=a" ( exit )
00104                                : "a" ( __from_data16 ( &undi_loader ) )
00105                                : "ebx", "ecx", "edx", "esi", "edi", "ebp" );
00106 
00107         /* UNDI API calls may rudely change the status of A20 and not
00108          * bother to restore it afterwards.  Intel is known to be
00109          * guilty of this.
00110          *
00111          * Note that we will return to this point even if A20 gets
00112          * screwed up by the UNDI driver, because Etherboot always
00113          * resides in an even megabyte of RAM.
00114          */     
00115         gateA20_set();
00116 
00117         if ( exit != PXENV_EXIT_SUCCESS ) {
00118                 /* Clear entry point */
00119                 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
00120 
00121                 rc = -undi_loader.Status;
00122                 if ( rc == 0 ) /* Paranoia */
00123                         rc = -EIO;
00124                 DBGC ( undi, "UNDI %p loader failed: %s\n",
00125                        undi, strerror ( rc ) );
00126                 return rc;
00127         }
00128 
00129         /* Populate PXE device structure */
00130         undi->pxenv = undi_loader.PXENVptr;
00131         undi->ppxe = undi_loader.PXEptr;
00132         copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
00133                          sizeof ( ppxe ) );
00134         undi->entry = ppxe.EntryPointSP;
00135         DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
00136                "entry %04x:%04x\n", undi, undi->pxenv.segment,
00137                undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
00138                undi->entry.segment, undi->entry.offset );
00139 
00140         /* Update free base memory counter */
00141         undi->fbms = ( fbms_seg >> 6 );
00142         set_fbms ( undi->fbms );
00143         DBGC ( undi, "UNDI %p using [%d,%d) kB of base memory\n",
00144                undi, undi->fbms, undi->restore_fbms );
00145 
00146         return 0;
00147 }
00148 
00149 /**
00150  * Unload a pixie
00151  *
00152  * @v undi              UNDI device
00153  * @ret rc              Return status code
00154  *
00155  * Erases the PXENV+ and !PXE signatures, and frees the used base
00156  * memory (if possible).
00157  */
00158 int undi_unload ( struct undi_device *undi ) {
00159         static uint32_t dead = 0xdeaddead;
00160 
00161         DBGC ( undi, "UNDI %p unloading\n", undi );
00162 
00163         /* Clear entry point */
00164         memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
00165 
00166         /* Erase signatures */
00167         if ( undi->pxenv.segment )
00168                 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
00169         if ( undi->ppxe.segment )
00170                 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
00171 
00172         /* Free base memory, if possible */
00173         if ( undi->fbms == get_fbms() ) {
00174                 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
00175                        undi, undi->fbms, undi->restore_fbms );
00176                 set_fbms ( undi->restore_fbms );
00177                 return 0;
00178         } else {
00179                 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
00180                        undi, undi->fbms, undi->restore_fbms );
00181                 return -EBUSY;
00182         }
00183 }

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