Browse Source

Add PXE driver-scanning code, capable of locating all PXE ROMs in a system

and matching them up to (e.g. PCI) devices.
tags/v0.9.3
Michael Brown 17 years ago
parent
commit
0638945c7e
2 changed files with 336 additions and 2 deletions
  1. 223
    0
      src/arch/i386/drivers/bus/pxedrv.c
  2. 113
    2
      src/include/pxe.h

+ 223
- 0
src/arch/i386/drivers/bus/pxedrv.c View File

@@ -0,0 +1,223 @@
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
+
25
+/** @file
26
+ *
27
+ * PXE drivers
28
+ *
29
+ */
30
+
31
+static LIST_HEAD ( pxe_drivers );
32
+
33
+/**
34
+ * Parse PXE ROM ID structure
35
+ *
36
+ * @v pxedrv		PXE driver
37
+ * @v pxeromid		Offset within ROM to PXE ROM ID structure
38
+ * @ret rc		Return status code
39
+ */
40
+static int pxedrv_parse_pxeromid ( struct pxe_driver *pxedrv,
41
+				   unsigned int pxeromid ) {
42
+	struct undi_rom_id undi_rom_id;
43
+	unsigned int undiloader;
44
+
45
+	DBGC ( pxedrv, "PXEDRV %p has PXE ROM ID at %04x:%04x\n", pxedrv,
46
+	       pxedrv->rom_segment, pxeromid );
47
+
48
+	/* Read PXE ROM ID structure and verify */
49
+	copy_from_real ( &undi_rom_id, pxedrv->rom_segment, pxeromid,
50
+			 sizeof ( undi_rom_id ) );
51
+	if ( undi_rom_id.Signature != UNDI_ROM_ID_SIGNATURE ) {
52
+		DBGC ( pxedrv, "PXEDRV %p has bad PXE ROM ID signature "
53
+		       "%08lx\n", pxedrv, undi_rom_id.Signature );
54
+		return -EINVAL;
55
+	}
56
+
57
+	/* Check for UNDI loader */
58
+	undiloader = undi_rom_id.UNDILoader;
59
+	if ( ! undiloader ) {
60
+		DBGC ( pxedrv, "PXEDRV %p has no UNDI loader\n", pxedrv );
61
+		return -EINVAL;
62
+	}
63
+
64
+	/* Fill in PXE driver loader fields */
65
+	pxedrv->loader.segment = pxedrv->rom_segment;
66
+	pxedrv->loader.offset = undiloader;
67
+	pxedrv->code_size = undi_rom_id.CodeSize;
68
+	pxedrv->data_size = undi_rom_id.DataSize;
69
+
70
+	DBGC ( pxedrv, "PXEDRV %p has UNDI loader at %04x:%04x "
71
+	       "(code %04x data %04x)\n", pxedrv, pxedrv->loader.segment,
72
+	       pxedrv->loader.offset, pxedrv->code_size, pxedrv->data_size );
73
+	return 0;
74
+}
75
+
76
+/**
77
+ * Parse PCI expansion header
78
+ *
79
+ * @v pxedrv		PXE driver
80
+ * @v pcirheader	Offset within ROM to PCI expansion header
81
+ */
82
+static int pxedrv_parse_pcirheader ( struct pxe_driver *pxedrv,
83
+				     unsigned int pcirheader ) {
84
+	struct pcir_header pcir_header;
85
+
86
+	DBGC ( pxedrv, "PXEDRV %p has PCI expansion header at %04x:%04x\n",
87
+	       pxedrv, pxedrv->rom_segment, pcirheader );
88
+
89
+	/* Read PCI expansion header and verify */
90
+	copy_from_real ( &pcir_header, pxedrv->rom_segment, pcirheader,
91
+			 sizeof ( pcir_header ) );
92
+	if ( pcir_header.signature != PCIR_SIGNATURE ) {
93
+		DBGC ( pxedrv, "PXEDRV %p has bad PCI expansion header "
94
+		       "signature %08lx\n", pxedrv, pcir_header.signature );
95
+		return -EINVAL;
96
+	}
97
+	DBGC ( pxedrv, "PXEDRV %p is a PCI ROM\n", pxedrv );
98
+
99
+	/* Fill in PXE driver PCI device fields */
100
+	pxedrv->bus_type = PCI_NIC;
101
+	pxedrv->bus_id.pci.vendor_id = pcir_header.vendor_id;
102
+	pxedrv->bus_id.pci.device_id = pcir_header.device_id;
103
+
104
+	DBGC ( pxedrv, "PXEDRV %p is for PCI devices %04x:%04x\n", pxedrv,
105
+	       pxedrv->bus_id.pci.vendor_id, pxedrv->bus_id.pci.device_id );
106
+	return 0;
107
+	
108
+}
109
+
110
+/**
111
+ * Create PXE driver for expansion ROM
112
+ *
113
+ * @v rom_segment	ROM segment address
114
+ * @ret rc		Return status code
115
+ */
116
+static int pxedrv_probe_rom ( unsigned int rom_segment ) {
117
+	struct pxe_driver *pxedrv = NULL;
118
+	struct undi_rom rom;
119
+	unsigned int pxeromid;
120
+	unsigned int pcirheader;
121
+	int rc;
122
+
123
+	/* Read expansion ROM header and verify */
124
+	copy_from_real ( &rom, rom_segment, 0, sizeof ( rom ) );
125
+	if ( rom.Signature != ROM_SIGNATURE ) {
126
+		rc = -EINVAL;
127
+		goto err;
128
+	}
129
+
130
+	/* Allocate memory for PXE driver */
131
+	pxedrv = malloc ( sizeof ( *pxedrv ) );
132
+	if ( ! pxedrv ) {
133
+		DBG ( "Could not allocate PXE driver structure\n" );
134
+		rc = -ENOMEM;
135
+		goto err;
136
+	}
137
+	memset ( pxedrv, 0, sizeof ( *pxedrv ) );
138
+	DBGC ( pxedrv, "PXEDRV %p using expansion ROM at %04x:0000 (%zdkB)\n",
139
+	       pxedrv, rom_segment, ( rom.ROMLength / 2 ) );
140
+	pxedrv->rom_segment = rom_segment;
141
+
142
+	/* Check for and parse PXE ROM ID */
143
+	pxeromid = rom.PXEROMID;
144
+	if ( ! pxeromid ) {
145
+		DBGC ( pxedrv, "PXEDRV %p has no PXE ROM ID\n", pxedrv );
146
+		rc = -EINVAL;
147
+		goto err;
148
+	}
149
+	if ( ( rc = pxedrv_parse_pxeromid ( pxedrv, pxeromid ) ) != 0 )
150
+		goto err;
151
+
152
+	/* Parse PCIR header, if present */
153
+	pcirheader = rom.PCIRHeader;
154
+	if ( pcirheader )
155
+		pxedrv_parse_pcirheader ( pxedrv, pcirheader );
156
+
157
+	/* Add to PXE driver list and return */
158
+	DBGC ( pxedrv, "PXEDRV %p registered\n", pxedrv );
159
+	list_add ( &pxedrv->list, &pxe_drivers );
160
+	return 0;
161
+
162
+ err:
163
+	free ( pxedrv );
164
+	return rc;
165
+}
166
+
167
+/**
168
+ * Create PXE drivers for all possible expansion ROMs
169
+ *
170
+ * @ret 
171
+ */
172
+static void pxedrv_probe_all_roms ( void ) {
173
+	static int probed = 0;
174
+	unsigned int rom_segment;
175
+
176
+	/* Perform probe only once */
177
+	if ( probed )
178
+		return;
179
+
180
+	DBG ( "Scanning for PXE expansion ROMs\n" );
181
+
182
+	/* Scan through expansion ROM region at 2kB intervals */
183
+	for ( rom_segment = 0xc000 ; rom_segment < 0x10000 ;
184
+	      rom_segment += 0x80 ) {
185
+		pxedrv_probe_rom ( rom_segment );
186
+	}
187
+
188
+	probed = 1;
189
+}
190
+
191
+/**
192
+ * Find PXE driver for PCI device
193
+ *
194
+ * @v vendor_id		PCI vendor ID
195
+ * @v device_id		PCI device ID
196
+ * @v rombase		ROM base address, or 0 for any
197
+ * @ret pxedrv		PXE driver, or NULL
198
+ */
199
+struct pxe_driver * pxedrv_find_pci_driver ( unsigned int vendor_id,
200
+					     unsigned int device_id,
201
+					     unsigned int rombase ) {
202
+	struct pxe_driver *pxedrv;
203
+
204
+	pxedrv_probe_all_roms();
205
+
206
+	list_for_each_entry ( pxedrv, &pxe_drivers, list ) {
207
+		if ( pxedrv->bus_type != PCI_NIC )
208
+			continue;
209
+		if ( pxedrv->bus_id.pci.vendor_id != vendor_id )
210
+			continue;
211
+		if ( pxedrv->bus_id.pci.device_id != device_id )
212
+			continue;
213
+		if ( rombase && ( ( pxedrv->rom_segment << 4 ) != rombase ) )
214
+			continue;
215
+		DBGC ( pxedrv, "PXEDRV %p matched PCI %04x:%04x (%08x)\n",
216
+		       pxedrv, vendor_id, device_id, rombase );
217
+		return pxedrv;
218
+	}
219
+
220
+	DBG ( "No PXE driver matched PCI %04x:%04x (%08x)\n",
221
+	      vendor_id, device_id, rombase );
222
+	return NULL;
223
+}

