use hotplug2 for serializing hotplug events on 2.6
[openwrt.git] / target / linux / brcm-2.4 / base-files / default / etc / preinit
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /etc/functions.sh
5 . /etc/diag.sh
6
7 failsafe() {
8         lock /tmp/.failsafe
9
10         echo "0 1 2 3 4 5u*" > /proc/switch/eth0/vlan/0/ports
11         
12         set_state failsafe
13         [ -x "/usr/sbin/nvram" ] && {
14                 [ "$(nvram get boot_wait)" != "on" ] && {
15                         nvram set boot_wait=on
16                         nvram commit
17                 }
18         }
19
20         netmsg 192.168.1.255 "Entering Failsafe!"
21         telnetd -l /bin/login <> /dev/null 2>&1
22
23         ash --login
24 }
25
26 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
27 mount none /proc -t proc
28 size=$(awk '/Mem:/ {l=5242880;print((s=$2/2)<l)?$2-l:s}' /proc/meminfo)
29 mount none /tmp -t tmpfs -o size=$size
30 if grep devfs /proc/filesystems > /dev/null; then
31         mount none /dev -t devfs
32         M0=/dev/pty/m0
33         M1=/dev/pty/m1
34         HOTPLUG=/sbin/hotplug-call
35 else
36         mount -t sysfs none /sys
37         mount -t tmpfs tmpfs /dev -o size=512K
38         mknod /dev/console c 5 1
39         mkdir /dev/shm
40         /sbin/hotplug2 --no-persistent --coldplug --set-rules-file /etc/hotplug2-init.rules
41         M0=/dev/ptmx
42         M1=/dev/ptmx
43         HOTPLUG=
44 fi
45 mkdir -p /dev/pts
46 mount none /dev/pts -t devpts
47
48 # the shell really doesn't like having stdin/out closed
49 # that's why we use /dev/pty/m0 and m1 as replacement
50 # for /dev/console if there's no serial console available
51 dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null || \
52         exec <$M0 >$M1 2>&0
53
54 insmod diag
55 set_state preinit
56 trap 'FAILSAFE=true' USR1
57 echo '/sbin/hotplug.failsafe' > /proc/sys/kernel/hotplug
58
59 ifname=eth0
60
61 # hardware specific overrides
62 case "$(cat /proc/diag/model)" in
63         "Linksys WAP54G V1") ifname=eth1;;
64         "ASUS WL-HDD") ifname=eth1;;
65         "ASUS WL-300g") ifname=eth1;;
66         "ASUS (unknown, BCM4702)") ifname=eth1;;
67 esac
68
69 insmod switch-core
70 insmod switch-robo || insmod switch-adm || rmmod switch-core
71
72 ifconfig $ifname 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255 up
73
74 [ -d /proc/switch/eth0 ] && {
75         echo 1 > /proc/switch/eth0/reset
76
77         # this would be easier if we blasted the message across all ports
78         # but we don't want packets leaking across interfaces
79         for port in $(seq 0 4); do {
80                 echo "$port 5u*" > /proc/switch/eth0/vlan/0/ports
81                 netmsg 192.168.1.255 "Press reset now, to enter Failsafe!"
82         }; done
83 } || netmsg 192.168.1.255 "Press reset now, to enter Failsafe!"
84
85 sleep 2
86
87 eval ${FAILSAFE:+failsafe}
88
89 lock -w /tmp/.failsafe
90 set_state preinit
91 echo "$HOTPLUG" > /proc/sys/kernel/hotplug
92
93 ifconfig $ifname 0.0.0.0 down
94
95 mount_root
96 exec /sbin/init