2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000,2001 Peter Wemm <peter@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
41 #define FBACK_MDENV 0 /* MD env (e.g. loader.conf) */
42 #define FBACK_STENV 1 /* Static env */
43 #define FBACK_STATIC 2 /* static_hints */
46 * We'll use hintenv_merged to indicate that the dynamic environment has been
47 * properly prepared for hint usage. This implies that the dynamic environment
48 * has already been setup (dynamic_kenv) and that we have added any supplied
49 * static_hints to the dynamic environment.
51 static bool hintenv_merged;
52 /* Static environment and static hints cannot change, so we'll skip known bad */
53 static bool stenv_skip;
54 static bool sthints_skip;
56 * Access functions for device resources.
60 static_hints_to_env(void *data __unused)
67 while (cp && *cp != '\0') {
75 line = malloc(i + 1, M_TEMP, M_WAITOK);
77 line[eqidx] = line[i] = '\0';
79 * Before adding a hint to the dynamic environment, check if
80 * another value for said hint has already been added. This is
81 * needed because static environment overrides static hints and
82 * dynamic environment overrides all.
84 if (testenv(line) == 0)
85 kern_setenv(line, line + eqidx + 1);
89 hintenv_merged = true;
92 /* Any time after dynamic env is setup */
93 SYSINIT(hintenv, SI_SUB_KMEM + 1, SI_ORDER_SECOND, static_hints_to_env, NULL);
96 * Checks the environment to see if we even have any hints. If it has no hints,
97 * then res_find can take the hint that there's no point in searching it and
98 * either move on to the next environment or fail early.
101 _res_checkenv(char *envp)
107 if (strncmp(cp, "hint.", 5) == 0)
119 * Evil wildcarding resource string lookup.
120 * This walks the supplied env string table and returns a match.
121 * The start point can be remembered for incremental searches.
124 res_find(char **hintp_cookie, int *line, int *startln,
125 const char *name, int *unit, const char *resname, const char *value,
126 const char **ret_name, int *ret_namelen, int *ret_unit,
127 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
129 int fbacklvl = FBACK_MDENV, i = 0, n = 0;
136 bool dyn_used = false;
140 * We are expecting that the caller will pass us a hintp_cookie that
141 * they are tracking. Upon entry, if *hintp_cookie is *not* set, this
142 * indicates to us that we should be figuring out based on the current
143 * environment where to search. This keeps us sane throughout the
144 * entirety of a single search.
146 if (*hintp_cookie == NULL) {
148 if (hintenv_merged) {
150 * static_hints, if it was previously used, has
151 * already been folded in to the environment
154 mtx_lock(&kenv_lock);
156 for (i = 0; cp != NULL; cp = kenvp[++i]) {
157 if (!strncmp(cp, "hint.", 5)) {
162 mtx_unlock(&kenv_lock);
166 * We'll have a chance to keep coming back here until
167 * we've actually exhausted all of our possibilities.
168 * We might have chosen the MD/Static env because it
169 * had some kind of hints, but perhaps it didn't have
170 * the hint we are looking for. We don't provide any
171 * fallback when searching the dynamic environment.
174 if (dyn_used || fbacklvl >= FBACK_STATIC)
180 if (_res_checkenv(md_envp)) {
188 if (!stenv_skip && _res_checkenv(kern_envp)) {
197 /* We'll fallback to static_hints if needed/can */
199 _res_checkenv(static_hints))
200 hintp = static_hints;
212 *hintp_cookie = hintp;
214 hintp = *hintp_cookie;
215 if (hintenv_merged && hintp == kenvp[0])
219 * If we aren't using the dynamic environment, we need
220 * to run through the proper fallback procedure again.
221 * This is so that we do continuations right if we're
222 * working with *line and *startln.
228 mtx_lock(&kenv_lock);
235 if (strncmp(cp, "hint.", 5) != 0)
237 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s", r_name, &r_unit,
240 printf("CONFIG: invalid hint '%s'\n", cp);
245 if (startln && *startln >= 0 && *line < *startln)
247 if (name && strcmp(name, r_name) != 0)
249 if (unit && *unit != r_unit)
251 if (resname && strcmp(resname, r_resname) != 0)
253 if (value && strcmp(value, r_value) != 0)
255 /* Successfully found a hint matching all criteria */
273 mtx_unlock(&kenv_lock);
278 /* This is a bit of a hack, but at least is reentrant */
279 /* Note that it returns some !unterminated! strings. */
280 s = strchr(s, '.') + 1; /* start of device */
283 s = strchr(s, '.') + 1; /* start of unit */
284 if (ret_namelen && ret_name)
285 *ret_namelen = s - *ret_name - 1; /* device length */
288 s = strchr(s, '.') + 1; /* start of resname */
291 s = strchr(s, '=') + 1; /* start of value */
292 if (ret_resnamelen && ret_resname)
293 *ret_resnamelen = s - *ret_resname - 1; /* value len */
296 if (startln) /* line number for anchor */
297 *startln = *line + 1;
302 * Search all the data sources for matches to our query. We look for
303 * dynamic hints first as overrides for static or fallback hints.
306 resource_find(int *line, int *startln,
307 const char *name, int *unit, const char *resname, const char *value,
308 const char **ret_name, int *ret_namelen, int *ret_unit,
309 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
318 /* Search for exact unit matches first */
319 i = res_find(&hintp, line, startln, name, unit, resname, value,
320 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
326 /* If we are still here, search for wildcard matches */
328 i = res_find(&hintp, line, startln, name, &un, resname, value,
329 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
337 resource_int_value(const char *name, int unit, const char *resname, int *result)
346 error = resource_find(&line, NULL, name, &unit, resname, NULL,
347 NULL, NULL, NULL, NULL, NULL, &str);
352 val = strtoul(str, &op, 0);
360 resource_long_value(const char *name, int unit, const char *resname,
370 error = resource_find(&line, NULL, name, &unit, resname, NULL,
371 NULL, NULL, NULL, NULL, NULL, &str);
376 val = strtoul(str, &op, 0);
384 resource_string_value(const char *name, int unit, const char *resname,
392 error = resource_find(&line, NULL, name, &unit, resname, NULL,
393 NULL, NULL, NULL, NULL, NULL, &str);
401 * This is a bit nasty, but allows us to not modify the env strings.
404 resource_string_copy(const char *s, int len)
406 static char stringbuf[256];
407 static int offset = 0;
414 if ((offset + len + 1) > 255)
416 bcopy(s, &stringbuf[offset], len);
417 stringbuf[offset + len] = '\0';
418 ret = &stringbuf[offset];
424 * err = resource_find_match(&anchor, &name, &unit, resname, value)
425 * Iteratively fetch a list of devices wired "at" something
426 * res and value are restrictions. eg: "at", "scbus0".
427 * For practical purposes, res = required, value = optional.
428 * *name and *unit are set.
429 * set *anchor to zero before starting.
432 resource_find_match(int *anchor, const char **name, int *unit,
433 const char *resname, const char *value)
435 const char *found_name;
442 ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
443 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
445 *name = resource_string_copy(found_name, found_namelen);
454 * err = resource_find_dev(&anchor, name, &unit, res, value);
455 * Iterate through a list of devices, returning their unit numbers.
456 * res and value are optional restrictions. eg: "at", "scbus0".
457 * *unit is set to the value.
458 * set *anchor to zero before starting.
461 resource_find_dev(int *anchor, const char *name, int *unit,
462 const char *resname, const char *value)
469 ret = resource_find(anchor, &newln, name, NULL, resname, value,
470 NULL, NULL, &found_unit, NULL, NULL, NULL);
479 * Check to see if a device is disabled via a disabled hint.
482 resource_disabled(const char *name, int unit)
486 error = resource_int_value(name, unit, "disabled", &value);
493 * Clear a value associated with a device by removing it from
494 * the kernel environment. This only removes a hint for an
498 resource_unset_value(const char *name, int unit, const char *resname)
501 const char *retname, *retvalue;
506 error = resource_find(&line, NULL, name, &unit, resname, NULL,
507 &retname, NULL, NULL, NULL, NULL, &retvalue);
511 retname -= strlen("hint.");
512 len = retvalue - retname - 1;
513 if (len > sizeof(varname) - 1)
514 return (ENAMETOOLONG);
515 memcpy(varname, retname, len);
517 return (kern_unsetenv(varname));