2 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3 -- All rights reserved.
5 -- Redistribution and use in source and binary forms, with or without
6 -- modification, are permitted provided that the following conditions
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.
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
29 local config = require('config');
33 -- Commonly appearing constants
34 core.KEY_BACKSPACE = 8;
36 core.KEY_DELETE = 127;
38 core.KEYSTR_ESCAPE = "\027";
40 core.MENU_RETURN = "return";
41 core.MENU_ENTRY = "entry";
42 core.MENU_SEPARATOR = "separator";
43 core.MENU_SUBMENU = "submenu";
44 core.MENU_CAROUSEL_ENTRY = "carousel_entry";
46 function core.setVerbose(b)
52 loader.setenv("boot_verbose", "YES");
54 loader.unsetenv("boot_verbose");
59 function core.setSingleUser(b)
65 loader.setenv("boot_single", "YES");
67 loader.unsetenv("boot_single");
72 function core.getACPIPresent(checkingSystemDefaults)
73 local c = loader.getenv("hint.acpi.0.rsdp");
76 if (checkingSystemDefaults == true) then
79 -- Otherwise, respect disabled if it's set
80 c = loader.getenv("hint.acpi.0.disabled");
81 return (c == nil) or (tonumber(c) ~= 1);
86 function core.setACPI(b)
92 loader.setenv("acpi_load", "YES");
93 loader.setenv("hint.acpi.0.disabled", "0");
94 loader.unsetenv("loader.acpi_disabled_by_user");
96 loader.unsetenv("acpi_load");
97 loader.setenv("hint.acpi.0.disabled", "1");
98 loader.setenv("loader.acpi_disabled_by_user", "1");
103 function core.setSafeMode(b)
108 loader.setenv("kern.smp.disabled", "1");
109 loader.setenv("hw.ata.ata_dma", "0");
110 loader.setenv("hw.ata.atapi_dma", "0");
111 loader.setenv("hw.ata.wc", "0");
112 loader.setenv("hw.eisa_slots", "0");
113 loader.setenv("kern.eventtimer.periodic", "1");
114 loader.setenv("kern.geom.part.check_integrity", "0");
116 loader.unsetenv("kern.smp.disabled");
117 loader.unsetenv("hw.ata.ata_dma");
118 loader.unsetenv("hw.ata.atapi_dma");
119 loader.unsetenv("hw.ata.wc");
120 loader.unsetenv("hw.eisa_slots");
121 loader.unsetenv("kern.eventtimer.periodic");
122 loader.unsetenv("kern.geom.part.check_integrity");
127 function core.kernelList()
128 local k = loader.getenv("kernel");
129 local v = loader.getenv("kernels") or "";
140 for n in v:gmatch("([^; ]+)[; ]?") do
141 if (unique[n] == nil) then
148 -- Automatically detect other bootable kernel directories using a
149 -- heuristic. Any directory in /boot that contains an ordinary file
150 -- named "kernel" is considered eligible.
151 for file in lfs.dir("/boot") do
152 local fname = "/boot/" .. file;
154 if (file == "." or file == "..") then
158 if (lfs.attributes(fname, "mode") ~= "directory") then
162 if (lfs.attributes(fname .. "/kernel", "mode") ~= "file") then
166 if (unique[file] == nil) then
177 function core.setDefaults()
178 core.setACPI(core.getACPIPresent(true));
179 core.setSafeMode(false);
180 core.setSingleUser(false);
181 core.setVerbose(false);
184 function core.autoboot()
186 loader.perform("autoboot");
191 loader.perform("boot");
194 function core.isSingleUserBoot()
195 local single_user = loader.getenv("boot_single");
196 return single_user ~= nil and single_user:lower() == "yes";
199 function core.isSerialBoot()
200 local c = loader.getenv("console");
203 if (c:find("comconsole") ~= nil) then
208 local s = loader.getenv("boot_serial");
213 local m = loader.getenv("boot_multicons");
220 -- This may be a better candidate for a 'utility' module.
221 function core.shallowCopyTable(tbl)
223 for k, v in pairs(tbl) do
224 if (type(v) == "table") then
225 new_tbl[k] = core.shallowCopyTable(v);
233 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
234 -- generally be set upon execution of the kernel. Because of this, we can't (or
235 -- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
236 -- enabled if we detect it and leave well enough alone if we don't.
237 if (core.getACPIPresent(false)) then