1#!/bin/sh
2# Copyright 2018 Intel Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#	http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16SOFS_MNT=/var/sofs
17SOFS_MACDIR=${SOFS_MNT}/factory-settings/network/mac
18
19read_hw_mac() {
20	local iface="$1"
21	cat /sys/class/net/"$iface"/address 2>/dev/null
22}
23
24set_hw_mac() {
25	local iface="$1"
26	local mac="$2"
27	ip link show dev "$iface" | grep -q "${iface}:.*\<UP\>" 2>/dev/null
28	local up=$?
29	[[ $up -eq 0 ]] && ip link set dev "$iface" down
30	ip link set dev "$iface" address "$mac"
31	[[ $up -eq 0 ]] && ip link set dev "$iface" up
32}
33
34read_sofs_mac() {
35	local iface="$1"
36	cat "${SOFS_MACDIR}/${iface}" 2>/dev/null
37}
38
39read_fw_env_mac() {
40	local envname="$1"
41	fw_printenv "$envname" 2>/dev/null | sed "s/^$envname=//"
42}
43
44set_fw_env_mac() {
45	local envname="$1"
46	local mac="$2"
47	fw_setenv "$envname" "$mac"
48}
49read_eeprom_mac() {
50	local iface="$1"
51	a=0
52	if [ $iface = "eth1" ];then
53		a=8
54	fi
55	for  ((i=a;i <= ((7+a));i++));do
56		i2cset -y 1 0x50 0x00 $i
57		tmp=`i2cget -y 1 0x50`
58		local mac[$i]=${tmp:2}
59		local mac_str=$mac_str:${mac[$i]}
60	done
61	mac_str=${mac_str:1:17}
62	echo $mac_str 2>/dev/null
63}
64
65create_macdir() {
66if [ -a ${SOFS_MACDIR} ]; then
67	if [ ! -d ${SOFS_MACDIR} ]; then
68		rm -rf ${SOFS_MACDIR}
69		mkdir -p ${SOFS_MACDIR}
70	fi
71else
72	mkdir -p ${SOFS_MACDIR}
73fi
74return 0
75}
76
77mac_check() {
78	local iface="$1"
79	local envname="$2"
80
81	# Read the MAC address in use by the NIC
82	local hw_mac=$(read_hw_mac "$iface")
83
84	# Read the MAC address stored in the non-volatile file provisioned in
85	# manufacturing.
86	local sofs_mac=$(read_sofs_mac "$iface")
87
88	local eeprom_mac=$(read_eeprom_mac "$iface")
89	if [ -n "$eeprom_mac" ] && [ -z "$sofs_mac"]; then
90		set_hw_mac "$iface" "$eeprom_mac"
91		set_fw_env_mac "$envname" "$eeprom_mac"
92		return $?
93	elif [ -n "$sofs_mac" ] && [ "$hw_mac" != "$sofs_mac" ]; then
94		# A factory assigned address was found, and it is newly assigned.
95		# Update the active interface and save the new value to the u-boot
96		# environment.
97		set_hw_mac "$iface" "$sofs_mac"
98		set_fw_env_mac "$envname" "$sofs_mac"
99		return $?
100	elif [ -n "$hw_mac" ]; then
101		# Read the MAC address stored by U-Boot
102		local fw_env_mac=$(read_fw_env_mac "$envname")
103		if [ -z "$fw_env_mac" ] || [ "$fw_env_mac" != "$hw_mac" ]; then
104			set_fw_env_mac "$envname" "$hw_mac"
105			return $?
106		fi
107	else
108	    # Could not identify a MAC address
109	    return 255
110	fi
111
112	return 0
113}
114
115create_macdir
116
117error=0
118first_error_seen=0
119
120while read IFACE UBDEV; do
121	mac_check "$IFACE" "$UBDEV"
122	error=$?
123	if [ $error -ne 0 ] && [ $first_error_seen -eq 0 ]; then
124		first_error_seen=$error
125	fi
126done <<-END_CONF
127	eth0 eth1addr
128	eth1 ethaddr
129END_CONF
130exit $first_error_seen
131