2020-11-11 12:50:00 +04:00
|
|
|
# Building
|
|
|
|
TARGET := riscv64gc-unknown-none-elf
|
|
|
|
MODE := release
|
|
|
|
KERNEL_ELF := target/$(TARGET)/$(MODE)/os
|
|
|
|
KERNEL_BIN := $(KERNEL_ELF).bin
|
|
|
|
DISASM_TMP := target/$(TARGET)/$(MODE)/asm
|
|
|
|
|
|
|
|
# BOARD
|
2021-02-02 13:44:45 +04:00
|
|
|
BOARD ?= qemu
|
|
|
|
SBI ?= rustsbi
|
|
|
|
BOOTLOADER := ../bootloader/$(SBI)-$(BOARD).bin
|
2021-02-07 14:17:00 +04:00
|
|
|
K210_BOOTLOADER_SIZE := 131072
|
|
|
|
|
|
|
|
# KERNEL ENTRY
|
|
|
|
ifeq ($(BOARD), qemu)
|
|
|
|
KERNEL_ENTRY_PA := 0x80200000
|
|
|
|
else ifeq ($(BOARD), k210)
|
|
|
|
KERNEL_ENTRY_PA := 0x80020000
|
|
|
|
endif
|
2020-11-11 12:50:00 +04:00
|
|
|
|
|
|
|
# Run K210
|
|
|
|
K210-SERIALPORT = /dev/ttyUSB0
|
2021-02-02 13:44:45 +04:00
|
|
|
K210-BURNER = ../tools/kflash.py
|
2020-11-11 12:50:00 +04:00
|
|
|
|
|
|
|
# Binutils
|
|
|
|
OBJDUMP := rust-objdump --arch-name=riscv64
|
|
|
|
OBJCOPY := rust-objcopy --binary-architecture=riscv64
|
|
|
|
|
|
|
|
# Disassembly
|
|
|
|
DISASM ?= -x
|
|
|
|
|
2021-07-12 18:09:34 +04:00
|
|
|
build: env switch-check $(KERNEL_BIN)
|
|
|
|
|
|
|
|
switch-check:
|
|
|
|
ifeq ($(BOARD), qemu)
|
2021-11-27 13:54:31 +04:00
|
|
|
(which last-qemu) || (rm -f last-k210 && touch last-qemu && make clean)
|
2021-07-12 18:09:34 +04:00
|
|
|
else ifeq ($(BOARD), k210)
|
2021-11-27 13:54:31 +04:00
|
|
|
(which last-k210) || (rm -f last-qemu && touch last-k210 && make clean)
|
2021-07-12 18:09:34 +04:00
|
|
|
endif
|
2021-01-02 06:03:24 +04:00
|
|
|
|
|
|
|
env:
|
2021-02-02 13:44:45 +04:00
|
|
|
(rustup target list | grep "riscv64gc-unknown-none-elf (installed)") || rustup target add $(TARGET)
|
2021-10-21 00:28:55 +04:00
|
|
|
cargo install cargo-binutils --vers =0.3.3
|
2021-02-02 13:44:45 +04:00
|
|
|
rustup component add rust-src
|
|
|
|
rustup component add llvm-tools-preview
|
2020-11-11 12:50:00 +04:00
|
|
|
|
|
|
|
$(KERNEL_BIN): kernel
|
|
|
|
@$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@
|
|
|
|
|
|
|
|
kernel:
|
2020-11-20 07:10:38 +04:00
|
|
|
@cd ../user && make build
|
2021-02-07 14:17:00 +04:00
|
|
|
@echo Platform: $(BOARD)
|
|
|
|
@cp src/linker-$(BOARD).ld src/linker.ld
|
2020-11-29 06:31:15 +04:00
|
|
|
@cargo build --release --features "board_$(BOARD)"
|
2021-02-07 14:17:00 +04:00
|
|
|
@rm src/linker.ld
|
2020-11-11 12:50:00 +04:00
|
|
|
|
|
|
|
clean:
|
|
|
|
@cargo clean
|
|
|
|
|
|
|
|
disasm: kernel
|
|
|
|
@$(OBJDUMP) $(DISASM) $(KERNEL_ELF) | less
|
|
|
|
|
|
|
|
disasm-vim: kernel
|
|
|
|
@$(OBJDUMP) $(DISASM) $(KERNEL_ELF) > $(DISASM_TMP)
|
|
|
|
@vim $(DISASM_TMP)
|
|
|
|
@rm $(DISASM_TMP)
|
|
|
|
|
2021-03-05 23:21:55 +04:00
|
|
|
run: run-inner
|
2021-02-08 07:26:57 +04:00
|
|
|
|
2021-03-05 23:21:55 +04:00
|
|
|
|
2020-11-11 12:50:00 +04:00
|
|
|
|
|
|
|
run-inner: build
|
|
|
|
ifeq ($(BOARD),qemu)
|
|
|
|
@qemu-system-riscv64 \
|
|
|
|
-machine virt \
|
|
|
|
-nographic \
|
|
|
|
-bios $(BOOTLOADER) \
|
2020-11-11 12:59:44 +04:00
|
|
|
-device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA)
|
2020-11-11 12:50:00 +04:00
|
|
|
else
|
2021-03-05 23:21:55 +04:00
|
|
|
(which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools)
|
2020-11-11 12:50:00 +04:00
|
|
|
@cp $(BOOTLOADER) $(BOOTLOADER).copy
|
2021-02-07 14:17:00 +04:00
|
|
|
@dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=$(K210_BOOTLOADER_SIZE) seek=1
|
2020-11-11 12:50:00 +04:00
|
|
|
@mv $(BOOTLOADER).copy $(KERNEL_BIN)
|
|
|
|
@sudo chmod 777 $(K210-SERIALPORT)
|
|
|
|
python3 $(K210-BURNER) -p $(K210-SERIALPORT) -b 1500000 $(KERNEL_BIN)
|
2021-01-20 20:16:33 +04:00
|
|
|
python3 -m serial.tools.miniterm --eol LF --dtr 0 --rts 0 --filter direct $(K210-SERIALPORT) 115200
|
2020-11-11 12:50:00 +04:00
|
|
|
endif
|
|
|
|
|
2020-11-11 12:59:44 +04:00
|
|
|
debug: build
|
|
|
|
@tmux new-session -d \
|
|
|
|
"qemu-system-riscv64 -machine virt -nographic -bios $(BOOTLOADER) -device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA) -s -S" && \
|
|
|
|
tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \
|
|
|
|
tmux -2 attach-session -d
|
|
|
|
|
2021-07-12 18:09:34 +04:00
|
|
|
.PHONY: build env kernel clean disasm disasm-vim run-inner switch-check
|