]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_hints.c
subr_hints: Fix acpi unit hinting (at the very least)
[FreeBSD/FreeBSD.git] / sys / kern / subr_hints.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000,2001 Peter Wemm <peter@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39
40 #define HINTMODE_KENV           0
41 #define HINTMODE_STATIC         1
42 #define HINTMODE_FALLBACK       2
43
44 /*
45  * Access functions for device resources.
46  */
47
48 static int checkmethod = 1;
49 static char *hintp;
50
51 /*
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).
55  */
56
57 static int
58 sysctl_hintmode(SYSCTL_HANDLER_ARGS)
59 {
60         const char *cp;
61         char *line, *eq;
62         int eqidx, error, i, value;
63
64         value = hintmode;
65
66         /* Fetch candidate for new hintmode value */
67         error = sysctl_handle_int(oidp, &value, 0, req);
68         if (error || req->newptr == NULL)
69                 return (error);
70
71         if (value != HINTMODE_FALLBACK)
72                 /* Only accept swithing to hintmode 2 */
73                 return (EINVAL);
74
75         /*
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.
83          */
84         if (hintmode != HINTMODE_STATIC) {
85                 hintmode = value;
86                 return (0);
87         }
88
89         cp = static_hints;
90         while (cp && *cp != '\0') {
91                 eq = strchr(cp, '=');
92                 if (eq == NULL)
93                         /* Bad hint value */
94                         continue;
95                 eqidx = eq - cp;
96
97                 i = strlen(cp);
98                 line = malloc(i+1, M_TEMP, M_WAITOK);
99                 strcpy(line, cp);
100                 line[eqidx] = '\0';
101                 kern_setenv(line, line + eqidx + 1);
102                 free(line, M_TEMP);
103                 cp += i + 1;
104         }
105
106         hintmode = value;
107         return (0);
108 }
109
110 SYSCTL_PROC(_kern, OID_AUTO, hintmode, CTLTYPE_INT|CTLFLAG_RW,
111     &hintmode, 0, sysctl_hintmode, "I", "Get/set current hintmode");
112
113 /*
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.
117  */
118 static int
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)
123 {
124         int n = 0, hit, i = 0;
125         char r_name[32];
126         int r_unit, use_kenv = (hintmode != HINTMODE_STATIC && dynamic_kenv);
127         char r_resname[32];
128         char r_value[128];
129         const char *s, *cp;
130         char *p;
131
132         if (checkmethod) {
133                 hintp = NULL;
134
135                 switch (hintmode) {
136                 case HINTMODE_KENV:     /* loader hints in environment only */
137                         break;
138                 case HINTMODE_STATIC:   /* static hints only */
139                         hintp = static_hints;
140                         checkmethod = 0;
141                         break;
142                 case HINTMODE_FALLBACK:         /* fallback mode */
143                         if (dynamic_kenv) {
144                                 mtx_lock(&kenv_lock);
145                                 cp = kenvp[0];
146                                 for (i = 0; cp != NULL; cp = kenvp[++i]) {
147                                         if (!strncmp(cp, "hint.", 5)) {
148                                                 use_kenv = 1;
149                                                 checkmethod = 0;
150                                                 break;
151                                         }
152                                 }
153                                 mtx_unlock(&kenv_lock);
154                         } else {
155                                 cp = kern_envp;
156                                 while (cp) {
157                                         if (strncmp(cp, "hint.", 5) == 0) {
158                                                 cp = NULL;
159                                                 hintp = kern_envp;
160                                                 break;
161                                         }
162                                         while (*cp != '\0')
163                                                 cp++;
164                                         cp++;
165                                         if (*cp == '\0') {
166                                                 cp = NULL;
167                                                 hintp = static_hints;
168                                                 break;
169                                         }
170                                 }
171                         }
172                         break;
173                 default:
174                         break;
175                 }
176                 if (hintp == NULL) {
177                         if (dynamic_kenv) {
178                                 use_kenv = 1;
179                                 checkmethod = 0;
180                         } else
181                                 hintp = kern_envp;
182                 }
183         }
184
185         if (use_kenv) {
186                 mtx_lock(&kenv_lock);
187                 i = 0;
188                 cp = kenvp[0];
189                 if (cp == NULL) {
190                         mtx_unlock(&kenv_lock);
191                         return (ENOENT);
192                 }
193         } else
194                 cp = hintp;
195         while (cp) {
196                 hit = 1;
197                 (*line)++;
198                 if (strncmp(cp, "hint.", 5) != 0)
199                         hit = 0;
200                 else
201                         n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s",
202                             r_name, &r_unit, r_resname, r_value);
203                 if (hit && n != 4) {
204                         printf("CONFIG: invalid hint '%s'\n", cp);
205                         p = strchr(cp, 'h');
206                         *p = 'H';
207                         hit = 0;
208                 }
209                 if (hit && startln && *startln >= 0 && *line < *startln)
210                         hit = 0;
211                 if (hit && name && strcmp(name, r_name) != 0)
212                         hit = 0;
213                 if (hit && unit && *unit != r_unit)
214                         hit = 0;
215                 if (hit && resname && strcmp(resname, r_resname) != 0)
216                         hit = 0;
217                 if (hit && value && strcmp(value, r_value) != 0)
218                         hit = 0;
219                 if (hit)
220                         break;
221                 if (use_kenv) {
222                         cp = kenvp[++i];
223                         if (cp == NULL)
224                                 break;
225                 } else {
226                         while (*cp != '\0')
227                                 cp++;
228                         cp++;
229                         if (*cp == '\0') {
230                                 cp = NULL;
231                                 break;
232                         }
233                 }
234         }
235         if (use_kenv)
236                 mtx_unlock(&kenv_lock);
237         if (cp == NULL)
238                 return ENOENT;
239
240         s = cp;
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 */
244         if (ret_name)
245                 *ret_name = s;
246         s = strchr(s, '.') + 1;         /* start of unit */
247         if (ret_namelen && ret_name)
248                 *ret_namelen = s - *ret_name - 1; /* device length */
249         if (ret_unit)
250                 *ret_unit = r_unit;
251         s = strchr(s, '.') + 1;         /* start of resname */
252         if (ret_resname)
253                 *ret_resname = s;
254         s = strchr(s, '=') + 1;         /* start of value */
255         if (ret_resnamelen && ret_resname)
256                 *ret_resnamelen = s - *ret_resname - 1; /* value len */
257         if (ret_value)
258                 *ret_value = s;
259         if (startln)                    /* line number for anchor */
260                 *startln = *line + 1;
261         return 0;
262 }
263
264 /*
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.
267  */
268 static int
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)
273 {
274         int i;
275         int un;
276
277         *line = 0;
278
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,
282             ret_value);
283         if (i == 0)
284                 return 0;
285         if (unit == NULL)
286                 return ENOENT;
287         /* If we are still here, search for wildcard matches */
288         un = -1;
289         i = res_find(line, startln, name, &un, resname, value,
290             ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen,
291             ret_value);
292         if (i == 0)
293                 return 0;
294         return ENOENT;
295 }
296
297 int
298 resource_int_value(const char *name, int unit, const char *resname, int *result)
299 {
300         int error;
301         const char *str;
302         char *op;
303         unsigned long val;
304         int line;
305
306         line = 0;
307         error = resource_find(&line, NULL, name, &unit, resname, NULL,
308             NULL, NULL, NULL, NULL, NULL, &str);
309         if (error)
310                 return error;
311         if (*str == '\0') 
312                 return EFTYPE;
313         val = strtoul(str, &op, 0);
314         if (*op != '\0') 
315                 return EFTYPE;
316         *result = val;
317         return 0;
318 }
319
320 int
321 resource_long_value(const char *name, int unit, const char *resname,
322     long *result)
323 {
324         int error;
325         const char *str;
326         char *op;
327         unsigned long val;
328         int line;
329
330         line = 0;
331         error = resource_find(&line, NULL, name, &unit, resname, NULL,
332             NULL, NULL, NULL, NULL, NULL, &str);
333         if (error)
334                 return error;
335         if (*str == '\0') 
336                 return EFTYPE;
337         val = strtoul(str, &op, 0);
338         if (*op != '\0') 
339                 return EFTYPE;
340         *result = val;
341         return 0;
342 }
343
344 int
345 resource_string_value(const char *name, int unit, const char *resname,
346     const char **result)
347 {
348         int error;
349         const char *str;
350         int line;
351
352         line = 0;
353         error = resource_find(&line, NULL, name, &unit, resname, NULL,
354             NULL, NULL, NULL, NULL, NULL, &str);
355         if (error)
356                 return error;
357         *result = str;
358         return 0;
359 }
360
361 /*
362  * This is a bit nasty, but allows us to not modify the env strings.
363  */
364 static const char *
365 resource_string_copy(const char *s, int len)
366 {
367         static char stringbuf[256];
368         static int offset = 0;
369         const char *ret;
370
371         if (len == 0)
372                 len = strlen(s);
373         if (len > 255)
374                 return NULL;
375         if ((offset + len + 1) > 255)
376                 offset = 0;
377         bcopy(s, &stringbuf[offset], len);
378         stringbuf[offset + len] = '\0';
379         ret = &stringbuf[offset];
380         offset += len + 1;
381         return ret;
382 }
383
384 /*
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.
391  */
392 int
393 resource_find_match(int *anchor, const char **name, int *unit,
394     const char *resname, const char *value)
395 {
396         const char *found_name;
397         int found_namelen;
398         int found_unit;
399         int ret;
400         int newln;
401
402         newln = *anchor;
403         ret = resource_find(anchor, &newln, NULL, NULL, resname, value,
404             &found_name, &found_namelen, &found_unit, NULL, NULL, NULL);
405         if (ret == 0) {
406                 *name = resource_string_copy(found_name, found_namelen);
407                 *unit = found_unit;
408         }
409         *anchor = newln;
410         return ret;
411 }
412
413
414 /*
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.
420  */
421 int
422 resource_find_dev(int *anchor, const char *name, int *unit,
423     const char *resname, const char *value)
424 {
425         int found_unit;
426         int newln;
427         int ret;
428
429         newln = *anchor;
430         ret = resource_find(anchor, &newln, name, NULL, resname, value,
431             NULL, NULL, &found_unit, NULL, NULL, NULL);
432         if (ret == 0) {
433                 *unit = found_unit;
434         }
435         *anchor = newln;
436         return ret;
437 }
438
439 /*
440  * Check to see if a device is disabled via a disabled hint.
441  */
442 int
443 resource_disabled(const char *name, int unit)
444 {
445         int error, value;
446
447         error = resource_int_value(name, unit, "disabled", &value);
448         if (error)
449                return (0);
450         return (value);
451 }
452
453 /*
454  * Clear a value associated with a device by removing it from
455  * the kernel environment.  This only removes a hint for an
456  * exact unit.
457  */
458 int
459 resource_unset_value(const char *name, int unit, const char *resname)
460 {
461         char varname[128];
462         const char *retname, *retvalue;
463         int error, line;
464         size_t len;
465
466         line = 0;
467         error = resource_find(&line, NULL, name, &unit, resname, NULL,
468             &retname, NULL, NULL, NULL, NULL, &retvalue);
469         if (error)
470                 return (error);
471
472         retname -= strlen("hint.");
473         len = retvalue - retname - 1;
474         if (len > sizeof(varname) - 1)
475                 return (ENAMETOOLONG);
476         memcpy(varname, retname, len);
477         varname[len] = '\0';
478         return (kern_unsetenv(varname));
479 }