forked from Halolo/orange-pi-distro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash-sdcard.sh
executable file
·162 lines (140 loc) · 3.52 KB
/
flash-sdcard.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
#!/bin/bash
# Quit on error
set -e
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# run in script dir
SCRIPT_DIR="$(dirname "$0")"
cd "$SCRIPT_DIR"
# Usage if bad arguments were passed
usage () {
echo ""
echo "Options:"
echo " -d device"
echo " -m machine (orange-pi-pc, orange-pi-pc-plus or orange-pi-zero)"
echo " -i image name"
echo ""
echo "Others:"
echo " -h = This help menu"
}
# Option parsing
while getopts ":hd:m:i:" opt; do
case "$opt" in
d)
DEVICE="$OPTARG"
;;
m)
MACHINE="$OPTARG"
;;
i)
IMAGE="$OPTARG"
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
IMG_PATH="$SCRIPT_DIR/build/tmp-glibc/deploy/images/$MACHINE"
BOOTLOADER_FILE="$IMG_PATH/u-boot-sunxi-with-spl.bin"
BOOTSCR_FILE="$IMG_PATH/boot.scr"
UIMAGE_FILE="$IMG_PATH/uImage"
if [ "$MACHINE" = "orange-pi-zero" ]; then
DTB_FILE="$IMG_PATH/uImage-sun8i-h2-plus-orangepi-zero.dtb"
elif [ "$MACHINE" = "orange-pi-pc" ]; then
DTB_FILE="$IMG_PATH/uImage-sun8i-h3-orangepi-pc.dtb"
elif [ "$MACHINE" = "orange-pi-pc-plus" ]; then
DTB_FILE="$IMG_PATH/uImage-sun8i-h3-orangepi-pc-plus.dtb"
fi
RFS_FILE="$IMG_PATH/$IMAGE-$MACHINE.tar.gz"
BOOT_PART="$SCRIPT_DIR/p1"
RFS_PART="$SCRIPT_DIR/p2"
if [ -z "$DEVICE" ]; then
echo "No devices" 1>&2
usage
exit 1
fi
if [ ! -e "$BOOTSCR_FILE" ] || [ ! -e "$UIMAGE_FILE" ] || [ ! -e "$DTB_FILE" ] || [ ! -e "$RFS_FILE" ] || [ ! -e "$BOOTLOADER_FILE" ]; then
echo "Invalid image path" 1>&2
usage
exit 1
fi
cleanup() {
echo "Cleaning up..."
cd "$SCRIPT_DIR"
rm -rf "$BOOT_PART"
rm -rf "$RFS_PART"
}
exit_script() {
echo "Unmounting partitions"
timeout 5m umount "$DEVICE"? &> /dev/null
cleanup
if [ "$1" -ne 0 ]; then
echo "Fail :(" 1>&2
else
echo "Success :)"
fi
}
trap 'exit_script $?' TERM EXIT INT
echo "Creating partition table..."
timeout 1m umount "$DEVICE"? || echo "Continue..."
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${DEVICE}" > /dev/null
o # New MSDOS partition table
n # new partition
# default
# default
4096
45055
t # partition type
c # win 95 fat32
a # bootable
n # new partition
# default
# default
45056
# default
t # partition type
2 # partition 2
83 # Linux
w # write the partition table
EOF
echo -e "Done.\n"
echo "Creating partitions..."
echo "BOOT..."
timeout 5m mkfs.vfat -n "$MACHINE" -S 512 "${DEVICE}1" > /dev/null
echo "RFS..."
timeout 5m mkfs.ext4 -L "rfs" "${DEVICE}2" > /dev/null
echo -e "Done.\n"
echo "Mounting partitions..."
rm -rf "$BOOT_PART"
rm -rf "$RFS_PART"
mkdir "$BOOT_PART"
mkdir "$RFS_PART"
timeout 1m mount "${DEVICE}1" "$BOOT_PART"
timeout 1m mount "${DEVICE}2" "$RFS_PART"
echo -e "Done.\n"
echo "Installing boot partition..."
rm -rf "${BOOT_PART:?}/"*
cp "$BOOTSCR_FILE" "$BOOT_PART"
cp "$UIMAGE_FILE" "$BOOT_PART"
if [ "$MACHINE" = "orange-pi-zero" ]; then
cp "$DTB_FILE" "$BOOT_PART/sun8i-h2-plus-orangepi-zero.dtb"
elif [ "$MACHINE" = "orange-pi-pc" ]; then
cp "$DTB_FILE" "$BOOT_PART/sun8i-h3-orangepi-pc.dtb"
elif [ "$MACHINE" = "orange-pi-pc-plus" ]; then
cp "$DTB_FILE" "$BOOT_PART/sun8i-h3-orangepi-pc-plus.dtb"
fi
echo "Extracting rfs (could take a while)..."
rm -rf "${RFS_PART:?}/"*
timeout 5m tar -xf "$RFS_FILE" -C "$RFS_PART"
timeout 10m sync
echo "Writing bootloader..."
timeout 5m dd if="$BOOTLOADER_FILE" of="$DEVICE" bs=1024 seek=8 conv=notrunc
echo -e "Done.\n"