1 /* ----------------------------------------------------------------------- *
3 * Copyright 2005-2008 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)
38 #include <syslinux/boot.h>
39 #include <syslinux/config.h>
42 # define dprintf printf
44 # define dprintf(...) ((void)0)
47 static char *skipspace(char *p
)
49 while (*p
&& *p
<= ' ')
57 /* Check to see if we are at a certain keyword (case insensitive) */
58 static int looking_at(const char *line
, const char *kwd
)
63 while (*p
&& *q
&& ((*p
^ *q
) & ~0x20) == 0) {
69 return 0; /* Didn't see the keyword */
71 return *p
<= ' '; /* Must be EOL or whitespace */
74 static char *get_did(char *p
, uint32_t * idptr
, uint32_t * maskptr
)
76 unsigned long vid
, did
, m1
, m2
;
79 *maskptr
= 0xffffffff;
81 vid
= strtoul(p
, &p
, 16);
83 return p
; /* Bogus ID */
84 did
= strtoul(p
+ 1, &p
, 16);
86 *idptr
= (did
<< 16) + vid
;
89 m1
= strtoul(p
+ 1, &p
, 16);
91 *maskptr
= (m1
<< 16) | 0xffff;
93 m2
= strtoul(p
+ 1, &p
, 16);
94 *maskptr
= (m1
<< 16) | m2
;
101 static char *get_rid_range(char *p
, uint8_t * rid_min
, uint8_t * rid_max
)
103 unsigned long r0
, r1
;
105 p
= skipspace(p
+ 3);
107 r0
= strtoul(p
, &p
, 16);
109 r1
= strtoul(p
+ 1, &p
, 16);
120 static struct match
*parse_config(const char *filename
)
122 char line
[MAX_LINE
], *p
;
124 struct match
*list
= NULL
;
125 struct match
**ep
= &list
;
129 filename
= syslinux_config_file();
131 f
= fopen(filename
, "r");
135 while (fgets(line
, sizeof line
, f
)) {
138 if (!looking_at(p
, "#"))
140 p
= skipspace(p
+ 1);
142 if (!looking_at(p
, "dev"))
144 p
= skipspace(p
+ 3);
146 m
= malloc(sizeof(struct match
));
150 memset(m
, 0, sizeof *m
);
156 if (looking_at(p
, "did")) {
157 p
= get_did(p
+ 3, &m
->did
, &m
->did_mask
);
158 } else if (looking_at(p
, "sid")) {
159 p
= get_did(p
+ 3, &m
->sid
, &m
->sid_mask
);
160 } else if (looking_at(p
, "rid")) {
161 p
= get_rid_range(p
+ 3, &m
->rid_min
, &m
->rid_max
);
172 m
->filename
= strdup(p
);
175 break; /* Done with this line */
179 dprintf("DEV DID %08x/%08x SID %08x/%08x RID %02x-%02x CMD %s\n",
180 m
->did
, m
->did_mask
, m
->sid
, m
->sid_mask
,
181 m
->rid_min
, m
->rid_max
, m
->filename
);
190 int main(int argc
, char *argv
[])
192 struct match
*list
, *match
;
193 struct pci_domain
*pci_domain
;
195 openconsole(&dev_null_r
, &dev_stdcon_w
);
196 pci_domain
= pci_scan();
199 list
= parse_config(argc
< 2 ? NULL
: argv
[1]);
201 match
= find_pci_device(pci_domain
, list
);
204 syslinux_run_command(match
->filename
);
207 /* On error, return to the command line */
208 fputs("Error: no recognized network card found!\n", stderr
);