build-kernel.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. # Download, configure and build the mainline kernel for the selected board type
  3. # (./select-board.sh), plus the board's device trees, overlays and out-of-tree
  4. # modules (boards/<board>.sh). Result: out/<board>/<board>-kernel-<release>.tar.gz
  5. # with boot/<subdir>/<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-common.config
  11. # + files/<board>/kernel.config
  12. # menuconfig edit the current .config (kept until "config" is run again)
  13. # build kernel, in-tree modules and DTBs only
  14. # modules only the out-of-tree modules (needs a built kernel)
  15. # package only stage and pack the results
  16. # clean remove the kernel build directory and staging
  17. set -euo pipefail
  18. . "$(dirname "$0")/config.sh"
  19. KBUILD="$OUT/kbuild"
  20. STAGE="$KOUT/stage"
  21. DTSDIR="arch/arm/boot/dts/allwinner"
  22. FRAGMENTS=("$FILES/kernel-common.config" "$BFILES/kernel.config")
  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. [ "$KERNEL_IMAGE" != uImage ] || need_tools mkimage
  27. local tarball="linux-$KERNEL_VERSION.tar.xz"
  28. local major="${KERNEL_VERSION%%.*}"
  29. local base="https://cdn.kernel.org/pub/linux/kernel/v$major.x"
  30. if [ ! -d "$KSRC" ]; then
  31. fetch "$DL/$tarball" "$base/$tarball"
  32. rm -f "$DL/sha256sums-v$major.asc"
  33. fetch "$DL/sha256sums-v$major.asc" "$base/sha256sums.asc"
  34. info "verifying $tarball"
  35. (cd "$DL" && grep " $tarball\$" "sha256sums-v$major.asc" | sha256sum -c -) ||
  36. die "checksum mismatch for $tarball"
  37. mkdir -p "$SRC"
  38. tar xJf "$DL/$tarball" -C "$SRC"
  39. fi
  40. # The board DTS files live in files/<board>/dts/; link them next to the
  41. # mainline ones they include. The source tree is shared by all board
  42. # types, so the links of the other boards are there too (harmless).
  43. local d
  44. for d in "$BFILES"/dts/*.dts "$BFILES"/dts/*.dtsi; do
  45. [ -e "$d" ] || continue
  46. ln -sf "$d" "$KSRC/$DTSDIR/$(basename "$d")"
  47. done
  48. if declare -F board_prepare_source >/dev/null; then board_prepare_source; fi
  49. }
  50. do_config() {
  51. mkdir -p "$KBUILD"
  52. info "configuring: sunxi_defconfig + ${FRAGMENTS[*]#"$TOP"/}"
  53. kmake sunxi_defconfig
  54. "$KSRC/scripts/kconfig/merge_config.sh" -m -O "$KBUILD" \
  55. "$KBUILD/.config" "${FRAGMENTS[@]}" >/dev/null
  56. kmake olddefconfig
  57. check_fragment "$KBUILD/.config" "${FRAGMENTS[@]}"
  58. local mods
  59. mods="$(grep -c '=m$' "$KBUILD/.config" || true)"
  60. info "$mods options are modules (=m): grep '=m\$' $KBUILD/.config"
  61. }
  62. do_build() {
  63. [ -f "$KBUILD/.config" ] || do_config
  64. info "building kernel $KERNEL_VERSION ($KERNEL_IMAGE)"
  65. if [ "$KERNEL_IMAGE" = uImage ]; then
  66. kmake LOADADDR="$UIMAGE_LOADADDR" uImage modules
  67. # The environment loads the uImage to 0x47000000 and U-Boot
  68. # relocates the DTB below 0x4a000000 (bootm_size=0xa000000):
  69. # keep the image well inside those 48 MiB.
  70. local size; size=$(stat -c %s "$KBUILD/arch/arm/boot/uImage")
  71. [ "$size" -lt $(( 32 * 1024 * 1024 )) ] ||
  72. die "uImage is $size bytes; over 32 MiB it may collide with the relocated DTB"
  73. else
  74. kmake "$KERNEL_IMAGE" modules
  75. fi
  76. # Only this board's device trees, with symbols so overlays can be applied.
  77. local dtb targets=()
  78. for dtb in $BOARD_DTBS; do targets+=("allwinner/$dtb"); done
  79. kmake DTC_FLAGS=-@ "${targets[@]}"
  80. }
  81. krelease() { cat "$KBUILD/include/config/kernel.release"; }
  82. do_modules() {
  83. [ -f "$KBUILD/vmlinux" ] || die "kernel not built yet"
  84. if declare -F board_build_extmod >/dev/null; then
  85. board_build_extmod
  86. else
  87. info "no out-of-tree modules for this board"
  88. fi
  89. }
  90. do_package() {
  91. need_tools depmod dtc
  92. local rel; rel="$(krelease)"
  93. local boot="$STAGE/boot/$BOOT_SUBDIR/$rel" dtb
  94. info "staging $rel"
  95. rm -rf "$STAGE"
  96. mkdir -p "$boot"
  97. kmake INSTALL_MOD_PATH="$STAGE" INSTALL_MOD_STRIP=1 modules_install
  98. if declare -F board_install_extmod >/dev/null; then board_install_extmod; fi
  99. # The build/source links point into this PC's tree; useless on the target.
  100. rm -f "$STAGE/lib/modules/$rel/build" "$STAGE/lib/modules/$rel/source"
  101. depmod -b "$STAGE" "$rel"
  102. cp "$KBUILD/arch/arm/boot/$KERNEL_IMAGE" "$boot/$KERNEL_IMAGE"
  103. for dtb in $BOARD_DTBS; do cp "$KBUILD/$DTSDIR/$dtb" "$boot/"; done
  104. cp "$KBUILD/.config" "$boot/config"
  105. cp "$KBUILD/System.map" "$boot/System.map"
  106. if declare -F board_package >/dev/null; then board_package "$boot"; fi
  107. local tarball="$OUT/$BOARD-kernel-$rel.tar.gz"
  108. tar czf "$tarball" --owner=0 --group=0 -C "$STAGE" boot lib
  109. echo "$rel" > "$OUT/kernel-release"
  110. info "done ($BOARD_NAME): $tarball"
  111. ls -la "$boot"
  112. info "modules: $(find "$STAGE/lib/modules/$rel" -name '*.ko*' | wc -l)"
  113. }
  114. cmd="${1:-all}"
  115. case "$cmd" in
  116. all) prepare_source; do_build; do_modules; do_package ;;
  117. config) prepare_source; do_config ;;
  118. menuconfig) prepare_source; [ -f "$KBUILD/.config" ] || do_config; kmake menuconfig ;;
  119. build) prepare_source; do_build ;;
  120. modules) do_modules ;;
  121. package) do_package ;;
  122. clean) rm -rf "$KBUILD" "$KOUT" ;;
  123. *) die "unknown command: $cmd" ;;
  124. esac