William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 1 | #!/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 | |
| 16 | source /usr/share/ipmi-fru/lib.sh || exit |
| 17 | |
William A. Kennington III | 8924a62 | 2021-10-12 23:23:33 -0700 | [diff] [blame] | 18 | ipmi_fru_alloc '@EEPROM@' eeprom || exit |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 19 | |
| 20 | header=() |
| 21 | read_header "$eeprom" header || exit |
| 22 | internal_offset=${header[$IPMI_FRU_COMMON_HEADER_INTERNAL_OFFSET_IDX]} |
| 23 | if (( internal_offset == 0 )); then |
| 24 | echo "Internal offset invalid for eeprom" >&2 |
| 25 | exit 1 |
| 26 | fi |
| 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 |
| 37 | internal=() |
| 38 | read_area "$eeprom" "$internal_offset" internal 4 || exit |
| 39 | if (( internal[1] != 1 || internal[2] != 32 )); then |
| 40 | echo "Not a MAC internal region" >&2 |
| 41 | exit 1 |
| 42 | fi |
| 43 | mac=("${internal[@]:3:6}") |
| 44 | num="${internal[@]:9:1}" |
| 45 | macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}") |
| 46 | echo "Base MAC $macstr num $num" >&2 |
| 47 | |
| 48 | rc=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 III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 52 | declare -A num_to_intfs=(@NUM_TO_INTFS@) |
| 53 | for key in "${!num_to_intfs[@]}"; do |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 54 | if (( key >= num )); then |
William A. Kennington III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 55 | echo "${num_to_intfs[$key]} at $key is out of range" >&2 |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 56 | rc=1 |
| 57 | fi |
| 58 | done |
| 59 | |
| 60 | # Write out each MAC override to the runtime networkd configuration |
| 61 | for (( i=0; i<num; i++ )); do |
William A. Kennington III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 62 | for intf in ${num_to_intfs[$i]}; do |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 63 | 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 III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 73 | done |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 74 | if (( ++mac[5] > 0xff )); then |
| 75 | echo "MAC assignment too large: ${mac[@]}" >&2 |
| 76 | rc=2 |
| 77 | break |
| 78 | fi |
| 79 | done |
| 80 | |
| 81 | exit $rc |