Skip to content

Commit 2656699

Browse files
committed
Generate DTB in runtime
1 parent 81dd26e commit 2656699

File tree

10 files changed

+39
-19
lines changed

10 files changed

+39
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ build/linux-x86-softfp/
2020
build/riscv32/
2121
build/sail_cSim/
2222
build/sha1sum-*
23+
build/minimal.dtb
2324
*.a
2425
*.o
2526
*.o.d
@@ -29,3 +30,4 @@ tests/arch-test-target/sail_cSim/riscv_sim_RV32
2930
tests/scimark2/
3031
__pycache__/
3132
src/rv32_jit.c
33+
src/minimal_dtb.h

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@
3131
path = tests/quake
3232
url = https://github.com/sysprog21/quake-embedded
3333
shallow = true
34+
[submodule "tools/bin_to_c"]
35+
path = tools/bin_to_c
36+
url = https://github.com/bitbank2/bin_to_c.git

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ include mk/artifact.mk
241241
include mk/wasm.mk
242242
include mk/system.mk
243243

244-
all: config $(BUILD_DTB) $(BIN)
244+
all: config $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
245245

246246
OBJS := \
247247
map.o \
@@ -369,7 +369,7 @@ endif
369369
endif
370370

371371
clean:
372-
$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
372+
$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(BUILD_DTB2C) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
373373
distclean: clean
374374
-$(RM) $(DOOM_DATA) $(QUAKE_DATA) $(BUILDROOT_DATA) $(LINUX_DATA)
375375
$(RM) -r $(OUT)/linux-image

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ $ make ENABLE_SYSTEM=1 system
7777
Build using run using specified images:
7878
```shell
7979
$ make ENABLE_SYSTEM=1
80-
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path> -b <dtb_path>
80+
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path>
8181
```
8282

8383
#### Build Linux image

mk/system.mk

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ ifeq ($(call has, SYSTEM), 1)
44
DEV_SRC := src/devices
55

66
DTC ?= dtc
7-
$(OUT)/minimal.dtb: $(DEV_SRC)/minimal.dts
7+
BUILD_DTB := $(OUT)/minimal.dtb
8+
$(BUILD_DTB): $(DEV_SRC)/minimal.dts
89
$(VECHO) " DTC\t$@\n"
910
$(Q)$(DTC) $^ -o $@
10-
BUILD_DTB := $(OUT)/minimal.dtb
11+
12+
BIN_TO_C_DIR := tools/bin_to_c
13+
$(BIN_TO_C_DIR)/makefile:
14+
git submodule update --init $(BIN_TO_C_DIR)
15+
16+
BIN_TO_C := $(BIN_TO_C_DIR)/bin_to_c
17+
$(BIN_TO_C): $(BIN_TO_C_DIR)/makefile
18+
$(MAKE) -C $(dir $<)
19+
20+
BUILD_DTB2C := src/minimal_dtb.h
21+
$(BUILD_DTB2C): $(BIN_TO_C) $(BUILD_DTB)
22+
$(BIN_TO_C) $(BUILD_DTB) > $(BUILD_DTB2C)
23+
sed -i 's/PROGMEM//g' $(BUILD_DTB2C)
1124

1225
$(DEV_OUT)/%.o: $(DEV_SRC)/%.c $(deps_emcc)
1326
$(Q)mkdir -p $(DEV_OUT)
@@ -20,8 +33,8 @@ OBJS_EXT += system.o
2033

2134
# system target execution by using default dependencies
2235
LINUX_IMAGE_DIR := linux-image
23-
system_action := ($(BIN) -k $(OUT)/$(LINUX_IMAGE_DIR)/Image -i $(OUT)/$(LINUX_IMAGE_DIR)/rootfs.cpio -b $(OUT)/minimal.dtb)
24-
system_deps += artifact $(BUILD_DTB) $(BIN)
36+
system_action := ($(BIN) -k $(OUT)/$(LINUX_IMAGE_DIR)/Image -i $(OUT)/$(LINUX_IMAGE_DIR)/rootfs.cpio)
37+
system_deps += artifact $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
2538
system: $(system_deps)
2639
$(system_action)
2740

