config.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Shared settings for all scripts. Sourced by the other scripts; override any
  2. # of these from the environment.
  3. #
  4. # The board type is picked with ./select-board.sh and kept in .board; BOARD=<id>
  5. # in the environment overrides it for one command. Board specifics (versions,
  6. # toolchain, defconfigs, device trees, install method, build hooks) are in
  7. # boards/<id>.sh, config fragments and device trees in files/<id>/.
  8. TOP="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  9. BOARDS_DIR="$TOP/boards"
  10. BOARD_SELECTION="$TOP/.board"
  11. die() { echo "ERROR: $*" >&2; exit 1; }
  12. info() { echo ">>> $*"; }
  13. # list_boards -> "id<TAB>name" per supported board
  14. list_boards() {
  15. local f
  16. for f in "$BOARDS_DIR"/*.sh; do
  17. [ -e "$f" ] || continue
  18. printf '%s\t%s\n' "$(basename "$f" .sh)" \
  19. "$(sed -n 's/^BOARD_NAME="\(.*\)"$/\1/p' "$f" | head -1)"
  20. done
  21. }
  22. # ---- board selection --------------------------------------------------------
  23. # select-board.sh sets CONFIG_NO_BOARD=1: it must work without a selection.
  24. if [ "${CONFIG_NO_BOARD:-0}" != 1 ]; then
  25. if [ -n "${BOARD:-}" ]; then
  26. BOARD_FROM="BOARD from the environment"
  27. else
  28. BOARD="$(cat "$BOARD_SELECTION" 2>/dev/null || true)"
  29. BOARD_FROM="selected with select-board.sh"
  30. fi
  31. [ -n "$BOARD" ] || die "no board type selected; run ./select-board.sh first"
  32. [ -f "$BOARDS_DIR/$BOARD.sh" ] ||
  33. die "unknown board type '$BOARD' (known: $(list_boards | cut -f1 | tr '\n' ' '))"
  34. . "$BOARDS_DIR/$BOARD.sh"
  35. # ---- defaults the board file did not set --------------------------
  36. : "${KERNEL_VERSION:=7.3.4}"
  37. : "${UBOOT_VERSION:=v2026.10}"
  38. : "${JOBS:=$(nproc)}"
  39. export ARCH=arm CROSS_COMPILE
  40. # ---- directories --------------------------------------------------
  41. DL="$TOP/dl" # downloaded tarballs (shared)
  42. SRC="$TOP/src" # extracted sources (shared, built out of tree)
  43. OUT="$TOP/out/$BOARD" # build results of this board type
  44. BACKUP_DIR="$TOP/out/backup" # bootloader areas saved by write-uboot.sh (all boards)
  45. FILES="$TOP/files" # config fragments shared by all boards
  46. BFILES="$FILES/$BOARD" # this board's fragments, device trees, overlays
  47. KSRC="$SRC/linux-$KERNEL_VERSION"
  48. USRC="$SRC/u-boot-${UBOOT_VERSION#v}"
  49. KOUT="$OUT/kernel" # staging: boot/ and lib/modules/
  50. UOUT="$OUT/u-boot"
  51. # Every command states which board type it works for.
  52. if [ -t 2 ]; then
  53. printf '\033[1;36m=== board: %s [%s] (%s)\033[0m\n' "$BOARD_NAME" "$BOARD" "$BOARD_FROM" >&2
  54. else
  55. printf '=== board: %s [%s] (%s)\n' "$BOARD_NAME" "$BOARD" "$BOARD_FROM" >&2
  56. fi
  57. fi
  58. # Fail early if a required program is missing; don't install anything.
  59. need_tools() {
  60. local t missing=()
  61. for t in "$@"; do command -v "$t" >/dev/null 2>&1 || missing+=("$t"); done
  62. [ ${#missing[@]} -eq 0 ] || die "missing tools: ${missing[*]} (install them and re-run)"
  63. }
  64. # check_fragment DOTCONFIG FRAGMENT...
  65. # Every option in the config fragments must have survived olddefconfig; a
  66. # renamed symbol or a missing dependency silently drops options otherwise.
  67. # Fragments are read in order and a later one overrides an earlier one, as
  68. # with merge_config.sh.
  69. check_fragment() {
  70. local dotconfig="$1"; shift
  71. local -A want=() from=()
  72. local -a order=()
  73. local frag line sym val bad=0 have
  74. for frag in "$@"; do
  75. while IFS= read -r line; do
  76. case "$line" in
  77. CONFIG_*=*) sym="${line%%=*}"; val="${line#*=}" ;;
  78. "# CONFIG_"*" is not set") sym="${line#\# }"; sym="${sym%% *}"; val=n ;;
  79. *) continue ;;
  80. esac
  81. [ -n "${want[$sym]+x}" ] || order+=("$sym")
  82. want[$sym]="$val"; from[$sym]="${frag#"$TOP"/}"
  83. done < "$frag"
  84. done
  85. for sym in "${order[@]}"; do
  86. have="$(grep -E "^$sym=" "$dotconfig" | cut -d= -f2- || true)"
  87. [ -n "$have" ] || have=n
  88. if [ "$have" != "${want[$sym]}" ]; then
  89. echo " config: $sym wanted ${want[$sym]} (${from[$sym]}), got $have" >&2
  90. bad=1
  91. fi
  92. done
  93. [ $bad -eq 0 ] || die ".config does not match the fragments (see above)"
  94. info "config check passed ($(for frag in "$@"; do printf '%s ' "${frag#"$TOP"/}"; done))"
  95. }
  96. # fetch DEST URL [fallback URL...]
  97. fetch() {
  98. local dest="$1"; shift
  99. [ -s "$dest" ] && return 0
  100. mkdir -p "$(dirname "$dest")"
  101. local url
  102. for url in "$@"; do
  103. info "downloading $url"
  104. if curl -fL --connect-timeout 20 --retry 2 -o "$dest.part" "$url"; then
  105. mv "$dest.part" "$dest"
  106. return 0
  107. fi
  108. done
  109. rm -f "$dest.part"
  110. die "could not download $(basename "$dest")"
  111. }