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 loader.setenv("boot_safe", "YES") 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("kern.eventtimer.periodic") 188 loader.unsetenv("kern.geom.part.check_integrity") 189 loader.unsetenv("boot_safe") 190 end 191 core.sm = safe_mode 192end 193 194function core.clearCachedKernels() 195 -- Clear the kernel cache on config changes, autodetect might have 196 -- changed or if we've switched boot environments then we could have 197 -- a new kernel set. 198 core.cached_kernels = nil 199end 200 201function core.kernelList() 202 if core.cached_kernels ~= nil then 203 return core.cached_kernels 204 end 205 206 local default_kernel = loader.getenv("kernel") 207 local v = loader.getenv("kernels") 208 local autodetect = loader.getenv("kernels_autodetect") or "" 209 210 local kernels = {} 211 local unique = {} 212 local i = 0 213 214 if default_kernel then 215 i = i + 1 216 kernels[i] = default_kernel 217 unique[default_kernel] = 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 -- Do not attempt to autodetect if underlying filesystem 231 -- do not support directory listing (e.g. tftp, http) 232 if not lfs.attributes("/boot", "mode") then 233 autodetect = "no" 234 loader.setenv("kernels_autodetect", "NO") 235 end 236 237 -- Base whether we autodetect kernels or not on a loader.conf(5) 238 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 239 -- any kernels we detect based on the criteria described. 240 if autodetect:lower() ~= "yes" then 241 core.cached_kernels = kernels 242 return core.cached_kernels 243 end 244 245 local present = {} 246 247 -- Automatically detect other bootable kernel directories using a 248 -- heuristic. Any directory in /boot that contains an ordinary file 249 -- named "kernel" is considered eligible. 250 for file, ftype in lfs.dir("/boot") do 251 local fname = "/boot/" .. file 252 253 if file == "." or file == ".." then 254 goto continue 255 end 256 257 if ftype then 258 if ftype ~= lfs.DT_DIR and ftype ~= (lfs.DT_LNK or 10) then 259 goto continue 260 end 261 elseif lfs.attributes(fname, "mode") ~= "directory" then 262 goto continue 263 end 264 265 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 266 goto continue 267 end 268 269 if unique[file] == nil then 270 i = i + 1 271 kernels[i] = file 272 unique[file] = true 273 end 274 275 present[file] = true 276 277 ::continue:: 278 end 279 280 -- If we found more than one kernel, prune the "kernel" specified kernel 281 -- off of the list if it wasn't found during traversal. If we didn't 282 -- actually find any kernels, we just assume that they know what they're 283 -- doing and leave it alone. 284 if default_kernel and not present[default_kernel] and #kernels > 1 then 285 for n = 1, #kernels do 286 if n == #kernels then 287 kernels[n] = nil 288 else 289 kernels[n] = kernels[n + 1] 290 end 291 end 292 293 -- The config/boot bits use the env var as a fallback if the 294 -- menu's kernel selector remains untouched, so we want to 295 -- update our notion of the default kernel to one that is 296 -- actually present. 297 loader.setenv("kernel", kernels[1]) 298 end 299 300 core.cached_kernels = kernels 301 return core.cached_kernels 302end 303 304function core.bootenvDefault() 305 return loader.getenv("zfs_be_active") 306end 307 308function core.bootenvFilter(func) 309 local oldf = core.bootenv_filter 310 311 -- Filter contract: returns true if the BE should be kept, false if it 312 -- should be hidden. 313 core.bootenv_filter = func 314 return oldf 315end 316 317function core.bootenvIter() 318 local envs = core.bootenvList() 319 320 if #envs ~= 0 then 321 local root = "zfs:" .. loader.getenv("zfs_be_root") .. "/" 322 323 for idx, bespec in ipairs(envs) do 324 bespec = bespec:gsub("^" .. root, "") 325 envs[idx] = bespec 326 end 327 end 328 329 return next, envs, nil 330end 331 332function core.bootenvList() 333 local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count")) 334 local bootenvs = {} 335 local curenv 336 local envcount = 0 337 local unique = {} 338 339 if bootenv_count == nil or bootenv_count <= 0 then 340 return bootenvs 341 end 342 343 -- Currently selected bootenv is always first/default 344 -- On the rewinded list the bootenv may not exists 345 if core.isRewinded() then 346 curenv = core.bootenvDefaultRewinded() 347 else 348 curenv = core.bootenvDefault() 349 end 350 if curenv ~= nil then 351 envcount = envcount + 1 352 bootenvs[envcount] = curenv 353 unique[curenv] = true 354 end 355 356 for curenv_idx = 0, bootenv_count - 1 do 357 curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]") 358 if curenv ~= nil and unique[curenv] == nil then 359 unique[curenv] = true 360 361 -- If we have a filter installed (by a local module), we 362 -- give it a chance to veto the BE. 363 if not core.bootenv_filter or 364 core.bootenv_filter(curenv) then 365 envcount = envcount + 1 366 bootenvs[envcount] = curenv 367 end 368 end 369 end 370 371 return bootenvs 372end 373 374function core.isCheckpointed() 375 return loader.getenv("zpool_checkpoint") ~= nil 376end 377 378function core.bootenvDefaultRewinded() 379 local defname = "zfs:!" .. string.sub(core.bootenvDefault(), 5) 380 local bootenv_count = tonumber("bootenvs_check_count") 381 382 if bootenv_count == nil or bootenv_count <= 0 then 383 return defname 384 end 385 386 for curenv_idx = 0, bootenv_count - 1 do 387 local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]") 388 if curenv == defname then 389 return defname 390 end 391 end 392 393 return loader.getenv("bootenvs_check[0]") 394end 395 396function core.isRewinded() 397 return bootenv_list == "bootenvs_check" 398end 399 400function core.changeRewindCheckpoint() 401 if core.isRewinded() then 402 bootenv_list = "bootenvs" 403 else 404 bootenv_list = "bootenvs_check" 405 end 406end 407 408function core.loadEntropy() 409 if core.isUEFIBoot() then 410 if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then 411 local seedsize = loader.getenv("entropy_efi_seed_size") or "2048" 412 loader.perform("efi-seed-entropy " .. seedsize) 413 end 414 end 415end 416 417function core.setDefaults() 418 core.setACPI(default_acpi) 419 core.setSafeMode(default_safe_mode) 420 core.setSingleUser(default_single_user) 421 core.setVerbose(default_verbose) 422end 423 424function core.autoboot(argstr) 425 -- loadelf() only if we've not already loaded a kernel 426 if loader.getenv("kernelname") == nil then 427 config.loadelf() 428 end 429 core.loadEntropy() 430 loader.perform(composeLoaderCmd("autoboot", argstr)) 431end 432 433function core.boot(argstr) 434 -- loadelf() only if we've not already loaded a kernel 435 if loader.getenv("kernelname") == nil then 436 config.loadelf() 437 end 438 core.loadEntropy() 439 loader.perform(composeLoaderCmd("boot", argstr)) 440end 441 442function core.hasFeature(name) 443 if not loader.has_feature then 444 -- Loader too old, no feature support 445 return nil, "No feature support in loaded loader" 446 end 447 448 return loader.has_feature(name) 449end 450 451function core.isSingleUserBoot() 452 local single_user = loader.getenv("boot_single") 453 return single_user ~= nil and single_user:lower() ~= "no" 454end 455 456function core.isUEFIBoot() 457 local efiver = loader.getenv("efi-version") 458 459 return efiver ~= nil 460end 461 462function core.isZFSBoot() 463 local c = loader.getenv("currdev") 464 465 if c ~= nil then 466 return c:match("^zfs:") ~= nil 467 end 468 return false 469end 470 471function core.isFramebufferConsole() 472 local c = loader.getenv("console") 473 if c ~= nil then 474 if c:find("efi") == nil and c:find("vidconsole") == nil then 475 return false 476 end 477 if loader.getenv("screen.depth") ~= nil then 478 return true 479 end 480 end 481 return false 482end 483 484function core.isSerialConsole() 485 local c = loader.getenv("console") 486 if c ~= nil then 487 -- serial console is comconsole, but also userboot. 488 -- userboot is there, because we have no way to know 489 -- if the user terminal can draw unicode box chars or not. 490 if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then 491 return true 492 end 493 end 494 return false 495end 496 497function core.isSerialBoot() 498 local s = loader.getenv("boot_serial") 499 if s ~= nil then 500 return true 501 end 502 503 local m = loader.getenv("boot_multicons") 504 if m ~= nil then 505 return true 506 end 507 return false 508end 509 510-- Is the menu skipped in the environment in which we've booted? 511function core.isMenuSkipped() 512 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 513end 514 515-- This may be a better candidate for a 'utility' module. 516function core.deepCopyTable(tbl) 517 local new_tbl = {} 518 for k, v in pairs(tbl) do 519 if type(v) == "table" then 520 new_tbl[k] = core.deepCopyTable(v) 521 else 522 new_tbl[k] = v 523 end 524 end 525 return new_tbl 526end 527 528-- XXX This should go away if we get the table lib into shape for importing. 529-- As of now, it requires some 'os' functions, so we'll implement this in lua 530-- for our uses 531function core.popFrontTable(tbl) 532 -- Shouldn't reasonably happen 533 if #tbl == 0 then 534 return nil, nil 535 elseif #tbl == 1 then 536 return tbl[1], {} 537 end 538 539 local first_value = tbl[1] 540 local new_tbl = {} 541 -- This is not a cheap operation 542 for k, v in ipairs(tbl) do 543 if k > 1 then 544 new_tbl[k - 1] = v 545 end 546 end 547 548 return first_value, new_tbl 549end 550 551function core.getConsoleName() 552 if loader.getenv("boot_multicons") ~= nil then 553 if loader.getenv("boot_serial") ~= nil then 554 return "Dual (Serial primary)" 555 else 556 return "Dual (Video primary)" 557 end 558 else 559 if loader.getenv("boot_serial") ~= nil then 560 return "Serial" 561 else 562 return "Video" 563 end 564 end 565end 566 567function core.nextConsoleChoice() 568 if loader.getenv("boot_multicons") ~= nil then 569 if loader.getenv("boot_serial") ~= nil then 570 loader.unsetenv("boot_serial") 571 else 572 loader.unsetenv("boot_multicons") 573 loader.setenv("boot_serial", "YES") 574 end 575 else 576 if loader.getenv("boot_serial") ~= nil then 577 loader.unsetenv("boot_serial") 578 else 579 loader.setenv("boot_multicons", "YES") 580 loader.setenv("boot_serial", "YES") 581 end 582 end 583end 584 585function core.switchBE(env) 586 -- This branch will most likely be taken by the switch-be CLI command, 587 -- not by the menu. We could do some more validation that it's a valid 588 -- BE and let the user fully specify a zfs:be/dataset to avoid the 589 -- validation, but this isn't done at the moment. 590 if not env:match("^zfs:") then 591 local root = loader.getenv("zfs_be_root") 592 593 if not root then 594 print("ZFS BE root not available -- no action taken") 595 return 596 end 597 598 if not env:match("^" .. root) then 599 env = "zfs:" .. root .. "/" .. env 600 else 601 env = "zfs:" .. env 602 end 603 end 604 605 loader.setenv("vfs.root.mountfrom", env) 606 loader.setenv("currdev", env .. ":") 607 config.reload() 608 if loader.getenv("kernelname") ~= nil then 609 loader.perform("unload") 610 end 611end 612 613-- The graphical-enabled loaders have unicode drawing character support. The 614-- text-only ones do not. We check the old and new bindings for term_drawrect as 615-- a proxy for unicode support, which will work on older boot loaders as well 616-- as be future proof for when we remove the old binding. This also abstracts 617-- out the test to one spot in case we start to export this notion more directly. 618function core.hasUnicode() 619 return gfx.term_drawrect ~= nil or loader.term_drawrect ~= nil 620end 621 622-- Sanity check the boot loader revision 623-- Loaders with version 3.0 have everything that we need without backwards 624-- compatible hacks. Warn users that still have old versions to upgrade so 625-- that we can remove the backwards compatible hacks in the future since 626-- they have been there a long time. 627local loader_major = 3 628 629function core.loaderTooOld() 630 return loader.version == nil or loader.version < loader_major * 1000 631end 632 633if core.loaderTooOld() then 634 print("**********************************************************************") 635 print("**********************************************************************") 636 print("***** *****") 637 print("***** BOOT LOADER IS TOO OLD. PLEASE UPGRADE. *****") 638 print("***** *****") 639 print("**********************************************************************") 640 print("**********************************************************************") 641end 642 643recordDefaults() 644hook.register("config.reloaded", core.clearCachedKernels) 645return core 646