2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 -- All rights reserved.
8 -- Redistribution and use in source and binary forms, with or without
9 -- modification, are permitted provided that the following conditions
11 -- 1. Redistributions of source code must retain the above copyright
12 -- notice, this list of conditions and the following disclaimer.
13 -- 2. Redistributions in binary form must reproduce the above copyright
14 -- notice, this list of conditions and the following disclaimer in the
15 -- documentation and/or other materials provided with the distribution.
17 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 -- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 local config = require("config")
33 local hook = require("hook")
37 local default_safe_mode = false
38 local default_single_user = false
39 local default_verbose = false
41 local function composeLoaderCmd(cmd_name, argstr)
43 cmd_name = cmd_name .. " " .. argstr
48 local function recordDefaults()
49 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386,
50 -- it will generally be set upon execution of the kernel. Because of
51 -- this, we can't (or don't really want to) detect/disable ACPI on !i386
52 -- reliably. Just set it enabled if we detect it and leave well enough
54 local boot_acpi = core.isSystem386() and core.getACPIPresent(false)
55 local boot_single = loader.getenv("boot_single") or "no"
56 local boot_verbose = loader.getenv("boot_verbose") or "no"
57 default_single_user = boot_single:lower() ~= "no"
58 default_verbose = boot_verbose:lower() ~= "no"
63 core.setSingleUser(default_single_user)
64 core.setVerbose(default_verbose)
69 -- try_include will return the loaded module on success, or false and the error
70 -- message on failure.
71 function try_include(module)
72 local status, ret = pcall(require, module)
73 -- ret is the module if we succeeded.
81 -- Commonly appearing constants
82 core.KEY_BACKSPACE = 8
86 -- Note that this is a decimal representation, despite the leading 0 that in
87 -- other contexts (outside of Lua) may mean 'octal'
88 core.KEYSTR_ESCAPE = "\027"
89 core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "["
91 core.MENU_RETURN = "return"
92 core.MENU_ENTRY = "entry"
93 core.MENU_SEPARATOR = "separator"
94 core.MENU_SUBMENU = "submenu"
95 core.MENU_CAROUSEL_ENTRY = "carousel_entry"
97 function core.setVerbose(verbose)
98 if verbose == nil then
99 verbose = not core.verbose
103 loader.setenv("boot_verbose", "YES")
105 loader.unsetenv("boot_verbose")
107 core.verbose = verbose
110 function core.setSingleUser(single_user)
111 if single_user == nil then
112 single_user = not core.su
116 loader.setenv("boot_single", "YES")
118 loader.unsetenv("boot_single")
120 core.su = single_user
123 function core.getACPIPresent(checking_system_defaults)
124 local c = loader.getenv("hint.acpi.0.rsdp")
127 if checking_system_defaults then
130 -- Otherwise, respect disabled if it's set
131 c = loader.getenv("hint.acpi.0.disabled")
132 return c == nil or tonumber(c) ~= 1
137 function core.setACPI(acpi)
143 loader.setenv("acpi_load", "YES")
144 loader.setenv("hint.acpi.0.disabled", "0")
145 loader.unsetenv("loader.acpi_disabled_by_user")
147 loader.unsetenv("acpi_load")
148 loader.setenv("hint.acpi.0.disabled", "1")
149 loader.setenv("loader.acpi_disabled_by_user", "1")
154 function core.setSafeMode(safe_mode)
155 if safe_mode == nil then
156 safe_mode = not core.sm
159 loader.setenv("kern.smp.disabled", "1")
160 loader.setenv("hw.ata.ata_dma", "0")
161 loader.setenv("hw.ata.atapi_dma", "0")
162 loader.setenv("hw.ata.wc", "0")
163 loader.setenv("hw.eisa_slots", "0")
164 loader.setenv("kern.eventtimer.periodic", "1")
165 loader.setenv("kern.geom.part.check_integrity", "0")
167 loader.unsetenv("kern.smp.disabled")
168 loader.unsetenv("hw.ata.ata_dma")
169 loader.unsetenv("hw.ata.atapi_dma")
170 loader.unsetenv("hw.ata.wc")
171 loader.unsetenv("hw.eisa_slots")
172 loader.unsetenv("kern.eventtimer.periodic")
173 loader.unsetenv("kern.geom.part.check_integrity")
178 function core.clearCachedKernels()
179 -- Clear the kernel cache on config changes, autodetect might have
180 -- changed or if we've switched boot environments then we could have
182 core.cached_kernels = nil
185 function core.kernelList()
186 if core.cached_kernels ~= nil then
187 return core.cached_kernels
190 local k = loader.getenv("kernel")
191 local v = loader.getenv("kernels")
192 local autodetect = loader.getenv("kernels_autodetect") or ""
204 for n in v:gmatch("([^;, ]+)[;, ]?") do
205 if unique[n] == nil then
213 -- Base whether we autodetect kernels or not on a loader.conf(5)
214 -- setting, kernels_autodetect. If it's set to 'yes', we'll add
215 -- any kernels we detect based on the criteria described.
216 if autodetect:lower() ~= "yes" then
217 core.cached_kernels = kernels
218 return core.cached_kernels
221 -- Automatically detect other bootable kernel directories using a
222 -- heuristic. Any directory in /boot that contains an ordinary file
223 -- named "kernel" is considered eligible.
224 for file in lfs.dir("/boot") do
225 local fname = "/boot/" .. file
227 if file == "." or file == ".." then
231 if lfs.attributes(fname, "mode") ~= "directory" then
235 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
239 if unique[file] == nil then
247 core.cached_kernels = kernels
248 return core.cached_kernels
251 function core.bootenvDefault()
252 return loader.getenv("zfs_be_active")
255 function core.bootenvList()
256 local bootenv_count = tonumber(loader.getenv("bootenvs_count"))
262 if bootenv_count == nil or bootenv_count <= 0 then
266 -- Currently selected bootenv is always first/default
267 curenv = core.bootenvDefault()
268 if curenv ~= nil then
269 envcount = envcount + 1
270 bootenvs[envcount] = curenv
271 unique[curenv] = true
274 for curenv_idx = 0, bootenv_count - 1 do
275 curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]")
276 if curenv ~= nil and unique[curenv] == nil then
277 envcount = envcount + 1
278 bootenvs[envcount] = curenv
279 unique[curenv] = true
285 function core.setDefaults()
286 core.setACPI(core.getACPIPresent(true))
287 core.setSafeMode(default_safe_mode)
288 core.setSingleUser(default_single_user)
289 core.setVerbose(default_verbose)
292 function core.autoboot(argstr)
293 -- loadelf() only if we've not already loaded a kernel
294 if loader.getenv("kernelname") == nil then
297 loader.perform(composeLoaderCmd("autoboot", argstr))
300 function core.boot(argstr)
301 -- loadelf() only if we've not already loaded a kernel
302 if loader.getenv("kernelname") == nil then
305 loader.perform(composeLoaderCmd("boot", argstr))
308 function core.isSingleUserBoot()
309 local single_user = loader.getenv("boot_single")
310 return single_user ~= nil and single_user:lower() == "yes"
313 function core.isUEFIBoot()
314 local efiver = loader.getenv("efi-version")
319 function core.isZFSBoot()
320 local c = loader.getenv("currdev")
323 return c:match("^zfs:") ~= nil
328 function core.isSerialBoot()
329 local s = loader.getenv("boot_serial")
334 local m = loader.getenv("boot_multicons")
341 function core.isSystem386()
342 return loader.machine_arch == "i386"
345 -- Is the menu skipped in the environment in which we've booted?
346 function core.isMenuSkipped()
347 return string.lower(loader.getenv("beastie_disable") or "") == "yes"
350 -- This may be a better candidate for a 'utility' module.
351 function core.deepCopyTable(tbl)
353 for k, v in pairs(tbl) do
354 if type(v) == "table" then
355 new_tbl[k] = core.deepCopyTable(v)
363 -- XXX This should go away if we get the table lib into shape for importing.
364 -- As of now, it requires some 'os' functions, so we'll implement this in lua
366 function core.popFrontTable(tbl)
367 -- Shouldn't reasonably happen
370 elseif #tbl == 1 then
374 local first_value = tbl[1]
376 -- This is not a cheap operation
377 for k, v in ipairs(tbl) do
383 return first_value, new_tbl
387 hook.register("config.reloaded", core.clearCachedKernels)