add-custom-hashes 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env bash
  2. set -e
  3. # Add hash files for packages with custom versions for
  4. # BR2_DOWNLOAD_FORCE_CHECK_HASHES=y
  5. #
  6. # Run in a configured Buildroot directory, E.G.
  7. # make foo_defconfig; ./utils/add-custom-hashes
  8. # print BR-style message
  9. # message <info message>
  10. message() {
  11. tput smso 2>/dev/null
  12. echo "$*"
  13. tput rmso 2>/dev/null
  14. }
  15. # print error message and exit
  16. # die <error message>
  17. die() {
  18. echo "Error: $*" >&2
  19. exit 1
  20. }
  21. # get package(s) for download file, if any
  22. # get_pkgs <json> <file>
  23. get_pkgs() {
  24. jq --arg file "$2" -r \
  25. 'to_entries[] | select(.value.downloads[0].source == $file) | .key | strings' "$1"
  26. }
  27. # get download dir for package
  28. # get_pkg_dl_dir <json> <package>
  29. get_pkg_dl_dir() {
  30. jq --arg pkg "$2" -r '.[$pkg].dl_dir | strings' "$1"
  31. }
  32. # generate hash file for download file
  33. # gen_hash <dir> <file>
  34. gen_hash() {
  35. (
  36. cd "$1" && printf '# Locally calculated\nsha256 ' && sha256sum "$2"
  37. )
  38. }
  39. command -v jq >/dev/null || die 'Script needs jq'
  40. [ -e .config ] || \
  41. die "No .config found, please run this in a configured Buildroot (O=) directory"
  42. message Collecting data
  43. eval "$(make -s VARS='TOPDIR DL_DIR BR_NO_CHECK_HASH_FOR BR2_GLOBAL_PATCH_DIR' QUOTED_VARS=YES printvars)"
  44. # global patch dir may already have quotes
  45. BR2_GLOBAL_PATCH_DIR=$(echo "$BR2_GLOBAL_PATCH_DIR" | tr -d '"')
  46. [ -n "$BR2_GLOBAL_PATCH_DIR" ] || die "No BR2_GLOBAL_PATCH_DIR defined, nothing to do"
  47. [ -n "$BR_NO_CHECK_HASH_FOR" ] || die "No packages without hashes found, nothing to do"
  48. [ -d "$TOPDIR" ] || die "TOPDIR ($TOPDIR) does not look correct"
  49. [ -d "$DL_DIR" ] || die "DL_DIR ($DL_DIR) does not look correct"
  50. # patch dir may contain multiple dirs, use the last one
  51. # shellcheck disable=SC2086 # we need the word splitting
  52. set -- $BR2_GLOBAL_PATCH_DIR
  53. if [ $# -gt 1 ]; then
  54. BR2_GLOBAL_PATCH_DIR="${!#}";
  55. message BR2_GLOBAL_PATCH_DIR contains multiple directories, using "$BR2_GLOBAL_PATCH_DIR"
  56. fi
  57. # patch dir may be relative to TOPDIR
  58. case "$BR2_GLOBAL_PATCH_DIR" in
  59. /*) ;;
  60. *) BR2_GLOBAL_PATCH_DIR="$TOPDIR/$BR2_GLOBAL_PATCH_DIR"
  61. ;;
  62. esac
  63. [ -d "$BR2_GLOBAL_PATCH_DIR" ] \
  64. || die "BR2_GLOBAL_PATCH_DIR ($BR2_GLOBAL_PATCH_DIR) does not look correct"
  65. trap 'rm -f "$JSON"' EXIT
  66. JSON=$(mktemp)
  67. make show-info > "$JSON"
  68. # ensure files have been downloaded, but without checking
  69. make BR2_DOWNLOAD_FORCE_CHECK_HASHES= source
  70. message Updating hashes
  71. for file in $BR_NO_CHECK_HASH_FOR; do
  72. for pkg in $(get_pkgs "$JSON" "$file"); do
  73. HASHFILE="$BR2_GLOBAL_PATCH_DIR/$pkg/$pkg.hash"
  74. PKG_DL_DIR=$(get_pkg_dl_dir "$JSON" "$pkg")
  75. message "Adding hash for $file to $HASHFILE"
  76. mkdir -p "${HASHFILE%/*}"
  77. gen_hash "$DL_DIR/$PKG_DL_DIR" "$file" > "$HASHFILE"
  78. done
  79. done
  80. # Symlink linux-headers to linux if identical
  81. linux_hash="$BR2_GLOBAL_PATCH_DIR/linux/linux.hash"
  82. linux_headers_hash="$BR2_GLOBAL_PATCH_DIR/linux-headers/linux-headers.hash"
  83. if [ -e "$linux_hash" ] && [ -e "$linux_headers_hash" ] \
  84. && cmp -s "$linux_hash" "$linux_headers_hash"; then
  85. ln -sf ../linux/linux.hash "$linux_headers_hash"
  86. fi
  87. message Verifying hashes
  88. make clean
  89. make BR2_DOWNLOAD_FORCE_CHECK_HASHES=y source