* Populate the initial kernel environment. * * This is called very early in MD startup, either to provide a copy of the * environment obtained from a boot loader, or to provide an empty buffer into * which MD code can store an initial environment using kern_setenv() calls. * * kern_envp is set to the static_env generated by config(8). This implements * the env keyword described in config(5
| 252 | * a panic. |
| 253 | */ |
| 254 | void |
| 255 | init_static_kenv(char *buf, size_t len) |
| 256 | { |
| 257 | |
| 258 | KASSERT(!dynamic_kenv, ("kenv: dynamic_kenv already initialized")); |
| 259 | /* |
| 260 | * Suitably sized means it must be able to hold at least one empty |
| 261 | * variable, otherwise things go belly up if a kern_getenv call is |
| 262 | * made without a prior call to kern_setenv as we have a malformed |
| 263 | * environment. |
| 264 | */ |
| 265 | KASSERT(len == 0 || len >= 2, |
| 266 | ("kenv: static env must be initialized or suitably sized")); |
| 267 | KASSERT(len == 0 || (*buf == '\0' && *(buf + 1) == '\0'), |
| 268 | ("kenv: sized buffer must be initially empty")); |
| 269 | |
| 270 | /* |
| 271 | * We may be called twice, with the second call needed to relocate |
| 272 | * md_envp after enabling paging. md_envp is then garbage if it is |
| 273 | * not null and the relocation will move it. Discard it so as to |
| 274 | * not crash using its old value in our first call to kern_getenv(). |
| 275 | * |
| 276 | * The second call gives the same environment as the first except |
| 277 | * in silly configurations where the static env disables itself. |
| 278 | * |
| 279 | * Other env calls don't handle possibly-garbage pointers, so must |
| 280 | * not be made between enabling paging and calling here. |
| 281 | */ |
| 282 | md_envp = NULL; |
| 283 | md_env_len = 0; |
| 284 | md_env_pos = 0; |
| 285 | |
| 286 | /* |
| 287 | * Give the static environment a chance to disable the loader(8) |
| 288 | * environment first. This is done with loader_env.disabled=1. |
| 289 | * |
| 290 | * static_env and static_hints may both be disabled, but in slightly |
| 291 | * different ways. For static_env, we just don't setup kern_envp and |
| 292 | * it's as if a static env wasn't even provided. For static_hints, |
| 293 | * we effectively zero out the buffer to stop the rest of the kernel |
| 294 | * from being able to use it. |
| 295 | * |
| 296 | * We're intentionally setting this up so that static_hints.disabled may |
| 297 | * be specified in either the MD env or the static env. This keeps us |
| 298 | * consistent in our new world view. |
| 299 | * |
| 300 | * As a warning, the static environment may not be disabled in any way |
| 301 | * if the static environment has disabled the loader environment. |
| 302 | */ |
| 303 | kern_envp = static_env; |
| 304 | if (!getenv_is_true("loader_env.disabled")) { |
| 305 | md_envp = buf; |
| 306 | md_env_len = len; |
| 307 | md_env_pos = 0; |
| 308 | |
| 309 | if (getenv_is_true("static_env.disabled")) { |
| 310 | kern_envp[0] = '\0'; |
| 311 | kern_envp[1] = '\0'; |
no test coverage detected