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 | |
William A. Kennington III | ced2bfd | 2023-06-05 14:38:02 -0700 | [diff] [blame] | 16 | # shellcheck source=meta-google/recipes-google/ipmi/ipmi-fru-sh/lib.sh |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 17 | source /usr/share/ipmi-fru/lib.sh || exit |
| 18 | |
William A. Kennington III | 8924a62 | 2021-10-12 23:23:33 -0700 | [diff] [blame] | 19 | ipmi_fru_alloc '@EEPROM@' eeprom || exit |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 20 | |
| 21 | header=() |
| 22 | read_header "$eeprom" header || exit |
| 23 | internal_offset=${header[$IPMI_FRU_COMMON_HEADER_INTERNAL_OFFSET_IDX]} |
| 24 | if (( internal_offset == 0 )); then |
| 25 | echo "Internal offset invalid for eeprom" >&2 |
| 26 | exit 1 |
| 27 | fi |
| 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 |
| 38 | internal=() |
| 39 | read_area "$eeprom" "$internal_offset" internal 4 || exit |
| 40 | if (( internal[1] != 1 || internal[2] != 32 )); then |
| 41 | echo "Not a MAC internal region" >&2 |
| 42 | exit 1 |
| 43 | fi |
| 44 | mac=("${internal[@]:3:6}") |
William A. Kennington III | ced2bfd | 2023-06-05 14:38:02 -0700 | [diff] [blame] | 45 | num="${internal[9]}" |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 46 | macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}") |
| 47 | echo "Base MAC $macstr num $num" >&2 |
| 48 | |
| 49 | rc=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 III | ced2bfd | 2023-06-05 14:38:02 -0700 | [diff] [blame] | 53 | # shellcheck disable=SC2190 |
William A. Kennington III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 54 | declare -A num_to_intfs=(@NUM_TO_INTFS@) |
| 55 | for key in "${!num_to_intfs[@]}"; do |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 56 | if (( key >= num )); then |
William A. Kennington III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 57 | 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] | 58 | rc=1 |
| 59 | fi |
| 60 | done |
| 61 | |
| 62 | # Write out each MAC override to the runtime networkd configuration |
| 63 | for (( i=0; i<num; i++ )); do |
Anthony | b06364d | 2023-05-30 13:30:41 +0800 | [diff] [blame] | 64 | if (( mac[5] > 0xff )); then |
William A. Kennington III | ced2bfd | 2023-06-05 14:38:02 -0700 | [diff] [blame] | 65 | echo "MAC assignment too large: ${mac[*]}" >&2 |
Anthony | b06364d | 2023-05-30 13:30:41 +0800 | [diff] [blame] | 66 | rc=2 |
| 67 | break |
| 68 | fi |
William A. Kennington III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 69 | for intf in ${num_to_intfs[$i]}; do |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 70 | macstr=$(printf '%02x:%02x:%02x:%02x:%02x:%02x' "${mac[@]}") |
| 71 | echo "Setting $intf to $macstr" >&2 |
| 72 | for override in /run/systemd/network/{00,}-bmc-$intf.network.d; do |
| 73 | mkdir -p "$override" |
| 74 | printf '[Link]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf |
| 75 | done |
| 76 | for override in /run/systemd/network/{00,}-bmc-$intf.netdev.d; do |
| 77 | mkdir -p "$override" |
| 78 | printf '[NetDev]\nMACAddress=%s\n' "$macstr" >"$override"/50-mac.conf |
| 79 | done |
Kyle Nieman | cabc5aa | 2023-05-30 10:56:46 -0500 | [diff] [blame] | 80 | # In case we don't have any interface configs, set the MAC directly |
| 81 | # This is safe to apply, as systemd-networkd will always override this |
| 82 | # value based on written configs. |
William A. Kennington III | ced2bfd | 2023-06-05 14:38:02 -0700 | [diff] [blame] | 83 | if ip link show "$intf" >/dev/null 2>&1 && \ |
| 84 | ! ip link set dev "$intf" address "$macstr"; then |
Kyle Nieman | cabc5aa | 2023-05-30 10:56:46 -0500 | [diff] [blame] | 85 | echo "Setting MAC($macstr) on $intf failed" >&2 |
| 86 | fi |
William A. Kennington III | 91da520 | 2022-03-01 01:08:28 -0800 | [diff] [blame] | 87 | done |
Anthony | b06364d | 2023-05-30 13:30:41 +0800 | [diff] [blame] | 88 | (( ++mac[5] )) |
William A. Kennington III | 3b3c40f | 2021-03-04 22:56:26 -0800 | [diff] [blame] | 89 | done |
| 90 | |
| 91 | exit $rc |