Procházet zdrojové kódy

[efi] Add the "snpnet" driver

Add a new network driver that consumes the EFI Simple Network
Protocol.  Also add a bus driver that can find the Simple Network
Protocol that iPXE was loaded from; the resulting behavior is similar
to the "undionly" driver for BIOS systems.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Geoff Lywood před 14 roky
rodič
revize
62149deb11

+ 1
- 0
src/Makefile Zobrazit soubor

@@ -67,6 +67,7 @@ SRCDIRS		+= drivers/net/phantom
67 67
 SRCDIRS		+= drivers/net/rtl818x
68 68
 SRCDIRS		+= drivers/net/ath5k
69 69
 SRCDIRS		+= drivers/net/vxge
70
+SRCDIRS		+= drivers/net/efi
70 71
 SRCDIRS		+= drivers/block
71 72
 SRCDIRS		+= drivers/nvs
72 73
 SRCDIRS		+= drivers/bitbash

+ 49
- 0
src/drivers/net/efi/snp.h Zobrazit soubor

@@ -0,0 +1,49 @@
1
+/*
2
+ * Copyright (C) 2010 VMware, Inc.  All Rights Reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
17
+ */
18
+
19
+#ifndef _SNP_H
20
+#define _SNP_H
21
+
22
+/** @file
23
+ *
24
+ * SNP driver
25
+ *
26
+ */
27
+
28
+FILE_LICENCE ( GPL2_OR_LATER );
29
+
30
+#include <ipxe/device.h>
31
+#include <ipxe/netdevice.h>
32
+#include <ipxe/efi/Protocol/SimpleNetwork.h>
33
+
34
+/** A network device that consumes the EFI Simple Network Protocol */
35
+struct snp_device {
36
+	/** Underlying simple network protocol instance */
37
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp;
38
+
39
+	/** Generic device */
40
+	struct device dev;
41
+
42
+	/** Network device */
43
+	struct net_device *netdev;
44
+
45
+	/** State to put the snp in when removing the device */
46
+	uint32 removal_state;
47
+};
48
+
49
+#endif /* _SNP_H */

+ 362
- 0
src/drivers/net/efi/snpnet.c Zobrazit soubor

