build: include variant name in STAMP_INSTALLED
[openwrt/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)/host.mk
12 include $(INCLUDE_DIR)/version.mk
13
14 override MAKE:=$(_SINGLE)$(SUBMAKE)
15 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
16
17 KDIR=$(KERNEL_BUILD_DIR)
18 DTS_DIR:=$(LINUX_DIR)/arch/$(ARCH)/boot/dts/
19
20 IMG_PREFIX:=openwrt-$(if $(CONFIG_VERSION_FILENAMES),$(VERSION_NUMBER)-)$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
21
22 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
23
24 ifneq ($(CONFIG_BIG_ENDIAN),)
25 JFFS2OPTS := --big-endian --squash-uids -v
26 else
27 JFFS2OPTS := --little-endian --squash-uids -v
28 endif
29
30 ifeq ($(CONFIG_JFFS2_RTIME),y)
31 JFFS2OPTS += -X rtime
32 endif
33 ifeq ($(CONFIG_JFFS2_ZLIB),y)
34 JFFS2OPTS += -X zlib
35 endif
36 ifeq ($(CONFIG_JFFS2_LZMA),y)
37 JFFS2OPTS += -X lzma --compression-mode=size
38 endif
39 ifneq ($(CONFIG_JFFS2_RTIME),y)
40 JFFS2OPTS += -x rtime
41 endif
42 ifneq ($(CONFIG_JFFS2_ZLIB),y)
43 JFFS2OPTS += -x zlib
44 endif
45 ifneq ($(CONFIG_JFFS2_LZMA),y)
46 JFFS2OPTS += -x lzma
47 endif
48
49 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
50
51 SQUASHFS_BLOCKSIZE := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
52 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
53 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
54 SQUASHFSCOMP := gzip
55 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
56 ifeq ($(CONFIG_SQUASHFS_LZMA),y)
57 SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
58 endif
59 ifeq ($(CONFIG_SQUASHFS_XZ),y)
60 ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
61 BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
62 endif
63 SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
64 endif
65
66 JFFS2_BLOCKSIZE ?= 64k 128k
67
68 fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs
69 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE))
70 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE))
71 fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4
72 fs-types-$(CONFIG_TARGET_ROOTFS_ISO) += iso
73 fs-subtypes-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addsuffix -raw,$(addprefix jffs2-,$(JFFS2_BLOCKSIZE)))
74 fs-subtypes-$(CONFIG_TARGET_ROOTFS_CPIOGZ) += cpiogz
75 fs-subtypes-$(CONFIG_TARGET_ROOTFS_TARGZ) += targz
76
77 TARGET_FILESYSTEMS := $(fs-types-y)
78
79 FS_64K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-64k
80 FS_128K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-128k
81 FS_256K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-256k
82
83 define add_jffs2_mark
84 echo -ne '\xde\xad\xc0\xde' >> $(1)
85 endef
86
87 define toupper
88 $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
89 endef
90
91 define split_args
92 $(foreach data, \
93 $(subst |,$(space),\
94 $(subst $(space),^,$(1))), \
95 $(call $(2),$(strip $(subst ^,$(space),$(data)))))
96 endef
97
98 define build_cmd
99 $(if $(Build/$(word 1,$(1))),,$(error Missing Build/$(word 1,$(1))))
100 $(call Build/$(word 1,$(1)),$(wordlist 2,$(words $(1)),$(1)))
101
102 endef
103
104 define concat_cmd
105 $(call split_args,$(1),build_cmd)
106 endef
107
108 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
109 define prepare_generic_squashfs
110 $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
111 endef
112
113 define Image/BuildKernel/Initramfs
114 $(call Image/Build/Initramfs)
115 endef
116
117 define Image/BuildKernel/MkuImage
118 mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
119 -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
120 endef
121
122 define Image/BuildKernel/MkFIT
123 $(TOPDIR)/scripts/mkits.sh \
124 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) -d $(3) -C $(4) -a $(5) -e $(6) \
125 -A $(ARCH) -v $(LINUX_VERSION)
126 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
127 endef
128
129 define Image/mkfs/jffs2/sub
130 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) --pad -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2) -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
131 $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
132 endef
133
134 define Image/mkfs/jffs2/sub-raw
135 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2)-raw -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
136 endef
137
138 define Image/mkfs/jffs2/template
139 Image/mkfs/jffs2-$(1) = $$(call Image/mkfs/jffs2/sub,$(1),$(1),$(JFFS2OPTS))
140 Image/mkfs/jffs2-$(1)-raw = $$(call Image/mkfs/jffs2/sub-raw,$(1),$(1),$(JFFS2OPTS))
141
142 endef
143
144 define Image/mkfs/jffs2-nand/template
145 Image/mkfs/jffs2-nand-$(1) = \
146 $$(call Image/mkfs/jffs2/sub, \
147 $(word 2,$(subst -, ,$(1))),nand-$(1), \
148 $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst -, ,$(1))))
149
150 endef
151
152 $(eval $(foreach S,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/template,$(S))))
153 $(eval $(foreach S,$(NAND_BLOCKSIZE),$(call Image/mkfs/jffs2-nand/template,$(S))))
154
155 define Image/mkfs/squashfs
156 $(STAGING_DIR_HOST)/bin/mksquashfs4 $(TARGET_DIR) $(KDIR)/root.squashfs -nopad -noappend -root-owned -comp $(SQUASHFSCOMP) $(SQUASHFSOPT) -processors $(if $(CONFIG_PKG_BUILD_JOBS),$(CONFIG_PKG_BUILD_JOBS),1)
157 endef
158
159 # $(1): board name
160 # $(2): rootfs type
161 # $(3): kernel image
162 ifneq ($(CONFIG_NAND_SUPPORT),)
163 define Image/Build/SysupgradeNAND
164 mkdir -p "$(KDIR_TMP)/sysupgrade-$(1)/"
165 echo "BOARD=$(1)" > "$(KDIR_TMP)/sysupgrade-$(1)/CONTROL"
166 [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(1)/root"
167 [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(1)/kernel"
168 (cd "$(KDIR_TMP)"; $(TAR) cvf \
169 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(1))
170 endef
171
172 # $(1) board name
173 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
174 # $(3) rootfstype (e.g. squashfs or ubifs)
175 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
176 define Image/Build/UbinizeImage
177 sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
178 "$(KDIR)/root.$(3)" \
179 "$(KDIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
180 $(4)
181 endef
182
183 endif
184
185 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
186 define Image/mkfs/ubifs/generate
187 $(CP) ./ubinize$(1).cfg $(KDIR)
188 ( cd $(KDIR); \
189 $(STAGING_DIR_HOST)/bin/ubinize \
190 $(if $($(PROFILE)_UBI_OPTS), \
191 $(shell echo $($(PROFILE)_UBI_OPTS)), \
192 $(shell echo $(UBI_OPTS)) \
193 ) \
194 -o $(KDIR)/root$(1).ubi \
195 ubinize$(1).cfg \
196 )
197 endef
198
199 define Image/mkfs/ubifs
200
201 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
202 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
203 $(if $($(PROFILE)_UBIFS_OPTS), \
204 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
205 $(shell echo $(UBIFS_OPTS)) \
206 ) \
207 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
208 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
209 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
210 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
211 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
212 --squash-uids \
213 -o $(KDIR)/root.ubifs \
214 -d $(TARGET_DIR)
215 endif
216 $(call Image/Build,ubifs)
217
218 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
219 $(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
220 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
221 endif
222 $(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
223 endef
224 endif
225
226 define Image/mkfs/cpiogz
227 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
228 endef
229
230 define Image/mkfs/targz
231 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
232 endef
233
234 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
235
236 define Image/mkfs/ext4
237 # generate an ext2 fs
238 $(STAGING_DIR_HOST)/bin/genext2fs -U -B $(CONFIG_TARGET_EXT4_BLOCKSIZE) -b $(E2SIZE) -N $(CONFIG_TARGET_EXT4_MAXINODE) -d $(TARGET_DIR)/ $(KDIR)/root.ext4 -m $(CONFIG_TARGET_EXT4_RESERVED_PCT) $(MKFS_DEVTABLE_OPT)
239 # convert it to ext4
240 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
241 # fix it up
242 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
243 endef
244
245 define Image/mkfs/prepare/default
246 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
247 - $(FIND) $(TARGET_DIR) -type f -not -perm +0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
248 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
249 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
250 $(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
251 chmod 1777 $(TARGET_DIR)/tmp
252 endef
253
254 define Image/mkfs/prepare
255 $(call Image/mkfs/prepare/default)
256 endef
257
258
259 define Image/Checksum
260 ( cd ${BIN_DIR} ; \
261 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
262 md5sum --binary > md5sums \
263 )
264 endef
265
266 define BuildImage/mkfs
267 install: mkfs-$(1)
268 .PHONY: mkfs-$(1)
269 mkfs-$(1): mkfs_prepare
270 $(Image/mkfs/$(1))
271 $(call Build/mkfs/default,$(1))
272 $(call Build/mkfs/$(1),$(1))
273 $(KDIR)/root.$(1): mkfs-$(1)
274
275 endef
276
277 # Build commands that can be called from Device/* templates
278 define Build/uImage
279 mkimage -A $(LINUX_KARCH) \
280 -O linux -T kernel \
281 -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
282 -n '$(call toupper,$(LINUX_KARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $@ $@.new
283 @mv $@.new $@
284 endef
285
286 define Build/lzma
287 $(STAGING_DIR_HOST)/bin/lzma e $@ -lc1 -lp2 -pb2 $(1) $@.new
288 @mv $@.new $@
289 endef
290
291 define Build/kernel-bin
292 rm -f $@
293 cp $^ $@
294 endef
295
296 define Build/patch-cmdline
297 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
298 endef
299
300 define Build/append-kernel
301 dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
302 endef
303
304 define Build/append-rootfs
305 dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
306 endef
307
308 define Build/pad-rootfs
309 $(call prepare_generic_squashfs,$@ $(1))
310 endef
311
312 define Build/pad-offset
313 let \
314 size="$$(stat -c%s $@)" \
315 pad="$(word 1, $(1))" \
316 offset="$(word 2, $(1))" \
317 pad="(pad - ((size + offset) % pad)) % pad" \
318 newsize='size + pad'; \
319 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
320 mv $@.new $@
321 endef
322
323 define Build/check-size
324 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -gt "$$(stat -c%s $@)" ] || { \
325 echo "WARNING: Image file $@ is too big"; \
326 rm -f $@; \
327 }
328 endef
329
330
331 define Device/Init
332 PROFILES := $(PROFILE)
333 KERNEL:=
334 KERNEL_INITRAMFS = $$(KERNEL)
335 KERNEL_SIZE:=
336 CMDLINE:=
337
338 IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
339 IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
340 KERNEL_PREFIX = $(1)
341 KERNEL_SUFFIX := -kernel.bin
342 KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
343 KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
344 KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_SUFFIX)
345 KERNEL_INSTALL :=
346 KERNEL_NAME := vmlinux
347 KERNEL_SIZE :=
348
349 FILESYSTEMS := $(TARGET_FILESYSTEMS)
350 endef
351
352 define Device/ExportVar
353 $(1) : $(2):=$$($(2))
354
355 endef
356 Device/Export = $(foreach var,$(DEVICE_VARS) KERNEL KERNEL_INITRAMFS FILESYSTEM,$(call Device/ExportVar,$(1),$(var)))
357
358 define Device/Check
359 _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
360 endef
361
362 define Device/Build/initramfs
363 $$(_TARGET): $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE)
364
365 $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_IMAGE)
366 cp $$^ $$@
367
368 $(KDIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_NAME)-initramfs
369 @rm -f $$@
370 $$(call concat_cmd,$$(KERNEL_INITRAMFS))
371 endef
372
373 define Device/Build/check_size
374 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -gt "$$(stat -c%s $@)" ] || { \
375 echo "WARNING: Image file $@ is too big"; \
376 rm -f $@; \
377 }
378 endef
379
380 define Device/Build/kernel
381 $(KDIR)/$$(KERNEL_NAME): image_prepare
382 $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
383 $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
384 cp $$^ $$@
385 $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME)
386 @rm -f $$@
387 $$(call concat_cmd,$$(KERNEL))
388 $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
389 endef
390
391 define Device/Build/image
392 FILESYSTEM := $(1)
393 $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
394 $(eval $(call Device/Export,$(KDIR)/$(KERNEL_IMAGE)))
395 $(eval $(call Device/Export,$(KDIR)/$(KERNEL_INITRAMFS_IMAGE)))
396 $(eval $(call Device/Export,$(KDIR)/$(call IMAGE_NAME,$(1),$(2))))
397 $(KDIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)
398 @rm -f $$@
399 [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
400 $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
401
402 .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
403 $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$(call IMAGE_NAME,$(1),$(2))
404 cp $$^ $$@
405
406 endef
407
408 define Device/Build
409 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
410 $(call Device/Build/kernel,$(1))
411
412 $$(eval $$(foreach image,$$(IMAGES), \
413 $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
414 $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
415 endef
416
417 define Device
418 $(call Device/Init,$(1))
419 $(call Device/Default,$(1))
420 $(call Device/Check,$(1))
421 $(call Device/$(1),$(1))
422 $(call Device/Build,$(1))
423
424 endef
425
426 define BuildImage
427
428 download:
429 prepare:
430 compile:
431 clean:
432 image_prepare:
433
434 ifeq ($(IB),)
435 .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
436 compile:
437 $(call Build/Compile)
438
439 clean:
440 $(call Build/Clean)
441
442 image_prepare: compile
443 $(call Image/Prepare)
444 endif
445
446 mkfs_prepare: image_prepare
447 $(call Image/mkfs/prepare)
448
449 kernel_prepare: mkfs_prepare
450 $(call Image/BuildKernel)
451 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Image/BuildKernel/Initramfs))
452 $(call Image/InstallKernel)
453
454 $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
455 $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
456
457 install: kernel_prepare
458 $(foreach fs,$(TARGET_FILESYSTEMS),
459 $(call Image/Build,$(fs))
460 )
461 $(call Image/mkfs/ubifs)
462 $(call Image/Checksum)
463
464 endef