Skip to content

Commit

Permalink
Generate DTB in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
RinHizakura committed Jan 5, 2025
1 parent 81dd26e commit 2656699
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build/linux-x86-softfp/
build/riscv32/
build/sail_cSim/
build/sha1sum-*
build/minimal.dtb
*.a
*.o
*.o.d
Expand All @@ -29,3 +30,4 @@ tests/arch-test-target/sail_cSim/riscv_sim_RV32
tests/scimark2/
__pycache__/
src/rv32_jit.c
src/minimal_dtb.h
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
path = tests/quake
url = https://github.com/sysprog21/quake-embedded
shallow = true
[submodule "tools/bin_to_c"]
path = tools/bin_to_c
url = https://github.com/bitbank2/bin_to_c.git
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ include mk/artifact.mk
include mk/wasm.mk
include mk/system.mk

all: config $(BUILD_DTB) $(BIN)
all: config $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)

OBJS := \
map.o \
Expand Down Expand Up @@ -369,7 +369,7 @@ endif
endif

clean:
$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
$(RM) $(BIN) $(OBJS) $(DEV_OBJS) $(BUILD_DTB) $(BUILD_DTB2C) $(HIST_BIN) $(HIST_OBJS) $(deps) $(WEB_FILES) $(CACHE_OUT) src/rv32_jit.c
distclean: clean
-$(RM) $(DOOM_DATA) $(QUAKE_DATA) $(BUILDROOT_DATA) $(LINUX_DATA)
$(RM) -r $(OUT)/linux-image
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ $ make ENABLE_SYSTEM=1 system
Build using run using specified images:
```shell
$ make ENABLE_SYSTEM=1
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path> -b <dtb_path>
$ build/rv32emu -k <kernel_img_path> -i <rootfs_img_path>
```

#### Build Linux image
Expand Down
21 changes: 17 additions & 4 deletions mk/system.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ ifeq ($(call has, SYSTEM), 1)
DEV_SRC := src/devices

DTC ?= dtc
$(OUT)/minimal.dtb: $(DEV_SRC)/minimal.dts
BUILD_DTB := $(OUT)/minimal.dtb
$(BUILD_DTB): $(DEV_SRC)/minimal.dts
$(VECHO) " DTC\t$@\n"
$(Q)$(DTC) $^ -o $@
BUILD_DTB := $(OUT)/minimal.dtb

BIN_TO_C_DIR := tools/bin_to_c
$(BIN_TO_C_DIR)/makefile:
git submodule update --init $(BIN_TO_C_DIR)

BIN_TO_C := $(BIN_TO_C_DIR)/bin_to_c
$(BIN_TO_C): $(BIN_TO_C_DIR)/makefile
$(MAKE) -C $(dir $<)

BUILD_DTB2C := src/minimal_dtb.h
$(BUILD_DTB2C): $(BIN_TO_C) $(BUILD_DTB)
$(BIN_TO_C) $(BUILD_DTB) > $(BUILD_DTB2C)
sed -i 's/PROGMEM//g' $(BUILD_DTB2C)

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

# system target execution by using default dependencies
LINUX_IMAGE_DIR := linux-image
system_action := ($(BIN) -k $(OUT)/$(LINUX_IMAGE_DIR)/Image -i $(OUT)/$(LINUX_IMAGE_DIR)/rootfs.cpio -b $(OUT)/minimal.dtb)
system_deps += artifact $(BUILD_DTB) $(BIN)
system_action := ($(BIN) -k $(OUT)/$(LINUX_IMAGE_DIR)/Image -i $(OUT)/$(LINUX_IMAGE_DIR)/rootfs.cpio)
system_deps += artifact $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
system: $(system_deps)
$(system_action)

Expand Down
1 change: 1 addition & 0 deletions src/dtc
Submodule dtc added at 039a99
9 changes: 1 addition & 8 deletions 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:b:";
static const char *optstr = "tgqmhpd:a:k:i:";

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

static void print_usage(const char *filename)
Expand All @@ -73,7 +72,6 @@ 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 <dtb> : use <dtb> as device tree blob\n"
#endif
" -d [filename]: dump registers as JSON to the "
"given file or `-` (STDOUT)\n"
Expand Down Expand Up @@ -114,10 +112,6 @@ static bool parse_args(int argc, char **args)
opt_rootfs_img = optarg;
emu_argc++;
break;
case 'b':
opt_dtb = optarg;
emu_argc++;
break;
#endif
case 'q':
opt_quiet_outputs = true;
Expand Down Expand Up @@ -263,7 +257,6 @@ 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.dtb = opt_dtb;
#else
attr.data.user.elf_program = opt_prog_name;
#endif
Expand Down
14 changes: 11 additions & 3 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ static void map_file(char **ram_loc, const char *name)
exit(EXIT_FAILURE);
}

static void load_dtb(char **ram_loc)
{
#include "minimal_dtb.h"
memcpy(*ram_loc, minimal, sizeof(minimal));
*ram_loc += sizeof(minimal);
return;
}

/*
* The control mode flag for keyboard.
*
Expand Down Expand Up @@ -294,6 +302,7 @@ static void capture_keyboard_input()
term.c_lflag &= ~TERMIOS_C_CFLAG;
tcsetattr(0, TCSANOW, &term);
}

#endif

/*
Expand Down Expand Up @@ -409,7 +418,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;
map_file(&ram_loc, attr->data.system.dtb);
load_dtb(&ram_loc);
/*
* Load optional initrd image at last 8 MiB before the dtb region to
* prevent kernel from overwritting it
Expand Down Expand Up @@ -517,8 +526,7 @@ void rv_run(riscv_t *rv)
vm_attr_t *attr = PRIV(rv);
assert(attr &&
#if RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER)
attr->data.system.kernel && attr->data.system.initrd &&
attr->data.system.dtb
attr->data.system.kernel && attr->data.system.initrd
#else
attr->data.user.elf_program
#endif
Expand Down
1 change: 0 additions & 1 deletion src/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ typedef struct {
typedef struct {
char *kernel;
char *initrd;
char *dtb;
} vm_system_t;
#endif /* RV32_HAS(SYSTEM) */

Expand Down
1 change: 1 addition & 0 deletions tools/bin_to_c
Submodule bin_to_c added at f389e7

0 comments on commit 2656699

Please sign in to comment.