@@ -0,0 +1,362 @@
1
+/*
2
+ * Copyright (C) 2010 VMware, Inc.  All Rights Reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+#include <errno.h>
22
+#include <string.h>
23
+#include <ipxe/io.h>
24
+#include <ipxe/iobuf.h>
25
+#include <ipxe/netdevice.h>
26
+#include <ipxe/if_ether.h>
27
+#include <ipxe/ethernet.h>
28
+#include <ipxe/efi/efi.h>
29
+#include <ipxe/efi/Protocol/SimpleNetwork.h>
30
+#include "snp.h"
31
+#include "snpnet.h"
32
+
33
+/** @file
34
+ *
35
+ * SNP network device driver
36
+ *
37
+ */
38
+
39
+/** SNP net device structure */
40
+struct snpnet_device {
41
+	/** The underlying simple network protocol */
42
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp;
43
+
44
+	/** State that the SNP should be in after close */
45
+	UINT32 close_state;
46
+};
47
+
48
+/**
49
+ * Transmit packet
50
+ *
51
+ * @v netdev		Network device
52
+ * @v iobuf		I/O buffer
53
+ * @ret rc		Return status code
54
+ */
55
+static int snpnet_transmit ( struct net_device *netdev,
56
+			     struct io_buffer *iobuf ) {
57
+	struct snpnet_device *snpnetdev = netdev->priv;
58
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpnetdev->snp;
59
+	EFI_STATUS efirc;
60
+	size_t len = iob_len ( iobuf );
61
+
62
+	efirc = snp->Transmit ( snp, 0, len, iobuf->data, NULL, NULL, NULL );
63
+	return EFIRC_TO_RC ( efirc );
64
+}
65
+
66
+/**
67
+ * Find a I/O buffer on the list of outstanding Tx buffers and complete it.
68
+ *
69
+ * @v snpnetdev		SNP network device
70
+ * @v txbuf		Buffer address
71
+ */
72
+static void snpnet_complete ( struct net_device *netdev, void *txbuf ) {
73
+	struct io_buffer *tmp;
74
+	struct io_buffer *iobuf;
75
+
76
+	list_for_each_entry_safe ( iobuf, tmp, &netdev->tx_queue, list ) {
77
+		if ( iobuf->data == txbuf ) {
78
+			netdev_tx_complete ( netdev, iobuf );
79
+			break;
80
+		}
81
+	}
82
+}
83
+
84
+/**
85
+ * Poll for received packets
86
+ *
87
+ * @v netdev		Network device
88
+ */
89
+static void snpnet_poll ( struct net_device *netdev ) {
90
+	struct snpnet_device *snpnetdev = netdev->priv;
91
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpnetdev->snp;
92
+	EFI_STATUS efirc;
93
+	struct io_buffer *iobuf = NULL;
94
+	UINTN len;
95
+	void *txbuf;
96
+
97
+	/* Process Tx completions */
98
+	while ( 1 ) {
99
+		efirc = snp->GetStatus ( snp, NULL, &txbuf );
100
+		if ( efirc ) {
101
+			DBGC ( snp, "SNP %p could not get status %s\n", snp,
102
+			       efi_strerror ( efirc ) );
103
+			break;
104
+		}
105
+
106
+		if ( txbuf == NULL )
107
+			break;
108
+
109
+		snpnet_complete ( netdev, txbuf );
110
+	}
111
+
112
+	/* Process received packets */
113
+	while ( 1 ) {
114
+		/* The spec is not clear if the max packet size refers to the
115
+		 * payload or the entire packet including headers. The Receive
116
+		 * function needs a buffer large enough to contain the headers,
117
+		 * and potentially a 4-byte CRC and 4-byte VLAN tag (?), so add
118
+		 * some breathing room.
119
+		 */
120
+		len = snp->Mode->MaxPacketSize + ETH_HLEN + 8;
121
+		iobuf = alloc_iob ( len );
122
+		if ( iobuf == NULL ) {
123
+			netdev_rx_err ( netdev, NULL, -ENOMEM );
124
+			break;
125
+		}
126
+
127
+		efirc = snp->Receive ( snp, NULL, &len, iobuf->data,
128
+				       NULL, NULL, NULL );
129
+
130
+		/* No packets left? */
131
+		if ( efirc == EFI_NOT_READY ) {
132
+			free_iob ( iobuf );
133
+			break;
134
+		}
135
+
136
+		/* Other error? */
137
+		if ( efirc ) {
138
+			DBGC ( snp, "SNP %p receive packet error: %s "
139
+				    "(len was %zd, is now %zd)\n",
140
+			       snp, efi_strerror ( efirc ), iob_len(iobuf),
141
+			       (size_t)len );
142
+			netdev_rx_err ( netdev, iobuf, efirc );
143
+			break;
144
+		}
145
+
146
+		/* Packet is valid, deliver it */
147
+		iob_put ( iobuf, len );
148
+		netdev_rx ( netdev, iob_disown ( iobuf ) );
149
+	}
150
+}
151
+
152
+/**
153
+ * Open NIC
154
+ *
155
+ * @v netdev		Net device
156
+ * @ret rc		Return status code
157
+ */
158
+static int snpnet_open ( struct net_device *netdev ) {
159
+	struct snpnet_device *snpnetdev = netdev->priv;
160
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpnetdev->snp;
161
+	EFI_STATUS efirc;
162
+	UINT32 enableFlags, disableFlags;
163
+
164
+	snpnetdev->close_state = snp->Mode->State;
165
+	if ( snp->Mode->State != EfiSimpleNetworkInitialized ) {
166
+		efirc = snp->Initialize ( snp, 0, 0 );
167
+		if ( efirc ) {
168
+			DBGC ( snp, "SNP %p could not initialize: %s\n",
169
+			       snp, efi_strerror ( efirc ) );
170
+			return EFIRC_TO_RC ( efirc );
171
+		}
172
+	}
173
+
174
+        /* Use the default MAC address */
175
+	efirc = snp->StationAddress ( snp, FALSE,
176
+				      (EFI_MAC_ADDRESS *)netdev->ll_addr );
177
+	if ( efirc ) {
178
+		DBGC ( snp, "SNP %p could not reset station address: %s\n",
179
+		       snp, efi_strerror ( efirc ) );
180
+	}
181
+
182
+	/* Set up receive filters to receive unicast and broadcast packets
183
+	 * always. Also, enable either promiscuous multicast (if possible) or
184
+	 * promiscuous operation, in order to catch all multicast packets.
185
+	 */
186
+	enableFlags = snp->Mode->ReceiveFilterMask &
187
+		      ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
188
+			EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
189
+	disableFlags = snp->Mode->ReceiveFilterMask &
190
+		       ( EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
191
+			 EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS |
192
+			 EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST );
193
+	if ( snp->Mode->ReceiveFilterMask &
194
+	     EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST ) {
195
+		enableFlags |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
196
+	} else if ( snp->Mode->ReceiveFilterMask &
197
+		    EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS ) {
198
+		enableFlags |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
199
+	}
200
+	disableFlags &= ~enableFlags;
201
+	efirc = snp->ReceiveFilters ( snp, enableFlags, disableFlags,
202
+				      FALSE, 0, NULL );
203
+	if ( efirc ) {
204
+		DBGC ( snp, "SNP %p could not set receive filters: %s\n",
205
+		       snp, efi_strerror ( efirc ) );
206
+	}
207
+
208
+	DBGC ( snp, "SNP %p opened\n", snp );
209
+	return 0;
210
+}
211
+
212
+/**
213
+ * Close NIC
214
+ *
215
+ * @v netdev		Net device
216
+ */
217
+static void snpnet_close ( struct net_device *netdev ) {
218
+	struct snpnet_device *snpnetdev = netdev->priv;
219
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpnetdev->snp;
220
+	EFI_STATUS efirc;
221
+
222
+	if ( snpnetdev->close_state != EfiSimpleNetworkInitialized ) {
223
+		efirc = snp->Shutdown ( snp );
224
+		if ( efirc ) {
225
+			DBGC ( snp, "SNP %p could not shut down: %s\n",
226
+			       snp, efi_strerror ( efirc ) );
227
+		}
228
+	}
229
+}
230
+
231
+/**
232
+ * Enable/disable interrupts
233
+ *
234
+ * @v netdev		Net device
235
+ * @v enable		Interrupts should be enabled
236
+ */
237
+static void snpnet_irq ( struct net_device *netdev, int enable ) {
238
+	struct snpnet_device *snpnetdev = netdev->priv;
239
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpnetdev->snp;
240
+
241
+	/* On EFI, interrupts are never necessary. (This function is only
242
+	 * required for BIOS PXE.) If interrupts were required, they could be
243
+	 * simulated using a fast timer.
244
+	 */
245
+	DBGC ( snp, "SNP %p cannot %s interrupts\n",
246
+	       snp, ( enable ? "enable" : "disable" ) );
247
+}
248
+
249
+/** SNP network device operations */
250
+static struct net_device_operations snpnet_operations = {
251
+	.open		= snpnet_open,
252
+	.close		= snpnet_close,
253
+	.transmit	= snpnet_transmit,
254
+	.poll		= snpnet_poll,
255
+	.irq   		= snpnet_irq,
256
+};
257
+
258
+/**
259
+ * Probe SNP device
260
+ *
261
+ * @v snpdev		SNP device
262
+ * @ret rc		Return status code
263
+ */
264
+int snpnet_probe ( struct snp_device *snpdev ) {
265
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpdev->snp;
266
+	EFI_STATUS efirc;
267
+	struct net_device *netdev;
268
+	struct snpnet_device *snpnetdev;
269
+	int rc;
270
+
271
+	DBGC ( snp, "SNP %p probing...\n", snp );
272
+
273
+	/* Allocate net device */
274
+	netdev = alloc_etherdev ( sizeof ( struct snpnet_device ) );
275
+	if ( ! netdev )
276
+		return -ENOMEM;
277
+	netdev_init ( netdev, &snpnet_operations );
278
+	netdev->dev = &snpdev->dev;
279
+	snpdev->netdev = netdev;
280
+	snpnetdev = netdev->priv;
281
+	snpnetdev->snp = snp;
282
+	snpdev->removal_state = snp->Mode->State;
283
+
284
+	/* Start the interface */
285
+	if ( snp->Mode->State == EfiSimpleNetworkStopped ) {
286
+		efirc = snp->Start ( snp );
287
+		if ( efirc ) {
288
+			DBGC ( snp, "SNP %p could not start: %s\n", snp,
289
+			       efi_strerror ( efirc ) );
290
+			rc = EFIRC_TO_RC ( efirc );
291
+			goto err_start;
292
+		}
293
+	}
294
+
295
+	if ( snp->Mode->HwAddressSize > sizeof ( netdev->hw_addr ) ) {
296
+		DBGC ( snp, "SNP %p hardware address is too large\n", snp );
297
+		rc = -EINVAL;
298
+		goto err_hwaddr;
299
+	}
300
+	memcpy ( netdev->hw_addr, snp->Mode->PermanentAddress.Addr,
301
+		 snp->Mode->HwAddressSize );
302
+
303
+	/* Mark as link up; we don't handle link state */
304
+	netdev_link_up ( netdev );
305
+
306
+	/* Register network device */
307
+	if ( ( rc = register_netdev ( netdev ) ) != 0 )
308
+		goto err_register;
309
+
310
+	DBGC ( snp, "SNP %p added\n", snp );
311
+	return 0;
312
+
313
+err_register:
314
+err_hwaddr:
315
+	if ( snpdev->removal_state == EfiSimpleNetworkStopped )
316
+		snp->Stop ( snp );
317
+
318
+err_start:
319
+	netdev_nullify ( netdev );
320
+	netdev_put ( netdev );
321
+	snpdev->netdev = NULL;
322
+	return rc;
323
+}
324
+
325
+/**
326
+ * Remove SNP device
327
+ *
328
+ * @v snpdev		SNP device
329
+ */
330
+void snpnet_remove ( struct snp_device *snpdev ) {
331
+	EFI_SIMPLE_NETWORK_PROTOCOL *snp = snpdev->snp;
332
+	EFI_STATUS efirc;
333
+	struct net_device *netdev = snpdev->netdev;
334
+
335
+	if ( snp->Mode->State == EfiSimpleNetworkInitialized &&
336
+	     snpdev->removal_state != EfiSimpleNetworkInitialized ) {
337
+		DBGC ( snp, "SNP %p shutting down\n", snp );
338
+		efirc = snp->Shutdown ( snp );
339
+		if ( efirc ) {
340
+			DBGC ( snp, "SNP %p could not shut down: %s\n",
341
+			       snp, efi_strerror ( efirc ) );
342
+		}
343
+	}
344
+
345
+	if ( snp->Mode->State == EfiSimpleNetworkStarted &&
346
+	     snpdev->removal_state == EfiSimpleNetworkStopped ) {
347
+		DBGC ( snp, "SNP %p stopping\n", snp );
348
+		efirc = snp->Stop ( snp );
349
+		if ( efirc ) {
350
+			DBGC ( snp, "SNP %p could not be stopped\n", snp );
351
+		}
352
+	}
353
+
354
+	/* Unregister net device */
355
+	unregister_netdev ( netdev );
356
+
357
+	/* Free network device */
358
+	netdev_nullify ( netdev );
359
+	netdev_put ( netdev );
360
+
361
+	DBGC ( snp, "SNP %p removed\n", snp );
362
+}

