Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to build my own payload ? #196

Open
DragonsAshes opened this issue Mar 3, 2021 · 1 comment
Open

How to build my own payload ? #196

DragonsAshes opened this issue Mar 3, 2021 · 1 comment

Comments

@DragonsAshes
Copy link

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?

@tswaehn
Copy link

tswaehn commented Aug 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants