2 * SPDX-License-Identifier: BSD-4-Clause
5 * Bill Paul <wpaul@windriver.com>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
43 #include <sys/queue.h>
50 const char *words[W_MAX]; /* More than we'll need. */
53 static struct section_head sh;
54 static struct reg_head rh;
55 static struct assign_head ah;
57 static char *sstrdup (const char *);
59 *find_assign (const char *, const char *);
64 *find_section (const char *);
65 static int dump_deviceids_pci (void);
66 static int dump_deviceids_pcmcia (void);
67 static int dump_deviceids_usb (void);
68 static void dump_pci_id (const char *);
69 static void dump_pcmcia_id (const char *);
70 static void dump_usb_id (const char *);
71 static void dump_regvals (void);
72 static void dump_paramreg (const struct section *,
73 const struct reg *, int);
78 inf_parse (FILE *fp, FILE *outfp)
88 if (dump_deviceids_pci() == 0 &&
89 dump_deviceids_pcmcia() == 0 &&
90 dump_deviceids_usb() == 0)
93 fprintf(outfp, "#ifdef NDIS_REGVALS\n");
95 fprintf(outfp, "#endif /* NDIS_REGVALS */\n");
101 section_add (const char *s)
105 sec = malloc(sizeof(struct section));
106 bzero(sec, sizeof(struct section));
108 TAILQ_INSERT_TAIL(&sh, sec, link);
113 static struct assign *
114 find_assign (const char *s, const char *k)
116 struct assign *assign;
119 /* Deal with string section lookups. */
121 if (k != NULL && k[0] == '%') {
122 bzero(newkey, sizeof(newkey));
123 strncpy(newkey, k + 1, strlen(k) - 2);
127 TAILQ_FOREACH(assign, &ah, link) {
128 if (strcasecmp(assign->section->name, s) == 0) {
132 if (strcasecmp(assign->key, k) == 0)
139 static struct assign *
140 find_next_assign (struct assign *a)
142 struct assign *assign;
144 TAILQ_FOREACH(assign, &ah, link) {
149 assign = assign->link.tqe_next;
151 if (assign == NULL || assign->section != a->section)
158 stringcvt(const char *s)
162 manf = find_assign("strings", s);
165 return(manf->vals[0]);
169 find_section (const char *s)
171 struct section *section;
173 TAILQ_FOREACH(section, &sh, link) {
174 if (strcasecmp(section->name, s) == 0)
181 dump_pcmcia_id(const char *s)
183 char *manstr, *devstr;
186 p0 = __DECONST(char *, s);
188 p = strchr(p0, '\\');
200 /* Convert any underscores to spaces. */
202 while (*p0 != '\0') {
216 /* Convert any underscores to spaces. */
218 while (*p0 != '\0') {
224 fprintf(ofp, "\t\\\n\t{ \"%s\", \"%s\", ", manstr, devstr);
229 dump_pci_id(const char *s)
232 char vidstr[7], didstr[7], subsysstr[14];
234 p = strcasestr(s, "VEN_");
238 strcpy(vidstr, "0x");
239 strncat(vidstr, p, 4);
240 p = strcasestr(s, "DEV_");
244 strcpy(didstr, "0x");
245 strncat(didstr, p, 4);
248 p = strcasestr(s, "SUBSYS_");
250 strcpy(subsysstr, "0x00000000");
253 strcpy(subsysstr, "0x");
254 strncat(subsysstr, p, 8);
257 fprintf(ofp, "\t\\\n\t{ %s, %s, %s, ", vidstr, didstr, subsysstr);
262 dump_usb_id(const char *s)
265 char vidstr[7], pidstr[7];
267 p = strcasestr(s, "VID_");
271 strcpy(vidstr, "0x");
272 strncat(vidstr, p, 4);
273 p = strcasestr(s, "PID_");
277 strcpy(pidstr, "0x");
278 strncat(pidstr, p, 4);
282 fprintf(ofp, "\t\\\n\t{ %s, %s, ", vidstr, pidstr);
288 struct assign *manf, *dev;
290 struct assign *assign;
292 int first = 1, found = 0;
294 /* Find manufacturer name */
295 manf = find_assign("Manufacturer", NULL);
299 /* Find manufacturer section */
300 if (manf->vals[1] != NULL &&
301 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
302 strcasecmp(manf->vals[1], "NTx86") == 0 ||
303 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
304 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
305 /* Handle Windows XP INF files. */
306 snprintf(xpsec, sizeof(xpsec), "%s.%s",
307 manf->vals[0], manf->vals[1]);
308 sec = find_section(xpsec);
310 sec = find_section(manf->vals[0]);
312 /* See if there are any PCI device definitions. */
314 TAILQ_FOREACH(assign, &ah, link) {
315 if (assign->section == sec) {
316 dev = find_assign("strings", assign->key);
317 if (strcasestr(assign->vals[1], "PCI") != NULL) {
330 /* Emit start of PCI device table */
331 fprintf (ofp, "#define NDIS_PCI_DEV_TABLE");
338 * Now run through all the device names listed
339 * in the manufacturer section and dump out the
340 * device descriptions and vendor/device IDs.
343 TAILQ_FOREACH(assign, &ah, link) {
344 if (assign->section == sec) {
345 dev = find_assign("strings", assign->key);
346 /* Emit device IDs. */
347 if (strcasestr(assign->vals[1], "PCI") != NULL)
348 dump_pci_id(assign->vals[1]);
351 /* Emit device description */
352 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
357 /* Someone tried to fool us. Shame on them. */
360 sec = find_section(manf->vals[0]);
364 /* Handle Manufacturer sections with multiple entries. */
365 manf = find_next_assign(manf);
371 /* Emit end of table */
373 fprintf(ofp, "\n\n");
379 dump_deviceids_pcmcia()
381 struct assign *manf, *dev;
383 struct assign *assign;
385 int first = 1, found = 0;
387 /* Find manufacturer name */
388 manf = find_assign("Manufacturer", NULL);
392 /* Find manufacturer section */
393 if (manf->vals[1] != NULL &&
394 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
395 strcasecmp(manf->vals[1], "NTx86") == 0 ||
396 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
397 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
398 /* Handle Windows XP INF files. */
399 snprintf(xpsec, sizeof(xpsec), "%s.%s",
400 manf->vals[0], manf->vals[1]);
401 sec = find_section(xpsec);
403 sec = find_section(manf->vals[0]);
405 /* See if there are any PCMCIA device definitions. */
407 TAILQ_FOREACH(assign, &ah, link) {
408 if (assign->section == sec) {
409 dev = find_assign("strings", assign->key);
410 if (strcasestr(assign->vals[1], "PCMCIA") != NULL) {
423 /* Emit start of PCMCIA device table */
424 fprintf (ofp, "#define NDIS_PCMCIA_DEV_TABLE");
431 * Now run through all the device names listed
432 * in the manufacturer section and dump out the
433 * device descriptions and vendor/device IDs.
436 TAILQ_FOREACH(assign, &ah, link) {
437 if (assign->section == sec) {
438 dev = find_assign("strings", assign->key);
439 /* Emit device IDs. */
440 if (strcasestr(assign->vals[1], "PCMCIA") != NULL)
441 dump_pcmcia_id(assign->vals[1]);
444 /* Emit device description */
445 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
450 /* Someone tried to fool us. Shame on them. */
453 sec = find_section(manf->vals[0]);
457 /* Handle Manufacturer sections with multiple entries. */
458 manf = find_next_assign(manf);
464 /* Emit end of table */
466 fprintf(ofp, "\n\n");
474 struct assign *manf, *dev;
476 struct assign *assign;
478 int first = 1, found = 0;
480 /* Find manufacturer name */
481 manf = find_assign("Manufacturer", NULL);
485 /* Find manufacturer section */
486 if (manf->vals[1] != NULL &&
487 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
488 strcasecmp(manf->vals[1], "NTx86") == 0 ||
489 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
490 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
491 /* Handle Windows XP INF files. */
492 snprintf(xpsec, sizeof(xpsec), "%s.%s",
493 manf->vals[0], manf->vals[1]);
494 sec = find_section(xpsec);
496 sec = find_section(manf->vals[0]);
498 /* See if there are any USB device definitions. */
500 TAILQ_FOREACH(assign, &ah, link) {
501 if (assign->section == sec) {
502 dev = find_assign("strings", assign->key);
503 if (strcasestr(assign->vals[1], "USB") != NULL) {
516 /* Emit start of USB device table */
517 fprintf (ofp, "#define NDIS_USB_DEV_TABLE");
524 * Now run through all the device names listed
525 * in the manufacturer section and dump out the
526 * device descriptions and vendor/device IDs.
529 TAILQ_FOREACH(assign, &ah, link) {
530 if (assign->section == sec) {
531 dev = find_assign("strings", assign->key);
532 /* Emit device IDs. */
533 if (strcasestr(assign->vals[1], "USB") != NULL)
534 dump_usb_id(assign->vals[1]);
537 /* Emit device description */
538 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
543 /* Someone tried to fool us. Shame on them. */
546 sec = find_section(manf->vals[0]);
550 /* Handle Manufacturer sections with multiple entries. */
551 manf = find_next_assign(manf);
557 /* Emit end of table */
559 fprintf(ofp, "\n\n");
565 dump_addreg(const char *s, int devidx)
570 /* Find the addreg section */
571 sec = find_section(s);
573 /* Dump all the keys defined in it. */
574 TAILQ_FOREACH(reg, &rh, link) {
576 * Keys with an empty subkey are very easy to parse,
577 * so just deal with them here. If a parameter key
578 * of the same name also exists, prefer that one and
581 if (reg->section == sec) {
582 if (reg->subkey == NULL) {
583 fprintf(ofp, "\n\t{ \"%s\",", reg->key);
584 fprintf(ofp,"\n\t\"%s \",", reg->key);
585 fprintf(ofp, "\n\t{ \"%s\" }, %d },",
586 reg->value == NULL ? "" :
587 stringcvt(reg->value), devidx);
588 } else if (strncasecmp(reg->subkey,
589 "Ndi\\params", strlen("Ndi\\params")-1) == 0 &&
590 (reg->key != NULL && strcasecmp(reg->key,
592 dump_paramreg(sec, reg, devidx);
600 dump_enumreg(const struct section *s, const struct reg *r)
605 sprintf(enumkey, "%s\\enum", r->subkey);
606 TAILQ_FOREACH(reg, &rh, link) {
607 if (reg->section != s)
609 if (reg->subkey == NULL || strcasecmp(reg->subkey, enumkey))
611 fprintf(ofp, " [%s=%s]", reg->key,
612 stringcvt(reg->value));
618 dump_editreg(const struct section *s, const struct reg *r)
622 TAILQ_FOREACH(reg, &rh, link) {
623 if (reg->section != s)
625 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
627 if (reg->key == NULL)
629 if (strcasecmp(reg->key, "LimitText") == 0)
630 fprintf(ofp, " [maxchars=%s]", reg->value);
631 if (strcasecmp(reg->key, "Optional") == 0 &&
632 strcmp(reg->value, "1") == 0)
633 fprintf(ofp, " [optional]");
638 /* Use this for int too */
640 dump_dwordreg(const struct section *s, const struct reg *r)
644 TAILQ_FOREACH(reg, &rh, link) {
645 if (reg->section != s)
647 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
649 if (reg->key == NULL)
651 if (strcasecmp(reg->key, "min") == 0)
652 fprintf(ofp, " [min=%s]", reg->value);
653 if (strcasecmp(reg->key, "max") == 0)
654 fprintf(ofp, " [max=%s]", reg->value);
660 dump_defaultinfo(const struct section *s, const struct reg *r, int devidx)
663 TAILQ_FOREACH(reg, &rh, link) {
664 if (reg->section != s)
666 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
668 if (reg->key == NULL || strcasecmp(reg->key, "Default"))
670 fprintf(ofp, "\n\t{ \"%s\" }, %d },", reg->value == NULL ? "" :
671 stringcvt(reg->value), devidx);
674 /* Default registry entry missing */
675 fprintf(ofp, "\n\t{ \"\" }, %d },", devidx);
680 dump_paramdesc(const struct section *s, const struct reg *r)
683 TAILQ_FOREACH(reg, &rh, link) {
684 if (reg->section != s)
686 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
688 if (reg->key == NULL || strcasecmp(reg->key, "ParamDesc"))
690 fprintf(ofp, "\n\t\"%s", stringcvt(r->value));
697 dump_typeinfo(const struct section *s, const struct reg *r)
700 TAILQ_FOREACH(reg, &rh, link) {
701 if (reg->section != s)
703 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
705 if (reg->key == NULL)
707 if (strcasecmp(reg->key, "type"))
709 if (strcasecmp(reg->value, "dword") == 0 ||
710 strcasecmp(reg->value, "int") == 0)
712 if (strcasecmp(reg->value, "enum") == 0)
714 if (strcasecmp(reg->value, "edit") == 0)
721 dump_paramreg(const struct section *s, const struct reg *r, int devidx)
725 keyname = r->subkey + strlen("Ndi\\params\\");
726 fprintf(ofp, "\n\t{ \"%s\",", keyname);
727 dump_paramdesc(s, r);
730 dump_defaultinfo(s, r, devidx);
738 struct assign *manf, *dev;
740 struct assign *assign;
742 int found = 0, i, is_winxp = 0, is_winnt = 0, devidx = 0;
744 /* Find signature to check for special case of WinNT. */
745 assign = find_assign("version", "signature");
746 if (strcasecmp(assign->vals[0], "$windows nt$") == 0)
749 /* Emit start of block */
750 fprintf (ofp, "ndis_cfg ndis_regvals[] = {");
752 /* Find manufacturer name */
753 manf = find_assign("Manufacturer", NULL);
757 /* Find manufacturer section */
758 if (manf->vals[1] != NULL &&
759 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
760 strcasecmp(manf->vals[1], "NTx86") == 0 ||
761 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
762 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
764 /* Handle Windows XP INF files. */
765 snprintf(sname, sizeof(sname), "%s.%s",
766 manf->vals[0], manf->vals[1]);
767 sec = find_section(sname);
769 sec = find_section(manf->vals[0]);
773 TAILQ_FOREACH(assign, &ah, link) {
774 if (assign->section == sec) {
777 * Find all the AddReg sections.
778 * Look for section names with .NT, unless
779 * this is a WinXP .INF file.
783 sprintf(sname, "%s.NTx86", assign->vals[0]);
784 dev = find_assign(sname, "AddReg");
786 sprintf(sname, "%s.NT",
788 dev = find_assign(sname, "AddReg");
791 dev = find_assign(assign->vals[0],
794 sprintf(sname, "%s.NT", assign->vals[0]);
795 dev = find_assign(sname, "AddReg");
796 if (dev == NULL && is_winnt)
797 dev = find_assign(assign->vals[0],
800 /* Section not found. */
803 for (i = 0; i < W_MAX; i++) {
804 if (dev->vals[i] != NULL)
805 dump_addreg(dev->vals[i], devidx);
812 sec = find_section(manf->vals[0]);
818 manf = find_next_assign(manf);
823 fprintf(ofp, "\n\t{ NULL, NULL, { 0 }, 0 }\n};\n\n");
829 assign_add (const char *a)
831 struct assign *assign;
834 assign = malloc(sizeof(struct assign));
835 bzero(assign, sizeof(struct assign));
836 assign->section = TAILQ_LAST(&sh, section_head);
837 assign->key = sstrdup(a);
838 for (i = 0; i < idx; i++)
839 assign->vals[(idx - 1) - i] = sstrdup(words[i]);
840 TAILQ_INSERT_TAIL(&ah, assign, link);
847 define_add (const char *d __unused)
850 fprintf(stderr, "define \"%s\"\n", d);
856 sstrdup(const char *str)
858 if (str != NULL && strlen(str))
859 return (strdup(str));
864 satoi (const char *nptr)
866 if (nptr != NULL && strlen(nptr))
872 regkey_add (const char *r)
876 reg = malloc(sizeof(struct reg));
877 bzero(reg, sizeof(struct reg));
878 reg->section = TAILQ_LAST(&sh, section_head);
879 reg->root = sstrdup(r);
880 reg->subkey = sstrdup(words[3]);
881 reg->key = sstrdup(words[2]);
882 reg->flags = satoi(words[1]);
883 reg->value = sstrdup(words[0]);
884 TAILQ_INSERT_TAIL(&rh, reg, link);
886 free(__DECONST(char *, r));
892 push_word (const char *w)
896 fprintf(stderr, "too many words; try bumping W_MAX in inf.h\n");
912 for (i = 0; i < idx; i++) {
914 free(__DECONST(char *, words[i]));
918 bzero(words, sizeof(words));