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 bootenv_list = "bootenvs"
43 local function composeLoaderCmd(cmd_name, argstr)
45 cmd_name = cmd_name .. " " .. argstr
50 local function recordDefaults()
51 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386,
52 -- it will generally be set upon execution of the kernel. Because of
53 -- this, we can't (or don't really want to) detect/disable ACPI on !i386
54 -- reliably. Just set it enabled if we detect it and leave well enough
56 local boot_acpi = core.isSystem386() and core.getACPIPresent(false)
57 local boot_single = loader.getenv("boot_single") or "no"
58 local boot_verbose = loader.getenv("boot_verbose") or "no"
59 default_single_user = boot_single:lower() ~= "no"
60 default_verbose = boot_verbose:lower() ~= "no"
65 core.setSingleUser(default_single_user)
66 core.setVerbose(default_verbose)
71 -- try_include will return the loaded module on success, or false and the error
72 -- message on failure.
73 function try_include(module)
74 if module:sub(1, 1) ~= "/" then
75 local lua_path = loader.lua_path
76 -- XXX Temporary compat shim; this should be removed once the
77 -- loader.lua_path export has sufficiently spread.
78 if lua_path == nil then
79 lua_path = "/boot/lua"
81 module = lua_path .. "/" .. module
82 -- We only attempt to append an extension if an absolute path
83 -- wasn't specified. This assumes that the caller either wants
84 -- to treat this like it would require() and specify just the
85 -- base filename, or they know what they're doing as they've
86 -- specified an absolute path and we shouldn't impede.
87 if module:match(".lua$") == nil then
88 module = module .. ".lua"
91 if lfs.attributes(module, "mode") ~= "file" then
99 -- Commonly appearing constants
100 core.KEY_BACKSPACE = 8
102 core.KEY_DELETE = 127
104 -- Note that this is a decimal representation, despite the leading 0 that in
105 -- other contexts (outside of Lua) may mean 'octal'
106 core.KEYSTR_ESCAPE = "\027"
107 core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "["
108 core.KEYSTR_RESET = core.KEYSTR_ESCAPE .. "c"
110 core.MENU_RETURN = "return"
111 core.MENU_ENTRY = "entry"
112 core.MENU_SEPARATOR = "separator"
113 core.MENU_SUBMENU = "submenu"
114 core.MENU_CAROUSEL_ENTRY = "carousel_entry"
116 function core.setVerbose(verbose)
117 if verbose == nil then
118 verbose = not core.verbose
122 loader.setenv("boot_verbose", "YES")
124 loader.unsetenv("boot_verbose")
126 core.verbose = verbose
129 function core.setSingleUser(single_user)
130 if single_user == nil then
131 single_user = not core.su
135 loader.setenv("boot_single", "YES")
137 loader.unsetenv("boot_single")
139 core.su = single_user
142 function core.getACPIPresent(checking_system_defaults)
143 local c = loader.getenv("hint.acpi.0.rsdp")
146 if checking_system_defaults then
149 -- Otherwise, respect disabled if it's set
150 c = loader.getenv("hint.acpi.0.disabled")
151 return c == nil or tonumber(c) ~= 1
156 function core.setACPI(acpi)
162 loader.setenv("acpi_load", "YES")
163 loader.setenv("hint.acpi.0.disabled", "0")
164 loader.unsetenv("loader.acpi_disabled_by_user")
166 loader.unsetenv("acpi_load")
167 loader.setenv("hint.acpi.0.disabled", "1")
168 loader.setenv("loader.acpi_disabled_by_user", "1")
173 function core.setSafeMode(safe_mode)
174 if safe_mode == nil then
175 safe_mode = not core.sm
178 loader.setenv("kern.smp.disabled", "1")
179 loader.setenv("hw.ata.ata_dma", "0")
180 loader.setenv("hw.ata.atapi_dma", "0")
181 loader.setenv("hw.ata.wc", "0")
182 loader.setenv("hw.eisa_slots", "0")
183 loader.setenv("kern.eventtimer.periodic", "1")
184 loader.setenv("kern.geom.part.check_integrity", "0")
186 loader.unsetenv("kern.smp.disabled")
187 loader.unsetenv("hw.ata.ata_dma")
188 loader.unsetenv("hw.ata.atapi_dma")
189 loader.unsetenv("hw.ata.wc")
190 loader.unsetenv("hw.eisa_slots")
191 loader.unsetenv("kern.eventtimer.periodic")
192 loader.unsetenv("kern.geom.part.check_integrity")
197 function core.clearCachedKernels()
198 -- Clear the kernel cache on config changes, autodetect might have
199 -- changed or if we've switched boot environments then we could have
201 core.cached_kernels = nil
204 function core.kernelList()
205 if core.cached_kernels ~= nil then
206 return core.cached_kernels
209 local k = loader.getenv("kernel")
210 local v = loader.getenv("kernels")
211 local autodetect = loader.getenv("kernels_autodetect") or ""
223 for n in v:gmatch("([^;, ]+)[;, ]?") do
224 if unique[n] == nil then
232 -- Do not attempt to autodetect if underlying filesystem
233 -- do not support directory listing (e.g. tftp, http)
234 if not lfs.attributes("/boot", "mode") then
236 loader.setenv("kernels_autodetect", "NO")
239 -- Base whether we autodetect kernels or not on a loader.conf(5)
240 -- setting, kernels_autodetect. If it's set to 'yes', we'll add
241 -- any kernels we detect based on the criteria described.
242 if autodetect:lower() ~= "yes" then
243 core.cached_kernels = kernels
244 return core.cached_kernels
247 -- Automatically detect other bootable kernel directories using a
248 -- heuristic. Any directory in /boot that contains an ordinary file
249 -- named "kernel" is considered eligible.
250 for file, ftype in lfs.dir("/boot") do
251 local fname = "/boot/" .. file
253 if file == "." or file == ".." then
258 if ftype ~= lfs.DT_DIR then
261 elseif lfs.attributes(fname, "mode") ~= "directory" then
265 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
269 if unique[file] == nil then
277 core.cached_kernels = kernels
278 return core.cached_kernels
281 function core.bootenvDefault()
282 return loader.getenv("zfs_be_active")
285 function core.bootenvList()
286 local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count"))
292 if bootenv_count == nil or bootenv_count <= 0 then
296 -- Currently selected bootenv is always first/default
297 -- On the rewinded list the bootenv may not exists
298 if core.isRewinded() then
299 curenv = core.bootenvDefaultRewinded()
301 curenv = core.bootenvDefault()
303 if curenv ~= nil then
304 envcount = envcount + 1
305 bootenvs[envcount] = curenv
306 unique[curenv] = true
309 for curenv_idx = 0, bootenv_count - 1 do
310 curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]")
311 if curenv ~= nil and unique[curenv] == nil then
312 envcount = envcount + 1
313 bootenvs[envcount] = curenv
314 unique[curenv] = true
320 function core.isCheckpointed()
321 return loader.getenv("zpool_checkpoint") ~= nil
324 function core.bootenvDefaultRewinded()
325 local defname = "zfs:!" .. string.sub(core.bootenvDefault(), 5)
326 local bootenv_count = tonumber("bootenvs_check_count")
328 if bootenv_count == nil or bootenv_count <= 0 then
332 for curenv_idx = 0, bootenv_count - 1 do
333 local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]")
334 if curenv == defname then
339 return loader.getenv("bootenvs_check[0]")
342 function core.isRewinded()
343 return bootenv_list == "bootenvs_check"
346 function core.changeRewindCheckpoint()
347 if core.isRewinded() then
348 bootenv_list = "bootenvs"
350 bootenv_list = "bootenvs_check"
354 function core.setDefaults()
355 core.setACPI(core.getACPIPresent(true))
356 core.setSafeMode(default_safe_mode)
357 core.setSingleUser(default_single_user)
358 core.setVerbose(default_verbose)
361 function core.autoboot(argstr)
362 -- loadelf() only if we've not already loaded a kernel
363 if loader.getenv("kernelname") == nil then
366 loader.perform(composeLoaderCmd("autoboot", argstr))
369 function core.boot(argstr)
370 -- loadelf() only if we've not already loaded a kernel
371 if loader.getenv("kernelname") == nil then
374 loader.perform(composeLoaderCmd("boot", argstr))
377 function core.isSingleUserBoot()
378 local single_user = loader.getenv("boot_single")
379 return single_user ~= nil and single_user:lower() == "yes"
382 function core.isUEFIBoot()
383 local efiver = loader.getenv("efi-version")
388 function core.isZFSBoot()
389 local c = loader.getenv("currdev")
392 return c:match("^zfs:") ~= nil
397 function core.isFramebufferConsole()
398 local c = loader.getenv("console")
400 if c:find("efi") == nil and c:find("vidconsole") == nil then
403 if loader.getenv("screen.depth") ~= nil then
410 function core.isSerialConsole()
411 local c = loader.getenv("console")
413 -- serial console is comconsole, but also userboot.
414 -- userboot is there, because we have no way to know
415 -- if the user terminal can draw unicode box chars or not.
416 if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then
423 function core.isSerialBoot()
424 local s = loader.getenv("boot_serial")
429 local m = loader.getenv("boot_multicons")
436 function core.isSystem386()
437 return loader.machine_arch == "i386"
440 -- Is the menu skipped in the environment in which we've booted?
441 function core.isMenuSkipped()
442 return string.lower(loader.getenv("beastie_disable") or "") == "yes"
445 -- This may be a better candidate for a 'utility' module.
446 function core.deepCopyTable(tbl)
448 for k, v in pairs(tbl) do
449 if type(v) == "table" then
450 new_tbl[k] = core.deepCopyTable(v)
458 -- XXX This should go away if we get the table lib into shape for importing.
459 -- As of now, it requires some 'os' functions, so we'll implement this in lua
461 function core.popFrontTable(tbl)
462 -- Shouldn't reasonably happen
465 elseif #tbl == 1 then
469 local first_value = tbl[1]
471 -- This is not a cheap operation
472 for k, v in ipairs(tbl) do
478 return first_value, new_tbl
481 function core.getConsoleName()
482 if loader.getenv("boot_multicons") ~= nil then
483 if loader.getenv("boot_serial") ~= nil then
484 return "Dual (Serial primary)"
486 return "Dual (Video primary)"
489 if loader.getenv("boot_serial") ~= nil then
497 function core.nextConsoleChoice()
498 if loader.getenv("boot_multicons") ~= nil then
499 if loader.getenv("boot_serial") ~= nil then
500 loader.unsetenv("boot_serial")
502 loader.unsetenv("boot_multicons")
503 loader.setenv("boot_serial", "YES")
506 if loader.getenv("boot_serial") ~= nil then
507 loader.unsetenv("boot_serial")
509 loader.setenv("boot_multicons", "YES")
510 loader.setenv("boot_serial", "YES")
516 hook.register("config.reloaded", core.clearCachedKernels)