#Makefile with the options specific to the build of a non-upgradable firmware with
#the espfs combined into the flash binary.

# Change tagname to some identifier that's unique for your project. 27 chars max.
TAGNAME ?= "generic"

# linker script used for the linker step

LD_MAP_1:=512:eagle.app.v6.new.512.app1.ld 1024:eagle.app.v6.new.1024.app1.ld 2048:eagle.app.v6.new.2048.ld 4096:eagle.app.v6.new.2048.ld
LD_MAP_2:=512:eagle.app.v6.new.512.app2.ld 1024:eagle.app.v6.new.1024.app2.ld 2048:eagle.app.v6.new.2048.ld 4096:eagle.app.v6.new.2048.ld
LD_SCRIPT_USR1	:= $(call maplookup,$(ESP_SPI_FLASH_SIZE_K),$(LD_MAP_1))
LD_SCRIPT_USR2	:= $(call maplookup,$(ESP_SPI_FLASH_SIZE_K),$(LD_MAP_2))

TARGET_OUT_USR1 := $(addprefix $(BUILD_BASE)/,$(TARGET).user1.out)
TARGET_OUT_USR2 := $(addprefix $(BUILD_BASE)/,$(TARGET).user2.out)
TARGET_OUT	:=  $(TARGET_OUT_USR1) $(TARGET_OUT_USR2)

TARGET_BIN_USR1 := $(addprefix $(BUILD_BASE)/,$(TARGET).user1.bin)
TARGET_BIN_USR2 := $(addprefix $(BUILD_BASE)/,$(TARGET).user2.bin)
TARGET_BIN	:=  $(TARGET_BIN_USR1) $(TARGET_BIN_USR2)
TARGET_OTAFILE := $(addprefix $(BUILD_BASE)/,$(TARGET).ota)


BLANKPOS="$$(printf "0x%X" $$(($(ESP_SPI_FLASH_SIZE_K)*512-0x2000)))"
INITDATAPOS="$$(printf "0x%X" $$(($(ESP_SPI_FLASH_SIZE_K)*512-0x4000)))"

#Convert SPI size into arg for appgen. Format: no=size
FLASH_MAP_CONV:=0:512 2:1024 5:2048 6:4096
ESP_FLASH_SIZE_IX:=$(maplookup $(ESP_SPI_FLASH_SIZE_K),,$(FLASH_MAP_CONV))

CFLAGS += -DOTA_TAGNAME=\"$(TAGNAME)\"

define genappbin
$(1): $$(APP_AR)
	$$(vecho) LD $$@
	$$(Q) $$(LD) -Llibesphttpd -L$$(SDK_LIBDIR) $(2) $$(LDFLAGS) -Wl,--start-group $$(LIBS) $$(APP_AR) -Wl,--end-group -o $$@

$(3): $(1)
	$$(vecho) APPGEN $$@
	$$(Q) $$(OBJCOPY) --only-section .text -O binary $1 build/eagle.app.v6.text.bin
	$$(Q) $$(OBJCOPY) --only-section .data -O binary $1 build/eagle.app.v6.data.bin
	$$(Q) $$(OBJCOPY) --only-section .rodata -O binary $1 build/eagle.app.v6.rodata.bin
	$$(Q) $$(OBJCOPY) --only-section .irom0.text -O binary $1 build/eagle.app.v6.irom0text.bin
	$$(Q) cd build; COMPILE=gcc PATH=$$(XTENSA_TOOLS_ROOT):$$(PATH) python $$(APPGEN) $(1:build/%=%) 2 $$(ESP_FLASH_MODE) $$(ESP_FLASH_FREQ_DIV) $$(ESP_FLASH_SIZE_IX) $(4)
	$$(Q) rm -f eagle.app.v6.*.bin
	$$(Q) mv build/eagle.app.flash.bin $$@
	@echo "** user1.bin uses $$$$(stat -c '%s' $$@) bytes of" $$(ESP_FLASH_MAX) "available"
endef

$(eval $(call genappbin,$(TARGET_OUT_USR1),$$(LD_SCRIPT_USR1),$$(TARGET_BIN_USR1),1))
$(eval $(call genappbin,$(TARGET_OUT_USR2),$$(LD_SCRIPT_USR2),$$(TARGET_BIN_USR2),2))

.PHONY: ldscript_memspecific.ld

ldscript_memspecific.ld:
	$(vecho) "GEN $@"
	$(Q) echo "MEMORY { irom0_0_seg : org = 0x40240000, len = "$$(printf "0x%X" $$(($(ESP_SPI_FLASH_SIZE_K)*1024-0x4000)))" }"> ldscript_memspecific.ld


#define makeuser
#$1: $(APP_AR)
#	$(vecho) "LD $@"
#	$(Q) $(LD) -Llibesphttpd -L$(SDK_LIBDIR) $(LD_SCRIPT) $(EXTRA_LD_SCRIPTS) $(LDFLAGS) -Wl,--start-group $(LIBS) $(APP_AR) -Wl,--end-group -o $@ 
#	$(Q) $(OBJCP) --only-section .text -O binary $2 build/eagle.app.v6.text.bin
#	$(Q) $(OBJCP) --only-section .data -O binary $2 build/eagle.app.v6.data.bin
#	$(Q) $(OBJCP) --only-section .rodata -O binary $2 build/eagle.app.v6.rodata.bin
#	$(Q) $(OBJCP) --only-section .irom0.text -O binary $2 build/eagle.app.v6.irom0text.bin
#	ls -ls build/eagle*bin
#	$(Q) COMPILE=gcc PATH=$(XTENSA_TOOLS_ROOT):$(PATH) python $(APPGEN_TOOL) $(USER1_OUT) 2 $(ESP_FLASH_MODE) $(ESP_FLASH_FREQ_DIV) $(ESP_SPI_SIZE)
#	$(Q) rm -f eagle.app.v6.*.bin
#	$(Q) mv eagle.app.flash.bin $@
#	@echo "** user1.bin uses $$(stat -c '%s' $@) bytes of" $(ESP_FLASH_MAX) "available"
#	$(Q) if [ $$(stat -c '%s' $@) -gt $$(( $(ESP_FLASH_MAX) )) ]; then echo "$@ too big!"; false; fi
#endef


#user1.bin:
#	$(call makeuser,user1.bin,1)

libesphttpd/mkupgimg/mkupgimg: libesphttpd/mkupgimg/
	make -C libesphttpd/mkupgimg/

$(FW_BASE): $(TARGET_BIN) libesphttpd/mkupgimg/mkupgimg
	$(Q) libesphttpd/mkupgimg/mkupgimg $(TARGET_BIN_USR1) $(TARGET_BIN_USR2) $(TAGNAME) $(TARGET_OTAFILE)

flash: $(TARGET_OUT) $(FW_BASE)
	$(Q) $(ESPTOOL) $(ESPTOOL_OPTS) write_flash $(ESPTOOL_FLASHDEF) 0x00000 "$(SDK_BASE)/bin/boot_v1.5.bin" 0x1000 $(TARGET_BIN_USR1)

blankflash:
	$(Q) $(ESPTOOL) $(ESPTOOL_OPTS) write_flash $(ESPTOOL_FLASHDEF) $(BLANKPOS) $(SDK_BASE)/bin/blank.bin $(INITDATAPOS) $(SDK_BASE)/bin/esp_init_data_default.bin

httpflash: $(FW_BASE)
	$(Q) curl -X POST --data-binary '@build/httpd.ota' $(ESPIP)/flash/upload > /dev/null
	$(Q) curl $(ESPIP)/flash/reboot
	$(Q) echo -e '\nDone'