-
Notifications
You must be signed in to change notification settings - Fork 16
/
fusing.sh
executable file
·152 lines (128 loc) · 3.59 KB
/
fusing.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
#!/bin/bash
set -eu
# Copyright (C) Guangzhou FriendlyElec Computer Tech. Co., Ltd.
# (http://www.friendlyelec.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.
# ----------------------------------------------------------
# Checking device for fusing
if [ $# -lt 2 ]; then
echo "Usage: $0 <device|rawfile> <img dir>"
exit 0
fi
if [ ! -e $1 ]; then
echo "Error: $1 does not exist."
exit 1
fi
if [ ! -d $2 ]; then
echo "Error: $2 does not exist."
exit 1
fi
true ${RK_PARAMETER_TXT:=}
if [ -z $RK_PARAMETER_TXT ]; then
if [ -f $2/parameter.txt ]; then
RK_PARAMETER_TXT=$(dirname $0)/${2}/parameter.txt
fi
fi
if [ -z $RK_PARAMETER_TXT ]; then
echo "Error: pls set RK_PARAMETER_TXT."
exit 1
fi
case $1 in
/dev/sd[a-z] | /dev/loop[0-9]* | /dev/mmcblk[0-9]*)
DEV_NAME=`basename $1`
BLOCK_CNT=`cat /sys/block/${DEV_NAME}/size`
;;
*)
echo "Error: Unsupported SD reader ($1)"
exit 0
esac
case $1 in
/dev/sd[a-z])
REMOVABLE=`cat /sys/block/${DEV_NAME}/removable` ;;
/dev/mmcblk[0-9]* | /dev/loop[0-9]*)
REMOVABLE=1 ;;
*)
echo "Error: Unsupported SD reader"
exit 0
esac
if [ ${REMOVABLE} -le 0 ]; then
echo "Error: $1 is a non-removable device. Stop."
exit 1
fi
if [ -z ${BLOCK_CNT} -o ${BLOCK_CNT} -le 0 ]; then
echo "Error: $1 is inaccessible. Stop fusing now!"
exit 1
fi
let DEV_SIZE=${BLOCK_CNT}/2
if [ ${DEV_SIZE} -gt 64000000 ]; then
echo "Error: $1 size (${DEV_SIZE} KB) is too large"
exit 1
fi
true ${MIN_SIZE:=600000}
if [ ${DEV_SIZE} -le ${MIN_SIZE} ]; then
echo "Error: $1 size (${DEV_SIZE} KB) is too small"
echo " please try another SD card."
exit 1
fi
# Automatically re-run script under sudo if not root
if [ -b $1 -a $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo --preserve-env "$0" "$@"
exit
fi
HOST_ARCH=
if uname -mpi | grep aarch64 >/dev/null; then
HOST_ARCH="aarch64/"
fi
# ----------------------------------------------------------
# Fusing idbloader, bootloader, trust to card
true ${BOOT_DIR:=./prebuilt}
function fusing_bin() {
[ -z $2 -o ! -f $1 ] && return 1
echo "---------------------------------"
echo "$1 fusing"
echo "dd if=$1 of=/dev/${DEV_NAME} bs=512 seek=$2"
dd if=$1 of=/dev/${DEV_NAME} bs=512 seek=$2 conv=fdatasync
ddret=$?
}
# umount all at first
if [ ! -z ${DEV_NAME} ]; then
set +e
umount /dev/${DEV_NAME}* > /dev/null 2>&1
set -e
fi
#<Message Display>
echo "---------------------------------"
echo "Bootloader image is fused successfully."
echo ""
# ----------------------------------------------------------
# partition card & fusing filesystem
true ${SD_UPDATE:=./tools/${HOST_ARCH}sd_update}
[[ -z $2 && ! -f "${RK_PARAMETER_TXT}" ]] && {
echo "Not found ${RK_PARAMETER_TXT}"
exit 1
}
${SD_UPDATE} -d /dev/${DEV_NAME} -p ${RK_PARAMETER_TXT}
if [ $? -ne 0 ]; then
echo "Error: filesystem fusing failed, Stop."
exit 1
fi
if ! command -v partprobe &>/dev/null; then
sudo apt-get install parted
fi
partprobe /dev/${DEV_NAME} -s 2>/dev/null
echo "---------------------------------"
echo "All done."