xref: /freebsd/stand/lua/core.lua (revision b140d14b02f2ee483806d0eeaaa2b29c6bb5463e)
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
29fa4a2394SKyle Evanslocal config = require('config');
30fa4a2394SKyle Evans
31088b4f5fSWarner Loshlocal core = {};
32088b4f5fSWarner Losh
33fe672a15SKyle Evans-- Commonly appearing constants
341504bce3SKyle Evanscore.KEY_BACKSPACE	= 8;
3527fac8ffSKyle Evanscore.KEY_ENTER		= 13;
361504bce3SKyle Evanscore.KEY_DELETE		= 127;
37fe672a15SKyle Evans
3839006570SKyle Evanscore.KEYSTR_ESCAPE	= "\027";
3939006570SKyle Evans
40a7cf0562SKyle Evanscore.MENU_RETURN	= "return";
41a7cf0562SKyle Evanscore.MENU_ENTRY		= "entry";
42a7cf0562SKyle Evanscore.MENU_SEPARATOR	= "separator";
43a7cf0562SKyle Evanscore.MENU_SUBMENU	= "submenu";
44a7cf0562SKyle Evanscore.MENU_CAROUSEL_ENTRY	= "carousel_entry";
45a7cf0562SKyle Evans
46088b4f5fSWarner Loshfunction core.setVerbose(b)
47088b4f5fSWarner Losh	if (b == nil) then
48088b4f5fSWarner Losh		b = not core.verbose;
49088b4f5fSWarner Losh	end
50088b4f5fSWarner Losh
51088b4f5fSWarner Losh	if (b == true) then
52088b4f5fSWarner Losh		loader.setenv("boot_verbose", "YES");
53088b4f5fSWarner Losh	else
54088b4f5fSWarner Losh		loader.unsetenv("boot_verbose");
55088b4f5fSWarner Losh	end
56088b4f5fSWarner Losh	core.verbose = b;
57088b4f5fSWarner Loshend
58088b4f5fSWarner Losh
59088b4f5fSWarner Loshfunction core.setSingleUser(b)
60088b4f5fSWarner Losh	if (b == nil) then
61088b4f5fSWarner Losh		b = not core.su;
62088b4f5fSWarner Losh	end
63088b4f5fSWarner Losh
64088b4f5fSWarner Losh	if (b == true) then
65088b4f5fSWarner Losh		loader.setenv("boot_single", "YES");
66088b4f5fSWarner Losh	else
67088b4f5fSWarner Losh		loader.unsetenv("boot_single");
68088b4f5fSWarner Losh	end
69088b4f5fSWarner Losh	core.su = b;
70088b4f5fSWarner Loshend
71088b4f5fSWarner Losh
726401094fSKyle Evansfunction core.getACPIPresent(checkingSystemDefaults)
736401094fSKyle Evans	local c = loader.getenv("hint.acpi.0.rsdp");
746401094fSKyle Evans
756401094fSKyle Evans	if (c ~= nil) then
766401094fSKyle Evans		if (checkingSystemDefaults == true) then
776401094fSKyle Evans			return true;
786401094fSKyle Evans		end
796401094fSKyle Evans		-- Otherwise, respect disabled if it's set
806401094fSKyle Evans		c = loader.getenv("hint.acpi.0.disabled");
816401094fSKyle Evans		return (c == nil) or (tonumber(c) ~= 1);
826401094fSKyle Evans	end
836401094fSKyle Evans	return false;
846401094fSKyle Evansend
856401094fSKyle Evans
86088b4f5fSWarner Loshfunction core.setACPI(b)
87088b4f5fSWarner Losh	if (b == nil) then
88088b4f5fSWarner Losh		b = not core.acpi;
89088b4f5fSWarner Losh	end
90088b4f5fSWarner Losh
91088b4f5fSWarner Losh	if (b == true) then
92088b4f5fSWarner Losh		loader.setenv("acpi_load", "YES");
93088b4f5fSWarner Losh		loader.setenv("hint.acpi.0.disabled", "0");
94088b4f5fSWarner Losh		loader.unsetenv("loader.acpi_disabled_by_user");
95088b4f5fSWarner Losh	else
96088b4f5fSWarner Losh		loader.unsetenv("acpi_load");
97088b4f5fSWarner Losh		loader.setenv("hint.acpi.0.disabled", "1");
98088b4f5fSWarner Losh		loader.setenv("loader.acpi_disabled_by_user", "1");
99088b4f5fSWarner Losh	end
100088b4f5fSWarner Losh	core.acpi = b;
101088b4f5fSWarner Loshend
102088b4f5fSWarner Losh
103088b4f5fSWarner Loshfunction core.setSafeMode(b)
104088b4f5fSWarner Losh	if (b == nil) then
105088b4f5fSWarner Losh		b = not core.sm;
106088b4f5fSWarner Losh	end
107088b4f5fSWarner Losh	if (b == true) then
108088b4f5fSWarner Losh		loader.setenv("kern.smp.disabled", "1");
109088b4f5fSWarner Losh		loader.setenv("hw.ata.ata_dma", "0");
110088b4f5fSWarner Losh		loader.setenv("hw.ata.atapi_dma", "0");
111088b4f5fSWarner Losh		loader.setenv("hw.ata.wc", "0");
112088b4f5fSWarner Losh		loader.setenv("hw.eisa_slots", "0");
113088b4f5fSWarner Losh		loader.setenv("kern.eventtimer.periodic", "1");
114088b4f5fSWarner Losh		loader.setenv("kern.geom.part.check_integrity", "0");
115088b4f5fSWarner Losh	else
116088b4f5fSWarner Losh		loader.unsetenv("kern.smp.disabled");
117088b4f5fSWarner Losh		loader.unsetenv("hw.ata.ata_dma");
118088b4f5fSWarner Losh		loader.unsetenv("hw.ata.atapi_dma");
119088b4f5fSWarner Losh		loader.unsetenv("hw.ata.wc");
120088b4f5fSWarner Losh		loader.unsetenv("hw.eisa_slots");
121088b4f5fSWarner Losh		loader.unsetenv("kern.eventtimer.periodic");
122088b4f5fSWarner Losh		loader.unsetenv("kern.geom.part.check_integrity");
123088b4f5fSWarner Losh	end
124088b4f5fSWarner Losh	core.sm = b;
125088b4f5fSWarner Loshend
126088b4f5fSWarner Losh
127088b4f5fSWarner Loshfunction core.kernelList()
128088b4f5fSWarner Losh	local k = loader.getenv("kernel");
129088b4f5fSWarner Losh	local v = loader.getenv("kernels") or "";
130088b4f5fSWarner Losh
131088b4f5fSWarner Losh	local kernels = {};
132a108046fSConrad Meyer	local unique = {};
133088b4f5fSWarner Losh	local i = 0;
13424a1bd54SKyle Evans	if (k ~= nil) then
135088b4f5fSWarner Losh		i = i + 1;
136088b4f5fSWarner Losh		kernels[i] = k;
137a108046fSConrad Meyer		unique[k] = true;
138088b4f5fSWarner Losh	end
139088b4f5fSWarner Losh
140088b4f5fSWarner Losh	for n in v:gmatch("([^; ]+)[; ]?") do
141a108046fSConrad Meyer		if (unique[n] == nil) then
142088b4f5fSWarner Losh			i = i + 1;
143088b4f5fSWarner Losh			kernels[i] = n;
144a108046fSConrad Meyer			unique[n] = true;
145088b4f5fSWarner Losh		end
146088b4f5fSWarner Losh	end
147a108046fSConrad Meyer
148a108046fSConrad Meyer	-- Automatically detect other bootable kernel directories using a
149a108046fSConrad Meyer	-- heuristic.  Any directory in /boot that contains an ordinary file
150a108046fSConrad Meyer	-- named "kernel" is considered eligible.
151a108046fSConrad Meyer	for file in lfs.dir("/boot") do
152a108046fSConrad Meyer		local fname = "/boot/" .. file;
153a108046fSConrad Meyer
154a108046fSConrad Meyer		if (file == "." or file == "..") then
155a108046fSConrad Meyer			goto continue;
156a108046fSConrad Meyer		end
157a108046fSConrad Meyer
158a108046fSConrad Meyer		if (lfs.attributes(fname, "mode") ~= "directory") then
159a108046fSConrad Meyer			goto continue;
160a108046fSConrad Meyer		end
161a108046fSConrad Meyer
162a108046fSConrad Meyer		if (lfs.attributes(fname .. "/kernel", "mode") ~= "file") then
163a108046fSConrad Meyer			goto continue;
164a108046fSConrad Meyer		end
165a108046fSConrad Meyer
166a108046fSConrad Meyer		if (unique[file] == nil) then
167a108046fSConrad Meyer			i = i + 1;
168a108046fSConrad Meyer			kernels[i] = file;
169a108046fSConrad Meyer			unique[file] = true;
170a108046fSConrad Meyer		end
171a108046fSConrad Meyer
172a108046fSConrad Meyer		::continue::
173a108046fSConrad Meyer	end
174088b4f5fSWarner Losh	return kernels;
175088b4f5fSWarner Loshend
176088b4f5fSWarner Losh
177088b4f5fSWarner Loshfunction core.setDefaults()
1786401094fSKyle Evans	core.setACPI(core.getACPIPresent(true));
179088b4f5fSWarner Losh	core.setSafeMode(false);
180088b4f5fSWarner Losh	core.setSingleUser(false);
181088b4f5fSWarner Losh	core.setVerbose(false);
182088b4f5fSWarner Loshend
183088b4f5fSWarner Losh
184088b4f5fSWarner Loshfunction core.autoboot()
185fa4a2394SKyle Evans	config.loadelf();
186088b4f5fSWarner Losh	loader.perform("autoboot");
187088b4f5fSWarner Loshend
188088b4f5fSWarner Losh
189088b4f5fSWarner Loshfunction core.boot()
190fa4a2394SKyle Evans	config.loadelf();
191088b4f5fSWarner Losh	loader.perform("boot");
192088b4f5fSWarner Loshend
193088b4f5fSWarner Losh
194*b140d14bSKyle Evansfunction core.isSerialBoot()
195088b4f5fSWarner Losh	local c = loader.getenv("console");
196088b4f5fSWarner Losh
19724a1bd54SKyle Evans	if (c ~= nil) then
19824a1bd54SKyle Evans		if (c:find("comconsole") ~= nil) then
199088b4f5fSWarner Losh			return true;
200088b4f5fSWarner Losh		end
201088b4f5fSWarner Losh	end
202088b4f5fSWarner Losh
203088b4f5fSWarner Losh	local s = loader.getenv("boot_serial");
20424a1bd54SKyle Evans	if (s ~= nil) then
205088b4f5fSWarner Losh		return true;
206088b4f5fSWarner Losh	end
207088b4f5fSWarner Losh
208088b4f5fSWarner Losh	local m = loader.getenv("boot_multicons");
20924a1bd54SKyle Evans	if (m ~= nil) then
210088b4f5fSWarner Losh		return true;
211088b4f5fSWarner Losh	end
212088b4f5fSWarner Losh	return false;
213088b4f5fSWarner Loshend
214088b4f5fSWarner Losh
21524a1bd54SKyle Evanscore.setACPI(core.getACPIPresent(false));
21624a1bd54SKyle Evansreturn core;
217