summaryrefslogtreecommitdiff
path: root/package/base-files/files/lib/preinit/10_indicate_preinit
blob: c3ec7bfdf77bc95d4881755a1ec59711da4448a3 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

preinit_ip_config() {
	local netdev vid

	netdev=${1%\.*}
	vid=${1#*\.}

	if [ "$vid" = "$netdev" ]; then
		vid=
	fi

	grep -q "$netdev" /proc/net/dev || return

	if [ -n "$vid" ]; then
		ip link add link $netdev name $1 type vlan id $vid
	fi

	ip link set dev $netdev up
	ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1
}

preinit_config_switch() {
	local role roles ports device enable reset

	local name=$1
	local lan_if=$2

	json_select switch
	json_select $name

	json_get_vars enable reset

	if json_is_a roles array; then
		json_get_keys roles roles
		json_select roles

		for role in $roles; do
			json_select "$role"
			json_get_vars ports device
			json_select ..

			if [ "$device" = "$lan_if" ]; then
				if [ "$reset" -eq "1" ]; then
					swconfig dev $name set reset
				fi

				swconfig dev $name set enable_vlan $enable
				swconfig dev $name vlan $role set ports "$ports"
				swconfig dev $name set apply
			fi
		done

		json_select ..
	fi

	json_select ..
	json_select ..
}

preinit_config_board() {
	/bin/board_detect /tmp/board.json

	[ -f "/tmp/board.json" ] || return

	. /usr/share/libubox/jshn.sh

	json_init
	json_load "$(cat /tmp/board.json)"

	json_select network
		json_select "lan"
			json_get_vars ifname
		json_select ..
	json_select ..

	[ -n "$ifname" ] || return

	# only use the first one
	ifname=${ifname%% *}

	if [ -x /sbin/swconfig ]; then
		# configure the switch, if present

		json_get_keys keys switch
		for key in $keys; do
			preinit_config_switch $key $ifname
		done
	else
		# trim any vlan ids
		ifname=${ifname%\.*}
	fi

	pi_ifname=$ifname

	preinit_ip_config $pi_ifname
}

preinit_ip() {
	[ "$pi_preinit_no_failsafe" = "y" ] && return

	# if the preinit interface isn't specified and ifname is set in
	# preinit.arch use that interface
	if [ -z "$pi_ifname" ]; then
		pi_ifname=$ifname
	fi

	if [ -n "$pi_ifname" ]; then
		preinit_ip_config $pi_ifname
	elif [ -d "/etc/board.d/" ]; then
		preinit_config_board
	fi

	preinit_net_echo "Doing OpenWrt Preinit\n"
}

preinit_ip_deconfig() {
	[ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
		local netdev vid

		netdev=${pi_ifname%\.*}
		vid=${pi_ifname#*\.}

		if [ "$vid" = "$netdev" ]; then
			vid=
		fi

		ip -4 address flush dev $pi_ifname
		ip link set dev $netdev down

		if [ -n "$vid" ]; then
			ip link delete $pi_ifname
		fi
	}
}

preinit_net_echo() {
	[ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
		{
			[ "$pi_preinit_net_messages" = "y" ] || {
				[ "$pi_failsafe_net_message" = "true" ] &&
					[ "$pi_preinit_no_failsafe_netmsg" != "y" ]
			}
		} && netmsg $pi_broadcast "$1"
	}
}

pi_indicate_preinit() {
	set_state preinit
}

boot_hook_add preinit_main preinit_ip
boot_hook_add preinit_main pi_indicate_preinit