Skip to content
This repository has been archived by the owner on Apr 13, 2019. It is now read-only.

Commit

Permalink
RISC-V: support loading binary files in addition to ELF files
Browse files Browse the repository at this point in the history
The kernel and bootloader are not always available as ELF files, or
directly executable. For example, the kernel might be included together
with a device tree inside of an image. Support this use case by adding
support for loading binary files in addition to ELF files.

With this patch, the files specified with the -bios and -kernel
parameters are attempted to be loaded as ELF files first. If this fails,
they are loaded directly to memory instead.

Signed-off-by: Lukas Auer <lukas.auer@aisec.fraunhofer.de>
  • Loading branch information
lukasauer committed Oct 19, 2018
1 parent 1431b8b commit feaccdf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
50 changes: 40 additions & 10 deletions hw/riscv/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ static uint64_t kernel_translate(void *opaque, uint64_t addr)
}
}

hwaddr riscv_load_firmware(const char *filename)
hwaddr riscv_load_firmware(const char *filename, hwaddr ram_start,
uint64_t ram_size)
{
uint64_t firmware_entry, firmware_start, firmware_end;
int64_t size;

if (load_elf(filename, NULL, NULL,
&firmware_entry, &firmware_start, &firmware_end,
0, EM_RISCV, 1, 0) < 0) {
error_report("riscv_boot: could not load firmware '%s'", filename);
exit(1);
firmware_entry = ram_start;
firmware_start = ram_start;

size = load_image_targphys(filename, firmware_start, ram_size);
if (size < 0) {
error_report("riscv_boot: could not load firmware '%s'", filename);
exit(1);
}

firmware_end = firmware_start + size;
}

/* align kernel load address to the megapage after the firmware */
Expand All @@ -71,15 +81,31 @@ hwaddr riscv_load_firmware(const char *filename)
return firmware_entry;
}

hwaddr riscv_load_kernel(const char *filename, void *fdt)
hwaddr riscv_load_kernel(const char *filename, void *fdt, hwaddr ram_start,
uint64_t ram_size)
{
uint64_t kernel_entry, kernel_start, kernel_end;
int64_t size;

if (load_elf(filename, kernel_translate, NULL,
&kernel_entry, &kernel_start, &kernel_end,
0, EM_RISCV, 1, 0) < 0) {
error_report("riscv_boot: could not load kernel '%s'", filename);
exit(1);
if (kernel_offset) {
kernel_entry = kernel_offset;
kernel_start = kernel_offset;
ram_size -= kernel_offset - ram_start;
} else {
kernel_entry = ram_start;
kernel_start = ram_start;
}

size = load_image_targphys(filename, kernel_start, ram_size);
if (size < 0) {
error_report("riscv_boot: could not load kernel '%s'", filename);
exit(1);
}

kernel_end = kernel_start + size;
}

boot_debug("entry=0x" TARGET_FMT_plx " start=0x" TARGET_FMT_plx " "
Expand Down Expand Up @@ -143,23 +169,27 @@ void riscv_load_initrd(const char *filename, uint64_t mem_size,
}
}

hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt)
hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt,
hwaddr ram_start)
{
hwaddr firmware_entry = 0;

/* load firmware e.g. -bios bbl */
if (machine->firmware) {
firmware_entry = riscv_load_firmware(machine->firmware);
firmware_entry = riscv_load_firmware(machine->firmware, ram_start,
machine->ram_size);
}

/* load combined bbl+kernel or separate kernel */
if (machine->kernel_filename) {
if (machine->firmware) {
/* load separate bios and kernel e.g. -bios bbl -kernel vmlinux */
riscv_load_kernel(machine->kernel_filename, fdt);
riscv_load_kernel(machine->kernel_filename, fdt, ram_start,
machine->ram_size);
} else {
/* load traditional combined bbl+kernel e.g. -kernel bbl_vmlimux */
firmware_entry = riscv_load_kernel(machine->kernel_filename, NULL);
firmware_entry = riscv_load_kernel(machine->kernel_filename, NULL,
ram_start, machine->ram_size);
}
if (machine->initrd_filename) {
/* load separate initrd */
Expand Down
3 changes: 2 additions & 1 deletion hw/riscv/virt.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ static void riscv_virt_board_init(MachineState *machine)
* separate firmware and kernel: -bios bbl -kernel vmlinux
* firmware, kernel and ramdisk: -bios bbl -kernel vmlinux -initrd initramfs
*/
firmware_entry = riscv_load_firmware_kernel_initrd(machine, fdt);
firmware_entry = riscv_load_firmware_kernel_initrd(machine, fdt,
memmap[VIRT_DRAM].base);

/* reset vector */
uint32_t reset_vec[8] = {
Expand Down
9 changes: 6 additions & 3 deletions include/hw/riscv/boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
#ifndef HW_RISCV_BOOT_H
#define HW_RISCV_BOOT_H

hwaddr riscv_load_firmware(const char *filename);
hwaddr riscv_load_kernel(const char *filename, void *fdt);
hwaddr riscv_load_firmware(const char *filename, hwaddr ram_start,
uint64_t ram_size);
hwaddr riscv_load_kernel(const char *filename, void *fdt, hwaddr ram_start,
uint64_t ram_size);
void riscv_load_initrd(const char *filename, uint64_t mem_size,
hwaddr firmware_entry, void *fdt);
hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt);
hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt,
hwaddr ram_start);

#endif

0 comments on commit feaccdf

Please sign in to comment.