mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-24 10:26:25 +04:00
96 lines
2.6 KiB
Makefile
96 lines
2.6 KiB
Makefile
# 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
|
|
BOARD ?= qemu
|
|
SBI ?= rustsbi
|
|
BOOTLOADER := ../bootloader/$(SBI)-$(BOARD).bin
|
|
K210_BOOTLOADER_SIZE := 131072
|
|
|
|
# KERNEL ENTRY
|
|
ifeq ($(BOARD), qemu)
|
|
KERNEL_ENTRY_PA := 0x80200000
|
|
else ifeq ($(BOARD), k210)
|
|
KERNEL_ENTRY_PA := 0x80020000
|
|
endif
|
|
|
|
# Run K210
|
|
K210-SERIALPORT = /dev/ttyUSB0
|
|
K210-BURNER = ../tools/kflash.py
|
|
|
|
# Binutils
|
|
OBJDUMP := rust-objdump --arch-name=riscv64
|
|
OBJCOPY := rust-objcopy --binary-architecture=riscv64
|
|
|
|
# Disassembly
|
|
DISASM ?= -x
|
|
|
|
build: env switch-check $(KERNEL_BIN)
|
|
|
|
switch-check:
|
|
ifeq ($(BOARD), qemu)
|
|
(which last-qemu) || (rm last-k210 -f && touch last-qemu && make clean)
|
|
else ifeq ($(BOARD), k210)
|
|
(which last-k210) || (rm last-qemu -f && touch last-k210 && make clean)
|
|
endif
|
|
|
|
env:
|
|
(rustup target list | grep "riscv64gc-unknown-none-elf (installed)") || rustup target add $(TARGET)
|
|
cargo install cargo-binutils --vers ~0.2
|
|
rustup component add rust-src
|
|
rustup component add llvm-tools-preview
|
|
|
|
$(KERNEL_BIN): kernel
|
|
@$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@
|
|
|
|
kernel:
|
|
@cd ../user && make build
|
|
@echo Platform: $(BOARD)
|
|
@cp src/linker-$(BOARD).ld src/linker.ld
|
|
@cargo build --release --features "board_$(BOARD)"
|
|
@rm src/linker.ld
|
|
|
|
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)
|
|
|
|
run: run-inner
|
|
|
|
|
|
|
|
run-inner: build
|
|
ifeq ($(BOARD),qemu)
|
|
@qemu-system-riscv64 \
|
|
-machine virt \
|
|
-nographic \
|
|
-bios $(BOOTLOADER) \
|
|
-device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA)
|
|
else
|
|
(which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools)
|
|
@cp $(BOOTLOADER) $(BOOTLOADER).copy
|
|
@dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=$(K210_BOOTLOADER_SIZE) seek=1
|
|
@mv $(BOOTLOADER).copy $(KERNEL_BIN)
|
|
@sudo chmod 777 $(K210-SERIALPORT)
|
|
python3 $(K210-BURNER) -p $(K210-SERIALPORT) -b 1500000 $(KERNEL_BIN)
|
|
python3 -m serial.tools.miniterm --eol LF --dtr 0 --rts 0 --filter direct $(K210-SERIALPORT) 115200
|
|
endif
|
|
|
|
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
|
|
|
|
.PHONY: build env kernel clean disasm disasm-vim run-inner switch-check
|