FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_hints.c
1 /*-
2 * Copyright (c) 2000,2001 Peter Wemm <peter@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/lock.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38
39 #define FBACK_MDENV 0 /* MD env (e.g. loader.conf) */
40 #define FBACK_STENV 1 /* Static env */
41 #define FBACK_STATIC 2 /* static_hints */
42
43 /*
44 * We'll use hintenv_merged to indicate that the dynamic environment has been
45 * properly prepared for hint usage. This implies that the dynamic environment
46 * has already been setup (dynamic_kenv) and that we have added any supplied
47 * static_hints to the dynamic environment.
48 */
49 static bool hintenv_merged;
50 /* Static environment and static hints cannot change, so we'll skip known bad */
51 static bool stenv_skip;
52 static bool sthints_skip;
53 /*
54 * Access functions for device resources.
55 */
56
57 static void
58 static_hints_to_env(void *data __unused)
59 {
60 const char *cp;
61 char *line, *eq;
62 int eqidx, i;
63
64 cp = static_hints;
65 while (cp && *cp != '\0') {
66 eq = strchr(cp, '=');
67 if (eq == NULL)
68 /* Bad hint value */
69 continue;
70 eqidx = eq - cp;
71
72 i = strlen(cp);
73 line = malloc(i + 1, M_TEMP, M_WAITOK);
74 strcpy(line, cp);
75 line[eqidx] = line[i] = '\0';
76 /*
77 * Before adding a hint to the dynamic environment, check if
78 * another value for said hint has already been added. This is
79 * needed because static environment overrides static hints and
80 * dynamic environment overrides all.
81 */
82 if (testenv(line) == 0)
83 kern_setenv(line, line + eqidx + 1);
84 free(line, M_TEMP);
85 cp += i + 1;
86 }
87 hintenv_merged = true;
88 }
89
90 /* Any time after dynamic env is setup */
91 SYSINIT(hintenv, SI_SUB_KMEM + 1, SI_ORDER_SECOND, static_hints_to_env, NULL);
92
93 /*
94 * Checks the environment to see if we even have any hints. If it has no hints,
95 * then res_find can take the hint that there's no point in searching it and
96 * either move on to the next environment or fail early.
97 */
98 static bool
99 _res_checkenv(char *envp)
100 {
101 char *cp;
102
103 cp = envp;
104 while (cp) {
105 if (strncmp(cp, "hint.", 5) == 0)
106 return (true);
107 while (*cp != '\0')
108 cp++;
109 cp++;
110 if (*cp == '\0')
111 break;
112 }
113 return (false);
114 }
115
116 /*
117 * Evil wildcarding resource string lookup.
118 * This walks the supplied env string table and returns a match.
119 * The start point can be remembered for incremental searches.
120 */
121 static int
122 res_find(char **hintp_cookie, int *line, int *startln,
123 const char *name, int *unit, const char *resname, const char *value,
124 const char **ret_name, int *ret_namelen, int *ret_unit,
125 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
126 {
127 int fbacklvl = FBACK_MDENV, i = 0, n = 0;
128 char r_name[32];
129 int r_unit;
130 char r_resname[32];
131 char r_value[128];
132 const char *s, *cp;
133 char *hintp, *p;
134 bool dyn_used = false;
135
136
137 /*
138 * We are expecting that the caller will pass us a hintp_cookie that
139 * they are tracking. Upon entry, if *hintp_cookie is *not* set, this
140 * indicates to us that we should be figuring out based on the current
141 * environment where to search. This keeps us sane throughout the
142 * entirety of a single search.
143 */
144 if (*hintp_cookie == NULL) {
145 hintp = NULL;
146 if (hintenv_merged) {
147 /*
148 * static_hints, if it was previously used, has
149 * already been folded in to the environment
150 * by this point.
151 */
152 mtx_lock(&kenv_lock);
153 cp = kenvp[0];
154 for (i = 0; cp != NULL; cp = kenvp[++i]) {
155 if (!strncmp(cp, "hint.", 5)) {
156 hintp = kenvp[0];
157 break;
158 }
159 }
160 mtx_unlock(&kenv_lock);
161 dyn_used = true;
162 } else {
163 /*
164 * We'll have a chance to keep coming back here until
165 * we've actually exhausted all of our possibilities.
166 * We might have chosen the MD/Static env because it
167 * had some kind of hints, but perhaps it didn't have
168 * the hint we are looking for. We don't provide any
169 * fallback when searching the dynamic environment.
170 */
171 fallback:
172 if (dyn_used || fbacklvl >= FBACK_STATIC)
173 return (ENOENT);
174
175 switch (fbacklvl) {
176 case FBACK_MDENV:
177 fbacklvl++;
178 if (_res_checkenv(md_envp)) {
179 hintp = md_envp;
180 break;
181 }
182
183 /* FALLTHROUGH */
184 case FBACK_STENV:
185 fbacklvl++;
186 if (!stenv_skip && _res_checkenv(kern_envp)) {
187 hintp = kern_envp;
188 break;
189 } else
190 stenv_skip = true;
191
192 /* FALLTHROUGH */
193 case FBACK_STATIC:
194 fbacklvl++;
195 /* We'll fallback to static_hints if needed/can */
196 if (!sthints_skip &&
197 _res_checkenv(static_hints))
198 hintp = static_hints;
199 else
200 sthints_skip = true;
201
202 break;
203 default:
204 return (ENOENT);
205 }
206 }
207
208 if (hintp == NULL)
209 return (ENOENT);
210 *hintp_cookie = hintp;
211 } else {
212 hintp = *hintp_cookie;
213 if (hintenv_merged && hintp == kenvp[0])
214 dyn_used = true;
215 else
216 /*
217 * If we aren't using the dynamic environment, we need
218 * to run through the proper fallback procedure again.
219 * This is so that we do continuations right if we're
220 * working with *line and *startln.
221 */
222 goto fallback;
223 }
224
225 if (dyn_used) {
226 mtx_lock(&kenv_lock);
227 i = 0;
228 }
229
230 cp = hintp;
231 while (cp) {
232 (*line)++;
233 if (strncmp(cp, "hint.", 5) != 0)
234 goto nexthint;
235 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s", r_name, &r_unit,
236 r_resname, r_value);
237 if (n != 4) {
238 printf("CONFIG: invalid hint '%s'\n", cp);
239 p = strchr(cp, 'h');
240 *p = 'H';
241 goto nexthint;
242 }
243 if (startln && *startln >= 0 && *line < *startln)
244 goto nexthint;
245 if (name && strcmp(name, r_name) != 0)
246 goto nexthint;
247 if (unit && *unit != r_unit)
248 goto nexthint;
249 if (resname && strcmp(resname, r_resname) != 0)
250 goto nexthint;
251 if (value && strcmp(value, r_value) != 0)
252 goto nexthint;
253 /* Successfully found a hint matching all criteria */
254 break;
255 nexthint:
256 if (dyn_used) {
257 cp = kenvp[++i];
258 if (cp == NULL)
259 break;
260 } else {
261 while (*cp != '\0')
262 cp++;
263 cp++;
264 if (*cp == '\0') {
265 cp = NULL;
266 break;
267 }
268 }
269 }
270 if (dyn_used)
271 mtx_unlock(&kenv_lock);
272 if (cp == NULL)
273 goto fallback;
274
275 s = cp;
276 /* This is a bit of a hack, but at least is reentrant */
277 /* Note that it returns some !unterminated! strings. */
278 s = strchr(s, '.') + 1; /* start of device */
279 if (ret_name)
280 *ret_name = s;
281 s = strchr(s, '.') + 1; /* start of unit */
282 if (ret_namelen && ret_name)
283 *ret_namelen = s - *ret_name - 1; /* device length */
284 if (ret_unit)
285 *ret_unit = r_unit;
286 s = strchr(s, '.') + 1; /* start of resname */
287 if (ret_resname)
288 *ret_resname = s;
289 s = strchr(s, '=') + 1; /* start of value */
290 if (ret_resnamelen && ret_resname)
291 *ret_resnamelen = s - *ret_resname - 1; /* value len */
292 if (ret_value)
293 *ret_value = s;
294 if (startln) /* line number for anchor */
295 *startln = *line + 1;
296 return 0;
297 }
298
299 /*
300 * Search all the data sources for matches to our query. We look for
301 * dynamic hints first as overrides for static or fallback hints.
302 */
303 static int
304 resource_find(int *line, int *startln,
305 const char *name, int *unit, const char *resname, const char *value,
306 const char **ret_name, int *ret_namelen, int *ret_unit,
307 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
308 {
309 int i;
310 int un;
311 char *hintp;
312
313 *line = 0;
314 hintp = NULL;
315
316 /* Search for exact unit matches first */
317 i = res_find(&hintp, line, startln, name, unit, resname, value,
318 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
319 ret_value);
320 if (i == 0)
321 return 0;
322 if (unit == NULL)
323 return ENOENT;
324 /* If we are still here, search for wildcard matches */
325 un = -1;
326 i = res_find(&hintp, line, startln, name, &un, resname, value,
327 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
328 ret_value);
329 if (i == 0)
330 return 0;
331 return ENOENT;
332 }
333
334 int
335 resource_int_value(const char *name, int unit, const char *resname, int *result)
336 {
337 int error;
338 const char *str;
339 char *op;
340 unsigned long val;
341 int line;
342
343 line = 0;
344 error = resource_find(&line, NULL, name, &unit, resname, NULL,
345 NULL, NULL, NULL, NULL, NULL, &str);
346 if (error)
347 return error;
348 if (*str == '\0')
349 return EFTYPE;
350 val = strtoul(str, &op, 0);
351 if (*op != '\0')
352 return EFTYPE;
353 *result = val;
354 return 0;
355 }
356
357 int
358 resource_long_value(const char *name, int unit, const char *resname,
359 long *result)
360 {
361 int error;
362 const char *str;
363 char *op;
364 unsigned long val;
365 int line;
366
367 line = 0;
368 error = resource_find(&line, NULL, name, &unit, resname, NULL,
369 NULL, NULL, NULL, NULL, NULL, &str);
370 if (error)
371 return error;
372 if (*str == '\0')
373 return EFTYPE;
374 val = strtoul(str, &op, 0);
375 if (*op != '\0')
376 return EFTYPE;
377 *result = val;
378 return 0;
379 }
380
381 int
382 resource_string_value(const char *name, int unit, const char *resname,
383 const char **result)
384 {
385 int error;
386 const char *str;
387 int line;
388
389 line = 0;
390 error = resource_find(&line, NULL, name, &unit, resname, NULL,
391 NULL, NULL, NULL, NULL, NULL, &str);
392 if (error)
393 return error;
394 *result = str;
395 return 0;
396 }
397
398 /*
399 * This is a bit nasty, but allows us to not modify the env strings.
400 */
401 static const char *
402 resource_string_copy(const char *s, int len)
403 {
404 static char stringbuf[256];
405 static int offset = 0;
406 const char *ret;
407
408 if (len == 0)
409 len = strlen(s);
410 if (len > 255)
411 return NULL;
412 if ((offset + len + 1) > 255)
413 offset = 0;
414 bcopy(s, &stringbuf[offset], len);
415 stringbuf[offset + len] = '\0';
416 ret = &stringbuf[offset];
417 offset += len + 1;
418 return ret;
419 }
420
421 /*
422 * err = resource_find_match(&anchor, &name, &unit, resname, value)
423 * Iteratively fetch a list of devices wired "at" something
424 * res and value are restrictions. eg: "at", "scbus0".
425 * For practical purposes, res = required, value = optional.
426 * *name and *unit are set.
427 * set *anchor to zero before starting.
428 */
429 int
430 resource_find_match(int *anchor, const char **name, int *unit,
431 const char *resname, const char *value)
432 {
433 const char *found_name;
434 int found_namelen;
435 int found_unit;
436 int ret;
437 int newln;
438
439 newln = *anchor;
440 ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
441 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
442 if (ret == 0) {
443 *name = resource_string_copy(found_name, found_namelen);
444 *unit = found_unit;
445 }
446 *anchor = newln;
447 return ret;
448 }
449
450
451 /*
452 * err = resource_find_dev(&anchor, name, &unit, res, value);
453 * Iterate through a list of devices, returning their unit numbers.
454 * res and value are optional restrictions. eg: "at", "scbus0".
455 * *unit is set to the value.
456 * set *anchor to zero before starting.
457 */
458 int
459 resource_find_dev(int *anchor, const char *name, int *unit,
460 const char *resname, const char *value)
461 {
462 int found_unit;
463 int newln;
464 int ret;
465
466 newln = *anchor;
467 ret = resource_find(anchor, &newln, name, NULL, resname, value,
468 NULL, NULL, &found_unit, NULL, NULL, NULL);
469 if (ret == 0) {
470 *unit = found_unit;
471 }
472 *anchor = newln;
473 return ret;
474 }
475
476 /*
477 * Check to see if a device is disabled via a disabled hint.
478 */
479 int
480 resource_disabled(const char *name, int unit)
481 {
482 int error, value;
483
484 error = resource_int_value(name, unit, "disabled", &value);
485 if (error)
486 return (0);
487 return (value);
488 }
489
490 /*
491 * Clear a value associated with a device by removing it from
492 * the kernel environment. This only removes a hint for an
493 * exact unit.
494 */
495 int
496 resource_unset_value(const char *name, int unit, const char *resname)
497 {
498 char varname[128];
499 const char *retname, *retvalue;
500 int error, line;
501 size_t len;
502
503 line = 0;
504 error = resource_find(&line, NULL, name, &unit, resname, NULL,
505 &retname, NULL, NULL, NULL, NULL, &retvalue);
506 if (error)
507 return (error);
508
509 retname -= strlen("hint.");
510 len = retvalue - retname - 1;
511 if (len > sizeof(varname) - 1)
512 return (ENAMETOOLONG);
513 memcpy(varname, retname, len);
514 varname[len] = '\0';
515 return (kern_unsetenv(varname));
516 }
Cache object: 1df6dfe2347e5619769e3d388f5a4e66
|