blob: d10eb980d4f2a62aa80c9810a19ae4e10deb5c94 [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
16source /usr/share/ipmi-fru/lib.sh || exit
17
William A. Kennington III8924a622021-10-12 23:23:33 -070018ipmi_fru_alloc '@EEPROM@' eeprom || exit
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080019
20header=()
21read_header "$eeprom" header || exit
22internal_offset=${header[$IPMI_FRU_COMMON_HEADER_INTERNAL_OFFSET_IDX]}
23if (( internal_offset == 0 )); then
24 echo "Internal offset invalid for eeprom" >&2
25 exit 1
26fi
27
28# Our MAC Address configuration lives in the internal area with a format
29# Offset Data
30# 0 Version (Always 1)
31# 1 Type (Always 1 for MAC Address)
32# 2 Area Length in bytes (Always 32 bytes or 4 IPMI FRU sectors)
33# 3-8 MAC Address Base Octets
34# 9 Num Allocate MACs from Base
35# 10-30 Padding (Always 0xFF)
36# 31 IPMI FRU Checksum
37internal=()
38read_area "$eeprom" "$internal_offset" internal 4 || exit
39if (( internal[1] != 1 || internal[2] != 32 )); then
40 echo "Not a MAC internal region" >&2
41 exit 1
42fi
43mac=("${internal[@]:3:6}")
44num="${internal[@]:9:1}"
45macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}")
46echo "Base MAC $macstr num $num" >&2
47
48rc=0
49
50# Pre-Determine if we will miss an allocation due to the number of
51# addresses the FRU actually supports.
William A. Kennington III91da5202022-03-01 01:08:28 -080052declare -A num_to_intfs=(@NUM_TO_INTFS@)
53for key in "${!num_to_intfs[@]}"; do
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080054 if (( key >= num )); then
William A. Kennington III91da5202022-03-01 01:08:28 -080055 echo "${num_to_intfs[$key]} at $key is out of range" >&2
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080056 rc=1
57 fi
58done
59
60# Write out each MAC override to the runtime networkd configuration
61for (( i=0; i<num; i++ )); do
William A. Kennington III91da5202022-03-01 01:08:28 -080062 for intf in ${num_to_intfs[$i]}; do
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080063 macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}")
64 echo "Setting $intf to $macstr" >&2
65 for override in /run/systemd/network/{00,}-bmc-$intf.network.d; do
66 mkdir -p "$override"
67 printf '[Link]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
68 done
69 for override in /run/systemd/network/{00,}-bmc-$intf.netdev.d; do
70 mkdir -p "$override"
71 printf '[NetDev]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
72 done
William A. Kennington III91da5202022-03-01 01:08:28 -080073 done
William A. Kennington III3b3c40f2021-03-04 22:56:26 -080074 if (( ++mac[5] > 0xff )); then
75 echo "MAC assignment too large: ${mac[@]}" >&2
76 rc=2
77 break
78 fi
79done
80
81exit $rc