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 void dump_deviceids_pci (void);
66 static void dump_deviceids_pcmcia (void);
67 static void 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)
89 dump_deviceids_pcmcia();
91 fprintf(outfp, "#ifdef NDIS_REGVALS\n");
93 fprintf(outfp, "#endif /* NDIS_REGVALS */\n");
99 section_add (const char *s)
103 sec = malloc(sizeof(struct section));
104 bzero(sec, sizeof(struct section));
106 TAILQ_INSERT_TAIL(&sh, sec, link);
111 static struct assign *
112 find_assign (const char *s, const char *k)
114 struct assign *assign;
117 /* Deal with string section lookups. */
119 if (k != NULL && k[0] == '%') {
120 bzero(newkey, sizeof(newkey));
121 strncpy(newkey, k + 1, strlen(k) - 2);
125 TAILQ_FOREACH(assign, &ah, link) {
126 if (strcasecmp(assign->section->name, s) == 0) {
130 if (strcasecmp(assign->key, k) == 0)
137 static struct assign *
138 find_next_assign (struct assign *a)
140 struct assign *assign;
142 TAILQ_FOREACH(assign, &ah, link) {
147 assign = assign->link.tqe_next;
149 if (assign == NULL || assign->section != a->section)
156 stringcvt(const char *s)
160 manf = find_assign("strings", s);
163 return(manf->vals[0]);
167 find_section (const char *s)
169 struct section *section;
171 TAILQ_FOREACH(section, &sh, link) {
172 if (strcasecmp(section->name, s) == 0)
179 dump_pcmcia_id(const char *s)
181 char *manstr, *devstr;
184 p0 = __DECONST(char *, s);
186 p = strchr(p0, '\\');
198 /* Convert any underscores to spaces. */
200 while (*p0 != '\0') {
214 /* Convert any underscores to spaces. */
216 while (*p0 != '\0') {
222 fprintf(ofp, "\t\\\n\t{ \"%s\", \"%s\", ", manstr, devstr);
227 dump_pci_id(const char *s)
230 char vidstr[7], didstr[7], subsysstr[14];
232 p = strcasestr(s, "VEN_");
236 strcpy(vidstr, "0x");
237 strncat(vidstr, p, 4);
238 p = strcasestr(s, "DEV_");
242 strcpy(didstr, "0x");
243 strncat(didstr, p, 4);
246 p = strcasestr(s, "SUBSYS_");
248 strcpy(subsysstr, "0x00000000");
251 strcpy(subsysstr, "0x");
252 strncat(subsysstr, p, 8);
255 fprintf(ofp, "\t\\\n\t{ %s, %s, %s, ", vidstr, didstr, subsysstr);
260 dump_usb_id(const char *s)
263 char vidstr[7], pidstr[7];
265 p = strcasestr(s, "VID_");
269 strcpy(vidstr, "0x");
270 strncat(vidstr, p, 4);
271 p = strcasestr(s, "PID_");
275 strcpy(pidstr, "0x");
276 strncat(pidstr, p, 4);
280 fprintf(ofp, "\t\\\n\t{ %s, %s, ", vidstr, pidstr);
286 struct assign *manf, *dev;
288 struct assign *assign;
290 int first = 1, found = 0;
292 /* Find manufacturer name */
293 manf = find_assign("Manufacturer", NULL);
297 /* Find manufacturer section */
298 if (manf->vals[1] != NULL &&
299 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
300 strcasecmp(manf->vals[1], "NTx86") == 0 ||
301 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
302 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
303 /* Handle Windows XP INF files. */
304 snprintf(xpsec, sizeof(xpsec), "%s.%s",
305 manf->vals[0], manf->vals[1]);
306 sec = find_section(xpsec);
308 sec = find_section(manf->vals[0]);
310 /* See if there are any PCI device definitions. */
312 TAILQ_FOREACH(assign, &ah, link) {
313 if (assign->section == sec) {
314 dev = find_assign("strings", assign->key);
315 if (strcasestr(assign->vals[1], "PCI") != NULL) {
328 /* Emit start of PCI device table */
329 fprintf (ofp, "#define NDIS_PCI_DEV_TABLE");
336 * Now run through all the device names listed
337 * in the manufacturer section and dump out the
338 * device descriptions and vendor/device IDs.
341 TAILQ_FOREACH(assign, &ah, link) {
342 if (assign->section == sec) {
343 dev = find_assign("strings", assign->key);
344 /* Emit device IDs. */
345 if (strcasestr(assign->vals[1], "PCI") != NULL)
346 dump_pci_id(assign->vals[1]);
349 /* Emit device description */
350 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
355 /* Someone tried to fool us. Shame on them. */
358 sec = find_section(manf->vals[0]);
362 /* Handle Manufacturer sections with multiple entries. */
363 manf = find_next_assign(manf);
369 /* Emit end of table */
371 fprintf(ofp, "\n\n");
377 dump_deviceids_pcmcia()
379 struct assign *manf, *dev;
381 struct assign *assign;
383 int first = 1, found = 0;
385 /* Find manufacturer name */
386 manf = find_assign("Manufacturer", NULL);
390 /* Find manufacturer section */
391 if (manf->vals[1] != NULL &&
392 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
393 strcasecmp(manf->vals[1], "NTx86") == 0 ||
394 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
395 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
396 /* Handle Windows XP INF files. */
397 snprintf(xpsec, sizeof(xpsec), "%s.%s",
398 manf->vals[0], manf->vals[1]);
399 sec = find_section(xpsec);
401 sec = find_section(manf->vals[0]);
403 /* See if there are any PCMCIA device definitions. */
405 TAILQ_FOREACH(assign, &ah, link) {
406 if (assign->section == sec) {
407 dev = find_assign("strings", assign->key);
408 if (strcasestr(assign->vals[1], "PCMCIA") != NULL) {
421 /* Emit start of PCMCIA device table */
422 fprintf (ofp, "#define NDIS_PCMCIA_DEV_TABLE");
429 * Now run through all the device names listed
430 * in the manufacturer section and dump out the
431 * device descriptions and vendor/device IDs.
434 TAILQ_FOREACH(assign, &ah, link) {
435 if (assign->section == sec) {
436 dev = find_assign("strings", assign->key);
437 /* Emit device IDs. */
438 if (strcasestr(assign->vals[1], "PCMCIA") != NULL)
439 dump_pcmcia_id(assign->vals[1]);
442 /* Emit device description */
443 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
448 /* Someone tried to fool us. Shame on them. */
451 sec = find_section(manf->vals[0]);
455 /* Handle Manufacturer sections with multiple entries. */
456 manf = find_next_assign(manf);
462 /* Emit end of table */
464 fprintf(ofp, "\n\n");
472 struct assign *manf, *dev;
474 struct assign *assign;
476 int first = 1, found = 0;
478 /* Find manufacturer name */
479 manf = find_assign("Manufacturer", NULL);
483 /* Find manufacturer section */
484 if (manf->vals[1] != NULL &&
485 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
486 strcasecmp(manf->vals[1], "NTx86") == 0 ||
487 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
488 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
489 /* Handle Windows XP INF files. */
490 snprintf(xpsec, sizeof(xpsec), "%s.%s",
491 manf->vals[0], manf->vals[1]);
492 sec = find_section(xpsec);
494 sec = find_section(manf->vals[0]);
496 /* See if there are any USB device definitions. */
498 TAILQ_FOREACH(assign, &ah, link) {
499 if (assign->section == sec) {
500 dev = find_assign("strings", assign->key);
501 if (strcasestr(assign->vals[1], "USB") != NULL) {
514 /* Emit start of USB device table */
515 fprintf (ofp, "#define NDIS_USB_DEV_TABLE");
522 * Now run through all the device names listed
523 * in the manufacturer section and dump out the
524 * device descriptions and vendor/device IDs.
527 TAILQ_FOREACH(assign, &ah, link) {
528 if (assign->section == sec) {
529 dev = find_assign("strings", assign->key);
530 /* Emit device IDs. */
531 if (strcasestr(assign->vals[1], "USB") != NULL)
532 dump_usb_id(assign->vals[1]);
535 /* Emit device description */
536 fprintf (ofp, "\t\\\n\t\"%s\" },", dev->vals[0]);
541 /* Someone tried to fool us. Shame on them. */
544 sec = find_section(manf->vals[0]);
548 /* Handle Manufacturer sections with multiple entries. */
549 manf = find_next_assign(manf);
555 /* Emit end of table */
557 fprintf(ofp, "\n\n");
563 dump_addreg(const char *s, int devidx)
568 /* Find the addreg section */
569 sec = find_section(s);
571 /* Dump all the keys defined in it. */
572 TAILQ_FOREACH(reg, &rh, link) {
574 * Keys with an empty subkey are very easy to parse,
575 * so just deal with them here. If a parameter key
576 * of the same name also exists, prefer that one and
579 if (reg->section == sec) {
580 if (reg->subkey == NULL) {
581 fprintf(ofp, "\n\t{ \"%s\",", reg->key);
582 fprintf(ofp,"\n\t\"%s \",", reg->key);
583 fprintf(ofp, "\n\t{ \"%s\" }, %d },",
584 reg->value == NULL ? "" :
585 stringcvt(reg->value), devidx);
586 } else if (strncasecmp(reg->subkey,
587 "Ndi\\params", strlen("Ndi\\params")-1) == 0 &&
588 (reg->key != NULL && strcasecmp(reg->key,
590 dump_paramreg(sec, reg, devidx);
598 dump_enumreg(const struct section *s, const struct reg *r)
603 sprintf(enumkey, "%s\\enum", r->subkey);
604 TAILQ_FOREACH(reg, &rh, link) {
605 if (reg->section != s)
607 if (reg->subkey == NULL || strcasecmp(reg->subkey, enumkey))
609 fprintf(ofp, " [%s=%s]", reg->key,
610 stringcvt(reg->value));
616 dump_editreg(const struct section *s, const struct reg *r)
620 TAILQ_FOREACH(reg, &rh, link) {
621 if (reg->section != s)
623 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
625 if (reg->key == NULL)
627 if (strcasecmp(reg->key, "LimitText") == 0)
628 fprintf(ofp, " [maxchars=%s]", reg->value);
629 if (strcasecmp(reg->key, "Optional") == 0 &&
630 strcmp(reg->value, "1") == 0)
631 fprintf(ofp, " [optional]");
636 /* Use this for int too */
638 dump_dwordreg(const struct section *s, const struct reg *r)
642 TAILQ_FOREACH(reg, &rh, link) {
643 if (reg->section != s)
645 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
647 if (reg->key == NULL)
649 if (strcasecmp(reg->key, "min") == 0)
650 fprintf(ofp, " [min=%s]", reg->value);
651 if (strcasecmp(reg->key, "max") == 0)
652 fprintf(ofp, " [max=%s]", reg->value);
658 dump_defaultinfo(const struct section *s, const struct reg *r, int devidx)
661 TAILQ_FOREACH(reg, &rh, link) {
662 if (reg->section != s)
664 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
666 if (reg->key == NULL || strcasecmp(reg->key, "Default"))
668 fprintf(ofp, "\n\t{ \"%s\" }, %d },", reg->value == NULL ? "" :
669 stringcvt(reg->value), devidx);
672 /* Default registry entry missing */
673 fprintf(ofp, "\n\t{ \"\" }, %d },", devidx);
678 dump_paramdesc(const struct section *s, const struct reg *r)
681 TAILQ_FOREACH(reg, &rh, link) {
682 if (reg->section != s)
684 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
686 if (reg->key == NULL || strcasecmp(reg->key, "ParamDesc"))
688 fprintf(ofp, "\n\t\"%s", stringcvt(r->value));
695 dump_typeinfo(const struct section *s, const struct reg *r)
698 TAILQ_FOREACH(reg, &rh, link) {
699 if (reg->section != s)
701 if (reg->subkey == NULL || strcasecmp(reg->subkey, r->subkey))
703 if (reg->key == NULL)
705 if (strcasecmp(reg->key, "type"))
707 if (strcasecmp(reg->value, "dword") == 0 ||
708 strcasecmp(reg->value, "int") == 0)
710 if (strcasecmp(reg->value, "enum") == 0)
712 if (strcasecmp(reg->value, "edit") == 0)
719 dump_paramreg(const struct section *s, const struct reg *r, int devidx)
723 keyname = r->subkey + strlen("Ndi\\params\\");
724 fprintf(ofp, "\n\t{ \"%s\",", keyname);
725 dump_paramdesc(s, r);
728 dump_defaultinfo(s, r, devidx);
736 struct assign *manf, *dev;
738 struct assign *assign;
740 int found = 0, i, is_winxp = 0, is_winnt = 0, devidx = 0;
742 /* Find signature to check for special case of WinNT. */
743 assign = find_assign("version", "signature");
744 if (strcasecmp(assign->vals[0], "$windows nt$") == 0)
747 /* Emit start of block */
748 fprintf (ofp, "ndis_cfg ndis_regvals[] = {");
750 /* Find manufacturer name */
751 manf = find_assign("Manufacturer", NULL);
755 /* Find manufacturer section */
756 if (manf->vals[1] != NULL &&
757 (strcasecmp(manf->vals[1], "NT.5.1") == 0 ||
758 strcasecmp(manf->vals[1], "NTx86") == 0 ||
759 strcasecmp(manf->vals[1], "NTx86.5.1") == 0 ||
760 strcasecmp(manf->vals[1], "NTamd64") == 0)) {
762 /* Handle Windows XP INF files. */
763 snprintf(sname, sizeof(sname), "%s.%s",
764 manf->vals[0], manf->vals[1]);
765 sec = find_section(sname);
767 sec = find_section(manf->vals[0]);
771 TAILQ_FOREACH(assign, &ah, link) {
772 if (assign->section == sec) {
775 * Find all the AddReg sections.
776 * Look for section names with .NT, unless
777 * this is a WinXP .INF file.
781 sprintf(sname, "%s.NTx86", assign->vals[0]);
782 dev = find_assign(sname, "AddReg");
784 sprintf(sname, "%s.NT",
786 dev = find_assign(sname, "AddReg");
789 dev = find_assign(assign->vals[0],
792 sprintf(sname, "%s.NT", assign->vals[0]);
793 dev = find_assign(sname, "AddReg");
794 if (dev == NULL && is_winnt)
795 dev = find_assign(assign->vals[0],
798 /* Section not found. */
801 for (i = 0; i < W_MAX; i++) {
802 if (dev->vals[i] != NULL)
803 dump_addreg(dev->vals[i], devidx);
810 sec = find_section(manf->vals[0]);
816 manf = find_next_assign(manf);
821 fprintf(ofp, "\n\t{ NULL, NULL, { 0 }, 0 }\n};\n\n");
827 assign_add (const char *a)
829 struct assign *assign;
832 assign = malloc(sizeof(struct assign));
833 bzero(assign, sizeof(struct assign));
834 assign->section = TAILQ_LAST(&sh, section_head);
835 assign->key = sstrdup(a);
836 for (i = 0; i < idx; i++)
837 assign->vals[(idx - 1) - i] = sstrdup(words[i]);
838 TAILQ_INSERT_TAIL(&ah, assign, link);
845 define_add (const char *d __unused)
848 fprintf(stderr, "define \"%s\"\n", d);
854 sstrdup(const char *str)
856 if (str != NULL && strlen(str))
857 return (strdup(str));
862 satoi (const char *nptr)
864 if (nptr != NULL && strlen(nptr))
870 regkey_add (const char *r)
874 reg = malloc(sizeof(struct reg));
875 bzero(reg, sizeof(struct reg));
876 reg->section = TAILQ_LAST(&sh, section_head);
877 reg->root = sstrdup(r);
878 reg->subkey = sstrdup(words[3]);
879 reg->key = sstrdup(words[2]);
880 reg->flags = satoi(words[1]);
881 reg->value = sstrdup(words[0]);
882 TAILQ_INSERT_TAIL(&rh, reg, link);
884 free(__DECONST(char *, r));
890 push_word (const char *w)
894 fprintf(stderr, "too many words; try bumping W_MAX in inf.h\n");
910 for (i = 0; i < idx; i++) {
912 free(__DECONST(char *, words[i]));
916 bzero(words, sizeof(words));