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>
41 * Access functions for device resources.
44 static int checkmethod = 1;
49 * Define kern.hintmode sysctl, which only accept value 2, that cause to
50 * switch from Static KENV mode to Dynamic KENV. So systems that have hints
51 * compiled into kernel will be able to see/modify KENV (and hints too).
55 sysctl_hintmode(SYSCTL_HANDLER_ARGS)
59 int eqidx, error, from_kenv, i, value;
65 /* Fetch candidate for new hintmode value */
66 error = sysctl_handle_int(oidp, &value, 0, req);
67 if (error || req->newptr == NULL)
71 /* Only accept swithing to hintmode 2 */
74 /* Migrate from static to dynamic hints */
79 * Already here. But assign hintmode to 2, to not
80 * check it in the future.
92 /* Nothing to do, hintmode already 2 */
101 if (strncmp(cp, "hint.", 5) != 0)
102 /* kenv can have not only hints */
105 eq = strchr(cp, '=');
111 line = malloc(i+1, M_TEMP, M_WAITOK);
114 kern_setenv(line, line + eqidx + 1);
124 SYSCTL_PROC(_kern, OID_AUTO, hintmode, CTLTYPE_INT|CTLFLAG_RW,
125 &hintmode, 0, sysctl_hintmode, "I", "Get/set current hintmode");
128 * Evil wildcarding resource string lookup.
129 * This walks the supplied env string table and returns a match.
130 * The start point can be remembered for incremental searches.
133 res_find(int *line, int *startln,
134 const char *name, int *unit, const char *resname, const char *value,
135 const char **ret_name, int *ret_namelen, int *ret_unit,
136 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
138 int n = 0, hit, i = 0;
150 case 0: /* loader hints in environment only */
152 case 1: /* static hints only */
153 hintp = static_hints;
156 case 2: /* fallback mode */
158 mtx_lock(&kenv_lock);
160 for (i = 0; cp != NULL; cp = kenvp[++i]) {
161 if (!strncmp(cp, "hint.", 5)) {
167 mtx_unlock(&kenv_lock);
171 if (strncmp(cp, "hint.", 5) == 0) {
181 hintp = static_hints;
200 mtx_lock(&kenv_lock);
204 mtx_unlock(&kenv_lock);
212 if (strncmp(cp, "hint.", 5) != 0)
215 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s",
216 r_name, &r_unit, r_resname, r_value);
218 printf("CONFIG: invalid hint '%s'\n", cp);
223 if (hit && startln && *startln >= 0 && *line < *startln)
225 if (hit && name && strcmp(name, r_name) != 0)
227 if (hit && unit && *unit != r_unit)
229 if (hit && resname && strcmp(resname, r_resname) != 0)
231 if (hit && value && strcmp(value, r_value) != 0)
250 mtx_unlock(&kenv_lock);
255 /* This is a bit of a hack, but at least is reentrant */
256 /* Note that it returns some !unterminated! strings. */
257 s = strchr(s, '.') + 1; /* start of device */
260 s = strchr(s, '.') + 1; /* start of unit */
261 if (ret_namelen && ret_name)
262 *ret_namelen = s - *ret_name - 1; /* device length */
265 s = strchr(s, '.') + 1; /* start of resname */
268 s = strchr(s, '=') + 1; /* start of value */
269 if (ret_resnamelen && ret_resname)
270 *ret_resnamelen = s - *ret_resname - 1; /* value len */
273 if (startln) /* line number for anchor */
274 *startln = *line + 1;
279 * Search all the data sources for matches to our query. We look for
280 * dynamic hints first as overrides for static or fallback hints.
283 resource_find(int *line, int *startln,
284 const char *name, int *unit, const char *resname, const char *value,
285 const char **ret_name, int *ret_namelen, int *ret_unit,
286 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
293 /* Search for exact unit matches first */
294 i = res_find(line, startln, name, unit, resname, value,
295 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
301 /* If we are still here, search for wildcard matches */
303 i = res_find(line, startln, name, &un, resname, value,
304 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
312 resource_int_value(const char *name, int unit, const char *resname, int *result)
321 error = resource_find(&line, NULL, name, &unit, resname, NULL,
322 NULL, NULL, NULL, NULL, NULL, &str);
327 val = strtoul(str, &op, 0);
335 resource_long_value(const char *name, int unit, const char *resname,
345 error = resource_find(&line, NULL, name, &unit, resname, NULL,
346 NULL, NULL, NULL, NULL, NULL, &str);
351 val = strtoul(str, &op, 0);
359 resource_string_value(const char *name, int unit, const char *resname,
367 error = resource_find(&line, NULL, name, &unit, resname, NULL,
368 NULL, NULL, NULL, NULL, NULL, &str);
376 * This is a bit nasty, but allows us to not modify the env strings.
379 resource_string_copy(const char *s, int len)
381 static char stringbuf[256];
382 static int offset = 0;
389 if ((offset + len + 1) > 255)
391 bcopy(s, &stringbuf[offset], len);
392 stringbuf[offset + len] = '\0';
393 ret = &stringbuf[offset];
399 * err = resource_find_match(&anchor, &name, &unit, resname, value)
400 * Iteratively fetch a list of devices wired "at" something
401 * res and value are restrictions. eg: "at", "scbus0".
402 * For practical purposes, res = required, value = optional.
403 * *name and *unit are set.
404 * set *anchor to zero before starting.
407 resource_find_match(int *anchor, const char **name, int *unit,
408 const char *resname, const char *value)
410 const char *found_name;
417 ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
418 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
420 *name = resource_string_copy(found_name, found_namelen);
429 * err = resource_find_dev(&anchor, name, &unit, res, value);
430 * Iterate through a list of devices, returning their unit numbers.
431 * res and value are optional restrictions. eg: "at", "scbus0".
432 * *unit is set to the value.
433 * set *anchor to zero before starting.
436 resource_find_dev(int *anchor, const char *name, int *unit,
437 const char *resname, const char *value)
444 ret = resource_find(anchor, &newln, name, NULL, resname, value,
445 NULL, NULL, &found_unit, NULL, NULL, NULL);
454 * Check to see if a device is disabled via a disabled hint.
457 resource_disabled(const char *name, int unit)
461 error = resource_int_value(name, unit, "disabled", &value);
468 * Clear a value associated with a device by removing it from
469 * the kernel environment. This only removes a hint for an
473 resource_unset_value(const char *name, int unit, const char *resname)
476 const char *retname, *retvalue;
481 error = resource_find(&line, NULL, name, &unit, resname, NULL,
482 &retname, NULL, NULL, NULL, NULL, &retvalue);
486 retname -= strlen("hint.");
487 len = retvalue - retname - 1;
488 if (len > sizeof(varname) - 1)
489 return (ENAMETOOLONG);
490 memcpy(varname, retname, len);
492 return (kern_unsetenv(varname));