Skip to content

Commit

Permalink
Allow loading more than one ELF binary
Browse files Browse the repository at this point in the history
This makes it easier to use a separate M-mode bootloader and kernel
payload (e.g. OpenSBI fw_jump). It also makes it easier to test booting
systems such as FreeBSD without bundling the kernel with the bootloader.
  • Loading branch information
arichardson committed Aug 3, 2023
1 parent 1aa2e89 commit a2c0389
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions c_emulator/riscv_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,9 @@ static struct option options[] = {

static void print_usage(const char *argv0, int ec)
{
fprintf(stdout, "Usage: %s [options] <elf_file> [<elf_file> ...]\n", argv0);
#ifdef RVFI_DII
fprintf(stdout,
"Usage: %s [options] <elf_file>\n %s [options] -r <port>\n",
argv0, argv0);
#else
fprintf(stdout, "Usage: %s [options] <elf_file>\n", argv0);
fprintf(stdout, " %s [options] -r <port>\n", argv0);
#endif
struct option *opt = options;
while (opt->name) {
Expand Down Expand Up @@ -218,8 +215,11 @@ static void read_dtb(const char *path)
}

/**
* Parses the command line arguments and returns the argv index for the ELF file
* that should be loaded.
* Parses the command line arguments and returns the argv index for the first
* ELF file that should be loaded. As getopt transforms the argv array, all
* argv values following that index are non-options and can be treated as
* additional ELF files that should be loaded into memory (but not scanned
* for the magic tohost/{begin,end}_signature symbols).
*/
static int process_args(int argc, char **argv)
{
Expand Down Expand Up @@ -1027,7 +1027,7 @@ int main(int argc, char **argv)
preinit_sail();

int files_start = process_args(argc, argv);
char *file = argv[files_start];
char *initial_elf_file = argv[files_start];
init_logs();

if (gettimeofday(&init_start, NULL) < 0) {
Expand Down Expand Up @@ -1093,15 +1093,29 @@ int main(int argc, char **argv)
}
printf("Connected\n");
} else
entry = load_sail(file);
entry = load_sail(initial_elf_file);
#else
uint64_t entry = load_sail(file);
uint64_t entry = load_sail(initial_elf_file);
#endif
/* Load any additional ELF files into memory */
for (int i = files_start + 1; i < argc; i++) {
bool is32bit;
uint64_t additional_entry;
fprintf(stdout, "Loading additional ELF file %s.\n", argv[i]);
/*
* We use load_elf() directly instead of load_sail() as we do not want to
* override tohost/{begin,end}_signature from the initial ELF file.
*/
load_elf(argv[i], &is32bit, &additional_entry);
check_elf(is32bit);
fprintf(stdout, "Additional ELF %d Entry @ 0x%" PRIx64 "\n",
i - files_start, additional_entry);
}

/* initialize spike before sail so that we can access the device-tree blob,
* until we roll our own.
*/
init_spike(file, entry, rv_ram_size);
init_spike(initial_elf_file, entry, rv_ram_size);
init_sail(entry);

if (!init_check(s))
Expand Down

0 comments on commit a2c0389

Please sign in to comment.