1#!/bin/sh
2# Write a fixed-size string message on the ivshmem shared memory
3# Usage write_shared_memory.sh [-s size in bytes] [-l location] [-m message] [-v]
4#
5# Copyright (c) 2023 Huawei Technologies France SASU
6#
7# SPDX-License-Identifier: Apache-2.0
8
9# default parameters
10SHM_SIZE=4194304
11SHM_LOC="/dev/shm/ivshmem"
12SHM_MSG="This is a message test for ivshmem shared memory"
13SHM_DD_VERBOSE="status=none"
14
15usage()
16{
17	echo "Usage: $0 [-s size in bytes] [-l location] [-m message] [-v]"
18	exit 1
19}
20
21write_message()
22{
23	WM_SIZE=$1
24	WM_LOC=$2
25	WM_MSG=$3
26	WM_DD=$4
27
28	WM_MSG_LEN=${#WM_MSG}
29
30	if [ "$WM_MSG_LEN" -gt "$WM_SIZE" ]; then
31		# make sure we only read and write up to WM_SIZE
32		printf %s "$WM_MSG" | dd of="$WM_LOC" count=1 bs="$WM_SIZE" "$WM_DD" || exit 1
33	else
34		# pad to WM_SIZE
35		printf %s "$WM_MSG" | dd of="$WM_LOC" ibs="$WM_SIZE" conv=sync "$WM_DD" || exit 1
36	fi
37}
38
39while :
40do
41	# make sure we only read $1 if it is defined (no unset)
42	PARAMS="${1:-0}"
43
44	case "$PARAMS" in
45	-s)
46		shift
47		SHM_SIZE="$1"
48		;;
49	-l)
50		shift
51		SHM_LOC="$1"
52		;;
53	-h)
54		usage
55		;;
56	-m)
57		shift
58		SHM_MSG="$1"
59		;;
60	-v)
61		SHM_DD_VERBOSE="status=progress"
62		;;
63	-*)
64		usage
65		;;
66	*)
67		break
68		;;
69	esac
70
71	shift
72done
73
74write_message "$SHM_SIZE" "$SHM_LOC" "$SHM_MSG" "$SHM_DD_VERBOSE"
75