1-- 2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3-- 4-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 5-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> 6-- All rights reserved. 7-- 8-- Redistribution and use in source and binary forms, with or without 9-- modification, are permitted provided that the following conditions 10-- are met: 11-- 1. Redistributions of source code must retain the above copyright 12-- notice, this list of conditions and the following disclaimer. 13-- 2. Redistributions in binary form must reproduce the above copyright 14-- notice, this list of conditions and the following disclaimer in the 15-- documentation and/or other materials provided with the distribution. 16-- 17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27-- SUCH DAMAGE. 28-- 29-- $FreeBSD: stable/12/stand/lua/core.lua 370540 2021-09-12 05:43:51Z git2svn $ 30-- 31 32local config = require("config") 33local hook = require("hook") 34 35local core = {} 36 37local default_safe_mode = false 38local default_single_user = false 39local default_verbose = false 40 41local function composeLoaderCmd(cmd_name, argstr) 42 if argstr ~= nil then 43 cmd_name = cmd_name .. " " .. argstr 44 end 45 return cmd_name 46end 47 48local function recordDefaults() 49 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, 50 -- it will generally be set upon execution of the kernel. Because of 51 -- this, we can't (or don't really want to) detect/disable ACPI on !i386 52 -- reliably. Just set it enabled if we detect it and leave well enough 53 -- alone if we don't. 54 local boot_acpi = core.isSystem386() and core.getACPIPresent(false) 55 local boot_single = loader.getenv("boot_single") or "no" 56 local boot_verbose = loader.getenv("boot_verbose") or "no" 57 default_single_user = boot_single:lower() ~= "no" 58 default_verbose = boot_verbose:lower() ~= "no" 59 60 if boot_acpi then 61 core.setACPI(true) 62 end 63 core.setSingleUser(default_single_user) 64 core.setVerbose(default_verbose) 65end 66 67 68-- Globals 69-- try_include will return the loaded module on success, or false and the error 70-- message on failure. 71function try_include(module) 72 if module:sub(1, 1) ~= "/" then 73 local lua_path = loader.lua_path 74 -- XXX Temporary compat shim; this should be removed once the 75 -- loader.lua_path export has sufficiently spread. 76 if lua_path == nil then 77 lua_path = "/boot/lua" 78 end 79 module = lua_path .. "/" .. module 80 -- We only attempt to append an extension if an absolute path 81 -- wasn't specified. This assumes that the caller either wants 82 -- to treat this like it would require() and specify just the 83 -- base filename, or they know what they're doing as they've 84 -- specified an absolute path and we shouldn't impede. 85 if module:match(".lua$") == nil then 86 module = module .. ".lua" 87 end 88 end 89 if lfs.attributes(module, "mode") ~= "file" then 90 return 91 end 92 93 return dofile(module) 94end 95 96-- Module exports 97-- Commonly appearing constants 98core.KEY_BACKSPACE = 8 99core.KEY_ENTER = 13 100core.KEY_DELETE = 127 101 102-- Note that this is a decimal representation, despite the leading 0 that in 103-- other contexts (outside of Lua) may mean 'octal' 104core.KEYSTR_ESCAPE = "\027" 105core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "[" 106core.KEYSTR_RESET = core.KEYSTR_ESCAPE .. "c" 107 108core.MENU_RETURN = "return" 109core.MENU_ENTRY = "entry" 110core.MENU_SEPARATOR = "separator" 111core.MENU_SUBMENU = "submenu" 112core.MENU_CAROUSEL_ENTRY = "carousel_entry" 113 114function core.setVerbose(verbose) 115 if verbose == nil then 116 verbose = not core.verbose 117 end 118 119 if verbose then 120 loader.setenv("boot_verbose", "YES") 121 else 122 loader.unsetenv("boot_verbose") 123 end 124 core.verbose = verbose 125end 126 127function core.setSingleUser(single_user) 128 if single_user == nil then 129 single_user = not core.su 130 end 131 132 if single_user then 133 loader.setenv("boot_single", "YES") 134 else 135 loader.unsetenv("boot_single") 136 end 137 core.su = single_user 138end 139 140function core.getACPIPresent(checking_system_defaults) 141 local c = loader.getenv("hint.acpi.0.rsdp") 142 143 if c ~= nil then 144 if checking_system_defaults then 145 return true 146 end 147 -- Otherwise, respect disabled if it's set 148 c = loader.getenv("hint.acpi.0.disabled") 149 return c == nil or tonumber(c) ~= 1 150 end 151 return false 152end 153 154function core.setACPI(acpi) 155 if acpi == nil then 156 acpi = not core.acpi 157 end 158 159 if acpi then 160 loader.setenv("acpi_load", "YES") 161 loader.setenv("hint.acpi.0.disabled", "0") 162 loader.unsetenv("loader.acpi_disabled_by_user") 163 else 164 loader.unsetenv("acpi_load") 165 loader.setenv("hint.acpi.0.disabled", "1") 166 loader.setenv("loader.acpi_disabled_by_user", "1") 167 end 168 core.acpi = acpi 169end 170 171function core.setSafeMode(safe_mode) 172 if safe_mode == nil then 173 safe_mode = not core.sm 174 end 175 if safe_mode then 176 loader.setenv("kern.smp.disabled", "1") 177 loader.setenv("hw.ata.ata_dma", "0") 178 loader.setenv("hw.ata.atapi_dma", "0") 179 loader.setenv("hw.ata.wc", "0") 180 loader.setenv("hw.eisa_slots", "0") 181 loader.setenv("kern.eventtimer.periodic", "1") 182 loader.setenv("kern.geom.part.check_integrity", "0") 183 else 184 loader.unsetenv("kern.smp.disabled") 185 loader.unsetenv("hw.ata.ata_dma") 186 loader.unsetenv("hw.ata.atapi_dma") 187 loader.unsetenv("hw.ata.wc") 188 loader.unsetenv("hw.eisa_slots") 189 loader.unsetenv("kern.eventtimer.periodic") 190 loader.unsetenv("kern.geom.part.check_integrity") 191 end 192 core.sm = safe_mode 193end 194 195function core.clearCachedKernels() 196 -- Clear the kernel cache on config changes, autodetect might have 197 -- changed or if we've switched boot environments then we could have 198 -- a new kernel set. 199 core.cached_kernels = nil 200end 201 202function core.kernelList() 203 if core.cached_kernels ~= nil then 204 return core.cached_kernels 205 end 206 207 local k = loader.getenv("kernel") 208 local v = loader.getenv("kernels") 209 local autodetect = loader.getenv("kernels_autodetect") or "" 210 211 local kernels = {} 212 local unique = {} 213 local i = 0 214 if k ~= nil then 215 i = i + 1 216 kernels[i] = k 217 unique[k] = true 218 end 219 220 if v ~= nil then 221 for n in v:gmatch("([^;, ]+)[;, ]?") do 222 if unique[n] == nil then 223 i = i + 1 224 kernels[i] = n 225 unique[n] = true 226 end 227 end 228 end 229 230 -- Base whether we autodetect kernels or not on a loader.conf(5) 231 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 232 -- any kernels we detect based on the criteria described. 233 if autodetect:lower() ~= "yes" then 234 core.cached_kernels = kernels 235 return core.cached_kernels 236 end 237 238 -- Automatically detect other bootable kernel directories using a 239 -- heuristic. Any directory in /boot that contains an ordinary file 240 -- named "kernel" is considered eligible. 241 for file, ftype in lfs.dir("/boot") do 242 local fname = "/boot/" .. file 243 244 if file == "." or file == ".." then 245 goto continue 246 end 247 248 if ftype then 249 if ftype ~= lfs.DT_DIR then 250 goto continue 251 end 252 elseif lfs.attributes(fname, "mode") ~= "directory" then 253 goto continue 254 end 255 256 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 257 goto continue 258 end 259 260 if unique[file] == nil then 261 i = i + 1 262 kernels[i] = file 263 unique[file] = true 264 end 265 266 ::continue:: 267 end 268 core.cached_kernels = kernels 269 return core.cached_kernels 270end 271 272function core.bootenvDefault() 273 return loader.getenv("zfs_be_active") 274end 275 276function core.bootenvList() 277 local bootenv_count = tonumber(loader.getenv("bootenvs_count")) 278 local bootenvs = {} 279 local curenv 280 local envcount = 0 281 local unique = {} 282 283 if bootenv_count == nil or bootenv_count <= 0 then 284 return bootenvs 285 end 286 287 -- Currently selected bootenv is always first/default 288 curenv = core.bootenvDefault() 289 if curenv ~= nil then 290 envcount = envcount + 1 291 bootenvs[envcount] = curenv 292 unique[curenv] = true 293 end 294 295 for curenv_idx = 0, bootenv_count - 1 do 296 curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]") 297 if curenv ~= nil and unique[curenv] == nil then 298 envcount = envcount + 1 299 bootenvs[envcount] = curenv 300 unique[curenv] = true 301 end 302 end 303 return bootenvs 304end 305 306function core.setDefaults() 307 core.setACPI(core.getACPIPresent(true)) 308 core.setSafeMode(default_safe_mode) 309 core.setSingleUser(default_single_user) 310 core.setVerbose(default_verbose) 311end 312 313function core.autoboot(argstr) 314 -- loadelf() only if we've not already loaded a kernel 315 if loader.getenv("kernelname") == nil then 316 config.loadelf() 317 end 318 loader.perform(composeLoaderCmd("autoboot", argstr)) 319end 320 321function core.boot(argstr) 322 -- loadelf() only if we've not already loaded a kernel 323 if loader.getenv("kernelname") == nil then 324 config.loadelf() 325 end 326 loader.perform(composeLoaderCmd("boot", argstr)) 327end 328 329function core.isSingleUserBoot() 330 local single_user = loader.getenv("boot_single") 331 return single_user ~= nil and single_user:lower() == "yes" 332end 333 334function core.isUEFIBoot() 335 local efiver = loader.getenv("efi-version") 336 337 return efiver ~= nil 338end 339 340function core.isZFSBoot() 341 local c = loader.getenv("currdev") 342 343 if c ~= nil then 344 return c:match("^zfs:") ~= nil 345 end 346 return false 347end 348 349function core.isSerialConsole() 350 local c = loader.getenv("console") 351 if c ~= nil then 352 -- serial console is comconsole, but also userboot. 353 -- userboot is there, because we have no way to know 354 -- if the user terminal can draw unicode box chars or not. 355 if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then 356 return true 357 end 358 end 359 return false 360end 361 362function core.isSerialBoot() 363 local s = loader.getenv("boot_serial") 364 if s ~= nil then 365 return true 366 end 367 368 local m = loader.getenv("boot_multicons") 369 if m ~= nil then 370 return true 371 end 372 return false 373end 374 375function core.isSystem386() 376 return loader.machine_arch == "i386" 377end 378 379-- Is the menu skipped in the environment in which we've booted? 380function core.isMenuSkipped() 381 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 382end 383 384-- This may be a better candidate for a 'utility' module. 385function core.deepCopyTable(tbl) 386 local new_tbl = {} 387 for k, v in pairs(tbl) do 388 if type(v) == "table" then 389 new_tbl[k] = core.deepCopyTable(v) 390 else 391 new_tbl[k] = v 392 end 393 end 394 return new_tbl 395end 396 397-- XXX This should go away if we get the table lib into shape for importing. 398-- As of now, it requires some 'os' functions, so we'll implement this in lua 399-- for our uses 400function core.popFrontTable(tbl) 401 -- Shouldn't reasonably happen 402 if #tbl == 0 then 403 return nil, nil 404 elseif #tbl == 1 then 405 return tbl[1], {} 406 end 407 408 local first_value = tbl[1] 409 local new_tbl = {} 410 -- This is not a cheap operation 411 for k, v in ipairs(tbl) do 412 if k > 1 then 413 new_tbl[k - 1] = v 414 end 415 end 416 417 return first_value, new_tbl 418end 419 420function core.getConsoleName() 421 if loader.getenv("boot_multicons") ~= nil then 422 if loader.getenv("boot_serial") ~= nil then 423 return "Dual (Serial primary)" 424 else 425 return "Dual (Video primary)" 426 end 427 else 428 if loader.getenv("boot_serial") ~= nil then 429 return "Serial" 430 else 431 return "Video" 432 end 433 end 434end 435 436function core.nextConsoleChoice() 437 if loader.getenv("boot_multicons") ~= nil then 438 if loader.getenv("boot_serial") ~= nil then 439 loader.unsetenv("boot_serial") 440 else 441 loader.unsetenv("boot_multicons") 442 loader.setenv("boot_serial", "YES") 443 end 444 else 445 if loader.getenv("boot_serial") ~= nil then 446 loader.unsetenv("boot_serial") 447 else 448 loader.setenv("boot_multicons", "YES") 449 loader.setenv("boot_serial", "YES") 450 end 451 end 452end 453 454recordDefaults() 455hook.register("config.reloaded", core.clearCachedKernels) 456return core 457