+ 35
- 0
src/drivers/net/efi/snpnet.h Zobrazit soubor

@@ -0,0 +1,35 @@
1
+/*
2
+ * Copyright (C) 2010 VMware, Inc.  All Rights Reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
17
+ */
18
+
19
+#ifndef _SNPNET_H
20
+#define _SNPNET_H
21
+
22
+/** @file
23
+ *
24
+ * EFI Simple Network Protocol network device driver
25
+ *
26
+ */
27
+
28
+FILE_LICENCE ( GPL2_OR_LATER );
29
+
30
+struct snp_device;
31
+
32
+extern int snpnet_probe ( struct snp_device *snpdev );
33
+extern void snpnet_remove ( struct snp_device *snpdev );
34
+
35
+#endif /* _SNPNET_H */

+ 129
- 0
src/drivers/net/efi/snponly.c Zobrazit soubor

@@ -0,0 +1,129 @@
1
+/*
2
+ * Copyright (C) 2010 VMware, Inc.  All Rights Reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+#include <string.h>
22
+#include <errno.h>
23
+#include <ipxe/device.h>
24
+#include <ipxe/init.h>
25
+#include <ipxe/efi/efi.h>
26
+#include <ipxe/efi/Protocol/SimpleNetwork.h>
27
+#include "snp.h"
28
+#include "snpnet.h"
29
+
30
+/** @file
31
+ *
32
+ * Chain-loading Simple Network Protocol Bus Driver
33
+ *
34
+ * This bus driver allows iPXE to use the EFI Simple Network Protocol provided
35
+ * by the platform to transmit and receive packets. It attaches to only the
36
+ * device handle that iPXE was loaded from, that is, it will only use the
37
+ * Simple Network Protocol on the current loaded image's device handle.
38
+ *
39
+ * Eseentially, this driver provides the EFI equivalent of the "undionly"
40
+ * driver.
41
+ */
42
+
43
+/** The one and only SNP network device */
44
+static struct snp_device snponly_dev;
45
+
46
+/** EFI simple network protocol GUID */
47
+static EFI_GUID efi_simple_network_protocol_guid
48
+	= EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
49
+
50
+/**
51
+ * Probe SNP root bus
52
+ *
53
+ * @v rootdev		SNP bus root device
54
+ *
55
+ * Look at the loaded image's device handle and see if the simple network
56
+ * protocol exists. If so, register a driver for it.
57
+ */
58
+static int snpbus_probe ( struct root_device *rootdev ) {
59
+	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
60
+	EFI_STATUS efirc;
61
+	int rc;
62
+	void *snp;
63
+
64
+	efirc = bs->OpenProtocol ( efi_loaded_image->DeviceHandle,
65
+				   &efi_simple_network_protocol_guid,
66
+				   &snp, efi_image_handle, NULL,
67
+				   EFI_OPEN_PROTOCOL_GET_PROTOCOL );
68
+	if ( efirc ) {
69
+		DBG ( "Could not find Simple Network Protocol!\n" );
70
+		return -ENODEV;
71
+	}
72
+	snponly_dev.snp = snp;
73
+
74
+	/* Add to device hierarchy */
75
+	strncpy ( snponly_dev.dev.name, "EFI SNP",
76
+		  ( sizeof ( snponly_dev.dev.name ) - 1 ) );
77
+	snponly_dev.dev.parent = &rootdev->dev;
78
+	list_add ( &snponly_dev.dev.siblings, &rootdev->dev.children);
79
+	INIT_LIST_HEAD ( &snponly_dev.dev.children );
80
+
81
+	/* Create network device */
82
+	if ( ( rc = snpnet_probe ( &snponly_dev ) ) != 0 )
83
+		goto err;
84
+
85
+	return 0;
86
+
87
+err:
88
+	list_del ( &snponly_dev.dev.siblings );
89
+	return rc;
90
+}
91
+
92
+/**
93
+ * Remove SNP root bus
94
+ *
95
+ * @v rootdev		SNP bus root device
96
+ */
97
+static void snpbus_remove ( struct root_device *rootdev __unused ) {
98
+	snpnet_remove ( &snponly_dev );
99
+	list_del ( &snponly_dev.dev.siblings );
100
+}
101
+
102
+/** SNP bus root device driver */
103
+static struct root_driver snp_root_driver = {
104
+	.probe = snpbus_probe,
105
+	.remove = snpbus_remove,
106
+};
107
+
108
+/** SNP bus root device */
109
+struct root_device snp_root_device __root_device = {
110
+	.dev = { .name = "EFI SNP" },
111
+	.driver = &snp_root_driver,
112
+};
113
+
114
+/**
115
+ * Prepare for exit
116
+ *
117
+ * @v flags		Shutdown flags
118
+ */
119
+static void snponly_shutdown ( int flags ) {
120
+	/* If we are shutting down to boot an OS, make sure the SNP does not
121
+	 * stay active.
122
+	 */
123
+	if ( flags & SHUTDOWN_BOOT )
124
+		snponly_dev.removal_state = EfiSimpleNetworkStopped;
125
+}
126
+
127
+struct startup_fn startup_snponly __startup_fn ( STARTUP_LATE ) = {
128
+	.shutdown = snponly_shutdown,
129
+};

