1-- 2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3-- 4-- Copyright (c) 2015 Pedro Souza <[email protected]> 5-- All rights reserved. 6-- 7-- Redistribution and use in source and binary forms, with or without 8-- modification, are permitted provided that the following conditions 9-- are met: 10-- 1. Redistributions of source code must retain the above copyright 11-- notice, this list of conditions and the following disclaimer. 12-- 2. Redistributions in binary form must reproduce the above copyright 13-- notice, this list of conditions and the following disclaimer in the 14-- documentation and/or other materials provided with the distribution. 15-- 16-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26-- SUCH DAMAGE. 27-- 28-- $FreeBSD$ 29-- 30 31local config = require("config") 32 33local core = {} 34 35local compose_loader_cmd = function(cmd_name, argstr) 36 if argstr ~= nil then 37 cmd_name = cmd_name .. " " .. argstr 38 end 39 return cmd_name 40end 41 42-- Module exports 43-- Commonly appearing constants 44core.KEY_BACKSPACE = 8 45core.KEY_ENTER = 13 46core.KEY_DELETE = 127 47 48core.KEYSTR_ESCAPE = "\027" 49 50core.MENU_RETURN = "return" 51core.MENU_ENTRY = "entry" 52core.MENU_SEPARATOR = "separator" 53core.MENU_SUBMENU = "submenu" 54core.MENU_CAROUSEL_ENTRY = "carousel_entry" 55 56function core.setVerbose(b) 57 if b == nil then 58 b = not core.verbose 59 end 60 61 if b then 62 loader.setenv("boot_verbose", "YES") 63 else 64 loader.unsetenv("boot_verbose") 65 end 66 core.verbose = b 67end 68 69function core.setSingleUser(b) 70 if b == nil then 71 b = not core.su 72 end 73 74 if b then 75 loader.setenv("boot_single", "YES") 76 else 77 loader.unsetenv("boot_single") 78 end 79 core.su = b 80end 81 82function core.getACPIPresent(checkingSystemDefaults) 83 local c = loader.getenv("hint.acpi.0.rsdp") 84 85 if c ~= nil then 86 if checkingSystemDefaults then 87 return true 88 end 89 -- Otherwise, respect disabled if it's set 90 c = loader.getenv("hint.acpi.0.disabled") 91 return c == nil or tonumber(c) ~= 1 92 end 93 return false 94end 95 96function core.setACPI(b) 97 if b == nil then 98 b = not core.acpi 99 end 100 101 if b then 102 loader.setenv("acpi_load", "YES") 103 loader.setenv("hint.acpi.0.disabled", "0") 104 loader.unsetenv("loader.acpi_disabled_by_user") 105 else 106 loader.unsetenv("acpi_load") 107 loader.setenv("hint.acpi.0.disabled", "1") 108 loader.setenv("loader.acpi_disabled_by_user", "1") 109 end 110 core.acpi = b 111end 112 113function core.setSafeMode(b) 114 if b == nil then 115 b = not core.sm 116 end 117 if b then 118 loader.setenv("kern.smp.disabled", "1") 119 loader.setenv("hw.ata.ata_dma", "0") 120 loader.setenv("hw.ata.atapi_dma", "0") 121 loader.setenv("hw.ata.wc", "0") 122 loader.setenv("hw.eisa_slots", "0") 123 loader.setenv("kern.eventtimer.periodic", "1") 124 loader.setenv("kern.geom.part.check_integrity", "0") 125 else 126 loader.unsetenv("kern.smp.disabled") 127 loader.unsetenv("hw.ata.ata_dma") 128 loader.unsetenv("hw.ata.atapi_dma") 129 loader.unsetenv("hw.ata.wc") 130 loader.unsetenv("hw.eisa_slots") 131 loader.unsetenv("kern.eventtimer.periodic") 132 loader.unsetenv("kern.geom.part.check_integrity") 133 end 134 core.sm = b 135end 136 137function core.kernelList() 138 local k = loader.getenv("kernel") 139 local v = loader.getenv("kernels") 140 local autodetect = loader.getenv("kernels_autodetect") or "" 141 142 local kernels = {} 143 local unique = {} 144 local i = 0 145 if k ~= nil then 146 i = i + 1 147 kernels[i] = k 148 unique[k] = true 149 end 150 151 if v ~= nil then 152 for n in v:gmatch("([^; ]+)[; ]?") do 153 if unique[n] == nil then 154 i = i + 1 155 kernels[i] = n 156 unique[n] = true 157 end 158 end 159 end 160 161 -- Base whether we autodetect kernels or not on a loader.conf(5) 162 -- setting, kernels_autodetect. If it's set to 'yes', we'll add 163 -- any kernels we detect based on the criteria described. 164 if autodetect:lower() ~= "yes" then 165 return kernels 166 end 167 168 -- Automatically detect other bootable kernel directories using a 169 -- heuristic. Any directory in /boot that contains an ordinary file 170 -- named "kernel" is considered eligible. 171 for file in lfs.dir("/boot") do 172 local fname = "/boot/" .. file 173 174 if file == "." or file == ".." then 175 goto continue 176 end 177 178 if lfs.attributes(fname, "mode") ~= "directory" then 179 goto continue 180 end 181 182 if lfs.attributes(fname .. "/kernel", "mode") ~= "file" then 183 goto continue 184 end 185 186 if unique[file] == nil then 187 i = i + 1 188 kernels[i] = file 189 unique[file] = true 190 end 191 192 ::continue:: 193 end 194 return kernels 195end 196 197function core.bootenvDefault() 198 return loader.getenv("zfs_be_active") 199end 200 201function core.bootenvList() 202 local bootenv_count = tonumber(loader.getenv("bootenvs_count")) 203 local bootenvs = {} 204 local curenv 205 local envcount = 0 206 local unique = {} 207 208 if bootenv_count == nil or bootenv_count <= 0 then 209 return bootenvs 210 end 211 212 -- Currently selected bootenv is always first/default 213 curenv = core.bootenvDefault() 214 if curenv ~= nil then 215 envcount = envcount + 1 216 bootenvs[envcount] = curenv 217 unique[curenv] = true 218 end 219 220 for curenv_idx = 0, bootenv_count - 1 do 221 curenv = loader.getenv("bootenvs[" .. curenv_idx .. "]") 222 if curenv ~= nil and unique[curenv] == nil then 223 envcount = envcount + 1 224 bootenvs[envcount] = curenv 225 unique[curenv] = true 226 end 227 end 228 return bootenvs 229end 230 231function core.setDefaults() 232 core.setACPI(core.getACPIPresent(true)) 233 core.setSafeMode(false) 234 core.setSingleUser(false) 235 core.setVerbose(false) 236end 237 238function core.autoboot(argstr) 239 config.loadelf() 240 loader.perform(compose_loader_cmd("autoboot", argstr)) 241end 242 243function core.boot(argstr) 244 config.loadelf() 245 loader.perform(compose_loader_cmd("boot", argstr)) 246end 247 248function core.isSingleUserBoot() 249 local single_user = loader.getenv("boot_single") 250 return single_user ~= nil and single_user:lower() == "yes" 251end 252 253function core.isZFSBoot() 254 local c = loader.getenv("currdev") 255 256 if c ~= nil then 257 return c:match("^zfs:") ~= nil 258 end 259 return false 260end 261 262function core.isSerialBoot() 263 local c = loader.getenv("console") 264 265 if c ~= nil then 266 if c:find("comconsole") ~= nil then 267 return true 268 end 269 end 270 271 local s = loader.getenv("boot_serial") 272 if s ~= nil then 273 return true 274 end 275 276 local m = loader.getenv("boot_multicons") 277 if m ~= nil then 278 return true 279 end 280 return false 281end 282 283function core.isSystem386() 284 return loader.machine_arch == "i386" 285end 286 287-- This may be a better candidate for a 'utility' module. 288function core.shallowCopyTable(tbl) 289 local new_tbl = {} 290 for k, v in pairs(tbl) do 291 if type(v) == "table" then 292 new_tbl[k] = core.shallowCopyTable(v) 293 else 294 new_tbl[k] = v 295 end 296 end 297 return new_tbl 298end 299 300-- XXX This should go away if we get the table lib into shape for importing. 301-- As of now, it requires some 'os' functions, so we'll implement this in lua 302-- for our uses 303function core.popFrontTable(tbl) 304 -- Shouldn't reasonably happen 305 if #tbl == 0 then 306 return nil, nil 307 elseif #tbl == 1 then 308 return tbl[1], {} 309 end 310 311 local first_value = tbl[1] 312 local new_tbl = {} 313 -- This is not a cheap operation 314 for k, v in ipairs(tbl) do 315 if k > 1 then 316 new_tbl[k - 1] = v 317 end 318 end 319 320 return first_value, new_tbl 321end 322 323-- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will 324-- generally be set upon execution of the kernel. Because of this, we can't (or 325-- don't really want to) detect/disable ACPI on !i386 reliably. Just set it 326-- enabled if we detect it and leave well enough alone if we don't. 327if core.isSystem386() and core.getACPIPresent(false) then 328 core.setACPI(true) 329end 330return core 331