# Makefile for building the small boot copier

S = @
E = @echo \\\# `date +%Y.%m.%d.%H.%M.%S` ---

OBJ = ./obj

ifeq ("$(CODE_BASE)","")
 CODE_BASE = 0x0
endif

ifeq ("$(FLASH_BASE)","")
 FLASH_BASE = 0x0
endif

ifeq ("$(BOOT_IMAGE_OFFSET)","")
 BOOT_IMAGE_OFFSET = 0x0
endif

ALL_TARGS = \
	$(OBJ)/small_boot_copier.elf \
	$(OBJ)/small_boot_copier_relocated.elf \
	$(OBJ)/small_boot_copier.objdump \
	$(OBJ)/small_boot_copier.nm \
	small_boot_copier.srec \
	small_boot_copier.hex \

all : $(OBJ) $(ALL_TARGS)

clean :
	$(E) Cleaning up
	$(S) -rm -vf $(ALL_TARGS)
	$(S) -rm -vf $(OBJ)/*.s
	$(S) if [ -d $(OBJ) ] ; then rmdir -v $(OBJ) ; fi

$(OBJ) :
	$(E) Creating $@
	$(S) mkdir $@

$(OBJ)/small_boot_copier.elf : \
		small_boot_copier.s \
		small_boot_copier.h \
		$(OBJ)
	$(E) Compiling to $@
	$(S) nios2-elf-cpp small_boot_copier.S \
			-o $(OBJ)/small_boot_copier_pp.s \
			-D FLASH_BASE=$(FLASH_BASE) \
			-D BOOT_IMAGE_OFFSET=$(BOOT_IMAGE_OFFSET)
	$(S) nios2-elf-gcc -nostdlib \
			$(OBJ)/small_boot_copier_pp.s \
			-gdwarf2 -Wa,-gdwarf \
			-o $@ -Os

$(OBJ)/small_boot_copier.objdump : $(OBJ)/small_boot_copier.elf $(OBJ)
	$(E) Objdumping small_boot_copier.elf
	$(S) nios2-elf-objdump -D --source $(OBJ)/small_boot_copier.elf > $@

$(OBJ)/small_boot_copier.nm : $(OBJ)/small_boot_copier.elf $(OBJ)
	$(E) Nm-ing small_boot_copier.elf
	$(S) nios2-elf-nm $(OBJ)/small_boot_copier.elf | sort > $@

small_boot_copier.srec : $(OBJ)/small_boot_copier.elf $(OBJ)
	$(E) Creating SREC file $@
	$(S) nios2-elf-objcopy --output-target=srec $(OBJ)/small_boot_copier.elf $@

small_boot_copier.hex : small_boot_copier.srec
	$(E) Creating HEX file $@
	$(S) nios-convert --infile=small_boot_copier.srec --outfile=$@ --width=32 --oformat=hex
	$(S) if [ -d small_boot_copier ] ; then rmdir -v small_boot_copier ; fi

$(OBJ)/small_boot_copier_relocated.elf : $(OBJ)/small_boot_copier.elf
	$(E) Relocating .elf to $(CODE_BASE)
	$(S) nios2-elf-objcopy -F elf32-littlenios2 --change-addresses $(CODE_BASE) \
		$(OBJ)/small_boot_copier.elf $@

run : $(OBJ) $(ALL_TARGS)
	$(E) Running .elf at $(CODE_BASE)
	$(S) nios2-download -g $(OBJ)/small_boot_copier_relocated.elf

help :
	@echo .
	@echo . To just build the boot copier:
	@echo .  - make all FLASH_BASE=0x0 BOOT_IMAGE_OFFSET=0x00100000
	@echo .
	@echo . To build and run the boot copier:
	@echo .  - make run CODE_BASE=0x01002000 FLASH_BASE=0x0 BOOT_IMAGE_OFFSET=0x00100000
	@echo .
	@echo . CODE_BASE  = Where the boot copier runs from \(likely on-chip ram\)
	@echo . FLASH_BASE = The base address of your CFI flash component
	@echo . BOOT_IMAGE_OFFSET = The offset within CFI flash where your boot 
	@echo .											image has been programmed
	@echo . 
	@echo . IMPORTANT: To use the \"run\" target, CODE_BASE must point to a RAM.  On-chip
	@echo . memory configured as ROM cannot be loaded externally.  If you intend to use
	@echo . the /"run/" target to run the boot copier from an on-chip memory, ensure the
	@echo . on-chip memory is configured as RAM, not ROM.
	@echo . 

# end of file
