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 );
35 /** List of all UNDI ROMs */
36 static LIST_HEAD ( undiroms );
39 * Parse PXE ROM ID structure
42 * @v pxeromid Offset within ROM to PXE ROM ID structure
43 * @ret rc Return status code
45 static int undirom_parse_pxeromid ( struct undi_rom *undirom,
46 unsigned int pxeromid ) {
47 struct undi_rom_id undi_rom_id;
48 unsigned int undiloader;
50 DBGC ( undirom, "UNDIROM %p has PXE ROM ID at %04x:%04x\n", undirom,
51 undirom->rom_segment, pxeromid );
53 /* Read PXE ROM ID structure and verify */
54 copy_from_real ( &undi_rom_id, undirom->rom_segment, pxeromid,
55 sizeof ( undi_rom_id ) );
56 if ( undi_rom_id.Signature != UNDI_ROM_ID_SIGNATURE ) {
57 DBGC ( undirom, "UNDIROM %p has bad PXE ROM ID signature "
58 "%08x\n", undirom, undi_rom_id.Signature );
62 /* Check for UNDI loader */
63 undiloader = undi_rom_id.UNDILoader;
65 DBGC ( undirom, "UNDIROM %p has no UNDI loader\n", undirom );
69 /* Fill in UNDI ROM loader fields */
70 undirom->loader_entry.segment = undirom->rom_segment;
71 undirom->loader_entry.offset = undiloader;
72 undirom->code_size = undi_rom_id.CodeSize;
73 undirom->data_size = undi_rom_id.DataSize;
75 DBGC ( undirom, "UNDIROM %p has UNDI loader at %04x:%04x "
76 "(code %04zx data %04zx)\n", undirom,
77 undirom->loader_entry.segment, undirom->loader_entry.offset,
78 undirom->code_size, undirom->data_size );
83 * Parse PCI expansion header
86 * @v pcirheader Offset within ROM to PCI expansion header
88 static int undirom_parse_pcirheader ( struct undi_rom *undirom,
89 unsigned int pcirheader ) {
90 struct pcir_header pcir_header;
92 DBGC ( undirom, "UNDIROM %p has PCI expansion header at %04x:%04x\n",
93 undirom, undirom->rom_segment, pcirheader );
95 /* Read PCI expansion header and verify */
96 copy_from_real ( &pcir_header, undirom->rom_segment, pcirheader,
97 sizeof ( pcir_header ) );
98 if ( pcir_header.signature != PCIR_SIGNATURE ) {
99 DBGC ( undirom, "UNDIROM %p has bad PCI expansion header "
100 "signature %08x\n", undirom, pcir_header.signature );
104 /* Fill in UNDI ROM PCI device fields */
105 undirom->bus_type = PCI_NIC;
106 undirom->bus_id.pci.vendor_id = pcir_header.vendor_id;
107 undirom->bus_id.pci.device_id = pcir_header.device_id;
109 DBGC ( undirom, "UNDIROM %p is for PCI devices %04x:%04x\n", undirom,
110 undirom->bus_id.pci.vendor_id, undirom->bus_id.pci.device_id );
118 * @v rom_segment ROM segment address
119 * @ret rc Return status code
121 static int undirom_probe ( unsigned int rom_segment ) {
122 struct undi_rom *undirom = NULL;
123 struct undi_rom_header romheader;
125 unsigned int pxeromid;
126 unsigned int pcirheader;
129 /* Read expansion ROM header and verify */
130 copy_from_real ( &romheader, rom_segment, 0, sizeof ( romheader ) );
131 if ( romheader.Signature != ROM_SIGNATURE ) {
135 rom_len = ( romheader.ROMLength * 512 );
137 /* Allocate memory for UNDI ROM */
138 undirom = zalloc ( sizeof ( *undirom ) );
140 DBG ( "Could not allocate UNDI ROM structure\n" );
144 DBGC ( undirom, "UNDIROM %p trying expansion ROM at %04x:0000 "
145 "(%zdkB)\n", undirom, rom_segment, ( rom_len / 1024 ) );
146 undirom->rom_segment = rom_segment;
148 /* Check for and parse PXE ROM ID */
149 pxeromid = romheader.PXEROMID;
151 DBGC ( undirom, "UNDIROM %p has no PXE ROM ID\n", undirom );
155 if ( pxeromid > rom_len ) {
156 DBGC ( undirom, "UNDIROM %p PXE ROM ID outside ROM\n",
161 if ( ( rc = undirom_parse_pxeromid ( undirom, pxeromid ) ) != 0 )
164 /* Parse PCIR header, if present */
165 pcirheader = romheader.PCIRHeader;
167 undirom_parse_pcirheader ( undirom, pcirheader );
169 /* Add to UNDI ROM list and return */
170 DBGC ( undirom, "UNDIROM %p registered\n", undirom );
171 list_add ( &undirom->list, &undiroms );
180 * Create UNDI ROMs for all possible expansion ROMs
184 static void undirom_probe_all_roms ( void ) {
185 static int probed = 0;
186 unsigned int rom_segment;
188 /* Perform probe only once */
192 DBG ( "Scanning for PXE expansion ROMs\n" );
194 /* Scan through expansion ROM region at 512 byte intervals */
195 for ( rom_segment = 0xc000 ; rom_segment < 0x10000 ;
196 rom_segment += 0x20 ) {
197 undirom_probe ( rom_segment );
204 * Find UNDI ROM for PCI device
206 * @v vendor_id PCI vendor ID
207 * @v device_id PCI device ID
208 * @v rombase ROM base address, or 0 for any
209 * @ret undirom UNDI ROM, or NULL
211 struct undi_rom * undirom_find_pci ( unsigned int vendor_id,
212 unsigned int device_id,
213 unsigned int rombase ) {
214 struct undi_rom *undirom;
216 undirom_probe_all_roms();
218 list_for_each_entry ( undirom, &undiroms, list ) {
219 if ( undirom->bus_type != PCI_NIC )
221 if ( undirom->bus_id.pci.vendor_id != vendor_id )
223 if ( undirom->bus_id.pci.device_id != device_id )
225 if ( rombase && ( ( undirom->rom_segment << 4 ) != rombase ) )
227 DBGC ( undirom, "UNDIROM %p matched PCI %04x:%04x (%08x)\n",
228 undirom, vendor_id, device_id, rombase );
232 DBG ( "No UNDI ROM matched PCI %04x:%04x (%08x)\n",
233 vendor_id, device_id, rombase );