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
36 local carousel_choices = {}
38 local pattern_table = {
41 process = function(_, _) end,
43 -- module_load="value"
45 str = "^%s*([%w_]+)_load%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
46 process = function(k, v)
47 if modules[k] == nil then
50 modules[k].load = v:upper()
53 -- module_name="value"
55 str = "^%s*([%w_]+)_name%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
56 process = function(k, v)
57 config.setKey(k, "name", v)
60 -- module_type="value"
62 str = "^%s*([%w_]+)_type%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
63 process = function(k, v)
64 config.setKey(k, "type", v)
67 -- module_flags="value"
69 str = "^%s*([%w_]+)_flags%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
70 process = function(k, v)
71 config.setKey(k, "flags", v)
74 -- module_before="value"
76 str = "^%s*([%w_]+)_before%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
77 process = function(k, v)
78 config.setKey(k, "before", v)
81 -- module_after="value"
83 str = "^%s*([%w_]+)_after%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
84 process = function(k, v)
85 config.setKey(k, "after", v)
88 -- module_error="value"
90 str = "^%s*([%w_]+)_error%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
91 process = function(k, v)
92 config.setKey(k, "error", v)
97 str = "^%s*exec%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
98 process = function(k, _)
99 if loader.perform(k) ~= 0 then
100 print("Failed to exec '" .. k .. "'")
106 str = "^%s*([%w%p]+)%s*=%s*\"([%w%s%p]-)\"%s*(.*)",
107 process = function(k, v)
108 if config.setenv(k, v) ~= 0 then
109 print("Failed to set '" .. k ..
110 "' with value: " .. v .. "")
116 str = "^%s*([%w%p]+)%s*=%s*(%d+)%s*(.*)",
117 process = function(k, v)
118 if config.setenv(k, v) ~= 0 then
119 print("Failed to set '" .. k ..
120 "' with value: " .. v .. "")
126 local function readFile(name, silent)
127 local f = io.open(name)
130 print("Failed to open config: '" .. name .. "'")
135 local text, _ = io.read(f)
136 -- We might have read in the whole file, this won't be needed any more.
141 print("Failed to read config: '" .. name .. "'")
148 local function checkNextboot()
149 local nextboot_file = loader.getenv("nextboot_file")
150 if nextboot_file == nil then
154 local text = readFile(nextboot_file, true)
159 if text:match("^nextboot_enable=\"NO\"") ~= nil then
160 -- We're done; nextboot is not enabled
164 if not config.parse(text) then
165 print("Failed to parse nextboot configuration: '" ..
166 nextboot_file .. "'")
169 -- Attempt to rewrite the first line and only the first line of the
170 -- nextboot_file. We overwrite it with nextboot_enable="NO", then
171 -- check for that on load.
172 -- It's worth noting that this won't work on every filesystem, so we
173 -- won't do anything notable if we have any errors in this process.
174 local nfile = io.open(nextboot_file, 'w')
176 -- We need the trailing space here to account for the extra
177 -- character taken up by the string nextboot_enable="YES"
178 -- Or new end quotation mark lands on the S, and we want to
179 -- rewrite the entirety of the first line.
180 io.write(nfile, "nextboot_enable=\"NO\" ")
186 -- Which variables we changed
187 config.env_changed = {}
188 -- Values to restore env to (nil to unset)
189 config.env_restore = {}
191 -- The first item in every carousel is always the default item.
192 function config.getCarouselIndex(id)
193 local val = carousel_choices[id]
200 function config.setCarouselIndex(id, idx)
201 carousel_choices[id] = idx
204 function config.restoreEnv()
205 -- Examine changed environment variables
206 for k, v in pairs(config.env_changed) do
207 local restore_value = config.env_restore[k]
208 if restore_value == nil then
209 -- This one doesn't need restored for some reason
212 local current_value = loader.getenv(k)
213 if current_value ~= v then
214 -- This was overwritten by some action taken on the menu
215 -- most likely; we'll leave it be.
218 restore_value = restore_value.value
219 if restore_value ~= nil then
220 loader.setenv(k, restore_value)
227 config.env_changed = {}
228 config.env_restore = {}
231 function config.setenv(key, value)
232 -- Track the original value for this if we haven't already
233 if config.env_restore[key] == nil then
234 config.env_restore[key] = {value = loader.getenv(key)}
237 config.env_changed[key] = value
239 return loader.setenv(key, value)
242 -- name here is one of 'name', 'type', flags', 'before', 'after', or 'error.'
243 -- These are set from lines in loader.conf(5): ${key}_${name}="${value}" where
244 -- ${key} is a module name.
245 function config.setKey(key, name, value)
246 if modules[key] == nil then
249 modules[key][name] = value
252 function config.isValidComment(line)
254 local s = line:match("^%s*#.*")
256 s = line:match("^%s*$")
265 function config.loadmod(mod, silent)
267 for k, v in pairs(mod) do
268 if v.load == "YES" then
270 if v.flags ~= nil then
271 str = str .. v.flags .. " "
273 if v.type ~= nil then
274 str = str .. "-t " .. v.type .. " "
276 if v.name ~= nil then
282 if v.before ~= nil then
283 if loader.perform(v.before) ~= 0 then
285 print("Failed to execute '" ..
287 "' before loading '" .. k ..
294 if loader.perform(str) ~= 0 then
296 print("Failed to execute '" .. str ..
299 if v.error ~= nil then
300 loader.perform(v.error)
305 if v.after ~= nil then
306 if loader.perform(v.after) ~= 0 then
308 print("Failed to execute '" ..
310 "' after loading '" .. k ..
318 -- if not silent then
319 -- print("Skipping module '". . k .. "'")
327 -- Returns true if we processed the file successfully, false if we did not.
328 -- If 'silent' is true, being unable to read the file is not considered a
330 function config.processFile(name, silent)
331 if silent == nil then
335 local text = readFile(name, silent)
340 return config.parse(text)
343 -- silent runs will not return false if we fail to open the file
344 function config.parse(text)
348 for line in text:gmatch("([^\n]+)") do
349 if line:match("^%s*$") == nil then
352 for _, val in ipairs(pattern_table) do
353 local k, v, c = line:match(val.str)
357 if config.isValidComment(c) then
360 print("Malformed line (" .. n ..
361 "):\n\t'" .. line .. "'")
370 print("Malformed line (" .. n .. "):\n\t'" ..
381 -- other_kernel is optionally the name of a kernel to load, if not the default
382 -- or autoloaded default from the module_path
383 function config.loadKernel(other_kernel)
384 local flags = loader.getenv("kernel_options") or ""
385 local kernel = other_kernel or loader.getenv("kernel")
387 local function tryLoad(names)
388 for name in names:gmatch("([^;]+)%s*;?") do
389 local r = loader.perform("load " .. flags ..
398 local function loadBootfile()
399 local bootfile = loader.getenv("bootfile")
401 -- append default kernel name
402 if bootfile == nil then
405 bootfile = bootfile .. ";kernel"
408 return tryLoad(bootfile)
411 -- kernel not set, try load from default module_path
412 if kernel == nil then
413 local res = loadBootfile()
416 -- Default kernel is loaded
417 config.kernel_loaded = nil
420 print("No kernel set, failed to load from module_path")
424 -- Use our cached module_path, so we don't end up with multiple
425 -- automatically added kernel paths to our final module_path
426 local module_path = config.module_path
429 if other_kernel ~= nil then
430 kernel = other_kernel
432 -- first try load kernel with module_path = /boot/${kernel}
433 -- then try load with module_path=${kernel}
434 local paths = {"/boot/" .. kernel, kernel}
436 for _, v in pairs(paths) do
437 loader.setenv("module_path", v)
440 -- succeeded, add path to module_path
442 config.kernel_loaded = kernel
443 if module_path ~= nil then
444 loader.setenv("module_path", v .. ";" ..
451 -- failed to load with ${kernel} as a directory
453 res = tryLoad(kernel)
455 config.kernel_loaded = kernel
458 print("Failed to load kernel '" .. kernel .. "'")
464 function config.selectKernel(kernel)
465 config.kernel_selected = kernel
468 function config.load(file)
470 file = "/boot/defaults/loader.conf"
473 if not config.processFile(file) then
474 print("Failed to parse configuration: '" .. file .. "'")
477 local f = loader.getenv("loader_conf_files")
479 for name in f:gmatch("([%w%p]+)%s*") do
480 -- These may or may not exist, and that's ok. Do a
481 -- silent parse so that we complain on parse errors but
482 -- not for them simply not existing.
483 if not config.processFile(name, true) then
484 print("Failed to parse configuration: '" ..
492 -- Cache the provided module_path at load time for later use
493 config.module_path = loader.getenv("module_path")
496 -- Reload configuration
497 function config.reload(file)
503 function config.loadelf()
504 local kernel = config.kernel_selected or config.kernel_loaded
507 print("Loading kernel...")
508 loaded = config.loadKernel(kernel)
511 print("Failed to load any kernel")
515 print("Loading configured modules...")
516 if not config.loadmod(modules) then
517 print("Could not load one or more modules!")