xref: /freebsd/stand/lua/core.lua (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1088b4f5fSWarner Losh--
2*4d846d26SWarner 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-- $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
232c4dc9072SEmmanuel Vadot	-- Do not attempt to autodetect if underlying filesystem
233c4dc9072SEmmanuel Vadot	-- do not support directory listing (e.g. tftp, http)
234c4dc9072SEmmanuel Vadot	if not lfs.attributes("/boot", "mode") then
235c4dc9072SEmmanuel Vadot		autodetect = "no"
236c4dc9072SEmmanuel Vadot		loader.setenv("kernels_autodetect", "NO")
237c4dc9072SEmmanuel Vadot	end
238c4dc9072SEmmanuel Vadot
2393889e6cdSKyle Evans	-- Base whether we autodetect kernels or not on a loader.conf(5)
2403889e6cdSKyle Evans	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
2413889e6cdSKyle Evans	-- any kernels we detect based on the criteria described.
2423889e6cdSKyle Evans	if autodetect:lower() ~= "yes" then
24335b0c718SKyle Evans		core.cached_kernels = kernels
24435b0c718SKyle Evans		return core.cached_kernels
2453f4eb56bSKyle Evans	end
2463f4eb56bSKyle Evans
247a108046fSConrad Meyer	-- Automatically detect other bootable kernel directories using a
248a108046fSConrad Meyer	-- heuristic.  Any directory in /boot that contains an ordinary file
249a108046fSConrad Meyer	-- named "kernel" is considered eligible.
250e25ee296SKyle Evans	for file, ftype in lfs.dir("/boot") do
251aedd6be5SKyle Evans		local fname = "/boot/" .. file
252a108046fSConrad Meyer
2539f71d421SKyle Evans		if file == "." or file == ".." then
254aedd6be5SKyle Evans			goto continue
255a108046fSConrad Meyer		end
256a108046fSConrad Meyer
257e25ee296SKyle Evans		if ftype then
258e25ee296SKyle Evans			if ftype ~= lfs.DT_DIR then
259e25ee296SKyle Evans				goto continue
260e25ee296SKyle Evans			end
261e25ee296SKyle Evans		elseif lfs.attributes(fname, "mode") ~= "directory" then
262aedd6be5SKyle Evans			goto continue
263a108046fSConrad Meyer		end
264a108046fSConrad Meyer
2659f71d421SKyle Evans		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
266aedd6be5SKyle Evans			goto continue
267a108046fSConrad Meyer		end
268a108046fSConrad Meyer
2699f71d421SKyle Evans		if unique[file] == nil then
270aedd6be5SKyle Evans			i = i + 1
271aedd6be5SKyle Evans			kernels[i] = file
272aedd6be5SKyle Evans			unique[file] = true
273a108046fSConrad Meyer		end
274a108046fSConrad Meyer
275a108046fSConrad Meyer		::continue::
276a108046fSConrad Meyer	end
27735b0c718SKyle Evans	core.cached_kernels = kernels
27835b0c718SKyle Evans	return core.cached_kernels
279088b4f5fSWarner Loshend
280088b4f5fSWarner Losh
2817efc058fSKyle Evansfunction core.bootenvDefault()
2827efc058fSKyle Evans	return loader.getenv("zfs_be_active")
2837efc058fSKyle Evansend
2847efc058fSKyle Evans
2857efc058fSKyle Evansfunction core.bootenvList()
286277f38abSMariusz Zaborski	local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count"))
2877efc058fSKyle Evans	local bootenvs = {}
2887efc058fSKyle Evans	local curenv
2897efc058fSKyle Evans	local envcount = 0
2907efc058fSKyle Evans	local unique = {}
2917efc058fSKyle Evans
2927efc058fSKyle Evans	if bootenv_count == nil or bootenv_count <= 0 then
2937efc058fSKyle Evans		return bootenvs
2947efc058fSKyle Evans	end
2957efc058fSKyle Evans
2967efc058fSKyle Evans	-- Currently selected bootenv is always first/default
297277f38abSMariusz Zaborski	-- On the rewinded list the bootenv may not exists
298277f38abSMariusz Zaborski	if core.isRewinded() then
299277f38abSMariusz Zaborski		curenv = core.bootenvDefaultRewinded()
300277f38abSMariusz Zaborski	else
3017efc058fSKyle Evans		curenv = core.bootenvDefault()
302277f38abSMariusz Zaborski	end
3037efc058fSKyle Evans	if curenv ~= nil then
3047efc058fSKyle Evans		envcount = envcount + 1
3057efc058fSKyle Evans		bootenvs[envcount] = curenv
3067efc058fSKyle Evans		unique[curenv] = true
3077efc058fSKyle Evans	end
3087efc058fSKyle Evans
3097efc058fSKyle Evans	for curenv_idx = 0, bootenv_count - 1 do
310277f38abSMariusz Zaborski		curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]")
3117efc058fSKyle Evans		if curenv ~= nil and unique[curenv] == nil then
3127efc058fSKyle Evans			envcount = envcount + 1
3137efc058fSKyle Evans			bootenvs[envcount] = curenv
3147efc058fSKyle Evans			unique[curenv] = true
3157efc058fSKyle Evans		end
3167efc058fSKyle Evans	end
3177efc058fSKyle Evans	return bootenvs
3187efc058fSKyle Evansend
3197efc058fSKyle Evans
320277f38abSMariusz Zaborskifunction core.isCheckpointed()
321277f38abSMariusz Zaborski	return loader.getenv("zpool_checkpoint") ~= nil
322277f38abSMariusz Zaborskiend
323277f38abSMariusz Zaborski
324277f38abSMariusz Zaborskifunction core.bootenvDefaultRewinded()
325277f38abSMariusz Zaborski	local defname =  "zfs:!" .. string.sub(core.bootenvDefault(), 5)
326277f38abSMariusz Zaborski	local bootenv_count = tonumber("bootenvs_check_count")
327277f38abSMariusz Zaborski
328277f38abSMariusz Zaborski	if bootenv_count == nil or bootenv_count <= 0 then
329277f38abSMariusz Zaborski		return defname
330277f38abSMariusz Zaborski	end
331277f38abSMariusz Zaborski
332277f38abSMariusz Zaborski	for curenv_idx = 0, bootenv_count - 1 do
33394510c29SKyle Evans		local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]")
334277f38abSMariusz Zaborski		if curenv == defname then
335277f38abSMariusz Zaborski			return defname
336277f38abSMariusz Zaborski		end
337277f38abSMariusz Zaborski	end
338277f38abSMariusz Zaborski
339277f38abSMariusz Zaborski	return loader.getenv("bootenvs_check[0]")
340277f38abSMariusz Zaborskiend
341277f38abSMariusz Zaborski
342277f38abSMariusz Zaborskifunction core.isRewinded()
343277f38abSMariusz Zaborski	return bootenv_list == "bootenvs_check"
344277f38abSMariusz Zaborskiend
345277f38abSMariusz Zaborski
346277f38abSMariusz Zaborskifunction core.changeRewindCheckpoint()
347277f38abSMariusz Zaborski	if core.isRewinded() then
348277f38abSMariusz Zaborski		bootenv_list = "bootenvs"
349277f38abSMariusz Zaborski	else
350277f38abSMariusz Zaborski		bootenv_list = "bootenvs_check"
351277f38abSMariusz Zaborski	end
352277f38abSMariusz Zaborskiend
353277f38abSMariusz Zaborski
3545c73b3e0SColin Percivalfunction core.loadEntropy()
3555c73b3e0SColin Percival	if core.isUEFIBoot() then
3565c73b3e0SColin Percival		if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then
3575c73b3e0SColin Percival			loader.perform("efi-seed-entropy")
3585c73b3e0SColin Percival		end
3595c73b3e0SColin Percival	end
3605c73b3e0SColin Percivalend
3615c73b3e0SColin Percival
362088b4f5fSWarner Loshfunction core.setDefaults()
363aedd6be5SKyle Evans	core.setACPI(core.getACPIPresent(true))
3641613f091SKyle Evans	core.setSafeMode(default_safe_mode)
3651613f091SKyle Evans	core.setSingleUser(default_single_user)
3661613f091SKyle Evans	core.setVerbose(default_verbose)
367088b4f5fSWarner Loshend
368088b4f5fSWarner Losh
3696d4ed94dSKyle Evansfunction core.autoboot(argstr)
370a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
371a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
372aedd6be5SKyle Evans		config.loadelf()
373a2a7830eSKyle Evans	end
3745c73b3e0SColin Percival	core.loadEntropy()
375322a2dddSKyle Evans	loader.perform(composeLoaderCmd("autoboot", argstr))
376088b4f5fSWarner Loshend
377088b4f5fSWarner Losh
3786d4ed94dSKyle Evansfunction core.boot(argstr)
379a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
380a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
381aedd6be5SKyle Evans		config.loadelf()
382a2a7830eSKyle Evans	end
3835c73b3e0SColin Percival	core.loadEntropy()
384322a2dddSKyle Evans	loader.perform(composeLoaderCmd("boot", argstr))
385088b4f5fSWarner Loshend
386088b4f5fSWarner Losh
387e07fc39cSKyle Evansfunction core.isSingleUserBoot()
388aedd6be5SKyle Evans	local single_user = loader.getenv("boot_single")
389aedd6be5SKyle Evans	return single_user ~= nil and single_user:lower() == "yes"
390e07fc39cSKyle Evansend
391e07fc39cSKyle Evans
3925f8cfbe1SKyle Evansfunction core.isUEFIBoot()
3935f8cfbe1SKyle Evans	local efiver = loader.getenv("efi-version")
3945f8cfbe1SKyle Evans
3955f8cfbe1SKyle Evans	return efiver ~= nil
3965f8cfbe1SKyle Evansend
3975f8cfbe1SKyle Evans
3987efc058fSKyle Evansfunction core.isZFSBoot()
3997efc058fSKyle Evans	local c = loader.getenv("currdev")
4007efc058fSKyle Evans
4017efc058fSKyle Evans	if c ~= nil then
4027efc058fSKyle Evans		return c:match("^zfs:") ~= nil
4037efc058fSKyle Evans	end
4047efc058fSKyle Evans	return false
4057efc058fSKyle Evansend
4067efc058fSKyle Evans
4073630506bSToomas Soomefunction core.isFramebufferConsole()
4083630506bSToomas Soome	local c = loader.getenv("console")
4093630506bSToomas Soome	if c ~= nil then
4103630506bSToomas Soome		if c:find("efi") == nil and c:find("vidconsole") == nil then
4113630506bSToomas Soome			return false
4123630506bSToomas Soome		end
4133630506bSToomas Soome		if loader.getenv("screen.depth") ~= nil then
4143630506bSToomas Soome			return true
4153630506bSToomas Soome		end
4163630506bSToomas Soome	end
4173630506bSToomas Soome	return false
4183630506bSToomas Soomeend
4193630506bSToomas Soome
42090a25417SKyle Evansfunction core.isSerialConsole()
42190a25417SKyle Evans	local c = loader.getenv("console")
42290a25417SKyle Evans	if c ~= nil then
4235d8c062fSToomas Soome		-- serial console is comconsole, but also userboot.
4245d8c062fSToomas Soome		-- userboot is there, because we have no way to know
4255d8c062fSToomas Soome		-- if the user terminal can draw unicode box chars or not.
4265d8c062fSToomas Soome		if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then
42790a25417SKyle Evans			return true
42890a25417SKyle Evans		end
42990a25417SKyle Evans	end
43090a25417SKyle Evans	return false
43190a25417SKyle Evansend
43290a25417SKyle Evans
433b140d14bSKyle Evansfunction core.isSerialBoot()
434aedd6be5SKyle Evans	local s = loader.getenv("boot_serial")
4359f71d421SKyle Evans	if s ~= nil then
436aedd6be5SKyle Evans		return true
437088b4f5fSWarner Losh	end
438088b4f5fSWarner Losh
439aedd6be5SKyle Evans	local m = loader.getenv("boot_multicons")
4409f71d421SKyle Evans	if m ~= nil then
441aedd6be5SKyle Evans		return true
442088b4f5fSWarner Losh	end
443aedd6be5SKyle Evans	return false
444088b4f5fSWarner Loshend
445088b4f5fSWarner Losh
446c1ab36f5SKyle Evansfunction core.isSystem386()
4479f71d421SKyle Evans	return loader.machine_arch == "i386"
448c1ab36f5SKyle Evansend
449c1ab36f5SKyle Evans
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