10_indicate_preinit 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. # Copyright (C) 2010 Vertical Communications
  4. preinit_ip_config() {
  5. local netdev vid
  6. netdev=${1%\.*}
  7. vid=${1#*\.}
  8. if [ "$vid" = "$netdev" ]; then
  9. vid=
  10. fi
  11. grep -q "$netdev" /proc/net/dev || return
  12. if [ -n "$vid" ]; then
  13. ip link add link $netdev name $1 type vlan id $vid
  14. fi
  15. ip link set dev $netdev up
  16. ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1
  17. }
  18. preinit_config_switch() {
  19. local role roles ports device enable reset
  20. local name=$1
  21. local lan_if=$2
  22. json_select switch
  23. json_select $name
  24. json_get_vars enable reset
  25. if json_is_a roles array; then
  26. json_get_keys roles roles
  27. json_select roles
  28. for role in $roles; do
  29. json_select "$role"
  30. json_get_vars ports device
  31. json_select ..
  32. if [ "$device" = "$lan_if" ]; then
  33. swconfig dev $name set reset $reset
  34. swconfig dev $name set enable_vlan $enable
  35. swconfig dev $name vlan $role set ports "$ports"
  36. swconfig dev $name set apply
  37. fi
  38. done
  39. json_select ..
  40. fi
  41. json_select ..
  42. json_select ..
  43. }
  44. preinit_config_board() {
  45. /bin/board_detect /tmp/board.json
  46. [ -f "/tmp/board.json" ] || return
  47. . /usr/share/libubox/jshn.sh
  48. json_init
  49. json_load "$(cat /tmp/board.json)"
  50. json_select network
  51. json_select "lan"
  52. json_get_vars ifname
  53. json_select ..
  54. json_select ..
  55. [ -n "$ifname" ] || return
  56. # only use the first one
  57. ifname=${ifname%% *}
  58. if [ -x /sbin/swconfig ]; then
  59. # configure the switch, if present
  60. json_get_keys keys switch
  61. for key in $keys; do
  62. preinit_config_switch $key $ifname
  63. done
  64. else
  65. # trim any vlan ids
  66. ifname=${ifname%\.*}
  67. fi
  68. pi_ifname=$ifname
  69. preinit_ip_config $pi_ifname
  70. }
  71. preinit_ip() {
  72. [ "$pi_preinit_no_failsafe" = "y" ] && return
  73. # if the preinit interface isn't specified and ifname is set in
  74. # preinit.arch use that interface
  75. if [ -z "$pi_ifname" ]; then
  76. pi_ifname=$ifname
  77. fi
  78. if [ -n "$pi_ifname" ]; then
  79. preinit_ip_config $pi_ifname
  80. elif [ -d "/etc/board.d/" ]; then
  81. preinit_config_board
  82. fi
  83. preinit_net_echo "Doing Lede Preinit\n"
  84. }
  85. preinit_ip_deconfig() {
  86. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  87. local netdev vid
  88. netdev=${pi_ifname%\.*}
  89. vid=${pi_ifname#*\.}
  90. if [ "$vid" = "$netdev" ]; then
  91. vid=
  92. fi
  93. ip -4 address flush dev $pi_ifname
  94. ip link set dev $netdev down
  95. if [ -n "$vid" ]; then
  96. ip link delete $pi_ifname
  97. fi
  98. }
  99. }
  100. preinit_net_echo() {
  101. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  102. {
  103. [ "$pi_preinit_net_messages" = "y" ] || {
  104. [ "$pi_failsafe_net_message" = "true" ] &&
  105. [ "$pi_preinit_no_failsafe_netmsg" != "y" ]
  106. }
  107. } && netmsg $pi_broadcast "$1"
  108. }
  109. }
  110. preinit_echo() {
  111. preinit_net_echo $1
  112. echo $1
  113. }
  114. pi_indicate_preinit() {
  115. set_state preinit
  116. }
  117. boot_hook_add preinit_main preinit_ip
  118. boot_hook_add preinit_main pi_indicate_preinit