History log of /freebsd-current/stand/lua/config.lua
Revision Date Author Comments
# 552f3072 29-Feb-2024 Warner Losh <imp@FreeBSD.org>

loader/lua: Remove workaround for command_error

loader.command_error was available prior to stable/12 branching. No need
to check if it is available or not.

Sponsored by: Netflix
Reviewed by: kevans
Differential Revision: https://reviews.freebsd.org/D44144


# c475e61f 21-Dec-2023 Stéphane Rochoy <stephane.rochoy@stormshield.eu>

stand/lua: always allow overriding with local config files

Loader now also read configuration files listed in local_loader_conf_files.
Files listed here are the last ones read. And /boot/loader.conf.local was
moved from loader_conf_files to local_loader_conf_files leaving only
loader.conf and device.hints in loader_conf_files by default.

The idea is to ensure local_loader_conf_files, i.e., /boot/loader.conf.local,
can always be used to override other user defined settings.

So the sequencing is now as follow:

1. Bootstrap:
/boot/defaults/loader.conf

2. Read loader_conf_files files:
/boot/device.hints
/boot/loader.conf

3. Read loader_conf_dirs files:
/boot/loader.conf.d/*.conf

4. And finally, rread local_loader_conf_files files:
/boot/loader.conf.local

Reviewed by: imp, kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/759


# c343eedc 04-May-2023 Stéphane Rochoy <stephane.rochoy@stormshield.eu>

stand/lua: per-product conf if requested via product_vars

If product_vars is set, it must be a space separated list of environment
variable names to walk through to guess the product. Each time a product can be
guessed (i.e., the corresponding variable is defined), prepend
/boot/loader.conf.d/PRODUCT/ to loader_conf_dirs.

It can be typically used as follow:

smbios.system.planar.maker="PLANAR_MAKER"
smbios.system.planar.product="PLANAR_PRODUCT"
smbios.system.product="PRODUCT"
uboot.m_product="M_PRODUCT"
product_vars="smbios.system.planar.maker smbios.system.planar.product smbios.system.product uboot.m_product"

to read files found in the following directories, in that order:

/boot/loader.conf.d/PLANAR_MAKER
/boot/loader.conf.d/PLANAR_PRODUCT
/boot/loader.conf.d/PRODUCT
/boot/loader.conf.d/M_PRODUCT

Reviewed by: imp, kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/759


# 994865ca 02-Feb-2024 Warner Losh <imp@FreeBSD.org>

Revert "stand/lua: per-product conf if requested via product_vars"

This reverts commit 754cac4b283eb024a3a6a194130199c860e32ebf. No mail
went out and commit message was wrong.

Sponsored by: Netflix


# 5fdf01db 02-Feb-2024 Warner Losh <imp@FreeBSD.org>

Revert "stand/lua: always allow overriding with local config files"

This reverts commit d3d0b735571d9562812ce5b343a6e91f7a795dbe. no mail
sent out, and the commit message was wrong.

Sponsored by: Netflix


# d3d0b735 21-Dec-2023 Stéphane Rochoy <stephane.rochoy@stormshield.eu>

stand/lua: always allow overriding with local config files

Loader now also read configuration files listed in local_loader_conf_files.
Files listed here are the last ones read. And /boot/loader.conf.local was
moved from loader_conf_files to local_loader_conf_files leaving only
loader.conf and device.hints in loader_conf_files by default.

The idea is to ensure local_loader_conf_files, i.e., /boot/loader.conf.local,
can always be used to override other user defined settings.

So the sequencing is now as follow:

1. Bootstrap:
/boot/defaults/loader.conf

2. Read loader_conf_files files:
/boot/device.hints
/boot/loader.conf

3. Read loader_conf_dirs files:
/boot/loader.conf.d/*.conf

4. And finally, rread local_loader_conf_files files:
/boot/loader.conf.local


# 754cac4b 04-May-2023 Stéphane Rochoy <stephane.rochoy@stormshield.eu>

stand/lua: per-product conf if requested via product_vars

If product_vars is set, it must be a space separated list of environment
variable names to walk through to guess the product. Each time a product can be
guessed (i.e., the corresponding variable is defined), prepend
/boot/loader.conf.d/PRODUCT/ to loader_conf_dirs.

It can be typically used as follow:

smbios.system.planar.maker="PLANAR_MAKER"
smbios.system.planar.product="PLANAR_PRODUCT"
smbios.system.product="PRODUCT"
uboot.m_product="M_PRODUCT"
product_vars="smbios.system.planar.maker smbios.system.planar.product smbios.system.product uboot.m_product"

to read files found in the following directories, in that order:

/boot/loader.conf.d/PLANAR_MAKER
/boot/loader.conf.d/PLANAR_PRODUCT
/boot/loader.conf.d/PRODUCT
/boot/loader.conf.d/M_PRODUCT


# 5bc1e0c2 04-Sep-2023 Kyle Evans <kevans@FreeBSD.org>

stand: lua: module names should permit more

The module entries should generally allow whatever is allowed as an
env_var in the pattern table. Notably, we're missing periods which
would allow proper entries for .dtb files in loader.conf that don't need
to specify a module_name entry for it.

%d in this expression is actually redundant as %w is actually
"all alphanumerics," but I've included it for now to match the env_var
entry. We should really remove it from both.

Reported by: "aribi" on the forums via allanjude@
MFC after: 1 week


# 9636a145 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: two-line lua tag

Remove /^--\n--\s*\$FreeBSD\$.*$\n/


# 4d846d26 10-May-2023 Warner Losh <imp@FreeBSD.org>

spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD

The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with: pfg
MFC After: 3 days
Sponsored by: Netflix


# 3cb2f5f3 10-May-2023 Kyle Evans <kevans@FreeBSD.org>

lualoader: add support for .lua configuration files

If a file is specified in loader_conf_files that ends in '.lua', lualoader
will now load and execute that file. These may be used in place of a
traditional loader.conf to use more complicated logic, where some values
may be set based on others or based on the environment that the C bits has
left us with.

Lua scripts are run in a limited environment. In particular, it does not get
access to any modules or, in-fact, anything except environment variable.

A config.buildenv hook has been added so that a local module can add
whatever it may need to to the environment.

When a global var is set in the lua script, it does not immediately alter
the loader environment. Instead, the script's environment is initially
empty and processed only if the whole script executes successfully.
Effectively, a lua configuration file either takes effect or it does not,
an error will not leave it in a half-baked state.

Reviewed by: bcr (manpages), imp
Differential Revision: https://reviews.freebsd.org/D28450


# e9eee0f2 09-Feb-2023 Warner Losh <imp@FreeBSD.org>

lua: Export loaded modules env vars via environment

To make auditing and debugging easier, export foo_load=XXX foo_name=yyy
etc to the loader env that we export to the kernel.

Original by: dhw
Sponsored by: Netflix
Discussed with: kevans
Differential Revsion: https://reviews.freebsd.org/D38466


# b24872cf 31-Jan-2021 Kyle Evans <kevans@FreeBSD.org>

lualoader: position hyphens at the beginning of character classes

According to the Lua 5.4 manual section 6.4.1 ("Patterns"), the interaction
between ranges and classes is not defined and hyphens must be specified at
either the beginning or the end of a set if they are not escaped.

Move all such occurrences to the beginning.

Reported-by: _parv (twitter)
MFC-after: 3 days


# 57656285 24-Jan-2021 Kyle Evans <kevans@FreeBSD.org>

lualoader: improve loader.conf var processing

lualoader was previously not processing \ as escapes; this commit fixes
that and does better error checking on the value as well.

Additionally, loader.conf had some odd restrictions on values that make
little sense. Previously, lines like:

kernel=foo

Would simply be discarded with a malformed line complaint you might not
see unless you disable beastie.

lualoader tries to process these as well as it can and manipulates the
environment, while forthloader did minimal processing and constructed a
`set` command to do the heavy lifting instead. The lua approach was
re-envisioned from building a `set` command so that we can appropriately
reset the environment when, for example, boot environments change.

Lift the previous restrictions to allow unquoted values on the right hand
side of an expression. Note that an unquoted value is effectively:

[A-Za-z0-9-][A-Za-z0-9-_.]*

This commit also stops trying to weirdly limit what it can handle in a
quoted value. Previously it only allowed spaces, alphanumeric, and
punctuation, which is kind of weird. Change it here to grab as much as it
can between two sets of quotes, then let processEnvVar() do the needful and
complain if it finds something malformed looking.

My extremely sophisticated test suite is as follows:

<<EOF
X_01_simple_string="simple"
X_02_escaped_string="s\imple"

X_03_unquoted_val=3
X_04_unquoted_strval=simple_test

X_05_subval="${X_03_unquoted_val}"
X_06_escaped_subval="\${X_03_unquoted_val}"

X_07_embedded="truth${X_03_unquoted_val}"
X_08_escaped_embedded="truth\${X_03_unquoted_val}"

X_09_unknown="${unknown_val}"
X_10_unknown_embedded="truth${unknown_val}"

X_11_crunchy="crunch$unknown_val crunch"
X_12_crunchy="crunch${unknown_val}crunch"

Y_01_badquote="te"lol"
Y_02_eolesc="lol\"
Y_02_noteolesc="lol\\"
Y_03_eolvar="lol$"
Y_03_noteolvar="lol\$"
Y_04_badvar="lol${"

exec="echo Done!"
EOF

Future work may provide a stub loader module in userland so that we can
formally test the loader scripts rather than sketchy setups like the above
in conjunction with the lua-* tools in ^/tools/boot.


# 72cf7db3 09-Jul-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: add loader_conf_dirs support (loader.conf.d)

loader_conf_dirs is the supporting mechanism for the included
/boot/loader.conf.d directory. When lualoader finishes processing all of
the loader_conf_files it finds after walking /boot/defaults/loader.conf,
it will now check any and all loader_conf_dirs and process files ending
in ".conf" as if they were a loader.conf.

Note that loader_conf_files may be specified in a loader.conf.d config
file, but loader_conf_dirs may *not*. It will only be processed as specified
in /boot/defaults/loader.conf and any loader_conf_files that were loaded
from there.

Reviewed by: allanjude, freqlabs, rpokala, tsoome
Includes suggestion from: imp
Relnotes: yes
Differential Revision: https://reviews.freebsd.org/D25608


# 7ed84fa1 17-Dec-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: cli: provide a show-module-options loader command

This effectively dumps everything lualoader knows about to the console using
the libsa pager; that particular lua interface was added in r368591.

A pager stub implementation has been added that just dumps the output as-is
as a compat shim for older loader binaries that do not have lpager. This
stub should be moved into a more appropriate .lua file if we add anything
else that needs the pager.


# 10aeb6cd 12-Dec-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: config: fix module enabled check

A last minute rewrite left this logically wrong; if it's present in
modules_blacklist, then we do not load it.


# 4634bb1f 11-Dec-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: provide module-manipulation commands

Specifically, we have:
- enable-module
- disable-module
- toggle-module

These can be used to add/remove modules to be loaded or force modules to be
loaded in spite of modules_blacklist. In the typical case, a user is
expected to use them to recover an issue happening due to a module directive
they've added to their loader.conf or because they discover that they've
under-specified what to load.

MFC after: 1 week


# e307eb94 21-Sep-2020 Toomas Soome <tsoome@FreeBSD.org>

loader: zfs should support bootonce an nextboot

bootonce feature is temporary, one time boot, activated by
"bectl activate -t BE", "bectl activate -T BE" will reset the bootonce flag.

By default, the bootonce setting is reset on attempt to boot and the next
boot will use previously active BE.

By setting zfs_bootonce_activate="YES" in rc.conf, the bootonce BE will
be set permanently active.

bootonce dataset name is recorded in boot pool labels, bootenv area.

in case of nextboot, the nextboot_enable boolean variable is recorded in
freebsd:nvstore nvlist, also stored in boot pool label bootenv area.
On boot, the loader will process /boot/nextboot.conf if nextboot_enable
is "YES", and will set nextboot_enable to "NO", preventing /boot/nextboot.conf
processing on next boot.

bootonce and nextboot features are usable in both UEFI and BIOS boot.

To use bootonce/nextboot features, the boot loader needs to be updated on disk;
if loader.efi is stored on ESP, then ESP needs to be updated and
for BIOS boot, stage2 (zfsboot or gptzfsboot) needs to be updated
(gpart or other tools).

At this time, only lua loader is updated.

Sponsored by: Netflix, Klara Inc.
Differential Revision: https://reviews.freebsd.org/D25512


# 3fe0ac6a 30-Apr-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: config: improve readConfFiles, rename to readConf

The previous interface was pretty bad, and required the caller to get some
implementation details correct that it really shouldn't need to (e.g.
loader_conf_files handling) and pass in an empty table for it to use.

The new and much improved interface, readConf, is much less of a hack;
hiding these implementation details and just doing the right thing.
config.lua will now use it to process /boot/defaults/loader.conf and the
subsequent loader_conf_files from there, and read-conf will also use it.

This improvement submitted by Olivier (cited below), loader_conf_files
handling from the original patch was changed to just clobber it before
processing and not bother restoring it after the fact following r360505
where it's now guaranteed to evade the loader environment.

PR: 244640
Submitted by: Olivier Certner (olivier freebsd free fr>


# bf832717 30-Apr-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: config: add a table for restricted environment vars

This new table should be used for transient values that don't need to end up
in the loader environment. Generally, these will be things that are internal
details that really aren't needed or interesting outside of the config
module (e.g. if we changed how ${module}_* directives work, they might use
this instead).

To start, populate it with loader_conf_files. Any specific value of
loader_conf_files isn't all that interesting; if we're going to export it,
we should really instead export a loader_conf_files that indicates all of
the configuration files we processed. This will be used to reduce
bookkeeping overhead in a future commit that cleans up readConfFiles.


# 7a5c6c8b 27-Apr-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader: config: start exporting readConfFiles

In the process, change it slightly: readConfFiles will take a string like
loader_conf_files in addition to the loaded_files table that it normally
takes. This is to facilitate the addition of a read-conf CLI command, which
will just pass in the single file to read and an empty table.

MFC after: 3 days


# ecacf5ff 27-Apr-2020 Kyle Evans <kevans@FreeBSD.org>

lualoader config: don't call loader.getenv() as much

We don't actually need to fetch loader_conf_files as much as we do; we've
already fetched it once at the beginning, we only really need to fetch it
again after each file we've processed. If it changes, then we can stash that
off into our local prefiles.

While here, drop a note about the recursion so that I stop trying to
change it. It may very well make redundant some of the work we're doing, but
that's OK.

MFC after: 3 days


# 4bee6189 18-Mar-2020 Ryan Moeller <freqlabs@FreeBSD.org>

Fixup line lost from previous commit (r359072)

Reviewed by: kevans
Approved by: mav (mentor)
MFC after: 1 week
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D24115


# 79c20f22 18-Mar-2020 Ryan Moeller <freqlabs@FreeBSD.org>

loader: Add a "kernel.loaded" hook

This hook can be useful, for example to run a local function to choose
different modules to load when a user has picked a different kernel
from the menu.

Reviewed by: kevans
Approved by: mav (mentor)
MFC after: 1 week
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D24115


# 0db2ca0c 24-Jan-2020 Kyle Evans <kevans@FreeBSD.org>

lua: add modules.loaded hook

This may be used for the local module to hook in and load any additional
modules that it wants, since it can't modify the modules table internal to
config. We may consider adding API to do so at a later time, but I suspect
it will be more complicated to use with little return.

status is captured but ignored for the purpose of loading the hook. status
will be false if *any* module failed to load, but we typically don't let
that halt the boot so there's no reason to let it halt hooks. Some vendors
or setups may have expected fails that would be actively thwarted by
checking it.

We may, at a later date, consider adding an API for letting non-config
modules check which modules have successfully (or not) loaded in case an
unexpected failure *should* halt whatever they are doing.

MFC after: 3 days


# e12ff891 26-Sep-2019 Kyle Evans <kevans@FreeBSD.org>

Further normalize copyright notices

- s/C/c/ where I've been inconsistent about it
- +SPDX tags
- Remove "All rights reserved" where possible

Requested by: rgrimes (all rights reserved)


# 8f7f3d08 25-Mar-2019 Kyle Evans <kevans@FreeBSD.org>

lualoader: Fix up some luacheck concerns

- Garbage collect an unused (removed because it was useless) constant
- Don't bother with vararg notation if args will not be used

MFC after: 1 week


# e1f1ddeb 26-Oct-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Always return a proper dictionary for blacklist

If module_blacklist isn't specified, we have an empty blacklist; effectively
the same as if module_blacklist="" were specified in loader.conf(5).

This was reported when switching to a BE that predated the module_blacklist
introduction, but the problem is valid all the same and likely to be tripped
over in other scenarios.

Reported by: bwidawsk
MFC after: 3 days


# 3078173c 24-Oct-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Improve module loading diagnostics

Some fixes:

- Maintain historical behavior more accurately w.r.t verbose_loading;
verbose_loading strictly prints "${module_name...}" and later "failed!"
or "ok" based on load success
- With or without verbose_loading, dump command_errbuf on load failure.
This usually happens prior to ok/failed if we're verbose_loading

Reviewed by: imp
MFC after: 3 days
Differential Revision: https://reviews.freebsd.org/D17694


# 532dc172 06-Oct-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Create a module blacklist, add DRM modules to it

This is a step in the process of easing migration into the new world order
of DRM drivers. Strongly encourage users towards loading DRM modules via
rc.conf(5) instead of loader.conf(5) by failing the load from loader(8).
Users so inclined may wipe out the blacklist via module_blacklist="" in
loader.conf(5), and it is expected that these modules will eventually be
removed from the blacklist. They may still be loaded as dependencies of
other modules or explicitly via the loader prompt, but this should not be a
major problem.

Approved by: re (rgrimes)
Relnotes: yes
Differential Revision: https://reviews.freebsd.org/D16914


# 5dd1b834 24-Aug-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Fix override of module_path on loader prompt

Earlier changes setup a config.module_path variable that was populated upon
reading of loader.conf(5) and used for restoring module_path to pristine
condition if multiple kernels are attempted. This broke the ability to
override module_path at the loader prompt in case of emergency.

Approved by: re (rgrimes)


# 1ee89ab5 23-Aug-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Accept that people use unquoted values in loader.conf

While loader.conf(5) suggests that all values should be quoted, reality is
that this was never strictly enforced and it is used. We already make some
concession to this in number values, which aren't strictly quoted either.

The compromise here is that multi-word values must be quoted. This lets
things like `foo_load=YES` work, while denying more complex expressions on
the right hand side. This likely catches the vast majority of current usage.

A bit of a kludge is needed to accomplish this since Lua regex doesn't
support branching. I had considered splitting up expressions and generating
the right-hand side of the expressions completely in config.parse, but
deemed this too large of an overhaul to take given the current timing. This
should be re-worked shortly after the thaw.

Reported by: royger


# 0d7bee6a 23-Aug-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Fix (add) Xen support

lualoader was not respecting the 'xen_kernel' environment variable, which
hints to the interpreter that it should load a Xen kernel prior to loading
any other kernel that might be specified. If a Xen kernel is specified and
we fail to load it, we should not proceed to boot.

Reported by: royger
Tested by: royger


# 9a16e110 21-Aug-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Fix loader.conf(5) EOL validation for 'exec' lines

This includes some light rework to simplify the line parsing, as well. If
we hit a line match, we'll always either use the line and move on to the
next line, or we'll spew out malformed line errors.

We had multiple spots to output the error and set the status based on
whether we had a non-nil first capture group or failed EOL validation, but
it was always the same error. Light rework entails a small label jump to
skip error handling and elimination of 'found' local.


# 058c692e 21-Aug-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Refactor config line expressions

A couple of issues addressed:

1.) Modules with - in the name were not recognized as modules
2.) The module regex was repeated for each place a module name may appear
3.) The 'strip leading space' bits were repeated for each expression
4.) The trailing 'comment validation' stuff was repeated every expression

#4 still has some more work to be done. exec lines, for instance, don't
capture a 'value' -- there's only one capture pattern. This throws off the
'c' value that we match, so the trailing bits aren't *actually* being
validated. This isn't a new issue, though, so a future comit will address
this.


# 90486977 13-Aug-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Fix parsing of negative number loader.conf(5) variables

They would previously cause errors, as the regex for these did not tolerate
a leading negative sign, and the variable would simply not parse.


# be2050da 27-Jul-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: "nextboot_file" should be spelled "nextboot_conf"

See: /boot/defaults/loader.conf

Reported by: gtetlow (inadvertently)


# 80eb81f6 19-Jun-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Correct kernel_options handling

`kernel_options` were being passed as flags to load, rather than to the
kernel being loaded. This is the kernel_options counterpart to r335009.


# 35beb928 12-Jun-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Match Forth module-loading behavior w.r.t flags

Also as documented in loader.conf(5), ${module}_flags are actually flags to
be passed to the module, not to `load` as was done thus far.


# deb8c8f5 09-Jun-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Support variable substitution in env var settings

We support both of the following cases of substitution:

bar="y"
foo="${bar}"
foo="$bar"

The latter substitution syntax is, of course, not recommended- all
punctuation must be considered potential variable names, and we do not go
through the effort of searching the different combinations of, for instance,
"$x.y.z" to determine if the variable is $x, $x.y, or $x.y.z.

This is not officially documented as supported, but it has worked in
forthloader for what is most likely a long time as `evaluate` is used to
process the right hand side of the assignment.


# 4072dcb3 09-Jun-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Process loader_conf_files properly

loader.conf(5) documents loader_conf_files to mean "additional configuration
files to be processed right after the present file." However, lualoader
ignored loader_conf_files after processing /boot/defaults/loader.conf.

Rewrite these bits to process loader_conf_files after each loaded file.


# 7aba5b2f 06-Jun-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Add a loaded hook for others to execute upon config load

This will not be executed on reload, though later work could allow for that.
It's intended/expected that later work won't generally need to happen on
every config load, just once (for, e.g., menu initialization) or just when
config is reloaded but not upon the initial load.


# 8d21763e 31-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Simplify some expressions

- No need for a 'goto' when our entire loop body is then wrapped in a
conditional.

- No need to leave commented out prints laying around

- If an expression is clearly going to be either nil or an expression that
isn't likely to be a boolean, we might as well use `or` to specify a
default value for the expression. e.g. `loader.getenv(...) or "no"`


# 9994e26f 31-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: revert whitespace change that snuck in


# 509b21c3 31-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Don't assume that {module}_load is set

The previous iteration of this assumed that {module}_load was set. In the
old world order of default loader.conf(5), this was probably a safe
assumption given that we had almost every module explicitly not-loaded in
it.

In the new world order, this is no longer the case, so one could delete a
_load line inadvertently while leaving a _name, _type, _flags, _before,
_after, or _error. This would have caused a confusing Lua error and borked
module loading.


# 9c2d9b9e 31-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Do case-insensitive comparison of "yes"


# dbef5253 23-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Privatize some more config.lua bits

These functions are also not quite suitable for a public API, so privatize
them to config.


# 64c91742 23-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Make config env-related bits private API

This pertains exclusively to the set/restore functionality that we offer,
where any changes made by loader.conf previously will be effectively removed
upon reload of the configuration. We don't currently have a need to export
these, so don't bother.


# aea262bf 20-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Add primitive hook module, use it to untangle bogus reference

See: comments in the hook module about intended usage, as well as the
introduced use for config.reloaded.

Use the newly introduced hook module to define a "config.reloaded" hook.
This is currently used to register core's clearKernelCache as a reload hook
to avoid a circular dependency and fix this functionality- it didn't
actually work out, and it isn't immediately obvious how it slipped into src.

Other hook types will be introduced into the core lualoader as useful hook
points are identified.


# f0b03262 12-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Sprinkle some verbose_loading salt

Our module loading messages should be hidden without verbose_loading -- go
ahead and do that as a first step.


# 35b0c718 09-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Cache kernel list

With autodetection turned on, hitting the filesystem everytime we need to
calculate choices for the kernel carousel is kind of slow. Cache once on the
first listing and reload it anytime the config is reloaded in case any of
the loader.conf(5) changes that affect this (kernel, kernels,
kernels_autodetect) have changed. This also picks up the case where we've
changed currdev and the autodetected kernels could change.


# 9ab2d3c5 07-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Use cli_execute_unparsed for commands passed in via loader.conf

This applies to:
- exec
- [module]_before
- [module]_error
- [module]_after

Before this commit, these used loader.perform to execute them as a pure,
unsalted loader command. This means that they were not able to take
advantage of any Lua-salted loader commands, like boot and autoboot, or pure
Lua loader commands (functions attached to the 'cli' module).

They now have access to the full arsenal, just shy of being able to execute
arbitrary Lua.


# 2f3ecc87 01-Mar-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Fix some lint-mentioned errors

- nextbootfile is not a variable, but nextboot_file is
- pstatus was explicitly initialized but later clobbered, so don't
initialize it.


# fdabb5f5 28-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: config: Pull some messages out into constants

Rather than hardcoding these things. This could lead to some form of loader
localization later, but the main goal at the moment is to get a clear view
of the strings we're outputting and strive to use more string.format() and
less wild concatenation all over the place.


# eda2d36b 27-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Remove debug function

Our module bits ended up more stable than I anticipated, so this turns out
to be no longer useful.

If things like this need to come back, we should do it in a separate 'debug'
module to serve as a collection of debugging aides. As a rule, this 'debug'
module would *not* be allowed as a requirement of any other modules in-tree.


# fb7275be 27-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Correct test sense, comments, and add some more comments


# 04af4229 25-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: More argument name expansion, part 2

screen also has some instances, but it also has other cleanup to go with it.
Because of this, I will be committing the screen changes separately.


# e1a8835a 24-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Don't explicitly index tables without reason

These indices were assigned the same values as they would've been implicitly
assigned anyways.

While here, throw terminating commas after the last value of tables.


# 67eae503 24-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Remove inaccurate part of comment


# 322a2ddd 24-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Clean up naming conventions a little bit

We mostly use camel case for function names, but some local functions got
mixed in using internal underscores. Doubles down on camel case.


# ddfae7e3 24-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: throw out nextboot's usage of standard config processing

It should use the common parser, but it should not be processed like a
standard file. Rewite check_nextboot to read the file in, check whether it
should continue, then parse as needed.

This allows us to throw the recently introduced check_and_halt callback
swiftly out the window.


# 4adde50d 24-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Strip config.parse of its I/O privileges

config.parse is now purely a parser, rather than a whole proccessor. The
standard process for loading a config file has been split out into
config.processFile.

This clears the way for having nextboot read its own config file and decide
there whether it should parse the rest of the file.


# 164b58fd 24-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Split config file I/O out into a separate function

This is step 1 towards revoking config.parse of it I/O privileges. Ideally,
all reading would be done before config.parse and config.parse would just
take text and parse it rather than being charged with the entire process.


# eed91cae 23-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Explain nextboot stuff a little bit more


# d930cdc2 23-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Add comment on trailing space, don't operate on nil

Functionally, the latter error wouldn't necessarily hurt anything. io.write
will just error out as it's not passed a valid file handle. Still, we can do
better than that.


# 483f0a33 23-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Correct test and name

The functionality was correct, but our style guidelines tend to request that
we shy away from using boolean operations in place of explicit comparisons
to nil.


# e83d7bf2 23-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Plug file handle not properly closed


# 3dcb7648 23-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Add nextboot support

config.parse now takes an extra callback that is invoked on the full text of
the config file. This callback dictates where we should actually try to
parse this file or not.

For nextboot, we use this to halt parsing if we see 'nextboot_enable="NO"'.
If we don't, parse it and write 'nextboot_enable="NO" ' to it. The same
caveat as with forth still applies- writing is only supported by UFS.


# 9ed6f9ef 22-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Use "local function x()" instead of "local x = function()"

The latter is good, but the former is more elegant and clear about what 'x'
is. Adopt it, preferably only using the latter kind of notation where needed
as values for tables.


# 72e39d71 22-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

Add SPDX tags to lua files


# 66964bbc 22-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Attend to some 80-col issues, pointed out by luacheck

Graphics have a tendency to cause 80-col issues, so make an exception to our
standard indentation guidelines for these graphics. This does not hamper
readability too badly.

Two 40-column strings of spaces is trivially replaced with
string.rep(" ", 80)


# 3c6b387a 21-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Clear up an empty conditional branch

We will likely uncomment this whole monster in the near future.


# e2df27e3 21-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Address some 'luacheck' concerns

luacheck pointed out an assortment of issues, ranging from non-standard
globals being created as well as unused parameters, variables, and redundant
assignments.

Using '_' as a placeholder for values unused (whether it be parameters
unused or return values unused, assuming multiple return values) feels clean
and gets the point across, so I've adopted it. It also helps flag candidates
for cleanup later in some of the lambdas I've created, giving me an easy way
to re-evaluate later if we're still not using some of these features.


# d66c7413 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Output "Failed to parse" messages

I can't find any good reason these aren't enabled, so enable them.

The silent runs will only return false on actual parse errors, so it's ok to
be loud about those failures.


# 062d62c9 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Don't return false for failure to open config on silent parse


# 49550489 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Drop explicit boolean tests; b or not b


# 9f71d421 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Drop excessive parenthesizing

This was also a convenience convention (for me) that is not very lua-tic.
Drop it.

I've maintained some parentheses where I'd prefer them, for example,
'if x or y or (z and w) then', but these situations are far and few between.


# aedd6be5 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Drop terminating semicolons

This was previously chosen out of convenience, as we had a mixed style and
needed to be consistent. I started learning Lua on Friday, so I switched
everything over. It is not a very lua-nic convention, though, so drop it.

Excessive parenthesizing around conditionals is next on the chopping block.


# e73c944f 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: When restoring environment, only restore unchanged vars

Track the latest value we've set an environment variable to, and only
restore those that are unchanged from that.

This gives us some leeway to make sure we're not clobbering variables
overwritten by menu changes.


# df9e50a9 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Don't try so hard to load a kernel

If the user's selected a kernel, we really should be trying to load that one
instead of falling back to some default kernel.

This should generally be a no-op and most desirable, unless you really
enjoyed surprises.


# ebddeb04 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Prefer selected kernel to currently loaded


# 25c4c7a5 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

lualoader: Move carousel storage out into config

Carousel storage doesn't need to happen in the menu module, and indeed
storing it there introduces a circular reference between drawer and menu
that only works because of global pollution in loader.lua.

Carousel choices generally map to config entries anyways, making it as good
of place as any to store these. Move {get,set}CarouselIndex functionality
out into config so that drawer and menu may both use it. If we had more
carousel functionality, it might make sense to create a carousel module, but
this is not the case.


# b5746545 20-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Consistently organize modules

We follow pretty closely the following structure of a module:

1. Copyright notice
2. Module requires
3. Module local declarations
4. Module local definitions
5. Module exports
6. return

Re-organize the one-offs (config/drawer) and denote the start of module
exports with a comment.


# 21d5bcbe 19-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Add copyright notice in places

I've made some not-insignificant changes to config, menu, and password bits
of our lua scripts. Add a copyright notice to them to reflect.


# 1f5696c7 19-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Round up some more style.lua(9) concerns


# e6db469b 19-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Remove inaccurate comment after r329590

lualoader does a pretty good job of reverting any environment changes now.
It will even wipe out boot_verbose if it's set explicitly in loader.conf(5)
and overwritten in the boot options menu.

Future work will likely change this, as explicit choices made in the menu
should probably override the new loader.conf(5). I don't suspect this will
cause much grief, though, so it is not a high priority until boot
environment support actually lands.


# ce4a5da5 19-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Restore environment upon config reload

This restores environment to whatever defaults we had coming into lualoader.


# df8a9d46 19-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Track env changes that come in via loader.conf(5)

This will be used when boot environment support lands to make a good-faith
effort to apply any new loader.conf(5) environment settings atop the default
configuration that we started with.


# fa4a2394 19-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Defer kernel/module loading until boot or menu escape

Loading the kernel and modules can be really slow. Loading before the menu
draws and every time one changes kernel/boot environment is even more
painful.

Defer loading until we either boot, auto-boot, or escape to loader prompt.
We still need to deal with configuration changes as the boot environment
changes, but this is generally much quicker.

This commit strips all ELF loading out of config.load/config.reload so that
these are purely for configuration. config.loadelf has been created to deal
with kernel/module loads. Unloading logic has been ripped out, as we won't
need to deal with it in the menu anymore.

Discussed in part with: allanjude


# 27dd7ddf 18-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: reload previously loaded kernel at config-load/reload

r329550 introduced config.kernel_loaded. config.load() doesn't provide a
means of overriding the kernel to load, but that likely isn't necessary as
it will not be a common case. When loading the kernel, just attempt to load
the kernel previously loaded and specified in config.kernel_loaded.

If we haven't loaded a kernel yet, config.kernel_loaded will be unset/nil
and the "default"/first kernel found will be loaded. If we've loaded a
kernel, we'll try to load that same kernel again and fallback to the default
kernel if we need to.

This in also in support of upcoming boot environment support.


# d4591301 18-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Store the loaded kernel as config.kernel_loaded

'nil' means the 'first kernel found in module_path', which is the same
interpretation as passing 'nil' to loadkernel.

Otherwise, it denotes the name of a kernel that we've successfully loaded.
When reloaded later, we will still need to do the full search again to
locate the actual kernel in case things have changed, so just the name is
good enough.

This is in support of upcoming boot environment support. vfs.root.mountfrom
and currdev will be changed, then we will reload configuration and attempt
to reload the currently chosen kernel unless we shouldn't.


# e402cd13 17-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: More style nits, config.lua

Some other points I think we need to be consistent on:
- Spacing around string concatenation (always)
- Test against 'nil' explicitly rather than relying on 'not' for things that
reasonably won't be returning a boolean. e.g. loader.getenv

Eventually this will all get formalized somewhere.


# faf2ee29 17-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Remove some debugging bits that snuck in... gr...


# 15d8e03c 17-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Fix module_path handling with multiple kernels

Once we've successfully loaded a kernel, we add its directory to
module_path. If we switch kernels with the kernel selector, we again prepend
the kernel directory to the current module_path and end up with multiple
kernel paths, potentially with mismatched kernel/modules, added to
module_path.

Fix it by caching module_path at load() time and using the cached version
whenever we load a new kernel.


# 7fc96e38 17-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Fix verbiage and some typos

"other_kernel" is decidedly not spelled "other_kern"


# 4afa0076 17-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: dumpModules => lsModules

rgrimes@ notes that this behavior is more befitting of the latter name than
the former, and I'm inclined to agree.

Reported by: rgrimes


# 24a1bd54 16-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Style pass

These are the style points that I'd like to try and maintain in our lua
scripts:
- Parentheses around conditionals
- Trailing semicolons, except on block terminators
- s:method(...) instead of string.method(s, ...) where applicable

There's likely more, but that'll get hammered out as we continue.


# 18c286a0 16-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Try to load alternate kernels as directories first

This is the procedure that config.loadkernel tries to go through, but
reloading kernel config didn't use this function. Amend config.loadkernel to
take an optional other_kernel.

While here, be a little more verbose ("Trying to load kernel") so that it's
easy to follow where we've gone wrong.


# 4d290ffb 16-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Correct test sense, this should have been 'not nil'


# 71049173 16-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Address some nits

1.) Instead of string.function(s, ...), use s:function(...)
2.) Don't try to concatenate `res`, it was just tested to be nil
3.) Note that "Loading configuration" is configured modules, and be a little
more precise in mentioning what failed ("loading of one or more modules")


# 41e77b53 16-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Add debug method to dump modules


# c990f0a9 16-Feb-2018 Kyle Evans <kevans@FreeBSD.org>

stand/lua: Correct some trivial errors in config

An empty module_path to start with isn't ideal, but if all modules are
contained within a kernel directory (which is what we just tested) then it
isn't strictly an error. Don't assume that module_path has a value already.

When we fail to load the kernel, printing the result (which is guaranteed to
be nil) is not intended; print the name of the kernel.


# 088b4f5f 12-Feb-2018 Warner Losh <imp@FreeBSD.org>

Add the lua scripts from the lua-bootloader SoC

These are the .lua files from from Pedro Souza's 2014 Summer of Code
project. Rui Paulo, Pedro Arthur and Wojciech A. Koszek also
contributed.

Obtained from: https://wiki.freebsd.org/SummerOfCode2014/LuaLoader
Sponsored by: Google Summer of Code

Improve the SoC lua menu code to bring it in line with forth
menu functionality

Submitted by: Zakary Nafziger
Sponsored by: FreeBSD Foundation

Use loader.setenv and loader.unsetenv instead of loader.perform

Convert from include("/boot/foo.lua") to foo = require("foo");
to bring in line with latest lua module conventions.

Enforce a uniform style for the new .lua files:
o hard tab indenation for 8 spaces
o don't have if foo then bar; else bas; end on one line

MFC After: 1 month
Relnotes: yes
Differential Review: https://reviews.freebsd.org/D14295