opensbi icon indicating copy to clipboard operation
opensbi copied to clipboard

How to build my own payload ?

Open DragonsAshes opened this issue 4 years ago • 1 comments

I would like to generate my own binary, which I would then like to run using the FW_PAYLOAD_PATH option. When I use the payload generated by default, the program runs correctly. On the other hand, if I use a binary that I compiled myself, it doesn't work. How can we compile our binary so that it works as a payload?

DragonsAshes avatar Mar 03 '21 12:03 DragonsAshes

Thats the way I put my own binary in qemu:

build openSBI for qemu-system-riscv32 (note for qemu-system-riscv64 the addresses are slightly different)

make PLATFORM=generic CROSS_COMPILE=riscv32-unknown-elf- PLATFORM_RISCV_XLEN=32 FW_TEXT_START=0x80000000 FW_JUMP_ADDR=0x80400000 FW_JUMP_FDT_ADDR=0x80800000

very simple hello.S custom binary

.section .text
.globl _start

_start:
    la t0, hello_string             # Load address of the string into t0
    call print_loop

loop:
    j loop


print_loop:
    lb a0, 0(t0)              # Load the byte at the address in t0 (current character)
    beqz a0, print_loop_end   # If the byte is zero (end of string), jump to end
    li a7, 1                  # Set a7 = 1 ... use putchar
    li a6, 1                  # Set a6 = 1
    ecall                     # Trigger the ecall
    addi t0, t0, 1            # Increment t0 to point to the next character
    j print_loop              # Repeat the loop
print_loop_end:
    ret


hello_string:
    .asciz "\n---hello from supervisor mode---\n"      # Define the string "hello world" in the .text section

and linker file link.ld

/* link.ld */
ENTRY(_start)
SECTIONS
{
    . = 0x80400000;
    .text : {
        *(.text)
    }
    .rodata : {
        *(.rodata)
    }
    .data : {
        *(.data)
    }
    .bss : {
        *(.bss)
    }
}

then I build it with my toolchain

riscv32-unknown-elf-as -o hello.o hello.S
riscv32-unknown-elf-ld -T link.ld -o hello.elf hello.o
riscv32-unknown-elf-objcopy -O binary hello.elf hello.bin

and finally run in qemu

qemu-system-riscv32 -machine virt -nographic -m 128M -bios build/platform/generic/firmware/fw_jump.bin -kernel hello.bin

tswaehn avatar Aug 17 '24 07:08 tswaehn