summaryrefslogtreecommitdiff
path: root/scripts/functions
blob: 73b773affb76857fdde13f03ce9eb51eb7122f6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
panic()
{
	echo $@
	FS1='(initramfs) ' exec /bin/sh
}

render()
{
	eval "echo -n \${$@}"
}

set_initlist()
{
	unset initlist
	for si_x in ${initdir}/*; do
		if [ ! -x ${si_x} ]; then
			continue
		fi
		initlist="${initlist} $(basename ${si_x})"
	done
}

reduce_satisfied()
{
	deplist="$(render array_${1})"
	for rs_x in ${runlist}; do
		pop_list_item ${rs_x} ${deplist}
		deplist=${tmppop}
	done
	eval array_${1}=\"${deplist}\"
}

get_prereqs()
{
	set_initlist
	for gp_x in ${initlist}; do
		tmp=$(${initdir}/${gp_x} prereqs)
		eval array_${gp_x}=\"${tmp}\"
	done
}

count_unsatisfied()
{
	set - ${@}
	return ${#}
}

# Removes $1 from initlist
pop_list_item()
{
	item=${1}
	shift
	set - ${@}
	unset tmppop
	# Iterate
	for pop in ${@}; do
		if [ ${pop} = ${item} ]; then
			continue
		fi
		tmppop="${tmppop} ${pop}"
	done

}

# This function generates the runlist, so we clear it first.
reduce_prereqs()
{
	unset runlist
	set_initlist
	set - ${initlist}
	i=$#
	# Loop until there's no more in the queue to loop through
	while [ ${i} -ne 0 ]; do
		oldi=${i}
		for rp_x in ${initlist}; do
			reduce_satisfied ${rp_x}
			count_unsatisfied $(render array_${rp_x})
			cnt=${?}
			if [ ${cnt} -eq 0 ]; then
				runlist="${runlist} ${rp_x}"
				pop_list_item ${rp_x} ${initlist}
				initlist=${tmppop}
				i=$((${i} - 1))
			fi
		done
		if [ ${i} -eq ${oldi} ]; then
			echo "PANIC: Circular dependancy.  Exiting." >&2
			exit 1
		fi
	done
}

call_scripts()
{
	echo ${runlist}
	for cs_x in ${runlist}; do
		${initdir}/${cs_x}
	done
}

run_scripts()
{
	initdir=${1}
	get_prereqs
	reduce_prereqs
	call_scripts
}

load_modules()
{
	depmod

	for x in /sys/bus/pci/devices/*; do
		if [ -e ${x}/modalias ]; then
			modprobe -q $(cat ${x}/modalias)
		fi
	done

	# Give the USB bus a moment to catch up
	sleep 2

	for x in /sys/bus/usb/devices/*; do
		if [ -e ${x}/modalias ]; then
			modprobe -q $(cat ${x}/modalias)
		fi
	done

	# Load the modules
	# FIXME - do module options here
	if [ -e /conf/modules ]; then
		for x in $(cat /conf/modules); do
			modprobe -v $x
		done
	fi
}