1088b4f5fSWarner Losh-- 272e39d71SKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 372e39d71SKyle Evans-- 4088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 5beaafe4fSKyle Evans-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> 6088b4f5fSWarner Losh-- All rights reserved. 7088b4f5fSWarner Losh-- 8088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without 9088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions 10088b4f5fSWarner Losh-- are met: 11088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright 12088b4f5fSWarner Losh-- notice, this list of conditions and the following disclaimer. 13088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright 14088b4f5fSWarner Losh-- notice, this list of conditions and the following disclaimer in the 15088b4f5fSWarner Losh-- documentation and/or other materials provided with the distribution. 16088b4f5fSWarner Losh-- 17088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20088b4f5fSWarner Losh-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27088b4f5fSWarner Losh-- SUCH DAMAGE. 28088b4f5fSWarner Losh-- 29088b4f5fSWarner Losh-- $FreeBSD$ 30088b4f5fSWarner Losh-- 31088b4f5fSWarner Losh 32011eae6cSKyle Evanslocal config = require("config") 33aea262bfSKyle Evanslocal hook = require("hook") 34fa4a2394SKyle Evans 35aedd6be5SKyle Evanslocal core = {} 36088b4f5fSWarner Losh 37*1613f091SKyle Evanslocal default_safe_mode = false 38*1613f091SKyle Evanslocal default_single_user = false 39*1613f091SKyle Evanslocal default_verbose = false 40*1613f091SKyle Evans 41322a2dddSKyle Evanslocal function composeLoaderCmd(cmd_name, argstr) 429f71d421SKyle Evans if argstr ~= nil then 43aedd6be5SKyle Evans cmd_name = cmd_name .. " " .. argstr 446d4ed94dSKyle Evans end 45aedd6be5SKyle Evans return cmd_name 466d4ed94dSKyle Evansend 476d4ed94dSKyle Evans 48*1613f091SKyle Evanslocal function recordDefaults() 49*1613f091SKyle Evans -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, 50*1613f091SKyle Evans -- it will generally be set upon execution of the kernel. Because of 51*1613f091SKyle Evans -- this, we can't (or don't really want to) detect/disable ACPI on !i386 52*1613f091SKyle Evans -- reliably. Just set it enabled if we detect it and leave well enough 53*1613f091SKyle Evans -- alone if we don't. 54*1613f091SKyle Evans local boot_acpi = core.isSystem386() and core.getACPIPresent(false) 55*1613f091SKyle Evans local boot_single = loader.getenv("boot_single") or "no" 56*1613f091SKyle Evans local boot_verbose = loader.getenv("boot_verbose") or "no" 57*1613f091SKyle Evans default_single_user = boot_single:lower() ~= "no" 58*1613f091SKyle Evans default_verbose = boot_verbose:lower() ~= "no" 59*1613f091SKyle Evans 60*1613f091SKyle Evans if boot_acpi then 61*1613f091SKyle Evans core.setACPI(true) 62*1613f091SKyle Evans end 63*1613f091SKyle Evans core.setSingleUser(default_single_user) 64*1613f091SKyle Evans core.setVerbose(default_verbose) 65*1613f091SKyle Evansend 66*1613f091SKyle Evans 67*1613f091SKyle Evans 6807faaf78SKyle Evans-- Globals 6907faaf78SKyle Evans-- try_include will return the loaded module on success, or nil on failure. 7007faaf78SKyle Evans-- A message will also be printed on failure, with one exception: non-verbose 7107faaf78SKyle Evans-- loading will suppress 'module not found' errors. 7207faaf78SKyle Evansfunction try_include(module) 7307faaf78SKyle Evans local status, ret = pcall(require, module) 7407faaf78SKyle Evans -- ret is the module if we succeeded. 7507faaf78SKyle Evans if status then 7607faaf78SKyle Evans return ret 7707faaf78SKyle Evans end 7807faaf78SKyle Evans -- Otherwise, ret is just a message; filter out ENOENT unless we're 7907faaf78SKyle Evans -- doing a verbose load. As a consequence, try_include prior to loading 8007faaf78SKyle Evans -- configuration will not display 'module not found'. All other errors 8107faaf78SKyle Evans -- in loading will be printed. 8207faaf78SKyle Evans if config.verbose or ret:match("^module .+ not found") == nil then 83892b3a52SKyle Evans error(ret, 2) 8407faaf78SKyle Evans end 8507faaf78SKyle Evans return nil 8607faaf78SKyle Evansend 8707faaf78SKyle Evans 88b5746545SKyle Evans-- Module exports 89fe672a15SKyle Evans-- Commonly appearing constants 90aedd6be5SKyle Evanscore.KEY_BACKSPACE = 8 91aedd6be5SKyle Evanscore.KEY_ENTER = 13 92aedd6be5SKyle Evanscore.KEY_DELETE = 127 93fe672a15SKyle Evans 94230061c5SKyle Evans-- Note that this is a decimal representation, despite the leading 0 that in 95230061c5SKyle Evans-- other contexts (outside of Lua) may mean 'octal' 96aedd6be5SKyle Evanscore.KEYSTR_ESCAPE = "\027" 972bb86aefSKyle Evanscore.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "[" 9839006570SKyle Evans 99aedd6be5SKyle Evanscore.MENU_RETURN = "return" 100aedd6be5SKyle Evanscore.MENU_ENTRY = "entry" 101aedd6be5SKyle Evanscore.MENU_SEPARATOR = "separator" 102aedd6be5SKyle Evanscore.MENU_SUBMENU = "submenu" 103aedd6be5SKyle Evanscore.MENU_CAROUSEL_ENTRY = "carousel_entry" 104a7cf0562SKyle Evans 10504af4229SKyle Evansfunction core.setVerbose(verbose) 10604af4229SKyle Evans if verbose == nil then 10704af4229SKyle Evans verbose = not core.verbose 108088b4f5fSWarner Losh end 109088b4f5fSWarner Losh 11004af4229SKyle Evans if verbose then 111aedd6be5SKyle Evans loader.setenv("boot_verbose", "YES") 112088b4f5fSWarner Losh else 113aedd6be5SKyle Evans loader.unsetenv("boot_verbose") 114088b4f5fSWarner Losh end 11504af4229SKyle Evans core.verbose = verbose 116088b4f5fSWarner Loshend 117088b4f5fSWarner Losh 11804af4229SKyle Evansfunction core.setSingleUser(single_user) 11904af4229SKyle Evans if single_user == nil then 12004af4229SKyle Evans single_user = not core.su 121088b4f5fSWarner Losh end 122088b4f5fSWarner Losh 12304af4229SKyle Evans if single_user then 124aedd6be5SKyle Evans loader.setenv("boot_single", "YES") 125088b4f5fSWarner Losh else 126aedd6be5SKyle Evans loader.unsetenv("boot_single") 127088b4f5fSWarner Losh end 12804af4229SKyle Evans core.su = single_user 129088b4f5fSWarner Loshend 130088b4f5fSWarner Losh 13104af4229SKyle Evansfunction core.getACPIPresent(checking_system_defaults) 132aedd6be5SKyle Evans local c = loader.getenv("hint.acpi.0.rsdp") 1336401094fSKyle Evans 1349f71d421SKyle Evans if c ~= nil then 13504af4229SKyle Evans if checking_system_defaults then 136aedd6be5SKyle Evans return true 1376401094fSKyle Evans end 1386401094fSKyle Evans -- Otherwise, respect disabled if it's set 139aedd6be5SKyle Evans c = loader.getenv("hint.acpi.0.disabled") 1409f71d421SKyle Evans return c == nil or tonumber(c) ~= 1 1416401094fSKyle Evans end 142aedd6be5SKyle Evans return false 1436401094fSKyle Evansend 1446401094fSKyle Evans 14504af4229SKyle Evansfunction core.setACPI(acpi) 14604af4229SKyle Evans if acpi == nil then 14704af4229SKyle Evans acpi = not core.acpi 148088b4f5fSWarner Losh end 149088b4f5fSWarner Losh 15004af4229SKyle Evans if acpi then 151aedd6be5SKyle Evans loader.setenv("acpi_load", "YES") 152aedd6be5SKyle Evans loader.setenv("hint.acpi.0.disabled", "0") 153aedd6be5SKyle Evans loader.unsetenv("loader.acpi_disabled_by_user") 154088b4f5fSWarner Losh else 155aedd6be5SKyle Evans loader.unsetenv("acpi_load") 156aedd6be5SKyle Evans loader.setenv("hint.acpi.0.disabled", "1") 157aedd6be5SKyle Evans loader.setenv("loader.acpi_disabled_by_user", "1") 158088b4f5fSWarner Losh end 15904af4229SKyle Evans core.acpi = acpi 160088b4f5fSWarner Loshend 161088b4f5fSWarner Losh 16204af4229SKyle Evansfunction core.setSafeMode(safe_mode) 16304af4229SKyle Evans if safe_mode == nil then 16404af4229SKyle Evans safe_mode = not core.sm 165088b4f5fSWarner Losh end 16604af4229SKyle Evans if safe_mode then 167aedd6be5SKyle Evans loader.setenv("kern.smp.disabled", "1") 168aedd6be5SKyle Evans loader.setenv("hw.ata.ata_dma", "0") 169aedd6be5SKyle Evans loader.setenv("hw.ata.atapi_dma", "0") 170aedd6be5SKyle Evans loader.setenv("hw.ata.wc", "0") 171aedd6be5SKyle Evans loader.setenv("hw.eisa_slots", "0") 172aedd6be5SKyle Evans loader.setenv("kern.eventtimer.periodic", "1") 173aedd6be5SKyle Evans loader.setenv("kern.geom.part.check_integrity", "0") 174088b4f5fSWarner Losh else 175aedd6be5SKyle Evans loader.unsetenv("kern.smp.disabled") 176aedd6be5SKyle Evans loader.unsetenv("hw.ata.ata_dma") 177aedd6be5SKyle Evans loader.unsetenv("hw.ata.atapi_dma") 178aedd6be5SKyle Evans loader.unsetenv("hw.ata.wc") 179aedd6be5SKyle Evans loader.unsetenv("hw.eisa_slots") 180aedd6be5SKyle Evans loader.unsetenv("kern.eventtimer.periodic") 181aedd6be5SKyle Evans loader.unsetenv("kern.geom.part.check_integrity") 182088b4f5fSWarner Losh end 18304af4229SKyle Evans core.sm = safe_mode 184088b4f5fSWarner Loshend 185088b4f5fSWarner Losh 186aea262bfSKyle Evansfunction core.clearCachedKernels() 18735b0c718SKyle Evans -- Clear the kernel cache on config changes, autodetect might have 18835b0c718SKyle Evans -- changed or if we've switched boot environments then we could have 18935b0c718SKyle Evans -- a new kernel set. 19035b0c718SKyle Evans core.cached_kernels = nil 19135b0c718SKyle Evansend 19235b0c718SKyle Evans 193088b4f5fSWarner Loshfunction core.kernelList() 19435b0c718SKyle Evans if core.cached_kernels ~= nil then 19535b0c718SKyle Evans return core.cached_kernels 19635b0c718SKyle Evans end 19735b0c718SKyle Evans 198aedd6be5SKyle Evans local k = loader.getenv("kernel") 1993f4eb56bSKyle Evans local v = loader.getenv("kernels") 2003889e6cdSKyle Evans local autodetect = loader.getenv("kernels_autodetect") or "" 201088b4f5fSWarner Losh 202aedd6be5SKyle Evans local kernels = {} 203aedd6be5SKyle Evans local unique = {} 204aedd6be5SKyle Evans local i = 0 2059f71d421SKyle Evans if k ~= nil then 206aedd6be5SKyle Evans i = i + 1 207aedd6be5SKyle Evans kernels[i] = k 208aedd6be5SKyle Evans unique[k] = true 209088b4f5fSWarner Losh end 210088b4f5fSWarner Losh 2113f4eb56bSKyle Evans if v ~= nil then 212a5003419SKyle Evans for n in v:gmatch("([^;, ]+)[;, ]?") do 2139f71d421SKyle Evans if unique[n] == nil then 214aedd6be5SKyle Evans i = i + 1 215aedd6be5SKyle Evans kernels[i] = n 216aedd6be5SKyle Evans unique[n] = true 217088b4f5fSWarner Losh end 218088b4f5fSWarner Losh end 2193889e6cdSKyle Evans end 220a108046fSConrad Meyer 2213889e6cdSKyle Evans -- Base whether we autodetect kernels or not on a loader.conf(5) 2223889e6cdSKyle Evans -- setting, kernels_autodetect. If it's set to 'yes', we'll add 2233889e6cdSKyle Evans -- any kernels we detect based on the criteria described. 2243889e6cdSKyle Evans if autodetect:lower() ~= "yes" then 22535b0c718SKyle Evans core.cached_kernels = kernels 22635b0c718SKyle Evans return core.cached_kernels 2273f4eb56bSKyle Evans end 2283f4eb56bSKyle Evans 229a108046fSConrad Meyer -- Automatically detect other bootable kernel directories using a 230a108046fSConrad Meyer -- heuristic. Any directory in /boot that contains an ordinary file 231a108046fSConrad Meyer -- named "kernel" is considered eligible. 232a108046fSConrad Meyer for file in lfs.dir("/boot") do 233aedd6be5SKyle Evans local fname = "/boot/" .. file 234a108046fSConrad Meyer 2359f71d421SKyle Evans if file == "." or file == ".." then 236aedd6be5SKyle Evans goto continue 237a108046fSConrad Meyer end 238a108046fSConrad Meyer 2399f71d421SKyle Evans if lfs.attributes(fname, "mode") ~= "directory" then 240aedd6be5SKyle Evans goto continue 241a108046fSConrad Meyer end 242a108046fSConrad Meyer 2439f71d421SKyle Evans if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 244aedd6be5SKyle Evans goto continue 245a108046fSConrad Meyer end 246a108046fSConrad Meyer 2479f71d421SKyle Evans if unique[file] == nil then 248aedd6be5SKyle Evans i = i + 1 249aedd6be5SKyle Evans kernels[i] = file 250aedd6be5SKyle Evans unique[file] = true 251a108046fSConrad Meyer end 252a108046fSConrad Meyer 253a108046fSConrad Meyer ::continue:: 254a108046fSConrad Meyer end 25535b0c718SKyle Evans core.cached_kernels = kernels 25635b0c718SKyle Evans return core.cached_kernels 257088b4f5fSWarner Loshend 258088b4f5fSWarner Losh 2597efc058fSKyle Evansfunction core.bootenvDefault() 2607efc058fSKyle Evans return loader.getenv("zfs_be_active") 2617efc058fSKyle Evansend 2627efc058fSKyle Evans 2637efc058fSKyle Evansfunction core.bootenvList() 2647efc058fSKyle Evans local bootenv_count = tonumber(loader.getenv("bootenvs_count")) 2657efc058fSKyle Evans local bootenvs = {} 2667efc058fSKyle Evans local curenv 2677efc058fSKyle Evans local envcount = 0 2687efc058fSKyle Evans local unique = {} 2697efc058fSKyle Evans 2707efc058fSKyle Evans if bootenv_count == nil or bootenv_count <= 0 then 2717efc058fSKyle Evans return bootenvs 2727efc058fSKyle Evans end 2737efc058fSKyle Evans 2747efc058fSKyle Evans -- Currently selected bootenv is always first/default 2757efc058fSKyle Evans curenv = core.bootenvDefault() 2767efc058fSKyle Evans if curenv ~= nil then 2777efc058fSKyle Evans envcount = envcount + 1 2787efc058fSKyle Evans bootenvs[envcount] = curenv 2797efc058fSKyle Evans unique[curenv] = true 2807efc058fSKyle Evans end 2817efc058fSKyle Evans 2827efc058fSKyle Evans for curenv_idx = 0, bootenv_count - 1 do 2837efc058fSKyle Evans curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]") 2847efc058fSKyle Evans if curenv ~= nil and unique[curenv] == nil then 2857efc058fSKyle Evans envcount = envcount + 1 2867efc058fSKyle Evans bootenvs[envcount] = curenv 2877efc058fSKyle Evans unique[curenv] = true 2887efc058fSKyle Evans end 2897efc058fSKyle Evans end 2907efc058fSKyle Evans return bootenvs 2917efc058fSKyle Evansend 2927efc058fSKyle Evans 293088b4f5fSWarner Loshfunction core.setDefaults() 294aedd6be5SKyle Evans core.setACPI(core.getACPIPresent(true)) 295*1613f091SKyle Evans core.setSafeMode(default_safe_mode) 296*1613f091SKyle Evans core.setSingleUser(default_single_user) 297*1613f091SKyle Evans core.setVerbose(default_verbose) 298088b4f5fSWarner Loshend 299088b4f5fSWarner Losh 3006d4ed94dSKyle Evansfunction core.autoboot(argstr) 301a2a7830eSKyle Evans -- loadelf() only if we've not already loaded a kernel 302a2a7830eSKyle Evans if loader.getenv("kernelname") == nil then 303aedd6be5SKyle Evans config.loadelf() 304a2a7830eSKyle Evans end 305322a2dddSKyle Evans loader.perform(composeLoaderCmd("autoboot", argstr)) 306088b4f5fSWarner Loshend 307088b4f5fSWarner Losh 3086d4ed94dSKyle Evansfunction core.boot(argstr) 309a2a7830eSKyle Evans -- loadelf() only if we've not already loaded a kernel 310a2a7830eSKyle Evans if loader.getenv("kernelname") == nil then 311aedd6be5SKyle Evans config.loadelf() 312a2a7830eSKyle Evans end 313322a2dddSKyle Evans loader.perform(composeLoaderCmd("boot", argstr)) 314088b4f5fSWarner Loshend 315088b4f5fSWarner Losh 316e07fc39cSKyle Evansfunction core.isSingleUserBoot() 317aedd6be5SKyle Evans local single_user = loader.getenv("boot_single") 318aedd6be5SKyle Evans return single_user ~= nil and single_user:lower() == "yes" 319e07fc39cSKyle Evansend 320e07fc39cSKyle Evans 3215f8cfbe1SKyle Evansfunction core.isUEFIBoot() 3225f8cfbe1SKyle Evans local efiver = loader.getenv("efi-version") 3235f8cfbe1SKyle Evans 3245f8cfbe1SKyle Evans return efiver ~= nil 3255f8cfbe1SKyle Evansend 3265f8cfbe1SKyle Evans 3277efc058fSKyle Evansfunction core.isZFSBoot() 3287efc058fSKyle Evans local c = loader.getenv("currdev") 3297efc058fSKyle Evans 3307efc058fSKyle Evans if c ~= nil then 3317efc058fSKyle Evans return c:match("^zfs:") ~= nil 3327efc058fSKyle Evans end 3337efc058fSKyle Evans return false 3347efc058fSKyle Evansend 3357efc058fSKyle Evans 336b140d14bSKyle Evansfunction core.isSerialBoot() 337aedd6be5SKyle Evans local s = loader.getenv("boot_serial") 3389f71d421SKyle Evans if s ~= nil then 339aedd6be5SKyle Evans return true 340088b4f5fSWarner Losh end 341088b4f5fSWarner Losh 342aedd6be5SKyle Evans local m = loader.getenv("boot_multicons") 3439f71d421SKyle Evans if m ~= nil then 344aedd6be5SKyle Evans return true 345088b4f5fSWarner Losh end 346aedd6be5SKyle Evans return false 347088b4f5fSWarner Loshend 348088b4f5fSWarner Losh 349c1ab36f5SKyle Evansfunction core.isSystem386() 3509f71d421SKyle Evans return loader.machine_arch == "i386" 351c1ab36f5SKyle Evansend 352c1ab36f5SKyle Evans 3539937e979SKyle Evans-- Is the menu skipped in the environment in which we've booted? 3549937e979SKyle Evansfunction core.isMenuSkipped() 355b83a355dSKyle Evans return string.lower(loader.getenv("beastie_disable") or "") == "yes" 3569937e979SKyle Evansend 3579937e979SKyle Evans 3585c1b5165SKyle Evans-- This may be a better candidate for a 'utility' module. 359ee4e69f1SKyle Evansfunction core.deepCopyTable(tbl) 360aedd6be5SKyle Evans local new_tbl = {} 3615c1b5165SKyle Evans for k, v in pairs(tbl) do 3629f71d421SKyle Evans if type(v) == "table" then 363ee4e69f1SKyle Evans new_tbl[k] = core.deepCopyTable(v) 3645c1b5165SKyle Evans else 365aedd6be5SKyle Evans new_tbl[k] = v 3665c1b5165SKyle Evans end 3675c1b5165SKyle Evans end 368aedd6be5SKyle Evans return new_tbl 3695c1b5165SKyle Evansend 3705c1b5165SKyle Evans 3716d4ed94dSKyle Evans-- XXX This should go away if we get the table lib into shape for importing. 3726d4ed94dSKyle Evans-- As of now, it requires some 'os' functions, so we'll implement this in lua 3736d4ed94dSKyle Evans-- for our uses 3746d4ed94dSKyle Evansfunction core.popFrontTable(tbl) 3756d4ed94dSKyle Evans -- Shouldn't reasonably happen 3769f71d421SKyle Evans if #tbl == 0 then 377aedd6be5SKyle Evans return nil, nil 3789f71d421SKyle Evans elseif #tbl == 1 then 379aedd6be5SKyle Evans return tbl[1], {} 3806d4ed94dSKyle Evans end 3816d4ed94dSKyle Evans 382aedd6be5SKyle Evans local first_value = tbl[1] 383aedd6be5SKyle Evans local new_tbl = {} 3846d4ed94dSKyle Evans -- This is not a cheap operation 3856d4ed94dSKyle Evans for k, v in ipairs(tbl) do 3869f71d421SKyle Evans if k > 1 then 387aedd6be5SKyle Evans new_tbl[k - 1] = v 3886d4ed94dSKyle Evans end 3896d4ed94dSKyle Evans end 3906d4ed94dSKyle Evans 391aedd6be5SKyle Evans return first_value, new_tbl 3926d4ed94dSKyle Evansend 3936d4ed94dSKyle Evans 394*1613f091SKyle EvansrecordDefaults() 395aea262bfSKyle Evanshook.register("config.reloaded", core.clearCachedKernels) 396aedd6be5SKyle Evansreturn core 397