2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
40 #define FBACK_MDENV 0 /* MD env (e.g. loader.conf) */
41 #define FBACK_STENV 1 /* Static env */
42 #define FBACK_STATIC 2 /* static_hints */
45 * We'll use hintenv_merged to indicate that the dynamic environment has been
46 * properly prepared for hint usage. This implies that the dynamic environment
47 * has already been setup (dynamic_kenv) and that we have added any supplied
48 * static_hints to the dynamic environment.
50 static bool hintenv_merged;
51 /* Static environment and static hints cannot change, so we'll skip known bad */
52 static bool stenv_skip;
53 static bool sthints_skip;
55 * Access functions for device resources.
59 static_hints_to_env(void *data __unused)
66 while (cp && *cp != '\0') {
74 line = malloc(i + 1, M_TEMP, M_WAITOK);
76 line[eqidx] = line[i] = '\0';
78 * Before adding a hint to the dynamic environment, check if
79 * another value for said hint has already been added. This is
80 * needed because static environment overrides static hints and
81 * dynamic environment overrides all.
83 if (testenv(line) == 0)
84 kern_setenv(line, line + eqidx + 1);
88 hintenv_merged = true;
91 /* Any time after dynamic env is setup */
92 SYSINIT(hintenv, SI_SUB_KMEM + 1, SI_ORDER_SECOND, static_hints_to_env, NULL);
95 * Checks the environment to see if we even have any hints. If it has no hints,
96 * then res_find can take the hint that there's no point in searching it and
97 * either move on to the next environment or fail early.
100 _res_checkenv(char *envp)
106 if (strncmp(cp, "hint.", 5) == 0)
118 * Evil wildcarding resource string lookup.
119 * This walks the supplied env string table and returns a match.
120 * The start point can be remembered for incremental searches.
123 res_find(char **hintp_cookie, int *line, int *startln,
124 const char *name, int *unit, const char *resname, const char *value,
125 const char **ret_name, int *ret_namelen, int *ret_unit,
126 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
128 int fbacklvl = FBACK_MDENV, i = 0, n = 0, namelen;
135 bool dyn_used = false;
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.
144 if (*hintp_cookie == NULL) {
146 if (hintenv_merged) {
148 * static_hints, if it was previously used, has
149 * already been folded in to the environment
152 mtx_lock(&kenv_lock);
154 for (i = 0; cp != NULL; cp = kenvp[++i]) {
155 if (!strncmp(cp, "hint.", 5)) {
160 mtx_unlock(&kenv_lock);
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.
172 if (dyn_used || fbacklvl >= FBACK_STATIC)
178 if (_res_checkenv(md_envp)) {
186 if (!stenv_skip && _res_checkenv(kern_envp)) {
195 /* We'll fallback to static_hints if needed/can */
197 _res_checkenv(static_hints))
198 hintp = static_hints;
210 *hintp_cookie = hintp;
212 hintp = *hintp_cookie;
213 if (hintenv_merged && hintp == kenvp[0])
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.
226 mtx_lock(&kenv_lock);
231 namelen = strlen(name);
235 if (strncmp(cp, "hint.", 5) != 0)
237 if (name && strncmp(cp + 5, name, namelen) != 0)
239 n = sscanf(cp + 5, "%32[^.].%d.%32[^=]=%127s", r_name, &r_unit,
242 printf("CONFIG: invalid hint '%s'\n", cp);
247 if (startln && *startln >= 0 && *line < *startln)
249 if (name && strcmp(name, r_name) != 0)
251 if (unit && *unit != r_unit)
253 if (resname && strcmp(resname, r_resname) != 0)
255 if (value && strcmp(value, r_value) != 0)
257 /* Successfully found a hint matching all criteria */
275 mtx_unlock(&kenv_lock);
280 /* This is a bit of a hack, but at least is reentrant */
281 /* Note that it returns some !unterminated! strings. */
282 s = strchr(s, '.') + 1; /* start of device */
285 s = strchr(s, '.') + 1; /* start of unit */
286 if (ret_namelen && ret_name)
287 *ret_namelen = s - *ret_name - 1; /* device length */
290 s = strchr(s, '.') + 1; /* start of resname */
293 s = strchr(s, '=') + 1; /* start of value */
294 if (ret_resnamelen && ret_resname)
295 *ret_resnamelen = s - *ret_resname - 1; /* value len */
298 if (startln) /* line number for anchor */
299 *startln = *line + 1;
304 * Search all the data sources for matches to our query. We look for
305 * dynamic hints first as overrides for static or fallback hints.
308 resource_find(int *line, int *startln,
309 const char *name, int *unit, const char *resname, const char *value,
310 const char **ret_name, int *ret_namelen, int *ret_unit,
311 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
320 /* Search for exact unit matches first */
321 i = res_find(&hintp, line, startln, name, unit, resname, value,
322 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
328 /* If we are still here, search for wildcard matches */
330 i = res_find(&hintp, line, startln, name, &un, resname, value,
331 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
339 resource_int_value(const char *name, int unit, const char *resname, int *result)
348 error = resource_find(&line, NULL, name, &unit, resname, NULL,
349 NULL, NULL, NULL, NULL, NULL, &str);
354 val = strtoul(str, &op, 0);
362 resource_long_value(const char *name, int unit, const char *resname,
372 error = resource_find(&line, NULL, name, &unit, resname, NULL,
373 NULL, NULL, NULL, NULL, NULL, &str);
378 val = strtoul(str, &op, 0);
386 resource_string_value(const char *name, int unit, const char *resname,
394 error = resource_find(&line, NULL, name, &unit, resname, NULL,
395 NULL, NULL, NULL, NULL, NULL, &str);
403 * This is a bit nasty, but allows us to not modify the env strings.
406 resource_string_copy(const char *s, int len)
408 static char stringbuf[256];
409 static int offset = 0;
416 if ((offset + len + 1) > 255)
418 bcopy(s, &stringbuf[offset], len);
419 stringbuf[offset + len] = '\0';
420 ret = &stringbuf[offset];
426 * err = resource_find_match(&anchor, &name, &unit, resname, value)
427 * Iteratively fetch a list of devices wired "at" something
428 * res and value are restrictions. eg: "at", "scbus0".
429 * For practical purposes, res = required, value = optional.
430 * *name and *unit are set.
431 * set *anchor to zero before starting.
434 resource_find_match(int *anchor, const char **name, int *unit,
435 const char *resname, const char *value)
437 const char *found_name;
444 ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
445 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
447 *name = resource_string_copy(found_name, found_namelen);
455 * err = resource_find_dev(&anchor, name, &unit, res, value);
456 * Iterate through a list of devices, returning their unit numbers.
457 * res and value are optional restrictions. eg: "at", "scbus0".
458 * *unit is set to the value.
459 * set *anchor to zero before starting.
462 resource_find_dev(int *anchor, const char *name, int *unit,
463 const char *resname, const char *value)
470 ret = resource_find(anchor, &newln, name, NULL, resname, value,
471 NULL, NULL, &found_unit, NULL, NULL, NULL);
480 * Check to see if a device is disabled via a disabled hint.
483 resource_disabled(const char *name, int unit)
487 error = resource_int_value(name, unit, "disabled", &value);
494 * Clear a value associated with a device by removing it from
495 * the kernel environment. This only removes a hint for an
499 resource_unset_value(const char *name, int unit, const char *resname)
502 const char *retname, *retvalue;
507 error = resource_find(&line, NULL, name, &unit, resname, NULL,
508 &retname, NULL, NULL, NULL, NULL, &retvalue);
512 retname -= strlen("hint.");
513 len = retvalue - retname - 1;
514 if (len > sizeof(varname) - 1)
515 return (ENAMETOOLONG);
516 memcpy(varname, retname, len);
518 return (kern_unsetenv(varname));