1-- 2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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-- $FreeBSD$ 30-- 31 32local config = require("config") 33local hook = require("hook") 34 35local core = {} 36 37local default_safe_mode = false 38local default_single_user = false 39local default_verbose = false 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 .. "[" 106 107core.MENU_RETURN = "return" 108core.MENU_ENTRY = "entry" 109core.MENU_SEPARATOR = "separator" 110core.MENU_SUBMENU = "submenu" 111core.MENU_CAROUSEL_ENTRY = "carousel_entry" 112 113function core.setVerbose(verbose) 114 if verbose == nil then 115 verbose = not core.verbose 116 end 117 118 if verbose then 119 loader.setenv("boot_verbose", "YES") 120 else 121 loader.unsetenv("boot_verbose") 122 end 123 core.verbose = verbose 124end 125 126function core.setSingleUser(single_user) 127 if single_user == nil then 128 single_user = not core.su 129 end 130 131 if single_user then 132 loader.setenv("boot_single", "YES") 133 else 134 loader.unsetenv("boot_single") 135 end 136 core.su = single_user 137end 138 139function core.getACPIPresent(checking_system_defaults) 140 local c = loader.getenv("hint.acpi.0.rsdp") 141 142 if c ~= nil then 143 if checking_system_defaults then 144 return true 145 end 146 -- Otherwise, respect disabled if it's set 147 c = loader.getenv("hint.acpi.0.disabled") 148 return c == nil or tonumber(c) ~= 1 149 end 150 return false 151end 152 153function core.setACPI(acpi) 154 if acpi == nil then 155 acpi = not core.acpi 156 end 157 158 if acpi then 159 loader.setenv("acpi_load", "YES") 160 loader.setenv("hint.acpi.0.disabled", "0") 161 loader.unsetenv("loader.acpi_disabled_by_user") 162 else 163 loader.unsetenv("acpi_load") 164 loader.setenv("hint.acpi.0.disabled", "1") 165 loader.setenv("loader.acpi_disabled_by_user", "1") 166 end 167 core.acpi = acpi 168end 169 170function core.setSafeMode(safe_mode) 171 if safe_mode == nil then 172 safe_mode = not core.sm 173 end 174 if safe_mode then 175 loader.setenv("kern.smp.disabled", "1") 176 loader.setenv("hw.ata.ata_dma", "0") 177 loader.setenv("hw.ata.atapi_dma", "0") 178 loader.setenv("hw.ata.wc", "0") 179 loader.setenv("hw.eisa_slots", "0") 180 loader.setenv("kern.eventtimer.periodic", "1") 181 loader.setenv("kern.geom.part.check_integrity", "0") 182 else 183 loader.unsetenv("kern.smp.disabled") 184 loader.unsetenv("hw.ata.ata_dma") 185 loader.unsetenv("hw.ata.atapi_dma") 186 loader.unsetenv("hw.ata.wc") 187 loader.unsetenv("hw.eisa_slots") 188 loader.unsetenv("kern.eventtimer.periodic") 189 loader.unsetenv("kern.geom.part.check_integrity") 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 k = 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 if k ~= nil then 214 i = i + 1 215 kernels[i] = k 216 unique[k] = true 217 end 218 219 if v ~= nil then 220 for n in v:gmatch("([^;, ]+)[;, ]?") do 221 if unique[n] == nil then 222 i = i + 1 223 kernels[i] = n 224 unique[n] = true 225 end 226 end 227 end 228 229 -- Base whether we autodetect kernels or not on a loader.conf(5) 230 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 231 -- any kernels we detect based on the criteria described. 232 if autodetect:lower() ~= "yes" then 233 core.cached_kernels = kernels 234 return core.cached_kernels 235 end 236 237 -- Automatically detect other bootable kernel directories using a 238 -- heuristic. Any directory in /boot that contains an ordinary file 239 -- named "kernel" is considered eligible. 240 for file in lfs.dir("/boot") do 241 local fname = "/boot/" .. file 242 243 if file == "." or file == ".." then 244 goto continue 245 end 246 247 if lfs.attributes(fname, "mode") ~= "directory" then 248 goto continue 249 end 250 251 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 252 goto continue 253 end 254 255 if unique[file] == nil then 256 i = i + 1 257 kernels[i] = file 258 unique[file] = true 259 end 260 261 ::continue:: 262 end 263 core.cached_kernels = kernels 264 return core.cached_kernels 265end 266 267function core.bootenvDefault() 268 return loader.getenv("zfs_be_active") 269end 270 271function core.bootenvList() 272 local bootenv_count = tonumber(loader.getenv("bootenvs_count")) 273 local bootenvs = {} 274 local curenv 275 local envcount = 0 276 local unique = {} 277 278 if bootenv_count == nil or bootenv_count <= 0 then 279 return bootenvs 280 end 281 282 -- Currently selected bootenv is always first/default 283 curenv = core.bootenvDefault() 284 if curenv ~= nil then 285 envcount = envcount + 1 286 bootenvs[envcount] = curenv 287 unique[curenv] = true 288 end 289 290 for curenv_idx = 0, bootenv_count - 1 do 291 curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]") 292 if curenv ~= nil and unique[curenv] == nil then 293 envcount = envcount + 1 294 bootenvs[envcount] = curenv 295 unique[curenv] = true 296 end 297 end 298 return bootenvs 299end 300 301function core.setDefaults() 302 core.setACPI(core.getACPIPresent(true)) 303 core.setSafeMode(default_safe_mode) 304 core.setSingleUser(default_single_user) 305 core.setVerbose(default_verbose) 306end 307 308function core.autoboot(argstr) 309 -- loadelf() only if we've not already loaded a kernel 310 if loader.getenv("kernelname") == nil then 311 config.loadelf() 312 end 313 loader.perform(composeLoaderCmd("autoboot", argstr)) 314end 315 316function core.boot(argstr) 317 -- loadelf() only if we've not already loaded a kernel 318 if loader.getenv("kernelname") == nil then 319 config.loadelf() 320 end 321 loader.perform(composeLoaderCmd("boot", argstr)) 322end 323 324function core.isSingleUserBoot() 325 local single_user = loader.getenv("boot_single") 326 return single_user ~= nil and single_user:lower() == "yes" 327end 328 329function core.isUEFIBoot() 330 local efiver = loader.getenv("efi-version") 331 332 return efiver ~= nil 333end 334 335function core.isZFSBoot() 336 local c = loader.getenv("currdev") 337 338 if c ~= nil then 339 return c:match("^zfs:") ~= nil 340 end 341 return false 342end 343 344function core.isSerialConsole() 345 local c = loader.getenv("console") 346 if c ~= nil then 347 if c:find("comconsole") ~= nil then 348 return true 349 end 350 end 351 return false 352end 353 354function core.isSerialBoot() 355 local s = loader.getenv("boot_serial") 356 if s ~= nil then 357 return true 358 end 359 360 local m = loader.getenv("boot_multicons") 361 if m ~= nil then 362 return true 363 end 364 return false 365end 366 367function core.isSystem386() 368 return loader.machine_arch == "i386" 369end 370 371-- Is the menu skipped in the environment in which we've booted? 372function core.isMenuSkipped() 373 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 374end 375 376-- This may be a better candidate for a 'utility' module. 377function core.deepCopyTable(tbl) 378 local new_tbl = {} 379 for k, v in pairs(tbl) do 380 if type(v) == "table" then 381 new_tbl[k] = core.deepCopyTable(v) 382 else 383 new_tbl[k] = v 384 end 385 end 386 return new_tbl 387end 388 389-- XXX This should go away if we get the table lib into shape for importing. 390-- As of now, it requires some 'os' functions, so we'll implement this in lua 391-- for our uses 392function core.popFrontTable(tbl) 393 -- Shouldn't reasonably happen 394 if #tbl == 0 then 395 return nil, nil 396 elseif #tbl == 1 then 397 return tbl[1], {} 398 end 399 400 local first_value = tbl[1] 401 local new_tbl = {} 402 -- This is not a cheap operation 403 for k, v in ipairs(tbl) do 404 if k > 1 then 405 new_tbl[k - 1] = v 406 end 407 end 408 409 return first_value, new_tbl 410end 411 412recordDefaults() 413hook.register("config.reloaded", core.clearCachedKernels) 414return core 415