From 106e3eba38979e3cbfec333e3badbeebb4da717d Mon Sep 17 00:00:00 2001 From: Rupert Swarbrick Date: Fri, 18 Feb 2022 11:50:54 +0000 Subject: Add simple error checking to DTB parsing code This catches silly mistakes like accidentally passing a DTS file when it should have been a DTB. Now, you get something like this: $ /opt/spike/latest/bin/spike --dtb=bogus.dtb -l obj.o Failed to read DTB from `bogus.dtb': FDT_ERR_BADMAGIC. --- riscv/sim.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/riscv/sim.cc b/riscv/sim.cc index 97b3746..2ee05d6 100644 --- a/riscv/sim.cc +++ b/riscv/sim.cc @@ -6,6 +6,7 @@ #include "remote_bitbang.h" #include "byteorder.h" #include "platform.h" +#include "libfdt.h" #include #include #include @@ -296,6 +297,18 @@ void sim_t::make_dtb() dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, initrd_start, initrd_end, bootargs, procs, mems); dtb = dts_compile(dts); } + + int fdt_code = fdt_check_header(dtb.c_str()); + if (fdt_code) { + std::cerr << "Failed to read DTB from "; + if (dtb_file.empty()) { + std::cerr << "auto-generated DTS string"; + } else { + std::cerr << "`" << dtb_file << "'"; + } + std::cerr << ": " << fdt_strerror(fdt_code) << ".\n"; + exit(-1); + } } void sim_t::set_rom() -- cgit v1.1