src/dtc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 039a99414e778332d8f9c04cbd3072e1dcc62798

src/main.c

Lines changed: 1 addition & 8 deletions
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:b:";
45+
static const char *optstr = "tgqmhpd:a:k:i:";
4646

4747
/* enable misaligned memory access */
4848
static bool opt_misaligned = false;
@@ -55,7 +55,6 @@ 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_dtb;
5958
#endif
6059

6160
static void print_usage(const char *filename)
@@ -73,7 +72,6 @@ static void print_usage(const char *filename)
7372
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
7473
" -k <image> : use <image> as kernel image\n"
7574
" -i <image> : use <image> as rootfs\n"
76-
" -b <dtb> : use <dtb> as device tree blob\n"
7775
#endif
7876
" -d [filename]: dump registers as JSON to the "
7977
"given file or `-` (STDOUT)\n"
@@ -114,10 +112,6 @@ static bool parse_args(int argc, char **args)
114112
opt_rootfs_img = optarg;
115113
emu_argc++;
116114
break;
117-
case 'b':
118-
opt_dtb = optarg;
119-
emu_argc++;
120-
break;
121115
#endif
122116
case 'q':
123117
opt_quiet_outputs = true;
@@ -263,7 +257,6 @@ int main(int argc, char **args)
263257
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
264258
attr.data.system.kernel = opt_kernel_img;
265259
attr.data.system.initrd = opt_rootfs_img;
266-
attr.data.system.dtb = opt_dtb;
267260
#else
268261
attr.data.user.elf_program = opt_prog_name;
269262
#endif

src/riscv.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ static void map_file(char **ram_loc, const char *name)
259259
exit(EXIT_FAILURE);
260260
}
261261

262+
static void load_dtb(char **ram_loc)
263+
{
264+
#include "minimal_dtb.h"
265+
memcpy(*ram_loc, minimal, sizeof(minimal));
266+
*ram_loc += sizeof(minimal);
267+
return;
268+
}
269+
262270
/*
263271
* The control mode flag for keyboard.
264272
*
@@ -294,6 +302,7 @@ static void capture_keyboard_input()
294302
term.c_lflag &= ~TERMIOS_C_CFLAG;
295303
tcsetattr(0, TCSANOW, &term);
296304
}
305+
297306
#endif
298307

299308
/*
@@ -409,7 +418,7 @@ riscv_t *rv_create(riscv_user_t rv_attr)
409418

410419
uint32_t dtb_addr = attr->mem->mem_size - (1 * 1024 * 1024);
411420
ram_loc = ((char *) attr->mem->mem_base) + dtb_addr;
412-
map_file(&ram_loc, attr->data.system.dtb);
421+
load_dtb(&ram_loc);
413422
/*
414423
* Load optional initrd image at last 8 MiB before the dtb region to
415424
* prevent kernel from overwritting it
@@ -517,8 +526,7 @@ void rv_run(riscv_t *rv)
517526
vm_attr_t *attr = PRIV(rv);
518527
assert(attr &&
519528
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
520-
attr->data.system.kernel && attr->data.system.initrd &&
521-
attr->data.system.dtb
529+
attr->data.system.kernel && attr->data.system.initrd
522530
#else
523531
attr->data.user.elf_program
524532
#endif

src/riscv.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ typedef struct {
551551
typedef struct {
552552
char *kernel;
553553
char *initrd;
554-
char *dtb;
555554
} vm_system_t;
556555
#endif /* RV32_HAS(SYSTEM) */
557556

tools/bin_to_c

Submodule bin_to_c added at f389e7b

0 commit comments

Comments
 (0)