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 return loader.getenv("acpi.rsdp") ~= nil 137end 138 139function core.isX86() 140 return loader.machine_arch == "i386" or loader.machine_arch == "amd64" 141end 142 143function core.getACPI() 144 if not core.hasACPI() then 145 -- x86 requires ACPI pretty much 146 return false or core.isX86() 147 end 148 149 -- Otherwise, respect disabled if it's set 150 local c = loader.getenv("hint.acpi.0.disabled") 151 return c == nil or tonumber(c) ~= 1 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("kern.eventtimer.periodic", "1") 180 loader.setenv("kern.geom.part.check_integrity", "0") 181 else 182 loader.unsetenv("kern.smp.disabled") 183 loader.unsetenv("hw.ata.ata_dma") 184 loader.unsetenv("hw.ata.atapi_dma") 185 loader.unsetenv("kern.eventtimer.periodic") 186 loader.unsetenv("kern.geom.part.check_integrity") 187 end 188 core.sm = safe_mode 189end 190 191function core.clearCachedKernels() 192 -- Clear the kernel cache on config changes, autodetect might have 193 -- changed or if we've switched boot environments then we could have 194 -- a new kernel set. 195 core.cached_kernels = nil 196end 197 198function core.kernelList() 199 if core.cached_kernels ~= nil then 200 return core.cached_kernels 201 end 202 203 local k = loader.getenv("kernel") 204 local v = loader.getenv("kernels") 205 local autodetect = loader.getenv("kernels_autodetect") or "" 206 207 local kernels = {} 208 local unique = {} 209 local i = 0 210 if k ~= nil then 211 i = i + 1 212 kernels[i] = k 213 unique[k] = true 214 end 215 216 if v ~= nil then 217 for n in v:gmatch("([^;, ]+)[;, ]?") do 218 if unique[n] == nil then 219 i = i + 1 220 kernels[i] = n 221 unique[n] = true 222 end 223 end 224 end 225 226 -- Do not attempt to autodetect if underlying filesystem 227 -- do not support directory listing (e.g. tftp, http) 228 if not lfs.attributes("/boot", "mode") then 229 autodetect = "no" 230 loader.setenv("kernels_autodetect", "NO") 231 end 232 233 -- Base whether we autodetect kernels or not on a loader.conf(5) 234 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 235 -- any kernels we detect based on the criteria described. 236 if autodetect:lower() ~= "yes" then 237 core.cached_kernels = kernels 238 return core.cached_kernels 239 end 240 241 -- Automatically detect other bootable kernel directories using a 242 -- heuristic. Any directory in /boot that contains an ordinary file 243 -- named "kernel" is considered eligible. 244 for file, ftype in lfs.dir("/boot") do 245 local fname = "/boot/" .. file 246 247 if file == "." or file == ".." then 248 goto continue 249 end 250 251 if ftype then 252 if ftype ~= lfs.DT_DIR then 253 goto continue 254 end 255 elseif lfs.attributes(fname, "mode") ~= "directory" then 256 goto continue 257 end 258 259 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 260 goto continue 261 end 262 263 if unique[file] == nil then 264 i = i + 1 265 kernels[i] = file 266 unique[file] = true 267 end 268 269 ::continue:: 270 end 271 core.cached_kernels = kernels 272 return core.cached_kernels 273end 274 275function core.bootenvDefault() 276 return loader.getenv("zfs_be_active") 277end 278 279function core.bootenvList() 280 local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count")) 281 local bootenvs = {} 282 local curenv 283 local envcount = 0 284 local unique = {} 285 286 if bootenv_count == nil or bootenv_count <= 0 then 287 return bootenvs 288 end 289 290 -- Currently selected bootenv is always first/default 291 -- On the rewinded list the bootenv may not exists 292 if core.isRewinded() then 293 curenv = core.bootenvDefaultRewinded() 294 else 295 curenv = core.bootenvDefault() 296 end 297 if curenv ~= nil then 298 envcount = envcount + 1 299 bootenvs[envcount] = curenv 300 unique[curenv] = true 301 end 302 303 for curenv_idx = 0, bootenv_count - 1 do 304 curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]") 305 if curenv ~= nil and unique[curenv] == nil then 306 envcount = envcount + 1 307 bootenvs[envcount] = curenv 308 unique[curenv] = true 309 end 310 end 311 return bootenvs 312end 313 314function core.isCheckpointed() 315 return loader.getenv("zpool_checkpoint") ~= nil 316end 317 318function core.bootenvDefaultRewinded() 319 local defname = "zfs:!" .. string.sub(core.bootenvDefault(), 5) 320 local bootenv_count = tonumber("bootenvs_check_count") 321 322 if bootenv_count == nil or bootenv_count <= 0 then 323 return defname 324 end 325 326 for curenv_idx = 0, bootenv_count - 1 do 327 local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]") 328 if curenv == defname then 329 return defname 330 end 331 end 332 333 return loader.getenv("bootenvs_check[0]") 334end 335 336function core.isRewinded() 337 return bootenv_list == "bootenvs_check" 338end 339 340function core.changeRewindCheckpoint() 341 if core.isRewinded() then 342 bootenv_list = "bootenvs" 343 else 344 bootenv_list = "bootenvs_check" 345 end 346end 347 348function core.loadEntropy() 349 if core.isUEFIBoot() then 350 if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then 351 loader.perform("efi-seed-entropy") 352 end 353 end 354end 355 356function core.setDefaults() 357 core.setACPI(default_acpi) 358 core.setSafeMode(default_safe_mode) 359 core.setSingleUser(default_single_user) 360 core.setVerbose(default_verbose) 361end 362 363function core.autoboot(argstr) 364 -- loadelf() only if we've not already loaded a kernel 365 if loader.getenv("kernelname") == nil then 366 config.loadelf() 367 end 368 core.loadEntropy() 369 loader.perform(composeLoaderCmd("autoboot", argstr)) 370end 371 372function core.boot(argstr) 373 -- loadelf() only if we've not already loaded a kernel 374 if loader.getenv("kernelname") == nil then 375 config.loadelf() 376 end 377 core.loadEntropy() 378 loader.perform(composeLoaderCmd("boot", argstr)) 379end 380 381function core.isSingleUserBoot() 382 local single_user = loader.getenv("boot_single") 383 return single_user ~= nil and single_user:lower() == "yes" 384end 385 386function core.isUEFIBoot() 387 local efiver = loader.getenv("efi-version") 388 389 return efiver ~= nil 390end 391 392function core.isZFSBoot() 393 local c = loader.getenv("currdev") 394 395 if c ~= nil then 396 return c:match("^zfs:") ~= nil 397 end 398 return false 399end 400 401function core.isFramebufferConsole() 402 local c = loader.getenv("console") 403 if c ~= nil then 404 if c:find("efi") == nil and c:find("vidconsole") == nil then 405 return false 406 end 407 if loader.getenv("screen.depth") ~= nil then 408 return true 409 end 410 end 411 return false 412end 413 414function core.isSerialConsole() 415 local c = loader.getenv("console") 416 if c ~= nil then 417 -- serial console is comconsole, but also userboot. 418 -- userboot is there, because we have no way to know 419 -- if the user terminal can draw unicode box chars or not. 420 if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then 421 return true 422 end 423 end 424 return false 425end 426 427function core.isSerialBoot() 428 local s = loader.getenv("boot_serial") 429 if s ~= nil then 430 return true 431 end 432 433 local m = loader.getenv("boot_multicons") 434 if m ~= nil then 435 return true 436 end 437 return false 438end 439 440-- Is the menu skipped in the environment in which we've booted? 441function core.isMenuSkipped() 442 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 443end 444 445-- This may be a better candidate for a 'utility' module. 446function core.deepCopyTable(tbl) 447 local new_tbl = {} 448 for k, v in pairs(tbl) do 449 if type(v) == "table" then 450 new_tbl[k] = core.deepCopyTable(v) 451 else 452 new_tbl[k] = v 453 end 454 end 455 return new_tbl 456end 457 458-- XXX This should go away if we get the table lib into shape for importing. 459-- As of now, it requires some 'os' functions, so we'll implement this in lua 460-- for our uses 461function core.popFrontTable(tbl) 462 -- Shouldn't reasonably happen 463 if #tbl == 0 then 464 return nil, nil 465 elseif #tbl == 1 then 466 return tbl[1], {} 467 end 468 469 local first_value = tbl[1] 470 local new_tbl = {} 471 -- This is not a cheap operation 472 for k, v in ipairs(tbl) do 473 if k > 1 then 474 new_tbl[k - 1] = v 475 end 476 end 477 478 return first_value, new_tbl 479end 480 481function core.getConsoleName() 482 if loader.getenv("boot_multicons") ~= nil then 483 if loader.getenv("boot_serial") ~= nil then 484 return "Dual (Serial primary)" 485 else 486 return "Dual (Video primary)" 487 end 488 else 489 if loader.getenv("boot_serial") ~= nil then 490 return "Serial" 491 else 492 return "Video" 493 end 494 end 495end 496 497function core.nextConsoleChoice() 498 if loader.getenv("boot_multicons") ~= nil then 499 if loader.getenv("boot_serial") ~= nil then 500 loader.unsetenv("boot_serial") 501 else 502 loader.unsetenv("boot_multicons") 503 loader.setenv("boot_serial", "YES") 504 end 505 else 506 if loader.getenv("boot_serial") ~= nil then 507 loader.unsetenv("boot_serial") 508 else 509 loader.setenv("boot_multicons", "YES") 510 loader.setenv("boot_serial", "YES") 511 end 512 end 513end 514 515recordDefaults() 516hook.register("config.reloaded", core.clearCachedKernels) 517return core 518