1 | #include <freebsd/machine/rtems-bsd-config.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2000,2001 Peter Wemm <peter@FreeBSD.org> |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
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 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
26 | * SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #include <freebsd/sys/cdefs.h> |
---|
30 | __FBSDID("$FreeBSD$"); |
---|
31 | |
---|
32 | #include <freebsd/sys/param.h> |
---|
33 | #include <freebsd/sys/lock.h> |
---|
34 | #include <freebsd/sys/mutex.h> |
---|
35 | #include <freebsd/sys/systm.h> |
---|
36 | #include <freebsd/sys/bus.h> |
---|
37 | |
---|
38 | /* |
---|
39 | * Access functions for device resources. |
---|
40 | */ |
---|
41 | |
---|
42 | static int checkmethod = 1; |
---|
43 | static int use_kenv; |
---|
44 | static char *hintp; |
---|
45 | |
---|
46 | /* |
---|
47 | * Evil wildcarding resource string lookup. |
---|
48 | * This walks the supplied env string table and returns a match. |
---|
49 | * The start point can be remembered for incremental searches. |
---|
50 | */ |
---|
51 | static int |
---|
52 | res_find(int *line, int *startln, |
---|
53 | const char *name, int *unit, const char *resname, const char *value, |
---|
54 | const char **ret_name, int *ret_namelen, int *ret_unit, |
---|
55 | const char **ret_resname, int *ret_resnamelen, const char **ret_value) |
---|
56 | { |
---|
57 | int n = 0, hit, i = 0; |
---|
58 | char r_name[32]; |
---|
59 | int r_unit; |
---|
60 | char r_resname[32]; |
---|
61 | char r_value[128]; |
---|
62 | const char *s, *cp; |
---|
63 | char *p; |
---|
64 | |
---|
65 | if (checkmethod) { |
---|
66 | hintp = NULL; |
---|
67 | |
---|
68 | switch (hintmode) { |
---|
69 | case 0: /* loader hints in environment only */ |
---|
70 | break; |
---|
71 | case 1: /* static hints only */ |
---|
72 | hintp = static_hints; |
---|
73 | checkmethod = 0; |
---|
74 | break; |
---|
75 | case 2: /* fallback mode */ |
---|
76 | if (dynamic_kenv) { |
---|
77 | mtx_lock(&kenv_lock); |
---|
78 | cp = kenvp[0]; |
---|
79 | for (i = 0; cp != NULL; cp = kenvp[++i]) { |
---|
80 | if (!strncmp(cp, "hint.", 5)) { |
---|
81 | use_kenv = 1; |
---|
82 | checkmethod = 0; |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | mtx_unlock(&kenv_lock); |
---|
87 | } else { |
---|
88 | cp = kern_envp; |
---|
89 | while (cp) { |
---|
90 | if (strncmp(cp, "hint.", 5) == 0) { |
---|
91 | cp = NULL; |
---|
92 | hintp = kern_envp; |
---|
93 | break; |
---|
94 | } |
---|
95 | while (*cp != '\0') |
---|
96 | cp++; |
---|
97 | cp++; |
---|
98 | if (*cp == '\0') { |
---|
99 | cp = NULL; |
---|
100 | hintp = static_hints; |
---|
101 | break; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | break; |
---|
106 | default: |
---|
107 | break; |
---|
108 | } |
---|
109 | if (hintp == NULL) { |
---|
110 | if (dynamic_kenv) { |
---|
111 | use_kenv = 1; |
---|
112 | checkmethod = 0; |
---|
113 | } else |
---|
114 | hintp = kern_envp; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | if (use_kenv) { |
---|
119 | mtx_lock(&kenv_lock); |
---|
120 | i = 0; |
---|
121 | cp = kenvp[0]; |
---|
122 | if (cp == NULL) { |
---|
123 | mtx_unlock(&kenv_lock); |
---|
124 | return (ENOENT); |
---|
125 | } |
---|
126 | } else |
---|
127 | cp = hintp; |
---|
128 | while (cp) { |
---|
129 | hit = 1; |
---|
130 | (*line)++; |
---|
131 | if (strncmp(cp, "hint.", 5) != 0) |
---|
132 | hit = 0; |
---|
133 | else |
---|
134 | n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%128s", |
---|
135 | r_name, &r_unit, r_resname, r_value); |
---|
136 | if (hit && n != 4) { |
---|
137 | printf("CONFIG: invalid hint '%s'\n", cp); |
---|
138 | /* XXX: abuse bogus index() declaration */ |
---|
139 | p = index(cp, 'h'); |
---|
140 | *p = 'H'; |
---|
141 | hit = 0; |
---|
142 | } |
---|
143 | if (hit && startln && *startln >= 0 && *line < *startln) |
---|
144 | hit = 0; |
---|
145 | if (hit && name && strcmp(name, r_name) != 0) |
---|
146 | hit = 0; |
---|
147 | if (hit && unit && *unit != r_unit) |
---|
148 | hit = 0; |
---|
149 | if (hit && resname && strcmp(resname, r_resname) != 0) |
---|
150 | hit = 0; |
---|
151 | if (hit && value && strcmp(value, r_value) != 0) |
---|
152 | hit = 0; |
---|
153 | if (hit) |
---|
154 | break; |
---|
155 | if (use_kenv) { |
---|
156 | cp = kenvp[++i]; |
---|
157 | if (cp == NULL) |
---|
158 | break; |
---|
159 | } else { |
---|
160 | while (*cp != '\0') |
---|
161 | cp++; |
---|
162 | cp++; |
---|
163 | if (*cp == '\0') { |
---|
164 | cp = NULL; |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | if (use_kenv) |
---|
170 | mtx_unlock(&kenv_lock); |
---|
171 | if (cp == NULL) |
---|
172 | return ENOENT; |
---|
173 | |
---|
174 | s = cp; |
---|
175 | /* This is a bit of a hack, but at least is reentrant */ |
---|
176 | /* Note that it returns some !unterminated! strings. */ |
---|
177 | s = index(s, '.') + 1; /* start of device */ |
---|
178 | if (ret_name) |
---|
179 | *ret_name = s; |
---|
180 | s = index(s, '.') + 1; /* start of unit */ |
---|
181 | if (ret_namelen && ret_name) |
---|
182 | *ret_namelen = s - *ret_name - 1; /* device length */ |
---|
183 | if (ret_unit) |
---|
184 | *ret_unit = r_unit; |
---|
185 | s = index(s, '.') + 1; /* start of resname */ |
---|
186 | if (ret_resname) |
---|
187 | *ret_resname = s; |
---|
188 | s = index(s, '=') + 1; /* start of value */ |
---|
189 | if (ret_resnamelen && ret_resname) |
---|
190 | *ret_resnamelen = s - *ret_resname - 1; /* value len */ |
---|
191 | if (ret_value) |
---|
192 | *ret_value = s; |
---|
193 | if (startln) /* line number for anchor */ |
---|
194 | *startln = *line + 1; |
---|
195 | return 0; |
---|
196 | } |
---|
197 | |
---|
198 | /* |
---|
199 | * Search all the data sources for matches to our query. We look for |
---|
200 | * dynamic hints first as overrides for static or fallback hints. |
---|
201 | */ |
---|
202 | static int |
---|
203 | resource_find(int *line, int *startln, |
---|
204 | const char *name, int *unit, const char *resname, const char *value, |
---|
205 | const char **ret_name, int *ret_namelen, int *ret_unit, |
---|
206 | const char **ret_resname, int *ret_resnamelen, const char **ret_value) |
---|
207 | { |
---|
208 | int i; |
---|
209 | int un; |
---|
210 | |
---|
211 | *line = 0; |
---|
212 | |
---|
213 | /* Search for exact unit matches first */ |
---|
214 | i = res_find(line, startln, name, unit, resname, value, |
---|
215 | ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen, |
---|
216 | ret_value); |
---|
217 | if (i == 0) |
---|
218 | return 0; |
---|
219 | if (unit == NULL) |
---|
220 | return ENOENT; |
---|
221 | /* If we are still here, search for wildcard matches */ |
---|
222 | un = -1; |
---|
223 | i = res_find(line, startln, name, &un, resname, value, |
---|
224 | ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen, |
---|
225 | ret_value); |
---|
226 | if (i == 0) |
---|
227 | return 0; |
---|
228 | return ENOENT; |
---|
229 | } |
---|
230 | |
---|
231 | int |
---|
232 | resource_int_value(const char *name, int unit, const char *resname, int *result) |
---|
233 | { |
---|
234 | int error; |
---|
235 | const char *str; |
---|
236 | char *op; |
---|
237 | unsigned long val; |
---|
238 | int line; |
---|
239 | |
---|
240 | line = 0; |
---|
241 | error = resource_find(&line, NULL, name, &unit, resname, NULL, |
---|
242 | NULL, NULL, NULL, NULL, NULL, &str); |
---|
243 | if (error) |
---|
244 | return error; |
---|
245 | if (*str == '\0') |
---|
246 | return EFTYPE; |
---|
247 | val = strtoul(str, &op, 0); |
---|
248 | if (*op != '\0') |
---|
249 | return EFTYPE; |
---|
250 | *result = val; |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | #ifndef __rtems__ |
---|
255 | int |
---|
256 | resource_long_value(const char *name, int unit, const char *resname, |
---|
257 | long *result) |
---|
258 | { |
---|
259 | int error; |
---|
260 | const char *str; |
---|
261 | char *op; |
---|
262 | unsigned long val; |
---|
263 | int line; |
---|
264 | |
---|
265 | line = 0; |
---|
266 | error = resource_find(&line, NULL, name, &unit, resname, NULL, |
---|
267 | NULL, NULL, NULL, NULL, NULL, &str); |
---|
268 | if (error) |
---|
269 | return error; |
---|
270 | if (*str == '\0') |
---|
271 | return EFTYPE; |
---|
272 | val = strtoul(str, &op, 0); |
---|
273 | if (*op != '\0') |
---|
274 | return EFTYPE; |
---|
275 | *result = val; |
---|
276 | return 0; |
---|
277 | } |
---|
278 | #endif /* __rtems__ */ |
---|
279 | |
---|
280 | int |
---|
281 | resource_string_value(const char *name, int unit, const char *resname, |
---|
282 | const char **result) |
---|
283 | { |
---|
284 | int error; |
---|
285 | const char *str; |
---|
286 | int line; |
---|
287 | |
---|
288 | line = 0; |
---|
289 | error = resource_find(&line, NULL, name, &unit, resname, NULL, |
---|
290 | NULL, NULL, NULL, NULL, NULL, &str); |
---|
291 | if (error) |
---|
292 | return error; |
---|
293 | *result = str; |
---|
294 | return 0; |
---|
295 | } |
---|
296 | |
---|
297 | /* |
---|
298 | * This is a bit nasty, but allows us to not modify the env strings. |
---|
299 | */ |
---|
300 | static const char * |
---|
301 | resource_string_copy(const char *s, int len) |
---|
302 | { |
---|
303 | static char stringbuf[256]; |
---|
304 | static int offset = 0; |
---|
305 | const char *ret; |
---|
306 | |
---|
307 | if (len == 0) |
---|
308 | len = strlen(s); |
---|
309 | if (len > 255) |
---|
310 | return NULL; |
---|
311 | if ((offset + len + 1) > 255) |
---|
312 | offset = 0; |
---|
313 | bcopy(s, &stringbuf[offset], len); |
---|
314 | stringbuf[offset + len] = '\0'; |
---|
315 | ret = &stringbuf[offset]; |
---|
316 | offset += len + 1; |
---|
317 | return ret; |
---|
318 | } |
---|
319 | |
---|
320 | /* |
---|
321 | * err = resource_find_match(&anchor, &name, &unit, resname, value) |
---|
322 | * Iteratively fetch a list of devices wired "at" something |
---|
323 | * res and value are restrictions. eg: "at", "scbus0". |
---|
324 | * For practical purposes, res = required, value = optional. |
---|
325 | * *name and *unit are set. |
---|
326 | * set *anchor to zero before starting. |
---|
327 | */ |
---|
328 | int |
---|
329 | resource_find_match(int *anchor, const char **name, int *unit, |
---|
330 | const char *resname, const char *value) |
---|
331 | { |
---|
332 | const char *found_name; |
---|
333 | int found_namelen; |
---|
334 | int found_unit; |
---|
335 | int ret; |
---|
336 | int newln; |
---|
337 | |
---|
338 | newln = *anchor; |
---|
339 | ret = resource_find(anchor, &newln, NULL, NULL, resname, value, |
---|
340 | &found_name, &found_namelen, &found_unit, NULL, NULL, NULL); |
---|
341 | if (ret == 0) { |
---|
342 | *name = resource_string_copy(found_name, found_namelen); |
---|
343 | *unit = found_unit; |
---|
344 | } |
---|
345 | *anchor = newln; |
---|
346 | return ret; |
---|
347 | } |
---|
348 | |
---|
349 | #ifndef __rtems__ |
---|
350 | |
---|
351 | /* |
---|
352 | * err = resource_find_dev(&anchor, name, &unit, res, value); |
---|
353 | * Iterate through a list of devices, returning their unit numbers. |
---|
354 | * res and value are optional restrictions. eg: "at", "scbus0". |
---|
355 | * *unit is set to the value. |
---|
356 | * set *anchor to zero before starting. |
---|
357 | */ |
---|
358 | int |
---|
359 | resource_find_dev(int *anchor, const char *name, int *unit, |
---|
360 | const char *resname, const char *value) |
---|
361 | { |
---|
362 | int found_unit; |
---|
363 | int newln; |
---|
364 | int ret; |
---|
365 | |
---|
366 | newln = *anchor; |
---|
367 | ret = resource_find(anchor, &newln, name, NULL, resname, value, |
---|
368 | NULL, NULL, &found_unit, NULL, NULL, NULL); |
---|
369 | if (ret == 0) { |
---|
370 | *unit = found_unit; |
---|
371 | } |
---|
372 | *anchor = newln; |
---|
373 | return ret; |
---|
374 | } |
---|
375 | |
---|
376 | /* |
---|
377 | * Check to see if a device is disabled via a disabled hint. |
---|
378 | */ |
---|
379 | int |
---|
380 | resource_disabled(const char *name, int unit) |
---|
381 | { |
---|
382 | int error, value; |
---|
383 | |
---|
384 | error = resource_int_value(name, unit, "disabled", &value); |
---|
385 | if (error) |
---|
386 | return (0); |
---|
387 | return (value); |
---|
388 | } |
---|
389 | #endif /* __rtems__ */ |
---|