+ 34
- 3
src/image/efi_image.c Zobrazit soubor

@@ -21,12 +21,27 @@ FILE_LICENCE ( GPL2_OR_LATER );
21 21
 #include <errno.h>
22 22
 #include <ipxe/efi/efi.h>
23 23
 #include <ipxe/image.h>
24
+#include <ipxe/init.h>
24 25
 #include <ipxe/features.h>
25 26
 
26 27
 FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
27 28
 
28 29
 struct image_type efi_image_type __image_type ( PROBE_NORMAL );
29 30
 
31
+/** Event used to signal shutdown */
32
+static EFI_EVENT efi_shutdown_event;
33
+
34
+/**
35
+ * Shut down in preparation for booting an OS.
36
+ *
37
+ * This hook gets called at ExitBootServices time in order to make sure that
38
+ * the network cards are properly shut down before the OS takes over.
39
+ */
40
+static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
41
+				       void *context __unused ) {
42
+	shutdown ( SHUTDOWN_BOOT );
43
+}
44
+
30 45
 /**
31 46
  * Execute EFI image
32 47
  *
@@ -39,6 +54,7 @@ static int efi_image_exec ( struct image *image ) {
39 54
 	UINTN exit_data_size;
40 55
 	CHAR16 *exit_data;
41 56
 	EFI_STATUS efirc;
57
+	int rc;
42 58
 
43 59
 	/* Attempt loading image */
