|
@@ -0,0 +1,189 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include <stdint.h>
|
|
20
|
+#include <stdlib.h>
|
|
21
|
+#include <string.h>
|
|
22
|
+#include <pxe.h>
|
|
23
|
+#include <realmode.h>
|
|
24
|
+#include <bios.h>
|
|
25
|
+#include <pnpbios.h>
|
|
26
|
+#include <gpxe/pci.h>
|
|
27
|
+#include <undi.h>
|
|
28
|
+#include <undirom.h>
|
|
29
|
+#include <undiload.h>
|
|
30
|
+
|
|
31
|
+/** @file
|
|
32
|
+ *
|
|
33
|
+ * UNDI load/unload
|
|
34
|
+ *
|
|
35
|
+ */
|
|
36
|
+
|
|
37
|
+/** Parameter block for calling UNDI loader */
|
|
38
|
+static struct s_UNDI_LOADER __data16 ( undi_loader );
|
|
39
|
+#define undi_loader __use_data16 ( undi_loader )
|
|
40
|
+
|
|
41
|
+/** UNDI loader entry point */
|
|
42
|
+static SEGOFF16_t __data16 ( undi_loader_entry );
|
|
43
|
+#define undi_loader_entry __use_data16 ( undi_loader_entry )
|
|
44
|
+
|
|
45
|
+/**
|
|
46
|
+ * Call UNDI loader to create a pixie
|
|
47
|
+ *
|
|
48
|
+ * @v undi UNDI device
|
|
49
|
+ * @v undirom UNDI ROM
|
|
50
|
+ * @ret rc Return status code
|
|
51
|
+ */
|
|
52
|
+static int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
|
|
53
|
+ struct s_PXE ppxe;
|
|
54
|
+ uint16_t fbms;
|
|
55
|
+ unsigned int fbms_seg;
|
|
56
|
+ uint16_t exit;
|
|
57
|
+ int rc;
|
|
58
|
+
|
|
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();
|
|
66
|
+
|
|
67
|
+ /* Allocate base memory for PXE stack */
|
|
68
|
+ get_real ( fbms, BDA_SEG, BDA_FBMS );
|
|
69
|
+ undi->restore_fbms = fbms;
|
|
70
|
+ fbms_seg = ( fbms << 6 );
|
|
71
|
+ fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
|
|
72
|
+ undi_loader.UNDI_CS = fbms_seg;
|
|
73
|
+ fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
|
|
74
|
+ undi_loader.UNDI_DS = fbms_seg;
|
|
75
|
+
|
|
76
|
+ /* Debug info */
|
|
77
|
+ DBGC ( undi, "UNDI %p loading UNDI ROM %p to CS %04x DS %04x for ",
|
|
78
|
+ undi, undirom, undi_loader.UNDI_CS, undi_loader.UNDI_DS );
|
|
79
|
+ if ( undi->pci_busdevfn ) {
|
|
80
|
+ unsigned int bus = ( undi->pci_busdevfn >> 8 );
|
|
81
|
+ unsigned int devfn = ( undi->pci_busdevfn & 0xff );
|
|
82
|
+ DBGC ( undi, "PCI %02x:%02x.%x\n",
|
|
83
|
+ bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
|
|
84
|
+ }
|
|
85
|
+ if ( undi->isapnp_csn != -1U ) {
|
|
86
|
+ DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
|
|
87
|
+ undi->isapnp_read_port, undi->isapnp_csn );
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ /* Call loader */
|
|
91
|
+ undi_loader_entry = undirom->loader_entry;
|
|
92
|
+ __asm__ __volatile__ ( REAL_CODE ( "pushw %%ds\n\t"
|
|
93
|
+ "pushw %%ax\n\t"
|
|
94
|
+ "lcall *%c2\n\t"
|
|
95
|
+ "addw $4, %%sp\n\t" )
|
|
96
|
+ : "=a" ( exit )
|
|
97
|
+ : "a" ( & __from_data16 ( undi_loader ) ),
|
|
98
|
+ "p" ( & __from_data16 ( undi_loader_entry ) )
|
|
99
|
+ : "ebx", "ecx", "edx", "esi", "edi", "ebp" );
|
|
100
|
+
|
|
101
|
+ /* UNDI API calls may rudely change the status of A20 and not
|
|
102
|
+ * bother to restore it afterwards. Intel is known to be
|
|
103
|
+ * guilty of this.
|
|
104
|
+ *
|
|
105
|
+ * Note that we will return to this point even if A20 gets
|
|
106
|
+ * screwed up by the UNDI driver, because Etherboot always
|
|
107
|
+ * resides in an even megabyte of RAM.
|
|
108
|
+ */
|
|
109
|
+ gateA20_set();
|
|
110
|
+
|
|
111
|
+ if ( exit != PXENV_EXIT_SUCCESS ) {
|
|
112
|
+ rc = -undi_loader.Status;
|
|
113
|
+ if ( rc == 0 ) /* Paranoia */
|
|
114
|
+ rc = -EIO;
|
|
115
|
+ DBGC ( undi, "UNDI %p loader failed: %s\n",
|
|
116
|
+ undi, strerror ( rc ) );
|
|
117
|
+ return rc;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ /* Populate PXE device structure */
|
|
121
|
+ undi->pxenv = undi_loader.PXENVptr;
|
|
122
|
+ undi->ppxe = undi_loader.PXEptr;
|
|
123
|
+ copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
|
|
124
|
+ sizeof ( ppxe ) );
|
|
125
|
+ undi->entry = ppxe.EntryPointSP;
|
|
126
|
+ DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
|
|
127
|
+ "entry %04x:%04x\n", undi, undi->pxenv.segment,
|
|
128
|
+ undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
|
|
129
|
+ undi->entry.segment, undi->entry.offset );
|
|
130
|
+
|
|
131
|
+ /* Update free base memory counter */
|
|
132
|
+ fbms = ( fbms_seg >> 6 );
|
|
133
|
+ put_real ( fbms, BDA_SEG, BDA_FBMS );
|
|
134
|
+ undi->fbms = fbms;
|
|
135
|
+ DBGC ( undi, "UNDI %p using %dkB-%dkB\n",
|
|
136
|
+ undi, undi->fbms, undi->restore_fbms );
|
|
137
|
+
|
|
138
|
+ return 0;
|
|
139
|
+}
|
|
140
|
+
|
|
141
|
+/**
|
|
142
|
+ * Call UNDI loader to create a pixie
|
|
143
|
+ *
|
|
144
|
+ * @v undi UNDI device
|
|
145
|
+ * @v undirom UNDI ROM
|
|
146
|
+ * @v pci_busdevfn PCI bus:dev.fn
|
|
147
|
+ * @ret rc Return status code
|
|
148
|
+ */
|
|
149
|
+int undi_load_pci ( struct undi_device *undi, struct undi_rom *undirom,
|
|
150
|
+ unsigned int bus, unsigned int devfn ) {
|
|
151
|
+ undi->pci_busdevfn = ( ( bus << 8 ) | devfn );
|
|
152
|
+ undi->isapnp_csn = -1U;
|
|
153
|
+ undi->isapnp_read_port = -1U;
|
|
154
|
+ return undi_load ( undi, undirom );
|
|
155
|
+}
|
|
156
|
+
|
|
157
|
+/**
|
|
158
|
+ * Unload a pixie
|
|
159
|
+ *
|
|
160
|
+ * @v undi UNDI device
|
|
161
|
+ * @ret rc Return status code
|
|
162
|
+ *
|
|
163
|
+ * Erases the PXENV+ and !PXE signatures, and frees the used base
|
|
164
|
+ * memory (if possible).
|
|
165
|
+ */
|
|
166
|
+int undi_unload ( struct undi_device *undi ) {
|
|
167
|
+ static uint32_t dead = 0xdeaddead;
|
|
168
|
+ uint16_t fbms;
|
|
169
|
+
|
|
170
|
+ DBGC ( undi, "UNDI %p unloading\n", undi );
|
|
171
|
+
|
|
172
|
+ /* Erase signatures */
|
|
173
|
+ put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
|
|
174
|
+ put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
|
|
175
|
+
|
|
176
|
+ /* Free base memory, if possible */
|
|
177
|
+ get_real ( fbms, BDA_SEG, BDA_FBMS );
|
|
178
|
+ if ( fbms == undi->fbms ) {
|
|
179
|
+ DBGC ( undi, "UNDI %p freeing %dkB-%dkB\n",
|
|
180
|
+ undi, undi->fbms, undi->restore_fbms );
|
|
181
|
+ fbms = undi->restore_fbms;
|
|
182
|
+ put_real ( fbms, BDA_SEG, BDA_FBMS );
|
|
183
|
+ return 0;
|
|
184
|
+ } else {
|
|
185
|
+ DBGC ( undi, "UNDI %p leaking %dkB-%dkB\n",
|
|
186
|
+ undi, undi->fbms, undi->restore_fbms );
|
|
187
|
+ return -EBUSY;
|
|
188
|
+ }
|
|
189
|
+}
|