-
Notifications
You must be signed in to change notification settings - Fork 7
/
flash.sh
executable file
·124 lines (106 loc) · 2.15 KB
/
flash.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
#!/bin/bash
#
# flash zipped (bz2/gz/xz) image to device
#
set -e
shopt -s nullglob
if [[ $(uname -s) != "Darwin" ]]
then
echo "Only support MacOSX!"
exit 5
fi
flash() {
local IMG=${1?:image is missing}
local DEV=${2?:device is missing}
local CMD=gzip
if [[ ! -f $IMG ]]
then
echo "No such image: $IMG"
exit 1
fi
if [[ ! -b $DEV ]]
then
echo "No such device: $DEV"
exit 2
fi
echo "+++ umount $DEV"
diskutil umountDisk $DEV
sleep 2
echo "+++ write $IMG => $DEV"
case $IMG in
*.img.bz2)
CMD=bz2
;;
*.img.gz)
CMD=gzip
;;
*.img.xz)
CMD=xz
;;
*)
echo "Bad image format!"
exit 4
;;
esac
$CMD -cd $IMG | pv | sudo dd of=/dev/r${DEV#/dev/} bs=32m
sleep 2
echo "+++ eject $DEV"
diskutil eject $DEV
sleep 2
}
main() {
local IMAGES=(*.img.bz2 *.img.gz *.img.xz)
local DEVICES=($(diskutil list | awk '/\(external, physical\):$/ {print $1}'))
local IMG DEV
echo "=== SELECT IMAGE ==="
if ((${#IMAGES[@]} == 0))
then
echo "No images to choose!"
exit 1
fi
select IMG in "${IMAGES[@]}"
do
case $IMG in
*.img.*)
break
;;
*)
echo "Select image failed!"
exit 1
;;
esac
done
echo "=== SELECT DEVICE ==="
if ((${#DEVICES[@]} == 0))
then
echo "No devices to choose!"
exit 2
fi
select DEV in "${DEVICES[@]}"
do
case $DEV in
/dev/disk[0-9])
break
;;
*)
echo "Select device failed!"
exit 2
;;
esac
done
echo "=== FLASH IMAGE TO DEVICE ==="
echo
echo "IMG: $IMG"
echo "DEV: $DEV"
echo
read -p 'Do you want to continue? (y/N): ' ANSWER
if [[ $ANSWER == [Yy] ]]
then
flash $IMG $DEV
exit 0
else
echo "Operation aborted!"
exit 3
fi
}
main