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.
38 /** Parameter block for calling UNDI loader */
39 static struct s_UNDI_LOADER __bss16 ( undi_loader );
40 #define undi_loader __use_data16 ( undi_loader )
42 /** UNDI loader entry point */
43 static SEGOFF16_t __bss16 ( undi_loader_entry );
44 #define undi_loader_entry __use_data16 ( undi_loader_entry )
47 * Call UNDI loader to create a pixie
51 * @ret rc Return status code
53 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
55 unsigned int fbms_seg;
59 /* Set up START_UNDI parameters */
60 memset ( &undi_loader, 0, sizeof ( undi_loader ) );
61 undi_loader.AX = undi->pci_busdevfn;
62 undi_loader.BX = undi->isapnp_csn;
63 undi_loader.DX = undi->isapnp_read_port;
64 undi_loader.ES = BIOS_SEG;
65 undi_loader.DI = find_pnp_bios();
67 /* Allocate base memory for PXE stack */
68 undi->restore_fbms = get_fbms();
69 fbms_seg = ( undi->restore_fbms << 6 );
70 fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
71 undi_loader.UNDI_CS = fbms_seg;
72 fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
73 undi_loader.UNDI_DS = fbms_seg;
76 DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
77 undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
78 if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
79 unsigned int bus = ( undi->pci_busdevfn >> 8 );
80 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
81 DBGC ( undi, "PCI %02x:%02x.%x\n",
82 bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
84 if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
85 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
86 undi->isapnp_read_port, undi->isapnp_csn );
90 undi_loader_entry = undirom->loader_entry;
91 __asm__ __volatile__ ( REAL_CODE ( "pushw %%ds\n\t"
93 "lcall *undi_loader_entry\n\t"
96 : "a" ( __from_data16 ( &undi_loader ) )
97 : "ebx", "ecx", "edx", "esi", "edi", "ebp" );
99 /* UNDI API calls may rudely change the status of A20 and not
100 * bother to restore it afterwards. Intel is known to be
103 * Note that we will return to this point even if A20 gets
104 * screwed up by the UNDI driver, because Etherboot always
105 * resides in an even megabyte of RAM.
109 if ( exit != PXENV_EXIT_SUCCESS ) {
110 rc = -undi_loader.Status;
111 if ( rc == 0 ) /* Paranoia */
113 DBGC ( undi, "UNDI %p loader failed: %s\n",
114 undi, strerror ( rc ) );
118 /* Populate PXE device structure */
119 undi->pxenv = undi_loader.PXENVptr;
120 undi->ppxe = undi_loader.PXEptr;
121 copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
123 undi->entry = ppxe.EntryPointSP;
124 DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
125 "entry %04x:%04x\n", undi, undi->pxenv.segment,
126 undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
127 undi->entry.segment, undi->entry.offset );
129 /* Update free base memory counter */
130 undi->fbms = ( fbms_seg >> 6 );
131 set_fbms ( undi->fbms );
132 DBGC ( undi, "UNDI %p using [%d,%d) kB of base memory\n",
133 undi, undi->fbms, undi->restore_fbms );
141 * @v undi UNDI device
142 * @ret rc Return status code
144 * Erases the PXENV+ and !PXE signatures, and frees the used base
145 * memory (if possible).
147 int undi_unload ( struct undi_device *undi ) {
148 static uint32_t dead = 0xdeaddead;
150 DBGC ( undi, "UNDI %p unloading\n", undi );
152 /* Erase signatures */
153 if ( undi->pxenv.segment )
154 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
155 if ( undi->ppxe.segment )
156 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
158 /* Free base memory, if possible */
159 if ( undi->fbms == get_fbms() ) {
160 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
161 undi, undi->fbms, undi->restore_fbms );
162 set_fbms ( undi->restore_fbms );
165 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
166 undi, undi->fbms, undi->restore_fbms );