#!/bin/sh
#
# Copyright (C) 2009 OpenWrt.org
#

. /lib/ar71xx.sh
. /lib/functions.sh            
. /lib/functions/system.sh

fetch_mac_from_mtd() {
        local mtd_part=$1
        local lan_env=$2
        local wan_env=$3
        local mtd mac

        mtd=$(grep $mtd_part /proc/mtd | cut -d: -f1)
        [ -z $mtd ] && return

        mac=$(grep $lan_env /dev/$mtd | cut -d= -f2)
        [ ! -z $mac ] && ifconfig eth0 hw ether $mac 2>/dev/null

        mac=$(grep $wan_env /dev/$mtd | cut -d= -f2)
        [ ! -z $mac ] && ifconfig eth1 hw ether $mac 2>/dev/null
}

fetch_mac_from_uboot() {
    tmpmac=`fw_printenv $1 2>&1`
    case $tmpmac in
    *Error*defined*)
        ;;
    *)
        echo $tmpmac
        ;;
    esac
}

wp_gen_mac_addr() {
    local addr_offset=$1
    local bapt_name=$2
    local bapt_mac=`fetch_mac_from_uboot $bapt_name | awk -F"=" '{print $2}'`

    if [ -z $bapt_mac ]; then
        local uboot_mac=`fetch_mac_from_uboot ethaddr | awk -F"=" '{print $2}'`
        uboot_mac=`macaddr_canonicalize $uboot_mac`
        local mac=`macaddr_add $uboot_mac $addr_offset`
        local ret=0
    else
        local mac=`macaddr_canonicalize $bapt_mac`
        local ret=1
    fi  

    echo $mac
    return $ret
}

wp_set_mac_addr() {
    local interface=$1
    local addr_offset=$2
    local bapt_name=$3

    mac=`wp_gen_mac_addr $addr_offset $bapt_name`
    local ret=$?
    ifconfig $interface hw ether $mac 2>/dev/null
    return $ret
}

wp_check_mac_addresses() {
    eth0mac=`mtd_get_mac_binary art 0`      #0x0000
    eth1mac=`mtd_get_mac_binary art 6`      #0x0006
    wlan0mac=`mtd_get_mac_binary art 20486` #0x5006 #5 GHz section
    wlan1mac=`mtd_get_mac_binary art 4098`  #0x1002 #2.4 GHz section

    if [ "$eth0mac" == "00:c0:02:11:55:99" \
        -a "$eth1mac" == "00:c0:02:11:55:88" \
        -a "$wlan0mac" == "00:03:07:12:34:56" \
        -a "$wlan1mac" == "00:c0:02:11:55:88" ]; then
        return 1
    fi
    return 0
}

wp_set_wifi_mac_fixup() {
    local phy=$1
    local addr_offset=$2
    local bapt_name=$3
    uci_toggle_state wireless $phy "" phy
    uci_set_state wireless $phy macaddr_fixup "$(wp_gen_mac_addr $addr_offset $bapt_name)"
}

preinit_set_mac_address() {
        case $(ar71xx_board_name) in
                tew-632brp)
                        fetch_mac_from_mtd config lan_mac wan_mac
                        ;;
                dir-615-c1)
                        fetch_mac_from_mtd config lan_mac wan_mac
                        echo 1 > /sys/class/leds/dir-615-c1:green:wancpu/brightness
                        ;;
                dir-615-i1)
                        fetch_mac_from_mtd nvram sys_lan_mac sys_wan_mac
                        ;;
                r6100)
                        mac_lan=$(mtd_get_mac_binary caldata 0)
                        [ -n "$mac_lan" ] && ifconfig eth1 hw ether "$mac_lan"
                        mac_wan=$(mtd_get_mac_binary caldata 6)
                        [ -n "$mac_wan" ] && ifconfig eth0 hw ether "$mac_wan"
                        ;;
                wrt160nl)
                        fetch_mac_from_mtd nvram lan_hwaddr wan_hwaddr
                        ;;
                dvl-1200e|\
                dvl-1750e)
                        wp_check_mac_addresses
                        if [ $? -eq 1 ]; then
                            wp_set_mac_addr eth0 2 "MacAddress0"
                            wp_set_mac_addr eth1 3 "MacAddress1"

                            wp_set_wifi_mac_fixup phy0 1 "WiFiMacAddress1"
                            wp_set_wifi_mac_fixup phy1 0 "WiFiMacAddress0"
                        fi
                        ;;
                dvl-1200i|\
                dvl-1750i|\
                dvl-1750c|\
                dvl-1750x)
                        wp_check_mac_addresses
                        if [ $? -eq 1 ]; then
                            wp_set_mac_addr eth0 2 "MacAddress0"

                            wp_set_wifi_mac_fixup phy0 1 "WiFiMacAddress1"
                            wp_set_wifi_mac_fixup phy1 0 "WiFiMacAddress0"
                        fi
                        ;;
        esac
}

boot_hook_add preinit_main preinit_set_mac_address