-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-to-board.sh
executable file
·278 lines (235 loc) · 5.75 KB
/
copy-to-board.sh
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
POSITIONAL_ARGS=()
DTBS=()
OVERLAYS=()
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dtb)
DTBS+=("$2")
shift
shift
;;
-v|--overlay)
OVERLAYS+=("$2")
shift
shift
;;
-m|--modules)
MODULES_PATH="$2"
shift
shift
;;
-o|--out)
KERNEL_OUT_PATH="$2"
shift
shift
;;
--nv-initrd)
NV_INIT_RD=1
shift
;;
--all-overlays)
ALL_OVERLAYS=1
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
usage="usage: $0 <board> [options] <scp <ip>>
board:
xil: Xilinx board
nv: Nvidia Tegra board
rpi3: Raspberry Pi 3 (32-bit)
rpi4: Raspberry Pi 4 (32-bit)
rpi4-64: Raspberry Pi 4 (64-bit)
rpi5: Raspberry Pi 5 (64-bit)
imx8mp-hummingboard-pulse: IMX8MP Hummingboard Pulse
tb-rk3399-vendor-u-boot: Toybrick RK3399 ProX
options:
-d|--dtb <dtb>: copy the specified dtb
-v|--overlay <overlay>: copy the specified overlay
--all-overlays: copy all overlays
-o|--out <kernel_out_path>: specify kernel out path
-m|--modules <modules_path>: specify modules out path
--nv-initrd: update nvidia init.rd
"
print_usage() {
echo "$usage"
exit 1
}
if [[ $# -lt 2 ]]; then
print_usage
fi
BOARD_TYPE="$1"
TRANSFER_MODE="$2"
KERNEL_VERSION_PATH=include/config/kernel.release
if [[ "$BOARD_TYPE" = "xil" ]]; then
KERNEL_SRC="arch/arm/boot/uImage"
KERNEL_TARGET="/boot/uImage"
DTB_SRC="arch/arm/boot/dts"
DTB_TARGET="/boot"
DTB_TARGET_NAME="devicetree.dtb"
elif [[ "$BOARD_TYPE" = "rpi3" ]]; then
KERNEL_SRC="arch/arm/boot/zImage"
KERNEL_TARGET="/boot/kernel7.img"
DTB_SRC="arch/arm/boot/dts/"
DTB_TARGET="/boot"
OVERLAYS_SRC="arch/arm/boot/dts/overlays"
OVERLAYS_TARGET="/boot/overlays"
elif [[ "$BOARD_TYPE" = "rpi4" ]]; then
KERNEL_SRC="arch/arm/boot/zImage"
KERNEL_TARGET="/boot/firmware/kernel7l.img"
DTB_SRC="arch/arm/boot/dts/broadcom"
DTB_TARGET="/boot/firmware"
OVERLAYS_SRC="arch/arm/boot/dts/overlays"
OVERLAYS_TARGET="/boot/firmware/overlays"
elif [[ "$BOARD_TYPE" = "rpi4-64" ]]; then
KERNEL_SRC="arch/arm64/boot/Image"
KERNEL_TARGET="/boot/firmware/kernel8.img"
DTB_SRC="arch/arm64/boot/dts/broadcom"
DTB_TARGET="/boot/firmware"
OVERLAYS_SRC="arch/arm64/boot/dts/overlays"
OVERLAYS_TARGET="/boot/firmware/overlays"
elif [[ "$BOARD_TYPE" = "rpi5" ]]; then
KERNEL_SRC="arch/arm64/boot/Image"
KERNEL_TARGET="/boot/firmware/kernel_2712.img"
DTB_SRC="arch/arm64/boot/dts/broadcom"
DTB_TARGET="/boot/firmware"
OVERLAYS_SRC="arch/arm64/boot/dts/overlays"
OVERLAYS_TARGET="/boot/firmware/overlays"
elif [[ "$BOARD_TYPE" = "nv" ]]; then
KERNEL_SRC="arch/arm64/boot/Image"
KERNEL_TARGET="/boot/Image"
DTB_PREFIX=kernel_
DTB_SRC="arch/arm64/boot/dts/nvidia"
DTB_TARGET="/boot/dtb"
OVERLAYS_SRC="$DTB_SRC"
OVERLAYS_TARGET="/boot"
elif [[ "$BOARD_TYPE" = "imx8mp-hummingboard-pulse" ]]; then
KERNEL_SRC="arch/arm64/boot/Image"
KERNEL_TARGET="/boot/Image"
DTB_SRC="arch/arm64/boot/dts/freescale"
DTB_TARGET="/boot"
DTB_TARGET_NAME="imx8mp-hummingboard-pulse.dtb"
elif [[ "$BOARD_TYPE" = "tb-rk3399-vendor-u-boot" ]]; then
KERNEL_SRC="arch/arm64/boot/Image"
KERNEL_TARGET="/boot/extlinux/Image"
DTB_SRC="arch/arm64/boot/dts/rockchip"
DTB_TARGET="/boot/extlinux"
DTB_TARGET_NAME="toybrick.dtb"
else
print_usage
fi
if [[ -n "$DTB_TARGET_NAME" ]] && [[ "${#DTBS[@]}" -ne "1" ]]; then
echo "Board does not support multiple DTBs"
exit 1
fi
if [[ "$TRANSFER_MODE" = "scp" ]]; then
if [[ $# -lt 3 ]]; then
print_usage
exit 1
fi
IS_SCP=1
IP="$3"
cp_transfer() {
SRC="$1"
TARGET="$2"
echo "Copy $SRC to $TARGET"
scp "$SRC" "root@$IP:$TARGET"
if [[ $? -ne 0 ]]; then
exit 1
fi
}
cmd() {
ssh "root@$IP" "$1"
}
rsync_transfer() {
SRC="$1"
TARGET="$2"
rsync -av --checksum --omit-dir-times --delete "$SRC" "root@$IP":"$TARGET"
}
rsync_transfer_file_arr() {
SRC="$1"
shift
TARGET="$1"
shift
PATHS=("$@")
RSYNC_TMP_FILE=$(mktemp)
printf "%s\n" "${PATHS[@]}" > "$RSYNC_TMP_FILE"
rsync -av --checksum --omit-dir-times --files-from="$RSYNC_TMP_FILE" "$SRC" "root@$IP":"$TARGET"
rm "$RSYNC_TMP_FILE"
}
else
echo "invalid transfer mode"
exit 1
fi
if [[ -z "$SOURCE_PATH" ]]; then
SOURCE_PATH="."
fi
SOURCE_PATH_ABS=$(realpath "$SOURCE_PATH")
SOURCE_PATH_NAME=$(basename "$SOURCE_PATH_ABS")
if ! [[ -v KERNEL_OUT_PATH ]]; then
KERNEL_OUT_PATH="$SOURCE_PATH/../kernel_out-$SOURCE_PATH_NAME"
fi
if ! [[ -v MODULES_PATH ]]; then
MODULES_PATH="$SOURCE_PATH/../modules_out-$SOURCE_PATH_NAME"
fi
KERNEL_VERSION_PATH="$KERNEL_OUT_PATH/$KERNEL_VERSION_PATH"
KERNEL_SRC="$KERNEL_OUT_PATH/$KERNEL_SRC"
OVERLAYS_SRC="$KERNEL_OUT_PATH/$OVERLAYS_SRC"
DTB_SRC="$KERNEL_OUT_PATH/$DTB_SRC"
cp_transfer "$KERNEL_SRC" "$KERNEL_TARGET"
for OVERLAY in "${OVERLAYS[@]}"; do
if [[ ! -f "$OVERLAYS_SRC/$OVERLAY" ]]; then
echo "Failed to find $OVERLAY in $OVERLAYS_SRC"
fi
done
if [[ -n "$ALL_OVERLAYS" ]]; then
pushd "$OVERLAYS_SRC"
while IFS= read -d $'\0' -r OVERLAY; do
OVERLAYS=("${OVERLAYS[@]}" "$OVERLAY")
done < <(find -name "*.dtbo" -print0)
popd
fi
if [[ ${#OVERLAYS[@]} -ne 0 ]]; then
rsync_transfer_file_arr "$OVERLAYS_SRC" "$OVERLAYS_TARGET" "${OVERLAYS[@]}"
fi
for DTB in "${DTBS[@]}"; do
if [[ -z "$DTB_TARGET_NAME" ]]; then
cp_transfer "$DTB_SRC"/"$DTB" "$DTB_TARGET"/"$DTB_PREFIX""$DTB"
else
cp_transfer "$DTB_SRC"/"$DTB" "$DTB_TARGET"/"$DTB_TARGET_NAME"
fi
done
if [[ -n "$IS_SCP" ]] && [[ -n "$MODULES_PATH" ]]; then
KERNEL_VERSION=$(cat "$KERNEL_VERSION_PATH")
rsync_transfer "$MODULES_PATH/lib/modules/$KERNEL_VERSION" "/lib/modules/"
fi
if [[ -n "$IS_SCP" ]]; then
if [[ -n "$NV_INIT_RD" ]]; then
cmd nv-update-initrd
fi
echo "Syncing..."
cmd sync
echo "Reboot?"
select yn in "Yes" "No"; do
case $yn in
Yes)
echo "Rebooting..."
cmd reboot
exit
;;
No)
exit
;;
esac
done
fi