Skip to content

Commit

Permalink
hw/riscv: implement -bios and -kernel support for the ESP32-C3 to rep…
Browse files Browse the repository at this point in the history
…lace the ROM binary
  • Loading branch information
o-marshmallow committed May 27, 2024
1 parent d6aa0b0 commit 7bb43fd
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions hw/riscv/esp32c3.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "sysemu/runstate.h"
#include "sysemu/reset.h"
#include "net/net.h"
#include "elf.h"
#include "hw/misc/esp32c3_reg.h"
#include "hw/misc/esp32c3_rtc_cntl.h"
#include "hw/misc/esp32c3_cache.h"
Expand Down Expand Up @@ -582,21 +583,47 @@ static void esp32c3_machine_init(MachineState *machine)
memory_region_add_subregion_overlap(sys_mem, esp32c3_memmap[ESP32C3_MEMREGION_FRAMEBUF].base, &ms->rgb.vram, 0);
}

/* Open and load the "bios", which is the ROM binary, also named "first stage bootloader" */
char *rom_binary = qemu_find_file(QEMU_FILE_TYPE_BIOS, "esp32c3-rom.bin");
if (rom_binary == NULL) {
error_report("Error: -bios argument not set, and ROM code binary not found (1)");
exit(1);

const char *bios_filename = NULL;

if (machine->firmware) {
bios_filename = machine->firmware;
}

/* Load ROM file at the reset address */
int size = load_image_targphys_as(rom_binary, ESP32C3_RESET_ADDRESS, 0x60000, CPU(&ms->soc)->as);
if (size < 0) {
error_report("Error: could not load ROM binary '%s'", rom_binary);
exit(1);
if (machine->kernel_filename) {
if (bios_filename) {
qemu_log("Warning: both -bios and -kernel arguments specified. Only loading the the -kernel file.\n");
}
bios_filename = machine->kernel_filename;
}

g_free(rom_binary);
if (bios_filename) {
/* Since EspRISCVCPU doens't have a RISCVHartArrayState field, let's bake one on the stack. It will only be
* used to get the type of the RISC-V CPU (32 or 64 bits) in `riscv_load_kernel` */
RISCVHartArrayState hart = {
.harts = &ms->soc.parent_obj,
.num_harts = 1,
};

/* On failure, riscv_load_kernel exits the program */
riscv_load_kernel(machine, &hart, ESP32C3_RESET_ADDRESS, false, NULL);
} else {
/* Open and load the "bios", which is the ROM binary, also named "first stage bootloader" */
char *rom_binary = qemu_find_file(QEMU_FILE_TYPE_BIOS, "esp32c3-rom.bin");
if (rom_binary == NULL) {
error_report("Error: -bios argument not set, and ROM code binary not found (1)");
exit(1);
}

/* Load ROM file at the reset address */
int size = load_image_targphys_as(rom_binary, ESP32C3_RESET_ADDRESS, 0x60000, CPU(&ms->soc)->as);
if (size < 0) {
error_report("Error: could not load ROM binary '%s'", rom_binary);
exit(1);
}

g_free(rom_binary);
}
}


Expand Down

0 comments on commit 7bb43fd

Please sign in to comment.