#
#                             IPSTB Project
#                   ---------------------------------
#
# Filename: aimage/Makefile
#
#
# Rev Date       Author      Comments
#-------------------------------------------------------------------------------
#   1 20070910   batelaan    Initial from A. McCurdy, SVN revision 250
#   2 20071114   amccurdy    Update to SVN rev 538

-include $(PRJROOT)/etc/setup_productversion.mk

REVISION_STRING := $(PRODUCTVERSION)

KEYFILES	:= $(wildcard keyfile_*.c)
PLATFORMS	:= $(sort $(subst keyfile_,,$(basename $(KEYFILES))))

SRCS_COMMON	:= aimage.c abn.c

CFLAGS		:= -Os -Wall $(if $(REVISION_STRING),-DREVISION_STRING="\"$(REVISION_STRING)\"")

#
# Warning: glibc doesn't officially support static linking (at least there
# are known bugs when linking statically which glibc maintainers refuse to
# fix...). Use of static linking with --gc-sections is one of them...
#
ifeq (a,a)
CFLAGS		+= -static
else
CFLAGS		+= -ffunction-sections -fdata-sections -Wl,--gc-sections
endif

CLEAN_FILES	:= aimage_generate_rsa_key tmsg_raw tmsg_signed \
		   $(addprefix aimage_sign_,$(PLATFORMS)) \
		   $(addprefix aimage_verify_,$(PLATFORMS))

# ----------------------------------------------------------------------------

.PHONY: all gen_test_keys test clean distclean clobber

all:
	@set -e; for pfm in $(PLATFORMS); do \
		CMD="gcc $(CFLAGS) -DENABLE_PRIVATE_KEY $(SRCS_COMMON) aimage_sign.c keyfile_$${pfm}.c -o aimage_sign_$${pfm} -lcrypto"; echo "$$CMD"; $$CMD; \
		CMD="gcc $(CFLAGS) $(SRCS_COMMON) aimage_verify.c keyfile_$${pfm}.c -o aimage_verify_$${pfm} -lcrypto"; echo "$$CMD"; $$CMD; \
		CMD="strip aimage_sign_$${pfm} aimage_verify_$${pfm}"; echo "$$CMD"; $$CMD; \
	done

aimage_generate_rsa_key: aimage_generate_rsa_key.c $(SRCS_COMMON)
	gcc $(CFLAGS) $@.c $(SRCS_COMMON) -o $@ -lcrypto

gen_test_keys: aimage_generate_rsa_key
	./aimage_generate_rsa_key --exp 3 keyfile_stb225_testkey_e3.c
	./aimage_generate_rsa_key --exp 17 keyfile_stb225_testkey_e17.c
	./aimage_generate_rsa_key --exp 65537 keyfile_stb225_testkey_e65537.c

test: all
	@set -e; echo; \
	echo "Testing... !!" > tmsg_raw; \
	for pfm in $(PLATFORMS); do \
		printf "%-32s" "$${pfm}:"; \
		./aimage_sign_$${pfm} --type debug_message tmsg_raw tmsg_signed >/dev/null 2>&1; \
		./aimage_verify_$${pfm} tmsg_signed 2>&1 | grep "signature"; \
	done; \
	rm tmsg_raw tmsg_signed; \
	echo

clean distclean clobber:
	rm -f $(CLEAN_FILES)

# ----------------------------------------------------------------------------

