-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathorangepi5_release.sh
More file actions
executable file
·294 lines (248 loc) · 7.16 KB
/
orangepi5_release.sh
File metadata and controls
executable file
·294 lines (248 loc) · 7.16 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
#
shell_lines=295 # Adjust it if the script changes
version_string=r114
targetdir=orangepi5-openfyde
TMPROOT=${TMPDIR:=./}
target=""
m2=""
TMP="$(mktemp -d ${TMPROOT}/XXXXXX)"
NVME_MAGIC='NVME'
SATA_MAGIC='SATA'
EMMC_MAGIC='EMMC'
BOARD_MAGIC=""
ORANGEPI5="orangepi5"
ORANGEPI5B="orangepi5b"
ORANGEPI5PLUS="orangepi5plus"
declare -A BOARD_MAP=(
["$ORANGEPI5"]="OP5 "
["$ORANGEPI5B"]="OP5B"
["$ORANGEPI5PLUS"]="OP5P"
)
board=""
SECTOR_SIZE=512
# Write magic to the last sector of RESERVED partition of ChromiumOS
MAGIC_SECTOR=65598
# board magic to the last 2 sector of RESERVED partition of ChromiumOS
BOARD_MAGIC_SECTOR=65597
self=$(realpath $0)
skip="false"
inplace="false"
src=""
cleanup()
{
[ -d "$TMP" ] && rm -rf $TMP
}
usage()
{
echo ""
echo "$0 [options]"
echo ""
echo "options:"
echo " '--help/-h'"
echo " This message"
echo " '--src/-i [src]'"
echo " path of src image"
echo " '--target/-o [target]'"
echo " path of target image"
echo " '--skip/-s"
echo " src image is plain, skip uncompression"
echo " '--inplace/-p"
echo " do not copy src to target, modify it inplace"
echo " '--boot sata/nvme/emmc'"
echo " generates images supporting boot from SATA/NVME/EMMC"
echo " '--board orangepi5/orangepi5b/orangepi5plus'"
echo " generates images for orange pi 5/5b/5plus (experimental)"
exit 1;
}
err()
{
echo "$@"
exit 1
}
read_cmd_line() {
while [ "$1" ]; do
case "$1" in
"--help"|"-h")
usage;
;;
"--src"|"-i")
if [ "$2" ]; then
src="$2"
shift;
else
err "ERROR: --src: no src specified."
fi
;;
"--target"|"-o")
if [ "$2" ]; then
target="$2"
shift;
else
err "ERROR: --target: no target specified."
fi
;;
"--skip"|"-s")
skip="true"
;;
"--inplace"|"-p")
inplace="true"
;;
"--board")
[ -z "$2" ] && usage
_board="$(echo "$2" | tr '[:upper:]' '[:lower:]')"
for b in "${!BOARD_MAP[@]}"; do
if [ "$b" == "$_board" ]; then
board="$b"
shift
break
fi
done
[ -z "$board" ] && err "unsupported board: $_board"
;;
"--boot")
if [ "$2" == "nvme" -o "$2" == "NVME" ]; then
m2=nvme
shift;
elif [ "$2" == "sata" -o "$2" == "SATA" ]; then
m2=sata
shift;
else
err "--boot requires an argument"
fi
;;
*)
usage
;;
esac
shift
done
}
enter_menu()
{
while :
do
clear
echo "*****************************"
echo "* Supported boards *"
echo "*****************************"
echo "[1] orange pi 5"
echo "[2] orange pi 5b"
echo "[3] orange pi 5 plus"
echo "[4] Quit"
echo "-----------------------------"
echo "Enter your choice:"
read your_choice
case $your_choice in
1) board=$ORANGEPI5; break ;;
2) board=$ORANGEPI5B; break ;;
3) board=$ORANGEPI5PLUS; break ;;
4) exit 0;;
*) err "Invalid choice, please choose between 1-4"; sleep 2 ;;
esac
done
[ "$board" == "ORANGEPI5B" ] && return 0
if [ "$board" == "$ORANGEPI5" ]; then
while :
do
clear
echo "*****************************"
echo "* Supported boot storage *"
echo "*****************************"
echo "[1] M.2 NVME"
echo "[2] M.2 SATA"
echo "[3] Quit"
echo "-----------------------------"
echo "Enter your choice:"
read your_choice
case $your_choice in
1) m2=nvme; break ;;
2) m2=sata; break ;;
3) exit 0;;
*) err "Invalid choice, please choose between 1-3"; sleep 2 ;;
esac
done
fi
if [ "$board" == "$ORANGEPI5PLUS" ]; then
while :
do
clear
echo "*****************************"
echo "* Supported boot storage *"
echo "*****************************"
echo "[1] M.2 NVME"
echo "[2] EMMC"
echo "[3] Quit"
echo "-----------------------------"
echo "Enter your choice:"
read your_choice
case $your_choice in
1) m2=nvme; break ;;
2) m2=emmc; break ;;
3) exit 0;;
*) err "Invalid choice, please choose between 1-3"; sleep 2 ;;
esac
done
fi
}
num_arg=$#
if [ "$num_arg" -eq 0 ]; then
enter_menu
else
read_cmd_line "$@"
fi
[ "$board" == "$ORANGEPI5B" ] && m2="emmc"
[ -z "$board" ] && err "option '--board orangepi5/orangepi5b/orangepi5plus' is required"
[ -z "$m2" ] && err "option '--boot sata/nvme/emmc' is required"
echo "*****************************"
echo "* board: $board, supported install storage: $m2 *"
echo "*****************************"
cwd="$(pwd)"
if [ -z "$target" ] && [ "$inplace" != "true" ] && [ -z "$src" ]; then
target="$(echo "$0" | sed s/.run/.img/g)"
echo "$target" | grep -q '.img' || target="${target}.img"
fi
if [ -f "$target" ]; then
rm $target || err "$target already exists, please remove it first"
fi
[ -d "$target" ] && err "$target already exists, please remove it first"
command -v "tar" > /dev/null 2>&1 || err "command tar is not found"
if [ "$skip" == "false" ]; then
cat $self | tail -n +${shell_lines} | tar xJf - -C $TMP
if [ "$?" -ne 0 ]; then
rm "$target"
err "failed to uncompress image"
fi
else
if [ "$inplace" == "false" ]; then
cp $src $target || err "failed to cp $src $target"
else
target="$src"
fi
fi
[ "$inplace" == "false" ] && mv "$(find $TMP -maxdepth 1 -name "*.img" -or -name "*.bin" )" $target || err "failed to cp $src $target"
if [ "$m2" == "nvme" ]; then
magic=$NVME_MAGIC
elif [ "$m2" == "emmc" ]; then
magic=$EMMC_MAGIC
else
magic=$SATA_MAGIC
fi
board_magic="${BOARD_MAP[$board]}"
[ -z "$board_magic" ] && err "empty board_magic for board $board"
echo "board: $board storage: $magic"
echo -n "$magic" | dd of="$target" bs=$SECTOR_SIZE seek="$MAGIC_SECTOR" conv=fdatasync,notrunc &>/dev/null
echo -n "$board_magic" | dd of="$target" bs=$SECTOR_SIZE seek="$BOARD_MAGIC_SECTOR" conv=fdatasync,notrunc &>/dev/null
echo "Generated image: $(realpath ${target})"
image_dir="${TMP}/${board}"
image="$target"
echo "Installing uboot firmware on ${image}"
dd if="${image_dir}/idbloader.img" of="$image" \
conv=notrunc,fsync \
seek=64 &>/dev/null || err "fail to install idbloader"
dd if="${image_dir}/u-boot.itb" of="$image" \
conv=notrunc,fsync \
seek=16384 &>/dev/null || err "fail to install u-boot"
echo "Installed bootloader to ${image}"
cleanup
exit 0