kernel: x86_64: drop CALGARY IOMMU on 5.4
[oweals/openwrt.git] / include / image.mk
1 #
2 # Copyright (C) 2006-2010 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 override TARGET_BUILD=
9 include $(INCLUDE_DIR)/prereq.mk
10 include $(INCLUDE_DIR)/kernel.mk
11 include $(INCLUDE_DIR)/version.mk
12 include $(INCLUDE_DIR)/image-commands.mk
13
14 ifndef IB
15   ifdef CONFIG_TARGET_PER_DEVICE_ROOTFS
16     TARGET_PER_DEVICE_ROOTFS := 1
17   endif
18 endif
19
20 include $(INCLUDE_DIR)/image-legacy.mk
21 include $(INCLUDE_DIR)/feeds.mk
22 include $(INCLUDE_DIR)/rootfs.mk
23
24 override MAKE:=$(_SINGLE)$(SUBMAKE)
25 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
26
27 target_params = $(subst +,$(space),$*)
28 param_get = $(patsubst $(1)=%,%,$(filter $(1)=%,$(2)))
29 param_get_default = $(firstword $(call param_get,$(1),$(2)) $(3))
30 param_mangle = $(subst $(space),_,$(strip $(1)))
31 param_unmangle = $(subst _,$(space),$(1))
32
33 mkfs_packages_id = $(shell echo $(sort $(1)) | mkhash md5 | cut -b1-8)
34 mkfs_target_dir = $(if $(call param_get,pkg,$(1)),$(KDIR)/target-dir-$(call param_get,pkg,$(1)),$(TARGET_DIR))
35
36 KDIR=$(KERNEL_BUILD_DIR)
37 KDIR_TMP=$(KDIR)/tmp
38 DTS_DIR:=$(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/dts
39
40 IMG_PREFIX_EXTRA:=$(if $(EXTRA_IMAGE_NAME),$(call sanitize,$(EXTRA_IMAGE_NAME))-)
41 IMG_PREFIX_VERNUM:=$(if $(CONFIG_VERSION_FILENAMES),$(call sanitize,$(VERSION_NUMBER))-)
42 IMG_PREFIX_VERCODE:=$(if $(CONFIG_VERSION_CODE_FILENAMES),$(call sanitize,$(VERSION_CODE))-)
43
44 IMG_PREFIX:=$(VERSION_DIST_SANITIZED)-$(IMG_PREFIX_VERNUM)$(IMG_PREFIX_VERCODE)$(IMG_PREFIX_EXTRA)$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
45 IMG_ROOTFS:=$(IMG_PREFIX)-rootfs
46 IMG_COMBINED:=$(IMG_PREFIX)-combined
47 IMG_PART_SIGNATURE:=$(shell echo $(SOURCE_DATE_EPOCH)$(LINUX_VERMAGIC) | mkhash md5 | cut -b1-8)
48
49 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
50
51 ifneq ($(CONFIG_BIG_ENDIAN),)
52   JFFS2OPTS     :=  --big-endian --squash-uids -v
53 else
54   JFFS2OPTS     :=  --little-endian --squash-uids -v
55 endif
56
57 ifeq ($(CONFIG_JFFS2_RTIME),y)
58   JFFS2OPTS += -X rtime
59 endif
60 ifeq ($(CONFIG_JFFS2_ZLIB),y)
61   JFFS2OPTS += -X zlib
62 endif
63 ifeq ($(CONFIG_JFFS2_LZMA),y)
64   JFFS2OPTS += -X lzma --compression-mode=size
65 endif
66 ifneq ($(CONFIG_JFFS2_RTIME),y)
67   JFFS2OPTS += -x rtime
68 endif
69 ifneq ($(CONFIG_JFFS2_ZLIB),y)
70   JFFS2OPTS += -x zlib
71 endif
72 ifneq ($(CONFIG_JFFS2_LZMA),y)
73   JFFS2OPTS += -x lzma
74 endif
75
76 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
77
78 SQUASHFS_BLOCKSIZE := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
79 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
80 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
81 SQUASHFSCOMP := gzip
82 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
83 ifeq ($(CONFIG_SQUASHFS_XZ),y)
84   ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
85     BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
86   endif
87   SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
88 endif
89
90 JFFS2_BLOCKSIZE ?= 64k 128k
91
92 fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs
93 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE))
94 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE))
95 fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4
96 fs-types-$(CONFIG_TARGET_ROOTFS_ISO) += iso
97 fs-types-$(CONFIG_TARGET_ROOTFS_UBIFS) += ubifs
98 fs-subtypes-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addsuffix -raw,$(addprefix jffs2-,$(JFFS2_BLOCKSIZE)))
99
100 TARGET_FILESYSTEMS := $(fs-types-y)
101
102 FS_64K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-64k
103 FS_128K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-128k
104 FS_256K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-256k
105
106 define add_jffs2_mark
107         echo -ne '\xde\xad\xc0\xde' >> $(1)
108 endef
109
110 PROFILE_SANITIZED := $(call tolower,$(subst DEVICE_,,$(subst $(space),-,$(PROFILE))))
111
112 define split_args
113 $(foreach data, \
114         $(subst |,$(space),\
115                 $(subst $(space),^,$(1))), \
116         $(call $(2),$(strip $(subst ^,$(space),$(data)))))
117 endef
118
119 define build_cmd
120 $(if $(Build/$(word 1,$(1))),,$(error Missing Build/$(word 1,$(1))))
121 $(call Build/$(word 1,$(1)),$(wordlist 2,$(words $(1)),$(1)))
122
123 endef
124
125 define concat_cmd
126 $(call split_args,$(1),build_cmd)
127 endef
128
129 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
130 define prepare_generic_squashfs
131         $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
132 endef
133
134 define Image/BuildKernel/Initramfs
135         $(call Image/Build/Initramfs)
136 endef
137
138 define Image/BuildKernel/MkuImage
139         mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
140                 -n '$(call toupper,$(ARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION)' -d $(4) $(5)
141 endef
142
143 define Image/BuildKernel/MkFIT
144         $(TOPDIR)/scripts/mkits.sh \
145                 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) $(if $(3),-d $(3)) -C $(4) -a $(5) -e $(6) \
146                 -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config@1") \
147                 -A $(LINUX_KARCH) -v $(LINUX_VERSION)
148         PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
149 endef
150
151 ifdef CONFIG_TARGET_IMAGES_GZIP
152   define Image/Gzip
153         rm -f $(1).gz
154         gzip -9n $(1)
155   endef
156 endif
157
158
159 # Disable noisy checks by default as in upstream
160 DTC_FLAGS += \
161   -Wno-unit_address_vs_reg \
162   -Wno-simple_bus_reg \
163   -Wno-unit_address_format \
164   -Wno-pci_bridge \
165   -Wno-pci_device_bus_num \
166   -Wno-pci_device_reg
167 ifeq ($(strip $(call kernel_patchver_ge,4.17.0)),1)
168   DTC_FLAGS += \
169         -Wno-avoid_unnecessary_addr_size \
170         -Wno-alias_paths
171 endif
172 ifeq ($(strip $(call kernel_patchver_ge,4.18.0)),1)
173   DTC_FLAGS += \
174         -Wno-graph_child_address \
175         -Wno-graph_port \
176         -Wno-unique_unit_address
177 endif
178
179 define Image/pad-to
180         dd if=$(1) of=$(1).new bs=$(2) conv=sync
181         mv $(1).new $(1)
182 endef
183
184 ROOTFS_PARTSIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024)))
185
186 define Image/pad-root-squashfs
187         $(call Image/pad-to,$(KDIR)/root.squashfs,$(if $(1),$(1),$(ROOTFS_PARTSIZE)))
188 endef
189
190 # $(1) source dts file
191 # $(2) target dtb file
192 # $(3) extra CPP flags
193 # $(4) extra DTC flags
194 define Image/BuildDTB
195         $(TARGET_CROSS)cpp -nostdinc -x assembler-with-cpp \
196                 -I$(DTS_DIR) \
197                 -I$(DTS_DIR)/include \
198                 -I$(LINUX_DIR)/include/ \
199                 -undef -D__DTS__ $(3) \
200                 -o $(2).tmp $(1)
201         $(LINUX_DIR)/scripts/dtc/dtc -O dtb \
202                 -i$(dir $(1)) $(DTC_FLAGS) $(4) \
203                 -o $(2) $(2).tmp
204         $(RM) $(2).tmp
205 endef
206
207 define Image/mkfs/jffs2/sub-raw
208         $(STAGING_DIR_HOST)/bin/mkfs.jffs2 \
209                 $(2) \
210                 -e $(patsubst %k,%KiB,$(1)) \
211                 -o $@ -d $(call mkfs_target_dir,$(3)) \
212                 -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
213 endef
214
215 define Image/mkfs/jffs2/sub
216         $(call Image/mkfs/jffs2/sub-raw,$(1),--pad $(2),$(3))
217         $(call add_jffs2_mark,$@)
218 endef
219
220 define Image/mkfs/jffs2/template
221   Image/mkfs/jffs2-$(1) = $$(call Image/mkfs/jffs2/sub,$(1),$(JFFS2OPTS),$$(1))
222   Image/mkfs/jffs2-$(1)-raw = $$(call Image/mkfs/jffs2/sub-raw,$(1),$(JFFS2OPTS),$$(1))
223
224 endef
225
226 define Image/mkfs/jffs2-nand/template
227   Image/mkfs/jffs2-nand-$(1) = \
228         $$(call Image/mkfs/jffs2/sub, \
229                 $(word 2,$(subst -, ,$(1))), \
230                         $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst -, ,$(1))),$$(1))
231
232 endef
233
234 $(eval $(foreach S,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/template,$(S))))
235 $(eval $(foreach S,$(NAND_BLOCKSIZE),$(call Image/mkfs/jffs2-nand/template,$(S))))
236
237 define Image/mkfs/squashfs
238         $(STAGING_DIR_HOST)/bin/mksquashfs4 $(call mkfs_target_dir,$(1)) $@ \
239                 -nopad -noappend -root-owned \
240                 -comp $(SQUASHFSCOMP) $(SQUASHFSOPT) \
241                 -processors 1
242 endef
243
244 # $(1): board name
245 # $(2): rootfs type
246 # $(3): kernel image
247 # $(4): compat string
248 ifneq ($(CONFIG_NAND_SUPPORT),)
249    define Image/Build/SysupgradeNAND
250         mkdir -p "$(KDIR_TMP)/sysupgrade-$(if $(4),$(4),$(1))/"
251         echo "BOARD=$(if $(4),$(4),$(1))" > "$(KDIR_TMP)/sysupgrade-$(if $(4),$(4),$(1))/CONTROL"
252         [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(if $(4),$(4),$(1))/root"
253         [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(if $(4),$(4),$(1))/kernel"
254         (cd "$(KDIR_TMP)"; $(TAR) cvf \
255                 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(if $(4),$(4),$(1)) \
256                         $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
257         )
258    endef
259
260 # $(1) board name
261 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
262 # $(3) rootfstype (e.g. squashfs or ubifs)
263 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
264    define Image/Build/UbinizeImage
265         sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
266                 "$(KDIR)/root.$(3)" \
267                 "$(KDIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
268                 $(4)
269    endef
270
271 endif
272
273 define Image/mkfs/ubifs
274         $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
275                 $(UBIFS_OPTS) $(call param_unmangle,$(call param_get,fs,$(1))) \
276                 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
277                 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--compr=none) \
278                 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--compr=lzo) \
279                 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--compr=zlib) \
280                 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
281                 --squash-uids \
282                 -o $@ -d $(call mkfs_target_dir,$(1))
283 endef
284
285 define Image/mkfs/ext4
286         $(STAGING_DIR_HOST)/bin/make_ext4fs -L rootfs \
287                 -l $(ROOTFS_PARTSIZE) -b $(CONFIG_TARGET_EXT4_BLOCKSIZE) \
288                 $(if $(CONFIG_TARGET_EXT4_RESERVED_PCT),-m $(CONFIG_TARGET_EXT4_RESERVED_PCT)) \
289                 $(if $(CONFIG_TARGET_EXT4_JOURNAL),,-J) \
290                 $(if $(SOURCE_DATE_EPOCH),-T $(SOURCE_DATE_EPOCH)) \
291                 $@ $(call mkfs_target_dir,$(1))/
292 endef
293
294 define Image/Manifest
295         $(call opkg,$(TARGET_DIR_ORIG)) list-installed > \
296                 $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED)).manifest
297 endef
298
299 define Image/gzip-ext4-padded-squashfs
300
301   define Image/Build/squashfs
302     $(call Image/pad-root-squashfs)
303   endef
304
305   ifneq ($(CONFIG_TARGET_IMAGES_GZIP),)
306     define Image/Build/gzip/ext4
307       $(call Image/Build/gzip,ext4)
308     endef
309     define Image/Build/gzip/squashfs
310       $(call Image/Build/gzip,squashfs)
311     endef
312   endif
313
314 endef
315
316 ifdef CONFIG_TARGET_ROOTFS_TARGZ
317   define Image/Build/targz
318         $(TAR) -cp --numeric-owner --owner=0 --group=0 --mode=a-s --sort=name \
319                 $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
320                 -C $(TARGET_DIR)/ . | gzip -9n > $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED))-rootfs.tar.gz
321   endef
322 endif
323
324 ifdef CONFIG_TARGET_ROOTFS_CPIOGZ
325   define Image/Build/cpiogz
326         ( cd $(TARGET_DIR); find . | cpio -o -H newc -R root:root | gzip -9n >$(BIN_DIR)/$(IMG_ROOTFS).cpio.gz )
327   endef
328 endif
329
330 mkfs_packages = $(filter-out @%,$(PACKAGES_$(call param_get,pkg,pkg=$(target_params))))
331 mkfs_packages_add = $(foreach pkg,$(filter-out -%,$(mkfs_packages)),$(pkg)$(call GetABISuffix,$(pkg)))
332 mkfs_packages_remove = $(foreach pkg,$(patsubst -%,%,$(filter -%,$(mkfs_packages))),$(pkg)$(call GetABISuffix,$(pkg)))
333 mkfs_cur_target_dir = $(call mkfs_target_dir,pkg=$(target_params))
334
335 opkg_target = \
336         $(call opkg,$(mkfs_cur_target_dir)) \
337                 -f $(mkfs_cur_target_dir).conf
338
339 target-dir-%: FORCE
340         rm -rf $(mkfs_cur_target_dir) $(mkfs_cur_target_dir).opkg
341         $(CP) $(TARGET_DIR_ORIG) $(mkfs_cur_target_dir)
342         -mv $(mkfs_cur_target_dir)/etc/opkg $(mkfs_cur_target_dir).opkg
343         echo 'src default file://$(PACKAGE_DIR_ALL)' > $(mkfs_cur_target_dir).conf
344         $(if $(mkfs_packages_remove), \
345                 -$(call opkg,$(mkfs_cur_target_dir)) remove \
346                         $(mkfs_packages_remove))
347         $(if $(call opkg_package_files,$(mkfs_packages_add)), \
348                 $(opkg_target) update && \
349                 $(opkg_target) install \
350                         $(call opkg_package_files,$(mkfs_packages_add)))
351         -$(CP) -T $(mkfs_cur_target_dir).opkg/ $(mkfs_cur_target_dir)/etc/opkg/
352         rm -rf $(mkfs_cur_target_dir).opkg $(mkfs_cur_target_dir).conf
353         $(call prepare_rootfs,$(mkfs_cur_target_dir),$(TOPDIR)/files)
354
355 $(KDIR)/root.%: kernel_prepare
356         $(call Image/mkfs/$(word 1,$(target_params)),$(target_params))
357
358 define Device/InitProfile
359   PROFILES := $(PROFILE)
360   DEVICE_TITLE = $$(DEVICE_VENDOR) $$(DEVICE_MODEL)$$(if $$(DEVICE_VARIANT), $$(DEVICE_VARIANT))
361   DEVICE_ALT0_TITLE = $$(DEVICE_ALT0_VENDOR) $$(DEVICE_ALT0_MODEL)$$(if $$(DEVICE_ALT0_VARIANT), $$(DEVICE_ALT0_VARIANT))
362   DEVICE_ALT1_TITLE = $$(DEVICE_ALT1_VENDOR) $$(DEVICE_ALT1_MODEL)$$(if $$(DEVICE_ALT1_VARIANT), $$(DEVICE_ALT1_VARIANT))
363   DEVICE_ALT2_TITLE = $$(DEVICE_ALT2_VENDOR) $$(DEVICE_ALT2_MODEL)$$(if $$(DEVICE_ALT2_VARIANT), $$(DEVICE_ALT2_VARIANT))
364   DEVICE_VENDOR :=
365   DEVICE_MODEL :=
366   DEVICE_VARIANT :=
367   DEVICE_ALT0_VENDOR :=
368   DEVICE_ALT0_MODEL :=
369   DEVICE_ALT0_VARIANT :=
370   DEVICE_ALT1_VENDOR :=
371   DEVICE_ALT1_MODEL :=
372   DEVICE_ALT1_VARIANT :=
373   DEVICE_ALT2_VENDOR :=
374   DEVICE_ALT2_MODEL :=
375   DEVICE_ALT2_VARIANT :=
376   DEVICE_PACKAGES :=
377   DEVICE_DESCRIPTION = Build firmware images for $$(DEVICE_TITLE)
378 endef
379
380 define Device/Init
381   DEVICE_NAME := $(1)
382   KERNEL:=
383   KERNEL_INITRAMFS = $$(KERNEL)
384   KERNEL_SIZE:=
385   CMDLINE:=
386
387   IMAGES :=
388   ARTIFACTS :=
389   IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
390   IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
391   KERNEL_PREFIX = $$(IMAGE_PREFIX)
392   KERNEL_SUFFIX := -kernel.bin
393   KERNEL_INITRAMFS_SUFFIX = $$(KERNEL_SUFFIX)
394   KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
395   KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
396   KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_INITRAMFS_SUFFIX)
397   KERNEL_INITRAMFS_NAME = $$(KERNEL_NAME)-initramfs
398   KERNEL_INSTALL :=
399   KERNEL_NAME := vmlinux
400   KERNEL_DEPENDS :=
401   KERNEL_SIZE :=
402
403   UBOOTENV_IN_UBI :=
404   KERNEL_IN_UBI :=
405   BLOCKSIZE :=
406   PAGESIZE :=
407   SUBPAGESIZE :=
408   VID_HDR_OFFSET :=
409   UBINIZE_OPTS :=
410   UBINIZE_PARTS :=
411   MKUBIFS_OPTS :=
412
413   FS_OPTIONS/ubifs = $$(MKUBIFS_OPTS)
414
415   DEVICE_DTS :=
416   DEVICE_DTS_CONFIG :=
417   DEVICE_DTS_DIR :=
418   SOC :=
419
420   BOARD_NAME :=
421   UIMAGE_NAME :=
422   SUPPORTED_DEVICES :=
423   IMAGE_METADATA :=
424
425   FILESYSTEMS := $(TARGET_FILESYSTEMS)
426
427   UBOOT_PATH :=  $(STAGING_DIR_IMAGE)/uboot-$(1)
428
429   DEFAULT :=
430 endef
431
432 DEFAULT_DEVICE_VARS := \
433   DEVICE_NAME KERNEL KERNEL_INITRAMFS KERNEL_INITRAMFS_IMAGE KERNEL_SIZE \
434   CMDLINE UBOOTENV_IN_UBI KERNEL_IN_UBI BLOCKSIZE PAGESIZE SUBPAGESIZE \
435   VID_HDR_OFFSET UBINIZE_OPTS UBINIZE_PARTS MKUBIFS_OPTS DEVICE_DTS \
436   DEVICE_DTS_CONFIG DEVICE_DTS_DIR SOC BOARD_NAME UIMAGE_NAME SUPPORTED_DEVICES \
437   IMAGE_METADATA KERNEL_ENTRY KERNEL_LOADADDR UBOOT_PATH DEVICE_VENDOR \
438   DEVICE_MODEL DEVICE_VARIANT \
439   DEVICE_ALT0_VENDOR DEVICE_ALT0_MODEL DEVICE_ALT0_VARIANT \
440   DEVICE_ALT1_VENDOR DEVICE_ALT1_MODEL DEVICE_ALT1_VARIANT \
441   DEVICE_ALT2_VENDOR DEVICE_ALT2_MODEL DEVICE_ALT2_VARIANT
442
443 define Device/ExportVar
444   $(1) : $(2):=$$($(2))
445
446 endef
447 define Device/Export
448   $(foreach var,$(DEVICE_VARS) $(DEFAULT_DEVICE_VARS),$(call Device/ExportVar,$(1),$(var)))
449   $(1) : FILESYSTEM:=$(2)
450 endef
451
452 ifdef IB
453   DEVICE_CHECK_PROFILE = $(filter $(1),DEVICE_$(PROFILE) $(PROFILE))
454 else
455   DEVICE_CHECK_PROFILE = $(CONFIG_TARGET_$(if $(CONFIG_TARGET_MULTI_PROFILE),DEVICE_)$(call target_conf,$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET)))_$(1))
456 endif
457
458 DEVICE_EXTRA_PACKAGES = $(call qstrip,$(CONFIG_TARGET_DEVICE_PACKAGES_$(call target_conf,$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET)))_DEVICE_$(1)))
459
460 define merge_packages
461   $(1) :=
462   $(foreach pkg,$(2),
463     $(1) := $$(strip $$(filter-out -$$(patsubst -%,%,$(pkg)) $$(patsubst -%,%,$(pkg)),$$($(1))) $(pkg))
464   )
465 endef
466
467 define Device/Check/Common
468   _PROFILE_SET = $$(strip $$(foreach profile,$$(PROFILES) DEVICE_$(1),$$(call DEVICE_CHECK_PROFILE,$$(profile))))
469   DEVICE_PACKAGES += $$(call extra_packages,$$(DEVICE_PACKAGES))
470   ifdef TARGET_PER_DEVICE_ROOTFS
471     $$(eval $$(call merge_packages,_PACKAGES,$$(DEVICE_PACKAGES) $$(call DEVICE_EXTRA_PACKAGES,$(1))))
472     ROOTFS_ID/$(1) := $$(if $$(_PROFILE_SET),$$(call mkfs_packages_id,$$(_PACKAGES)))
473     PACKAGES_$$(ROOTFS_ID/$(1)) := $$(_PACKAGES)
474   endif
475 endef
476
477 define Device/Check
478   $(Device/Check/Common)
479   KDIR_KERNEL_IMAGE := $(KDIR)/$(1)$$(KERNEL_SUFFIX)
480   _TARGET := $$(if $$(_PROFILE_SET),install-images,install-disabled)
481   ifndef IB
482     _COMPILE_TARGET := $$(if $(CONFIG_IB)$$(_PROFILE_SET),compile,compile-disabled)
483   endif
484 endef
485
486 ifndef IB
487 define Device/Build/initramfs
488   $(call Device/Export,$(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE),$(1))
489   $$(_TARGET): $$(if $$(KERNEL_INITRAMFS),$(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE))
490
491   $(KDIR)/$$(KERNEL_INITRAMFS_NAME):: image_prepare
492   $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
493         cp $$^ $$@
494
495   $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS) image_prepare
496         @rm -f $$@
497         $$(call concat_cmd,$$(KERNEL_INITRAMFS))
498 endef
499 endif
500
501 define Device/Build/compile
502   $$(_COMPILE_TARGET): $(KDIR)/$(1)
503   $(eval $(call Device/Export,$(KDIR)/$(1)))
504   $(KDIR)/$(1):
505         $$(call concat_cmd,$(COMPILE/$(1)))
506
507 endef
508
509 ifndef IB
510 define Device/Build/dtb
511   ifndef BUILD_DTS_$(1)
512   BUILD_DTS_$(1) := 1
513   $(KDIR)/image-$(1).dtb: FORCE
514         $(call Image/BuildDTB,$(strip $(2))/$(strip $(3)).dts,$$@)
515
516   image_prepare: $(KDIR)/image-$(1).dtb
517   endif
518
519 endef
520 endif
521
522 define Device/Build/kernel
523   $$(eval $$(foreach dts,$$(DEVICE_DTS), \
524         $$(call Device/Build/dtb,$$(notdir $$(dts)), \
525                 $$(if $$(DEVICE_DTS_DIR),$$(DEVICE_DTS_DIR),$$(DTS_DIR)), \
526                 $$(dts) \
527         ) \
528   ))
529
530   $(KDIR)/$$(KERNEL_NAME):: image_prepare
531   $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
532   $(call Device/Export,$$(KDIR_KERNEL_IMAGE),$(1))
533   $(BIN_DIR)/$$(KERNEL_IMAGE): $$(KDIR_KERNEL_IMAGE)
534         cp $$^ $$@
535   ifndef IB
536     ifdef CONFIG_IB
537       install: $$(KDIR_KERNEL_IMAGE)
538     endif
539     $$(KDIR_KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS) image_prepare
540         @rm -f $$@
541         $$(call concat_cmd,$$(KERNEL))
542         $$(if $$(KERNEL_SIZE),$$(call Build/check-size,$$(KERNEL_SIZE)))
543   endif
544 endef
545
546 define Device/Build/image
547   GZ_SUFFIX := $(if $(filter %dtb %gz,$(2)),,$(if $(and $(findstring ext4,$(1)),$(CONFIG_TARGET_IMAGES_GZIP)),.gz))
548   $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))$$(GZ_SUFFIX)
549   $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
550   ROOTFS/$(1)/$(3) := \
551         $(KDIR)/root.$(1)$$(strip \
552                 $$(if $$(FS_OPTIONS/$(1)),+fs=$$(call param_mangle,$$(FS_OPTIONS/$(1)))) \
553         )$$(strip \
554                 $(if $(TARGET_PER_DEVICE_ROOTFS),+pkg=$$(ROOTFS_ID/$(3))) \
555         )
556   ifndef IB
557     $$(ROOTFS/$(1)/$(3)): $(if $(TARGET_PER_DEVICE_ROOTFS),target-dir-$$(ROOTFS_ID/$(3)))
558   endif
559   $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $$(KDIR_KERNEL_IMAGE) $$(ROOTFS/$(1)/$(3))
560         @rm -f $$@
561         [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
562         $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
563
564   .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
565
566   $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)).gz: $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
567         gzip -c -9n $$^ > $$@
568
569   $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
570         cp $$^ $$@
571         $(if $(CONFIG_JSON_ADD_IMAGE_INFO), \
572                 DEVICE_ID="$(DEVICE_NAME)" \
573                 BIN_DIR="$(BIN_DIR)" \
574                 IMAGE_NAME="$(IMAGE_NAME)" \
575                 IMAGE_TYPE=$(word 1,$(subst ., ,$(2))) \
576                 IMAGE_PREFIX="$(IMAGE_PREFIX)" \
577                 DEVICE_VENDOR="$(DEVICE_VENDOR)" \
578                 DEVICE_MODEL="$(DEVICE_MODEL)" \
579                 DEVICE_VARIANT="$(DEVICE_VARIANT)" \
580                 DEVICE_ALT0_VENDOR="$(DEVICE_ALT0_VENDOR)" \
581                 DEVICE_ALT0_MODEL="$(DEVICE_ALT0_MODEL)" \
582                 DEVICE_ALT0_VARIANT="$(DEVICE_ALT0_VARIANT)" \
583                 DEVICE_ALT1_VENDOR="$(DEVICE_ALT1_VENDOR)" \
584                 DEVICE_ALT1_MODEL="$(DEVICE_ALT1_MODEL)" \
585                 DEVICE_ALT1_VARIANT="$(DEVICE_ALT1_VARIANT)" \
586                 DEVICE_ALT2_VENDOR="$(DEVICE_ALT2_VENDOR)" \
587                 DEVICE_ALT2_MODEL="$(DEVICE_ALT2_MODEL)" \
588                 DEVICE_ALT2_VARIANT="$(DEVICE_ALT2_VARIANT)" \
589                 DEVICE_TITLE="$(DEVICE_TITLE)" \
590                 TARGET="$(BOARD)" \
591                 SUBTARGET="$(if $(SUBTARGET),$(SUBTARGET),generic)" \
592                 VERSION_NUMBER="$(VERSION_NUMBER)" \
593                 VERSION_CODE="$(VERSION_CODE)" \
594                 SUPPORTED_DEVICES="$(SUPPORTED_DEVICES)" \
595                 $(TOPDIR)/scripts/json_add_image_info.py \
596         )
597
598 endef
599
600 define Device/Build/artifact
601   $$(_TARGET): $(BIN_DIR)/$(IMAGE_PREFIX)-$(1)
602   $(eval $(call Device/Export,$(KDIR)/tmp/$(IMAGE_PREFIX)-$(1)))
603   $(KDIR)/tmp/$(IMAGE_PREFIX)-$(1): $$(KDIR_KERNEL_IMAGE)
604         @rm -f $$@
605         $$(call concat_cmd,$(ARTIFACT/$(1)))
606
607   .IGNORE: $(BIN_DIR)/$(IMAGE_PREFIX)-$(1)
608
609   $(BIN_DIR)/$(IMAGE_PREFIX)-$(1): $(KDIR)/tmp/$(IMAGE_PREFIX)-$(1)
610         cp $$^ $$@
611
612 endef
613
614 define Device/Build
615   $(shell rm -f $(BIN_DIR)/$(IMG_PREFIX)-$(1).json)
616
617   $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
618   $(call Device/Build/kernel,$(1))
619
620   $$(eval $$(foreach compile,$$(COMPILE), \
621     $$(call Device/Build/compile,$$(compile),$(1))))
622
623   $$(eval $$(foreach image,$$(IMAGES), \
624     $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
625       $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
626
627   $$(eval $$(foreach artifact,$$(ARTIFACTS), \
628     $$(call Device/Build/artifact,$$(artifact))))
629
630 endef
631
632 define Device/DumpInfo
633 Target-Profile: DEVICE_$(1)
634 Target-Profile-Name: $(DEVICE_DISPLAY)
635 Target-Profile-Packages: $(DEVICE_PACKAGES)
636 Target-Profile-hasImageMetadata: $(if $(foreach image,$(IMAGES),$(findstring append-metadata,$(IMAGE/$(image)))),1,0)
637 Target-Profile-SupportedDevices: $(SUPPORTED_DEVICES)
638 $(if $(DEFAULT),Target-Profile-Default: $(DEFAULT))
639 Target-Profile-Description:
640 $(DEVICE_DESCRIPTION)
641 $(if $(strip $(DEVICE_ALT0_TITLE)),Alternative device titles:
642 - $(DEVICE_ALT0_TITLE))
643 $(if $(strip $(DEVICE_ALT1_TITLE)),- $(DEVICE_ALT1_TITLE))
644 $(if $(strip $(DEVICE_ALT2_TITLE)),- $(DEVICE_ALT2_TITLE))
645 @@
646
647 endef
648
649 define Device/Dump
650 ifneq ($$(strip $$(DEVICE_ALT0_TITLE)),)
651 DEVICE_DISPLAY = $$(DEVICE_ALT0_TITLE) ($$(DEVICE_TITLE))
652 $$(info $$(call Device/DumpInfo,$(1)))
653 endif
654 ifneq ($$(strip $$(DEVICE_ALT1_TITLE)),)
655 DEVICE_DISPLAY = $$(DEVICE_ALT1_TITLE) ($$(DEVICE_TITLE))
656 $$(info $$(call Device/DumpInfo,$(1)))
657 endif
658 ifneq ($$(strip $$(DEVICE_ALT2_TITLE)),)
659 DEVICE_DISPLAY = $$(DEVICE_ALT2_TITLE) ($$(DEVICE_TITLE))
660 $$(info $$(call Device/DumpInfo,$(1)))
661 endif
662 DEVICE_DISPLAY = $$(DEVICE_TITLE)
663 $$(eval $$(if $$(DEVICE_TITLE),$$(info $$(call Device/DumpInfo,$(1)))))
664 endef
665
666 define Device
667   $(call Device/InitProfile,$(1))
668   $(call Device/Init,$(1))
669   $(call Device/Default,$(1))
670   $(call Device/$(1),$(1))
671   $(call Device/Check,$(1))
672   $(call Device/$(if $(DUMP),Dump,Build),$(1))
673
674 endef
675
676 define BuildImage
677
678   ifneq ($(DUMP),)
679     all: dumpinfo
680     dumpinfo: FORCE
681         @true
682   endif
683
684   download:
685   prepare:
686   compile:
687   clean:
688   legacy-images-prepare:
689   legacy-images:
690   image_prepare:
691
692   ifeq ($(IB),)
693     .PHONY: download prepare compile clean image_prepare kernel_prepare install install-images
694     compile:
695                 $(call Build/Compile)
696
697     clean:
698                 $(call Build/Clean)
699
700     image_prepare: compile
701                 mkdir -p $(BIN_DIR) $(KDIR)/tmp
702                 $(call Image/Prepare)
703
704     legacy-images-prepare-make: image_prepare
705                 $(MAKE) legacy-images-prepare BIN_DIR="$(BIN_DIR)"
706
707   else
708     image_prepare:
709                 mkdir -p $(BIN_DIR) $(KDIR)/tmp
710   endif
711
712   kernel_prepare: image_prepare
713         $(call Image/Build/targz)
714         $(call Image/Build/cpiogz)
715         $(call Image/BuildKernel)
716         $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
717         $(call Image/InstallKernel)
718
719   $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
720   $(foreach device,$(LEGACY_DEVICES),$(call LegacyDevice,$(device)))
721
722   install-images: kernel_prepare $(foreach fs,$(filter-out $(if $(UBIFS_OPTS),,ubifs),$(TARGET_FILESYSTEMS) $(fs-subtypes-y)),$(KDIR)/root.$(fs))
723         $(foreach fs,$(TARGET_FILESYSTEMS),
724                 $(call Image/Build,$(fs))
725         )
726
727   legacy-images-make: install-images
728         $(call Image/mkfs/ubifs/legacy)
729         $(MAKE) legacy-images BIN_DIR="$(BIN_DIR)"
730
731   install: install-images
732         $(call Image/Manifest)
733
734 endef