xref: /freebsd/stand/lua/core.lua (revision 5beb5507121c66e9af38523735b3a6d94310d8a8)
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
41322a2dddSKyle Evanslocal function composeLoaderCmd(cmd_name, argstr)
429f71d421SKyle Evans	if argstr ~= nil then
43aedd6be5SKyle Evans		cmd_name = cmd_name .. " " .. argstr
446d4ed94dSKyle Evans	end
45aedd6be5SKyle Evans	return cmd_name
466d4ed94dSKyle Evansend
476d4ed94dSKyle Evans
481613f091SKyle Evanslocal function recordDefaults()
491613f091SKyle Evans	-- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386,
501613f091SKyle Evans	-- it will generally be set upon execution of the kernel. Because of
511613f091SKyle Evans	-- this, we can't (or don't really want to) detect/disable ACPI on !i386
521613f091SKyle Evans	-- reliably. Just set it enabled if we detect it and leave well enough
531613f091SKyle Evans	-- alone if we don't.
541613f091SKyle Evans	local boot_acpi = core.isSystem386() and core.getACPIPresent(false)
551613f091SKyle Evans	local boot_single = loader.getenv("boot_single") or "no"
561613f091SKyle Evans	local boot_verbose = loader.getenv("boot_verbose") or "no"
571613f091SKyle Evans	default_single_user = boot_single:lower() ~= "no"
581613f091SKyle Evans	default_verbose = boot_verbose:lower() ~= "no"
591613f091SKyle Evans
601613f091SKyle Evans	if boot_acpi then
611613f091SKyle Evans		core.setACPI(true)
621613f091SKyle Evans	end
631613f091SKyle Evans	core.setSingleUser(default_single_user)
641613f091SKyle Evans	core.setVerbose(default_verbose)
651613f091SKyle Evansend
661613f091SKyle Evans
671613f091SKyle Evans
6807faaf78SKyle Evans-- Globals
69*5beb5507SKyle Evans-- try_include will return the loaded module on success, or false and the error
70*5beb5507SKyle Evans-- message on failure.
7107faaf78SKyle Evansfunction try_include(module)
7207faaf78SKyle Evans	local status, ret = pcall(require, module)
7307faaf78SKyle Evans	-- ret is the module if we succeeded.
7407faaf78SKyle Evans	if status then
7507faaf78SKyle Evans		return ret
7607faaf78SKyle Evans	end
77*5beb5507SKyle Evans	return false, ret
7807faaf78SKyle Evansend
7907faaf78SKyle Evans
80b5746545SKyle Evans-- Module exports
81fe672a15SKyle Evans-- Commonly appearing constants
82aedd6be5SKyle Evanscore.KEY_BACKSPACE	= 8
83aedd6be5SKyle Evanscore.KEY_ENTER		= 13
84aedd6be5SKyle Evanscore.KEY_DELETE		= 127
85fe672a15SKyle Evans
86230061c5SKyle Evans-- Note that this is a decimal representation, despite the leading 0 that in
87230061c5SKyle Evans-- other contexts (outside of Lua) may mean 'octal'
88aedd6be5SKyle Evanscore.KEYSTR_ESCAPE	= "\027"
892bb86aefSKyle Evanscore.KEYSTR_CSI		= core.KEYSTR_ESCAPE .. "["
9039006570SKyle Evans
91aedd6be5SKyle Evanscore.MENU_RETURN	= "return"
92aedd6be5SKyle Evanscore.MENU_ENTRY		= "entry"
93aedd6be5SKyle Evanscore.MENU_SEPARATOR	= "separator"
94aedd6be5SKyle Evanscore.MENU_SUBMENU	= "submenu"
95aedd6be5SKyle Evanscore.MENU_CAROUSEL_ENTRY	= "carousel_entry"
96a7cf0562SKyle Evans
9704af4229SKyle Evansfunction core.setVerbose(verbose)
9804af4229SKyle Evans	if verbose == nil then
9904af4229SKyle Evans		verbose = not core.verbose
100088b4f5fSWarner Losh	end
101088b4f5fSWarner Losh
10204af4229SKyle Evans	if verbose then
103aedd6be5SKyle Evans		loader.setenv("boot_verbose", "YES")
104088b4f5fSWarner Losh	else
105aedd6be5SKyle Evans		loader.unsetenv("boot_verbose")
106088b4f5fSWarner Losh	end
10704af4229SKyle Evans	core.verbose = verbose
108088b4f5fSWarner Loshend
109088b4f5fSWarner Losh
11004af4229SKyle Evansfunction core.setSingleUser(single_user)
11104af4229SKyle Evans	if single_user == nil then
11204af4229SKyle Evans		single_user = not core.su
113088b4f5fSWarner Losh	end
114088b4f5fSWarner Losh
11504af4229SKyle Evans	if single_user then
116aedd6be5SKyle Evans		loader.setenv("boot_single", "YES")
117088b4f5fSWarner Losh	else
118aedd6be5SKyle Evans		loader.unsetenv("boot_single")
119088b4f5fSWarner Losh	end
12004af4229SKyle Evans	core.su = single_user
121088b4f5fSWarner Loshend
122088b4f5fSWarner Losh
12304af4229SKyle Evansfunction core.getACPIPresent(checking_system_defaults)
124aedd6be5SKyle Evans	local c = loader.getenv("hint.acpi.0.rsdp")
1256401094fSKyle Evans
1269f71d421SKyle Evans	if c ~= nil then
12704af4229SKyle Evans		if checking_system_defaults then
128aedd6be5SKyle Evans			return true
1296401094fSKyle Evans		end
1306401094fSKyle Evans		-- Otherwise, respect disabled if it's set
131aedd6be5SKyle Evans		c = loader.getenv("hint.acpi.0.disabled")
1329f71d421SKyle Evans		return c == nil or tonumber(c) ~= 1
1336401094fSKyle Evans	end
134aedd6be5SKyle Evans	return false
1356401094fSKyle Evansend
1366401094fSKyle Evans
13704af4229SKyle Evansfunction core.setACPI(acpi)
13804af4229SKyle Evans	if acpi == nil then
13904af4229SKyle Evans		acpi = not core.acpi
140088b4f5fSWarner Losh	end
141088b4f5fSWarner Losh
14204af4229SKyle Evans	if acpi then
143aedd6be5SKyle Evans		loader.setenv("acpi_load", "YES")
144aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "0")
145aedd6be5SKyle Evans		loader.unsetenv("loader.acpi_disabled_by_user")
146088b4f5fSWarner Losh	else
147aedd6be5SKyle Evans		loader.unsetenv("acpi_load")
148aedd6be5SKyle Evans		loader.setenv("hint.acpi.0.disabled", "1")
149aedd6be5SKyle Evans		loader.setenv("loader.acpi_disabled_by_user", "1")
150088b4f5fSWarner Losh	end
15104af4229SKyle Evans	core.acpi = acpi
152088b4f5fSWarner Loshend
153088b4f5fSWarner Losh
15404af4229SKyle Evansfunction core.setSafeMode(safe_mode)
15504af4229SKyle Evans	if safe_mode == nil then
15604af4229SKyle Evans		safe_mode = not core.sm
157088b4f5fSWarner Losh	end
15804af4229SKyle Evans	if safe_mode then
159aedd6be5SKyle Evans		loader.setenv("kern.smp.disabled", "1")
160aedd6be5SKyle Evans		loader.setenv("hw.ata.ata_dma", "0")
161aedd6be5SKyle Evans		loader.setenv("hw.ata.atapi_dma", "0")
162aedd6be5SKyle Evans		loader.setenv("hw.ata.wc", "0")
163aedd6be5SKyle Evans		loader.setenv("hw.eisa_slots", "0")
164aedd6be5SKyle Evans		loader.setenv("kern.eventtimer.periodic", "1")
165aedd6be5SKyle Evans		loader.setenv("kern.geom.part.check_integrity", "0")
166088b4f5fSWarner Losh	else
167aedd6be5SKyle Evans		loader.unsetenv("kern.smp.disabled")
168aedd6be5SKyle Evans		loader.unsetenv("hw.ata.ata_dma")
169aedd6be5SKyle Evans		loader.unsetenv("hw.ata.atapi_dma")
170aedd6be5SKyle Evans		loader.unsetenv("hw.ata.wc")
171aedd6be5SKyle Evans		loader.unsetenv("hw.eisa_slots")
172aedd6be5SKyle Evans		loader.unsetenv("kern.eventtimer.periodic")
173aedd6be5SKyle Evans		loader.unsetenv("kern.geom.part.check_integrity")
174088b4f5fSWarner Losh	end
17504af4229SKyle Evans	core.sm = safe_mode
176088b4f5fSWarner Loshend
177088b4f5fSWarner Losh
178aea262bfSKyle Evansfunction core.clearCachedKernels()
17935b0c718SKyle Evans	-- Clear the kernel cache on config changes, autodetect might have
18035b0c718SKyle Evans	-- changed or if we've switched boot environments then we could have
18135b0c718SKyle Evans	-- a new kernel set.
18235b0c718SKyle Evans	core.cached_kernels = nil
18335b0c718SKyle Evansend
18435b0c718SKyle Evans
185088b4f5fSWarner Loshfunction core.kernelList()
18635b0c718SKyle Evans	if core.cached_kernels ~= nil then
18735b0c718SKyle Evans		return core.cached_kernels
18835b0c718SKyle Evans	end
18935b0c718SKyle Evans
190aedd6be5SKyle Evans	local k = loader.getenv("kernel")
1913f4eb56bSKyle Evans	local v = loader.getenv("kernels")
1923889e6cdSKyle Evans	local autodetect = loader.getenv("kernels_autodetect") or ""
193088b4f5fSWarner Losh
194aedd6be5SKyle Evans	local kernels = {}
195aedd6be5SKyle Evans	local unique = {}
196aedd6be5SKyle Evans	local i = 0
1979f71d421SKyle Evans	if k ~= nil then
198aedd6be5SKyle Evans		i = i + 1
199aedd6be5SKyle Evans		kernels[i] = k
200aedd6be5SKyle Evans		unique[k] = true
201088b4f5fSWarner Losh	end
202088b4f5fSWarner Losh
2033f4eb56bSKyle Evans	if v ~= nil then
204a5003419SKyle Evans		for n in v:gmatch("([^;, ]+)[;, ]?") do
2059f71d421SKyle Evans			if unique[n] == nil then
206aedd6be5SKyle Evans				i = i + 1
207aedd6be5SKyle Evans				kernels[i] = n
208aedd6be5SKyle Evans				unique[n] = true
209088b4f5fSWarner Losh			end
210088b4f5fSWarner Losh		end
2113889e6cdSKyle Evans	end
212a108046fSConrad Meyer
2133889e6cdSKyle Evans	-- Base whether we autodetect kernels or not on a loader.conf(5)
2143889e6cdSKyle Evans	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
2153889e6cdSKyle Evans	-- any kernels we detect based on the criteria described.
2163889e6cdSKyle Evans	if autodetect:lower() ~= "yes" then
21735b0c718SKyle Evans		core.cached_kernels = kernels
21835b0c718SKyle Evans		return core.cached_kernels
2193f4eb56bSKyle Evans	end
2203f4eb56bSKyle Evans
221a108046fSConrad Meyer	-- Automatically detect other bootable kernel directories using a
222a108046fSConrad Meyer	-- heuristic.  Any directory in /boot that contains an ordinary file
223a108046fSConrad Meyer	-- named "kernel" is considered eligible.
224a108046fSConrad Meyer	for file in lfs.dir("/boot") do
225aedd6be5SKyle Evans		local fname = "/boot/" .. file
226a108046fSConrad Meyer
2279f71d421SKyle Evans		if file == "." or file == ".." then
228aedd6be5SKyle Evans			goto continue
229a108046fSConrad Meyer		end
230a108046fSConrad Meyer
2319f71d421SKyle Evans		if lfs.attributes(fname, "mode") ~= "directory" then
232aedd6be5SKyle Evans			goto continue
233a108046fSConrad Meyer		end
234a108046fSConrad Meyer
2359f71d421SKyle Evans		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
236aedd6be5SKyle Evans			goto continue
237a108046fSConrad Meyer		end
238a108046fSConrad Meyer
2399f71d421SKyle Evans		if unique[file] == nil then
240aedd6be5SKyle Evans			i = i + 1
241aedd6be5SKyle Evans			kernels[i] = file
242aedd6be5SKyle Evans			unique[file] = true
243a108046fSConrad Meyer		end
244a108046fSConrad Meyer
245a108046fSConrad Meyer		::continue::
246a108046fSConrad Meyer	end
24735b0c718SKyle Evans	core.cached_kernels = kernels
24835b0c718SKyle Evans	return core.cached_kernels
249088b4f5fSWarner Loshend
250088b4f5fSWarner Losh
2517efc058fSKyle Evansfunction core.bootenvDefault()
2527efc058fSKyle Evans	return loader.getenv("zfs_be_active")
2537efc058fSKyle Evansend
2547efc058fSKyle Evans
2557efc058fSKyle Evansfunction core.bootenvList()
2567efc058fSKyle Evans	local bootenv_count = tonumber(loader.getenv("bootenvs_count"))
2577efc058fSKyle Evans	local bootenvs = {}
2587efc058fSKyle Evans	local curenv
2597efc058fSKyle Evans	local envcount = 0
2607efc058fSKyle Evans	local unique = {}
2617efc058fSKyle Evans
2627efc058fSKyle Evans	if bootenv_count == nil or bootenv_count <= 0 then
2637efc058fSKyle Evans		return bootenvs
2647efc058fSKyle Evans	end
2657efc058fSKyle Evans
2667efc058fSKyle Evans	-- Currently selected bootenv is always first/default
2677efc058fSKyle Evans	curenv = core.bootenvDefault()
2687efc058fSKyle Evans	if curenv ~= nil then
2697efc058fSKyle Evans		envcount = envcount + 1
2707efc058fSKyle Evans		bootenvs[envcount] = curenv
2717efc058fSKyle Evans		unique[curenv] = true
2727efc058fSKyle Evans	end
2737efc058fSKyle Evans
2747efc058fSKyle Evans	for curenv_idx = 0, bootenv_count - 1 do
2757efc058fSKyle Evans		curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]")
2767efc058fSKyle Evans		if curenv ~= nil and unique[curenv] == nil then
2777efc058fSKyle Evans			envcount = envcount + 1
2787efc058fSKyle Evans			bootenvs[envcount] = curenv
2797efc058fSKyle Evans			unique[curenv] = true
2807efc058fSKyle Evans		end
2817efc058fSKyle Evans	end
2827efc058fSKyle Evans	return bootenvs
2837efc058fSKyle Evansend
2847efc058fSKyle Evans
285088b4f5fSWarner Loshfunction core.setDefaults()
286aedd6be5SKyle Evans	core.setACPI(core.getACPIPresent(true))
2871613f091SKyle Evans	core.setSafeMode(default_safe_mode)
2881613f091SKyle Evans	core.setSingleUser(default_single_user)
2891613f091SKyle Evans	core.setVerbose(default_verbose)
290088b4f5fSWarner Loshend
291088b4f5fSWarner Losh
2926d4ed94dSKyle Evansfunction core.autoboot(argstr)
293a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
294a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
295aedd6be5SKyle Evans		config.loadelf()
296a2a7830eSKyle Evans	end
297322a2dddSKyle Evans	loader.perform(composeLoaderCmd("autoboot", argstr))
298088b4f5fSWarner Loshend
299088b4f5fSWarner Losh
3006d4ed94dSKyle Evansfunction core.boot(argstr)
301a2a7830eSKyle Evans	-- loadelf() only if we've not already loaded a kernel
302a2a7830eSKyle Evans	if loader.getenv("kernelname") == nil then
303aedd6be5SKyle Evans		config.loadelf()
304a2a7830eSKyle Evans	end
305322a2dddSKyle Evans	loader.perform(composeLoaderCmd("boot", argstr))
306088b4f5fSWarner Loshend
307088b4f5fSWarner Losh
308e07fc39cSKyle Evansfunction core.isSingleUserBoot()
309aedd6be5SKyle Evans	local single_user = loader.getenv("boot_single")
310aedd6be5SKyle Evans	return single_user ~= nil and single_user:lower() == "yes"
311e07fc39cSKyle Evansend
312e07fc39cSKyle Evans
3135f8cfbe1SKyle Evansfunction core.isUEFIBoot()
3145f8cfbe1SKyle Evans	local efiver = loader.getenv("efi-version")
3155f8cfbe1SKyle Evans
3165f8cfbe1SKyle Evans	return efiver ~= nil
3175f8cfbe1SKyle Evansend
3185f8cfbe1SKyle Evans
3197efc058fSKyle Evansfunction core.isZFSBoot()
3207efc058fSKyle Evans	local c = loader.getenv("currdev")
3217efc058fSKyle Evans
3227efc058fSKyle Evans	if c ~= nil then
3237efc058fSKyle Evans		return c:match("^zfs:") ~= nil
3247efc058fSKyle Evans	end
3257efc058fSKyle Evans	return false
3267efc058fSKyle Evansend
3277efc058fSKyle Evans
328b140d14bSKyle Evansfunction core.isSerialBoot()
329aedd6be5SKyle Evans	local s = loader.getenv("boot_serial")
3309f71d421SKyle Evans	if s ~= nil then
331aedd6be5SKyle Evans		return true
332088b4f5fSWarner Losh	end
333088b4f5fSWarner Losh
334aedd6be5SKyle Evans	local m = loader.getenv("boot_multicons")
3359f71d421SKyle Evans	if m ~= nil then
336aedd6be5SKyle Evans		return true
337088b4f5fSWarner Losh	end
338aedd6be5SKyle Evans	return false
339088b4f5fSWarner Loshend
340088b4f5fSWarner Losh
341c1ab36f5SKyle Evansfunction core.isSystem386()
3429f71d421SKyle Evans	return loader.machine_arch == "i386"
343c1ab36f5SKyle Evansend
344c1ab36f5SKyle Evans
3459937e979SKyle Evans-- Is the menu skipped in the environment in which we've booted?
3469937e979SKyle Evansfunction core.isMenuSkipped()
347b83a355dSKyle Evans	return string.lower(loader.getenv("beastie_disable") or "") == "yes"
3489937e979SKyle Evansend
3499937e979SKyle Evans
3505c1b5165SKyle Evans-- This may be a better candidate for a 'utility' module.
351ee4e69f1SKyle Evansfunction core.deepCopyTable(tbl)
352aedd6be5SKyle Evans	local new_tbl = {}
3535c1b5165SKyle Evans	for k, v in pairs(tbl) do
3549f71d421SKyle Evans		if type(v) == "table" then
355ee4e69f1SKyle Evans			new_tbl[k] = core.deepCopyTable(v)
3565c1b5165SKyle Evans		else
357aedd6be5SKyle Evans			new_tbl[k] = v
3585c1b5165SKyle Evans		end
3595c1b5165SKyle Evans	end
360aedd6be5SKyle Evans	return new_tbl
3615c1b5165SKyle Evansend
3625c1b5165SKyle Evans
3636d4ed94dSKyle Evans-- XXX This should go away if we get the table lib into shape for importing.
3646d4ed94dSKyle Evans-- As of now, it requires some 'os' functions, so we'll implement this in lua
3656d4ed94dSKyle Evans-- for our uses
3666d4ed94dSKyle Evansfunction core.popFrontTable(tbl)
3676d4ed94dSKyle Evans	-- Shouldn't reasonably happen
3689f71d421SKyle Evans	if #tbl == 0 then
369aedd6be5SKyle Evans		return nil, nil
3709f71d421SKyle Evans	elseif #tbl == 1 then
371aedd6be5SKyle Evans		return tbl[1], {}
3726d4ed94dSKyle Evans	end
3736d4ed94dSKyle Evans
374aedd6be5SKyle Evans	local first_value = tbl[1]
375aedd6be5SKyle Evans	local new_tbl = {}
3766d4ed94dSKyle Evans	-- This is not a cheap operation
3776d4ed94dSKyle Evans	for k, v in ipairs(tbl) do
3789f71d421SKyle Evans		if k > 1 then
379aedd6be5SKyle Evans			new_tbl[k - 1] = v
3806d4ed94dSKyle Evans		end
3816d4ed94dSKyle Evans	end
3826d4ed94dSKyle Evans
383aedd6be5SKyle Evans	return first_value, new_tbl
3846d4ed94dSKyle Evansend
3856d4ed94dSKyle Evans
3861613f091SKyle EvansrecordDefaults()
387aea262bfSKyle Evanshook.register("config.reloaded", core.clearCachedKernels)
388aedd6be5SKyle Evansreturn core
389