dmi: check both the AC and ID flags at the same time
[syslinux.git] / com32 / modules / ethersel.c
blob039de27659fc53a2e76ec79902fb1fac3e9bdfb6
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 <ctype.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <console.h>
37 #include <sys/pci.h>
38 #include <com32.h>
39 #include <syslinux/boot.h>
40 #include <syslinux/config.h>
41 #include <dprintf.h>
43 #define MAX_LINE 512
45 /* Check to see if we are at a certain keyword (case insensitive) */
46 static int looking_at(const char *line, const char *kwd)
48 const char *p = line;
49 const char *q = kwd;
51 while (*p && *q && ((*p ^ *q) & ~0x20) == 0) {
52 p++;
53 q++;
56 if (*q)
57 return 0; /* Didn't see the keyword */
59 return *p <= ' '; /* Must be EOL or whitespace */
62 static char *get_did(char *p, uint32_t * idptr, uint32_t * maskptr)
64 unsigned long vid, did, m1, m2;
66 *idptr = -1;
67 *maskptr = 0xffffffff;
69 vid = strtoul(p, &p, 16);
70 if (*p != ':')
71 return p; /* Bogus ID */
72 did = strtoul(p + 1, &p, 16);
74 *idptr = (did << 16) + vid;
76 if (*p == '/') {
77 m1 = strtoul(p + 1, &p, 16);
78 if (*p != ':') {
79 *maskptr = (m1 << 16) | 0xffff;
80 } else {
81 m2 = strtoul(p + 1, &p, 16);
82 *maskptr = (m1 << 16) | m2;
86 return p;
89 static char *get_rid_range(char *p, uint8_t * rid_min, uint8_t * rid_max)
91 unsigned long r0, r1;
93 p = skipspace(p + 3);
95 r0 = strtoul(p, &p, 16);
96 if (*p == '-') {
97 r1 = strtoul(p + 1, &p, 16);
98 } else {
99 r1 = r0;
102 *rid_min = r0;
103 *rid_max = r1;
105 return p;
108 static struct match *parse_config(const char *filename)
110 char line[MAX_LINE], *p;
111 FILE *f;
112 struct match *list = NULL;
113 struct match **ep = &list;
114 struct match *m;
116 if (!filename)
117 filename = syslinux_config_file();
119 f = fopen(filename, "r");
120 if (!f)
121 return list;
123 while (fgets(line, sizeof line, f)) {
124 p = skipspace(line);
126 if (!looking_at(p, "#"))
127 continue;
128 p = skipspace(p + 1);
130 if (!looking_at(p, "dev"))
131 continue;
132 p = skipspace(p + 3);
134 m = malloc(sizeof(struct match));
135 if (!m)
136 continue;
138 memset(m, 0, sizeof *m);
139 m->rid_max = 0xff;
141 for (;;) {
142 p = skipspace(p);
144 if (looking_at(p, "did")) {
145 p = get_did(p + 3, &m->did, &m->did_mask);
146 } else if (looking_at(p, "sid")) {
147 p = get_did(p + 3, &m->sid, &m->sid_mask);
148 } else if (looking_at(p, "rid")) {
149 p = get_rid_range(p + 3, &m->rid_min, &m->rid_max);
150 } else {
151 char *e;
153 e = strchr(p, '\n');
154 if (*e)
155 *e = '\0';
156 e = strchr(p, '\r');
157 if (*e)
158 *e = '\0';
160 m->filename = strdup(p);
161 if (!m->filename)
162 m->did = -1;
163 break; /* Done with this line */
167 dprintf("DEV DID %08x/%08x SID %08x/%08x RID %02x-%02x CMD %s\n",
168 m->did, m->did_mask, m->sid, m->sid_mask,
169 m->rid_min, m->rid_max, m->filename);
171 *ep = m;
172 ep = &m->next;
175 return list;
178 int main(int argc, char *argv[])
180 struct match *list, *match;
181 struct pci_domain *pci_domain;
183 pci_domain = pci_scan();
185 if (pci_domain) {
186 list = parse_config(argc < 2 ? NULL : argv[1]);
188 match = find_pci_device(pci_domain, list);
190 if (match)
191 syslinux_run_command(match->filename);
194 /* On error, return to the command line */
195 fputs("Error: no recognized network card found!\n", stderr);
196 return 1;