git: 9398a495eb30 - main - loader: Add loader.exit

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 27 Feb 2024 03:50:16 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=9398a495eb305d5bf240bc998ee2f6128d75f4ca

commit 9398a495eb305d5bf240bc998ee2f6128d75f4ca
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-02-27 03:19:58 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-02-27 03:49:46 +0000

    loader: Add loader.exit
    
    Add loader.exit(status). While one can get alomst this behavior with
    loader.perform("quit"), quit doesn't allow a value to be returned to the
    firmware. The interpretation of 'status' is firmware specific. This can
    be used when autobooting doesn't work in scripts, for example, to allow
    the firmware to try something else...
    
    Sponsored by:           Netflix
    
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D44094
---
 stand/liblua/lutils.c  | 16 ++++++++++++----
 stand/lua/loader.lua.8 |  4 ++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/stand/liblua/lutils.c b/stand/liblua/lutils.c
index 0be9f5f28ac3..874dc8bf7d5d 100644
--- a/stand/liblua/lutils.c
+++ b/stand/liblua/lutils.c
@@ -109,6 +109,13 @@ lua_perform(lua_State *L)
 	return 1;
 }
 
+static int
+lua_exit(lua_State *L)
+{
+	exit(luaL_checkinteger(L, 1));
+	return 0;
+}
+
 static int
 lua_command_error(lua_State *L)
 {
@@ -380,14 +387,15 @@ lua_writefile(lua_State *L)
 
 #define REG_SIMPLE(n)	{ #n, lua_ ## n }
 static const struct luaL_Reg loaderlib[] = {
-	REG_SIMPLE(delay),
-	REG_SIMPLE(command_error),
 	REG_SIMPLE(command),
-	REG_SIMPLE(interpret),
-	REG_SIMPLE(parse),
+	REG_SIMPLE(command_error),
+	REG_SIMPLE(delay),
+	REG_SIMPLE(exit),
 	REG_SIMPLE(getenv),
 	REG_SIMPLE(has_command),
 	REG_SIMPLE(has_feature),
+	REG_SIMPLE(interpret),
+	REG_SIMPLE(parse),
 	REG_SIMPLE(perform),
 	REG_SIMPLE(printc),	/* Also registered as the global 'printc' */
 	REG_SIMPLE(setenv),
diff --git a/stand/lua/loader.lua.8 b/stand/lua/loader.lua.8
index ff3b91ddfb09..e5aee7e8602d 100644
--- a/stand/lua/loader.lua.8
+++ b/stand/lua/loader.lua.8
@@ -61,6 +61,10 @@ Returns the error string from the last command to fail.
 Like
 .Fn perform
 but the arguments are already parsed onto the stack.
+.It Fn exit status
+Exit the boot loader back to the firmware with a status of
+.Va status .
+The interpretation of this value is firmware specific.
 .It Fn interpret str
 Execute the loader builtin command
 .Va str