1-- 2-- SPDX-License-Identifier: BSD-2-Clause 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 30local hook = require("hook") 31 32local config = {} 33local modules = {} 34local carousel_choices = {} 35-- Which variables we changed 36local env_changed = {} 37-- Values to restore env to (nil to unset) 38local env_restore = {} 39 40local MSG_FAILDIR = "Failed to load conf dir '%s': not a directory" 41local MSG_FAILEXEC = "Failed to exec '%s'" 42local MSG_FAILSETENV = "Failed to '%s' with value: %s" 43local MSG_FAILOPENCFG = "Failed to open config: '%s'" 44local MSG_FAILREADCFG = "Failed to read config: '%s'" 45local MSG_FAILPARSECFG = "Failed to parse config: '%s'" 46local MSG_FAILEXECLUA = "Failed to execute lua conf '%s': '%s'" 47local MSG_FAILPARSEVAR = "Failed to parse variable '%s': %s" 48local MSG_FAILEXBEF = "Failed to execute '%s' before loading '%s'" 49local MSG_FAILEXAF = "Failed to execute '%s' after loading '%s'" 50local MSG_MALFORMED = "Malformed line (%d):\n\t'%s'" 51local MSG_DEFAULTKERNFAIL = "No kernel set, failed to load from module_path" 52local MSG_KERNFAIL = "Failed to load kernel '%s'" 53local MSG_XENKERNFAIL = "Failed to load Xen kernel '%s'" 54local MSG_XENKERNLOADING = "Loading Xen kernel..." 55local MSG_KERNLOADING = "Loading kernel..." 56local MSG_MODLOADING = "Loading configured modules..." 57local MSG_MODBLACKLIST = "Not loading blacklisted module '%s'" 58 59local MSG_FAILSYN_QUOTE = "Stray quote at position '%d'" 60local MSG_FAILSYN_EOLESC = "Stray escape at end of line" 61local MSG_FAILSYN_EOLVAR = "Unescaped $ at end of line" 62local MSG_FAILSYN_BADVAR = "Malformed variable expression at position '%d'" 63 64-- MODULEEXPR should more or less allow the exact same set of characters as the 65-- env_var entries in the pattern table. This is perhaps a good target for a 66-- little refactoring. 67local MODULEEXPR = '([%w%d-_.]+)' 68local QVALEXPR = '"(.*)"' 69local QVALREPL = QVALEXPR:gsub('%%', '%%%%') 70local WORDEXPR = "([-%w%d][-%w%d_.]*)" 71local WORDREPL = WORDEXPR:gsub('%%', '%%%%') 72 73-- Entries that should never make it into the environment; each one should have 74-- a documented reason for its existence, and these should all be implementation 75-- details of the config module. 76local loader_env_restricted_table = { 77 -- loader_conf_files should be considered write-only, and consumers 78 -- should not rely on any particular value; it's a loader implementation 79 -- detail. Moreover, it's not a particularly useful variable to have in 80 -- the kenv. Save the overhead, let it get fetched other ways. 81 loader_conf_files = true, 82} 83 84local function restoreEnv() 85 -- Examine changed environment variables 86 for k, v in pairs(env_changed) do 87 local restore_value = env_restore[k] 88 if restore_value == nil then 89 -- This one doesn't need restored for some reason 90 goto continue 91 end 92 local current_value = loader.getenv(k) 93 if current_value ~= v then 94 -- This was overwritten by some action taken on the menu 95 -- most likely; we'll leave it be. 96 goto continue 97 end 98 restore_value = restore_value.value 99 if restore_value ~= nil then 100 loader.setenv(k, restore_value) 101 else 102 loader.unsetenv(k) 103 end 104 ::continue:: 105 end 106 107 env_changed = {} 108 env_restore = {} 109end 110 111-- XXX This getEnv/setEnv should likely be exported at some point. We can save 112-- the call back into loader.getenv for any variable that's been set or 113-- overridden by any loader.conf using this implementation with little overhead 114-- since we're already tracking the values. 115local function getEnv(key) 116 if loader_env_restricted_table[key] ~= nil or 117 env_changed[key] ~= nil then 118 return env_changed[key] 119 end 120 121 return loader.getenv(key) 122end 123 124local function setEnv(key, value) 125 env_changed[key] = value 126 127 if loader_env_restricted_table[key] ~= nil then 128 return 0 129 end 130 131 -- Track the original value for this if we haven't already 132 if env_restore[key] == nil then 133 env_restore[key] = {value = loader.getenv(key)} 134 end 135 136 return loader.setenv(key, value) 137end 138 139-- name here is one of 'name', 'type', flags', 'before', 'after', or 'error.' 140-- These are set from lines in loader.conf(5): ${key}_${name}="${value}" where 141-- ${key} is a module name. 142local function setKey(key, name, value) 143 if modules[key] == nil then 144 modules[key] = {} 145 end 146 modules[key][name] = value 147end 148 149-- Escapes the named value for use as a literal in a replacement pattern. 150-- e.g. dhcp.host-name gets turned into dhcp%.host%-name to remove the special 151-- meaning. 152local function escapeName(name) 153 return name:gsub("([%p])", "%%%1") 154end 155 156local function processEnvVar(value) 157 local pval, vlen = '', #value 158 local nextpos, vdelim, vinit = 1 159 local vpat 160 for i = 1, vlen do 161 if i < nextpos then 162 goto nextc 163 end 164 165 local c = value:sub(i, i) 166 if c == '\\' then 167 if i == vlen then 168 return nil, MSG_FAILSYN_EOLESC 169 end 170 nextpos = i + 2 171 pval = pval .. value:sub(i + 1, i + 1) 172 elseif c == '"' then 173 return nil, MSG_FAILSYN_QUOTE:format(i) 174 elseif c == "$" then 175 if i == vlen then 176 return nil, MSG_FAILSYN_EOLVAR 177 else 178 if value:sub(i + 1, i + 1) == "{" then 179 -- Skip ${ 180 vinit = i + 2 181 vdelim = '}' 182 vpat = "^([^}]+)}" 183 else 184 -- Skip the $ 185 vinit = i + 1 186 vdelim = nil 187 vpat = "^([%w][-%w%d_.]*)" 188 end 189 190 local name = value:match(vpat, vinit) 191 if not name then 192 return nil, MSG_FAILSYN_BADVAR:format(i) 193 else 194 nextpos = vinit + #name 195 if vdelim then 196 nextpos = nextpos + 1 197 end 198 199 local repl = loader.getenv(name) or "" 200 pval = pval .. repl 201 end 202 end 203 else 204 pval = pval .. c 205 end 206 ::nextc:: 207 end 208 209 return pval 210end 211 212local function checkPattern(line, pattern) 213 local function _realCheck(_line, _pattern) 214 return _line:match(_pattern) 215 end 216 217 if pattern:find('$VALUE') then 218 local k, v, c 219 k, v, c = _realCheck(line, pattern:gsub('$VALUE', QVALREPL)) 220 if k ~= nil then 221 return k,v, c 222 end 223 return _realCheck(line, pattern:gsub('$VALUE', WORDREPL)) 224 else 225 return _realCheck(line, pattern) 226 end 227end 228 229-- str in this table is a regex pattern. It will automatically be anchored to 230-- the beginning of a line and any preceding whitespace will be skipped. The 231-- pattern should have no more than two captures patterns, which correspond to 232-- the two parameters (usually 'key' and 'value') that are passed to the 233-- process function. All trailing characters will be validated. Any $VALUE 234-- token included in a pattern will be tried first with a quoted value capture 235-- group, then a single-word value capture group. This is our kludge for Lua 236-- regex not supporting branching. 237-- 238-- We have two special entries in this table: the first is the first entry, 239-- a full-line comment. The second is for 'exec' handling. Both have a single 240-- capture group, but the difference is that the full-line comment pattern will 241-- match the entire line. This does not run afoul of the later end of line 242-- validation that we'll do after a match. However, the 'exec' pattern will. 243-- We document the exceptions with a special 'groups' index that indicates 244-- the number of capture groups, if not two. We'll use this later to do 245-- validation on the proper entry. 246-- 247local pattern_table = { 248 { 249 luaexempt = true, 250 str = "(#.*)", 251 process = function(_, _) end, 252 groups = 1, 253 }, 254 -- module_load="value" 255 { 256 name = MODULEEXPR .. "_load%s*", 257 val = "%s*$VALUE", 258 process = function(k, v) 259 if modules[k] == nil then 260 modules[k] = {} 261 end 262 modules[k].load = v:upper() 263 setEnv(k .. "_load", v:upper()) 264 end, 265 }, 266 -- module_name="value" 267 { 268 name = MODULEEXPR .. "_name%s*", 269 val = "%s*$VALUE", 270 process = function(k, v) 271 setKey(k, "name", v) 272 setEnv(k .. "_name", v) 273 end, 274 }, 275 -- module_type="value" 276 { 277 name = MODULEEXPR .. "_type%s*", 278 val = "%s*$VALUE", 279 process = function(k, v) 280 setKey(k, "type", v) 281 setEnv(k .. "_type", v) 282 end, 283 }, 284 -- module_flags="value" 285 { 286 name = MODULEEXPR .. "_flags%s*", 287 val = "%s*$VALUE", 288 process = function(k, v) 289 setKey(k, "flags", v) 290 setEnv(k .. "_flags", v) 291 end, 292 }, 293 -- module_before="value" 294 { 295 name = MODULEEXPR .. "_before%s*", 296 val = "%s*$VALUE", 297 process = function(k, v) 298 setKey(k, "before", v) 299 setEnv(k .. "_before", v) 300 end, 301 }, 302 -- module_after="value" 303 { 304 name = MODULEEXPR .. "_after%s*", 305 val = "%s*$VALUE", 306 process = function(k, v) 307 setKey(k, "after", v) 308 setEnv(k .. "_after", v) 309 end, 310 }, 311 -- module_error="value" 312 { 313 name = MODULEEXPR .. "_error%s*", 314 val = "%s*$VALUE", 315 process = function(k, v) 316 setKey(k, "error", v) 317 setEnv(k .. "_error", v) 318 end, 319 }, 320 -- exec="command" 321 { 322 luaexempt = true, 323 name = "exec%s*", 324 val = "%s*" .. QVALEXPR, 325 process = function(k, _) 326 if cli_execute_unparsed(k) ~= 0 then 327 print(MSG_FAILEXEC:format(k)) 328 end 329 end, 330 groups = 1, 331 }, 332 -- env_var="value" or env_var=[word|num] 333 { 334 name = "([%w][%w%d-_.]*)%s*", 335 val = "%s*$VALUE", 336 process = function(k, v) 337 local pv, msg = processEnvVar(v) 338 if not pv then 339 print(MSG_FAILPARSEVAR:format(k, msg)) 340 return 341 end 342 if setEnv(k, pv) ~= 0 then 343 print(MSG_FAILSETENV:format(k, v)) 344 else 345 return pv 346 end 347 end, 348 }, 349} 350 351local function isValidComment(line) 352 if line ~= nil then 353 local s = line:match("^%s*#.*") 354 if s == nil then 355 s = line:match("^%s*$") 356 end 357 if s == nil then 358 return false 359 end 360 end 361 return true 362end 363 364local function getBlacklist() 365 local blacklist = {} 366 local blacklist_str = loader.getenv('module_blacklist') 367 if blacklist_str == nil then 368 return blacklist 369 end 370 371 for mod in blacklist_str:gmatch("[;, ]?([-%w_]+)[;, ]?") do 372 blacklist[mod] = true 373 end 374 return blacklist 375end 376 377local function loadModule(mod, silent) 378 local status = true 379 local blacklist = getBlacklist() 380 local pstatus 381 for k, v in pairs(mod) do 382 if v.load ~= nil and v.load:lower() == "yes" then 383 local module_name = v.name or k 384 if not v.force and blacklist[module_name] ~= nil then 385 if not silent then 386 print(MSG_MODBLACKLIST:format(module_name)) 387 end 388 goto continue 389 end 390 if not silent then 391 loader.printc(module_name .. "...") 392 end 393 local str = "load " 394 if v.type ~= nil then 395 str = str .. "-t " .. v.type .. " " 396 end 397 str = str .. module_name 398 if v.flags ~= nil then 399 str = str .. " " .. v.flags 400 end 401 if v.before ~= nil then 402 pstatus = cli_execute_unparsed(v.before) == 0 403 if not pstatus and not silent then 404 print(MSG_FAILEXBEF:format(v.before, k)) 405 end 406 status = status and pstatus 407 end 408 409 if cli_execute_unparsed(str) ~= 0 then 410 -- XXX Temporary shim: don't break the boot if 411 -- loader hadn't been recompiled with this 412 -- function exposed. 413 if loader.command_error then 414 print(loader.command_error()) 415 end 416 if not silent then 417 print("failed!") 418 end 419 if v.error ~= nil then 420 cli_execute_unparsed(v.error) 421 end 422 status = false 423 elseif v.after ~= nil then 424 pstatus = cli_execute_unparsed(v.after) == 0 425 if not pstatus and not silent then 426 print(MSG_FAILEXAF:format(v.after, k)) 427 end 428 if not silent then 429 print("ok") 430 end 431 status = status and pstatus 432 end 433 end 434 ::continue:: 435 end 436 437 return status 438end 439 440local function readFile(name, silent) 441 local f = io.open(name) 442 if f == nil then 443 if not silent then 444 print(MSG_FAILOPENCFG:format(name)) 445 end 446 return nil 447 end 448 449 local text, _ = io.read(f) 450 -- We might have read in the whole file, this won't be needed any more. 451 io.close(f) 452 453 if text == nil and not silent then 454 print(MSG_FAILREADCFG:format(name)) 455 end 456 return text 457end 458 459local function checkNextboot() 460 local nextboot_file = loader.getenv("nextboot_conf") 461 local nextboot_enable = loader.getenv("nextboot_enable") 462 463 if nextboot_file == nil then 464 return 465 end 466 467 -- is nextboot_enable set in nvstore? 468 if nextboot_enable == "NO" then 469 return 470 end 471 472 local text = readFile(nextboot_file, true) 473 if text == nil then 474 return 475 end 476 477 if nextboot_enable == nil and 478 text:match("^nextboot_enable=\"NO\"") ~= nil then 479 -- We're done; nextboot is not enabled 480 return 481 end 482 483 if not config.parse(text) then 484 print(MSG_FAILPARSECFG:format(nextboot_file)) 485 end 486 487 -- Attempt to rewrite the first line and only the first line of the 488 -- nextboot_file. We overwrite it with nextboot_enable="NO", then 489 -- check for that on load. 490 -- It's worth noting that this won't work on every filesystem, so we 491 -- won't do anything notable if we have any errors in this process. 492 local nfile = io.open(nextboot_file, 'w') 493 if nfile ~= nil then 494 -- We need the trailing space here to account for the extra 495 -- character taken up by the string nextboot_enable="YES" 496 -- Or new end quotation mark lands on the S, and we want to 497 -- rewrite the entirety of the first line. 498 io.write(nfile, "nextboot_enable=\"NO\" ") 499 io.close(nfile) 500 end 501 loader.setenv("nextboot_enable", "NO") 502end 503 504local function processEnv(k, v) 505 for _, val in ipairs(pattern_table) do 506 if not val.luaexempt and val.name then 507 local matched = k:match(val.name) 508 509 if matched then 510 return val.process(matched, v) 511 end 512 end 513 end 514end 515 516-- Module exports 517config.verbose = false 518 519-- The first item in every carousel is always the default item. 520function config.getCarouselIndex(id) 521 return carousel_choices[id] or 1 522end 523 524function config.setCarouselIndex(id, idx) 525 carousel_choices[id] = idx 526end 527 528-- Returns true if we processed the file successfully, false if we did not. 529-- If 'silent' is true, being unable to read the file is not considered a 530-- failure. 531function config.processFile(name, silent) 532 if silent == nil then 533 silent = false 534 end 535 536 local text = readFile(name, silent) 537 if text == nil then 538 return silent 539 end 540 541 if name:match(".lua$") then 542 local cfg_env = setmetatable({}, { 543 indices = {}, 544 __index = function(env, key) 545 if getmetatable(env).indices[key] then 546 return rawget(env, key) 547 end 548 549 return loader.getenv(key) 550 end, 551 __newindex = function(env, key, val) 552 getmetatable(env).indices[key] = true 553 rawset(env, key, val) 554 end, 555 }) 556 557 -- Give local modules a chance to populate the config 558 -- environment. 559 hook.runAll("config.buildenv", cfg_env) 560 local res, err = pcall(load(text, name, "t", cfg_env)) 561 if res then 562 for k, v in pairs(cfg_env) do 563 local t = type(v) 564 if t ~= "function" and t ~= "table" then 565 if t ~= "string" then 566 v = tostring(v) 567 end 568 local pval = processEnv(k, v) 569 if pval then 570 setEnv(k, pval) 571 end 572 end 573 end 574 else 575 print(MSG_FAILEXECLUA:format(name, err)) 576 end 577 578 return res 579 else 580 return config.parse(text) 581 end 582end 583 584-- silent runs will not return false if we fail to open the file 585function config.parse(text) 586 local n = 1 587 local status = true 588 589 for line in text:gmatch("([^\n]+)") do 590 if line:match("^%s*$") == nil then 591 for _, val in ipairs(pattern_table) do 592 if val.str == nil then 593 val.str = val.name .. "=" .. val.val 594 end 595 local pattern = '^%s*' .. val.str .. '%s*(.*)'; 596 local cgroups = val.groups or 2 597 local k, v, c = checkPattern(line, pattern) 598 if k ~= nil then 599 -- Offset by one, drats 600 if cgroups == 1 then 601 c = v 602 v = nil 603 end 604 605 if isValidComment(c) then 606 val.process(k, v) 607 goto nextline 608 end 609 610 break 611 end 612 end 613 614 print(MSG_MALFORMED:format(n, line)) 615 status = false 616 end 617 ::nextline:: 618 n = n + 1 619 end 620 621 return status 622end 623 624function config.readConf(file, loaded_files) 625 if loaded_files == nil then 626 loaded_files = {} 627 end 628 629 if loaded_files[file] ~= nil then 630 return 631 end 632 633 local top_level = next(loaded_files) == nil -- Are we the top-level readConf? 634 print("Loading " .. file) 635 636 -- The final value of loader_conf_files is not important, so just 637 -- clobber it here. We'll later check if it's no longer nil and process 638 -- the new value for files to read. 639 setEnv("loader_conf_files", nil) 640 641 -- These may or may not exist, and that's ok. Do a 642 -- silent parse so that we complain on parse errors but 643 -- not for them simply not existing. 644 if not config.processFile(file, true) then 645 print(MSG_FAILPARSECFG:format(file)) 646 end 647 648 loaded_files[file] = true 649 650 -- Going to process "loader_conf_files" extra-files 651 local loader_conf_files = getEnv("loader_conf_files") 652 if loader_conf_files ~= nil then 653 for name in loader_conf_files:gmatch("[%w%p]+") do 654 config.readConf(name, loaded_files) 655 end 656 end 657 658 if top_level then 659 local loader_conf_dirs = getEnv("loader_conf_dirs") 660 661 -- If product_vars is set, it must be a list of environment variable names 662 -- to walk through to guess product information. The order matters as 663 -- reading a config files override the previously defined values. 664 -- 665 -- If product information can be guessed, for each product information 666 -- found, also read config files found in /boot/loader.conf.d/PRODUCT/. 667 local product_vars = getEnv("product_vars") 668 if product_vars then 669 local product_conf_dirs = "" 670 for var in product_vars:gmatch("%S+") do 671 local product = getEnv(var) 672 if product then 673 product_conf_dirs = product_conf_dirs .. " /boot/loader.conf.d/" .. product 674 end 675 end 676 677 if loader_conf_dirs then 678 loader_conf_dirs = loader_conf_dirs .. product_conf_dirs 679 else 680 loader_conf_dirs = product_conf_dirs 681 end 682 end 683 684 -- Process "loader_conf_dirs" extra-directories 685 if loader_conf_dirs ~= nil then 686 for name in loader_conf_dirs:gmatch("[%w%p]+") do 687 if lfs.attributes(name, "mode") ~= "directory" then 688 print(MSG_FAILDIR:format(name)) 689 goto nextdir 690 end 691 692 for cfile in lfs.dir(name) do 693 if cfile:match(".conf$") then 694 local fpath = name .. "/" .. cfile 695 if lfs.attributes(fpath, "mode") == "file" then 696 config.readConf(fpath, loaded_files) 697 end 698 end 699 end 700 ::nextdir:: 701 end 702 end 703 704 -- Always allow overriding with local config files, e.g., 705 -- /boot/loader.conf.local. 706 local local_loader_conf_files = getEnv("local_loader_conf_files") 707 if local_loader_conf_files then 708 for name in local_loader_conf_files:gmatch("[%w%p]+") do 709 config.readConf(name, loaded_files) 710 end 711 end 712 end 713end 714 715-- other_kernel is optionally the name of a kernel to load, if not the default 716-- or autoloaded default from the module_path 717function config.loadKernel(other_kernel) 718 local flags = loader.getenv("kernel_options") or "" 719 local kernel = other_kernel or loader.getenv("kernel") 720 721 local function tryLoad(names) 722 for name in names:gmatch("([^;]+)%s*;?") do 723 local r = loader.perform("load " .. name .. 724 " " .. flags) 725 if r == 0 then 726 return name 727 end 728 end 729 return nil 730 end 731 732 local function getModulePath() 733 local module_path = loader.getenv("module_path") 734 local kernel_path = loader.getenv("kernel_path") 735 736 if kernel_path == nil then 737 return module_path 738 end 739 740 -- Strip the loaded kernel path from module_path. This currently assumes 741 -- that the kernel path will be prepended to the module_path when it's 742 -- found. 743 kernel_path = escapeName(kernel_path .. ';') 744 return module_path:gsub(kernel_path, '') 745 end 746 747 local function loadBootfile() 748 local bootfile = loader.getenv("bootfile") 749 750 -- append default kernel name 751 if bootfile == nil then 752 bootfile = "kernel" 753 else 754 bootfile = bootfile .. ";kernel" 755 end 756 757 return tryLoad(bootfile) 758 end 759 760 -- kernel not set, try load from default module_path 761 if kernel == nil then 762 local res = loadBootfile() 763 764 if res ~= nil then 765 -- Default kernel is loaded 766 config.kernel_loaded = nil 767 return true 768 else 769 print(MSG_DEFAULTKERNFAIL) 770 return false 771 end 772 else 773 -- Use our cached module_path, so we don't end up with multiple 774 -- automatically added kernel paths to our final module_path 775 local module_path = getModulePath() 776 local res 777 778 if other_kernel ~= nil then 779 kernel = other_kernel 780 end 781 -- first try load kernel with module_path = /boot/${kernel} 782 -- then try load with module_path=${kernel} 783 local paths = {"/boot/" .. kernel, kernel} 784 785 for _, v in pairs(paths) do 786 loader.setenv("module_path", v) 787 res = loadBootfile() 788 789 -- succeeded, add path to module_path 790 if res ~= nil then 791 config.kernel_loaded = kernel 792 if module_path ~= nil then 793 loader.setenv("module_path", v .. ";" .. 794 module_path) 795 loader.setenv("kernel_path", v) 796 end 797 return true 798 end 799 end 800 801 -- failed to load with ${kernel} as a directory 802 -- try as a file 803 res = tryLoad(kernel) 804 if res ~= nil then 805 config.kernel_loaded = kernel 806 return true 807 else 808 print(MSG_KERNFAIL:format(kernel)) 809 return false 810 end 811 end 812end 813 814function config.selectKernel(kernel) 815 config.kernel_selected = kernel 816end 817 818function config.load(file, reloading) 819 if not file then 820 file = "/boot/defaults/loader.conf" 821 end 822 823 config.readConf(file) 824 825 checkNextboot() 826 827 local verbose = loader.getenv("verbose_loading") or "no" 828 config.verbose = verbose:lower() == "yes" 829 if not reloading then 830 hook.runAll("config.loaded") 831 end 832end 833 834-- Reload configuration 835function config.reload(file) 836 modules = {} 837 restoreEnv() 838 config.load(file, true) 839 hook.runAll("config.reloaded") 840end 841 842function config.loadelf() 843 local xen_kernel = loader.getenv('xen_kernel') 844 local kernel = config.kernel_selected or config.kernel_loaded 845 local status 846 847 if xen_kernel ~= nil then 848 print(MSG_XENKERNLOADING) 849 if cli_execute_unparsed('load ' .. xen_kernel) ~= 0 then 850 print(MSG_XENKERNFAIL:format(xen_kernel)) 851 return false 852 end 853 end 854 print(MSG_KERNLOADING) 855 if not config.loadKernel(kernel) then 856 return false 857 end 858 hook.runAll("kernel.loaded") 859 860 print(MSG_MODLOADING) 861 status = loadModule(modules, not config.verbose) 862 hook.runAll("modules.loaded") 863 return status 864end 865 866function config.enableModule(modname) 867 if modules[modname] == nil then 868 modules[modname] = {} 869 elseif modules[modname].load == "YES" then 870 modules[modname].force = true 871 return true 872 end 873 874 modules[modname].load = "YES" 875 modules[modname].force = true 876 return true 877end 878 879function config.disableModule(modname) 880 if modules[modname] == nil then 881 return false 882 elseif modules[modname].load ~= "YES" then 883 return true 884 end 885 886 modules[modname].load = "NO" 887 modules[modname].force = nil 888 return true 889end 890 891function config.isModuleEnabled(modname) 892 local mod = modules[modname] 893 if not mod or mod.load ~= "YES" then 894 return false 895 end 896 897 if mod.force then 898 return true 899 end 900 901 local blacklist = getBlacklist() 902 return not blacklist[modname] 903end 904 905function config.getModuleInfo() 906 return { 907 modules = modules, 908 blacklist = getBlacklist() 909 } 910end 911 912hook.registerType("config.buildenv") 913hook.registerType("config.loaded") 914hook.registerType("config.reloaded") 915hook.registerType("kernel.loaded") 916hook.registerType("modules.loaded") 917return config 918