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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
45 /* Disambiguate the various error causes */
46 #define EINFO_EUNDILOAD \
47 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
49 #define EUNDILOAD( status ) EPLATFORM ( EINFO_EUNDILOAD, status )
51 /** Parameter block for calling UNDI loader */
52 static struct s_UNDI_LOADER __bss16 ( undi_loader );
53 #define undi_loader __use_data16 ( undi_loader )
55 /** UNDI loader entry point */
56 static SEGOFF16_t __bss16 ( undi_loader_entry );
57 #define undi_loader_entry __use_data16 ( undi_loader_entry )
60 * Call UNDI loader to create a pixie
64 * @ret rc Return status code
66 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
68 unsigned int fbms_seg;
72 /* Only one UNDI instance may be loaded at any given time */
73 if ( undi_loader_entry.segment ) {
74 DBG ( "UNDI %p cannot load multiple instances\n", undi );
78 /* Set up START_UNDI parameters */
79 memset ( &undi_loader, 0, sizeof ( undi_loader ) );
80 undi_loader.AX = undi->pci_busdevfn;
81 undi_loader.BX = undi->isapnp_csn;
82 undi_loader.DX = undi->isapnp_read_port;
83 undi_loader.ES = BIOS_SEG;
84 undi_loader.DI = find_pnp_bios();
86 /* Allocate base memory for PXE stack */
87 undi->restore_fbms = get_fbms();
88 fbms_seg = ( undi->restore_fbms << 6 );
89 fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
90 undi_loader.UNDI_CS = fbms_seg;
91 fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
92 undi_loader.UNDI_DS = fbms_seg;
95 DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
96 undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
97 if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
98 unsigned int bus = ( undi->pci_busdevfn >> 8 );
99 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
100 DBGC ( undi, "PCI %02x:%02x.%x\n",
101 bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
103 if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
104 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
105 undi->isapnp_read_port, undi->isapnp_csn );
109 undi_loader_entry = undirom->loader_entry;
110 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
113 "lcall *undi_loader_entry\n\t"
114 "popl %%ebp\n\t" /* discard */
115 "popl %%ebp\n\t" /* gcc bug */ )
117 : "a" ( __from_data16 ( &undi_loader ) )
118 : "ebx", "ecx", "edx", "esi", "edi" );
120 if ( exit != PXENV_EXIT_SUCCESS ) {
121 /* Clear entry point */
122 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
124 rc = -EUNDILOAD ( undi_loader.Status );
125 DBGC ( undi, "UNDI %p loader failed: %s\n",
126 undi, strerror ( rc ) );
130 /* Populate PXE device structure */
131 undi->pxenv = undi_loader.PXENVptr;
132 undi->ppxe = undi_loader.PXEptr;
133 copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
135 undi->entry = ppxe.EntryPointSP;
136 DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
137 "entry %04x:%04x\n", undi, undi->pxenv.segment,
138 undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
139 undi->entry.segment, undi->entry.offset );
141 /* Update free base memory counter */
142 undi->fbms = ( fbms_seg >> 6 );
143 set_fbms ( undi->fbms );
144 DBGC ( undi, "UNDI %p using [%d,%d) kB of base memory\n",
145 undi, undi->fbms, undi->restore_fbms );
153 * @v undi UNDI device
154 * @ret rc Return status code
156 * Erases the PXENV+ and !PXE signatures, and frees the used base
157 * memory (if possible).
159 int undi_unload ( struct undi_device *undi ) {
160 static uint32_t dead = 0xdeaddead;
162 DBGC ( undi, "UNDI %p unloading\n", undi );
164 /* Clear entry point */
165 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
167 /* Erase signatures */
168 if ( undi->pxenv.segment )
169 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
170 if ( undi->ppxe.segment )
171 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
173 /* Free base memory, if possible */
174 if ( undi->fbms == get_fbms() ) {
175 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
176 undi, undi->fbms, undi->restore_fbms );
177 set_fbms ( undi->restore_fbms );
180 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
181 undi, undi->fbms, undi->restore_fbms );