xref: /freebsd/stand/lua/core.lua (revision 0abe05aeac29d99786401b9078e97dcead35f7f3)
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()
136e0f3dc82SR. Christian McDonald	return loader.getenv("acpi.rsdp") ~= nil
137e0f3dc82SR. Christian McDonaldend
1386401094fSKyle Evans
139*0abe05aeSWarner Loshfunction core.isX86()
140*0abe05aeSWarner Losh	return loader.machine_arch == "i386" or loader.machine_arch == "amd64"
141*0abe05aeSWarner Loshend
142*0abe05aeSWarner Losh
143e0f3dc82SR. Christian McDonaldfunction core.getACPI()
144e0f3dc82SR. Christian McDonald	if not core.hasACPI() then
145*0abe05aeSWarner Losh		-- x86 requires ACPI pretty much
146*0abe05aeSWarner Losh		return false or core.isX86()
1476401094fSKyle Evans	end
1486401094fSKyle Evans
149e0f3dc82SR. Christian McDonald	-- Otherwise, respect disabled if it's set
150e0f3dc82SR. Christian McDonald	local c = loader.getenv("hint.acpi.0.disabled")
151e0f3dc82SR. Christian McDonald	return c == nil or tonumber(c) ~= 1
152e0f3dc82SR. Christian McDonaldend
153e0f3dc82SR. Christian McDonald
15404af4229SKyle Evansfunction core.setACPI(acpi)
15504af4229SKyle Evans	if acpi == nil then
15604af4229SKyle Evans		acpi = not core.acpi
157088b4f5fSWarner Losh	end
158088b4f5fSWarner Losh
15904af4229SKyle Evans	if acpi then
160aedd6be5SKyle Evans		loader.setenv("acpi_load", "YES")
161aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "0")
162aedd6be5SKyle Evans		loader.unsetenv("loader.acpi_disabled_by_user")
163088b4f5fSWarner Losh	else
164aedd6be5SKyle Evans		loader.unsetenv("acpi_load")
165aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "1")
166aedd6be5SKyle Evans		loader.setenv("loader.acpi_disabled_by_user", "1")
167088b4f5fSWarner Losh	end
16804af4229SKyle Evans	core.acpi = acpi
169088b4f5fSWarner Loshend
170088b4f5fSWarner Losh
17104af4229SKyle Evansfunction core.setSafeMode(safe_mode)
17204af4229SKyle Evans	if safe_mode == nil then
17304af4229SKyle Evans		safe_mode = not core.sm
174088b4f5fSWarner Losh	end
17504af4229SKyle Evans	if safe_mode then
176aedd6be5SKyle Evans		loader.setenv("kern.smp.disabled", "1")
177aedd6be5SKyle Evans		loader.setenv("hw.ata.ata_dma", "0")
178aedd6be5SKyle Evans		loader.setenv("hw.ata.atapi_dma", "0")
179aedd6be5SKyle Evans		loader.setenv("hw.ata.wc", "0")
180aedd6be5SKyle Evans		loader.setenv("hw.eisa_slots", "0")
181aedd6be5SKyle Evans		loader.setenv("kern.eventtimer.periodic", "1")
182aedd6be5SKyle Evans		loader.setenv("kern.geom.part.check_integrity", "0")
183088b4f5fSWarner Losh	else
184aedd6be5SKyle Evans		loader.unsetenv("kern.smp.disabled")
185aedd6be5SKyle Evans		loader.unsetenv("hw.ata.ata_dma")
186aedd6be5SKyle Evans		loader.unsetenv("hw.ata.atapi_dma")
187aedd6be5SKyle Evans		loader.unsetenv("hw.ata.wc")
188aedd6be5SKyle Evans		loader.unsetenv("hw.eisa_slots")
189aedd6be5SKyle Evans		loader.unsetenv("kern.eventtimer.periodic")
190aedd6be5SKyle Evans		loader.unsetenv("kern.geom.part.check_integrity")
191088b4f5fSWarner Losh	end
19204af4229SKyle Evans	core.sm = safe_mode
193088b4f5fSWarner Loshend
194088b4f5fSWarner Losh
195aea262bfSKyle Evansfunction core.clearCachedKernels()
19635b0c718SKyle Evans	-- Clear the kernel cache on config changes, autodetect might have
19735b0c718SKyle Evans	-- changed or if we've switched boot environments then we could have
19835b0c718SKyle Evans	-- a new kernel set.
19935b0c718SKyle Evans	core.cached_kernels = nil
20035b0c718SKyle Evansend
20135b0c718SKyle Evans
202088b4f5fSWarner Loshfunction core.kernelList()
20335b0c718SKyle Evans	if core.cached_kernels ~= nil then
20435b0c718SKyle Evans		return core.cached_kernels
20535b0c718SKyle Evans	end
20635b0c718SKyle Evans
207aedd6be5SKyle Evans	local k = loader.getenv("kernel")
2083f4eb56bSKyle Evans	local v = loader.getenv("kernels")
2093889e6cdSKyle Evans	local autodetect = loader.getenv("kernels_autodetect") or ""
210088b4f5fSWarner Losh
211aedd6be5SKyle Evans	local kernels = {}
212aedd6be5SKyle Evans	local unique = {}
213aedd6be5SKyle Evans	local i = 0
2149f71d421SKyle Evans	if k ~= nil then
215aedd6be5SKyle Evans		i = i + 1
216aedd6be5SKyle Evans		kernels[i] = k
217aedd6be5SKyle Evans		unique[k] = true
218088b4f5fSWarner Losh	end
219088b4f5fSWarner Losh
2203f4eb56bSKyle Evans	if v ~= nil then
221a5003419SKyle Evans		for n in v:gmatch("([^;, ]+)[;, ]?") do
2229f71d421SKyle Evans			if unique[n] == nil then
223aedd6be5SKyle Evans				i = i + 1
224aedd6be5SKyle Evans				kernels[i] = n
225aedd6be5SKyle Evans				unique[n] = true
226088b4f5fSWarner Losh			end
227088b4f5fSWarner Losh		end
2283889e6cdSKyle Evans	end
229a108046fSConrad Meyer
230c4dc9072SEmmanuel Vadot	-- Do not attempt to autodetect if underlying filesystem
231c4dc9072SEmmanuel Vadot	-- do not support directory listing (e.g. tftp, http)
232c4dc9072SEmmanuel Vadot	if not lfs.attributes("/boot", "mode") then
233c4dc9072SEmmanuel Vadot		autodetect = "no"
234c4dc9072SEmmanuel Vadot		loader.setenv("kernels_autodetect", "NO")
235c4dc9072SEmmanuel Vadot	end
236c4dc9072SEmmanuel Vadot
2373889e6cdSKyle Evans	-- Base whether we autodetect kernels or not on a loader.conf(5)
2383889e6cdSKyle Evans	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
2393889e6cdSKyle Evans	-- any kernels we detect based on the criteria described.
2403889e6cdSKyle Evans	if autodetect:lower() ~= "yes" then
24135b0c718SKyle Evans		core.cached_kernels = kernels
24235b0c718SKyle Evans		return core.cached_kernels
2433f4eb56bSKyle Evans	end
2443f4eb56bSKyle Evans
245a108046fSConrad Meyer	-- Automatically detect other bootable kernel directories using a
246a108046fSConrad Meyer	-- heuristic.  Any directory in /boot that contains an ordinary file
247a108046fSConrad Meyer	-- named "kernel" is considered eligible.
248e25ee296SKyle Evans	for file, ftype in lfs.dir("/boot") do
249aedd6be5SKyle Evans		local fname = "/boot/" .. file
250a108046fSConrad Meyer
2519f71d421SKyle Evans		if file == "." or file == ".." then
252aedd6be5SKyle Evans			goto continue
253a108046fSConrad Meyer		end
254a108046fSConrad Meyer
255e25ee296SKyle Evans		if ftype then
256e25ee296SKyle Evans			if ftype ~= lfs.DT_DIR then
257e25ee296SKyle Evans				goto continue
258e25ee296SKyle Evans			end
259e25ee296SKyle Evans		elseif lfs.attributes(fname, "mode") ~= "directory" then
260aedd6be5SKyle Evans			goto continue
261a108046fSConrad Meyer		end
262a108046fSConrad Meyer
2639f71d421SKyle Evans		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
264aedd6be5SKyle Evans			goto continue
265a108046fSConrad Meyer		end
266a108046fSConrad Meyer
2679f71d421SKyle Evans		if unique[file] == nil then
268aedd6be5SKyle Evans			i = i + 1
269aedd6be5SKyle Evans			kernels[i] = file
270aedd6be5SKyle Evans			unique[file] = true
271a108046fSConrad Meyer		end
272a108046fSConrad Meyer
273a108046fSConrad Meyer		::continue::
274a108046fSConrad Meyer	end
27535b0c718SKyle Evans	core.cached_kernels = kernels
27635b0c718SKyle Evans	return core.cached_kernels
277088b4f5fSWarner Loshend
278088b4f5fSWarner Losh
2797efc058fSKyle Evansfunction core.bootenvDefault()
2807efc058fSKyle Evans	return loader.getenv("zfs_be_active")
2817efc058fSKyle Evansend
2827efc058fSKyle Evans
2837efc058fSKyle Evansfunction core.bootenvList()
284277f38abSMariusz Zaborski	local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count"))
2857efc058fSKyle Evans	local bootenvs = {}
2867efc058fSKyle Evans	local curenv
2877efc058fSKyle Evans	local envcount = 0
2887efc058fSKyle Evans	local unique = {}
2897efc058fSKyle Evans
2907efc058fSKyle Evans	if bootenv_count == nil or bootenv_count <= 0 then
2917efc058fSKyle Evans		return bootenvs
2927efc058fSKyle Evans	end
2937efc058fSKyle Evans
2947efc058fSKyle Evans	-- Currently selected bootenv is always first/default
295277f38abSMariusz Zaborski	-- On the rewinded list the bootenv may not exists
296277f38abSMariusz Zaborski	if core.isRewinded() then
297277f38abSMariusz Zaborski		curenv = core.bootenvDefaultRewinded()
298277f38abSMariusz Zaborski	else
2997efc058fSKyle Evans		curenv = core.bootenvDefault()
300277f38abSMariusz Zaborski	end
3017efc058fSKyle Evans	if curenv ~= nil then
3027efc058fSKyle Evans		envcount = envcount + 1
3037efc058fSKyle Evans		bootenvs[envcount] = curenv
3047efc058fSKyle Evans		unique[curenv] = true
3057efc058fSKyle Evans	end
3067efc058fSKyle Evans
3077efc058fSKyle Evans	for curenv_idx = 0, bootenv_count - 1 do
308277f38abSMariusz Zaborski		curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]")
3097efc058fSKyle Evans		if curenv ~= nil and unique[curenv] == nil then
3107efc058fSKyle Evans			envcount = envcount + 1
3117efc058fSKyle Evans			bootenvs[envcount] = curenv
3127efc058fSKyle Evans			unique[curenv] = true
3137efc058fSKyle Evans		end
3147efc058fSKyle Evans	end
3157efc058fSKyle Evans	return bootenvs
3167efc058fSKyle Evansend
3177efc058fSKyle Evans
318277f38abSMariusz Zaborskifunction core.isCheckpointed()
319277f38abSMariusz Zaborski	return loader.getenv("zpool_checkpoint") ~= nil
320277f38abSMariusz Zaborskiend
321277f38abSMariusz Zaborski
322277f38abSMariusz Zaborskifunction core.bootenvDefaultRewinded()
323277f38abSMariusz Zaborski	local defname =  "zfs:!" .. string.sub(core.bootenvDefault(), 5)
324277f38abSMariusz Zaborski	local bootenv_count = tonumber("bootenvs_check_count")
325277f38abSMariusz Zaborski
326277f38abSMariusz Zaborski	if bootenv_count == nil or bootenv_count <= 0 then
327277f38abSMariusz Zaborski		return defname
328277f38abSMariusz Zaborski	end
329277f38abSMariusz Zaborski
330277f38abSMariusz Zaborski	for curenv_idx = 0, bootenv_count - 1 do
33194510c29SKyle Evans		local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]")
332277f38abSMariusz Zaborski		if curenv == defname then
333277f38abSMariusz Zaborski			return defname
334277f38abSMariusz Zaborski		end
335277f38abSMariusz Zaborski	end
336277f38abSMariusz Zaborski
337277f38abSMariusz Zaborski	return loader.getenv("bootenvs_check[0]")
338277f38abSMariusz Zaborskiend
339277f38abSMariusz Zaborski
340277f38abSMariusz Zaborskifunction core.isRewinded()
341277f38abSMariusz Zaborski	return bootenv_list == "bootenvs_check"
342277f38abSMariusz Zaborskiend
343277f38abSMariusz Zaborski
344277f38abSMariusz Zaborskifunction core.changeRewindCheckpoint()
345277f38abSMariusz Zaborski	if core.isRewinded() then
346277f38abSMariusz Zaborski		bootenv_list = "bootenvs"
347277f38abSMariusz Zaborski	else
348277f38abSMariusz Zaborski		bootenv_list = "bootenvs_check"
349277f38abSMariusz Zaborski	end
350277f38abSMariusz Zaborskiend
351277f38abSMariusz Zaborski
3525c73b3e0SColin Percivalfunction core.loadEntropy()
3535c73b3e0SColin Percival	if core.isUEFIBoot() then
3545c73b3e0SColin Percival		if (loader.getenv("entropy_efi_seed") or "no"):lower() == "yes" then
3555c73b3e0SColin Percival			loader.perform("efi-seed-entropy")
3565c73b3e0SColin Percival		end
3575c73b3e0SColin Percival	end
3585c73b3e0SColin Percivalend
3595c73b3e0SColin Percival
360088b4f5fSWarner Loshfunction core.setDefaults()
361e0f3dc82SR. Christian McDonald	core.setACPI(default_acpi)
3621613f091SKyle Evans	core.setSafeMode(default_safe_mode)
3631613f091SKyle Evans	core.setSingleUser(default_single_user)
3641613f091SKyle Evans	core.setVerbose(default_verbose)
365088b4f5fSWarner Loshend
366088b4f5fSWarner Losh
3676d4ed94dSKyle Evansfunction core.autoboot(argstr)
368a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
369a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
370aedd6be5SKyle Evans		config.loadelf()
371a2a7830eSKyle Evans	end
3725c73b3e0SColin Percival	core.loadEntropy()
373322a2dddSKyle Evans	loader.perform(composeLoaderCmd("autoboot", argstr))
374088b4f5fSWarner Loshend
375088b4f5fSWarner Losh
3766d4ed94dSKyle Evansfunction core.boot(argstr)
377a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
378a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
379aedd6be5SKyle Evans		config.loadelf()
380a2a7830eSKyle Evans	end
3815c73b3e0SColin Percival	core.loadEntropy()
382322a2dddSKyle Evans	loader.perform(composeLoaderCmd("boot", argstr))
383088b4f5fSWarner Loshend
384088b4f5fSWarner Losh
385e07fc39cSKyle Evansfunction core.isSingleUserBoot()
386aedd6be5SKyle Evans	local single_user = loader.getenv("boot_single")
387aedd6be5SKyle Evans	return single_user ~= nil and single_user:lower() == "yes"
388e07fc39cSKyle Evansend
389e07fc39cSKyle Evans
3905f8cfbe1SKyle Evansfunction core.isUEFIBoot()
3915f8cfbe1SKyle Evans	local efiver = loader.getenv("efi-version")
3925f8cfbe1SKyle Evans
3935f8cfbe1SKyle Evans	return efiver ~= nil
3945f8cfbe1SKyle Evansend
3955f8cfbe1SKyle Evans
3967efc058fSKyle Evansfunction core.isZFSBoot()
3977efc058fSKyle Evans	local c = loader.getenv("currdev")
3987efc058fSKyle Evans
3997efc058fSKyle Evans	if c ~= nil then
4007efc058fSKyle Evans		return c:match("^zfs:") ~= nil
4017efc058fSKyle Evans	end
4027efc058fSKyle Evans	return false
4037efc058fSKyle Evansend
4047efc058fSKyle Evans
4053630506bSToomas Soomefunction core.isFramebufferConsole()
4063630506bSToomas Soome	local c = loader.getenv("console")
4073630506bSToomas Soome	if c ~= nil then
4083630506bSToomas Soome		if c:find("efi") == nil and c:find("vidconsole") == nil then
4093630506bSToomas Soome			return false
4103630506bSToomas Soome		end
4113630506bSToomas Soome		if loader.getenv("screen.depth") ~= nil then
4123630506bSToomas Soome			return true
4133630506bSToomas Soome		end
4143630506bSToomas Soome	end
4153630506bSToomas Soome	return false
4163630506bSToomas Soomeend
4173630506bSToomas Soome
41890a25417SKyle Evansfunction core.isSerialConsole()
41990a25417SKyle Evans	local c = loader.getenv("console")
42090a25417SKyle Evans	if c ~= nil then
4215d8c062fSToomas Soome		-- serial console is comconsole, but also userboot.
4225d8c062fSToomas Soome		-- userboot is there, because we have no way to know
4235d8c062fSToomas Soome		-- if the user terminal can draw unicode box chars or not.
4245d8c062fSToomas Soome		if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then
42590a25417SKyle Evans			return true
42690a25417SKyle Evans		end
42790a25417SKyle Evans	end
42890a25417SKyle Evans	return false
42990a25417SKyle Evansend
43090a25417SKyle Evans
431b140d14bSKyle Evansfunction core.isSerialBoot()
432aedd6be5SKyle Evans	local s = loader.getenv("boot_serial")
4339f71d421SKyle Evans	if s ~= nil then
434aedd6be5SKyle Evans		return true
435088b4f5fSWarner Losh	end
436088b4f5fSWarner Losh
437aedd6be5SKyle Evans	local m = loader.getenv("boot_multicons")
4389f71d421SKyle Evans	if m ~= nil then
439aedd6be5SKyle Evans		return true
440088b4f5fSWarner Losh	end
441aedd6be5SKyle Evans	return false
442088b4f5fSWarner Loshend
443088b4f5fSWarner Losh
4449937e979SKyle Evans-- Is the menu skipped in the environment in which we've booted?
4459937e979SKyle Evansfunction core.isMenuSkipped()
446b83a355dSKyle Evans	return string.lower(loader.getenv("beastie_disable") or "") == "yes"
4479937e979SKyle Evansend
4489937e979SKyle Evans
4495c1b5165SKyle Evans-- This may be a better candidate for a 'utility' module.
450ee4e69f1SKyle Evansfunction core.deepCopyTable(tbl)
451aedd6be5SKyle Evans	local new_tbl = {}
4525c1b5165SKyle Evans	for k, v in pairs(tbl) do
4539f71d421SKyle Evans		if type(v) == "table" then
454ee4e69f1SKyle Evans			new_tbl[k] = core.deepCopyTable(v)
4555c1b5165SKyle Evans		else
456aedd6be5SKyle Evans			new_tbl[k] = v
4575c1b5165SKyle Evans		end
4585c1b5165SKyle Evans	end
459aedd6be5SKyle Evans	return new_tbl
4605c1b5165SKyle Evansend
4615c1b5165SKyle Evans
4626d4ed94dSKyle Evans-- XXX This should go away if we get the table lib into shape for importing.
4636d4ed94dSKyle Evans-- As of now, it requires some 'os' functions, so we'll implement this in lua
4646d4ed94dSKyle Evans-- for our uses
4656d4ed94dSKyle Evansfunction core.popFrontTable(tbl)
4666d4ed94dSKyle Evans	-- Shouldn't reasonably happen
4679f71d421SKyle Evans	if #tbl == 0 then
468aedd6be5SKyle Evans		return nil, nil
4699f71d421SKyle Evans	elseif #tbl == 1 then
470aedd6be5SKyle Evans		return tbl[1], {}
4716d4ed94dSKyle Evans	end
4726d4ed94dSKyle Evans
473aedd6be5SKyle Evans	local first_value = tbl[1]
474aedd6be5SKyle Evans	local new_tbl = {}
4756d4ed94dSKyle Evans	-- This is not a cheap operation
4766d4ed94dSKyle Evans	for k, v in ipairs(tbl) do
4779f71d421SKyle Evans		if k > 1 then
478aedd6be5SKyle Evans			new_tbl[k - 1] = v
4796d4ed94dSKyle Evans		end
4806d4ed94dSKyle Evans	end
4816d4ed94dSKyle Evans
482aedd6be5SKyle Evans	return first_value, new_tbl
4836d4ed94dSKyle Evansend
4846d4ed94dSKyle Evans
4858f3b3610SWarner Loshfunction core.getConsoleName()
4868f3b3610SWarner Losh	if loader.getenv("boot_multicons") ~= nil then
4878f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4888f3b3610SWarner Losh			return "Dual (Serial primary)"
4898f3b3610SWarner Losh		else
4908f3b3610SWarner Losh			return "Dual (Video primary)"
4918f3b3610SWarner Losh		end
4928f3b3610SWarner Losh	else
4938f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
4948f3b3610SWarner Losh			return "Serial"
4958f3b3610SWarner Losh		else
4968f3b3610SWarner Losh			return "Video"
4978f3b3610SWarner Losh		end
4988f3b3610SWarner Losh	end
4998f3b3610SWarner Loshend
5008f3b3610SWarner Losh
5018f3b3610SWarner Loshfunction core.nextConsoleChoice()
5028f3b3610SWarner Losh	if loader.getenv("boot_multicons") ~= nil then
5038f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
5048f3b3610SWarner Losh			loader.unsetenv("boot_serial")
5058f3b3610SWarner Losh		else
5068f3b3610SWarner Losh			loader.unsetenv("boot_multicons")
5078f3b3610SWarner Losh			loader.setenv("boot_serial", "YES")
5088f3b3610SWarner Losh		end
5098f3b3610SWarner Losh	else
5108f3b3610SWarner Losh		if loader.getenv("boot_serial") ~= nil then
5118f3b3610SWarner Losh			loader.unsetenv("boot_serial")
5128f3b3610SWarner Losh		else
5138f3b3610SWarner Losh			loader.setenv("boot_multicons", "YES")
5148f3b3610SWarner Losh			loader.setenv("boot_serial", "YES")
5158f3b3610SWarner Losh		end
5168f3b3610SWarner Losh	end
5178f3b3610SWarner Loshend
5188f3b3610SWarner Losh
5191613f091SKyle EvansrecordDefaults()
520aea262bfSKyle Evanshook.register("config.reloaded", core.clearCachedKernels)
521aedd6be5SKyle Evansreturn core
522