sdk.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. # Downloads an OpenWrt SDK and verifies it against the target sha256sums
  3. # before extracting it. Sourced by package*.sh.
  4. #
  5. # Usage:
  6. # SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
  7. download_verified_sdk() {
  8. local dir_url="$1"
  9. local work_dir="$2"
  10. local name sums line sdk_path
  11. sums="$work_dir/sha256sums"
  12. if ! curl -fsSL "${dir_url}sha256sums" -o "$sums"; then
  13. echo "ERROR: no sha256sums under ${dir_url}" >&2
  14. return 1
  15. fi
  16. name="$(curl -fsSL "$dir_url" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
  17. if [ -z "$name" ]; then
  18. echo "ERROR: SDK not found under ${dir_url}" >&2
  19. return 1
  20. fi
  21. line="$(awk -v n="$name" '
  22. {
  23. candidate = $NF
  24. sub(/^\*/, "", candidate)
  25. if (candidate == n) {
  26. print
  27. found = 1
  28. }
  29. }
  30. END { exit found ? 0 : 1 }
  31. ' "$sums")" || {
  32. echo "ERROR: ${name} is not listed in sha256sums" >&2
  33. return 1
  34. }
  35. printf '%s\n' "$line" > "$work_dir/sdk.sha256"
  36. echo " SDK: ${dir_url}${name}" >&2
  37. sdk_path="$work_dir/$name"
  38. if ! curl -fsSL "${dir_url}${name}" -o "$sdk_path"; then
  39. echo "ERROR: could not download ${name}" >&2
  40. return 1
  41. fi
  42. if ! (cd "$work_dir" && sha256sum -c sdk.sha256) >&2; then
  43. echo "ERROR: SDK checksum mismatch for ${name}" >&2
  44. return 1
  45. fi
  46. if ! tar --zstd -xf "$sdk_path" -C "$work_dir"; then
  47. echo "ERROR: could not extract ${name}" >&2
  48. return 1
  49. fi
  50. printf '%s\n' "$work_dir/${name%.tar.zst}"
  51. }
  52. # verify_ipk_modes <ipk> <path>=<octal-mode>... checks that the packaged files
  53. # carry the expected permission bits inside data.tar.gz (#1701: ipkg-build
  54. # does not fix permissions, and a non-executable /etc/init.d script breaks
  55. # enable/start on the router). Uses ar + tar, both present on CI hosts.
  56. verify_ipk_modes() {
  57. local ipk="$1"
  58. shift
  59. local spec path want got line
  60. if [ ! -f "$ipk" ]; then
  61. echo "ERROR: verify_ipk_modes: ipk not found: $ipk" >&2
  62. exit 1
  63. fi
  64. echo " Verifying packaged file modes in $(basename "$ipk")..."
  65. for spec in "$@"; do
  66. path="${spec%%=*}"
  67. want="${spec##*=}"
  68. got="$(ar p "$ipk" data.tar.gz | tar -tvzf - 2>/dev/null \
  69. | awk -v p="$path" '$NF ~ ("(^|/)" p "$") {print $1; exit}')"
  70. if [ -z "$got" ]; then
  71. echo "ERROR: $path not found inside the ipk" >&2
  72. exit 1
  73. fi
  74. case "$want" in
  75. 755) line="rwxr-xr-x" ;;
  76. 644) line="rw-r--r--" ;;
  77. *) echo "ERROR: verify_ipk_modes: unsupported mode $want" >&2; exit 1 ;;
  78. esac
  79. if [ "$got" != "-$line" ]; then
  80. echo "ERROR: $path has mode $got, want -$line ($want)" >&2
  81. exit 1
  82. fi
  83. echo " $path: $got OK"
  84. done
  85. }