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