From 08cf745aaab48d67fafc995663ec96757ff6b816 Mon Sep 17 00:00:00 2001 From: Anup Patel Date: Fri, 14 Feb 2020 11:31:38 +0530 Subject: Make spike capable of booting Linux Latest Linux does not boot Spike mainly because: 1. Spike does not set bootargs in DTS 2. Spike does not provide mechanism to load initrd for Linux This patch addresses both above issues and we can now get latest Linux to prompt on Spike. Signed-off-by: Anup Patel Signed-off-by: Chih-Min Chao --- spike_main/spike.cc | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'spike_main') diff --git a/spike_main/spike.cc b/spike_main/spike.cc index 5a79973..f56337c 100644 --- a/spike_main/spike.cc +++ b/spike_main/spike.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include "../VERSION" static void help(int exit_code = 1) @@ -52,6 +53,7 @@ static void help(int exit_code = 1) fprintf(stderr, " --rbb-port= Listen on for remote bitbang connection\n"); fprintf(stderr, " --dump-dts Print device tree string and exit\n"); fprintf(stderr, " --disable-dtb Don't write the device tree blob into memory\n"); + fprintf(stderr, " --initrd= Load kernel initrd into memory\n"); fprintf(stderr, " --dm-progsize= Progsize for the debug module [default 2]\n"); fprintf(stderr, " --dm-sba= Debug bus master supports up to " " wide accesses [default 0]\n"); @@ -73,6 +75,26 @@ static void suggest_help() exit(1); } +static bool check_file_exists(const char *fileName) +{ + std::ifstream infile(fileName); + return infile.good(); +} + +static std::ifstream::pos_type get_file_size(const char *filename) +{ + std::ifstream in(filename, std::ios::ate | std::ios::binary); + return in.tellg(); +} + +static void read_file_bytes(const char *filename,size_t fileoff, + char *read_buf, size_t read_sz) +{ + std::ifstream in(filename, std::ios::in | std::ios::binary); + in.seekg(fileoff, std::ios::beg); + in.read(read_buf, read_sz); +} + static std::vector> make_mems(const char* arg) { // handle legacy mem argument @@ -125,6 +147,8 @@ int main(int argc, char** argv) bool dump_dts = false; bool dtb_enabled = true; size_t nprocs = 1; + size_t initrd_size; + reg_t initrd_start = 0, initrd_end = 0; reg_t start_pc = reg_t(-1); std::vector> mems; std::vector> plugin_devices; @@ -134,6 +158,7 @@ int main(int argc, char** argv) bool log_cache = false; bool log_commits = false; std::function extension; + const char* initrd = NULL; const char* isa = DEFAULT_ISA; const char* priv = DEFAULT_PRIV; const char* varch = DEFAULT_VARCH; @@ -246,6 +271,7 @@ int main(int argc, char** argv) #endif parser.option(0, "vector-mistrap", 0, [&](const char *s){g_vector_mistrap = true;}); parser.option(0, "disable-dtb", 0, [&](const char *s){dtb_enabled = false;}); + parser.option(0, "initrd", 1, [&](const char* s){initrd = s;}); parser.option(0, "extlib", 1, [&](const char *s){ void *lib = dlopen(s, RTLD_NOW | RTLD_GLOBAL); if (lib == NULL) { @@ -283,7 +309,19 @@ int main(int argc, char** argv) atexit(exit_with_unsupport); #endif - sim_t s(isa, priv, varch, nprocs, halted, start_pc, mems, plugin_devices, htif_args, + if (initrd && check_file_exists(initrd)) { + initrd_size = get_file_size(initrd); + for (auto& m : mems) { + if (initrd_size && (initrd_size + 0x1000) < m.second->size()) { + initrd_end = m.first + m.second->size() - 0x1000; + initrd_start = initrd_end - initrd_size; + read_file_bytes(initrd, 0, m.second->contents() + (initrd_start - m.first), initrd_size); + break; + } + } + } + + sim_t s(isa, priv, varch, nprocs, halted, initrd_start, initrd_end, start_pc, mems, plugin_devices, htif_args, std::move(hartids), dm_config); std::unique_ptr remote_bitbang((remote_bitbang_t *) NULL); std::unique_ptr jtag_dtm( -- cgit v1.1