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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/sh

# Generic ramdisk init process for booting a device. Based Marko Saukko's
# initrd for Galaxy Note GT-i9250.
#
# Copyright (C) 2014 Jolla Ltd.
# Copyright (C) 2012 Marko Saukko
#
# 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, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# IRC: Sage @ #mer & #nemomobile @ Freenode

# Location of the device init script, if not set, few defaults are tried.
INITBIN=/sbin/preinit

export PATH=/sbin:/bin:/usr/bin:/usr/sbin

# Where to mount the rootfs
ROOTMNTDIR="/rootfs"

# With MNTSCRIPT, you can use your own mounting script and bypass the default
# root mounting. The script should take $ROOTMNTDIR as parameter
# for where to mount the root.
MNTSCRIPT="/sbin/root-mount"

# Default setting is rndis - add mass_storage for a debug boot
# enable using usb_setup
USB_FUNCTIONS=rndis

# We're in the real rootfs running as init-debug
#EXPLICIT_BUSYBOX="/bin/busybox-static"
EXPLICIT_BUSYBOX="/sbin/busybox-static"
TELNET_DEBUG_PORT=2323

ANDROID_USB=/sys/class/android_usb/android0
GADGET_DIR=/config/usb_gadget
#LOCAL_IP=192.168.2.15
LOCAL_IP=10.42.66.66

fail()
{
	echo "initrd: Failed" > /dev/kmsg
	echo "initrd: $1" > /dev/kmsg
	reboot2 recovery
}

# This sets up the USB with whatever USB_FUNCTIONS are set to via configfs
usb_setup_configfs() {
    G_USB_ISERIAL=$GADGET_DIR/g1/strings/0x409/serialnumber

    mkdir $GADGET_DIR/g1
    write $GADGET_DIR/g1/idVendor                   "0x18D1"
    write $GADGET_DIR/g1/idProduct                  "0xD001"
    mkdir $GADGET_DIR/g1/strings/0x409
    write $GADGET_DIR/g1/strings/0x409/serialnumber "$1"
    write $GADGET_DIR/g1/strings/0x409/manufacturer "Mer Boat Loader"
    write $GADGET_DIR/g1/strings/0x409/product      "$CUSTOMPRODUCT"

    if echo $USB_FUNCTIONS | grep -q "rndis"; then
        mkdir $GADGET_DIR/g1/functions/rndis.usb0
        mkdir $GADGET_DIR/g1/functions/rndis_bam.rndis
    fi
    #echo $USB_FUNCTIONS | grep -q "mass_storage" && mkdir $GADGET_DIR/g1/functions/storage.0

    mkdir $GADGET_DIR/g1/configs/c.1
    mkdir $GADGET_DIR/g1/configs/c.1/strings/0x409
    write $GADGET_DIR/g1/configs/c.1/strings/0x409/configuration "$USB_FUNCTIONS"

    if echo $USB_FUNCTIONS | grep -q "rndis"; then
        ln -s $GADGET_DIR/g1/functions/rndis.usb0 $GADGET_DIR/g1/configs/c.1
        ln -s $GADGET_DIR/g1/functions/rndis_bam.rndis $GADGET_DIR/g1/configs/c.1
    fi
    #echo $USB_FUNCTIONS | grep -q "mass_storage" && ln -s $GADGET_DIR/g1/functions/storage.0 $GADGET_DIR/g1/configs/c.1

    echo "$(ls /sys/class/udc)" > $GADGET_DIR/g1/UDC
}

# This sets up the USB with whatever USB_FUNCTIONS are set to via android_usb
usb_setup_android_usb() {
    G_USB_ISERIAL=$ANDROID_USB/iSerial
    write $ANDROID_USB/enable          0
    write $ANDROID_USB/functions       ""
    write $ANDROID_USB/enable          1
    usleep 500000 # 0.5 delay to attempt to remove rndis function
    write $ANDROID_USB/enable          0
    write $ANDROID_USB/idVendor        18D1
    write $ANDROID_USB/idProduct       D001
    write $ANDROID_USB/iManufacturer   "Mer Boat Loader"
    write $ANDROID_USB/iProduct        "$CUSTOMPRODUCT"
    write $ANDROID_USB/iSerial         "$1"
    write $ANDROID_USB/functions       $USB_FUNCTIONS
    write $ANDROID_USB/enable          1
}

# This determines which USB setup method is going to be used
usb_setup() {
    if [ -d $ANDROID_USB ]; then
        usb_setup_android_usb $1
    elif [ -d $GADGET_DIR ]; then
        usb_setup_configfs $1
    fi
}

# This lets us communicate errors to host (if it needs disable/enable then that's a problem)
usb_info() {
    # make sure USB is settled
    echo "########################## usb_info: $1"
    sleep 1
    write $G_USB_ISERIAL "$1"
}

new_debug_session() {
    usb_setup "Jolla Debug setting up"
    ip link set rndis0 up
    if ipaddr add 10.42.66.66/29 broadcast 10.42.66.255 dev rndis0 ; then
        USB_IFACE=rndis0
    elif ipaddr add 10.42.66.66/29 broadcast 10.42.66.255 dev usb0 ; then
        USB_IFACE=usb0
    else
        usb_info "Jolla Debug: ERROR: could not setup USB as usb0 or rndis0"
        dmesg
        sleep 60 # plenty long enough to check usb on host
        reboot -f
    fi
    ipaddr add 192.168.2.15/24 broadcast 192.168.2.255 dev $USB_IFACE label rndis0:0

    udhcpd

    echo "Starting telnetd..."
    telnetd -l/usr/bin/recovery-menu &
}

