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