blob: 7a5635affca87970fee7e810383658bcebf8de76 [file] [log] [blame]
Adriana Kobylakfb4d2252016-06-29 21:44:30 -05001#!/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 Kobylakfb4d2252016-06-29 21:44:30 -050018import sys
Brad Bishop32057422016-10-05 22:48:39 -040019import os
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050020import dbus
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050021import uuid
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050022import argparse
23import subprocess
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050024import obmc.mapper
25import shutil
Brad Bishop3d13c282016-09-21 08:07:47 -040026
Patrick Williams75fe8cc2022-07-22 16:12:12 -050027INV_INTF_NAME = "xyz.openbmc_project.Inventory.Item.NetworkInterface"
28NET_DBUS_NAME = "org.openbmc.NetworkManager"
29NET_OBJ_NAME = "/org/openbmc/NetworkManager/Interface"
30CHS_DBUS_NAME = "org.openbmc.control.Chassis"
31CHS_INTF_NAME = "xyz.openbmc_project.Common.UUID"
32CHS_OBJ_NAME = "/org/openbmc/control/chassis0"
33PROP_INTF_NAME = "org.freedesktop.DBus.Properties"
34INVENTORY_ROOT = "/xyz/openbmc_project/inventory"
35NETWORK_ROOT = "/xyz/openbmc_project/network"
36ETHERNET_INTF_NAME = "xyz.openbmc_project.Network.EthernetInterface"
37MAC_INTF_NAME = "xyz.openbmc_project.Network.MACAddress"
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050038
Brad Bishop3d13c282016-09-21 08:07:47 -040039FRUS = {}
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050040
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.
44MAC_LOCALLY_ADMIN_MASK = 0x20000000000
45
46
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050047# Get inventory MACAddress value.
48def 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 Williams75fe8cc2022-07-22 16:12:12 -050053 for path, info in mapper.get_subtree(
54 path=INVENTORY_ROOT, interfaces=[INV_INTF_NAME]
55 ).iteritems():
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050056
Patrick Williams75fe8cc2022-07-22 16:12:12 -050057 # Find a NetworkInterface with 'bmc' in the path.
58 if "bmc" not in path:
59 continue
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050060
Patrick Williams75fe8cc2022-07-22 16:12:12 -050061 # Only expecting a single service to implement
62 # NetworkInterface. Get the service connection
63 # from the mapper response
64 conn = info.keys()[0]
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050065
Patrick Williams75fe8cc2022-07-22 16:12:12 -050066 # Get the inventory object implementing NetworkInterface.
67 obj = bus.get_object(conn, path)
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050068
Patrick Williams75fe8cc2022-07-22 16:12:12 -050069 # Get the MAC address
70 mproxy = obj.get_dbus_method("Get", PROP_INTF_NAME)
71 return mproxy(INV_INTF_NAME, prop)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050072
Adriana Kobylak24341f92018-01-26 15:07:23 -060073
Ratan Guptaf1470e92017-08-28 17:34:53 +053074# Get Network Interface object.
75def 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 Williams75fe8cc2022-07-22 16:12:12 -050080 for path, info in mapper.get_subtree(
81 path=NETWORK_ROOT, interfaces=[ETHERNET_INTF_NAME]
82 ).iteritems():
Ratan Guptaf1470e92017-08-28 17:34:53 +053083
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 Williams75fe8cc2022-07-22 16:12:12 -050087 if path.split("/")[-1].find("_") < 0:
Ratan Guptaf1470e92017-08-28 17:34:53 +053088 service = info.keys()[0]
89 net_obj = bus.get_object(service, path)
90 return net_obj
91
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050092
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050093# Get inventory UUID value.
94def get_uuid(bus, prop):
95 mapper = obmc.mapper.Mapper(bus)
96
97 # Get the inventory subtree, limited
98 # to objects that implement UUID.
Patrick Williams75fe8cc2022-07-22 16:12:12 -050099 resp = mapper.get_subtree(path=INVENTORY_ROOT, interfaces=[CHS_INTF_NAME])
Dinesh Chinaric47c6a62017-03-29 23:09:59 -0500100
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 Williams75fe8cc2022-07-22 16:12:12 -0500116 mproxy = obj.get_dbus_method("Get", PROP_INTF_NAME)
Dinesh Chinaric47c6a62017-03-29 23:09:59 -0500117 return mproxy(CHS_INTF_NAME, prop)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500118
119
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500120# Get the value of the mac on the system (from u-boot) without ':' separators
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500121def get_sys_mac(obj):
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500122 sys_mac = ""
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500123 try:
124 sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"])
Adriana Kobylak24341f92018-01-26 15:07:23 -0600125 except Exception:
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500126 # Handle when mac does not exist in u-boot
127 return sys_mac
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500128 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
134def sync_mac(obj, inv_mac, sys_mac):
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500135 if sys_mac:
136 # Convert sys MAC to int to perform bitwise '&'
Ratan Guptaf1470e92017-08-28 17:34:53 +0530137 sys_mac = sys_mac.replace(":", "")
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500138 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 Kobylakfb4d2252016-06-29 21:44:30 -0500142 if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK:
143 # Sys MAC is not locally administered, go replace it with inv value
Ratan Guptaf1470e92017-08-28 17:34:53 +0530144 intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
145 intf.Set(MAC_INTF_NAME, "MACAddress", inv_mac)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500146
147
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500148# Set sys uuid, this reboots the BMC for the value to take effect
149def set_sys_uuid(uuid):
150 rc = subprocess.call(["fw_setenv", "uuid", uuid])
151 if rc == 0:
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500152 print("Rebooting BMC to set uuid")
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500153 # 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 Williams75fe8cc2022-07-22 16:12:12 -0500157 print("Error setting uuid")
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500158
Adriana Kobylak24341f92018-01-26 15:07:23 -0600159
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500160if __name__ == "__main__":
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500161 arg = argparse.ArgumentParser()
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500162 arg.add_argument("-p")
163 arg.add_argument("-s")
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500164
165 opt = arg.parse_args()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500166 prop_name = opt.p
167 sync_type = opt.s
168
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500169 bus = dbus.SystemBus()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500170 if sync_type == "mac":
Ratan Guptaf1470e92017-08-28 17:34:53 +0530171 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 Williams75fe8cc2022-07-22 16:12:12 -0500176 print("Unable to get the network object")
Ratan Guptaf1470e92017-08-28 17:34:53 +0530177 sys.exit(1)
178 sys_mac = get_sys_mac(net_obj)
179 if inv_mac != sys_mac:
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500180 print("Inventory MAC=%s,System MAC=%s" % (inv_mac, sys_mac))
Ratan Guptaf1470e92017-08-28 17:34:53 +0530181 sync_mac(net_obj, inv_mac, sys_mac)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500182 elif sync_type == "uuid":
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500183 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 Bishop53066752016-09-21 08:48:04 -0400190
191# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4