44 60
 	if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
@@ -50,21 +66,36 @@ static int efi_image_exec ( struct image *image ) {
50 66
 		return -ENOEXEC;
51 67
 	}
52 68
 
69
+	/* Be sure to shut down the NIC at ExitBootServices time, or else
70
+	 * DMA from the card can corrupt the OS.
71
+	 */
72
+	efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
73
+				  TPL_CALLBACK, efi_shutdown_hook,
74
+				  NULL, &efi_shutdown_event );
75
+	if ( efirc ) {
76
+		rc = EFIRC_TO_RC ( efirc );
77
+		goto done;
78
+	}
79
+
53 80
 	/* Start the image */
54 81
 	if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
55 82
 					&exit_data ) ) != 0 ) {
56 83
 		DBGC ( image, "EFIIMAGE %p returned with status %s\n",
57 84
 		       image, efi_strerror ( efirc ) );
58
-		goto done;
59 85
 	}
60 86
 
61
- done:
87
+	rc = EFIRC_TO_RC ( efirc );
88
+
89
+	/* Remove the shutdown hook */
90
+	bs->CloseEvent ( efi_shutdown_event );
91
+
92
+done:
62 93
 	/* Unload the image.  We can't leave it loaded, because we
63 94
 	 * have no "unload" operation.
64 95
 	 */
