1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2d39a206bSSam Ravnborg#
3d39a206bSSam Ravnborg# kbuild file for usr/ - including initramfs image
4d39a206bSSam Ravnborg#
51da177e4SLinus Torvalds
665e00e04SMasahiro Yamada# cmd_bzip2, cmd_lzma, cmd_lzo, cmd_lz4 from scripts/Makefile.lib appends the
765e00e04SMasahiro Yamada# size at the end of the compressed file, which unfortunately does not work
865e00e04SMasahiro Yamada# with unpack_to_rootfs(). Make size_append no-op.
965e00e04SMasahiro Yamadaoverride size_append := :
109e3596b0SNicholas Piggin
11d4e9056dSMasahiro Yamadacompress-y					:= shipped
1265e00e04SMasahiro Yamadacompress-$(CONFIG_INITRAMFS_COMPRESSION_GZIP)	:= gzip
1365e00e04SMasahiro Yamadacompress-$(CONFIG_INITRAMFS_COMPRESSION_BZIP2)	:= bzip2
1465e00e04SMasahiro Yamadacompress-$(CONFIG_INITRAMFS_COMPRESSION_LZMA)	:= lzma
1565e00e04SMasahiro Yamadacompress-$(CONFIG_INITRAMFS_COMPRESSION_XZ)	:= xzmisc
1665e00e04SMasahiro Yamadacompress-$(CONFIG_INITRAMFS_COMPRESSION_LZO)	:= lzo
1765e00e04SMasahiro Yamadacompress-$(CONFIG_INITRAMFS_COMPRESSION_LZ4)	:= lz4
18a30d8a39SNick Terrellcompress-$(CONFIG_INITRAMFS_COMPRESSION_ZSTD)	:= zstd
196ae64e42SHendrik Brueckner
206ae64e42SHendrik Bruecknerobj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o
211da177e4SLinus Torvalds
2265e00e04SMasahiro Yamada$(obj)/initramfs_data.o: $(obj)/initramfs_inc_data
231da177e4SLinus Torvalds
2465e00e04SMasahiro Yamadaramfs-input := $(strip $(shell echo $(CONFIG_INITRAMFS_SOURCE)))
2565e00e04SMasahiro Yamadacpio-data :=
2665e00e04SMasahiro Yamada
2765e00e04SMasahiro Yamada# If CONFIG_INITRAMFS_SOURCE is empty, generate a small initramfs with the
2865e00e04SMasahiro Yamada# default contents.
2965e00e04SMasahiro Yamadaifeq ($(ramfs-input),)
3065e00e04SMasahiro Yamadaramfs-input := $(srctree)/$(src)/default_cpio_list
3165e00e04SMasahiro Yamadaendif
3265e00e04SMasahiro Yamada
3365e00e04SMasahiro Yamadaifeq ($(words $(ramfs-input)),1)
3465e00e04SMasahiro Yamada
3565e00e04SMasahiro Yamada# If CONFIG_INITRAMFS_SOURCE specifies a single file, and it is suffixed with
3665e00e04SMasahiro Yamada# .cpio, use it directly as an initramfs.
3765e00e04SMasahiro Yamadaifneq ($(filter %.cpio,$(ramfs-input)),)
3865e00e04SMasahiro Yamadacpio-data := $(ramfs-input)
3965e00e04SMasahiro Yamadaendif
4065e00e04SMasahiro Yamada
4165e00e04SMasahiro Yamada# If CONFIG_INITRAMFS_SOURCE specifies a single file, and it is suffixed with
4265e00e04SMasahiro Yamada# .cpio.*, use it directly as an initramfs, and avoid double compression.
4365e00e04SMasahiro Yamadaifeq ($(words $(subst .cpio.,$(space),$(ramfs-input))),2)
4465e00e04SMasahiro Yamadacpio-data := $(ramfs-input)
4565e00e04SMasahiro Yamadacompress-y := shipped
4665e00e04SMasahiro Yamadaendif
4765e00e04SMasahiro Yamada
4865e00e04SMasahiro Yamadaendif
4965e00e04SMasahiro Yamada
5065e00e04SMasahiro Yamada# For other cases, generate the initramfs cpio archive based on the contents
5165e00e04SMasahiro Yamada# specified by CONFIG_INITRAMFS_SOURCE.
5265e00e04SMasahiro Yamadaifeq ($(cpio-data),)
5365e00e04SMasahiro Yamada
5465e00e04SMasahiro Yamadacpio-data := $(obj)/initramfs_data.cpio
55d39a206bSSam Ravnborg
565f2fb52fSMasahiro Yamadahostprogs := gen_init_cpio
57d39a206bSSam Ravnborg
5865e00e04SMasahiro Yamada# .initramfs_data.cpio.d is used to identify all files included
59d39a206bSSam Ravnborg# in initramfs and to detect if any files are added/removed.
60d39a206bSSam Ravnborg# Removed files are identified by directory timestamp being updated
61d39a206bSSam Ravnborg# The dependency list is generated by gen_initramfs.sh -l
6265e00e04SMasahiro Yamada-include $(obj)/.initramfs_data.cpio.d
639e3596b0SNicholas Piggin
6458a2f7d8SSam Ravnborg# do not try to update files included in initramfs
6558a2f7d8SSam Ravnborg$(deps_initramfs): ;
6658a2f7d8SSam Ravnborg
6765e00e04SMasahiro Yamadaquiet_cmd_initfs = GEN     $@
6865e00e04SMasahiro Yamada      cmd_initfs = \
6965e00e04SMasahiro Yamada	$(CONFIG_SHELL) $< -o $@ -l $(obj)/.initramfs_data.cpio.d \
7065e00e04SMasahiro Yamada	$(if $(CONFIG_INITRAMFS_ROOT_UID), -u $(CONFIG_INITRAMFS_ROOT_UID)) \
7165e00e04SMasahiro Yamada	$(if $(CONFIG_INITRAMFS_ROOT_GID), -g $(CONFIG_INITRAMFS_ROOT_GID)) \
7265e00e04SMasahiro Yamada	$(ramfs-input)
7365e00e04SMasahiro Yamada
74a26ee60fSAlain Knaff# We rebuild initramfs_data.cpio if:
75c7fe0608SEugen Cazacu# 1) Any included file is newer than initramfs_data.cpio
76d39a206bSSam Ravnborg# 2) There are changes in which files are included (added or deleted)
77a26ee60fSAlain Knaff# 3) If gen_init_cpio are newer than initramfs_data.cpio
78c7fe0608SEugen Cazacu# 4) Arguments to gen_initramfs.sh changes
7965e00e04SMasahiro Yamada$(obj)/initramfs_data.cpio: $(src)/gen_initramfs.sh $(obj)/gen_init_cpio $(deps_initramfs) FORCE
80d39a206bSSam Ravnborg	$(call if_changed,initfs)
81d6fc9fcbSMasahiro Yamada
8265e00e04SMasahiro Yamadaendif
8365e00e04SMasahiro Yamada
8465e00e04SMasahiro Yamada$(obj)/initramfs_inc_data: $(cpio-data) FORCE
8565e00e04SMasahiro Yamada	$(call if_changed,$(compress-y))
8665e00e04SMasahiro Yamada
8765e00e04SMasahiro Yamadatargets += initramfs_data.cpio initramfs_inc_data
8865e00e04SMasahiro Yamada
89d6fc9fcbSMasahiro Yamadasubdir-$(CONFIG_UAPI_HEADER_TEST) += include
90