#!/bin/sh # SPDX-License-Identifier: GPL-2.0+ set -e kbuild_root=$(dirname $(readlink -f "$0")) #OPTIONS='CONFIG_DEBUG_SECTION_MISMATCH=y' #OPTIONS='C=1' OPTIONS="KALLSYMS_EXTRA_PASS=0" KCFLAGS="-Werror -Wno-error=cpp" NFS_ROOT=$HOME/src/netboot TFTP_ROOT=$HOME/tftpboot # # Options parsing # do_compile_doc=0 do_menu_config=0 do_package_deb=0 opt_bootloader= opt_cmdline_file= opt_defconfig= while [[ $# != 0 ]] ; do option=$1 shift case $option in --bootloader) opt_bootloader=$1 if [[ ! -r $opt_bootloader ]] ; then echo "Boot loader file $opt_bootloader is not readable" exit 1 fi shift ;; --cmdline) opt_cmdline_file=$1 if [[ ! -r $opt_cmdline_file ]] ; then echo "Command line file $opt_cmdline_file is not readable" exit 1 fi shift ;; --config) do_menu_config=1 ;; --deb) do_package_deb=1 ;; --defconfig) opt_defconfig=$1 if [[ ! -r $opt_defconfig ]] ; then echo "defconfig file $opt_defconfig is not readable" exit 1 fi shift ;; --doc) do_compile_doc=1 ;; --*) echo "Unrecognized option $option" exit 1 ;; *) platform=$option ;; esac done # # Platform # if [[ -z "$platform" ]] ; then for f in $kbuild_root/platforms/*.sh ; do pattern=$(cat $f | grep "dir_pattern=" | head -1) pattern=${pattern/#dir_pattern=/} if [[ -n "$pattern" ]] ; then echo $PWD | grep -q "$pattern" || continue platform=$(basename -s .sh $f) break fi done fi if [[ ! -r "$kbuild_root/platforms/$platform.sh" ]] ; then echo "Unknown platform '$platform'. Supported platforms are" for f in $kbuild_root/platforms/*.sh ; do echo "- $(basename -s .sh $f)" done exit 1 fi source "${kbuild_root}/platforms/$platform.sh" || exit 1 # # Architecture # if [[ ! -r "$kbuild_root/arch/$arch.sh" ]] ; then echo "Unknown architecture '$arch'. Supported architectures are" for f in $kbuild_root/arch/*.sh ; do echo "- $(basename -s .sh $f)" done exit 1 fi source "${kbuild_root}/arch/$arch.sh" || exit 1 target_dir=${target_dir:-$NFS_ROOT/$arch_dir} output_dir=${output_dir:-$PWD/output/$arch_dir} # # Parallelize make with twice as many jobs as CPUs # num_cpus=$(cat /proc/cpuinfo | grep '^processor\b' | wc -l) pmake="make -j$((num_cpus*2))" OPTIONS="KALLSYMS_EXTRA_PASS=0 KCFLAGS='$KCFLAGS'" echo "Compiling for platform $platform with $num_cpus CPUs" mkdir -p $output_dir/ if [[ -n "$opt_defconfig" ]] ; then mkdir -p $output_dir/arch/$arch/configs/ cp $opt_defconfig $output_dir/arch/$arch/configs/build_defconfig make ARCH=$arch O=$output_dir build_defconfig rm $output_dir/arch/$arch/configs/build_defconfig fi if [[ $do_menu_config = 1 ]] ; then make ARCH=$arch O=$output_dir menuconfig exit 0 fi # # Compile documentation if requested # if [[ $do_compile_doc = 1 ]] ; then $pmake ARCH=$arch O=$output_dir DOCBOOKS='' htmldocs fi # # Compile the kernel, modules and DTBs # dtbs= for dtb in $DTBS ; do dtb=${dtb/:*/} dtbs="$dtbs $dtb" done eval $pmake ARCH=$arch O=$output_dir CROSS_COMPILE=$cross_compile $OPTIONS \ $image_kernel $dtbs grep 'CONFIG_MODULES=y' $output_dir/.config > /dev/null && modules=modules if [[ x$modules == xmodules ]] ; then eval $pmake ARCH=$arch O=$output_dir CROSS_COMPILE=$cross_compile $OPTIONS \ $modules fi version=$(cat $output_dir/include/generated/utsrelease.h | cut -d '"' -f 2) # # Create the image # make_fit_image() { local kernel_its=$output_dir/arch/$arch/boot/kernel_fdt.its cat < $kernel_its /dts-v1/; / { description = "Kernel + FDT image for $board board"; #address-cells = <1>; images { kernel { description = "Linux kernel"; data = /incbin/("$output_dir/arch/$arch/boot/$image_kernel"); type = "kernel"; arch = "$arch"; os = "linux"; compression = "none"; load = <$LOADADDR>; entry = <$LOADADDR>; hash-1 { algo = "crc32"; }; hash-2 { algo = "sha1"; }; }; EOF local dtb for dtb in $DTBS ; do local src=${dtb/:*/} local dst=${dtb/*:/} dst=${dst/*\//} cat <> $kernel_its fdt-$dst { description = "Flattened Device Tree blob $dst"; data = /incbin/("$output_dir/arch/$arch/boot/dts/$src"); type = "flat_dt"; arch = "$arch"; compression = "none"; hash-1 { algo = "crc32"; }; hash-2 { algo = "sha1"; }; }; EOF done local config=$(echo $DTBS | sed 's/.*[:/]//') cat <> $kernel_its }; configurations { default = "conf-$config"; EOF for dtb in $DTBS ; do local dst=${dtb/*:/} dst=${dst/*\//} cat <> $kernel_its conf-$dst { description = "Boot Linux kernel with $dst blob"; kernel = "kernel"; fdt = "fdt-$dst"; }; EOF done cat <> $kernel_its }; }; EOF PATH="$output_dir/scripts/dtc:$PATH" mkimage -f $kernel_its ${kernel_its/its/itb} } make_srec_image() { local image=$output_dir/arch/$arch/boot/$image_kernel cat $image | lzma -z -e > $image.xz objcopy -I binary -O srec --srec-forceS3 --srec-len 516 \ $image.xz $image.xz.srec for dtb in $DTBS ; do local src=${dtb/:*/} objcopy -I binary -O srec --srec-forceS3 --srec-len 516 \ $src $src.srec done } make_uboot_image() { mkimage -A $arch -O linux -T kernel -C none -a $LOADADDR -e $LOADADDR \ -n "Linux-${version}" -d $output_dir/arch/$arch/boot/$kernel_image \ $output_dir/arch/$arch/boot/uImage } make_cros_image() { local boot_dir=$output_dir/arch/$arch/boot local vmlinuz=$boot_dir/$1 local bootloader local config if [[ -f $opt_bootloader ]] ; then bootloader=$opt_bootloader else dd if=/dev/zero of=$boot_dir/bootloader bs=512 count=1 bootloader=$boot_dir/bootloader fi if [[ -f $opt_cmdline_file ]] ; then config=$opt_cmdline_file else echo "$CMDLINE" > $boot_dir/cmdline config=$boot_dir/cmdline fi vbutil_kernel \ --pack $boot_dir/vmlinuz.image \ --version 1 \ --vmlinuz $vmlinuz \ --arch $arch \ --keyblock /usr/share/vboot/devkeys/kernel.keyblock \ --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \ --config $config \ --bootloader $bootloader } case $IMAGE in FIT) make_fit_image image_file=$output_dir/arch/$arch/boot/kernel_fdt.itb ;; cros-arm) make_fit_image make_cros_image kernel_fdt.itb image_file=$output_dir/arch/$arch/boot/vmlinuz.image ;; cros-x86) make_fit_image make_cros_image bzImage image_file=$output_dir/arch/$arch/boot/vmlinuz.image ;; srec) make_srec_image image_file=$output_dir/arch/$arch/boot/$kernel_image.xz.srec ;; uImage) make_uboot_image image_file=$output_dir/arch/$arch/boot/uImage ;; *) image_file=$output_dir/arch/$arch/boot/$IMAGE ;; esac # # Copy the files to their destination directories # copy_dtbs() { local dtb local target_dir=$1 for dtb in $DTBS ; do local src=${dtb/:*/} local dst=${dtb/*:/} dst=${dst/*\//} cp $output_dir/arch/${ARCH}/boot/dts/$src* $target_dir/$dst done } mkdir -p $BOOT_DIR cp $image_file $BOOT_DIR/ if [[ $IMAGE != FIT && $IMAGE != cros-arm && $IMAGE != cros-x86 ]] ; then copy_dtbs $BOOT_DIR/ fi if [[ x$modules = xmodules ]] ; then $pmake ARCH=$arch O=$output_dir CROSS_COMPILE=$cross_compile INSTALL_MOD_PATH=$target_dir modules_install echo "Kernel modules installed to $target_dir/lib/modules/$version" fi if [[ $do_package_deb = 1 ]] ; then $pmake ARCH=$arch O=$output_dir CROSS_COMPILE=$cross_compile bindeb-pkg fi echo "Kernel image available in $image_file" echo "Build $version completed at $(date)"