Adding upstream version 3.53.
[syslinux-debian/hramrach.git] / com32 / modules / ethersel.c
blobad1d052d7abaa7476efc869c17c17ed03ad4dda1
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2005-2007 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * ethersel.c
16 * Search for an Ethernet card with a known PCI signature, and run
17 * the corresponding Ethernet module.
19 * To use this, set up a syslinux config file like this:
21 * PROMPT 0
22 * DEFAULT ethersel.c32
23 * # DEV [DID xxxx:yyyy[/mask]] [RID zz-zz] [SID uuuu:vvvv[/mask]] commandline
24 * # ...
26 * DID = PCI device ID
27 * RID = Revision ID (range)
28 * SID = Subsystem ID
31 #include <inttypes.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <console.h>
36 #include <sys/pci.h>
37 #include <com32.h>
39 #ifdef DEBUG
40 # define dprintf printf
41 #else
42 # define dprintf(...) ((void)0)
43 #endif
45 static const char *
46 get_config(void)
48 static com32sys_t r;
50 r.eax.w[0] = 0x000E;
51 __intcall(0x22, &r, &r);
53 return MK_PTR(r.es, r.ebx.w[0]);
56 static char *
57 skipspace(char *p)
59 while ( *p && *p <= ' ' )
60 p++;
62 return p;
65 #define MAX_LINE 512
67 /* Check to see if we are at a certain keyword (case insensitive) */
68 static int looking_at(const char *line, const char *kwd)
70 const char *p = line;
71 const char *q = kwd;
73 while ( *p && *q && ((*p^*q) & ~0x20) == 0 ) {
74 p++;
75 q++;
78 if ( *q )
79 return 0; /* Didn't see the keyword */
81 return *p <= ' '; /* Must be EOL or whitespace */
84 static char *
85 get_did(char *p, uint32_t *idptr, uint32_t *maskptr)
87 unsigned long vid, did, m1, m2;
89 *idptr = -1;
90 *maskptr = 0xffffffff;
92 vid = strtoul(p, &p, 16);
93 if ( *p != ':' )
94 return p; /* Bogus ID */
95 did = strtoul(p+1, &p, 16);
97 *idptr = (did << 16) + vid;
99 if ( *p == '/' ) {
100 m1 = strtoul(p+1, &p, 16);
101 if ( *p != ':' ) {
102 *maskptr = (m1 << 16) | 0xffff;
103 } else {
104 m2 = strtoul(p+1, &p, 16);
105 *maskptr = (m1 << 16) | m2;
109 return p;
112 static char *
113 get_rid_range(char *p, uint8_t *rid_min, uint8_t *rid_max)
115 unsigned long r0, r1;
117 p = skipspace(p+3);
119 r0 = strtoul(p, &p, 16);
120 if ( *p == '-' ) {
121 r1 = strtoul(p+1, &p, 16);
122 } else {
123 r1 = r0;
126 *rid_min = r0;
127 *rid_max = r1;
129 return p;
132 static struct match *
133 parse_config(const char *filename)
135 char line[MAX_LINE], *p;
136 FILE *f;
137 struct match *list = NULL;
138 struct match **ep = &list;
139 struct match *m;
141 if ( !filename )
142 filename = get_config();
144 f = fopen(filename, "r");
145 if ( !f )
146 return list;
148 while ( fgets(line, sizeof line, f) ) {
149 p = skipspace(line);
151 if ( !looking_at(p, "#") )
152 continue;
153 p = skipspace(p+1);
155 if ( !looking_at(p, "dev") )
156 continue;
157 p = skipspace(p+3);
159 m = malloc(sizeof(struct match));
160 if ( !m )
161 continue;
163 memset(m, 0, sizeof *m);
164 m->rid_max = 0xff;
166 for(;;) {
167 p = skipspace(p);
169 if ( looking_at(p, "did") ) {
170 p = get_did(p+3, &m->did, &m->did_mask);
171 } else if ( looking_at(p, "sid") ) {
172 p = get_did(p+3, &m->sid, &m->sid_mask);
173 } else if ( looking_at(p, "rid") ) {
174 p = get_rid_range(p+3, &m->rid_min, &m->rid_max);
175 } else {
176 char *e;
178 e = strchr(p, '\n');
179 if ( *e ) *e = '\0';
180 e = strchr(p, '\r');
181 if ( *e ) *e = '\0';
183 m->filename = strdup(p);
184 if ( !m->filename )
185 m->did = -1;
186 break; /* Done with this line */
190 dprintf("DEV DID %08x/%08x SID %08x/%08x RID %02x-%02x CMD %s\n",
191 m->did, m->did_mask, m->sid, m->sid_mask,
192 m->rid_min, m->rid_max, m->filename);
194 *ep = m;
195 ep = &m->next;
198 return list;
201 static void __attribute__((noreturn))
202 execute(const char *cmdline)
204 static com32sys_t ireg;
206 strcpy(__com32.cs_bounce, cmdline);
207 ireg.eax.w[0] = 0x0003; /* Run command */
208 ireg.ebx.w[0] = OFFS(__com32.cs_bounce);
209 ireg.es = SEG(__com32.cs_bounce);
210 __intcall(0x22, &ireg, NULL);
211 exit(255); /* Shouldn't return */
214 int main(int argc, char *argv[])
216 struct match *list, *match;
217 struct pci_device_list pci_device_list;
218 struct pci_bus_list pci_bus_list;
220 openconsole(&dev_null_r, &dev_stdcon_w);
221 pci_scan(&pci_bus_list,&pci_device_list);
223 list = parse_config(argc < 2 ? NULL : argv[1]);
225 match = find_pci_device(&pci_device_list,list);
227 if ( match )
228 execute(match->filename);
230 /* On error, return to the command line */
231 fputs("Error: no recognized network card found!\n", stderr);
232 return 1;