diff --git a/utils/gen-version.sh b/utils/gen-version.sh
index b09ad495f86a..ba5185fa264c 100755
--- a/utils/gen-version.sh
+++ b/utils/gen-version.sh
@@ -2,15 +2,26 @@
 
 # SPDX-License-Identifier: GPL-2.0-or-later
 # Generate a version string using git describe
+#
+# An optional sha string can be passed as the second command line argument
+# if the caller wishes to override the SHA value picked up by git. This may be
+# useful when distribution packages are build outside of the upstream repo.
 
 build_dir="$1"
+sha_override="$2"
 
-# Bail out if the directory isn't under git control
-src_dir=$(git rev-parse --git-dir 2>&1) || exit 1
-src_dir=$(readlink -f "$src_dir/..")
-
-# Get a short description from the tree.
-version=$(git describe --abbrev=8 --match "v[0-9]*" 2>/dev/null)
+if [ -n "$sha_override" ]
+then
+	# Use the sha provided on the command line.
+	version="v0.0.0-g$sha_override"
+else
+	# Bail out if the directory isn't under git control
+	src_dir=$(git rev-parse --git-dir 2>&1) || exit 1
+	src_dir=$(readlink -f "$src_dir/..")
+
+	# Get a short description from the tree.
+	version=$(git describe --abbrev=8 --match "v[0-9]*" 2>/dev/null)
+fi
 
 if [ -z "$version" ]
 then
@@ -26,7 +37,7 @@ if [ -z "$build_dir" ] || (echo "$build_dir" | grep -q "$src_dir")
 then
 	git update-index --refresh > /dev/null 2>&1
 fi
-git diff-index --quiet HEAD || version="$version-dirty ($(date --iso-8601=seconds))"
+[ -n "$sha_override" ] || git diff-index --quiet HEAD || version="$version-dirty ($(date --iso-8601=seconds))"
 
 # Replace first '-' with a '+' to denote build metadata, strip the 'g' in from
 # of the git SHA1 and remove the initial 'v'.
