include $(ROOT)/usr/include/make/NGdefs

TARGETS =  eepromtest_dpki.img eepromtest_ppki.img 

COMMONCFILES = sd_write.c \
	eepromtest.c 

DPKIFILES = cacert_dpki.c \
	mscert_dpki.c 

PPKIFILES = cacert_ppki.c \
	mscert_ppki.c

CFILES = $(COMMONCFILES) $(DPKIFILES) $(PPKIFILES)


default: $(TARGETS)

DPKI_OBJS = sd_write.o eepromtest.o mscert_dpki.o cacert_dpki.o
PPKI_OBJS = sd_write.o eepromtest.o mscert_ppki.o cacert_ppki.o

eepromtest_dpki.elf: $(CRT0) $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $(CRT0) $(DPKI_OBJS) $(DDR_LDFLAGS)

eepromtest_ppki.elf: $(CRT0) $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $(CRT0) $(PPKI_OBJS) $(DDR_LDFLAGS)

LLDLIBS +=   -lsdi -lisfs -lios -liosc -lioslibc -lsys -ldiagutils
LDIRT += *.bin *.elf *.mrg *.img cacert*.c mscert*.c btoa

DDR_LDFLAGS =  $(LDLIBS) $(LDFLAGS) -T$(ROOT)/usr/lib/rvl/ddr.link -Ttext $(MK_APPS_START)


eepromtest_dpki.img: eepromtest_dpki.mrg $(ROOT)/usr/bin/rvl/loader.bin
	$(OSMERGE) -b $(ROOT)/usr/bin/rvl/loader.bin -m eepromtest_dpki.mrg -o $@

eepromtest_dpki.mrg: eepromtest_dpki.elf $(ROOT)/usr/bin/rvl/ios_real.elf 
	$(MERGE) -k $(ROOT)/usr/bin/rvl/ios_real.elf -s $(MK_SHARED_START) -z  $(MK_SHARED_SIZE) -c $(ROOT)/usr/bin/rvl/sdi.elf  -f $(ROOT)/usr/bin/rvl/fs.elf  -e eepromtest_dpki.elf -o $@

eepromtest_ppki.img: eepromtest_ppki.mrg $(ROOT)/usr/bin/rvl/loader.bin
	$(OSMERGE) -b $(ROOT)/usr/bin/rvl/loader.bin -m eepromtest_ppki.mrg -o $@

eepromtest_ppki.mrg: eepromtest_ppki.elf $(ROOT)/usr/bin/rvl/ios_real.elf 
	$(MERGE) -k $(ROOT)/usr/bin/rvl/ios_prod.elf -s $(MK_SHARED_START) -z  $(MK_SHARED_SIZE) -c $(ROOT)/usr/bin/rvl/sdi.elf  -f $(ROOT)/usr/bin/rvl/fs.elf -e eepromtest_ppki.elf -o $@

cacert_dpki.c:
	$(HOST_CC) -o btoa btoa.c 
	./btoa $(ROOT)/usr/etc/pki_data/ca_dpki.cert cacert_dpki.c caCert

mscert_dpki.c:
	$(HOST_CC) -o btoa btoa.c
	./btoa $(ROOT)/usr/etc/pki_data/ms_dpki.cert mscert_dpki.c msCert

cacert_ppki.c:
	$(HOST_CC) -o btoa btoa.c 
	./btoa $(ROOT)/usr/etc/pki_data/ca_ppki.cert cacert_ppki.c caCert

mscert_ppki.c:
	$(HOST_CC) -o btoa btoa.c
	./btoa $(ROOT)/usr/etc/pki_data/ms_ppki.cert mscert_ppki.c msCert

# Ticket it with these ids

eepromtest_titid = 10000011
eepromtest_tikid = 11
eepromtest_mid = eepromtest_ppki
eepromtest_cid = 00000011

eepromtest_ppki_eTicket: eepromtest_ppki.img
	rm -f $(eepromtest_titid).tmd $(eepromtest_tikid).tik $(eepromtest_cid).app
	$(ROOT)/usr/bin/x86/tmd -O -C 1 -T 0x$(eepromtest_titid) -n $^ -c 0x$(eepromtest_cid) -i 0 -t 1 -b 0 -P c -k aeskey -s $(ROOT)/usr/etc/pki_data/cp_ppki.cert -D $(ROOT)/usr/etc/pki_data/cp_dpki.rsa 
	$(ROOT)/usr/bin/x86/ticket -O -t 0x$(eepromtest_tikid) -T 0x$(eepromtest_titid) -K $(eepromtest_titid).titleKey -P c -X $(ROOT)/usr/etc/pki_data/xs_ppki.cert -x $(ROOT)/usr/etc/pki_data/xs_dpki.rsa -S $(ROOT)/usr/etc/pki_data/xs_dpki.eccPubKey 


cert_ppki.sys:
	cat $(ROOT)/usr/etc/pki_data/ca_ppki.cert $(ROOT)/usr/etc/pki_data/cp_ppki.cert $(ROOT)/usr/etc/pki_data/xs_ppki.cert > cert_ppki.sys

eepromtest_ppki.bwf: cert_ppki.sys
	$(PUBLISH) -n eepromtest_ppki -w "cert_ppki.sys $(eepromtest_tikid).tik $(eepromtest_titid).tmd eeprom.met $(eepromtest_cid).app" -d

install: default $(TARGETS)
	$(INSTALL) $(TARGETS) $(ROOT)/usr/bin/rvl/tests

include $(COMMONRULES)
