summaryrefslogtreecommitdiff
path: root/utils/ypr0tools/files/common/etc/safemode/smode
blob: 9a0cea9d66fcc64f9cc21fcde3b9c7b1ca00f261 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/sh

######################################################################
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
#
#   * Safe Mode for Samsung YP-R0 / YP-R1 *
#   * Copyright (C) 2013 Lorenzo Miori and VanniX
######################################################################

# 0 = R0 ; 1 = R1
PLATFORM=0
BUTTON_DEVICE="/dev/r0Btn"
PMK=$(echo -e -n "\x01") # same for both devices
STORAGES="/dev/stl3,/dev/stl2,/dev/mmcblk0"

MODE="normal"

if [ $# -eq 1 ]
then
    MODE=$1
fi

# rather simple but effective check
# different key codes to trigger safe mode
if [ -e "/usr/local/bin/r1" ]
then
    # running on YP-R1 model
    BACK=$(echo -e -n "\x03")
    PLATFORM=1
    BUTTON_DEVICE="/dev/r1Button"
    STORAGES="/dev/stl3,/dev/stl2"
else
    BACK=$(echo -e -n "\x08")
    PLATFORM=0
    BUTTON_DEVICE="/dev/r0Btn"
    STORAGES="/dev/stl3,/dev/stl2,/dev/mmcblk0"
fi

power_pressed()
{
    VAR=$(dd if=$BUTTON_DEVICE bs=4 count=1)
    [[ "$VAR" = "$PMK" ]]
    return $?
}

cable_disconnected()
{
    if [ $PLATFORM -eq 0 ]
    then
        /usr/local/bin/minird 0x0a | grep -q 0x00
        return $?
    else
        /etc/safemode/cable_detect
        return $?
    fi
}

enable_display()
{
    echo -n "0" > /sys/devices/platform/afe.0/bli
    echo -n "1" > /sys/class/graphics/fb0/blank
    echo -n "0" >> /sys/class/graphics/fb0/blank
    echo -n "1" > /sys/devices/platform/afe.0/bli
}

display_image()
{
    cat $1 > "/dev/fb0"
}

if [ $MODE != "forced" ]
then
    KEY=$(dd if=$BUTTON_DEVICE bs=4 count=1)
    if [[ "$KEY" != "$BACK" ]]; then
        # do not enter safe mode and continue normal boot
        exit 0
    fi
fi

# Here we entered safe mode, so first clean the display
# and turn on backlight at minimum level
enable_display

# there are different ROMs around...
if [ -e "/etc/safemode/safemode.raw" ]
then
    DefIMG="/etc/safemode/safemode.raw"
else
    DefIMG="/etc/mods/safe_mode.raw"
fi
RbtIMG="/etc/safemode/post_smode.raw"
PreIMG="/etc/safemode/pre_smode.raw"

NOUSB=true

if cable_disconnected
then

    display_image $PreIMG

    while $NOUSB
    do

        # User aborts safe mode before mounting anything, just exit
        # and continue normal boot
        if power_pressed
        then
            sleep 1
            if power_pressed
            then
                exit 1
            fi
        fi

        if cable_disconnected
        then
            echo "USB not connected - Waiting"
        else
            sleep 1
            if cable_disconnected
            then
                echo "USB first check OK - Waiting"
            else
                sleep 1
                if cable_disconnected
                then
                    echo "USB second check OK - Waiting"
                else
                    NOUSB=false
                    USB=true
                fi
            fi
        fi
    done
else
    while $NOUSB
    do
        if cable_disconnected
        then
            echo "USB not connected - Waiting"
        else
            sleep 1
            if cable_disconnected
            then
                echo "USB first check OK - Waiting"
            else
                NOUSB=false
                USB=true
            fi
        fi
    done
fi

display_image $DefIMG

echo "Enabling usb storage..."
lsmod | grep g_file_storage
if [ $? == 0 ]
then
    umount /mnt/media1/dev/gadget
fi

umount /mnt/media1
umount /mnt/media0

lsmod | grep rfs
if [ $? == 0 ]
then
    rmmod rfs
fi

lsmod | grep g_file_storage
if [ $? == 0 ]
then
    rmmod gadgetfs
    rmmod g_file_storage
    rmmod arcotg_udc
fi

lsmod | grep g_serial
if [ $? == 0 ]
then
    rmmod g_serial
fi

lsmod | grep g_file_storage
if [ $? != 0 ]
then
    modprobe g-file-storage file=$STORAGES removable=1
fi

SAFE=true
while $SAFE
do
    if power_pressed
    then
        sleep 1
        if power_pressed
        then
            SAFE=false
        fi
    fi
done

echo "Removing loaded modules..."
rmmod g_file_storage
rmmod arcotg_udc

display_image $RbtIMG

USB=true

while $USB
do
    if cable_disconnected
    then
        sleep 1
        if cable_disconnected
        then
            if cable_disconnected
            then
                sleep 1
                if cable_disconnected
                then
                    USB=false
                else
                    echo "USB connected - Waiting"
                    USB=true
                fi
            else
                echo "USB connected - Waiting"
                USB=true
            fi
        else
            echo "USB connected - Waiting"
            USB=true
        fi
    else
        echo "USB connected - Waiting"
        USB=true
    fi
done

#power key pressed for almost 2 sec and cable disconnected. Power off!
exit 1