1# Normally this makefile shouldn't be called directly and we expect the output
2# path to be on a certain location to fit together with the other OP-TEE
3# gits and helper scripts.
4
5include ../../scripts/common.mk
6out-dir := $(call strip-trailing-slashes-and-dots,$(O))
7ifeq ($(out-dir),)
8$(error invalid output directory (O=$(O)))
9endif
10
11include $(TA_DEV_KIT_DIR)/host_include/conf.mk
12
13# By default we expect optee_client exported folder to be on a certain relative
14# path, but if the client specifies the OPTEE_CLIENT_EXPORT then that path will
15# be used instead.
16OPTEE_CLIENT_EXPORT ?= ../../../optee_client/out/export
17
18CC		?= $(CROSS_COMPILE)gcc
19CPP		?= $(CROSS_COMPILE)cpp
20LD		?= $(CROSS_COMPILE)ld
21AR		?= $(CROSS_COMPILE)ar
22NM		?= $(CROSS_COMPILE)nm
23OBJCOPY		?= $(CROSS_COMPILE)objcopy
24OBJDUMP		?= $(CROSS_COMPILE)objdump
25READELF		?= $(CROSS_COMPILE)readelf
26
27# Macros to detect the targeted architecture (e.g., arm-linux-gnueabihf or
28# aarch64-linux-gnu) and the corresponding bit size (32 or 64).
29define cc-arch
30$(shell $(1) -v 2>&1 | grep Target | sed 's/Target: \([^-]*\).*/\1/')
31endef
32define cc-bits
33$(if $(filter arm, $(1)),32,$(if $(filter aarch64, $(1)),64,unknown-arch))
34endef
35
36# OpenSSL is used by:
37# - GP tests series 8500
38# - Mbed TLS test 8103
39# - User/group login tests 1027 and 1028
40WITH_OPENSSL ?= y
41ifeq ($(WITH_OPENSSL),y)
42CFLAGS += -DOPENSSL_FOUND=1
43ifneq ($(OPTEE_OPENSSL_EXPORT),)
44LDFLAGS += -lcrypto
45CFLAGS += -I$(OPTEE_OPENSSL_EXPORT)
46else #OPTEE_OPENSSL_EXPORT
47CFLAGS += -I../openssl/include
48ifeq ($(call cc-bits, $(call cc-arch, $(CC))),32)
49LDFLAGS += ../openssl/lib/arm/libcrypto.a -ldl
50else
51LDFLAGS += ../openssl/lib/aarch64/libcrypto.a -ldl
52endif
53endif #OPTEE_OPENSSL_EXPORT
54endif #require OpenSSL
55
56srcs := regression_1000.c
57
58ifeq ($(CFG_GP_SOCKETS),y)
59srcs += regression_2000.c \
60	sock_server.c \
61	rand_stream.c
62endif
63
64srcs +=	adbg/src/adbg_case.c \
65	adbg/src/adbg_enum.c \
66	adbg/src/adbg_expect.c \
67	adbg/src/adbg_log.c \
68	adbg/src/adbg_run.c \
69	adbg/src/security_utils_hex.c \
70	aes_perf.c \
71	benchmark_1000.c \
72	benchmark_2000.c \
73	regression_4000.c \
74	regression_4100.c \
75	regression_5000.c \
76	regression_6000.c \
77	regression_8000.c \
78	regression_8100.c \
79	sha_perf.c \
80	stats.c \
81	xtest_helpers.c \
82	xtest_main.c \
83	xtest_test.c \
84	xtest_uuid_helpers.c
85
86ifeq ($(CFG_SECSTOR_TA_MGMT_PTA),y)
87srcs += install_ta.c
88endif
89
90ifeq ($(CFG_SECURE_DATA_PATH),y)
91srcs += sdp_basic.c
92endif
93
94ifeq ($(CFG_PKCS11_TA),y)
95srcs += pkcs11_1000.c
96endif
97
98objs 	:= $(patsubst %.c,$(out-dir)/xtest/%.o, $(srcs))
99
100ifeq ($(CFG_PKCS11_TA),y)
101CFLAGS += -DCFG_PKCS11_TA
102endif
103CFLAGS += -I./
104CFLAGS += -I./adbg/include
105CFLAGS += -I../supp_plugin/include
106CFLAGS += -I$(out-dir)/xtest
107
108CFLAGS += -I$(OPTEE_CLIENT_EXPORT)/include
109CFLAGS += -I$(TA_DEV_KIT_DIR)/host_include
110
111CFLAGS += -I../../ta/include
112CFLAGS += -I../../ta/create_fail_test/include
113CFLAGS += -I../../ta/crypt/include
114CFLAGS += -I../../ta/enc_fs/include
115CFLAGS += -I../../ta/os_test/include
116CFLAGS += -I../../ta/rpc_test/include
117CFLAGS += -I../../ta/sims/include
118CFLAGS += -I../../ta/miss/include
119CFLAGS += -I../../ta/sims_keepalive/include
120CFLAGS += -I../../ta/storage_benchmark/include
121CFLAGS += -I../../ta/concurrent/include
122CFLAGS += -I../../ta/concurrent_large/include
123CFLAGS += -I../../ta/sha_perf/include
124CFLAGS += -I../../ta/aes_perf/include
125CFLAGS += -I../../ta/socket/include
126CFLAGS += -I../../ta/sdp_basic/include
127CFLAGS += -I../../ta/tpm_log_test/include
128CFLAGS += -I../../ta/large/include
129CFLAGS += -I../../ta/supp_plugin/include
130CFLAGS += -I../../ta/bti_test/include
131
132TA_DIR ?= /lib/optee_armtz
133CFLAGS += -DTA_DIR=\"$(TA_DIR)\"
134
135# Include configuration file generated by OP-TEE OS (CFG_* macros)
136CFLAGS += -include conf.h
137
138CFLAGS += -Wall -Wcast-align -Werror \
139	  -Werror-implicit-function-declaration -Wextra -Wfloat-equal \
140	  -Wformat-nonliteral -Wformat-security -Wformat=2 -Winit-self \
141	  -Wmissing-declarations -Wmissing-format-attribute \
142	  -Wmissing-include-dirs \
143	  -Wmissing-prototypes -Wnested-externs -Wpointer-arith \
144	  -Wshadow -Wstrict-prototypes -Wswitch-default \
145	  -Wwrite-strings -Wno-unused-parameter \
146	  -Wno-declaration-after-statement \
147	  -Wno-missing-field-initializers -Wno-format-zero-length
148
149CFLAGS += -g3
150
151LDFLAGS += -L$(OPTEE_CLIENT_EXPORT)/lib -lteec
152ifeq ($(CFG_PKCS11_TA),y)
153LDFLAGS += -lckteec
154endif
155LDFLAGS += -lpthread -lm
156
157.PHONY: all
158all: xtest
159
160xtest: $(objs)
161	@echo "  LD      $(out-dir)/xtest/$@"
162	$(q)$(CC) -o $(out-dir)/xtest/$@ $+ $(LDFLAGS)
163
164$(out-dir)/xtest/%.o: $(CURDIR)/%.c
165	$(q)mkdir -p $(out-dir)/xtest/adbg/src
166	@echo '  CC      $<'
167	$(q)$(CC) $(CFLAGS) -c $< -o $@
168
169RMDIR := rmdir --ignore-fail-on-non-empty
170define rm-build-dirs
171	$(q)for d in $1; do $(RMDIR) $(out-dir)/xtest/$$d 2> /dev/null; true; done
172	$(q)$(RMDIR) $(out-dir)/xtest 2> /dev/null; true
173	$(q)$(RMDIR) $(out-dir) 2> /dev/null; true
174endef
175
176ifeq ($(CFG_GCM_NIST_VECTORS),y)
177GCM_NIST_VECTORS_DECRYPT = gcmDecrypt128 gcmDecrypt192 gcmDecrypt256
178GCM_NIST_VECTORS_ENCRYPT = gcmEncryptExtIV128 gcmEncryptExtIV192 \
179			   gcmEncryptExtIV256
180
181cleanfiles += $(out-dir)/gcmtestvectors.zip
182$(out-dir)/gcmtestvectors.zip:
183	@echo '  DL      $@'
184	$(q)curl https://csrc.nist.gov/csrc/media/projects/cryptographic-algorithm-validation-program/documents/mac/gcmtestvectors.zip -o $@
185
186define create-nist-gcm-vectors
187cleanfiles += $(out-dir)/xtest/$(1).h $(out-dir)/$(1).rsp
188
189$(out-dir)/$(1).rsp: $(out-dir)/gcmtestvectors.zip
190	@echo '  UNZIP   $$@'
191	$(q)unzip -o $$< $$(notdir $$@) -d $$(dir $$@)
192	$(q)touch $$@
193
194
195$(out-dir)/xtest/$(1).h: $(out-dir)/$(1).rsp
196	@echo '  GEN     $$@'
197	$(q)$(PYTHON3) ../../scripts/rsp_to_gcm_test.py --inf $$< --outf $$@ --mode=$(2) \
198		$(if $(filter y,$(CFG_GCM_NIST_VECTORS_LIMITED)),--limited)
199
200$(CURDIR)/regression_4000.c: $(out-dir)/xtest/$(1).h
201endef
202
203$(foreach v, $(GCM_NIST_VECTORS_DECRYPT), $(eval $(call \
204	create-nist-gcm-vectors,$v,decrypt)))
205$(foreach v, $(GCM_NIST_VECTORS_ENCRYPT), $(eval $(call \
206	create-nist-gcm-vectors,$v,encrypt)))
207endif
208
209define embed-file
210cleanfiles += $(out-dir)/xtest/$(1).h
211
212$(out-dir)/xtest/$(1).h: $(2)
213	@echo '  GEN     $$@'
214	$(q)$(PYTHON3) ../../scripts/file_to_c.py --inf $$< --out $$@ --name $(1)
215
216$(CURDIR)/regression_8100.c: $(out-dir)/xtest/$(1).h
217endef
218
219$(eval $(call embed-file,regression_8100_ca_crt,../../cert/ca.crt))
220$(eval $(call embed-file,regression_8100_mid_crt,../../cert/mid.crt))
221$(eval $(call embed-file,regression_8100_my_crt,../../cert/my.crt))
222$(eval $(call embed-file,regression_8100_my_csr,../../cert/my.csr))
223
224.PHONY: clean
225clean:
226	@echo '  CLEAN $(out-dir)'
227	$(q)rm -f $(out-dir)/xtest/xtest
228	$(q)$(foreach obj,$(objs), rm -f $(obj))
229	$(q)rm -f $(cleanfiles)
230	$(call rm-build-dirs,adbg/src adbg)
231