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