-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk-sd-image.sh
executable file
·154 lines (124 loc) · 3.75 KB
/
mk-sd-image.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
#!/bin/bash
# Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd.
# (http://www.friendlyarm.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
# Automatically re-run script under sudo if not root
if [ $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo "$0" "$@"
exit
fi
function usage() {
echo "Usage: $0 <debian|buildroot|friendlycore-arm64|friendlydesktop-arm64|lubuntu|eflasher|yocto>"
exit 0
}
if [ -z $1 ]; then
usage
fi
# ----------------------------------------------------------
# Get platform, target OS
true ${SOC:=rk3399}
true ${TARGET_OS:=${1,,}}
case ${TARGET_OS} in
debian* | buildroot* | friendlycore* | friendlydesktop* | lubuntu* | eflasher* | yocto*)
;;
*)
echo "Error: Unsupported target OS: ${TARGET_OS}"
exit 0
esac
# ----------------------------------------------------------
# Create zero file
CODENAME=bionic
case ${TARGET_OS} in
friendlycore-arm64)
RAW_FILE=${SOC}-sd-friendlycore-${CODENAME}-4.4-arm64-$(date +%Y%m%d).img
RAW_SIZE_MB=7800 ;;
friendlydesktop-arm64)
RAW_FILE=${SOC}-sd-friendlydesktop-${CODENAME}-4.4-arm64-$(date +%Y%m%d).img
RAW_SIZE_MB=7800 ;;
debian)
RAW_FILE=${SOC}-sd-debian9-4.4-armhf-$(date +%Y%m%d).img
RAW_SIZE_MB=7800 ;;
lubuntu)
RAW_FILE=${SOC}-sd-lubuntu-desktop-xenial-4.4-armhf-$(date +%Y%m%d).img
RAW_SIZE_MB=7800 ;;
eflasher)
RAW_FILE=${SOC}-eflasher-$(date +%Y%m%d).img
RAW_SIZE_MB=7800 ;;
buildroot)
RAW_FILE=${SOC}-sd-buildroot-linux-4.4-arm64-$(date +%Y%m%d).img
RAW_SIZE_MB=4000 ;;
yocto)
RAW_FILE=${SOC}-sd-yocto-linux-4.4-arm64-$(date +%Y%m%d).img
RAW_SIZE_MB=4000 ;;
*)
RAW_FILE=${SOC}-${TARGET_OS}-sd4g-$(date +%Y%m%d).img
RAW_SIZE_MB=3800 ;;
esac
OUT=out
if [ ! -d $OUT ]; then
echo "path not found: $PWD/$OUT"
exit 1
fi
RAW_FILE=${OUT}/${RAW_FILE}
BLOCK_SIZE=1024
let RAW_SIZE=(${RAW_SIZE_MB}*1000*1000)/${BLOCK_SIZE}
echo "Creating RAW image: ${RAW_FILE} (${RAW_SIZE_MB} MB)"
echo "---------------------------------"
if [ -f "${RAW_FILE}" ]; then
rm -f ${RAW_FILE}
fi
dd if=/dev/zero of=${RAW_FILE} bs=${BLOCK_SIZE} count=0 \
seek=${RAW_SIZE} || exit 1
sfdisk -u S -L -q ${RAW_FILE} 2>/dev/null << EOF
2048,,0x0C,-
EOF
if [ $? -ne 0 ]; then
echo "Error: ${RAW_FILE}: Create RAW file failed"
exit 1
fi
# ----------------------------------------------------------
# Setup loop device
LOOP_DEVICE=$(losetup -f)
echo "Using device: ${LOOP_DEVICE}"
if losetup ${LOOP_DEVICE} ${RAW_FILE}; then
USE_KPARTX=1
PART_DEVICE=/dev/mapper/`basename ${LOOP_DEVICE}`
sleep 1
else
echo "Error: attach ${LOOP_DEVICE} failed, stop now."
rm ${RAW_FILE}
exit 1
fi
# ----------------------------------------------------------
# Fusing all
true ${SD_FUSING:=$(dirname $0)/fusing.sh}
${SD_FUSING} ${LOOP_DEVICE} ${TARGET_OS}
RET=$?
if [ "x${TARGET_OS}" = "xeflasher" ]; then
mkfs.exfat ${LOOP_DEVICE}p1 -n FriendlyARM
fi
# cleanup
losetup -d ${LOOP_DEVICE}
if [ ${RET} -ne 0 ]; then
echo "Error: ${RAW_FILE}: Fusing image failed, cleanup"
rm -f ${RAW_FILE}
exit 1
fi
echo "---------------------------------"
echo "RAW image successfully created (`date +%T`)."
ls -l ${RAW_FILE}
echo "Tip: You can compress it to save disk space."