From 22bebd4c1997f93de32b6a1249bab6f961539b64 Mon Sep 17 00:00:00 2001 From: Tobias Powalowski Date: Tue, 9 Jul 2024 21:04:22 +0200 Subject: [PATCH] simplify _kver function --- usr/lib/archboot/common.sh | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/usr/lib/archboot/common.sh b/usr/lib/archboot/common.sh index 086eef2c0..9bfabece3 100644 --- a/usr/lib/archboot/common.sh +++ b/usr/lib/archboot/common.sh @@ -160,26 +160,14 @@ _run_update_environment() { } _kver() { - # this is intentionally very loose. only ensure that we're - # dealing with some sort of string that starts with something - # resembling dotted decimal notation. remember that there's no - # requirement for CONFIG_LOCALVERSION to be set. - local kver re='^[[:digit:]]+(\.[[:digit:]]+)+' - local arch bytes reader - arch="${_RUNNING_ARCH}" - if [[ $arch == @(i?86|x86_64) ]]; then - local -i offset - offset="$(od -An -j0x20E -dN2 "$1")" || return - read -r kver _ < \ - <(dd if="$1" bs=1 count=127 skip=$((offset + 0x200)) 2>"${_NO_LOG}") - else - reader='cat' - bytes="$(od -An -t x2 -N2 "$1" | tr -dc '[:alnum:]')" - [[ "$bytes" == '8b1f' ]] && reader='zcat' - read -r _ _ kver _ < <($reader "$1" | rg -m1 -ao 'Linux version .(\.[-[:alnum:]+]+)+') + if [[ -f "${1}" ]]; then + # x86_64 default image + rg --no-heading -Noazm 1 'ABCDEF(.*)\(.*@' -r '$1' ${1} || + # aarch64, works for compressed and uncompressed image + rg --no-heading -Noazm 1 'Linux version ([0-9].*) \(.*@' -r '$1' "${1}" || + # riscv64, rg cannot detect compression without suffix + zcat "${1}" | rg -Noazm 1 'Linux version ([0-9].*) \(.*@' -r '$1' fi - [[ "$kver" =~ $re ]] || return 1 - printf '%s' "$kver" } ### check architecture