CC = gcc
INCLUDES =
CFLAGS = -Wall -O2 -g $(INCLUDES)
LIBS = -lusb
LDFLAGS = -g $(LIBS)
APP_NAME = tt-loader
APP_VERSION = 0.2.2

# Adapt this to your system
CROSS ?= armv5tel-redhat-linux-gnueabi
DIS_ADDR ?= 0

# You can overide the name of the extracted MTD partition to get the kernel
KERNEL_MTD ?= mtd4

OBJS = main.o crc32.o

all: $(APP_NAME)

$(APP_NAME): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $^

clean:
	rm -rf $(APP_NAME) *.o *~
	$(MAKE) -C 2nd clean

2nd:
	$(MAKE) -C 2nd all

pkg: 2nd
	rm -f $(APP_NAME)-$(APP_VERSION).tar.gz
	mkdir -p $(APP_NAME)-$(APP_VERSION)/2nd
	cp -a Makefile *.c COPYING README CMD-SYNTAX start-kernel.txt \
		commands.txt write-splash.txt FlashWriteNAND.txt tt-loader.sh \
		$(APP_NAME)-$(APP_VERSION)
	cp -a 2nd/arm.ld 2nd/*.h 2nd/*.c 2nd/Makefile 2nd/start.S 2nd/2nd.bin \
		$(APP_NAME)-$(APP_VERSION)/2nd
	tar czf $(APP_NAME)-$(APP_VERSION).tar.gz $(APP_NAME)-$(APP_VERSION)
	rm -rf $(APP_NAME)-$(APP_VERSION)

%.dis: %.bin
	rm -f $@
	$(CROSS)-objcopy -I binary -O elf32-littlearm -B arm $< $(@F).elf
	$(CROSS)-objdump -D --adjust-vma=$(DIS_ADDR) $(@F).elf > $@
	rm -f $(@F).elf

# Extract kernel from mtd4 nand partition extraction
kernel.bin: $(KERNEL_MTD)
	./kernel-extract.sh $^ $@

.PHONY: all clean 2nd
