summaryrefslogtreecommitdiff
path: root/com32/modules/ethersel.c
blob: 4b197c728800a2144c527fcefc31a64c5087c553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2005 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * ethersel.c
 *
 * Search for an Ethernet card with a known PCI signature, and run
 * the corresponding Ethernet module.
 *
 * To use this, set up a syslinux config file like this:
 *
 * PROMPT 0
 * DEFAULT ethersel.c32
 * # DEV [DID xxxx:yyyy[/mask]] [RID zz-zz] [SID uuuu:vvvv[/mask]] commandline
 * # ...
 * 
 * DID = PCI device ID
 * RID = Revision ID (range)
 * SID = Subsystem ID
 */

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <console.h>
#include <sys/pci.h>
#include <com32.h>

#ifdef DEBUG
# define dprintf printf
#else
# define dprintf(...) ((void)0)
#endif

struct match {
  struct match *next;
  uint32_t did;
  uint32_t did_mask;
  uint32_t sid;
  uint32_t sid_mask;
  uint8_t rid_min, rid_max;
  char *filename;
};

static const char *
get_config(void)
{
  static com32sys_t r;

  r.eax.w[0] = 0x000E;
  __intcall(0x22, &r, &r);

  return MK_PTR(r.es, r.ebx.w[0]);
}

static char *
skipspace(char *p)
{
  while ( *p && *p <= ' ' )
    p++;

  return p;
}

#define MAX_LINE 512

/* Check to see if we are at a certain keyword (case insensitive) */
static int looking_at(const char *line, const char *kwd)
{
  const char *p = line;
  const char *q = kwd;

  while ( *p && *q && ((*p^*q) & ~0x20) == 0 ) {
    p++;
    q++;
  }

  if ( *q )
    return 0;                   /* Didn't see the keyword */

  return *p <= ' ';             /* Must be EOL or whitespace */
}

static char *
get_did(char *p, uint32_t *idptr, uint32_t *maskptr)
{
  unsigned long vid, did, m1, m2;

  *idptr   = -1;
  *maskptr = 0xffffffff;

  vid = strtoul(p, &p, 16);
  if ( *p != ':' )
    return p;			/* Bogus ID */
  did = strtoul(p+1, &p, 16);

  *idptr = (did << 16) + vid;

  if ( *p == '/' ) {
    m1 = strtoul(p+1, &p, 16);
    if ( *p != ':' ) {
      *maskptr = (m1 << 16) | 0xffff;
    } else {
      m2 = strtoul(p+1, &p, 16);
      *maskptr = (m1 << 16) | m2;
    }
  }

  return p;
}

static struct match *
parse_config(const char *filename)
{
  char line[MAX_LINE], *p;
  FILE *f;
  struct match *list = NULL;
  struct match **ep = &list;
  struct match *m;

  if ( !filename )
    filename = get_config();

  f = fopen(filename, "r");
  if ( !f )
    return list;

  while ( fgets(line, sizeof line, f) ) {
    p = skipspace(line);

    if ( !looking_at(p, "#") )
      continue;
    p = skipspace(p+1);

    if ( !looking_at(p, "dev") )
      continue;
    p = skipspace(p+3);

    m = malloc(sizeof(struct match));
    if ( !m )
      continue;

    memset(m, 0, sizeof *m);
    m->rid_max = 0xff;

    for(;;) {
      p = skipspace(p);

      if ( looking_at(p, "did") ) {
	p = get_did(p+3, &m->did, &m->did_mask);
	m->did_mask = 0xffffffff;
      } else if ( looking_at(p, "sid") ) {
	p = get_did(p+3, &m->sid, &m->sid_mask);
      } else if ( looking_at(p, "rid") ) {
	unsigned long r0, r1;

	p = skipspace(p+3);

	r0 = strtoul(p, &p, 16);
	if ( *p == '-' ) {
	  r1 = strtoul(p+1, &p, 16);
	} else {
	  r1 = r0;
	}

	m->rid_min = r0;
	m->rid_max = r1;
      } else {
	char *e;

	e = strchr(p, '\n');
	if ( *e ) *e = '\0';
	e = strchr(p, '\r');
	if ( *e ) *e = '\0';

	m->filename = strdup(p);
	if ( !m->filename )
	  m->did = -1;
	break;			/* Done with this line */
      }
    }

    dprintf("DEV DID %08x/%08x SID %08x/%08x RID %02x-%02x CMD %s\n",
	    m->did, m->did_mask, m->sid, m->sid_mask,
	    m->rid_min, m->rid_max, m->filename);

    *ep = m;
    ep = &m->next;
  }

  return list;
}

static struct match *
pciscan(struct match *list)
{
  unsigned int bus, dev, func, maxfunc;
  uint32_t did, sid;
  uint8_t hdrtype, rid;
  pciaddr_t a;
  struct match *m;

  for ( bus = 0 ; bus <= 0xff ; bus++ ) {
    for ( dev = 0 ; dev <= 0x1f ; dev++ ) {
      maxfunc = 0;
      for ( func = 0 ; func <= maxfunc ; func++ ) {
	a = pci_mkaddr(bus, dev, func, 0);

	did = pci_readl(a);
	
	if ( did == 0xffffffff || did == 0xffff0000 ||
	     did == 0x0000ffff || did == 0x00000000 )
	  continue;

	hdrtype = pci_readb(a + 0x0e);

	if ( hdrtype & 0x80 )
	  maxfunc = 7;		/* Multifunction device */

	if ( hdrtype & 0x7f )
	  continue;		/* Ignore bridge devices */

	rid = pci_readb(a + 0x08);
	sid = pci_readl(a + 0x2c);

	dprintf("Scanning: DID %08x SID %08x RID %02x\n", did, sid, rid);

	for ( m = list ; m ; m = m->next ) {
	  if ( ((did ^ m->did) & m->did_mask) == 0 &&
	       ((sid ^ m->sid) & m->sid_mask) == 0 &&
	       rid >= m->rid_min && rid <= m->rid_max )
	    return m;
	}
      }
    }
  }

  return NULL;
}

static void __attribute__((noreturn))
execute(const char *cmdline)
{
  static com32sys_t ireg;

  strcpy(__com32.cs_bounce, cmdline);
  ireg.eax.w[0] = 0x0003;       /* Run command */
  ireg.ebx.w[0] = OFFS(__com32.cs_bounce);
  ireg.es = SEG(__com32.cs_bounce);
  __intcall(0x22, &ireg, NULL);
  exit(255);  /* Shouldn't return */
}

int main(int argc, char *argv[])
{
  struct match *list, *match;

  openconsole(&dev_null_r, &dev_stdcon_w);

  list = parse_config(argc < 2 ? NULL : argv[1]);

  match = pciscan(list);

  if ( match )
    execute(match->filename);

  /* On error, return to the command line */
  fputs("Error: no recognized network card found!\n", stderr);
  return 1;
}