blob: 55ccb2b1a07bcc783ba668b5773b6f8905667dde [file] [log] [blame]
Patrick Williams2ceeb112023-04-14 08:45:22 -05001#!/bin/bash
Lotus Xu58c48862020-10-27 15:00:31 +08002# 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
Patrick Williams2ceeb112023-04-14 08:45:22 -050052 if [ "$iface" = "eth1" ];then
Lotus Xu58c48862020-10-27 15:00:31 +080053 a=8
54 fi
Patrick Williams2ceeb112023-04-14 08:45:22 -050055 for ((i=a;i <= (7+a);i++));do
Lotus Xu58c48862020-10-27 15:00:31 +080056 i2cset -y 1 0x50 0x00 $i
Patrick Williams2ceeb112023-04-14 08:45:22 -050057 tmp=$(i2cget -y 1 0x50)
58 local mac[i]=${tmp:2}
Lotus Xu58c48862020-10-27 15:00:31 +080059 local mac_str=$mac_str:${mac[$i]}
60 done
61 mac_str=${mac_str:1:17}
Patrick Williams2ceeb112023-04-14 08:45:22 -050062 echo "$mac_str" 2>/dev/null
Lotus Xu58c48862020-10-27 15:00:31 +080063}
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
Patrick Williams2ceeb112023-04-14 08:45:22 -050082 local hw_mac
83 hw_mac=$(read_hw_mac "$iface")
Lotus Xu58c48862020-10-27 15:00:31 +080084
85 # Read the MAC address stored in the non-volatile file provisioned in
86 # manufacturing.
Patrick Williams2ceeb112023-04-14 08:45:22 -050087 local sofs_mac
88 sofs_mac=$(read_sofs_mac "$iface")
Lotus Xu58c48862020-10-27 15:00:31 +080089
Patrick Williams2ceeb112023-04-14 08:45:22 -050090 local eeprom_mac
91 eeprom_mac=$(read_eeprom_mac "$iface")
92 if [ -n "$eeprom_mac" ] && [ -z "$sofs_mac" ]; then
Lotus Xu58c48862020-10-27 15:00:31 +080093 set_hw_mac "$iface" "$eeprom_mac"
94 set_fw_env_mac "$envname" "$eeprom_mac"
95 return $?
96 elif [ -n "$sofs_mac" ] && [ "$hw_mac" != "$sofs_mac" ]; then
97 # A factory assigned address was found, and it is newly assigned.
98 # Update the active interface and save the new value to the u-boot
99 # environment.
100 set_hw_mac "$iface" "$sofs_mac"
101 set_fw_env_mac "$envname" "$sofs_mac"
102 return $?
103 elif [ -n "$hw_mac" ]; then
104 # Read the MAC address stored by U-Boot
Patrick Williams2ceeb112023-04-14 08:45:22 -0500105 local fw_env_mac
106 fw_env_mac=$(read_fw_env_mac "$envname")
Lotus Xu58c48862020-10-27 15:00:31 +0800107 if [ -z "$fw_env_mac" ] || [ "$fw_env_mac" != "$hw_mac" ]; then
108 set_fw_env_mac "$envname" "$hw_mac"
109 return $?
110 fi
111 else
112 # Could not identify a MAC address
113 return 255
114 fi
115
116 return 0
117}
118
119create_macdir
120
121error=0
122first_error_seen=0
123
Patrick Williams2ceeb112023-04-14 08:45:22 -0500124while read -r IFACE UBDEV; do
Lotus Xu58c48862020-10-27 15:00:31 +0800125 mac_check "$IFACE" "$UBDEV"
126 error=$?
127 if [ $error -ne 0 ] && [ $first_error_seen -eq 0 ]; then
128 first_error_seen=$error
129 fi
130done <<-END_CONF
131 eth0 eth1addr
132 eth1 ethaddr
133END_CONF
134exit $first_error_seen