xref: /freebsd-14.2/stand/lua/core.lua (revision ab4a4d40)
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 core = {};
30
31-- Commonly appearing constants
32core.KEY_BACKSPACE	= 8;
33core.KEY_ENTER		= 13;
34core.KEY_DELETE		= 127;
35
36core.KEYSTR_ESCAPE	= "\027";
37
38core.MENU_RETURN	= "return";
39core.MENU_ENTRY		= "entry";
40core.MENU_SEPARATOR	= "separator";
41core.MENU_SUBMENU	= "submenu";
42core.MENU_CAROUSEL_ENTRY	= "carousel_entry";
43
44function core.setVerbose(b)
45	if (b == nil) then
46		b = not core.verbose;
47	end
48
49	if (b == true) then
50		loader.setenv("boot_verbose", "YES");
51	else
52		loader.unsetenv("boot_verbose");
53	end
54	core.verbose = b;
55end
56
57function core.setSingleUser(b)
58	if (b == nil) then
59		b = not core.su;
60	end
61
62	if (b == true) then
63		loader.setenv("boot_single", "YES");
64	else
65		loader.unsetenv("boot_single");
66	end
67	core.su = b;
68end
69
70function core.getACPIPresent(checkingSystemDefaults)
71	local c = loader.getenv("hint.acpi.0.rsdp");
72
73	if (c ~= nil) then
74		if (checkingSystemDefaults == true) then
75			return true;
76		end
77		-- Otherwise, respect disabled if it's set
78		c = loader.getenv("hint.acpi.0.disabled");
79		return (c == nil) or (tonumber(c) ~= 1);
80	end
81	return false;
82end
83
84function core.setACPI(b)
85	if (b == nil) then
86		b = not core.acpi;
87	end
88
89	if (b == true) then
90		loader.setenv("acpi_load", "YES");
91		loader.setenv("hint.acpi.0.disabled", "0");
92		loader.unsetenv("loader.acpi_disabled_by_user");
93	else
94		loader.unsetenv("acpi_load");
95		loader.setenv("hint.acpi.0.disabled", "1");
96		loader.setenv("loader.acpi_disabled_by_user", "1");
97	end
98	core.acpi = b;
99end
100
101function core.setSafeMode(b)
102	if (b == nil) then
103		b = not core.sm;
104	end
105	if (b == true) then
106		loader.setenv("kern.smp.disabled", "1");
107		loader.setenv("hw.ata.ata_dma", "0");
108		loader.setenv("hw.ata.atapi_dma", "0");
109		loader.setenv("hw.ata.wc", "0");
110		loader.setenv("hw.eisa_slots", "0");
111		loader.setenv("kern.eventtimer.periodic", "1");
112		loader.setenv("kern.geom.part.check_integrity", "0");
113	else
114		loader.unsetenv("kern.smp.disabled");
115		loader.unsetenv("hw.ata.ata_dma");
116		loader.unsetenv("hw.ata.atapi_dma");
117		loader.unsetenv("hw.ata.wc");
118		loader.unsetenv("hw.eisa_slots");
119		loader.unsetenv("kern.eventtimer.periodic");
120		loader.unsetenv("kern.geom.part.check_integrity");
121	end
122	core.sm = b;
123end
124
125function core.kernelList()
126	local k = loader.getenv("kernel");
127	local v = loader.getenv("kernels") or "";
128
129	local kernels = {};
130	local i = 0;
131	if k ~= nil then
132		i = i + 1;
133		kernels[i] = k;
134	end
135
136	for n in v:gmatch("([^; ]+)[; ]?") do
137		if n ~= k then
138			i = i + 1;
139			kernels[i] = n;
140		end
141	end
142	return kernels;
143end
144
145function core.setDefaults()
146	core.setACPI(core.getACPIPresent(true));
147	core.setSafeMode(false);
148	core.setSingleUser(false);
149	core.setVerbose(false);
150end
151
152function core.autoboot()
153	loader.perform("autoboot");
154end
155
156function core.boot()
157	loader.perform("boot");
158end
159
160function core.bootserial()
161	local c = loader.getenv("console");
162
163	if c ~= nil then
164		if c:find("comconsole") ~= nil then
165			return true;
166		end
167	end
168
169	local s = loader.getenv("boot_serial");
170	if s ~= nil then
171		return true;
172	end
173
174	local m = loader.getenv("boot_multicons");
175	if m ~= nil then
176		return true;
177	end
178	return false;
179end
180
181core.setACPI(core.getACPIPresent(false))
182return core
183