2 * Copyright (c) 2000,2001 Peter Wemm <peter@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/systm.h>
37 * Access functions for device resources.
40 static int checkmethod = 1;
45 * Evil wildcarding resource string lookup.
46 * This walks the supplied env string table and returns a match.
47 * The start point can be remembered for incremental searches.
50 res_find(int *line, int *startln,
51 const char *name, int *unit, const char *resname, const char *value,
52 const char **ret_name, int *ret_namelen, int *ret_unit,
53 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
55 int n = 0, hit, i = 0;
65 case 0: /* loader hints in environment only */
67 case 1: /* static hints only */
71 case 2: /* fallback mode */
75 for (i = 0; cp != NULL; cp = kenvp[++i]) {
76 if (!strncmp(cp, "hint.", 5)) {
82 sx_sunlock(&kenv_lock);
86 if (strncmp(cp, "hint.", 5) == 0) {
115 sx_slock(&kenv_lock);
119 sx_sunlock(&kenv_lock);
127 if (strncmp(cp, "hint.", 5) != 0)
130 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%128s",
131 r_name, &r_unit, r_resname, r_value);
133 printf("CONFIG: invalid hint '%s'\n", cp);
134 /* XXX: abuse bogus index() declaration */
139 if (hit && startln && *startln >= 0 && *line < *startln)
141 if (hit && name && strcmp(name, r_name) != 0)
143 if (hit && unit && *unit != r_unit)
145 if (hit && resname && strcmp(resname, r_resname) != 0)
147 if (hit && value && strcmp(value, r_value) != 0)
166 sx_sunlock(&kenv_lock);
171 /* This is a bit of a hack, but at least is reentrant */
172 /* Note that it returns some !unterminated! strings. */
173 s = index(s, '.') + 1; /* start of device */
176 s = index(s, '.') + 1; /* start of unit */
177 if (ret_namelen && ret_name)
178 *ret_namelen = s - *ret_name - 1; /* device length */
181 s = index(s, '.') + 1; /* start of resname */
184 s = index(s, '=') + 1; /* start of value */
185 if (ret_resnamelen && ret_resname)
186 *ret_resnamelen = s - *ret_resname - 1; /* value len */
189 if (startln) /* line number for anchor */
190 *startln = *line + 1;
195 * Search all the data sources for matches to our query. We look for
196 * dynamic hints first as overrides for static or fallback hints.
199 resource_find(int *line, int *startln,
200 const char *name, int *unit, const char *resname, const char *value,
201 const char **ret_name, int *ret_namelen, int *ret_unit,
202 const char **ret_resname, int *ret_resnamelen, const char **ret_value)
209 /* Search for exact unit matches first */
210 i = res_find(line, startln, name, unit, resname, value,
211 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
217 /* If we are still here, search for wildcard matches */
219 i = res_find(line, startln, name, &un, resname, value,
220 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
228 resource_int_value(const char *name, int unit, const char *resname, int *result)
237 error = resource_find(&line, NULL, name, &unit, resname, NULL,
238 NULL, NULL, NULL, NULL, NULL, &str);
243 val = strtoul(str, &op, 0);
251 resource_long_value(const char *name, int unit, const char *resname,
261 error = resource_find(&line, NULL, name, &unit, resname, NULL,
262 NULL, NULL, NULL, NULL, NULL, &str);
267 val = strtoul(str, &op, 0);
275 resource_string_value(const char *name, int unit, const char *resname,
283 error = resource_find(&line, NULL, name, &unit, resname, NULL,
284 NULL, NULL, NULL, NULL, NULL, &str);
292 * This is a bit nasty, but allows us to not modify the env strings.
295 resource_string_copy(const char *s, int len)
297 static char stringbuf[256];
298 static int offset = 0;
305 if ((offset + len + 1) > 255)
307 bcopy(s, &stringbuf[offset], len);
308 stringbuf[offset + len] = '\0';
309 ret = &stringbuf[offset];
315 * err = resource_find_match(&anchor, &name, &unit, resname, value)
316 * Iteratively fetch a list of devices wired "at" something
317 * res and value are restrictions. eg: "at", "scbus0".
318 * For practical purposes, res = required, value = optional.
319 * *name and *unit are set.
320 * set *anchor to zero before starting.
323 resource_find_match(int *anchor, const char **name, int *unit,
324 const char *resname, const char *value)
326 const char *found_name;
333 ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
334 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
336 *name = resource_string_copy(found_name, found_namelen);
345 * err = resource_find_dev(&anchor, name, &unit, res, value);
346 * Iterate through a list of devices, returning their unit numbers.
347 * res and value are optional restrictions. eg: "at", "scbus0".
348 * *unit is set to the value.
349 * set *anchor to zero before starting.
352 resource_find_dev(int *anchor, const char *name, int *unit,
353 const char *resname, const char *value)
360 ret = resource_find(anchor, &newln, name, NULL, resname, value,
361 NULL, NULL, &found_unit, NULL, NULL, NULL);
370 * Check to see if a device is disabled via a disabled hint.
373 resource_disabled(const char *name, int unit)
377 error = resource_int_value(name, unit, "disabled", &value);