65 96
 	bs->UnloadImage ( handle );
66 97
 
67
-	return EFIRC_TO_RC ( efirc );
98
+	return rc;
68 99
 }
69 100
 
70 101
 /**

+ 88
- 0
src/include/ipxe/efi/Protocol/LoadedImage.h Zobrazit soubor

@@ -0,0 +1,88 @@
1
+/** @file
2
+  UEFI 2.0 Loaded image protocol definition.
3
+
4
+  Every EFI driver and application is passed an image handle when it is loaded.
5
+  This image handle will contain a Loaded Image Protocol.
6
+
7
+  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
8
+  This program and the accompanying materials
9
+  are licensed and made available under the terms and conditions of the BSD License
10
+  which accompanies this distribution.  The full text of the license may be found at
11
+  http://opensource.org/licenses/bsd-license.php
12
+
13
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
+
16
+**/
17
+
18
+#ifndef __LOADED_IMAGE_PROTOCOL_H__
19
+#define __LOADED_IMAGE_PROTOCOL_H__
20
+
21
+#define EFI_LOADED_IMAGE_PROTOCOL_GUID \
22
+  { \
23
+    0x5B1B31A1, 0x9562, 0x11d2, {0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B } \
24
+  }
25
+
26
+#define EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID \
27
+  { \
28
+    0xbc62157e, 0x3e33, 0x4fec, {0x99, 0x20, 0x2d, 0x3b, 0x36, 0xd7, 0x50, 0xdf } \
29
+  }
30
+
31
+///
32
+/// Protocol GUID defined in EFI1.1.
33
+///
34
+#define LOADED_IMAGE_PROTOCOL   EFI_LOADED_IMAGE_PROTOCOL_GUID
35
+
36
+///
37
+/// EFI_SYSTEM_TABLE & EFI_IMAGE_UNLOAD are defined in EfiApi.h
38
+///
39
+#define EFI_LOADED_IMAGE_PROTOCOL_REVISION  0x1000
40
+
41
+///
42
+/// Revision defined in EFI1.1.
43
+///
44
+#define EFI_LOADED_IMAGE_INFORMATION_REVISION    EFI_LOADED_IMAGE_PROTOCOL_REVISION
45
+
46
+///
47
+/// Can be used on any image handle to obtain information about the loaded image.
48
+///
49
+typedef struct {
50
+  UINT32            Revision;       ///< Defines the revision of the EFI_LOADED_IMAGE_PROTOCOL structure.
51
+                                    ///< All future revisions will be backward compatible to the current revision.
52
+  EFI_HANDLE        ParentHandle;   ///< Parent image's image handle. NULL if the image is loaded directly from
53
+                                    ///< the firmware's boot manager.
54
+  EFI_SYSTEM_TABLE  *SystemTable;   ///< the image's EFI system table pointer.
55
+
56
+  //
57
+  // Source location of image
58
+  //
59
+  EFI_HANDLE        DeviceHandle;   ///< The device handle that the EFI Image was loaded from.
60
+  EFI_DEVICE_PATH_PROTOCOL  *FilePath;  ///< A pointer to the file path portion specific to DeviceHandle
61
+                                        ///< that the EFI Image was loaded from.
62
+  VOID              *Reserved;      ///< Reserved. DO NOT USE.
63
+
64
+  //
65
+  // Images load options
66
+  //
67
+  UINT32            LoadOptionsSize;///< The size in bytes of LoadOptions.
68
+  VOID              *LoadOptions;   ///< A pointer to the image's binary load options.
69
+
70
+  //
71
+  // Location of where image was loaded
72
+  //
73
+  VOID              *ImageBase;     ///< The base address at which the image was loaded.
74
+  UINT64            ImageSize;      ///< The size in bytes of the loaded image.
75
+  EFI_MEMORY_TYPE   ImageCodeType;  ///< The memory type that the code sections were loaded as.
76
+  EFI_MEMORY_TYPE   ImageDataType;  ///< The memory type that the data sections were loaded as.
77
+  EFI_IMAGE_UNLOAD  Unload;
78
+} EFI_LOADED_IMAGE_PROTOCOL;
79
+
80
+//
81
+// For backward-compatible with EFI1.1.
82
+//
83
+typedef EFI_LOADED_IMAGE_PROTOCOL EFI_LOADED_IMAGE;
84
+
85
+extern EFI_GUID gEfiLoadedImageProtocolGuid;
86
+extern EFI_GUID gEfiLoadedImageDevicePathProtocolGuid;
87
+
88
+#endif

