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