1 #ident "$Id: ethersel.c,v 1.3 2005/01/05 07:30:47 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
4 * Copyright 2005 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
17 * Search for an Ethernet card with a known PCI signature, and run
18 * the corresponding Ethernet module.
20 * To use this, set up a syslinux config file like this:
23 * DEFAULT ethersel.c32
24 * # DEV [DID xxxx:yyyy[/mask]] [RID zz-zz] [SID uuuu:vvvv[/mask]] commandline
28 * RID = Revision ID (range)
41 # define dprintf printf
43 # define dprintf(...) ((void)0)
52 uint8_t rid_min, rid_max;
62 __intcall(0x22, &r, &r);
64 return MK_PTR(r.es, r.ebx.w[0]);
70 while ( *p && *p <= ' ' )
78 /* Check to see if we are at a certain keyword (case insensitive) */
79 static int looking_at(const char *line, const char *kwd)
84 while ( *p && *q && ((*p^*q) & ~0x20) == 0 ) {
90 return 0; /* Didn't see the keyword */
92 return *p <= ' '; /* Must be EOL or whitespace */
96 get_did(char *p, uint32_t *idptr, uint32_t *maskptr)
98 unsigned long vid, did, m1, m2;
101 *maskptr = 0xffffffff;
103 vid = strtoul(p, &p, 16);
105 return p; /* Bogus ID */
106 did = strtoul(p+1, &p, 16);
108 *idptr = (did << 16) + vid;
111 m1 = strtoul(p+1, &p, 16);
113 *maskptr = (m1 << 16) | 0xffff;
115 m2 = strtoul(p+1, &p, 16);
116 *maskptr = (m1 << 16) | m2;
123 static struct match *
124 parse_config(const char *filename)
126 char line[MAX_LINE], *p;
128 struct match *list = NULL;
129 struct match **ep = &list;
133 filename = get_config();
135 f = fopen(filename, "r");
139 while ( fgets(line, sizeof line, f) ) {
142 if ( !looking_at(p, "#") )
146 if ( !looking_at(p, "dev") )
150 m = malloc(sizeof(struct match));
154 memset(m, 0, sizeof *m);
160 if ( looking_at(p, "did") ) {
161 p = get_did(p+3, &m->did, &m->did_mask);
162 m->did_mask = 0xffffffff;
163 } else if ( looking_at(p, "sid") ) {
164 p = get_did(p+3, &m->sid, &m->sid_mask);
165 } else if ( looking_at(p, "rid") ) {
166 unsigned long r0, r1;
170 r0 = strtoul(p, &p, 16);
172 r1 = strtoul(p+1, &p, 16);
187 m->filename = strdup(p);
190 break; /* Done with this line */
194 dprintf("DEV DID %08x/%08x SID %08x/%08x RID %02x-%02x CMD %s\n",
195 m->did, m->did_mask, m->sid, m->sid_mask,
196 m->rid_min, m->rid_max, m->filename);
205 static struct match *
206 pciscan(struct match *list)
208 unsigned int bus, dev, func, maxfunc;
210 uint8_t hdrtype, rid;
214 for ( bus = 0 ; bus <= 0xff ; bus++ ) {
215 for ( dev = 0 ; dev <= 0x1f ; dev++ ) {
217 for ( func = 0 ; func <= maxfunc ; func++ ) {
218 a = pci_mkaddr(bus, dev, func, 0);
222 if ( did == 0xffffffff || did == 0xffff0000 ||
223 did == 0x0000ffff || did == 0x00000000 )
226 hdrtype = pci_readb(a + 0x0e);
228 if ( hdrtype & 0x80 )
229 maxfunc = 7; /* Multifunction device */
231 if ( hdrtype & 0x7f )
232 continue; /* Ignore bridge devices */
234 rid = pci_readb(a + 0x08);
235 sid = pci_readl(a + 0x2c);
237 dprintf("Scanning: DID %08x SID %08x RID %02x\n", did, sid, rid);
239 for ( m = list ; m ; m = m->next ) {
240 if ( ((did ^ m->did) & m->did_mask) == 0 &&
241 ((sid ^ m->sid) & m->sid_mask) == 0 &&
242 rid >= m->rid_min && rid <= m->rid_max )
252 static void __attribute__((noreturn))
253 execute(const char *cmdline)
255 static com32sys_t ireg;
257 strcpy(__com32.cs_bounce, cmdline);
258 ireg.eax.w[0] = 0x0003; /* Run command */
259 ireg.ebx.w[0] = OFFS(__com32.cs_bounce);
260 ireg.es = SEG(__com32.cs_bounce);
261 __intcall(0x22, &ireg, NULL);
262 exit(255); /* Shouldn't return */
265 int main(int argc, char *argv[])
267 struct match *list, *match;
269 openconsole(&dev_null_r, &dev_stdcon_w);
271 list = parse_config(argc < 2 ? NULL : argv[1]);
273 match = pciscan(list);
276 execute(match->filename);
278 /* On error, return to the command line */
279 fputs("Error: no recognized network card found!\n", stderr);