Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | # |
| 3 | # Copyright 2016 IBM Corporation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 18 | import sys |
Brad Bishop | 3205742 | 2016-10-05 22:48:39 -0400 | [diff] [blame] | 19 | import os |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 20 | import dbus |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 21 | import uuid |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 22 | import argparse |
| 23 | import subprocess |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 24 | import obmc.mapper |
| 25 | import shutil |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 26 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 27 | INV_INTF_NAME = "xyz.openbmc_project.Inventory.Item.NetworkInterface" |
| 28 | NET_DBUS_NAME = "org.openbmc.NetworkManager" |
| 29 | NET_OBJ_NAME = "/org/openbmc/NetworkManager/Interface" |
| 30 | CHS_DBUS_NAME = "org.openbmc.control.Chassis" |
| 31 | CHS_INTF_NAME = "xyz.openbmc_project.Common.UUID" |
| 32 | CHS_OBJ_NAME = "/org/openbmc/control/chassis0" |
| 33 | PROP_INTF_NAME = "org.freedesktop.DBus.Properties" |
| 34 | INVENTORY_ROOT = "/xyz/openbmc_project/inventory" |
| 35 | NETWORK_ROOT = "/xyz/openbmc_project/network" |
| 36 | ETHERNET_INTF_NAME = "xyz.openbmc_project.Network.EthernetInterface" |
| 37 | MAC_INTF_NAME = "xyz.openbmc_project.Network.MACAddress" |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 38 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 39 | FRUS = {} |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 40 | |
| 41 | # IEEE 802 MAC address mask for locally administered. |
| 42 | # This means the admin has set the MAC and is no longer |
| 43 | # the unique number set by the device manufacturer. |
| 44 | MAC_LOCALLY_ADMIN_MASK = 0x20000000000 |
| 45 | |
| 46 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 47 | # Get inventory MACAddress value. |
| 48 | def get_bmc_mac_address(bus, prop): |
| 49 | mapper = obmc.mapper.Mapper(bus) |
| 50 | |
| 51 | # Get the inventory subtree, limited |
| 52 | # to objects that implement NetworkInterface. |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 53 | for path, info in mapper.get_subtree( |
| 54 | path=INVENTORY_ROOT, interfaces=[INV_INTF_NAME] |
| 55 | ).iteritems(): |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 56 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 57 | # Find a NetworkInterface with 'bmc' in the path. |
| 58 | if "bmc" not in path: |
| 59 | continue |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 60 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 61 | # Only expecting a single service to implement |
| 62 | # NetworkInterface. Get the service connection |
| 63 | # from the mapper response |
| 64 | conn = info.keys()[0] |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 65 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 66 | # Get the inventory object implementing NetworkInterface. |
| 67 | obj = bus.get_object(conn, path) |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 68 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 69 | # Get the MAC address |
| 70 | mproxy = obj.get_dbus_method("Get", PROP_INTF_NAME) |
| 71 | return mproxy(INV_INTF_NAME, prop) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 72 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 73 | |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 74 | # Get Network Interface object. |
| 75 | def get_network_interface_object(bus): |
| 76 | mapper = obmc.mapper.Mapper(bus) |
| 77 | |
| 78 | # Get the network subtree, limited |
| 79 | # to objects that implements EthernetInterface. |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 80 | for path, info in mapper.get_subtree( |
| 81 | path=NETWORK_ROOT, interfaces=[ETHERNET_INTF_NAME] |
| 82 | ).iteritems(): |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 83 | |
| 84 | # Find the one which is having physical interface,it may happen |
| 85 | # that vlan interface is there and we want the physical |
| 86 | # interface here. |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 87 | if path.split("/")[-1].find("_") < 0: |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 88 | service = info.keys()[0] |
| 89 | net_obj = bus.get_object(service, path) |
| 90 | return net_obj |
| 91 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 92 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 93 | # Get inventory UUID value. |
| 94 | def get_uuid(bus, prop): |
| 95 | mapper = obmc.mapper.Mapper(bus) |
| 96 | |
| 97 | # Get the inventory subtree, limited |
| 98 | # to objects that implement UUID. |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 99 | resp = mapper.get_subtree(path=INVENTORY_ROOT, interfaces=[CHS_INTF_NAME]) |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 100 | |
| 101 | # Only expecting a single object to implement UUID. |
| 102 | try: |
| 103 | path, info = resp.items()[0] |
| 104 | except IndexError as e: |
| 105 | return None |
| 106 | |
| 107 | # Only expecting a single service to implement |
| 108 | # UUID. Get the service connection |
| 109 | # from the mapper response |
| 110 | conn = info.keys()[0] |
| 111 | |
| 112 | # Get the inventory object implementing UUID. |
| 113 | obj = bus.get_object(conn, path) |
| 114 | |
| 115 | # Get the uuid |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 116 | mproxy = obj.get_dbus_method("Get", PROP_INTF_NAME) |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 117 | return mproxy(CHS_INTF_NAME, prop) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 118 | |
| 119 | |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 120 | # Get the value of the mac on the system (from u-boot) without ':' separators |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 121 | def get_sys_mac(obj): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 122 | sys_mac = "" |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 123 | try: |
| 124 | sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"]) |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 125 | except Exception: |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 126 | # Handle when mac does not exist in u-boot |
| 127 | return sys_mac |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 128 | return sys_mac |
| 129 | |
| 130 | |
| 131 | # Replace the value of the system mac with the value of the inventory |
| 132 | # MAC if the system MAC is not locally administered because this means |
| 133 | # the system admin has purposely set the MAC |
| 134 | def sync_mac(obj, inv_mac, sys_mac): |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 135 | if sys_mac: |
| 136 | # Convert sys MAC to int to perform bitwise '&' |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 137 | sys_mac = sys_mac.replace(":", "") |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 138 | int_sys_mac = int(sys_mac, 16) |
| 139 | else: |
| 140 | # Set mac to 0 for when u-boot mac is not present |
| 141 | int_sys_mac = 0 |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 142 | if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK: |
| 143 | # Sys MAC is not locally administered, go replace it with inv value |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 144 | intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 145 | intf.Set(MAC_INTF_NAME, "MACAddress", inv_mac) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 146 | |
| 147 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 148 | # Set sys uuid, this reboots the BMC for the value to take effect |
| 149 | def set_sys_uuid(uuid): |
| 150 | rc = subprocess.call(["fw_setenv", "uuid", uuid]) |
| 151 | if rc == 0: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 152 | print("Rebooting BMC to set uuid") |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 153 | # TODO Uncomment once sync from u-boot to /etc/machine-id is in place |
| 154 | # Issue openbmc/openbmc#479 |
| 155 | # rc = subprocess.call(["reboot"]) |
| 156 | else: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 157 | print("Error setting uuid") |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 158 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 159 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 160 | if __name__ == "__main__": |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 161 | arg = argparse.ArgumentParser() |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 162 | arg.add_argument("-p") |
| 163 | arg.add_argument("-s") |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 164 | |
| 165 | opt = arg.parse_args() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 166 | prop_name = opt.p |
| 167 | sync_type = opt.s |
| 168 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 169 | bus = dbus.SystemBus() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 170 | if sync_type == "mac": |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 171 | inv_mac = get_bmc_mac_address(bus, prop_name) |
| 172 | if not inv_mac: |
| 173 | sys.exit(1) |
| 174 | net_obj = get_network_interface_object(bus) |
| 175 | if not net_obj: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 176 | print("Unable to get the network object") |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 177 | sys.exit(1) |
| 178 | sys_mac = get_sys_mac(net_obj) |
| 179 | if inv_mac != sys_mac: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 180 | print("Inventory MAC=%s,System MAC=%s" % (inv_mac, sys_mac)) |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 181 | sync_mac(net_obj, inv_mac, sys_mac) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 182 | elif sync_type == "uuid": |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 183 | inv_uuid = get_uuid(bus, prop_name) |
| 184 | if inv_uuid: |
| 185 | inv_uuid = uuid.UUID(inv_uuid) |
| 186 | chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME) |
| 187 | chs_uuid = get_sys_uuid(chs_obj) |
| 188 | if inv_uuid != sys_uuid: |
| 189 | set_sys_uuid(inv_uuid) |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 190 | |
| 191 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |