xref: /freebsd/stand/lua/core.lua (revision fe672a15db5b32f0cb57ed64b847b7586ca1c8c2)
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
31*fe672a15SKyle Evans-- Commonly appearing constants
32*fe672a15SKyle Evanscore.KEY_ENTER = 13
33*fe672a15SKyle Evanscore.KEY_BACKSPACE = 127
34*fe672a15SKyle Evans
35088b4f5fSWarner Loshfunction core.setVerbose(b)
36088b4f5fSWarner Losh	if (b == nil) then
37088b4f5fSWarner Losh		b = not core.verbose;
38088b4f5fSWarner Losh	end
39088b4f5fSWarner Losh
40088b4f5fSWarner Losh	if (b == true) then
41088b4f5fSWarner Losh		loader.setenv("boot_verbose", "YES");
42088b4f5fSWarner Losh	else
43088b4f5fSWarner Losh		loader.unsetenv("boot_verbose");
44088b4f5fSWarner Losh	end
45088b4f5fSWarner Losh	core.verbose = b;
46088b4f5fSWarner Loshend
47088b4f5fSWarner Losh
48088b4f5fSWarner Loshfunction core.setSingleUser(b)
49088b4f5fSWarner Losh	if (b == nil) then
50088b4f5fSWarner Losh		b = not core.su;
51088b4f5fSWarner Losh	end
52088b4f5fSWarner Losh
53088b4f5fSWarner Losh	if (b == true) then
54088b4f5fSWarner Losh		loader.setenv("boot_single", "YES");
55088b4f5fSWarner Losh	else
56088b4f5fSWarner Losh		loader.unsetenv("boot_single");
57088b4f5fSWarner Losh	end
58088b4f5fSWarner Losh	core.su = b;
59088b4f5fSWarner Loshend
60088b4f5fSWarner Losh
61088b4f5fSWarner Loshfunction core.setACPI(b)
62088b4f5fSWarner Losh	if (b == nil) then
63088b4f5fSWarner Losh		b = not core.acpi;
64088b4f5fSWarner Losh	end
65088b4f5fSWarner Losh
66088b4f5fSWarner Losh	if (b == true) then
67088b4f5fSWarner Losh		loader.setenv("acpi_load", "YES");
68088b4f5fSWarner Losh		loader.setenv("hint.acpi.0.disabled", "0");
69088b4f5fSWarner Losh		loader.unsetenv("loader.acpi_disabled_by_user");
70088b4f5fSWarner Losh	else
71088b4f5fSWarner Losh		loader.unsetenv("acpi_load");
72088b4f5fSWarner Losh		loader.setenv("hint.acpi.0.disabled", "1");
73088b4f5fSWarner Losh		loader.setenv("loader.acpi_disabled_by_user", "1");
74088b4f5fSWarner Losh	end
75088b4f5fSWarner Losh	core.acpi = b;
76088b4f5fSWarner Loshend
77088b4f5fSWarner Losh
78088b4f5fSWarner Loshfunction core.setSafeMode(b)
79088b4f5fSWarner Losh	if (b == nil) then
80088b4f5fSWarner Losh		b = not core.sm;
81088b4f5fSWarner Losh	end
82088b4f5fSWarner Losh	if (b == true) then
83088b4f5fSWarner Losh		loader.setenv("kern.smp.disabled", "1");
84088b4f5fSWarner Losh		loader.setenv("hw.ata.ata_dma", "0");
85088b4f5fSWarner Losh		loader.setenv("hw.ata.atapi_dma", "0");
86088b4f5fSWarner Losh		loader.setenv("hw.ata.wc", "0");
87088b4f5fSWarner Losh		loader.setenv("hw.eisa_slots", "0");
88088b4f5fSWarner Losh		loader.setenv("kern.eventtimer.periodic", "1");
89088b4f5fSWarner Losh		loader.setenv("kern.geom.part.check_integrity", "0");
90088b4f5fSWarner Losh	else
91088b4f5fSWarner Losh		loader.unsetenv("kern.smp.disabled");
92088b4f5fSWarner Losh		loader.unsetenv("hw.ata.ata_dma");
93088b4f5fSWarner Losh		loader.unsetenv("hw.ata.atapi_dma");
94088b4f5fSWarner Losh		loader.unsetenv("hw.ata.wc");
95088b4f5fSWarner Losh		loader.unsetenv("hw.eisa_slots");
96088b4f5fSWarner Losh		loader.unsetenv("kern.eventtimer.periodic");
97088b4f5fSWarner Losh		loader.unsetenv("kern.geom.part.check_integrity");
98088b4f5fSWarner Losh	end
99088b4f5fSWarner Losh	core.sm = b;
100088b4f5fSWarner Loshend
101088b4f5fSWarner Losh
102088b4f5fSWarner Loshfunction core.kernelList()
103088b4f5fSWarner Losh	local k = loader.getenv("kernel");
104088b4f5fSWarner Losh	local v = loader.getenv("kernels") or "";
105088b4f5fSWarner Losh
106088b4f5fSWarner Losh	local kernels = {};
107088b4f5fSWarner Losh	local i = 0;
108088b4f5fSWarner Losh	if k ~= nil then
109088b4f5fSWarner Losh		i = i + 1;
110088b4f5fSWarner Losh		kernels[i] = k;
111088b4f5fSWarner Losh	end
112088b4f5fSWarner Losh
113088b4f5fSWarner Losh	for n in v:gmatch("([^; ]+)[; ]?") do
114088b4f5fSWarner Losh		if n ~= k then
115088b4f5fSWarner Losh			i = i + 1;
116088b4f5fSWarner Losh			kernels[i] = n;
117088b4f5fSWarner Losh		end
118088b4f5fSWarner Losh	end
119088b4f5fSWarner Losh	return kernels;
120088b4f5fSWarner Loshend
121088b4f5fSWarner Losh
122088b4f5fSWarner Loshfunction core.setDefaults()
123088b4f5fSWarner Losh	core.setACPI(true);
124088b4f5fSWarner Losh	core.setSafeMode(false);
125088b4f5fSWarner Losh	core.setSingleUser(false);
126088b4f5fSWarner Losh	core.setVerbose(false);
127088b4f5fSWarner Loshend
128088b4f5fSWarner Losh
129088b4f5fSWarner Loshfunction core.autoboot()
130088b4f5fSWarner Losh	loader.perform("autoboot");
131088b4f5fSWarner Loshend
132088b4f5fSWarner Losh
133088b4f5fSWarner Loshfunction core.boot()
134088b4f5fSWarner Losh	loader.perform("boot");
135088b4f5fSWarner Loshend
136088b4f5fSWarner Losh
137088b4f5fSWarner Loshfunction core.bootserial()
138088b4f5fSWarner Losh	local c = loader.getenv("console");
139088b4f5fSWarner Losh
140088b4f5fSWarner Losh	if c ~= nil then
141088b4f5fSWarner Losh		if c:find("comconsole") ~= nil then
142088b4f5fSWarner Losh			return true;
143088b4f5fSWarner Losh		end
144088b4f5fSWarner Losh	end
145088b4f5fSWarner Losh
146088b4f5fSWarner Losh	local s = loader.getenv("boot_serial");
147088b4f5fSWarner Losh	if s ~= nil then
148088b4f5fSWarner Losh		return true;
149088b4f5fSWarner Losh	end
150088b4f5fSWarner Losh
151088b4f5fSWarner Losh	local m = loader.getenv("boot_multicons");
152088b4f5fSWarner Losh	if m ~= nil then
153088b4f5fSWarner Losh		return true;
154088b4f5fSWarner Losh	end
155088b4f5fSWarner Losh	return false;
156088b4f5fSWarner Loshend
157088b4f5fSWarner Losh
158088b4f5fSWarner Loshreturn core
159