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)
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)
46 if with_kernel == nil then
59 for k, v in ipairs(argv) do
60 if with_kernel and v:sub(1,1) ~= "-" then
63 argstr = argstr .. " " .. v
67 return kernel_name, argstr
77 cmd_name, argv = core.popFrontTable(argv)
78 local kernel, argstr = parse_boot_args(argv)
80 loader.perform("unload")
81 config.selectkernel(kernel)
86 function autoboot(...)
89 cmd_name, argv = core.popFrontTable(argv)
90 local argstr = parse_boot_args(argv, false)
95 -- Commonly appearing constants
96 core.KEY_BACKSPACE = 8
100 core.KEYSTR_ESCAPE = "\027"
102 core.MENU_RETURN = "return"
103 core.MENU_ENTRY = "entry"
104 core.MENU_SEPARATOR = "separator"
105 core.MENU_SUBMENU = "submenu"
106 core.MENU_CAROUSEL_ENTRY = "carousel_entry"
108 function core.setVerbose(b)
114 loader.setenv("boot_verbose", "YES")
116 loader.unsetenv("boot_verbose")
121 function core.setSingleUser(b)
127 loader.setenv("boot_single", "YES")
129 loader.unsetenv("boot_single")
134 function core.getACPIPresent(checkingSystemDefaults)
135 local c = loader.getenv("hint.acpi.0.rsdp")
138 if checkingSystemDefaults then
141 -- Otherwise, respect disabled if it's set
142 c = loader.getenv("hint.acpi.0.disabled")
143 return c == nil or tonumber(c) ~= 1
148 function core.setACPI(b)
154 loader.setenv("acpi_load", "YES")
155 loader.setenv("hint.acpi.0.disabled", "0")
156 loader.unsetenv("loader.acpi_disabled_by_user")
158 loader.unsetenv("acpi_load")
159 loader.setenv("hint.acpi.0.disabled", "1")
160 loader.setenv("loader.acpi_disabled_by_user", "1")
165 function core.setSafeMode(b)
170 loader.setenv("kern.smp.disabled", "1")
171 loader.setenv("hw.ata.ata_dma", "0")
172 loader.setenv("hw.ata.atapi_dma", "0")
173 loader.setenv("hw.ata.wc", "0")
174 loader.setenv("hw.eisa_slots", "0")
175 loader.setenv("kern.eventtimer.periodic", "1")
176 loader.setenv("kern.geom.part.check_integrity", "0")
178 loader.unsetenv("kern.smp.disabled")
179 loader.unsetenv("hw.ata.ata_dma")
180 loader.unsetenv("hw.ata.atapi_dma")
181 loader.unsetenv("hw.ata.wc")
182 loader.unsetenv("hw.eisa_slots")
183 loader.unsetenv("kern.eventtimer.periodic")
184 loader.unsetenv("kern.geom.part.check_integrity")
189 function core.kernelList()
190 local k = loader.getenv("kernel")
191 local v = loader.getenv("kernels")
203 for n in v:gmatch("([^; ]+)[; ]?") do
204 if unique[n] == nil then
211 -- We will not automatically detect kernels to be displayed if
212 -- loader.conf(5) explicitly set 'kernels'.
216 -- Automatically detect other bootable kernel directories using a
217 -- heuristic. Any directory in /boot that contains an ordinary file
218 -- named "kernel" is considered eligible.
219 for file in lfs.dir("/boot") do
220 local fname = "/boot/" .. file
222 if file == "." or file == ".." then
226 if lfs.attributes(fname, "mode") ~= "directory" then
230 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
234 if unique[file] == nil then
245 function core.setDefaults()
246 core.setACPI(core.getACPIPresent(true))
247 core.setSafeMode(false)
248 core.setSingleUser(false)
249 core.setVerbose(false)
252 function core.autoboot(argstr)
254 loader.perform(compose_loader_cmd("autoboot", argstr))
257 function core.boot(argstr)
259 loader.perform(compose_loader_cmd("boot", argstr))
262 function core.isSingleUserBoot()
263 local single_user = loader.getenv("boot_single")
264 return single_user ~= nil and single_user:lower() == "yes"
267 function core.isSerialBoot()
268 local c = loader.getenv("console")
271 if c:find("comconsole") ~= nil then
276 local s = loader.getenv("boot_serial")
281 local m = loader.getenv("boot_multicons")
288 function core.isSystem386()
289 return loader.machine_arch == "i386"
292 -- This may be a better candidate for a 'utility' module.
293 function core.shallowCopyTable(tbl)
295 for k, v in pairs(tbl) do
296 if type(v) == "table" then
297 new_tbl[k] = core.shallowCopyTable(v)
305 -- XXX This should go away if we get the table lib into shape for importing.
306 -- As of now, it requires some 'os' functions, so we'll implement this in lua
308 function core.popFrontTable(tbl)
309 -- Shouldn't reasonably happen
312 elseif #tbl == 1 then
316 local first_value = tbl[1]
318 -- This is not a cheap operation
319 for k, v in ipairs(tbl) do
325 return first_value, new_tbl
328 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
329 -- generally be set upon execution of the kernel. Because of this, we can't (or
330 -- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
331 -- enabled if we detect it and leave well enough alone if we don't.
332 if core.isSystem386() and core.getACPIPresent(false) then