]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/lua/core.lua
Optionally bind ktls threads to NUMA domains
[FreeBSD/FreeBSD.git] / stand / lua / core.lua
1 --
2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 --
4 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 -- All rights reserved.
7 --
8 -- Redistribution and use in source and binary forms, with or without
9 -- modification, are permitted provided that the following conditions
10 -- are met:
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.
16 --
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
27 -- SUCH DAMAGE.
28 --
29 -- $FreeBSD$
30 --
31
32 local config = require("config")
33 local hook = require("hook")
34
35 local core = {}
36
37 local default_safe_mode = false
38 local default_single_user = false
39 local default_verbose = false
40
41 local bootenv_list = "bootenvs"
42
43 local function composeLoaderCmd(cmd_name, argstr)
44         if argstr ~= nil then
45                 cmd_name = cmd_name .. " " .. argstr
46         end
47         return cmd_name
48 end
49
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
55         -- alone if we don't.
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"
61
62         if boot_acpi then
63                 core.setACPI(true)
64         end
65         core.setSingleUser(default_single_user)
66         core.setVerbose(default_verbose)
67 end
68
69
70 -- Globals
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"
80                 end
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"
89                 end
90         end
91         if lfs.attributes(module, "mode") ~= "file" then
92                 return
93         end
94
95         return dofile(module)
96 end
97
98 -- Module exports
99 -- Commonly appearing constants
100 core.KEY_BACKSPACE      = 8
101 core.KEY_ENTER          = 13
102 core.KEY_DELETE         = 127
103
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"
109
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"
115
116 function core.setVerbose(verbose)
117         if verbose == nil then
118                 verbose = not core.verbose
119         end
120
121         if verbose then
122                 loader.setenv("boot_verbose", "YES")
123         else
124                 loader.unsetenv("boot_verbose")
125         end
126         core.verbose = verbose
127 end
128
129 function core.setSingleUser(single_user)
130         if single_user == nil then
131                 single_user = not core.su
132         end
133
134         if single_user then
135                 loader.setenv("boot_single", "YES")
136         else
137                 loader.unsetenv("boot_single")
138         end
139         core.su = single_user
140 end
141
142 function core.getACPIPresent(checking_system_defaults)
143         local c = loader.getenv("hint.acpi.0.rsdp")
144
145         if c ~= nil then
146                 if checking_system_defaults then
147                         return true
148                 end
149                 -- Otherwise, respect disabled if it's set
150                 c = loader.getenv("hint.acpi.0.disabled")
151                 return c == nil or tonumber(c) ~= 1
152         end
153         return false
154 end
155
156 function core.setACPI(acpi)
157         if acpi == nil then
158                 acpi = not core.acpi
159         end
160
161         if acpi then
162                 loader.setenv("acpi_load", "YES")
163                 loader.setenv("hint.acpi.0.disabled", "0")
164                 loader.unsetenv("loader.acpi_disabled_by_user")
165         else
166                 loader.unsetenv("acpi_load")
167                 loader.setenv("hint.acpi.0.disabled", "1")
168                 loader.setenv("loader.acpi_disabled_by_user", "1")
169         end
170         core.acpi = acpi
171 end
172
173 function core.setSafeMode(safe_mode)
174         if safe_mode == nil then
175                 safe_mode = not core.sm
176         end
177         if safe_mode then
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")
185         else
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")
193         end
194         core.sm = safe_mode
195 end
196
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
200         -- a new kernel set.
201         core.cached_kernels = nil
202 end
203
204 function core.kernelList()
205         if core.cached_kernels ~= nil then
206                 return core.cached_kernels
207         end
208
209         local k = loader.getenv("kernel")
210         local v = loader.getenv("kernels")
211         local autodetect = loader.getenv("kernels_autodetect") or ""
212
213         local kernels = {}
214         local unique = {}
215         local i = 0
216         if k ~= nil then
217                 i = i + 1
218                 kernels[i] = k
219                 unique[k] = true
220         end
221
222         if v ~= nil then
223                 for n in v:gmatch("([^;, ]+)[;, ]?") do
224                         if unique[n] == nil then
225                                 i = i + 1
226                                 kernels[i] = n
227                                 unique[n] = true
228                         end
229                 end
230         end
231
232         -- Base whether we autodetect kernels or not on a loader.conf(5)
233         -- setting, kernels_autodetect. If it's set to 'yes', we'll add
234         -- any kernels we detect based on the criteria described.
235         if autodetect:lower() ~= "yes" then
236                 core.cached_kernels = kernels
237                 return core.cached_kernels
238         end
239
240         -- Automatically detect other bootable kernel directories using a
241         -- heuristic.  Any directory in /boot that contains an ordinary file
242         -- named "kernel" is considered eligible.
243         for file in lfs.dir("/boot") do
244                 local fname = "/boot/" .. file
245
246                 if file == "." or file == ".." then
247                         goto continue
248                 end
249
250                 if lfs.attributes(fname, "mode") ~= "directory" then
251                         goto continue
252                 end
253
254                 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
255                         goto continue
256                 end
257
258                 if unique[file] == nil then
259                         i = i + 1
260                         kernels[i] = file
261                         unique[file] = true
262                 end
263
264                 ::continue::
265         end
266         core.cached_kernels = kernels
267         return core.cached_kernels
268 end
269
270 function core.bootenvDefault()
271         return loader.getenv("zfs_be_active")
272 end
273
274 function core.bootenvList()
275         local bootenv_count = tonumber(loader.getenv(bootenv_list .. "_count"))
276         local bootenvs = {}
277         local curenv
278         local envcount = 0
279         local unique = {}
280
281         if bootenv_count == nil or bootenv_count <= 0 then
282                 return bootenvs
283         end
284
285         -- Currently selected bootenv is always first/default
286         -- On the rewinded list the bootenv may not exists
287         if core.isRewinded() then
288                 curenv = core.bootenvDefaultRewinded()
289         else
290                 curenv = core.bootenvDefault()
291         end
292         if curenv ~= nil then
293                 envcount = envcount + 1
294                 bootenvs[envcount] = curenv
295                 unique[curenv] = true
296         end
297
298         for curenv_idx = 0, bootenv_count - 1 do
299                 curenv = loader.getenv(bootenv_list .. "[" .. curenv_idx .. "]")
300                 if curenv ~= nil and unique[curenv] == nil then
301                         envcount = envcount + 1
302                         bootenvs[envcount] = curenv
303                         unique[curenv] = true
304                 end
305         end
306         return bootenvs
307 end
308
309 function core.isCheckpointed()
310         return loader.getenv("zpool_checkpoint") ~= nil
311 end
312
313 function core.bootenvDefaultRewinded()
314         local defname =  "zfs:!" .. string.sub(core.bootenvDefault(), 5)
315         local bootenv_count = tonumber("bootenvs_check_count")
316
317         if bootenv_count == nil or bootenv_count <= 0 then
318                 return defname
319         end
320
321         for curenv_idx = 0, bootenv_count - 1 do
322                 local curenv = loader.getenv("bootenvs_check[" .. curenv_idx .. "]")
323                 if curenv == defname then
324                         return defname
325                 end
326         end
327
328         return loader.getenv("bootenvs_check[0]")
329 end
330
331 function core.isRewinded()
332         return bootenv_list == "bootenvs_check"
333 end
334
335 function core.changeRewindCheckpoint()
336         if core.isRewinded() then
337                 bootenv_list = "bootenvs"
338         else
339                 bootenv_list = "bootenvs_check"
340         end
341 end
342
343 function core.setDefaults()
344         core.setACPI(core.getACPIPresent(true))
345         core.setSafeMode(default_safe_mode)
346         core.setSingleUser(default_single_user)
347         core.setVerbose(default_verbose)
348 end
349
350 function core.autoboot(argstr)
351         -- loadelf() only if we've not already loaded a kernel
352         if loader.getenv("kernelname") == nil then
353                 config.loadelf()
354         end
355         loader.perform(composeLoaderCmd("autoboot", argstr))
356 end
357
358 function core.boot(argstr)
359         -- loadelf() only if we've not already loaded a kernel
360         if loader.getenv("kernelname") == nil then
361                 config.loadelf()
362         end
363         loader.perform(composeLoaderCmd("boot", argstr))
364 end
365
366 function core.isSingleUserBoot()
367         local single_user = loader.getenv("boot_single")
368         return single_user ~= nil and single_user:lower() == "yes"
369 end
370
371 function core.isUEFIBoot()
372         local efiver = loader.getenv("efi-version")
373
374         return efiver ~= nil
375 end
376
377 function core.isZFSBoot()
378         local c = loader.getenv("currdev")
379
380         if c ~= nil then
381                 return c:match("^zfs:") ~= nil
382         end
383         return false
384 end
385
386 function core.isSerialConsole()
387         local c = loader.getenv("console")
388         if c ~= nil then
389                 if c:find("comconsole") ~= nil then
390                         return true
391                 end
392         end
393         return false
394 end
395
396 function core.isSerialBoot()
397         local s = loader.getenv("boot_serial")
398         if s ~= nil then
399                 return true
400         end
401
402         local m = loader.getenv("boot_multicons")
403         if m ~= nil then
404                 return true
405         end
406         return false
407 end
408
409 function core.isSystem386()
410         return loader.machine_arch == "i386"
411 end
412
413 -- Is the menu skipped in the environment in which we've booted?
414 function core.isMenuSkipped()
415         return string.lower(loader.getenv("beastie_disable") or "") == "yes"
416 end
417
418 -- This may be a better candidate for a 'utility' module.
419 function core.deepCopyTable(tbl)
420         local new_tbl = {}
421         for k, v in pairs(tbl) do
422                 if type(v) == "table" then
423                         new_tbl[k] = core.deepCopyTable(v)
424                 else
425                         new_tbl[k] = v
426                 end
427         end
428         return new_tbl
429 end
430
431 -- XXX This should go away if we get the table lib into shape for importing.
432 -- As of now, it requires some 'os' functions, so we'll implement this in lua
433 -- for our uses
434 function core.popFrontTable(tbl)
435         -- Shouldn't reasonably happen
436         if #tbl == 0 then
437                 return nil, nil
438         elseif #tbl == 1 then
439                 return tbl[1], {}
440         end
441
442         local first_value = tbl[1]
443         local new_tbl = {}
444         -- This is not a cheap operation
445         for k, v in ipairs(tbl) do
446                 if k > 1 then
447                         new_tbl[k - 1] = v
448                 end
449         end
450
451         return first_value, new_tbl
452 end
453
454 function core.getConsoleName()
455         if loader.getenv("boot_multicons") ~= nil then
456                 if loader.getenv("boot_serial") ~= nil then
457                         return "Dual (Serial primary)"
458                 else
459                         return "Dual (Video primary)"
460                 end
461         else
462                 if loader.getenv("boot_serial") ~= nil then
463                         return "Serial"
464                 else
465                         return "Video"
466                 end
467         end
468 end
469
470 function core.nextConsoleChoice()
471         if loader.getenv("boot_multicons") ~= nil then
472                 if loader.getenv("boot_serial") ~= nil then
473                         loader.unsetenv("boot_serial")
474                 else
475                         loader.unsetenv("boot_multicons")
476                         loader.setenv("boot_serial", "YES")
477                 end
478         else
479                 if loader.getenv("boot_serial") ~= nil then
480                         loader.unsetenv("boot_serial")
481                 else
482                         loader.setenv("boot_multicons", "YES")
483                         loader.setenv("boot_serial", "YES")
484                 end
485         end
486 end
487
488 recordDefaults()
489 hook.register("config.reloaded", core.clearCachedKernels)
490 return core