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/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/mutex.h> 37 #include <sys/sysctl.h> 38 #include <sys/systm.h> 39 #include <sys/bus.h> 40 41 #define FBACK_MDENV 0 /* MD env (e.g. loader.conf) */ 42 #define FBACK_STENV 1 /* Static env */ 43 #define FBACK_STATIC 2 /* static_hints */ 44 45 /* 46 * We'll use hintenv_merged to indicate that the dynamic environment has been 47 * properly prepared for hint usage. This implies that the dynamic environment 48 * has already been setup (dynamic_kenv) and that we have added any supplied 49 * static_hints to the dynamic environment. 50 */ 51 static int hintenv_merged; 52 53 /* 54 * Access functions for device resources. 55 */ 56 57 static void 58 static_hints_to_env(void *data __unused) 59 { 60 const char *cp; 61 char *line, *eq; 62 int eqidx, i; 63 64 cp = static_hints; 65 while (cp && *cp != '\0') { 66 eq = strchr(cp, '='); 67 if (eq == NULL) 68 /* Bad hint value */ 69 continue; 70 eqidx = eq - cp; 71 72 i = strlen(cp); 73 line = malloc(i + 1, M_TEMP, M_WAITOK); 74 strcpy(line, cp); 75 line[eqidx] = line[i] = '\0'; 76 /* 77 * Before adding a hint to the dynamic environment, check if 78 * another value for said hint has already been added. This is 79 * needed because static environment overrides static hints and 80 * dynamic environment overrides all. 81 */ 82 if (testenv(line) == 0) 83 kern_setenv(line, line + eqidx + 1); 84 free(line, M_TEMP); 85 cp += i + 1; 86 } 87 hintenv_merged = 1; 88 } 89 90 /* Any time after dynamic env is setup */ 91 SYSINIT(hintenv, SI_SUB_KMEM + 1, SI_ORDER_SECOND, static_hints_to_env, NULL); 92 93 /* 94 * Checks the environment to see if we even have any hints. If it has no hints, 95 * then res_find can take the hint that there's no point in searching it and 96 * either move on to the next environment or fail early. 97 */ 98 static bool 99 _res_checkenv(char *envp) 100 { 101 char *cp; 102 103 cp = envp; 104 while (cp) { 105 if (strncmp(cp, "hint.", 5) == 0) 106 return (true); 107 while (*cp != '\0') 108 cp++; 109 cp++; 110 if (*cp == '\0') 111 break; 112 } 113 return (false); 114 } 115 116 /* 117 * Evil wildcarding resource string lookup. 118 * This walks the supplied env string table and returns a match. 119 * The start point can be remembered for incremental searches. 120 */ 121 static int 122 res_find(char **hintp_cookie, int *line, int *startln, 123 const char *name, int *unit, const char *resname, const char *value, 124 const char **ret_name, int *ret_namelen, int *ret_unit, 125 const char **ret_resname, int *ret_resnamelen, const char **ret_value) 126 { 127 int dyn_used = 0, fbacklvl = FBACK_MDENV, hit, i = 0, n = 0; 128 char r_name[32]; 129 int r_unit; 130 char r_resname[32]; 131 char r_value[128]; 132 const char *s, *cp; 133 char *hintp, *p; 134 135 136 /* 137 * We are expecting that the caller will pass us a hintp_cookie that 138 * they are tracking. Upon entry, if *hintp_cookie is *not* set, this 139 * indicates to us that we should be figuring out based on the current 140 * environment where to search. This keeps us sane throughout the 141 * entirety of a single search. 142 */ 143 if (*hintp_cookie == NULL) { 144 hintp = NULL; 145 if (hintenv_merged) { 146 /* 147 * static_hints, if it was previously used, has 148 * already been folded in to the environment 149 * by this point. 150 */ 151 mtx_lock(&kenv_lock); 152 cp = kenvp[0]; 153 for (i = 0; cp != NULL; cp = kenvp[++i]) { 154 if (!strncmp(cp, "hint.", 5)) { 155 hintp = kenvp[0]; 156 break; 157 } 158 } 159 mtx_unlock(&kenv_lock); 160 dyn_used = 1; 161 } else { 162 /* 163 * We'll have a chance to keep coming back here until 164 * we've actually exhausted all of our possibilities. 165 * We might have chosen the MD/Static env because it 166 * had some kind of hints, but perhaps it didn't have 167 * the hint we are looking for. We don't provide any 168 * fallback when searching the dynamic environment. 169 */ 170 fallback: 171 if (dyn_used || fbacklvl >= FBACK_STATIC) 172 return (ENOENT); 173 174 if (fbacklvl <= FBACK_MDENV && 175 _res_checkenv(md_envp)) { 176 hintp = md_envp; 177 goto found; 178 } 179 fbacklvl++; 180 181 if (fbacklvl <= FBACK_STENV && 182 _res_checkenv(kern_envp)) { 183 hintp = kern_envp; 184 goto found; 185 } 186 fbacklvl++; 187 188 /* We'll fallback to static_hints if needed/can */ 189 if (fbacklvl <= FBACK_STATIC && 190 _res_checkenv(static_hints)) 191 hintp = static_hints; 192 found: 193 fbacklvl++; 194 } 195 196 if (hintp == NULL) 197 return (ENOENT); 198 *hintp_cookie = hintp; 199 } else { 200 hintp = *hintp_cookie; 201 if (hintenv_merged && hintp == kenvp[0]) 202 dyn_used = 1; 203 else 204 /* 205 * If we aren't using the dynamic environment, we need 206 * to run through the proper fallback procedure again. 207 * This is so that we do continuations right if we're 208 * working with *line and *startln. 209 */ 210 goto fallback; 211 } 212 213 if (dyn_used) { 214 mtx_lock(&kenv_lock); 215 i = 0; 216 } 217 218 cp = hintp; 219 while (cp) { 220 hit = 1; 221 (*line)++; 222 if (strncmp(cp, "hint.", 5) != 0) 223 hit = 0; 224 else 225 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s", 226 r_name, &r_unit, r_resname, r_value); 227 /* We'll circumvent all of the checks if we already know */ 228 if (hit) { 229 if (n != 4) { 230 printf("CONFIG: invalid hint '%s'\n", cp); 231 p = strchr(cp, 'h'); 232 *p = 'H'; 233 hit = 0; 234 } 235 if (hit && startln && *startln >= 0 && *line < *startln) 236 hit = 0; 237 if (hit && name && strcmp(name, r_name) != 0) 238 hit = 0; 239 if (hit && unit && *unit != r_unit) 240 hit = 0; 241 if (hit && resname && strcmp(resname, r_resname) != 0) 242 hit = 0; 243 if (hit && value && strcmp(value, r_value) != 0) 244 hit = 0; 245 if (hit) 246 break; 247 } 248 if (dyn_used) { 249 cp = kenvp[++i]; 250 if (cp == NULL) 251 break; 252 } else { 253 while (*cp != '\0') 254 cp++; 255 cp++; 256 if (*cp == '\0') { 257 cp = NULL; 258 break; 259 } 260 } 261 } 262 if (dyn_used) 263 mtx_unlock(&kenv_lock); 264 if (cp == NULL) 265 goto fallback; 266 267 s = cp; 268 /* This is a bit of a hack, but at least is reentrant */ 269 /* Note that it returns some !unterminated! strings. */ 270 s = strchr(s, '.') + 1; /* start of device */ 271 if (ret_name) 272 *ret_name = s; 273 s = strchr(s, '.') + 1; /* start of unit */ 274 if (ret_namelen && ret_name) 275 *ret_namelen = s - *ret_name - 1; /* device length */ 276 if (ret_unit) 277 *ret_unit = r_unit; 278 s = strchr(s, '.') + 1; /* start of resname */ 279 if (ret_resname) 280 *ret_resname = s; 281 s = strchr(s, '=') + 1; /* start of value */ 282 if (ret_resnamelen && ret_resname) 283 *ret_resnamelen = s - *ret_resname - 1; /* value len */ 284 if (ret_value) 285 *ret_value = s; 286 if (startln) /* line number for anchor */ 287 *startln = *line + 1; 288 return 0; 289 } 290 291 /* 292 * Search all the data sources for matches to our query. We look for 293 * dynamic hints first as overrides for static or fallback hints. 294 */ 295 static int 296 resource_find(int *line, int *startln, 297 const char *name, int *unit, const char *resname, const char *value, 298 const char **ret_name, int *ret_namelen, int *ret_unit, 299 const char **ret_resname, int *ret_resnamelen, const char **ret_value) 300 { 301 int i; 302 int un; 303 char *hintp; 304 305 *line = 0; 306 hintp = NULL; 307 308 /* Search for exact unit matches first */ 309 i = res_find(&hintp, line, startln, name, unit, resname, value, 310 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen, 311 ret_value); 312 if (i == 0) 313 return 0; 314 if (unit == NULL) 315 return ENOENT; 316 /* If we are still here, search for wildcard matches */ 317 un = -1; 318 i = res_find(&hintp, line, startln, name, &un, resname, value, 319 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen, 320 ret_value); 321 if (i == 0) 322 return 0; 323 return ENOENT; 324 } 325 326 int 327 resource_int_value(const char *name, int unit, const char *resname, int *result) 328 { 329 int error; 330 const char *str; 331 char *op; 332 unsigned long val; 333 int line; 334 335 line = 0; 336 error = resource_find(&line, NULL, name, &unit, resname, NULL, 337 NULL, NULL, NULL, NULL, NULL, &str); 338 if (error) 339 return error; 340 if (*str == '\0') 341 return EFTYPE; 342 val = strtoul(str, &op, 0); 343 if (*op != '\0') 344 return EFTYPE; 345 *result = val; 346 return 0; 347 } 348 349 int 350 resource_long_value(const char *name, int unit, const char *resname, 351 long *result) 352 { 353 int error; 354 const char *str; 355 char *op; 356 unsigned long val; 357 int line; 358 359 line = 0; 360 error = resource_find(&line, NULL, name, &unit, resname, NULL, 361 NULL, NULL, NULL, NULL, NULL, &str); 362 if (error) 363 return error; 364 if (*str == '\0') 365 return EFTYPE; 366 val = strtoul(str, &op, 0); 367 if (*op != '\0') 368 return EFTYPE; 369 *result = val; 370 return 0; 371 } 372 373 int 374 resource_string_value(const char *name, int unit, const char *resname, 375 const char **result) 376 { 377 int error; 378 const char *str; 379 int line; 380 381 line = 0; 382 error = resource_find(&line, NULL, name, &unit, resname, NULL, 383 NULL, NULL, NULL, NULL, NULL, &str); 384 if (error) 385 return error; 386 *result = str; 387 return 0; 388 } 389 390 /* 391 * This is a bit nasty, but allows us to not modify the env strings. 392 */ 393 static const char * 394 resource_string_copy(const char *s, int len) 395 { 396 static char stringbuf[256]; 397 static int offset = 0; 398 const char *ret; 399 400 if (len == 0) 401 len = strlen(s); 402 if (len > 255) 403 return NULL; 404 if ((offset + len + 1) > 255) 405 offset = 0; 406 bcopy(s, &stringbuf[offset], len); 407 stringbuf[offset + len] = '\0'; 408 ret = &stringbuf[offset]; 409 offset += len + 1; 410 return ret; 411 } 412 413 /* 414 * err = resource_find_match(&anchor, &name, &unit, resname, value) 415 * Iteratively fetch a list of devices wired "at" something 416 * res and value are restrictions. eg: "at", "scbus0". 417 * For practical purposes, res = required, value = optional. 418 * *name and *unit are set. 419 * set *anchor to zero before starting. 420 */ 421 int 422 resource_find_match(int *anchor, const char **name, int *unit, 423 const char *resname, const char *value) 424 { 425 const char *found_name; 426 int found_namelen; 427 int found_unit; 428 int ret; 429 int newln; 430 431 newln = *anchor; 432 ret = resource_find(anchor, &newln, NULL, NULL, resname, value, 433 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL); 434 if (ret == 0) { 435 *name = resource_string_copy(found_name, found_namelen); 436 *unit = found_unit; 437 } 438 *anchor = newln; 439 return ret; 440 } 441 442 443 /* 444 * err = resource_find_dev(&anchor, name, &unit, res, value); 445 * Iterate through a list of devices, returning their unit numbers. 446 * res and value are optional restrictions. eg: "at", "scbus0". 447 * *unit is set to the value. 448 * set *anchor to zero before starting. 449 */ 450 int 451 resource_find_dev(int *anchor, const char *name, int *unit, 452 const char *resname, const char *value) 453 { 454 int found_unit; 455 int newln; 456 int ret; 457 458 newln = *anchor; 459 ret = resource_find(anchor, &newln, name, NULL, resname, value, 460 NULL, NULL, &found_unit, NULL, NULL, NULL); 461 if (ret == 0) { 462 *unit = found_unit; 463 } 464 *anchor = newln; 465 return ret; 466 } 467 468 /* 469 * Check to see if a device is disabled via a disabled hint. 470 */ 471 int 472 resource_disabled(const char *name, int unit) 473 { 474 int error, value; 475 476 error = resource_int_value(name, unit, "disabled", &value); 477 if (error) 478 return (0); 479 return (value); 480 } 481 482 /* 483 * Clear a value associated with a device by removing it from 484 * the kernel environment. This only removes a hint for an 485 * exact unit. 486 */ 487 int 488 resource_unset_value(const char *name, int unit, const char *resname) 489 { 490 char varname[128]; 491 const char *retname, *retvalue; 492 int error, line; 493 size_t len; 494 495 line = 0; 496 error = resource_find(&line, NULL, name, &unit, resname, NULL, 497 &retname, NULL, NULL, NULL, NULL, &retvalue); 498 if (error) 499 return (error); 500 501 retname -= strlen("hint."); 502 len = retvalue - retname - 1; 503 if (len > sizeof(varname) - 1) 504 return (ENAMETOOLONG); 505 memcpy(varname, retname, len); 506 varname[len] = '\0'; 507 return (kern_unsetenv(varname)); 508 } 509