xref: /freebsd/stand/lua/core.lua (revision 39006570a401ef5fb6d7d8029089fb11280bfd24)
1088b4f5fSWarner Losh--
2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3088b4f5fSWarner Losh-- All rights reserved.
4088b4f5fSWarner Losh--
5088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
6088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
7088b4f5fSWarner Losh-- are met:
8088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
9088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
10088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
11088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
12088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
13088b4f5fSWarner Losh--
14088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24088b4f5fSWarner Losh-- SUCH DAMAGE.
25088b4f5fSWarner Losh--
26088b4f5fSWarner Losh-- $FreeBSD$
27088b4f5fSWarner Losh--
28088b4f5fSWarner Losh
29088b4f5fSWarner Loshlocal core = {};
30088b4f5fSWarner Losh
31fe672a15SKyle Evans-- Commonly appearing constants
3227fac8ffSKyle Evanscore.KEY_ENTER = 13;
3327fac8ffSKyle Evanscore.KEY_BACKSPACE = 127;
34fe672a15SKyle Evans
35*39006570SKyle Evanscore.KEYSTR_ESCAPE = "\027";
36*39006570SKyle Evans
37088b4f5fSWarner Loshfunction core.setVerbose(b)
38088b4f5fSWarner Losh	if (b == nil) then
39088b4f5fSWarner Losh		b = not core.verbose;
40088b4f5fSWarner Losh	end
41088b4f5fSWarner Losh
42088b4f5fSWarner Losh	if (b == true) then
43088b4f5fSWarner Losh		loader.setenv("boot_verbose", "YES");
44088b4f5fSWarner Losh	else
45088b4f5fSWarner Losh		loader.unsetenv("boot_verbose");
46088b4f5fSWarner Losh	end
47088b4f5fSWarner Losh	core.verbose = b;
48088b4f5fSWarner Loshend
49088b4f5fSWarner Losh
50088b4f5fSWarner Loshfunction core.setSingleUser(b)
51088b4f5fSWarner Losh	if (b == nil) then
52088b4f5fSWarner Losh		b = not core.su;
53088b4f5fSWarner Losh	end
54088b4f5fSWarner Losh
55088b4f5fSWarner Losh	if (b == true) then
56088b4f5fSWarner Losh		loader.setenv("boot_single", "YES");
57088b4f5fSWarner Losh	else
58088b4f5fSWarner Losh		loader.unsetenv("boot_single");
59088b4f5fSWarner Losh	end
60088b4f5fSWarner Losh	core.su = b;
61088b4f5fSWarner Loshend
62088b4f5fSWarner Losh
636401094fSKyle Evansfunction core.getACPIPresent(checkingSystemDefaults)
646401094fSKyle Evans	local c = loader.getenv("hint.acpi.0.rsdp");
656401094fSKyle Evans
666401094fSKyle Evans	if (c ~= nil) then
676401094fSKyle Evans		if (checkingSystemDefaults == true) then
686401094fSKyle Evans			return true;
696401094fSKyle Evans		end
706401094fSKyle Evans		-- Otherwise, respect disabled if it's set
716401094fSKyle Evans		c = loader.getenv("hint.acpi.0.disabled");
726401094fSKyle Evans		return (c == nil) or (tonumber(c) ~= 1);
736401094fSKyle Evans	end
746401094fSKyle Evans	return false;
756401094fSKyle Evansend
766401094fSKyle Evans
77088b4f5fSWarner Loshfunction core.setACPI(b)
78088b4f5fSWarner Losh	if (b == nil) then
79088b4f5fSWarner Losh		b = not core.acpi;
80088b4f5fSWarner Losh	end
81088b4f5fSWarner Losh
82088b4f5fSWarner Losh	if (b == true) then
83088b4f5fSWarner Losh		loader.setenv("acpi_load", "YES");
84088b4f5fSWarner Losh		loader.setenv("hint.acpi.0.disabled", "0");
85088b4f5fSWarner Losh		loader.unsetenv("loader.acpi_disabled_by_user");
86088b4f5fSWarner Losh	else
87088b4f5fSWarner Losh		loader.unsetenv("acpi_load");
88088b4f5fSWarner Losh		loader.setenv("hint.acpi.0.disabled", "1");
89088b4f5fSWarner Losh		loader.setenv("loader.acpi_disabled_by_user", "1");
90088b4f5fSWarner Losh	end
91088b4f5fSWarner Losh	core.acpi = b;
92088b4f5fSWarner Loshend
93088b4f5fSWarner Losh
94088b4f5fSWarner Loshfunction core.setSafeMode(b)
95088b4f5fSWarner Losh	if (b == nil) then
96088b4f5fSWarner Losh		b = not core.sm;
97088b4f5fSWarner Losh	end
98088b4f5fSWarner Losh	if (b == true) then
99088b4f5fSWarner Losh		loader.setenv("kern.smp.disabled", "1");
100088b4f5fSWarner Losh		loader.setenv("hw.ata.ata_dma", "0");
101088b4f5fSWarner Losh		loader.setenv("hw.ata.atapi_dma", "0");
102088b4f5fSWarner Losh		loader.setenv("hw.ata.wc", "0");
103088b4f5fSWarner Losh		loader.setenv("hw.eisa_slots", "0");
104088b4f5fSWarner Losh		loader.setenv("kern.eventtimer.periodic", "1");
105088b4f5fSWarner Losh		loader.setenv("kern.geom.part.check_integrity", "0");
106088b4f5fSWarner Losh	else
107088b4f5fSWarner Losh		loader.unsetenv("kern.smp.disabled");
108088b4f5fSWarner Losh		loader.unsetenv("hw.ata.ata_dma");
109088b4f5fSWarner Losh		loader.unsetenv("hw.ata.atapi_dma");
110088b4f5fSWarner Losh		loader.unsetenv("hw.ata.wc");
111088b4f5fSWarner Losh		loader.unsetenv("hw.eisa_slots");
112088b4f5fSWarner Losh		loader.unsetenv("kern.eventtimer.periodic");
113088b4f5fSWarner Losh		loader.unsetenv("kern.geom.part.check_integrity");
114088b4f5fSWarner Losh	end
115088b4f5fSWarner Losh	core.sm = b;
116088b4f5fSWarner Loshend
117088b4f5fSWarner Losh
118088b4f5fSWarner Loshfunction core.kernelList()
119088b4f5fSWarner Losh	local k = loader.getenv("kernel");
120088b4f5fSWarner Losh	local v = loader.getenv("kernels") or "";
121088b4f5fSWarner Losh
122088b4f5fSWarner Losh	local kernels = {};
123088b4f5fSWarner Losh	local i = 0;
124088b4f5fSWarner Losh	if k ~= nil then
125088b4f5fSWarner Losh		i = i + 1;
126088b4f5fSWarner Losh		kernels[i] = k;
127088b4f5fSWarner Losh	end
128088b4f5fSWarner Losh
129088b4f5fSWarner Losh	for n in v:gmatch("([^; ]+)[; ]?") do
130088b4f5fSWarner Losh		if n ~= k then
131088b4f5fSWarner Losh			i = i + 1;
132088b4f5fSWarner Losh			kernels[i] = n;
133088b4f5fSWarner Losh		end
134088b4f5fSWarner Losh	end
135088b4f5fSWarner Losh	return kernels;
136088b4f5fSWarner Loshend
137088b4f5fSWarner Losh
138088b4f5fSWarner Loshfunction core.setDefaults()
1396401094fSKyle Evans	core.setACPI(core.getACPIPresent(true));
140088b4f5fSWarner Losh	core.setSafeMode(false);
141088b4f5fSWarner Losh	core.setSingleUser(false);
142088b4f5fSWarner Losh	core.setVerbose(false);
143088b4f5fSWarner Loshend
144088b4f5fSWarner Losh
145088b4f5fSWarner Loshfunction core.autoboot()
146088b4f5fSWarner Losh	loader.perform("autoboot");
147088b4f5fSWarner Loshend
148088b4f5fSWarner Losh
149088b4f5fSWarner Loshfunction core.boot()
150088b4f5fSWarner Losh	loader.perform("boot");
151088b4f5fSWarner Loshend
152088b4f5fSWarner Losh
153088b4f5fSWarner Loshfunction core.bootserial()
154088b4f5fSWarner Losh	local c = loader.getenv("console");
155088b4f5fSWarner Losh
156088b4f5fSWarner Losh	if c ~= nil then
157088b4f5fSWarner Losh		if c:find("comconsole") ~= nil then
158088b4f5fSWarner Losh			return true;
159088b4f5fSWarner Losh		end
160088b4f5fSWarner Losh	end
161088b4f5fSWarner Losh
162088b4f5fSWarner Losh	local s = loader.getenv("boot_serial");
163088b4f5fSWarner Losh	if s ~= nil then
164088b4f5fSWarner Losh		return true;
165088b4f5fSWarner Losh	end
166088b4f5fSWarner Losh
167088b4f5fSWarner Losh	local m = loader.getenv("boot_multicons");
168088b4f5fSWarner Losh	if m ~= nil then
169088b4f5fSWarner Losh		return true;
170088b4f5fSWarner Losh	end
171088b4f5fSWarner Losh	return false;
172088b4f5fSWarner Loshend
173088b4f5fSWarner Losh
17427fac8ffSKyle Evanscore.acpi = core.getACPIPresent(false);
175088b4f5fSWarner Loshreturn core
176