xref: /freebsd/stand/lua/core.lua (revision 74fe6c29fb7eef3418d7919dcd41dc1a04a982a1)
1--
2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3--
4-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6-- All rights reserved.
7--
8-- Redistribution and use in source and binary forms, with or without
9-- modification, are permitted provided that the following conditions
10-- are met:
11-- 1. Redistributions of source code must retain the above copyright
12--    notice, this list of conditions and the following disclaimer.
13-- 2. Redistributions in binary form must reproduce the above copyright
14--    notice, this list of conditions and the following disclaimer in the
15--    documentation and/or other materials provided with the distribution.
16--
17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27-- SUCH DAMAGE.
28--
29-- $FreeBSD$
30--
31
32local config = require("config")
33
34local core = {}
35
36local function composeLoaderCmd(cmd_name, argstr)
37	if argstr ~= nil then
38		cmd_name = cmd_name .. " " .. argstr
39	end
40	return cmd_name
41end
42
43-- Module exports
44-- Commonly appearing constants
45core.KEY_BACKSPACE	= 8
46core.KEY_ENTER		= 13
47core.KEY_DELETE		= 127
48
49-- Note that this is a decimal representation, despite the leading 0 that in
50-- other contexts (outside of Lua) may mean 'octal'
51core.KEYSTR_ESCAPE	= "\027"
52core.KEYSTR_CSI		= core.KEYSTR_ESCAPE .. "["
53
54core.MENU_RETURN	= "return"
55core.MENU_ENTRY		= "entry"
56core.MENU_SEPARATOR	= "separator"
57core.MENU_SUBMENU	= "submenu"
58core.MENU_CAROUSEL_ENTRY	= "carousel_entry"
59
60function core.setVerbose(verbose)
61	if verbose == nil then
62		verbose = not core.verbose
63	end
64
65	if verbose then
66		loader.setenv("boot_verbose", "YES")
67	else
68		loader.unsetenv("boot_verbose")
69	end
70	core.verbose = verbose
71end
72
73function core.setSingleUser(single_user)
74	if single_user == nil then
75		single_user = not core.su
76	end
77
78	if single_user then
79		loader.setenv("boot_single", "YES")
80	else
81		loader.unsetenv("boot_single")
82	end
83	core.su = single_user
84end
85
86function core.getACPIPresent(checking_system_defaults)
87	local c = loader.getenv("hint.acpi.0.rsdp")
88
89	if c ~= nil then
90		if checking_system_defaults then
91			return true
92		end
93		-- Otherwise, respect disabled if it's set
94		c = loader.getenv("hint.acpi.0.disabled")
95		return c == nil or tonumber(c) ~= 1
96	end
97	return false
98end
99
100function core.setACPI(acpi)
101	if acpi == nil then
102		acpi = not core.acpi
103	end
104
105	if acpi then
106		loader.setenv("acpi_load", "YES")
107		loader.setenv("hint.acpi.0.disabled", "0")
108		loader.unsetenv("loader.acpi_disabled_by_user")
109	else
110		loader.unsetenv("acpi_load")
111		loader.setenv("hint.acpi.0.disabled", "1")
112		loader.setenv("loader.acpi_disabled_by_user", "1")
113	end
114	core.acpi = acpi
115end
116
117function core.setSafeMode(safe_mode)
118	if safe_mode == nil then
119		safe_mode = not core.sm
120	end
121	if safe_mode then
122		loader.setenv("kern.smp.disabled", "1")
123		loader.setenv("hw.ata.ata_dma", "0")
124		loader.setenv("hw.ata.atapi_dma", "0")
125		loader.setenv("hw.ata.wc", "0")
126		loader.setenv("hw.eisa_slots", "0")
127		loader.setenv("kern.eventtimer.periodic", "1")
128		loader.setenv("kern.geom.part.check_integrity", "0")
129	else
130		loader.unsetenv("kern.smp.disabled")
131		loader.unsetenv("hw.ata.ata_dma")
132		loader.unsetenv("hw.ata.atapi_dma")
133		loader.unsetenv("hw.ata.wc")
134		loader.unsetenv("hw.eisa_slots")
135		loader.unsetenv("kern.eventtimer.periodic")
136		loader.unsetenv("kern.geom.part.check_integrity")
137	end
138	core.sm = safe_mode
139end
140
141function core.configReloaded()
142	-- Clear the kernel cache on config changes, autodetect might have
143	-- changed or if we've switched boot environments then we could have
144	-- a new kernel set.
145	core.cached_kernels = nil
146end
147
148function core.kernelList()
149	if core.cached_kernels ~= nil then
150		return core.cached_kernels
151	end
152
153	local k = loader.getenv("kernel")
154	local v = loader.getenv("kernels")
155	local autodetect = loader.getenv("kernels_autodetect") or ""
156
157	local kernels = {}
158	local unique = {}
159	local i = 0
160	if k ~= nil then
161		i = i + 1
162		kernels[i] = k
163		unique[k] = true
164	end
165
166	if v ~= nil then
167		for n in v:gmatch("([^; ]+)[; ]?") do
168			if unique[n] == nil then
169				i = i + 1
170				kernels[i] = n
171				unique[n] = true
172			end
173		end
174	end
175
176	-- Base whether we autodetect kernels or not on a loader.conf(5)
177	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
178	-- any kernels we detect based on the criteria described.
179	if autodetect:lower() ~= "yes" then
180		core.cached_kernels = kernels
181		return core.cached_kernels
182	end
183
184	-- Automatically detect other bootable kernel directories using a
185	-- heuristic.  Any directory in /boot that contains an ordinary file
186	-- named "kernel" is considered eligible.
187	for file in lfs.dir("/boot") do
188		local fname = "/boot/" .. file
189
190		if file == "." or file == ".." then
191			goto continue
192		end
193
194		if lfs.attributes(fname, "mode") ~= "directory" then
195			goto continue
196		end
197
198		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
199			goto continue
200		end
201
202		if unique[file] == nil then
203			i = i + 1
204			kernels[i] = file
205			unique[file] = true
206		end
207
208		::continue::
209	end
210	core.cached_kernels = kernels
211	return core.cached_kernels
212end
213
214function core.bootenvDefault()
215	return loader.getenv("zfs_be_active")
216end
217
218function core.bootenvList()
219	local bootenv_count = tonumber(loader.getenv("bootenvs_count"))
220	local bootenvs = {}
221	local curenv
222	local envcount = 0
223	local unique = {}
224
225	if bootenv_count == nil or bootenv_count <= 0 then
226		return bootenvs
227	end
228
229	-- Currently selected bootenv is always first/default
230	curenv = core.bootenvDefault()
231	if curenv ~= nil then
232		envcount = envcount + 1
233		bootenvs[envcount] = curenv
234		unique[curenv] = true
235	end
236
237	for curenv_idx = 0, bootenv_count - 1 do
238		curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]")
239		if curenv ~= nil and unique[curenv] == nil then
240			envcount = envcount + 1
241			bootenvs[envcount] = curenv
242			unique[curenv] = true
243		end
244	end
245	return bootenvs
246end
247
248function core.setDefaults()
249	core.setACPI(core.getACPIPresent(true))
250	core.setSafeMode(false)
251	core.setSingleUser(false)
252	core.setVerbose(false)
253end
254
255function core.autoboot(argstr)
256	-- loadelf() only if we've not already loaded a kernel
257	if loader.getenv("kernelname") == nil then
258		config.loadelf()
259	end
260	loader.perform(composeLoaderCmd("autoboot", argstr))
261end
262
263function core.boot(argstr)
264	-- loadelf() only if we've not already loaded a kernel
265	if loader.getenv("kernelname") == nil then
266		config.loadelf()
267	end
268	loader.perform(composeLoaderCmd("boot", argstr))
269end
270
271function core.isSingleUserBoot()
272	local single_user = loader.getenv("boot_single")
273	return single_user ~= nil and single_user:lower() == "yes"
274end
275
276function core.isZFSBoot()
277	local c = loader.getenv("currdev")
278
279	if c ~= nil then
280		return c:match("^zfs:") ~= nil
281	end
282	return false
283end
284
285function core.isSerialBoot()
286	local c = loader.getenv("console")
287
288	if c ~= nil then
289		if c:find("comconsole") ~= nil then
290			return true
291		end
292	end
293
294	local s = loader.getenv("boot_serial")
295	if s ~= nil then
296		return true
297	end
298
299	local m = loader.getenv("boot_multicons")
300	if m ~= nil then
301		return true
302	end
303	return false
304end
305
306function core.isSystem386()
307	return loader.machine_arch == "i386"
308end
309
310-- Is the menu skipped in the environment in which we've booted?
311function core.isMenuSkipped()
312	if core.isSerialBoot() then
313		return true
314	end
315	local c = string.lower(loader.getenv("console") or "")
316	if c:match("^efi[ ;]") ~= nil or c:match("[ ;]efi[ ;]") ~= nil then
317		return true
318	end
319
320	c = string.lower(loader.getenv("beastie_disable") or "")
321	return c == "yes"
322end
323
324-- This may be a better candidate for a 'utility' module.
325function core.deepCopyTable(tbl)
326	local new_tbl = {}
327	for k, v in pairs(tbl) do
328		if type(v) == "table" then
329			new_tbl[k] = core.deepCopyTable(v)
330		else
331			new_tbl[k] = v
332		end
333	end
334	return new_tbl
335end
336
337-- XXX This should go away if we get the table lib into shape for importing.
338-- As of now, it requires some 'os' functions, so we'll implement this in lua
339-- for our uses
340function core.popFrontTable(tbl)
341	-- Shouldn't reasonably happen
342	if #tbl == 0 then
343		return nil, nil
344	elseif #tbl == 1 then
345		return tbl[1], {}
346	end
347
348	local first_value = tbl[1]
349	local new_tbl = {}
350	-- This is not a cheap operation
351	for k, v in ipairs(tbl) do
352		if k > 1 then
353			new_tbl[k - 1] = v
354		end
355	end
356
357	return first_value, new_tbl
358end
359
360-- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
361-- generally be set upon execution of the kernel. Because of this, we can't (or
362-- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
363-- enabled if we detect it and leave well enough alone if we don't.
364if core.isSystem386() and core.getACPIPresent(false) then
365	core.setACPI(true)
366end
367return core
368