Skip to content

Commit 14457cc

Browse files
committed
Add test script to validate USB MSD
The shell script verifies the enumeration of USB Mass Storage Devices. Signed-off-by: Aanchal Chaurasia <achauras@qti.qualcomm.com>
1 parent 229b45a commit 14457cc

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

  • Runner/suites/Kernel/Baseport/USB/usb_msd
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
# Validate USB Mass Storage device detection
6+
# Requires at least one USB Mass Storage peripheral (USB flash drive, external HDD/SSD, etc.) connected to a USB Host port.
7+
8+
TESTNAME="usb_msd"
9+
10+
# Robustly find and source init_env
11+
SCRIPT_DIR="$(
12+
cd "$(dirname "$0")" || exit 1
13+
pwd
14+
)"
15+
16+
# Default result file (works even before functestlib is available)
17+
# shellcheck disable=SC2034
18+
RES_FILE="$SCRIPT_DIR/${TESTNAME}.res"
19+
20+
INIT_ENV=""
21+
SEARCH="$SCRIPT_DIR"
22+
while [ "$SEARCH" != "/" ]; do
23+
if [ -f "$SEARCH/init_env" ]; then
24+
INIT_ENV="$SEARCH/init_env"
25+
break
26+
fi
27+
SEARCH=$(dirname "$SEARCH")
28+
done
29+
30+
if [ -z "$INIT_ENV" ]; then
31+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
32+
echo "$TESTNAME SKIP" >"$RES_FILE" 2>/dev/null || true
33+
exit 0
34+
fi
35+
36+
# Only source if not already loaded (idempotent)
37+
if [ -z "${__INIT_ENV_LOADED:-}" ]; then
38+
# shellcheck disable=SC1090
39+
. "$INIT_ENV"
40+
__INIT_ENV_LOADED=1
41+
fi
42+
# Always source functestlib.sh, using $TOOLS exported by init_env
43+
# shellcheck disable=SC1090,SC1091
44+
. "$TOOLS/functestlib.sh"
45+
46+
# Resolve test path and cd (single SKIP/exit path)
47+
SKIP_REASON=""
48+
test_path=$(find_test_case_by_name "$TESTNAME")
49+
if [ -z "$test_path" ] || [ ! -d "$test_path" ]; then
50+
SKIP_REASON="$TESTNAME SKIP - test path not found"
51+
elif ! cd "$test_path"; then
52+
SKIP_REASON="$TESTNAME SKIP - cannot cd into $test_path"
53+
else
54+
RES_FILE="$test_path/${TESTNAME}.res"
55+
fi
56+
57+
if [ -n "$SKIP_REASON" ]; then
58+
log_skip "$SKIP_REASON"
59+
echo "$TESTNAME SKIP" >"$RES_FILE" 2>/dev/null || true
60+
exit 0
61+
fi
62+
63+
log_info "-----------------------------------------------------------------------------------------"
64+
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
65+
log_info "=== Test Initialization ==="
66+
67+
# Check if dependecies are installed, else skip test
68+
deps_list="grep sed sort wc tr"
69+
if ! check_dependencies "$deps_list"; then
70+
log_skip "$TESTNAME SKIP - missing dependencies: $deps_list"
71+
echo "$TESTNAME SKIP" >"$RES_FILE"
72+
exit 0
73+
fi
74+
75+
# Detect unique devices with bInterfaceClass = 08 (MSD) under /sys/bus/usb/devices
76+
log_info "=== USB Mass Storage device Detection ==="
77+
msd_device_list="$(
78+
for f in /sys/bus/usb/devices/*/bInterfaceClass; do
79+
[ -r "$f" ] || continue
80+
if grep -qx '08' "$f"; then
81+
d=${f%/bInterfaceClass}
82+
d=${d%:*}
83+
printf '%s\n' "${d##*/}"
84+
fi
85+
done 2>/dev/null | sort -u
86+
)"
87+
88+
msd_device_count="$(printf "%s\n" "$msd_device_list" | sed '/^$/d' | wc -l | tr -d '[:space:]')"
89+
log_info "Number of MSD devices found: $msd_device_count"
90+
91+
if [ "$msd_device_count" -gt 0 ] 2>/dev/null; then
92+
log_info "=== Enumerated Mass Storage Devices ==="
93+
printf '\n%-9s %-9s %-s\n' "DEVICE" "VID:PID" "PRODUCT"
94+
printf '%s\n' "--------------------------------------------------------"
95+
printf "%s\n" "$msd_device_list" | sed '/^$/d' | while IFS= read -r dev; do
96+
sys="/sys/bus/usb/devices/$dev"
97+
vid=$([ -r "$sys/idVendor" ] && tr -d '[:space:]' < "$sys/idVendor" || echo -)
98+
pid=$([ -r "$sys/idProduct" ] && tr -d '[:space:]' < "$sys/idProduct" || echo -)
99+
if [ -r "$sys/product" ]; then
100+
product=$(tr -d '\000' < "$sys/product")
101+
else
102+
product="-"
103+
fi
104+
printf '%-9s %-9s %-s\n' "$dev" "$vid:$pid" "$product"
105+
done
106+
printf '\n'
107+
fi
108+
109+
if [ "$msd_device_count" -gt 0 ]; then
110+
log_pass "$TESTNAME : Test Passed - USB Mass Storage device(s) detected"
111+
echo "$TESTNAME PASS" > "$RES_FILE"
112+
exit 0
113+
else
114+
log_fail "$TESTNAME : Test Failed - No USB 'Mass Storage Device' found"
115+
echo "$TESTNAME FAIL" > "$RES_FILE"
116+
exit 0
117+
fi

0 commit comments

Comments
 (0)