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