Skip to content

Commit 9df29e0

Browse files
committed
Allow to modify bootargs for system emulation
1 parent 2656699 commit 9df29e0

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434
[submodule "tools/bin_to_c"]
3535
path = tools/bin_to_c
3636
url = https://github.com/bitbank2/bin_to_c.git
37+
[submodule "src/dtc"]
38+
path = src/dtc
39+
url = https://git.kernel.org/pub/scm/utils/dtc/dtc.git

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ $(OBJS): $(GDBSTUB_LIB)
269269
endif
270270

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

mk/system.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Peripherals for system emulation
22
ifeq ($(call has, SYSTEM), 1)
33

4+
CFLAGS += -Isrc/dtc/libfdt
5+
46
DEV_SRC := src/devices
57

68
DTC ?= dtc
@@ -30,6 +32,11 @@ DEV_OBJS := $(patsubst $(DEV_SRC)/%.c, $(DEV_OUT)/%.o, $(wildcard $(DEV_SRC)/*.c
3032
deps := $(DEV_OBJS:%.o=%.o.d)
3133

3234
OBJS_EXT += system.o
35+
FDT_OBJS := fdt.o fdt_ro.o fdt_rw.o
36+
OBJS_EXT +=$(addprefix dtc/libfdt/,$(FDT_OBJS))
37+
38+
src/dtc/libfdt/%.c:
39+
git submodule update --init src/dtc
3340

3441
# system target execution by using default dependencies
3542
LINUX_IMAGE_DIR := linux-image

src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static char *opt_prog_name;
4242
/* target argc and argv */
4343
static int prog_argc;
4444
static char **prog_args;
45-
static const char *optstr = "tgqmhpd:a:k:i:";
45+
static const char *optstr = "tgqmhpd:a:k:i:b:";
4646

4747
/* enable misaligned memory access */
4848
static bool opt_misaligned = false;
@@ -55,6 +55,7 @@ static char *prof_out_file;
5555
/* Linux kernel data */
5656
static char *opt_kernel_img;
5757
static char *opt_rootfs_img;
58+
static char *opt_bootargs;
5859
#endif
5960

6061
static void print_usage(const char *filename)
@@ -72,6 +73,7 @@ static void print_usage(const char *filename)
7273
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
7374
" -k <image> : use <image> as kernel image\n"
7475
" -i <image> : use <image> as rootfs\n"
76+
" -b <bootargs> : use customized <bootargs> for the kernel\n"
7577
#endif
7678
" -d [filename]: dump registers as JSON to the "
7779
"given file or `-` (STDOUT)\n"
@@ -112,6 +114,10 @@ static bool parse_args(int argc, char **args)
112114
opt_rootfs_img = optarg;
113115
emu_argc++;
114116
break;
117+
case 'b':
118+
opt_bootargs = optarg;
119+
emu_argc++;
120+
break;
115121
#endif
116122
case 'q':
117123
opt_quiet_outputs = true;
@@ -257,6 +263,7 @@ int main(int argc, char **args)
257263
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
258264
attr.data.system.kernel = opt_kernel_img;
259265
attr.data.system.initrd = opt_rootfs_img;
266+
attr.data.system.bootargs = opt_bootargs;
260267
#else
261268
attr.data.user.elf_program = opt_prog_name;
262269
#endif

src/riscv.c

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
1616
#include <termios.h>
17+
#include "dtc/libfdt/libfdt.h"
1718
#endif
1819

1920
#if !defined(_WIN32) && !defined(_WIN64)
@@ -259,11 +260,59 @@ static void map_file(char **ram_loc, const char *name)
259260
exit(EXIT_FAILURE);
260261
}
261262

262-
static void load_dtb(char **ram_loc)
263+
#define ALIGN_FDT(x) (((x) + (FDT_TAGSIZE) - 1) & ~((FDT_TAGSIZE) - 1))
264+
static char *realloc_property(char *fdt,
265+
int nodeoffset,
266+
const char *name,
267+
int newlen)
268+
{
269+
/* FIXME: assume RAM is enough */
270+
int delta = 0;
271+
int oldlen = 0;
272+
273+
if (!fdt_get_property(fdt, nodeoffset, name, &oldlen))
274+
/* strings + property header */
275+
delta = sizeof(struct fdt_property) + strlen(name) + 1;
276+
277+
if (newlen > oldlen)
278+
/* actual value in off_struct */
279+
delta += ALIGN_FDT(newlen) - ALIGN_FDT(oldlen);
280+
281+
int new_sz = fdt_totalsize(fdt) + delta;
282+
fdt_open_into(fdt, fdt, new_sz);
283+
return fdt;
284+
}
285+
286+
static void load_dtb(char **ram_loc, char *bootargs)
263287
{
264288
#include "minimal_dtb.h"
265-
memcpy(*ram_loc, minimal, sizeof(minimal));
266-
*ram_loc += sizeof(minimal);
289+
char *blob = *ram_loc;
290+
char *buf;
291+
size_t len;
292+
int node, err;
293+
int totalsize;
294+
295+
memcpy(blob, minimal, sizeof(minimal));
296+
297+
if (bootargs) {
298+
node = fdt_path_offset(blob, "/chosen");
299+
assert(node > 0);
300+
301+
len = strlen(bootargs) + 1;
302+
buf = malloc(len);
303+
memcpy(buf, bootargs, len - 1);
304+
buf[len] = 0;
305+
err = fdt_setprop(blob, node, "bootargs", buf, len + 1);
306+
if (err == -FDT_ERR_NOSPACE) {
307+
blob = realloc_property(blob, node, "bootargs", len);
308+
err = fdt_setprop(blob, node, "bootargs", buf, len);
309+
}
310+
free(buf);
311+
assert(!err);
312+
}
313+
314+
totalsize = fdt_totalsize(blob);
315+
*ram_loc += totalsize;
267316
return;
268317
}
269318

@@ -418,7 +467,7 @@ riscv_t *rv_create(riscv_user_t rv_attr)
418467

419468
uint32_t dtb_addr = attr->mem->mem_size - (1 * 1024 * 1024);
420469
ram_loc = ((char *) attr->mem->mem_base) + dtb_addr;
421-
load_dtb(&ram_loc);
470+
load_dtb(&ram_loc, attr->data.system.bootargs);
422471
/*
423472
* Load optional initrd image at last 8 MiB before the dtb region to
424473
* prevent kernel from overwritting it

src/riscv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ typedef struct {
551551
typedef struct {
552552
char *kernel;
553553
char *initrd;
554+
char *bootargs;
554555
} vm_system_t;
555556
#endif /* RV32_HAS(SYSTEM) */
556557

0 commit comments

Comments
 (0)