blob: dce4611a46165b87d608db67c6b290dd481f61cd [file] [log] [blame]
William A. Kennington III3b3c40f2021-03-04 22:56:26 -08001#!/bin/bash
2# Copyright 2021 Google LLC
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
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070016# shellcheck source=meta-google/recipes-google/ipmi/ipmi-fru-sh/lib.sh
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080017source /usr/share/ipmi-fru/lib.sh || exit
18
William A. Kennington III8924a622021-10-12 23:23:33 -070019ipmi_fru_alloc '@EEPROM@' eeprom || exit
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080020
21header=()
22read_header "$eeprom" header || exit
23internal_offset=${header[$IPMI_FRU_COMMON_HEADER_INTERNAL_OFFSET_IDX]}
24if (( internal_offset == 0 )); then
25 echo "Internal offset invalid for eeprom" >&2
26 exit 1
27fi
28
29# Our MAC Address configuration lives in the internal area with a format
30# Offset Data
31# 0 Version (Always 1)
32# 1 Type (Always 1 for MAC Address)
33# 2 Area Length in bytes (Always 32 bytes or 4 IPMI FRU sectors)
34# 3-8 MAC Address Base Octets
35# 9 Num Allocate MACs from Base
36# 10-30 Padding (Always 0xFF)
37# 31 IPMI FRU Checksum
38internal=()
39read_area "$eeprom" "$internal_offset" internal 4 || exit
40if (( internal[1] != 1 || internal[2] != 32 )); then
41 echo "Not a MAC internal region" >&2
42 exit 1
43fi
44mac=("${internal[@]:3:6}")
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070045num="${internal[9]}"
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080046macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}")
47echo "Base MAC $macstr num $num" >&2
48
49rc=0
50
51# Pre-Determine if we will miss an allocation due to the number of
52# addresses the FRU actually supports.
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070053# shellcheck disable=SC2190
William A. Kennington III91da5202022-03-01 01:08:28 -080054declare -A num_to_intfs=(@NUM_TO_INTFS@)
55for key in "${!num_to_intfs[@]}"; do
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080056 if (( key >= num )); then
William A. Kennington III91da5202022-03-01 01:08:28 -080057 echo "${num_to_intfs[$key]} at $key is out of range" >&2
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080058 rc=1
59 fi
60done
61
62# Write out each MAC override to the runtime networkd configuration
63for (( i=0; i<num; i++ )); do
Anthonyb06364d2023-05-30 13:30:41 +080064 if (( mac[5] > 0xff )); then
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070065 echo "MAC assignment too large: ${mac[*]}" >&2
Anthonyb06364d2023-05-30 13:30:41 +080066 rc=2
67 break
68 fi
William A. Kennington III91da5202022-03-01 01:08:28 -080069 for intf in ${num_to_intfs[$i]}; do
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080070 macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}")
71 echo "Setting $intf to $macstr" >&2
72 for override in /run/systemd/network/{00,}-bmc-$intf.network.d; do
73 mkdir -p "$override"
74 printf '[Link]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
75 done
76 for override in /run/systemd/network/{00,}-bmc-$intf.netdev.d; do
77 mkdir -p "$override"
78 printf '[NetDev]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
79 done
Kyle Niemancabc5aa2023-05-30 10:56:46 -050080 # In case we don't have any interface configs, set the MAC directly
81 # This is safe to apply, as systemd-networkd will always override this
82 # value based on written configs.
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070083 if ip link show "$intf" >/dev/null 2>&1 && \
84 ! ip link set dev "$intf" address "$macstr"; then
Kyle Niemancabc5aa2023-05-30 10:56:46 -050085 echo "Setting MAC($macstr) on $intf failed" >&2
86 fi
William A. Kennington III91da5202022-03-01 01:08:28 -080087 done
Anthonyb06364d2023-05-30 13:30:41 +080088 (( ++mac[5] ))
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080089done
90
91exit $rc