本文主要介绍OpenWrt内核的编译和安装流程的流程。
1. 在include/toplevel.mk中加入如下语句
include/toplevel.mk:
kernel_install: prepare_kernel_conf
$(_SINGLE)$(NO_TRACE_MAKE) -C target/linux install
此时,在OpenWrt源码的根目录下就可以执行make kernel_install编译安装内核源码了
2. 进入target/linux,BOARD=mediatek,所以进入target/linux/mediatek下,Makefile源码如下:
target/linux/Makefile:
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/target.mk
export TARGET_BUILD=1
prereq clean download prepare compile install oldconfig menuconfig nconfig xconfig update refresh install-image clean-linux: FORCE
@+$(NO_TRACE_MAKE) -C $(BOARD) $@
@+$(NO_TRACE_MAKE) -C $(BOARD) $@指令展开为make -C mediatek install。注意,这里TARGET_BUILD=1了。
3. include/linux/mediatek/Makefile的源码如下:
target/linux/mediatek/Makefile:
# Copyright (c) 2015 OpenWrt.org
#
include $(TOPDIR)/rules.mk
ARCH:=arm
BOARD:=mediatek
BOARDNAME:=MediaTek Ralink ARM
SUBTARGETS:=mt7622 mt7623 mt7629 mt7986 mt7981
FEATURES:=squashfs nand ramdisk fpu
KERNEL_PATCHVER:=5.4
KERNEL_TESTING_PATCHVER:=5.4
include $(INCLUDE_DIR)/target.mk
DEFAULT_PACKAGES += \
kmod-leds-gpio kmod-gpio-button-hotplug
$(eval $(call BuildTarget))
include/target.mk:
......
include $(INCLUDE_DIR)/kernel.mk
ifeq ($(TARGET_BUILD),1)
include $(INCLUDE_DIR)/kernel-build.mk
BuildTarget?=$(BuildKernel)
endif
由上分析可知,此时BuildTarget=$(BuildKernel)。
4. make -C mediatek install命令的install目标在BuildKernel中,BuildKernel代码如下:
include/kernel-build.mk:
define BuildKernel
$(if $(QUILT),$(Build/Quilt))
$(if $(LINUX_SITE),$(call Download,kernel))
$(if $(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),$(call Download,git-kernel))
# 所有命令按照串行方式执行
.NOTPARALLEL:
$(Kernel/Autoclean)
$(STAMP_PREPARED): $(if $(LINUX_SITE),$(DL_DIR)/$(LINUX_SOURCE))
-rm -rf $(KERNEL_BUILD_DIR)
-mkdir -p $(KERNEL_BUILD_DIR)
$(Kernel/Prepare)
touch $$@
$(STAMP_CONFIGURED): $(STAMP_PREPARED) $(LINUX_KCONFIG_LIST) $(TOPDIR)/.config FORCE
$(Kernel/Configure)
touch $$@
......
$(LINUX_DIR)/.image: export STAGING_PREFIX=$$(STAGING_DIR_HOST)
$(LINUX_DIR)/.image: export PKG_CONFIG_PATH=$$(STAGING_DIR_HOST)/lib/pkgconfig
$(LINUX_DIR)/.image: export PKG_CONFIG_LIBDIR=$$(STAGING_DIR_HOST)/lib/pkgconfig
$(LINUX_DIR)/.image: $(STAMP_CONFIGURED) $(if $(CONFIG_STRIP_KERNEL_EXPORTS),$(KERNEL_BUILD_DIR)/symtab.h) FORCE
$(Kernel/CompileImage)
$(Kernel/CollectDebug)
touch $$@
mostlyclean: FORCE
$(Kernel/Clean)
......
oldconfig menuconfig nconfig xconfig: $(STAMP_PREPARED) $(STAMP_CHECKED) FORCE
rm -f $(LINUX_DIR)/.config.prev
rm -f $(STAMP_CONFIGURED)
$(LINUX_RECONF_CMD) > $(LINUX_DIR)/.config
$(_SINGLE)$(KERNEL_MAKE) \
$(if $(findstring Darwin,$(HOST_OS)), \
HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib -lncurses" \
HOSTLDLIBS_mconf="-L$(STAGING_DIR_HOST)/lib -lncurses" \
filechk_conf_cfg=" :" \
) \
YACC=$(STAGING_DIR_HOST)/bin/bison \
$$@
$(call LINUX_RECONF_DIFF,$(LINUX_DIR)/.config) > $(LINUX_RECONFIG_TARGET)
install: $(LINUX_DIR)/.image
+$(MAKE) -C image compile install TARGET_BUILD=
......
endef
由上可知,install目标依赖于$(LINUX_DIR)/.image,$(LINUX_DIR)/.image依赖$(STAMP_CONFIGURED),$(STAMP_CONFIGURED)依赖$(STAMP_PREPARED)。
$(STAMP_PREPARED)目标的实现如下:
define Kernel/Prepare/Default
$(LINUX_CAT) $(DL_DIR)/$(LINUX_SOURCE) | $(TAR) -C $(KERNEL_BUILD_DIR) $(TAR_OPTIONS)
$(Kernel/Patch)
$(if $(QUILT),touch $(LINUX_DIR)/.quilt_used)
endef
define Kernel/Prepare
$(call Kernel/Prepare/Default)
endef
$(STAMP_PREPARED): $(if $(LINUX_SITE),$(DL_DIR)/$(LINUX_SOURCE))
-rm -rf $(KERNEL_BUILD_DIR)
-mkdir -p $(KERNEL_BUILD_DIR)
$(Kernel/Prepare)
touch $$@
以上代码主要工作:解压linux源码包,为源码打patch。
$(STAMP_CONFIGURED)目标的实现如下:
define Kernel/Configure/Default
rm -f $(LINUX_DIR)/localversion
$(LINUX_CONF_CMD) > $(LINUX_DIR)/.config.target
# copy CONFIG_KERNEL_* settings over to .config.target
awk '/^(#[[:space:]]+)?CONFIG_KERNEL/{sub("CONFIG_KERNEL_","CONFIG_");print}' $(TOPDIR)/.config >> $(LINUX_DIR)/.config.target
echo "# CONFIG_KALLSYMS_EXTRA_PASS is not set" >> $(LINUX_DIR)/.config.target
echo "# CONFIG_KALLSYMS_ALL is not set" >> $(LINUX_DIR)/.config.target
echo "CONFIG_KALLSYMS_UNCOMPRESSED=y" >> $(LINUX_DIR)/.config.target
$(SCRIPT_DIR)/package-metadata.pl kconfig $(TMP_DIR)/.packageinfo $(TOPDIR)/.config $(KERNEL_PATCHVER) > $(LINUX_DIR)/.config.override
$(SCRIPT_DIR)/kconfig.pl 'm+' '+' $(LINUX_DIR)/.config.target /dev/null $(LINUX_DIR)/.config.override > $(LINUX_DIR)/.config.set
$(call Kernel/SetNoInitramfs)
rm -rf $(KERNEL_BUILD_DIR)/modules
cmp -s $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev || { \
cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config; \
cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev; \
}
$(_SINGLE) [ -d $(LINUX_DIR)/user_headers ] || $(KERNEL_MAKE) INSTALL_HDR_PATH=$(LINUX_DIR)/user_headers headers_install
grep '=[ym]' $(LINUX_DIR)/.config.set | LC_ALL=C sort | mkhash md5 > $(LINUX_DIR)/.vermagic
endef
define Kernel/Configure
$(call Kernel/Configure/Default)
endef
$(STAMP_CONFIGURED): $(STAMP_PREPARED) $(LINUX_KCONFIG_LIST) $(TOPDIR)/.config FORCE
$(Kernel/Configure)
touch $$@
以上代码主要工作:生成内核的.config配置文件,安装内核的头文件(user_header)。
$(LINUX_DIR)/.image目标的实现:
include/kernel-defaults.mk:
KERNEL_BUILD_DIR=build_dir/target-xxx/linux-mediatek_mt7981
define Kernel/CopyImage
cmp -s $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug || { \
$(KERNEL_CROSS)objcopy -O binary $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(LINUX_KERNEL)$(1); \
$(KERNEL_CROSS)objcopy $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).elf; \
$(CP) $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug; \
# cp -fpR */linux-5.4.203/arch/arm64/boot//Image */linux-mediatek_mt7981/Image
$(foreach k, \
$(if $(KERNEL_IMAGES),$(KERNEL_IMAGES),$(filter-out vmlinux dtbs,$(KERNELNAME))), \
$(CP) $(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/$(IMAGES_DIR)/$(k) $(KERNEL_BUILD_DIR)/$(k)$(1); \
) \
}
endef
include/kernel-defaults.mk:
define Kernel/SetInitramfs
rm -f $(LINUX_DIR)/.config.prev
mv $(LINUX_DIR)/.config $(LINUX_DIR)/.config.old
$(call Kernel/SetInitramfs/PreConfigure)
echo 'CONFIG_INITRAMFS_ROOT_UID=$(shell id -u)' >> $(LINUX_DIR)/.config
echo 'CONFIG_INITRAMFS_ROOT_GID=$(shell id -g)' >> $(LINUX_DIR)/.config
echo "$(if $(CONFIG_TARGET_INITRAMFS_FORCE),CONFIG_INITRAMFS_FORCE=y,# CONFIG_INITRAMFS_FORCE is not set)" >> $(LINUX_DIR)/.config
echo "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_NONE),CONFIG_INITRAMFS_COMPRESSION_NONE=y,# CONFIG_INITRAMFS_COMPRESSION_NONE is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP),CONFIG_INITRAMFS_COMPRESSION_GZIP=y\nCONFIG_RD_GZIP=y,# CONFIG_INITRAMFS_COMPRESSION_GZIP is not set\n# CONFIG_RD_GZIP is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2),CONFIG_INITRAMFS_COMPRESSION_BZIP2=y\nCONFIG_RD_BZIP2=y,# CONFIG_INITRAMFS_COMPRESSION_BZIP2 is not set\n# CONFIG_RD_BZIP2 is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA),CONFIG_INITRAMFS_COMPRESSION_LZMA=y\nCONFIG_RD_LZMA=y,# CONFIG_INITRAMFS_COMPRESSION_LZMA is not set\n# CONFIG_RD_LZMA is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO),CONFIG_INITRAMFS_COMPRESSION_LZO=y\nCONFIG_RD_LZO=y,# CONFIG_INITRAMFS_COMPRESSION_LZO is not set\n# CONFIG_RD_LZO is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ),CONFIG_INITRAMFS_COMPRESSION_XZ=y\nCONFIG_RD_XZ=y,# CONFIG_INITRAMFS_COMPRESSION_XZ is not set\n# CONFIG_RD_XZ is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4),CONFIG_INITRAMFS_COMPRESSION_LZ4=y\nCONFIG_RD_LZ4=y,# CONFIG_INITRAMFS_COMPRESSION_LZ4 is not set\n# CONFIG_RD_LZ4 is not set)" >> $(LINUX_DIR)/.config
echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD),CONFIG_INITRAMFS_COMPRESSION_ZSTD=y\nCONFIG_RD_STD=y,# CONFIG_INITRAMFS_COMPRESSION_ZSTD is not set\n# CONFIG_RD_ZSTD is not set)" >> $(LINUX_DIR)/.config
endef
include/kernel-defaults.mk:
define Kernel/Configure/Initramfs
$(call Kernel/SetInitramfs)
endef
define Kernel/CompileImage/Initramfs
$(call Kernel/Configure/Initramfs)
# $(CP) $(GENERIC_PLATFORM_DIR)/other-files/init $(TARGET_DIR)/init
$(LN) usr/bin/gateway $(TARGET_DIR)/init
$(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $(TARGET_DIR)/init)
rm -rf $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/usr/initramfs_data.cpio*
+$(KERNEL_MAKE) $(if $(KERNELNAME),$(KERNELNAME),all) modules
# cp -fpR */linux-5.4.203/arch/arm64/boot//Image */linux-mediatek_mt7981/Image-initramfs
$(call Kernel/CopyImage,-initramfs)
endef
define Kernel/CompileImage/Default
# TARGET_DIR=build_dir/target-xxx/root-mediatek
rm -f $(TARGET_DIR)/init
# KERNEL_MAKE=$(MAKE) -j12 -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
# KERNELNAME=Image dtbs,
+$(KERNEL_MAKE) $(if $(KERNELNAME),$(KERNELNAME),all) modules
$(call Kernel/CopyImage)
endef
define Kernel/CompileImage
$(call Kernel/CompileImage/Default)
$(call Kernel/CompileImage/Initramfs)
endef
$(LINUX_DIR)/.image: $(STAMP_CONFIGURED) $(if $(CONFIG_STRIP_KERNEL_EXPORTS),$(KERNEL_BUILD_DIR)/symtab.h) FORCE
$(Kernel/CompileImage)
$(Kernel/CollectDebug)
touch $$@
至此,install目标的$(LINUX_DIR)/.image的依赖执行完毕,主要做了如下事情:
1. 解压内核,为内核打patch,安装内核头文件(user_header)。
2. 配置内核,主要是通过拷贝target/linux下的config-xxx文件到build_dir下的内核源码路径。
3. 编译生成内核镜像,拷贝到对应目录。