3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
41 #include <sys/queue.h>
48 const char *words[W_MAX]; /* More than we'll need. */
51 static struct section_head sh;
52 static struct reg_head rh;
53 static struct assign_head ah;
55 static char *sstrdup (const char *);
57 *find_assign (const char *, const char *);
62 *find_section (const char *);
63 static void dump_deviceids_pci (void);
64 static void dump_deviceids_pcmcia (void);
65 static void dump_pci_id (const char *);
66 static void dump_pcmcia_id (const char *);
67 static void dump_regvals (void);
68 static void dump_paramreg (const struct section *,
69 const struct reg *, int);
74 inf_parse (FILE *fp, FILE *outfp)
85 dump_deviceids_pcmcia();
86 fprintf(outfp, "#ifdef NDIS_REGVALS\n");
88 fprintf(outfp, "#endif /* NDIS_REGVALS */\n");
94 section_add (const char *s)
98 sec = malloc(sizeof(struct section));
99 bzero(sec, sizeof(struct section));
101 TAILQ_INSERT_TAIL(&sh, sec, link);
106 static struct assign *
107 find_assign (const char *s, const char *k)
109 struct assign *assign;
112 /* Deal with string section lookups. */
114 if (k != NULL && k[0] == '%') {
115 bzero(newkey, sizeof(newkey));
116 strncpy(newkey, k + 1, strlen(k) - 2);
120 TAILQ_FOREACH(assign, &ah, link) {
121 if (strcasecmp(assign->section->name, s) == 0) {
125 if (strcasecmp(assign->key, k) == 0)
132 static struct assign *
133 find_next_assign (struct assign *a)
135 struct assign *assign;
137 TAILQ_FOREACH(assign, &ah, link) {
142 assign = assign->link.tqe_next;
144 if (assign == NULL || assign->section != a->section)
151 stringcvt(const char *s)
155 manf = find_assign("strings", s);
158 return(manf->vals[0]);
162 find_section (const char *s)
164 struct section *section;
166 TAILQ_FOREACH(section, &sh, link) {
167 if (strcasecmp(section->name, s) == 0)
174 dump_pcmcia_id(const char *s)
176 char *manstr, *devstr;
179 p0 = __DECONST(char *, s);
181 p = strchr(p0, '\\');
193 /* Convert any underscores to spaces. */
195 while (*p0 != '\0') {
209 /* Convert any underscores to spaces. */
211 while (*p0 != '\0') {
217 fprintf(ofp, "\t\\\n\t{ \"%s\", \"%s\", ", manstr, devstr);
222 dump_pci_id(const char *s)
225 char vidstr[7], didstr[7], subsysstr[14];
227 p = strcasestr(s, "VEN_");
231 strcpy(vidstr, "0x");
232 strncat(vidstr, p, 4);
233 p = strcasestr(s, "DEV_");
237 strcpy(didstr, "0x");
238 strncat(didstr, p, 4);
241 p = strcasestr(s, "SUBSYS_");
243 strcpy(subsysstr, "0x00000000");
246 strcpy(subsysstr, "0x");
247 strncat(subsysstr, p, 8);
250 fprintf(ofp, "\t\\\n\t{ %s, %s, %s, ", vidstr, didstr, subsysstr);
257 struct assign *manf, *dev;
259 struct assign *assign;
263 /* Emit start of PCI device table */
264 fprintf (ofp, "#define NDIS_PCI_DEV_TABLE");
266 /* Find manufacturer name */
267 manf = find_assign("Manufacturer", NULL);
271 /* Find manufacturer section */
272 if (manf->vals[1] != NULL &&
273 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
274 strcasecmp(manf->vals[1], "NTx86") == 0 ||
275 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
276 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
277 /* Handle Windows XP INF files. */
278 snprintf(xpsec, sizeof(xpsec), "%s.%s",
279 manf->vals[0], manf->vals[1]);
280 sec = find_section(xpsec);
282 sec = find_section(manf->vals[0]);
284 /* See if there are any PCI device definitions. */
286 TAILQ_FOREACH(assign, &ah, link) {
287 if (assign->section == sec) {
288 dev = find_assign("strings", assign->key);
289 if (strcasestr(assign->vals[1], "PCI") != NULL) {
304 * Now run through all the device names listed
305 * in the manufacturer section and dump out the
306 * device descriptions and vendor/device IDs.
309 TAILQ_FOREACH(assign, &ah, link) {
310 if (assign->section == sec) {
311 dev = find_assign("strings", assign->key);
312 /* Emit device IDs. */
313 if (strcasestr(assign->vals[1], "PCI") != NULL)
314 dump_pci_id(assign->vals[1]);
317 /* Emit device description */
318 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
323 /* Someone tried to fool us. Shame on them. */
326 sec = find_section(manf->vals[0]);
330 /* Handle Manufacturer sections with multiple entries. */
331 manf = find_next_assign(manf);
337 /* Emit end of table */
339 fprintf(ofp, "\n\n");
345 dump_deviceids_pcmcia()
347 struct assign *manf, *dev;
349 struct assign *assign;
353 /* Find manufacturer name */
354 manf = find_assign("Manufacturer", NULL);
358 /* Find manufacturer section */
359 if (manf->vals[1] != NULL &&
360 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
361 strcasecmp(manf->vals[1], "NTx86") == 0 ||
362 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
363 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
364 /* Handle Windows XP INF files. */
365 snprintf(xpsec, sizeof(xpsec), "%s.%s",
366 manf->vals[0], manf->vals[1]);
367 sec = find_section(xpsec);
369 sec = find_section(manf->vals[0]);
371 /* See if there are any PCMCIA device definitions. */
373 TAILQ_FOREACH(assign, &ah, link) {
374 if (assign->section == sec) {
375 dev = find_assign("strings", assign->key);
376 if (strcasestr(assign->vals[1], "PCMCIA") != NULL) {
388 /* Emit start of PCMCIA device table */
389 fprintf (ofp, "#define NDIS_PCMCIA_DEV_TABLE");
394 * Now run through all the device names listed
395 * in the manufacturer section and dump out the
396 * device descriptions and vendor/device IDs.
399 TAILQ_FOREACH(assign, &ah, link) {
400 if (assign->section == sec) {
401 dev = find_assign("strings", assign->key);
402 /* Emit device IDs. */
403 if (strcasestr(assign->vals[1], "PCMCIA") != NULL)
404 dump_pcmcia_id(assign->vals[1]);
407 /* Emit device description */
408 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
413 /* Someone tried to fool us. Shame on them. */
416 sec = find_section(manf->vals[0]);
420 /* Handle Manufacturer sections with multiple entries. */
421 manf = find_next_assign(manf);
427 /* Emit end of table */
429 fprintf(ofp, "\n\n");
435 dump_addreg(const char *s, int devidx)
440 /* Find the addreg section */
441 sec = find_section(s);
443 /* Dump all the keys defined in it. */
444 TAILQ_FOREACH(reg, &rh, link) {
446 * Keys with an empty subkey are very easy to parse,
447 * so just deal with them here. If a parameter key
448 * of the same name also exists, prefer that one and
451 if (reg->section == sec) {
452 if (reg->subkey == NULL) {
453 fprintf(ofp, "\n\t{ \"%s\",", reg->key);
454 fprintf(ofp,"\n\t\"%s \",", reg->key);
455 fprintf(ofp, "\n\t{ \"%s\" }, %d },",
456 reg->value == NULL ? "" :
457 stringcvt(reg->value), devidx);
458 } else if (strncasecmp(reg->subkey,
459 "Ndi\\params", strlen("Ndi\\params")-1) == 0 &&
460 (reg->key != NULL && strcasecmp(reg->key,
462 dump_paramreg(sec, reg, devidx);
470 dump_enumreg(const struct section *s, const struct reg *r)
475 sprintf(enumkey, "%s\\enum", r->subkey);
476 TAILQ_FOREACH(reg, &rh, link) {
477 if (reg->section != s)
479 if (reg->subkey == NULL || strcasecmp(reg->subkey, enumkey))
481 fprintf(ofp, " [%s=%s]", reg->key,
482 stringcvt(reg->value));
488 dump_editreg(const struct section *s, const struct reg *r)
492 TAILQ_FOREACH(reg, &rh, link) {
493 if (reg->section != s)
495 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
497 if (reg->key == NULL)
499 if (strcasecmp(reg->key, "LimitText") == 0)
500 fprintf(ofp, " [maxchars=%s]", reg->value);
501 if (strcasecmp(reg->key, "Optional") == 0 &&
502 strcmp(reg->value, "1") == 0)
503 fprintf(ofp, " [optional]");
508 /* Use this for int too */
510 dump_dwordreg(const struct section *s, const struct reg *r)
514 TAILQ_FOREACH(reg, &rh, link) {
515 if (reg->section != s)
517 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
519 if (reg->key == NULL)
521 if (strcasecmp(reg->key, "min") == 0)
522 fprintf(ofp, " [min=%s]", reg->value);
523 if (strcasecmp(reg->key, "max") == 0)
524 fprintf(ofp, " [max=%s]", reg->value);
530 dump_defaultinfo(const struct section *s, const struct reg *r, int devidx)
533 TAILQ_FOREACH(reg, &rh, link) {
534 if (reg->section != s)
536 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
538 if (reg->key == NULL || strcasecmp(reg->key, "Default"))
540 fprintf(ofp, "\n\t{ \"%s\" }, %d },", reg->value == NULL ? "" :
541 stringcvt(reg->value), devidx);
544 /* Default registry entry missing */
545 fprintf(ofp, "\n\t{ \"\" }, %d },", devidx);
550 dump_paramdesc(const struct section *s, const struct reg *r)
553 TAILQ_FOREACH(reg, &rh, link) {
554 if (reg->section != s)
556 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
558 if (reg->key == NULL || strcasecmp(reg->key, "ParamDesc"))
560 fprintf(ofp, "\n\t\"%s", stringcvt(r->value));
567 dump_typeinfo(const struct section *s, const struct reg *r)
570 TAILQ_FOREACH(reg, &rh, link) {
571 if (reg->section != s)
573 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
575 if (reg->key == NULL)
577 if (strcasecmp(reg->key, "type"))
579 if (strcasecmp(reg->value, "dword") == 0 ||
580 strcasecmp(reg->value, "int") == 0)
582 if (strcasecmp(reg->value, "enum") == 0)
584 if (strcasecmp(reg->value, "edit") == 0)
591 dump_paramreg(const struct section *s, const struct reg *r, int devidx)
595 keyname = r->subkey + strlen("Ndi\\params\\");
596 fprintf(ofp, "\n\t{ \"%s\",", keyname);
597 dump_paramdesc(s, r);
600 dump_defaultinfo(s, r, devidx);
608 struct assign *manf, *dev;
610 struct assign *assign;
612 int found = 0, i, is_winxp = 0, is_winnt = 0, devidx = 0;
614 /* Find signature to check for special case of WinNT. */
615 assign = find_assign("version", "signature");
616 if (strcasecmp(assign->vals[0], "$windows nt$") == 0)
619 /* Emit start of block */
620 fprintf (ofp, "ndis_cfg ndis_regvals[] = {");
622 /* Find manufacturer name */
623 manf = find_assign("Manufacturer", NULL);
627 /* Find manufacturer section */
628 if (manf->vals[1] != NULL &&
629 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
630 strcasecmp(manf->vals[1], "NTx86") == 0 ||
631 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
632 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
634 /* Handle Windows XP INF files. */
635 snprintf(sname, sizeof(sname), "%s.%s",
636 manf->vals[0], manf->vals[1]);
637 sec = find_section(sname);
639 sec = find_section(manf->vals[0]);
643 TAILQ_FOREACH(assign, &ah, link) {
644 if (assign->section == sec) {
647 * Find all the AddReg sections.
648 * Look for section names with .NT, unless
649 * this is a WinXP .INF file.
653 sprintf(sname, "%s.NTx86", assign->vals[0]);
654 dev = find_assign(sname, "AddReg");
656 sprintf(sname, "%s.NT",
658 dev = find_assign(sname, "AddReg");
661 dev = find_assign(assign->vals[0],
664 sprintf(sname, "%s.NT", assign->vals[0]);
665 dev = find_assign(sname, "AddReg");
666 if (dev == NULL && is_winnt)
667 dev = find_assign(assign->vals[0],
670 /* Section not found. */
673 for (i = 0; i < W_MAX; i++) {
674 if (dev->vals[i] != NULL)
675 dump_addreg(dev->vals[i], devidx);
682 sec = find_section(manf->vals[0]);
688 manf = find_next_assign(manf);
693 fprintf(ofp, "\n\t{ NULL, NULL, { 0 }, 0 }\n};\n\n");
699 assign_add (const char *a)
701 struct assign *assign;
704 assign = malloc(sizeof(struct assign));
705 bzero(assign, sizeof(struct assign));
706 assign->section = TAILQ_LAST(&sh, section_head);
707 assign->key = sstrdup(a);
708 for (i = 0; i < idx; i++)
709 assign->vals[(idx - 1) - i] = sstrdup(words[i]);
710 TAILQ_INSERT_TAIL(&ah, assign, link);
717 define_add (const char *d __unused)
720 fprintf(stderr, "define \"%s\"\n", d);
726 sstrdup(const char *str)
728 if (str != NULL && strlen(str))
729 return (strdup(str));
734 satoi (const char *nptr)
736 if (nptr != NULL && strlen(nptr))
742 regkey_add (const char *r)
746 reg = malloc(sizeof(struct reg));
747 bzero(reg, sizeof(struct reg));
748 reg->section = TAILQ_LAST(&sh, section_head);
749 reg->root = sstrdup(r);
750 reg->subkey = sstrdup(words[3]);
751 reg->key = sstrdup(words[2]);
752 reg->flags = satoi(words[1]);
753 reg->value = sstrdup(words[0]);
754 TAILQ_INSERT_TAIL(&rh, reg, link);
756 free(__DECONST(char *, r));
762 push_word (const char *w)
776 for (i = 0; i < idx; i++) {
778 free(__DECONST(char *, words[i]));
782 bzero(words, sizeof(words));