CFLAGS := -Wall -Werror -I include -MMD

CFLAGS := -Wall -Werror -I include -MMD -Wno-unused-variable

BIN_FILES = reset_example.bin jtag_example.bin intermezzo.bin boot_bct.bin payload.bin

all: shofel2_t124 $(BIN_FILES)

# --------- x86 ----------

CC_x86 = gcc
CFLAGS_x86 := $(CFLAGS)
# shameless copypasta from https://stackoverflow.com/a/2908351/375416
C_FILES_x86:= $(wildcard exploit/*.c)
OBJ_FILES_x86 := $(addprefix build/obj_x86/,$(notdir $(C_FILES_x86:.c=.o)))
-include $(OBJ_FILES_x86:.o=.d)

build/obj_x86/%.o: exploit/%.c
	$(CC_x86) $(CFLAGS_x86) -c -o $@ $<

shofel2_t124: $(OBJ_FILES_x86)
	$(CC_x86) $(CFLAGS_x86) -o $@ $^

# ------------------------


# ----- ARMv4t Thumb -----
TOOLCHAIN_ARM := $(TOOLCHAINENV)/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-

CC_ARM = $(TOOLCHAIN_ARM)gcc
AS_ARM = $(TOOLCHAIN_ARM)as
OBJCOPY_ARM = $(TOOLCHAIN_ARM)objcopy

CFLAGS_ARM := $(CFLAGS) -march=armv4t -mthumb -Os -ffreestanding \
	-fno-common	-fomit-frame-pointer -nostdlib -fno-builtin-printf \
	-fno-asynchronous-unwind-tables -fPIE -fno-builtin -fno-exceptions \
	-Wl,--no-dynamic-linker,--build-id=none,-T,payloads/payload.ld

# shameless copypasta from https://stackoverflow.com/a/2908351/375416
C_FILES_ARM := $(wildcard payloads/*.c)
OBJ_FILES_ARM := $(addprefix build/obj_arm/,$(notdir $(C_FILES_ARM:.c=.o)))
-include $(OBJ_FILES_ARM:.o=.d)

build/obj_arm/%.o: payloads/%.c
	$(CC_ARM) $(CFLAGS_ARM) -c -o $@ $<

build/reset_example.elf: build/obj_arm/reset_example.o
	$(CC_ARM) $(CFLAGS_ARM) -o $@ $^

build/jtag_example.elf: build/obj_arm/jtag_example.o
	$(CC_ARM) $(CFLAGS_ARM) -o $@ $^

build/boot_bct.elf: build/obj_arm/boot_bct.o
	$(CC_ARM) $(CFLAGS_ARM) -o $@ $^

build/mem_dumper_usb_server.elf: build/obj_arm/mem_dumper_usb_server.o
	$(CC_ARM) $(CFLAGS_ARM) -o $@ $^

build/payload.elf: build/obj_arm/payload.o
	$(CC_ARM) $(CFLAGS_ARM) -o $@ $^

build/intermezzo.elf: build/obj_arm/intermezzo.o
	$(CC_ARM) $(CFLAGS_ARM) -o $@ $^

%.bin: build/%.elf
	$(OBJCOPY_ARM) -O binary $< $@

# ------------------------


clean:
	rm -f $(OBJ_FILES_ARM) $(OBJ_FILES_x86)
	rm -f shofel2_t124 build/*.elf $(BIN_FILES)

cleanall: clean
	rm -f build/obj_arm/*.d build/obj_x86/*.d