1#
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17
18# Define rule to build one custom image.
19# Input variables: my_custom_imag_makefile
20
21$(call clear-var-list, $(custom_image_parameter_variables))
22
23include $(my_custom_imag_makefile)
24
25my_custom_image_name := $(basename $(notdir $(my_custom_imag_makefile)))
26
27intermediates := $(call intermediates-dir-for,PACKAGING,$(my_custom_image_name))
28my_built_custom_image := $(intermediates)/$(my_custom_image_name).img
29my_staging_dir := $(intermediates)/$(CUSTOM_IMAGE_MOUNT_POINT)
30
31# Collect CUSTOM_IMAGE_MODULES's installd files and their PICKUP_FILES.
32my_built_modules :=
33my_copy_pairs :=
34my_pickup_files :=
35
36$(foreach m,$(CUSTOM_IMAGE_MODULES),\
37  $(eval _pickup_files := $(strip $(ALL_MODULES.$(m).PICKUP_FILES)\
38    $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).PICKUP_FILES)))\
39  $(eval _built_files := $(strip $(ALL_MODULES.$(m).BUILT_INSTALLED)\
40    $(ALL_MODULES.$(m)$(TARGET_2ND_ARCH_MODULE_SUFFIX).BUILT_INSTALLED)))\
41  $(if $(_pickup_files)$(_built_files),,\
42    $(warning Unknown installed file for module '$(m)'))\
43  $(eval my_pickup_files += $(_pickup_files))\
44  $(foreach i, $(_built_files),\
45    $(eval bui_ins := $(subst :,$(space),$(i)))\
46    $(eval ins := $(word 2,$(bui_ins)))\
47    $(if $(filter $(TARGET_OUT_ROOT)/%,$(ins)),\
48      $(eval bui := $(word 1,$(bui_ins)))\
49      $(eval my_built_modules += $(bui))\
50      $(eval my_copy_dest := $(patsubst $(PRODUCT_OUT)/%,%,$(ins)))\
51      $(eval my_copy_dest := $(subst /,$(space),$(my_copy_dest)))\
52      $(eval my_copy_dest := $(wordlist 2,999,$(my_copy_dest)))\
53      $(eval my_copy_dest := $(subst $(space),/,$(my_copy_dest)))\
54      $(eval my_copy_pairs += $(bui):$(my_staging_dir)/$(my_copy_dest)))\
55  ))
56
57# Collect CUSTOM_IMAGE_COPY_FILES.
58my_image_copy_files :=
59$(foreach f,$(CUSTOM_IMAGE_COPY_FILES),\
60  $(eval pair := $(subst :,$(space),$(f)))\
61  $(eval src := $(word 1,$(pair)))\
62  $(eval my_image_copy_files += $(src))\
63  $(eval my_copy_pairs += $(src):$(my_staging_dir)/$(word 2,$(pair))))
64
65ifdef CUSTOM_IMAGE_AVB_KEY_PATH
66ifndef CUSTOM_IMAGE_AVB_ALGORITHM
67  $(error CUSTOM_IMAGE_AVB_ALGORITHM is not defined)
68endif
69ifndef CUSTOM_IMAGE_AVB_ROLLBACK_INDEX
70  $(error CUSTOM_IMAGE_AVB_ROLLBACK_INDEX is not defined)
71endif
72# set rollback_index via footer args
73CUSTOM_IMAGE_AVB_ADD_HASH_FOOTER_ARGS += --rollback_index $(CUSTOM_IMAGE_AVB_ROLLBACK_INDEX)
74CUSTOM_IMAGE_AVB_ADD_HASHTREE_FOOTER_ARGS += --rollback_index $(CUSTOM_IMAGE_AVB_ROLLBACK_INDEX)
75endif
76
77$(my_built_custom_image): PRIVATE_INTERMEDIATES := $(intermediates)
78$(my_built_custom_image): PRIVATE_MOUNT_POINT := $(CUSTOM_IMAGE_MOUNT_POINT)
79$(my_built_custom_image): PRIVATE_PARTITION_SIZE := $(CUSTOM_IMAGE_PARTITION_SIZE)
80$(my_built_custom_image): PRIVATE_FILE_SYSTEM_TYPE := $(CUSTOM_IMAGE_FILE_SYSTEM_TYPE)
81$(my_built_custom_image): PRIVATE_STAGING_DIR := $(my_staging_dir)
82$(my_built_custom_image): PRIVATE_COPY_PAIRS := $(my_copy_pairs)
83$(my_built_custom_image): PRIVATE_PICKUP_FILES := $(my_pickup_files)
84$(my_built_custom_image): PRIVATE_SELINUX := $(CUSTOM_IMAGE_SELINUX)
85$(my_built_custom_image): PRIVATE_SUPPORT_VERITY := $(CUSTOM_IMAGE_SUPPORT_VERITY)
86$(my_built_custom_image): PRIVATE_SUPPORT_VERITY_FEC := $(CUSTOM_IMAGE_SUPPORT_VERITY_FEC)
87$(my_built_custom_image): PRIVATE_VERITY_KEY := $(PRODUCT_VERITY_SIGNING_KEY)
88$(my_built_custom_image): PRIVATE_VERITY_BLOCK_DEVICE := $(CUSTOM_IMAGE_VERITY_BLOCK_DEVICE)
89$(my_built_custom_image): PRIVATE_DICT_FILE := $(CUSTOM_IMAGE_DICT_FILE)
90$(my_built_custom_image): PRIVATE_AVB_AVBTOOL := $(AVBTOOL)
91$(my_built_custom_image): PRIVATE_AVB_KEY_PATH := $(CUSTOM_IMAGE_AVB_KEY_PATH)
92$(my_built_custom_image): PRIVATE_AVB_ALGORITHM:= $(CUSTOM_IMAGE_AVB_ALGORITHM)
93$(my_built_custom_image): PRIVATE_AVB_HASH_ENABLE := $(CUSTOM_IMAGE_AVB_HASH_ENABLE)
94$(my_built_custom_image): PRIVATE_AVB_ADD_HASH_FOOTER_ARGS := $(CUSTOM_IMAGE_AVB_ADD_HASH_FOOTER_ARGS)
95$(my_built_custom_image): PRIVATE_AVB_HASHTREE_ENABLE := $(CUSTOM_IMAGE_AVB_HASHTREE_ENABLE)
96$(my_built_custom_image): PRIVATE_AVB_ADD_HASHTREE_FOOTER_ARGS := $(CUSTOM_IMAGE_AVB_ADD_HASHTREE_FOOTER_ARGS)
97ifeq (true,$(filter true, $(CUSTOM_IMAGE_AVB_HASH_ENABLE) $(CUSTOM_IMAGE_AVB_HASHTREE_ENABLE)))
98  $(my_built_custom_image): $(AVBTOOL)
99else ifneq (,$(filter true, $(CUSTOM_IMAGE_AVB_HASH_ENABLE) $(CUSTOM_IMAGE_AVB_HASHTREE_ENABLE)))
100  $(error Cannot set both CUSTOM_IMAGE_AVB_HASH_ENABLE and CUSTOM_IMAGE_AVB_HASHTREE_ENABLE to true)
101endif
102ifeq (true,$(CUSTOM_IMAGE_SUPPORT_VERITY_FEC))
103  $(my_built_custom_image): $(FEC)
104endif
105my_custom_image_modules_var:=BOARD_$(strip $(call to-upper,$(my_custom_image_name)))_KERNEL_MODULES
106my_custom_image_modules:=$($(my_custom_image_modules_var))
107my_custom_image_modules_dep:=$(if $(my_custom_image_modules),$(my_custom_image_modules) $(DEPMOD),)
108$(my_built_custom_image): PRIVATE_KERNEL_MODULES := $(my_custom_image_modules)
109$(my_built_custom_image): PRIVATE_IMAGE_NAME := $(my_custom_image_name)
110$(my_built_custom_image): $(INTERNAL_USERIMAGES_DEPS) $(my_built_modules) $(my_image_copy_files) $(my_custom_image_modules_dep) \
111  $(CUSTOM_IMAGE_DICT_FILE)
112	@echo "Build image $@"
113	$(hide) rm -rf $(PRIVATE_INTERMEDIATES) && mkdir -p $(PRIVATE_INTERMEDIATES)
114	$(hide) rm -rf $(PRIVATE_STAGING_DIR) && mkdir -p $(PRIVATE_STAGING_DIR)
115	# Copy all the files.
116	$(hide) $(foreach p,$(PRIVATE_COPY_PAIRS),\
117	          $(eval pair := $(subst :,$(space),$(p)))\
118	          mkdir -p $(dir $(word 2,$(pair)));\
119	          cp -Rf $(word 1,$(pair)) $(word 2,$(pair));)
120	$(if $(PRIVATE_KERNEL_MODULES), \
121		$(call build-image-kernel-modules,$(PRIVATE_KERNEL_MODULES),$(PRIVATE_STAGING_DIR),$(PRIVATE_IMAGE_NAME)/,$(call intermediates-dir-for,PACKAGING,depmod_$(PRIVATE_IMAGE_NAME))))
122	$(if $($(PRIVATE_PICKUP_FILES)),$(hide) cp -Rf $(PRIVATE_PICKUP_FILES) $(PRIVATE_STAGING_DIR))
123	# Generate the dict.
124	$(hide) echo "# For all accepted properties, see BuildImage() in tools/releasetools/build_image.py" > $(PRIVATE_INTERMEDIATES)/image_info.txt
125	$(hide) echo "mount_point=$(PRIVATE_MOUNT_POINT)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
126	$(hide) echo "partition_name=$(PRIVATE_MOUNT_POINT)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
127	$(hide) echo "fs_type=$(PRIVATE_FILE_SYSTEM_TYPE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
128	$(hide) echo "partition_size=$(PRIVATE_PARTITION_SIZE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
129	$(hide) echo "ext_mkuserimg=$(notdir $(MKEXTUSERIMG))" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
130	$(if $(PRIVATE_SELINUX),$(hide) echo "selinux_fc=$(SELINUX_FC)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
131	$(if $(PRIVATE_SUPPORT_VERITY),\
132	  $(hide) echo "verity=$(PRIVATE_SUPPORT_VERITY)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
133	    echo "verity_key=$(PRIVATE_VERITY_KEY)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
134	    echo "verity_signer_cmd=$(VERITY_SIGNER)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
135	    echo "verity_block_device=$(PRIVATE_VERITY_BLOCK_DEVICE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
136	$(if $(PRIVATE_SUPPORT_VERITY_FEC),\
137	  $(hide) echo "verity_fec=$(PRIVATE_SUPPORT_VERITY_FEC)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
138	$(if $(filter eng, $(TARGET_BUILD_VARIANT)),$(hide) echo "verity_disable=true" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
139	$(hide) echo "avb_avbtool=$(PRIVATE_AVB_AVBTOOL)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
140	$(if $(PRIVATE_AVB_KEY_PATH),\
141	  $(hide) echo "avb_key_path=$(PRIVATE_AVB_KEY_PATH)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
142	    echo "avb_algorithm=$(PRIVATE_AVB_ALGORITHM)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
143	$(if $(PRIVATE_AVB_HASH_ENABLE),\
144	  $(hide) echo "avb_hash_enable=$(PRIVATE_AVB_HASH_ENABLE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
145	    echo "avb_add_hash_footer_args=$(PRIVATE_AVB_ADD_HASH_FOOTER_ARGS)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
146	$(if $(PRIVATE_AVB_HASHTREE_ENABLE),\
147	  $(hide) echo "avb_hashtree_enable=$(PRIVATE_AVB_HASHTREE_ENABLE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
148	    echo "avb_add_hashtree_footer_args=$(PRIVATE_AVB_ADD_HASHTREE_FOOTER_ARGS)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
149	$(if $(PRIVATE_DICT_FILE),\
150	  $(hide) echo "# Properties from $(PRIVATE_DICT_FILE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
151	    cat $(PRIVATE_DICT_FILE) >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
152	# Generate the image.
153	$(if $(filter oem,$(PRIVATE_MOUNT_POINT)), \
154	  $(hide) echo "oem.buildnumber=$(BUILD_NUMBER_FROM_FILE)" >> $(PRIVATE_STAGING_DIR)/oem.prop)
155	$(hide) PATH=$(foreach p,$(INTERNAL_USERIMAGES_BINARY_PATHS),$(p):)$$PATH \
156	  build/make/tools/releasetools/build_image.py \
157	  $(PRIVATE_STAGING_DIR) $(PRIVATE_INTERMEDIATES)/image_info.txt $@ $(TARGET_OUT)
158
159my_installed_custom_image := $(PRODUCT_OUT)/$(notdir $(my_built_custom_image))
160$(my_installed_custom_image) : $(my_built_custom_image)
161	$(call copy-file-to-new-target-with-cp)
162
163.PHONY: $(my_custom_image_name)
164custom_images $(my_custom_image_name) : $(my_installed_custom_image)
165
166# Archive the built image.
167$(call dist-for-goals, $(my_custom_image_name) custom_images,$(my_installed_custom_image))
168
169my_staging_dir :=
170my_built_modules :=
171my_copy_dest :=
172my_copy_pairs :=
173my_pickup_files :=
174