+ 2
- 0
src/include/ipxe/efi/efi.h Zobrazit soubor

@@ -42,6 +42,7 @@
42 42
 /* Include the top-level EFI header files */
43 43
 #include <ipxe/efi/Uefi.h>
44 44
 #include <ipxe/efi/PiDxe.h>
45
+#include <ipxe/efi/Protocol/LoadedImage.h>
45 46
 
46 47
 /* Reset any trailing #pragma pack directives */
47 48
 #pragma pack(1)
@@ -135,6 +136,7 @@ struct efi_config_table {
135 136
 #define EFIRC_TO_RC( efirc ) (efirc)
136 137
 
137 138
 extern EFI_HANDLE efi_image_handle;
139
+extern EFI_LOADED_IMAGE_PROTOCOL *efi_loaded_image;
138 140
 extern EFI_SYSTEM_TABLE *efi_systab;
139 141
 
140 142
 extern const char * efi_strerror ( EFI_STATUS efirc );

+ 2
- 0
src/include/ipxe/errfile.h Zobrazit soubor

@@ -123,6 +123,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
123 123
 #define ERRFILE_vxge_config	     ( ERRFILE_DRIVER | 0x00560000 )
124 124
 #define ERRFILE_vxge_traffic	     ( ERRFILE_DRIVER | 0x00570000 )
125 125
 #define ERRFILE_igb_main	     ( ERRFILE_DRIVER | 0x00580000 )
126
+#define ERRFILE_snpnet		     ( ERRFILE_DRIVER | 0x00590000 )
127
+#define ERRFILE_snponly		     ( ERRFILE_DRIVER | 0x005a0000 )
126 128
 
127 129
 #define ERRFILE_scsi		     ( ERRFILE_DRIVER | 0x00700000 )
128 130
 #define ERRFILE_arbel		     ( ERRFILE_DRIVER | 0x00710000 )

+ 22
- 1
src/interface/efi/efi_init.c Zobrazit soubor

@@ -20,14 +20,22 @@ FILE_LICENCE ( GPL2_OR_LATER );
20 20
 
21 21
 #include <string.h>
22 22
 #include <ipxe/efi/efi.h>
23
+#include <ipxe/efi/Protocol/LoadedImage.h>
23 24
 #include <ipxe/uuid.h>
24 25
 
25 26
 /** Image handle passed to entry point */
26 27
 EFI_HANDLE efi_image_handle;
27 28
 
29
+/** Loaded image protocol for this image */
30
+EFI_LOADED_IMAGE_PROTOCOL *efi_loaded_image;
31
+
28 32
 /** System table passed to entry point */
29 33
 EFI_SYSTEM_TABLE *efi_systab;
30 34
 
35
+/** EFI loaded image protocol GUID */
36
+static EFI_GUID efi_loaded_image_protocol_guid
37
+	= EFI_LOADED_IMAGE_PROTOCOL_GUID;
38
+
31 39
 /**
32 40
  * Look up EFI configuration table
33 41
  *
@@ -59,6 +67,7 @@ EFI_STATUS efi_init ( EFI_HANDLE image_handle,
59 67
 	struct efi_protocol *prot;
60 68
 	struct efi_config_table *tab;
61 69
 	EFI_STATUS efirc;
70
+	void *loaded_image;
62 71
 
63 72
 	/* Store image handle and system table pointer for future use */
64 73
 	efi_image_handle = image_handle;
@@ -80,8 +89,20 @@ EFI_STATUS efi_init ( EFI_HANDLE image_handle,
80 89
 	}
81 90
 	DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
82 91
 
83
-	/* Look up used protocols */
84 92
 	bs = systab->BootServices;
93
+	efirc = bs->OpenProtocol ( image_handle,
94
+				   &efi_loaded_image_protocol_guid,
95
+				   &loaded_image, image_handle, NULL,
96
+				   EFI_OPEN_PROTOCOL_GET_PROTOCOL );
97
+	if ( efirc ) {
98
+	   DBGC ( systab, "Could not get loaded image protocol" );
99
+	   return efirc;
100
+	}
101
+
102
+	efi_loaded_image = loaded_image;
103
+	DBG ( "Image base address = %p\n", efi_loaded_image->ImageBase );
104
+
105
+	/* Look up used protocols */
85 106
 	for_each_table_entry ( prot, EFI_PROTOCOLS ) {
86 107
 		if ( ( efirc = bs->LocateProtocol ( &prot->u.guid, NULL,
87 108
 						    prot->protocol ) ) == 0 ) {

Načítá se…
Zrušit
Uložit