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 local compose_loader_cmd = function(cmd_name, argstr)
34 if (argstr ~= nil) then
35 cmd_name = cmd_name .. " " .. argstr;
41 -- Parses arguments to boot and returns two values: kernel_name, argstr
42 -- Defaults to nil and "" respectively.
43 -- This will also parse arguments to autoboot, but the with_kernel argument
44 -- will need to be explicitly overwritten to false
45 local parse_boot_args = function(argv, with_kernel)
49 if (with_kernel == nil) then
55 for k, v in ipairs(argv) do
56 if (with_kernel) and (v:sub(1,1) ~= "-") then
59 argstr = argstr .. " " .. v;
63 return kernel_name, argstr;
73 cmd_name, argv = core.popFrontTable(argv);
74 local kernel, argstr = parse_boot_args(argv);
75 if (kernel ~= nil) then
76 loader.perform("unload");
77 config.selectkernel(kernel);
82 function autoboot(...)
85 cmd_name, argv = core.popFrontTable(argv);
86 local argstr = parse_boot_args(argv, false);
87 core.autoboot(argstr);
91 -- Commonly appearing constants
92 core.KEY_BACKSPACE = 8;
94 core.KEY_DELETE = 127;
96 core.KEYSTR_ESCAPE = "\027";
98 core.MENU_RETURN = "return";
99 core.MENU_ENTRY = "entry";
100 core.MENU_SEPARATOR = "separator";
101 core.MENU_SUBMENU = "submenu";
102 core.MENU_CAROUSEL_ENTRY = "carousel_entry";
104 function core.setVerbose(b)
106 b = not core.verbose;
110 loader.setenv("boot_verbose", "YES");
112 loader.unsetenv("boot_verbose");
117 function core.setSingleUser(b)
123 loader.setenv("boot_single", "YES");
125 loader.unsetenv("boot_single");
130 function core.getACPIPresent(checkingSystemDefaults)
131 local c = loader.getenv("hint.acpi.0.rsdp");
134 if (checkingSystemDefaults == true) then
137 -- Otherwise, respect disabled if it's set
138 c = loader.getenv("hint.acpi.0.disabled");
139 return (c == nil) or (tonumber(c) ~= 1);
144 function core.setACPI(b)
150 loader.setenv("acpi_load", "YES");
151 loader.setenv("hint.acpi.0.disabled", "0");
152 loader.unsetenv("loader.acpi_disabled_by_user");
154 loader.unsetenv("acpi_load");
155 loader.setenv("hint.acpi.0.disabled", "1");
156 loader.setenv("loader.acpi_disabled_by_user", "1");
161 function core.setSafeMode(b)
166 loader.setenv("kern.smp.disabled", "1");
167 loader.setenv("hw.ata.ata_dma", "0");
168 loader.setenv("hw.ata.atapi_dma", "0");
169 loader.setenv("hw.ata.wc", "0");
170 loader.setenv("hw.eisa_slots", "0");
171 loader.setenv("kern.eventtimer.periodic", "1");
172 loader.setenv("kern.geom.part.check_integrity", "0");
174 loader.unsetenv("kern.smp.disabled");
175 loader.unsetenv("hw.ata.ata_dma");
176 loader.unsetenv("hw.ata.atapi_dma");
177 loader.unsetenv("hw.ata.wc");
178 loader.unsetenv("hw.eisa_slots");
179 loader.unsetenv("kern.eventtimer.periodic");
180 loader.unsetenv("kern.geom.part.check_integrity");
185 function core.kernelList()
186 local k = loader.getenv("kernel");
187 local v = loader.getenv("kernels") or "";
198 for n in v:gmatch("([^; ]+)[; ]?") do
199 if (unique[n] == nil) then
206 -- Automatically detect other bootable kernel directories using a
207 -- heuristic. Any directory in /boot that contains an ordinary file
208 -- named "kernel" is considered eligible.
209 for file in lfs.dir("/boot") do
210 local fname = "/boot/" .. file;
212 if (file == "." or file == "..") then
216 if (lfs.attributes(fname, "mode") ~= "directory") then
220 if (lfs.attributes(fname .. "/kernel", "mode") ~= "file") then
224 if (unique[file] == nil) then
235 function core.setDefaults()
236 core.setACPI(core.getACPIPresent(true));
237 core.setSafeMode(false);
238 core.setSingleUser(false);
239 core.setVerbose(false);
242 function core.autoboot(argstr)
244 loader.perform(compose_loader_cmd("autoboot", argstr));
247 function core.boot(argstr)
249 loader.perform(compose_loader_cmd("boot", argstr));
252 function core.isSingleUserBoot()
253 local single_user = loader.getenv("boot_single");
254 return single_user ~= nil and single_user:lower() == "yes";
257 function core.isSerialBoot()
258 local c = loader.getenv("console");
261 if (c:find("comconsole") ~= nil) then
266 local s = loader.getenv("boot_serial");
271 local m = loader.getenv("boot_multicons");
278 function core.isSystem386()
279 return (loader.machine_arch == "i386");
282 -- This may be a better candidate for a 'utility' module.
283 function core.shallowCopyTable(tbl)
285 for k, v in pairs(tbl) do
286 if (type(v) == "table") then
287 new_tbl[k] = core.shallowCopyTable(v);
295 -- XXX This should go away if we get the table lib into shape for importing.
296 -- As of now, it requires some 'os' functions, so we'll implement this in lua
298 function core.popFrontTable(tbl)
299 -- Shouldn't reasonably happen
302 elseif (#tbl == 1) then
306 local first_value = tbl[1];
308 -- This is not a cheap operation
309 for k, v in ipairs(tbl) do
315 return first_value, new_tbl;
318 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
319 -- generally be set upon execution of the kernel. Because of this, we can't (or
320 -- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
321 -- enabled if we detect it and leave well enough alone if we don't.
322 if (core.isSystem386()) and (core.getACPIPresent(false)) then