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 nil on failure. 70-- A message will also be printed on failure, with one exception: non-verbose 71-- loading will suppress 'module not found' errors. 72function try_include(module) 73 local status, ret = pcall(require, module) 74 -- ret is the module if we succeeded. 75 if status then 76 return ret 77 end 78 -- Otherwise, ret is just a message; filter out ENOENT unless we're 79 -- doing a verbose load. As a consequence, try_include prior to loading 80 -- configuration will not display 'module not found'. All other errors 81 -- in loading will be printed. 82 if config.verbose or ret:match("^module .+ not found") == nil then 83 error(ret, 2) 84 end 85 return nil 86end 87 88-- Module exports 89-- Commonly appearing constants 90core.KEY_BACKSPACE = 8 91core.KEY_ENTER = 13 92core.KEY_DELETE = 127 93 94-- Note that this is a decimal representation, despite the leading 0 that in 95-- other contexts (outside of Lua) may mean 'octal' 96core.KEYSTR_ESCAPE = "\027" 97core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "[" 98 99core.MENU_RETURN = "return" 100core.MENU_ENTRY = "entry" 101core.MENU_SEPARATOR = "separator" 102core.MENU_SUBMENU = "submenu" 103core.MENU_CAROUSEL_ENTRY = "carousel_entry" 104 105function core.setVerbose(verbose) 106 if verbose == nil then 107 verbose = not core.verbose 108 end 109 110 if verbose then 111 loader.setenv("boot_verbose", "YES") 112 else 113 loader.unsetenv("boot_verbose") 114 end 115 core.verbose = verbose 116end 117 118function core.setSingleUser(single_user) 119 if single_user == nil then 120 single_user = not core.su 121 end 122 123 if single_user then 124 loader.setenv("boot_single", "YES") 125 else 126 loader.unsetenv("boot_single") 127 end 128 core.su = single_user 129end 130 131function core.getACPIPresent(checking_system_defaults) 132 local c = loader.getenv("hint.acpi.0.rsdp") 133 134 if c ~= nil then 135 if checking_system_defaults then 136 return true 137 end 138 -- Otherwise, respect disabled if it's set 139 c = loader.getenv("hint.acpi.0.disabled") 140 return c == nil or tonumber(c) ~= 1 141 end 142 return false 143end 144 145function core.setACPI(acpi) 146 if acpi == nil then 147 acpi = not core.acpi 148 end 149 150 if acpi then 151 loader.setenv("acpi_load", "YES") 152 loader.setenv("hint.acpi.0.disabled", "0") 153 loader.unsetenv("loader.acpi_disabled_by_user") 154 else 155 loader.unsetenv("acpi_load") 156 loader.setenv("hint.acpi.0.disabled", "1") 157 loader.setenv("loader.acpi_disabled_by_user", "1") 158 end 159 core.acpi = acpi 160end 161 162function core.setSafeMode(safe_mode) 163 if safe_mode == nil then 164 safe_mode = not core.sm 165 end 166 if safe_mode then 167 loader.setenv("kern.smp.disabled", "1") 168 loader.setenv("hw.ata.ata_dma", "0") 169 loader.setenv("hw.ata.atapi_dma", "0") 170 loader.setenv("hw.ata.wc", "0") 171 loader.setenv("hw.eisa_slots", "0") 172 loader.setenv("kern.eventtimer.periodic", "1") 173 loader.setenv("kern.geom.part.check_integrity", "0") 174 else 175 loader.unsetenv("kern.smp.disabled") 176 loader.unsetenv("hw.ata.ata_dma") 177 loader.unsetenv("hw.ata.atapi_dma") 178 loader.unsetenv("hw.ata.wc") 179 loader.unsetenv("hw.eisa_slots") 180 loader.unsetenv("kern.eventtimer.periodic") 181 loader.unsetenv("kern.geom.part.check_integrity") 182 end 183 core.sm = safe_mode 184end 185 186function core.clearCachedKernels() 187 -- Clear the kernel cache on config changes, autodetect might have 188 -- changed or if we've switched boot environments then we could have 189 -- a new kernel set. 190 core.cached_kernels = nil 191end 192 193function core.kernelList() 194 if core.cached_kernels ~= nil then 195 return core.cached_kernels 196 end 197 198 local k = loader.getenv("kernel") 199 local v = loader.getenv("kernels") 200 local autodetect = loader.getenv("kernels_autodetect") or "" 201 202 local kernels = {} 203 local unique = {} 204 local i = 0 205 if k ~= nil then 206 i = i + 1 207 kernels[i] = k 208 unique[k] = true 209 end 210 211 if v ~= nil then 212 for n in v:gmatch("([^;, ]+)[;, ]?") do 213 if unique[n] == nil then 214 i = i + 1 215 kernels[i] = n 216 unique[n] = true 217 end 218 end 219 end 220 221 -- Base whether we autodetect kernels or not on a loader.conf(5) 222 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 223 -- any kernels we detect based on the criteria described. 224 if autodetect:lower() ~= "yes" then 225 core.cached_kernels = kernels 226 return core.cached_kernels 227 end 228 229 -- Automatically detect other bootable kernel directories using a 230 -- heuristic. Any directory in /boot that contains an ordinary file 231 -- named "kernel" is considered eligible. 232 for file in lfs.dir("/boot") do 233 local fname = "/boot/" .. file 234 235 if file == "." or file == ".." then 236 goto continue 237 end 238 239 if lfs.attributes(fname, "mode") ~= "directory" then 240 goto continue 241 end 242 243 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 244 goto continue 245 end 246 247 if unique[file] == nil then 248 i = i + 1 249 kernels[i] = file 250 unique[file] = true 251 end 252 253 ::continue:: 254 end 255 core.cached_kernels = kernels 256 return core.cached_kernels 257end 258 259function core.bootenvDefault() 260 return loader.getenv("zfs_be_active") 261end 262 263function core.bootenvList() 264 local bootenv_count = tonumber(loader.getenv("bootenvs_count")) 265 local bootenvs = {} 266 local curenv 267 local envcount = 0 268 local unique = {} 269 270 if bootenv_count == nil or bootenv_count <= 0 then 271 return bootenvs 272 end 273 274 -- Currently selected bootenv is always first/default 275 curenv = core.bootenvDefault() 276 if curenv ~= nil then 277 envcount = envcount + 1 278 bootenvs[envcount] = curenv 279 unique[curenv] = true 280 end 281 282 for curenv_idx = 0, bootenv_count - 1 do 283 curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]") 284 if curenv ~= nil and unique[curenv] == nil then 285 envcount = envcount + 1 286 bootenvs[envcount] = curenv 287 unique[curenv] = true 288 end 289 end 290 return bootenvs 291end 292 293function core.setDefaults() 294 core.setACPI(core.getACPIPresent(true)) 295 core.setSafeMode(default_safe_mode) 296 core.setSingleUser(default_single_user) 297 core.setVerbose(default_verbose) 298end 299 300function core.autoboot(argstr) 301 -- loadelf() only if we've not already loaded a kernel 302 if loader.getenv("kernelname") == nil then 303 config.loadelf() 304 end 305 loader.perform(composeLoaderCmd("autoboot", argstr)) 306end 307 308function core.boot(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("boot", argstr)) 314end 315 316function core.isSingleUserBoot() 317 local single_user = loader.getenv("boot_single") 318 return single_user ~= nil and single_user:lower() == "yes" 319end 320 321function core.isUEFIBoot() 322 local efiver = loader.getenv("efi-version") 323 324 return efiver ~= nil 325end 326 327function core.isZFSBoot() 328 local c = loader.getenv("currdev") 329 330 if c ~= nil then 331 return c:match("^zfs:") ~= nil 332 end 333 return false 334end 335 336function core.isSerialBoot() 337 local s = loader.getenv("boot_serial") 338 if s ~= nil then 339 return true 340 end 341 342 local m = loader.getenv("boot_multicons") 343 if m ~= nil then 344 return true 345 end 346 return false 347end 348 349function core.isSystem386() 350 return loader.machine_arch == "i386" 351end 352 353-- Is the menu skipped in the environment in which we've booted? 354function core.isMenuSkipped() 355 return string.lower(loader.getenv("beastie_disable") or "") == "yes" 356end 357 358-- This may be a better candidate for a 'utility' module. 359function core.deepCopyTable(tbl) 360 local new_tbl = {} 361 for k, v in pairs(tbl) do 362 if type(v) == "table" then 363 new_tbl[k] = core.deepCopyTable(v) 364 else 365 new_tbl[k] = v 366 end 367 end 368 return new_tbl 369end 370 371-- XXX This should go away if we get the table lib into shape for importing. 372-- As of now, it requires some 'os' functions, so we'll implement this in lua 373-- for our uses 374function core.popFrontTable(tbl) 375 -- Shouldn't reasonably happen 376 if #tbl == 0 then 377 return nil, nil 378 elseif #tbl == 1 then 379 return tbl[1], {} 380 end 381 382 local first_value = tbl[1] 383 local new_tbl = {} 384 -- This is not a cheap operation 385 for k, v in ipairs(tbl) do 386 if k > 1 then 387 new_tbl[k - 1] = v 388 end 389 end 390 391 return first_value, new_tbl 392end 393 394recordDefaults() 395hook.register("config.reloaded", core.clearCachedKernels) 396return core 397