cvw/linux/Makefile

131 lines
4.6 KiB
Makefile
Raw Normal View History

2025-01-22 04:14:01 +00:00
BUILDROOT := buildroot
IMAGE_DIR := ${BUILDROOT}/output/images
DISASSEMBLY_DIR := ${IMAGE_DIR}/disassembly
WALLYLINUX := $(WALLY)/linux
2025-01-04 16:22:39 +00:00
BR2_EXTERNAL_TREE := $(WALLYLINUX)/br2-external-tree
2025-01-22 04:14:01 +00:00
LINUX_TESTVECTORS := $(RISCV)/linux-testvectors
2025-01-22 04:14:01 +00:00
BUILDROOT_OUTPUTS := Image fw_jump.bin fw_jump.elf rootfs.cpio vmlinux busybox
2024-07-26 18:46:09 +00:00
# Device tree files
DTS ?= $(wildcard devicetree/*.dts)
2025-01-22 04:14:01 +00:00
DTB := $(foreach name, $(DTS:%.dts=%.dtb), $(IMAGE_DIR)/$(notdir $(name)))
2025-01-22 04:14:01 +00:00
# Disassembly files
BINARIES := fw_jump.elf vmlinux busybox
2025-01-22 04:14:01 +00:00
OBJDUMPS := $(foreach name, $(basename $(BINARIES) .elf), $(DISASSEMBLY_DIR)/$(name).objdump)
# Testvector files
RAW_RAM_FILE := ${LINUX_TESTVECTORS}/ramGDB.bin
RAM_FILE := ${LINUX_TESTVECTORS}/ram.bin
RAW_BOOTMEM_FILE := ${LINUX_TESTVECTORS}/bootmemGDB.bin
BOOTMEM_FILE := ${LINUX_TESTVECTORS}/bootmem.bin
2025-01-22 04:14:01 +00:00
.PHONY: all check_environment check_write_permissions config build disassemble devicetrees install dumptvs clean cleanDTB
2025-01-22 04:14:01 +00:00
# Default target
all: check_write_permissions clean config build disassemble install dumptvs
2025-01-22 04:14:01 +00:00
# Check if the environment variables are set correctly
check_environment: $(RISCV)
ifeq ($(findstring :$(RISCV)/lib:,:$(LD_LIBRARY_PATH):),)
@(echo "ERROR: Your environment variables are not set correctly." >&2 \
&& echo "Make sure to source setup.sh or install buildroot using the wally-tool-chain-install.sh script." >&2 \
&& exit 1)
endif
2025-01-22 04:14:01 +00:00
# Check if the user has write permissions to the RISCV directory, potentially using sudo
SUDO := $(shell mkdir -p $(RISCV)/.test > /dev/null 2>&1 || echo sudo)
check_write_permissions: check_environment
2024-07-26 18:46:09 +00:00
ifeq ($(SUDO), sudo)
@echo "Cannot write to '$(RISCV)'." \
"Using sudo (you may be prompted for your password several times throughout the install)"
endif
@$(SUDO) mkdir -p $(RISCV)/.test || \
(echo "ERROR: Still unable to write to '$(RISCV)'." >&2 \
&& exit 1)
2024-07-26 18:46:09 +00:00
@$(SUDO) rm -r $(RISCV)/.test
2025-01-22 04:14:01 +00:00
# Build buildroot and device tree binaries
build: $(BUILDROOT_OUTPUTS) devicetrees
# Build buildroot itself
# LD_LIBRARY_PATH must be unset to avoid conflicts between the host and cross compiler
$(BUILDROOT_OUTPUTS): check_environment $(BUILDROOT)
2024-07-26 22:46:08 +00:00
bash -c "unset LD_LIBRARY_PATH; $(MAKE) -C $(BUILDROOT)"
2025-01-22 04:14:01 +00:00
# Install buildroot to $RISCV
install: check_write_permissions
2024-07-26 18:46:09 +00:00
$(SUDO) rm -rf $(RISCV)/$(BUILDROOT)
$(SUDO) mv $(BUILDROOT) $(RISCV)/$(BUILDROOT)
2025-01-22 04:14:01 +00:00
# Generate linux boot testvectors
dumptvs: ${RAM_FILE} ${BOOTMEM_FILE}
2025-01-22 04:14:01 +00:00
# Format QEMU memory dumps for use as testvectors
${LINUX_TESTVECTORS}/%.bin: ${LINUX_TESTVECTORS}/%GDB.bin
truncate -s %8 $^ # Extend file to 8 byte multiple
objcopy --reverse-bytes=8 -F binary $^ $@ # Reverse bytes
2025-01-22 04:14:01 +00:00
# Generate memory dumps from QEMU buildroot boot
TCP_PORT := 1235
${LINUX_TESTVECTORS}/%GDB.bin: | $(LINUX_TESTVECTORS)
${WALLYLINUX}/qemuBoot.sh --gdb ${TCP_PORT} &
riscv64-unknown-elf-gdb -batch \
-ex "target remote :${TCP_PORT}" \
-ex "maintenance packet Qqemu.PhyMemMode:1" \
-ex "printf \"Creating ${RAW_BOOTMEM_FILE}\n\"" \
-ex "dump binary memory ${RAW_BOOTMEM_FILE} 0x1000 0x1fff" \
-ex "printf \"Creating ${RAW_RAM_FILE}\n\"" \
-ex "dump binary memory ${RAW_RAM_FILE} 0x80000000 0x8fffffff" \
2025-01-22 04:14:01 +00:00
-ex "kill"
2025-01-22 04:14:01 +00:00
# Generate device tree binaries
devicetrees: $(DTB)
$(IMAGE_DIR)/%.dtb: ${WALLYLINUX}/devicetree/%.dts
dtc -I dts -O dtb $< > $@
2025-01-22 04:14:01 +00:00
# Create disassembly files
disassemble: check_environment $(OBJDUMPS) $(DISASSEMBLY_DIR)/rootfs
2025-01-22 04:14:01 +00:00
# Extract rootfs
$(DISASSEMBLY_DIR)/rootfs: $(IMAGE_DIR)/rootfs.cpio
2024-07-26 22:46:08 +00:00
@echo "Ignore error about dev/console when extracting rootfs from rootfs.cpio"
2025-01-22 04:14:01 +00:00
mkdir -p $(DISASSEMBLY_DIR)/rootfs
-cpio -i -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)rootfs.cpio
2025-01-22 04:14:01 +00:00
# Disassemble binaries
objdump_flags = $(if $(findstring .elf,$<),-SD,-S) # Add -D flag for .elf files
$(DISASSEMBLY_DIR)/%.objdump: $(IMAGE_DIR)/% | $(DISASSEMBLY_DIR)
riscv64-unknown-elf-objdump $(objdump_flags) $< >> $@
$(WALLY)/bin/extractFunctionRadix.sh $@
2025-01-22 04:14:01 +00:00
# Load wally buildroot configuration
config: $(BUILDROOT) $(BR2_EXTERNAL_TREE)/configs/wally_defconfig
2025-01-04 06:32:11 +00:00
$(MAKE) -C $(BUILDROOT) wally_defconfig BR2_EXTERNAL=$(BR2_EXTERNAL_TREE)
2025-01-22 04:14:01 +00:00
# Clone buildroot and checkout the correct version
$(BUILDROOT):
git clone https://github.com/buildroot/buildroot.git $@
cd $@; git checkout 2024.11.x
2025-01-22 04:14:01 +00:00
# Create directories
$(LINUX_TESTVECTORS): check_write_permissions
$(SUDO) mkdir -p $@
2025-01-22 04:14:01 +00:00
$(DISASSEMBLY_DIR):
mkdir -p $@
2025-01-22 04:14:01 +00:00
# Remove device tree binaries
cleanDTB:
2025-01-22 04:14:01 +00:00
rm -f $(IMAGE_DIR)/*.dtb
2025-01-22 04:14:01 +00:00
# Remove buildroot directory
clean:
rm -rf $(BUILDROOT)
2025-01-22 04:14:01 +00:00
# Check if the RISCV environment variable is set
$(RISCV):
@ echo "ERROR: No $(RISCV) directory. Make sure you have installed the Wally Toolchain."
@ echo "and sourced setup.sh"