[undi] Ensure only one UNDI instance is loaded
[gpxe.git] / src / arch / i386 / drivers / net / undiload.c
blob47a2baede43f3046e0bfa1fe993fa636c81fd1af
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pxe.h>
25 #include <realmode.h>
26 #include <bios.h>
27 #include <pnpbios.h>
28 #include <basemem.h>
29 #include <gpxe/pci.h>
30 #include <undi.h>
31 #include <undirom.h>
32 #include <undiload.h>
34 /** @file
36 * UNDI load/unload
40 /** Parameter block for calling UNDI loader */
41 static struct s_UNDI_LOADER __bss16 ( undi_loader );
42 #define undi_loader __use_data16 ( undi_loader )
44 /** UNDI loader entry point */
45 static SEGOFF16_t __bss16 ( undi_loader_entry );
46 #define undi_loader_entry __use_data16 ( undi_loader_entry )
48 /**
49 * Call UNDI loader to create a pixie
51 * @v undi UNDI device
52 * @v undirom UNDI ROM
53 * @ret rc Return status code
55 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
56 struct s_PXE ppxe;
57 unsigned int fbms_seg;
58 uint16_t exit;
59 int rc;
61 /* Only one UNDI instance may be loaded at any given time */
62 if ( undi_loader_entry.segment ) {
63 DBG ( "UNDI %p cannot load multiple instances\n", undi );
64 return -EBUSY;
67 /* Set up START_UNDI parameters */
68 memset ( &undi_loader, 0, sizeof ( undi_loader ) );
69 undi_loader.AX = undi->pci_busdevfn;
70 undi_loader.BX = undi->isapnp_csn;
71 undi_loader.DX = undi->isapnp_read_port;
72 undi_loader.ES = BIOS_SEG;
73 undi_loader.DI = find_pnp_bios();
75 /* Allocate base memory for PXE stack */
76 undi->restore_fbms = get_fbms();
77 fbms_seg = ( undi->restore_fbms << 6 );
78 fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
79 undi_loader.UNDI_CS = fbms_seg;
80 fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
81 undi_loader.UNDI_DS = fbms_seg;
83 /* Debug info */
84 DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
85 undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
86 if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
87 unsigned int bus = ( undi->pci_busdevfn >> 8 );
88 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
89 DBGC ( undi, "PCI %02x:%02x.%x\n",
90 bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
92 if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
93 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
94 undi->isapnp_read_port, undi->isapnp_csn );
97 /* Call loader */
98 undi_loader_entry = undirom->loader_entry;
99 __asm__ __volatile__ ( REAL_CODE ( "pushw %%ds\n\t"
100 "pushw %%ax\n\t"
101 "lcall *undi_loader_entry\n\t"
102 "addw $4, %%sp\n\t" )
103 : "=a" ( exit )
104 : "a" ( __from_data16 ( &undi_loader ) )
105 : "ebx", "ecx", "edx", "esi", "edi", "ebp" );
107 /* UNDI API calls may rudely change the status of A20 and not
108 * bother to restore it afterwards. Intel is known to be
109 * guilty of this.
111 * Note that we will return to this point even if A20 gets
112 * screwed up by the UNDI driver, because Etherboot always
113 * resides in an even megabyte of RAM.
115 gateA20_set();
117 if ( exit != PXENV_EXIT_SUCCESS ) {
118 /* Clear entry point */
119 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
121 rc = -undi_loader.Status;
122 if ( rc == 0 ) /* Paranoia */
123 rc = -EIO;
124 DBGC ( undi, "UNDI %p loader failed: %s\n",
125 undi, strerror ( rc ) );
126 return rc;
129 /* Populate PXE device structure */
130 undi->pxenv = undi_loader.PXENVptr;
131 undi->ppxe = undi_loader.PXEptr;
132 copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
133 sizeof ( ppxe ) );
134 undi->entry = ppxe.EntryPointSP;
135 DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
136 "entry %04x:%04x\n", undi, undi->pxenv.segment,
137 undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
138 undi->entry.segment, undi->entry.offset );
140 /* Update free base memory counter */
141 undi->fbms = ( fbms_seg >> 6 );
142 set_fbms ( undi->fbms );
143 DBGC ( undi, "UNDI %p using [%d,%d) kB of base memory\n",
144 undi, undi->fbms, undi->restore_fbms );
146 return 0;
150 * Unload a pixie
152 * @v undi UNDI device
153 * @ret rc Return status code
155 * Erases the PXENV+ and !PXE signatures, and frees the used base
156 * memory (if possible).
158 int undi_unload ( struct undi_device *undi ) {
159 static uint32_t dead = 0xdeaddead;
161 DBGC ( undi, "UNDI %p unloading\n", undi );
163 /* Clear entry point */
164 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
166 /* Erase signatures */
167 if ( undi->pxenv.segment )
168 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
169 if ( undi->ppxe.segment )
170 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
172 /* Free base memory, if possible */
173 if ( undi->fbms == get_fbms() ) {
174 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
175 undi, undi->fbms, undi->restore_fbms );
176 set_fbms ( undi->restore_fbms );
177 return 0;
178 } else {
179 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
180 undi, undi->fbms, undi->restore_fbms );
181 return -EBUSY;