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