Skip to content

Commit

Permalink
iotests: Add poke_file_[bl]e functions
Browse files Browse the repository at this point in the history
Similarly to peek_file_[bl]e, we may want to write binary integers into
a file.  Currently, this often means messing around with poke_file and
raw binary strings.  I hope these functions make it a bit more
comfortable.

Signed-off-by: Max Reitz <[email protected]>
Code-suggested-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Signed-off-by: Max Reitz <[email protected]>
  • Loading branch information
XanClic committed Mar 26, 2020
1 parent 1656324 commit 2f8bb28
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/qemu-iotests/common.rc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ poke_file()
printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null
}

# poke_file_le $img_filename $offset $byte_width $value
# Example: poke_file_le "$TEST_IMG" 512 2 65534
poke_file_le()
{
local img=$1 ofs=$2 len=$3 val=$4 str=''

while ((len--)); do
str+=$(printf '\\x%02x' $((val & 0xff)))
val=$((val >> 8))
done

poke_file "$img" "$ofs" "$str"
}

# poke_file_be $img_filename $offset $byte_width $value
# Example: poke_file_be "$TEST_IMG" 512 2 65279
poke_file_be()
{
local img=$1 ofs=$2 len=$3 val=$4
local str=$(printf "%0$((len * 2))x\n" $val | sed 's/\(..\)/\\x\1/g')

poke_file "$img" "$ofs" "$str"
}

# peek_file_le 'test.img' 512 2 => 65534
peek_file_le()
{
Expand Down

0 comments on commit 2f8bb28

Please sign in to comment.