blob: b00ef5ce132eadfb4dfc2bb9337bd821e54d7a94 [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
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050027INV_INTF_NAME = 'xyz.openbmc_project.Inventory.Item.NetworkInterface'
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050028NET_DBUS_NAME = 'org.openbmc.NetworkManager'
29NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface'
30CHS_DBUS_NAME = 'org.openbmc.control.Chassis'
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050031CHS_INTF_NAME = 'xyz.openbmc_project.Common.UUID'
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050032CHS_OBJ_NAME = '/org/openbmc/control/chassis0'
33PROP_INTF_NAME = 'org.freedesktop.DBus.Properties'
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050034INVENTORY_ROOT = '/xyz/openbmc_project/inventory'
Ratan Guptaf1470e92017-08-28 17:34:53 +053035NETWORK_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.
53 for path, info in \
54 mapper.get_subtree(
55 path=INVENTORY_ROOT,
56 interfaces=[INV_INTF_NAME]).iteritems():
57
58 # Find a NetworkInterface with 'bmc' in the path.
59 if 'bmc' not in path:
60 continue
61
62 # Only expecting a single service to implement
63 # NetworkInterface. Get the service connection
64 # from the mapper response
65 conn = info.keys()[0]
66
67 # Get the inventory object implementing NetworkInterface.
68 obj = bus.get_object(conn, path)
69
70 # Get the MAC address
71 mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME)
72 return mproxy(INV_INTF_NAME, prop)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050073
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.
80 for path, info in \
81 mapper.get_subtree(
82 path=NETWORK_ROOT,
83 interfaces=[ETHERNET_INTF_NAME]).iteritems():
84
85 # Find the one which is having physical interface,it may happen
86 # that vlan interface is there and we want the physical
87 # interface here.
88 if path.split('/')[-1].find('_') < 0:
89 service = info.keys()[0]
90 net_obj = bus.get_object(service, path)
91 return net_obj
92
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050093
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050094# Get inventory UUID value.
95def get_uuid(bus, prop):
96 mapper = obmc.mapper.Mapper(bus)
97
98 # Get the inventory subtree, limited
99 # to objects that implement UUID.
100 resp = mapper.get_subtree(
101 path=INVENTORY_ROOT,
102 interfaces=[CHS_INTF_NAME])
103
104 # Only expecting a single object to implement UUID.
105 try:
106 path, info = resp.items()[0]
107 except IndexError as e:
108 return None
109
110 # Only expecting a single service to implement
111 # UUID. Get the service connection
112 # from the mapper response
113 conn = info.keys()[0]
114
115 # Get the inventory object implementing UUID.
116 obj = bus.get_object(conn, path)
117
118 # Get the uuid
119 mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME)
120 return mproxy(CHS_INTF_NAME, prop)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500121
122
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500123# Get the value of the mac on the system (from u-boot) without ':' separators
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500124def get_sys_mac(obj):
125 sys_mac = ''
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500126 try:
127 sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"])
128 except:
129 # Handle when mac does not exist in u-boot
130 return sys_mac
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500131 return sys_mac
132
133
134# Replace the value of the system mac with the value of the inventory
135# MAC if the system MAC is not locally administered because this means
136# the system admin has purposely set the MAC
137def sync_mac(obj, inv_mac, sys_mac):
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500138 if sys_mac:
139 # Convert sys MAC to int to perform bitwise '&'
Ratan Guptaf1470e92017-08-28 17:34:53 +0530140 sys_mac = sys_mac.replace(":", "")
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500141 int_sys_mac = int(sys_mac, 16)
142 else:
143 # Set mac to 0 for when u-boot mac is not present
144 int_sys_mac = 0
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500145 if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK:
146 # Sys MAC is not locally administered, go replace it with inv value
Ratan Guptaf1470e92017-08-28 17:34:53 +0530147 intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
148 intf.Set(MAC_INTF_NAME, "MACAddress", inv_mac)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500149
150
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500151# Set sys uuid, this reboots the BMC for the value to take effect
152def set_sys_uuid(uuid):
153 rc = subprocess.call(["fw_setenv", "uuid", uuid])
154 if rc == 0:
155 print "Rebooting BMC to set uuid"
156 # TODO Uncomment once sync from u-boot to /etc/machine-id is in place
157 # Issue openbmc/openbmc#479
158 # rc = subprocess.call(["reboot"])
159 else:
160 print "Error setting uuid"
161
162if __name__ == '__main__':
163 arg = argparse.ArgumentParser()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500164 arg.add_argument('-p')
165 arg.add_argument('-s')
166
167 opt = arg.parse_args()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500168 prop_name = opt.p
169 sync_type = opt.s
170
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500171 bus = dbus.SystemBus()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500172 if sync_type == "mac":
Ratan Guptaf1470e92017-08-28 17:34:53 +0530173 inv_mac = get_bmc_mac_address(bus, prop_name)
174 if not inv_mac:
175 sys.exit(1)
176 net_obj = get_network_interface_object(bus)
177 if not net_obj:
178 print "Unable to get the network object"
179 sys.exit(1)
180 sys_mac = get_sys_mac(net_obj)
181 if inv_mac != sys_mac:
182 print "Inventory MAC=%s,System MAC=%s" % (inv_mac, sys_mac)
183 sync_mac(net_obj, inv_mac, sys_mac)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500184 elif sync_type == "uuid":
Dinesh Chinaric47c6a62017-03-29 23:09:59 -0500185 inv_uuid = get_uuid(bus, prop_name)
186 if inv_uuid:
187 inv_uuid = uuid.UUID(inv_uuid)
188 chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME)
189 chs_uuid = get_sys_uuid(chs_obj)
190 if inv_uuid != sys_uuid:
191 set_sys_uuid(inv_uuid)
Brad Bishop53066752016-09-21 08:48:04 -0400192
193# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4