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
20 FILE_LICENCE ( GPL2_OR_LATER );
41 /* Disambiguate the various error causes */
42 #define EINFO_EUNDILOAD \
43 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
45 #define EUNDILOAD( status ) EPLATFORM ( EINFO_EUNDILOAD, status )
47 /** Parameter block for calling UNDI loader */
48 static struct s_UNDI_LOADER __bss16 ( undi_loader );
49 #define undi_loader __use_data16 ( undi_loader )
51 /** UNDI loader entry point */
52 static SEGOFF16_t __bss16 ( undi_loader_entry );
53 #define undi_loader_entry __use_data16 ( undi_loader_entry )
56 * Call UNDI loader to create a pixie
60 * @ret rc Return status code
62 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
64 unsigned int fbms_seg;
68 /* Only one UNDI instance may be loaded at any given time */
69 if ( undi_loader_entry.segment ) {
70 DBG ( "UNDI %p cannot load multiple instances\n", undi );
74 /* Set up START_UNDI parameters */
75 memset ( &undi_loader, 0, sizeof ( undi_loader ) );
76 undi_loader.AX = undi->pci_busdevfn;
77 undi_loader.BX = undi->isapnp_csn;
78 undi_loader.DX = undi->isapnp_read_port;
79 undi_loader.ES = BIOS_SEG;
80 undi_loader.DI = find_pnp_bios();
82 /* Allocate base memory for PXE stack */
83 undi->restore_fbms = get_fbms();
84 fbms_seg = ( undi->restore_fbms << 6 );
85 fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
86 undi_loader.UNDI_CS = fbms_seg;
87 fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
88 undi_loader.UNDI_DS = fbms_seg;
91 DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
92 undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
93 if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
94 unsigned int bus = ( undi->pci_busdevfn >> 8 );
95 unsigned int devfn = ( undi->pci_busdevfn & 0xff );
96 DBGC ( undi, "PCI %02x:%02x.%x\n",
97 bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
99 if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
100 DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
101 undi->isapnp_read_port, undi->isapnp_csn );
105 undi_loader_entry = undirom->loader_entry;
106 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
109 "lcall *undi_loader_entry\n\t"
110 "popl %%ebp\n\t" /* discard */
111 "popl %%ebp\n\t" /* gcc bug */ )
113 : "a" ( __from_data16 ( &undi_loader ) )
114 : "ebx", "ecx", "edx", "esi", "edi" );
116 if ( exit != PXENV_EXIT_SUCCESS ) {
117 /* Clear entry point */
118 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
120 rc = -EUNDILOAD ( undi_loader.Status );
121 DBGC ( undi, "UNDI %p loader failed: %s\n",
122 undi, strerror ( rc ) );
126 /* Populate PXE device structure */
127 undi->pxenv = undi_loader.PXENVptr;
128 undi->ppxe = undi_loader.PXEptr;
129 copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
131 undi->entry = ppxe.EntryPointSP;
132 DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
133 "entry %04x:%04x\n", undi, undi->pxenv.segment,
134 undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
135 undi->entry.segment, undi->entry.offset );
137 /* Update free base memory counter */
138 undi->fbms = ( fbms_seg >> 6 );
139 set_fbms ( undi->fbms );
140 DBGC ( undi, "UNDI %p using [%d,%d) kB of base memory\n",
141 undi, undi->fbms, undi->restore_fbms );
149 * @v undi UNDI device
150 * @ret rc Return status code
152 * Erases the PXENV+ and !PXE signatures, and frees the used base
153 * memory (if possible).
155 int undi_unload ( struct undi_device *undi ) {
156 static uint32_t dead = 0xdeaddead;
158 DBGC ( undi, "UNDI %p unloading\n", undi );
160 /* Clear entry point */
161 memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
163 /* Erase signatures */
164 if ( undi->pxenv.segment )
165 put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
166 if ( undi->ppxe.segment )
167 put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
169 /* Free base memory, if possible */
170 if ( undi->fbms == get_fbms() ) {
171 DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
172 undi, undi->fbms, undi->restore_fbms );
173 set_fbms ( undi->restore_fbms );
176 DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
177 undi, undi->fbms, undi->restore_fbms );