blob: aaa9920c7ce71afaf5f13e8fc403180645585e96 [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
William A. Kennington III02e48702023-02-28 18:52:44 -080063lower=$(((mac[3] << 16) | (mac[4] << 8) | mac[5]))
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080064for (( i=0; i<num; i++ )); do
William A. Kennington III02e48702023-02-28 18:52:44 -080065 if (( lower > 0xffffff )); then
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070066 echo "MAC assignment too large: ${mac[*]}" >&2
Anthonyb06364d2023-05-30 13:30:41 +080067 rc=2
68 break
69 fi
William A. Kennington III91da5202022-03-01 01:08:28 -080070 for intf in ${num_to_intfs[$i]}; do
William A. Kennington III02e48702023-02-28 18:52:44 -080071 mac[3]=$(((lower >> 16) & 0xff))
72 mac[4]=$(((lower >> 8) & 0xff))
73 mac[5]=$(((lower >> 0) & 0xff))
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080074 macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}")
75 echo "Setting $intf to $macstr" >&2
76 for override in /run/systemd/network/{00,}-bmc-$intf.network.d; do
77 mkdir -p "$override"
78 printf '[Link]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
79 done
80 for override in /run/systemd/network/{00,}-bmc-$intf.netdev.d; do
81 mkdir -p "$override"
82 printf '[NetDev]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
83 done
Kyle Niemancabc5aa2023-05-30 10:56:46 -050084 # In case we don't have any interface configs, set the MAC directly
85 # This is safe to apply, as systemd-networkd will always override this
86 # value based on written configs.
William A. Kennington IIIced2bfd2023-06-05 14:38:02 -070087 if ip link show "$intf" >/dev/null 2>&1 && \
88 ! ip link set dev "$intf" address "$macstr"; then
Kyle Niemancabc5aa2023-05-30 10:56:46 -050089 echo "Setting MAC($macstr) on $intf failed" >&2
90 fi
William A. Kennington III91da5202022-03-01 01:08:28 -080091 done
William A. Kennington III02e48702023-02-28 18:52:44 -080092 (( ++lower ))
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080093done
94
95exit $rc