xref: /freebsd-13.1/stand/lua/core.lua (revision 4f866698)
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-- Internal function
41-- Parses arguments to boot and returns two values: kernel_name, argstr
42-- Defaults to nil and "" respectively.
43-- This will also parse arguments to autoboot, but the with_kernel argument
44-- will need to be explicitly overwritten to false
45local parse_boot_args = function(argv, with_kernel)
46	if with_kernel == nil then
47		with_kernel = true
48	end
49	if #argv == 0 then
50		if with_kernel then
51			return nil, ""
52		else
53			return ""
54		end
55	end
56	local kernel_name
57	local argstr = ""
58
59	for k, v in ipairs(argv) do
60		if with_kernel and v:sub(1,1) ~= "-" then
61			kernel_name = v
62		else
63			argstr = argstr .. " " .. v
64		end
65	end
66	if with_kernel then
67		return kernel_name, argstr
68	else
69		return argstr
70	end
71end
72
73-- Globals
74function boot(...)
75	local argv = {...}
76	local cmd_name = ""
77	cmd_name, argv = core.popFrontTable(argv)
78	local kernel, argstr = parse_boot_args(argv)
79	if kernel ~= nil then
80		loader.perform("unload")
81		config.selectkernel(kernel)
82	end
83	core.boot(argstr)
84end
85
86function autoboot(...)
87	local argv = {...}
88	local cmd_name = ""
89	cmd_name, argv = core.popFrontTable(argv)
90	local argstr = parse_boot_args(argv, false)
91	core.autoboot(argstr)
92end
93
94-- Module exports
95-- Commonly appearing constants
96core.KEY_BACKSPACE	= 8
97core.KEY_ENTER		= 13
98core.KEY_DELETE		= 127
99
100core.KEYSTR_ESCAPE	= "\027"
101
102core.MENU_RETURN	= "return"
103core.MENU_ENTRY		= "entry"
104core.MENU_SEPARATOR	= "separator"
105core.MENU_SUBMENU	= "submenu"
106core.MENU_CAROUSEL_ENTRY	= "carousel_entry"
107
108function core.setVerbose(b)
109	if b == nil then
110		b = not core.verbose
111	end
112
113	if b then
114		loader.setenv("boot_verbose", "YES")
115	else
116		loader.unsetenv("boot_verbose")
117	end
118	core.verbose = b
119end
120
121function core.setSingleUser(b)
122	if b == nil then
123		b = not core.su
124	end
125
126	if b then
127		loader.setenv("boot_single", "YES")
128	else
129		loader.unsetenv("boot_single")
130	end
131	core.su = b
132end
133
134function core.getACPIPresent(checkingSystemDefaults)
135	local c = loader.getenv("hint.acpi.0.rsdp")
136
137	if c ~= nil then
138		if checkingSystemDefaults then
139			return true
140		end
141		-- Otherwise, respect disabled if it's set
142		c = loader.getenv("hint.acpi.0.disabled")
143		return c == nil or tonumber(c) ~= 1
144	end
145	return false
146end
147
148function core.setACPI(b)
149	if b == nil then
150		b = not core.acpi
151	end
152
153	if b then
154		loader.setenv("acpi_load", "YES")
155		loader.setenv("hint.acpi.0.disabled", "0")
156		loader.unsetenv("loader.acpi_disabled_by_user")
157	else
158		loader.unsetenv("acpi_load")
159		loader.setenv("hint.acpi.0.disabled", "1")
160		loader.setenv("loader.acpi_disabled_by_user", "1")
161	end
162	core.acpi = b
163end
164
165function core.setSafeMode(b)
166	if b == nil then
167		b = not core.sm
168	end
169	if b then
170		loader.setenv("kern.smp.disabled", "1")
171		loader.setenv("hw.ata.ata_dma", "0")
172		loader.setenv("hw.ata.atapi_dma", "0")
173		loader.setenv("hw.ata.wc", "0")
174		loader.setenv("hw.eisa_slots", "0")
175		loader.setenv("kern.eventtimer.periodic", "1")
176		loader.setenv("kern.geom.part.check_integrity", "0")
177	else
178		loader.unsetenv("kern.smp.disabled")
179		loader.unsetenv("hw.ata.ata_dma")
180		loader.unsetenv("hw.ata.atapi_dma")
181		loader.unsetenv("hw.ata.wc")
182		loader.unsetenv("hw.eisa_slots")
183		loader.unsetenv("kern.eventtimer.periodic")
184		loader.unsetenv("kern.geom.part.check_integrity")
185	end
186	core.sm = b
187end
188
189function core.kernelList()
190	local k = loader.getenv("kernel")
191	local v = loader.getenv("kernels")
192	local autodetect = loader.getenv("kernels_autodetect") or ""
193
194	local kernels = {}
195	local unique = {}
196	local i = 0
197	if k ~= nil then
198		i = i + 1
199		kernels[i] = k
200		unique[k] = true
201	end
202
203	if v ~= nil then
204		for n in v:gmatch("([^; ]+)[; ]?") do
205			if unique[n] == nil then
206				i = i + 1
207				kernels[i] = n
208				unique[n] = true
209			end
210		end
211	end
212
213	-- Base whether we autodetect kernels or not on a loader.conf(5)
214	-- setting, kernels_autodetect. If it's set to 'yes', we'll add
215	-- any kernels we detect based on the criteria described.
216	if autodetect:lower() ~= "yes" then
217		return kernels
218	end
219
220	-- Automatically detect other bootable kernel directories using a
221	-- heuristic.  Any directory in /boot that contains an ordinary file
222	-- named "kernel" is considered eligible.
223	for file in lfs.dir("/boot") do
224		local fname = "/boot/" .. file
225
226		if file == "." or file == ".." then
227			goto continue
228		end
229
230		if lfs.attributes(fname, "mode") ~= "directory" then
231			goto continue
232		end
233
234		if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
235			goto continue
236		end
237
238		if unique[file] == nil then
239			i = i + 1
240			kernels[i] = file
241			unique[file] = true
242		end
243
244		::continue::
245	end
246	return kernels
247end
248
249function core.bootenvDefault()
250	return loader.getenv("zfs_be_active")
251end
252
253function core.bootenvList()
254	local bootenv_count = tonumber(loader.getenv("bootenvs_count"))
255	local bootenvs = {}
256	local curenv
257	local curenv_idx = 0
258	local envcount = 0
259	local unique = {}
260
261	if bootenv_count == nil or bootenv_count <= 0 then
262		return bootenvs
263	end
264
265	-- Currently selected bootenv is always first/default
266	curenv = core.bootenvDefault()
267	if curenv ~= nil then
268		envcount = envcount + 1
269		bootenvs[envcount] = curenv
270		unique[curenv] = true
271	end
272
273	for curenv_idx = 0, bootenv_count - 1 do
274		curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]")
275		if curenv ~= nil and unique[curenv] == nil then
276			envcount = envcount + 1
277			bootenvs[envcount] = curenv
278			unique[curenv] = true
279		end
280	end
281	return bootenvs
282end
283
284function core.setDefaults()
285	core.setACPI(core.getACPIPresent(true))
286	core.setSafeMode(false)
287	core.setSingleUser(false)
288	core.setVerbose(false)
289end
290
291function core.autoboot(argstr)
292	config.loadelf()
293	loader.perform(compose_loader_cmd("autoboot", argstr))
294end
295
296function core.boot(argstr)
297	config.loadelf()
298	loader.perform(compose_loader_cmd("boot", argstr))
299end
300
301function core.isSingleUserBoot()
302	local single_user = loader.getenv("boot_single")
303	return single_user ~= nil and single_user:lower() == "yes"
304end
305
306function core.isZFSBoot()
307	local c = loader.getenv("currdev")
308
309	if c ~= nil then
310		return c:match("^zfs:") ~= nil
311	end
312	return false
313end
314
315function core.isSerialBoot()
316	local c = loader.getenv("console")
317
318	if c ~= nil then
319		if c:find("comconsole") ~= nil then
320			return true
321		end
322	end
323
324	local s = loader.getenv("boot_serial")
325	if s ~= nil then
326		return true
327	end
328
329	local m = loader.getenv("boot_multicons")
330	if m ~= nil then
331		return true
332	end
333	return false
334end
335
336function core.isSystem386()
337	return loader.machine_arch == "i386"
338end
339
340-- This may be a better candidate for a 'utility' module.
341function core.shallowCopyTable(tbl)
342	local new_tbl = {}
343	for k, v in pairs(tbl) do
344		if type(v) == "table" then
345			new_tbl[k] = core.shallowCopyTable(v)
346		else
347			new_tbl[k] = v
348		end
349	end
350	return new_tbl
351end
352
353-- XXX This should go away if we get the table lib into shape for importing.
354-- As of now, it requires some 'os' functions, so we'll implement this in lua
355-- for our uses
356function core.popFrontTable(tbl)
357	-- Shouldn't reasonably happen
358	if #tbl == 0 then
359		return nil, nil
360	elseif #tbl == 1 then
361		return tbl[1], {}
362	end
363
364	local first_value = tbl[1]
365	local new_tbl = {}
366	-- This is not a cheap operation
367	for k, v in ipairs(tbl) do
368		if k > 1 then
369			new_tbl[k - 1] = v
370		end
371	end
372
373	return first_value, new_tbl
374end
375
376-- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
377-- generally be set upon execution of the kernel. Because of this, we can't (or
378-- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
379-- enabled if we detect it and leave well enough alone if we don't.
380if core.isSystem386() and core.getACPIPresent(false) then
381	core.setACPI(true)
382end
383return core
384