iPXE
undiload.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pxe.h>
30 #include <realmode.h>
31 #include <bios.h>
32 #include <pnpbios.h>
33 #include <basemem.h>
34 #include <ipxe/pci.h>
35 #include <undi.h>
36 #include <undirom.h>
37 #include <undiload.h>
38 
39 /** @file
40  *
41  * UNDI load/unload
42  *
43  */
44 
45 /* Disambiguate the various error causes */
46 #define EINFO_EUNDILOAD \
47  __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
48  "UNDI loader error" )
49 #define EUNDILOAD( status ) EPLATFORM ( EINFO_EUNDILOAD, status )
50 
51 /** Parameter block for calling UNDI loader */
52 static struct s_UNDI_LOADER __bss16 ( undi_loader );
53 #define undi_loader __use_data16 ( undi_loader )
54 
55 /** UNDI loader entry point */
56 static SEGOFF16_t __bss16 ( undi_loader_entry );
57 #define undi_loader_entry __use_data16 ( undi_loader_entry )
58 
59 /**
60  * Call UNDI loader to create a pixie
61  *
62  * @v undi UNDI device
63  * @v undirom UNDI ROM
64  * @ret rc Return status code
65  */
66 int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
67  struct s_PXE ppxe;
68  unsigned int fbms_seg;
69  uint16_t exit;
70  int rc;
71 
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 );
75  rc = -EBUSY;
76  goto err_multiple;
77  }
78 
79  /* Set up START_UNDI parameters */
80  memset ( &undi_loader, 0, sizeof ( undi_loader ) );
81  undi_loader.AX = undi->pci_busdevfn;
82  undi_loader.BX = undi->isapnp_csn;
83  undi_loader.DX = undi->isapnp_read_port;
84  undi_loader.ES = BIOS_SEG;
86 
87  /* Allocate base memory for PXE stack */
88  undi->restore_fbms = get_fbms();
89  fbms_seg = ( undi->restore_fbms << 6 );
90  fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
91  undi_loader.UNDI_CS = fbms_seg;
92  fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
93  undi_loader.UNDI_DS = fbms_seg;
94  undi->fbms = ( fbms_seg >> 6 );
95  set_fbms ( undi->fbms );
96  DBGC ( undi, "UNDI %p allocated [%d,%d) kB of base memory\n",
97  undi, undi->fbms, undi->restore_fbms );
98 
99  /* Debug info */
100  DBGC ( undi, "UNDI %p loading ROM %p to CS %04x:%04zx DS %04x:%04zx "
101  "for ", undi, undirom, undi_loader.UNDI_CS, undirom->code_size,
102  undi_loader.UNDI_DS, undirom->data_size );
103  if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
104  unsigned int bus = ( undi->pci_busdevfn >> 8 );
105  unsigned int devfn = ( undi->pci_busdevfn & 0xff );
106  DBGC ( undi, "PCI %02x:%02x.%x\n",
107  bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
108  }
109  if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
110  DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
111  undi->isapnp_read_port, undi->isapnp_csn );
112  }
113 
114  /* Call loader */
115  undi_loader_entry = undirom->loader_entry;
116  __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
117  "pushw %%ds\n\t"
118  "pushw %%ax\n\t"
119  "lcall *undi_loader_entry\n\t"
120  "popl %%ebp\n\t" /* discard */
121  "popl %%ebp\n\t" /* gcc bug */ )
122  : "=a" ( exit )
123  : "a" ( __from_data16 ( &undi_loader ) )
124  : "ebx", "ecx", "edx", "esi", "edi" );
125  if ( exit != PXENV_EXIT_SUCCESS ) {
126  rc = -EUNDILOAD ( undi_loader.Status );
127  DBGC ( undi, "UNDI %p loader failed: %s\n",
128  undi, strerror ( rc ) );
129  goto err_loader;
130  }
131 
132  /* Populate PXE device structure */
133  undi->pxenv = undi_loader.PXENVptr;
134  undi->ppxe = undi_loader.PXEptr;
135  copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
136  sizeof ( ppxe ) );
137  undi->entry = ppxe.EntryPointSP;
138  DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
139  "entry %04x:%04x\n", undi, undi->pxenv.segment,
140  undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
141  undi->entry.segment, undi->entry.offset );
142 
143  return 0;
144 
145  err_loader:
146  set_fbms ( undi->restore_fbms );
147  memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
148  err_multiple:
149  return rc;
150 }
151 
152 /**
153  * Unload a pixie
154  *
155  * @v undi UNDI device
156  * @ret rc Return status code
157  *
158  * Erases the PXENV+ and !PXE signatures, and frees the used base
159  * memory (if possible).
160  */
161 int undi_unload ( struct undi_device *undi ) {
162  static uint32_t dead = 0xdeaddead;
163 
164  DBGC ( undi, "UNDI %p unloading\n", undi );
165 
166  /* Clear entry point */
167  memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
168 
169  /* Erase signatures */
170  if ( undi->pxenv.segment )
171  put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
172  if ( undi->ppxe.segment )
173  put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
174 
175  /* Free base memory, if possible */
176  if ( undi->fbms == get_fbms() ) {
177  DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
178  undi, undi->fbms, undi->restore_fbms );
179  set_fbms ( undi->restore_fbms );
180  return 0;
181  } else {
182  DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
183  undi, undi->fbms, undi->restore_fbms );
184  return -EBUSY;
185  }
186 }
UINT16_t restore_fbms
Free base memory prior to load.
Definition: undi.h:32
#define PCI_FUNC(busdevfn)
Definition: pci.h:281
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
size_t code_size
Code segment size.
Definition: undirom.h:37
UNDI load/unload.
#define EBUSY
Device or resource busy.
Definition: errno.h:338
#define undi_loader_entry
Definition: undiload.c:57
static unsigned int get_fbms(void)
Read the BIOS free base memory counter.
Definition: basemem.h:21
size_t data_size
Data segment size.
Definition: undirom.h:39
#define ppxe
Definition: pxe_call.h:28
static struct s_UNDI_LOADER __bss16(undi_loader)
Parameter block for calling UNDI loader.
Base memory allocation.
#define DBGC(...)
Definition: compiler.h:505
SEGOFF16_t ppxe
!PXE structure address
Definition: undi.h:26
#define UNDI_NO_ISAPNP_CSN
ISAPnP card select number field is invalid.
Definition: undi.h:90
UNDI driver.
An UNDI ROM.
Definition: undirom.h:29
SEGOFF16_t pxenv
PXENV+ structure address.
Definition: undi.h:24
#define put_real
Definition: libkir.h:150
UINT16_t isapnp_csn
ISAPnP card select number, or UNDI_NO_ISAPNP_CSN.
Definition: undi.h:36
#define EUNDILOAD(status)
Definition: undiload.c:49
UNDI expansion ROMs.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
SEGOFF16_t loader_entry
UNDI loader entry point.
Definition: undirom.h:35
SEGOFF16_t entry
Entry point.
Definition: undi.h:28
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define PCI_SLOT(busdevfn)
Definition: pci.h:280
PCI bus.
UINT16_t isapnp_read_port
ISAPnP read port, or UNDI_NO_ISAPNP_READ_PORT.
Definition: undi.h:38
UINT16_t fbms
Free base memory after load.
Definition: undi.h:30
#define UNDI_NO_PCI_BUSDEVFN
PCI bus:dev.fn field is invalid.
Definition: undi.h:87
int undi_unload(struct undi_device *undi)
Unload a pixie.
Definition: undiload.c:161
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
unsigned int uint32_t
Definition: stdint.h:12
An UNDI device.
Definition: undi.h:22
PnP BIOS.
UINT16_t pci_busdevfn
PCI bus:dev.fn, or UNDI_NO_PCI_BUSDEVFN.
Definition: undi.h:34
#define PXENV_EXIT_SUCCESS
No error occurred.
Definition: pxe_types.h:45
#define undi_loader
Definition: undiload.c:53
#define copy_from_real
Definition: libkir.h:79
The !PXE structure.
Definition: pxe_api.h:129
void set_fbms(unsigned int new_fbms)
Set the BIOS free base memory counter.
Definition: basemem.c:43
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define __from_data16(pointer)
Definition: libkir.h:22
Parameter block for undi_loader()
Definition: pxe_api.h:1729
int find_pnp_bios(void)
Locate Plug-and-Play BIOS.
Definition: pnpbios.c:101
int undi_load(struct undi_device *undi, struct undi_rom *undirom)
Call UNDI loader to create a pixie.
Definition: undiload.c:66
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define BIOS_SEG
Definition: pnpbios.h:13
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
String functions.
uint8_t bus
Bus.
Definition: edd.h:14
void * memset(void *dest, int character, size_t len) __nonnull