build-kernel.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. # Download, configure and build the mainline kernel for the Orange Pi Zero,
  3. # plus the out-of-tree modules (xradio Wi-Fi, s1d13700-tca radio panel) and
  4. # the radio device tree overlays (s1d13700-tca, radio-keyboard-tca). Result: out/opi0-kernel-<release>.tar.gz
  5. # with boot/opi0/<release>/ and lib/modules/<release>/, ready for
  6. # install-kernel.sh.
  7. #
  8. # Usage: ./build-kernel.sh [all|config|menuconfig|build|modules|package|clean]
  9. # all (default) config if needed, build, modules, package
  10. # config regenerate .config from sunxi_defconfig + files/kernel.config
  11. # menuconfig edit the current .config (kept until "config" is run again)
  12. # build kernel, in-tree modules and DTBs only
  13. # modules only the out-of-tree modules (needs a built kernel)
  14. # package only stage and pack the results
  15. # clean remove the kernel build directory and staging
  16. set -euo pipefail
  17. . "$(dirname "$0")/config.sh"
  18. KBUILD="$OUT/kbuild"
  19. STAGE="$KOUT/stage"
  20. BOARD_DTS="$FILES/dts/sun8i-h2-plus-orangepi-zero-radio.dts"
  21. BOARD_DTB="sun8i-h2-plus-orangepi-zero-radio.dtb"
  22. DTSDIR="arch/arm/boot/dts/allwinner"
  23. kmake() { make -C "$KSRC" O="$KBUILD" -j"$JOBS" "$@"; }
  24. prepare_source() {
  25. need_tools curl tar xz sha256sum make "${CROSS_COMPILE}gcc" bison flex bc perl dtc
  26. local tarball="linux-$KERNEL_VERSION.tar.xz"
  27. local major="${KERNEL_VERSION%%.*}"
  28. local base="https://cdn.kernel.org/pub/linux/kernel/v$major.x"
  29. if [ ! -d "$KSRC" ]; then
  30. fetch "$DL/$tarball" "$base/$tarball"
  31. rm -f "$DL/sha256sums-v$major.asc"
  32. fetch "$DL/sha256sums-v$major.asc" "$base/sha256sums.asc"
  33. info "verifying $tarball"
  34. (cd "$DL" && grep " $tarball\$" "sha256sums-v$major.asc" | sha256sum -c -) ||
  35. die "checksum mismatch for $tarball"
  36. mkdir -p "$SRC"
  37. tar xJf "$DL/$tarball" -C "$SRC"
  38. fi
  39. if [ ! -d "$XSRC" ]; then
  40. local xt="$DL/xradio-$XRADIO_COMMIT.tar.gz"
  41. fetch "$xt" "https://github.com/$XRADIO_REPO/archive/$XRADIO_COMMIT.tar.gz"
  42. mkdir -p "$XSRC"
  43. tar xzf "$xt" -C "$XSRC" --strip-components=1
  44. fi
  45. # The board DTS lives in files/; link it next to the mainline one it includes.
  46. ln -sf "$BOARD_DTS" "$KSRC/$DTSDIR/$(basename "$BOARD_DTS")"
  47. sync_radio_driver
  48. }
  49. # Put the radio panel driver into the kernel tree as a DRM tiny driver, so it
  50. # can be built in (needed for the boot console on the panel). The sources are
  51. # copied from the s1d13700-tca repository ($PANEL_DIR) on every run; only
  52. # Kconfig/Makefile are ours.
  53. sync_radio_driver() {
  54. local rd="$PANEL_DIR/driver" td="$KSRC/drivers/gpu/drm/tiny/s1d13700-tca"
  55. [ -f "$rd/s1d13700_drm.c" ] || die "s1d13700-tca driver not found in $rd"
  56. mkdir -p "$td"
  57. # cp -u keeps timestamps sane, so unchanged sources are not rebuilt
  58. cp -u "$rd"/s1d13700_drm.c "$rd"/s1d13700_core.c "$rd"/s1d13700_core.h "$td/"
  59. cat > "$td/Kconfig.new" <<-'EOF'
  60. # Generated by build-kernel.sh (OPi0); sources from the s1d13700-tca repository.
  61. config DRM_S1D13700_TCA
  62. tristate "S1D13700 panel behind a TCA9539 I2C expander (radio)"
  63. depends on DRM && I2C
  64. select DRM_CLIENT_SELECTION
  65. select DRM_KMS_HELPER
  66. select DRM_GEM_SHMEM_HELPER
  67. select BACKLIGHT_CLASS_DEVICE
  68. help
  69. Epson S1D13700 graphic LCD controller driven through a TCA9539
  70. I2C GPIO expander, as on the radio front panel.
  71. EOF
  72. cat > "$td/Makefile.new" <<-'EOF'
  73. # Generated by build-kernel.sh (OPi0); sources from the s1d13700-tca repository.
  74. obj-$(CONFIG_DRM_S1D13700_TCA) += s1d13700-tca.o
  75. s1d13700-tca-y := s1d13700_drm.o s1d13700_core.o
  76. # API compatibility for the DRM part only (see s1d13700_compat.h).
  77. CFLAGS_s1d13700_drm.o += -include $(src)/s1d13700_compat.h
  78. EOF
  79. # Kernel 7.3 renamed struct drm_atomic_state to struct drm_atomic_commit;
  80. # the driver uses the old name, only as the type of the atomic hooks'
  81. # argument. The DRM headers are included before the rename is defined,
  82. # so it applies to the driver's own code only, never inside a kernel
  83. # header. s1d13700_compat_check() stops the build if the new struct is
  84. # not a complete type with the expected members (rename reverted or
  85. # done differently): adjust the version check then, rather than build
  86. # against the wrong type. Below 7.3 the header does nothing.
  87. cat > "$td/s1d13700_compat.h.new" <<-'EOF'
  88. /* Generated by build-kernel.sh (OPi0). Forced in with -include. */
  89. #ifndef S1D13700_COMPAT_H
  90. #define S1D13700_COMPAT_H
  91. #include <linux/version.h>
  92. #if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 3, 0) && !defined(drm_atomic_state)
  93. #include <drm/drm_atomic.h>
  94. #include <drm/drm_atomic_helper.h>
  95. #include <drm/drm_crtc.h>
  96. #include <drm/drm_gem_atomic_helper.h>
  97. #include <drm/drm_modeset_helper_vtables.h>
  98. #include <drm/drm_plane.h>
  99. #define drm_atomic_state drm_atomic_commit
  100. /* Fails if the headers above still define the old struct. */
  101. struct drm_atomic_commit;
  102. static inline void s1d13700_compat_check(struct drm_atomic_commit *c)
  103. {
  104. (void)sizeof(c->dev);
  105. }
  106. #endif
  107. #endif /* S1D13700_COMPAT_H */
  108. EOF
  109. local f
  110. for f in Kconfig Makefile s1d13700_compat.h; do
  111. cmp -s "$td/$f.new" "$td/$f" && rm "$td/$f.new" || mv "$td/$f.new" "$td/$f"
  112. done
  113. local tk="$KSRC/drivers/gpu/drm/tiny/Kconfig" tm="$KSRC/drivers/gpu/drm/tiny/Makefile"
  114. grep -q 's1d13700-tca/Kconfig' "$tk" ||
  115. printf '\nsource "drivers/gpu/drm/tiny/s1d13700-tca/Kconfig"\n' >> "$tk"
  116. grep -q 's1d13700-tca/' "$tm" ||
  117. echo 'obj-$(CONFIG_DRM_S1D13700_TCA) += s1d13700-tca/' >> "$tm"
  118. }
  119. do_config() {
  120. mkdir -p "$KBUILD"
  121. info "configuring: sunxi_defconfig + files/kernel.config"
  122. kmake sunxi_defconfig
  123. "$KSRC/scripts/kconfig/merge_config.sh" -m -O "$KBUILD" \
  124. "$KBUILD/.config" "$FILES/kernel.config" >/dev/null
  125. kmake olddefconfig
  126. check_fragment "$FILES/kernel.config" "$KBUILD/.config"
  127. }
  128. do_build() {
  129. [ -f "$KBUILD/.config" ] || do_config
  130. info "building kernel $KERNEL_VERSION"
  131. kmake zImage modules dtbs
  132. kmake DTC_FLAGS=-@ "allwinner/$BOARD_DTB"
  133. }
  134. krelease() { cat "$KBUILD/include/config/kernel.release"; }
  135. do_modules() {
  136. [ -f "$KBUILD/vmlinux" ] || die "kernel not built yet"
  137. # xradio: built out of tree in its own (extracted) source directory.
  138. info "building xradio (XR819 Wi-Fi)"
  139. kmake M="$XSRC" modules
  140. }
  141. # keys_of DTB NODE -> one "label pin flags keycode" line per key, sorted
  142. keys_of() {
  143. local n
  144. for n in $(fdtget -l "$1" "$2"); do
  145. printf '%s %s %s\n' "$(fdtget -t s "$1" "$2/$n" label)" \
  146. "$(fdtget -t u "$1" "$2/$n" gpios | cut -d' ' -f2,3)" \
  147. "$(fdtget -t u "$1" "$2/$n" linux,code)"
  148. done | sort
  149. }
  150. # The key table exists twice: in files/dts (board DTB) and in radio-keyboard-tca's
  151. # dts/radio-keys.dts, which deliberately avoids #include so it
  152. # also works with armbian-add-overlay. Compare the compiled tables instead
  153. # of sharing a .dtsi: label, U2 pin, flags and key code must all match.
  154. check_key_tables() {
  155. local boot="$1" ov="$1/overlay/radio-keys.dtbo"
  156. [ -f "$ov" ] || return 0
  157. if ! diff -u <(keys_of "$boot/$BOARD_DTB" /radio-keys) \
  158. <(keys_of "$ov" /fragment@1/__overlay__/radio-keys) >&2; then
  159. die "key tables differ: files/dts/$(basename "$BOARD_DTS") vs $KEYBOARD_DIR/dts/radio-keys.dts (-board +overlay)"
  160. fi
  161. info "key tables match (board DTB and radio-keys overlay)"
  162. }
  163. do_package() {
  164. need_tools depmod dtc fdtget
  165. local rel; rel="$(krelease)"
  166. local boot="$STAGE/boot/opi0/$rel"
  167. info "staging $rel"
  168. rm -rf "$STAGE"
  169. mkdir -p "$boot/overlay"
  170. kmake INSTALL_MOD_PATH="$STAGE" INSTALL_MOD_STRIP=1 modules_install
  171. kmake INSTALL_MOD_PATH="$STAGE" INSTALL_MOD_STRIP=1 M="$XSRC" modules_install
  172. # The build/source links point into this PC's tree; useless on the target.
  173. rm -f "$STAGE/lib/modules/$rel/build" "$STAGE/lib/modules/$rel/source"
  174. depmod -b "$STAGE" "$rel"
  175. cp "$KBUILD/arch/arm/boot/zImage" "$boot/zImage"
  176. cp "$KBUILD/$DTSDIR/$BOARD_DTB" "$boot/"
  177. cp "$KBUILD/$DTSDIR/$KERNEL_DTB" "$boot/"
  178. cp "$KBUILD/.config" "$boot/config"
  179. cp "$KBUILD/System.map" "$boot/System.map"
  180. # Panel and key overlays (dts/*.dts in s1d13700-tca and
  181. # radio-keyboard-tca), for extlinux fdtoverlays.
  182. local d
  183. for d in "$PANEL_DIR"/dts/*.dts "$KEYBOARD_DIR"/dts/*.dts; do
  184. [ -e "$d" ] || continue
  185. dtc -q -@ -I dts -O dtb -o "$boot/overlay/$(basename "${d%.dts}").dtbo" "$d"
  186. done
  187. check_key_tables "$boot"
  188. local tarball="$OUT/opi0-kernel-$rel.tar.gz"
  189. tar czf "$tarball" --owner=0 --group=0 -C "$STAGE" boot lib
  190. echo "$rel" > "$OUT/kernel-release"
  191. info "done: $tarball"
  192. ls -la "$boot" "$boot/overlay"
  193. find "$STAGE/lib/modules/$rel/updates" -name '*.ko*'
  194. }
  195. cmd="${1:-all}"
  196. case "$cmd" in
  197. all) prepare_source; do_build; do_modules; do_package ;;
  198. config) prepare_source; do_config ;;
  199. menuconfig) prepare_source; [ -f "$KBUILD/.config" ] || do_config; kmake menuconfig ;;
  200. build) prepare_source; do_build ;;
  201. modules) do_modules ;;
  202. package) do_package ;;
  203. clean) rm -rf "$KBUILD" "$KOUT" "$OUT/extmod" ;;
  204. *) die "unknown command: $cmd" ;;
  205. esac