xref: /minix3/etc/usr/rc (revision b80da2a01d0bb632707b7b4e974aa32eaebbcc6f)
1# /usr/etc/rc - continued system initialization.
2
3RANDOM_FILE=/usr/adm/random.dat
4LOCAL_FILE=/usr/etc/rc.local
5
6ARCH="`sysenv arch`"
7
8if [ ! "$ARCH" ]
9then    # Older kernels do not provide an arch sysenv variable.
10        # We assume we are on x86 then, as existing systems with
11        # kernel and userland (i.e. this script) unsynchronized
12        # will be x86.
13        ARCH=i386
14fi
15
16# Get $SERVICES_DIRS
17. /etc/rc.conf
18
19# Directories to find services in
20if [ ! "$SERVICES_DIRS" ]
21then	SERVICES_DIRS=/service
22fi
23
24# Booting from cd?
25bootcd="`/bin/sysenv bootcd`"
26
27case "$#:$1" in
281:autoboot)
29    action=start
30    ;;
311:start|1:stop|1:down)
32    action=$1
33    ;;
34*)  echo >&2 "Usage: $0 autoboot|start|stop|down"
35    exit 1
36esac
37
38if [ -f "$LOCAL_FILE" ]
39then	. "$LOCAL_FILE" $1
40fi
41
42disabled()
43{
44    ifs="$IFS"; IFS=,
45    for skip in `sysenv disable`
46    do
47        if [ "$skip" = "$1" ]
48	then
49                IFS="$ifs"; unset ifs
50		return 0
51	fi
52    done
53    IFS="$ifs"; unset ifs
54    return 1
55}
56
57daemonize()
58{
59    # Function to start a daemon, if it exists.
60    local IFS=':'
61    local name="$1"
62    test "$1" = tcpd && name="$2"
63
64    for dir in $PATH
65    do
66	if [ -f "$dir/$1" ]
67	then
68
69            # check if this service is disabled at the boot monitor.
70            if disabled $name; then return; fi
71
72	    echo -n " $name"
73	    "$@" &
74	    return
75	fi
76    done
77}
78
79up()
80{
81    # Function to dynamically start a system service
82    opt=""
83    prefix=$(expr "$1 " : '\(-\)')
84    if [ "$prefix" = "-" ];
85    then
86         opt=$1
87         shift
88    fi
89    service=$1
90    shift
91
92    # First check if this service is disabled at the boot monitor.
93    if disabled $service; then return; fi
94
95    # Service is not disabled. Try to bring it up.
96    found=""
97    for dir in $SERVICES_DIRS
98    do	bin=$dir/$service
99	if [ -x $bin -a -z "$found" ]
100	then	service $opt up $bin "$@"
101    		echo -n " $service"
102		found=yes
103	fi
104    done
105    if [ -z "$found" ]
106    then	echo " ($service not found in $SERVICES_DIRS)"
107    fi
108}
109
110get_eth_labels() {
111  # Filter out the non-vlan ethernet entries from inet.conf.
112  # Produce as output a list of "drivername_instancenr"-formatted labels.
113  sed 's/\008/ /g' /etc/inet.conf | \
114    sed -n 's/^ *eth[0-9][0-9]* *\([^ ][^ ]*\) *\([0-9][0-9]*\).*$/\1_\2/p' | \
115    grep -v '^vlan_'
116}
117
118# Detect expansion boards on the BeagleBone and load the proper drivers.
119capemgr() {
120
121    # Probe each possible cape EEPROM slave address for a BeagleBone cape.
122    for slave_addr in 54 55 56 57
123    do
124
125        # See if there is a readable EEPROM with address ${slave_addr}.
126        eepromread -f /dev/i2c-3 -a 0x${slave_addr} > /dev/null 2>&1
127        RESULT=$?
128	if [ $RESULT -eq 0 ]
129	then
130
131	    # Found an alive EEPROM. Try reading the cape name.
132            CAPE=`eepromread -i -f /dev/i2c-3 -a 0x${slave_addr} | \
133	        sed -n 's/^PART_NUMBER     : \(.*\)$/\1/p' | \
134		sed -e 's/\.*$//g'` # Strip trailing periods.
135
136	    # Look for a cape specific RC script.
137            if [ -x /etc/rc.capes/${CAPE} ]
138	    then
139
140	        # CAT24C256 EEPROM -- all capes have this chip.
141		test -e /dev/eepromb3s${slave_addr} || \
142		    (cd /dev && MAKEDEV eepromb3s${slave_addr})
143		up cat24c256 -dev /dev/eepromb3s${slave_addr} \
144		    -label cat24c256.3.${slave_addr} \
145		    -args "bus=3 address=0x${slave_addr}"
146
147                # Load the drivers for the cape and do any other configuration.
148		. "/etc/rc.capes/${CAPE}"
149
150	    else
151
152		echo ""
153	        echo "** UNSUPPORTED CAPE: ${CAPE}"
154		echo ""
155
156	    fi
157	fi
158    done
159}
160
161DAEMONS=/etc/rc.daemons
162
163case $action in
164start|autoboot)
165    # Select console font.
166    test -f /etc/font && loadfont /etc/font </dev/console
167
168    # Cleanup.
169    rm -rf /tmp/* /usr/run/* /usr/spool/lpd/* /usr/spool/locks/*
170
171    # Start servers and drivers set at the boot monitor.
172    echo -n "Starting services:"
173    up -n random -dev /dev/random -period 3HZ
174
175    # load random number generator
176    if [ -f $RANDOM_FILE ]
177    then
178    	cat < $RANDOM_FILE >/dev/random
179    	# overwrite $RANDOM_FILE. We don't want to use this data again
180    	dd if=/dev/random of=$RANDOM_FILE bs=1024 count=1 2> /dev/null
181    fi
182
183    # start network driver instances for all configured ethernet devices
184    for label in $(get_eth_labels); do
185        driver=$(echo $label | sed 's/\(.*\)_.*/\1/')
186        instance=$(echo $label | sed 's/.*_//')
187        eval arg=\$${driver}_arg
188        if [ ! -z "$arg" ]; then arg=" $arg"; fi
189        arg="-args \"instance=$instance$arg\""
190        eval up $driver -label $label $arg -period 5HZ
191    done
192    if [ X`/bin/sysenv lwip` = Xyes ]
193    then
194	up lwip -script /etc/rs.inet -dev /dev/ip
195    else
196	up inet -script /etc/rs.inet -dev /dev/ip
197    fi
198
199    # pty needs to know the "tty" group ID
200    up pty -dev /dev/ptmx -args "gid=`stat -f '%g' /dev/ptmx`"
201
202    up uds -dev /dev/uds
203
204    up -n ipc
205
206    up log -dev /dev/klog
207
208    if [ $ARCH = i386 ]
209    then
210	up -n printer -dev /dev/lp -period 10HZ
211	# start VirtualBox time sync driver if the device is there
212	if grep '^[^ ]* [^ ]* 80EE:CAFE[^ ]* ' /proc/pci >/dev/null; then
213		up -n vbox -period 10HZ
214	fi
215    fi
216
217    echo .
218
219    # Network initialization.
220    (: </dev/tcp) 2>/dev/null && net=t	# Is there a TCP/IP server?
221
222    echo -n "Starting daemons:"
223    daemonize update
224
225    # Ugly error message when starting cron from CD.
226    # (and cron unnecessary then so..)
227    if [ ! -f /CD ]
228    then	daemonize cron
229    else	mkdir /tmp/log
230    		rm -f /var/log || true
231		ln -s /tmp/log /var/log || true
232		. /etc/rc.cd
233    fi
234    # syslogd has not been started yet
235    rm -f /var/run/syslogd.pid
236    daemonize syslogd
237    echo .
238
239    # i2c only supported on ARM at the moment
240    if [ $ARCH = earm ]
241    then
242	echo -n "Starting i2c subsystem: "
243	for bus in 1 2 3
244	do
245		test -e /dev/i2c-${bus} || (cd /dev && MAKEDEV i2c-${bus})
246		up i2c -dev /dev/i2c-${bus} -label i2c.${bus} \
247			-args instance=${bus}
248	done
249	echo .
250
251	BOARD_NAME=`sysenv board`
252	case "${BOARD_NAME}" in
253
254		ARM-ARMV7-TI-BB-WHITE)
255			echo "Running on a BeagleBone"
256			echo -n "Starting i2c device drivers: "
257
258			# start EEPROM driver for reading board info
259			test -e /dev/eepromb1s50 || \
260				(cd /dev && MAKEDEV eepromb1s50)
261			up cat24c256 -dev /dev/eepromb1s50 \
262				-label cat24c256.1.50 \
263				-args 'bus=1 address=0x50'
264
265			# Start TPS65217 driver for power management.
266			up tps65217 -label tps65217.1.24 \
267			        -args 'bus=1 address=0x24'
268
269			# check for the presence of a display
270			eepromread -f /dev/i2c-2 -n > /dev/null 2>&1
271			RESULT=$?
272			if [ $RESULT -eq 0 ]
273			then
274				# start eeprom driver for reading EDID.
275				test -e /dev/eepromb2s50 || \
276					(cd /dev && MAKEDEV eepromb2s50)
277				up cat24c256 -dev /dev/eepromb2s50 \
278					-label cat24c256.2.50 \
279					-args 'bus=2 address=0x50'
280
281				# start frame buffer
282				#up fb -dev /dev/fb0 -args edid.0=cat24c256.2.50
283				# fb hasn't been ported to AM335X yet.
284			fi
285
286			if [ -e /service/usbd ]
287			then
288				echo "Starting USBD"
289				up usbd
290			fi
291			# Detect expansion boards and start drivers.
292			capemgr
293
294			;;
295
296		ARM-ARMV7-TI-BB-BLACK)
297			echo "Running on a BeagleBone Black"
298			echo -n "Starting i2c device drivers: "
299
300			# start EEPROM driver for reading board info
301			test -e /dev/eepromb1s50 || \
302				(cd /dev && MAKEDEV eepromb1s50)
303			up cat24c256 -dev /dev/eepromb1s50 \
304				-label cat24c256.1.50 \
305				-args 'bus=1 address=0x50'
306
307			# Start TPS65217 driver for power management.
308			up tps65217 -label tps65217.1.24 \
309			        -args 'bus=1 address=0x24'
310
311			# Start TDA19988 driver for reading EDID.
312			up tda19988 -label tda19988.1.3470 -args \
313				'cec_bus=1 cec_address=0x34 hdmi_bus=1 hdmi_address=0x70'
314
315			# start frame buffer
316			#up fb -dev /dev/fb0 -args edid.0=tda19988.1.3470
317			# fb hasn't been ported to AM335X yet.
318
319			if [ -e /service/usbd ]
320			then
321				echo "Starting USBD"
322				up usbd
323			fi
324			# Detect expansion boards and start drivers.
325			capemgr
326
327			;;
328
329		ARM-ARMV7-TI-BBXM-GENERIC)
330			echo "Running on a BeagleBoard-xM"
331			echo -n "Starting i2c device drivers: "
332
333			# Start TPS65950 driver for power management.
334			up tps65950 -label tps65950.1.48 \
335				-args 'bus=1 address=0x48'
336
337			# Set the system time to the time in the TPS65950's RTC
338			readclock
339
340			# check for the presence of a display
341			eepromread -f /dev/i2c-3 -n > /dev/null 2>&1
342			RESULT=$?
343			if [ $RESULT -eq 0 ]
344			then
345				# start eeprom driver for reading edid
346				test -e /dev/eepromb3s50 || \
347					(cd /dev && MAKEDEV eepromb3s50)
348				up cat24c256 -dev /dev/eepromb3s50 \
349					-label cat24c256.3.50 \
350					-args 'bus=3 address=0x50'
351
352				# start frame buffer
353				up fb -dev /dev/fb0 -args edid.0=cat24c256.3.50
354			fi
355
356			;;
357	esac
358
359	echo .
360    fi
361
362    if [ "$net" ]
363    then
364	if [ -f /etc/rc.net ]
365	then
366	    # Let a customized TCP/IP initialization script figure it out.
367	    . /etc/rc.net
368	else
369	    # Standard network daemons.
370    	    echo -n "Starting networking:"
371	    if grep -s 'psip0.*default' /etc/inet.conf >/dev/null
372	    then	ifconfig -h 10.0.0.1
373	    else
374		    if [ X`/bin/sysenv lwip` = Xyes ]
375		    then
376			dhcpd --lwip &
377			echo -n " dhcpd"
378		    else
379			daemonize dhcpd
380		    fi
381	    fi
382	    daemonize nonamed -L
383	    if [ -f "$DAEMONS" ]
384	    then	. "$DAEMONS"
385	    fi
386	    # The last daemon has been started, so close the list:
387	    echo .
388	fi
389    fi
390
391    if [ "$net" ]
392    then
393	# Get the nodename from the DNS and set it.
394	trap '' 2
395	intr -t 20 hostaddr -h
396	trap 2
397    fi
398
399    # Recover files being edited when the system crashed.
400    test -f /usr/bin/elvprsv && elvprsv /usr/tmp/elv*
401
402    # Run the daily cleanup on systems that are not on at night.
403    test -f /usr/etc/daily && sh /usr/etc/daily boot &
404;;
405stop|down)
406    	# Save random data, if /usr is mounted rw.
407	if grep ' \/usr .*rw.*' /etc/mtab >/dev/null
408	then
409	  if dd if=/dev/random of=$RANDOM_FILE.new bs=1024 count=1 2>/dev/null
410    	  then
411    		mv $RANDOM_FILE.new $RANDOM_FILE
412	  else
413		echo 'Failed to save random data.'
414	  fi
415	fi
416esac
417
418d=
419# Let packages run their own scripts
420for d in /usr/local/etc/rc.d /usr/pkg/etc/rc.d
421do
422if [ -d "$d" -a -z "$bootcd" ]
423then	( if cd $d
424	then
425		echo -n "Local packages ($action): "
426		for f in *
427		do
428			if [ -x "$f" ]
429			then	echo -n "$f "
430				sh "$f" "$action"
431			fi
432		done
433		echo " done."
434	fi
435	)
436fi
437done
438