#!/bin/bash # Download, configure and build the mainline kernel for the Orange Pi Zero, # plus the out-of-tree modules (xradio Wi-Fi, s1d13700-tca radio panel) and # the radio device tree overlays (s1d13700-tca, radio-keyboard-tca). Result: out/opi0-kernel-.tar.gz # with boot/opi0// 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.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" BOARD_DTS="$FILES/dts/sun8i-h2-plus-orangepi-zero-radio.dts" BOARD_DTB="sun8i-h2-plus-orangepi-zero-radio.dtb" DTSDIR="arch/arm/boot/dts/allwinner" 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 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 if [ ! -d "$XSRC" ]; then local xt="$DL/xradio-$XRADIO_COMMIT.tar.gz" fetch "$xt" "https://github.com/$XRADIO_REPO/archive/$XRADIO_COMMIT.tar.gz" mkdir -p "$XSRC" tar xzf "$xt" -C "$XSRC" --strip-components=1 fi # The board DTS lives in files/; link it next to the mainline one it includes. ln -sf "$BOARD_DTS" "$KSRC/$DTSDIR/$(basename "$BOARD_DTS")" sync_radio_driver } # Put the radio panel driver into the kernel tree as a DRM tiny driver, so it # can be built in (needed for the boot console on the panel). The sources are # copied from the s1d13700-tca repository ($PANEL_DIR) on every run; only # Kconfig/Makefile are ours. sync_radio_driver() { local rd="$PANEL_DIR/driver" td="$KSRC/drivers/gpu/drm/tiny/s1d13700-tca" [ -f "$rd/s1d13700_drm.c" ] || die "s1d13700-tca driver not found in $rd" mkdir -p "$td" # cp -u keeps timestamps sane, so unchanged sources are not rebuilt cp -u "$rd"/s1d13700_drm.c "$rd"/s1d13700_core.c "$rd"/s1d13700_core.h "$td/" cat > "$td/Kconfig.new" <<-'EOF' # Generated by build-kernel.sh (OPi0); sources from the s1d13700-tca repository. config DRM_S1D13700_TCA tristate "S1D13700 panel behind a TCA9539 I2C expander (radio)" depends on DRM && I2C select DRM_CLIENT_SELECTION select DRM_KMS_HELPER select DRM_GEM_SHMEM_HELPER select BACKLIGHT_CLASS_DEVICE help Epson S1D13700 graphic LCD controller driven through a TCA9539 I2C GPIO expander, as on the radio front panel. EOF cat > "$td/Makefile.new" <<-'EOF' # Generated by build-kernel.sh (OPi0); sources from the s1d13700-tca repository. obj-$(CONFIG_DRM_S1D13700_TCA) += s1d13700-tca.o s1d13700-tca-y := s1d13700_drm.o s1d13700_core.o # API compatibility for the DRM part only (see s1d13700_compat.h). CFLAGS_s1d13700_drm.o += -include $(src)/s1d13700_compat.h EOF # Kernel 7.3 renamed struct drm_atomic_state to struct drm_atomic_commit; # the driver uses the old name, only as the type of the atomic hooks' # argument. The DRM headers are included before the rename is defined, # so it applies to the driver's own code only, never inside a kernel # header. s1d13700_compat_check() stops the build if the new struct is # not a complete type with the expected members (rename reverted or # done differently): adjust the version check then, rather than build # against the wrong type. Below 7.3 the header does nothing. cat > "$td/s1d13700_compat.h.new" <<-'EOF' /* Generated by build-kernel.sh (OPi0). Forced in with -include. */ #ifndef S1D13700_COMPAT_H #define S1D13700_COMPAT_H #include #if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 3, 0) && !defined(drm_atomic_state) #include #include #include #include #include #include #define drm_atomic_state drm_atomic_commit /* Fails if the headers above still define the old struct. */ struct drm_atomic_commit; static inline void s1d13700_compat_check(struct drm_atomic_commit *c) { (void)sizeof(c->dev); } #endif #endif /* S1D13700_COMPAT_H */ EOF local f for f in Kconfig Makefile s1d13700_compat.h; do cmp -s "$td/$f.new" "$td/$f" && rm "$td/$f.new" || mv "$td/$f.new" "$td/$f" done local tk="$KSRC/drivers/gpu/drm/tiny/Kconfig" tm="$KSRC/drivers/gpu/drm/tiny/Makefile" grep -q 's1d13700-tca/Kconfig' "$tk" || printf '\nsource "drivers/gpu/drm/tiny/s1d13700-tca/Kconfig"\n' >> "$tk" grep -q 's1d13700-tca/' "$tm" || echo 'obj-$(CONFIG_DRM_S1D13700_TCA) += s1d13700-tca/' >> "$tm" } do_config() { mkdir -p "$KBUILD" info "configuring: sunxi_defconfig + files/kernel.config" kmake sunxi_defconfig "$KSRC/scripts/kconfig/merge_config.sh" -m -O "$KBUILD" \ "$KBUILD/.config" "$FILES/kernel.config" >/dev/null kmake olddefconfig check_fragment "$FILES/kernel.config" "$KBUILD/.config" } do_build() { [ -f "$KBUILD/.config" ] || do_config info "building kernel $KERNEL_VERSION" kmake zImage modules dtbs kmake DTC_FLAGS=-@ "allwinner/$BOARD_DTB" } krelease() { cat "$KBUILD/include/config/kernel.release"; } do_modules() { [ -f "$KBUILD/vmlinux" ] || die "kernel not built yet" # xradio: built out of tree in its own (extracted) source directory. info "building xradio (XR819 Wi-Fi)" kmake M="$XSRC" modules } # keys_of DTB NODE -> one "label pin flags keycode" line per key, sorted keys_of() { local n for n in $(fdtget -l "$1" "$2"); do printf '%s %s %s\n' "$(fdtget -t s "$1" "$2/$n" label)" \ "$(fdtget -t u "$1" "$2/$n" gpios | cut -d' ' -f2,3)" \ "$(fdtget -t u "$1" "$2/$n" linux,code)" done | sort } # The key table exists twice: in files/dts (board DTB) and in radio-keyboard-tca's # dts/radio-keys.dts, which deliberately avoids #include so it # also works with armbian-add-overlay. Compare the compiled tables instead # of sharing a .dtsi: label, U2 pin, flags and key code must all match. check_key_tables() { local boot="$1" ov="$1/overlay/radio-keys.dtbo" [ -f "$ov" ] || return 0 if ! diff -u <(keys_of "$boot/$BOARD_DTB" /radio-keys) \ <(keys_of "$ov" /fragment@1/__overlay__/radio-keys) >&2; then die "key tables differ: files/dts/$(basename "$BOARD_DTS") vs $KEYBOARD_DIR/dts/radio-keys.dts (-board +overlay)" fi info "key tables match (board DTB and radio-keys overlay)" } do_package() { need_tools depmod dtc fdtget local rel; rel="$(krelease)" local boot="$STAGE/boot/opi0/$rel" info "staging $rel" rm -rf "$STAGE" mkdir -p "$boot/overlay" kmake INSTALL_MOD_PATH="$STAGE" INSTALL_MOD_STRIP=1 modules_install kmake INSTALL_MOD_PATH="$STAGE" INSTALL_MOD_STRIP=1 M="$XSRC" modules_install # 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/zImage" "$boot/zImage" cp "$KBUILD/$DTSDIR/$BOARD_DTB" "$boot/" cp "$KBUILD/$DTSDIR/$KERNEL_DTB" "$boot/" cp "$KBUILD/.config" "$boot/config" cp "$KBUILD/System.map" "$boot/System.map" # Panel and key overlays (dts/*.dts in s1d13700-tca and # radio-keyboard-tca), for extlinux fdtoverlays. local d for d in "$PANEL_DIR"/dts/*.dts "$KEYBOARD_DIR"/dts/*.dts; do [ -e "$d" ] || continue dtc -q -@ -I dts -O dtb -o "$boot/overlay/$(basename "${d%.dts}").dtbo" "$d" done check_key_tables "$boot" local tarball="$OUT/opi0-kernel-$rel.tar.gz" tar czf "$tarball" --owner=0 --group=0 -C "$STAGE" boot lib echo "$rel" > "$OUT/kernel-release" info "done: $tarball" ls -la "$boot" "$boot/overlay" find "$STAGE/lib/modules/$rel/updates" -name '*.ko*' } 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" "$OUT/extmod" ;; *) die "unknown command: $cmd" ;; esac