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