]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/lua/core.lua
Import tzdata 2018d
[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 function composeLoaderCmd(cmd_name, argstr)
38         if argstr ~= nil then
39                 cmd_name = cmd_name .. " " .. argstr
40         end
41         return cmd_name
42 end
43
44 -- Module exports
45 -- Commonly appearing constants
46 core.KEY_BACKSPACE      = 8
47 core.KEY_ENTER          = 13
48 core.KEY_DELETE         = 127
49
50 -- Note that this is a decimal representation, despite the leading 0 that in
51 -- other contexts (outside of Lua) may mean 'octal'
52 core.KEYSTR_ESCAPE      = "\027"
53 core.KEYSTR_CSI         = core.KEYSTR_ESCAPE .. "["
54
55 core.MENU_RETURN        = "return"
56 core.MENU_ENTRY         = "entry"
57 core.MENU_SEPARATOR     = "separator"
58 core.MENU_SUBMENU       = "submenu"
59 core.MENU_CAROUSEL_ENTRY        = "carousel_entry"
60
61 function core.setVerbose(verbose)
62         if verbose == nil then
63                 verbose = not core.verbose
64         end
65
66         if verbose then
67                 loader.setenv("boot_verbose", "YES")
68         else
69                 loader.unsetenv("boot_verbose")
70         end
71         core.verbose = verbose
72 end
73
74 function core.setSingleUser(single_user)
75         if single_user == nil then
76                 single_user = not core.su
77         end
78
79         if single_user then
80                 loader.setenv("boot_single", "YES")
81         else
82                 loader.unsetenv("boot_single")
83         end
84         core.su = single_user
85 end
86
87 function core.getACPIPresent(checking_system_defaults)
88         local c = loader.getenv("hint.acpi.0.rsdp")
89
90         if c ~= nil then
91                 if checking_system_defaults then
92                         return true
93                 end
94                 -- Otherwise, respect disabled if it's set
95                 c = loader.getenv("hint.acpi.0.disabled")
96                 return c == nil or tonumber(c) ~= 1
97         end
98         return false
99 end
100
101 function core.setACPI(acpi)
102         if acpi == nil then
103                 acpi = not core.acpi
104         end
105
106         if acpi then
107                 loader.setenv("acpi_load", "YES")
108                 loader.setenv("hint.acpi.0.disabled", "0")
109                 loader.unsetenv("loader.acpi_disabled_by_user")
110         else
111                 loader.unsetenv("acpi_load")
112                 loader.setenv("hint.acpi.0.disabled", "1")
113                 loader.setenv("loader.acpi_disabled_by_user", "1")
114         end
115         core.acpi = acpi
116 end
117
118 function core.setSafeMode(safe_mode)
119         if safe_mode == nil then
120                 safe_mode = not core.sm
121         end
122         if safe_mode then
123                 loader.setenv("kern.smp.disabled", "1")
124                 loader.setenv("hw.ata.ata_dma", "0")
125                 loader.setenv("hw.ata.atapi_dma", "0")
126                 loader.setenv("hw.ata.wc", "0")
127                 loader.setenv("hw.eisa_slots", "0")
128                 loader.setenv("kern.eventtimer.periodic", "1")
129                 loader.setenv("kern.geom.part.check_integrity", "0")
130         else
131                 loader.unsetenv("kern.smp.disabled")
132                 loader.unsetenv("hw.ata.ata_dma")
133                 loader.unsetenv("hw.ata.atapi_dma")
134                 loader.unsetenv("hw.ata.wc")
135                 loader.unsetenv("hw.eisa_slots")
136                 loader.unsetenv("kern.eventtimer.periodic")
137                 loader.unsetenv("kern.geom.part.check_integrity")
138         end
139         core.sm = safe_mode
140 end
141
142 function core.clearCachedKernels()
143         -- Clear the kernel cache on config changes, autodetect might have
144         -- changed or if we've switched boot environments then we could have
145         -- a new kernel set.
146         core.cached_kernels = nil
147 end
148
149 function core.kernelList()
150         if core.cached_kernels ~= nil then
151                 return core.cached_kernels
152         end
153
154         local k = loader.getenv("kernel")
155         local v = loader.getenv("kernels")
156         local autodetect = loader.getenv("kernels_autodetect") or ""
157
158         local kernels = {}
159         local unique = {}
160         local i = 0
161         if k ~= nil then
162                 i = i + 1
163                 kernels[i] = k
164                 unique[k] = true
165         end
166
167         if v ~= nil then
168                 for n in v:gmatch("([^; ]+)[; ]?") do
169                         if unique[n] == nil then
170                                 i = i + 1
171                                 kernels[i] = n
172                                 unique[n] = true
173                         end
174                 end
175         end
176
177         -- Base whether we autodetect kernels or not on a loader.conf(5)
178         -- setting, kernels_autodetect. If it's set to 'yes', we'll add
179         -- any kernels we detect based on the criteria described.
180         if autodetect:lower() ~= "yes" then
181                 core.cached_kernels = kernels
182                 return core.cached_kernels
183         end
184
185         -- Automatically detect other bootable kernel directories using a
186         -- heuristic.  Any directory in /boot that contains an ordinary file
187         -- named "kernel" is considered eligible.
188         for file in lfs.dir("/boot") do
189                 local fname = "/boot/" .. file
190
191                 if file == "." or file == ".." then
192                         goto continue
193                 end
194
195                 if lfs.attributes(fname, "mode") ~= "directory" then
196                         goto continue
197                 end
198
199                 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then
200                         goto continue
201                 end
202
203                 if unique[file] == nil then
204                         i = i + 1
205                         kernels[i] = file
206                         unique[file] = true
207                 end
208
209                 ::continue::
210         end
211         core.cached_kernels = kernels
212         return core.cached_kernels
213 end
214
215 function core.bootenvDefault()
216         return loader.getenv("zfs_be_active")
217 end
218
219 function core.bootenvList()
220         local bootenv_count = tonumber(loader.getenv("bootenvs_count"))
221         local bootenvs = {}
222         local curenv
223         local envcount = 0
224         local unique = {}
225
226         if bootenv_count == nil or bootenv_count <= 0 then
227                 return bootenvs
228         end
229
230         -- Currently selected bootenv is always first/default
231         curenv = core.bootenvDefault()
232         if curenv ~= nil then
233                 envcount = envcount + 1
234                 bootenvs[envcount] = curenv
235                 unique[curenv] = true
236         end
237
238         for curenv_idx = 0, bootenv_count - 1 do
239                 curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]")
240                 if curenv ~= nil and unique[curenv] == nil then
241                         envcount = envcount + 1
242                         bootenvs[envcount] = curenv
243                         unique[curenv] = true
244                 end
245         end
246         return bootenvs
247 end
248
249 function core.setDefaults()
250         core.setACPI(core.getACPIPresent(true))
251         core.setSafeMode(false)
252         core.setSingleUser(false)
253         core.setVerbose(false)
254 end
255
256 function core.autoboot(argstr)
257         -- loadelf() only if we've not already loaded a kernel
258         if loader.getenv("kernelname") == nil then
259                 config.loadelf()
260         end
261         loader.perform(composeLoaderCmd("autoboot", argstr))
262 end
263
264 function core.boot(argstr)
265         -- loadelf() only if we've not already loaded a kernel
266         if loader.getenv("kernelname") == nil then
267                 config.loadelf()
268         end
269         loader.perform(composeLoaderCmd("boot", argstr))
270 end
271
272 function core.isSingleUserBoot()
273         local single_user = loader.getenv("boot_single")
274         return single_user ~= nil and single_user:lower() == "yes"
275 end
276
277 function core.isUEFIBoot()
278         local efiver = loader.getenv("efi-version")
279
280         return efiver ~= nil
281 end
282
283 function core.isZFSBoot()
284         local c = loader.getenv("currdev")
285
286         if c ~= nil then
287                 return c:match("^zfs:") ~= nil
288         end
289         return false
290 end
291
292 function core.isSerialBoot()
293         local c = loader.getenv("console")
294
295         if c ~= nil then
296                 if c:find("comconsole") ~= nil then
297                         return true
298                 end
299         end
300
301         local s = loader.getenv("boot_serial")
302         if s ~= nil then
303                 return true
304         end
305
306         local m = loader.getenv("boot_multicons")
307         if m ~= nil then
308                 return true
309         end
310         return false
311 end
312
313 function core.isSystem386()
314         return loader.machine_arch == "i386"
315 end
316
317 -- Is the menu skipped in the environment in which we've booted?
318 function core.isMenuSkipped()
319         if core.isSerialBoot() then
320                 return true
321         end
322         local c = string.lower(loader.getenv("console") or "")
323         if c:match("^efi[ ;]") ~= nil or c:match("[ ;]efi[ ;]") ~= nil then
324                 return true
325         end
326
327         c = string.lower(loader.getenv("beastie_disable") or "")
328         return c == "yes"
329 end
330
331 -- This may be a better candidate for a 'utility' module.
332 function core.deepCopyTable(tbl)
333         local new_tbl = {}
334         for k, v in pairs(tbl) do
335                 if type(v) == "table" then
336                         new_tbl[k] = core.deepCopyTable(v)
337                 else
338                         new_tbl[k] = v
339                 end
340         end
341         return new_tbl
342 end
343
344 -- XXX This should go away if we get the table lib into shape for importing.
345 -- As of now, it requires some 'os' functions, so we'll implement this in lua
346 -- for our uses
347 function core.popFrontTable(tbl)
348         -- Shouldn't reasonably happen
349         if #tbl == 0 then
350                 return nil, nil
351         elseif #tbl == 1 then
352                 return tbl[1], {}
353         end
354
355         local first_value = tbl[1]
356         local new_tbl = {}
357         -- This is not a cheap operation
358         for k, v in ipairs(tbl) do
359                 if k > 1 then
360                         new_tbl[k - 1] = v
361                 end
362         end
363
364         return first_value, new_tbl
365 end
366
367 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
368 -- generally be set upon execution of the kernel. Because of this, we can't (or
369 -- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
370 -- enabled if we detect it and leave well enough alone if we don't.
371 if core.isSystem386() and core.getACPIPresent(false) then
372         core.setACPI(true)
373 end
374
375 hook.register("config.reloaded", core.clearCachedKernels)
376 return core