1-- 2-- SPDX-License-Identifier: BSD-2-Clause 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 30local config = require("config") 31local hook = require("hook") 32 33local core = {} 34 35local default_acpi = false 36local default_safe_mode = false 37local default_single_user = false 38local default_verbose = false 39 40local bootenv_list = "bootenvs" 41 42local function composeLoaderCmd(cmd_name, argstr) 43 if argstr ~= nil then 44 cmd_name = cmd_name .. " " .. argstr 45 end 46 return cmd_name 47end 48 49local function recordDefaults() 50 local boot_single = loader.getenv("boot_single") or "no" 51 local boot_verbose = loader.getenv("boot_verbose") or "no" 52 53 default_acpi = core.getACPI() 54 default_single_user = boot_single:lower() ~= "no" 55 default_verbose = boot_verbose:lower() ~= "no" 56 57 core.setACPI(default_acpi) 58 core.setSingleUser(default_single_user) 59 core.setVerbose(default_verbose) 60end 61 62 63-- Globals 64-- try_include will return the loaded module on success, or false and the error 65-- message on failure. 66function try_include(module) 67 if module:sub(1, 1) ~= "/" then 68 local lua_path = loader.lua_path 69 -- XXX Temporary compat shim; this should be removed once the 70 -- loader.lua_path export has sufficiently spread. 71 if lua_path == nil then 72 lua_path = "/boot/lua" 73 end 74 module = lua_path .. "/" .. module 75 -- We only attempt to append an extension if an absolute path 76 -- wasn't specified. This assumes that the caller either wants 77 -- to treat this like it would require() and specify just the 78 -- base filename, or they know what they're doing as they've 79 -- specified an absolute path and we shouldn't impede. 80 if module:match(".lua$") == nil then 81 module = module .. ".lua" 82 end 83 end 84 if lfs.attributes(module, "mode") ~= "file" then 85 return 86 end 87 88 return dofile(module) 89end 90 91-- Module exports 92-- Commonly appearing constants 93core.KEY_BACKSPACE = 8 94core.KEY_ENTER = 13 95core.KEY_DELETE = 127 96 97-- Note that this is a decimal representation, despite the leading 0 that in 98-- other contexts (outside of Lua) may mean 'octal' 99core.KEYSTR_ESCAPE = "\027" 100core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "[" 101core.KEYSTR_RESET = core.KEYSTR_ESCAPE .. "c" 102 103core.MENU_RETURN = "return" 104core.MENU_ENTRY = "entry" 105core.MENU_SEPARATOR = "separator" 106core.MENU_SUBMENU = "submenu" 107core.MENU_CAROUSEL_ENTRY = "carousel_entry" 108 109function core.setVerbose(verbose) 110 if verbose == nil then 111 verbose = not core.verbose 112 end 113 114 if verbose then 115 loader.setenv("boot_verbose", "YES") 116 else 117 loader.unsetenv("boot_verbose") 118 end 119 core.verbose = verbose 120end 121 122function core.setSingleUser(single_user) 123 if single_user == nil then 124 single_user = not core.su 125 end 126 127 if single_user then 128 loader.setenv("boot_single", "YES") 129 else 130 loader.unsetenv("boot_single") 131 end 132 core.su = single_user 133end 134 135function core.hasACPI() 136 -- We can't trust acpi.rsdp to be set if the loader binary doesn't do 137 -- ACPI detection early enough. UEFI loader historically didn't, so 138 -- we'll fallback to assuming ACPI is enabled if this binary does not 139 -- declare that it probes for ACPI early enough 140 if loader.getenv("acpi.rsdp") ~= nil then 141 return true 142 end 143 144 return not core.hasFeature("EARLY_ACPI") 145end 146 147function core.getACPI() 148 if not core.hasACPI() then 149 return false 150 end 151 152 -- Otherwise, respect disabled if it's set 153 local c = loader.getenv("hint.acpi.0.disabled") 154 return c == nil or tonumber(c) ~= 1 155end 156 157function core.setACPI(acpi) 158 if acpi == nil then 159 acpi = not core.acpi 160 end 161 162 if acpi then 163 config.enableModule("acpi") 164 loader.setenv("hint.acpi.0.disabled", "0") 165 else 166 config.disableModule("acpi") 167 loader.setenv("hint.acpi.0.disabled", "1") 168 end 169 core.acpi = acpi 170end 171 172function core.setSafeMode(safe_mode) 173 if safe_mode == nil then 174 safe_mode = not core.sm 175 end 176 if safe_mode then 177 loader.setenv("kern.smp.disabled", "1") 178 loader.setenv("hw.ata.ata_dma", "0") 179 loader.setenv("hw.ata.atapi_dma", "0") 180 loader.setenv("kern.eventtimer.periodic", "1") 181 loader.setenv("kern.geom.part.check_integrity", "0") 182 else 183 loader.unsetenv("kern.smp.disabled") 184 loader.unsetenv("hw.ata.ata_dma") 185 loader.unsetenv("hw.ata.atapi_dma") 186 loader.unsetenv("kern.eventtimer.periodic") 187 loader.unsetenv("kern.geom.part.check_integrity") 188 end 189 core.sm = safe_mode 190end 191 192function core.clearCachedKernels() 193 -- Clear the kernel cache on config changes, autodetect might have 194 -- changed or if we've switched boot environments then we could have 195 -- a new kernel set. 196 core.cached_kernels = nil 197end 198 199function core.kernelList() 200 if core.cached_kernels ~= nil then 201 return core.cached_kernels 202 end 203 204 local k = loader.getenv("kernel") 205 local v = loader.getenv("kernels") 206 local autodetect = loader.getenv("kernels_autodetect") or "" 207 208 local kernels = {} 209 local unique = {} 210 local i = 0 211 if k ~= nil then 212 i = i + 1 213 kernels[i] = k 214 unique[k] = true 215 end 216 217 if v ~= nil then 218 for n in v:gmatch("([^;, ]+)[;, ]?") do 219 if unique[n] == nil then 220 i = i + 1 221 kernels[i] = n 222 unique[n] = true 223 end 224 end 225 end 226 227 -- Do not attempt to autodetect if underlying filesystem 228 -- do not support directory listing (e.g. tftp, http) 229 if not lfs.attributes("/boot", "mode") then 230 autodetect = "no" 231 loader.setenv("kernels_autodetect", "NO") 232 end 233 234 -- Base whether we autodetect kernels or not on a loader.conf(5) 235 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 236 -- any kernels we detect based on the criteria described. 237 if autodetect:lower() ~= "yes" then 238 core.cached_kernels = kernels 239 return core.cached_kernels 240 end 241 242 -- Automatically detect other bootable kernel directories using a 243 -- heuristic. Any directory in /boot that contains an ordinary file 244 -- named "kernel" is considered eligible. 245 for file, ftype in lfs.dir("/boot") do 246 local fname = "/boot/" .. file 247 248 if file == "." or file == ".." then 249 goto continue 250 end 251 252 if ftype then 253 if ftype ~= lfs.DT_DIR then 254 goto continue 255 end 256 elseif lfs.attributes(fname, "mode") ~= "directory" then 257 goto continue 258 end 259 260 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 261 goto continue 262 end 263 264 if unique[file] == nil then 265 i = i + 1 266 kernels[i] = file 267 unique[file] = true 268 end 269 270 ::continue:: 271 end 272 core.cached_kernels = kernels 273 return core.cached_kernels 274end 275 276function core.bootenvDefault() 277 return loader.getenv("zfs_be_active") 278end 279 280function core.bootenvList() 281 local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count")) 282 local bootenvs = {} 283 local curenv 284 local envcount = 0 285 local unique = {} 286 287 if bootenv_count == nil or bootenv_count <= 0 then 288 return bootenvs 289 end 290 291 -- Currently selected bootenv is always first/default 292 -- On the rewinded list the bootenv may not exists 293 if core.isRewinded() then 294 curenv = core.bootenvDefaultRewinded() 295 else 296 curenv = core.bootenvDefault() 297 end 298 if curenv ~= nil then 299 envcount = envcount + 1 300 bootenvs[envcount] = curenv 301 unique[curenv] = true 302 end 303 304 for curenv_idx = 0, bootenv_count - 1 do 305 curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]") 306 if curenv ~= nil and unique[curenv] == nil then 307 envcount = envcount + 1 308 bootenvs[envcount] = curenv 309 unique[curenv] = true 310 end 311 end 312 return bootenvs 313end 314 315function core.isCheckpointed() 316 return loader.getenv("zpool_checkpoint") ~= nil 317end 318 319function core.bootenvDefaultRewinded() 320 local defname = "zfs:!" .. string.sub(core.bootenvDefault(), 5) 321 local bootenv_count = tonumber("bootenvs_check_count") 322 323 if bootenv_count == nil or bootenv_count <= 0 then 324 return defname 325 end 326 327 for curenv_idx = 0, bootenv_count - 1 do 328 local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]") 329 if curenv == defname then 330 return defname 331 end 332 end 333 334 return loader.getenv("bootenvs_check[0]") 335end 336 337function core.isRewinded() 338 return bootenv_list == "bootenvs_check" 339end 340 341function core.changeRewindCheckpoint() 342 if core.isRewinded() then 343 bootenv_list = "bootenvs" 344 else 345 bootenv_list = "bootenvs_check" 346 end 347end 348 349function core.loadEntropy() 350 if core.isUEFIBoot() then 351 if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then 352 loader.perform("efi-seed-entropy") 353 end 354 end 355end 356 357function core.setDefaults() 358 core.setACPI(default_acpi) 359 core.setSafeMode(default_safe_mode) 360 core.setSingleUser(default_single_user) 361 core.setVerbose(default_verbose) 362end 363 364function core.autoboot(argstr) 365 -- loadelf() only if we've not already loaded a kernel 366 if loader.getenv("kernelname") == nil then 367 config.loadelf() 368 end 369 core.loadEntropy() 370 loader.perform(composeLoaderCmd("autoboot", argstr)) 371end 372 373function core.boot(argstr) 374 -- loadelf() only if we've not already loaded a kernel 375 if loader.getenv("kernelname") == nil then 376 config.loadelf() 377 end 378 core.loadEntropy() 379 loader.perform(composeLoaderCmd("boot", argstr)) 380end 381 382function core.hasFeature(name) 383 if not loader.has_feature then 384 -- Loader too old, no feature support 385 return nil, "No feature support in loaded loader" 386 end 387 388 return loader.has_feature(name) 389end 390 391function core.isSingleUserBoot() 392 local single_user = loader.getenv("boot_single") 393 return single_user ~= nil and single_user:lower() == "yes" 394end 395 396function core.isUEFIBoot() 397 local efiver = loader.getenv("efi-version") 398 399 return efiver ~= nil 400end 401 402function core.isZFSBoot() 403 local c = loader.getenv("currdev") 404 405 if c ~= nil then 406 return c:match("^zfs:") ~= nil 407 end 408 return false 409end 410 411function core.isFramebufferConsole() 412 local c = loader.getenv("console") 413 if c ~= nil then 414 if c:find("efi") == nil and c:find("vidconsole") == nil then 415 return false 416 end 417 if loader.getenv("screen.depth") ~= nil then 418 return true 419 end 420 end 421 return false 422end 423 424function core.isSerialConsole() 425 local c = loader.getenv("console") 426 if c ~= nil then 427 -- serial console is comconsole, but also userboot. 428 -- userboot is there, because we have no way to know 429 -- if the user terminal can draw unicode box chars or not. 430 if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then 431 return true 432 end 433 end 434 return false 435end 436 437function core.isSerialBoot() 438 local s = loader.getenv("boot_serial") 439 if s ~= nil then 440 return true 441 end 442 443 local m = loader.getenv("boot_multicons") 444 if m ~= nil then 445 return true 446 end 447 return false 448end 449 450-- Is the menu skipped in the environment in which we've booted? 451function core.isMenuSkipped() 452 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 453end 454 455-- This may be a better candidate for a 'utility' module. 456function core.deepCopyTable(tbl) 457 local new_tbl = {} 458 for k, v in pairs(tbl) do 459 if type(v) == "table" then 460 new_tbl[k] = core.deepCopyTable(v) 461 else 462 new_tbl[k] = v 463 end 464 end 465 return new_tbl 466end 467 468-- XXX This should go away if we get the table lib into shape for importing. 469-- As of now, it requires some 'os' functions, so we'll implement this in lua 470-- for our uses 471function core.popFrontTable(tbl) 472 -- Shouldn't reasonably happen 473 if #tbl == 0 then 474 return nil, nil 475 elseif #tbl == 1 then 476 return tbl[1], {} 477 end 478 479 local first_value = tbl[1] 480 local new_tbl = {} 481 -- This is not a cheap operation 482 for k, v in ipairs(tbl) do 483 if k > 1 then 484 new_tbl[k - 1] = v 485 end 486 end 487 488 return first_value, new_tbl 489end 490 491function core.getConsoleName() 492 if loader.getenv("boot_multicons") ~= nil then 493 if loader.getenv("boot_serial") ~= nil then 494 return "Dual (Serial primary)" 495 else 496 return "Dual (Video primary)" 497 end 498 else 499 if loader.getenv("boot_serial") ~= nil then 500 return "Serial" 501 else 502 return "Video" 503 end 504 end 505end 506 507function core.nextConsoleChoice() 508 if loader.getenv("boot_multicons") ~= nil then 509 if loader.getenv("boot_serial") ~= nil then 510 loader.unsetenv("boot_serial") 511 else 512 loader.unsetenv("boot_multicons") 513 loader.setenv("boot_serial", "YES") 514 end 515 else 516 if loader.getenv("boot_serial") ~= nil then 517 loader.unsetenv("boot_serial") 518 else 519 loader.setenv("boot_multicons", "YES") 520 loader.setenv("boot_serial", "YES") 521 end 522 end 523end 524 525recordDefaults() 526hook.register("config.reloaded", core.clearCachedKernels) 527return core 528