-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild-emu.sh
More file actions
228 lines (210 loc) · 7.06 KB
/
build-emu.sh
File metadata and controls
228 lines (210 loc) · 7.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/sh
# Default image name
SRC_IMAGE_NAME="johnwanzi/ok-emu:latest"
IMAGE_NAME="ok-emu:latest"
REPO_PATH=""
EMU_TYPE=""
X11_ENABLED=0
# Ports for VNC mode
VNC_WEBSOCKET_PORT=6088
VNC_ONEKEY_BRIDGE_PORT=21333
if [ -n "$BASH_SOURCE" ]; then
cd "$(dirname "${BASH_SOURCE[0]}")"
else
cd "$(dirname "$0")"
fi
# Argument parsing
for arg in "$@"; do
case $arg in
pro-emu|1s-emu)
if [ -n "$EMU_TYPE" ]; then
echo "Error: Only one of 'pro-emu' or '1s-emu' can be specified." >&2
return 1 2>/dev/null || exit 1
fi
EMU_TYPE="$arg"
;;
--x11)
X11_ENABLED=1
;;
*)
echo "Usage: $0 [pro-emu|1s-emu] [--x11]" >&2
return 1 2>/dev/null || exit 1
;;
esac
done
if [ -z "$EMU_TYPE" ]; then
echo "Usage: $0 [pro-emu|1s-emu] [--x11]" >&2
return 1 2>/dev/null || exit 1
fi
# Check if IMAGE_NAME image exists
if ! docker image inspect "$IMAGE_NAME" > /dev/null 2>&1; then
echo "$IMAGE_NAME not found! pulling $SRC_IMAGE_NAME..."
docker pull "$SRC_IMAGE_NAME"
docker tag "$SRC_IMAGE_NAME" "$IMAGE_NAME"
docker rmi "$SRC_IMAGE_NAME"
else
echo "Found $IMAGE_NAME"
fi
# function to configure environment
env_config() {
if [ "$(uname)" = "Linux" ]; then
# Check DISPLAY environment variable, set to :0 if not set
if [ -z "$DISPLAY" ]; then
export DISPLAY=:0
fi
# Check if xhost is installed, install if not
if ! command -v xhost > /dev/null 2>&1; then
echo "xhost not found, installing..."
if command -v apt-get > /dev/null 2>&1; then
sudo apt-get update && sudo apt-get install -y x11-xserver-utils
elif command -v yum > /dev/null 2>&1; then
sudo yum install -y xorg-x11-server-utils
else
echo "No supported package manager found for installing xhost." >&2
return 1 2>/dev/null || exit 1
fi
fi
echo "Enabling xhost for Docker..."
xhost +local:docker
fi
if [ "$(uname)" = "Darwin" ]; then
echo "X11 has compatible problems on macOS, please use VNC to connect to the emulator."
return 1 2>/dev/null || exit 1
fi
}
# function to build pro emulator
build_pro_emu_x11() {
# check firmware-pro repository
if [ ! -d "firmware-pro" ]; then
echo "firmware-pro directory not found, cloning..."
git clone --recursive https://github.com/OneKeyHQ/firmware-pro.git
cd firmware-pro
git checkout emulator
git submodule update --init --recursive
cd ..
fi
REPO_PATH="firmware-pro"
if [ "$(uname)" = "Linux" ]; then
# Run interactive Docker container with X11 forwarding and current directory mounted
docker run -it --rm \
--env DISPLAY=$DISPLAY \
--env XAUTHORITY=$XAUTHORITY \
-e XDG_RUNTIME_DIR=/tmp/$(id -u)-runtime-dir \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $(pwd):/home \
--privileged --network=host "$IMAGE_NAME" bash -c \
"source \$HOME/.cargo/env && \
cd /home/$REPO_PATH && \
git config --global --add safe.directory '*' && \
git lfs pull && git lfs checkout && \
cd / && \
poetry run make -C /home/$REPO_PATH/core build_unix && \
poetry run /home/$REPO_PATH/core/emu.py && \
exec bash"
elif [ "$(uname)" = "Darwin" ]; then
echo "X11 has compatible problems on macOS, please use VNC to connect to the emulator."
return 1 2>/dev/null || exit 1
fi
}
# function to build 1s emulator
build_1s_emu_x11() {
# check firmware-classic1s repository
if [ ! -d "firmware-classic1s" ]; then
echo "firmware-classic1s directory not found."
git clone --recursive https://github.com/OneKeyHQ/firmware-classic1s.git
cd firmware-classic1s
git checkout emulator
git submodule update --init --recursive
cd ..
fi
REPO_PATH="firmware-classic1s"
if [ "$(uname)" = "Linux" ]; then
docker run -it --rm \
--env DISPLAY=$DISPLAY \
--env XAUTHORITY=$XAUTHORITY \
-e XDG_RUNTIME_DIR=/tmp/$(id -u)-runtime-dir \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $(pwd):/home \
--privileged --network=host "$IMAGE_NAME" bash -c \
"source \$HOME/.cargo/env && \
cd /home/$REPO_PATH && \
git config --global --add safe.directory '*' && \
cd / && \
export EMULATOR=1 DEBUG_LINK=0 && \
poetry run make -C /home/$REPO_PATH/legacy build_emu && \
./home/$REPO_PATH/legacy/firmware/onekey_emu.elf\
exec bash"
elif [ "$(uname)" = "Darwin" ]; then
echo "X11 has compatible problems on macOS, please use VNC to connect to the emulator."
return 1 2>/dev/null || exit 1
fi
}
# function to build pro emulator using VNC
build_pro_emu_vnc() {
# check firmware-pro repository
if [ ! -d "firmware-pro" ]; then
echo "firmware-pro directory not found, cloning..."
git clone --recursive https://github.com/OneKeyHQ/firmware-pro.git
cd firmware-pro
git checkout emulator
git submodule update --init --recursive
cd ..
fi
REPO_PATH="firmware-pro"
docker run -it --rm \
-e REPO_PATH=$REPO_PATH \
-p $VNC_WEBSOCKET_PORT:6080 \
-p $VNC_ONEKEY_BRIDGE_PORT:21333 \
-e DISPLAY=$DISPLAY \
-e XDG_RUNTIME_DIR=/tmp/$(id -u)-runtime-dir \
-v $(pwd):/home "$IMAGE_NAME" bash -c \
"source \$HOME/.cargo/env && \
cd /home/$REPO_PATH && \
git config --global --add safe.directory '*' && \
git lfs pull && git lfs checkout && \
cd / && \
poetry run make -C /home/$REPO_PATH/core build_unix && \
DISPLAY=:1 /startup-pro-emu.sh && \
exec bash"
}
# function to build 1s emulator using VNC
build_1s_emu_vnc() {
# check firmware-classic1s repository
if [ ! -d "firmware-classic1s" ]; then
echo "firmware-classic1s directory not found."
git clone --recursive https://github.com/OneKeyHQ/firmware-classic1s.git
cd firmware-classic1s
git checkout emulator
git submodule update --init --recursive
cd ..
fi
REPO_PATH="firmware-classic1s"
docker run -it --rm \
-e REPO_PATH=$REPO_PATH \
-p $VNC_WEBSOCKET_PORT:6080 \
-p $VNC_ONEKEY_BRIDGE_PORT:21333 \
-v $(pwd):/home "$IMAGE_NAME" bash -c \
"source \$HOME/.cargo/env && \
cd /home/$REPO_PATH && \
git config --global --add safe.directory '*' && \
cd / && \
export EMULATOR=1 DEBUG_LINK=0 && \
poetry run make -C /home/$REPO_PATH/legacy build_emu && \
DISPLAY=:1 /startup-1s-emu.sh && \
exec bash"
}
if [ "$X11_ENABLED" = "1" ]; then
env_config
# Check the first argument
if [ "$EMU_TYPE" = "pro-emu" ]; then
build_pro_emu_x11
elif [ "$EMU_TYPE" = "1s-emu" ]; then
build_1s_emu_x11
fi
else
if [ "$EMU_TYPE" = "pro-emu" ]; then
build_pro_emu_vnc
elif [ "$EMU_TYPE" = "1s-emu" ]; then
build_1s_emu_vnc
fi
fi