blob: a3430a65e5ac4ed1382966d49d7a0a54e1306496 [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
18eeprom="$(of_name_to_eeprom '@EEPROM@')" || exit
19
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.
52declare -A num_to_if=(@NUM_TO_IF@)
53for key in "${!num_to_if[@]}"; do
54 if (( key >= num )); then
55 echo "${num_to_if[$key]} at $key is out of range" >&2
56 rc=1
57 fi
58done
59
60# Write out each MAC override to the runtime networkd configuration
61for (( i=0; i<num; i++ )); do
62 intf="${num_to_if[$i]}"
63 if [ -n "$intf" ]; then
64 macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}")
65 echo "Setting $intf to $macstr" >&2
66 for override in /run/systemd/network/{00,}-bmc-$intf.network.d; do
67 mkdir -p "$override"
68 printf '[Link]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
69 done
70 for override in /run/systemd/network/{00,}-bmc-$intf.netdev.d; do
71 mkdir -p "$override"
72 printf '[NetDev]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf
73 done
74 fi
75 if (( ++mac[5] > 0xff )); then
76 echo "MAC assignment too large: ${mac[@]}" >&2
77 rc=2
78 break
79 fi
80done
81
82exit $rc