2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 static const char rcsid[] =
29 "$FreeBSD: src/usr.sbin/pccard/pccardd/file.c,v 1.24.2.5 2001/06/05 07:01:40 imp Exp $";
35 #include <sys/types.h>
39 static int includes = 0;
44 } configfiles[MAXINCLUDES] = {{NULL, NULL, 0}, };
46 static int pushc, pusht;
48 static char *filename;
50 static char *keys[] = {
63 "debuglevel", /* 13 */
82 #define KWD_DEBUGLEVEL 13
83 #define KWD_INCLUDE 14
84 #define KWD_FUNCTION 15
87 /* for keyword compatibility with PAO/plain FreeBSD */
92 {"generic", KWD_FUNCTION},
101 extern int doverbose;
103 static void parsefile(void);
104 static char *getline(void);
105 static char *next_tok(void);
106 static int num_tok(void);
107 static void error(char *);
108 static int keyword(char *);
109 static int irq_tok(int);
110 static int config_tok(unsigned char *);
111 static int func_tok(void);
112 static int debuglevel_tok(int);
113 static struct allocblk *ioblk_tok(int);
114 static struct allocblk *memblk_tok(int);
115 static struct driver *new_driver(char *);
116 static int iosize_tok(void);
117 static void file_include(char *);
119 static void addcmd(struct cmd **);
120 static void parse_card(int);
123 delete_card(struct card *cp)
125 struct ether *etherp, *ether_next;
126 struct card_config *configp, *config_next;
127 struct cmd *cmdp, *cmd_next;
136 /* free structures */
137 for (etherp = cp->ether; etherp; etherp = ether_next) {
138 ether_next = etherp->next;
141 for (configp = cp->config; configp; configp = config_next) {
142 config_next = configp->next;
145 for (cmdp = cp->insert; cmdp; cmdp = cmd_next) {
146 cmd_next = cmdp->next;
150 for (cmdp = cp->remove; cmdp; cmdp = cmd_next) {
151 cmd_next = cmdp->next;
159 * Read a file and parse the pcmcia configuration data.
160 * After parsing, verify the links.
166 struct card *cp, *card_next;
167 struct card *genericp, *tail_gp;
168 struct card_config *configp;
170 /* delete all card configuration data before we proceed */
173 cards = last_card = 0;
175 card_next = cp->next;
177 /* check whether this card is in use */
179 for (configp = cp->config; configp; configp = configp->next) {
180 if (configp->inuse) {
187 * don't delete entry in use for consistency.
188 * leave normal entry in the cards list,
189 * insert generic entry into the list after re-loading config files.
192 cp->next = 0; /* unchain from the cards list */
193 switch (cp->deftype) {
195 /* don't delete this entry for consistency */
196 if (debug_level >= 1) {
197 logmsg("Card \"%s\"(\"%s\") is in use, "
198 "can't change configuration\n",
199 cp->manuf, cp->version);
201 /* add this to the card list */
203 cards = last_card = cp;
205 last_card->next = cp;
211 /* generic entry must be inserted to the list later */
212 if (debug_level >= 1) {
213 logmsg("Generic entry is in use, "
214 "can't change configuration\n");
227 for (i = 0; i < MAXINCLUDES; i++) {
228 if (configfiles[i].filep) {
229 fclose(configfiles[i].filep);
230 configfiles[i].filep = NULL;
232 free(configfiles[i].filename);
236 in = fopen(name, "r");
242 configfiles[includes].filep = in;
243 filename = configfiles[includes].filename = name;
246 for (cp = cards; cp; cp = cp->next) {
248 logmsg("warning: card %s(%s) has no valid configuration\n",
249 cp->manuf, cp->version);
252 /* insert generic entries in use into the top of generic entries */
254 /* search tail of generic entries in use */
255 for (tail_gp = genericp; tail_gp->next; tail_gp = tail_gp->next)
259 * if the top of cards list is generic entry,
260 * insert generic entries in use before it.
262 if (cards && cards->deftype == DT_FUNC) {
263 tail_gp->next = cards;
268 /* search top of generic entries */
269 for (cp = cards; cp; cp = cp->next) {
270 if (cp->next && cp->next->deftype == DT_FUNC) {
276 * if we have generic entry in the cards list,
277 * insert generic entries in use into there.
280 tail_gp->next = cp->next;
286 * otherwise we don't have generic entries in
287 * cards list, just add them to the list.
292 last_card->next = genericp;
298 /* save the initial state of resource pool */
299 bcopy(io_avail, io_init, bitstr_size(IOPORTS));
300 bcopy(mem_avail, mem_init, bitstr_size(MEMBLKS));
301 bcopy(pool_irq, irq_init, sizeof(pool_irq));
309 struct allocblk *bp, *next;
315 switch (keyword(next_tok())) {
320 /* override reserved I/O blocks */
321 bit_nclear(io_avail, 0, IOPORTS-1);
322 for (bp = pool_ioblks; bp; bp = next) {
328 while ((bp = ioblk_tok(0)) != 0) {
329 if (bp->size == 0 || bp->addr == 0) {
333 bit_nset(io_avail, bp->addr,
334 bp->addr + bp->size - 1);
335 bp->next = pool_ioblks;
341 /* override reserved irqs */
342 bzero(pool_irq, sizeof(pool_irq));
343 while ((i = irq_tok(0)) > 0)
348 /* override reserved memory blocks. */
349 bit_nclear(mem_avail, 0, MEMBLKS-1);
350 for (bp = pool_mem; bp; bp = next) {
356 while ((bp = memblk_tok(0)) != 0) {
357 if (bp->size == 0 || bp->addr == 0) {
361 bit_nset(mem_avail, MEM2BIT(bp->addr),
362 MEM2BIT(bp->addr + bp->size) - 1);
369 /* Card definition. */
373 /* Function definition. */
377 i = debuglevel_tok(0);
382 incl = newstr(next_tok());
386 error("syntax error");
388 if (errors++ >= MAXERRORS) {
389 error("too many errors, giving up");
397 * Parse a card definition.
400 parse_card(int deftype)
402 char *man, *vers, *tmp;
404 unsigned char index_type;
407 struct card_config *confp, *lastp;
411 cp = xmalloc(sizeof(*cp));
412 cp->deftype = deftype;
415 man = newstr(next_tok());
416 vers = newstr(next_tok());
417 add_info = newstr(next_tok());
418 if (keyword(add_info)) {
421 cp->add_info1 = NULL;
422 cp->add_info2 = NULL;
424 cp->add_info1 = add_info;
425 add_info = newstr(next_tok());
426 if (keyword(add_info)) {
429 cp->add_info2 = NULL;
431 cp->add_info2 = add_info;
437 asprintf(&cp->logstr, "%s (%s)", man, vers);
444 cp->func_id = (u_char) func_tok();
447 fprintf(stderr, "parse_card: unknown deftype %d\n", deftype);
453 cards = last_card = cp;
455 last_card->next = cp;
459 switch (keyword(next_tok())) {
462 i = config_tok(&index_type);
464 error("illegal card config index");
467 confp = xmalloc(sizeof(*confp));
469 confp->driver = new_driver(man);
470 confp->irq = irq_tok(1);
471 confp->flags = num_tok();
472 if (confp->flags == -1) {
476 if (confp->irq < 0 || confp->irq > 15) {
477 error("illegal card IRQ value");
480 confp->index = i & 0x3F;
481 confp->index_type = index_type;
484 * If no valid driver for this config, then do not save
485 * this configuration entry.
491 for (lastp = cp->config; lastp->next;
492 lastp = lastp->next);
502 error("illegal card reset time");
509 ether = xmalloc(sizeof(*ether));
510 ether->type = ETHTYPE_GENERIC;
512 if (strcmp("attr2", tmp) == 0)
513 ether->type = ETHTYPE_ATTR2;
516 ether->value = num_tok();
517 if (ether->value == -1) {
518 error("illegal ether address offset");
523 ether->next = cp->ether;
536 iosize = iosize_tok();
538 error("Illegal cardio arguments");
542 error("iosize should be placed after config");
549 cp->logstr = newstr(next_tok());
559 * Generate a new driver structure. If one exists, use
560 * that one after confirming the correct class.
562 static struct driver *
563 new_driver(char *name)
568 for (drvp = drivers; drvp; drvp = drvp->next)
569 if (strcmp(drvp->name, name) == 0)
571 drvp = xmalloc(sizeof(*drvp));
572 drvp->next = drivers;
574 drvp->name = newstr(name);
575 drvp->kernel = newstr(name);
578 if (*p >= '0' && *p <= '9') {
579 drvp->unit = atoi(p);
584 printf("Drv %s%d created\n", drvp->kernel, drvp->unit);
591 * Parse one I/O block.
593 static struct allocblk *
599 /* ignore the keyword to allow separete blocks in multiple lines */
600 if (keyword(next_tok()) != KWD_IO) {
604 if ((i = num_tok()) >= 0) {
605 if (strcmp("-", next_tok()) || (j = num_tok()) < 0 || j < i) {
606 error("I/O block format error");
609 io = xmalloc(sizeof(*io));
611 io->size = j - i + 1;
613 error("I/O port out of range");
618 io->addr = io->size = 0;
623 error("illegal or missing I/O block spec");
628 * Parse a memory block.
630 static struct allocblk *
631 memblk_tok(int force)
633 struct allocblk *mem;
636 /* ignore the keyword to allow separete blocks in multiple lines */
637 if (keyword(next_tok()) != KWD_MEMORY) {
641 if ((i = num_tok()) >= 0) {
642 if ((j = num_tok()) < 0)
643 error("illegal memory block");
645 mem = xmalloc(sizeof(*mem));
646 mem->addr = i & ~(MEMUNIT - 1);
647 mem->size = (j + MEMUNIT - 1) & ~(MEMUNIT - 1);
648 if (i < MEMSTART || (i + j) > MEMEND) {
649 error("memory address out of range");
654 mem->addr = mem->size = 0;
660 error("illegal or missing memory block spec");
665 * IRQ token. Must be number > 0 && < 16.
666 * If force is set, IRQ must exist, and can also be '?'.
673 /* ignore the keyword to allow separete blocks in multiple lines */
674 if (keyword(next_tok()) != KWD_IRQ) {
678 if (strcmp("?", next_tok()) == 0 && force)
685 error("illegal IRQ value");
693 config_tok(unsigned char *index_type)
695 if (strcmp("default", next_tok()) == 0) {
696 *index_type = DEFAULT_INDEX;
700 if (strcmp("auto", next_tok()) == 0) {
701 *index_type = AUTO_INDEX;
705 *index_type = NORMAL_INDEX;
714 if (strcmp("serial", next_tok()) == 0)
717 if (strcmp("fixed_disk", next_tok()) == 0)
725 * debuglevel token. Must be between 0 and 9.
728 debuglevel_tok(int force)
733 if (i >= 0 && i <= 9)
740 * iosize {<size>|auto}
746 if (strcmp("auto", next_tok()) == 0)
747 iosize = -1; /* wildcard */
756 printf("iosize: size=%x\n", iosize);
763 * search the table for a match.
771 for (s = keys; *s; s++, i++)
772 if (strcmp(*s, str) == 0)
775 /* search keyword aliases too */
776 for (i = 0; key_aliases[i].key ; i++)
777 if (strcmp(key_aliases[i].alias, str) == 0)
778 return (key_aliases[i].key);
784 * addcmd - Append the command line to the list of
788 addcmd(struct cmd **cp)
794 ncp = xmalloc(sizeof(*ncp));
807 logmsg("%s: %s at line %d, near %s\n",
808 filename, msg, lineno, next_tok());
812 static int last_char;
828 return (last_char = c);
840 while (((c = get()) != '\n') && (c != EOF));
841 return (last_char = c);
845 * num_tok - expecting a number token. If not a number,
847 * Handles octal (who uses octal anymore?)
850 * Looks for a 'k' at the end of decimal numbers
851 * and multiplies by 1024.
856 char *s = next_tok(), c;
864 if (c == '\0') return 0;
865 else if (c == 'x' || c == 'X') {
874 if (val && base == 10 && *s == 0)
887 val = val * base + c - '0';
895 val = val * base + c - '0';
904 val = val * base + c - 'a' + 10;
915 val = val * base + c - 'A' + 10;
920 } while ((c = *s++) != 0);
924 static char *_next_tok(void);
929 char *s = _next_tok();
931 printf("Tok = %s\n", s);
937 * get one token. Handles string quoting etc.
942 static char buf[1024];
943 char *p = buf, instr = 0;
965 error("unterminated string");
970 /* Eat whitespace unless in a string. */
982 /* Special characters that are tokens on their own. */
997 /* go back to previous config file */
999 in = configfiles[includes].filep;
1000 filename = configfiles[includes].filename;
1001 lineno = configfiles[includes].lineno;
1002 return _next_tok(); /* recursive */
1008 strcpy(buf, "__EOF__");
1015 * get the rest of the line. If the
1016 * last character scanned was a newline, then
1017 * return an empty line. If this isn't checked, then
1018 * a getline may incorrectly return the next line.
1023 char buf[1024], *p = buf;
1026 if (last_char == '\n')
1027 return (newstr(""));
1030 } while (c == ' ' || c == '\t');
1031 for (; c != '\n' && c != EOF; c = get())
1032 if (i++ < sizeof(buf) - 10)
1035 return (newstr(buf));
1039 * Include configuration file
1042 file_include(char *incl)
1047 /* check nesting overflow */
1048 if (includes >= MAXINCLUDES) {
1049 if (debug_level >= 1) {
1050 logmsg("%s: include nesting overflow "
1051 "at line %d, near %s\n", filename, lineno, incl);
1057 /* check recursive inclusion */
1058 for (i = 0, included = 0; i <= includes; i++) {
1059 if (strcmp(incl, configfiles[i].filename) == 0) {
1064 if (included == 1) {
1065 if (debug_level >= 1) {
1066 logmsg("%s: can't include the same file twice "
1067 "at line %d, near %s\n", filename, lineno, incl);
1073 if (!(fp = fopen(incl, "r"))) {
1074 if (debug_level >= 1) {
1075 logmsg("%s: can't open include file "
1076 "at line %d, near %s\n", filename, lineno, incl);
1082 /* save line number of the current config file */
1083 configfiles[includes].lineno = lineno;
1086 /* now we start parsing new config file */
1088 in = configfiles[includes].filep = fp;
1089 filename = configfiles[includes].filename = incl;