
KEYDIR:=.
ST:=signtool.exe
PYTHON:=C:/Python27/python.exe

WRAP_KEY := $(KEYDIR)/wrapper_key.bin
AUTH_KEY := $(KEYDIR)/auth_key.bin
ENC_KEY  := $(KEYDIR)/encrypt_key.bin
JTAG_KEY  := $(KEYDIR)/jtag_key.bin

all: $(WRAP_KEY) $(AUTH_KEY) $(ENC_KEY) secure_boot_public_key.h  $(WRAP_KEY:.bin=.h) $(ENC_KEY:.bin=.h) $(JTAG_KEY) $(JTAG_KEY:.bin=.h) $(JTAG_KEY:.bin=.xml)

# Create any of the keys required
$(AUTH_KEY):
	$(ST) genkeypair -algo ecdsa224  -outfile $@

# Create a randome 128 bit key
%_key.bin:
	$(PYTHON) genRandKey.py $@
	
# extract key from keypair as C structure
secure_boot_public_key.h: $(AUTH_KEY)
	$(ST) getkey -key $(AUTH_KEY) -type  BLKeyC -outfile $@

# convert a binary file into an includable format
%.h: %.bin
	$(PYTHON) bin2inc.py -mode c -infile $< -o $@

# convert a binary file into an xml file for emulator resets.xml
%.xml: %.bin
	$(PYTHON) bin2inc.py -mode xml -infile $< -o $@

# Signing in various formats
%.blpldr: %.ldr $(AUTH_KEY)
	$(ST) sign -type BLp -prikey $(AUTH_KEY) -infile $< -outfile $@
	$(PYTHON) $(KEYDIR)/bin2inc.py -mode ascii -infile $@ -o $@.dat

%.blxldr: %.ldr $(AUTH_KEY) $(ENC_KEY)
	$(ST) sign -type BLx -prikey $(AUTH_KEY) -enckey $(ENC_KEY) -infile $< -outfile $@
	$(PYTHON) $(KEYDIR)/bin2inc.py -mode ascii -infile $@ -o $@.dat

%.blwldr: %.ldr $(AUTH_KEY) $(ENC_KEY) $(WRAP_KEY)
	$(ST) sign -type BLw -prikey $(AUTH_KEY) -enckey $(ENC_KEY) -wrapkey $(WRAP_KEY) -infile $< -outfile $@
	$(PYTHON) $(KEYDIR)/bin2inc.py -mode ascii -infile $@ -o $@.dat


clean:
	@echo "Cleaning is disabled to protect losing keys"

secret:
	rm -f $(WRAP_KEY) $(AUTH_KEY) $(ENC_KEY) $(KEYDIR)/*.bak $(KEYDIR)/secure_boot_public_key.h $(WRAP_KEY:.bin=.h) $(ENC_KEY:.bin=.h)  $(JTAG_KEY) $(JTAG_KEY:.bin=.h) 
