1-- 2-- SPDX-License-Identifier: BSD-2-Clause 3-- 4-- Copyright (c) 2015 Pedro Souza <[email protected]> 5-- Copyright (c) 2018 Kyle Evans <[email protected]> 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_safe_mode = false 36local default_single_user = false 37local default_verbose = false 38 39local bootenv_list = "bootenvs" 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 default_kernel = 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 215 if default_kernel then 216 i = i + 1 217 kernels[i] = default_kernel 218 unique[default_kernel] = true 219 end 220 221 if v ~= nil then 222 for n in v:gmatch("([^;, ]+)[;, ]?") do 223 if unique[n] == nil then 224 i = i + 1 225 kernels[i] = n 226 unique[n] = true 227 end 228 end 229 end 230 231 -- Do not attempt to autodetect if underlying filesystem 232 -- do not support directory listing (e.g. tftp, http) 233 if not lfs.attributes("/boot", "mode") then 234 autodetect = "no" 235 loader.setenv("kernels_autodetect", "NO") 236 end 237 238 -- Base whether we autodetect kernels or not on a loader.conf(5) 239 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 240 -- any kernels we detect based on the criteria described. 241 if autodetect:lower() ~= "yes" then 242 core.cached_kernels = kernels 243 return core.cached_kernels 244 end 245 246 local present = {} 247 248 -- Automatically detect other bootable kernel directories using a 249 -- heuristic. Any directory in /boot that contains an ordinary file 250 -- named "kernel" is considered eligible. 251 for file, ftype in lfs.dir("/boot") do 252 local fname = "/boot/" .. file 253 254 if file == "." or file == ".." then 255 goto continue 256 end 257 258 if ftype then 259 if ftype ~= lfs.DT_DIR then 260 goto continue 261 end 262 elseif lfs.attributes(fname, "mode") ~= "directory" then 263 goto continue 264 end 265 266 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 267 goto continue 268 end 269 270 if unique[file] == nil then 271 i = i + 1 272 kernels[i] = file 273 unique[file] = true 274 end 275 276 present[file] = true 277 278 ::continue:: 279 end 280 281 -- If we found more than one kernel, prune the "kernel" specified kernel 282 -- off of the list if it wasn't found during traversal. If we didn't 283 -- actually find any kernels, we just assume that they know what they're 284 -- doing and leave it alone. 285 if default_kernel and not present[default_kernel] and #kernels > 1 then 286 for i = 1, #kernels do 287 if i == #kernels then 288 kernels[i] = nil 289 else 290 kernels[i] = kernels[i + 1] 291 end 292 end 293 end 294 295 core.cached_kernels = kernels 296 return core.cached_kernels 297end 298 299function core.bootenvDefault() 300 return loader.getenv("zfs_be_active") 301end 302 303function core.bootenvList() 304 local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count")) 305 local bootenvs = {} 306 local curenv 307 local envcount = 0 308 local unique = {} 309 310 if bootenv_count == nil or bootenv_count <= 0 then 311 return bootenvs 312 end 313 314 -- Currently selected bootenv is always first/default 315 -- On the rewinded list the bootenv may not exists 316 if core.isRewinded() then 317 curenv = core.bootenvDefaultRewinded() 318 else 319 curenv = core.bootenvDefault() 320 end 321 if curenv ~= nil then 322 envcount = envcount + 1 323 bootenvs[envcount] = curenv 324 unique[curenv] = true 325 end 326 327 for curenv_idx = 0, bootenv_count - 1 do 328 curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]") 329 if curenv ~= nil and unique[curenv] == nil then 330 envcount = envcount + 1 331 bootenvs[envcount] = curenv 332 unique[curenv] = true 333 end 334 end 335 return bootenvs 336end 337 338function core.isCheckpointed() 339 return loader.getenv("zpool_checkpoint") ~= nil 340end 341 342function core.bootenvDefaultRewinded() 343 local defname = "zfs:!" .. string.sub(core.bootenvDefault(), 5) 344 local bootenv_count = tonumber("bootenvs_check_count") 345 346 if bootenv_count == nil or bootenv_count <= 0 then 347 return defname 348 end 349 350 for curenv_idx = 0, bootenv_count - 1 do 351 local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]") 352 if curenv == defname then 353 return defname 354 end 355 end 356 357 return loader.getenv("bootenvs_check[0]") 358end 359 360function core.isRewinded() 361 return bootenv_list == "bootenvs_check" 362end 363 364function core.changeRewindCheckpoint() 365 if core.isRewinded() then 366 bootenv_list = "bootenvs" 367 else 368 bootenv_list = "bootenvs_check" 369 end 370end 371 372function core.loadEntropy() 373 if core.isUEFIBoot() then 374 if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then 375 loader.perform("efi-seed-entropy") 376 end 377 end 378end 379 380function core.setDefaults() 381 core.setACPI(core.getACPIPresent(true)) 382 core.setSafeMode(default_safe_mode) 383 core.setSingleUser(default_single_user) 384 core.setVerbose(default_verbose) 385end 386 387function core.autoboot(argstr) 388 -- loadelf() only if we've not already loaded a kernel 389 if loader.getenv("kernelname") == nil then 390 config.loadelf() 391 end 392 core.loadEntropy() 393 loader.perform(composeLoaderCmd("autoboot", argstr)) 394end 395 396function core.boot(argstr) 397 -- loadelf() only if we've not already loaded a kernel 398 if loader.getenv("kernelname") == nil then 399 config.loadelf() 400 end 401 core.loadEntropy() 402 loader.perform(composeLoaderCmd("boot", argstr)) 403end 404 405function core.hasFeature(name) 406 if not loader.has_feature then 407 -- Loader too old, no feature support 408 return nil, "No feature support in loaded loader" 409 end 410 411 return loader.has_feature(name) 412end 413 414function core.isSingleUserBoot() 415 local single_user = loader.getenv("boot_single") 416 return single_user ~= nil and single_user:lower() == "yes" 417end 418 419function core.isUEFIBoot() 420 local efiver = loader.getenv("efi-version") 421 422 return efiver ~= nil 423end 424 425function core.isZFSBoot() 426 local c = loader.getenv("currdev") 427 428 if c ~= nil then 429 return c:match("^zfs:") ~= nil 430 end 431 return false 432end 433 434function core.isFramebufferConsole() 435 local c = loader.getenv("console") 436 if c ~= nil then 437 if c:find("efi") == nil and c:find("vidconsole") == nil then 438 return false 439 end 440 if loader.getenv("screen.depth") ~= nil then 441 return true 442 end 443 end 444 return false 445end 446 447function core.isSerialConsole() 448 local c = loader.getenv("console") 449 if c ~= nil then 450 -- serial console is comconsole, but also userboot. 451 -- userboot is there, because we have no way to know 452 -- if the user terminal can draw unicode box chars or not. 453 if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then 454 return true 455 end 456 end 457 return false 458end 459 460function core.isSerialBoot() 461 local s = loader.getenv("boot_serial") 462 if s ~= nil then 463 return true 464 end 465 466 local m = loader.getenv("boot_multicons") 467 if m ~= nil then 468 return true 469 end 470 return false 471end 472 473function core.isSystem386() 474 return loader.machine_arch == "i386" 475end 476 477-- Is the menu skipped in the environment in which we've booted? 478function core.isMenuSkipped() 479 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 480end 481 482-- This may be a better candidate for a 'utility' module. 483function core.deepCopyTable(tbl) 484 local new_tbl = {} 485 for k, v in pairs(tbl) do 486 if type(v) == "table" then 487 new_tbl[k] = core.deepCopyTable(v) 488 else 489 new_tbl[k] = v 490 end 491 end 492 return new_tbl 493end 494 495-- XXX This should go away if we get the table lib into shape for importing. 496-- As of now, it requires some 'os' functions, so we'll implement this in lua 497-- for our uses 498function core.popFrontTable(tbl) 499 -- Shouldn't reasonably happen 500 if #tbl == 0 then 501 return nil, nil 502 elseif #tbl == 1 then 503 return tbl[1], {} 504 end 505 506 local first_value = tbl[1] 507 local new_tbl = {} 508 -- This is not a cheap operation 509 for k, v in ipairs(tbl) do 510 if k > 1 then 511 new_tbl[k - 1] = v 512 end 513 end 514 515 return first_value, new_tbl 516end 517 518function core.getConsoleName() 519 if loader.getenv("boot_multicons") ~= nil then 520 if loader.getenv("boot_serial") ~= nil then 521 return "Dual (Serial primary)" 522 else 523 return "Dual (Video primary)" 524 end 525 else 526 if loader.getenv("boot_serial") ~= nil then 527 return "Serial" 528 else 529 return "Video" 530 end 531 end 532end 533 534function core.nextConsoleChoice() 535 if loader.getenv("boot_multicons") ~= nil then 536 if loader.getenv("boot_serial") ~= nil then 537 loader.unsetenv("boot_serial") 538 else 539 loader.unsetenv("boot_multicons") 540 loader.setenv("boot_serial", "YES") 541 end 542 else 543 if loader.getenv("boot_serial") ~= nil then 544 loader.unsetenv("boot_serial") 545 else 546 loader.setenv("boot_multicons", "YES") 547 loader.setenv("boot_serial", "YES") 548 end 549 end 550end 551 552recordDefaults() 553hook.register("config.reloaded", core.clearCachedKernels) 554return core 555