Skip to content

Commit 4b25318

Browse files
opensbi: optionally support fw_payload
Current cheribuild only uses the fw_jump variant of OpenSBI while there exists other variants as well such as fw_payload that could be used. This commit enables choosing the payload variant of opensbi (which could embed u-boot and/or other OSes) and install it as an optional (QEMU's) firmware variant besides fw_jump.
1 parent ef97ca0 commit 4b25318

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

pycheribuild/projects/cross/opensbi.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,50 @@ def install(self, **kwargs):
148148
args.set(PLATFORM=platform)
149149
self.run_make_install(cwd=self.source_dir, options=args)
150150
# Only install BuildOpenSBI as the QEMU bios and not the GFE version by checking build_dir_suffix
151-
if self.crosscompile_target.is_hybrid_or_purecap_cheri() and not self.build_dir_suffix:
151+
if not self.build_dir_suffix:
152152
# Install into the QEMU firware directory so that `-bios default` works
153153
qemu_fw_install_bin = self._qemu_fw_install_path()
154154
self.makedirs(qemu_fw_install_bin.parent)
155155
abi = self.target_info.get_riscv_abi(self.crosscompile_target, softfloat=True)
156156
fw_elf = self.install_dir / f"share/opensbi/{abi}/generic/firmware/fw_jump.elf"
157+
# If OpenSBI is built with a payload (eg u-boot), use fw_payload.elf
158+
fw_payload_elf = self.install_dir / f"share/opensbi/{abi}/generic/firmware/fw_payload.elf"
157159
# TODO: looks like newer versions install a .bin that we could just copy instead.
158160
self.run_cmd(
159161
[self.sdk_bindir / "llvm-objcopy", "-S", "-O", "binary", fw_elf, qemu_fw_install_bin],
160162
print_verbose_only=False,
161163
)
162164

163-
def _qemu_fw_install_path(self) -> Path:
165+
# Install the fw_payload version besides the fw_jump one and let run-time flags and/or
166+
# projects choose which variant to use.
167+
self.run_cmd(
168+
[
169+
self.sdk_bindir / "llvm-objcopy",
170+
"-S",
171+
"-O",
172+
"binary",
173+
fw_payload_elf,
174+
self._qemu_fw_install_path(is_payload=True),
175+
],
176+
print_verbose_only=False,
177+
)
178+
179+
def _qemu_fw_install_path(self, is_payload=False) -> Path:
164180
qemu_fw_dir = self._qemu_install_dir() / "share/qemu/"
165181
suffix = ""
182+
fw_type = "jump"
183+
184+
if is_payload:
185+
fw_type = "payload"
186+
166187
if self.crosscompile_target.is_cheri_purecap():
167188
suffix = "cheri"
168189
if self.crosscompile_target.is_experimental_cheri093_std(self.config):
169190
suffix += "std"
170-
return qemu_fw_dir / f"opensbi-riscv64{suffix}-generic-fw_jump.bin"
191+
return qemu_fw_dir / f"opensbi-riscv64{suffix}-generic-fw_{fw_type}.bin"
171192

172-
def _fw_jump_path(self) -> Path:
173-
return self._qemu_fw_install_path()
193+
def _fw_path(self, is_payload=False) -> Path:
194+
return self._qemu_fw_install_path(is_payload)
174195

175196
def _qemu_install_dir(self) -> Path:
176197
return BuildQEMU.get_install_dir(self, cross_target=CompilationTargets.NATIVE)
@@ -186,15 +207,15 @@ def get_hybrid_instance(cls, caller, cpu_arch=CPUArchitecture.RISCV64) -> "Build
186207
return cls.get_instance(caller, cross_target=CompilationTargets.FREESTANDING_RISCV64_HYBRID)
187208

188209
@classmethod
189-
def get_nocap_bios(cls, caller, xtarget: CrossCompileTarget) -> Path:
210+
def get_nocap_bios(cls, caller, xtarget: CrossCompileTarget, is_payload=False) -> Path:
190211
assert xtarget.is_riscv64(include_purecap=True), "RV32 not supported yet"
191-
return cls.get_nocap_instance(caller)._fw_jump_path()
212+
return cls.get_nocap_instance(caller)._fw_path(is_payload)
192213

193214
@classmethod
194-
def get_cheri_bios(cls, caller, xtarget: CrossCompileTarget):
215+
def get_cheri_bios(cls, caller, xtarget: CrossCompileTarget, is_payload=False):
195216
assert xtarget.is_riscv64(include_purecap=True), "RV32 not supported yet"
196217
# We currently use a hybrid build for ISAv9
197-
return cls.get_hybrid_instance(caller)._fw_jump_path()
218+
return cls.get_hybrid_instance(caller)._fw_path(is_payload)
198219

199220
def run_tests(self):
200221
options = QemuOptions(
@@ -274,7 +295,7 @@ def all_platforms(self):
274295
return ["generic"]
275296

276297
@classmethod
277-
def get_cheri_bios(cls, caller, xtarget: CrossCompileTarget):
298+
def get_cheri_bios(cls, caller, xtarget: CrossCompileTarget, is_payload=False):
278299
assert xtarget.is_riscv(include_purecap=True), "Should only call this for RISC-V"
279300
if xtarget.is_riscv32(include_purecap=True):
280301
bios_xtarget = CompilationTargets.FREESTANDING_RISCV32_PURECAP_093
@@ -283,7 +304,7 @@ def get_cheri_bios(cls, caller, xtarget: CrossCompileTarget):
283304
# This version of OpenSBI requires a purecap build to support CHERI
284305
proj = cls.get_instance(caller, cross_target=bios_xtarget)
285306
assert isinstance(proj, BuildOpenSBI)
286-
return proj._fw_jump_path()
307+
return proj._fw_path(is_payload)
287308

288309

289310
class BuildAllianceOpenSBIGFE(BuildAllianceOpenSBI):

0 commit comments

Comments
 (0)