forked from zaclimon/android_device_samsung_ypg1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_mtd.sh
220 lines (189 loc) · 3.75 KB
/
convert_to_mtd.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
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
#!/tmp/busybox sh
exec >& /sdcard/convert.debug
set -x
export PATH="/tmp"
CUT="/tmp/busybox cut"
DATE="/tmp/busybox date"
DD="/tmp/busybox dd"
GREP="/tmp/busybox grep"
MKDIR="/tmp/busybox mkdir"
MOUNTPOINT="/tmp/busybox mountpoint"
MOUNT="/tmp/busybox mount"
REBOOT="/tmp/busybox reboot recovery"
RM="/tmp/busybox rm"
SED="/tmp/busybox sed"
SYNC="/tmp/busybox sync"
TAR="/tmp/busybox tar"
TOUCH="/tmp/busybox touch"
UMOUNT="/tmp/busybox umount"
BOOTMODE="/tmp/bootmode"
ERASE_IMAGE="/tmp/erase_image"
MKE4FS="/tmp/make_ext4fs"
# For sanity
return_true="return 0"
return_false="return 1"
log()
{
echo "$@" >> /sdcard/convert.log
}
is_mounted()
{
local path="$1"
$MOUNTPOINT "$path" >/dev/null 2>/dev/null
return $?
}
mtd_dev_for()
{
local name="$1"
local line
local mtdname
local dev
line=`$GREP \"$name\" /proc/mtd`
if test -z "$line"; then
log "No such MTD partition: $name"
exit 1
fi
mtdname=`echo $line | $CUT -d':' -f1`
if test -z "$mtdname"; then
log "Error parsing /proc/mtd for $name"
exit 1
fi
mtdname=`echo $mtdname | $SED 's/mtd/mtdblock/'`
dev="/dev/block/$mtdname"
if test ! -e "$dev"; then
log "Cannot find device $dev"
exit 1
fi
echo $dev
}
fs_save()
{
local name="$1"
local dev="/dev/block/$2"
local was_mounted
if is_mounted "/$name"; then
was_mounted="yes"
else
was_mounted="no"
$MKDIR -p "/$name"
$MOUNT -t rfs -o ro $dev "/$name"
if test $? -ne 0; then
$MOUNT -t ext4 -o ro $dev "/$name"
fi
if test $? -ne 0; then
log "Cannot mount $dev on $name: tried rfs and ext4"
exit 1
fi
fi
cd "/$name"
$TAR -c -v -f "/sdcard/$name.tar" .
if test $? -ne 0; then
log "Cannot save $name: tar failed"
exit 1
fi
cd /
if test "$was_mounted" = "no"; then
$UMOUNT "/$name"
fi
}
fs_restore_yaffs2()
{
local name="$1"
local mtdname="$2"
local dev
local was_mounted
dev=`mtd_dev_for $mtdname`
if test ! -e /sdcard/$name.tar; then
log "Cannot restore $name: no backup found"
exit 1
fi
if is_mounted "/$name"; then
was_mounted="yes"
$UMOUNT "/$name"
else
was_mounted="no"
$MKDIR -p "/$name"
fi
$ERASE_IMAGE $mtdname
$MOUNT -t yaffs2 $dev "/$name"
if test $? -ne 0; then
log "Cannot restore $name: mount failed"
exit 1
fi
cd "/$name"
$TAR -x -v -f "/sdcard/$name.tar"
if test $? -ne 0; then
log "Cannot restore $name: tar failed"
exit 1
fi
cd /
if test "$was_mounted" = "no"; then
$UMOUNT "/$name"
fi
}
fs_restore_ext4()
{
local name="$1"
local blkname="$2"
local dev
local was_mounted
dev="/dev/block/$blkname"
if test ! -e /sdcard/$name.tar; then
log "Cannot restore $name: no backup found"
exit 1
fi
if is_mounted "/$name"; then
was_mounted="yes"
$UMOUNT "/$name"
else
was_mounted="no"
fi
$MKE4FS -b 4096 -g 32768 -i 8192 -I 256 -a ERASE_IMAGE $dev
$MOUNT -t ext4 $dev "/$name"
cd "/$name"
$TAR -xf "/sdcard/$name.tar"
if test $? -ne 0; then
log "Cannot restore $name: tar failed"
exit 1
fi
cd /
if test "$was_mounted" = "no"; then
$UMOUNT "/$name"
fi
}
fs_format_yaffs2()
{
local name="$1"
local mtdname="$2"
$ERASE_IMAGE $mtdname
}
if ! is_mounted "/sdcard"; then
log "Cannot convert: sdcard not mounted"
exit 1
fi
log "$0: start at `$DATE`"
if test -e "/proc/mtd"; then
# Running on an MTD kernel
log "mtd kernel detected"
if test -e /sdcard/.convert_to_mtd; then
log "Restoring filesystems"
fs_restore_yaffs2 efs efs
fs_restore_yaffs2 dbdata dbdatafs
fs_restore_ext4 data mmcblk0p2
fs_format_yaffs2 cache cache
$RM -f /sdcard/.convert_to_mtd
else
log "Not restoring filesystems"
fi
else
# Running on a BML/STL kernel
log "bml/stl kernel detected"
fs_save efs stl3
fs_save dbdata stl10
fs_save data mmcblk0p2
$TOUCH /sdcard/.convert_to_mtd
# $BOOTMODE set recovery
$SYNC
$REBOOT
fi
log "$0: end at `$DATE`"