xref: /minix/etc/usr/rc (revision 9f81acbc)
1# /usr/etc/rc - continued system initialization.
2
3RANDOM_FILE=/usr/adm/random.dat
4TCPISN_FILE=/usr/adm/tcpisn.dat
5LOCAL_FILE=/usr/etc/rc.local
6
7ARCH="`sysenv arch`"
8
9if [ ! "$ARCH" ]
10then    # Older kernels do not provide an arch sysenv variable.
11        # We assume we are on x86 then, as existing systems with
12        # kernel and userland (i.e. this script) unsynchronized
13        # will be x86.
14        ARCH=i386
15fi
16
17# Get $SERVICES_DIRS
18. /etc/rc.conf
19
20# Directories to find services in
21if [ ! "$SERVICES_DIRS" ]
22then	SERVICES_DIRS=/service
23fi
24
25# Booting from cd?
26bootcd="`/bin/sysenv bootcd`"
27
28case "$#:$1" in
291:start|1:stop)
30    action=$1
31    ;;
32*)  echo >&2 "Usage: $0 [start|stop]"
33    exit 1
34esac
35
36if [ -f "$LOCAL_FILE" ]
37then	. "$LOCAL_FILE" $1
38fi
39
40disabled()
41{
42    ifs="$IFS"; IFS=,
43    for skip in `sysenv disable`
44    do
45        if [ "$skip" = "$1" ]
46	then
47                IFS="$ifs"; unset ifs
48		return 0
49	fi
50    done
51    IFS="$ifs"; unset ifs
52    return 1
53}
54
55daemonize()
56{
57    # Function to start a daemon, if it exists.
58    local IFS=':'
59    local name="$1"
60
61    for dir in $PATH
62    do
63	if [ -f "$dir/$1" ]
64	then
65
66            # check if this service is disabled at the boot monitor.
67            if disabled $name; then return; fi
68
69	    echo -n " $name"
70	    "$@" &
71	    return
72	fi
73    done
74}
75
76up()
77{
78    # Function to dynamically start a system service
79    opt=""
80    prefix=$(expr "$1 " : '\(-\)')
81    if [ "$prefix" = "-" ];
82    then
83         opt=$1
84         shift
85    fi
86    service=$1
87    shift
88
89    # First check if this service is disabled at the boot monitor.
90    if disabled $service; then return; fi
91
92    # Service is not disabled. Try to bring it up.
93    found=""
94    for dir in $SERVICES_DIRS
95    do	bin=$dir/$service
96	if [ -x $bin -a -z "$found" ]
97	then	minix-service $opt up $bin "$@"
98    		echo -n " $service"
99		found=yes
100	fi
101    done
102    if [ -z "$found" ]
103    then	echo " ($service not found in $SERVICES_DIRS)"
104    fi
105}
106
107# Print a list of labels of detected PCI ethernet hardware devices.
108get_pci_eth_labels()
109{
110	# We need to match all PCI ethernet hardware devices against all
111	# drivers.  For performance reasons, we construct a lookup table on the
112	# fly here.  In order to do that, we need to give both a list of all
113	# available network drivers with PCI device IDs (part 1 of the code
114	# below) and a list of all actually present ethernet hardware devices
115	# (part 2) to an awk script (part 3).  The awk script can tell the
116	# difference between the list entries based on whether there is a
117	# leading space on the line.  For part 2, we grab only devices that are
118	# in PCI class 2 (network controller) subclass 0 (ethernet controller).
119
120	# Part 1: collect all network drivers with supported PCI IDs
121	(for dir in $SYSTEM_CONF_DIRS; do
122		for f in $dir/$SYSTEM_CONF_SUBDIR/*; do
123			if [ -f $f ]; then
124				printconfig $f | grep ',type net.*,pci device'
125			fi
126		done
127	done | sed 's/^service \([^,]*\),.*,pci device/ \1/g';
128	# Part 2: grab all PCI IDs of ethernet hardware devices (class 2/0)
129	cat /proc/pci | grep '^[^ ]* 2/0/' | cut -d' ' -f3) | awk '
130	# Part 3: first construct a PCI-ID-to-driver table based on the lines
131	# produced by part 1 (with leading space), which each contain one
132	# driver name followed by one or more PCI IDs; then, go through all
133	# the ethernet hardware devices found in part 2 (no leading space) and
134	# if if there is a hit in the table, print the driver label to use.
135	/^ / { for (i=2;i<=NF;i++) drivers[$(i)]=$1 }
136	/^[^ ]/ {
137		# There is a bit of a discrepancy between output formats of
138		# /proc/pci and printconfig: the former uses
139		# "vid:did:sub_vid:sub_did" whereas the latter uses
140		# "vid:did/sub_vid:sub_did".  No problem; in the common case
141		# (= no sub IDs used) we need to split the PCI ID anyway.
142		if (split($1,id,":") >= 4) {
143			# Try a full "vid:did:sub_vid:sub_did" match.
144			name=drivers[id[1]":"id[2]"/"id[3]":"id[4]]
145		}
146		# No sub IDs or no match found?  Try a "vid:did" match.
147		if (!name) name=drivers[id[1]":"id[2]]
148		# If found, print the resulting label (<name>_<instance>)
149		if (name) {
150			print name"_"(instance[name]+0)
151			instance[name]++
152		}
153	}
154	'
155}
156
157# Print a list of labels of ethernet hardware devices that have been detected
158# to be present on the system.  Each label has the format '<driver>_<instance>'
159# where <driver> is the file name of the driver (in /service) and <instance> is
160# a zero-based, per-driver instance number of the device.
161get_eth_labels() {
162	# For now, do autodetection only on platforms with (x86) PCI support.
163	# For (x86) ISA drivers, a custom network setting script is required;
164	# see below.  For ARM platforms, the driver (if any) is started based
165	# on the board; there is no device autodetection.
166	if [ -f /proc/pci ]; then
167		get_pci_eth_labels
168	fi
169
170	# Add any network drivers manually configured in /usr/etc/rc.local by
171	# the netconf(8) utility.
172	if [ -n "$netdriver" ]; then
173		echo "${netdriver}_0"
174	fi
175}
176
177# Detect expansion boards on the BeagleBone and load the proper drivers.
178capemgr() {
179
180    # Probe each possible cape EEPROM slave address for a BeagleBone cape.
181    for slave_addr in 54 55 56 57
182    do
183
184        # See if there is a readable EEPROM with address ${slave_addr}.
185        eepromread -f /dev/i2c-3 -a 0x${slave_addr} > /dev/null 2>&1
186        RESULT=$?
187	if [ $RESULT -eq 0 ]
188	then
189
190	    # Found an alive EEPROM. Try reading the cape name.
191            CAPE=`eepromread -i -f /dev/i2c-3 -a 0x${slave_addr} | \
192	        sed -n 's/^PART_NUMBER     : \(.*\)$/\1/p' | \
193		sed -e 's/\.*$//g'` # Strip trailing periods.
194
195	    # Look for a cape specific RC script.
196            if [ -x /etc/rc.capes/${CAPE} ]
197	    then
198
199	        # CAT24C256 EEPROM -- all capes have this chip.
200		test -e /dev/eepromb3s${slave_addr} || \
201		    (cd /dev && MAKEDEV eepromb3s${slave_addr})
202		up cat24c256 -dev /dev/eepromb3s${slave_addr} \
203		    -label cat24c256.3.${slave_addr} \
204		    -args "bus=3 address=0x${slave_addr}"
205
206                # Load the drivers for the cape and do any other configuration.
207		. "/etc/rc.capes/${CAPE}"
208
209	    else
210
211		echo ""
212	        echo "** UNSUPPORTED CAPE: ${CAPE}"
213		echo ""
214
215	    fi
216	fi
217    done
218}
219
220case $action in
221start)
222    # Select console font.
223    test -f /etc/font && loadfont /etc/font </dev/console
224
225    # Cleanup.
226    rm -rf /tmp/* /usr/run/* /usr/spool/lpd/* /usr/spool/locks/*
227
228    # Start servers and drivers set at the boot monitor.
229    echo -n "Starting services:"
230    up -n random -dev /dev/random -period 3HZ
231
232    # load random number generator
233    if [ -f $RANDOM_FILE ]
234    then
235    	cat < $RANDOM_FILE >/dev/random
236    	# overwrite $RANDOM_FILE. We don't want to use this data again
237    	dd if=/dev/random of=$RANDOM_FILE bs=1024 count=1 2> /dev/null
238    else
239	# We couldn't find the old state to restart from, so use a binary
240	# file and the current date instead, even if this is less than ideal.
241	cat /bin/sh >> /dev/urandom
242	date >> /dev/urandom
243    fi
244
245    # start network driver instances for all detected ethernet devices
246    for label in $(get_eth_labels); do
247        driver=$(echo $label | sed 's/\(.*\)_.*/\1/')
248        instance=$(echo $label | sed 's/.*_//')
249        eval arg=\$${driver}_arg
250        if [ ! -z "$arg" ]; then arg=" $arg"; fi
251        arg="-args \"instance=$instance$arg\""
252        eval up $driver -label $label $arg -period 5HZ
253    done
254
255    # pty needs to know the "tty" group ID
256    up pty -dev /dev/ptmx -args "gid=`stat -f '%g' /dev/ptmx`"
257
258    # Start the LWIP service.
259    up lwip -dev /dev/bpf -script /etc/rs.lwip
260
261    # Load stable seed for TCP Initial Sequence Number generation (RFC 6528).
262    # The idea here is that (especially) after a system crash, the seed stays
263    # the same, so as to make it unlikely that incoming packets for connections
264    # from before the crash are accepted on connections after the crash.
265    TCPISN_LEN=$(sysctl -n net.inet.tcp.isn_secret | awk '{print length/2}')
266    if [ ! -f $TCPISN_FILE ]; then
267	# If the /usr file system is read-only, we cannot create the file.  In
268	# that case, we draw a temporary secret from the random service.
269	if grep ' \/usr .*rw.*' /etc/mtab >/dev/null; then
270	    dd if=/dev/random of=$TCPISN_FILE bs=$TCPISN_LEN count=1 2>/dev/null
271	else
272	    TCPISN_FILE=/dev/random
273	fi
274    fi
275    sysctl -qw net.inet.tcp.isn_secret=`dd if=$TCPISN_FILE bs=$TCPISN_LEN \
276	count=1 2>/dev/null | hexdump -v -e '/1 "%02x"'` 2>/dev/null
277
278    # LWIP does not block until all network drivers have fully initialized and
279    # reported back to LWIP.  That may prevent proper configuration of the
280    # corresponding interfaces a bit later.  Sleep up to five seconds waiting
281    # for all registered network drivers to initialize and report to LWIP.
282    for i in 1 2 3 4 5; do
283	[ $(sysctl -n minix.lwip.drivers.pending) -gt 0 ] && sleep 1
284    done
285
286    up uds
287
288    up -n ipc
289
290    up log -dev /dev/klog
291
292    if [ $ARCH = i386 ]
293    then
294	up -n printer -dev /dev/lp -period 10HZ
295	# start VirtualBox time sync driver if the device is there
296	if grep '^[^ ]* [^ ]* 80EE:CAFE[^ ]* ' /proc/pci >/dev/null; then
297		up -n vbox -period 10HZ
298	fi
299    fi
300
301    echo .
302
303    echo -n "Starting daemons:"
304    daemonize update
305
306    # Ugly error message when starting cron from CD.
307    # (and cron unnecessary then so..)
308    if [ ! -f /CD ]
309    then	daemonize cron
310    else	mkdir /tmp/log
311    		rm -f /var/log || true
312		ln -s /tmp/log /var/log || true
313		. /etc/rc.cd
314    fi
315
316    echo .
317
318    # i2c only supported on ARM at the moment
319    if [ $ARCH = earm ]
320    then
321	echo -n "Starting i2c subsystem: "
322	for bus in 1 2 3
323	do
324		test -e /dev/i2c-${bus} || (cd /dev && MAKEDEV i2c-${bus})
325		up i2c -dev /dev/i2c-${bus} -label i2c.${bus} \
326			-args instance=${bus}
327	done
328	echo .
329
330	BOARD_NAME=`sysenv board`
331	case "${BOARD_NAME}" in
332
333		ARM-ARMV7-TI-BB-WHITE)
334			echo "Running on a BeagleBone"
335			echo -n "Starting i2c device drivers: "
336
337			# start EEPROM driver for reading board info
338			test -e /dev/eepromb1s50 || \
339				(cd /dev && MAKEDEV eepromb1s50)
340			up cat24c256 -dev /dev/eepromb1s50 \
341				-label cat24c256.1.50 \
342				-args 'bus=1 address=0x50'
343
344			# Start TPS65217 driver for power management.
345			up tps65217 -label tps65217.1.24 \
346			        -args 'bus=1 address=0x24'
347
348			# Start ethernet driver.
349			up lan8710a -label lan8710a_0 -args 'instance=0'
350
351			# check for the presence of a display
352			eepromread -f /dev/i2c-2 -n > /dev/null 2>&1
353			RESULT=$?
354			if [ $RESULT -eq 0 ]
355			then
356				# start eeprom driver for reading EDID.
357				test -e /dev/eepromb2s50 || \
358					(cd /dev && MAKEDEV eepromb2s50)
359				up cat24c256 -dev /dev/eepromb2s50 \
360					-label cat24c256.2.50 \
361					-args 'bus=2 address=0x50'
362
363				# start frame buffer
364				#up fb -dev /dev/fb0 -args edid.0=cat24c256.2.50
365				# fb hasn't been ported to AM335X yet.
366			fi
367
368			if [ -e /service/usbd ]
369			then
370				echo "Starting USBD"
371				up usbd
372			fi
373			# Detect expansion boards and start drivers.
374			capemgr
375
376			;;
377
378		ARM-ARMV7-TI-BB-BLACK)
379			echo "Running on a BeagleBone Black"
380			echo -n "Starting i2c device drivers: "
381
382			# start EEPROM driver for reading board info
383			test -e /dev/eepromb1s50 || \
384				(cd /dev && MAKEDEV eepromb1s50)
385			up cat24c256 -dev /dev/eepromb1s50 \
386				-label cat24c256.1.50 \
387				-args 'bus=1 address=0x50'
388
389			# Start TPS65217 driver for power management.
390			up tps65217 -label tps65217.1.24 \
391			        -args 'bus=1 address=0x24'
392
393			# Start TDA19988 driver for reading EDID.
394			up tda19988 -label tda19988.1.3470 -args \
395				'cec_bus=1 cec_address=0x34 hdmi_bus=1 hdmi_address=0x70'
396
397			# Start ethernet driver.
398			up lan8710a -label lan8710a_0 -args 'instance=0'
399
400			# start frame buffer
401			#up fb -dev /dev/fb0 -args edid.0=tda19988.1.3470
402			# fb hasn't been ported to AM335X yet.
403
404			if [ -e /service/usbd ]
405			then
406				echo "Starting USBD"
407				up usbd
408			fi
409			# Detect expansion boards and start drivers.
410			capemgr
411
412			;;
413
414		ARM-ARMV7-TI-BBXM-GENERIC)
415			echo "Running on a BeagleBoard-xM"
416			echo -n "Starting i2c device drivers: "
417
418			# Start TPS65950 driver for power management.
419			up tps65950 -label tps65950.1.48 \
420				-args 'bus=1 address=0x48'
421
422			# Set the system time to the time in the TPS65950's RTC
423			readclock
424
425			# check for the presence of a display
426			eepromread -f /dev/i2c-3 -n > /dev/null 2>&1
427			RESULT=$?
428			if [ $RESULT -eq 0 ]
429			then
430				# start eeprom driver for reading edid
431				test -e /dev/eepromb3s50 || \
432					(cd /dev && MAKEDEV eepromb3s50)
433				up cat24c256 -dev /dev/eepromb3s50 \
434					-label cat24c256.3.50 \
435					-args 'bus=3 address=0x50'
436
437				# start frame buffer
438				up fb -dev /dev/fb0 -args edid.0=cat24c256.3.50
439			fi
440
441			;;
442	esac
443
444	echo .
445    fi
446
447    # Load the stored hostname into the sysctl database.
448    test -r /etc/hostname.file && hostname $(cat /etc/hostname.file)
449
450    # Recover files being edited when the system crashed.
451    test -f /usr/bin/elvprsv && elvprsv /usr/tmp/elv*
452
453    # Run the daily cleanup on systems that are not on at night.
454    test -f /usr/etc/daily && sh /usr/etc/daily boot &
455;;
456stop)
457    	# Save random data, if /usr is mounted rw.
458	if grep ' \/usr .*rw.*' /etc/mtab >/dev/null
459	then
460	  if dd if=/dev/random of=$RANDOM_FILE.new bs=1024 count=1 2>/dev/null
461    	  then
462    		mv $RANDOM_FILE.new $RANDOM_FILE
463	  else
464		echo 'Failed to save random data.'
465	  fi
466	fi
467esac
468