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 | |
| 18 | eeprom="$(of_name_to_eeprom '@EEPROM@')" || exit |
| 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. |
| 52 | declare -A num_to_if=(@NUM_TO_IF@) |
| 53 | for 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 |
| 58 | done |
| 59 | |
| 60 | # Write out each MAC override to the runtime networkd configuration |
| 61 | for (( 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 |
| 80 | done |
| 81 | |
| 82 | exit $rc |