xref: /freebsd/stand/lua/core.lua (revision e25ee296c919d6567aa76058a7049eac974797fb)
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
371613f091SKyle Evanslocal default_safe_mode = false
381613f091SKyle Evanslocal default_single_user = false
391613f091SKyle Evanslocal default_verbose = false
401613f091SKyle Evans
41277f38abSMariusz Zaborskilocal bootenv_list = "bootenvs"
42277f38abSMariusz Zaborski
43322a2dddSKyle Evanslocal function composeLoaderCmd(cmd_name, argstr)
449f71d421SKyle Evans	if argstr ~= nil then
45aedd6be5SKyle Evans		cmd_name = cmd_name .. " " .. argstr
466d4ed94dSKyle Evans	end
47aedd6be5SKyle Evans	return cmd_name
486d4ed94dSKyle Evansend
496d4ed94dSKyle Evans
501613f091SKyle Evanslocal function recordDefaults()
511613f091SKyle Evans	-- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386,
521613f091SKyle Evans	-- it will generally be set upon execution of the kernel. Because of
531613f091SKyle Evans	-- this, we can't (or don't really want to) detect/disable ACPI on !i386
541613f091SKyle Evans	-- reliably. Just set it enabled if we detect it and leave well enough
551613f091SKyle Evans	-- alone if we don't.
561613f091SKyle Evans	local boot_acpi = core.isSystem386() and core.getACPIPresent(false)
571613f091SKyle Evans	local boot_single = loader.getenv("boot_single") or "no"
581613f091SKyle Evans	local boot_verbose = loader.getenv("boot_verbose") or "no"
591613f091SKyle Evans	default_single_user = boot_single:lower() ~= "no"
601613f091SKyle Evans	default_verbose = boot_verbose:lower() ~= "no"
611613f091SKyle Evans
621613f091SKyle Evans	if boot_acpi then
631613f091SKyle Evans		core.setACPI(true)
641613f091SKyle Evans	end
651613f091SKyle Evans	core.setSingleUser(default_single_user)
661613f091SKyle Evans	core.setVerbose(default_verbose)
671613f091SKyle Evansend
681613f091SKyle Evans
691613f091SKyle Evans
7007faaf78SKyle Evans-- Globals
715beb5507SKyle Evans-- try_include will return the loaded module on success, or false and the error
725beb5507SKyle Evans-- message on failure.
7307faaf78SKyle Evansfunction try_include(module)
74bac5966eSKyle Evans	if module:sub(1, 1) ~= "/" then
75366f9979SKyle Evans		local lua_path = loader.lua_path
76bac5966eSKyle Evans		-- XXX Temporary compat shim; this should be removed once the
77bac5966eSKyle Evans		-- loader.lua_path export has sufficiently spread.
78bac5966eSKyle Evans		if lua_path == nil then
79bac5966eSKyle Evans			lua_path = "/boot/lua"
8007faaf78SKyle Evans		end
81bac5966eSKyle Evans		module = lua_path .. "/" .. module
82bac5966eSKyle Evans		-- We only attempt to append an extension if an absolute path
83bac5966eSKyle Evans		-- wasn't specified.  This assumes that the caller either wants
84bac5966eSKyle Evans		-- to treat this like it would require() and specify just the
85bac5966eSKyle Evans		-- base filename, or they know what they're doing as they've
86bac5966eSKyle Evans		-- specified an absolute path and we shouldn't impede.
87bac5966eSKyle Evans		if module:match(".lua$") == nil then
88bac5966eSKyle Evans			module = module .. ".lua"
89bac5966eSKyle Evans		end
90bac5966eSKyle Evans	end
91bac5966eSKyle Evans	if lfs.attributes(module, "mode") ~= "file" then
92bac5966eSKyle Evans		return
93bac5966eSKyle Evans	end
94bac5966eSKyle Evans
95bac5966eSKyle Evans	return dofile(module)
9607faaf78SKyle Evansend
9707faaf78SKyle Evans
98b5746545SKyle Evans-- Module exports
99fe672a15SKyle Evans-- Commonly appearing constants
100aedd6be5SKyle Evanscore.KEY_BACKSPACE	= 8
101aedd6be5SKyle Evanscore.KEY_ENTER		= 13
102aedd6be5SKyle Evanscore.KEY_DELETE		= 127
103fe672a15SKyle Evans
104230061c5SKyle Evans-- Note that this is a decimal representation, despite the leading 0 that in
105230061c5SKyle Evans-- other contexts (outside of Lua) may mean 'octal'
106aedd6be5SKyle Evanscore.KEYSTR_ESCAPE	= "\027"
1072bb86aefSKyle Evanscore.KEYSTR_CSI		= core.KEYSTR_ESCAPE .. "["
10873531a2aSRyan Moellercore.KEYSTR_RESET	= core.KEYSTR_ESCAPE .. "c"
10939006570SKyle Evans
110aedd6be5SKyle Evanscore.MENU_RETURN	= "return"
111aedd6be5SKyle Evanscore.MENU_ENTRY		= "entry"
112aedd6be5SKyle Evanscore.MENU_SEPARATOR	= "separator"
113aedd6be5SKyle Evanscore.MENU_SUBMENU	= "submenu"
114aedd6be5SKyle Evanscore.MENU_CAROUSEL_ENTRY	= "carousel_entry"
115a7cf0562SKyle Evans
11604af4229SKyle Evansfunction core.setVerbose(verbose)
11704af4229SKyle Evans	if verbose == nil then
11804af4229SKyle Evans		verbose = not core.verbose
119088b4f5fSWarner Losh	end
120088b4f5fSWarner Losh
12104af4229SKyle Evans	if verbose then
122aedd6be5SKyle Evans		loader.setenv("boot_verbose", "YES")
123088b4f5fSWarner Losh	else
124aedd6be5SKyle Evans		loader.unsetenv("boot_verbose")
125088b4f5fSWarner Losh	end
12604af4229SKyle Evans	core.verbose = verbose
127088b4f5fSWarner Loshend
128088b4f5fSWarner Losh
12904af4229SKyle Evansfunction core.setSingleUser(single_user)
13004af4229SKyle Evans	if single_user == nil then
13104af4229SKyle Evans		single_user = not core.su
132088b4f5fSWarner Losh	end
133088b4f5fSWarner Losh
13404af4229SKyle Evans	if single_user then
135aedd6be5SKyle Evans		loader.setenv("boot_single", "YES")
136088b4f5fSWarner Losh	else
137aedd6be5SKyle Evans		loader.unsetenv("boot_single")
138088b4f5fSWarner Losh	end
13904af4229SKyle Evans	core.su = single_user
140088b4f5fSWarner Loshend
141088b4f5fSWarner Losh
14204af4229SKyle Evansfunction core.getACPIPresent(checking_system_defaults)
143aedd6be5SKyle Evans	local c = loader.getenv("hint.acpi.0.rsdp")
1446401094fSKyle Evans
1459f71d421SKyle Evans	if c ~= nil then
14604af4229SKyle Evans		if checking_system_defaults then
147aedd6be5SKyle Evans			return true
1486401094fSKyle Evans		end
1496401094fSKyle Evans		-- Otherwise, respect disabled if it's set
150aedd6be5SKyle Evans		c = loader.getenv("hint.acpi.0.disabled")
1519f71d421SKyle Evans		return c == nil or tonumber(c) ~= 1
1526401094fSKyle Evans	end
153aedd6be5SKyle Evans	return false
1546401094fSKyle Evansend
1556401094fSKyle Evans
15604af4229SKyle Evansfunction core.setACPI(acpi)
15704af4229SKyle Evans	if acpi == nil then
15804af4229SKyle Evans		acpi = not core.acpi
159088b4f5fSWarner Losh	end
160088b4f5fSWarner Losh
16104af4229SKyle Evans	if acpi then
162aedd6be5SKyle Evans		loader.setenv("acpi_load", "YES")
163aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "0")
164aedd6be5SKyle Evans		loader.unsetenv("loader.acpi_disabled_by_user")
165088b4f5fSWarner Losh	else
166aedd6be5SKyle Evans		loader.unsetenv("acpi_load")
167aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "1")
168aedd6be5SKyle Evans		loader.setenv("loader.acpi_disabled_by_user", "1")
169088b4f5fSWarner Losh	end
17004af4229SKyle Evans	core.acpi = acpi
171088b4f5fSWarner Loshend
172088b4f5fSWarner Losh
17304af4229SKyle Evansfunction core.setSafeMode(safe_mode)
17404af4229SKyle Evans	if safe_mode == nil then
17504af4229SKyle Evans		safe_mode = not core.sm
176088b4f5fSWarner Losh	end
17704af4229SKyle Evans	if safe_mode then
178aedd6be5SKyle Evans		loader.setenv("kern.smp.disabled", "1")
179aedd6be5SKyle Evans		loader.setenv("hw.ata.ata_dma", "0")
180aedd6be5SKyle Evans		loader.setenv("hw.ata.atapi_dma", "0")
181aedd6be5SKyle Evans		loader.setenv("hw.ata.wc", "0")
182aedd6be5SKyle Evans		loader.setenv("hw.eisa_slots", "0")
183aedd6be5SKyle Evans		loader.setenv("kern.eventtimer.periodic", "1")
184aedd6be5SKyle Evans		loader.setenv("kern.geom.part.check_integrity", "0")
185088b4f5fSWarner Losh	else
186aedd6be5SKyle Evans		loader.unsetenv("kern.smp.disabled")
187aedd6be5SKyle Evans		loader.unsetenv("hw.ata.ata_dma")
188aedd6be5SKyle Evans		loader.unsetenv("hw.ata.atapi_dma")
189aedd6be5SKyle Evans		loader.unsetenv("hw.ata.wc")
190aedd6be5SKyle Evans		loader.unsetenv("hw.eisa_slots")
191aedd6be5SKyle Evans		loader.unsetenv("kern.eventtimer.periodic")
192aedd6be5SKyle Evans		loader.unsetenv("kern.geom.part.check_integrity")
193088b4f5fSWarner Losh	end
19404af4229SKyle Evans	core.sm = safe_mode
195088b4f5fSWarner Loshend
196088b4f5fSWarner Losh
197aea262bfSKyle Evansfunction core.clearCachedKernels()
19835b0c718SKyle Evans	-- Clear the kernel cache on config changes, autodetect might have
19935b0c718SKyle Evans	-- changed or if we've switched boot environments then we could have
20035b0c718SKyle Evans	-- a new kernel set.
20135b0c718SKyle Evans	core.cached_kernels = nil
20235b0c718SKyle Evansend
20335b0c718SKyle Evans
204088b4f5fSWarner Loshfunction core.kernelList()
20535b0c718SKyle Evans	if core.cached_kernels ~= nil then
20635b0c718SKyle Evans		return core.cached_kernels
20735b0c718SKyle Evans	end
20835b0c718SKyle Evans
209aedd6be5SKyle Evans	local k = loader.getenv("kernel")
2103f4eb56bSKyle Evans	local v = loader.getenv("kernels")
2113889e6cdSKyle Evans	local autodetect = loader.getenv("kernels_autodetect") or ""
212088b4f5fSWarner Losh
213aedd6be5SKyle Evans	local kernels = {}
214aedd6be5SKyle Evans	local unique = {}
215aedd6be5SKyle Evans	local i = 0
2169f71d421SKyle Evans	if k ~= nil then
217aedd6be5SKyle Evans		i = i + 1
218aedd6be5SKyle Evans		kernels[i] = k
219aedd6be5SKyle Evans		unique[k] = true
220088b4f5fSWarner Losh	end
221088b4f5fSWarner Losh
2223f4eb56bSKyle Evans	if v ~= nil then
223a5003419SKyle Evans		for n in v:gmatch("([^;, ]+)[;, ]?") do
2249f71d421SKyle Evans			if unique[n] == nil then
225aedd6be5SKyle Evans				i = i + 1
226aedd6be5SKyle Evans				kernels[i] = n
227aedd6be5SKyle Evans				unique[n] = true
228088b4f5fSWarner Losh			end
229088b4f5fSWarner Losh		end
2303889e6cdSKyle Evans	end
231a108046fSConrad Meyer
2323889e6cdSKyle Evans	-- Base whether we autodetect kernels or not on a loader.conf(5)
2333889e6cdSKyle Evans	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
2343889e6cdSKyle Evans	-- any kernels we detect based on the criteria described.
2353889e6cdSKyle Evans	if autodetect:lower() ~= "yes" then
23635b0c718SKyle Evans		core.cached_kernels = kernels
23735b0c718SKyle Evans		return core.cached_kernels
2383f4eb56bSKyle Evans	end
2393f4eb56bSKyle Evans
240a108046fSConrad Meyer	-- Automatically detect other bootable kernel directories using a
241a108046fSConrad Meyer	-- heuristic.  Any directory in /boot that contains an ordinary file
242a108046fSConrad Meyer	-- named "kernel" is considered eligible.
243*e25ee296SKyle Evans	for file, ftype in lfs.dir("/boot") do
244aedd6be5SKyle Evans		local fname = "/boot/" .. file
245a108046fSConrad Meyer
2469f71d421SKyle Evans		if file == "." or file == ".." then
247aedd6be5SKyle Evans			goto continue
248a108046fSConrad Meyer		end
249a108046fSConrad Meyer
250*e25ee296SKyle Evans		if ftype then
251*e25ee296SKyle Evans			if ftype ~= lfs.DT_DIR then
252*e25ee296SKyle Evans				goto continue
253*e25ee296SKyle Evans			end
254*e25ee296SKyle Evans		elseif lfs.attributes(fname, "mode") ~= "directory" then
255aedd6be5SKyle Evans			goto continue
256a108046fSConrad Meyer		end
257a108046fSConrad Meyer
2589f71d421SKyle Evans		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
259aedd6be5SKyle Evans			goto continue
260a108046fSConrad Meyer		end
261a108046fSConrad Meyer
2629f71d421SKyle Evans		if unique[file] == nil then
263aedd6be5SKyle Evans			i = i + 1
264aedd6be5SKyle Evans			kernels[i] = file
265aedd6be5SKyle Evans			unique[file] = true
266a108046fSConrad Meyer		end
267a108046fSConrad Meyer
268a108046fSConrad Meyer		::continue::
269a108046fSConrad Meyer	end
27035b0c718SKyle Evans	core.cached_kernels = kernels
27135b0c718SKyle Evans	return core.cached_kernels
272088b4f5fSWarner Loshend
273088b4f5fSWarner Losh
2747efc058fSKyle Evansfunction core.bootenvDefault()
2757efc058fSKyle Evans	return loader.getenv("zfs_be_active")
2767efc058fSKyle Evansend
2777efc058fSKyle Evans
2787efc058fSKyle Evansfunction core.bootenvList()
279277f38abSMariusz Zaborski	local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count"))
2807efc058fSKyle Evans	local bootenvs = {}
2817efc058fSKyle Evans	local curenv
2827efc058fSKyle Evans	local envcount = 0
2837efc058fSKyle Evans	local unique = {}
2847efc058fSKyle Evans
2857efc058fSKyle Evans	if bootenv_count == nil or bootenv_count <= 0 then
2867efc058fSKyle Evans		return bootenvs
2877efc058fSKyle Evans	end
2887efc058fSKyle Evans
2897efc058fSKyle Evans	-- Currently selected bootenv is always first/default
290277f38abSMariusz Zaborski	-- On the rewinded list the bootenv may not exists
291277f38abSMariusz Zaborski	if core.isRewinded() then
292277f38abSMariusz Zaborski		curenv = core.bootenvDefaultRewinded()
293277f38abSMariusz Zaborski	else
2947efc058fSKyle Evans		curenv = core.bootenvDefault()
295277f38abSMariusz Zaborski	end
2967efc058fSKyle Evans	if curenv ~= nil then
2977efc058fSKyle Evans		envcount = envcount + 1
2987efc058fSKyle Evans		bootenvs[envcount] = curenv
2997efc058fSKyle Evans		unique[curenv] = true
3007efc058fSKyle Evans	end
3017efc058fSKyle Evans
3027efc058fSKyle Evans	for curenv_idx = 0, bootenv_count - 1 do
303277f38abSMariusz Zaborski		curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]")
3047efc058fSKyle Evans		if curenv ~= nil and unique[curenv] == nil then
3057efc058fSKyle Evans			envcount = envcount + 1
3067efc058fSKyle Evans			bootenvs[envcount] = curenv
3077efc058fSKyle Evans			unique[curenv] = true
3087efc058fSKyle Evans		end
3097efc058fSKyle Evans	end
3107efc058fSKyle Evans	return bootenvs
3117efc058fSKyle Evansend
3127efc058fSKyle Evans
313277f38abSMariusz Zaborskifunction core.isCheckpointed()
314277f38abSMariusz Zaborski	return loader.getenv("zpool_checkpoint") ~= nil
315277f38abSMariusz Zaborskiend
316277f38abSMariusz Zaborski
317277f38abSMariusz Zaborskifunction core.bootenvDefaultRewinded()
318277f38abSMariusz Zaborski	local defname =  "zfs:!" .. string.sub(core.bootenvDefault(), 5)
319277f38abSMariusz Zaborski	local bootenv_count = tonumber("bootenvs_check_count")
320277f38abSMariusz Zaborski
321277f38abSMariusz Zaborski	if bootenv_count == nil or bootenv_count <= 0 then
322277f38abSMariusz Zaborski		return defname
323277f38abSMariusz Zaborski	end
324277f38abSMariusz Zaborski
325277f38abSMariusz Zaborski	for curenv_idx = 0, bootenv_count - 1 do
32694510c29SKyle Evans		local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]")
327277f38abSMariusz Zaborski		if curenv == defname then
328277f38abSMariusz Zaborski			return defname
329277f38abSMariusz Zaborski		end
330277f38abSMariusz Zaborski	end
331277f38abSMariusz Zaborski
332277f38abSMariusz Zaborski	return loader.getenv("bootenvs_check[0]")
333277f38abSMariusz Zaborskiend
334277f38abSMariusz Zaborski
335277f38abSMariusz Zaborskifunction core.isRewinded()
336277f38abSMariusz Zaborski	return bootenv_list == "bootenvs_check"
337277f38abSMariusz Zaborskiend
338277f38abSMariusz Zaborski
339277f38abSMariusz Zaborskifunction core.changeRewindCheckpoint()
340277f38abSMariusz Zaborski	if core.isRewinded() then
341277f38abSMariusz Zaborski		bootenv_list = "bootenvs"
342277f38abSMariusz Zaborski	else
343277f38abSMariusz Zaborski		bootenv_list = "bootenvs_check"
344277f38abSMariusz Zaborski	end
345277f38abSMariusz Zaborskiend
346277f38abSMariusz Zaborski
347088b4f5fSWarner Loshfunction core.setDefaults()
348aedd6be5SKyle Evans	core.setACPI(core.getACPIPresent(true))
3491613f091SKyle Evans	core.setSafeMode(default_safe_mode)
3501613f091SKyle Evans	core.setSingleUser(default_single_user)
3511613f091SKyle Evans	core.setVerbose(default_verbose)
352088b4f5fSWarner Loshend
353088b4f5fSWarner Losh
3546d4ed94dSKyle Evansfunction core.autoboot(argstr)
355a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
356a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
357aedd6be5SKyle Evans		config.loadelf()
358a2a7830eSKyle Evans	end
359322a2dddSKyle Evans	loader.perform(composeLoaderCmd("autoboot", argstr))
360088b4f5fSWarner Loshend
361088b4f5fSWarner Losh
3626d4ed94dSKyle Evansfunction core.boot(argstr)
363a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
364a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
365aedd6be5SKyle Evans		config.loadelf()
366a2a7830eSKyle Evans	end
367322a2dddSKyle Evans	loader.perform(composeLoaderCmd("boot", argstr))
368088b4f5fSWarner Loshend
369088b4f5fSWarner Losh
370e07fc39cSKyle Evansfunction core.isSingleUserBoot()
371aedd6be5SKyle Evans	local single_user = loader.getenv("boot_single")
372aedd6be5SKyle Evans	return single_user ~= nil and single_user:lower() == "yes"
373e07fc39cSKyle Evansend
374e07fc39cSKyle Evans
3755f8cfbe1SKyle Evansfunction core.isUEFIBoot()
3765f8cfbe1SKyle Evans	local efiver = loader.getenv("efi-version")
3775f8cfbe1SKyle Evans
3785f8cfbe1SKyle Evans	return efiver ~= nil
3795f8cfbe1SKyle Evansend
3805f8cfbe1SKyle Evans
3817efc058fSKyle Evansfunction core.isZFSBoot()
3827efc058fSKyle Evans	local c = loader.getenv("currdev")
3837efc058fSKyle Evans
3847efc058fSKyle Evans	if c ~= nil then
3857efc058fSKyle Evans		return c:match("^zfs:") ~= nil
3867efc058fSKyle Evans	end
3877efc058fSKyle Evans	return false
3887efc058fSKyle Evansend
3897efc058fSKyle Evans
3903630506bSToomas Soomefunction core.isFramebufferConsole()
3913630506bSToomas Soome	local c = loader.getenv("console")
3923630506bSToomas Soome	if c ~= nil then
3933630506bSToomas Soome		if c:find("efi") == nil and c:find("vidconsole") == nil then
3943630506bSToomas Soome			return false
3953630506bSToomas Soome		end
3963630506bSToomas Soome		if loader.getenv("screen.depth") ~= nil then
3973630506bSToomas Soome			return true
3983630506bSToomas Soome		end
3993630506bSToomas Soome	end
4003630506bSToomas Soome	return false
4013630506bSToomas Soomeend
4023630506bSToomas Soome
40390a25417SKyle Evansfunction core.isSerialConsole()
40490a25417SKyle Evans	local c = loader.getenv("console")
40590a25417SKyle Evans	if c ~= nil then
40690a25417SKyle Evans		if c:find("comconsole") ~= nil then
40790a25417SKyle Evans			return true
40890a25417SKyle Evans		end
40990a25417SKyle Evans	end
41090a25417SKyle Evans	return false
41190a25417SKyle Evansend
41290a25417SKyle Evans
413b140d14bSKyle Evansfunction core.isSerialBoot()
414aedd6be5SKyle Evans	local s = loader.getenv("boot_serial")
4159f71d421SKyle Evans	if s ~= nil then
416aedd6be5SKyle Evans		return true
417088b4f5fSWarner Losh	end
418088b4f5fSWarner Losh
419aedd6be5SKyle Evans	local m = loader.getenv("boot_multicons")
4209f71d421SKyle Evans	if m ~= nil then
421aedd6be5SKyle Evans		return true
422088b4f5fSWarner Losh	end
423aedd6be5SKyle Evans	return false
424088b4f5fSWarner Loshend
425088b4f5fSWarner Losh
426c1ab36f5SKyle Evansfunction core.isSystem386()
4279f71d421SKyle Evans	return loader.machine_arch == "i386"
428c1ab36f5SKyle Evansend
429c1ab36f5SKyle Evans
4309937e979SKyle Evans-- Is the menu skipped in the environment in which we've booted?
4319937e979SKyle Evansfunction core.isMenuSkipped()
432b83a355dSKyle Evans	return string.lower(loader.getenv("beastie_disable") or "") == "yes"
4339937e979SKyle Evansend
4349937e979SKyle Evans
4355c1b5165SKyle Evans-- This may be a better candidate for a 'utility' module.
436ee4e69f1SKyle Evansfunction core.deepCopyTable(tbl)
437aedd6be5SKyle Evans	local new_tbl = {}
4385c1b5165SKyle Evans	for k, v in pairs(tbl) do
4399f71d421SKyle Evans		if type(v) == "table" then
440ee4e69f1SKyle Evans			new_tbl[k] = core.deepCopyTable(v)
4415c1b5165SKyle Evans		else
442aedd6be5SKyle Evans			new_tbl[k] = v
4435c1b5165SKyle Evans		end
4445c1b5165SKyle Evans	end
445aedd6be5SKyle Evans	return new_tbl
4465c1b5165SKyle Evansend
4475c1b5165SKyle Evans
4486d4ed94dSKyle Evans-- XXX This should go away if we get the table lib into shape for importing.
4496d4ed94dSKyle Evans-- As of now, it requires some 'os' functions, so we'll implement this in lua
4506d4ed94dSKyle Evans-- for our uses
4516d4ed94dSKyle Evansfunction core.popFrontTable(tbl)
4526d4ed94dSKyle Evans	-- Shouldn't reasonably happen
4539f71d421SKyle Evans	if #tbl == 0 then
454aedd6be5SKyle Evans		return nil, nil
4559f71d421SKyle Evans	elseif #tbl == 1 then
456aedd6be5SKyle Evans		return tbl[1], {}
4576d4ed94dSKyle Evans	end
4586d4ed94dSKyle Evans
459aedd6be5SKyle Evans	local first_value = tbl[1]
460aedd6be5SKyle Evans	local new_tbl = {}
4616d4ed94dSKyle Evans	-- This is not a cheap operation
4626d4ed94dSKyle Evans	for k, v in ipairs(tbl) do
4639f71d421SKyle Evans		if k > 1 then
464aedd6be5SKyle Evans			new_tbl[k - 1] = v
4656d4ed94dSKyle Evans		end
4666d4ed94dSKyle Evans	end
4676d4ed94dSKyle Evans
468aedd6be5SKyle Evans	return first_value, new_tbl
4696d4ed94dSKyle Evansend
4706d4ed94dSKyle Evans
4718f3b3610SWarner Loshfunction core.getConsoleName()
4728f3b3610SWarner Losh	if loader.getenv("boot_multicons") ~= nil then
4738f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4748f3b3610SWarner Losh			return "Dual (Serial primary)"
4758f3b3610SWarner Losh		else
4768f3b3610SWarner Losh			return "Dual (Video primary)"
4778f3b3610SWarner Losh		end
4788f3b3610SWarner Losh	else
4798f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4808f3b3610SWarner Losh			return "Serial"
4818f3b3610SWarner Losh		else
4828f3b3610SWarner Losh			return "Video"
4838f3b3610SWarner Losh		end
4848f3b3610SWarner Losh	end
4858f3b3610SWarner Loshend
4868f3b3610SWarner Losh
4878f3b3610SWarner Loshfunction core.nextConsoleChoice()
4888f3b3610SWarner Losh	if loader.getenv("boot_multicons") ~= nil then
4898f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4908f3b3610SWarner Losh			loader.unsetenv("boot_serial")
4918f3b3610SWarner Losh		else
4928f3b3610SWarner Losh			loader.unsetenv("boot_multicons")
4938f3b3610SWarner Losh			loader.setenv("boot_serial", "YES")
4948f3b3610SWarner Losh		end
4958f3b3610SWarner Losh	else
4968f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4978f3b3610SWarner Losh			loader.unsetenv("boot_serial")
4988f3b3610SWarner Losh		else
4998f3b3610SWarner Losh			loader.setenv("boot_multicons", "YES")
5008f3b3610SWarner Losh			loader.setenv("boot_serial", "YES")
5018f3b3610SWarner Losh		end
5028f3b3610SWarner Losh	end
5038f3b3610SWarner Loshend
5048f3b3610SWarner Losh
5051613f091SKyle EvansrecordDefaults()
506aea262bfSKyle Evanshook.register("config.reloaded", core.clearCachedKernels)
507aedd6be5SKyle Evansreturn core
508