syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / com32 / modules / ethersel.c
1 #ident "$Id: ethersel.c,v 1.3 2005/01/05 07:30:47 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 2005 H. Peter Anvin - All Rights Reserved
5  *
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.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * ethersel.c
16  *
17  * Search for an Ethernet card with a known PCI signature, and run
18  * the corresponding Ethernet module.
19  *
20  * To use this, set up a syslinux config file like this:
21  *
22  * PROMPT 0
23  * DEFAULT ethersel.c32
24  * # DEV [DID xxxx:yyyy[/mask]] [RID zz-zz] [SID uuuu:vvvv[/mask]] commandline
25  * # ...
26  * 
27  * DID = PCI device ID
28  * RID = Revision ID (range)
29  * SID = Subsystem ID
30  */
31
32 #include <inttypes.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <console.h>
37 #include <sys/pci.h>
38 #include <com32.h>
39
40 #ifdef DEBUG
41 # define dprintf printf
42 #else
43 # define dprintf(...) ((void)0)
44 #endif
45
46 struct match {
47   struct match *next;
48   uint32_t did;
49   uint32_t did_mask;
50   uint32_t sid;
51   uint32_t sid_mask;
52   uint8_t rid_min, rid_max;
53   char *filename;
54 };
55
56 static const char *
57 get_config(void)
58 {
59   static com32sys_t r;
60
61   r.eax.w[0] = 0x000E;
62   __intcall(0x22, &r, &r);
63
64   return MK_PTR(r.es, r.ebx.w[0]);
65 }
66
67 static char *
68 skipspace(char *p)
69 {
70   while ( *p && *p <= ' ' )
71     p++;
72
73   return p;
74 }
75
76 #define MAX_LINE 512
77
78 /* Check to see if we are at a certain keyword (case insensitive) */
79 static int looking_at(const char *line, const char *kwd)
80 {
81   const char *p = line;
82   const char *q = kwd;
83
84   while ( *p && *q && ((*p^*q) & ~0x20) == 0 ) {
85     p++;
86     q++;
87   }
88
89   if ( *q )
90     return 0;                   /* Didn't see the keyword */
91
92   return *p <= ' ';             /* Must be EOL or whitespace */
93 }
94
95 static char *
96 get_did(char *p, uint32_t *idptr, uint32_t *maskptr)
97 {
98   unsigned long vid, did, m1, m2;
99
100   *idptr   = -1;
101   *maskptr = 0xffffffff;
102
103   vid = strtoul(p, &p, 16);
104   if ( *p != ':' )
105     return p;                   /* Bogus ID */
106   did = strtoul(p+1, &p, 16);
107
108   *idptr = (did << 16) + vid;
109
110   if ( *p == '/' ) {
111     m1 = strtoul(p+1, &p, 16);
112     if ( *p != ':' ) {
113       *maskptr = (m1 << 16) | 0xffff;
114     } else {
115       m2 = strtoul(p+1, &p, 16);
116       *maskptr = (m1 << 16) | m2;
117     }
118   }
119
120   return p;
121 }
122
123 static struct match *
124 parse_config(const char *filename)
125 {
126   char line[MAX_LINE], *p;
127   FILE *f;
128   struct match *list = NULL;
129   struct match **ep = &list;
130   struct match *m;
131
132   if ( !filename )
133     filename = get_config();
134
135   f = fopen(filename, "r");
136   if ( !f )
137     return list;
138
139   while ( fgets(line, sizeof line, f) ) {
140     p = skipspace(line);
141
142     if ( !looking_at(p, "#") )
143       continue;
144     p = skipspace(p+1);
145
146     if ( !looking_at(p, "dev") )
147       continue;
148     p = skipspace(p+3);
149
150     m = malloc(sizeof(struct match));
151     if ( !m )
152       continue;
153
154     memset(m, 0, sizeof *m);
155     m->rid_max = 0xff;
156
157     for(;;) {
158       p = skipspace(p);
159
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;
167
168         p = skipspace(p+3);
169
170         r0 = strtoul(p, &p, 16);
171         if ( *p == '-' ) {
172           r1 = strtoul(p+1, &p, 16);
173         } else {
174           r1 = r0;
175         }
176
177         m->rid_min = r0;
178         m->rid_max = r1;
179       } else {
180         char *e;
181
182         e = strchr(p, '\n');
183         if ( *e ) *e = '\0';
184         e = strchr(p, '\r');
185         if ( *e ) *e = '\0';
186
187         m->filename = strdup(p);
188         if ( !m->filename )
189           m->did = -1;
190         break;                  /* Done with this line */
191       }
192     }
193
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);
197
198     *ep = m;
199     ep = &m->next;
200   }
201
202   return list;
203 }
204
205 static struct match *
206 pciscan(struct match *list)
207 {
208   unsigned int bus, dev, func, maxfunc;
209   uint32_t did, sid;
210   uint8_t hdrtype, rid;
211   pciaddr_t a;
212   struct match *m;
213
214   for ( bus = 0 ; bus <= 0xff ; bus++ ) {
215     for ( dev = 0 ; dev <= 0x1f ; dev++ ) {
216       maxfunc = 0;
217       for ( func = 0 ; func <= maxfunc ; func++ ) {
218         a = pci_mkaddr(bus, dev, func, 0);
219
220         did = pci_readl(a);
221         
222         if ( did == 0xffffffff || did == 0xffff0000 ||
223              did == 0x0000ffff || did == 0x00000000 )
224           continue;
225
226         hdrtype = pci_readb(a + 0x0e);
227
228         if ( hdrtype & 0x80 )
229           maxfunc = 7;          /* Multifunction device */
230
231         if ( hdrtype & 0x7f )
232           continue;             /* Ignore bridge devices */
233
234         rid = pci_readb(a + 0x08);
235         sid = pci_readl(a + 0x2c);
236
237         dprintf("Scanning: DID %08x SID %08x RID %02x\n", did, sid, rid);
238
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 )
243             return m;
244         }
245       }
246     }
247   }
248
249   return NULL;
250 }
251
252 static void __attribute__((noreturn))
253 execute(const char *cmdline)
254 {
255   static com32sys_t ireg;
256
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 */
263 }
264
265 int main(int argc, char *argv[])
266 {
267   struct match *list, *match;
268
269   openconsole(&dev_null_r, &dev_stdcon_w);
270
271   list = parse_config(argc < 2 ? NULL : argv[1]);
272
273   match = pciscan(list);
274
275   if ( match )
276     execute(match->filename);
277
278   /* On error, return to the command line */
279   fputs("Error: no recognized network card found!\n", stderr);
280   return 1;
281 }
282