diff --git a/stand/efi/loader/main.c b/stand/efi/loader/main.c --- a/stand/efi/loader/main.c +++ b/stand/efi/loader/main.c @@ -909,6 +909,7 @@ char buf[24]; int revision; + loader_features |= FEATURE_EARLY_ACPI; if ((rsdp = efi_get_table(&acpi20)) == NULL) if ((rsdp = efi_get_table(&acpi)) == NULL) return; diff --git a/stand/liblua/lutils.c b/stand/liblua/lutils.c --- a/stand/liblua/lutils.c +++ b/stand/liblua/lutils.c @@ -580,6 +580,40 @@ }; #undef REG_SIMPLE +static void +lua_add_feature(void *cookie, const char *name, const char *desc, bool enabled) +{ + lua_State *L = cookie; + + /* + * We don't currently make features not enabled available to the + * interpreter, but there's no reason we couldn't make it so. + */ + if (!enabled) + return; + + /* + * The feature table consists solely of features that are enabled, and + * their associated descriptions for debugging purposes. + */ + lua_pushstring(L, desc); + lua_setfield(L, -2, name); +} + +static void +lua_add_features(lua_State *L) +{ + + lua_newtable(L); + loader_iter_features(&lua_add_feature, L); + + /* + * We should still have just the table on the stack after we're done + * iterating. + */ + lua_setfield(L, -2, "features"); +} + int luaopen_loader(lua_State *L) { @@ -593,6 +627,7 @@ lua_setfield(L, -2, "lua_path"); lua_pushinteger(L, bootprog_rev); lua_setfield(L, -2, "version"); + lua_add_features(L); /* Set global printc to loader.printc */ lua_register(L, "printc", lua_printc); return 1; diff --git a/stand/libsa/Makefile b/stand/libsa/Makefile --- a/stand/libsa/Makefile +++ b/stand/libsa/Makefile @@ -13,7 +13,7 @@ # standalone components and stuff we have modified locally SRCS+= gzguts.h zutil.h __main.c abort.c assert.c bcd.c environment.c \ - getopt.c gets.c globals.c \ + features.c getopt.c gets.c globals.c \ hexdump.c nvstore.c pager.c panic.c printf.c strdup.c strerror.c \ random.c sbrk.c tslog.c twiddle.c zalloc.c zalloc_malloc.c diff --git a/stand/libsa/features.c b/stand/libsa/features.c new file mode 100644 --- /dev/null +++ b/stand/libsa/features.c @@ -0,0 +1,54 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2023 Kyle Evans + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include "stand.h" + +uint32_t loader_features; + +#define FEATURE_ENTRY(name, desc) { FEATURE_##name, #name, desc } +static const struct loader_feature_entry { + uint32_t value; + const char *name; + const char *desc; +} loader_feature_map[] = { + FEATURE_ENTRY(EARLY_ACPI, "Loader probes ACPI in early startup"), +}; + +void +loader_iter_features(loader_iter_feature_fn *iter_fn, void *cookie) +{ + const struct loader_feature_entry *entry; + + for (size_t i = 0; i < nitems(loader_feature_map); i++) { + entry = &loader_feature_map[i]; + + (*iter_fn)(cookie, entry->name, entry->desc, + (loader_features & entry->value) != 0); + } +} diff --git a/stand/libsa/stand.h b/stand/libsa/stand.h --- a/stand/libsa/stand.h +++ b/stand/libsa/stand.h @@ -498,6 +498,20 @@ */ caddr_t ptov(uintptr_t); +/* features.c */ +typedef void (loader_iter_feature_fn)(void *, const char *, const char *, bool); + +extern void loader_iter_features(loader_iter_feature_fn *, void *); +extern uint32_t loader_features; + +/* + * Note that these should also be added to the mapping table in features.c, + * which the interpreter may query to provide details from. The name with + * FEATURE_ removed is assumed to be the name we'll provide in the loader + * features table, just to simplify reasoning about these. + */ +#define FEATURE_EARLY_ACPI 0x0001 + /* hexdump.c */ void hexdump(caddr_t region, size_t len); diff --git a/stand/lua/core.lua b/stand/lua/core.lua --- a/stand/lua/core.lua +++ b/stand/lua/core.lua @@ -382,6 +382,16 @@ loader.perform(composeLoaderCmd("boot", argstr)) end +-- XXX TODO: Document in manpage +function core.hasFeature(name) + if not loader.features then + -- Loader too old, no feature support + return false + end + + return loader.features[name] +end + function core.isSingleUserBoot() local single_user = loader.getenv("boot_single") return single_user ~= nil and single_user:lower() == "yes"