Skip to content

Commit

Permalink
Allow to modify bootargs for system emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
RinHizakura committed Jan 7, 2025
1 parent 496f408 commit 4c73b1a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
path = tests/quake
url = https://github.com/sysprog21/quake-embedded
shallow = true
[submodule "src/dtc"]
path = src/dtc
url = https://git.kernel.org/pub/scm/utils/dtc/dtc.git
shallow = true
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ $(OBJS): $(GDBSTUB_LIB)
endif

$(OUT)/%.o: src/%.c $(deps_emcc)
$(Q)mkdir -p $(shell dirname $@)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_emcc) -c -MMD -MF $@.d $<

Expand Down
4 changes: 4 additions & 0 deletions mk/system.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Peripherals for system emulation
ifeq ($(call has, SYSTEM), 1)

CFLAGS += -Isrc/dtc/libfdt
LIBFDT_HACK := $(shell git submodule update --init src/dtc)

DEV_SRC := src/devices

DTC ?= dtc
Expand All @@ -25,6 +28,7 @@ DEV_OBJS := $(patsubst $(DEV_SRC)/%.c, $(DEV_OUT)/%.o, $(wildcard $(DEV_SRC)/*.c
deps := $(DEV_OBJS:%.o=%.o.d)

OBJS_EXT += system.o
OBJS_EXT += dtc/libfdt/fdt.o dtc/libfdt/fdt_ro.o dtc/libfdt/fdt_rw.o

# system target execution by using default dependencies
LINUX_IMAGE_DIR := linux-image
Expand Down
9 changes: 8 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static char *opt_prog_name;
/* target argc and argv */
static int prog_argc;
static char **prog_args;
static const char *optstr = "tgqmhpd:a:k:i:";
static const char *optstr = "tgqmhpd:a:k:i:b:";

/* enable misaligned memory access */
static bool opt_misaligned = false;
Expand All @@ -55,6 +55,7 @@ static char *prof_out_file;
/* Linux kernel data */
static char *opt_kernel_img;
static char *opt_rootfs_img;
static char *opt_bootargs;
#endif

static void print_usage(const char *filename)
Expand All @@ -72,6 +73,7 @@ static void print_usage(const char *filename)
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
" -k <image> : use <image> as kernel image\n"
" -i <image> : use <image> as rootfs\n"
" -b <bootargs> : use customized <bootargs> for the kernel\n"
#endif
" -d [filename]: dump registers as JSON to the "
"given file or `-` (STDOUT)\n"
Expand Down Expand Up @@ -112,6 +114,10 @@ static bool parse_args(int argc, char **args)
opt_rootfs_img = optarg;
emu_argc++;
break;
case 'b':
opt_bootargs = optarg;
emu_argc++;
break;
#endif
case 'q':
opt_quiet_outputs = true;
Expand Down Expand Up @@ -257,6 +263,7 @@ int main(int argc, char **args)
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
attr.data.system.kernel = opt_kernel_img;
attr.data.system.initrd = opt_rootfs_img;
attr.data.system.bootargs = opt_bootargs;
#else
attr.data.user.elf_program = opt_prog_name;
#endif
Expand Down
59 changes: 55 additions & 4 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
#include <termios.h>
#include "dtc/libfdt/libfdt.h"
#endif

#if !defined(_WIN32) && !defined(_WIN64)
Expand Down Expand Up @@ -259,11 +260,61 @@ static void map_file(char **ram_loc, const char *name)
exit(EXIT_FAILURE);
}

static void load_dtb(char **ram_loc)
#define ALIGN_FDT(x) (((x) + (FDT_TAGSIZE) - 1) & ~((FDT_TAGSIZE) - 1))
static char *realloc_property(char *fdt,
int nodeoffset,
const char *name,
int newlen)
{
int delta = 0;
int oldlen = 0;

if (!fdt_get_property(fdt, nodeoffset, name, &oldlen))
/* strings + property header */
delta = sizeof(struct fdt_property) + strlen(name) + 1;

if (newlen > oldlen)
/* actual value in off_struct */
delta += ALIGN_FDT(newlen) - ALIGN_FDT(oldlen);

int new_sz = fdt_totalsize(fdt) + delta;
/* Assume the pre-allocated RAM is enough here, so we
* don't realloc any memory for fdt */
fdt_open_into(fdt, fdt, new_sz);
return fdt;
}

static void load_dtb(char **ram_loc, char *bootargs)
{
#include "minimal_dtb.h"
memcpy(*ram_loc, minimal, sizeof(minimal));
*ram_loc += sizeof(minimal);
char *blob = *ram_loc;
char *buf;
size_t len;
int node, err;
int totalsize;

memcpy(blob, minimal, sizeof(minimal));

if (bootargs) {
node = fdt_path_offset(blob, "/chosen");
assert(node > 0);

len = strlen(bootargs) + 1;
buf = malloc(len);
assert(buf);
memcpy(buf, bootargs, len - 1);
buf[len] = 0;
err = fdt_setprop(blob, node, "bootargs", buf, len + 1);
if (err == -FDT_ERR_NOSPACE) {
blob = realloc_property(blob, node, "bootargs", len);
err = fdt_setprop(blob, node, "bootargs", buf, len);
}
free(buf);
assert(!err);
}

totalsize = fdt_totalsize(blob);
*ram_loc += totalsize;
return;
}

Expand Down Expand Up @@ -418,7 +469,7 @@ riscv_t *rv_create(riscv_user_t rv_attr)

uint32_t dtb_addr = attr->mem->mem_size - (1 * 1024 * 1024);
ram_loc = ((char *) attr->mem->mem_base) + dtb_addr;
load_dtb(&ram_loc);
load_dtb(&ram_loc, attr->data.system.bootargs);
/*
* Load optional initrd image at last 8 MiB before the dtb region to
* prevent kernel from overwritting it
Expand Down
1 change: 1 addition & 0 deletions src/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ typedef struct {
typedef struct {
char *kernel;
char *initrd;
char *bootargs;
} vm_system_t;
#endif /* RV32_HAS(SYSTEM) */

Expand Down

0 comments on commit 4c73b1a

Please sign in to comment.