10_indicate_preinit 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if [ "$reset" -eq "1" ]; then
  34. swconfig dev $name set reset
  35. fi
  36. swconfig dev $name set enable_vlan $enable
  37. swconfig dev $name vlan $role set ports "$ports"
  38. swconfig dev $name set apply
  39. fi
  40. done
  41. json_select ..
  42. fi
  43. json_select ..
  44. json_select ..
  45. }
  46. preinit_config_board() {
  47. /bin/board_detect /tmp/board.json
  48. [ -f "/tmp/board.json" ] || return
  49. . /usr/share/libubox/jshn.sh
  50. json_init
  51. json_load "$(cat /tmp/board.json)"
  52. json_select network
  53. json_select "lan"
  54. json_get_vars ifname
  55. json_select ..
  56. json_select ..
  57. [ -n "$ifname" ] || return
  58. # only use the first one
  59. ifname=${ifname%% *}
  60. if [ -x /sbin/swconfig ]; then
  61. # configure the switch, if present
  62. json_get_keys keys switch
  63. for key in $keys; do
  64. preinit_config_switch $key $ifname
  65. done
  66. else
  67. # trim any vlan ids
  68. ifname=${ifname%\.*}
  69. fi
  70. pi_ifname=$ifname
  71. preinit_ip_config $pi_ifname
  72. }
  73. preinit_ip() {
  74. [ "$pi_preinit_no_failsafe" = "y" ] && return
  75. # if the preinit interface isn't specified and ifname is set in
  76. # preinit.arch use that interface
  77. if [ -z "$pi_ifname" ]; then
  78. pi_ifname=$ifname
  79. fi
  80. if [ -n "$pi_ifname" ]; then
  81. preinit_ip_config $pi_ifname
  82. elif [ -d "/etc/board.d/" ]; then
  83. preinit_config_board
  84. fi
  85. preinit_net_echo "Doing libreCMC Preinit\n"
  86. }
  87. preinit_ip_deconfig() {
  88. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  89. local netdev vid
  90. netdev=${pi_ifname%\.*}
  91. vid=${pi_ifname#*\.}
  92. if [ "$vid" = "$netdev" ]; then
  93. vid=
  94. fi
  95. ip -4 address flush dev $pi_ifname
  96. ip link set dev $netdev down
  97. if [ -n "$vid" ]; then
  98. ip link delete $pi_ifname
  99. fi
  100. }
  101. }
  102. preinit_net_echo() {
  103. [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
  104. {
  105. [ "$pi_preinit_net_messages" = "y" ] || {
  106. [ "$pi_failsafe_net_message" = "true" ] &&
  107. [ "$pi_preinit_no_failsafe_netmsg" != "y" ]
  108. }
  109. } && netmsg $pi_broadcast "$1"
  110. }
  111. }
  112. pi_indicate_preinit() {
  113. set_state preinit
  114. }
  115. boot_hook_add preinit_main preinit_ip
  116. boot_hook_add preinit_main pi_indicate_preinit