Makefile: perform sanity checks on payload during build
PATCH 1/1 Makefile: perform sanity checks on payload during build
To make mistakes more obvious and debugging easier we can check that the payload is not an ELF file, and that the first byte is possibly the start of a valid instruction.
I would have preferred to not do this check in Bash but I didn't want to introduce any additional dependencies, and there isn't a proper language already in use.
I also made it an #error to not define FW_PAYLOAD_PATH since the build system will always do that. If the user doesn't specify one a default file is used.
--- firmware/external_deps.mk | 3 ++- firmware/fw_payload.S | 6 ++---- scripts/check-payload.sh | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100755 scripts/check-payload.sh
firmware/external_deps.mk +2 -1
@@ -10,5 +10,6 @@ $(platform_build_dir)/firmware/fw_dynamic.o: $(FW_FDT_PATH) $(platform_build_dir)/firmware/fw_jump.o: $(FW_FDT_PATH) $(platform_build_dir)/firmware/fw_payload.o: $(FW_FDT_PATH) - + scripts/check-payload.sh $(FW_PAYLOAD_PATH_FINAL) + $(call compile_as,$@,$<) $(platform_build_dir)/firmware/fw_payload.o: $(FW_PAYLOAD_PATH_FINAL)
firmware/fw_payload.S +2 -4
@@ -94,8 +94,6 @@ fw_options: .globl payload_bin payload_bin: #ifndef FW_PAYLOAD_PATH - wfi - j payload_bin -#else - .incbin FW_PAYLOAD_PATH +#error FW_PAYLOAD_PATH should always be set by the build system #endif + .incbin FW_PAYLOAD_PATH
scripts/check-payload.sh new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH 1/1 Makefile: perform sanity checks on payload during build
Hi Tim,
Quoted 5 lines · Tim Hutt
To make mistakes more obvious and debugging easier we can check that the payload is not an ELF file, and that the first byte is possibly the start of a valid instruction.
I would have preferred to not do this check in Bash but I didn't want to introduce any additional dependencies, and there isn't a proper language already in use.
I also made it an
#errorto not defineFW_PAYLOAD_PATHsince the build system will always do that. If the user doesn't specify one a default file is used.
You need a Signed-off-by. Please also wrap the commit message to 72 characters.
Quoted 12 lines
--- firmware/external_deps.mk | 3 ++- firmware/fw_payload.S | 6 ++---- scripts/check-payload.sh | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100755 scripts/check-payload.shfirmware/external_deps.mk +0
-0
firmware/external_deps.mk line 14
Quoted 6 lines
firmware/external_deps.mk +2
-1@@ -10,5 +10,6 @@ $(platform_build_dir)/firmware/fw_dynamic.o: $(FW_FDT_PATH) $(platform_build_dir)/firmware/fw_jump.o: $(FW_FDT_PATH) $(platform_build_dir)/firmware/fw_payload.o: $(FW_FDT_PATH) - + scripts/check-payload.sh $(FW_PAYLOAD_PATH_FINAL) + $(call compile_as,$@,$<)
I would suggest leaving the recipe for fw_payload.o alone, and using a separate, possibly phony, target for this check. It's surprising to have the recipe for assembling this one file defined in a separate file.
Quoted 22 lines
firmware/external_deps.mk +0
-0$(platform_build_dir)/firmware/fw_payload.o: $(FW_PAYLOAD_PATH_FINAL)firmware/fw_payload.S +2
-4@@ -94,8 +94,6 @@ fw_options: .globl payload_bin payload_bin: #ifndef FW_PAYLOAD_PATH - wfi - j payload_bin -#else - .incbin FW_PAYLOAD_PATH +#error FW_PAYLOAD_PATH should always be set by the build system #endif + .incbin FW_PAYLOAD_PATHscripts/check-payload.sh new file +0
-0
scripts/check-payload.sh line 1
Quoted 1 line
scripts/check-payload.sh new file +1
-0@@ -0,0 +1,43 @@ +#!/usr/bin/env bash
With the below changes this script can be POSIX-compatible and use /bin/sh.
Quoted 4 lines
scripts/check-payload.sh new file +4
-0+ +# Simple script to try and detect invalid payloads. The payload should be a flat +# image and the first bytes should be valid instructions - OpenSBI will jump +# to the start of the file. It shouldn't be an ELF file.
scripts/check-payload.sh line 15
Quoted 10 lines
scripts/check-payload.sh new file +10
-0+# +# Bash is a terrible language for this purpose but we can make it work. + +if [ -z "$1" ]; then + echo "Usage: $0 <file>" + exit 1 +fi + +# Check the payload is not an ELF file. +readelf -h "$1" >/dev/null 2>&1 && {
This should get $READELF from Makefile; see how that file sets OBJCOPY.
scripts/check-payload.sh line 24
Quoted 9 lines
scripts/check-payload.sh new file +9
-0+ echo "Error: The payload is an ELF file; it should be a flat executable instead." + exit 1 +} + +# Check the first bytes are a valid RISC-V instruction. Since we don't know +# what is valid we can only do limited checks but this will check that it is at +# least a 16 or 32-bit instruction. Larger instructions are currently unused. +first_byte="$(head -c 1 "$1")" +LC_ALL=C printf -v first_byte_dec %d \'$first_byte
This can be done portably with od(1):
first_byte=$(od -An -N1 -td1 "$1")
scripts/check-payload.sh line 37
Quoted 13 lines
scripts/check-payload.sh new file +13
-0+ +# for byte in range(256): +# if not ((byte & 0b11) != 0b11 or (byte & 0b11100) != 0b11100): +# print(byte) +[ \ + "$first_byte_dec" == 31 -o \ + "$first_byte_dec" == 63 -o \ + "$first_byte_dec" == 95 -o \ + "$first_byte_dec" == 127 -o \ + "$first_byte_dec" == 159 -o \ + "$first_byte_dec" == 191 -o \ + "$first_byte_dec" == 223 -o \ + "$first_byte_dec" == 255 \
You can use arithmetic expansion here (which is also POSIX):
[ $((first_byte & 31)) -eq 31 ]
scripts/check-payload.sh line 43
Quoted 6 lines
scripts/check-payload.sh new file +6
-0+] && { + echo "Error: The payload's first byte is not a 16- or 32-bit RISC-V instruction. The payload should be a flat executable." + exit 1 +} + +exit 0
No need for an explicit "exit 0" at the end.
Regards,
Samuel
A message that is not in the archive (sent off-list or not yet received): CAKuVd4DNsOsLVp-R9kkTrtAeEjPkA-UwNo40CZOy6JPUrjHspg@mail.gmail.com
PATCH 1/1 Makefile: perform sanity checks on payload during build
Quoted 4 lines
I would suggest leaving the recipe for fw_payload.o alone, and using a separate, possibly phony, target for this check. It's surprising to have the recipe for assembling this one file defined in a separate file.
I think the check then needs to write to a file... something like this?
$(platform_build_dir)/firmware/fw_payload.o: $(FW_FDT_PATH) $(platform_build_dir)/firmware/fw_payload.o: $(FW_PAYLOAD_PATH_FINAL) $(platform_build_dir)/firmware/fw_payload.o:
$(platform_build_dir)/firmware/payload_check.stamp $(platform_build_dir)/firmware/payload_check.stamp: $(FW_PAYLOAD_PATH_FINAL)
@scripts/check-payload.sh $<
@touch $@Quoted 1 line
This can be done portably with od(1):
Ah cool, I didn't know about od (and googling everyone uses xxi and hacky sed pipelines), thanks!
Quoted 1 line
No need for an explicit "exit 0" at the end.
Actually there is because the exit code will be the value from the last command, which is false with the way I've written the script. Try this script:
#!/bin/bash
false && {
echo "Failed"
exit 1
}
It will not print "Failed" and it will also exit with a failure exit code. I have change the script so it's like this:
true || { ... fail ... }
But maybe it's worth keeping the explicit exit 0 just because shell is crazy like this?
PATCH 1/1 Makefile: perform sanity checks on payload during build
Hi Tim,
Quoted 6 lines · Tim Hutt
Quoted 3 lines
I would suggest leaving the recipe for fw_payload.o alone, and using a separate, possibly phony, target for this check. It's surprising to have the recipe for assembling this one file defined in a separate file.
I think the check then needs to write to a file... something like this?
Quoted 7 lines
$(platform_build_dir)/firmware/fw_payload.o: $(FW_FDT_PATH) $(platform_build_dir)/firmware/fw_payload.o: $(FW_PAYLOAD_PATH_FINAL) $(platform_build_dir)/firmware/fw_payload.o:
$(platform_build_dir)/firmware/payload_check.stamp $(platform_build_dir)/firmware/payload_check.stamp: $(FW_PAYLOAD_PATH_FINAL)@scripts/check-payload.sh $< @touch $@
Yes, that would work.
Quoted 17 lines
Quoted 1 line
This can be done portably with od(1):
Ah cool, I didn't know about
od(and googling everyone usesxxiand hacky sed pipelines), thanks!Quoted 1 line
No need for an explicit "exit 0" at the end.
Actually there is because the exit code will be the value from the last command, which is false with the way I've written the script. Try this script:
#!/bin/bash
false && {
echo "Failed"
exit 1
}
Quoted 7 lines
It will not print "Failed" and it will also exit with a failure exit code. I have change the script so it's like this:
true || { ... fail ... }
But maybe it's worth keeping the explicit exit 0 just because shell is crazy like this?
You're right. Sorry for the misinformation. Keeping the exit 0 is fine, or you could put the touch command inside the script, so the last command is something that should succeed.
Regards,
Samuel