+ 113
- 2
src/include/pxe.h View File

@@ -62,6 +62,113 @@ union u_PXENV_ANY {
62 62
 
63 63
 typedef union u_PXENV_ANY PXENV_ANY_t;
64 64
 
65
+/** An UNDI expansion ROM */
66
+struct undi_rom {
67
+	/** Signature
68
+	 *
69
+	 * Must be equal to @c ROM_SIGNATURE
70
+	 */
71
+	UINT16_t Signature;
72
+	/** ROM length in 512-byte blocks */
73
+	UINT8_t ROMLength;
74
+	/** Unused */
75
+	UINT8_t unused[0x13];
76
+	/** Offset of the PXE ROM ID structure */
77
+	UINT16_t PXEROMID;
78
+	/** Offset of the PCI ROM structure */
79
+	UINT16_t PCIRHeader;
80
+} PACKED;
81
+
82
+/** Signature for an expansion ROM */
83
+#define ROM_SIGNATURE 0xaa55
84
+
85
+/** An UNDI ROM ID structure */
86
+struct undi_rom_id {
87
+	/** Signature
88
+	 *
89
+	 * Must be equal to @c UNDI_ROM_ID_SIGNATURE
90
+	 */
91
+	UINT32_t Signature;
92
+	/** Length of structure */
93
+	UINT8_t StructLength;
94
+	/** Checksum */
95
+	UINT8_t StructCksum;
96
+	/** Structure revision
97
+	 *
98
+	 * Must be zero.
99
+	 */
100
+	UINT8_t StructRev;
101
+	/** UNDI revision
102
+	 *
103
+	 * Version 2.1.0 is encoded as the byte sequence 0x00, 0x01, 0x02.
104
+	 */
105
+	UINT8_t UNDIRev[3];
106
+	/** Offset to UNDI loader */
107
+	UINT16_t UNDILoader;
108
+	/** Minimum required stack segment size */
109
+	UINT16_t StackSize;
110
+	/** Minimum required data segment size */
111
+	UINT16_t DataSize;
112
+	/** Minimum required code segment size */
113
+	UINT16_t CodeSize;
114
+} PACKED;
115
+
116
+/** Signature for an UNDI ROM ID structure */
117
+#define UNDI_ROM_ID_SIGNATURE \
118
+	( ( 'U' << 0 ) + ( 'N' << 8 ) + ( 'D' << 16 ) + ( 'I' << 24 ) )
119
+
120
+/** A PCI expansion header */
121
+struct pcir_header {
122
+	/** Signature
123
+	 *
124
+	 * Must be equal to @c PCIR_SIGNATURE
125
+	 */
126
+	uint32_t signature;
127
+	/** PCI vendor ID */
128
+	uint16_t vendor_id;
129
+	/** PCI device ID */
130
+	uint16_t device_id;
131
+} PACKED;
132
+
133
+/** Signature for an UNDI ROM ID structure */
134
+#define PCIR_SIGNATURE \
135
+	( ( 'P' << 0 ) + ( 'C' << 8 ) + ( 'I' << 16 ) + ( 'R' << 24 ) )
136
+
137
+/** A PXE PCI device ID */
138
+struct pxe_pci_device_id {
139
+	/** PCI vendor ID */
140
+	unsigned int vendor_id;
141
+	/** PCI device ID */
142
+	unsigned int device_id;
143
+};
144
+
145
+/** A PXE device ID */
146
+union pxe_device_id {
147
+	/** PCI device ID */
148
+	struct pxe_pci_device_id pci;
149
+};
150
+
151
+/** A PXE driver */
152
+struct pxe_driver {
153
+	/** List of PXE drivers */
154
+	struct list_head list;
155
+	/** ROM segment address */
156
+	unsigned int rom_segment;
157
+	/** UNDI loader entry point */
158
+	SEGOFF16_t loader;
159
+	/** Code segment size */
160
+	size_t code_size;
161
+	/** Data segment size */
162
+	size_t data_size;
163
+	/** Bus type
164
+	 *
165
+	 * Values are as used by @c PXENV_UNDI_GET_NIC_TYPE
166
+	 */
167
+	unsigned int bus_type;
168
+	/** Device ID */
169
+	union pxe_device_id bus_id;
170
+};
171
+
65 172
 /** A PXE device */
66 173
 struct pxe_device {
67 174
 	/** Generic device */
@@ -83,8 +190,8 @@ struct pxe_device {
83 190
 	MAC_ADDR_t hwaddr;
84 191
 	/** Assigned IRQ number */
85 192
 	UINT16_t irq;
86
-	/** ROM address */
87
-	SEGSEL_t rom;
193
+	/** ROM segment address */
194
+	SEGSEL_t rom_segment;
88 195
 };
89 196
 
90 197
 /**
@@ -112,6 +219,10 @@ extern int pxe_call ( struct pxe_device *pxe, unsigned int function,
112 219
 extern int undi_probe ( struct pxe_device *pxe );
113 220
 extern void undi_remove ( struct pxe_device *pxe );
114 221
 
222
+extern struct pxe_driver * pxedrv_find_pci_driver ( unsigned int vendor_id,
223
+						    unsigned int device_id,
224
+						    unsigned int rombase );
225
+
115 226
 extern struct net_device *pxe_netdev;
116 227
 
117 228
 #endif /* PXE_H */

Loading…
Cancel
Save