Adding upstream version 3.86+dfsg.
[syslinux-debian/hramrach.git] / com32 / modules / ethersel.c
blobf586e8366abfccbe460a905e0b25c10162c8dd60
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 * ----------------------------------------------------------------------- */
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>
38 #include <syslinux/boot.h>
39 #include <syslinux/config.h>
41 #ifdef DEBUG
42 # define dprintf printf
43 #else
44 # define dprintf(...) ((void)0)
45 #endif
47 static char *skipspace(char *p)
49 while (*p && *p <= ' ')
50 p++;
52 return p;
55 #define MAX_LINE 512
57 /* Check to see if we are at a certain keyword (case insensitive) */
58 static int looking_at(const char *line, const char *kwd)
60 const char *p = line;
61 const char *q = kwd;
63 while (*p && *q && ((*p ^ *q) & ~0x20) == 0) {
64 p++;
65 q++;
68 if (*q)
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;
78 *idptr = -1;
79 *maskptr = 0xffffffff;
81 vid = strtoul(p, &p, 16);
82 if (*p != ':')
83 return p; /* Bogus ID */
84 did = strtoul(p + 1, &p, 16);
86 *idptr = (did << 16) + vid;
88 if (*p == '/') {
89 m1 = strtoul(p + 1, &p, 16);
90 if (*p != ':') {
91 *maskptr = (m1 << 16) | 0xffff;
92 } else {
93 m2 = strtoul(p + 1, &p, 16);
94 *maskptr = (m1 << 16) | m2;
98 return p;
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);
108 if (*p == '-') {
109 r1 = strtoul(p + 1, &p, 16);
110 } else {
111 r1 = r0;
114 *rid_min = r0;
115 *rid_max = r1;
117 return p;
120 static struct match *parse_config(const char *filename)
122 char line[MAX_LINE], *p;
123 FILE *f;
124 struct match *list = NULL;
125 struct match **ep = &list;
126 struct match *m;
128 if (!filename)
129 filename = syslinux_config_file();
131 f = fopen(filename, "r");
132 if (!f)
133 return list;
135 while (fgets(line, sizeof line, f)) {
136 p = skipspace(line);
138 if (!looking_at(p, "#"))
139 continue;
140 p = skipspace(p + 1);
142 if (!looking_at(p, "dev"))
143 continue;
144 p = skipspace(p + 3);
146 m = malloc(sizeof(struct match));
147 if (!m)
148 continue;
150 memset(m, 0, sizeof *m);
151 m->rid_max = 0xff;
153 for (;;) {
154 p = skipspace(p);
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);
162 } else {
163 char *e;
165 e = strchr(p, '\n');
166 if (*e)
167 *e = '\0';
168 e = strchr(p, '\r');
169 if (*e)
170 *e = '\0';
172 m->filename = strdup(p);
173 if (!m->filename)
174 m->did = -1;
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);
183 *ep = m;
184 ep = &m->next;
187 return list;
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();
198 if (pci_domain) {
199 list = parse_config(argc < 2 ? NULL : argv[1]);
201 match = find_pci_device(pci_domain, list);
203 if (match)
204 syslinux_run_command(match->filename);
207 /* On error, return to the command line */
208 fputs("Error: no recognized network card found!\n", stderr);
209 return 1;