Skip to contentSkip to conversation

riscv: lib: Add sbi-exit-code to configure and environment

PATCH In review Jesse Taube 2 participants 4 messages Jul 3, 2025 – Jul 7, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: lib: Add sbi-exit-code to configure and environment

Add --[enable|disable]-sbi-exit-code to configure script. With the default value disabled.
Add a check for SBI_PASS_EXIT_CODE in the environment, so that passing of the test status is configurable from both the environment and the configure script

---
 configure      | 11 +++++++++++
 lib/riscv/io.c | 12 +++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
configure +11 -0
index 20bf5042..7c949bdc 100755
@@ -67,6 +67,7 @@ earlycon=
 console=
 efi=
 efi_direct=
+sbi_exit_code=0
 target_cpu=
 
 # Enable -Werror by default for git repositories only (i.e. developer builds)
@@ -141,6 +142,9 @@ usage() {
 	                           system and run from the UEFI shell. Ignored when efi isn't enabled
 	                           and defaults to enabled when efi is enabled for riscv64.
 	                           (arm64 and riscv64 only)
+	    --[enable|disable]-sbi-exit-code
+	                           Enable or disable sending pass/fail exit code to SBI SRST.
+	                           (disabled by default, riscv only)
 EOF
     exit 1
 }
@@ -236,6 +240,12 @@ while [[ $optno -le $argc ]]; do
 	--disable-efi-direct)
 	    efi_direct=n
 	    ;;
+	--enable-sbi-exit-code)
+	    sbi_exit_code=1
+	    ;;
+	--disable-sbi-exit-code)
+	    sbi_exit_code=0
+	    ;;
 	--enable-werror)
 	    werror=-Werror
 	    ;;
@@ -551,6 +561,7 @@ EOF
 elif [ "$arch" = "riscv32" ] || [ "$arch" = "riscv64" ]; then
     echo "#define CONFIG_UART_EARLY_BASE ${uart_early_addr}" >> lib/config.h
     [ "$console" = "sbi" ] && echo "#define CONFIG_SBI_CONSOLE" >> lib/config.h
+    echo "#define CONFIG_SBI_EXIT_CODE ${sbi_exit_code}" >> lib/config.h
     echo >> lib/config.h
 fi
 echo "#endif" >> lib/config.h
lib/riscv/io.c +11 -1
index b1163404..0e666009 100644
@@ -162,8 +162,18 @@ void halt(int code);
 
 void exit(int code)
 {
+	char *s = getenv("SBI_PASS_EXIT_CODE");
+	bool pass_exit = CONFIG_SBI_EXIT_CODE;
+
 	printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-	sbi_shutdown(code == 0);
+
+	if (s)
+		pass_exit = (*s == '1' || *s == 'y' || *s == 'Y');
+
+	if (pass_exit)
+		sbi_shutdown(code == 0);
+	else
+		sbi_shutdown(true);
 	halt(code);
 	__builtin_unreachable();
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 60 lines · Jesse Taube

Add --[enable|disable]-sbi-exit-code to configure script. With the default value disabled.
Add a check for SBI_PASS_EXIT_CODE in the environment, so that passing of the test status is configurable from both the environment and the configure script

---
 configure      | 11 +++++++++++
 lib/riscv/io.c | 12 +++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
configure +11 -0
index 20bf5042..7c949bdc 100755
@@ -67,6 +67,7 @@ earlycon=
 console=
 efi=
 efi_direct=
+sbi_exit_code=0
 target_cpu=
 
 # Enable -Werror by default for git repositories only (i.e. developer builds)
@@ -141,6 +142,9 @@ usage() {
 	                           system and run from the UEFI shell. Ignored when efi isn't enabled
 	                           and defaults to enabled when efi is enabled for riscv64.
 	                           (arm64 and riscv64 only)
+	    --[enable|disable]-sbi-exit-code
+	                           Enable or disable sending pass/fail exit code to SBI SRST.
+	                           (disabled by default, riscv only)
 EOF
     exit 1
 }
@@ -236,6 +240,12 @@ while [[ $optno -le $argc ]]; do
 	--disable-efi-direct)
 	    efi_direct=n
 	    ;;
+	--enable-sbi-exit-code)
+	    sbi_exit_code=1
+	    ;;
+	--disable-sbi-exit-code)
+	    sbi_exit_code=0
+	    ;;
 	--enable-werror)
 	    werror=-Werror
 	    ;;
@@ -551,6 +561,7 @@ EOF
 elif [ "$arch" = "riscv32" ] || [ "$arch" = "riscv64" ]; then
     echo "#define CONFIG_UART_EARLY_BASE ${uart_early_addr}" >> lib/config.h
     [ "$console" = "sbi" ] && echo "#define CONFIG_SBI_CONSOLE" >> lib/config.h
+    echo "#define CONFIG_SBI_EXIT_CODE ${sbi_exit_code}" >> lib/config.h
     echo >> lib/config.h
 fi
 echo "#endif" >> lib/config.h
lib/riscv/io.c +0 -0
index b1163404..0e666009 100644

lib/riscv/io.c void halt(int code); · line 166

