#!/bin/bash # Download, configure and build the mainline kernel for the selected board type # (./select-board.sh), plus the board's device trees, overlays and out-of-tree # modules (boards/.sh). Result: out//-kernel-.tar.gz # with boot/// and lib/modules//, ready for # install-kernel.sh. # # Usage: ./build-kernel.sh [all|config|menuconfig|build|modules|package|clean] # all (default) config if needed, build, modules, package # config regenerate .config from sunxi_defconfig + files/kernel-common.config # + files//kernel.config # menuconfig edit the current .config (kept until "config" is run again) # build kernel, in-tree modules and DTBs only # modules only the out-of-tree modules (needs a built kernel) # package only stage and pack the results # clean remove the kernel build directory and staging set -euo pipefail . "$(dirname "$0")/config.sh" KBUILD="$OUT/kbuild" STAGE="$KOUT/stage" DTSDIR="arch/arm/boot/dts/allwinner" FRAGMENTS=("$FILES/kernel-common.config" "$BFILES/kernel.config") kmake() { make -C "$KSRC" O="$KBUILD" -j"$JOBS" "$@"; } prepare_source() { need_tools curl tar xz sha256sum make "${CROSS_COMPILE}gcc" bison flex bc perl dtc [ "$KERNEL_IMAGE" != uImage ] || need_tools mkimage local tarball="linux-$KERNEL_VERSION.tar.xz" local major="${KERNEL_VERSION%%.*}" local base="https://cdn.kernel.org/pub/linux/kernel/v$major.x" if [ ! -d "$KSRC" ]; then fetch "$DL/$tarball" "$base/$tarball" rm -f "$DL/sha256sums-v$major.asc" fetch "$DL/sha256sums-v$major.asc" "$base/sha256sums.asc" info "verifying $tarball" (cd "$DL" && grep " $tarball\$" "sha256sums-v$major.asc" | sha256sum -c -) || die "checksum mismatch for $tarball" mkdir -p "$SRC" tar xJf "$DL/$tarball" -C "$SRC" fi # The board DTS files live in files//dts/; link them next to the # mainline ones they include. The source tree is shared by all board # types, so the links of the other boards are there too (harmless). local d for d in "$BFILES"/dts/*.dts "$BFILES"/dts/*.dtsi; do [ -e "$d" ] || continue ln -sf "$d" "$KSRC/$DTSDIR/$(basename "$d")" done if declare -F board_prepare_source >/dev/null; then board_prepare_source; fi } do_config() { mkdir -p "$KBUILD" info "configuring: sunxi_defconfig + ${FRAGMENTS[*]#"$TOP"/}" kmake sunxi_defconfig "$KSRC/scripts/kconfig/merge_config.sh" -m -O "$KBUILD" \ "$KBUILD/.config" "${FRAGMENTS[@]}" >/dev/null kmake olddefconfig check_fragment "$KBUILD/.config" "${FRAGMENTS[@]}" local mods mods="$(grep -c '=m$' "$KBUILD/.config" || true)" info "$mods options are modules (=m): grep '=m\$' $KBUILD/.config" } do_build() { [ -f "$KBUILD/.config" ] || do_config info "building kernel $KERNEL_VERSION ($KERNEL_IMAGE)" if [ "$KERNEL_IMAGE" = uImage ]; then kmake LOADADDR="$UIMAGE_LOADADDR" uImage modules # The environment loads the uImage to 0x47000000 and U-Boot # relocates the DTB below 0x4a000000 (bootm_size=0xa000000): # keep the image well inside those 48 MiB. local size; size=$(stat -c %s "$KBUILD/arch/arm/boot/uImage") [ "$size" -lt $(( 32 * 1024 * 1024 )) ] || die "uImage is $size bytes; over 32 MiB it may collide with the relocated DTB" else kmake "$KERNEL_IMAGE" modules fi # Only this board's device trees, with symbols so overlays can be applied. local dtb targets=() for dtb in $BOARD_DTBS; do targets+=("allwinner/$dtb"); done kmake DTC_FLAGS=-@ "${targets[@]}" } krelease() { cat "$KBUILD/include/config/kernel.release"; } do_modules() { [ -f "$KBUILD/vmlinux" ] || die "kernel not built yet" if declare -F board_build_extmod >/dev/null; then board_build_extmod else info "no out-of-tree modules for this board" fi } do_package() { need_tools depmod dtc local rel; rel="$(krelease)" local boot="$STAGE/boot/$BOOT_SUBDIR/$rel" dtb info "staging $rel" rm -rf "$STAGE" mkdir -p "$boot" kmake INSTALL_MOD_PATH="$STAGE" INSTALL_MOD_STRIP=1 modules_install if declare -F board_install_extmod >/dev/null; then board_install_extmod; fi # The build/source links point into this PC's tree; useless on the target. rm -f "$STAGE/lib/modules/$rel/build" "$STAGE/lib/modules/$rel/source" depmod -b "$STAGE" "$rel" cp "$KBUILD/arch/arm/boot/$KERNEL_IMAGE" "$boot/$KERNEL_IMAGE" for dtb in $BOARD_DTBS; do cp "$KBUILD/$DTSDIR/$dtb" "$boot/"; done cp "$KBUILD/.config" "$boot/config" cp "$KBUILD/System.map" "$boot/System.map" if declare -F board_package >/dev/null; then board_package "$boot"; fi local tarball="$OUT/$BOARD-kernel-$rel.tar.gz" tar czf "$tarball" --owner=0 --group=0 -C "$STAGE" boot lib echo "$rel" > "$OUT/kernel-release" info "done ($BOARD_NAME): $tarball" ls -la "$boot" info "modules: $(find "$STAGE/lib/modules/$rel" -name '*.ko*' | wc -l)" } cmd="${1:-all}" case "$cmd" in all) prepare_source; do_build; do_modules; do_package ;; config) prepare_source; do_config ;; menuconfig) prepare_source; [ -f "$KBUILD/.config" ] || do_config; kmake menuconfig ;; build) prepare_source; do_build ;; modules) do_modules ;; package) do_package ;; clean) rm -rf "$KBUILD" "$KOUT" ;; *) die "unknown command: $cmd" ;; esac