xref: /freebsd/stand/lua/core.lua (revision e183039f0882009c455c3b59fe1ab58a4fd25a5e)
1088b4f5fSWarner Losh--
24d846d26SWarner Losh-- SPDX-License-Identifier: BSD-2-Clause
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
30011eae6cSKyle Evanslocal config = require("config")
31aea262bfSKyle Evanslocal hook = require("hook")
32fa4a2394SKyle Evans
33aedd6be5SKyle Evanslocal core = {}
34088b4f5fSWarner Losh
35e0f3dc82SR. Christian McDonaldlocal default_acpi = false
361613f091SKyle Evanslocal default_safe_mode = false
371613f091SKyle Evanslocal default_single_user = false
381613f091SKyle Evanslocal default_verbose = false
391613f091SKyle Evans
40277f38abSMariusz Zaborskilocal bootenv_list = "bootenvs"
41277f38abSMariusz Zaborski
42322a2dddSKyle Evanslocal function composeLoaderCmd(cmd_name, argstr)
439f71d421SKyle Evans	if argstr ~= nil then
44aedd6be5SKyle Evans		cmd_name = cmd_name .. " " .. argstr
456d4ed94dSKyle Evans	end
46aedd6be5SKyle Evans	return cmd_name
476d4ed94dSKyle Evansend
486d4ed94dSKyle Evans
491613f091SKyle Evanslocal function recordDefaults()
501613f091SKyle Evans	local boot_single = loader.getenv("boot_single") or "no"
511613f091SKyle Evans	local boot_verbose = loader.getenv("boot_verbose") or "no"
52e0f3dc82SR. Christian McDonald
53e0f3dc82SR. Christian McDonald	default_acpi = core.getACPI()
541613f091SKyle Evans	default_single_user = boot_single:lower() ~= "no"
551613f091SKyle Evans	default_verbose = boot_verbose:lower() ~= "no"
561613f091SKyle Evans
57e0f3dc82SR. Christian McDonald	core.setACPI(default_acpi)
581613f091SKyle Evans	core.setSingleUser(default_single_user)
591613f091SKyle Evans	core.setVerbose(default_verbose)
601613f091SKyle Evansend
611613f091SKyle Evans
621613f091SKyle Evans
6307faaf78SKyle Evans-- Globals
645beb5507SKyle Evans-- try_include will return the loaded module on success, or false and the error
655beb5507SKyle Evans-- message on failure.
6607faaf78SKyle Evansfunction try_include(module)
67bac5966eSKyle Evans	if module:sub(1, 1) ~= "/" then
68366f9979SKyle Evans		local lua_path = loader.lua_path
69bac5966eSKyle Evans		-- XXX Temporary compat shim; this should be removed once the
70bac5966eSKyle Evans		-- loader.lua_path export has sufficiently spread.
71bac5966eSKyle Evans		if lua_path == nil then
72bac5966eSKyle Evans			lua_path = "/boot/lua"
7307faaf78SKyle Evans		end
74bac5966eSKyle Evans		module = lua_path .. "/" .. module
75bac5966eSKyle Evans		-- We only attempt to append an extension if an absolute path
76bac5966eSKyle Evans		-- wasn't specified.  This assumes that the caller either wants
77bac5966eSKyle Evans		-- to treat this like it would require() and specify just the
78bac5966eSKyle Evans		-- base filename, or they know what they're doing as they've
79bac5966eSKyle Evans		-- specified an absolute path and we shouldn't impede.
80bac5966eSKyle Evans		if module:match(".lua$") == nil then
81bac5966eSKyle Evans			module = module .. ".lua"
82bac5966eSKyle Evans		end
83bac5966eSKyle Evans	end
84bac5966eSKyle Evans	if lfs.attributes(module, "mode") ~= "file" then
85bac5966eSKyle Evans		return
86bac5966eSKyle Evans	end
87bac5966eSKyle Evans
88bac5966eSKyle Evans	return dofile(module)
8907faaf78SKyle Evansend
9007faaf78SKyle Evans
91b5746545SKyle Evans-- Module exports
92fe672a15SKyle Evans-- Commonly appearing constants
93aedd6be5SKyle Evanscore.KEY_BACKSPACE	= 8
94aedd6be5SKyle Evanscore.KEY_ENTER		= 13
95aedd6be5SKyle Evanscore.KEY_DELETE		= 127
96fe672a15SKyle Evans
97230061c5SKyle Evans-- Note that this is a decimal representation, despite the leading 0 that in
98230061c5SKyle Evans-- other contexts (outside of Lua) may mean 'octal'
99aedd6be5SKyle Evanscore.KEYSTR_ESCAPE	= "\027"
1002bb86aefSKyle Evanscore.KEYSTR_CSI		= core.KEYSTR_ESCAPE .. "["
10173531a2aSRyan Moellercore.KEYSTR_RESET	= core.KEYSTR_ESCAPE .. "c"
10239006570SKyle Evans
103aedd6be5SKyle Evanscore.MENU_RETURN	= "return"
104aedd6be5SKyle Evanscore.MENU_ENTRY		= "entry"
105aedd6be5SKyle Evanscore.MENU_SEPARATOR	= "separator"
106aedd6be5SKyle Evanscore.MENU_SUBMENU	= "submenu"
107aedd6be5SKyle Evanscore.MENU_CAROUSEL_ENTRY	= "carousel_entry"
108a7cf0562SKyle Evans
10904af4229SKyle Evansfunction core.setVerbose(verbose)
11004af4229SKyle Evans	if verbose == nil then
11104af4229SKyle Evans		verbose = not core.verbose
112088b4f5fSWarner Losh	end
113088b4f5fSWarner Losh
11404af4229SKyle Evans	if verbose then
115aedd6be5SKyle Evans		loader.setenv("boot_verbose", "YES")
116088b4f5fSWarner Losh	else
117aedd6be5SKyle Evans		loader.unsetenv("boot_verbose")
118088b4f5fSWarner Losh	end
11904af4229SKyle Evans	core.verbose = verbose
120088b4f5fSWarner Loshend
121088b4f5fSWarner Losh
12204af4229SKyle Evansfunction core.setSingleUser(single_user)
12304af4229SKyle Evans	if single_user == nil then
12404af4229SKyle Evans		single_user = not core.su
125088b4f5fSWarner Losh	end
126088b4f5fSWarner Losh
12704af4229SKyle Evans	if single_user then
128aedd6be5SKyle Evans		loader.setenv("boot_single", "YES")
129088b4f5fSWarner Losh	else
130aedd6be5SKyle Evans		loader.unsetenv("boot_single")
131088b4f5fSWarner Losh	end
13204af4229SKyle Evans	core.su = single_user
133088b4f5fSWarner Loshend
134088b4f5fSWarner Losh
135e0f3dc82SR. Christian McDonaldfunction core.hasACPI()
136*e183039fSKyle Evans	-- We can't trust acpi.rsdp to be set if the loader binary doesn't do
137*e183039fSKyle Evans	-- ACPI detection early enough.  UEFI loader historically didn't, so
138*e183039fSKyle Evans	-- we'll fallback to assuming ACPI is enabled if this binary does not
139*e183039fSKyle Evans	-- declare that it probes for ACPI early enough
140*e183039fSKyle Evans	if loader.getenv("acpi.rsdp") ~= nil then
141*e183039fSKyle Evans		return true
142e0f3dc82SR. Christian McDonald	end
1436401094fSKyle Evans
144*e183039fSKyle Evans	return not core.hasFeature("EARLY_ACPI")
1450abe05aeSWarner Loshend
1460abe05aeSWarner Losh
147e0f3dc82SR. Christian McDonaldfunction core.getACPI()
148e0f3dc82SR. Christian McDonald	if not core.hasACPI() then
149*e183039fSKyle Evans		return false
1506401094fSKyle Evans	end
1516401094fSKyle Evans
152e0f3dc82SR. Christian McDonald	-- Otherwise, respect disabled if it's set
153e0f3dc82SR. Christian McDonald	local c = loader.getenv("hint.acpi.0.disabled")
154e0f3dc82SR. Christian McDonald	return c == nil or tonumber(c) ~= 1
155e0f3dc82SR. Christian McDonaldend
156e0f3dc82SR. Christian McDonald
15704af4229SKyle Evansfunction core.setACPI(acpi)
15804af4229SKyle Evans	if acpi == nil then
15904af4229SKyle Evans		acpi = not core.acpi
160088b4f5fSWarner Losh	end
161088b4f5fSWarner Losh
16204af4229SKyle Evans	if acpi then
163*e183039fSKyle Evans		config.enableModule("acpi")
164aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "0")
165088b4f5fSWarner Losh	else
166*e183039fSKyle Evans		config.disableModule("acpi")
167aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "1")
168088b4f5fSWarner Losh	end
16904af4229SKyle Evans	core.acpi = acpi
170088b4f5fSWarner Loshend
171088b4f5fSWarner Losh
17204af4229SKyle Evansfunction core.setSafeMode(safe_mode)
17304af4229SKyle Evans	if safe_mode == nil then
17404af4229SKyle Evans		safe_mode = not core.sm
175088b4f5fSWarner Losh	end
17604af4229SKyle Evans	if safe_mode then
177aedd6be5SKyle Evans		loader.setenv("kern.smp.disabled", "1")
178aedd6be5SKyle Evans		loader.setenv("hw.ata.ata_dma", "0")
179aedd6be5SKyle Evans		loader.setenv("hw.ata.atapi_dma", "0")
180aedd6be5SKyle Evans		loader.setenv("kern.eventtimer.periodic", "1")
181aedd6be5SKyle Evans		loader.setenv("kern.geom.part.check_integrity", "0")
182088b4f5fSWarner Losh	else
183aedd6be5SKyle Evans		loader.unsetenv("kern.smp.disabled")
184aedd6be5SKyle Evans		loader.unsetenv("hw.ata.ata_dma")
185aedd6be5SKyle Evans		loader.unsetenv("hw.ata.atapi_dma")
186aedd6be5SKyle Evans		loader.unsetenv("kern.eventtimer.periodic")
187aedd6be5SKyle Evans		loader.unsetenv("kern.geom.part.check_integrity")
188088b4f5fSWarner Losh	end
18904af4229SKyle Evans	core.sm = safe_mode
190088b4f5fSWarner Loshend
191088b4f5fSWarner Losh
192aea262bfSKyle Evansfunction core.clearCachedKernels()
19335b0c718SKyle Evans	-- Clear the kernel cache on config changes, autodetect might have
19435b0c718SKyle Evans	-- changed or if we've switched boot environments then we could have
19535b0c718SKyle Evans	-- a new kernel set.
19635b0c718SKyle Evans	core.cached_kernels = nil
19735b0c718SKyle Evansend
19835b0c718SKyle Evans
199088b4f5fSWarner Loshfunction core.kernelList()
20035b0c718SKyle Evans	if core.cached_kernels ~= nil then
20135b0c718SKyle Evans		return core.cached_kernels
20235b0c718SKyle Evans	end
20335b0c718SKyle Evans
204aedd6be5SKyle Evans	local k = loader.getenv("kernel")
2053f4eb56bSKyle Evans	local v = loader.getenv("kernels")
2063889e6cdSKyle Evans	local autodetect = loader.getenv("kernels_autodetect") or ""
207088b4f5fSWarner Losh
208aedd6be5SKyle Evans	local kernels = {}
209aedd6be5SKyle Evans	local unique = {}
210aedd6be5SKyle Evans	local i = 0
2119f71d421SKyle Evans	if k ~= nil then
212aedd6be5SKyle Evans		i = i + 1
213aedd6be5SKyle Evans		kernels[i] = k
214aedd6be5SKyle Evans		unique[k] = true
215088b4f5fSWarner Losh	end
216088b4f5fSWarner Losh
2173f4eb56bSKyle Evans	if v ~= nil then
218a5003419SKyle Evans		for n in v:gmatch("([^;, ]+)[;, ]?") do
2199f71d421SKyle Evans			if unique[n] == nil then
220aedd6be5SKyle Evans				i = i + 1
221aedd6be5SKyle Evans				kernels[i] = n
222aedd6be5SKyle Evans				unique[n] = true
223088b4f5fSWarner Losh			end
224088b4f5fSWarner Losh		end
2253889e6cdSKyle Evans	end
226a108046fSConrad Meyer
227c4dc9072SEmmanuel Vadot	-- Do not attempt to autodetect if underlying filesystem
228c4dc9072SEmmanuel Vadot	-- do not support directory listing (e.g. tftp, http)
229c4dc9072SEmmanuel Vadot	if not lfs.attributes("/boot", "mode") then
230c4dc9072SEmmanuel Vadot		autodetect = "no"
231c4dc9072SEmmanuel Vadot		loader.setenv("kernels_autodetect", "NO")
232c4dc9072SEmmanuel Vadot	end
233c4dc9072SEmmanuel Vadot
2343889e6cdSKyle Evans	-- Base whether we autodetect kernels or not on a loader.conf(5)
2353889e6cdSKyle Evans	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
2363889e6cdSKyle Evans	-- any kernels we detect based on the criteria described.
2373889e6cdSKyle Evans	if autodetect:lower() ~= "yes" then
23835b0c718SKyle Evans		core.cached_kernels = kernels
23935b0c718SKyle Evans		return core.cached_kernels
2403f4eb56bSKyle Evans	end
2413f4eb56bSKyle Evans
242a108046fSConrad Meyer	-- Automatically detect other bootable kernel directories using a
243a108046fSConrad Meyer	-- heuristic.  Any directory in /boot that contains an ordinary file
244a108046fSConrad Meyer	-- named "kernel" is considered eligible.
245e25ee296SKyle Evans	for file, ftype in lfs.dir("/boot") do
246aedd6be5SKyle Evans		local fname = "/boot/" .. file
247a108046fSConrad Meyer
2489f71d421SKyle Evans		if file == "." or file == ".." then
249aedd6be5SKyle Evans			goto continue
250a108046fSConrad Meyer		end
251a108046fSConrad Meyer
252e25ee296SKyle Evans		if ftype then
253e25ee296SKyle Evans			if ftype ~= lfs.DT_DIR then
254e25ee296SKyle Evans				goto continue
255e25ee296SKyle Evans			end
256e25ee296SKyle Evans		elseif lfs.attributes(fname, "mode") ~= "directory" then
257aedd6be5SKyle Evans			goto continue
258a108046fSConrad Meyer		end
259a108046fSConrad Meyer
2609f71d421SKyle Evans		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
261aedd6be5SKyle Evans			goto continue
262a108046fSConrad Meyer		end
263a108046fSConrad Meyer
2649f71d421SKyle Evans		if unique[file] == nil then
265aedd6be5SKyle Evans			i = i + 1
266aedd6be5SKyle Evans			kernels[i] = file
267aedd6be5SKyle Evans			unique[file] = true
268a108046fSConrad Meyer		end
269a108046fSConrad Meyer
270a108046fSConrad Meyer		::continue::
271a108046fSConrad Meyer	end
27235b0c718SKyle Evans	core.cached_kernels = kernels
27335b0c718SKyle Evans	return core.cached_kernels
274088b4f5fSWarner Loshend
275088b4f5fSWarner Losh
2767efc058fSKyle Evansfunction core.bootenvDefault()
2777efc058fSKyle Evans	return loader.getenv("zfs_be_active")
2787efc058fSKyle Evansend
2797efc058fSKyle Evans
2807efc058fSKyle Evansfunction core.bootenvList()
281277f38abSMariusz Zaborski	local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count"))
2827efc058fSKyle Evans	local bootenvs = {}
2837efc058fSKyle Evans	local curenv
2847efc058fSKyle Evans	local envcount = 0
2857efc058fSKyle Evans	local unique = {}
2867efc058fSKyle Evans
2877efc058fSKyle Evans	if bootenv_count == nil or bootenv_count <= 0 then
2887efc058fSKyle Evans		return bootenvs
2897efc058fSKyle Evans	end
2907efc058fSKyle Evans
2917efc058fSKyle Evans	-- Currently selected bootenv is always first/default
292277f38abSMariusz Zaborski	-- On the rewinded list the bootenv may not exists
293277f38abSMariusz Zaborski	if core.isRewinded() then
294277f38abSMariusz Zaborski		curenv = core.bootenvDefaultRewinded()
295277f38abSMariusz Zaborski	else
2967efc058fSKyle Evans		curenv = core.bootenvDefault()
297277f38abSMariusz Zaborski	end
2987efc058fSKyle Evans	if curenv ~= nil then
2997efc058fSKyle Evans		envcount = envcount + 1
3007efc058fSKyle Evans		bootenvs[envcount] = curenv
3017efc058fSKyle Evans		unique[curenv] = true
3027efc058fSKyle Evans	end
3037efc058fSKyle Evans
3047efc058fSKyle Evans	for curenv_idx = 0, bootenv_count - 1 do
305277f38abSMariusz Zaborski		curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]")
3067efc058fSKyle Evans		if curenv ~= nil and unique[curenv] == nil then
3077efc058fSKyle Evans			envcount = envcount + 1
3087efc058fSKyle Evans			bootenvs[envcount] = curenv
3097efc058fSKyle Evans			unique[curenv] = true
3107efc058fSKyle Evans		end
3117efc058fSKyle Evans	end
3127efc058fSKyle Evans	return bootenvs
3137efc058fSKyle Evansend
3147efc058fSKyle Evans
315277f38abSMariusz Zaborskifunction core.isCheckpointed()
316277f38abSMariusz Zaborski	return loader.getenv("zpool_checkpoint") ~= nil
317277f38abSMariusz Zaborskiend
318277f38abSMariusz Zaborski
319277f38abSMariusz Zaborskifunction core.bootenvDefaultRewinded()
320277f38abSMariusz Zaborski	local defname =  "zfs:!" .. string.sub(core.bootenvDefault(), 5)
321277f38abSMariusz Zaborski	local bootenv_count = tonumber("bootenvs_check_count")
322277f38abSMariusz Zaborski
323277f38abSMariusz Zaborski	if bootenv_count == nil or bootenv_count <= 0 then
324277f38abSMariusz Zaborski		return defname
325277f38abSMariusz Zaborski	end
326277f38abSMariusz Zaborski
327277f38abSMariusz Zaborski	for curenv_idx = 0, bootenv_count - 1 do
32894510c29SKyle Evans		local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]")
329277f38abSMariusz Zaborski		if curenv == defname then
330277f38abSMariusz Zaborski			return defname
331277f38abSMariusz Zaborski		end
332277f38abSMariusz Zaborski	end
333277f38abSMariusz Zaborski
334277f38abSMariusz Zaborski	return loader.getenv("bootenvs_check[0]")
335277f38abSMariusz Zaborskiend
336277f38abSMariusz Zaborski
337277f38abSMariusz Zaborskifunction core.isRewinded()
338277f38abSMariusz Zaborski	return bootenv_list == "bootenvs_check"
339277f38abSMariusz Zaborskiend
340277f38abSMariusz Zaborski
341277f38abSMariusz Zaborskifunction core.changeRewindCheckpoint()
342277f38abSMariusz Zaborski	if core.isRewinded() then
343277f38abSMariusz Zaborski		bootenv_list = "bootenvs"
344277f38abSMariusz Zaborski	else
345277f38abSMariusz Zaborski		bootenv_list = "bootenvs_check"
346277f38abSMariusz Zaborski	end
347277f38abSMariusz Zaborskiend
348277f38abSMariusz Zaborski
3495c73b3e0SColin Percivalfunction core.loadEntropy()
3505c73b3e0SColin Percival	if core.isUEFIBoot() then
3515c73b3e0SColin Percival		if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then
3525c73b3e0SColin Percival			loader.perform("efi-seed-entropy")
3535c73b3e0SColin Percival		end
3545c73b3e0SColin Percival	end
3555c73b3e0SColin Percivalend
3565c73b3e0SColin Percival
357088b4f5fSWarner Loshfunction core.setDefaults()
358e0f3dc82SR. Christian McDonald	core.setACPI(default_acpi)
3591613f091SKyle Evans	core.setSafeMode(default_safe_mode)
3601613f091SKyle Evans	core.setSingleUser(default_single_user)
3611613f091SKyle Evans	core.setVerbose(default_verbose)
362088b4f5fSWarner Loshend
363088b4f5fSWarner Losh
3646d4ed94dSKyle Evansfunction core.autoboot(argstr)
365a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
366a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
367aedd6be5SKyle Evans		config.loadelf()
368a2a7830eSKyle Evans	end
3695c73b3e0SColin Percival	core.loadEntropy()
370322a2dddSKyle Evans	loader.perform(composeLoaderCmd("autoboot", argstr))
371088b4f5fSWarner Loshend
372088b4f5fSWarner Losh
3736d4ed94dSKyle Evansfunction core.boot(argstr)
374a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
375a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
376aedd6be5SKyle Evans		config.loadelf()
377a2a7830eSKyle Evans	end
3785c73b3e0SColin Percival	core.loadEntropy()
379322a2dddSKyle Evans	loader.perform(composeLoaderCmd("boot", argstr))
380088b4f5fSWarner Loshend
381088b4f5fSWarner Losh
3821631382cSKyle Evansfunction core.hasFeature(name)
3831631382cSKyle Evans	if not loader.has_feature then
3841631382cSKyle Evans		-- Loader too old, no feature support
3851631382cSKyle Evans		return nil, "No feature support in loaded loader"
3861631382cSKyle Evans	end
3871631382cSKyle Evans
3881631382cSKyle Evans	return loader.has_feature(name)
3891631382cSKyle Evansend
3901631382cSKyle Evans
391e07fc39cSKyle Evansfunction core.isSingleUserBoot()
392aedd6be5SKyle Evans	local single_user = loader.getenv("boot_single")
393aedd6be5SKyle Evans	return single_user ~= nil and single_user:lower() == "yes"
394e07fc39cSKyle Evansend
395e07fc39cSKyle Evans
3965f8cfbe1SKyle Evansfunction core.isUEFIBoot()
3975f8cfbe1SKyle Evans	local efiver = loader.getenv("efi-version")
3985f8cfbe1SKyle Evans
3995f8cfbe1SKyle Evans	return efiver ~= nil
4005f8cfbe1SKyle Evansend
4015f8cfbe1SKyle Evans
4027efc058fSKyle Evansfunction core.isZFSBoot()
4037efc058fSKyle Evans	local c = loader.getenv("currdev")
4047efc058fSKyle Evans
4057efc058fSKyle Evans	if c ~= nil then
4067efc058fSKyle Evans		return c:match("^zfs:") ~= nil
4077efc058fSKyle Evans	end
4087efc058fSKyle Evans	return false
4097efc058fSKyle Evansend
4107efc058fSKyle Evans
4113630506bSToomas Soomefunction core.isFramebufferConsole()
4123630506bSToomas Soome	local c = loader.getenv("console")
4133630506bSToomas Soome	if c ~= nil then
4143630506bSToomas Soome		if c:find("efi") == nil and c:find("vidconsole") == nil then
4153630506bSToomas Soome			return false
4163630506bSToomas Soome		end
4173630506bSToomas Soome		if loader.getenv("screen.depth") ~= nil then
4183630506bSToomas Soome			return true
4193630506bSToomas Soome		end
4203630506bSToomas Soome	end
4213630506bSToomas Soome	return false
4223630506bSToomas Soomeend
4233630506bSToomas Soome
42490a25417SKyle Evansfunction core.isSerialConsole()
42590a25417SKyle Evans	local c = loader.getenv("console")
42690a25417SKyle Evans	if c ~= nil then
4275d8c062fSToomas Soome		-- serial console is comconsole, but also userboot.
4285d8c062fSToomas Soome		-- userboot is there, because we have no way to know
4295d8c062fSToomas Soome		-- if the user terminal can draw unicode box chars or not.
4305d8c062fSToomas Soome		if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then
43190a25417SKyle Evans			return true
43290a25417SKyle Evans		end
43390a25417SKyle Evans	end
43490a25417SKyle Evans	return false
43590a25417SKyle Evansend
43690a25417SKyle Evans
437b140d14bSKyle Evansfunction core.isSerialBoot()
438aedd6be5SKyle Evans	local s = loader.getenv("boot_serial")
4399f71d421SKyle Evans	if s ~= nil then
440aedd6be5SKyle Evans		return true
441088b4f5fSWarner Losh	end
442088b4f5fSWarner Losh
443aedd6be5SKyle Evans	local m = loader.getenv("boot_multicons")
4449f71d421SKyle Evans	if m ~= nil then
445aedd6be5SKyle Evans		return true
446088b4f5fSWarner Losh	end
447aedd6be5SKyle Evans	return false
448088b4f5fSWarner Loshend
449088b4f5fSWarner Losh
4509937e979SKyle Evans-- Is the menu skipped in the environment in which we've booted?
4519937e979SKyle Evansfunction core.isMenuSkipped()
452b83a355dSKyle Evans	return string.lower(loader.getenv("beastie_disable") or "") == "yes"
4539937e979SKyle Evansend
4549937e979SKyle Evans
4555c1b5165SKyle Evans-- This may be a better candidate for a 'utility' module.
456ee4e69f1SKyle Evansfunction core.deepCopyTable(tbl)
457aedd6be5SKyle Evans	local new_tbl = {}
4585c1b5165SKyle Evans	for k, v in pairs(tbl) do
4599f71d421SKyle Evans		if type(v) == "table" then
460ee4e69f1SKyle Evans			new_tbl[k] = core.deepCopyTable(v)
4615c1b5165SKyle Evans		else
462aedd6be5SKyle Evans			new_tbl[k] = v
4635c1b5165SKyle Evans		end
4645c1b5165SKyle Evans	end
465aedd6be5SKyle Evans	return new_tbl
4665c1b5165SKyle Evansend
4675c1b5165SKyle Evans
4686d4ed94dSKyle Evans-- XXX This should go away if we get the table lib into shape for importing.
4696d4ed94dSKyle Evans-- As of now, it requires some 'os' functions, so we'll implement this in lua
4706d4ed94dSKyle Evans-- for our uses
4716d4ed94dSKyle Evansfunction core.popFrontTable(tbl)
4726d4ed94dSKyle Evans	-- Shouldn't reasonably happen
4739f71d421SKyle Evans	if #tbl == 0 then
474aedd6be5SKyle Evans		return nil, nil
4759f71d421SKyle Evans	elseif #tbl == 1 then
476aedd6be5SKyle Evans		return tbl[1], {}
4776d4ed94dSKyle Evans	end
4786d4ed94dSKyle Evans
479aedd6be5SKyle Evans	local first_value = tbl[1]
480aedd6be5SKyle Evans	local new_tbl = {}
4816d4ed94dSKyle Evans	-- This is not a cheap operation
4826d4ed94dSKyle Evans	for k, v in ipairs(tbl) do
4839f71d421SKyle Evans		if k > 1 then
484aedd6be5SKyle Evans			new_tbl[k - 1] = v
4856d4ed94dSKyle Evans		end
4866d4ed94dSKyle Evans	end
4876d4ed94dSKyle Evans
488aedd6be5SKyle Evans	return first_value, new_tbl
4896d4ed94dSKyle Evansend
4906d4ed94dSKyle Evans
4918f3b3610SWarner Loshfunction core.getConsoleName()
4928f3b3610SWarner Losh	if loader.getenv("boot_multicons") ~= nil then
4938f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4948f3b3610SWarner Losh			return "Dual (Serial primary)"
4958f3b3610SWarner Losh		else
4968f3b3610SWarner Losh			return "Dual (Video primary)"
4978f3b3610SWarner Losh		end
4988f3b3610SWarner Losh	else
4998f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
5008f3b3610SWarner Losh			return "Serial"
5018f3b3610SWarner Losh		else
5028f3b3610SWarner Losh			return "Video"
5038f3b3610SWarner Losh		end
5048f3b3610SWarner Losh	end
5058f3b3610SWarner Loshend
5068f3b3610SWarner Losh
5078f3b3610SWarner Loshfunction core.nextConsoleChoice()
5088f3b3610SWarner Losh	if loader.getenv("boot_multicons") ~= nil then
5098f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
5108f3b3610SWarner Losh			loader.unsetenv("boot_serial")
5118f3b3610SWarner Losh		else
5128f3b3610SWarner Losh			loader.unsetenv("boot_multicons")
5138f3b3610SWarner Losh			loader.setenv("boot_serial", "YES")
5148f3b3610SWarner Losh		end
5158f3b3610SWarner Losh	else
5168f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
5178f3b3610SWarner Losh			loader.unsetenv("boot_serial")
5188f3b3610SWarner Losh		else
5198f3b3610SWarner Losh			loader.setenv("boot_multicons", "YES")
5208f3b3610SWarner Losh			loader.setenv("boot_serial", "YES")
5218f3b3610SWarner Losh		end
5228f3b3610SWarner Losh	end
5238f3b3610SWarner Loshend
5248f3b3610SWarner Losh
5251613f091SKyle EvansrecordDefaults()
526aea262bfSKyle Evanshook.register("config.reloaded", core.clearCachedKernels)
527aedd6be5SKyle Evansreturn core
528