From 3cc0b4cdea7699d39455ba565f015599e50ed9cd Mon Sep 17 00:00:00 2001 From: Steffen Eiden Date: Fri, 25 Nov 2022 14:31:24 +0100 Subject: [PATCH] pvattest/tools: remove xxd dependency from scripts xxd has vim-common as a dependency. We can avoid that by using od instead which is from coreutils. While at it, add the require_command function from the pvattest-info script to the pvextract-hdr script and use it. Suggested-by: Christian Borntraeger Reviewed-by: Marc Hartmayer Signed-off-by: Steffen Eiden --- pvattest/tools/pvattest-info | 22 ++++++++++++++-------- pvattest/tools/pvextract-hdr | 25 ++++++++++++++++++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pvattest/tools/pvattest-info b/pvattest/tools/pvattest-info index b3eceb45..e54cde92 100755 --- a/pvattest/tools/pvattest-info +++ b/pvattest/tools/pvattest-info @@ -14,6 +14,8 @@ set -o pipefail set -o nounset set -e +XDUMP='od -A x -t x2z -v' + usage() { cat <<-EOF Usage: $(basename "$0") FILE @@ -33,16 +35,16 @@ function check_is_pvattest_binary() { exit 1 fi - xxd -l 16 "${input}" | grep -q pvattest || + ${XDUMP} --read-bytes 16 -- "${input}" 2>/dev/null | grep -q pvattest || { echo "ERROR: ${input} does not contain a pvattest binary output." >&2 && exit 1; } - size=$(xxd -s 12 -l 4 "${input}" | awk 'NR==1 {print "0x" $2 $3}') + size=$(${XDUMP} --skip-bytes 12 --read-bytes 4 -- "${input}" 2>/dev/null | awk 'NR==1 {print "0x" $2 $3}') if [ $((size)) -lt 64 ]; then echo "ERROR: ${input} does not contain a pvattest binary output." >&2 exit 1 fi - version=$(xxd -s 8 -l 4 "$input") + version=$(${XDUMP} --skip-bytes 8 --read-bytes 4 -- "$input" 2>/dev/null) echo "$version" | grep -q "0000 0100" || { echo -n "WARNING: unknown hdr version " >&2 && echo "$version" | awk '{print "0x" $2 $3}'>&2 ; } @@ -55,24 +57,28 @@ function print_entry() { local size local off - size=$(xxd -s $((file_off)) -l 4 "${input}" | awk 'NR==1 {print "0x" $2 $3}') - off=$(xxd -s $((file_off + 4)) -l 4 "${input}" | awk 'NR==1 {print "0x" $2 $3}') + size=$(${XDUMP} --skip-bytes $((file_off)) --read-bytes 4 -- "${input}" 2>/dev/null | + awk 'NR==1 {print "0x" $2 $3}') + off=$(${XDUMP} --skip-bytes $((file_off + 4)) --read-bytes 4 -- "${input}" 2>/dev/null | + awk 'NR==1 {print "0x" $2 $3}') if [[ $size != "0x00000000" ]] || [[ $off != "0x00000000" ]]; then echo "${text}:" - xxd -s $((off)) -l $((size)) -p "${input}" + od -A n -w$((size)) -t x8 --skip-bytes $((off)) --read-bytes $((size)) -- "${input}" 2>/dev/null |\ + sed -e 's/\s//g' fi } function require_command() { local cmd="$1" - command -v "$cmd" >/dev/null 2>&1 || { echo >&2 "ERROR: $cmd required but not installed."; exit 1; } + command -v "$cmd" >/dev/null 2>&1 || \ + { echo >&2 "ERROR: $cmd required but not installed."; exit 1; } } -require_command xxd require_command awk require_command wc +require_command od if [ $# -eq 0 ]; then echo "ERROR: Input not set. Use '$(basename "$0") [FILE]' to specify the Input file" >&2 diff --git a/pvattest/tools/pvextract-hdr b/pvattest/tools/pvextract-hdr index d8116dd9..ea5c0425 100755 --- a/pvattest/tools/pvextract-hdr +++ b/pvattest/tools/pvextract-hdr @@ -14,7 +14,9 @@ set -o pipefail set -o nounset set -e -def_output="sehdr.bin" +XDUMP='od -A x -t x2z -v' + +def_output='sehdr.bin' def_skip=0x14 def_len=0x4 @@ -37,11 +39,22 @@ function check_file() { function check_hdr_ver() { local hdr_start="$1" local input="$2" - xxd -s $((hdr_start + 8)) -l 4 "$input" | grep -q "000 0100" || + ${XDUMP} --skip-bytes $((hdr_start + 8)) --read-bytes 4 -- "$input" 2>/dev/null | grep -q "000 0100" || { echo -n "WARNING: unknown hdr version " && - xxd -s $((hdr_start + 8)) -l 4 "$input" | awk '{print "0x" $2 $3}'; } + ${XDUMP} --skip-bytes $((hdr_start + 8)) --read_bytes 4 -- "$input" 2>/dev/null | awk '{print "0x" $2 $3}'; } } +function require_command() { + local cmd="$1" + + command -v "$cmd" >/dev/null 2>&1 || \ + { echo >&2 "ERROR: $cmd required but not installed."; exit 1; } +} + +require_command od +require_command awk +require_command grep + output=${def_output} parsed_skip=${def_skip} parsed_len=${def_len} @@ -77,13 +90,15 @@ if [ $# -eq 0 ]; then fi check_file "$input" -hdr_start=$(xxd -s $((skip)) -l $((len)) "${input}" | grep IBMSecEx || { echo ERROR: "${input} does not contain an SE header." >&2 && exit 1; }) +hdr_start=$(${XDUMP} --skip-bytes $((skip)) --read-bytes $((len)) -- "${input}" 2>/dev/null | grep IBMSecEx || + { echo ERROR: "${input} does not contain an SE header." >&2 && exit 1; }) hdr_start=$(echo "${hdr_start}" | awk '{print "0x" $1}' | cut -c 1-10) echo "SE header found at offset ${hdr_start}" check_hdr_ver "$hdr_start" "$input" -size=$(xxd -s $((hdr_start + 12)) -l 4 "${input}" | awk 'NR==1 {print "0x" $2 $3}') +size=$(${XDUMP} --skip-bytes $((hdr_start + 12)) --read-bytes 4 -- "${input}" 2>/dev/null | + awk 'NR==1 {print "0x" $2 $3}') dd if="${input}" of="${output}" bs=1 count=$((size)) skip=$((hdr_start)) status=none echo "SE header written to '${output}' ($((size)) bytes)"