Changeset View
Changeset View
Standalone View
Standalone View
stand/lua/core.lua
| -- | -- | ||||
| -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD | -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||||
| -- | -- | ||||
| -- Copyright (c) 2023 The SmartixOS Project <ip@smartix.llc> | |||||
| -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> | -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> | ||||
| -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> | -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> | ||||
| -- All rights reserved. | -- All rights reserved. | ||||
| Context not available. | |||||
| local config = require("config") | local config = require("config") | ||||
| local hook = require("hook") | local hook = require("hook") | ||||
| local core = {} | -- sm (safe mode). A variable indicating whether safe mode is on or not. It is set below | ||||
| -- su (single user mode). A variable indicating whether single user mode is on or not. It is set below | |||||
| local core = {sm, su, acpi,} | |||||
| local default_safe_mode = false | local default_safe_mode = false | ||||
| local default_single_user = false | local default_single_user = false | ||||
| local default_verbose = false | local default_verbose = false | ||||
| local bootenv_list = "bootenvs" | local bootenv_list = "bootenvs" | ||||
| -- The list of directories expected to contain bootloader scripts. | |||||
| loader_search_path = { "/boot/lua/", "/boot/lua/logo/", "/boot/lua/brand/",} | |||||
| -- Module exports | |||||
| -- Commonly appearing constants | |||||
| core.KEY_BACKSPACE = 8 | |||||
| core.KEY_ENTER = 13 | |||||
| core.KEY_DELETE = 127 | |||||
| -- Note that this is a decimal representation, despite the leading 0 that in | |||||
| -- other contexts (outside of Lua) may mean 'octal' | |||||
| core.KEYSTR_ESCAPE = "\027" | |||||
| core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "[" | |||||
| core.KEYSTR_RESET = core.KEYSTR_ESCAPE .. "c" | |||||
| core.MENU_RETURN = "return" | |||||
| core.MENU_ENTRY = "entry" | |||||
| core.MENU_SEPARATOR = "separator" | |||||
| core.MENU_SUBMENU = "submenu" | |||||
| core.MENU_CAROUSEL_ENTRY = "carousel_entry" | |||||
| local function composeLoaderCmd(cmd_name, argstr) | local function composeLoaderCmd(cmd_name, argstr) | ||||
| if argstr ~= nil then | if argstr ~= nil then | ||||
| cmd_name = cmd_name .. " " .. argstr | cmd_name = cmd_name .. " " .. argstr | ||||
| Context not available. | |||||
| return dofile(module) | return dofile(module) | ||||
| end | end | ||||
| -- Module exports | |||||
| -- Commonly appearing constants | |||||
| core.KEY_BACKSPACE = 8 | |||||
| core.KEY_ENTER = 13 | |||||
| core.KEY_DELETE = 127 | |||||
| -- Note that this is a decimal representation, despite the leading 0 that in | -- A helper function used to check the attributes of the given file name. | ||||
| -- other contexts (outside of Lua) may mean 'octal' | -- This function is used in the boot procedure and its role is to take a file and go through | ||||
| core.KEYSTR_ESCAPE = "\027" | -- a fixed list of search directories in search for its existence. These directories normally contain bootloader scripts | ||||
| core.KEYSTR_CSI = core.KEYSTR_ESCAPE .. "[" | -- that partake in the display of the bootloader menu. | ||||
| core.KEYSTR_RESET = core.KEYSTR_ESCAPE .. "c" | |||||
| core.MENU_RETURN = "return" | -- Return values | ||||
| core.MENU_ENTRY = "entry" | -- This function returns two values | ||||
| core.MENU_SEPARATOR = "separator" | -- In the ideal case the function returns the full path of the given filename if the file exists | ||||
| core.MENU_SUBMENU = "submenu" | -- and a success message normally just 'success' as the second return value. | ||||
| core.MENU_CAROUSEL_ENTRY = "carousel_entry" | -- In the worse case scenario the function will always return nil as the first return value | ||||
| -- and a specific error message giving more details about the failure as the second return value. | |||||
| function check_file_attr (filename) | |||||
| local fullpath | |||||
| local array_size = #loader_search_path | |||||
| for i = 1, array_size do | |||||
| fullpath = loader_search_path[i] .. filename .. ".lua" | |||||
| -- get the file type of the given filename | |||||
| local attr, errorMsg = lfs.attributes (fullpath,"mode") | |||||
| -- the file in question has been found | |||||
| if(attr == "file") then | |||||
| return fullpath, "success" | |||||
| -- we did not find the file but we are not done going through the list of search path directories | |||||
| elseif (not attr and i < array_size) then | |||||
| goto continue | |||||
| -- we did not find the file and we are done going through the list of search path directories | |||||
| elseif (not attr and i == array_size) then | |||||
| return nil, errorMsg | |||||
| -- the inode in question is not a file but we are not done going through the list of search path directories | |||||
| elseif (attr ~= "file" and i < array_size) then | |||||
| goto continue | |||||
| -- the inode in question is not a file and we are done going through the list of search path directories | |||||
| elseif (attr ~= "file" and i == array_size) then | |||||
| return nil, errorMsg | |||||
| -- catch all clause | |||||
| else | |||||
| return nil, errorMsg | |||||
| end -- if | |||||
| ::continue:: | |||||
| end -- for | |||||
| end -- check_file_attr | |||||
| -- This helper function is used to read lua script files used during the boot process | |||||
| function include_file(file_name) | |||||
| local file, message = check_file_attr(file_name) | |||||
| if(file ~= nil) then | |||||
| return dofile(file) | |||||
| else | |||||
| return | |||||
| end | |||||
| end -- include_file | |||||
| function core.setVerbose(verbose) | function core.setVerbose(verbose) | ||||
| if verbose == nil then | if verbose == nil then | ||||
| Context not available. | |||||
| end | end | ||||
| function core.getACPIPresent(checking_system_defaults) | function core.getACPIPresent(checking_system_defaults) | ||||
| local c = loader.getenv("hint.acpi.0.rsdp") | local acpi_value = loader.getenv("hint.acpi.0.rsdp") | ||||
| if c ~= nil then | if acpi_value ~= nil then | ||||
| if checking_system_defaults then | if checking_system_defaults then | ||||
| return true | return true | ||||
| end | end | ||||
| -- Otherwise, respect disabled if it's set | -- Otherwise, respect disabled if it's set | ||||
| c = loader.getenv("hint.acpi.0.disabled") | acpi_value = loader.getenv("hint.acpi.0.disabled") | ||||
| return c == nil or tonumber(c) ~= 1 | return acpi_value == nil or tonumber(acpi_value) ~= 1 | ||||
| end | end | ||||
| return false | return false | ||||
| end | end | ||||
| function core.setACPI(acpi) | function core.setACPI(arg_acpi) | ||||
| if acpi == nil then | if arg_acpi == nil then | ||||
| acpi = not core.acpi | arg_acpi = not core.acpi | ||||
| end | end | ||||
| if acpi then | if arg_acpi then | ||||
| loader.setenv("acpi_load", "YES") | loader.setenv("acpi_load", "YES") | ||||
| loader.setenv("hint.acpi.0.disabled", "0") | loader.setenv("hint.acpi.0.disabled", "0") | ||||
| loader.unsetenv("loader.acpi_disabled_by_user") | loader.unsetenv("loader.acpi_disabled_by_user") | ||||
| Context not available. | |||||
| loader.setenv("hint.acpi.0.disabled", "1") | loader.setenv("hint.acpi.0.disabled", "1") | ||||
| loader.setenv("loader.acpi_disabled_by_user", "1") | loader.setenv("loader.acpi_disabled_by_user", "1") | ||||
| end | end | ||||
| core.acpi = acpi | core.acpi = arg_acpi | ||||
| end | end | ||||
| function core.setSafeMode(safe_mode) | function core.setSafeMode(safe_mode) | ||||
| if safe_mode == nil then | if safe_mode == nil then | ||||
| -- if the argument is null use the default | |||||
| safe_mode = not core.sm | safe_mode = not core.sm | ||||
| end | end | ||||
| if safe_mode then | if safe_mode then | ||||
| Context not available. | |||||
| return core.cached_kernels | return core.cached_kernels | ||||
| end | end | ||||
| local k = loader.getenv("kernel") | local kernel = loader.getenv("kernel") | ||||
| local v = loader.getenv("kernels") | local kernels_list = loader.getenv("kernels") | ||||
| local autodetect = loader.getenv("kernels_autodetect") or "" | local autodetect = loader.getenv("kernels_autodetect") or "" | ||||
| local kernels = {} | local kernels_table = {} | ||||
| local unique = {} | local unique = {} | ||||
| local i = 0 | local i = 0 | ||||
| if k ~= nil then | if kernel ~= nil then | ||||
| i = i + 1 | i = i + 1 | ||||
| kernels[i] = k | kernels_table[i] = kernel | ||||
| unique[k] = true | unique[kernel] = true | ||||
| end | end | ||||
| if v ~= nil then | if kernels_list ~= nil then | ||||
| for n in v:gmatch("([^;, ]+)[;, ]?") do | for n in kernels_list:gmatch("([^;, ]+)[;, ]?") do | ||||
| if unique[n] == nil then | if unique[n] == nil then | ||||
| i = i + 1 | i = i + 1 | ||||
| kernels[i] = n | kernels_table[i] = n | ||||
| unique[n] = true | unique[n] = true | ||||
| end | end | ||||
| end | end | ||||
| Context not available. | |||||
| -- setting, kernels_autodetect. If it's set to 'yes', we'll add | -- setting, kernels_autodetect. If it's set to 'yes', we'll add | ||||
| -- any kernels we detect based on the criteria described. | -- any kernels we detect based on the criteria described. | ||||
| if autodetect:lower() ~= "yes" then | if autodetect:lower() ~= "yes" then | ||||
| core.cached_kernels = kernels | core.cached_kernels = kernels_table | ||||
| return core.cached_kernels | return core.cached_kernels | ||||
| end | end | ||||
| Context not available. | |||||
| if unique[file] == nil then | if unique[file] == nil then | ||||
| i = i + 1 | i = i + 1 | ||||
| kernels[i] = file | kernels_table[i] = file | ||||
| unique[file] = true | unique[file] = true | ||||
| end | end | ||||
| ::continue:: | ::continue:: | ||||
| end | end | ||||
| core.cached_kernels = kernels | core.cached_kernels = kernels_table | ||||
| return core.cached_kernels | return core.cached_kernels | ||||
| end | end | ||||
| Context not available. | |||||
| end | end | ||||
| function core.isZFSBoot() | function core.isZFSBoot() | ||||
| local c = loader.getenv("currdev") | local current_dev = loader.getenv("currdev") | ||||
| if c ~= nil then | if current_dev ~= nil then | ||||
| return c:match("^zfs:") ~= nil | return current_dev:match("^zfs:") ~= nil | ||||
| end | end | ||||
| return false | return false | ||||
| end | end | ||||
| function core.isFramebufferConsole() | function core.isFramebufferConsole() | ||||
| local c = loader.getenv("console") | local cnsole = loader.getenv("console") | ||||
| if c ~= nil then | if cnsole ~= nil then | ||||
| if c:find("efi") == nil and c:find("vidconsole") == nil then | if cnsole:find("efi") == nil and cnsole:find("vidconsole") == nil then | ||||
| return false | return false | ||||
| end | end | ||||
| if loader.getenv("screen.depth") ~= nil then | if loader.getenv("screen.depth") ~= nil then | ||||
| Context not available. | |||||
| end | end | ||||
| function core.isSerialConsole() | function core.isSerialConsole() | ||||
| local c = loader.getenv("console") | local cnsole = loader.getenv("console") | ||||
| if c ~= nil then | if cnsole ~= nil then | ||||
| -- serial console is comconsole, but also userboot. | -- serial console is comconsole, but also userboot. | ||||
| -- userboot is there, because we have no way to know | -- userboot is there, because we have no way to know | ||||
| -- if the user terminal can draw unicode box chars or not. | -- if the user terminal can draw unicode box chars or not. | ||||
| if c:find("comconsole") ~= nil or c:find("userboot") ~= nil then | if cnsole:find("comconsole") ~= nil or cnsole:find("userboot") ~= nil then | ||||
| return true | return true | ||||
| end | end | ||||
| end | end | ||||
| Context not available. | |||||
| end | end | ||||
| function core.isSerialBoot() | function core.isSerialBoot() | ||||
| local s = loader.getenv("boot_serial") | local serialB = loader.getenv("boot_serial") | ||||
| if s ~= nil then | if serialB ~= nil then | ||||
| return true | return true | ||||
| end | end | ||||
| local m = loader.getenv("boot_multicons") | local multiB = loader.getenv("boot_multicons") | ||||
| if m ~= nil then | if multiB ~= nil then | ||||
| return true | return true | ||||
| end | end | ||||
| return false | return false | ||||
| Context not available. | |||||