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