run_debug_session() {
    CUSTOMPRODUCT=$1
    echo "########################## Debug session : $1"
    usb_setup "Mer Debug setting up (DONE_SWITCH=$DONE_SWITCH)"

    USB_IFACE=notfound
    #/sbin/ifconfig rndis0 $LOCAL_IP && USB_IFACE=rndis0
    /bin/ifconfig rndis0 $LOCAL_IP && USB_IFACE=rndis0
    if [ x$USB_IFACE = xnotfound ]; then
        #/sbin/ifconfig usb0 $LOCAL_IP && USB_IFACE=usb0
        /bin/ifconfig usb0 $LOCAL_IP && USB_IFACE=usb0
    fi
    # Report for the logs
    #/sbin/ifconfig -a
    /bin/ifconfig -a

    # Unable to set up USB interface? Reboot.
    if [ x$USB_IFACE = xnotfound ]; then
	    usb_info "Mer Debug: ERROR: could not setup USB as usb0 or rndis0"
	    dmesg
	    sleep 60 # plenty long enough to check usb on host
	    reboot -f
    fi

    # Create /etc/udhcpd.conf file.
    echo "start 192.168.2.20" > /etc/udhcpd.conf
    echo "end 192.168.2.90" >> /etc/udhcpd.conf
    echo "lease_file /var/udhcpd.leases" >> /etc/udhcpd.conf
    echo "interface $USB_IFACE" >> /etc/udhcpd.conf
    echo "option subnet 255.255.255.0" >> /etc/udhcpd.conf

    # Be explicit about busybox so this works in a rootfs too
    echo "########################## starting dhcpd"
    $EXPLICIT_BUSYBOX udhcpd

    if [ -z $DISABLE_TELNET ]; then
    # Non-blocking telnetd
    echo "########################## starting telnetd"
    # We run telnetd on different ports pre/post-switch_root This
    # avoids problems with an unterminated pre-switch_root telnetd
    # hogging the port
    $EXPLICIT_BUSYBOX telnetd -b ${LOCAL_IP}:${TELNET_DEBUG_PORT} -l /bin/sh

    # For some reason this does not work in rootfs
    usb_info "Mer Debug telnet on port $TELNET_DEBUG_PORT on $USB_IFACE $LOCAL_IP - also running udhcpd"
    fi
}

. /etc/sysconfig/display
#. /usr/bin/recovery-functions.sh

echo "Doing mounts... "

mkdir -p /proc
mount -t proc proc /proc

mkdir -p /sys
mount -t sysfs sys /sys

mkdir -p /dev
mount -t devtmpfs devtmpfs /dev
#mount /dev

# telnetd needs /dev/pts/ entries
mkdir /dev/pts
mount -t devpts devpts /dev/pts

mkdir /config
mount -t configfs none /config

echo "initrd: Starting ramdisk.." > /dev/kmsg

# Reset watchdog timer
echo "V" > /dev/watchdog

# Some filesystem tools may need mtab to work
cat /proc/mounts > /etc/mtab

#Run mdev
#echo "Running mdev..."
#mdev -s

#Set mdev as hotplug manager
#echo /bin/mdev > /proc/sys/kernel/hotplug

# Minimize power consumption by lowering display brightness to minimum
#write $DISPLAY_BRIGHTNESS_PATH $DISPLAY_BRIGHTNESS

mkdir -p $ROOTMNTDIR

# Mount the root filesystem
if [ -e $MNTSCRIPT ]; then
	$MNTSCRIPT $ROOTMNTDIR
	if [ $? -eq 0 ]; then
		echo "initrd: Mounting root succeeded" > /dev/kmsg
	else
		fail "Mouting root failed"
	fi
else
	fail "$MNTSCRIPT does not exist, cannot mount root!"
fi

echo "initrd: Searching for init process..." > /dev/kmsg

if [ -n $INITBIN ] || [ -e $ROOTMNTDIR/$INITBIN ]; then
	echo "initrd: Found $INITBIN" > /dev/kmsg
elif [ -e ${ROOTMNTDIR}/usr/sbin/init ]; then
	INITBIN="/usr/sbin/init"
elif [ -e ${ROOTMNTDIR}/sbin/init ]; then
	INITBIN="/sbin/init"
elif [ -e ${ROOTMNTDIR}/init ]; then
	INITBIN="/init"
else
	fail "Unable to find init process from rootfs."
fi

#run_debug_session "init-debug in real rootfs"
new_debug_session "init-debug in real rootfs"

# umount everything before doing switch root as the init process
# is responsible of doing these inside the final boot env.
umount -l /dev/pts
umount -l /config
umount -l /dev
umount -l /sys
umount -l /proc

# Old preinit ( < 1.0.4.* Sailfish releases) does not mount /dev so let's mount
# it here.
mkdir -p ${ROOTMNTDIR}/dev
mount -t devtmpfs devtmpfs ${ROOTMNTDIR}/dev

echo "initrd: Switching to rootfs at ${ROOTMNTDIR}, with init ${INITBIN}" > ${ROOTMNTDIR}/dev/kmsg

# usage: switch_root <newrootdir> <init> <args to init>
exec switch_root ${ROOTMNTDIR} ${INITBIN}