From 1fb67220211a6b49870107bff052623c51ecbffb Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 12 Jan 2013 20:12:03 -0500 Subject: factor out kver detection to functions file This leaves bash completion with a duplication, but this is intentional to avoid pulling in the entire functions file and worrying about pollution. Signed-off-by: Dave Reisner --- bash-completion | 1 - functions | 22 ++++++++++++++++++++++ lsinitcpio | 5 +---- mkinitcpio | 19 +------------------ 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/bash-completion b/bash-completion index 597eab2..0108863 100644 --- a/bash-completion +++ b/bash-completion @@ -1,5 +1,4 @@ #!/bin/bash -# mkinitcpio bash completion by Seblu detect_kver() { local kver_validator='^[[:digit:]]+(\.[[:digit:]]+)+' diff --git a/functions b/functions index 5a44ac9..8be43ed 100644 --- a/functions +++ b/functions @@ -133,6 +133,28 @@ parseopts() { return 0 } +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:]]+)+' + + # scrape the version out of the kernel image. locate the offset + # to the version string by reading 2 bytes out of image at at + # address 0x20E. this leads us to a string of, at most, 128 bytes. + # read the first word from this string as the kernel version. + local offset=$(hexdump -s 526 -n 2 -e '"%0d"' "$1") + [[ $offset = +([0-9]) ]] || return 1 + + read kver _ < \ + <(dd if="$1" bs=1 count=127 skip=$(( offset + 0x200 )) 2>/dev/null) + + [[ $kver =~ $re ]] || return 1 + + printf '%s' "$kver" +} + plain() { local mesg=$1; shift printf " $_color_bold$mesg$_color_none\n" "$@" >&1 diff --git a/lsinitcpio b/lsinitcpio index 5984cf8..c4129d5 100755 --- a/lsinitcpio +++ b/lsinitcpio @@ -108,10 +108,7 @@ detect_filetype() { fi # still nothing? hrmm, maybe the user goofed and it's a kernel - offt=$(hexdump -s 526 -n 2 -e '"%0d"' "$1") - read kver _ < \ - <(dd if="$1" bs=1 count=127 skip=$(( offt + 0x200 )) 2>/dev/null) - if [[ $kver =~ $kver_validator ]]; then + if kver "$1" >/dev/null; then die '%s is a kernel image, not an initramfs image!' "$1" fi diff --git a/mkinitcpio b/mkinitcpio index 30e9236..d5cf895 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -84,12 +84,6 @@ cleanup() { resolve_kernver() { local kernel=$1 offset 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_validator='^[[:digit:]]+(\.[[:digit:]]+)+' - if [[ -z $kernel ]]; then uname -r return 0 @@ -105,18 +99,7 @@ resolve_kernver() { return 1 fi - # scrape the version out of the kernel image. locate the offset - # to the version string by reading 2 bytes out of image at at - # address 0x20E. this leads us to a string of, at most, 128 bytes. - # read the first word from this string as the kernel version. - offset=$(hexdump -s 526 -n 2 -e '"%0d"' "$kernel") - read kver _ < \ - <(dd if="$kernel" bs=1 count=127 skip=$(( offset + 0x200 )) 2>/dev/null) - - if [[ $kver =~ $kver_validator ]]; then - printf '%s' "$kver" - return 0 - fi + kver "$kernel" && return error "invalid kernel specified: \`%s'" "$_optkver" -- cgit v1.2.3-24-g4f1b