Skip to content

Commit 2b50e1a

Browse files
committed
Allow to modify bootargs for system emulation
1 parent 9246d30 commit 2b50e1a

File tree

6 files changed

+74
-5
lines changed

6 files changed

+74
-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: 8 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
@@ -31,6 +33,12 @@ deps := $(DEV_OBJS:%.o=%.o.d)
3133

3234
OBJS_EXT += system.o
3335

36+
src/fdt/*.c:
37+
git submodule update --init src/dtc
38+
39+
FDT_OBJS := fdt.o fdt_ro.o fdt_rw.o
40+
OBJS_EXT +=$(addprefix dtc/libfdt/,$(FDT_OBJS))
41+
3442
# system target execution by using default dependencies
3543
LINUX_IMAGE_DIR := linux-image
3644
system_action := ($(BIN) -k $(OUT)/$(LINUX_IMAGE_DIR)/Image -i $(OUT)/$(LINUX_IMAGE_DIR)/rootfs.cpio)

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
@@ -16,6 +16,7 @@
1616

1717
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
1818
#include <termios.h>
19+
#include "dtc/libfdt/libfdt.h"
1920
#endif
2021

2122
#if !defined(_WIN32) && !defined(_WIN64)
@@ -261,11 +262,59 @@ static void map_file(char **ram_loc, const char *name)
261262
exit(EXIT_FAILURE);
262263
}
263264

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

@@ -420,7 +469,7 @@ riscv_t *rv_create(riscv_user_t rv_attr)
420469

421470
uint32_t dtb_addr = attr->mem->mem_size - (1 * 1024 * 1024);
422471
ram_loc = ((char *) attr->mem->mem_base) + dtb_addr;
423-
load_dtb(&ram_loc);
472+
load_dtb(&ram_loc, attr->data.system.bootargs);
424473
/*
425474
* Load optional initrd image at last 8 MiB before the dtb region to
426475
* 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)