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/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
40 #define HINTMODE_KENV 0
41 #define HINTMODE_STATIC 1
42 #define HINTMODE_FALLBACK 2
45 * Access functions for device resources.
48 static int checkmethod = 1;
52 * Define kern.hintmode sysctl, which only accept value 2, that cause to
53 * switch from Static KENV mode to Dynamic KENV. So systems that have hints
54 * compiled into kernel will be able to see/modify KENV (and hints too).
58 sysctl_hintmode(SYSCTL_HANDLER_ARGS)
62 int eqidx, error, i, value;
66 /* Fetch candidate for new hintmode value */
67 error = sysctl_handle_int(oidp, &value, 0, req);
68 if (error || req->newptr == NULL)
71 if (value != HINTMODE_FALLBACK)
72 /* Only accept swithing to hintmode 2 */
76 * The rest of the sysctl handler is just making sure that our
77 * environment is consistent with the world we've already seen.
78 * If we came from kenv at all, then we have nothing to do: static
79 * kenv will get merged into dynamic kenv as soon as kmem becomes
80 * available, dynamic kenv is the environment we'd be setting these
81 * things in anyways. Therefore, we have nothing left to do unless
82 * we came from a static hints configuration.
84 if (hintmode != HINTMODE_STATIC) {
90 while (cp && *cp != '\0') {
98 line = malloc(i+1, M_TEMP, M_WAITOK);
101 kern_setenv(line, line + eqidx + 1);
110 SYSCTL_PROC(_kern, OID_AUTO, hintmode, CTLTYPE_INT|CTLFLAG_RW,
111 &hintmode, 0, sysctl_hintmode, "I", "Get/set current hintmode");
114 * Evil wildcarding resource string lookup.
115 * This walks the supplied env string table and returns a match.
116 * The start point can be remembered for incremental searches.
119 res_find(int *line, int *startln,
120 const char *name, int *unit, const char *resname, const char *value,
121 const char **ret_name, int *ret_namelen, int *ret_unit,
122 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
124 int n = 0, hit, i = 0;
126 int r_unit, use_kenv = (hintmode != HINTMODE_STATIC && dynamic_kenv);
136 case HINTMODE_KENV: /* loader hints in environment only */
138 case HINTMODE_STATIC: /* static hints only */
139 hintp = static_hints;
142 case HINTMODE_FALLBACK: /* fallback mode */
144 mtx_lock(&kenv_lock);
146 for (i = 0; cp != NULL; cp = kenvp[++i]) {
147 if (!strncmp(cp, "hint.", 5)) {
153 mtx_unlock(&kenv_lock);
157 if (strncmp(cp, "hint.", 5) == 0) {
167 hintp = static_hints;
186 mtx_lock(&kenv_lock);
190 mtx_unlock(&kenv_lock);
198 if (strncmp(cp, "hint.", 5) != 0)
201 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s",
202 r_name, &r_unit, r_resname, r_value);
204 printf("CONFIG: invalid hint '%s'\n", cp);
209 if (hit && startln && *startln >= 0 && *line < *startln)
211 if (hit && name && strcmp(name, r_name) != 0)
213 if (hit && unit && *unit != r_unit)
215 if (hit && resname && strcmp(resname, r_resname) != 0)
217 if (hit && value && strcmp(value, r_value) != 0)
236 mtx_unlock(&kenv_lock);
241 /* This is a bit of a hack, but at least is reentrant */
242 /* Note that it returns some !unterminated! strings. */
243 s = strchr(s, '.') + 1; /* start of device */
246 s = strchr(s, '.') + 1; /* start of unit */
247 if (ret_namelen && ret_name)
248 *ret_namelen = s - *ret_name - 1; /* device length */
251 s = strchr(s, '.') + 1; /* start of resname */
254 s = strchr(s, '=') + 1; /* start of value */
255 if (ret_resnamelen && ret_resname)
256 *ret_resnamelen = s - *ret_resname - 1; /* value len */
259 if (startln) /* line number for anchor */
260 *startln = *line + 1;
265 * Search all the data sources for matches to our query. We look for
266 * dynamic hints first as overrides for static or fallback hints.
269 resource_find(int *line, int *startln,
270 const char *name, int *unit, const char *resname, const char *value,
271 const char **ret_name, int *ret_namelen, int *ret_unit,
272 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
279 /* Search for exact unit matches first */
280 i = res_find(line, startln, name, unit, resname, value,
281 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
287 /* If we are still here, search for wildcard matches */
289 i = res_find(line, startln, name, &un, resname, value,
290 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
298 resource_int_value(const char *name, int unit, const char *resname, int *result)
307 error = resource_find(&line, NULL, name, &unit, resname, NULL,
308 NULL, NULL, NULL, NULL, NULL, &str);
313 val = strtoul(str, &op, 0);
321 resource_long_value(const char *name, int unit, const char *resname,
331 error = resource_find(&line, NULL, name, &unit, resname, NULL,
332 NULL, NULL, NULL, NULL, NULL, &str);
337 val = strtoul(str, &op, 0);
345 resource_string_value(const char *name, int unit, const char *resname,
353 error = resource_find(&line, NULL, name, &unit, resname, NULL,
354 NULL, NULL, NULL, NULL, NULL, &str);
362 * This is a bit nasty, but allows us to not modify the env strings.
365 resource_string_copy(const char *s, int len)
367 static char stringbuf[256];
368 static int offset = 0;
375 if ((offset + len + 1) > 255)
377 bcopy(s, &stringbuf[offset], len);
378 stringbuf[offset + len] = '\0';
379 ret = &stringbuf[offset];
385 * err = resource_find_match(&anchor, &name, &unit, resname, value)
386 * Iteratively fetch a list of devices wired "at" something
387 * res and value are restrictions. eg: "at", "scbus0".
388 * For practical purposes, res = required, value = optional.
389 * *name and *unit are set.
390 * set *anchor to zero before starting.
393 resource_find_match(int *anchor, const char **name, int *unit,
394 const char *resname, const char *value)
396 const char *found_name;
403 ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
404 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
406 *name = resource_string_copy(found_name, found_namelen);
415 * err = resource_find_dev(&anchor, name, &unit, res, value);
416 * Iterate through a list of devices, returning their unit numbers.
417 * res and value are optional restrictions. eg: "at", "scbus0".
418 * *unit is set to the value.
419 * set *anchor to zero before starting.
422 resource_find_dev(int *anchor, const char *name, int *unit,
423 const char *resname, const char *value)
430 ret = resource_find(anchor, &newln, name, NULL, resname, value,
431 NULL, NULL, &found_unit, NULL, NULL, NULL);
440 * Check to see if a device is disabled via a disabled hint.
443 resource_disabled(const char *name, int unit)
447 error = resource_int_value(name, unit, "disabled", &value);
454 * Clear a value associated with a device by removing it from
455 * the kernel environment. This only removes a hint for an
459 resource_unset_value(const char *name, int unit, const char *resname)
462 const char *retname, *retvalue;
467 error = resource_find(&line, NULL, name, &unit, resname, NULL,
468 &retname, NULL, NULL, NULL, NULL, &retvalue);
472 retname -= strlen("hint.");
473 len = retvalue - retname - 1;
474 if (len > sizeof(varname) - 1)
475 return (ENAMETOOLONG);
476 memcpy(varname, retname, len);
478 return (kern_unsetenv(varname));