-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfpga_upgrade
More file actions
executable file
·154 lines (127 loc) · 4.06 KB
/
fpga_upgrade
File metadata and controls
executable file
·154 lines (127 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
set -euo pipefail
# Configuration
LOCKFILE="fpga_upgrade.lock"
IP1="192.168.40.252"
IP2="192.168.40.253"
BASE_DIR="/disk2/mu2e/mu2e_UEM_firmware/mu2e_UEM_firmware.runs"
DEFAULT_FILE_PATH="${DEFAULT_FILE_PATH:-$BASE_DIR/impl_1/top_fmc228_pcie.bit}"
REMOTE_PATH="${REMOTE_PATH:-/upgrade}"
SOCKET_DIR="/tmp/ssh_mux_$USER"
SSH_OPTIONS="-o ConnectTimeout=5 -o StrictHostKeyChecking=no -o ForwardX11=no -o ControlPath=$SOCKET_DIR/%r@%h:%p"
usage() {
echo "Usage:"
echo " $0 # upload default bitfile, run upgrade"
echo " $0 -noupload # no upload, use default bitfile already on system"
echo " $0 new_bitfile.bit # upload new bitfile, run upgrade"
echo " $0 -noupload new_bitfile.bit # no upload, use specified bitfile already on system"
exit 1
}
# Default values
UPLOAD=true
FILE_TO_UPLOAD="$DEFAULT_FILE_PATH"
# Parse arguments
if [ "$#" -eq 0 ]; then
UPLOAD=true
FILE_TO_UPLOAD="$DEFAULT_FILE_PATH"
elif [ "$#" -eq 1 ]; then
if [ "$1" = "-noupload" ]; then
UPLOAD=false
FILE_TO_UPLOAD="$DEFAULT_FILE_PATH"
else
UPLOAD=true
FILE_TO_UPLOAD="$1"
fi
elif [ "$#" -eq 2 ]; then
if [ "$1" = "-noupload" ]; then
UPLOAD=false
FILE_TO_UPLOAD="$2"
else
usage
fi
else
usage
fi
# Functions
check_ip() { ping -c 1 -W 1 "$1" >/dev/null 2>&1; }
remount_rw() {
ssh $SSH_OPTIONS root@"$ACTIVE_IP" "mount -o remount,rw /"
}
remount_ro() {
ssh $SSH_OPTIONS root@"$ACTIVE_IP" "mount -o remount,ro /"
}
run_upgrade() {
local file_name="$1"
SSH_COMMAND="export PATH=\$PATH:/sbin:/usr/sbin;
cd \"$REMOTE_PATH\" || exit 1;
echo 'Y' | fpga_upgrade \"$file_name\""
if ssh $SSH_OPTIONS root@"$ACTIVE_IP" "$SSH_COMMAND"; then
echo "[PROMPT] Check above console result to see if upgrade succeeded (press Ctrl + C if not)"
else
echo "[ERROR] Upgrade failed"
exit 1
fi
}
cleanup() {
echo "[INFO] Cleaning up..."
if [ -n "${ACTIVE_IP:-}" ]; then
remount_ro && echo "[SUCCESS] Remote filesystem read-only."
ssh -O exit $SSH_OPTIONS root@"$ACTIVE_IP" && echo "[INFO] SSH connection closed."
fi
[ -d "$SOCKET_DIR" ] && rm -rf "$SOCKET_DIR" && echo "[INFO] Removed socket directory."
[ -f "$LOCKFILE" ] && rm -f "$LOCKFILE" && echo "[INFO] Removed lock file."
}
trap cleanup EXIT
echo "[INFO] Starting the upgrade process..."
[ -e "$LOCKFILE" ] && {
echo "[ERROR] Another instance already running. Exiting."
exit 1
}
touch "$LOCKFILE"
# Determine active IP
if check_ip "$IP1"; then
ACTIVE_IP="$IP1"
elif check_ip "$IP2"; then
ACTIVE_IP="$IP2"
else
echo "[ERROR] Neither IP is reachable. Exiting."
exit 1
fi
echo "[INFO] Using active IP: $ACTIVE_IP"
mkdir -p "$SOCKET_DIR"
ssh $SSH_OPTIONS -o ControlMaster=auto -o ControlPersist=600 root@"$ACTIVE_IP" "echo '[INFO] SSH established.'"
if [ "$UPLOAD" = true ]; then
if [ ! -f "$FILE_TO_UPLOAD" ]; then
echo "[ERROR] File '$FILE_TO_UPLOAD' does not exist. Exiting."
usage
exit 1
fi
if remount_rw; then
echo "[SUCCESS] Remote filesystem remounted as writable."
else
echo "[ERROR] Failed to remount filesystem as writable. Exiting."
exit 1
fi
if scp $SSH_OPTIONS "$FILE_TO_UPLOAD" root@"$ACTIVE_IP":"$REMOTE_PATH"; then
echo "[SUCCESS] File '$FILE_TO_UPLOAD' successfully copied to '$REMOTE_PATH'."
else
echo "[ERROR] Failed to copy file. Exiting."
exit 1
fi
remount_ro && echo "[SUCCESS] Remote filesystem remounted as read-only."
else
echo "[INFO] Skipping file upload and filesystem remount as per '-noupload' argument."
fi
file_name=$(basename "$FILE_TO_UPLOAD")
# Run upgrade command
if run_upgrade "$file_name"; then
:
else
echo "[ERROR] FPGA Upgrade failed."
exit 1
fi
echo "$file_name" > last_bitfile_used.txt
read -n 1 -p "[PROMPT] Press any key once you see the 'Purdue debug monitor 1.00' confirmation in minicom..."
./setup
echo "[SUCCESS] Completed upgrade. Now starting UDP receiver."
# ./udp-recv