xref: /inferno-os/os/boot/mpc/conf.c (revision 7ef44d652ae9e5e1f5b3465d73684e4a54de73c0)
1 #include "u.h"
2 #include "lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "io.h"
7 
8 #include "dosfs.h"
9 
10 static char *confname[MAXCONF];
11 static char *confval[MAXCONF];
12 static int nconf;
13 
14 extern char **ini;
15 
16 char*
17 getconf(char *name)
18 {
19 	int i;
20 
21 	for(i = 0; i < nconf; i++)
22 		if(strcmp(confname[i], name) == 0)
23 			return confval[i];
24 	return 0;
25 }
26 
27 /*
28  *  read configuration file
29  */
30 int
31 plan9ini(Dos *dos, char *val)
32 {
33 	Dosfile rc;
34 	int i, n;
35 	char *cp, *p, *q, *line[MAXCONF];
36 
37 	cp = BOOTARGS;
38 	if(dos) {
39 		if(dosstat(dos, *ini, &rc) <= 0)
40 			return -1;
41 
42 		*cp = 0;
43 		n = dosread(&rc, cp, BOOTARGSLEN-1);
44 		if(n <= 0)
45 			return -1;
46 		cp[n] = 0;
47 	} else if(val != nil){
48 		if(memchr(val, 0, BOOTARGSLEN-1) == nil)
49 			return -1;
50 		print("Using flash configuration\n");
51 		strcpy(cp, val);
52 		n = strlen(cp);
53 	}else{
54 		strcpy(cp, archconfig());
55 		n = strlen(cp);
56 	}
57 
58 	/*
59 	 * Make a working copy.
60 	 * We could change this to pass the parsed strings
61 	 * to the booted programme instead of the raw
62 	 * string, then it only gets done once.
63 	 */
64 	memmove(cp+BOOTARGSLEN, cp, n+1);
65 	cp += BOOTARGSLEN;
66 
67 	/*
68 	 * Strip out '\r', change '\t' -> ' '.
69 	 */
70 	p = cp;
71 	for(q = cp; *q; q++){
72 		if(*q == '\r')
73 			continue;
74 		if(*q == '\t')
75 			*q = ' ';
76 		*p++ = *q;
77 	}
78 	*p = 0;
79 	n = getcfields(cp, line, MAXCONF, "\n");
80 	for(i = 0; i < n; i++){
81 		cp = strchr(line[i], '=');
82 		if(cp == 0)
83 			continue;
84 		*cp++ = 0;
85 		if(cp - line[i] >= NAMELEN+1)
86 			*(line[i]+NAMELEN-1) = 0;
87 		confname[nconf] = line[i];
88 		confval[nconf] = cp;
89 		nconf++;
90 	}
91 	return 0;
92 }
93 
94 int
95 parseether(uchar *to, char *from)
96 {
97 	char nip[4];
98 	char *p;
99 	int i;
100 
101 	p = from;
102 	while(*p == ' ')
103 		++p;
104 	for(i = 0; i < 6; i++){
105 		if(*p == 0)
106 			return -1;
107 		nip[0] = *p++;
108 		if(*p == 0)
109 			return -1;
110 		nip[1] = *p++;
111 		nip[2] = 0;
112 		to[i] = strtoul(nip, 0, 16);
113 		if(*p == ':')
114 			p++;
115 	}
116 	return 0;
117 }
118 
119 int
120 isaconfig(char *class, int ctlrno, ISAConf *isa)
121 {
122 	char cc[NAMELEN], *p, *q, *r;
123 	int n;
124 
125 	sprint(cc, "%s%d", class, ctlrno);
126 	for(n = 0; n < nconf; n++){
127 		if(strncmp(confname[n], cc, NAMELEN))
128 			continue;
129 		isa->nopt = 0;
130 		p = confval[n];
131 		while(*p){
132 			while(*p == ' ' || *p == '\t')
133 				p++;
134 			if(*p == '\0')
135 				break;
136 			if(strncmp(p, "type=", 5) == 0){
137 				p += 5;
138 				for(q = isa->type; q < &isa->type[NAMELEN-1]; q++){
139 					if(*p == '\0' || *p == ' ' || *p == '\t')
140 						break;
141 					*q = *p++;
142 				}
143 				*q = '\0';
144 			}
145 			else if(strncmp(p, "port=", 5) == 0)
146 				isa->port = strtoul(p+5, &p, 0);
147 			else if(strncmp(p, "irq=", 4) == 0)
148 				isa->irq = strtoul(p+4, &p, 0);
149 			else if(strncmp(p, "mem=", 4) == 0)
150 				isa->mem = strtoul(p+4, &p, 0);
151 			else if(strncmp(p, "size=", 5) == 0)
152 				isa->size = strtoul(p+5, &p, 0);
153 			else if(strncmp(p, "ea=", 3) == 0){
154 				if(parseether(isa->ea, p+3) == -1)
155 					memset(isa->ea, 0, 6);
156 			}
157 			else if(isa->nopt < NISAOPT){
158 				r = isa->opt[isa->nopt];
159 				while(*p && *p != ' ' && *p != '\t'){
160 					*r++ = *p++;
161 					if(r-isa->opt[isa->nopt] >= ISAOPTLEN-1)
162 						break;
163 				}
164 				*r = '\0';
165 				isa->nopt++;
166 			}
167 			while(*p && *p != ' ' && *p != '\t')
168 				p++;
169 		}
170 		return 1;
171 	}
172 	return 0;
173 }
174