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