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