xref: /minix/etc/usr/rc (revision e1cdaee1)
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    else
182	# We couldn't find the old state to restart from, so use a binary
183	# file and the current date instead, even if this is less than ideal.
184	cat /bin/sh >> /dev/urandom
185	date >> /dev/urandom
186    fi
187
188    # start network driver instances for all configured ethernet devices
189    for label in $(get_eth_labels); do
190        driver=$(echo $label | sed 's/\(.*\)_.*/\1/')
191        instance=$(echo $label | sed 's/.*_//')
192        eval arg=\$${driver}_arg
193        if [ ! -z "$arg" ]; then arg=" $arg"; fi
194        arg="-args \"instance=$instance$arg\""
195        eval up $driver -label $label $arg -period 5HZ
196    done
197    if [ X`/bin/sysenv lwip` = Xyes ]
198    then
199	up lwip -script /etc/rs.inet -dev /dev/ip
200    else
201	up inet -script /etc/rs.inet -dev /dev/ip
202    fi
203
204    # pty needs to know the "tty" group ID
205    up pty -dev /dev/ptmx -args "gid=`stat -f '%g' /dev/ptmx`"
206
207    up uds -dev /dev/uds
208
209    up -n ipc
210
211    up log -dev /dev/klog
212
213    if [ $ARCH = i386 ]
214    then
215	up -n printer -dev /dev/lp -period 10HZ
216	# start VirtualBox time sync driver if the device is there
217	if grep '^[^ ]* [^ ]* 80EE:CAFE[^ ]* ' /proc/pci >/dev/null; then
218		up -n vbox -period 10HZ
219	fi
220    fi
221
222    echo .
223
224    # Network initialization.
225    (: </dev/tcp) 2>/dev/null && net=t	# Is there a TCP/IP server?
226
227    echo -n "Starting daemons:"
228    daemonize update
229
230    # Ugly error message when starting cron from CD.
231    # (and cron unnecessary then so..)
232    if [ ! -f /CD ]
233    then	daemonize cron
234    else	mkdir /tmp/log
235    		rm -f /var/log || true
236		ln -s /tmp/log /var/log || true
237		. /etc/rc.cd
238    fi
239    # syslogd has not been started yet
240    rm -f /var/run/syslogd.pid
241    daemonize syslogd
242    echo .
243
244    # i2c only supported on ARM at the moment
245    if [ $ARCH = earm ]
246    then
247	echo -n "Starting i2c subsystem: "
248	for bus in 1 2 3
249	do
250		test -e /dev/i2c-${bus} || (cd /dev && MAKEDEV i2c-${bus})
251		up i2c -dev /dev/i2c-${bus} -label i2c.${bus} \
252			-args instance=${bus}
253	done
254	echo .
255
256	BOARD_NAME=`sysenv board`
257	case "${BOARD_NAME}" in
258
259		ARM-ARMV7-TI-BB-WHITE)
260			echo "Running on a BeagleBone"
261			echo -n "Starting i2c device drivers: "
262
263			# start EEPROM driver for reading board info
264			test -e /dev/eepromb1s50 || \
265				(cd /dev && MAKEDEV eepromb1s50)
266			up cat24c256 -dev /dev/eepromb1s50 \
267				-label cat24c256.1.50 \
268				-args 'bus=1 address=0x50'
269
270			# Start TPS65217 driver for power management.
271			up tps65217 -label tps65217.1.24 \
272			        -args 'bus=1 address=0x24'
273
274			# check for the presence of a display
275			eepromread -f /dev/i2c-2 -n > /dev/null 2>&1
276			RESULT=$?
277			if [ $RESULT -eq 0 ]
278			then
279				# start eeprom driver for reading EDID.
280				test -e /dev/eepromb2s50 || \
281					(cd /dev && MAKEDEV eepromb2s50)
282				up cat24c256 -dev /dev/eepromb2s50 \
283					-label cat24c256.2.50 \
284					-args 'bus=2 address=0x50'
285
286				# start frame buffer
287				#up fb -dev /dev/fb0 -args edid.0=cat24c256.2.50
288				# fb hasn't been ported to AM335X yet.
289			fi
290
291			if [ -e /service/usbd ]
292			then
293				echo "Starting USBD"
294				up usbd
295			fi
296			# Detect expansion boards and start drivers.
297			capemgr
298
299			;;
300
301		ARM-ARMV7-TI-BB-BLACK)
302			echo "Running on a BeagleBone Black"
303			echo -n "Starting i2c device drivers: "
304
305			# start EEPROM driver for reading board info
306			test -e /dev/eepromb1s50 || \
307				(cd /dev && MAKEDEV eepromb1s50)
308			up cat24c256 -dev /dev/eepromb1s50 \
309				-label cat24c256.1.50 \
310				-args 'bus=1 address=0x50'
311
312			# Start TPS65217 driver for power management.
313			up tps65217 -label tps65217.1.24 \
314			        -args 'bus=1 address=0x24'
315
316			# Start TDA19988 driver for reading EDID.
317			up tda19988 -label tda19988.1.3470 -args \
318				'cec_bus=1 cec_address=0x34 hdmi_bus=1 hdmi_address=0x70'
319
320			# start frame buffer
321			#up fb -dev /dev/fb0 -args edid.0=tda19988.1.3470
322			# fb hasn't been ported to AM335X yet.
323
324			if [ -e /service/usbd ]
325			then
326				echo "Starting USBD"
327				up usbd
328			fi
329			# Detect expansion boards and start drivers.
330			capemgr
331
332			;;
333
334		ARM-ARMV7-TI-BBXM-GENERIC)
335			echo "Running on a BeagleBoard-xM"
336			echo -n "Starting i2c device drivers: "
337
338			# Start TPS65950 driver for power management.
339			up tps65950 -label tps65950.1.48 \
340				-args 'bus=1 address=0x48'
341
342			# Set the system time to the time in the TPS65950's RTC
343			readclock
344
345			# check for the presence of a display
346			eepromread -f /dev/i2c-3 -n > /dev/null 2>&1
347			RESULT=$?
348			if [ $RESULT -eq 0 ]
349			then
350				# start eeprom driver for reading edid
351				test -e /dev/eepromb3s50 || \
352					(cd /dev && MAKEDEV eepromb3s50)
353				up cat24c256 -dev /dev/eepromb3s50 \
354					-label cat24c256.3.50 \
355					-args 'bus=3 address=0x50'
356
357				# start frame buffer
358				up fb -dev /dev/fb0 -args edid.0=cat24c256.3.50
359			fi
360
361			;;
362	esac
363
364	echo .
365    fi
366
367    if [ "$net" ]
368    then
369	if [ -f /etc/rc.net ]
370	then
371	    # Let a customized TCP/IP initialization script figure it out.
372	    . /etc/rc.net
373	else
374	    # Standard network daemons.
375    	    echo -n "Starting networking:"
376	    if grep -s 'psip0.*default' /etc/inet.conf >/dev/null
377	    then	ifconfig -h 10.0.0.1
378	    else
379		    if [ X`/bin/sysenv lwip` = Xyes ]
380		    then
381			dhcpd --lwip &
382			echo -n " dhcpd"
383		    else
384			daemonize dhcpd
385		    fi
386	    fi
387	    daemonize nonamed -L
388	    if [ -f "$DAEMONS" ]
389	    then	. "$DAEMONS"
390	    fi
391	    # The last daemon has been started, so close the list:
392	    echo .
393	fi
394    fi
395
396    # Now that all device files should be created, see if we need to update the
397    # device file database.  This code is butchered from NetBSD etc/rc.d/sysdb.
398    DEVDIR=/dev
399    DEVDB=/var/run/dev.cdb
400    if [ ! -f "$DEVDB" -o "$DEVDIR" -nt "$DEVDB" ]
401    then
402	dev_mkdb
403    fi
404
405    if [ "$net" ]
406    then
407	# Get the nodename from the DNS and set it.
408	trap '' 2
409	intr -t 20 hostaddr -h
410	trap 2
411    fi
412
413    # Load the stored hostname into the sysctl database.
414    test -r /etc/hostname.file && hostname $(cat /etc/hostname.file)
415
416    # Recover files being edited when the system crashed.
417    test -f /usr/bin/elvprsv && elvprsv /usr/tmp/elv*
418
419    # Run the daily cleanup on systems that are not on at night.
420    test -f /usr/etc/daily && sh /usr/etc/daily boot &
421;;
422stop|down)
423    	# Save random data, if /usr is mounted rw.
424	if grep ' \/usr .*rw.*' /etc/mtab >/dev/null
425	then
426	  if dd if=/dev/random of=$RANDOM_FILE.new bs=1024 count=1 2>/dev/null
427    	  then
428    		mv $RANDOM_FILE.new $RANDOM_FILE
429	  else
430		echo 'Failed to save random data.'
431	  fi
432	fi
433esac
434
435d=
436# Let packages run their own scripts
437for d in /usr/local/etc/rc.d /usr/pkg/etc/rc.d
438do
439if [ -d "$d" -a -z "$bootcd" ]
440then	( if cd $d
441	then
442		echo -n "Local packages ($action): "
443		for f in *
444		do
445			if [ -x "$f" ]
446			then	echo -n "$f "
447				sh "$f" "$action"
448			fi
449		done
450		echo " done."
451	fi
452	)
453fi
454done
455