Quoted 5 lines
lib/riscv/io.c +2 -0
@@ -162,8 +162,18 @@ void halt(int code);
 
 void exit(int code)
 {
+	char *s = getenv("SBI_PASS_EXIT_CODE");
+	bool pass_exit = CONFIG_SBI_EXIT_CODE;

This is the first case of what may become more common - a config variable which also has an env override. I think it may be good convention to name them the same, i.e. the env name would also be CONFIG_SBI_EXIT_CODE, unless you think that would be confusing for some reason?

lib/riscv/io.c void halt(int code); · line 171

Quoted 6 lines
lib/riscv/io.c +4 -1
+
 	printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-	sbi_shutdown(code == 0);
+
+	if (s)
+		pass_exit = (*s == '1' || *s == 'y' || *s == 'Y');

We now have this logic in four places[1]. I think it's time we factor it, and it's counterpart "!(s && (*s == '0' || *s == 'n' || *s == 'N'))" into a couple helper macros. I'm not sure where the best place for those macros to live is, though. I guess libcflat.h, but we really ought to split that thing apart someday...

[1]
- twice in lib/errata.h
- once in riscv/sbi-tests.ha
- and now here

lib/riscv/io.c void halt(int code); · line 176

Quoted 5 lines
lib/riscv/io.c +5 -0
+
+	if (pass_exit)
+		sbi_shutdown(code == 0);
+	else
+		sbi_shutdown(true);

nit: can be written more concisely as

sbi_shutdown(pass_exit ? code == 0 : true)

Quoted 6 lines
lib/riscv/io.c +0 -0
 	halt(code);
 	__builtin_unreachable();
 }
Signature · 1 line
2.43.0

Thanks,
drew

#2 lore raw
Quoted 63 lines · Andrew Jones
Quoted 61 lines · Jesse Taube

Add --[enable|disable]-sbi-exit-code to configure script. With the default value disabled.
Add a check for SBI_PASS_EXIT_CODE in the environment, so that passing of the test status is configurable from both the environment and the configure script

---
 configure      | 11 +++++++++++
 lib/riscv/io.c | 12 +++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
configure +11 -0
index 20bf5042..7c949bdc 100755
@@ -67,6 +67,7 @@ earlycon=
 console=
 efi=
 efi_direct=
+sbi_exit_code=0
 target_cpu=
 
 # Enable -Werror by default for git repositories only (i.e. developer builds)
@@ -141,6 +142,9 @@ usage() {
                                 system and run from the UEFI shell. Ignored when efi isn't enabled
                                 and defaults to enabled when efi is enabled for riscv64.
                                 (arm64 and riscv64 only)
+         --[enable|disable]-sbi-exit-code
+                                Enable or disable sending pass/fail exit code to SBI SRST.
+                                (disabled by default, riscv only)
 EOF
     exit 1
 }
@@ -236,6 +240,12 @@ while [[ $optno -le $argc ]]; do
      --disable-efi-direct)
          efi_direct=n
          ;;
+     --enable-sbi-exit-code)
+         sbi_exit_code=1
+         ;;
+     --disable-sbi-exit-code)
+         sbi_exit_code=0
+         ;;
      --enable-werror)
          werror=-Werror
          ;;
@@ -551,6 +561,7 @@ EOF
 elif [ "$arch" = "riscv32" ] || [ "$arch" = "riscv64" ]; then
     echo "#define CONFIG_UART_EARLY_BASE ${uart_early_addr}" >> lib/config.h
     [ "$console" = "sbi" ] && echo "#define CONFIG_SBI_CONSOLE" >> lib/config.h
+    echo "#define CONFIG_SBI_EXIT_CODE ${sbi_exit_code}" >> lib/config.h
     echo >> lib/config.h
 fi
 echo "#endif" >> lib/config.h
lib/riscv/io.c +0 -0
index b1163404..0e666009 100644
@@ -162,8 +162,18 @@ void halt(int code);
 
Quoted 9 lines

lib/riscv/io.c void halt(int code); · line 166

Quoted 4 lines
lib/riscv/io.c +2 -0
 void exit(int code)
 {
+     char *s = getenv("SBI_PASS_EXIT_CODE");
+     bool pass_exit = CONFIG_SBI_EXIT_CODE;

This is the first case of what may become more common - a config variable which also has an env override. I think it may be good convention to name them the same, i.e. the env name would also be CONFIG_SBI_EXIT_CODE, unless you think that would be confusing for some reason?

I changed the name because the configure option seemed very long. I will make them both SBI_EXIT_CODE. Should I add a macro to simplify future uses
of a config variable which also has an env override.

Quoted 13 lines

lib/riscv/io.c void halt(int code); · line 171

Quoted 7 lines
lib/riscv/io.c +4 -1
+
      printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-     sbi_shutdown(code == 0);
+
+     if (s)
+             pass_exit = (*s == '1' || *s == 'y' || *s == 'Y');

We now have this logic in four places[1]. I think it's time we factor it, and it's counterpart "!(s && (*s == '0' || *s == 'n' || *s == 'N'))" into a couple helper macros. I'm not sure where the best place for those macros to live is, though. I guess libcflat.h, but we really ought to split that thing apart someday...

Is lib/argv.h an ok place?
getenv is defined in lib/string.c which is interesting. I wonder if it could be moved to lib/argv.c?

Thanks,
Jesse Taube

Quoted 25 lines

[1]
- twice in lib/errata.h
- once in riscv/sbi-tests.ha
- and now here

lib/riscv/io.c void halt(int code); · line 176

Quoted 5 lines
lib/riscv/io.c +5 -0
+
+     if (pass_exit)
+             sbi_shutdown(code == 0);
+     else
+             sbi_shutdown(true);

nit: can be written more concisely as

sbi_shutdown(pass_exit ? code == 0 : true)

Quoted 6 lines
lib/riscv/io.c +0 -1
      halt(code);
      __builtin_unreachable();
 }
