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 * ----------------------------------------------------------------------- */
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:
22 * DEFAULT ethersel.c32
23 * # DEV [DID xxxx:yyyy[/mask]] [RID zz-zz] [SID uuuu:vvvv[/mask]] commandline
27 * RID = Revision ID (range)
40 # define dprintf printf
42 # define dprintf(...) ((void)0)
51 __intcall(0x22, &r
, &r
);
53 return MK_PTR(r
.es
, r
.ebx
.w
[0]);
59 while ( *p
&& *p
<= ' ' )
67 /* Check to see if we are at a certain keyword (case insensitive) */
68 static int looking_at(const char *line
, const char *kwd
)
73 while ( *p
&& *q
&& ((*p
^*q
) & ~0x20) == 0 ) {
79 return 0; /* Didn't see the keyword */
81 return *p
<= ' '; /* Must be EOL or whitespace */
85 get_did(char *p
, uint32_t *idptr
, uint32_t *maskptr
)
87 unsigned long vid
, did
, m1
, m2
;
90 *maskptr
= 0xffffffff;
92 vid
= strtoul(p
, &p
, 16);
94 return p
; /* Bogus ID */
95 did
= strtoul(p
+1, &p
, 16);
97 *idptr
= (did
<< 16) + vid
;
100 m1
= strtoul(p
+1, &p
, 16);
102 *maskptr
= (m1
<< 16) | 0xffff;
104 m2
= strtoul(p
+1, &p
, 16);
105 *maskptr
= (m1
<< 16) | m2
;
113 get_rid_range(char *p
, uint8_t *rid_min
, uint8_t *rid_max
)
115 unsigned long r0
, r1
;
119 r0
= strtoul(p
, &p
, 16);
121 r1
= strtoul(p
+1, &p
, 16);
132 static struct match
*
133 parse_config(const char *filename
)
135 char line
[MAX_LINE
], *p
;
137 struct match
*list
= NULL
;
138 struct match
**ep
= &list
;
142 filename
= get_config();
144 f
= fopen(filename
, "r");
148 while ( fgets(line
, sizeof line
, f
) ) {
151 if ( !looking_at(p
, "#") )
155 if ( !looking_at(p
, "dev") )
159 m
= malloc(sizeof(struct match
));
163 memset(m
, 0, sizeof *m
);
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
);
183 m
->filename
= strdup(p
);
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
);
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
);
228 execute(match
->filename
);
230 /* On error, return to the command line */
231 fputs("Error: no recognized network card found!\n", stderr
);