xref: /freebsd/stand/lua/core.lua (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1--
2-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
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 unique = {};
131	local i = 0;
132	if (k ~= nil) then
133		i = i + 1;
134		kernels[i] = k;
135		unique[k] = true;
136	end
137
138	for n in v:gmatch("([^; ]+)[; ]?") do
139		if (unique[n] == nil) then
140			i = i + 1;
141			kernels[i] = n;
142			unique[n] = true;
143		end
144	end
145
146	-- Automatically detect other bootable kernel directories using a
147	-- heuristic.  Any directory in /boot that contains an ordinary file
148	-- named "kernel" is considered eligible.
149	for file in lfs.dir("/boot") do
150		local fname = "/boot/" .. file;
151
152		if (file == "." or file == "..") then
153			goto continue;
154		end
155
156		if (lfs.attributes(fname, "mode") ~= "directory") then
157			goto continue;
158		end
159
160		if (lfs.attributes(fname .. "/kernel", "mode") ~= "file") then
161			goto continue;
162		end
163
164		if (unique[file] == nil) then
165			i = i + 1;
166			kernels[i] = file;
167			unique[file] = true;
168		end
169
170		::continue::
171	end
172	return kernels;
173end
174
175function core.setDefaults()
176	core.setACPI(core.getACPIPresent(true));
177	core.setSafeMode(false);
178	core.setSingleUser(false);
179	core.setVerbose(false);
180end
181
182function core.autoboot()
183	loader.perform("autoboot");
184end
185
186function core.boot()
187	loader.perform("boot");
188end
189
190function core.bootserial()
191	local c = loader.getenv("console");
192
193	if (c ~= nil) then
194		if (c:find("comconsole") ~= nil) then
195			return true;
196		end
197	end
198
199	local s = loader.getenv("boot_serial");
200	if (s ~= nil) then
201		return true;
202	end
203
204	local m = loader.getenv("boot_multicons");
205	if (m ~= nil) then
206		return true;
207	end
208	return false;
209end
210
211core.setACPI(core.getACPIPresent(false));
212return core;
213