--

2.43.0

Thanks,
drew

#3 lore raw
Quoted 74 lines · Jesse Taube
Quoted 63 lines · Andrew Jones
Quoted 61 lines · Jesse Taube

Add --[enable|disable]-sbi-exit-code to configure script. With the default value disabled.
Add a check for SBI_PASS_EXIT_CODE in the environment, so that passing of the test status is configurable from both the environment and the configure script

---
 configure      | 11 +++++++++++
 lib/riscv/io.c | 12 +++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
configure +11 -0
index 20bf5042..7c949bdc 100755
@@ -67,6 +67,7 @@ earlycon=
 console=
 efi=
 efi_direct=
+sbi_exit_code=0
 target_cpu=
 
 # Enable -Werror by default for git repositories only (i.e. developer builds)
@@ -141,6 +142,9 @@ usage() {
                                 system and run from the UEFI shell. Ignored when efi isn't enabled
                                 and defaults to enabled when efi is enabled for riscv64.
                                 (arm64 and riscv64 only)
+         --[enable|disable]-sbi-exit-code
+                                Enable or disable sending pass/fail exit code to SBI SRST.
+                                (disabled by default, riscv only)
 EOF
     exit 1
 }
@@ -236,6 +240,12 @@ while [[ $optno -le $argc ]]; do
      --disable-efi-direct)
          efi_direct=n
          ;;
+     --enable-sbi-exit-code)
+         sbi_exit_code=1
+         ;;
+     --disable-sbi-exit-code)
+         sbi_exit_code=0
+         ;;
      --enable-werror)
          werror=-Werror
          ;;
@@ -551,6 +561,7 @@ EOF
 elif [ "$arch" = "riscv32" ] || [ "$arch" = "riscv64" ]; then
     echo "#define CONFIG_UART_EARLY_BASE ${uart_early_addr}" >> lib/config.h
     [ "$console" = "sbi" ] && echo "#define CONFIG_SBI_CONSOLE" >> lib/config.h
+    echo "#define CONFIG_SBI_EXIT_CODE ${sbi_exit_code}" >> lib/config.h
     echo >> lib/config.h
 fi
 echo "#endif" >> lib/config.h
lib/riscv/io.c +0 -0
index b1163404..0e666009 100644
@@ -162,8 +162,18 @@ void halt(int code);
 
Quoted 9 lines

lib/riscv/io.c void halt(int code); · line 166

Quoted 4 lines
lib/riscv/io.c +2 -0
 void exit(int code)
 {
+     char *s = getenv("SBI_PASS_EXIT_CODE");
+     bool pass_exit = CONFIG_SBI_EXIT_CODE;

This is the first case of what may become more common - a config variable which also has an env override. I think it may be good convention to name them the same, i.e. the env name would also be CONFIG_SBI_EXIT_CODE, unless you think that would be confusing for some reason?

Quoted 2 lines

I changed the name because the configure option seemed very long. I will make them both SBI_EXIT_CODE.

We need the CONFIG_ part on the config name in order keep it consistent with other configs, so I think we just need a long name for both.

Quoted 3 lines

Should I add a macro to simplify
future uses
of a config variable which also has an env override.

Sounds good to me.

Quoted 15 lines
Quoted 14 lines

lib/riscv/io.c void halt(int code); · line 171

Quoted 8 lines
lib/riscv/io.c +4 -1
+
      printf("\nEXIT: STATUS=%d\n", ((code) << 1) | 1);
-     sbi_shutdown(code == 0);
+
+     if (s)
+             pass_exit = (*s == '1' || *s == 'y' || *s == 'Y');

We now have this logic in four places[1]. I think it's time we factor it, and it's counterpart "!(s && (*s == '0' || *s == 'n' || *s == 'N'))" into a couple helper macros. I'm not sure where the best place for those macros to live is, though. I guess libcflat.h, but we really ought to split that thing apart someday...

Quoted 1 line

Is lib/argv.h an ok place?

That could work.

Quoted 2 lines

getenv is defined in lib/string.c which is interesting. I wonder if it could be moved to lib/argv.c?

getenv is libc and in lives in stdlib in libc, so we should move our prototype to lib/stdlib.h from libcflat.h, but we don't have a stdlib.c file so string.c and argv.c are both "wrong", but at least string.c also has all the functions getenv depends on locally...

Thanks,
drew