diff --git a/app/src/main/assets/eth.sh b/app/src/main/assets/eth.sh index 558a174d..dc28073c 100644 --- a/app/src/main/assets/eth.sh +++ b/app/src/main/assets/eth.sh @@ -27,10 +27,11 @@ cleanup() { } trap cleanup INT TERM +# Get script directory for finding tools like ethtool SCRIPT_PATH="$0" -# 确保路径是绝对路径 +# Ensure path is absolute case "$SCRIPT_PATH" in - /*) ;; # 已经是绝对路径 + /*) ;; # Already absolute path *) SCRIPT_PATH="$PWD/$SCRIPT_PATH" ;; esac SCRIPT_DIR=$(dirname "$SCRIPT_PATH") @@ -77,7 +78,7 @@ fi if [ -x "$SCRIPT_DIR/ethtool" ]; then echo "Using ethtool from script directory: $SCRIPT_DIR/ethtool" "$SCRIPT_DIR/ethtool" -s eth0 speed 10 duplex full autoneg off -# 其次尝试使用原路径 +# Try alternative path next elif [ -x "/data/data/com.xypower.mpapp/files/ethtool" ]; then echo "Configuring eth0 to 10Mbps full duplex..." /data/data/com.xypower.mpapp/files/ethtool -s eth0 speed 10 duplex full autoneg off @@ -91,31 +92,90 @@ else fi fi -# Configure IP address +# ==================================================== +# MTK Android 9 IP configuration with loss prevention +# ==================================================== + +# Configure IP address first while interface is DOWN +echo "Setting IP address while interface is DOWN..." /system/bin/ip addr add $ETH_IP/$ETH_NETMASK broadcast $ETH_BROADCAST dev eth0 +PRE_UP_IP=$(/system/bin/ip addr show eth0 | grep -c "inet $ETH_IP") +echo "IP configuration before UP: $PRE_UP_IP (1=configured, 0=missing)" # Enable interface and wait for UP +echo "Bringing up interface..." /system/bin/ip link set eth0 up - -sleep 1 +sleep 0.2 + +# Check if IP was lost after interface UP (common issue on MTK devices) +POST_UP_IP=$(/system/bin/ip addr show eth0 | grep -c "inet $ETH_IP") +echo "IP configuration after UP: $POST_UP_IP (1=retained, 0=lost)" + +# IP address lost detection and recovery +if [ "$PRE_UP_IP" = "1" ] && [ "$POST_UP_IP" = "0" ]; then + echo "Warning: IP address was lost after bringing interface up - MTK issue detected" + echo "Reapplying IP configuration..." + /system/bin/ip addr add $ETH_IP/$ETH_NETMASK broadcast $ETH_BROADCAST dev eth0 + + # Check if reapplied configuration worked + FIXED_IP=$(/system/bin/ip addr show eth0 | grep -c "inet $ETH_IP") + echo "IP reapplication result: $FIXED_IP (1=success, 0=still missing)" + + # If standard method fails, try MTK-specific approaches + if [ "$FIXED_IP" = "0" ]; then + echo "Standard IP configuration failed, trying MTK-specific methods" + + # Try ifconfig if available (works better on some MTK devices) + if command -v ifconfig >/dev/null 2>&1; then + echo "Using ifconfig method..." + ifconfig eth0 $ETH_IP netmask 255.255.255.0 up + sleep 1 + fi + + # Try Android's netd service if available + if [ -x "/system/bin/ndc" ]; then + echo "Using MTK netd service..." + /system/bin/ndc network interface setcfg eth0 $ETH_IP 255.255.255.0 up + sleep 1 + fi + fi +fi # Use loop to wait for interface UP instead of fixed sleep WAITED=0 while [ $WAITED -lt $MAX_UP_WAIT ]; do + # Check both link status and IP configuration IF_STATUS=$(/system/bin/ip link show eth0 | grep -c ",UP") - if [ "$IF_STATUS" = "1" ]; then - echo "Interface is UP after $WAITED seconds" + IP_STATUS=$(/system/bin/ip addr show eth0 | grep -c "inet $ETH_IP") + + if [ "$IF_STATUS" = "1" ] && [ "$IP_STATUS" = "1" ]; then + echo "Interface is UP with correct IP after $WAITED seconds" break fi - echo "Waiting for interface UP... ($WAITED/$MAX_UP_WAIT)" + + echo "Waiting for interface UP with IP... ($WAITED/$MAX_UP_WAIT)" + + # If interface is UP but IP is missing, reapply IP + if [ "$IF_STATUS" = "1" ] && [ "$IP_STATUS" = "0" ]; then + echo "Interface UP but IP missing, reapplying IP..." + /system/bin/ip addr add $ETH_IP/$ETH_NETMASK broadcast $ETH_BROADCAST dev eth0 + fi + sleep 0.5 WAITED=$((WAITED+1)) done -if [ "$IF_STATUS" != "1" ]; then - echo "Warning: Interface did not come UP within expected time, attempting to enable again" >&2 - /system/bin/ip link set eth0 up - sleep 1 +# Final status check +FINAL_IF_STATUS=$(/system/bin/ip link show eth0 | grep -c ",UP") +FINAL_IP_STATUS=$(/system/bin/ip addr show eth0 | grep -c "inet $ETH_IP") + +if [ "$FINAL_IF_STATUS" != "1" ] || [ "$FINAL_IP_STATUS" != "1" ]; then + echo "Warning: Failed to achieve stable interface state with IP" >&2 + echo "Final interface status: $FINAL_IF_STATUS (1=UP, 0=DOWN)" + echo "Final IP status: $FINAL_IP_STATUS (1=configured, 0=missing)" + /system/bin/ip addr show eth0 +else + echo "Successfully configured eth0 with IP $ETH_IP" fi # First add to main routing table